diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f4d3e2996..c0cdc4dca 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -53,6 +53,11 @@ variables: .apply_bot_filter: &apply_bot_filter python $APPLY_BOT_FILTER_SCRIPT || exit 0 +.setup_tools_unless_target_test: &setup_tools_unless_target_test | + if [[ "$SETUP_TOOLS" == "1" || "$CI_JOB_STAGE" != "target_test" ]]; then + tools/idf_tools.py --non-interactive install && eval "$(tools/idf_tools.py --non-interactive export)" || exit 1 + fi + before_script: - source tools/ci/setup_python.sh - *git_clean_stale_submodules @@ -65,6 +70,8 @@ before_script: - base64 --decode --ignore-garbage ~/.ssh/id_rsa_base64 > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - echo -e "Host gitlab.espressif.cn\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config + # Download and install tools, if needed + - *setup_tools_unless_target_test # Set IS_PRIVATE or IS_PUBLIC depending on if our branch is public or not # @@ -122,11 +129,11 @@ build_template_app: # Set the variable for 'esp-idf-template' testing - ESP_IDF_TEMPLATE_GIT=${ESP_IDF_TEMPLATE_GIT:-"https://github.com/espressif/esp-idf-template.git"} - git clone ${ESP_IDF_TEMPLATE_GIT} - - cd esp-idf-template # Try to use the same branch name for esp-idf-template that we're # using on esp-idf. If it doesn't exist then just stick to the default # branch - - python $CHECKOUT_REF_SCRIPT esp-idf-template + - python $CHECKOUT_REF_SCRIPT esp-idf-template esp-idf-template + - cd esp-idf-template - make defconfig # Test debug build (default) - make all V=1 @@ -149,8 +156,9 @@ build_template_app: BATCH_BUILD: "1" V: "0" -.build_ssc_template: &build_ssc_template +build_ssc: <<: *build_template + parallel: 3 artifacts: paths: - SSC/ssc_bin @@ -165,22 +173,9 @@ build_template_app: - $BOT_LABEL_REGULAR_TEST script: - git clone $SSC_REPOSITORY + - python $CHECKOUT_REF_SCRIPT SSC SSC - cd SSC - - python $CHECKOUT_REF_SCRIPT SSC - - MAKEFLAGS= ./ci_build_ssc.sh "${CI_JOB_NAME}" "${IDF_PATH}/.gitlab-ci.yml" - -# don't forget to add to dependency to test_template when adding new build_ssc jobs -build_ssc_00: - <<: *build_ssc_template - -build_ssc_01: - <<: *build_ssc_template - -build_ssc_02: - <<: *build_ssc_template - -# If you want to add new build ssc jobs, please add it into dependencies of `assign_test` and `.test_template` - + - MAKEFLAGS= ./ci_build_ssc.sh .build_esp_idf_unit_test_template: &build_esp_idf_unit_test_template <<: *build_template @@ -188,7 +183,7 @@ build_ssc_02: paths: - tools/unit-test-app/output - components/idf_test/unit_test/TestCaseAll.yml - expire_in: 2 days + expire_in: 4 days only: variables: - $BOT_TRIGGER_WITH_LABEL == null @@ -226,8 +221,9 @@ build_esp_idf_tests_cmake: - rm -rf builds output sdkconfig - rm $CI_PROJECT_DIR/components/idf_test/unit_test/TestCaseAll.yml -.build_examples_make_template: &build_examples_make_template +build_examples_make: <<: *build_template + parallel: 8 # This is a workaround for a rarely encountered issue with building examples in CI. # Probably related to building of Kconfig in 'make clean' stage retry: 1 @@ -241,7 +237,7 @@ build_esp_idf_tests_cmake: - build_examples/*/*/*/build/download.config - build_examples/*/*/*/build/bootloader/*.bin - $LOG_PATH - expire_in: 2 days + expire_in: 4 days variables: LOG_PATH: "$CI_PROJECT_DIR/log_examples_make" only: @@ -250,6 +246,7 @@ build_esp_idf_tests_cmake: - $BOT_LABEL_BUILD - $BOT_LABEL_EXAMPLE_TEST - $BOT_LABEL_REGULAR_TEST + - $BOT_LABEL_WEEKEND_TEST script: # it's not possible to build 100% out-of-tree and have the "artifacts" # mechanism work, but this is the next best thing @@ -258,11 +255,12 @@ build_esp_idf_tests_cmake: - cd build_examples # build some of examples - mkdir -p ${LOG_PATH} - - ${IDF_PATH}/tools/ci/build_examples.sh "${CI_JOB_NAME}" + - ${IDF_PATH}/tools/ci/build_examples.sh # same as above, but for CMake -.build_examples_cmake_template: &build_examples_cmake_template +build_examples_cmake: <<: *build_template + parallel: 5 artifacts: when: always paths: @@ -273,7 +271,7 @@ build_esp_idf_tests_cmake: - build_examples_cmake/*/*/*/build/flasher_args.json - build_examples_cmake/*/*/*/build/bootloader/*.bin - $LOG_PATH - expire_in: 2 days + expire_in: 4 days variables: LOG_PATH: "$CI_PROJECT_DIR/log_examples_cmake" only: @@ -282,6 +280,7 @@ build_esp_idf_tests_cmake: - $BOT_LABEL_BUILD - $BOT_LABEL_EXAMPLE_TEST - $BOT_LABEL_REGULAR_TEST + - $BOT_LABEL_WEEKEND_TEST script: # it's not possible to build 100% out-of-tree and have the "artifacts" # mechanism work, but this is the next best thing @@ -290,57 +289,7 @@ build_esp_idf_tests_cmake: - cd build_examples_cmake # build some of examples - mkdir -p ${LOG_PATH} - - ${IDF_PATH}/tools/ci/build_examples_cmake.sh "${CI_JOB_NAME}" - -build_examples_make_00: - <<: *build_examples_make_template - -build_examples_make_01: - <<: *build_examples_make_template - -build_examples_make_02: - <<: *build_examples_make_template - -build_examples_make_03: - <<: *build_examples_make_template - -build_examples_make_04: - <<: *build_examples_make_template - -build_examples_make_05: - <<: *build_examples_make_template - -build_examples_make_06: - <<: *build_examples_make_template - -build_examples_make_07: - <<: *build_examples_make_template - -build_examples_cmake_00: - <<: *build_examples_cmake_template - -build_examples_cmake_01: - <<: *build_examples_cmake_template - -build_examples_cmake_02: - <<: *build_examples_cmake_template - -build_examples_cmake_03: - <<: *build_examples_cmake_template - -build_examples_cmake_04: - <<: *build_examples_cmake_template - -build_examples_cmake_05: - <<: *build_examples_cmake_template - -build_examples_cmake_06: - <<: *build_examples_cmake_template - -build_examples_cmake_07: - <<: *build_examples_cmake_template - -# If you want to add new build example jobs, please add it into dependencies of `.example_test_template` + - ${IDF_PATH}/tools/ci/build_examples_cmake.sh build_docs: stage: build @@ -361,7 +310,7 @@ build_docs: - docs/zh_CN/sphinx-warning-log.txt - docs/zh_CN/sphinx-warning-log-sanitized.txt - docs/zh_CN/_build/html - expire_in: 1 day + expire_in: 4 days only: variables: - $BOT_TRIGGER_WITH_LABEL == null @@ -399,6 +348,29 @@ verify_cmake_style: script: tools/cmake/run_cmake_lint.sh +build_docker: + stage: build + image: espressif/docker-builder:1 + tags: + - build_docker_amd64_brno + only: + refs: + - master + - /^release\/v/ + - /^v\d+\.\d+(\.\d+)?($|-)/ + - schedules + variables: + DOCKER_TMP_IMAGE_NAME: "idf_tmp_image" + before_script: [] + script: + - export DOCKER_BUILD_ARGS="--build-arg IDF_CLONE_URL=${CI_REPOSITORY_URL} --build-arg IDF_CLONE_BRANCH_OR_TAG=${CI_COMMIT_REF_NAME} --build-arg IDF_CHECKOUT_REF=${CI_COMMIT_TAG:-$CI_COMMIT_SHA}" + # Build + - docker build --tag ${DOCKER_TMP_IMAGE_NAME} ${DOCKER_BUILD_ARGS} tools/docker/ + # We can't mount $PWD/examples/get-started/blink into the container, see https://gitlab.com/gitlab-org/gitlab-ce/issues/41227. + # The workaround mentioned there works, but leaves around directories which need to be cleaned up manually. + # Therefore, build a copy of the example located inside the container. + - docker run --rm --workdir /opt/esp/idf/examples/get-started/blink ${DOCKER_TMP_IMAGE_NAME} idf.py build + .host_test_template: &host_test_template stage: host_test image: $CI_DOCKER_REGISTRY/esp32-ci-env$BOT_DOCKER_IMAGE_TAG @@ -603,7 +575,7 @@ test_esp_efuse_table_on_host: - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 2.7.15 ./efuse_table_gen.py ${IDF_PATH}/components/efuse/esp32/esp_efuse_table.csv - git diff --exit-code -- esp32/esp_efuse_table.c || { echo 'Differences found. Please run make efuse_common_table or idf.py efuse_common_table and commit the changes.'; exit 1; } - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh -p 3.4.8 ./efuse_table_gen.py ${IDF_PATH}/components/efuse/esp32/esp_efuse_table.csv - - git diff --exit-code -- ../components/esp32/esp_efuse_table.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } + - git diff --exit-code -- esp32/esp_efuse_table.c || { echo 'Differences found between running under Python 2 and 3.'; exit 1; } - cd ${IDF_PATH}/components/efuse/test_efuse_host - ${IDF_PATH}/tools/ci/multirun_with_pyenv.sh ./efuse_tests.py @@ -625,9 +597,12 @@ push_to_github: tags: - deploy only: - - master - - /^release\/v/ - - /^v\d+\.\d+(\.\d+)?($|-)/ + refs: + - master + - /^release\/v/ + - /^v\d+\.\d+(\.\d+)?($|-)/ + variables: + - $BOT_TRIGGER_WITH_LABEL == null when: on_success dependencies: [] before_script: *do_nothing_before @@ -647,6 +622,7 @@ deploy_docs: image: $CI_DOCKER_REGISTRY/esp32-ci-env$BOT_DOCKER_IMAGE_TAG tags: - deploy + - shiny only: refs: - master @@ -723,6 +699,18 @@ check_permissions: script: - tools/ci/check-executable.sh +check_version: + <<: *check_job_template + # Don't run this for feature/bugfix branches, so that it is possible to modify + # esp_idf_version.h in a branch before tagging the next version. + only: + - master + - /^release\/v/ + - /^v\d+\.\d+(\.\d+)?($|-)/ + script: + - export IDF_PATH=$PWD + - tools/ci/check_idf_version.sh + check_examples_cmake_make: <<: *check_job_template except: @@ -812,9 +800,7 @@ assign_test: # gitlab ci do not support match job with RegEx or wildcard now in dependencies. # we have a lot build example jobs. now we don't use dependencies, just download all artificats of build stage. dependencies: - - build_ssc_00 - - build_ssc_01 - - build_ssc_02 + - build_ssc - build_esp_idf_tests_make - build_esp_idf_tests_cmake variables: @@ -839,11 +825,16 @@ assign_test: - python $TEST_FW_PATH/CIAssignUnitTest.py $IDF_PATH/components/idf_test/unit_test/TestCaseAll.yml $IDF_PATH/.gitlab-ci.yml $IDF_PATH/components/idf_test/unit_test/CIConfigs # clone test script to assign tests - git clone $TEST_SCRIPT_REPOSITORY + - python $CHECKOUT_REF_SCRIPT auto_test_script auto_test_script - cd auto_test_script - - python $CHECKOUT_REF_SCRIPT auto_test_script # assgin integration test cases - python CIAssignTestCases.py -t $IDF_PATH/components/idf_test/integration_test -c $IDF_PATH/.gitlab-ci.yml -b $IDF_PATH/SSC/ssc_bin +.define_config_file_name: &define_config_file_name | + JOB_NAME_PREFIX=$(echo ${CI_JOB_NAME} | awk '{print $1}') + JOG_FULL_NAME="${JOB_NAME_PREFIX}_${CI_NODE_INDEX}" + CONFIG_FILE="${CONFIG_FILE_PATH}/${JOG_FULL_NAME}.yml" + .example_test_template: &example_test_template stage: target_test when: on_success @@ -859,22 +850,8 @@ assign_test: - $BOT_LABEL_EXAMPLE_TEST dependencies: - assign_test - - build_examples_make_00 - - build_examples_make_01 - - build_examples_make_02 - - build_examples_make_03 - - build_examples_make_04 - - build_examples_make_05 - - build_examples_make_06 - - build_examples_make_07 - - build_examples_cmake_00 - - build_examples_cmake_01 - - build_examples_cmake_02 - - build_examples_cmake_03 - - build_examples_cmake_04 - - build_examples_cmake_05 - - build_examples_cmake_06 - - build_examples_cmake_07 + - build_examples_make + - build_examples_cmake artifacts: when: always paths: @@ -885,16 +862,16 @@ assign_test: variables: TEST_FW_PATH: "$CI_PROJECT_DIR/tools/tiny-test-fw" TEST_CASE_PATH: "$CI_PROJECT_DIR/examples" - CONFIG_FILE: "$CI_PROJECT_DIR/examples/test_configs/$CI_JOB_NAME.yml" + CONFIG_FILE_PATH: "${CI_PROJECT_DIR}/examples/test_configs" LOG_PATH: "$CI_PROJECT_DIR/TEST_LOGS" ENV_FILE: "$CI_PROJECT_DIR/ci-test-runner-configs/$CI_RUNNER_DESCRIPTION/EnvConfig.yml" script: + - *define_config_file_name # first test if config file exists, if not exist, exit 0 - test -e $CONFIG_FILE || exit 0 # clone test env configs - git clone $TEST_ENV_CONFIG_REPOSITORY - - cd ci-test-runner-configs - - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs + - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs ci-test-runner-configs - cd $TEST_FW_PATH # run test - python Runner.py $TEST_CASE_PATH -c $CONFIG_FILE -e $ENV_FILE @@ -919,7 +896,7 @@ assign_test: variables: TEST_FW_PATH: "$CI_PROJECT_DIR/tools/tiny-test-fw" TEST_CASE_PATH: "$CI_PROJECT_DIR/tools/unit-test-app" - CONFIG_FILE: "$CI_PROJECT_DIR/components/idf_test/unit_test/CIConfigs/$CI_JOB_NAME.yml" + CONFIG_FILE_PATH: "${CI_PROJECT_DIR}/components/idf_test/unit_test/CIConfigs" LOG_PATH: "$CI_PROJECT_DIR/TEST_LOGS" ENV_FILE: "$CI_PROJECT_DIR/ci-test-runner-configs/$CI_RUNNER_DESCRIPTION/EnvConfig.yml" @@ -938,9 +915,7 @@ assign_test: - $BOT_LABEL_INTEGRATION_TEST dependencies: - assign_test - - build_ssc_00 - - build_ssc_01 - - build_ssc_02 + - build_ssc artifacts: when: always paths: @@ -951,21 +926,22 @@ assign_test: LOG_PATH: "$CI_PROJECT_DIR/$CI_COMMIT_SHA" TEST_CASE_FILE_PATH: "$CI_PROJECT_DIR/components/idf_test/integration_test" MODULE_UPDATE_FILE: "$CI_PROJECT_DIR/components/idf_test/ModuleDefinition.yml" - CONFIG_FILE: "$CI_PROJECT_DIR/components/idf_test/integration_test/CIConfigs/$CI_JOB_NAME.yml" + CONFIG_FILE_PATH: "${CI_PROJECT_DIR}/components/idf_test/integration_test/CIConfigs" before_script: *add_gitlab_key_before script: + - *define_config_file_name # first test if config file exists, if not exist, exit 0 - test -e $CONFIG_FILE || exit 0 # clone local test env configs - git clone $TEST_ENV_CONFIG_REPOSITORY + - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs ci-test-runner-configs - cd ci-test-runner-configs - - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs # clone test bench - git clone $TEST_SCRIPT_REPOSITORY + - python $CHECKOUT_REF_SCRIPT auto_test_script auto_test_script - cd auto_test_script - - python $CHECKOUT_REF_SCRIPT auto_test_script # run test - - python CIRunner.py -l "$LOG_PATH/$CI_JOB_NAME" -c $CONFIG_FILE -e $LOCAL_ENV_CONFIG_PATH -t $TEST_CASE_FILE_PATH -m $MODULE_UPDATE_FILE + - python CIRunner.py -l "$LOG_PATH/$JOG_FULL_NAME" -c $CONFIG_FILE -e $LOCAL_ENV_CONFIG_PATH -t $TEST_CASE_FILE_PATH nvs_compatible_test: <<: *test_template @@ -979,57 +955,61 @@ nvs_compatible_test: - ESP32_IDF - NVS_Compatible script: + - *define_config_file_name + # first test if config file exists, if not exist, exit 0 + - test -e $CONFIG_FILE || exit 0 # clone local test env configs - git clone $TEST_ENV_CONFIG_REPOSITORY + - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs ci-test-runner-configs - cd ci-test-runner-configs - - python $CHECKOUT_REF_SCRIPT ci-test-runner-configs # clone test bench - git clone $TEST_SCRIPT_REPOSITORY + - python $CHECKOUT_REF_SCRIPT auto_test_script auto_test_script - cd auto_test_script - - git checkout ${CI_COMMIT_REF_NAME} || echo "Using default branch..." # prepare nvs bins - ./Tools/prepare_nvs_bin.sh # run test - - python CIRunner.py -l "$LOG_PATH/$CI_JOB_NAME" -c $CONFIG_FILE -e $LOCAL_ENV_CONFIG_PATH -t $TEST_CASE_FILE_PATH -m $MODULE_UPDATE_FILE + - python CIRunner.py -l "$LOG_PATH/$JOG_FULL_NAME" -c $CONFIG_FILE -e $LOCAL_ENV_CONFIG_PATH -t $TEST_CASE_FILE_PATH -example_test_001_01: +example_test_001: <<: *example_test_template + parallel: 3 tags: - ESP32 - Example_WIFI -example_test_001_02: - <<: *example_test_template - tags: - - ESP32 - - Example_WIFI - -example_test_002_01: +example_test_002: <<: *example_test_template image: $CI_DOCKER_REGISTRY/ubuntu-test-env$BOT_DOCKER_IMAGE_TAG tags: - ESP32 - Example_ShieldBox_Basic -.example_test_003_01: +.example_test_003: <<: *example_test_template tags: - ESP32 - Example_SDIO -example_test_004_01: +example_test_004A: <<: *example_test_template tags: - ESP32 - - Example_CAN + - Example_CAN1 -example_test_005_01: +example_test_004B: + <<: *example_test_template + tags: + - ESP32 + - Example_CAN2 + +example_test_005: <<: *example_test_template tags: - ESP32 - Example_WIFI_BT -example_test_006_01: +example_test_006: <<: *example_test_template image: $CI_DOCKER_REGISTRY/ubuntu-test-env$BOT_DOCKER_IMAGE_TAG only: @@ -1039,1075 +1019,371 @@ example_test_006_01: - ESP32 - Example_ShieldBox -example_test_007_01: +example_test_007: <<: *example_test_template tags: - ESP32 - Example_I2C_CCS811_SENSOR -UT_001_01: +UT_001: <<: *unit_test_template + parallel: 50 tags: - ESP32_IDF - UT_T1_1 -UT_001_02: +# Max. allowed value of 'parallel' is 50. + +UT_002: <<: *unit_test_template + parallel: 16 tags: - ESP32_IDF - UT_T1_1 + - psram -UT_001_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_05: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_06: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_07: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_08: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_09: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_10: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_11: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_12: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_13: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_14: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_15: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_16: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_17: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_18: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_19: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_20: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_21: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_22: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_23: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_24: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_25: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_26: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_27: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_28: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_29: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_30: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_31: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_32: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_33: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_34: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_35: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_36: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_37: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_38: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_39: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_40: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_41: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_42: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_001_43: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - -UT_002_01: +UT_003: <<: *unit_test_template + parallel: 3 tags: - ESP32_IDF - UT_T1_SDMODE -UT_002_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_SDMODE - -UT_002_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_SDMODE - -UT_003_01: +UT_004: <<: *unit_test_template + parallel: 3 tags: - ESP32_IDF - UT_T1_SPIMODE -UT_003_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_SPIMODE - -UT_003_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_SPIMODE - -UT_004_01: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_05: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_06: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_07: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_08: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_09: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_10: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_11: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_12: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_13: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_14: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_15: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_004_16: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - psram - -UT_005_01: +UT_005: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_SDMODE - psram -UT_005_02: +UT_006: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_SPIMODE - psram -UT_005_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_SPIMODE - - psram - -UT_006_01: +UT_007: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_GPIO -UT_006_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_GPIO - -UT_006_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_GPIO - -UT_006_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_GPIO - -UT_006_05: +UT_008: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_GPIO - psram -UT_007_01: +UT_009: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_PCNT -UT_007_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_PCNT - -UT_007_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_PCNT - -UT_007_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_PCNT - -UT_007_05: +UT_010: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_PCNT - psram -UT_008_01: +UT_011: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_LEDC -UT_008_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_LEDC - -UT_008_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_LEDC - -UT_008_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_LEDC - -UT_008_05: +UT_012: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_LEDC - psram -UT_009_01: +UT_013: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T2_RS485 -UT_009_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_RS485 - -UT_009_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_RS485 - -UT_009_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_RS485 - -UT_009_05: +UT_014: <<: *unit_test_template tags: - ESP32_IDF - UT_T2_RS485 - psram -UT_010_01: +UT_015: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_RMT -UT_010_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_RMT - -UT_010_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_RMT - -UT_010_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_RMT - -UT_010_05: +UT_016: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_RMT - psram -UT_011_01: +UT_017: <<: *unit_test_template + parallel: 3 tags: - ESP32_IDF - EMMC -UT_011_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - EMMC - -UT_011_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - EMMC - -UT_012_01: +UT_018: <<: *unit_test_template + parallel: 5 tags: - ESP32_IDF - UT_T1_1 - 8Mpsram -UT_012_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - 8Mpsram - -UT_012_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - 8Mpsram - -UT_012_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - 8Mpsram - -UT_012_05: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_1 - - 8Mpsram - -UT_013_01: +UT_019: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - Example_SPI_Multi_device -UT_013_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - Example_SPI_Multi_device - -UT_013_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - Example_SPI_Multi_device - -UT_013_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - Example_SPI_Multi_device - -UT_013_05: +UT_020: <<: *unit_test_template tags: - ESP32_IDF - Example_SPI_Multi_device - psram -UT_014_01: +UT_021: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T2_I2C -UT_014_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_I2C - -UT_014_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_I2C - -UT_014_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_I2C - -UT_014_05: +UT_022: <<: *unit_test_template tags: - ESP32_IDF - UT_T2_I2C - psram -UT_015_01: +UT_023: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_MCPWM -UT_015_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_MCPWM - -UT_015_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_MCPWM - -UT_015_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_MCPWM - -UT_015_05: +UT_024: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_MCPWM - psram -UT_016_01: +UT_025: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T1_I2S -UT_016_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_I2S - -UT_016_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_I2S - -UT_016_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T1_I2S - -UT_016_05: +UT_026: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_I2S - psram -UT_017_01: +UT_027: <<: *unit_test_template + parallel: 4 tags: - ESP32_IDF - UT_T2_1 -UT_017_02: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_1 - -UT_017_03: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_1 - -UT_017_04: - <<: *unit_test_template - tags: - - ESP32_IDF - - UT_T2_1 - -UT_017_05: +UT_028: <<: *unit_test_template tags: - ESP32_IDF - UT_T2_1 - psram -UT_017_06: +UT_029: <<: *unit_test_template tags: - ESP32_IDF - UT_T2_1 - 8Mpsram -UT_601_01: +UT_030: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_1 -UT_601_02: +UT_031: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_1 -UT_601_03: +UT_032: <<: *unit_test_template tags: - ESP32_IDF - UT_T1_1 + - psram -UT_601_04: +UT_033: + extends: .unit_test_template + parallel: 2 + tags: + - ESP32_IDF + - UT_T1_PSRAMV0 + - psram + +UT_034: <<: *unit_test_template tags: - ESP32_IDF - - UT_T1_1 + - UT_T1_no32kXTAL -UT_601_05: +UT_035: <<: *unit_test_template tags: - ESP32_IDF - - UT_T1_1 + - UT_T1_no32kXTAL -UT_601_06: +UT_036: <<: *unit_test_template tags: - ESP32_IDF - - UT_T1_1 + - UT_T1_no32kXTAL -UT_601_07: +UT_037: <<: *unit_test_template tags: - ESP32_IDF - - UT_T1_1 + - UT_T1_no32kXTAL + - psram -IT_001_01: +UT_038: + <<: *unit_test_template + tags: + - ESP32_IDF + - UT_T1_32kXTAL + +UT_039: + <<: *unit_test_template + tags: + - ESP32_IDF + - UT_T1_32kXTAL + +UT_040: + <<: *unit_test_template + tags: + - ESP32_IDF + - UT_T1_32kXTAL + +UT_041: + <<: *unit_test_template + tags: + - ESP32_IDF + - UT_T1_32kXTAL + - psram + +IT_001: <<: *test_template + parallel: 3 tags: - ESP32_IDF - SSC_T1_4 -IT_001_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_4 - -IT_001_03: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_4 - -IT_002_01: +IT_002: <<: *test_template tags: - ESP32_IDF - SSC_T1_2 -IT_003_01: +IT_003: <<: *test_template + parallel: 13 tags: - ESP32_IDF - SSC_T2_5 -IT_003_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_03: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_04: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_05: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_06: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_07: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_08: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_09: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_10: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_11: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_12: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_003_13: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_5 - -IT_004_01: +IT_004: <<: *test_template tags: - ESP32_IDF - SSC_T1_APC -IT_005_01: +IT_005: <<: *test_template + parallel: 2 tags: - ESP32_IDF - SSC_T1_5 -IT_005_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_5 - -IT_006_01: +IT_006: <<: *test_template + parallel: 8 tags: - ESP32_IDF - SSC_T1_6 -IT_006_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_006_03: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_006_04: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_006_05: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_006_06: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_006_07: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_6 - -IT_007_01: +IT_007: <<: *test_template + parallel: 3 tags: - ESP32_IDF - SSC_T1_7 -IT_007_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_7 - -IT_007_03: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_7 - -IT_008_01: +IT_008: <<: *test_template tags: - ESP32_IDF - SSC_T1_8 -IT_009_01: +IT_009: <<: *test_template tags: - ESP32_IDF - SSC_T1_3 -IT_010_01: +IT_010: <<: *test_template + parallel: 4 tags: - ESP32_IDF - SSC_T5_1 -IT_010_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T5_1 - -IT_010_03: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T5_1 - -IT_010_04: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T5_1 - -IT_011_01: +IT_011: <<: *test_template tags: - ESP32_IDF - SSC_T1_MESH1 -IT_011_02: +IT_012: <<: *test_template + parallel: 2 tags: - ESP32_IDF - SSC_T2_MESH1 @@ -2118,67 +1394,57 @@ IT_011_03: - ESP32_IDF - SSC_T2_MESH1 -IT_011_04: +IT_013: <<: *test_template tags: - ESP32_IDF - SSC_T3_MESH1 -IT_011_05: +IT_014: <<: *test_template tags: - ESP32_IDF - SSC_T6_MESH1 -IT_011_06: +IT_015: <<: *test_template tags: - ESP32_IDF - SSC_T12_MESH1 -IT_011_07: +IT_016: <<: *test_template tags: - ESP32_IDF - SSC_T50_MESH1 -IT_011_08: +IT_017: <<: *test_template tags: - ESP32_IDF - SSC_T1_MESH2 -IT_012_01: +IT_018: <<: *test_template + parallel: 2 tags: - ESP32_IDF - SSC_T1_9 -IT_012_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T1_9 - -IT_013_01: +IT_019: <<: *test_template + parallel: 2 tags: - ESP32_IDF - SSC_T2_2 -IT_013_02: - <<: *test_template - tags: - - ESP32_IDF - - SSC_T2_2 - -IT_014_01: +IT_020: <<: *test_template tags: - ESP32_IDF - SSC_T2_3 -IT_015_01: +IT_021: <<: *test_template tags: - ESP32_IDF diff --git a/.gitmodules b/.gitmodules index a9489d8a1..12c657a5d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -69,3 +69,7 @@ [submodule "examples/build_system/cmake/import_lib/main/lib/tinyxml2"] path = examples/build_system/cmake/import_lib/main/lib/tinyxml2 url = https://github.com/leethomason/tinyxml2 + +[submodule "components/nimble/nimble"] + path = components/nimble/nimble + url = https://github.com/espressif/esp-nimble.git diff --git a/.readthedocs.yml b/.readthedocs.yml index 988ce5c1d..e08e6b5b3 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -7,7 +7,6 @@ version: 2 # Optionally build your docs in additional formats such as PDF and ePub formats: - - htmlzip - pdf # Optionally set the version of Python and requirements required to build your docs @@ -16,7 +15,7 @@ python: install: - requirements: docs/requirements.txt -# We need to list all the submodules included in documenation build by Doxygen +# We need to list all the submodules included in documentation build by Doxygen submodules: include: - components/mqtt/esp-mqtt \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c6ef7621..cb7956db4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,108 +1,6 @@ cmake_minimum_required(VERSION 3.5) project(esp-idf C CXX ASM) -if(NOT IDF_PATH) - set(IDF_PATH ${CMAKE_CURRENT_LIST_DIR}) -endif() - -include(tools/cmake/idf_functions.cmake) - -# -# Set variables that control the build configuration and the build itself -# -idf_set_variables() - -kconfig_set_variables() - -# -# Generate a component dependencies file, enumerating components to be included in the build -# as well as their dependencies. -# -execute_process(COMMAND "${CMAKE_COMMAND}" - -D "COMPONENTS=${IDF_COMPONENTS}" - -D "COMPONENT_REQUIRES_COMMON=${IDF_COMPONENT_REQUIRES_COMMON}" - -D "EXCLUDE_COMPONENTS=${IDF_EXCLUDE_COMPONENTS}" - -D "TEST_COMPONENTS=${IDF_TEST_COMPONENTS}" - -D "TEST_EXCLUDE_COMPONENTS=${IDF_TEST_EXCLUDE_COMPONENTS}" - -D "BUILD_TESTS=${IDF_BUILD_TESTS}" - -D "DEPENDENCIES_FILE=${CMAKE_BINARY_DIR}/component_depends.cmake" - -D "COMPONENT_DIRS=${IDF_COMPONENT_DIRS}" - -D "BOOTLOADER_BUILD=${BOOTLOADER_BUILD}" - -D "IDF_TARGET=${IDF_TARGET}" - -D "IDF_PATH=${IDF_PATH}" - -D "DEBUG=${DEBUG}" - -P "${IDF_PATH}/tools/cmake/scripts/expand_requirements.cmake" - WORKING_DIRECTORY "${PROJECT_PATH}" - RESULT_VARIABLE expand_requirements_result) - -if(expand_requirements_result) - message(FATAL_ERROR "Failed to expand component requirements") -endif() - -include("${CMAKE_BINARY_DIR}/component_depends.cmake") - -# -# We now have the following component-related variables: -# -# IDF_COMPONENTS is the list of initial components set by the user -# (or empty to include all components in the build). -# BUILD_COMPONENTS is the list of components to include in the build. -# BUILD_COMPONENT_PATHS is the paths to all of these components, obtained from the component dependencies file. -# -# Print the list of found components and test components -# -string(REPLACE ";" " " BUILD_COMPONENTS_SPACES "${BUILD_COMPONENTS}") -message(STATUS "Component names: ${BUILD_COMPONENTS_SPACES}") -unset(BUILD_COMPONENTS_SPACES) -message(STATUS "Component paths: ${BUILD_COMPONENT_PATHS}") - -# Print list of test components -if(TESTS_ALL EQUAL 1 OR TEST_COMPONENTS) - string(REPLACE ";" " " BUILD_TEST_COMPONENTS_SPACES "${BUILD_TEST_COMPONENTS}") - message(STATUS "Test component names: ${BUILD_TEST_COMPONENTS_SPACES}") - unset(BUILD_TEST_COMPONENTS_SPACES) - message(STATUS "Test component paths: ${BUILD_TEST_COMPONENT_PATHS}") -endif() - -# Generate project configuration -kconfig_process_config() - -# Include sdkconfig.cmake so rest of the build knows the configuration -include(${SDKCONFIG_CMAKE}) - -# Verify the environment is configured correctly -idf_verify_environment() - -# Check git revision (may trigger reruns of cmake) -## sets IDF_VER to IDF git revision -idf_get_git_revision() - -# Check that the targets set in cache, sdkconfig, and in environment all match -idf_check_config_target() - -## get PROJECT_VER -if(NOT BOOTLOADER_BUILD) - app_get_revision("${CMAKE_SOURCE_DIR}") -endif() - -# Add some idf-wide definitions -idf_set_global_compile_options() - -# generate compile_commands.json (needs to come after project) -set(CMAKE_EXPORT_COMPILE_COMMANDS 1) - -# -# Setup variables for linker script generation -# -ldgen_set_variables() - -# Include any top-level project_include.cmake files from components -foreach(component ${BUILD_COMPONENT_PATHS}) - set(COMPONENT_PATH "${component}") - include_if_exists("${component}/project_include.cmake") - unset(COMPONENT_PATH) -endforeach() - # # Add each component to the build as a library # @@ -144,18 +42,4 @@ foreach(component ${BUILD_COMPONENTS}) add_component_dependencies(${component_target} ${dep_target} PRIVATE) endforeach() endif() -endforeach() - -if(IDF_BUILD_ARTIFACTS) - # Write project description JSON file - make_json_list("${BUILD_COMPONENTS}" build_components_json) - make_json_list("${BUILD_COMPONENT_PATHS}" build_component_paths_json) - configure_file("${IDF_PATH}/tools/cmake/project_description.json.in" - "${IDF_BUILD_ARTIFACTS_DIR}/project_description.json") - unset(build_components_json) - unset(build_component_paths_json) -endif() - -set(BUILD_COMPONENTS ${BUILD_COMPONENTS} PARENT_SCOPE) - -ldgen_add_dependencies() +endforeach() \ No newline at end of file diff --git a/Kconfig b/Kconfig index 75815ba1a..d4e9c1a9b 100644 --- a/Kconfig +++ b/Kconfig @@ -23,6 +23,10 @@ mainmenu "Espressif IoT Development Framework Configuration" default "IDF_TARGET_NOT_SET" if IDF_TARGET_ENV="" default IDF_TARGET_ENV + config IDF_FIRMWARE_CHIP_ID + hex + default 0x0000 if IDF_TARGET="esp32" + default 0xFFFF menu "SDK tool configuration" config TOOLPREFIX diff --git a/SUPPORT_POLICY.md b/SUPPORT_POLICY.md new file mode 100644 index 000000000..bd2b6db19 --- /dev/null +++ b/SUPPORT_POLICY.md @@ -0,0 +1,66 @@ +The latest support policy for ESP-IDF can be found at [https://github.com/espressif/esp-idf/blob/master/SUPPORT_POLICY.md](https://github.com/espressif/esp-idf/blob/master/SUPPORT_POLICY.md) + +Support Period Policy +===================== + +Each ESP-IDF major and minor release (V4.0, V4.1, etc) is supported for +18 months after the initial stable release date. + +Supported means that the ESP-IDF team will continue to apply bug fixes, +security fixes, etc to the release branch on GitHub, and periodically +make new bugfix releases as needed. + +Users are encouraged to upgrade to a newer ESP-IDF release before the +support period finishes and the release becomes End of Life (EOL). It is +our policy to not continue fixing bugs in End of Life releases. + +Pre-release versions (betas, previews, `-rc` and `-dev` versions, etc) +are not covered by any support period. Sometimes a particular feature is +marked as \"Preview\" in a release, which means it is also not covered +by the support period. + +The ESP-IDF Programming Guide has information about the +[different versions of ESP-IDF](https://docs.espressif.com/projects/esp-idf/en/latest/versions.html) +(major, minor, bugfix, etc). + +Long Term Support releases +-------------------------- + +Some releases (starting with ESP-IDF V3.3) are designated Long Term +Support (LTS). LTS releases are supported for 30 months (2.5 years) +after the initial stable release date. + +A new LTS release will be made at least every 18 months. This means +there will always be a period of at least 12 months to upgrade from the +previous LTS release to the following LTS release. + +Example +------- + +ESP-IDF V3.3 was released in September 2019 and is a Long Term Support +(LTS) release, meaning it will be supported for 30 months until February +2022. + +- The first V3.3 release was `v3.3` in September 2019. +- The ESP-IDF team continues to backport bug fixes, security fixes, + etc to the release branch `release/v3.3`. +- Periodically stable bugfix releases are created from the release + branch. For example `v3.3.1`, `v3.3.2`, etc. Users are encouraged to + always update to the latest bugfix release. +- V3.3 bugfix releases continue until February 2022, when all V3.3.x + releases become End of Life. + +Existing Releases +----------------- + +ESP-IDF release V3.3 and all newer releases will follow this support +period policy. The support period for each release will be announced +when the release is made. + +For releases made before the support period policy was announced, +the following support periods apply: + +- ESP-IDF V3.1.x and V3.2.x will both be supported until October 2020. +- ESP-IDF V3.0.9 (planned for October 2019) will be the last V3.0 + bugfix release. ESP-IDF V3.0.x is End of Life from October 2019. +- ESP-IDF versions before V3.0 are already End of Life. diff --git a/components/app_update/Kconfig.projbuild b/components/app_update/Kconfig.projbuild index 2c77c8ae2..8b40e0434 100644 --- a/components/app_update/Kconfig.projbuild +++ b/components/app_update/Kconfig.projbuild @@ -22,4 +22,14 @@ menu "Application manager" The PROJECT_NAME variable from the build system will not affect the firmware image. This value will not be contained in the esp_app_desc structure. + config APP_RETRIEVE_LEN_ELF_SHA + int "The length of APP ELF SHA is stored in RAM(chars)" + default 16 + range 8 64 + help + At startup, the app will read this many hex characters from the embedded APP ELF SHA-256 hash value + and store it in static RAM. This ensures the app ELF SHA-256 value is always available + if it needs to be printed by the panic handler code. + Changing this value will change the size of a static buffer, in bytes. + endmenu # "Application manager" diff --git a/components/app_update/esp_app_desc.c b/components/app_update/esp_app_desc.c index 23d3b4cc7..e8eabd978 100644 --- a/components/app_update/esp_app_desc.c +++ b/components/app_update/esp_app_desc.c @@ -72,13 +72,35 @@ static inline char IRAM_ATTR to_hex_digit(unsigned val) return (val < 10) ? ('0' + val) : ('a' + val - 10); } +__attribute__((constructor)) void esp_ota_init_app_elf_sha256(void) +{ + esp_ota_get_app_elf_sha256(NULL, 0); +} + +/* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled. + * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and + * can lead to a complete lock-up of the CPU. + * For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_ota_init_app_elf_sha256() + * and keep it in the static s_app_elf_sha256 value. + */ int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size) { - size_t n = MIN((size - 1) / 2, sizeof(esp_app_desc.app_elf_sha256)); - const uint8_t* src = esp_app_desc.app_elf_sha256; + static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2]; + static bool first_call = true; + if (first_call) { + first_call = false; + const uint8_t* src = esp_app_desc.app_elf_sha256; + for (size_t i = 0; i < sizeof(s_app_elf_sha256); ++i) { + s_app_elf_sha256[i] = src[i]; + } + } + if (dst == NULL || size == 0) { + return 0; + } + size_t n = MIN((size - 1) / 2, sizeof(s_app_elf_sha256)); for (size_t i = 0; i < n; ++i) { - dst[2*i] = to_hex_digit(src[i] >> 4); - dst[2*i + 1] = to_hex_digit(src[i] & 0xf); + dst[2*i] = to_hex_digit(s_app_elf_sha256[i] >> 4); + dst[2*i + 1] = to_hex_digit(s_app_elf_sha256[i] & 0xf); } dst[2*n] = 0; return 2*n + 1; diff --git a/components/app_update/esp_ota_ops.c b/components/app_update/esp_ota_ops.c index 02eab8eb6..56cd8e2ec 100644 --- a/components/app_update/esp_ota_ops.c +++ b/components/app_update/esp_ota_ops.c @@ -43,7 +43,7 @@ #include "esp_efuse.h" -#define SUB_TYPE_ID(i) (i & 0x0F) +#define SUB_TYPE_ID(i) (i & 0x0F) typedef struct ota_ops_entry_ { uint32_t handle; @@ -165,7 +165,8 @@ esp_err_t esp_ota_begin(const esp_partition_t *partition, size_t image_size, esp if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) { ret = esp_partition_erase_range(partition, 0, partition->size); } else { - ret = esp_partition_erase_range(partition, 0, (image_size / SPI_FLASH_SEC_SIZE + 1) * SPI_FLASH_SEC_SIZE); + const int aligned_erase_size = (image_size + SPI_FLASH_SEC_SIZE - 1) & ~(SPI_FLASH_SEC_SIZE - 1); + ret = esp_partition_erase_range(partition, 0, aligned_erase_size); } if (ret != ESP_OK) { @@ -208,7 +209,7 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size) // must erase the partition before writing to it assert(it->erased_size > 0 && "must erase the partition before writing to it"); if (it->wrote_size == 0 && it->partial_bytes == 0 && size > 0 && data_bytes[0] != ESP_IMAGE_HEADER_MAGIC) { - ESP_LOGE(TAG, "OTA image has invalid magic byte (expected 0xE9, saw 0x%02x", data_bytes[0]); + ESP_LOGE(TAG, "OTA image has invalid magic byte (expected 0xE9, saw 0x%02x)", data_bytes[0]); return ESP_ERR_OTA_VALIDATE_FAILED; } diff --git a/components/app_update/test/test_app_desc.c b/components/app_update/test/test_app_desc.c index 318729432..f15a79784 100644 --- a/components/app_update/test/test_app_desc.c +++ b/components/app_update/test/test_app_desc.c @@ -4,7 +4,7 @@ TEST_CASE("esp_ota_get_app_elf_sha256 test", "[esp_app_desc]") { - const int sha256_hex_len = 64; + const int sha256_hex_len = CONFIG_APP_RETRIEVE_LEN_ELF_SHA; char dst[sha256_hex_len + 2]; const char fill = 0xcc; int res; diff --git a/components/app_update/test/test_switch_ota.c b/components/app_update/test/test_switch_ota.c index f2adb2f79..7fc828f26 100644 --- a/components/app_update/test/test_switch_ota.c +++ b/components/app_update/test/test_switch_ota.c @@ -295,7 +295,7 @@ static void test_flow1(void) // 3 Stage: run OTA0 -> check it -> copy OTA0 to OTA1 -> reboot --//-- // 4 Stage: run OTA1 -> check it -> copy OTA1 to OTA0 -> reboot --//-- // 5 Stage: run OTA0 -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, OTA0", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow1, test_flow1, test_flow1, test_flow1); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow1, test_flow1, test_flow1, test_flow1); static void test_flow2(void) { @@ -332,7 +332,7 @@ static void test_flow2(void) // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//-- // 3 Stage: run OTA0 -> check it -> corrupt ota data -> reboot --//-- // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, corrupt ota_sec1, factory", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow2, test_flow2, test_flow2); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, corrupt ota_sec1, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow2, test_flow2, test_flow2); static void test_flow3(void) { @@ -376,7 +376,7 @@ static void test_flow3(void) // 3 Stage: run OTA0 -> check it -> copy OTA0 to OTA1 -> reboot --//-- // 3 Stage: run OTA1 -> check it -> corrupt ota sector2 -> reboot --//-- // 4 Stage: run OTA0 -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, currupt ota_sec2, OTA0", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow3, test_flow3, test_flow3, test_flow3); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, currupt ota_sec2, OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow3, test_flow3, test_flow3, test_flow3); #ifdef CONFIG_BOOTLOADER_FACTORY_RESET #define STORAGE_NAMESPACE "update_ota" @@ -443,7 +443,7 @@ static void test_flow4(void) // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//-- // 3 Stage: run OTA0 -> check it -> set_pin_factory_reset -> reboot --//-- // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, sets pin_factory_reset, factory", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow4, test_flow4, test_flow4); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, sets pin_factory_reset, factory", "[app_update][timeout=90][ignore][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow4, test_flow4, test_flow4); #endif #ifdef CONFIG_BOOTLOADER_APP_TEST @@ -486,7 +486,7 @@ static void test_flow5(void) // 2 Stage: run factory -> check it -> copy factory to Test and set pin_test_app -> reboot --//-- // 3 Stage: run test -> check it -> reset pin_test_app -> reboot --//-- // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Switching between factory, test, factory", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow5, test_flow5, test_flow5); +TEST_CASE_MULTIPLE_STAGES("Switching between factory, test, factory", "[app_update][timeout=90][ignore][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow5, test_flow5, test_flow5); #endif static const esp_partition_t* app_update(void) @@ -580,7 +580,7 @@ static void test_rollback1_1(void) // 3 Stage: run OTA0 -> check it -> esp_ota_mark_app_valid_cancel_rollback() -> reboot --//-- // 4 Stage: run OTA0 -> check it -> esp_ota_mark_app_invalid_rollback_and_reboot() -> reboot // 5 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA0, rollback -> factory", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback1, test_rollback1, test_rollback1, test_rollback1_1); +TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA0, rollback -> factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback1, test_rollback1, test_rollback1, test_rollback1_1); static void test_rollback2(void) { @@ -678,7 +678,7 @@ static void test_rollback2_1(void) // 3 Stage: run OTA0 -> check it -> esp_ota_mark_app_valid_cancel_rollback(), copy to next app slot -> reboot --//-- // 4 Stage: run OTA1 -> check it -> PENDING_VERIFY/esp_ota_mark_app_invalid_rollback_and_reboot() -> reboot // 5 Stage: run OTA0(rollback) -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA1, rollback -> OTA0", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback2, test_rollback2, test_rollback2, test_rollback2_1); +TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA1, rollback -> OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback2, test_rollback2, test_rollback2, test_rollback2_1); static void test_erase_last_app_flow(void) { @@ -729,4 +729,4 @@ static void test_erase_last_app_rollback(void) // 3 Stage: run OTA0 -> check it -> copy factory to OTA1 -> reboot --//-- // 4 Stage: run OTA1 -> check it -> erase OTA0 and rollback -> reboot // 5 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS -TEST_CASE_MULTIPLE_STAGES("Test erase_last_boot_app_partition. factory, OTA1, OTA0, factory", "[app_update][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_rollback); +TEST_CASE_MULTIPLE_STAGES("Test erase_last_boot_app_partition. factory, OTA1, OTA0, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_rollback); diff --git a/components/bootloader/Kconfig.projbuild b/components/bootloader/Kconfig.projbuild index 5370d4796..450cb459e 100644 --- a/components/bootloader/Kconfig.projbuild +++ b/components/bootloader/Kconfig.projbuild @@ -223,6 +223,7 @@ endmenu # Bootloader menu "Security features" + visible if !IDF_CMAKE # These three are the actual options to check in code, # selected by the displayed options @@ -505,4 +506,22 @@ menu "Security features" Only set this option in testing environments. endmenu # Potentially Insecure + + config FLASH_ENCRYPTION_DISABLE_PLAINTEXT + bool "Disable serial reflashing of plaintext firmware" + depends on FLASH_ENCRYPTION_ENABLED + default y if SECURE_BOOT_ENABLED + default n if !SECURE_BOOT_ENABLED + help + If this option is enabled, flash encryption is permanently enabled after first boot by write-protecting + the FLASH_CRYPT_CNT efuse. This is the recommended configuration for a secure production system. + + If this option is disabled, FLASH_CRYPT_CNT is left writeable and up to 4 plaintext re-flashes are allowed. + An attacker with physical access will be able to read out encrypted flash contents until all plaintext + re-flashes have been used up. + + If this option is disabled and hardware Secure Boot is enabled, Secure Boot must be configured in + Reflashable mode so that a new Secure Boot digest can be flashed at the same time as plaintext firmware. + This combination is not secure and should not be used for a production system. + endmenu # Security features diff --git a/components/bootloader/subproject/main/bootloader_start.c b/components/bootloader/subproject/main/bootloader_start.c index 6f7edc42e..a6d88aba1 100644 --- a/components/bootloader/subproject/main/bootloader_start.c +++ b/components/bootloader/subproject/main/bootloader_start.c @@ -24,6 +24,7 @@ #include "bootloader_common.h" #include "sdkconfig.h" #include "esp_image_format.h" +#include "rom/rtc.h" static const char* TAG = "boot"; @@ -74,7 +75,8 @@ static int selected_boot_partition(const bootloader_state_t *bs) int boot_index = bootloader_utility_get_selected_boot_partition(bs); if (boot_index == INVALID_INDEX) { return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents) - } else { + } + if (rtc_get_reset_reason(0) != DEEPSLEEP_RESET) { // Factory firmware. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) { diff --git a/components/bootloader_support/CMakeLists.txt b/components/bootloader_support/CMakeLists.txt index 3bd3e9b14..8b741ed23 100644 --- a/components/bootloader_support/CMakeLists.txt +++ b/components/bootloader_support/CMakeLists.txt @@ -1,6 +1,7 @@ set(COMPONENT_SRCS "src/bootloader_clock.c" "src/bootloader_common.c" "src/bootloader_flash.c" + "src/bootloader_flash_config.c" "src/bootloader_random.c" "src/bootloader_sha.c" "src/bootloader_utility.c" diff --git a/components/bootloader_support/include/bootloader_common.h b/components/bootloader_support/include/bootloader_common.h index 405c94c02..a948adbe6 100644 --- a/components/bootloader_support/include/bootloader_common.h +++ b/components/bootloader_support/include/bootloader_common.h @@ -15,6 +15,7 @@ #pragma once #include "esp_flash_data_types.h" #include "esp_image_format.h" +#include "esp_image_format.h" /// Type of hold a GPIO in low state typedef enum { @@ -23,6 +24,11 @@ typedef enum { GPIO_NOT_HOLD = 0 /*!< If the GPIO input is not low */ } esp_comm_gpio_hold_t; +typedef enum { + ESP_IMAGE_BOOTLOADER, + ESP_IMAGE_APPLICATION +} esp_image_type; + /** * @brief Calculate crc for the OTA data select. * @@ -125,7 +131,7 @@ int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, /** * @brief Returns esp_app_desc structure for app partition. This structure includes app version. - * + * * Returns a description for the requested app partition. * @param[in] partition App partition description. * @param[out] app_desc Structure of info about app. @@ -137,6 +143,24 @@ int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, */ esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc); +/** + * @brief Get chip revision + * + * @return Chip revision number + */ +uint8_t bootloader_common_get_chip_revision(void); + +/** + * @brief Check if the image (bootloader and application) has valid chip ID and revision + * + * @param[in] img_hdr: image header + * @param[in] type: image type, bootloader or application + * @return + * - ESP_OK: image and chip are matched well + * - ESP_FAIL: image doesn't match to the chip + */ +esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type); + /** * @brief Configure VDDSDIO, call this API to rise VDDSDIO to 1.9V when VDDSDIO regulator is enabled as 1.8V mode. */ diff --git a/components/bootloader_support/include/bootloader_flash_config.h b/components/bootloader_support/include/bootloader_flash_config.h new file mode 100644 index 000000000..2f716cce2 --- /dev/null +++ b/components/bootloader_support/include/bootloader_flash_config.h @@ -0,0 +1,71 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include "esp_image_format.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Update the flash id in g_rom_flashchip(global esp_rom_spiflash_chip_t structure). + * + * @return None + */ +void bootloader_flash_update_id(); + +/** + * @brief Set the flash CS setup and hold time. + * + * @note CS setup time is recomemded to be 1.5T, and CS hold time is recommended to be 2.5T. + * cs_setup = 1, cs_setup_time = 0; cs_hold = 1, cs_hold_time = 1. + * + * @return None + */ +void bootloader_flash_cs_timing_config(); + +/** + * @brief Configure SPI flash clock. + * + * @note This function only set clock frequency for SPI0. + * + * @param pfhdr Pointer to App image header, from where to fetch flash settings. + * + * @return None + */ +void bootloader_flash_clock_config(const esp_image_header_t* pfhdr); + +/** + * @brief Configure SPI flash gpio, include the IO matrix and drive strength configuration. + * + * @param pfhdr Pointer to App image header, from where to fetch flash settings. + * + * @return None + */ +void bootloader_flash_gpio_config(const esp_image_header_t* pfhdr); + +/** + * @brief Configure SPI flash read dummy based on different mode and frequency. + * + * @param pfhdr Pointer to App image header, from where to fetch flash settings. + * + * @return None + */ +void bootloader_flash_dummy_config(const esp_image_header_t* pfhdr); + +#ifdef __cplusplus +} +#endif diff --git a/components/bootloader_support/include/esp_image_format.h b/components/bootloader_support/include/esp_image_format.h index 7006cae98..fe6cf89bd 100644 --- a/components/bootloader_support/include/esp_image_format.h +++ b/components/bootloader_support/include/esp_image_format.h @@ -55,6 +55,19 @@ typedef enum { #define ESP_IMAGE_HEADER_MAGIC 0xE9 +/** + * @brief ESP chip ID + * + */ +typedef enum { + ESP_CHIP_ID_ESP32 = 0x0000, /*!< chip ID: ESP32 */ + ESP_CHIP_ID_INVALID = 0xFFFF /*!< Invalid chip ID (we defined it to make sure the esp_chip_id_t is 2 bytes size) */ +} __attribute__((packed)) esp_chip_id_t; + +/** @cond */ +_Static_assert(sizeof(esp_chip_id_t) == 2, "esp_chip_id_t should be 16 bit"); +/** @endcond */ + /* Main header of binary image */ typedef struct { uint8_t magic; @@ -71,8 +84,9 @@ typedef struct { uint8_t wp_pin; /* Drive settings for the SPI flash pins (read by ROM bootloader) */ uint8_t spi_pin_drv[3]; - /* Reserved bytes in ESP32 additional header space, currently unused */ - uint8_t reserved[11]; + esp_chip_id_t chip_id; /*!< Chip identification number */ + uint8_t min_chip_rev; /*!< Minimum chip revision supported by image */ + uint8_t reserved[8]; /*!< Reserved bytes in additional header space, currently unused */ /* If 1, a SHA256 digest "simple hash" (of the entire image) is appended after the checksum. Included in image length. This digest * is separate to secure boot and only used for detecting corruption. For secure boot signed images, the signature * is appended after this (and the simple hash is included in the signed data). */ diff --git a/components/bootloader_support/include_bootloader/bootloader_flash.h b/components/bootloader_support/include_bootloader/bootloader_flash.h index ce867d757..6ac724646 100644 --- a/components/bootloader_support/include_bootloader/bootloader_flash.h +++ b/components/bootloader_support/include_bootloader/bootloader_flash.h @@ -30,6 +30,13 @@ bootloader_support components only. */ +/** + * @brief Get number of free pages + * + * @return Number of free pages + */ +uint32_t bootloader_mmap_get_free_pages(); + /** * @brief Map a region of flash to data memory * diff --git a/components/bootloader_support/src/bootloader_clock.c b/components/bootloader_support/src/bootloader_clock.c index 24267cc32..9980a0278 100644 --- a/components/bootloader_support/src/bootloader_clock.c +++ b/components/bootloader_support/src/bootloader_clock.c @@ -59,3 +59,12 @@ void bootloader_clock_configure() } #endif } + +#ifdef BOOTLOADER_BUILD + +int esp_clk_apb_freq(void) +{ + return rtc_clk_apb_freq_get(); +} + +#endif // BOOTLOADER_BUILD diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index aa64db482..61d89f67e 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -29,6 +29,9 @@ #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/rtc.h" +#include "soc/spi_reg.h" +#include "soc/efuse_reg.h" +#include "soc/apb_ctrl_reg.h" #include "esp_image_format.h" #include "bootloader_sha.h" #include "sys/param.h" @@ -274,3 +277,51 @@ void bootloader_common_vddsdio_configure() } #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST } + +#ifdef CONFIG_IDF_TARGET_ESP32 +uint8_t bootloader_common_get_chip_revision(void) +{ + uint8_t eco_bit0, eco_bit1, eco_bit2; + eco_bit0 = (REG_READ(EFUSE_BLK0_RDATA3_REG) & 0xF000) >> 15; + eco_bit1 = (REG_READ(EFUSE_BLK0_RDATA5_REG) & 0x100000) >> 20; + eco_bit2 = (REG_READ(APB_CTRL_DATE_REG) & 0x80000000) >> 31; + uint32_t combine_value = (eco_bit2 << 2) | (eco_bit1 << 1) | eco_bit0; + uint8_t chip_ver = 0; + switch (combine_value) { + case 0: + chip_ver = 0; + break; + case 1: + chip_ver = 1; + break; + case 3: + chip_ver = 2; + break; + case 7: + chip_ver = 3; + break; + default: + chip_ver = 0; + break; + } + return chip_ver; +} +#endif + +esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type) +{ + esp_err_t err = ESP_OK; + esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID; + if (chip_id != img_hdr->chip_id) { + ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id); + err = ESP_FAIL; + } + uint8_t revision = bootloader_common_get_chip_revision(); + if (revision < img_hdr->min_chip_rev) { + ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev); + err = ESP_FAIL; + } else if (revision != img_hdr->min_chip_rev) { + ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev); + } + return err; +} diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/bootloader_flash.c index dbdf84ee2..ea7ce2c77 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/bootloader_flash.c @@ -25,6 +25,11 @@ static const char *TAG = "bootloader_mmap"; static spi_flash_mmap_handle_t map; +uint32_t bootloader_mmap_get_free_pages() +{ + return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA); +} + const void *bootloader_mmap(uint32_t src_addr, uint32_t size) { if (map) { @@ -91,12 +96,22 @@ static const char *TAG = "bootloader_flash"; */ #define MMU_BLOCK0_VADDR 0x3f400000 #define MMU_BLOCK50_VADDR 0x3f720000 +#define MMU_FREE_PAGES ((MMU_BLOCK50_VADDR - MMU_BLOCK0_VADDR) / FLASH_BLOCK_SIZE) static bool mapped; // Current bootloader mapping (ab)used for bootloader_read() static uint32_t current_read_mapping = UINT32_MAX; +uint32_t bootloader_mmap_get_free_pages() +{ + /** + * Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads) + * Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this. + */ + return MMU_FREE_PAGES; +} + const void *bootloader_mmap(uint32_t src_addr, uint32_t size) { if (mapped) { diff --git a/components/bootloader_support/src/bootloader_flash_config.c b/components/bootloader_support/src/bootloader_flash_config.c new file mode 100644 index 000000000..b3a3ed9d7 --- /dev/null +++ b/components/bootloader_support/src/bootloader_flash_config.c @@ -0,0 +1,166 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include +#include +#include "string.h" +#include "sdkconfig.h" +#include "esp_err.h" +#include "esp_log.h" +#include "rom/gpio.h" +#include "rom/spi_flash.h" +#include "rom/efuse.h" +#include "soc/gpio_periph.h" +#include "soc/efuse_reg.h" +#include "soc/spi_reg.h" +#include "soc/spi_pins.h" +#include "flash_qio_mode.h" +#include "bootloader_flash_config.h" + +void bootloader_flash_update_id() +{ + g_rom_flashchip.device_id = bootloader_read_flash_id(); +} + +void IRAM_ATTR bootloader_flash_cs_timing_config() +{ + SET_PERI_REG_MASK(SPI_USER_REG(0), SPI_CS_HOLD_M | SPI_CS_SETUP_M); + SET_PERI_REG_BITS(SPI_CTRL2_REG(0), SPI_HOLD_TIME_V, 1, SPI_HOLD_TIME_S); + SET_PERI_REG_BITS(SPI_CTRL2_REG(0), SPI_SETUP_TIME_V, 0, SPI_SETUP_TIME_S); + SET_PERI_REG_MASK(SPI_USER_REG(1), SPI_CS_HOLD_M | SPI_CS_SETUP_M); + SET_PERI_REG_BITS(SPI_CTRL2_REG(1), SPI_HOLD_TIME_V, 1, SPI_HOLD_TIME_S); + SET_PERI_REG_BITS(SPI_CTRL2_REG(1), SPI_SETUP_TIME_V, 0, SPI_SETUP_TIME_S); +} + +void IRAM_ATTR bootloader_flash_clock_config(const esp_image_header_t* pfhdr) +{ + uint32_t spi_clk_div = 0; + switch (pfhdr->spi_speed) { + case ESP_IMAGE_SPI_SPEED_80M: + spi_clk_div = 1; + break; + case ESP_IMAGE_SPI_SPEED_40M: + spi_clk_div = 2; + break; + case ESP_IMAGE_SPI_SPEED_26M: + spi_clk_div = 3; + break; + case ESP_IMAGE_SPI_SPEED_20M: + spi_clk_div = 4; + break; + default: + break; + } + esp_rom_spiflash_config_clk(spi_clk_div, 0); + esp_rom_spiflash_config_clk(spi_clk_div, 1); +} + +void IRAM_ATTR bootloader_flash_gpio_config(const esp_image_header_t* pfhdr) +{ + uint32_t drv = 2; + if (pfhdr->spi_speed == ESP_IMAGE_SPI_SPEED_80M) { + drv = 3; + } + + uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG); + uint32_t pkg_ver = chip_ver & 0x7; + + if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) { + // For ESP32D2WD the SPI pins are already configured + // flash clock signal should come from IO MUX. + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); + } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) { + // For ESP32PICOD2 the SPI pins are already configured + // flash clock signal should come from IO MUX. + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); + } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) { + // For ESP32PICOD4 the SPI pins are already configured + // flash clock signal should come from IO MUX. + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); + } else { + const uint32_t spiconfig = ets_efuse_get_spiconfig(); + if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) { + gpio_matrix_out(SPI_IOMUX_PIN_NUM_CS, SPICS0_OUT_IDX, 0, 0); + gpio_matrix_out(SPI_IOMUX_PIN_NUM_MISO, SPIQ_OUT_IDX, 0, 0); + gpio_matrix_in(SPI_IOMUX_PIN_NUM_MISO, SPIQ_IN_IDX, 0); + gpio_matrix_out(SPI_IOMUX_PIN_NUM_MOSI, SPID_OUT_IDX, 0, 0); + gpio_matrix_in(SPI_IOMUX_PIN_NUM_MOSI, SPID_IN_IDX, 0); + gpio_matrix_out(SPI_IOMUX_PIN_NUM_WP, SPIWP_OUT_IDX, 0, 0); + gpio_matrix_in(SPI_IOMUX_PIN_NUM_WP, SPIWP_IN_IDX, 0); + gpio_matrix_out(SPI_IOMUX_PIN_NUM_HD, SPIHD_OUT_IDX, 0, 0); + gpio_matrix_in(SPI_IOMUX_PIN_NUM_HD, SPIHD_IN_IDX, 0); + //select pin function gpio + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, PIN_FUNC_GPIO); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, PIN_FUNC_GPIO); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA2_U, PIN_FUNC_GPIO); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA3_U, PIN_FUNC_GPIO); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, PIN_FUNC_GPIO); + // flash clock signal should come from IO MUX. + // set drive ability for clock + PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); + + uint32_t flash_id = g_rom_flashchip.device_id; + if (flash_id == FLASH_ID_GD25LQ32C) { + // Set drive ability for 1.8v flash in 80Mhz. + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA0_U, FUN_DRV, 3, FUN_DRV_S); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA1_U, FUN_DRV, 3, FUN_DRV_S); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA2_U, FUN_DRV, 3, FUN_DRV_S); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA3_U, FUN_DRV, 3, FUN_DRV_S); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CMD_U, FUN_DRV, 3, FUN_DRV_S); + SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, 3, FUN_DRV_S); + } + } + } +} + +void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t* pfhdr) +{ + int spi_cache_dummy = 0; + uint32_t modebit = READ_PERI_REG(SPI_CTRL_REG(0)); + if (modebit & SPI_FASTRD_MODE) { + if (modebit & SPI_FREAD_QIO) { //SPI mode is QIO + spi_cache_dummy = SPI0_R_QIO_DUMMY_CYCLELEN; + } else if (modebit & SPI_FREAD_DIO) { //SPI mode is DIO + spi_cache_dummy = SPI0_R_DIO_DUMMY_CYCLELEN; + SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_ADDR_BITLEN_V, SPI0_R_DIO_ADDR_BITSLEN, SPI_USR_ADDR_BITLEN_S); + } else if(modebit & (SPI_FREAD_QUAD | SPI_FREAD_DUAL)) { //SPI mode is QOUT or DIO + spi_cache_dummy = SPI0_R_FAST_DUMMY_CYCLELEN; + } + } + + extern uint8_t g_rom_spiflash_dummy_len_plus[]; + switch (pfhdr->spi_speed) { + case ESP_IMAGE_SPI_SPEED_80M: + g_rom_spiflash_dummy_len_plus[0] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M; + g_rom_spiflash_dummy_len_plus[1] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M; + break; + case ESP_IMAGE_SPI_SPEED_40M: + g_rom_spiflash_dummy_len_plus[0] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M; + g_rom_spiflash_dummy_len_plus[1] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M; + break; + case ESP_IMAGE_SPI_SPEED_26M: + case ESP_IMAGE_SPI_SPEED_20M: + g_rom_spiflash_dummy_len_plus[0] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M; + g_rom_spiflash_dummy_len_plus[1] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M; + break; + default: + break; + } + + SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy + g_rom_spiflash_dummy_len_plus[0], + SPI_USR_DUMMY_CYCLELEN_S); +} \ No newline at end of file diff --git a/components/bootloader_support/src/bootloader_init.c b/components/bootloader_support/src/bootloader_init.c index 15b46dded..17d0fb161 100644 --- a/components/bootloader_support/src/bootloader_init.c +++ b/components/bootloader_support/src/bootloader_init.c @@ -49,6 +49,7 @@ #include "bootloader_config.h" #include "bootloader_clock.h" #include "bootloader_common.h" +#include "bootloader_flash_config.h" #include "flash_qio_mode.h" @@ -62,7 +63,7 @@ static const char* TAG = "boot"; static esp_err_t bootloader_main(); static void print_flash_info(const esp_image_header_t* pfhdr); static void update_flash_config(const esp_image_header_t* pfhdr); -static void flash_gpio_configure(const esp_image_header_t* pfhdr); +static void bootloader_init_flash_configure(const esp_image_header_t* pfhdr); static void uart_console_configure(void); static void wdt_reset_check(void); @@ -125,7 +126,15 @@ static esp_err_t bootloader_main() ESP_LOGE(TAG, "failed to load bootloader header!"); return ESP_FAIL; } - flash_gpio_configure(&fhdr); + + /* Check chip ID and minimum chip revision that supported by this image */ + uint8_t revision = bootloader_common_get_chip_revision(); + ESP_LOGI(TAG, "Chip Revision: %d", revision); + if (bootloader_common_check_chip_validity(&fhdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) { + return ESP_FAIL; + } + + bootloader_init_flash_configure(&fhdr); #if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ == 240) //Check if ESP32 is rated for a CPU frequency of 160MHz only if (REG_GET_BIT(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_CPU_FREQ_RATED) && @@ -285,118 +294,15 @@ static void print_flash_info(const esp_image_header_t* phdr) #endif } -#define FLASH_CLK_IO 6 -#define FLASH_CS_IO 11 -#define FLASH_SPIQ_IO 7 -#define FLASH_SPID_IO 8 -#define FLASH_SPIWP_IO 10 -#define FLASH_SPIHD_IO 9 -#define FLASH_IO_MATRIX_DUMMY_40M 1 -#define FLASH_IO_MATRIX_DUMMY_80M 2 -#define FLASH_IO_DRIVE_GD_WITH_1V8PSRAM 3 - /* * Bootloader reads SPI configuration from bin header, so that * the burning configuration can be different with compiling configuration. */ -static void IRAM_ATTR flash_gpio_configure(const esp_image_header_t* pfhdr) +static void IRAM_ATTR bootloader_init_flash_configure(const esp_image_header_t* pfhdr) { - int spi_cache_dummy = 0; - int drv = 2; - switch (pfhdr->spi_mode) { - case ESP_IMAGE_SPI_MODE_QIO: - spi_cache_dummy = SPI0_R_QIO_DUMMY_CYCLELEN; - break; - case ESP_IMAGE_SPI_MODE_DIO: - spi_cache_dummy = SPI0_R_DIO_DUMMY_CYCLELEN; - SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_ADDR_BITLEN_V, SPI0_R_DIO_ADDR_BITSLEN, SPI_USR_ADDR_BITLEN_S); - break; - case ESP_IMAGE_SPI_MODE_QOUT: - case ESP_IMAGE_SPI_MODE_DOUT: - default: - spi_cache_dummy = SPI0_R_FAST_DUMMY_CYCLELEN; - break; - } - - /* dummy_len_plus values defined in ROM for SPI flash configuration */ - extern uint8_t g_rom_spiflash_dummy_len_plus[]; - switch (pfhdr->spi_speed) { - case ESP_IMAGE_SPI_SPEED_80M: - g_rom_spiflash_dummy_len_plus[0] = FLASH_IO_MATRIX_DUMMY_80M; - g_rom_spiflash_dummy_len_plus[1] = FLASH_IO_MATRIX_DUMMY_80M; - SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy + FLASH_IO_MATRIX_DUMMY_80M, - SPI_USR_DUMMY_CYCLELEN_S); //DUMMY - drv = 3; - break; - case ESP_IMAGE_SPI_SPEED_40M: - g_rom_spiflash_dummy_len_plus[0] = FLASH_IO_MATRIX_DUMMY_40M; - g_rom_spiflash_dummy_len_plus[1] = FLASH_IO_MATRIX_DUMMY_40M; - SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy + FLASH_IO_MATRIX_DUMMY_40M, - SPI_USR_DUMMY_CYCLELEN_S); //DUMMY - break; - case ESP_IMAGE_SPI_SPEED_26M: - case ESP_IMAGE_SPI_SPEED_20M: - SET_PERI_REG_BITS(SPI_USER1_REG(0), SPI_USR_DUMMY_CYCLELEN_V, spi_cache_dummy, SPI_USR_DUMMY_CYCLELEN_S); //DUMMY - break; - default: - break; - } - - uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG); - uint32_t pkg_ver = chip_ver & 0x7; - - if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5) { - // For ESP32D2WD the SPI pins are already configured - // flash clock signal should come from IO MUX. - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); - } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) { - // For ESP32PICOD2 the SPI pins are already configured - // flash clock signal should come from IO MUX. - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); - } else if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) { - // For ESP32PICOD4 the SPI pins are already configured - // flash clock signal should come from IO MUX. - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); - } else { - const uint32_t spiconfig = ets_efuse_get_spiconfig(); - if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) { - gpio_matrix_out(FLASH_CS_IO, SPICS0_OUT_IDX, 0, 0); - gpio_matrix_out(FLASH_SPIQ_IO, SPIQ_OUT_IDX, 0, 0); - gpio_matrix_in(FLASH_SPIQ_IO, SPIQ_IN_IDX, 0); - gpio_matrix_out(FLASH_SPID_IO, SPID_OUT_IDX, 0, 0); - gpio_matrix_in(FLASH_SPID_IO, SPID_IN_IDX, 0); - gpio_matrix_out(FLASH_SPIWP_IO, SPIWP_OUT_IDX, 0, 0); - gpio_matrix_in(FLASH_SPIWP_IO, SPIWP_IN_IDX, 0); - gpio_matrix_out(FLASH_SPIHD_IO, SPIHD_OUT_IDX, 0, 0); - gpio_matrix_in(FLASH_SPIHD_IO, SPIHD_IN_IDX, 0); - //select pin function gpio - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, PIN_FUNC_GPIO); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, PIN_FUNC_GPIO); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA2_U, PIN_FUNC_GPIO); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA3_U, PIN_FUNC_GPIO); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, PIN_FUNC_GPIO); - // flash clock signal should come from IO MUX. - // set drive ability for clock - PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, FUNC_SD_CLK_SPICLK); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, drv, FUN_DRV_S); - - #if CONFIG_SPIRAM_TYPE_ESPPSRAM32 - uint32_t flash_id = g_rom_flashchip.device_id; - if (flash_id == FLASH_ID_GD25LQ32C) { - // Set drive ability for 1.8v flash in 80Mhz. - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA0_U, FUN_DRV, 3, FUN_DRV_S); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA1_U, FUN_DRV, 3, FUN_DRV_S); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA2_U, FUN_DRV, 3, FUN_DRV_S); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_DATA3_U, FUN_DRV, 3, FUN_DRV_S); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CMD_U, FUN_DRV, 3, FUN_DRV_S); - SET_PERI_REG_BITS(PERIPHS_IO_MUX_SD_CLK_U, FUN_DRV, 3, FUN_DRV_S); - } - #endif - } - } + bootloader_flash_gpio_config(pfhdr); + bootloader_flash_dummy_config(pfhdr); + bootloader_flash_cs_timing_config(); } static void uart_console_configure(void) diff --git a/components/bootloader_support/src/bootloader_random.c b/components/bootloader_support/src/bootloader_random.c index eb34d6add..1b9c504ca 100644 --- a/components/bootloader_support/src/bootloader_random.c +++ b/components/bootloader_support/src/bootloader_random.c @@ -114,17 +114,18 @@ void bootloader_random_enable(void) void bootloader_random_disable(void) { - /* Disable i2s clock */ - DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_I2S0_CLK_EN); - - /* Reset some i2s configuration (possibly redundant as we reset entire I2S peripheral further down). */ + CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_START); + SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET); + CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_RESET); CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_CAMERA_EN); CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_LCD_EN); CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE_TEST_EN); CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE); - CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_START); + + /* Disable i2s clock */ + DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_I2S0_CLK_EN); /* Restore SYSCON mode registers */ CLEAR_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DIG_FORCE); diff --git a/components/bootloader_support/src/esp_image_format.c b/components/bootloader_support/src/esp_image_format.c index 958f3a6a9..7f5595251 100644 --- a/components/bootloader_support/src/esp_image_format.c +++ b/components/bootloader_support/src/esp_image_format.c @@ -24,6 +24,7 @@ #include #include #include "bootloader_util.h" +#include "bootloader_common.h" /* Checking signatures as part of verifying images is necessary: - Always if secure boot is enabled @@ -280,6 +281,9 @@ static esp_err_t verify_image_header(uint32_t src_addr, const esp_image_header_t } err = ESP_ERR_IMAGE_INVALID; } + if (bootloader_common_check_chip_validity(image, ESP_IMAGE_APPLICATION) != ESP_OK) { + err = ESP_ERR_IMAGE_INVALID; + } if (!silent) { if (image->spi_mode > ESP_IMAGE_SPI_MODE_SLOW_READ) { ESP_LOGW(TAG, "image at 0x%x has invalid SPI mode %d", src_addr, image->spi_mode); @@ -368,24 +372,22 @@ static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segme } #endif // BOOTLOADER_BUILD -#ifndef BOOTLOADER_BUILD - uint32_t free_page_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA); - ESP_LOGD(TAG, "free data page_count 0x%08x",free_page_count); - uint32_t offset_page = 0; - while (data_len >= free_page_count * SPI_FLASH_MMU_PAGE_SIZE) { - offset_page = ((data_addr & MMAP_ALIGNED_MASK) != 0)?1:0; - err = process_segment_data(load_addr, data_addr, (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE, do_load, sha_handle, checksum); + uint32_t free_page_count = bootloader_mmap_get_free_pages(); + ESP_LOGD(TAG, "free data page_count 0x%08x", free_page_count); + + int32_t data_len_remain = data_len; + while (data_len_remain > 0) { + uint32_t offset_page = ((data_addr & MMAP_ALIGNED_MASK) != 0) ? 1 : 0; + /* Data we could map in case we are not aligned to PAGE boundary is one page size lesser. */ + data_len = MIN(data_len_remain, ((free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE)); + err = process_segment_data(load_addr, data_addr, data_len, do_load, sha_handle, checksum); if (err != ESP_OK) { return err; } - data_addr += (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE; - data_len -= (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE; - } -#endif - err = process_segment_data(load_addr, data_addr, data_len, do_load, sha_handle, checksum); - if (err != ESP_OK) { - return err; + data_addr += data_len; + data_len_remain -= data_len; } + return ESP_OK; err: diff --git a/components/bootloader_support/src/flash_encrypt.c b/components/bootloader_support/src/flash_encrypt.c index f9d7bfdb3..ccb3c4214 100644 --- a/components/bootloader_support/src/flash_encrypt.c +++ b/components/bootloader_support/src/flash_encrypt.c @@ -205,6 +205,14 @@ static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_cry uint32_t new_flash_crypt_cnt = flash_crypt_cnt + (1 << (ffs_inv - 1)); ESP_LOGD(TAG, "FLASH_CRYPT_CNT 0x%x -> 0x%x", flash_crypt_cnt, new_flash_crypt_cnt); REG_SET_FIELD(EFUSE_BLK0_WDATA0_REG, EFUSE_FLASH_CRYPT_CNT, new_flash_crypt_cnt); + +#ifdef CONFIG_FLASH_ENCRYPTION_DISABLE_PLAINTEXT + ESP_LOGI(TAG, "Write protecting FLASH_CRYPT_CNT efuse..."); + REG_SET_BIT(EFUSE_BLK0_WDATA0_REG, EFUSE_WR_DIS_FLASH_CRYPT_CNT); +#else + ESP_LOGW(TAG, "Not disabling FLASH_CRYPT_CNT - plaintext flashing is still possible"); +#endif + esp_efuse_burn_new_values(); ESP_LOGI(TAG, "Flash encryption completed"); diff --git a/components/bootloader_support/src/secure_boot_signatures.c b/components/bootloader_support/src/secure_boot_signatures.c index b6681bc79..6981872f2 100644 --- a/components/bootloader_support/src/secure_boot_signatures.c +++ b/components/bootloader_support/src/secure_boot_signatures.c @@ -21,12 +21,7 @@ #include "uECC.h" -#ifdef BOOTLOADER_BUILD -#include "rom/sha.h" -typedef SHA_CTX sha_context; -#else -#include "mbedtls/sha256.h" -#endif +#include static const char* TAG = "secure_boot"; @@ -37,6 +32,9 @@ extern const uint8_t signature_verification_key_end[] asm("_binary_signature_ver #define DIGEST_LEN 32 +/* Mmap source address mask */ +#define MMAP_ALIGNED_MASK 0x0000FFFF + esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length) { uint8_t digest[DIGEST_LEN]; @@ -45,26 +43,44 @@ esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length) ESP_LOGD(TAG, "verifying signature src_addr 0x%x length 0x%x", src_addr, length); - data = bootloader_mmap(src_addr, length + sizeof(esp_secure_boot_sig_block_t)); - if(data == NULL) { - ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", src_addr, length+sizeof(esp_secure_boot_sig_block_t)); - return ESP_FAIL; + bootloader_sha256_handle_t handle = bootloader_sha256_start(); + + uint32_t free_page_count = bootloader_mmap_get_free_pages(); + ESP_LOGD(TAG, "free data page_count 0x%08x", free_page_count); + + int32_t data_len_remain = length; + uint32_t data_addr = src_addr; + while (data_len_remain > 0) { + uint32_t offset_page = ((data_addr & MMAP_ALIGNED_MASK) != 0) ? 1 : 0; + /* Data we could map in case we are not aligned to PAGE boundary is one page size lesser. */ + uint32_t data_len = MIN(data_len_remain, ((free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE)); + data = (const uint8_t *) bootloader_mmap(data_addr, data_len); + if(!data) { + ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", data_addr, data_len); + bootloader_sha256_finish(handle, NULL); + return ESP_FAIL; + } + bootloader_sha256_data(handle, data, data_len); + bootloader_munmap(data); + + data_addr += data_len; + data_len_remain -= data_len; } - // Calculate digest of main image -#ifdef BOOTLOADER_BUILD - bootloader_sha256_handle_t handle = bootloader_sha256_start(); - bootloader_sha256_data(handle, data, length); + /* Done! Get the digest */ bootloader_sha256_finish(handle, digest); -#else - /* Use thread-safe mbedTLS version */ - mbedtls_sha256_ret(data, length, digest, 0); -#endif - // Map the signature block and verify the signature - sigblock = (const esp_secure_boot_sig_block_t *)(data + length); + // Map the signature block + sigblock = (const esp_secure_boot_sig_block_t *) bootloader_mmap(src_addr + length, sizeof(esp_secure_boot_sig_block_t)); + if(!sigblock) { + ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", src_addr + length, sizeof(esp_secure_boot_sig_block_t)); + return ESP_FAIL; + } + // Verify the signature esp_err_t err = esp_secure_boot_verify_signature_block(sigblock, digest); - bootloader_munmap(data); + // Unmap + bootloader_munmap(sigblock); + return err; } diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index d0d8b38ff..b5d4bd154 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -6,6 +6,8 @@ if(CONFIG_BT_ENABLED) if(CONFIG_BLUEDROID_ENABLED) list(APPEND COMPONENT_PRIV_INCLUDEDIRS + common/btc/include + common/include bluedroid/bta/include bluedroid/bta/ar/include bluedroid/bta/av/include @@ -18,7 +20,6 @@ if(CONFIG_BT_ENABLED) bluedroid/bta/sys/include bluedroid/device/include bluedroid/hci/include - bluedroid/osi/include bluedroid/external/sbc/decoder/include bluedroid/external/sbc/encoder/include bluedroid/btc/profile/esp/blufi/include @@ -38,11 +39,29 @@ if(CONFIG_BT_ENABLED) bluedroid/stack/a2dp/include bluedroid/stack/rfcomm/include bluedroid/stack/include - bluedroid/common/include) + bluedroid/common/include + common/btc/include + common/include) - list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/api/include/api) + list(APPEND COMPONENT_ADD_INCLUDEDIRS bluedroid/api/include/api + common/osi/include) - list(APPEND COMPONENT_SRCS "bluedroid/api/esp_a2dp_api.c" + list(APPEND COMPONENT_SRCS "common/btc/core/btc_alarm.c" + "common/btc/core/btc_manage.c" + "common/btc/core/btc_task.c" + "common/osi/alarm.c" + "common/osi/allocator.c" + "common/osi/buffer.c" + "common/osi/config.c" + "common/osi/fixed_queue.c" + "common/osi/future.c" + "common/osi/hash_functions.c" + "common/osi/hash_map.c" + "common/osi/list.c" + "common/osi/mutex.c" + "common/osi/osi.c" + "common/osi/semaphore.c" + "bluedroid/api/esp_a2dp_api.c" "bluedroid/api/esp_avrc_api.c" "bluedroid/api/esp_blufi_api.c" "bluedroid/api/esp_bt_device.c" @@ -109,18 +128,15 @@ if(CONFIG_BT_ENABLED) "bluedroid/bta/sys/bta_sys_conn.c" "bluedroid/bta/sys/bta_sys_main.c" "bluedroid/bta/sys/utl.c" - "bluedroid/btc/core/btc_alarm.c" "bluedroid/btc/core/btc_ble_storage.c" "bluedroid/btc/core/btc_config.c" "bluedroid/btc/core/btc_dev.c" "bluedroid/btc/core/btc_dm.c" "bluedroid/btc/core/btc_main.c" - "bluedroid/btc/core/btc_manage.c" "bluedroid/btc/core/btc_profile_queue.c" "bluedroid/btc/core/btc_sec.c" "bluedroid/btc/core/btc_sm.c" "bluedroid/btc/core/btc_storage.c" - "bluedroid/btc/core/btc_task.c" "bluedroid/btc/core/btc_util.c" "bluedroid/btc/profile/esp/blufi/blufi_prf.c" "bluedroid/btc/profile/esp/blufi/blufi_protocol.c" @@ -173,18 +189,6 @@ if(CONFIG_BT_ENABLED) "bluedroid/hci/packet_fragmenter.c" "bluedroid/main/bte_init.c" "bluedroid/main/bte_main.c" - "bluedroid/osi/alarm.c" - "bluedroid/osi/allocator.c" - "bluedroid/osi/buffer.c" - "bluedroid/osi/config.c" - "bluedroid/osi/fixed_queue.c" - "bluedroid/osi/future.c" - "bluedroid/osi/hash_functions.c" - "bluedroid/osi/hash_map.c" - "bluedroid/osi/list.c" - "bluedroid/osi/mutex.c" - "bluedroid/osi/osi.c" - "bluedroid/osi/semaphore.c" "bluedroid/stack/a2dp/a2d_api.c" "bluedroid/stack/a2dp/a2d_sbc.c" "bluedroid/stack/avct/avct_api.c" @@ -279,11 +283,125 @@ if(CONFIG_BT_ENABLED) "bluedroid/stack/smp/smp_l2c.c" "bluedroid/stack/smp/smp_main.c" "bluedroid/stack/smp/smp_utils.c") + + if(CONFIG_BLE_MESH) + list(APPEND COMPONENT_SRCS "esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c") + endif() + + endif() + + if(CONFIG_BLE_MESH) + list(APPEND COMPONENT_ADD_INCLUDEDIRS + "esp_ble_mesh/mesh_common/include" + "esp_ble_mesh/mesh_core" + "esp_ble_mesh/mesh_core/include" + "esp_ble_mesh/mesh_core/storage" + "esp_ble_mesh/btc/include" + "esp_ble_mesh/mesh_models/common/include" + "esp_ble_mesh/mesh_models/client/include" + "esp_ble_mesh/mesh_models/server/include" + "esp_ble_mesh/api/core/include" + "esp_ble_mesh/api/models/include" + "esp_ble_mesh/api") + + list(APPEND COMPONENT_SRCS + "esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_common_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c" + "esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c" + "esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c" + "esp_ble_mesh/btc/btc_ble_mesh_config_model.c" + "esp_ble_mesh/btc/btc_ble_mesh_generic_model.c" + "esp_ble_mesh/btc/btc_ble_mesh_health_model.c" + "esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c" + "esp_ble_mesh/btc/btc_ble_mesh_prov.c" + "esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c" + "esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c" + "esp_ble_mesh/mesh_common/mesh_aes_encrypt.c" + "esp_ble_mesh/mesh_common/mesh_atomic.c" + "esp_ble_mesh/mesh_common/mesh_buf.c" + "esp_ble_mesh/mesh_common/mesh_common.c" + "esp_ble_mesh/mesh_common/mesh_kernel.c" + "esp_ble_mesh/mesh_common/mesh_mutex.c" + "esp_ble_mesh/mesh_common/mesh_timer.c" + "esp_ble_mesh/mesh_common/mesh_util.c" + "esp_ble_mesh/mesh_core/storage/settings_nvs.c" + "esp_ble_mesh/mesh_core/access.c" + "esp_ble_mesh/mesh_core/adv.c" + "esp_ble_mesh/mesh_core/beacon.c" + "esp_ble_mesh/mesh_core/cfg_cli.c" + "esp_ble_mesh/mesh_core/cfg_srv.c" + "esp_ble_mesh/mesh_core/crypto.c" + "esp_ble_mesh/mesh_core/friend.c" + "esp_ble_mesh/mesh_core/health_cli.c" + "esp_ble_mesh/mesh_core/health_srv.c" + "esp_ble_mesh/mesh_core/local_operation.c" + "esp_ble_mesh/mesh_core/lpn.c" + "esp_ble_mesh/mesh_core/main.c" + "esp_ble_mesh/mesh_core/net.c" + "esp_ble_mesh/mesh_core/prov.c" + "esp_ble_mesh/mesh_core/provisioner_main.c" + "esp_ble_mesh/mesh_core/provisioner_prov.c" + "esp_ble_mesh/mesh_core/proxy_client.c" + "esp_ble_mesh/mesh_core/proxy_server.c" + "esp_ble_mesh/mesh_core/settings.c" + "esp_ble_mesh/mesh_core/test.c" + "esp_ble_mesh/mesh_core/transport.c" + "esp_ble_mesh/mesh_models/client/client_common.c" + "esp_ble_mesh/mesh_models/client/generic_client.c" + "esp_ble_mesh/mesh_models/client/lighting_client.c" + "esp_ble_mesh/mesh_models/client/sensor_client.c" + "esp_ble_mesh/mesh_models/client/time_scene_client.c" + "esp_ble_mesh/mesh_models/server/device_property.c" + "esp_ble_mesh/mesh_models/server/generic_server.c" + "esp_ble_mesh/mesh_models/server/lighting_server.c" + "esp_ble_mesh/mesh_models/server/sensor_server.c" + "esp_ble_mesh/mesh_models/server/server_common.c" + "esp_ble_mesh/mesh_models/server/state_binding.c" + "esp_ble_mesh/mesh_models/server/state_transition.c" + "esp_ble_mesh/mesh_models/server/time_scene_server.c") + endif() + + if(CONFIG_NIMBLE_ENABLED) + + if (CONFIG_BLE_MESH) + list(APPEND COMPONENT_PRIV_INCLUDEDIRS + common/btc/include + common/include) + + list(APPEND COMPONENT_ADD_INCLUDEDIRS common/osi/include) + + list(APPEND COMPONENT_SRCS "esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c" + "common/btc/core/btc_alarm.c" + "common/btc/core/btc_manage.c" + "common/btc/core/btc_task.c" + "common/osi/alarm.c" + "common/osi/allocator.c" + "common/osi/buffer.c" + "common/osi/config.c" + "common/osi/fixed_queue.c" + "common/osi/future.c" + "common/osi/hash_functions.c" + "common/osi/hash_map.c" + "common/osi/list.c" + "common/osi/mutex.c" + "common/osi/osi.c" + "common/osi/semaphore.c") + endif() + endif() endif() # requirements can't depend on config -set(COMPONENT_PRIV_REQUIRES nvs_flash) +set(COMPONENT_PRIV_REQUIRES nvs_flash nimble) register_component() diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 0bb8ad47d..a0f63c84e 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -53,6 +53,43 @@ menu Bluetooth BR/EDR Synchronize maximum connections of bluetooth controller. Each connection uses 2KB static DRAM whenever the BT controller is enabled. + choice BTDM_CTRL_BR_EDR_SCO_DATA_PATH + prompt "BR/EDR Sync(SCO/eSCO) default data path" + depends on BTDM_CTRL_MODE_BR_EDR_ONLY || BTDM_CTRL_MODE_BTDM + default BTDM_CTRL_BR_EDR_SCO_DATA_PATH_PCM + help + SCO data path, i.e. HCI or PCM. + SCO data can be sent/received through HCI synchronous packets, or the data + can be routed to on-chip PCM module on ESP32. PCM input/output signals can + be "matrixed" to GPIOs. The default data path can also be set using API + "esp_bredr_sco_datapath_set" + + config BTDM_CTRL_BR_EDR_SCO_DATA_PATH_HCI + bool "HCI" + config BTDM_CTRL_BR_EDR_SCO_DATA_PATH_PCM + bool "PCM" + endchoice + + config BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF + int + default 0 if BTDM_CTRL_BR_EDR_SCO_DATA_PATH_HCI + default 1 if BTDM_CTRL_BR_EDR_SCO_DATA_PATH_PCM + default 0 + + config BTDM_CTRL_AUTO_LATENCY + bool "Auto latency" + depends on BTDM_CONTROLLER_MODE_BTDM + default n + help + BLE auto latency, used to enhance classic BT performance + while classic BT and BLE are enabled at the same time. + + config BTDM_CTRL_AUTO_LATENCY_EFF + bool + default BTDM_CTRL_AUTO_LATENCY if BTDM_CONTROLLER_MODE_BTDM + default n + + config BTDM_CONTROLLER_BLE_MAX_CONN_EFF int default BTDM_CONTROLLER_BLE_MAX_CONN if BTDM_CONTROLLER_MODE_BLE_ONLY || BTDM_CONTROLLER_MODE_BTDM @@ -252,8 +289,8 @@ menu Bluetooth config BTDM_CONTROLLER_FULL_SCAN_SUPPORTED bool "BLE full scan feature supported" - depends on BTDM_CONTROLLER_MODE_BLE_ONLY - default n + depends on BTDM_CONTROLLER_MODE_BLE_ONLY || BTDM_CONTROLLER_MODE_BTDM + default y help The full scan function is mainly used to provide BLE scan performance. This is required for scenes with high scan performance requirements, such as BLE Mesh scenes. @@ -292,6 +329,21 @@ menu Bluetooth If you set `BLE_ADV_REPORT_DISCARD_THRSHOLD` to a small value or printf every adv lost event, it may cause adv packets lost more. + menuconfig BTDM_COEX_BT_OPTIONS + bool "Coexistence Bluetooth Side Options" + depends on SW_COEXIST_ENABLE + default n + help + Options of Bluetooth Side of WiFi and bluetooth coexistence. + + config BTDM_COEX_BLE_ADV_HIGH_PRIORITY + bool "Improve BLE ADV priority for WiFi & BLE coexistence" + depends on BTDM_COEX_BT_OPTIONS + default n + help + Improve BLE ADV coexistence priority to make it better performance. + For example, BLE mesh need to enable this option to improve BLE adv performance. + endmenu menuconfig BLUEDROID_ENABLED @@ -390,15 +442,15 @@ menu Bluetooth choice HFP_AUDIO_DATA_PATH prompt "audio(SCO) data path" depends on HFP_ENABLE + help + SCO data path, i.e. HCI or PCM. This option is set using API + "esp_bredr_sco_datapath_set" in Bluetooth host. Default SCO data + path can also be set in Bluetooth Controller. config HFP_AUDIO_DATA_PATH_PCM bool "PCM" - help - This enables the Serial Port Profile config HFP_AUDIO_DATA_PATH_HCI bool "HCI" - help - This enables the Serial Port Profile endchoice config BT_SSP_ENABLED @@ -1278,4 +1330,1393 @@ menu Bluetooth default 0xdb5c if BT_ENABLED default 0 + + menuconfig NIMBLE_ENABLED + bool "Enable NimBLE host stack" + depends on BTDM_CONTROLLER_HCI_MODE_VHCI && !BLUEDROID_ENABLED + default n + help + This enables NimBLE host stack + + choice NIMBLE_MEM_ALLOC_MODE + prompt "Memory allocation strategy" + default NIMBLE_MEM_ALLOC_MODE_INTERNAL + depends on NIMBLE_ENABLED + help + Allocation strategy for NimBLE host stack, essentially provides ability to + allocate all required dynamic allocations from, + + - Internal DRAM memory only + - External SPIRAM memory only + - Either internal or external memory based on default malloc() + behavior in ESP-IDF + + Recommended mode here is always internal, since that is most preferred + from security perspective. But if application requirement does not + allow sufficient free internal memory then alternate mode can be + selected. + + config NIMBLE_MEM_ALLOC_MODE_INTERNAL + bool "Internal memory" + + config NIMBLE_MEM_ALLOC_MODE_EXTERNAL + bool "External SPIRAM" + depends on ESP32_SPIRAM_SUPPORT + + config NIMBLE_MEM_ALLOC_MODE_DEFAULT + bool "Default alloc mode" + + endchoice + + config NIMBLE_MAX_CONNECTIONS + int "Maximum number of concurrent connections" + range 1 9 + default BTDM_CONTROLLER_BLE_MAX_CONN + depends on NIMBLE_ENABLED + help + Defines maximum number of concurrent BLE connections + + config NIMBLE_MAX_BONDS + int "Maximum number of bonds to save across reboots" + default 3 + depends on NIMBLE_ENABLED + help + Defines maximum number of bonds to save for peer security and our security + + config NIMBLE_MAX_CCCDS + int "Maximum number of CCC descriptors to save across reboots" + default 8 + depends on NIMBLE_ENABLED + help + Defines maximum number of CCC descriptors to save + + config NIMBLE_L2CAP_COC_MAX_NUM + int "Maximum number of connection oriented channels" + range 0 9 + depends on NIMBLE_ENABLED + default 0 + help + Defines maximum number of BLE Connection Oriented Channels. When set to (0), BLE COC is not compiled in + + choice NIMBLE_PINNED_TO_CORE_CHOICE + prompt "The CPU core on which NimBLE host will run" + depends on NIMBLE_ENABLED && !FREERTOS_UNICORE + help + The CPU core on which NimBLE host will run. You can choose Core 0 or Core 1. + Cannot specify no-affinity + + config NIMBLE_PINNED_TO_CORE_0 + bool "Core 0 (PRO CPU)" + config NIMBLE_PINNED_TO_CORE_1 + bool "Core 1 (APP CPU)" + depends on !FREERTOS_UNICORE + endchoice + + config NIMBLE_PINNED_TO_CORE + int + depends on NIMBLE_ENABLED + default 0 if NIMBLE_PINNED_TO_CORE_0 + default 1 if NIMBLE_PINNED_TO_CORE_1 + default 0 + + config NIMBLE_TASK_STACK_SIZE + int "NimBLE Host task stack size" + depends on NIMBLE_ENABLED + default 5120 if BLE_MESH + default 4096 + help + This configures stack size of NimBLE host task + + config NIMBLE_ROLE_CENTRAL + bool "Enable BLE Central role" + depends on NIMBLE_ENABLED + default y + + config NIMBLE_ROLE_PERIPHERAL + bool "Enable BLE Peripheral role" + depends on NIMBLE_ENABLED + default y + + config NIMBLE_ROLE_BROADCASTER + bool "Enable BLE Broadcaster role" + depends on NIMBLE_ENABLED + default y + + config NIMBLE_ROLE_OBSERVER + bool "Enable BLE Observer role" + depends on NIMBLE_ENABLED + default y + + config NIMBLE_NVS_PERSIST + bool "Persist the BLE Bonding keys in NVS" + depends on NIMBLE_ENABLED + default y + help + Enable this flag to make bonding persistent across device reboots + + config NIMBLE_SM_LEGACY + bool "Security manager legacy pairing" + depends on NIMBLE_ENABLED + default y + help + Enable security manager legacy pairing + + config NIMBLE_SM_SC + bool "Security manager secure connections (4.2)" + depends on NIMBLE_ENABLED + default y + help + Enable security manager secure connections + + config NIMBLE_DEBUG + bool "Enable extra runtime asserts and host debugging" + default n + depends on NIMBLE_ENABLED + help + This enables extra runtime asserts and host debugging + + config NIMBLE_SM_SC_DEBUG_KEYS + bool "Use predefined public-private key pair" + default n + depends on NIMBLE_ENABLED && NIMBLE_SM_SC + help + If this option is enabled, SM uses predefined DH key pair as described + in Core Specification, Vol. 3, Part H, 2.3.5.6.1. This allows to + decrypt air traffic easily and thus should only be used for debugging. + + config NIMBLE_SVC_GAP_DEVICE_NAME + string "BLE GAP default device name" + depends on NIMBLE_ENABLED + default "nimble" + help + The Device Name characteristic shall contain the name of the device as an UTF-8 string. + This name can be changed by using API ble_svc_gap_device_name_set() + + config NIMBLE_GAP_DEVICE_NAME_MAX_LEN + int "Maximum length of BLE device name in octets" + depends on NIMBLE_ENABLED + default 31 + help + Device Name characteristic value shall be 0 to 248 octets in length + + config NIMBLE_ATT_PREFERRED_MTU + int "Preferred MTU size in octets" + depends on NIMBLE_ENABLED + default 256 + help + This is the default value of ATT MTU indicated by the device during an ATT MTU exchange. + This value can be changed using API ble_att_set_preferred_mtu() + + config NIMBLE_SVC_GAP_APPEARANCE + hex "External appearance of the device" + depends on NIMBLE_ENABLED + default 0 + help + Standard BLE GAP Appearance value in HEX format e.g. 0x02C0 + + config NIMBLE_ACL_BUF_COUNT + int "ACL Buffer count" + depends on NIMBLE_ENABLED + default 12 + help + The number of ACL data buffers. + + config NIMBLE_ACL_BUF_SIZE + int "ACL Buffer size" + depends on NIMBLE_ENABLED + default 255 + help + This is the maximum size of the data portion of HCI ACL data packets. + It does not include the HCI data header (of 4 bytes) + + config NIMBLE_HCI_EVT_BUF_SIZE + int "HCI Event Buffer size" + depends on NIMBLE_ENABLED + default 70 + help + This is the size of each HCI event buffer in bytes + + config NIMBLE_HCI_EVT_HI_BUF_COUNT + int "High Priority HCI Event Buffer count" + depends on NIMBLE_ENABLED + default 30 + help + This is the high priority HCI events' buffer size. High-priority + event buffers are for everything except advertising reports. If there + are no free high-priority event buffers then host will try to allocate a + low-priority buffer instead + + config NIMBLE_HCI_EVT_LO_BUF_COUNT + int "Low Priority HCI Event Buffer count" + depends on NIMBLE_ENABLED + default 8 + help + This is the low priority HCI events' buffer size. Low-priority event + buffers are only used for advertising reports. If there are no free + low-priority event buffers, then an incoming advertising report will + get dropped + + config NIMBLE_MSYS1_BLOCK_COUNT + int "MSYS_1 Block Count" + depends on NIMBLE_ENABLED + default 12 + help + MSYS is a system level mbuf registry. For prepare write & prepare + responses MBUFs are allocated out of msys_1 pool. For NIMBLE_MESH + enabled cases, this block count is increased by 8 than user defined + count. + + config NIMBLE_HS_FLOW_CTRL + bool "Enable Host Flow control" + depends on NIMBLE_ENABLED + default y + help + Enable Host Flow control + + config NIMBLE_HS_FLOW_CTRL_ITVL + int "Host Flow control interval" + depends on NIMBLE_HS_FLOW_CTRL + default 1000 + help + Host flow control interval in msecs + + config NIMBLE_HS_FLOW_CTRL_THRESH + int "Host Flow control threshold" + depends on NIMBLE_HS_FLOW_CTRL + default 2 + help + Host flow control threshold, if the number of free buffers are at or + below this threshold, send an immediate number-of-completed-packets + event + + config NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT + bool "Host Flow control on disconnect" + depends on NIMBLE_HS_FLOW_CTRL + default y + help + Enable this option to send number-of-completed-packets event to + controller after disconnection + + config NIMBLE_RPA_TIMEOUT + int "RPA timeout in seconds" + range 1 41400 + depends on NIMBLE_ENABLED + default 900 + help + Time interval between RPA address change. This is applicable in case of + Host based RPA + + menuconfig NIMBLE_MESH + bool "Enable BLE mesh functionality" + select NIMBLE_SM_SC + depends on NIMBLE_ENABLED + default n + help + Enable BLE Mesh functionality + + config NIMBLE_MESH_PROXY + bool "Enable mesh proxy functionality" + default n + depends on NIMBLE_MESH + help + Enable proxy. This is automatically set whenever NIMBLE_MESH_PB_GATT or + NIMBLE_MESH_GATT_PROXY is set + + + config NIMBLE_MESH_PROV + bool "Enable BLE mesh provisioning" + default y + depends on NIMBLE_MESH + help + Enable mesh provisioning + + config NIMBLE_MESH_PB_ADV + bool "Enable mesh provisioning over advertising bearer" + default y + depends on NIMBLE_MESH_PROV + help + Enable this option to allow the device to be provisioned over + the advertising bearer + + + config NIMBLE_MESH_PB_GATT + bool "Enable mesh provisioning over GATT bearer" + default y + select NIMBLE_MESH_PROXY + depends on NIMBLE_MESH_PROV + help + Enable this option to allow the device to be provisioned over the GATT + bearer + + config NIMBLE_MESH_GATT_PROXY + bool "Enable GATT Proxy functionality" + default y + select NIMBLE_MESH_PROXY + depends on NIMBLE_MESH + help + This option enables support for the Mesh GATT Proxy Service, + i.e. the ability to act as a proxy between a Mesh GATT Client + and a Mesh network + + config NIMBLE_MESH_RELAY + bool "Enable mesh relay functionality" + default n + depends on NIMBLE_MESH + help + Support for acting as a Mesh Relay Node + + config NIMBLE_MESH_LOW_POWER + bool "Enable mesh low power mode" + default n + depends on NIMBLE_MESH + help + Enable this option to be able to act as a Low Power Node + + config NIMBLE_MESH_FRIEND + bool "Enable mesh friend functionality" + default n + depends on NIMBLE_MESH + help + Enable this option to be able to act as a Friend Node + + config NIMBLE_MESH_DEVICE_NAME + string "Set mesh device name" + default "nimble-mesh-node" + depends on NIMBLE_MESH + help + This value defines Bluetooth Mesh device/node name + + config NIMBLE_CRYPTO_STACK_MBEDTLS + bool "Override TinyCrypt with mbedTLS for crypto computations" + default y + depends on NIMBLE_ENABLED + select MBEDTLS_ECP_RESTARTABLE + select MBEDTLS_CMAC_C + help + Enable this option to choose mbedTLS instead of TinyCrypt for crypto + computations. + endmenu + +menuconfig BLE_MESH + bool "ESP BLE Mesh Support" + help + This option enables ESP BLE Mesh support. The specific features that are + available may depend on other features that have been enabled in the + stack, such as Bluetooth Support, Bluedroid Support & GATT support. + +if BLE_MESH + + config BLE_MESH_HCI_5_0 + bool "Support sending 20ms non-connectable adv packets" + default y + help + It is a temporary solution and needs further modifications. + + config BLE_MESH_USE_DUPLICATE_SCAN + bool "Support Duplicate Scan in BLE Mesh" + depends on BLUEDROID_ENABLED + select BLE_SCAN_DUPLICATE + select BLE_MESH_SCAN_DUPLICATE_EN + default y + help + Enable this option to allow using specific duplicate scan filter + in BLE Mesh, and Scan Duplicate Type must be set by choosing the + option in the Bluetooth Controller section in menuconfig, which is + "Scan Duplicate By Device Address and Advertising Data". + + config BLE_MESH_ALLOC_FROM_PSRAM_FIRST + bool "BLE Mesh will first allocate memory from PSRAM" + default n + help + When this option is enabled, BLE Mesh stack will try to allocate memory + from PSRAM firstly. This will save the internal RAM if PSRAM exists. + + config BLE_MESH_FAST_PROV + bool "Enable BLE Mesh Fast Provisioning" + select BLE_MESH_NODE + select BLE_MESH_PROVISIONER + select BLE_MESH_PB_ADV + default n + help + Enable this option to allow BLE Mesh fast provisioning solution to be used. + When there are multiple unprovisioned devices around, fast provisioning can + greatly reduce the time consumption of the whole provisioning process. + When this option is enabled, and after an unprovisioned device is provisioned + into a node successfully, it can be changed to a temporary Provisioner. + + config BLE_MESH_NODE + bool "Support for BLE Mesh Node" + help + Enable the device to be provisioned into a node. This option should be + enabled when an unprovisioned device is going to be provisioned into a + node and communicate with other nodes in the BLE Mesh network. + + config BLE_MESH_PROVISIONER + bool "Support for BLE Mesh Provisioner" + help + Enable the device to be a Provisioner. The option should be enabled when + a device is going to act as a Provisioner and provision unprovisioned + devices into the BLE Mesh network. + + if BLE_MESH_PROVISIONER + + config BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM + int "Maximum number of unprovisioned devices that can be added to device queue" + default 10 + range 1 100 + help + This option specifies how many unprovisioned devices can be added to device + queue for provisioning. Users can use this option to define the size of the + queue in the bottom layer which is used to store unprovisioned device + information (e.g. Device UUID, address). + + config BLE_MESH_MAX_PROV_NODES + int "Maximum number of devices that can be provisioned by Provisioner" + default 10 + range 1 1000 + help + This option specifies how many devices can be provisioned by a Provisioner. + This value indicates the maximum number of unprovisioned devices which can be + provisioned by a Provisioner. For instance, if the value is 6 then it means the + Provisioner can provision up to 6 unprovisioned devices. + Theoretically a Provisioner without the limitation of its memory can provision + up to 32766 unprovisioned devices, here we limit the maximum number to 100 + just to limit the memory used by a Provisioner. The bigger the value is, the + more memory it will cost by a Provisioner to store the information of nodes. + + if BLE_MESH_PB_ADV + config BLE_MESH_PBA_SAME_TIME + int "Maximum number of PB-ADV running at the same time by Provisioner" + default 2 + range 1 10 + help + This option specifies how many devices can be provisioned at the same time + using PB-ADV. For examples, if the value is 2, it means a Provisioner can + provision two unprovisioned devices with PB-ADV at the same time. + + endif # BLE_MESH_PB_ADV + + if BLE_MESH_PB_GATT + config BLE_MESH_PBG_SAME_TIME + int "Maximum number of PB-GATT running at the same time by Provisioner" + default 1 + range 1 5 + help + This option specifies how many devices can be provisioned at the same + time using PB-GATT. For example, if the value is 2, it means a Provisioner + can provision two unprovisioned devices with PB-GATT at the same time. + + endif # BLE_MESH_PB_GATT + + config BLE_MESH_PROVISIONER_SUBNET_COUNT + int "Maximum number of mesh subnets that can be created by Provisioner" + default 3 + range 1 4096 + help + This option specifies how many subnets per network a Provisioner can create. + Indeed, this value decides the number of network keys which can be added by a Provisioner. + + config BLE_MESH_PROVISIONER_APP_KEY_COUNT + int "Maximum number of application keys that can be owned by Provisioner" + default 3 + range 1 4096 + help + This option specifies how many application keys the Provisioner can have. + Indeed, this value decides the number of the application keys which can be added by a Provisioner. + + endif # BLE_MESH_PROVISIONER + + # Virtual option enabled whenever Generic Provisioning layer is needed + config BLE_MESH_PROV + bool "BLE Mesh Provisioning support" + default y + help + Enable this option to support BLE Mesh Provisioning functionality. For + BLE Mesh, this option should be always enabled. + + config BLE_MESH_PB_ADV + bool "Provisioning support using the advertising bearer (PB-ADV)" + select BLE_MESH_PROV + default y + help + Enable this option to allow the device to be provisioned over the + advertising bearer. This option should be enabled if PB-ADV is + going to be used during provisioning procedure. + + config BLE_MESH_PB_GATT + bool "Provisioning support using GATT (PB-GATT)" + select BLE_MESH_PROXY + select BLE_MESH_PROV + help + Enable this option to allow the device to be provisioned over GATT. + This option should be enabled if PB-GATT is going to be used during + provisioning procedure. + + # Virtual option enabled whenever any Proxy protocol is needed + config BLE_MESH_PROXY + bool "BLE Mesh Proxy protocol support" + default y + help + Enable this option to support BLE Mesh Proxy protocol used by PB-GATT + and other proxy pdu transmission. + + config BLE_MESH_GATT_PROXY_SERVER + bool "BLE Mesh GATT Proxy Server" + select BLE_MESH_PROXY + depends on BLE_MESH_NODE + default y + help + This option enables support for Mesh GATT Proxy Service, i.e. the + ability to act as a proxy between a Mesh GATT Client and a Mesh network. + This option should be enabled if a node is going to be a Proxy Server. + + config BLE_MESH_NODE_ID_TIMEOUT + int "Node Identity advertising timeout" + depends on BLE_MESH_GATT_PROXY_SERVER + range 1 60 + default 60 + help + This option determines for how long the local node advertises using + Node Identity. The given value is in seconds. The specification limits + this to 60 seconds and lists it as the recommended value as well. + So leaving the default value is the safest option. + When an unprovisioned device is provisioned successfully and becomes a + node, it will start to advertise using Node Identity during the time + set by this option. And after that, Network ID will be advertised. + + config BLE_MESH_PROXY_FILTER_SIZE + int "Maximum number of filter entries per Proxy Client" + depends on BLE_MESH_GATT_PROXY_SERVER + default 4 + range 1 32767 + help + This option specifies how many Proxy Filter entries the local node supports. + The entries of Proxy filter (whitelist or blacklist) are used to store a + list of addresses which can be used to decide which messages will be forwarded + to the Proxy Client by the Proxy Server. + + config BLE_MESH_GATT_PROXY_CLIENT + bool "BLE Mesh GATT Proxy Client" + select BLE_MESH_PROXY + default n + help + This option enables support for Mesh GATT Proxy Client. The Proxy Client + can use the GATT bearer to send mesh messages to a node that supports the + advertising bearer. + + config BLE_MESH_NET_BUF_POOL_USAGE + bool "BLE Mesh net buffer pool usage tracking" + default y + help + Enable BLE Mesh net buffer pool tracking. This option is used to introduce another + variable in the bottom layer to record the usage of advertising buffers of BLE Mesh + devices. Recommend to enable this option as default. + + config BLE_MESH_SETTINGS + bool "Store BLE Mesh configuration persistently" + default n + help + When selected, the BLE Mesh stack will take care of storing/restoring the BLE + Mesh configuration persistently in flash. + If the device is a BLE Mesh node, when this option is enabled, the configuration + of the device will be stored persistently, including unicast address, NetKey, + AppKey, etc. + And if the device is a BLE Mesh Provisioner, the information of the device will + be stored persistently, including the information of provisioned nodes, NetKey, + AppKey, etc. + + if BLE_MESH_SETTINGS + + config BLE_MESH_SPECIFIC_PARTITION + bool "Use a specific NVS partition for BLE Mesh" + default n + help + When selected, the mesh stack will use a specified NVS partition instead of + default NVS partition. Note that the specified partition must be registered + with NVS using nvs_flash_init_partition() API, and the partition must exists + in the csv file. + When Provisioner needs to store a large amount of nodes' information in the + flash (e.g. more than 20), this option is recommended to be enabled. + + if BLE_MESH_SPECIFIC_PARTITION + + config BLE_MESH_PARTITION_NAME + string "Name of the NVS partition for BLE Mesh" + default "ble_mesh" + help + This value defines the name of the specified NVS partition used by the + mesh stack. + + endif # BLE_MESH_SPECIFIC_PARTITION + + config BLE_MESH_STORE_TIMEOUT + int "Delay (in seconds) before storing anything persistently" + range 0 1000000 + default 0 + help + This value defines in seconds how soon any pending changes are actually + written into persistent storage (flash) after a change occurs. + The option allows nodes to delay a certain period of time to save proper + information to flash. The default value is 0, which means information + will be stored immediately once there are updates. + + config BLE_MESH_SEQ_STORE_RATE + int "How often the sequence number gets updated in storage" + range 0 1000000 + default 6 + help + This value defines how often the local sequence number gets updated in + persistent storage (i.e. flash). e.g. a value of 100 means that the + sequence number will be stored to flash on every 100th increment. + If the node sends messages very frequently a higher value makes more + sense, whereas if the node sends infrequently a value as low as 0 + (update storage for every increment) can make sense. When the stack + gets initialized it will add sequence number to the last stored one, + so that it starts off with a value that's guaranteed to be larger than + the last one used before power off. + + config BLE_MESH_RPL_STORE_TIMEOUT + int "Minimum frequency that the RPL gets updated in storage" + range 0 1000000 + default 5 + help + This value defines in seconds how soon the RPL (Replay Protection List) + gets written to persistent storage after a change occurs. If the node + receives messages frequently, then a large value is recommended. If the + node receives messages rarely, then the value can be as low as 0 (which + means the RPL is written into the storage immediately). + Note that if the node operates in a security-sensitive case, and there is + a risk of sudden power-off, then a value of 0 is strongly recommended. + Otherwise, a power loss before RPL being written into the storage may + introduce message replay attacks and system security will be in a + vulnerable state. + + config BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY + bool "A specific option for settings backward compatibility" + depends on BLE_MESH_NODE + default n + help + This option is created to solve the issue of failure in recovering + node information after mesh stack updates. In the old version mesh + stack, there is no key of "mesh/role" in nvs. In the new version + mesh stack, key of "mesh/role" is added in nvs, recovering node + information needs to check "mesh/role" key in nvs and implements + selective recovery of mesh node information. Therefore, there may + be failure in recovering node information during node restarting + after OTA. + + The new version mesh stack adds the option of "mesh/role" because + we have added the support of storing Provisioner information, while + the old version only supports storing node information. + + If users are updating their nodes from old version to new version, + we recommend enabling this option, so that system could set the flag + in advance before recovering node information and make sure the node + information recovering could work as expected. + + endif # if BLE_MESH_SETTINGS + + config BLE_MESH_SUBNET_COUNT + int "Maximum number of mesh subnets per network" + default 3 + range 1 4096 + help + This option specifies how many subnets a Mesh network can have at the same time. + Indeed, this value decides the number of the network keys which can be owned by a node. + + config BLE_MESH_APP_KEY_COUNT + int "Maximum number of application keys per network" + default 3 + range 1 4096 + help + This option specifies how many application keys the device can store per network. + Indeed, this value decides the number of the application keys which can be owned by a node. + + config BLE_MESH_MODEL_KEY_COUNT + int "Maximum number of application keys per model" + default 3 + range 1 4096 + help + This option specifies the maximum number of application keys to which each model + can be bound. + + config BLE_MESH_MODEL_GROUP_COUNT + int "Maximum number of group address subscriptions per model" + default 3 + range 1 4096 + help + This option specifies the maximum number of addresses to which each model can + be subscribed. + + config BLE_MESH_LABEL_COUNT + int "Maximum number of Label UUIDs used for Virtual Addresses" + default 3 + range 0 4096 + help + This option specifies how many Label UUIDs can be stored. + Indeed, this value decides the number of the Virtual Addresses can be supported by a node. + + config BLE_MESH_CRPL + int "Maximum capacity of the replay protection list" + default 10 + range 2 65535 + help + This option specifies the maximum capacity of the replay protection list. + It is similar to Network message cache size, but has a different purpose. + The replay protection list is used to prevent a node from replay attack, + which will store the source address and sequence number of the received + mesh messages. + For Provisioner, the replay protection list size should not be smaller than + the maximum number of nodes whose information can be stored. And the element + number of each node should also be taken into consideration. For example, if + Provisioner can provision up to 20 nodes and each node contains two elements, + then the replay protection list size of Provisioner should be at least 40. + + config BLE_MESH_MSG_CACHE_SIZE + int "Network message cache size" + default 10 + range 2 65535 + help + Number of messages that are cached for the network. This helps prevent + unnecessary decryption operations and unnecessary relays. This option + is similar to Replay protection list, but has a different purpose. + A node is not required to cache the entire Network PDU and may cache + only part of it for tracking, such as values for SRC/SEQ or others. + + config BLE_MESH_ADV_BUF_COUNT + int "Number of advertising buffers" + default 60 + range 6 256 + help + Number of advertising buffers available. The transport layer reserves + ADV_BUF_COUNT - 3 buffers for outgoing segments. The maximum outgoing + SDU size is 12 times this value (out of which 4 or 8 bytes are used + for the Transport Layer MIC). For example, 5 segments means the maximum + SDU size is 60 bytes, which leaves 56 bytes for application layer data + using a 4-byte MIC, or 52 bytes using an 8-byte MIC. + + config BLE_MESH_SUPPORT_BLE_ADV + bool "Support sending normal BLE advertising packets" + default n + help + When selected, users can send normal BLE advertising packets + with specific API. + + if BLE_MESH_SUPPORT_BLE_ADV + + config BLE_MESH_BLE_ADV_BUF_COUNT + int "Number of advertising buffers for BLE advertising packets" + default 3 + range 1 255 + help + Number of advertising buffers for BLE packets available. + + endif # BLE_MESH_SUPPORT_BLE_ADV + + config BLE_MESH_IVU_DIVIDER + int "Divider for IV Update state refresh timer" + default 4 + range 2 96 + help + When the IV Update state enters Normal operation or IV Update + in Progress, we need to keep track of how many hours has passed + in the state, since the specification requires us to remain in + the state at least for 96 hours (Update in Progress has an + additional upper limit of 144 hours). + + In order to fulfill the above requirement, even if the node might + be powered off once in a while, we need to store persistently + how many hours the node has been in the state. This doesn't + necessarily need to happen every hour (thanks to the flexible + duration range). The exact cadence will depend a lot on the + ways that the node will be used and what kind of power source it + has. + + Since there is no single optimal answer, this configuration + option allows specifying a divider, i.e. how many intervals + the 96 hour minimum gets split into. After each interval the + duration that the node has been in the current state gets + stored to flash. E.g. the default value of 4 means that the + state is saved every 24 hours (96 / 4). + + config BLE_MESH_TX_SEG_MSG_COUNT + int "Maximum number of simultaneous outgoing segmented messages" + default 1 + range 1 BLE_MESH_ADV_BUF_COUNT + help + Maximum number of simultaneous outgoing multi-segment and/or reliable messages. + The default value is 1, which means the device can only send one segmented + message at a time. And if another segmented message is going to be sent, it + should wait for the completion of the previous one. + If users are going to send multiple segmented messages at the same time, this + value should be configured properly. + + config BLE_MESH_RX_SEG_MSG_COUNT + int "Maximum number of simultaneous incoming segmented messages" + default 1 + range 1 255 + help + Maximum number of simultaneous incoming multi-segment and/or reliable messages. + The default value is 1, which means the device can only receive one segmented + message at a time. And if another segmented message is going to be received, + it should wait for the completion of the previous one. + If users are going to receive multiple segmented messages at the same time, this + value should be configured properly. + + config BLE_MESH_RX_SDU_MAX + int "Maximum incoming Upper Transport Access PDU length" + default 384 + range 36 384 + help + Maximum incoming Upper Transport Access PDU length. Leave this to the default + value, unless you really need to optimize memory usage. + + config BLE_MESH_TX_SEG_MAX + int "Maximum number of segments in outgoing messages" + default 32 + range 2 32 + help + Maximum number of segments supported for outgoing messages. + This value should typically be fine-tuned based on what + models the local node supports, i.e. what's the largest + message payload that the node needs to be able to send. + This value affects memory and call stack consumption, which + is why the default is lower than the maximum that the + specification would allow (32 segments). + + The maximum outgoing SDU size is 12 times this number (out of + which 4 or 8 bytes is used for the Transport Layer MIC). For + example, 5 segments means the maximum SDU size is 60 bytes, + which leaves 56 bytes for application layer data using a + 4-byte MIC and 52 bytes using an 8-byte MIC. + + Be sure to specify a sufficient number of advertising buffers + when setting this option to a higher value. There must be at + least three more advertising buffers (BLE_MESH_ADV_BUF_COUNT) + as there are outgoing segments. + + config BLE_MESH_RELAY + bool "Relay support" + depends on BLE_MESH_NODE + default y + help + Support for acting as a Mesh Relay Node. Enabling this option will allow + a node to support the Relay feature, and the Relay feature can still + be enabled or disabled by proper configuration messages. Disabling this + option will let a node not support the Relay feature. + + if BLE_MESH_RELAY + + config BLE_MESH_RELAY_ADV_BUF + bool "Use separate advertising buffers for relay packets" + default n + help + When selected, self-send packets will be put in a high-priority + queue and relay packets will be put in a low-priority queue. + + if BLE_MESH_RELAY_ADV_BUF + + config BLE_MESH_RELAY_ADV_BUF_COUNT + int "Number of advertising buffers for relay packets" + default 60 + range 6 256 + help + Number of advertising buffers for relay packets available. + + endif # BLE_MESH_RELAY_ADV_BUF + + endif # BLE_MESH_RELAY + + config BLE_MESH_LOW_POWER + bool "Support for Low Power features" + depends on BLE_MESH_NODE + help + Enable this option to operate as a Low Power Node. If low power consumption + is required by a node, this option should be enabled. And once the node + enters the mesh network, it will try to find a Friend node and establish a + friendship. + + if BLE_MESH_LOW_POWER + + config BLE_MESH_LPN_ESTABLISHMENT + bool "Perform Friendship establishment using low power" + default n + help + Perform the Friendship establishment using low power with the help of a + reduced scan duty cycle. The downside of this is that the node may miss + out on messages intended for it until it has successfully set up Friendship + with a Friend node. + When this option is enabled, the node will stop scanning for a period of + time after a Friend Request or Friend Poll is sent, so as to reduce more + power consumption. + + config BLE_MESH_LPN_AUTO + bool "Automatically start looking for Friend nodes once provisioned" + default n + help + Once provisioned, automatically enable LPN functionality and start looking + for Friend nodes. If this option is disabled LPN mode needs to be manually + enabled by calling bt_mesh_lpn_set(true). + When an unprovisioned device is provisioned successfully and becomes a node, + enabling this option will trigger the node starts to send Friend Request at + a certain period until it finds a proper Friend node. + + config BLE_MESH_LPN_AUTO_TIMEOUT + int "Time from last received message before going to LPN mode" + default 15 + range 0 3600 + depends on BLE_MESH_LPN_AUTO + help + Time in seconds from the last received message, that the node waits out + before starting to look for Friend nodes. + + config BLE_MESH_LPN_RETRY_TIMEOUT + int "Retry timeout for Friend requests" + default 6 + range 1 3600 + help + Time in seconds between Friend Requests, if a previous Friend Request did + not yield any acceptable Friend Offers. + + config BLE_MESH_LPN_RSSI_FACTOR + int "RSSIFactor, used in Friend Offer Delay calculation" + range 0 3 + default 0 + help + The contribution of the RSSI, measured by the Friend node, used in Friend + Offer Delay calculations. 0 = 1, 1 = 1.5, 2 = 2, 3 = 2.5. + RSSIFactor, one of the parameters carried by Friend Request sent by Low Power + node, which is used to calculate the Friend Offer Delay. + + config BLE_MESH_LPN_RECV_WIN_FACTOR + int "ReceiveWindowFactor, used in Friend Offer Delay calculation" + range 0 3 + default 0 + help + The contribution of the supported Receive Window used in Friend Offer + Delay calculations. 0 = 1, 1 = 1.5, 2 = 2, 3 = 2.5. + ReceiveWindowFactor, one of the parameters carried by Friend Request sent by + Low Power node, which is used to calculate the Friend Offer Delay. + + config BLE_MESH_LPN_MIN_QUEUE_SIZE + int "Minimum size of the acceptable friend queue (MinQueueSizeLog)" + range 1 7 + default 1 + help + The MinQueueSizeLog field is defined as log_2(N), where N is the minimum + number of maximum size Lower Transport PDUs that the Friend node can store + in its Friend Queue. As an example, MinQueueSizeLog value 1 gives N = 2, + and value 7 gives N = 128. + + config BLE_MESH_LPN_RECV_DELAY + int "Receive delay requested by the local node" + range 10 255 + default 100 + help + The ReceiveDelay is the time between the Low Power node sending a + request and listening for a response. This delay allows the Friend + node time to prepare the response. The value is in units of milliseconds. + + config BLE_MESH_LPN_POLL_TIMEOUT + int "The value of the PollTimeout timer" + range 10 244735 + default 300 + help + PollTimeout timer is used to measure time between two consecutive + requests sent by a Low Power node. If no requests are received + the Friend node before the PollTimeout timer expires, then the + friendship is considered terminated. The value is in units of 100 + milliseconds, so e.g. a value of 300 means 30 seconds. + The smaller the value, the faster the Low Power node tries to get + messages from corresponding Friend node and vice versa. + + config BLE_MESH_LPN_INIT_POLL_TIMEOUT + int "The starting value of the PollTimeout timer" + range 10 BLE_MESH_LPN_POLL_TIMEOUT + default BLE_MESH_LPN_POLL_TIMEOUT + help + The initial value of the PollTimeout timer when Friendship is to be + established for the first time. After this, the timeout gradually + grows toward the actual PollTimeout, doubling in value for each iteration. + The value is in units of 100 milliseconds, so e.g. a value of 300 means + 30 seconds. + + config BLE_MESH_LPN_SCAN_LATENCY + int "Latency for enabling scanning" + range 0 50 + default 10 + help + Latency (in milliseconds) is the time it takes to enable scanning. In + practice, it means how much time in advance of the Receive Window, the + request to enable scanning is made. + + config BLE_MESH_LPN_GROUPS + int "Number of groups the LPN can subscribe to" + range 0 16384 + default 8 + help + Maximum number of groups to which the LPN can subscribe. + + endif # BLE_MESH_LOW_POWER + + config BLE_MESH_FRIEND + bool "Support for Friend feature" + help + Enable this option to be able to act as a Friend Node. + + if BLE_MESH_FRIEND + + config BLE_MESH_FRIEND_RECV_WIN + int "Friend Receive Window" + range 1 255 + default 255 + help + Receive Window in milliseconds supported by the Friend node. + + config BLE_MESH_FRIEND_QUEUE_SIZE + int "Minimum number of buffers supported per Friend Queue" + range 2 65536 + default 16 + help + Minimum number of buffers available to be stored for each local Friend Queue. + This option decides the size of each buffer which can be used by a Friend node + to store messages for each Low Power node. + + config BLE_MESH_FRIEND_SUB_LIST_SIZE + int "Friend Subscription List Size" + range 0 1023 + default 3 + help + Size of the Subscription List that can be supported by a Friend node for a + Low Power node. And Low Power node can send Friend Subscription List Add or + Friend Subscription List Remove messages to the Friend node to add or remove + subscription addresses. + + config BLE_MESH_FRIEND_LPN_COUNT + int "Number of supported LPN nodes" + range 1 1000 + default 2 + help + Number of Low Power Nodes with which a Friend can have Friendship simultaneously. + A Friend node can have friendship with multiple Low Power nodes at the same time, + while a Low Power node can only establish friendship with only one Friend node at + the same time. + + config BLE_MESH_FRIEND_SEG_RX + int "Number of incomplete segment lists per LPN" + range 1 1000 + default 1 + help + Number of incomplete segment lists tracked for each Friends' LPN. + In other words, this determines from how many elements can segmented + messages destined for the Friend queue be received simultaneously. + + endif # BLE_MESH_FRIEND + + config BLE_MESH_NO_LOG + bool "Disable BLE Mesh debug logs (minimize bin size)" + depends on BLE_MESH + default n + help + Select this to save the BLE Mesh related rodata code size. Enabling this option + will disable the output of BLE Mesh debug log. + + menu "BLE Mesh STACK DEBUG LOG LEVEL" + depends on BLE_MESH && !BLE_MESH_NO_LOG + + choice BLE_MESH_STACK_TRACE_LEVEL + prompt "BLE_MESH_STACK" + default BLE_MESH_TRACE_LEVEL_WARNING + depends on BLE_MESH && !BLE_MESH_NO_LOG + help + Define BLE Mesh trace level for BLE Mesh stack. + + config BLE_MESH_TRACE_LEVEL_NONE + bool "NONE" + config BLE_MESH_TRACE_LEVEL_ERROR + bool "ERROR" + config BLE_MESH_TRACE_LEVEL_WARNING + bool "WARNING" + config BLE_MESH_TRACE_LEVEL_INFO + bool "INFO" + config BLE_MESH_TRACE_LEVEL_DEBUG + bool "DEBUG" + config BLE_MESH_TRACE_LEVEL_VERBOSE + bool "VERBOSE" + endchoice + + config BLE_MESH_STACK_TRACE_LEVEL + int + depends on BLE_MESH + default 0 if BLE_MESH_TRACE_LEVEL_NONE + default 1 if BLE_MESH_TRACE_LEVEL_ERROR + default 2 if BLE_MESH_TRACE_LEVEL_WARNING + default 3 if BLE_MESH_TRACE_LEVEL_INFO + default 4 if BLE_MESH_TRACE_LEVEL_DEBUG + default 5 if BLE_MESH_TRACE_LEVEL_VERBOSE + default 2 + + endmenu #BLE Mesh DEBUG LOG LEVEL + + menu "BLE Mesh NET BUF DEBUG LOG LEVEL" + depends on BLE_MESH && !BLE_MESH_NO_LOG + + choice BLE_MESH_NET_BUF_TRACE_LEVEL + prompt "BLE_MESH_NET_BUF" + default BLE_MESH_NET_BUF_TRACE_LEVEL_WARNING + depends on BLE_MESH && !BLE_MESH_NO_LOG + help + Define BLE Mesh trace level for BLE Mesh net buffer. + + config BLE_MESH_NET_BUF_TRACE_LEVEL_NONE + bool "NONE" + config BLE_MESH_NET_BUF_TRACE_LEVEL_ERROR + bool "ERROR" + config BLE_MESH_NET_BUF_TRACE_LEVEL_WARNING + bool "WARNING" + config BLE_MESH_NET_BUF_TRACE_LEVEL_INFO + bool "INFO" + config BLE_MESH_NET_BUF_TRACE_LEVEL_DEBUG + bool "DEBUG" + config BLE_MESH_NET_BUF_TRACE_LEVEL_VERBOSE + bool "VERBOSE" + endchoice + + config BLE_MESH_NET_BUF_TRACE_LEVEL + int + depends on BLE_MESH + default 0 if BLE_MESH_NET_BUF_TRACE_LEVEL_NONE + default 1 if BLE_MESH_NET_BUF_TRACE_LEVEL_ERROR + default 2 if BLE_MESH_NET_BUF_TRACE_LEVEL_WARNING + default 3 if BLE_MESH_NET_BUF_TRACE_LEVEL_INFO + default 4 if BLE_MESH_NET_BUF_TRACE_LEVEL_DEBUG + default 5 if BLE_MESH_NET_BUF_TRACE_LEVEL_VERBOSE + default 2 + + endmenu #BLE Mesh NET BUF DEBUG LOG LEVEL + + config BLE_MESH_CLIENT_MSG_TIMEOUT + int "Timeout(ms) for client message response" + range 100 1200000 + default 4000 + help + Timeout value used by the node to get response of the acknowledged + message which is sent by the client model. + This value indicates the maximum time that a client model waits for + the response of the sent acknowledged messages. If a client model + uses 0 as the timeout value when sending acknowledged messages, then + the default value will be used which is four seconds. + + menu "Support for BLE Mesh Client Models" + + config BLE_MESH_CFG_CLI + bool "Configuration Client Model" + help + Enable support for Configuration client model. + + config BLE_MESH_HEALTH_CLI + bool "Health Client Model" + help + Enable support for Health client model. + + config BLE_MESH_GENERIC_ONOFF_CLI + bool "Generic OnOff Client Model" + help + Enable support for Generic OnOff client model. + + config BLE_MESH_GENERIC_LEVEL_CLI + bool "Generic Level Client Model" + help + Enable support for Generic Level client model. + + config BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI + bool "Generic Default Transition Time Client Model" + help + Enable support for Generic Default Transition Time client model. + + config BLE_MESH_GENERIC_POWER_ONOFF_CLI + bool "Generic Power OnOff Client Model" + help + Enable support for Generic Power OnOff client model. + + config BLE_MESH_GENERIC_POWER_LEVEL_CLI + bool "Generic Power Level Client Model" + help + Enable support for Generic Power Level client model. + + config BLE_MESH_GENERIC_BATTERY_CLI + bool "Generic Battery Client Model" + help + Enable support for Generic Battery client model. + + config BLE_MESH_GENERIC_LOCATION_CLI + bool "Generic Location Client Model" + help + Enable support for Generic Location client model. + + config BLE_MESH_GENERIC_PROPERTY_CLI + bool "Generic Property Client Model" + help + Enable support for Generic Property client model. + + config BLE_MESH_SENSOR_CLI + bool "Sensor Client Model" + help + Enable support for Sensor client model. + + config BLE_MESH_TIME_CLI + bool "Time Client Model" + help + Enable support for Time client model. + + config BLE_MESH_SCENE_CLI + bool "Scene Client Model" + help + Enable support for Scene client model. + + config BLE_MESH_SCHEDULER_CLI + bool "Scheduler Client Model" + help + Enable support for Scheduler client model. + + config BLE_MESH_LIGHT_LIGHTNESS_CLI + bool "Light Lightness Client Model" + help + Enable support for Light Lightness client model. + + config BLE_MESH_LIGHT_CTL_CLI + bool "Light CTL Client Model" + help + Enable support for Light CTL client model. + + config BLE_MESH_LIGHT_HSL_CLI + bool "Light HSL Client Model" + help + Enable support for Light HSL client model. + + config BLE_MESH_LIGHT_XYL_CLI + bool "Light XYL Client Model" + help + Enable support for Light XYL client model. + + config BLE_MESH_LIGHT_LC_CLI + bool "Light LC Client Model" + help + Enable support for Light LC client model. + + endmenu + + config BLE_MESH_IV_UPDATE_TEST + bool "Test the IV Update Procedure" + default n + help + This option removes the 96 hour limit of the IV Update Procedure and + lets the state to be changed at any time. + If IV Update test mode is going to be used, this option should be enabled. + + menu "BLE Mesh specific test option" + + config BLE_MESH_SELF_TEST + bool "Perform BLE Mesh self-tests" + default n + help + This option adds extra self-tests which are run every time BLE Mesh + networking is initialized. + + if BLE_MESH_SELF_TEST + + config BLE_MESH_TEST_AUTO_ENTER_NETWORK + bool "Unprovisioned device enters mesh network automatically" + default y + help + With this option enabled, an unprovisioned device can automatically + enters mesh network using a specific test function without the pro- + visioning procedure. And on the Provisioner side, a test function + needs to be invoked to add the node information into the mesh stack. + + config BLE_MESH_TEST_USE_WHITE_LIST + bool "Use white list to filter mesh advertising packets" + default n + help + With this option enabled, users can use white list to filter mesh + advertising packets while scanning. + + endif # BLE_MESH_SELF_TEST + + config BLE_MESH_SHELL + bool "Enable BLE Mesh shell" + default n + help + Activate shell module that provides BLE Mesh commands to the console. + + config BLE_MESH_DEBUG + bool "Enable BLE Mesh debug logs" + default n + help + Enable debug logs for the BLE Mesh functionality. + + if BLE_MESH_DEBUG + + config BLE_MESH_DEBUG_NET + bool "Network layer debug" + help + Enable Network layer debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_TRANS + bool "Transport layer debug" + help + Enable Transport layer debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_BEACON + bool "Beacon debug" + help + Enable Beacon-related debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_CRYPTO + bool "Crypto debug" + help + Enable cryptographic debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_PROV + bool "Provisioning debug" + help + Enable Provisioning debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_ACCESS + bool "Access layer debug" + help + Enable Access layer debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_MODEL + bool "Foundation model debug" + help + Enable Foundation Models debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_ADV + bool "Advertising debug" + help + Enable advertising debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_LOW_POWER + bool "Low Power debug" + help + Enable Low Power debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_FRIEND + bool "Friend debug" + help + Enable Friend debug logs for the BLE Mesh functionality. + + config BLE_MESH_DEBUG_PROXY + bool "Proxy debug" + depends on BLE_MESH_PROXY + help + Enable Proxy protocol debug logs for the BLE Mesh functionality. + + endif # BLE_MESH_DEBUG + + endmenu + +endif # BLE_MESH + diff --git a/components/bt/bluedroid/api/esp_gap_bt_api.c b/components/bt/bluedroid/api/esp_gap_bt_api.c index ac0326e11..89e1df33f 100644 --- a/components/bt/bluedroid/api/esp_gap_bt_api.c +++ b/components/bt/bluedroid/api/esp_gap_bt_api.c @@ -17,6 +17,7 @@ #include "esp_bt_main.h" #include "esp_gap_bt_api.h" #include "common/bt_trace.h" +#include "bta/bta_api.h" #include "btc/btc_manage.h" #include "btc_gap_bt.h" #include "btc/btc_storage.h" diff --git a/components/bt/bluedroid/api/esp_gatt_common_api.c b/components/bt/bluedroid/api/esp_gatt_common_api.c index 1146750c7..9b532953f 100644 --- a/components/bt/bluedroid/api/esp_gatt_common_api.c +++ b/components/bt/bluedroid/api/esp_gatt_common_api.c @@ -46,4 +46,13 @@ esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu) arg.set_mtu.mtu = mtu; return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gatt_com_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); -} \ No newline at end of file +} + +#if (BLE_INCLUDED == TRUE) +extern uint16_t L2CA_GetFreePktBufferNum_LE(void); + +uint16_t esp_ble_get_sendable_packets_num () +{ + return L2CA_GetFreePktBufferNum_LE(); +} +#endif diff --git a/components/bt/bluedroid/api/esp_spp_api.c b/components/bt/bluedroid/api/esp_spp_api.c index 46878d659..05157ebdc 100644 --- a/components/bt/bluedroid/api/esp_spp_api.c +++ b/components/bt/bluedroid/api/esp_spp_api.c @@ -95,6 +95,10 @@ esp_err_t esp_spp_connect(esp_spp_sec_t sec_mask, btc_spp_args_t arg; ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED); + if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) { + LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n"); + } + msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_SPP; msg.act = BTC_SPP_ACT_CONNECT; @@ -133,6 +137,10 @@ esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask, return ESP_ERR_INVALID_ARG; } + if (sec_mask != ESP_SPP_SEC_NONE && sec_mask != ESP_SPP_SEC_AUTHORIZE && sec_mask != ESP_SPP_SEC_AUTHENTICATE) { + LOG_WARN("Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only\n"); + } + msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_SPP; msg.act = BTC_SPP_ACT_START_SRV; diff --git a/components/bt/bluedroid/api/include/api/esp_blufi_api.h b/components/bt/bluedroid/api/include/api/esp_blufi_api.h index 690518748..f17fcbc22 100644 --- a/components/bt/bluedroid/api/include/api/esp_blufi_api.h +++ b/components/bt/bluedroid/api/include/api/esp_blufi_api.h @@ -83,6 +83,7 @@ typedef enum { ESP_BLUFI_DH_PARAM_ERROR, ESP_BLUFI_READ_PARAM_ERROR, ESP_BLUFI_MAKE_PUBLIC_ERROR, + ESP_BLUFI_DATA_FORMAT_ERROR, } esp_blufi_error_state_t; /** diff --git a/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h index 44dd7c4b2..12bc48f1b 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_ble_api.h @@ -267,14 +267,25 @@ typedef enum { typedef enum { ESP_BLE_SM_PASSKEY = 0, + /* Authentication requirements of local device */ ESP_BLE_SM_AUTHEN_REQ_MODE, + /* The IO capability of local device */ ESP_BLE_SM_IOCAP_MODE, + /* Initiator Key Distribution/Generation */ ESP_BLE_SM_SET_INIT_KEY, + /* Responder Key Distribution/Generation */ ESP_BLE_SM_SET_RSP_KEY, + /* Maximum Encryption key size to support */ ESP_BLE_SM_MAX_KEY_SIZE, + /* Minimum Encryption key size requirement from Peer */ + ESP_BLE_SM_MIN_KEY_SIZE, + /* Set static Passkey */ ESP_BLE_SM_SET_STATIC_PASSKEY, + /* Reset static Passkey */ ESP_BLE_SM_CLEAR_STATIC_PASSKEY, + /* Accept only specified SMP Authentication requirement */ ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, + /* Enable/Disable OOB support */ ESP_BLE_SM_OOB_SUPPORT, ESP_BLE_SM_MAX_PARAM, } esp_ble_sm_param_t; @@ -589,7 +600,7 @@ typedef enum { typedef enum { ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_ADV_ADDR = 0, /*!< BLE advertising address , device info will be added into ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_ADDR_LIST */ ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_LINK_ID, /*!< BLE mesh link ID, it is for BLE mesh, device info will be added into ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_MESH_LINK_ID_LIST */ - ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_BEACON_TYPE, /*!< BLE mesh beacon AD type, the format is | Len | 0x2B | Beacon Type | Beacon Data | */ + ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_BEACON_TYPE, /*!< BLE mesh beacon AD type, the format is | Len | 0x2B | Beacon Type | Beacon Data | */ ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_PROV_SRV_ADV, /*!< BLE mesh provisioning service uuid, the format is | 0x02 | 0x01 | flags | 0x03 | 0x03 | 0x1827 | .... |` */ ESP_BLE_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_PROXY_SRV_ADV, /*!< BLE mesh adv with proxy service uuid, the format is | 0x02 | 0x01 | flags | 0x03 | 0x03 | 0x1828 | .... |` */ } esp_ble_duplicate_exceptional_info_type_t; diff --git a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h index f5b2c8f15..1156f10da 100644 --- a/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gap_bt_api.h @@ -361,13 +361,15 @@ esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback); esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode); /** - * @brief Start device discovery. This function should be called after esp_bluedroid_enable() completes successfully. - * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is started or halted. - * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_RES_EVT if discovery result is got. + * @brief This function starts Inquiry and Name Discovery. It should be called after esp_bluedroid_enable() completes successfully. + * When Inquiry is halted and cached results do not contain device name, then Name Discovery will connect to the peer target to get the device name. + * esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT when Inquriry is started or Name Discovery is completed. + * esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_RES_EVT each time the two types of discovery results are got. * - * @param[in] mode - inquiry mode - * @param[in] inq_len - inquiry duration in 1.28 sec units, ranging from 0x01 to 0x30 - * @param[in] num_rsps - number of inquiry responses that can be received, value 0 indicates an unlimited number of responses + * @param[in] mode - Inquiry mode + * @param[in] inq_len - Inquiry duration in 1.28 sec units, ranging from 0x01 to 0x30. This parameter only specifies the total duration of the Inquiry process, + * - when this time expires, Inquiry will be halted. + * @param[in] num_rsps - Number of responses that can be received before the Inquiry is halted, value 0 indicates an unlimited number of responses. * * @return * - ESP_OK : Succeed @@ -378,8 +380,9 @@ esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode); esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, uint8_t num_rsps); /** - * @brief Cancel device discovery. This function should be called after esp_bluedroid_enable() completes successfully - * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is stopped. + * @brief Cancel Inquiry and Name Discovery. This function should be called after esp_bluedroid_enable() completes successfully. + * esp_bt_gap_cb_t will be called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if Inquiry or Name Discovery is cancelled by + * calling this function. * * @return * - ESP_OK : Succeed diff --git a/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h b/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h index 3a9b74458..bee2c28f0 100644 --- a/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h +++ b/components/bt/bluedroid/api/include/api/esp_gatt_common_api.h @@ -44,6 +44,10 @@ extern "C" { */ extern esp_err_t esp_ble_gatt_set_local_mtu (uint16_t mtu); +#if (BLE_INCLUDED == TRUE) +extern uint16_t esp_ble_get_sendable_packets_num (void); +#endif + #ifdef __cplusplus } #endif diff --git a/components/bt/bluedroid/api/include/api/esp_gatt_defs.h b/components/bt/bluedroid/api/include/api/esp_gatt_defs.h index d6c140a14..615ea418b 100644 --- a/components/bt/bluedroid/api/include/api/esp_gatt_defs.h +++ b/components/bt/bluedroid/api/include/api/esp_gatt_defs.h @@ -34,7 +34,7 @@ extern "C" { * All "ESP_GATT_UUID_xxx" is attribute types */ #define ESP_GATT_UUID_IMMEDIATE_ALERT_SVC 0x1802 /* Immediate alert Service*/ -#define ESP_GATT_UUID_LINK_LOSS_SVC 0x1803 /* Link Loss Service*/ +#define ESP_GATT_UUID_LINK_LOSS_SVC 0x1803 /* Link Loss Service*/ #define ESP_GATT_UUID_TX_POWER_SVC 0x1804 /* TX Power Service*/ #define ESP_GATT_UUID_CURRENT_TIME_SVC 0x1805 /* Current Time Service Service*/ #define ESP_GATT_UUID_REF_TIME_UPDATE_SVC 0x1806 /* Reference Time Update Service*/ @@ -68,8 +68,14 @@ extern "C" { #define ESP_GATT_UUID_CHAR_PRESENT_FORMAT 0x2904 /* Characteristic Presentation Format*/ #define ESP_GATT_UUID_CHAR_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/ #define ESP_GATT_UUID_CHAR_VALID_RANGE 0x2906 /* Characteristic Valid Range */ -#define ESP_GATT_UUID_EXT_RPT_REF_DESCR 0x2907 -#define ESP_GATT_UUID_RPT_REF_DESCR 0x2908 +#define ESP_GATT_UUID_EXT_RPT_REF_DESCR 0x2907 /* External Report Reference */ +#define ESP_GATT_UUID_RPT_REF_DESCR 0x2908 /* Report Reference */ +#define ESP_GATT_UUID_NUM_DIGITALS_DESCR 0x2909 /* Number of Digitals */ +#define ESP_GATT_UUID_VALUE_TRIGGER_DESCR 0x290A /* Value Trigger Setting */ +#define ESP_GATT_UUID_ENV_SENSING_CONFIG_DESCR 0x290B /* Environmental Sensing Configuration */ +#define ESP_GATT_UUID_ENV_SENSING_MEASUREMENT_DESCR 0x290C /* Environmental Sensing Measurement */ +#define ESP_GATT_UUID_ENV_SENSING_TRIGGER_DESCR 0x290D /* Environmental Sensing Trigger Setting */ +#define ESP_GATT_UUID_TIME_TRIGGER_DESCR 0x290E /* Time Trigger Setting */ /* GAP Profile Attributes */ #define ESP_GATT_UUID_GAP_DEVICE_NAME 0x2A00 @@ -299,7 +305,7 @@ typedef enum { * @brief Attribute description (used to create database) */ typedef struct - { + { uint16_t uuid_length; /*!< UUID length */ uint8_t *uuid_p; /*!< UUID value */ uint16_t perm; /*!< Attribute permission */ @@ -348,23 +354,23 @@ typedef struct /** * @brief Gatt include service entry element */ -typedef struct +typedef struct { - uint16_t start_hdl; /*!< Gatt start handle value of included service */ - uint16_t end_hdl; /*!< Gatt end handle value of included service */ - uint16_t uuid; /*!< Gatt attribute value UUID of included service */ + uint16_t start_hdl; /*!< Gatt start handle value of included service */ + uint16_t end_hdl; /*!< Gatt end handle value of included service */ + uint16_t uuid; /*!< Gatt attribute value UUID of included service */ } esp_gatts_incl_svc_desc_t; /*!< Gatt include service entry element */ /** * @brief Gatt include 128 bit service entry element */ -typedef struct +typedef struct { - uint16_t start_hdl; /*!< Gatt start handle value of included 128 bit service */ - uint16_t end_hdl; /*!< Gatt end handle value of included 128 bit service */ -} esp_gatts_incl128_svc_desc_t; /*!< Gatt include 128 bit service entry element */ + uint16_t start_hdl; /*!< Gatt start handle value of included 128 bit service */ + uint16_t end_hdl; /*!< Gatt end handle value of included 128 bit service */ +} esp_gatts_incl128_svc_desc_t; /*!< Gatt include 128 bit service entry element */ -/// Gatt attribute value +/// Gatt attribute value typedef struct { uint8_t value[ESP_GATT_MAX_ATTR_LEN]; /*!< Gatt attribute value */ uint16_t handle; /*!< Gatt attribute handle */ @@ -426,8 +432,8 @@ typedef struct { /** * @brief service element */ -typedef struct { - bool is_primary; /*!< The service flag, true if the service is primary service, else is secondly service */ +typedef struct { + bool is_primary; /*!< The service flag, true if the service is primary service, else is secondary service */ uint16_t start_handle; /*!< The start handle of the service */ uint16_t end_handle; /*!< The end handle of the service */ esp_bt_uuid_t uuid; /*!< The uuid of the service */ diff --git a/components/bt/bluedroid/api/include/api/esp_spp_api.h b/components/bt/bluedroid/api/include/api/esp_spp_api.h index 31bcf1c68..9628a1ed0 100644 --- a/components/bt/bluedroid/api/include/api/esp_spp_api.h +++ b/components/bt/bluedroid/api/include/api/esp_spp_api.h @@ -30,7 +30,7 @@ typedef enum { ESP_SPP_NO_RESOURCE /*!< No more set pm control block */ } esp_spp_status_t; -/* Security Setting Mask */ +/* Security Setting Mask, Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only.*/ #define ESP_SPP_SEC_NONE 0x0000 /*!< No security. relate to BTA_SEC_NONE in bta/bta_api.h */ #define ESP_SPP_SEC_AUTHORIZE 0x0001 /*!< Authorization required (only needed for out going connection ) relate to BTA_SEC_AUTHORIZE in bta/bta_api.h*/ #define ESP_SPP_SEC_AUTHENTICATE 0x0012 /*!< Authentication required. relate to BTA_SEC_AUTHENTICATE in bta/bta_api.h*/ @@ -229,7 +229,7 @@ esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr); * When the connection is established or failed, * the callback is called with ESP_SPP_OPEN_EVT. * - * @param[in] sec_mask: Security Setting Mask . + * @param[in] sec_mask: Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only. * @param[in] role: Master or slave. * @param[in] remote_scn: Remote device bluetooth device SCN. * @param[in] peer_bd_addr: Remote device bluetooth device address. @@ -260,7 +260,7 @@ esp_err_t esp_spp_disconnect(uint32_t handle); * When the connection is established, the callback is called * with ESP_SPP_SRV_OPEN_EVT. * - * @param[in] sec_mask: Security Setting Mask . + * @param[in] sec_mask: Security Setting Mask. Security Setting Mask. Suggest to use ESP_SPP_SEC_NONE, ESP_SPP_SEC_AUTHORIZE or ESP_SPP_SEC_AUTHENTICATE only. * @param[in] role: Master or slave. * @param[in] local_scn: The specific channel you want to get. * If channel is 0, means get any channel. diff --git a/components/bt/bluedroid/bta/av/bta_av_aact.c b/components/bt/bluedroid/bta/av/bta_av_aact.c index 27970a990..88cb8e442 100644 --- a/components/bt/bluedroid/bta/av/bta_av_aact.c +++ b/components/bt/bluedroid/bta/av/bta_av_aact.c @@ -41,6 +41,7 @@ #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE) #include "bta/bta_ar_api.h" #endif +#include "bta/bta_api.h" /***************************************************************************** ** Constants @@ -528,8 +529,21 @@ static void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, t /* look up application event */ if ((p_data == NULL) || (p_data->hdr.err_code == 0)) { p_msg->hdr.event = bta_av_stream_evt_ok[event]; + if (p_msg->hdr.event == BTA_AV_STR_START_OK_EVT) { + BTA_DmCoexEventTrigger(BTA_COEX_EVT_STREAMING_STARTED); + } else if (p_msg->hdr.event == BTA_AV_STR_START_FAIL_EVT || + p_msg->hdr.event == BTA_AV_STR_SUSPEND_CFM_EVT || + p_msg->hdr.event == BTA_AV_STR_CLOSE_EVT) { + BTA_DmCoexEventTrigger(BTA_COEX_EVT_STREAMING_STOPPED); + } } else { p_msg->hdr.event = bta_av_stream_evt_fail[event]; + if (p_msg->hdr.event == BTA_AV_STR_START_FAIL_EVT || + p_msg->hdr.event == BTA_AV_STR_START_OK_EVT || + p_msg->hdr.event == BTA_AV_STR_SUSPEND_CFM_EVT || + p_msg->hdr.event == BTA_AV_STR_CLOSE_EVT) { + BTA_DmCoexEventTrigger(BTA_COEX_EVT_STREAMING_STOPPED); + } } p_msg->initiator = FALSE; diff --git a/components/bt/bluedroid/bta/av/bta_av_api.c b/components/bt/bluedroid/bta/av/bta_av_api.c index a0daa89f1..dc1dea220 100644 --- a/components/bt/bluedroid/bta/av/bta_av_api.c +++ b/components/bt/bluedroid/bta/av/bta_av_api.c @@ -114,7 +114,7 @@ void BTA_AvRegister(tBTA_AV_CHNL chnl, const char *p_service_name, UINT8 app_id, p_buf->hdr.layer_specific = chnl; p_buf->hdr.event = BTA_AV_API_REGISTER_EVT; if (p_service_name) { - BCM_STRNCPY_S(p_buf->p_service_name, sizeof(p_buf->p_service_name), p_service_name, BTA_SERVICE_NAME_LEN); + BCM_STRNCPY_S(p_buf->p_service_name, p_service_name, BTA_SERVICE_NAME_LEN); p_buf->p_service_name[BTA_SERVICE_NAME_LEN - 1] = 0; } else { p_buf->p_service_name[0] = 0; diff --git a/components/bt/bluedroid/bta/av/bta_av_cfg.c b/components/bt/bluedroid/bta/av/bta_av_cfg.c index 144df6ea3..4280d2800 100644 --- a/components/bt/bluedroid/bta/av/bta_av_cfg.c +++ b/components/bt/bluedroid/bta/av/bta_av_cfg.c @@ -40,8 +40,9 @@ const UINT32 bta_av_meta_caps_co_ids[] = { AVRC_CO_BROADCOM }; -/* AVRCP cupported categories */ -#define BTA_AV_RC_SUPF_CT (AVRC_SUPF_CT_CAT2) +/* AVRCP supported categories */ +#define BTA_AV_RC_SNK_SUPF_CT (AVRC_SUPF_CT_CAT1) +#define BTA_AV_RC_SRC_SUPF_CT (AVRC_SUPF_CT_CAT2) /* Added to modify ** 1. flush timeout @@ -62,9 +63,11 @@ const UINT16 bta_av_audio_flush_to[] = { /* Note: Android doesnt support AVRC_SUPF_TG_GROUP_NAVI */ /* Note: if AVRC_SUPF_TG_GROUP_NAVI is set, bta_av_cfg.avrc_group should be TRUE */ #if AVRC_METADATA_INCLUDED == TRUE -#define BTA_AV_RC_SUPF_TG (AVRC_SUPF_TG_CAT1) /* TODO: | AVRC_SUPF_TG_APP_SETTINGS) */ +#define BTA_AV_RC_SNK_SUPF_TG (AVRC_SUPF_TG_CAT2) /* TODO: | AVRC_SUPF_TG_APP_SETTINGS) */ +#define BTA_AV_RC_SRC_SUPF_TG (AVRC_SUPF_TG_CAT1) /* TODO: | AVRC_SUPF_TG_APP_SETTINGS) */ #else -#define BTA_AV_RC_SUPF_TG (AVRC_SUPF_TG_CAT1) +#define BTA_AV_RC_SNK_SUPF_TG (AVRC_SUPF_TG_CAT2) +#define BTA_AV_RC_SRC_SUPF_TG (AVRC_SUPF_TG_CAT1) #endif /* @@ -95,8 +98,10 @@ const tBTA_AV_CFG bta_av_cfg = { 48, /* AVRCP MTU at L2CAP for control channel */ #endif BTA_AV_MAX_RC_BR_MTU, /* AVRCP MTU at L2CAP for browsing channel */ - BTA_AV_RC_SUPF_CT, /* AVRCP controller categories */ - BTA_AV_RC_SUPF_TG, /* AVRCP target categories */ + BTA_AV_RC_SNK_SUPF_CT, /* AVRCP controller categories as SNK */ + BTA_AV_RC_SNK_SUPF_TG, /* AVRCP target categories as SNK */ + BTA_AV_RC_SRC_SUPF_CT, /* AVRCP controller categories as SRC */ + BTA_AV_RC_SRC_SUPF_TG, /* AVRCP target categories as SRC */ 672, /* AVDTP signaling channel MTU at L2CAP */ BTA_AV_MAX_A2DP_MTU, /* AVDTP audio transport channel MTU at L2CAP */ bta_av_audio_flush_to, /* AVDTP audio transport channel flush timeout */ diff --git a/components/bt/bluedroid/bta/av/bta_av_main.c b/components/bt/bluedroid/bta/av/bta_av_main.c index b75a8ddd9..800e7e401 100644 --- a/components/bt/bluedroid/bta/av/bta_av_main.c +++ b/components/bt/bluedroid/bta/av/bta_av_main.c @@ -483,8 +483,7 @@ static void bta_av_api_sink_enable(tBTA_AV_DATA *p_data) activate_sink = p_data->hdr.layer_specific; APPL_TRACE_DEBUG("bta_av_api_sink_enable %d \n", activate_sink) char p_service_name[BTA_SERVICE_NAME_LEN + 1]; - BCM_STRNCPY_S(p_service_name, sizeof(p_service_name), - BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN); + BCM_STRNCPY_S(p_service_name, BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN); if (activate_sink) { AVDT_SINK_Activate(); @@ -526,7 +525,7 @@ static void bta_av_api_register(tBTA_AV_DATA *p_data) tBTA_UTL_COD cod; UINT8 index = 0; char p_avk_service_name[BTA_SERVICE_NAME_LEN + 1]; - BCM_STRNCPY_S(p_avk_service_name, sizeof(p_avk_service_name), BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN); + BCM_STRNCPY_S(p_avk_service_name, BTIF_AVK_SERVICE_NAME, BTA_SERVICE_NAME_LEN); memset(&cs, 0, sizeof(tAVDT_CS)); @@ -571,9 +570,13 @@ static void bta_av_api_register(tBTA_AV_DATA *p_data) bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu, (UINT8)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)), BTA_ID_AV); #endif - - bta_ar_reg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target\n", NULL, - p_bta_av_cfg->avrc_tg_cat, BTA_ID_AV); + if (p_data->api_reg.tsep == AVDT_TSEP_SRC) { + bta_ar_reg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target\n", NULL, + p_bta_av_cfg->avrc_src_tg_cat, BTA_ID_AV); + } else { + bta_ar_reg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target\n", NULL, + p_bta_av_cfg->avrc_snk_tg_cat, BTA_ID_AV); + } #endif } @@ -707,8 +710,13 @@ static void bta_av_api_register(tBTA_AV_DATA *p_data) } #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE) /* create an SDP record as AVRC CT. */ - bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL, - p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV); + if (p_data->api_reg.tsep == AVDT_TSEP_SRC) { + bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, "AV Remote Control Controller\n", NULL, + p_bta_av_cfg->avrc_src_ct_cat, BTA_ID_AV); + } else { + bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, "AV Remote Control Controller\n", NULL, + p_bta_av_cfg->avrc_snk_ct_cat, BTA_ID_AV); + } #endif } } diff --git a/components/bt/bluedroid/bta/dm/bta_dm_act.c b/components/bt/bluedroid/bta/dm/bta_dm_act.c index 5ded9a7f9..99fd45cc8 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_act.c @@ -323,6 +323,9 @@ void bta_dm_deinit_cb(void) } #endif memset(&bta_dm_cb, 0, sizeof(bta_dm_cb)); +#if BTA_DYNAMIC_MEMORY + xSemaphoreGive(deinit_semaphore); +#endif /* #if BTA_DYNAMIC_MEMORY */ } /******************************************************************************* @@ -693,7 +696,6 @@ void bta_dm_set_visibility(tBTA_DM_MSG *p_data) if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE || p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) { BTM_SetPairableMode((BOOLEAN)(!(bta_dm_cb.disable_pair_mode)), bta_dm_cb.conn_paired_only); } - } /******************************************************************************* @@ -1684,7 +1686,7 @@ void bta_dm_sdp_result (tBTA_DM_MSG *p_data) if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) { /* send result back to app now, one by one */ bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN)); result.disc_ble_res.bd_name[BD_NAME_LEN] = 0; result.disc_ble_res.service.len = service_uuid.len; result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16; @@ -1825,8 +1827,7 @@ void bta_dm_sdp_result (tBTA_DM_MSG *p_data) } bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), - bta_dm_get_remname(), (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1)); /* make sure the string is null terminated */ p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0; @@ -1852,8 +1853,7 @@ void bta_dm_sdp_result (tBTA_DM_MSG *p_data) p_msg->disc_result.result.disc_res.result = BTA_FAILURE; p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), - bta_dm_get_remname(), (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1)); /* make sure the string is null terminated */ p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0; @@ -2237,8 +2237,7 @@ static void bta_dm_find_services ( BD_ADDR bd_addr) p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), - bta_dm_get_remname(), (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1)); /* make sure the string is terminated */ p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0; @@ -2423,8 +2422,7 @@ static void bta_dm_discover_device(BD_ADDR remote_bd_addr) p_msg->disc_result.result.disc_res.result = BTA_SUCCESS; p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), - (char *)bta_dm_search_cb.peer_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, (char *)bta_dm_search_cb.peer_name, (BD_NAME_LEN - 1)); /* make sure the string is terminated */ p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0; @@ -2568,7 +2566,7 @@ static void bta_dm_service_search_remname_cback (BD_ADDR bd_addr, DEV_CLASS dc, rem_name.length = (BD_NAME_LEN - 1); rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0; } - BCM_STRNCPY_S((char *)rem_name.remote_bd_name, sizeof(BD_NAME), (char *)bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)rem_name.remote_bd_name, (char *)bd_name, (BD_NAME_LEN - 1)); rem_name.status = BTM_SUCCESS; bta_dm_remname_cback(&rem_name); @@ -2611,7 +2609,7 @@ static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name) /* remote name discovery is done but it could be failed */ bta_dm_search_cb.name_discover_done = TRUE; - BCM_STRNCPY_S((char *)bta_dm_search_cb.peer_name, sizeof(BD_NAME), (char *)p_remote_name->remote_bd_name, (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)bta_dm_search_cb.peer_name,(char *)p_remote_name->remote_bd_name, (BD_NAME_LEN)); bta_dm_search_cb.peer_name[BD_NAME_LEN] = 0; BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback); @@ -2624,7 +2622,7 @@ static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name) if ((p_msg = (tBTA_DM_REM_NAME *) osi_malloc(sizeof(tBTA_DM_REM_NAME))) != NULL) { bdcpy (p_msg->result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->result.disc_res.bd_name, sizeof(BD_NAME), (char *)p_remote_name->remote_bd_name, (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)p_msg->result.disc_res.bd_name, (char *)p_remote_name->remote_bd_name, (BD_NAME_LEN)); /* make sure the string is null terminated */ p_msg->result.disc_res.bd_name[BD_NAME_LEN] = 0; @@ -2656,7 +2654,7 @@ static UINT8 bta_dm_authorize_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NA bdcpy(sec_event.authorize.bd_addr, bd_addr); memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN); - BCM_STRNCPY_S((char *)sec_event.authorize.bd_name, sizeof(BD_NAME), (char *)bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.authorize.bd_name, (char *)bd_name, (BD_NAME_LEN - 1)); /* make sure the string is null terminated */ sec_event.authorize.bd_name[BD_NAME_LEN - 1] = 0; @@ -2768,7 +2766,7 @@ static UINT8 bta_dm_pin_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_ bdcpy(sec_event.pin_req.bd_addr, bd_addr); BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class); - BCM_STRNCPY_S((char *)sec_event.pin_req.bd_name, sizeof(BD_NAME), (char *)bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.pin_req.bd_name, (char *)bd_name, (BD_NAME_LEN - 1)); sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0; sec_event.pin_req.min_16_digit = min_16_digit; @@ -2938,8 +2936,7 @@ static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data) copy these values into key_notif from cfm_req */ bdcpy(sec_event.key_notif.bd_addr, p_data->cfm_req.bd_addr); BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->cfm_req.dev_class); - BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, sizeof(BD_NAME), - (char *)p_data->cfm_req.bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->cfm_req.bd_name, (BD_NAME_LEN - 1)); sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0; } } @@ -2960,8 +2957,7 @@ static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data) } else { bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr); BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class); - BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, sizeof(BD_NAME), - (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1)); sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0; } } @@ -2982,8 +2978,7 @@ static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data) } else { bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr); BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class); - BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, sizeof(BD_NAME), - (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1)); sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0; } } @@ -3012,7 +3007,7 @@ static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data) bdcpy(sec_event.rmt_oob.bd_addr, p_data->rmt_oob.bd_addr); BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class, p_data->rmt_oob.dev_class); - BCM_STRNCPY_S((char *)sec_event.rmt_oob.bd_name, sizeof(BD_NAME), (char *)p_data->rmt_oob.bd_name, (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)sec_event.rmt_oob.bd_name, (char *)p_data->rmt_oob.bd_name, (BD_NAME_LEN - 1)); sec_event.rmt_oob.bd_name[BD_NAME_LEN - 1] = 0; bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event); @@ -4348,8 +4343,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D bdcpy(sec_event.ble_req.bd_addr, bda); p_name = BTM_SecReadDevName(bda); if (p_name != NULL) { - BCM_STRNCPY_S((char *)sec_event.ble_req.bd_name, - sizeof(BD_NAME), p_name, (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)sec_event.ble_req.bd_name, p_name, (BD_NAME_LEN)); } else { sec_event.ble_req.bd_name[0] = 0; } @@ -4361,8 +4355,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D bdcpy(sec_event.key_notif.bd_addr, bda); p_name = BTM_SecReadDevName(bda); if (p_name != NULL) { - BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, - sizeof(BD_NAME), p_name, (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, p_name, (BD_NAME_LEN)); } else { sec_event.key_notif.bd_name[0] = 0; } @@ -4383,7 +4376,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D case BTM_LE_NC_REQ_EVT: bdcpy(sec_event.key_notif.bd_addr, bda); - BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, bta_dm_get_remname(), (BD_NAME_LEN)); sec_event.ble_req.bd_name[BD_NAME_LEN] = 0; sec_event.key_notif.passkey = p_data->key_notif; bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event); @@ -4403,8 +4396,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D #endif p_name = BTM_SecReadDevName(bda); if (p_name != NULL) { - BCM_STRNCPY_S((char *)sec_event.auth_cmpl.bd_name, - sizeof(BD_NAME), p_name, (BD_NAME_LEN)); + BCM_STRNCPY_S((char *)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN)); } else { sec_event.auth_cmpl.bd_name[0] = 0; } @@ -5685,7 +5677,7 @@ static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) /* send result back to app now, one by one */ bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name,bta_dm_get_remname(), (BD_NAME_LEN - 1)); result.disc_ble_res.bd_name[BD_NAME_LEN] = 0; memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID)); @@ -5727,8 +5719,7 @@ static void bta_dm_gatt_disc_complete(UINT16 conn_id, tBTA_GATT_STATUS status) p_msg->disc_result.result.disc_res.num_uuids = 0; p_msg->disc_result.result.disc_res.p_uuid_list = NULL; bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); - BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), - bta_dm_get_remname(), (BD_NAME_LEN - 1)); + BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1)); /* make sure the string is terminated */ p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0; diff --git a/components/bt/bluedroid/bta/dm/bta_dm_api.c b/components/bt/bluedroid/bta/dm/bta_dm_api.c index c5d5440fc..2eec142b7 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_api.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_api.c @@ -174,7 +174,7 @@ void BTA_DmSetDeviceName(const char *p_name) if ((p_msg = (tBTA_DM_API_SET_NAME *) osi_malloc(sizeof(tBTA_DM_API_SET_NAME))) != NULL) { p_msg->hdr.event = BTA_DM_API_SET_NAME_EVT; /* truncate the name if needed */ - BCM_STRNCPY_S((char *)p_msg->name, sizeof(p_msg->name), p_name, BD_NAME_LEN - 1); + BCM_STRNCPY_S((char *)p_msg->name, p_name, BD_NAME_LEN - 1); p_msg->name[BD_NAME_LEN - 1] = 0; bta_sys_sendmsg(p_msg); @@ -736,7 +736,7 @@ UINT16 BTA_DmGetConnectionState( BD_ADDR bd_addr ) ** ** Description This function adds a DI record to the local SDP database. ** -** Returns BTA_SUCCESS if record set sucessfully, otherwise error code. +** Returns BTA_SUCCESS if record set successfully, otherwise error code. ** *******************************************************************************/ tBTA_STATUS BTA_DmSetLocalDiRecord( tBTA_DI_RECORD *p_device_info, @@ -1819,7 +1819,7 @@ void BTA_DmBleConfigLocalIcon(uint16_t icon) ** p_cback: callback function associated to this adv instance. ** p_ref: reference data pointer to this adv instance. ** -** Returns BTA_SUCCESS if command started sucessfully; otherwise failure. +** Returns BTA_SUCCESS if command started successfully; otherwise failure. ** *******************************************************************************/ void BTA_BleEnableAdvInstance (tBTA_BLE_ADV_PARAMS *p_params, @@ -1857,7 +1857,7 @@ void BTA_BleEnableAdvInstance (tBTA_BLE_ADV_PARAMS *p_params, ** Parameters inst_id: Adv instance to update the parameter. ** p_params: pointer to the adv parameter structure. ** -** Returns BTA_SUCCESS if command started sucessfully; otherwise failure. +** Returns BTA_SUCCESS if command started successfully; otherwise failure. ** *******************************************************************************/ void BTA_BleUpdateAdvInstParam (UINT8 inst_id, tBTA_BLE_ADV_PARAMS *p_params) @@ -1892,7 +1892,7 @@ void BTA_BleUpdateAdvInstParam (UINT8 inst_id, tBTA_BLE_ADV_PARAMS *p_params) ** memory space can not be freed until BTA_BLE_MULTI_ADV_DATA_EVT ** is sent to application. ** -** Returns BTA_SUCCESS if command started sucessfully; otherwise failure. +** Returns BTA_SUCCESS if command started successfully; otherwise failure. ** *******************************************************************************/ void BTA_BleCfgAdvInstData (UINT8 inst_id, BOOLEAN is_scan_rsp, @@ -1925,7 +1925,7 @@ void BTA_BleCfgAdvInstData (UINT8 inst_id, BOOLEAN is_scan_rsp, ** ** Parameter inst_id: instance ID to disable. ** -** Returns BTA_SUCCESS if command started sucessfully; otherwise failure. +** Returns BTA_SUCCESS if command started successfully; otherwise failure. ** *******************************************************************************/ void BTA_BleDisableAdvInstance (UINT8 inst_id) //this function just used for vendor debug diff --git a/components/bt/bluedroid/bta/dm/bta_dm_cfg.c b/components/bt/bluedroid/bta/dm/bta_dm_cfg.c index e9ad1f38d..981da1b92 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_cfg.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_cfg.c @@ -228,9 +228,9 @@ tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_SPEC bta_dm_pm_spec[BTA_DM_NUM_PM_SPEC] = { {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ - {{BTA_DM_PM_SNIFF3, 7000 + BTA_DM_PM_SPEC_TO_OFFSET}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */ - {{BTA_DM_PM_SNIFF, 7000 + BTA_DM_PM_SPEC_TO_OFFSET}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */ - {{BTA_DM_PM_SNIFF, 7000 + BTA_DM_PM_SPEC_TO_OFFSET}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ {{BTA_DM_PM_RETRY, 7000 + BTA_DM_PM_SPEC_TO_OFFSET}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ } diff --git a/components/bt/bluedroid/bta/dm/bta_dm_co.c b/components/bt/bluedroid/bta/dm/bta_dm_co.c index 7e6cb4808..5fdf0812a 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_co.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_co.c @@ -48,6 +48,7 @@ tBTE_APPL_CFG bte_appl_cfg = { BTM_BLE_INITIATOR_KEY_SIZE, BTM_BLE_RESPONDER_KEY_SIZE, BTM_BLE_MAX_KEY_SIZE, + BTM_BLE_MIN_KEY_SIZE, BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE, BTM_BLE_OOB_DISABLE, }; @@ -407,7 +408,7 @@ void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key) void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size) { #if (SMP_INCLUDED == TRUE) - if(ble_key_size >= BTM_BLE_MIN_KEY_SIZE && ble_key_size <= BTM_BLE_MAX_KEY_SIZE) { + if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= BTM_BLE_MAX_KEY_SIZE) { bte_appl_cfg.ble_max_key_size = ble_key_size; } else { APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size); @@ -415,6 +416,17 @@ void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size) #endif ///SMP_INCLUDED == TRUE } +void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size) +{ +#if (SMP_INCLUDED == TRUE) + if(ble_key_size >= BTM_BLE_MIN_KEY_SIZE && ble_key_size <= bte_appl_cfg.ble_max_key_size) { + bte_appl_cfg.ble_min_key_size = ble_key_size; + } else { + APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size); + } +#endif ///SMP_INCLUDED == TRUE +} + void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable) { #if (SMP_INCLUDED == TRUE) diff --git a/components/bt/bluedroid/bta/dm/bta_dm_main.c b/components/bt/bluedroid/bta/dm/bta_dm_main.c index 0f45dfbe9..eef04e7d7 100644 --- a/components/bt/bluedroid/bta/dm/bta_dm_main.c +++ b/components/bt/bluedroid/bta/dm/bta_dm_main.c @@ -28,6 +28,7 @@ #include "osi/allocator.h" #include +#include "esp_coexist.h" /***************************************************************************** ** Constants and types @@ -452,3 +453,27 @@ BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg) return TRUE; } +void BTA_DmCoexEventTrigger(uint32_t event) +{ + switch(event) { + case BTA_COEX_EVT_SCAN_STARTED: + case BTA_COEX_EVT_SCAN_STOPPED: + case BTA_COEX_EVT_SNIFF_ENTER: + case BTA_COEX_EVT_SNIFF_EXIT: + case BTA_COEX_EVT_A2DP_PAUSED_ENTER: + case BTA_COEX_EVT_A2DP_PAUSED_EXIT: + case BTA_COEX_EVT_ACL_CONNECTED: + case BTA_COEX_EVT_ACL_DISCONNECTED: + break; + case BTA_COEX_EVT_STREAMING_STARTED: + esp_coex_status_bit_set(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_STREAMING); + esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_PAUSED); + break; + case BTA_COEX_EVT_STREAMING_STOPPED: + esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_STREAMING); + esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_PAUSED); + break; + default: + break; + } +} \ No newline at end of file diff --git a/components/bt/bluedroid/bta/dm/include/bta_dm_int.h b/components/bt/bluedroid/bta/dm/include/bta_dm_int.h index 6e962d5d5..23c2d6328 100644 --- a/components/bt/bluedroid/bta/dm/include/bta_dm_int.h +++ b/components/bt/bluedroid/bta/dm/include/bta_dm_int.h @@ -25,7 +25,7 @@ #define BTA_DM_INT_H #include "common/bt_target.h" - +#include "freertos/semphr.h" #if (BLE_INCLUDED == TRUE && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) #include "bta/bta_gatt_api.h" #endif @@ -1209,6 +1209,7 @@ extern tBTA_DM_DI_CB bta_dm_di_cb; #else extern tBTA_DM_DI_CB *bta_dm_di_cb_ptr; #define bta_dm_di_cb (*bta_dm_di_cb_ptr) +SemaphoreHandle_t deinit_semaphore; #endif extern BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg); diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c index 73fd86ef2..4ae5eba31 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_act.c @@ -745,6 +745,8 @@ void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data) void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data) { if (p_rcb) { + // Clear up the notification registration information by BD_ADDR + bta_gattc_clear_notif_registration_by_bda(p_rcb, p_data->int_conn.remote_bda); bta_gattc_send_disconnect_cback(p_rcb, p_data->int_conn.reason, p_data->int_conn.remote_bda, diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c index 00771981d..9a43c5588 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_api.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_api.c @@ -76,7 +76,7 @@ void BTA_GATTC_Disable(void) ** Description This function is called to register application callbacks ** with BTA GATTC module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_client_cb - pointer to the application callback function. ** ** Returns None @@ -338,13 +338,13 @@ const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 hand } void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_service_with_uuid(conn_id, svc_uuid, db, count); } void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_ALL_CHAR, @@ -359,7 +359,7 @@ void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle } void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_ALL_DESCRI, @@ -374,7 +374,7 @@ void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, } void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_CHAR_BY_UUID, @@ -390,7 +390,7 @@ void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_han void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle, tBT_UUID char_uuid, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_DESCRI_BY_UUID, @@ -405,7 +405,7 @@ void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t en } void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_DESCRI_BY_HANDLE, @@ -420,7 +420,7 @@ void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID } void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - tBT_UUID *incl_uuid, btgatt_db_element_t **db, int *count) + tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_db_with_opration(conn_id, GATT_OP_GET_INCLUDE_SVC, @@ -434,13 +434,13 @@ void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end count); } -void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count) +void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count) { bta_gattc_get_db_size_handle(conn_id, start_handle, end_handle, count); } void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count) + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count) { bta_gattc_get_db_size_with_type_handle(conn_id, type, start_handle, end_handle, char_handle, count); } @@ -459,7 +459,7 @@ void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, ** *******************************************************************************/ void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count) + btgatt_db_element_t **db, UINT16 *count) { bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count); } diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c index 128dc299c..2347457a2 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_cache.c @@ -1227,7 +1227,7 @@ tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(UINT16 conn_id, UINT16 handle) void bta_gattc_get_service_with_uuid(UINT16 conn_id, tBT_UUID *svc_uuid, btgatt_db_element_t **svc_db, - int *count) + UINT16 *count) { const list_t* svc = bta_gattc_get_services(conn_id); if(!svc) { @@ -1301,7 +1301,7 @@ void bta_gattc_get_db_with_opration(UINT16 conn_id, tBT_UUID *descr_uuid, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **char_db, - int *count) + UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1666,7 +1666,7 @@ static size_t bta_gattc_get_db_size(list_t *services, return db_size; } -void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count) +void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1685,7 +1685,7 @@ void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 en } void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count) + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); @@ -1732,7 +1732,7 @@ void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV *p_srvc_cb, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, - int *count) + UINT16 *count) { APPL_TRACE_DEBUG("%s: start_handle 0x%04x, end_handle 0x%04x", __func__, start_handle, end_handle); @@ -1880,7 +1880,7 @@ static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV *p_srvc_cb, ** Returns None. ** *******************************************************************************/ -void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, int *count) +void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, UINT16 *count) { tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); diff --git a/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c b/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c index d20245144..c8ea25236 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c +++ b/components/bt/bluedroid/bta/gatt/bta_gattc_utils.c @@ -596,6 +596,30 @@ void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id, return; } +/******************************************************************************* +** +** Function bta_gattc_clear_notif_registration_by_bda +** +** Description Clear up the notification registration information by BD_ADDR. +** +** +** Returns None. +** +*******************************************************************************/ +void bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB *p_clrcb, BD_ADDR remote_bda) +{ + if(p_clrcb == NULL) { + return; + } + for (uint8_t i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) { + if (p_clrcb->notif_reg[i].in_use && + !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda)) + { + memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG)); + } + } +} + /******************************************************************************* ** ** Function bta_gattc_mark_bg_conn diff --git a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c index 47bad4afb..40274aa90 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gatts_act.c +++ b/components/bt/bluedroid/bta/gatt/bta_gatts_act.c @@ -1014,22 +1014,14 @@ static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, ** Returns none. ** *******************************************************************************/ +extern void btc_congest_callback(tBTA_GATTS *param); static void bta_gatts_cong_cback (UINT16 conn_id, BOOLEAN congested) { - tBTA_GATTS_RCB *p_rcb; - tGATT_IF gatt_if; - tBTA_GATT_TRANSPORT transport; tBTA_GATTS cb_data; - if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport)) { - p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if); + cb_data.congest.conn_id = conn_id; + cb_data.congest.congested = congested; - if (p_rcb && p_rcb->p_cback) { - cb_data.congest.conn_id = conn_id; - cb_data.congest.congested = congested; - - (*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data); - } - } + btc_congest_callback(&cb_data); } #endif /* GATTS_INCLUDED */ diff --git a/components/bt/bluedroid/bta/gatt/bta_gatts_api.c b/components/bt/bluedroid/bta/gatt/bta_gatts_api.c index 87e559ab7..8f1882503 100644 --- a/components/bt/bluedroid/bta/gatt/bta_gatts_api.c +++ b/components/bt/bluedroid/bta/gatt/bta_gatts_api.c @@ -76,7 +76,7 @@ void BTA_GATTS_Disable(void) ** Description This function is called to register application callbacks ** with BTA GATTS module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_cback - pointer to the application callback function. ** ** Returns None @@ -347,7 +347,7 @@ void BTA_GATTS_DeleteService(UINT16 service_id) ** Description This function is called to start a service. ** ** Parameters service_id: the service ID to be started. -** sup_transport: supported trasnport. +** sup_transport: supported transport. ** ** Returns None. ** diff --git a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h index ad440c2bc..5c2b1a85a 100644 --- a/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h +++ b/components/bt/bluedroid/bta/gatt/include/bta_gattc_int.h @@ -498,6 +498,7 @@ extern BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR rem extern BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role); extern UINT8 bta_gattc_num_reg_app(void); extern void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id, UINT16 start_handle, UINT16 end_handle); +extern void bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB *p_clrcb, BD_ADDR remote_bda); extern tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda); /* discovery functions */ @@ -511,12 +512,12 @@ extern const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle(UINT16 conn_id tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb(tBTA_GATTC_SERV *p_srcb, UINT16 handle); extern tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(UINT16 conn_id, UINT16 handle); extern tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(UINT16 conn_id, UINT16 handle); -extern void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count); +extern void bta_gattc_get_db_size_handle(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count); extern void bta_gattc_get_db_size_with_type_handle(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count); + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count); extern void bta_gattc_get_service_with_uuid(UINT16 conn_id, tBT_UUID *svc_uuid, btgatt_db_element_t **svc_db, - int *count); + UINT16 *count); extern void bta_gattc_get_db_with_opration(UINT16 conn_id, bt_gatt_get_db_op_t op, @@ -526,9 +527,9 @@ extern void bta_gattc_get_db_with_opration(UINT16 conn_id, tBT_UUID *descr_uuid, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **char_db, - int *count); + UINT16 *count); -extern void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, int *count); +extern void bta_gattc_get_gatt_db(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, btgatt_db_element_t **db, UINT16 *count); extern tBTA_GATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV *p_srvc_cb); extern void bta_gattc_rebuild_cache(tBTA_GATTC_SERV *p_srcv, UINT16 num_attr, tBTA_GATTC_NV_ATTR *attr); diff --git a/components/bt/bluedroid/bta/hf_client/bta_hf_client_api.c b/components/bt/bluedroid/bta/hf_client/bta_hf_client_api.c index 434b2b520..ea51879c2 100644 --- a/components/bt/bluedroid/bta/hf_client/bta_hf_client_api.c +++ b/components/bt/bluedroid/bta/hf_client/bta_hf_client_api.c @@ -141,7 +141,7 @@ void BTA_HfClientRegister(tBTA_SEC sec_mask, tBTA_HF_CLIENT_FEAT features, p_buf->features = features; p_buf->sec_mask = sec_mask; if (p_service_name) { - BCM_STRNCPY_S(p_buf->name, BTA_SERVICE_NAME_LEN + 1, p_service_name, BTA_SERVICE_NAME_LEN); + BCM_STRNCPY_S(p_buf->name, p_service_name, BTA_SERVICE_NAME_LEN); p_buf->name[BTA_SERVICE_NAME_LEN] = 0; } else { p_buf->name[0] = '\0'; diff --git a/components/bt/bluedroid/bta/hf_client/bta_hf_client_at.c b/components/bt/bluedroid/bta/hf_client/bta_hf_client_at.c index 33ae29348..3b91de60b 100644 --- a/components/bt/bluedroid/bta/hf_client/bta_hf_client_at.c +++ b/components/bt/bluedroid/bta/hf_client/bta_hf_client_at.c @@ -75,12 +75,12 @@ typedef struct { /* CIND: storage room for indicators value range and their statuses */ static const tBTA_HF_CLIENT_INDICATOR bta_hf_client_indicators[BTA_HF_CLIENT_AT_SUPPORTED_INDICATOR_COUNT] = { /* name | min | max | name length - used by parser */ - {BTA_HF_CLIENT_INDICATOR_BATTERYCHG, 0, 5, sizeof(BTA_HF_CLIENT_INDICATOR_BATTERYCHG)}, - {BTA_HF_CLIENT_INDICATOR_SIGNAL, 0, 5, sizeof(BTA_HF_CLIENT_INDICATOR_SIGNAL)}, - {BTA_HF_CLIENT_INDICATOR_SERVICE, 0, 1, sizeof(BTA_HF_CLIENT_INDICATOR_SERVICE)}, {BTA_HF_CLIENT_INDICATOR_CALL, 0, 1, sizeof(BTA_HF_CLIENT_INDICATOR_CALL)}, - {BTA_HF_CLIENT_INDICATOR_ROAM, 0, 1, sizeof(BTA_HF_CLIENT_INDICATOR_ROAM)}, {BTA_HF_CLIENT_INDICATOR_CALLSETUP, 0, 3, sizeof(BTA_HF_CLIENT_INDICATOR_CALLSETUP)}, + {BTA_HF_CLIENT_INDICATOR_SERVICE, 0, 1, sizeof(BTA_HF_CLIENT_INDICATOR_SERVICE)}, + {BTA_HF_CLIENT_INDICATOR_SIGNAL, 0, 5, sizeof(BTA_HF_CLIENT_INDICATOR_SIGNAL)}, + {BTA_HF_CLIENT_INDICATOR_ROAM, 0, 1, sizeof(BTA_HF_CLIENT_INDICATOR_ROAM)}, + {BTA_HF_CLIENT_INDICATOR_BATTERYCHG, 0, 5, sizeof(BTA_HF_CLIENT_INDICATOR_BATTERYCHG)}, {BTA_HF_CLIENT_INDICATOR_CALLHELD, 0, 2, sizeof(BTA_HF_CLIENT_INDICATOR_CALLHELD)} }; @@ -94,7 +94,7 @@ UINT32 service_index = 0; BOOLEAN service_availability = TRUE; /* helper functions for handling AT commands queueing */ -static void bta_hf_client_handle_ok(); +static void bta_hf_client_handle_ok(void); static void bta_hf_client_clear_queued_at(void) { @@ -268,7 +268,7 @@ static void bta_hf_client_start_at_hold_timer(void) ** No buffer parsing is being done here. *******************************************************************************/ -static void bta_hf_client_handle_ok() +static void bta_hf_client_handle_ok(void) { APPL_TRACE_DEBUG("%s", __FUNCTION__); @@ -342,7 +342,7 @@ static void bta_hf_client_handle_error(tBTA_HF_CLIENT_AT_RESULT_TYPE type, UINT1 bta_hf_client_send_queued_at(); } -static void bta_hf_client_handle_ring() +static void bta_hf_client_handle_ring(void) { APPL_TRACE_DEBUG("%s", __FUNCTION__); bta_hf_client_evt_val(BTA_HF_CLIENT_RING_INDICATION, 0); @@ -427,7 +427,7 @@ static void bta_hf_client_handle_ciev(UINT32 index, UINT32 value) APPL_TRACE_DEBUG("%s index: %u value: %u", __FUNCTION__, index, value); - if (index == 0 || index > BTA_HF_CLIENT_AT_INDICATOR_COUNT) { + if (index >= BTA_HF_CLIENT_AT_INDICATOR_COUNT) { return; } @@ -435,7 +435,7 @@ static void bta_hf_client_handle_ciev(UINT32 index, UINT32 value) service_availability = value == 0 ? FALSE : TRUE; } - realind = bta_hf_client_cb.scb.at_cb.indicator_lookup[index - 1]; + realind = bta_hf_client_cb.scb.at_cb.indicator_lookup[index]; if (realind >= 0 && realind < BTA_HF_CLIENT_AT_SUPPORTED_INDICATOR_COUNT) { /* get the real in-array index from lookup table by index it comes at */ @@ -576,15 +576,17 @@ static void bta_hf_client_handle_btrh( UINT16 code) /* Check if prefix match and skip spaces if any */ #define AT_CHECK_EVENT(buf, event) \ - if (strncmp("\r\n"event, buf,sizeof("\r\n"event) - 1) != 0) return buf; \ - buf += sizeof("\r\n"event) - 1; \ - while (*buf == ' ') buf++; +if (strncmp("\r\n"event,buf,sizeof("\r\n"event) - 1) != 0) \ + return buf; \ +buf += sizeof("\r\n"event) - 1; \ +while (*buf == ' ') buf++; /* check for and forward buffer if match */ #define AT_CHECK_RN(buf) \ if (strncmp("\r\n", buf, sizeof("\r\n") - 1) != 0) { \ - APPL_TRACE_DEBUG("%s missing end ", __FUNCTION__); \ - return NULL;} \ + APPL_TRACE_ERROR("%s missing end ", __FUNCTION__); \ + return NULL;\ + } \ buf += sizeof("\r\n") - 1; /* skip rest of AT string up to */ @@ -1022,20 +1024,20 @@ static char *bta_hf_client_parse_clcc(char *buffer) static char *bta_hf_client_parse_cnum(char *buffer) { char numstr[33]; /* spec forces 32 chars, plus one for \0*/ - UINT16 type; - UINT16 service = 0; /* 0 in case this optional parameter is not being sent */ + int type; + int service = 0; /* 0 in case this optional parameter is not being sent */ int res; int offset; AT_CHECK_EVENT(buffer, "+CNUM:"); - res = sscanf(buffer, ",\"%32[^\"]\",%hu,,%hu%n", numstr, &type, &service, &offset); + res = sscanf(buffer, ",\"%32[^\"]\",%d%n,,%d%n", numstr, &type, &offset, &service, &offset); if (res < 0) { return NULL; } if (res == 0) { - res = sscanf(buffer, ",\"\",%hu,,%hu%n", &type, &service, &offset); + res = sscanf(buffer, ",\"\",%d%n,,%d%n", &type, &offset, &service, &offset); if (res < 0) { return NULL; } @@ -1045,7 +1047,7 @@ static char *bta_hf_client_parse_cnum(char *buffer) numstr[0] = '\0'; } - if (res < 3) { + if (res < 2) { return NULL; } @@ -1257,7 +1259,7 @@ static void bta_hf_client_at_parse_start(void) for (i = 0; i < bta_hf_client_psraser_cb_count; i++) { tmp = bta_hf_client_parser_cb[i](buf); if (tmp == NULL) { - APPL_TRACE_ERROR("HFPCient: AT event/reply parsing failed, skipping"); + APPL_TRACE_ERROR("HFPCient: AT event/reply parsing failed, skipping %d", i); tmp = bta_hf_client_skip_unknown(buf); break; } diff --git a/components/bt/bluedroid/bta/hh/bta_hh_le.c b/components/bt/bluedroid/bta/hh/bta_hh_le.c index c049275e4..6c86cb9be 100644 --- a/components/bt/bluedroid/bta/hh/bta_hh_le.c +++ b/components/bt/bluedroid/bta/hh/bta_hh_le.c @@ -850,7 +850,7 @@ void bta_hh_le_register_input_notif(tBTA_HH_DEV_CB *p_dev_cb, UINT8 srvc_inst, ** ** Function bta_hh_le_open_cmpl ** -** Description HID over GATT connection sucessfully opened +** Description HID over GATT connection successfully opened ** *******************************************************************************/ void bta_hh_le_open_cmpl(tBTA_HH_DEV_CB *p_cb) @@ -1556,7 +1556,7 @@ void bta_hh_le_srvc_search_cmpl(tBTA_GATTC_SEARCH_CMPL *p_data) /* close the connection and report service discovery complete with error */ bta_hh_le_api_disc_act(p_dev_cb); } - /* GATT service discovery sucessfully finished */ + /* GATT service discovery successfully finished */ else { if (p_dev_cb->disc_active & BTA_HH_LE_DISC_SCPS) { p_dev_cb->disc_active &= ~BTA_HH_LE_DISC_SCPS; diff --git a/components/bt/bluedroid/bta/include/bta/bta_api.h b/components/bt/bluedroid/bta/include/bta/bta_api.h index f384edeb4..c580433ba 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_api.h @@ -589,10 +589,10 @@ typedef struct { typedef union { tBLE_BD_ADDR target_addr; - tBTA_DM_BLE_PF_LOCAL_NAME_COND local_name; /* lcoal name filtering */ - tBTA_DM_BLE_PF_MANU_COND manu_data; /* manufactuer data filtering */ + tBTA_DM_BLE_PF_LOCAL_NAME_COND local_name; /* local name filtering */ + tBTA_DM_BLE_PF_MANU_COND manu_data; /* manufacturer data filtering */ tBTA_DM_BLE_PF_UUID_COND srvc_uuid; /* service UUID filtering */ - tBTA_DM_BLE_PF_UUID_COND solicitate_uuid; /* solicitated service UUID filtering */ + tBTA_DM_BLE_PF_UUID_COND solicitate_uuid; /* solicited service UUID filtering */ tBTA_DM_BLE_PF_SRVC_PATTERN_COND srvc_data; /* service data pattern */ } tBTA_DM_BLE_PF_COND_PARAM; @@ -1204,7 +1204,7 @@ typedef UINT16 tBTA_DM_LP_MASK; #define BTA_DM_PM_ACTIVE 0x40 /* prefers active mode */ #define BTA_DM_PM_RETRY 0x80 /* retry power mode based on current settings */ #define BTA_DM_PM_SUSPEND 0x04 /* prefers suspend mode */ -#define BTA_DM_PM_NO_PREF 0x01 /* service has no prefernce on power mode setting. eg. connection to service got closed */ +#define BTA_DM_PM_NO_PREF 0x01 /* service has no preference on power mode setting. eg. connection to service got closed */ typedef UINT8 tBTA_DM_PM_ACTION; @@ -1375,6 +1375,7 @@ typedef UINT8 tBTA_DM_LINK_TYPE; #define ALLOW_ALL_FILTER 0x00 #define LOWEST_RSSI_VALUE 129 + /***************************************************************************** ** External Function Declarations *****************************************************************************/ @@ -1732,7 +1733,7 @@ extern UINT16 BTA_DmGetConnectionState( BD_ADDR bd_addr ); ** ** Description This function adds a DI record to the local SDP database. ** -** Returns BTA_SUCCESS if record set sucessfully, otherwise error code. +** Returns BTA_SUCCESS if record set successfully, otherwise error code. ** *******************************************************************************/ extern tBTA_STATUS BTA_DmSetLocalDiRecord( tBTA_DI_RECORD *p_device_info, @@ -2601,6 +2602,21 @@ extern void BTA_VendorInit (void); *******************************************************************************/ extern void BTA_VendorCleanup (void); +enum { + BTA_COEX_EVT_SCAN_STARTED = 1, + BTA_COEX_EVT_SCAN_STOPPED, + BTA_COEX_EVT_ACL_CONNECTED, + BTA_COEX_EVT_ACL_DISCONNECTED, + BTA_COEX_EVT_STREAMING_STARTED, + BTA_COEX_EVT_STREAMING_STOPPED, + BTA_COEX_EVT_SNIFF_ENTER, + BTA_COEX_EVT_SNIFF_EXIT, + BTA_COEX_EVT_A2DP_PAUSED_ENTER, + BTA_COEX_EVT_A2DP_PAUSED_EXIT, +}; + +extern void BTA_DmCoexEventTrigger(uint32_t event); + #endif #ifdef __cplusplus diff --git a/components/bt/bluedroid/bta/include/bta/bta_av_api.h b/components/bt/bluedroid/bta/include/bta/bta_av_api.h index 87c2d374a..21ce307b4 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_av_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_av_api.h @@ -509,8 +509,10 @@ typedef struct { UINT32 company_id; /* AVRCP Company ID */ UINT16 avrc_mtu; /* AVRCP MTU at L2CAP for control channel */ UINT16 avrc_br_mtu; /* AVRCP MTU at L2CAP for browsing channel */ - UINT16 avrc_ct_cat; /* AVRCP controller categories */ - UINT16 avrc_tg_cat; /* AVRCP target categories */ + UINT16 avrc_snk_ct_cat; /* AVRCP controller categories as SNK */ + UINT16 avrc_snk_tg_cat; /* AVRCP target categories SNK */ + UINT16 avrc_src_ct_cat; /* AVRCP controller categories as SRC */ + UINT16 avrc_src_tg_cat; /* AVRCP target categories as SRC */ UINT16 sig_mtu; /* AVDTP signaling channel MTU at L2CAP */ UINT16 audio_mtu; /* AVDTP audio transport channel MTU at L2CAP */ const UINT16 *p_audio_flush_to;/* AVDTP audio transport channel flush timeout */ diff --git a/components/bt/bluedroid/bta/include/bta/bta_dm_co.h b/components/bt/bluedroid/bta/include/bta/bta_dm_co.h index b9e98b465..99667cb01 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_dm_co.h +++ b/components/bt/bluedroid/bta/include/bta/bta_dm_co.h @@ -205,6 +205,8 @@ extern void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key); extern void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size); +extern void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size); + extern void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable); extern UINT8 bta_dm_co_ble_get_accept_auth_enable(void); diff --git a/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h index 5048511c4..997d7b7d1 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_gatt_api.h @@ -152,7 +152,7 @@ typedef UINT8 tBTA_GATT_STATUS; #define BTA_GATTC_CLOSE_EVT 5 /* GATTC close request status event */ #define BTA_GATTC_SEARCH_CMPL_EVT 6 /* GATT discovery complete event */ #define BTA_GATTC_SEARCH_RES_EVT 7 /* GATT discovery result event */ -#define BTA_GATTC_READ_DESCR_EVT 8 /* GATT read characterisitc descriptor event */ +#define BTA_GATTC_READ_DESCR_EVT 8 /* GATT read characteristic descriptor event */ #define BTA_GATTC_WRITE_DESCR_EVT 9 /* GATT write characteristic descriptor event */ #define BTA_GATTC_NOTIF_EVT 10 /* GATT attribute notification event */ #define BTA_GATTC_PREP_WRITE_EVT 11 /* GATT prepare write event */ @@ -739,7 +739,7 @@ extern void BTA_GATTC_Disable(void); ** Description This function is called to register application callbacks ** with BTA GATTC module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_client_cb - pointer to the application callback function. ** ** Returns None @@ -867,32 +867,32 @@ extern const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_ *******************************************************************************/ extern const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle); -extern void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, - btgatt_db_element_t **db, int *count); +extern void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid, + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle, tBT_UUID char_uuid, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); extern void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - tBT_UUID *incl_uuid, btgatt_db_element_t **db, int *count); + tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count); -extern void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, int *count); +extern void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count); extern void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type, - UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, int *count); + UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count); /******************************************************************************* ** @@ -907,7 +907,7 @@ extern void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_ ** *******************************************************************************/ extern void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, - btgatt_db_element_t **db, int *count); + btgatt_db_element_t **db, UINT16 *count); /******************************************************************************* ** @@ -1202,7 +1202,7 @@ extern void BTA_GATTS_Disable(void); ** Description This function is called to register application callbacks ** with BTA GATTS module. ** -** Parameters p_app_uuid - applicaiton UUID +** Parameters p_app_uuid - application UUID ** p_cback - pointer to the application callback function. ** ** Returns None @@ -1325,7 +1325,7 @@ extern void BTA_GATTS_DeleteService(UINT16 service_id); ** Description This function is called to start a service. ** ** Parameters service_id: the service ID to be started. -** sup_transport: supported trasnport. +** sup_transport: supported transport. ** ** Returns None. ** diff --git a/components/bt/bluedroid/bta/include/bta/bta_hf_client_api.h b/components/bt/bluedroid/bta/include/bta/bta_hf_client_api.h index ade9f63f2..d2e760b22 100644 --- a/components/bt/bluedroid/bta/include/bta/bta_hf_client_api.h +++ b/components/bt/bluedroid/bta/include/bta/bta_hf_client_api.h @@ -115,15 +115,16 @@ typedef UINT8 tBTA_HF_CLIENT_EVT; typedef UINT8 tBTA_HF_CLIENT_STATUS; -/* indicator type */ -#define BTA_HF_CLIENT_IND_BATTCH 0 /* Battery charge indicator */ -#define BTA_HF_CLIENT_IND_SIGNAL 1 /* Signal Strength indicator */ -#define BTA_HF_CLIENT_IND_SERVICE 2 /* Service availability indicator */ -#define BTA_HF_CLIENT_IND_CALL 3 /* Standard call status indicator*/ -#define BTA_HF_CLIENT_IND_ROAM 4 /* Roaming status indicator */ -#define BTA_HF_CLIENT_IND_CALLSETUP 5 /* Call setup status indicator */ -#define BTA_HF_CLIENT_IND_CALLHELD 6 /* Call hold status indicator */ - +/* indicator constants HFP 1.1 and later */ +#define BTA_HF_CLIENT_IND_CALL 0 /* position of call indicator */ +#define BTA_HF_CLIENT_IND_CALLSETUP 1 /* position of callsetup indicator */ +#define BTA_HF_CLIENT_IND_SERVICE 2 /* position of service indicator */ +/* indicator constants HFP 1.5 and later */ +#define BTA_HF_CLIENT_IND_SIGNAL 3 /* position of signal strength indicator */ +#define BTA_HF_CLIENT_IND_ROAM 4 /* position of roaming indicator */ +#define BTA_HF_CLIENT_IND_BATTCH 5 /* position of battery charge indicator */ +#define BTA_HF_CLIENT_IND_CALLHELD 6 /* position of callheld indicator */ +#define BTA_HF_CLIENT_IND_BEARER 7 /* position of bearer indicator */ typedef UINT8 tBTA_HF_CLIENT_IND_TYPE; /* AT commands */ diff --git a/components/bt/bluedroid/btc/core/btc_ble_storage.c b/components/bt/bluedroid/btc/core/btc_ble_storage.c index c8590ce80..c9b23e82a 100644 --- a/components/bt/bluedroid/btc/core/btc_ble_storage.c +++ b/components/bt/bluedroid/btc/core/btc_ble_storage.c @@ -80,7 +80,7 @@ static void _btc_storage_save(void) //delete device info string_to_bdaddr(need_remove_section, &bd_addr); BTM_SecDeleteDevice(bd_addr.address, BT_TRANSPORT_LE); - //delet config info + //delete config info if(btc_config_remove_section(need_remove_section)) { BTIF_TRACE_WARNING("exceeded the maximum nubmer of bonded devices, delete the last device info : %s", need_remove_section); } @@ -893,11 +893,12 @@ bt_status_t btc_storage_get_bonded_ble_devices_list(esp_ble_bond_dev_t *bond_dev if (_btc_storage_get_ble_bonding_key(&bd_addr, BTM_LE_KEY_PID, buffer, sizeof(tBTM_LE_PID_KEYS)) == BT_STATUS_SUCCESS) { bond_dev->bond_key.key_mask |= ESP_BLE_ID_KEY_MASK; tBTM_LE_PID_KEYS *pid_key = (tBTM_LE_PID_KEYS *) buffer; - memcpy(&bond_dev->bond_key.pid_key.irk, pid_key->irk, BT_OCTET16_LEN); + //Note: The memory size of the pid_key.addr_type in bond_key is different from that of (tBTM_LE_PID_KEYS) *pid_key. + memcpy(&bond_dev->bond_key.pid_key.irk, pid_key->irk, ESP_BT_OCTET16_LEN); bond_dev->bond_key.pid_key.addr_type = pid_key->addr_type; - memcpy(&bond_dev->bond_key.pid_key.static_addr, pid_key->static_addr, sizeof(BD_ADDR)); + memcpy(&bond_dev->bond_key.pid_key.static_addr, pid_key->static_addr, ESP_BD_ADDR_LEN); } - //serch for the next bond device + //search for the next bond device bond_dev++; } btc_config_unlock(); diff --git a/components/bt/bluedroid/btc/core/btc_dm.c b/components/bt/bluedroid/btc/core/btc_dm.c index f3b9caeb4..fbf153b5e 100644 --- a/components/bt/bluedroid/btc/core/btc_dm.c +++ b/components/bt/bluedroid/btc/core/btc_dm.c @@ -706,8 +706,12 @@ void btc_dm_sec_cb_handler(btc_msg_t *msg) pairing_cb.ble.is_pid_key_rcvd = TRUE; memcpy(&pairing_cb.ble.pid_key, &p_data->ble_key.p_key_value->pid_key, sizeof(tBTM_LE_PID_KEYS)); - memcpy(¶m.ble_security.ble_key.p_key_value.pid_key, - &p_data->ble_key.p_key_value->pid_key, sizeof(tBTM_LE_PID_KEYS)); + //Note: The memory size of the addr_type in ble_security.ble_key.p_key_value.pid_key is different from that of p_data->ble_key.p_key_value->pid_key. + memcpy(¶m.ble_security.ble_key.p_key_value.pid_key.irk, + &p_data->ble_key.p_key_value->pid_key.irk, ESP_BT_OCTET16_LEN); + param.ble_security.ble_key.p_key_value.pid_key.addr_type = p_data->ble_key.p_key_value->pid_key.addr_type; + memcpy(¶m.ble_security.ble_key.p_key_value.pid_key.static_addr, + &p_data->ble_key.p_key_value->pid_key.static_addr, ESP_BD_ADDR_LEN); break; } case BTM_LE_KEY_PCSRK: { diff --git a/components/bt/bluedroid/btc/core/btc_main.c b/components/bt/bluedroid/btc/core/btc_main.c index 75a2cd45c..652799802 100644 --- a/components/bt/bluedroid/btc/core/btc_main.c +++ b/components/bt/bluedroid/btc/core/btc_main.c @@ -67,11 +67,18 @@ static void btc_init_bluetooth(void) //load the ble local key which has been stored in the flash btc_dm_load_ble_local_keys(); #endif /* #if (SMP_INCLUDED) */ +#if BTA_DYNAMIC_MEMORY + deinit_semaphore = xSemaphoreCreateBinary(); +#endif /* #if BTA_DYNAMIC_MEMORY */ } static void btc_deinit_bluetooth(void) { + /* Wait for the disable operation to complete */ +#if BTA_DYNAMIC_MEMORY + xSemaphoreTake(deinit_semaphore, BTA_DISABLE_DELAY / portTICK_PERIOD_MS); +#endif /* #if BTA_DYNAMIC_MEMORY */ btc_gap_ble_deinit(); bta_dm_sm_deinit(); #if (GATTC_INCLUDED) @@ -87,6 +94,10 @@ static void btc_deinit_bluetooth(void) osi_alarm_deinit(); osi_alarm_delete_mux(); future_ready(*btc_main_get_future_p(BTC_MAIN_DEINIT_FUTURE), FUTURE_SUCCESS); +#if BTA_DYNAMIC_MEMORY + vSemaphoreDelete(deinit_semaphore); + deinit_semaphore = NULL; +#endif /* #if BTA_DYNAMIC_MEMORY */ } void btc_main_call_handler(btc_msg_t *msg) diff --git a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c index 45c220b3b..5bcc2ebea 100644 --- a/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c +++ b/components/bt/bluedroid/btc/profile/esp/blufi/blufi_prf.c @@ -147,6 +147,7 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) if (blufi_env.prepare_buf == NULL) { blufi_env.prepare_buf = osi_malloc(BLUFI_PREPAIR_BUF_MAX_SIZE); + blufi_env.prepare_len = 0; if (blufi_env.prepare_buf == NULL) { BLUFI_TRACE_ERROR("Blufi prep no mem\n"); status = GATT_NO_RESOURCES; @@ -174,6 +175,7 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) if (blufi_env.prepare_buf) { osi_free(blufi_env.prepare_buf); blufi_env.prepare_buf = NULL; + blufi_env.prepare_len = 0; } BLUFI_TRACE_ERROR("write data error , error code 0x%x\n", status); return; @@ -209,6 +211,7 @@ static void blufi_profile_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) if (blufi_env.prepare_buf) { osi_free(blufi_env.prepare_buf); blufi_env.prepare_buf = NULL; + blufi_env.prepare_len = 0; } break; @@ -426,11 +429,19 @@ static void btc_blufi_recv_handler(uint8_t *data, int len) blufi_env.aggr_buf = osi_malloc(blufi_env.total_len); if (blufi_env.aggr_buf == NULL) { BTC_TRACE_ERROR("%s no mem, len %d\n", __func__, blufi_env.total_len); + btc_blufi_report_error(ESP_BLUFI_DH_MALLOC_ERROR); return; } } - memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data + 2, hdr->data_len - 2); - blufi_env.offset += (hdr->data_len - 2); + if (blufi_env.offset + hdr->data_len - 2 <= blufi_env.total_len){ + memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data + 2, hdr->data_len - 2); + blufi_env.offset += (hdr->data_len - 2); + } else { + BTC_TRACE_ERROR("%s payload is longer than packet length, len %d \n", __func__, blufi_env.total_len); + btc_blufi_report_error(ESP_BLUFI_DATA_FORMAT_ERROR); + return; + } + } else { if (blufi_env.offset > 0) { /* if previous pkt is frag */ memcpy(blufi_env.aggr_buf + blufi_env.offset, hdr->data, hdr->data_len); @@ -585,7 +596,7 @@ static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, u *p++ = info->softap_max_conn_num; } if (info->softap_channel_set) { - *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_MAX_CONN_NUM; + *p++ = BLUFI_TYPE_DATA_SUBTYPE_SOFTAP_CHANNEL; *p++ = 1; *p++ = info->softap_channel; } diff --git a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c index aca3f5d86..d5e1b1ca8 100644 --- a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c +++ b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_control.c @@ -117,7 +117,7 @@ void btc_a2dp_control_media_ctrl(esp_a2d_media_ctrl_t ctrl) APPL_TRACE_DEBUG("BTC MEDIA (A2DP-DATA) EVENT %u", ctrl); if (btc_aa_ctrl_cb.a2dp_cmd_pending != ESP_A2D_MEDIA_CTRL_NONE) { - APPL_TRACE_DEBUG("un-acked a2dp cmd: %u", btc_aa_ctrl_cb.a2dp_cmd_pending); + APPL_TRACE_WARNING("un-acked a2dp cmd: %u", btc_aa_ctrl_cb.a2dp_cmd_pending); a2dp_cmd_acknowledge(ctrl, ESP_A2D_MEDIA_CTRL_ACK_BUSY); return; } @@ -179,6 +179,11 @@ void btc_a2dp_control_media_ctrl(esp_a2d_media_ctrl_t ctrl) /* local suspend */ if (btc_av_stream_started_ready()) { btc_dispatch_sm_event(BTC_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0); +#if (BTC_AV_SINK_INCLUDED == TRUE) + if (btc_av_get_peer_sep() == AVDT_TSEP_SRC && btc_av_get_service_id() == BTA_A2DP_SINK_SERVICE_ID) { + btc_a2dp_control_command_ack(ESP_A2D_MEDIA_CTRL_ACK_SUCCESS); + } +#endif } else { /* we are not in started state; just ack back ok. This can happen if we are remotely suspended; clear REMOTE SUSPEND Flag */ diff --git a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c index 619e0143a..4ad446e46 100644 --- a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c +++ b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_sink.c @@ -828,6 +828,8 @@ static void btc_a2dp_sink_thread_cleanup(UNUSED_ATTR void *context) fixed_queue_free(btc_aa_snk_cb.RxSbcQ, osi_free_func); + btc_aa_snk_cb.RxSbcQ = NULL; + future_ready(btc_a2dp_sink_future, NULL); } diff --git a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c index 5afbd3722..10c6283c4 100644 --- a/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c +++ b/components/bt/bluedroid/btc/profile/std/a2dp/btc_a2dp_source.c @@ -1664,6 +1664,8 @@ static void btc_a2dp_source_thread_cleanup(UNUSED_ATTR void *context) fixed_queue_free(btc_aa_src_cb.TxAaQ, osi_free_func); + btc_aa_src_cb.TxAaQ = NULL; + future_ready(btc_a2dp_source_future, NULL); } diff --git a/components/bt/bluedroid/btc/profile/std/avrc/btc_avrc.c b/components/bt/bluedroid/btc/profile/std/avrc/btc_avrc.c index 539760b92..b62ff866d 100644 --- a/components/bt/bluedroid/btc/profile/std/avrc/btc_avrc.c +++ b/components/bt/bluedroid/btc/profile/std/avrc/btc_avrc.c @@ -251,7 +251,7 @@ static void handle_rc_attributes_rsp ( tAVRC_MSG_VENDOR *vendor_msg) btc_avrc_ct_cb_to_app(ESP_AVRC_CT_METADATA_RSP_EVT, ¶m[i]); - attr_index += (int) vendor_msg->p_vendor_data[7 + attr_index] + 8; + attr_index += attr_length + 8; } } diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c index ef13106dd..6046c57c4 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c @@ -1189,6 +1189,12 @@ void btc_gap_ble_call_handler(btc_msg_t *msg) bta_dm_co_ble_set_max_key_size(key_size); break; } + case ESP_BLE_SM_MIN_KEY_SIZE: { + uint8_t key_size = 0; + STREAM_TO_UINT8(key_size, value); + bta_dm_co_ble_set_min_key_size(key_size); + break; + } case ESP_BLE_SM_SET_STATIC_PASSKEY: { uint32_t passkey = 0; for(uint8_t i = 0; i < arg->set_security_param.len; i++) diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c index a5af828e1..0f9789d52 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -830,11 +830,14 @@ void btc_gap_bt_busy_level_updated(uint8_t bl_flags) param.disc_st_chg.state = ESP_BT_GAP_DISCOVERY_STARTED; btc_gap_bt_cb_to_app(ESP_BT_GAP_DISC_STATE_CHANGED_EVT, ¶m); btc_gap_bt_inquiry_in_progress = true; - } else if (bl_flags == BTM_BL_INQUIRY_CANCELLED || - bl_flags == BTM_BL_INQUIRY_COMPLETE) { + } else if (bl_flags == BTM_BL_INQUIRY_CANCELLED) { param.disc_st_chg.state = ESP_BT_GAP_DISCOVERY_STOPPED; btc_gap_bt_cb_to_app(ESP_BT_GAP_DISC_STATE_CHANGED_EVT, ¶m); btc_gap_bt_inquiry_in_progress = false; + } else if (bl_flags == BTM_BL_INQUIRY_COMPLETE) { + /* The Inquiry Complete event is not transported to app layer, + since the app only cares about the Name Discovery Complete event */ + btc_gap_bt_inquiry_in_progress = false; } } diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c index 4c45f829d..300d802f9 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gattc.c @@ -315,7 +315,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int svc_num = 0; + uint16_t svc_num = 0; tBT_UUID *bta_uuid = NULL; if (svc_uuid) { bta_uuid = osi_malloc(sizeof(tBT_UUID)); @@ -324,7 +324,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc BTA_GATTC_GetServiceWithUUID(conn_id, bta_uuid, &db, &svc_num); - if ((status = btc_gattc_check_valid_param(svc_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)svc_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } @@ -334,7 +334,7 @@ esp_gatt_status_t btc_ble_gattc_get_service(uint16_t conn_id, esp_bt_uuid_t *svc *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, svc_num, ESP_GATT_DB_PRIMARY_SERVICE, offset, (void *)result, db); } *count = svc_num; @@ -356,17 +356,17 @@ esp_gatt_status_t btc_ble_gattc_get_all_char(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int char_num = 0; + uint16_t char_num = 0; BTA_GATTC_GetAllChar(conn_id, start_handle, end_handle, &db, &char_num); - if ((status = btc_gattc_check_valid_param(char_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)char_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, offset, (void *)result, db); } *count = char_num; @@ -384,17 +384,17 @@ esp_gatt_status_t btc_ble_gattc_get_all_descr(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; BTA_GATTC_GetAllDescriptor(conn_id, char_handle, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, offset)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, offset)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, offset, (void *)result, db); } *count = descr_num; @@ -414,19 +414,19 @@ esp_gatt_status_t btc_ble_gattc_get_char_by_uuid(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int char_num = 0; + uint16_t char_num = 0; tBT_UUID bta_uuid = {0}; btc_to_bta_uuid(&bta_uuid, &char_uuid); BTA_GATTC_GetCharByUUID(conn_id, start_handle, end_handle, bta_uuid, &db, &char_num); - if ((status = btc_gattc_check_valid_param(char_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)char_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, char_num, ESP_GATT_DB_CHARACTERISTIC, 0, (void *)result, db); } *count = char_num; @@ -447,7 +447,7 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; tBT_UUID bta_char_uuid = {0}; tBT_UUID bta_descr_uuid = {0}; btc_to_bta_uuid(&bta_char_uuid, &char_uuid); @@ -456,14 +456,14 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_uuid(uint16_t conn_id, BTA_GATTC_GetDescrByUUID(conn_id, start_handle, end_handle, bta_char_uuid, bta_descr_uuid, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); } *count = descr_num; @@ -482,20 +482,20 @@ esp_gatt_status_t btc_ble_gattc_get_descr_by_char_handle(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int descr_num = 0; + uint16_t descr_num = 0; tBT_UUID bta_descr_uuid = {0}; btc_to_bta_uuid(&bta_descr_uuid, &descr_uuid); BTA_GATTC_GetDescrByCharHandle(conn_id, char_handle, bta_descr_uuid, &db, &descr_num); - if ((status = btc_gattc_check_valid_param(descr_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)descr_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; } else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, descr_num, ESP_GATT_DB_DESCRIPTOR, 0, (void *)result, db); } *count = descr_num; @@ -516,7 +516,7 @@ esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id, { esp_gatt_status_t status; btgatt_db_element_t *db = NULL; - int incl_num = 0; + uint16_t incl_num = 0; tBT_UUID bta_uuid = {0}; if (incl_uuid != NULL) { @@ -526,14 +526,14 @@ esp_gatt_status_t btc_ble_gattc_get_include_service(uint16_t conn_id, BTA_GATTC_GetIncludeService(conn_id, start_handle, end_handle, NULL, &db, &incl_num); } - if ((status = btc_gattc_check_valid_param(incl_num, 0)) != ESP_GATT_OK) { + if ((status = btc_gattc_check_valid_param((int)incl_num, 0)) != ESP_GATT_OK) { if (db) { osi_free(db); } *count = 0; return status; }else { - btc_gattc_fill_gatt_db_conversion(*count, (uint16_t)incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db); + btc_gattc_fill_gatt_db_conversion(*count, incl_num, ESP_GATT_DB_INCLUDED_SERVICE, 0, (void *)result, db); } *count = incl_num; @@ -552,9 +552,9 @@ esp_gatt_status_t btc_ble_gattc_get_attr_count(uint16_t conn_id, uint16_t *count) { if (type == ESP_GATT_DB_ALL) { - BTA_GATTC_GetDBSize(conn_id, start_handle, end_handle, (int *)count); + BTA_GATTC_GetDBSize(conn_id, start_handle, end_handle, count); } else { - BTA_GATTC_GetDBSizeByType(conn_id, type, start_handle, end_handle, char_handle, (int *)count); + BTA_GATTC_GetDBSizeByType(conn_id, type, start_handle, end_handle, char_handle, count); } return ESP_GATT_OK; @@ -564,7 +564,7 @@ esp_gatt_status_t btc_ble_gattc_get_db(uint16_t conn_id, uint16_t start_handle, esp_gattc_db_elem_t *db, uint16_t *count) { btgatt_db_element_t *get_db = NULL; - int num = 0; + uint16_t num = 0; tBT_UUID bta_uuid; uint16_t db_size = 0; BTA_GATTC_GetGattDb(conn_id, start_handle, end_handle, &get_db, &num); diff --git a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c index eff4d41a1..3a33b7b1e 100644 --- a/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c +++ b/components/bt/bluedroid/btc/profile/std/gatt/btc_gatts.c @@ -389,7 +389,13 @@ static void btc_gatts_act_create_attr_tab(esp_gatts_attr_db_t *gatts_attr_db, case ESP_GATT_UUID_CHAR_AGG_FORMAT: case ESP_GATT_UUID_CHAR_VALID_RANGE: case ESP_GATT_UUID_EXT_RPT_REF_DESCR: - case ESP_GATT_UUID_RPT_REF_DESCR:{ + case ESP_GATT_UUID_RPT_REF_DESCR: + case ESP_GATT_UUID_NUM_DIGITALS_DESCR: + case ESP_GATT_UUID_VALUE_TRIGGER_DESCR: + case ESP_GATT_UUID_ENV_SENSING_CONFIG_DESCR: + case ESP_GATT_UUID_ENV_SENSING_MEASUREMENT_DESCR: + case ESP_GATT_UUID_ENV_SENSING_TRIGGER_DESCR: + case ESP_GATT_UUID_TIME_TRIGGER_DESCR: { uint16_t svc_hal = btc_creat_tab_env.svc_start_hdl; tBT_UUID bta_char_uuid; esp_bt_uuid_t uuid_temp; @@ -449,7 +455,7 @@ static esp_gatt_status_t btc_gatts_check_valid_attr_tab(esp_gatts_attr_db_t *gat case ESP_GATT_UUID_PRI_SERVICE: case ESP_GATT_UUID_SEC_SERVICE: if (++svc_num > 1) { - BTC_TRACE_ERROR("Each service table can only created one primary service or secondly service."); + BTC_TRACE_ERROR("Each service table can only created one primary service or secondary service."); return ESP_GATT_ERROR; } break; @@ -961,4 +967,13 @@ void btc_gatts_cb_handler(btc_msg_t *msg) btc_gatts_cb_param_copy_free(msg, p_data); } +void btc_congest_callback(tBTA_GATTS *param) +{ + esp_ble_gatts_cb_param_t esp_param; + esp_gatt_if_t gatts_if = BTC_GATT_GET_GATT_IF(param->congest.conn_id); + esp_param.congest.conn_id = BTC_GATT_GET_CONN_ID(param->congest.conn_id); + esp_param.congest.congested = param->congest.congested; + btc_gatts_cb_to_app(ESP_GATTS_CONGEST_EVT, gatts_if, &esp_param); + +} #endif ///GATTS_INCLUDED diff --git a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h index 67e03cb3f..847b93819 100644 --- a/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h +++ b/components/bt/bluedroid/btc/profile/std/include/btc_gap_bt.h @@ -16,6 +16,7 @@ #define __BTC_GAP_BT_H__ #include "common/bt_target.h" +#include "common/bt_defs.h" #include "esp_bt_defs.h" #include "esp_gap_bt_api.h" #include "btc/btc_task.h" diff --git a/components/bt/bluedroid/common/include/common/bt_defs.h b/components/bt/bluedroid/common/include/common/bt_defs.h index 77719bc84..7421e7b14 100644 --- a/components/bt/bluedroid/common/include/common/bt_defs.h +++ b/components/bt/bluedroid/common/include/common/bt_defs.h @@ -21,7 +21,7 @@ #include #include -#include "common/bt_trace.h" +#include "bt_common.h" #include "common/bt_target.h" #define UNUSED(x) (void)(x) @@ -65,31 +65,6 @@ typedef struct { uint8_t uu[16]; } bt_uuid_t; -/** Bluetooth Error Status */ -/** We need to build on this */ - -/* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ -typedef enum { - BT_STATUS_SUCCESS = 0, - BT_STATUS_FAIL, - BT_STATUS_NOT_READY, - BT_STATUS_NOMEM, - BT_STATUS_BUSY, - BT_STATUS_DONE, /* request already completed */ - BT_STATUS_UNSUPPORTED, - BT_STATUS_PARM_INVALID, - BT_STATUS_UNHANDLED, - BT_STATUS_AUTH_FAILURE, - BT_STATUS_RMT_DEV_DOWN, - BT_STATUS_AUTH_REJECTED, - BT_STATUS_INVALID_STATIC_RAND_ADDR, - BT_STATUS_PENDING, - BT_STATUS_UNACCEPT_CONN_INTERVAL, - BT_STATUS_PARAM_OUT_OF_RANGE, - BT_STATUS_TIMEOUT, - BT_STATUS_MEMORY_FULL, -} bt_status_t; - #ifndef CPU_LITTLE_ENDIAN #define CPU_LITTLE_ENDIAN #endif diff --git a/components/bt/bluedroid/common/include/common/bt_target.h b/components/bt/bluedroid/common/include/common/bt_target.h index 7f7027fb9..4504e0cd8 100644 --- a/components/bt/bluedroid/common/include/common/bt_target.h +++ b/components/bt/bluedroid/common/include/common/bt_target.h @@ -20,6 +20,8 @@ #ifndef BT_TARGET_H #define BT_TARGET_H +#include "bt_common.h" + #ifndef BUILDCFG #define BUILDCFG #endif @@ -98,6 +100,10 @@ #define CLASSIC_BT_INCLUDED FALSE #endif /* CLASSIC_BT_INCLUDED */ +#ifndef CLASSIC_BT_GATT_INCLUDED +#define CLASSIC_BT_GATT_INCLUDED FALSE +#endif /* CLASSIC_BT_GATT_INCLUDED */ + #ifndef CONFIG_GATTC_CACHE_NVS_FLASH #define CONFIG_GATTC_CACHE_NVS_FLASH FALSE #endif /* CONFIG_GATTC_CACHE_NVS_FLASH */ @@ -151,13 +157,13 @@ #ifndef CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_NUM #define BLE_ADV_REPORT_FLOW_CONTROL_NUM 100 -#else +#else #define BLE_ADV_REPORT_FLOW_CONTROL_NUM CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_NUM #endif /* CONFIG_BLE_ADV_REPORT_FLOW_CONTROL_NUM */ #ifndef CONFIG_BLE_ADV_REPORT_DISCARD_THRSHOLD #define BLE_ADV_REPORT_DISCARD_THRSHOLD 20 -#else +#else #define BLE_ADV_REPORT_DISCARD_THRSHOLD CONFIG_BLE_ADV_REPORT_DISCARD_THRSHOLD #endif /* CONFIG_BLE_ADV_REPORT_DISCARD_THRSHOLD */ @@ -345,10 +351,6 @@ #define BTA_AV_CO_CP_SCMS_T FALSE//FALSE #endif -#ifndef QUEUE_CONGEST_SIZE -#define QUEUE_CONGEST_SIZE 40 -#endif - #ifndef CONFIG_BLE_HOST_QUEUE_CONGESTION_CHECK #define SCAN_QUEUE_CONGEST_CHECK FALSE #else diff --git a/components/bt/bluedroid/common/include/common/bt_trace.h b/components/bt/bluedroid/common/include/common/bt_trace.h index 7583479b2..780ff2d08 100644 --- a/components/bt/bluedroid/common/include/common/bt_trace.h +++ b/components/bt/bluedroid/common/include/common/bt_trace.h @@ -23,38 +23,7 @@ #include #include "stack/bt_types.h" - -#ifndef LOG_LOCAL_LEVEL -#ifndef BOOTLOADER_BUILD -#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL -#else -#define LOG_LOCAL_LEVEL CONFIG_LOG_BOOTLOADER_LEVEL -#endif -#endif - -#include "esp_log.h" - -// Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL -#if (LOG_LOCAL_LEVEL >= 4) -#define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) -#else -#define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL -#endif - -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) - -//#define TAG "BT" - -#define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } -#define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } - -#ifndef assert -#define assert(x) do { if (!(x)) BT_PRINT_E("bt host error %s %u\n", __FILE__, __LINE__); } while (0) -#endif +#include "bt_common.h" inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) { @@ -324,18 +293,6 @@ inline void trc_dump_buffer(const char *prefix, uint8_t *data, uint16_t len) #define BTIF_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING #endif -#ifdef CONFIG_BTC_INITIAL_TRACE_LEVEL -#define BTC_INITIAL_TRACE_LEVEL CONFIG_BTC_INITIAL_TRACE_LEVEL -#else -#define BTC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING -#endif - -#ifdef CONFIG_OSI_INITIAL_TRACE_LEVEL -#define OSI_INITIAL_TRACE_LEVEL CONFIG_OSI_INITIAL_TRACE_LEVEL -#else -#define OSI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING -#endif - #ifdef CONFIG_BLUFI_INITIAL_TRACE_LEVEL #define BLUFI_INITIAL_TRACE_LEVEL CONFIG_BLUFI_INITIAL_TRACE_LEVEL #else @@ -497,22 +454,6 @@ extern UINT8 btif_trace_level; #define HCI_TRACE_EVENT(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(HCI,EVENT)) BT_PRINT_D("BT_HCI", fmt,## args);} #define HCI_TRACE_DEBUG(fmt, args...) {if (HCI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(HCI,DEBUG)) BT_PRINT_D("BT_HCI", fmt,## args);} -/* define traces for BTC */ -#define BTC_TRACE_ERROR(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BTC, ERROR)) BT_PRINT_E("BT_BTC", fmt, ## args);} -#define BTC_TRACE_WARNING(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BTC, WARNING)) BT_PRINT_W("BT_BTC", fmt, ## args);} -#define BTC_TRACE_API(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BTC,API)) BT_PRINT_I("BT_BTC", fmt, ## args);} -#define BTC_TRACE_EVENT(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BTC,EVENT)) BT_PRINT_D("BT_BTC", fmt, ## args);} -#define BTC_TRACE_DEBUG(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BTC,DEBUG)) BT_PRINT_D("BT_BTC", fmt, ## args);} -#define BTC_TRACE_VERBOSE(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BTC,VERBOSE)) BT_PRINT_V("BT_BTC", fmt, ## args);} - -/* define traces for OSI */ -#define OSI_TRACE_ERROR(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(OSI, ERROR)) BT_PRINT_E("BT_OSI", fmt, ## args);} -#define OSI_TRACE_WARNING(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(OSI, WARNING)) BT_PRINT_W("BT_OSI", fmt, ## args);} -#define OSI_TRACE_API(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(OSI,API)) BT_PRINT_I("BT_OSI", fmt, ## args);} -#define OSI_TRACE_EVENT(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(OSI,EVENT)) BT_PRINT_D("BT_OSI", fmt, ## args);} -#define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);} -#define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);} - /* define traces for BLUFI */ #define BLUFI_TRACE_ERROR(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BLUFI, ERROR)) BT_PRINT_E("BT_BLUFI", fmt, ## args);} #define BLUFI_TRACE_WARNING(fmt, args...) {if (BLUFI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BLUFI, WARNING)) BT_PRINT_W("BT_BLUFI", fmt, ## args);} @@ -671,22 +612,6 @@ extern UINT8 btif_trace_level; #define APPL_TRACE_DEBUG(fmt, args...) #define APPL_TRACE_VERBOSE(fmt, args...) -/* define traces for BTC */ -#define BTC_TRACE_ERROR(fmt, args...) -#define BTC_TRACE_WARNING(fmt, args...) -#define BTC_TRACE_API(fmt, args...) -#define BTC_TRACE_EVENT(fmt, args...) -#define BTC_TRACE_DEBUG(fmt, args...) -#define BTC_TRACE_VERBOSE(fmt, args...) - -/* define traces for OSI */ -#define OSI_TRACE_ERROR(fmt, args...) -#define OSI_TRACE_WARNING(fmt, args...) -#define OSI_TRACE_API(fmt, args...) -#define OSI_TRACE_EVENT(fmt, args...) -#define OSI_TRACE_DEBUG(fmt, args...) -#define OSI_TRACE_VERBOSE(fmt, args...) - /* define traces for BLUFI */ #define BLUFI_TRACE_ERROR(fmt, args...) #define BLUFI_TRACE_WARNING(fmt, args...) diff --git a/components/bt/bluedroid/common/include/common/bte_appl.h b/components/bt/bluedroid/common/include/common/bte_appl.h index 4810bd6ff..67f410835 100644 --- a/components/bt/bluedroid/common/include/common/bte_appl.h +++ b/components/bt/bluedroid/common/include/common/bte_appl.h @@ -31,6 +31,7 @@ typedef struct { UINT8 ble_init_key; UINT8 ble_resp_key; UINT8 ble_max_key_size; + UINT8 ble_min_key_size; UINT8 ble_accept_auth_enable; UINT8 oob_support; #endif @@ -48,4 +49,4 @@ typedef struct { #endif } tBTE_BT_APPL_CFG; -extern tBTE_BT_APPL_CFG bte_bt_appl_cfg; \ No newline at end of file +extern tBTE_BT_APPL_CFG bte_bt_appl_cfg; diff --git a/components/bt/bluedroid/hci/hci_hal_h4.c b/components/bt/bluedroid/hci/hci_hal_h4.c index 89fba87b5..742a448db 100644 --- a/components/bt/bluedroid/hci/hci_hal_h4.c +++ b/components/bt/bluedroid/hci/hci_hal_h4.c @@ -189,10 +189,10 @@ task_post_status_t hci_hal_h4_task_post(task_post_t timeout) evt.par = 0; if (xQueueSend(xHciH4Queue, &evt, timeout) != pdTRUE) { - return TASK_POST_SUCCESS; + return TASK_POST_FAIL; } - return TASK_POST_FAIL; + return TASK_POST_SUCCESS; } #if (C2H_FLOW_CONTROL_INCLUDED == TRUE) diff --git a/components/bt/bluedroid/hci/packet_fragmenter.c b/components/bt/bluedroid/hci/packet_fragmenter.c index 87cb99c74..a2b62f2d8 100644 --- a/components/bt/bluedroid/hci/packet_fragmenter.c +++ b/components/bt/bluedroid/hci/packet_fragmenter.c @@ -88,6 +88,13 @@ static void fragment_and_dispatch(BT_HDR *packet) controller->get_acl_data_size_ble(); max_packet_size = max_data_size + HCI_ACL_PREAMBLE_SIZE; + if((packet->len > max_packet_size) && (packet->layer_specific == 0) && (event == MSG_STACK_TO_HC_HCI_ACL)) { + packet->event = MSG_HC_TO_STACK_L2C_SEG_XMIT; + current_fragment_packet = NULL; + callbacks->transmit_finished(packet, false); + return; + + } remaining_length = packet->len; STREAM_TO_UINT16(continuation_handle, stream); continuation_handle = APPLY_CONTINUATION_FLAG(continuation_handle); diff --git a/components/bt/bluedroid/main/bte_init.c b/components/bt/bluedroid/main/bte_init.c index 38cfc812f..5477e9dd8 100644 --- a/components/bt/bluedroid/main/bte_init.c +++ b/components/bt/bluedroid/main/bte_init.c @@ -366,4 +366,9 @@ void BTE_DeinitStack(void) #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE) A2D_Deinit(); #endif + +#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE) + RFCOMM_Deinit(); +#endif + } diff --git a/components/bt/bluedroid/stack/avdt/avdt_api.c b/components/bt/bluedroid/stack/avdt/avdt_api.c index 88d374130..808bd3124 100644 --- a/components/bt/bluedroid/stack/avdt/avdt_api.c +++ b/components/bt/bluedroid/stack/avdt/avdt_api.c @@ -1195,7 +1195,7 @@ UINT16 AVDT_SendReport(UINT8 handle, AVDT_REPORT_TYPE type, len = AVDT_MAX_CNAME_SIZE; } *p++ = (UINT8)len; - BCM_STRNCPY_S((char *)p, len + 1, (char *)p_data->cname, len + 1); + BCM_STRNCPY_S((char *)p, (char *)p_data->cname, AVDT_MAX_CNAME_SIZE + 1); p += len; break; } diff --git a/components/bt/bluedroid/stack/avdt/avdt_l2c.c b/components/bt/bluedroid/stack/avdt/avdt_l2c.c index 3227fa69b..c7c1e58ac 100644 --- a/components/bt/bluedroid/stack/avdt/avdt_l2c.c +++ b/components/bt/bluedroid/stack/avdt/avdt_l2c.c @@ -127,7 +127,7 @@ static void avdt_sec_check_complete_term (BD_ADDR bd_addr, tBT_TRANSPORT transpo ** Returns void ** *******************************************************************************/ -static void avdt_sec_check_complete_orig (BD_ADDR bd_addr, tBT_TRANSPORT trasnport, +static void avdt_sec_check_complete_orig (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, UINT8 res) { tAVDT_CCB *p_ccb = NULL; diff --git a/components/bt/bluedroid/stack/btm/btm_acl.c b/components/bt/bluedroid/stack/btm/btm_acl.c index e42724f23..8129e903a 100644 --- a/components/bt/bluedroid/stack/btm/btm_acl.c +++ b/components/bt/bluedroid/stack/btm/btm_acl.c @@ -2069,7 +2069,7 @@ void BTM_BleGetWhiteListSize(uint16_t *length) { tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb; if (p_cb->white_list_avail_size == 0) { - BTM_TRACE_DEBUG("%s Whitelist full.", __func__); + BTM_TRACE_WARNING("%s Whitelist full.", __func__); } *length = p_cb->white_list_avail_size; return; diff --git a/components/bt/bluedroid/stack/btm/btm_ble.c b/components/bt/bluedroid/stack/btm/btm_ble.c index 3d8a36d04..db3d75119 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble.c +++ b/components/bt/bluedroid/stack/btm/btm_ble.c @@ -121,8 +121,7 @@ BOOLEAN BTM_SecAddBleDevice (BD_ADDR bd_addr, BD_NAME bd_name, tBT_DEVICE_TYPE d if (bd_name && bd_name[0]) { p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN; - BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name), - (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN); + BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name,(char *)bd_name, BTM_MAX_REM_BD_NAME_LEN); } p_dev_rec->device_type |= dev_type; p_dev_rec->ble.ble_addr_type = addr_type; diff --git a/components/bt/bluedroid/stack/btm/btm_ble_adv_filter.c b/components/bt/bluedroid/stack/btm/btm_ble_adv_filter.c index 578e2c5db..81b6d129f 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_adv_filter.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_adv_filter.c @@ -913,7 +913,7 @@ tBTM_STATUS btm_ble_clear_scan_pf_filter(tBTM_BLE_SCAN_COND_OP action, /* clear the general filter entry */ if (NULL == p_target) { - /* clear manufactuer data filter */ + /* clear manufacturer data filter */ st = btm_ble_update_pf_manu_data(BTM_BLE_SCAN_COND_CLEAR, filt_index, NULL, BTM_BLE_PF_MANU_DATA, cb_evt, ref_value); if (BTM_CMD_STARTED == st) { @@ -1205,7 +1205,7 @@ tBTM_STATUS BTM_BleCfgFilterCondition(tBTM_BLE_SCAN_COND_OP action, st = btm_ble_update_addr_filter(action, filt_index, p_cond); break; - /* filter on service/solicitated UUID */ + /* filter on service/solicited UUID */ case BTM_BLE_PF_SRVC_UUID: case BTM_BLE_PF_SRVC_SOL_UUID: st = btm_ble_update_uuid_filter(action, filt_index, cond_type, p_cond, 0, ref_value); diff --git a/components/bt/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/bluedroid/stack/btm/btm_ble_gap.c index b3353eddc..fa4399149 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_gap.c @@ -586,7 +586,7 @@ tBTM_STATUS BTM_BleBroadcast(BOOLEAN start, tBTM_START_STOP_ADV_CMPL_CBACK *p_s } #endif - if (start && p_cb->adv_mode == BTM_BLE_ADV_DISABLE) { + if (start) { /* update adv params */ if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min : BTM_BLE_GAP_ADV_INT), @@ -606,19 +606,13 @@ tBTM_STATUS BTM_BleBroadcast(BOOLEAN start, tBTM_START_STOP_ADV_CMPL_CBACK *p_s } status = btm_ble_start_adv (); - } else if (!start && p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { + } else { //save the stop adv callback to the BTM env. p_cb->p_stop_adv_cb = p_stop_adv_cback; status = btm_ble_stop_adv(); #if BLE_PRIVACY_SPT == TRUE btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE); #endif - } else { - /* - 1. start adv when adv has already started (not used) - 2. stop adv shen adv has already stoped - */ - status = BTM_SUCCESS; } return status; } @@ -3920,7 +3914,7 @@ tBTM_STATUS btm_ble_stop_adv(void) { tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var; tBTM_STATUS rt = BTM_SUCCESS; - if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { + if (p_cb) { osi_mutex_lock(&adv_enable_lock, OSI_MUTEX_MAX_TIMEOUT); UINT8 temp_adv_mode = p_cb->adv_mode; BOOLEAN temp_fast_adv_on = p_cb->fast_adv_on; diff --git a/components/bt/bluedroid/stack/btm/btm_ble_privacy.c b/components/bt/bluedroid/stack/btm/btm_ble_privacy.c index 71f06eb82..bbe9f4128 100644 --- a/components/bt/bluedroid/stack/btm/btm_ble_privacy.c +++ b/components/bt/bluedroid/stack/btm/btm_ble_privacy.c @@ -292,8 +292,11 @@ void btm_ble_add_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len) } } else if (status == HCI_ERR_MEMORY_FULL) { /* BT_ERROR_CODE_MEMORY_CAPACITY_EXCEEDED */ btm_cb.ble_ctr_cb.resolving_list_avail_size = 0; - BTM_TRACE_DEBUG("%s Resolving list Full ", __func__); + BTM_TRACE_WARNING("%s Resolving list Full ", __func__); + } else { + BTM_TRACE_ERROR("%s Add resolving list error %d ", __func__, status); } + } /******************************************************************************* @@ -811,6 +814,8 @@ BOOLEAN btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC *p_dev_rec) } else { btm_ble_enable_resolving_list(BTM_BLE_RL_INIT); } + } else { + BTM_TRACE_WARNING("%s Resolving list full ", __func__); } } else { BTM_TRACE_DEBUG("Device already in Resolving list\n"); diff --git a/components/bt/bluedroid/stack/btm/btm_dev.c b/components/bt/bluedroid/stack/btm/btm_dev.c index 0d96bea6f..2e877a85e 100644 --- a/components/bt/bluedroid/stack/btm/btm_dev.c +++ b/components/bt/bluedroid/stack/btm/btm_dev.c @@ -81,7 +81,7 @@ BOOLEAN BTM_SecAddDevice (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, p_dev_rec->sec_flags = BTM_SEC_IN_USE; memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN); p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR); - + p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE); #if BLE_INCLUDED == TRUE /* use default value for background connection params */ /* update conn params, use default value for background connection params */ @@ -107,8 +107,7 @@ BOOLEAN BTM_SecAddDevice (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, if (bd_name && bd_name[0]) { p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN; - BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name), - (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN); + BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN); } p_dev_rec->num_read_pages = 0; diff --git a/components/bt/bluedroid/stack/btm/btm_devctl.c b/components/bt/bluedroid/stack/btm/btm_devctl.c index dfd2c3bf6..f34165b3b 100644 --- a/components/bt/bluedroid/stack/btm/btm_devctl.c +++ b/components/bt/bluedroid/stack/btm/btm_devctl.c @@ -457,7 +457,7 @@ tBTM_STATUS BTM_SetLocalDeviceName (char *p_name) /* Save the device name if local storage is enabled */ p = (UINT8 *)btm_cb.cfg.bd_name; if (p != (UINT8 *)p_name) { - BCM_STRNCPY_S(btm_cb.cfg.bd_name, sizeof(btm_cb.cfg.bd_name), p_name, BTM_MAX_LOC_BD_NAME_LEN); + BCM_STRNCPY_S(btm_cb.cfg.bd_name, p_name, BTM_MAX_LOC_BD_NAME_LEN); btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0'; } #else diff --git a/components/bt/bluedroid/stack/btm/btm_sec.c b/components/bt/bluedroid/stack/btm/btm_sec.c index 1ad3cfce3..877d3d6d2 100644 --- a/components/bt/bluedroid/stack/btm/btm_sec.c +++ b/components/bt/bluedroid/stack/btm/btm_sec.c @@ -583,7 +583,7 @@ static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, const char if (is_originator) { p_srec->orig_mx_chan_id = mx_chan_id; #if BTM_SEC_SERVICE_NAME_LEN > 0 - BCM_STRNCPY_S ((char *)p_srec->orig_service_name, sizeof(p_srec->orig_service_name), p_name, BTM_SEC_SERVICE_NAME_LEN); + BCM_STRNCPY_S ((char *)p_srec->orig_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN); #endif /* clear out the old setting, just in case it exists */ #if (L2CAP_UCD_INCLUDED == TRUE) @@ -628,7 +628,7 @@ static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, const char } else { p_srec->term_mx_chan_id = mx_chan_id; #if BTM_SEC_SERVICE_NAME_LEN > 0 - BCM_STRNCPY_S ((char *)p_srec->term_service_name, sizeof(p_srec->term_service_name), p_name, BTM_SEC_SERVICE_NAME_LEN); + BCM_STRNCPY_S ((char *)p_srec->term_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN); #endif /* clear out the old setting, just in case it exists */ #if (L2CAP_UCD_INCLUDED == TRUE) @@ -3010,7 +3010,7 @@ void btm_sec_rmt_name_request_complete (UINT8 *p_bd_addr, UINT8 *p_bd_name, UINT if (p_dev_rec) { old_sec_state = p_dev_rec->sec_state; if (status == HCI_SUCCESS) { - BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name), (char *)p_bd_name, BTM_MAX_REM_BD_NAME_LEN); + BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)p_bd_name, BTM_MAX_REM_BD_NAME_LEN); p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN; BTM_TRACE_EVENT ("setting BTM_SEC_NAME_KNOWN sec_flags:0x%x\n", p_dev_rec->sec_flags); } else { @@ -3506,7 +3506,7 @@ void btm_proc_sp_req_evt (tBTM_SP_EVT event, UINT8 *p) memcpy (evt_data.cfm_req.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN); memcpy (evt_data.cfm_req.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN); - BCM_STRNCPY_S ((char *)evt_data.cfm_req.bd_name, sizeof(evt_data.cfm_req.bd_name), (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN); + BCM_STRNCPY_S ((char *)evt_data.cfm_req.bd_name, (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN); switch (event) { case BTM_SP_CFM_REQ_EVT: @@ -3731,7 +3731,7 @@ void btm_rem_oob_req (UINT8 *p) btm_cb.api.p_sp_callback) { memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN); memcpy (evt_data.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN); - BCM_STRNCPY_S((char *)evt_data.bd_name, sizeof(evt_data.bd_name), (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN + 1); + BCM_STRNCPY_S((char *)evt_data.bd_name, (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN + 1); evt_data.bd_name[BTM_MAX_REM_BD_NAME_LEN] = 0; btm_sec_change_pairing_state(BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP); diff --git a/components/bt/bluedroid/stack/btm/include/btm_int.h b/components/bt/bluedroid/stack/btm/include/btm_int.h index 47ddfd344..99f5bc7a8 100644 --- a/components/bt/bluedroid/stack/btm/include/btm_int.h +++ b/components/bt/bluedroid/stack/btm/include/btm_int.h @@ -1106,7 +1106,7 @@ void btm_sec_link_key_notification (UINT8 *p_bda, UINT8 *p_link_key, UINT8 key_ void btm_sec_link_key_request (UINT8 *p_bda); void btm_sec_pin_code_request (UINT8 *p_bda); void btm_sec_update_clock_offset (UINT16 handle, UINT16 clock_offset); -void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_trasnport); +void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_transport); void btm_sec_set_peer_sec_caps (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec); #if BLE_INCLUDED == TRUE diff --git a/components/bt/bluedroid/stack/btu/btu_init.c b/components/bt/bluedroid/stack/btu/btu_init.c index 7014cfde0..f9c3d12c3 100644 --- a/components/bt/bluedroid/stack/btu/btu_init.c +++ b/components/bt/bluedroid/stack/btu/btu_init.c @@ -240,7 +240,7 @@ UINT16 BTU_BleAclPktSize(void) bool BTU_check_queue_is_congest(void) { UBaseType_t wait_size = uxQueueMessagesWaiting(xBtuQueue); - if(wait_size >= QUEUE_CONGEST_SIZE ) { + if(wait_size >= BT_QUEUE_CONGEST_SIZE ) { return true; } return false; diff --git a/components/bt/bluedroid/stack/gatt/att_protocol.c b/components/bt/bluedroid/stack/gatt/att_protocol.c index adbedab52..15ca89f91 100644 --- a/components/bt/bluedroid/stack/gatt/att_protocol.c +++ b/components/bt/bluedroid/stack/gatt/att_protocol.c @@ -455,7 +455,7 @@ BT_HDR *attp_build_sr_msg(tGATT_TCB *p_tcb, UINT8 op_code, tGATT_SR_MSG *p_msg) ** Parameter p_tcb: pointer to the connecton control block. ** p_msg: pointer to message parameters structure. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** ** *******************************************************************************/ @@ -526,7 +526,7 @@ tGATT_STATUS attp_cl_send_cmd(tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 cmd_code, ** op_code: message op code. ** p_msg: pointer to message parameters structure. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** ** *******************************************************************************/ diff --git a/components/bt/bluedroid/stack/gatt/gatt_api.c b/components/bt/bluedroid/stack/gatt/gatt_api.c index 7f360d52d..1cb0ee115 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_api.c +++ b/components/bt/bluedroid/stack/gatt/gatt_api.c @@ -80,7 +80,7 @@ UINT8 GATT_SetTraceLevel (UINT8 new_level) ** ** Parameter p_hndl_range: pointer to allocated handles information ** -** Returns TRUE if handle range is added sucessfully; otherwise FALSE. +** Returns TRUE if handle range is added successfully; otherwise FALSE. ** *******************************************************************************/ @@ -382,7 +382,7 @@ BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, UINT16 svc_ GATT_TRACE_DEBUG ("GATTS_DeleteService"); if (p_reg == NULL) { - GATT_TRACE_ERROR ("Applicaiton not foud"); + GATT_TRACE_ERROR ("Application not found"); return (FALSE); } p_app_uuid128 = &p_reg->app_uuid128; @@ -434,7 +434,7 @@ BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, UINT16 svc_ ** p_cback : application service callback functions. ** sup_transport : supported transport(s) for this primary service ** -** return GATT_SUCCESS if sucessfully started; otherwise error code. +** return GATT_SUCCESS if successfully started; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle, @@ -443,9 +443,9 @@ tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle, tGATT_SR_REG *p_sreg; tGATT_HDL_LIST_ELEM *p_list = NULL; UINT8 i_sreg; -#if (SDP_INCLUDED == TRUE) +#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE) tBT_UUID *p_uuid; -#endif ///SDP_INCLUDED == TRUE +#endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE tGATT_REG *p_reg = gatt_get_regcb(gatt_if); tGATTS_PENDING_NEW_SRV_START *p_buf; @@ -454,7 +454,7 @@ tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle, if (p_reg == NULL) { /* Not found */ - GATT_TRACE_ERROR ("Applicaiton not found "); + GATT_TRACE_ERROR ("Application not found "); return GATT_NOT_FOUND; } @@ -484,10 +484,10 @@ tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle, case GATT_TRANSPORT_BR_EDR: case GATT_TRANSPORT_LE_BR_EDR: if (p_sreg->type == GATT_UUID_PRI_SERVICE) { -#if (SDP_INCLUDED == TRUE) +#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE) p_uuid = gatts_get_service_uuid (p_sreg->p_db); p_sreg->sdp_handle = gatt_add_sdp_record(p_uuid, p_sreg->s_hdl, p_sreg->e_hdl); -#endif ///SDP_INCLUDED == TRUE +#endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE } break; default: @@ -539,11 +539,11 @@ void GATTS_StopService (UINT16 service_handle) /* Index 0 is reserved for GATT, and is never stopped */ if ( (ii > 0) && (ii < GATT_MAX_SR_PROFILES) && (gatt_cb.sr_reg[ii].in_use) ) { -#if(SDP_INCLUDED == TRUE) +#if(SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE) if (gatt_cb.sr_reg[ii].sdp_handle) { SDP_DeleteRecord(gatt_cb.sr_reg[ii].sdp_handle); } -#endif ///SDP_INCLUDED == TRUE +#endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE gatt_remove_a_srv_from_list(&gatt_cb.srv_list_info, &gatt_cb.srv_list[ii]); gatt_cb.srv_list[ii].in_use = FALSE; memset (&gatt_cb.sr_reg[ii], 0, sizeof(tGATT_SR_REG)); @@ -562,7 +562,7 @@ void GATTS_StopService (UINT16 service_handle) ** val_len: Length of the indicated attribute value. ** p_val: Pointer to the indicated attribute value data. ** -** Returns GATT_SUCCESS if sucessfully sent or queued; otherwise error code. +** Returns GATT_SUCCESS if successfully sent or queued; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, UINT16 val_len, UINT8 *p_val) @@ -629,7 +629,7 @@ tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, U ** val_len: Length of the indicated attribute value. ** p_val: Pointer to the indicated attribute value data. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_handle, @@ -677,7 +677,7 @@ tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_handle, ** status: response status ** p_msg: pointer to message parameters structure. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id, @@ -1054,7 +1054,7 @@ tGATT_STATUS GATTC_Write (UINT16 conn_id, tGATT_WRITE_TYPE type, tGATT_VALUE *p_ ** the server. ** ** Parameters conn_id: connection identifier. -** is_execute - to execute or cancel the prepare write requet(s) +** is_execute - to execute or cancel the prepare write request(s) ** ** Returns GATT_SUCCESS if command started successfully. ** @@ -1232,7 +1232,7 @@ tGATT_IF GATT_Register (tBT_UUID *p_app_uuid128, tGATT_CBACK *p_cb_info) ** ** Description This function deregistered the application from GATT. ** -** Parameters gatt_if: applicaiton interface. +** Parameters gatt_if: application interface. ** ** Returns None. ** @@ -1309,7 +1309,7 @@ void GATT_Deregister (tGATT_IF gatt_if) ** callbacks for registered interface. Function may call back ** with connection status and queued notifications ** -** Parameter gatt_if: applicaiton interface. +** Parameter gatt_if: application interface. ** ** Returns None. ** @@ -1342,13 +1342,13 @@ void GATT_StartIf (tGATT_IF gatt_if) ** ** Function GATT_Connect ** -** Description This function initiate a connecttion to a remote device on GATT +** Description This function initiate a connection to a remote device on GATT ** channel. ** -** Parameters gatt_if: applicaiton interface +** Parameters gatt_if: application interface ** bd_addr: peer device address. ** bd_addr_type: peer device address type. -** is_direct: is a direct conenection or a background auto connection +** is_direct: is a direct connection or a background auto connection ** ** Returns TRUE if connection started; FALSE if connection start failure. ** @@ -1532,10 +1532,10 @@ tGATT_STATUS GATT_SendServiceChangeIndication (BD_ADDR bd_addr) ** interface ** ** Parameters conn_id: connection id (input) -** p_gatt_if: applicaiton interface (output) +** p_gatt_if: application interface (output) ** bd_addr: peer device address. (output) ** -** Returns TRUE the ligical link information is found for conn_id +** Returns TRUE the logical link information is found for conn_id ** *******************************************************************************/ BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, BD_ADDR bd_addr, @@ -1567,7 +1567,7 @@ BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, BD_ADDR bd_ ** Description This function find the conn_id if the logical link for BD address ** and applciation interface is connected ** -** Parameters gatt_if: applicaiton interface (input) +** Parameters gatt_if: application interface (input) ** bd_addr: peer device address. (input) ** p_conn_id: connection id (output) ** transport: transport option @@ -1599,7 +1599,7 @@ BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, UINT16 *p_c ** Description This function start or stop LE advertisement and listen for ** connection. ** -** Parameters gatt_if: applicaiton interface +** Parameters gatt_if: application interface ** p_bd_addr: listen for specific address connection, or NULL for ** listen to all device connection. ** start: start or stop listening. diff --git a/components/bt/bluedroid/stack/gatt/gatt_db.c b/components/bt/bluedroid/stack/gatt/gatt_db.c index 8a0511748..e0ee0119c 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_db.c +++ b/components/bt/bluedroid/stack/gatt/gatt_db.c @@ -392,7 +392,7 @@ tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb, p_rsp->len += (len + 2); *p_len -= (len + 2); } else { - GATT_TRACE_ERROR("format mismatch"); + GATT_TRACE_WARNING("format mismatch"); status = GATT_NO_RESOURCES; break; } diff --git a/components/bt/bluedroid/stack/gatt/gatt_main.c b/components/bt/bluedroid/stack/gatt/gatt_main.c index af07313a5..fd300b381 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_main.c +++ b/components/bt/bluedroid/stack/gatt/gatt_main.c @@ -47,7 +47,7 @@ static void gatt_le_connect_cback (UINT16 chan, BD_ADDR bd_addr, BOOLEAN connect UINT16 reason, tBT_TRANSPORT transport); static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf); static void gatt_le_cong_cback(BD_ADDR remote_bda, BOOLEAN congest); -#if (CLASSIC_BT_INCLUDED == TRUE) +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id); static void gatt_l2cif_connect_cfm_cback (UINT16 l2cap_cid, UINT16 result); @@ -56,9 +56,9 @@ static void gatt_l2cif_config_cfm_cback (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cf static void gatt_l2cif_disconnect_ind_cback (UINT16 l2cap_cid, BOOLEAN ack_needed); static void gatt_l2cif_disconnect_cfm_cback (UINT16 l2cap_cid, UINT16 result); static void gatt_l2cif_data_ind_cback (UINT16 l2cap_cid, BT_HDR *p_msg); -#endif ///CLASSIC_BT_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE static void gatt_send_conn_cback (tGATT_TCB *p_tcb); -#if (CLASSIC_BT_INCLUDED == TRUE) +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) static void gatt_l2cif_congest_cback (UINT16 cid, BOOLEAN congested); static const tL2CAP_APPL_INFO dyn_info = { gatt_l2cif_connect_ind_cback, @@ -73,7 +73,7 @@ static const tL2CAP_APPL_INFO dyn_info = { gatt_l2cif_congest_cback, NULL } ; -#endif ///SMP_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE #if GATT_DYNAMIC_MEMORY == FALSE tGATT_CB gatt_cb; @@ -125,12 +125,13 @@ void gatt_init (void) fixed_reg.default_idle_tout = 0xffff; /* 0xffff default idle timeout */ L2CA_RegisterFixedChannel (L2CAP_ATT_CID, &fixed_reg); -#if (CLASSIC_BT_INCLUDED == TRUE) + +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) /* Now, register with L2CAP for ATT PSM over BR/EDR */ if (!L2CA_Register (BT_PSM_ATT, (tL2CAP_APPL_INFO *) &dyn_info)) { GATT_TRACE_ERROR ("ATT Dynamic Registration failed"); } -#endif ///CLASSIC_BT_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE BTM_SetSecurityLevel(TRUE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0); BTM_SetSecurityLevel(FALSE, "", BTM_SEC_SERVICE_ATT, BTM_SEC_NONE, BT_PSM_ATT, 0, 0); @@ -221,12 +222,12 @@ BOOLEAN gatt_connect (BD_ADDR rem_bda, tBLE_ADDR_TYPE bd_addr_type, tGATT_TCB *p if (transport == BT_TRANSPORT_LE) { p_tcb->att_lcid = L2CAP_ATT_CID; gatt_ret = L2CA_ConnectFixedChnl (L2CAP_ATT_CID, rem_bda, bd_addr_type); -#if (CLASSIC_BT_INCLUDED == TRUE) +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) } else { if ((p_tcb->att_lcid = L2CA_ConnectReq(BT_PSM_ATT, rem_bda)) != 0) { gatt_ret = TRUE; } -#endif ///CLASSIC_BT_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE } @@ -262,10 +263,10 @@ BOOLEAN gatt_disconnect (tGATT_TCB *p_tcb) gatt_set_ch_state(p_tcb, GATT_CH_CLOSING); ret = L2CA_CancelBleConnectReq (p_tcb->peer_bda); } -#if (CLASSIC_BT_INCLUDED == TRUE) +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) } else { ret = L2CA_DisconnectReq(p_tcb->att_lcid); -#endif ///CLASSIC_BT_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE } } else { GATT_TRACE_DEBUG ("gatt_disconnect already in closing state"); @@ -581,7 +582,7 @@ static void gatt_le_data_ind (UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf) ** Returns void ** *******************************************************************************/ -#if (CLASSIC_BT_INCLUDED == TRUE) +#if (CLASSIC_BT_GATT_INCLUDED == TRUE) static void gatt_l2cif_connect_ind_cback (BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id) { /* do we already have a control channel for this peer? */ @@ -887,7 +888,7 @@ static void gatt_l2cif_congest_cback (UINT16 lcid, BOOLEAN congested) } } -#endif ///CLASSIC_BT_INCLUDED == TRUE +#endif ///CLASSIC_BT_GATT_INCLUDED == TRUE /******************************************************************************* ** @@ -1027,7 +1028,7 @@ void gatt_add_a_bonded_dev_for_srv_chg (BD_ADDR bda) ** Description This function is called to send a service changed indication to ** the specified bd address ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code +** Returns GATT_SUCCESS if successfully sent; otherwise error code ** *******************************************************************************/ #if (GATTS_INCLUDED == TRUE) diff --git a/components/bt/bluedroid/stack/gatt/gatt_sr.c b/components/bt/bluedroid/stack/gatt/gatt_sr.c index eccc9ec97..215c95e77 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_sr.c +++ b/components/bt/bluedroid/stack/gatt/gatt_sr.c @@ -669,7 +669,7 @@ static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_ ** Description fill the find information response information in the given ** buffer. ** -** Returns TRUE: if data filled sucessfully. +** Returns TRUE: if data filled successfully. ** FALSE: packet full, or format mismatch. ** *******************************************************************************/ @@ -962,7 +962,7 @@ static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data) if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL) { attp_send_sr_msg (p_tcb, p_buf); - /* Notify all registered applicaiton with new MTU size. Us a transaction ID */ + /* Notify all registered application with new MTU size. Us a transaction ID */ /* of 0, as no response is allowed from applcations */ for (i = 0; i < GATT_MAX_APPS; i ++) { @@ -1087,6 +1087,7 @@ void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, } } else { attp_send_sr_msg(p_tcb, p_msg); + gatt_dequeue_sr_cmd(p_tcb); } } diff --git a/components/bt/bluedroid/stack/gatt/gatt_utils.c b/components/bt/bluedroid/stack/gatt/gatt_utils.c index a08495e54..5d2b387e4 100644 --- a/components/bt/bluedroid/stack/gatt/gatt_utils.c +++ b/components/bt/bluedroid/stack/gatt/gatt_utils.c @@ -1493,7 +1493,7 @@ tGATT_STATUS gatt_send_error_rsp (tGATT_TCB *p_tcb, UINT8 err_code, UINT8 op_cod return status; } -#if (SDP_INCLUDED == TRUE) +#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE) /******************************************************************************* ** ** Function gatt_add_sdp_record @@ -1559,7 +1559,7 @@ UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl) return (sdp_handle); } -#endif ///SDP_INCLUDED == TRUE +#endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE #if GATT_CONFORMANCE_TESTING == TRUE /******************************************************************************* @@ -1887,7 +1887,7 @@ void gatt_sr_reset_prep_cnt(tGATT_TCB *p_tcb ) ** ** Function gatt_sr_update_cback_cnt ** -** Description Update the teh applicaiton callback count +** Description Update the teh application callback count ** ** Returns None ** @@ -1977,7 +1977,7 @@ BOOLEAN gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda) ** ** Function gatt_find_app_hold_link ** -** Description find the applicaiton that is holding the specified link +** Description find the application that is holding the specified link ** ** Returns Boolean ** @@ -2002,7 +2002,7 @@ BOOLEAN gatt_find_app_hold_link(tGATT_TCB *p_tcb, UINT8 start_idx, UINT8 *p_foun ** ** Function gatt_find_specific_app_in_hold_link ** -** Description find the specific applicaiton that is holding the specified link +** Description find the specific application that is holding the specified link ** ** Returns Boolean ** @@ -2325,7 +2325,7 @@ void gatt_dbg_display_uuid(tBT_UUID bt_uuid) bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); } else { - BCM_STRNCPY_S(str_buf, sizeof(str_buf), "Unknown UUID 0", 15); + BCM_STRNCPY_S(str_buf, "Unknown UUID 0", 15); } GATT_TRACE_DEBUG ("UUID=[%s]", str_buf); diff --git a/components/bt/bluedroid/stack/gatt/include/gatt_int.h b/components/bt/bluedroid/stack/gatt/include/gatt_int.h index 2b770a7f2..0762b519f 100644 --- a/components/bt/bluedroid/stack/gatt/include/gatt_int.h +++ b/components/bt/bluedroid/stack/gatt/include/gatt_int.h @@ -507,7 +507,7 @@ typedef struct { UINT16 next_handle; /* next available handle */ tGATT_SVC_CHG gattp_attr; /* GATT profile attribute service change */ tGATT_IF gatt_if; -#if (GATTS_INCLUDED == TRUE) +#if (GATTS_INCLUDED == TRUE) tGATT_HDL_LIST_INFO hdl_list_info; tGATT_HDL_LIST_ELEM hdl_list[GATT_MAX_SR_PROFILES]; tGATT_SRV_LIST_INFO srv_list_info; @@ -601,9 +601,9 @@ extern tGATT_STATUS attp_send_msg_to_l2cap(tGATT_TCB *p_tcb, BT_HDR *p_toL2CAP); /* utility functions */ extern UINT8 *gatt_dbg_op_name(UINT8 op_code); -#if (SDP_INCLUDED == TRUE) +#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE) extern UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl); -#endif ///SDP_INCLUDED == TRUE +#endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE extern BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid, UINT16 len, UINT8 **p_data); extern UINT8 gatt_build_uuid_to_stream(UINT8 **p_dst, tBT_UUID uuid); extern BOOLEAN gatt_uuid_compare(tBT_UUID src, tBT_UUID tar); @@ -725,16 +725,16 @@ extern BOOLEAN gatts_init_service_db (tGATT_SVC_DB *p_db, tBT_UUID *p_service, B extern UINT16 gatts_add_included_service (tGATT_SVC_DB *p_db, UINT16 s_handle, UINT16 e_handle, tBT_UUID service); extern UINT16 gatts_add_characteristic (tGATT_SVC_DB *p_db, tGATT_PERM perm, tGATT_CHAR_PROP property, - tBT_UUID *p_char_uuid, tGATT_ATTR_VAL *attr_val, + tBT_UUID *p_char_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control); -extern UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm, +extern UINT16 gatts_add_char_descr (tGATT_SVC_DB *p_db, tGATT_PERM perm, tBT_UUID *p_dscp_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control); -extern tGATT_STATUS gatts_set_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle, +extern tGATT_STATUS gatts_set_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle, UINT16 length, UINT8 *value); -extern tGATT_STATUS gatts_get_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle, +extern tGATT_STATUS gatts_get_attribute_value(tGATT_SVC_DB *p_db, UINT16 attr_handle, UINT16 *length, UINT8 **value); extern BOOLEAN gatts_is_auto_response(UINT16 attr_handle); extern tGATT_STATUS gatts_db_read_attr_value_by_type (tGATT_TCB *p_tcb, tGATT_SVC_DB *p_db, UINT8 op_code, BT_HDR *p_rsp, UINT16 s_handle, diff --git a/components/bt/bluedroid/stack/include/stack/bt_types.h b/components/bt/bluedroid/stack/include/stack/bt_types.h index 52385cb51..adbc30d10 100644 --- a/components/bt/bluedroid/stack/include/stack/bt_types.h +++ b/components/bt/bluedroid/stack/include/stack/bt_types.h @@ -21,14 +21,7 @@ #include #include - -#ifndef FALSE -# define FALSE false -#endif - -#ifndef TRUE -# define TRUE true -#endif +#include "bt_common.h" typedef uint8_t UINT8; typedef uint16_t UINT16; @@ -43,8 +36,8 @@ typedef bool BOOLEAN; #define PACKED __packed // #define INLINE __inline -#define BCM_STRCPY_S(x1,x2,x3) strcpy((x1),(x3)) -#define BCM_STRNCPY_S(x1,x2,x3,x4) strncpy((x1),(x3),(x4)) +#define BCM_STRCPY_S(x1,x2) strcpy((x1),(x2)) +#define BCM_STRNCPY_S(x1,x2,x3) strncpy((x1),(x2),(x3)) /* READ WELL !! ** @@ -524,19 +517,6 @@ typedef struct { typedef UINT8 tBT_DEVICE_TYPE; /*****************************************************************************/ - -/* Define trace levels */ -#define BT_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */ -#define BT_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */ -#define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */ -#define BT_TRACE_LEVEL_API 3 /* API traces */ -#define BT_TRACE_LEVEL_EVENT 4 /* Debug messages for events */ -#define BT_TRACE_LEVEL_DEBUG 5 /* Full debug messages */ -#define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */ - -#define MAX_TRACE_LEVEL 6 - - /* Define New Trace Type Definition */ /* TRACE_CTRL_TYPE 0x^^000000*/ #define TRACE_CTRL_MASK 0xff000000 diff --git a/components/bt/bluedroid/stack/include/stack/btm_api.h b/components/bt/bluedroid/stack/include/stack/btm_api.h index 6aa833c03..a997e6fa5 100644 --- a/components/bt/bluedroid/stack/include/stack/btm_api.h +++ b/components/bt/bluedroid/stack/include/stack/btm_api.h @@ -1558,7 +1558,7 @@ typedef void (tBTM_MKEY_CALLBACK) (BD_ADDR bd_addr, UINT8 status, UINT8 key_flag ** optional data passed in by BTM_SetEncryption ** tBTM_STATUS - result of the operation */ -typedef void (tBTM_SEC_CBACK) (BD_ADDR bd_addr, tBT_TRANSPORT trasnport, +typedef void (tBTM_SEC_CBACK) (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result); /* Bond Cancel complete. Parameters are diff --git a/components/bt/bluedroid/stack/include/stack/btm_ble_api.h b/components/bt/bluedroid/stack/include/stack/btm_ble_api.h index 7e6feb39b..8931ca91b 100644 --- a/components/bt/bluedroid/stack/include/stack/btm_ble_api.h +++ b/components/bt/bluedroid/stack/include/stack/btm_ble_api.h @@ -105,7 +105,7 @@ typedef UINT8 tBTM_BLE_SFP; #endif /* adv parameter boundary values */ -#define BTM_BLE_ADV_INT_MIN 0x0020 +#define BTM_BLE_ADV_INT_MIN 0x0010 #define BTM_BLE_ADV_INT_MAX 0x4000 /* Full scan boundary values */ @@ -448,7 +448,7 @@ typedef struct { typedef struct { tBTM_BLE_INT_RANGE int_range; /* slave prefered conn interval range */ - tBTM_BLE_MANU *p_manu; /* manufactuer data */ + tBTM_BLE_MANU *p_manu; /* manufacturer data */ tBTM_BLE_SERVICE *p_services; /* services */ tBTM_BLE_128SERVICE *p_services_128b; /* 128 bits service */ tBTM_BLE_32SERVICE *p_service_32b; /* 32 bits Service UUID */ @@ -732,10 +732,10 @@ typedef struct { typedef union { tBLE_BD_ADDR target_addr; - tBTM_BLE_PF_LOCAL_NAME_COND local_name; /* lcoal name filtering */ - tBTM_BLE_PF_MANU_COND manu_data; /* manufactuer data filtering */ + tBTM_BLE_PF_LOCAL_NAME_COND local_name; /* local name filtering */ + tBTM_BLE_PF_MANU_COND manu_data; /* manufacturer data filtering */ tBTM_BLE_PF_UUID_COND srvc_uuid; /* service UUID filtering */ - tBTM_BLE_PF_UUID_COND solicitate_uuid; /* solicitated service UUID filtering */ + tBTM_BLE_PF_UUID_COND solicitate_uuid; /* solicited service UUID filtering */ tBTM_BLE_PF_SRVC_PATTERN_COND srvc_data; /* service data pattern */ } tBTM_BLE_PF_COND_PARAM; @@ -1052,9 +1052,9 @@ void BTM_BleReadAdvParams (UINT16 *adv_int_min, UINT16 *adv_int_max, ** ** Function BTM_BleObtainVendorCapabilities ** -** Description This function is called to obatin vendor capabilties +** Description This function is called to obtain vendor capabilities ** -** Parameters p_cmn_vsc_cb - Returns the vednor capabilities +** Parameters p_cmn_vsc_cb - Returns the vendor capabilities ** ** Returns void ** diff --git a/components/bt/bluedroid/stack/include/stack/gatt_api.h b/components/bt/bluedroid/stack/include/stack/gatt_api.h index b2cecb057..c9014d1be 100644 --- a/components/bt/bluedroid/stack/include/stack/gatt_api.h +++ b/components/bt/bluedroid/stack/include/stack/gatt_api.h @@ -111,7 +111,7 @@ typedef UINT8 tGATT_STATUS; #define GATT_SIGN_CMD_WRITE 0xD2 /* changed in V4.0 1101-0010 (signed write) see write cmd above*/ #define GATT_OP_CODE_MAX GATT_HANDLE_VALUE_CONF + 1 /* 0x1E = 30 + 1 = 31*/ -#define GATT_COMMAND_FLAG 0x40 /* Command Flag: set to one means commond */ +#define GATT_COMMAND_FLAG 0x40 /* Command Flag: set to one means command */ #define GATT_HANDLE_IS_VALID(x) ((x) != 0) @@ -131,7 +131,7 @@ typedef UINT16 tGATT_DISCONN_REASON; #define GATT_MAX_MTU_SIZE 517 #endif -/* max legth of an attribute value +/* max length of an attribute value */ #ifndef GATT_MAX_ATTR_LEN #define GATT_MAX_ATTR_LEN 600 @@ -247,7 +247,7 @@ typedef UINT8 tGATT_FORMAT; /* Characteristic Presentation Format Descriptor value */ typedef struct { - UINT16 unit; /* as UUIUD defined by SIG */ + UINT16 unit; /* as UUID defined by SIG */ UINT16 descr; /* as UUID as defined by SIG */ tGATT_FORMAT format; INT8 exp; @@ -317,7 +317,7 @@ typedef UINT8 tGATT_AUTH_REQ; typedef struct { UINT16 conn_id; UINT16 handle; /* attribute handle */ - UINT16 offset; /* attribute value offset, if no offfset is needed for the command, ignore it */ + UINT16 offset; /* attribute value offset, if no offset is needed for the command, ignore it */ UINT16 len; /* length of attribute value */ tGATT_AUTH_REQ auth_req; /* authentication request */ UINT8 value[GATT_MAX_ATTR_LEN]; /* the actual attribute value */ @@ -368,7 +368,7 @@ typedef struct { /* write request data */ typedef struct { UINT16 handle; /* attribute handle */ - UINT16 offset; /* attribute value offset, if no offfset is needed for the command, ignore it */ + UINT16 offset; /* attribute value offset, if no offset is needed for the command, ignore it */ UINT16 len; /* length of attribute value */ UINT8 value[GATT_MAX_ATTR_LEN]; /* the actual attribute value */ BOOLEAN need_rsp; /* need write response */ @@ -468,7 +468,7 @@ typedef struct { */ typedef union { tGATT_READ_BY_TYPE service; - tGATT_READ_BY_TYPE char_type; /* characterisitc type */ + tGATT_READ_BY_TYPE char_type; /* characteristic type */ tGATT_READ_MULTI read_multiple; tGATT_READ_BY_HANDLE by_handle; tGATT_READ_PARTIAL partial; @@ -505,7 +505,7 @@ typedef UINT8 tGATTC_OPTYPE; /* characteristic declaration */ typedef struct { - tGATT_CHAR_PROP char_prop; /* characterisitc properties */ + tGATT_CHAR_PROP char_prop; /* characteristic properties */ UINT16 val_handle; /* characteristic value attribute handle */ tBT_UUID char_uuid; /* characteristic UUID type */ } tGATT_CHAR_DCLR_VAL; @@ -639,7 +639,7 @@ typedef struct { tGATTS_HNDL_RANGE *p_new_srv_start; } tGATTS_PENDING_NEW_SRV_START; -/* Attibute server handle ranges NV storage callback functions +/* Attribute server handle ranges NV storage callback functions */ typedef void (tGATTS_NV_SAVE_CBACK)(BOOLEAN is_saved, tGATTS_HNDL_RANGE *p_hndl_range); typedef BOOLEAN (tGATTS_NV_SRV_CHG_CBACK)(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ *p_req, @@ -688,7 +688,7 @@ extern UINT8 GATT_SetTraceLevel (UINT8 new_level); ** ** Parameter p_hndl_range: pointer to allocated handles information ** -** Returns TRUE if handle range is added sucessfully; otherwise FALSE. +** Returns TRUE if handle range is added successfully; otherwise FALSE. ** *******************************************************************************/ @@ -724,7 +724,7 @@ extern BOOLEAN GATTS_NVRegister (tGATT_APPL_INFO *p_cb_info); ** num_handles : number of handles needed by the service. ** is_pri : is a primary service or not. ** -** Returns service handle if sucessful, otherwise 0. +** Returns service handle if successful, otherwise 0. ** *******************************************************************************/ extern UINT16 GATTS_CreateService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, @@ -819,7 +819,7 @@ extern BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, ** p_cback : application service callback functions. ** sup_transport : supported transport(s) for this primary service ** -** return GATT_SUCCESS if sucessfully started; otherwise error code. +** return GATT_SUCCESS if successfully started; otherwise error code. ** *******************************************************************************/ extern tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle, @@ -851,7 +851,7 @@ extern void GATTS_StopService (UINT16 service_handle); ** val_len: Length of the indicated attribute value. ** p_val: Pointer to the indicated attribute value data. ** -** Returns GATT_SUCCESS if sucessfully sent or queued; otherwise error code. +** Returns GATT_SUCCESS if successfully sent or queued; otherwise error code. ** *******************************************************************************/ extern tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, @@ -869,7 +869,7 @@ extern tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, ** val_len: Length of the indicated attribute value. ** p_val: Pointer to the indicated attribute value data. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ extern tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_handle, @@ -887,7 +887,7 @@ extern tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_ ** status: response status ** p_msg: pointer to message parameters structure. ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ extern tGATT_STATUS GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id, @@ -904,7 +904,7 @@ extern tGATT_STATUS GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id, ** length: the attribute length ** value: the value to be set to the attribute in the database ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_SetAttributeValue(UINT16 attr_handle, UINT16 length, UINT8 *value); @@ -920,7 +920,7 @@ tGATT_STATUS GATTS_SetAttributeValue(UINT16 attr_handle, UINT16 length, UINT8 *v ** length:the attribute value length in the database ** value: the attribute value out put ** -** Returns GATT_SUCCESS if sucessfully sent; otherwise error code. +** Returns GATT_SUCCESS if successfully sent; otherwise error code. ** *******************************************************************************/ tGATT_STATUS GATTS_GetAttributeValue(UINT16 attr_handle, UINT16 *length, UINT8 **value); @@ -1005,7 +1005,7 @@ extern tGATT_STATUS GATTC_Write (UINT16 conn_id, tGATT_WRITE_TYPE type, ** the server. ** ** Parameters conn_id: connection identifier. -** is_execute - to execute or cancel the prepare write requet(s) +** is_execute - to execute or cancel the prepare write request(s) ** ** Returns GATT_SUCCESS if command started successfully. ** @@ -1037,7 +1037,7 @@ extern tGATT_STATUS GATTC_SendHandleValueConfirm (UINT16 conn_id, UINT16 handle) ** ** Parameter bd_addr: target device bd address. ** idle_tout: timeout value in seconds. -** transport: trasnport option. +** transport: transport option. ** ** Returns void ** @@ -1067,7 +1067,7 @@ extern tGATT_IF GATT_Register (tBT_UUID *p_app_uuid128, tGATT_CBACK *p_cb_info) ** ** Description This function deregistered the application from GATT. ** -** Parameters gatt_if: applicaiton interface. +** Parameters gatt_if: application interface. ** ** Returns None. ** @@ -1082,7 +1082,7 @@ extern void GATT_Deregister (tGATT_IF gatt_if); ** callbacks for registered interface. Function may call back ** with connection status and queued notifications ** -** Parameter gatt_if: applicaiton interface. +** Parameter gatt_if: application interface. ** ** Returns None ** @@ -1093,13 +1093,13 @@ extern void GATT_StartIf (tGATT_IF gatt_if); ** ** Function GATT_Connect ** -** Description This function initiate a connecttion to a remote device on GATT +** Description This function initiate a connection to a remote device on GATT ** channel. ** -** Parameters gatt_if: applicaiton interface +** Parameters gatt_if: application interface ** bd_addr: peer device address. ** bd_addr_type: peer device address type. -** is_direct: is a direct conenection or a background auto connection +** is_direct: is a direct connection or a background auto connection ** transport : Physical transport for GATT connection (BR/EDR or LE) ** ** Returns TRUE if connection started; FALSE if connection start failure. @@ -1119,7 +1119,7 @@ extern BOOLEAN GATT_Connect (tGATT_IF gatt_if, BD_ADDR bd_addr, tBLE_ADDR_TYPE b ** Parameters gatt_if: client interface. If 0 used as unconditionally disconnect, ** typically used for direct connection cancellation. ** bd_addr: peer device address. -** is_direct: is a direct conenection or a background auto connection +** is_direct: is a direct connection or a background auto connection ** ** Returns TRUE if connection started; FALSE if connection start failure. ** @@ -1158,15 +1158,15 @@ extern tGATT_STATUS GATT_SendServiceChangeIndication (BD_ADDR bd_addr); ** ** Function GATT_GetConnectionInfor ** -** Description This function use conn_id to find its associated BD address and applciation +** Description This function use conn_id to find its associated BD address and application ** interface ** ** Parameters conn_id: connection id (input) -** p_gatt_if: applicaiton interface (output) +** p_gatt_if: application interface (output) ** bd_addr: peer device address. (output) ** transport : physical transport of the GATT connection (BR/EDR or LE) ** -** Returns TRUE the ligical link information is found for conn_id +** Returns TRUE the logical link information is found for conn_id ** *******************************************************************************/ extern BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, @@ -1178,14 +1178,14 @@ extern BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, ** Function GATT_GetConnIdIfConnected ** ** Description This function find the conn_id if the logical link for BD address -** and applciation interface is connected +** and application interface is connected ** -** Parameters gatt_if: applicaiton interface (input) +** Parameters gatt_if: application interface (input) ** bd_addr: peer device address. (input) ** p_conn_id: connection id (output) ** transport : physical transport of the GATT connection (BR/EDR or LE) ** -** Returns TRUE the ligical link is connected +** Returns TRUE the logical link is connected ** *******************************************************************************/ extern BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, @@ -1199,10 +1199,10 @@ extern BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, ** Description This function start or stop LE advertisement and listen for ** connection. ** -** Parameters gatt_if: applicaiton interface +** Parameters gatt_if: application interface ** p_bd_addr: listen for specific address connection, or NULL for ** listen to all device connection. -** start: is a direct conenection or a background auto connection +** start: is a direct connection or a background auto connection ** ** Returns TRUE if advertisement is started; FALSE if adv start failure. ** diff --git a/components/bt/bluedroid/stack/include/stack/hcidefs.h b/components/bt/bluedroid/stack/include/stack/hcidefs.h index 6249f2c39..af5360f45 100644 --- a/components/bt/bluedroid/stack/include/stack/hcidefs.h +++ b/components/bt/bluedroid/stack/include/stack/hcidefs.h @@ -1521,8 +1521,7 @@ typedef struct { #define HCI_FEATURE_SWITCH_MASK 0x20 #define HCI_FEATURE_SWITCH_OFF 0 -// temporarily disable ROLE_SWITCH since there is an issue to be fixed -#define HCI_SWITCH_SUPPORTED(x) (0 & ((x)[HCI_FEATURE_SWITCH_OFF] & HCI_FEATURE_SWITCH_MASK)) +#define HCI_SWITCH_SUPPORTED(x) ((x)[HCI_FEATURE_SWITCH_OFF] & HCI_FEATURE_SWITCH_MASK) #define HCI_FEATURE_HOLD_MODE_MASK 0x40 #define HCI_FEATURE_HOLD_MODE_OFF 0 diff --git a/components/bt/bluedroid/stack/include/stack/port_api.h b/components/bt/bluedroid/stack/include/stack/port_api.h index 10b037868..8145a177a 100644 --- a/components/bt/bluedroid/stack/include/stack/port_api.h +++ b/components/bt/bluedroid/stack/include/stack/port_api.h @@ -623,6 +623,17 @@ extern int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len); *******************************************************************************/ extern void RFCOMM_Init (void); +/******************************************************************************* +** +** Function RFCOMM_Deinit +** +** Description This function is called to deinitialize the control block +** for this layer. +** +** Returns void +** +*******************************************************************************/ +extern void RFCOMM_Deinit(void); /******************************************************************************* ** diff --git a/components/bt/bluedroid/stack/l2cap/include/l2c_int.h b/components/bt/bluedroid/stack/l2cap/include/l2c_int.h index e4cd48749..d24562e51 100644 --- a/components/bt/bluedroid/stack/l2cap/include/l2c_int.h +++ b/components/bt/bluedroid/stack/l2cap/include/l2c_int.h @@ -251,7 +251,7 @@ typedef struct { tL2CAP_APPL_INFO api; } tL2C_RCB; -typedef void (tL2CAP_SEC_CBACK) (BD_ADDR bd_addr, tBT_TRANSPORT trasnport, +typedef void (tL2CAP_SEC_CBACK) (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result); typedef struct @@ -264,7 +264,7 @@ typedef struct }tL2CAP_SEC_DATA; #ifndef L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA -#define L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA 100 +#define L2CAP_CBB_DEFAULT_DATA_RATE_BUFF_QUOTA 10 #endif /* Define a channel control block (CCB). There may be many channel control blocks ** between the same two Bluetooth devices (i.e. on the same link). @@ -723,7 +723,7 @@ extern void l2c_link_process_num_completed_blocks (UINT8 controller_id, UINT extern void l2c_link_processs_num_bufs (UINT16 num_lm_acl_bufs); extern UINT8 l2c_link_pkts_rcvd (UINT16 *num_pkts, UINT16 *handles); extern void l2c_link_role_changed (BD_ADDR bd_addr, UINT8 new_role, UINT8 hci_status); -extern void l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT trasnport, void *p_ref_data, UINT8 status); +extern void l2c_link_sec_comp (BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status); extern void l2c_link_segments_xmitted (BT_HDR *p_msg); extern void l2c_pin_code_request (BD_ADDR bd_addr); extern void l2c_link_adjust_chnl_allocation (void); diff --git a/components/bt/bluedroid/stack/l2cap/l2c_api.c b/components/bt/bluedroid/stack/l2cap/l2c_api.c index 906b961d2..d9fcc6ded 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_api.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_api.c @@ -1836,7 +1836,7 @@ UINT16 L2CA_SendFixedChnlData (UINT16 fixed_cid, BD_ADDR rem_bda, BT_HDR *p_buf) // If already congested, do not accept any more packets if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) { - L2CAP_TRACE_DEBUG ("L2CAP - CID: 0x%04x cannot send, already congested\ + L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested\ xmit_hold_q.count: %u buff_quota: %u", fixed_cid, fixed_queue_length(p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->xmit_hold_q), p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->buff_quota); @@ -1871,6 +1871,14 @@ BOOLEAN L2CA_CheckIsCongest(UINT16 fixed_cid, UINT16 handle) return TRUE; } + +#if (BLE_INCLUDED == TRUE) +UINT16 L2CA_GetFreePktBufferNum_LE(void) +{ + return l2cb.controller_le_xmit_window; +} +#endif + /******************************************************************************* ** ** Function L2CA_RemoveFixedChnl diff --git a/components/bt/bluedroid/stack/l2cap/l2c_link.c b/components/bt/bluedroid/stack/l2cap/l2c_link.c index 15de6060b..ea1990fc3 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_link.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_link.c @@ -250,7 +250,7 @@ BOOLEAN l2c_link_hci_conn_comp (UINT8 status, UINT16 handle, BD_ADDR p_bda) l2cu_release_lcb (p_lcb); } else { /* there are any CCBs remaining */ if (ci.status == HCI_ERR_CONNECTION_EXISTS) { - /* we are in collision situation, wait for connecttion request from controller */ + /* we are in collision situation, wait for connection request from controller */ p_lcb->link_state = LST_CONNECTING; } else { l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR); diff --git a/components/bt/bluedroid/stack/l2cap/l2c_utils.c b/components/bt/bluedroid/stack/l2cap/l2c_utils.c index 4768d8808..8388df027 100644 --- a/components/bt/bluedroid/stack/l2cap/l2c_utils.c +++ b/components/bt/bluedroid/stack/l2cap/l2c_utils.c @@ -3655,8 +3655,9 @@ void l2cu_check_channel_congestion (tL2C_CCB *p_ccb) #endif } } else { + tL2C_LCB *p_lcb = p_ccb->p_lcb; /* If this channel was not congested but it is congested now, tell the app */ - if (q_count > p_ccb->buff_quota) { + if ((q_count > p_ccb->buff_quota) || (p_lcb && (p_ccb->local_cid == L2CAP_ATT_CID) && (p_lcb->link_xmit_quota > 0) && (p_lcb->link_xmit_quota <= p_lcb->sent_not_acked))) { p_ccb->cong_sent = TRUE; if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) { L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u", diff --git a/components/bt/bluedroid/stack/rfcomm/port_api.c b/components/bt/bluedroid/stack/rfcomm/port_api.c index fd8246c53..7407694da 100644 --- a/components/bt/bluedroid/stack/rfcomm/port_api.c +++ b/components/bt/bluedroid/stack/rfcomm/port_api.c @@ -1729,6 +1729,26 @@ void RFCOMM_Init (void) rfcomm_l2cap_if_init (); } +/******************************************************************************* +** +** Function RFCOMM_Deinit +** +** Description This function is called to deinitialize the control block +** for this layer. +** +** Returns void +** +*******************************************************************************/ +void RFCOMM_Deinit(void) +{ +#if RFC_DYNAMIC_MEMORY == TRUE + if (rfc_cb_ptr){ + osi_free(rfc_cb_ptr); + rfc_cb_ptr = NULL; + } +#endif +} + /******************************************************************************* ** ** Function PORT_SetTraceLevel diff --git a/components/bt/bluedroid/stack/smp/smp_act.c b/components/bt/bluedroid/stack/smp/smp_act.c index d11506e65..33c4c2736 100644 --- a/components/bt/bluedroid/stack/smp/smp_act.c +++ b/components/bt/bluedroid/stack/smp/smp_act.c @@ -1715,13 +1715,13 @@ void smp_match_dhkey_checks(tSMP_CB *p_cb, tSMP_INT_DATA *p_data) SMP_TRACE_DEBUG("%s\n", __func__); if (memcmp(p_data->key.p_data, p_cb->remote_dhkey_check, BT_OCTET16_LEN)) { - SMP_TRACE_WARNING ("dhkey chcks do no match\n"); + SMP_TRACE_WARNING ("dhkey checks do no match\n"); p_cb->failure = reason; smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason); return; } - SMP_TRACE_EVENT ("dhkey chcks match\n"); + SMP_TRACE_EVENT ("dhkey checks match\n"); /* compare the max encryption key size, and save the smaller one for the link */ if (p_cb->peer_enc_size < p_cb->loc_enc_size) { @@ -1945,8 +1945,7 @@ void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable) smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable); } - else if(p_dev_rec && !p_dev_rec->enc_init_by_we){ - + else if(p_dev_rec && !p_dev_rec->role_master && !p_dev_rec->enc_init_by_we ){ /* if enc_init_by_we is false, it means that client initiates encryption before slave calls esp_ble_set_encryption() we need initiate pairing_bda and p_cb->role then encryption, for example iPhones @@ -1956,6 +1955,12 @@ void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable) p_cb->role = HCI_ROLE_SLAVE; p_dev_rec->enc_init_by_we = FALSE; smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable); + }else if(p_dev_rec && p_dev_rec->role_master && p_dev_rec->enc_init_by_we){ + memcpy(&smp_cb.pairing_bda[0], bda, BD_ADDR_LEN); + p_cb->state = SMP_STATE_ENCRYPTION_PENDING; + p_cb->role = HCI_ROLE_MASTER; + p_dev_rec->enc_init_by_we = FALSE; + smp_sm_event(&smp_cb, SMP_ENCRYPTED_EVT, &encr_enable); } } diff --git a/components/bt/bluedroid/stack/smp/smp_utils.c b/components/bt/bluedroid/stack/smp/smp_utils.c index 19c2cde99..06ec46203 100644 --- a/components/bt/bluedroid/stack/smp/smp_utils.c +++ b/components/bt/bluedroid/stack/smp/smp_utils.c @@ -36,6 +36,7 @@ #include "smp_int.h" #include "device/controller.h" #include "btm_int.h" +#include "common/bte_appl.h" #define SMP_PAIRING_REQ_SIZE 7 #define SMP_CONFIRM_CMD_SIZE (BT_OCTET16_LEN + 1) @@ -590,7 +591,7 @@ static BT_HDR *smp_build_id_addr_cmd(UINT8 cmd_code, tSMP_CB *p_cb) p = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET; UINT8_TO_STREAM (p, SMP_OPCODE_ID_ADDR); - /* Identity Address Information is used in the Transport Specific Key Distribution phase to distribute + /* Identity Address Information is used in the Transport Specific Key Distribution phase to distribute its public device address or static random address. if slave using static random address is encrypted, it should distribute its static random address */ if(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && memcmp(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr,6) == 0) { @@ -1119,9 +1120,27 @@ BOOLEAN smp_pairing_request_response_parameters_are_valid(tSMP_CB *p_cb) return FALSE; } - if ((enc_size < SMP_ENCR_KEY_SIZE_MIN) || (enc_size > SMP_ENCR_KEY_SIZE_MAX)) { + /* `bte_appl_cfg.ble_min_enc_key_size` will be `SMP_ENCR_KEY_SIZE_MIN` by + * default if not set explicitly */ +#if (BLE_INCLUDED == TRUE) + if (enc_size < bte_appl_cfg.ble_min_key_size) { SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \ - Key value (0x%02x) out of range).\n", + Key value (0x%02x) less than minimum required key size).\n", + p_cb->rcvd_cmd_code, enc_size); + return FALSE; + } +#else + if (enc_size < SMP_ENCR_KEY_SIZE_MIN) { + SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \ + Key value (0x%02x) less than minimum required key size).\n", + p_cb->rcvd_cmd_code, enc_size); + return FALSE; + } +#endif + + if (enc_size > SMP_ENCR_KEY_SIZE_MAX) { + SMP_TRACE_WARNING("Rcvd from the peer cmd 0x%02x with Maximum Encryption \ + Key value (0x%02x) greater than supported by stack).\n", p_cb->rcvd_cmd_code, enc_size); return FALSE; } diff --git a/components/bt/bt.c b/components/bt/bt.c index dc17d9b02..aa44c5895 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -37,14 +37,15 @@ #include "esp_err.h" #include "esp_log.h" #include "esp_pm.h" -#include "esp_ipc.h" #include "driver/periph_ctrl.h" #include "soc/rtc.h" #include "soc/rtc_cntl_reg.h" #include "soc/soc_memory_layout.h" #include "esp_clk.h" #include "esp_coexist_internal.h" - +#if !CONFIG_FREERTOS_UNICORE +#include "esp_ipc.h" +#endif #if CONFIG_BT_ENABLED @@ -90,7 +91,7 @@ do{\ } while(0) #define OSI_FUNCS_TIME_BLOCKING 0xffffffff -#define OSI_VERSION 0x00010001 +#define OSI_VERSION 0x00010002 #define OSI_MAGIC_VALUE 0xFADEBEAD /* SPIRAM Configuration */ @@ -167,6 +168,8 @@ struct osi_funcs_t { void (* _btdm_sleep_exit_phase1)(void); /* called from ISR */ void (* _btdm_sleep_exit_phase2)(void); /* called from ISR */ void (* _btdm_sleep_exit_phase3)(void); /* called from task */ + bool (* _coex_bt_wakeup_request)(void); + void (* _coex_bt_wakeup_request_end)(void); int (* _coex_bt_request)(uint32_t event, uint32_t latency, uint32_t duration); int (* _coex_bt_release)(uint32_t event); int (* _coex_register_bt_cb)(coex_func_cb_t cb); @@ -196,7 +199,8 @@ extern void btdm_controller_enable_sleep(bool enable); extern void btdm_controller_set_sleep_mode(uint8_t mode); extern uint8_t btdm_controller_get_sleep_mode(void); extern bool btdm_power_state_active(void); -extern void btdm_wakeup_request(void); +extern void btdm_wakeup_request(bool request_lock); +extern void btdm_wakeup_request_end(void); /* Low Power Clock */ extern bool btdm_lpclk_select_src(uint32_t sel); extern bool btdm_lpclk_set_div(uint32_t div); @@ -217,6 +221,7 @@ extern int coex_bt_release_wrapper(uint32_t event); extern int coex_register_bt_cb_wrapper(coex_func_cb_t cb); extern uint32_t coex_bb_reset_lock_wrapper(void); extern void coex_bb_reset_unlock_wrapper(uint32_t restore); +extern void coex_ble_adv_priority_high_set(bool high); extern char _bss_start_btdm; extern char _bss_end_btdm; @@ -276,6 +281,8 @@ static void btdm_sleep_enter_phase1_wrapper(uint32_t lpcycles); static void btdm_sleep_enter_phase2_wrapper(void); static void IRAM_ATTR btdm_sleep_exit_phase1_wrapper(void); static void btdm_sleep_exit_phase3_wrapper(void); +static bool coex_bt_wakeup_request(void); +static void coex_bt_wakeup_request_end(void); /* Local variable definition *************************************************************************** @@ -323,6 +330,8 @@ static const struct osi_funcs_t osi_funcs_ro = { ._btdm_sleep_exit_phase1 = btdm_sleep_exit_phase1_wrapper, ._btdm_sleep_exit_phase2 = NULL, ._btdm_sleep_exit_phase3 = btdm_sleep_exit_phase3_wrapper, + ._coex_bt_wakeup_request = coex_bt_wakeup_request, + ._coex_bt_wakeup_request_end = coex_bt_wakeup_request_end, ._coex_bt_request = coex_bt_request_wrapper, ._coex_bt_release = coex_bt_release_wrapper, ._coex_register_bt_cb = coex_register_bt_cb_wrapper, @@ -726,7 +735,7 @@ static void task_delete_wrapper(void *task_handle) static bool IRAM_ATTR is_in_isr_wrapper(void) { - return (bool)xPortInIsrContext(); + return !xPortCanYield(); } static void IRAM_ATTR cause_sw_intr(void *arg) @@ -740,18 +749,21 @@ static int IRAM_ATTR cause_sw_intr_to_core_wrapper(int core_id, int intr_no) { esp_err_t err = ESP_OK; +#if CONFIG_FREERTOS_UNICORE + cause_sw_intr((void *)intr_no); +#else /* CONFIG_FREERTOS_UNICORE */ if (xPortGetCoreID() == core_id) { cause_sw_intr((void *)intr_no); } else { err = esp_ipc_call(core_id, cause_sw_intr, (void *)intr_no); } - +#endif /* !CONFIG_FREERTOS_UNICORE */ return err; } static void *malloc_internal_wrapper(size_t size) { - return heap_caps_malloc(size, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); + return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); } static int32_t IRAM_ATTR read_mac_wrapper(uint8_t mac[6]) @@ -872,13 +884,26 @@ static void IRAM_ATTR btdm_slp_tmr_callback(void *arg) } #endif -bool esp_vhci_host_check_send_available(void) -{ - return API_vhci_host_check_send_available(); -} +#define BTDM_ASYNC_WAKEUP_REQ_HCI 0 +#define BTDM_ASYNC_WAKEUP_REQ_COEX 1 +#define BTDM_ASYNC_WAKEUP_REQMAX 2 -void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) +static bool async_wakeup_request(int event) { + bool request_lock = false; + switch (event) { + case BTDM_ASYNC_WAKEUP_REQ_HCI: + request_lock = true; + break; + case BTDM_ASYNC_WAKEUP_REQ_COEX: + request_lock = false; + break; + default: + return false; + } + + bool do_wakeup_request = false; + if (!btdm_power_state_active()) { #if CONFIG_PM_ENABLE if (semphr_take_wrapper(s_pm_lock_sem, 0)) { @@ -886,9 +911,59 @@ void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) } esp_timer_stop(s_btdm_slp_tmr); #endif - btdm_wakeup_request(); + do_wakeup_request = true; + btdm_wakeup_request(request_lock); } + + return do_wakeup_request; +} + +static void async_wakeup_request_end(int event) +{ + bool request_lock = false; + switch (event) { + case BTDM_ASYNC_WAKEUP_REQ_HCI: + request_lock = true; + break; + case BTDM_ASYNC_WAKEUP_REQ_COEX: + request_lock = false; + break; + default: + return; + } + + if (request_lock) { + btdm_wakeup_request_end(); + } + + return; +} + +static bool coex_bt_wakeup_request(void) +{ + return async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_COEX); +} + +static void coex_bt_wakeup_request_end(void) +{ + async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_COEX); + return; +} + +bool esp_vhci_host_check_send_available(void) +{ + return API_vhci_host_check_send_available(); +} + +void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) +{ + bool do_wakeup_request = async_wakeup_request(BTDM_ASYNC_WAKEUP_REQ_HCI); + API_vhci_host_send_packet(data, len); + + if (do_wakeup_request) { + async_wakeup_request_end(BTDM_ASYNC_WAKEUP_REQ_HCI); + } } esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback) @@ -947,7 +1022,7 @@ static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end) esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) { bool update = true; - intptr_t mem_start, mem_end; + intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL; if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { return ESP_ERR_INVALID_STATE; @@ -1152,6 +1227,12 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto error; } +#ifdef CONFIG_BTDM_COEX_BLE_ADV_HIGH_PRIORITY + coex_ble_adv_priority_high_set(true); +#else + coex_ble_adv_priority_high_set(false); +#endif + btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; return ESP_OK; @@ -1290,7 +1371,7 @@ esp_err_t esp_bt_controller_disable(void) if (btdm_controller_get_sleep_mode() == BTDM_MODEM_SLEEP_MODE_ORIG) { btdm_controller_enable_sleep(false); if (!btdm_power_state_active()) { - btdm_wakeup_request(); + btdm_wakeup_request(false); } while (!btdm_power_state_active()) { ets_delay_us(1000); @@ -1426,7 +1507,7 @@ void esp_bt_controller_wakeup_request(void) return; } - btdm_wakeup_request(); + btdm_wakeup_request(false); } esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path) diff --git a/components/bt/bluedroid/btc/core/btc_alarm.c b/components/bt/common/btc/core/btc_alarm.c similarity index 97% rename from components/bt/bluedroid/btc/core/btc_alarm.c rename to components/bt/common/btc/core/btc_alarm.c index ade9f093a..653f9826d 100644 --- a/components/bt/bluedroid/btc/core/btc_alarm.c +++ b/components/bt/common/btc/core/btc_alarm.c @@ -14,6 +14,7 @@ #include "btc/btc_task.h" #include "btc/btc_alarm.h" +#include "esp_log.h" void btc_alarm_handler(btc_msg_t *msg) { diff --git a/components/bt/bluedroid/btc/core/btc_manage.c b/components/bt/common/btc/core/btc_manage.c similarity index 93% rename from components/bt/bluedroid/btc/core/btc_manage.c rename to components/bt/common/btc/core/btc_manage.c index 81ecad410..037deee3a 100644 --- a/components/bt/bluedroid/btc/core/btc_manage.c +++ b/components/bt/common/btc/core/btc_manage.c @@ -14,10 +14,7 @@ #include "btc/btc_task.h" -#include "common/bt_trace.h" #include "osi/thread.h" -#include "esp_bt_defs.h" -#include "esp_gatt_defs.h" static void *btc_profile_cb_tab[BTC_PID_NUM] = {}; diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/common/btc/core/btc_task.c similarity index 73% rename from components/bt/bluedroid/btc/core/btc_task.c rename to components/bt/common/btc/core/btc_task.c index 1022e770d..53f77629a 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/common/btc/core/btc_task.c @@ -14,12 +14,14 @@ #include #include -#include "common/bt_target.h" #include "btc/btc_task.h" -#include "common/bt_trace.h" #include "osi/thread.h" -#include "common/bt_defs.h" +#include "esp_log.h" +#include "bt_common.h" #include "osi/allocator.h" +#include "btc/btc_alarm.h" +#ifdef CONFIG_BLUEDROID_ENABLED +#include "common/bt_target.h" #include "btc/btc_main.h" #include "btc/btc_dev.h" #include "btc_gatts.h" @@ -28,7 +30,6 @@ #include "btc_gap_ble.h" #include "btc_blufi_prf.h" #include "btc/btc_dm.h" -#include "btc/btc_alarm.h" #include "bta/bta_gatt_api.h" #if CONFIG_CLASSIC_BT_ENABLED #include "btc/btc_profile_queue.h" @@ -46,12 +47,23 @@ #include "btc_hf_client.h" #endif /* #if BTC_HF_CLIENT_INCLUDED */ #endif /* #if CONFIG_CLASSIC_BT_ENABLED */ +#endif +#if CONFIG_BLE_MESH +#include "btc_ble_mesh_prov.h" +#include "btc_ble_mesh_health_model.h" +#include "btc_ble_mesh_config_model.h" +#include "btc_ble_mesh_generic_model.h" +#include "btc_ble_mesh_lighting_model.h" +#include "btc_ble_mesh_sensor_model.h" +#include "btc_ble_mesh_time_scene_model.h" +#endif /* #if CONFIG_BLE_MESH */ static xTaskHandle xBtcTaskHandle = NULL; static xQueueHandle xBtcQueue = 0; static btc_func_t profile_tab[BTC_PID_NUM] = { +#ifdef CONFIG_BLUEDROID_ENABLED [BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL }, [BTC_PID_DEV] = {btc_dev_call_handler, NULL }, #if (GATTS_INCLUDED == TRUE) @@ -70,7 +82,9 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler }, #endif ///GATTS_INCLUDED == TRUE [BTC_PID_DM_SEC] = {NULL, btc_dm_sec_cb_handler }, +#endif [BTC_PID_ALARM] = {btc_alarm_handler, NULL }, +#ifdef CONFIG_BLUEDROID_ENABLED #if CONFIG_CLASSIC_BT_ENABLED #if (BTC_GAP_BT_INCLUDED == TRUE) [BTC_PID_GAP_BT] = {btc_gap_bt_call_handler, btc_gap_bt_cb_handler }, @@ -87,6 +101,23 @@ static btc_func_t profile_tab[BTC_PID_NUM] = { [BTC_PID_HF_CLIENT] = {btc_hf_client_call_handler, btc_hf_client_cb_handler}, #endif /* #if BTC_HF_CLIENT_INCLUDED */ #endif /* #if CONFIG_CLASSIC_BT_ENABLED */ +#endif +#if CONFIG_BLE_MESH + [BTC_PID_PROV] = {btc_ble_mesh_prov_call_handler, btc_ble_mesh_prov_cb_handler }, + [BTC_PID_MODEL] = {btc_ble_mesh_model_call_handler, btc_ble_mesh_model_cb_handler }, + [BTC_PID_HEALTH_CLIENT] = {btc_ble_mesh_health_client_call_handler, btc_ble_mesh_health_client_cb_handler }, + [BTC_PID_HEALTH_SERVER] = {btc_ble_mesh_health_server_call_handler, btc_ble_mesh_health_server_cb_handler }, + [BTC_PID_CONFIG_CLIENT] = {btc_ble_mesh_config_client_call_handler, btc_ble_mesh_config_client_cb_handler }, + [BTC_PID_CONFIG_SERVER] = {NULL, btc_ble_mesh_config_server_cb_handler }, + [BTC_PID_GENERIC_CLIENT] = {btc_ble_mesh_generic_client_call_handler, btc_ble_mesh_generic_client_cb_handler }, + [BTC_PID_LIGHTING_CLIENT] = {btc_ble_mesh_lighting_client_call_handler, btc_ble_mesh_lighting_client_cb_handler }, + [BTC_PID_SENSOR_CLIENT] = {btc_ble_mesh_sensor_client_call_handler, btc_ble_mesh_sensor_client_cb_handler }, + [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler}, + [BTC_PID_GENERIC_SERVER] = {NULL, btc_ble_mesh_generic_server_cb_handler }, + [BTC_PID_LIGHTING_SERVER] = {NULL, btc_ble_mesh_lighting_server_cb_handler }, + [BTC_PID_SENSOR_SERVER] = {NULL, btc_ble_mesh_sensor_server_cb_handler }, + [BTC_PID_TIME_SCENE_SERVER] = {NULL, btc_ble_mesh_time_scene_server_cb_handler}, +#endif /* #if CONFIG_BLE_MESH */ }; /***************************************************************************** @@ -169,7 +200,10 @@ int btc_init(void) if (xBtcTaskHandle == NULL || xBtcQueue == 0){ return BT_STATUS_NOMEM; } +#ifdef CONFIG_BLUEDROID_ENABLED btc_gap_callback_init(); +#endif + #if SCAN_QUEUE_CONGEST_CHECK btc_adv_list_init(); #endif @@ -191,7 +225,7 @@ void btc_deinit(void) bool btc_check_queue_is_congest(void) { UBaseType_t wait_size = uxQueueMessagesWaiting(xBtcQueue); - if(wait_size >= QUEUE_CONGEST_SIZE) { + if(wait_size >= BT_QUEUE_CONGEST_SIZE) { return true; } return false; diff --git a/components/bt/bluedroid/btc/include/btc/btc_alarm.h b/components/bt/common/btc/include/btc/btc_alarm.h similarity index 100% rename from components/bt/bluedroid/btc/include/btc/btc_alarm.h rename to components/bt/common/btc/include/btc/btc_alarm.h diff --git a/components/bt/bluedroid/btc/include/btc/btc_manage.h b/components/bt/common/btc/include/btc/btc_manage.h similarity index 94% rename from components/bt/bluedroid/btc/include/btc/btc_manage.h rename to components/bt/common/btc/include/btc/btc_manage.h index 46f746e8d..778786baa 100644 --- a/components/bt/bluedroid/btc/include/btc/btc_manage.h +++ b/components/bt/common/btc/include/btc/btc_manage.h @@ -15,9 +15,7 @@ #ifndef __BTC_MANAGE_H__ #define __BTC_MANAGE_H__ -#include "bta/bta_api.h" #include "btc/btc_task.h" -#include "esp_bt_defs.h" /* reset gatt callback table */ void esp_profile_cb_reset(void); diff --git a/components/bt/bluedroid/btc/include/btc/btc_task.h b/components/bt/common/btc/include/btc/btc_task.h similarity index 82% rename from components/bt/bluedroid/btc/include/btc/btc_task.h rename to components/bt/common/btc/include/btc/btc_task.h index 5813c5217..00b63e55c 100644 --- a/components/bt/bluedroid/btc/include/btc/btc_task.h +++ b/components/bt/common/btc/include/btc/btc_task.h @@ -16,10 +16,13 @@ #define __BTC_TASK_H__ #include -#include "common/bt_target.h" -#include "common/bt_defs.h" +#include "bt_common.h" #include "osi/thread.h" +#if CONFIG_BLUEDROID_ENABLED +#include "common/bt_target.h" +#endif + typedef struct btc_msg { uint8_t sig; //event signal uint8_t aid; //application id @@ -63,6 +66,22 @@ typedef enum { BTC_PID_HF_CLIENT, #endif /* BTC_HF_CLIENT_INCLUDED */ #endif /* CONFIG_CLASSIC_BT_ENABLED */ +#if CONFIG_BLE_MESH + BTC_PID_PROV, + BTC_PID_MODEL, + BTC_PID_HEALTH_CLIENT, + BTC_PID_HEALTH_SERVER, + BTC_PID_CONFIG_CLIENT, + BTC_PID_CONFIG_SERVER, + BTC_PID_GENERIC_CLIENT, + BTC_PID_LIGHTING_CLIENT, + BTC_PID_SENSOR_CLIENT, + BTC_PID_TIME_SCENE_CLIENT, + BTC_PID_GENERIC_SERVER, + BTC_PID_LIGHTING_SERVER, + BTC_PID_SENSOR_SERVER, + BTC_PID_TIME_SCENE_SERVER, +#endif /* CONFIG_BLE_MESH */ BTC_PID_NUM, } btc_pid_t; //btc profile id diff --git a/components/bt/common/include/bt_common.h b/components/bt/common/include/bt_common.h new file mode 100644 index 000000000..32c7ce348 --- /dev/null +++ b/components/bt/common/include/bt_common.h @@ -0,0 +1,169 @@ + +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BT_COMMON_H_ +#define _BT_COMMON_H_ + +#include "esp_log.h" + +#ifndef FALSE +#define FALSE false +#endif + +#ifndef TRUE +#define TRUE true +#endif + +#ifndef BT_QUEUE_CONGEST_SIZE +#define BT_QUEUE_CONGEST_SIZE 40 +#endif + +#ifdef CONFIG_BTC_INITIAL_TRACE_LEVEL +#define BTC_INITIAL_TRACE_LEVEL CONFIG_BTC_INITIAL_TRACE_LEVEL +#else +#define BTC_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING +#endif + +#ifdef CONFIG_OSI_INITIAL_TRACE_LEVEL +#define OSI_INITIAL_TRACE_LEVEL CONFIG_OSI_INITIAL_TRACE_LEVEL +#else +#define OSI_INITIAL_TRACE_LEVEL BT_TRACE_LEVEL_WARNING +#endif + +#if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY +#define BT_BLE_DYNAMIC_ENV_MEMORY TRUE +#else +#define BT_BLE_DYNAMIC_ENV_MEMORY FALSE +#endif + +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig +#endif + +#ifdef CONFIG_NIMBLE_ENABLED +#define TASK_PINNED_TO_CORE (CONFIG_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#define BTC_TASK_STACK_SIZE 4096 +#endif + +#define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) +#define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) +#define BTC_TASK_QUEUE_LEN 60 + +/* Define trace levels */ +#define BT_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */ +#define BT_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */ +#define BT_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */ +#define BT_TRACE_LEVEL_API 3 /* API traces */ +#define BT_TRACE_LEVEL_EVENT 4 /* Debug messages for events */ +#define BT_TRACE_LEVEL_DEBUG 5 /* Full debug messages */ +#define BT_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */ + +#define MAX_TRACE_LEVEL 6 + +#ifndef LOG_LOCAL_LEVEL +#ifndef BOOTLOADER_BUILD +#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL +#else +#define LOG_LOCAL_LEVEL CONFIG_LOG_BOOTLOADER_LEVEL +#endif +#endif + +// Mapping between ESP_LOG_LEVEL and BT_TRACE_LEVEL +#if (LOG_LOCAL_LEVEL >= 4) +#define LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL+1) +#else +#define LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL +#endif + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define BT_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_INITIAL_TRACE_LEVEL, LOG_LOCAL_LEVEL_MAPPING) >= BT_TRACE_LEVEL_##LEVEL) + +#define BT_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BT_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } + +#ifndef assert +#define assert(x) do { if (!(x)) BT_PRINT_E("BT", "bt host error %s %u\n", __FILE__, __LINE__); } while (0) +#endif + + +#if !CONFIG_BT_STACK_NO_LOG +/* define traces for BTC */ +#define BTC_TRACE_ERROR(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(BTC, ERROR)) BT_PRINT_E("BT_BTC", fmt, ## args);} +#define BTC_TRACE_WARNING(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(BTC, WARNING)) BT_PRINT_W("BT_BTC", fmt, ## args);} +#define BTC_TRACE_API(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(BTC,API)) BT_PRINT_I("BT_BTC", fmt, ## args);} +#define BTC_TRACE_EVENT(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(BTC,EVENT)) BT_PRINT_D("BT_BTC", fmt, ## args);} +#define BTC_TRACE_DEBUG(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(BTC,DEBUG)) BT_PRINT_D("BT_BTC", fmt, ## args);} +#define BTC_TRACE_VERBOSE(fmt, args...) {if (BTC_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(BTC,VERBOSE)) BT_PRINT_V("BT_BTC", fmt, ## args);} + +/* define traces for OSI */ +#define OSI_TRACE_ERROR(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_ERROR && BT_LOG_LEVEL_CHECK(OSI, ERROR)) BT_PRINT_E("BT_OSI", fmt, ## args);} +#define OSI_TRACE_WARNING(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_WARNING && BT_LOG_LEVEL_CHECK(OSI, WARNING)) BT_PRINT_W("BT_OSI", fmt, ## args);} +#define OSI_TRACE_API(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_API && BT_LOG_LEVEL_CHECK(OSI,API)) BT_PRINT_I("BT_OSI", fmt, ## args);} +#define OSI_TRACE_EVENT(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_EVENT && BT_LOG_LEVEL_CHECK(OSI,EVENT)) BT_PRINT_D("BT_OSI", fmt, ## args);} +#define OSI_TRACE_DEBUG(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_DEBUG && BT_LOG_LEVEL_CHECK(OSI,DEBUG)) BT_PRINT_D("BT_OSI", fmt, ## args);} +#define OSI_TRACE_VERBOSE(fmt, args...) {if (OSI_INITIAL_TRACE_LEVEL >= BT_TRACE_LEVEL_VERBOSE && BT_LOG_LEVEL_CHECK(OSI,VERBOSE)) BT_PRINT_V("BT_OSI", fmt, ## args);} + +#else + +/* define traces for BTC */ +#define BTC_TRACE_ERROR(fmt, args...) +#define BTC_TRACE_WARNING(fmt, args...) +#define BTC_TRACE_API(fmt, args...) +#define BTC_TRACE_EVENT(fmt, args...) +#define BTC_TRACE_DEBUG(fmt, args...) +#define BTC_TRACE_VERBOSE(fmt, args...) + +/* define traces for OSI */ +#define OSI_TRACE_ERROR(fmt, args...) +#define OSI_TRACE_WARNING(fmt, args...) +#define OSI_TRACE_API(fmt, args...) +#define OSI_TRACE_EVENT(fmt, args...) +#define OSI_TRACE_DEBUG(fmt, args...) +#define OSI_TRACE_VERBOSE(fmt, args...) + +#endif + +/** Bluetooth Error Status */ +/** We need to build on this */ + +/* relate to ESP_BT_STATUS_xxx in esp_bt_defs.h */ +typedef enum { + BT_STATUS_SUCCESS = 0, + BT_STATUS_FAIL, + BT_STATUS_NOT_READY, + BT_STATUS_NOMEM, + BT_STATUS_BUSY, + BT_STATUS_DONE, /* request already completed */ + BT_STATUS_UNSUPPORTED, + BT_STATUS_PARM_INVALID, + BT_STATUS_UNHANDLED, + BT_STATUS_AUTH_FAILURE, + BT_STATUS_RMT_DEV_DOWN, + BT_STATUS_AUTH_REJECTED, + BT_STATUS_INVALID_STATIC_RAND_ADDR, + BT_STATUS_PENDING, + BT_STATUS_UNACCEPT_CONN_INTERVAL, + BT_STATUS_PARAM_OUT_OF_RANGE, + BT_STATUS_TIMEOUT, + BT_STATUS_MEMORY_FULL, + BT_STATUS_EIR_TOO_LARGE, +} bt_status_t; + +#endif /* _BT_COMMON_H_ */ diff --git a/components/bt/bluedroid/osi/alarm.c b/components/bt/common/osi/alarm.c similarity index 98% rename from components/bt/bluedroid/osi/alarm.c rename to components/bt/common/osi/alarm.c index 530701059..0a216a8db 100644 --- a/components/bt/bluedroid/osi/alarm.c +++ b/components/bt/common/osi/alarm.c @@ -18,8 +18,6 @@ #include #include #include -#include "common/bt_defs.h" -#include "common/bt_trace.h" #include "osi/alarm.h" #include "osi/allocator.h" #include "osi/list.h" @@ -27,6 +25,7 @@ #include "btc/btc_task.h" #include "btc/btc_alarm.h" #include "osi/mutex.h" +#include "bt_common.h" typedef struct alarm_t { /* timer id point to here */ @@ -251,12 +250,12 @@ end: osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout) { - return alarm_set(alarm, timeout, false); + return alarm_set(alarm, timeout, FALSE); } osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period) { - return alarm_set(alarm, period, true); + return alarm_set(alarm, period, TRUE); } osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm) diff --git a/components/bt/bluedroid/osi/allocator.c b/components/bt/common/osi/allocator.c similarity index 99% rename from components/bt/bluedroid/osi/allocator.c rename to components/bt/common/osi/allocator.c index 7a06015e2..006f44209 100644 --- a/components/bt/bluedroid/osi/allocator.c +++ b/components/bt/common/osi/allocator.c @@ -18,7 +18,7 @@ #include #include -#include "common/bt_defs.h" +#include "bt_common.h" #include "osi/allocator.h" extern void *pvPortZalloc(size_t size); diff --git a/components/bt/bluedroid/osi/buffer.c b/components/bt/common/osi/buffer.c similarity index 96% rename from components/bt/bluedroid/osi/buffer.c rename to components/bt/common/osi/buffer.c index 6b21ed8e2..559367529 100644 --- a/components/bt/bluedroid/osi/buffer.c +++ b/components/bt/common/osi/buffer.c @@ -16,11 +16,9 @@ * ******************************************************************************/ #include -#include "common/bt_trace.h" +#include "bt_common.h" #include "osi/allocator.h" #include "osi/buffer.h" -#include "common/bt_defs.h" -#include "common/bt_trace.h" struct buffer_t { buffer_t *root; diff --git a/components/bt/bluedroid/osi/config.c b/components/bt/common/osi/config.c similarity index 94% rename from components/bt/bluedroid/osi/config.c rename to components/bt/common/osi/config.c index 55a3b3d4b..698c37be0 100644 --- a/components/bt/bluedroid/osi/config.c +++ b/components/bt/common/osi/config.c @@ -23,10 +23,10 @@ #include #include +#include "bt_common.h" #include "osi/allocator.h" #include "osi/config.h" #include "osi/list.h" -#include "common/bt_trace.h" #define CONFIG_FILE_MAX_SIZE (1536)//1.5k #define CONFIG_FILE_DEFAULE_LENGTH (2048) @@ -389,7 +389,7 @@ bool config_save(const config_t *config, const char *filename) const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i)) char *keyname = osi_calloc(keyname_bufsz); int config_size = get_config_size(config); - char *buf = osi_calloc(config_size + 100); + char *buf = osi_calloc(config_size); if (!line || !buf || !keyname) { err_code |= 0x01; goto error; @@ -409,6 +409,16 @@ bool config_save(const config_t *config, const char *filename) for (const list_node_t *node = list_begin(config->sections); node != list_end(config->sections); node = list_next(node)) { const section_t *section = (const section_t *)list_node(node); w_cnt = snprintf(line, 1024, "[%s]\n", section->name); + if(w_cnt < 0) { + OSI_TRACE_ERROR("snprintf error w_cnt %d.",w_cnt); + err_code |= 0x10; + goto error; + } + if(w_cnt_total + w_cnt > config_size) { + OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size (config_size = %d).", __func__, (w_cnt + w_cnt_total), config_size); + err_code |= 0x20; + goto error; + } OSI_TRACE_DEBUG("section name: %s, w_cnt + w_cnt_total = %d\n", section->name, w_cnt + w_cnt_total); memcpy(buf + w_cnt_total, line, w_cnt); w_cnt_total += w_cnt; @@ -417,6 +427,16 @@ bool config_save(const config_t *config, const char *filename) const entry_t *entry = (const entry_t *)list_node(enode); OSI_TRACE_DEBUG("(key, val): (%s, %s)\n", entry->key, entry->value); w_cnt = snprintf(line, 1024, "%s = %s\n", entry->key, entry->value); + if(w_cnt < 0) { + OSI_TRACE_ERROR("snprintf error w_cnt %d.",w_cnt); + err_code |= 0x10; + goto error; + } + if(w_cnt_total + w_cnt > config_size) { + OSI_TRACE_ERROR("%s, memcpy size (w_cnt + w_cnt_total = %d) is larger than buffer size.(config_size = %d)", __func__, (w_cnt + w_cnt_total), config_size); + err_code |= 0x20; + goto error; + } OSI_TRACE_DEBUG("%s, w_cnt + w_cnt_total = %d", __func__, w_cnt + w_cnt_total); memcpy(buf + w_cnt_total, line, w_cnt); w_cnt_total += w_cnt; @@ -523,7 +543,10 @@ static void config_parse(nvs_handle fp, config_t *config) const size_t keyname_bufsz = sizeof(CONFIG_KEY) + 5 + 1; // including log10(sizeof(i)) char *keyname = osi_calloc(keyname_bufsz); int buf_size = get_config_size_from_flash(fp); - char *buf = osi_calloc(buf_size + 100); + char *buf = osi_calloc(buf_size); + if(buf_size == 0) { //First use nvs + goto error; + } if (!line || !section || !buf || !keyname) { err_code |= 0x01; goto error; diff --git a/components/bt/bluedroid/osi/fixed_queue.c b/components/bt/common/osi/fixed_queue.c similarity index 94% rename from components/bt/bluedroid/osi/fixed_queue.c rename to components/bt/common/osi/fixed_queue.c index 30290060a..df8bfff33 100644 --- a/components/bt/bluedroid/osi/fixed_queue.c +++ b/components/bt/common/osi/fixed_queue.c @@ -16,12 +16,10 @@ * ******************************************************************************/ -#include "common/bt_defs.h" #include "osi/allocator.h" #include "osi/fixed_queue.h" #include "osi/list.h" #include "osi/osi.h" -#include "common/bt_trace.h" #include "osi/mutex.h" #include "osi/semaphore.h" @@ -131,17 +129,19 @@ size_t fixed_queue_capacity(fixed_queue_t *queue) void fixed_queue_enqueue(fixed_queue_t *queue, void *data) { + bool status=false; //Flag whether enqueued success + assert(queue != NULL); assert(data != NULL); osi_sem_take(&queue->enqueue_sem, OSI_SEM_MAX_TIMEOUT); osi_mutex_lock(&queue->lock, OSI_MUTEX_MAX_TIMEOUT); - - list_append(queue->list, data); + status = list_append(queue->list, data); //Check whether enqueued success osi_mutex_unlock(&queue->lock); - osi_sem_give(&queue->dequeue_sem); + if(status == true) + osi_sem_give(&queue->dequeue_sem); } void *fixed_queue_dequeue(fixed_queue_t *queue) @@ -189,7 +189,7 @@ void *fixed_queue_try_dequeue(fixed_queue_t *queue) return NULL; } - if (osi_sem_take(queue->dequeue_sem, 0) != 0) { + if (osi_sem_take(&queue->dequeue_sem, 0) != 0) { return NULL; } @@ -243,14 +243,14 @@ void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data) osi_mutex_lock(&queue->lock, OSI_MUTEX_MAX_TIMEOUT); if (list_contains(queue->list, data) && - osi_sem_take(queue->dequeue_sem, 0) == 0) { + osi_sem_take(&queue->dequeue_sem, 0) == 0) { removed = list_remove(queue->list, data); assert(removed); } osi_mutex_unlock(&queue->lock); if (removed) { - osi_sem_give(queue->enqueue_sem); + osi_sem_give(&queue->enqueue_sem); return data; } diff --git a/components/bt/bluedroid/osi/future.c b/components/bt/common/osi/future.c similarity index 98% rename from components/bt/bluedroid/osi/future.c rename to components/bt/common/osi/future.c index 25eb5605e..aee33b1c0 100644 --- a/components/bt/bluedroid/osi/future.c +++ b/components/bt/common/osi/future.c @@ -16,8 +16,7 @@ * ******************************************************************************/ -#include "common/bt_trace.h" - +#include "bt_common.h" #include "osi/allocator.h" #include "osi/future.h" #include "osi/osi.h" diff --git a/components/bt/bluedroid/osi/hash_functions.c b/components/bt/common/osi/hash_functions.c similarity index 100% rename from components/bt/bluedroid/osi/hash_functions.c rename to components/bt/common/osi/hash_functions.c diff --git a/components/bt/bluedroid/osi/hash_map.c b/components/bt/common/osi/hash_map.c similarity index 99% rename from components/bt/bluedroid/osi/hash_map.c rename to components/bt/common/osi/hash_map.c index 1487b07ed..bd7f67d00 100644 --- a/components/bt/bluedroid/osi/hash_map.c +++ b/components/bt/common/osi/hash_map.c @@ -16,8 +16,7 @@ * ******************************************************************************/ -#include "common/bt_defs.h" -#include "common/bt_trace.h" +#include "bt_common.h" #include "osi/list.h" #include "osi/hash_map.h" #include "osi/allocator.h" diff --git a/components/bt/bluedroid/osi/include/osi/alarm.h b/components/bt/common/osi/include/osi/alarm.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/alarm.h rename to components/bt/common/osi/include/osi/alarm.h diff --git a/components/bt/bluedroid/osi/include/osi/allocator.h b/components/bt/common/osi/include/osi/allocator.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/allocator.h rename to components/bt/common/osi/include/osi/allocator.h diff --git a/components/bt/bluedroid/osi/include/osi/buffer.h b/components/bt/common/osi/include/osi/buffer.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/buffer.h rename to components/bt/common/osi/include/osi/buffer.h diff --git a/components/bt/bluedroid/osi/include/osi/config.h b/components/bt/common/osi/include/osi/config.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/config.h rename to components/bt/common/osi/include/osi/config.h diff --git a/components/bt/bluedroid/osi/include/osi/fixed_queue.h b/components/bt/common/osi/include/osi/fixed_queue.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/fixed_queue.h rename to components/bt/common/osi/include/osi/fixed_queue.h diff --git a/components/bt/bluedroid/osi/include/osi/future.h b/components/bt/common/osi/include/osi/future.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/future.h rename to components/bt/common/osi/include/osi/future.h diff --git a/components/bt/bluedroid/osi/include/osi/hash_functions.h b/components/bt/common/osi/include/osi/hash_functions.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/hash_functions.h rename to components/bt/common/osi/include/osi/hash_functions.h diff --git a/components/bt/bluedroid/osi/include/osi/hash_map.h b/components/bt/common/osi/include/osi/hash_map.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/hash_map.h rename to components/bt/common/osi/include/osi/hash_map.h diff --git a/components/bt/bluedroid/osi/include/osi/list.h b/components/bt/common/osi/include/osi/list.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/list.h rename to components/bt/common/osi/include/osi/list.h diff --git a/components/bt/bluedroid/osi/include/osi/mutex.h b/components/bt/common/osi/include/osi/mutex.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/mutex.h rename to components/bt/common/osi/include/osi/mutex.h diff --git a/components/bt/bluedroid/osi/include/osi/osi.h b/components/bt/common/osi/include/osi/osi.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/osi.h rename to components/bt/common/osi/include/osi/osi.h diff --git a/components/bt/bluedroid/osi/include/osi/semaphore.h b/components/bt/common/osi/include/osi/semaphore.h similarity index 100% rename from components/bt/bluedroid/osi/include/osi/semaphore.h rename to components/bt/common/osi/include/osi/semaphore.h diff --git a/components/bt/bluedroid/osi/include/osi/thread.h b/components/bt/common/osi/include/osi/thread.h similarity index 88% rename from components/bt/bluedroid/osi/include/osi/thread.h rename to components/bt/common/osi/include/osi/thread.h index 17da2d948..381bb5336 100644 --- a/components/bt/bluedroid/osi/include/osi/thread.h +++ b/components/bt/common/osi/include/osi/thread.h @@ -22,7 +22,7 @@ #include "freertos/queue.h" #include "freertos/task.h" #include "esp_task.h" -#include "common/bt_defs.h" +#include "bt_common.h" #define portBASE_TYPE int @@ -58,8 +58,6 @@ typedef enum { SIG_BTU_NUM, } SIG_BTU_t; -#define TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) - #define HCI_HOST_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define HCI_HOST_TASK_STACK_SIZE (2048 + BT_TASK_EXTRA_STACK_SIZE) #define HCI_HOST_TASK_PRIO (configMAX_PRIORITIES - 3) @@ -78,12 +76,6 @@ typedef enum { #define BTU_TASK_NAME "btuT" #define BTU_QUEUE_LEN 50 -#define BTC_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) -#define BTC_TASK_STACK_SIZE (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) //by menuconfig -#define BTC_TASK_NAME "btcT" -#define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) -#define BTC_TASK_QUEUE_LEN 60 - #define BTC_A2DP_SINK_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE) #define BTC_A2DP_SINK_TASK_STACK_SIZE (CONFIG_A2DP_SINK_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE) // by menuconfig #define BTC_A2DP_SINK_TASK_NAME "BtA2dSinkT" diff --git a/components/bt/bluedroid/osi/list.c b/components/bt/common/osi/list.c similarity index 95% rename from components/bt/bluedroid/osi/list.c rename to components/bt/common/osi/list.c index 1a41873ac..93db17fb3 100644 --- a/components/bt/bluedroid/osi/list.c +++ b/components/bt/common/osi/list.c @@ -1,6 +1,5 @@ -#include "common/bt_defs.h" - +#include "bt_common.h" #include "osi/allocator.h" #include "osi/list.h" #include "osi/osi.h" @@ -103,6 +102,7 @@ bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) { assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = prev_node->next; @@ -121,6 +121,7 @@ bool list_prepend(list_t *list, void *data) assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = list->head; @@ -139,6 +140,7 @@ bool list_append(list_t *list, void *data) assert(data != NULL); list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t)); if (!node) { + OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ ); return false; } node->next = NULL; diff --git a/components/bt/bluedroid/osi/mutex.c b/components/bt/common/osi/mutex.c similarity index 100% rename from components/bt/bluedroid/osi/mutex.c rename to components/bt/common/osi/mutex.c diff --git a/components/bt/bluedroid/osi/osi.c b/components/bt/common/osi/osi.c similarity index 100% rename from components/bt/bluedroid/osi/osi.c rename to components/bt/common/osi/osi.c diff --git a/components/bt/bluedroid/osi/semaphore.c b/components/bt/common/osi/semaphore.c similarity index 100% rename from components/bt/bluedroid/osi/semaphore.c rename to components/bt/common/osi/semaphore.c diff --git a/components/bt/component.mk b/components/bt/component.mk index eb9b39c02..04061b160 100644 --- a/components/bt/component.mk +++ b/components/bt/component.mk @@ -22,11 +22,11 @@ ifeq ($(GCC_NOT_5_2_0), 1) CFLAGS += -Wno-implicit-fallthrough endif -endif - - ifdef CONFIG_BLUEDROID_ENABLED +COMPONENT_SRCDIRS += common/osi \ + common/btc/core + COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/bta/ar/include \ bluedroid/bta/av/include \ @@ -41,7 +41,6 @@ COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/device/include \ bluedroid/gki/include \ bluedroid/hci/include \ - bluedroid/osi/include \ bluedroid/utils/include \ bluedroid/external/sbc/decoder/include \ bluedroid/external/sbc/encoder/include \ @@ -69,7 +68,10 @@ COMPONENT_PRIV_INCLUDEDIRS += bluedroid/bta/include \ bluedroid/stack/rfcomm/include \ bluedroid/stack/include \ bluedroid/utils/include \ - bluedroid/common/include + bluedroid/common/include \ + common/btc/include \ + common/osi/include \ + common/include COMPONENT_ADD_INCLUDEDIRS += bluedroid/api/include/api @@ -127,4 +129,48 @@ bluedroid/stack/btm/btm_sec.o: CFLAGS += -Wno-unused-const-variable bluedroid/stack/smp/smp_keys.o: CFLAGS += -Wno-unused-const-variable endif +ifdef CONFIG_BLE_MESH + +COMPONENT_ADD_INCLUDEDIRS += common/osi/include +COMPONENT_SRCDIRS += esp_ble_mesh/mesh_core/bluedroid_host + +endif +endif + +ifdef CONFIG_BLE_MESH +COMPONENT_ADD_INCLUDEDIRS += esp_ble_mesh/mesh_common/include \ + esp_ble_mesh/mesh_core \ + esp_ble_mesh/mesh_core/include \ + esp_ble_mesh/mesh_core/storage \ + esp_ble_mesh/btc/include \ + esp_ble_mesh/mesh_models/common/include \ + esp_ble_mesh/mesh_models/client/include \ + esp_ble_mesh/mesh_models/server/include \ + esp_ble_mesh/api/core/include \ + esp_ble_mesh/api/models/include \ + esp_ble_mesh/api + +COMPONENT_SRCDIRS += esp_ble_mesh/mesh_common \ + esp_ble_mesh/mesh_core \ + esp_ble_mesh/mesh_core/storage \ + esp_ble_mesh/btc \ + esp_ble_mesh/mesh_models/client \ + esp_ble_mesh/mesh_models/server \ + esp_ble_mesh/api/core \ + esp_ble_mesh/api/models +endif + +ifdef CONFIG_NIMBLE_ENABLED +ifdef CONFIG_BLE_MESH +COMPONENT_PRIV_INCLUDEDIRS += common/btc/include \ + common/include + +COMPONENT_SRCDIRS += common/osi \ + common/btc/core \ + esp_ble_mesh/mesh_core/nimble_host + +COMPONENT_ADD_INCLUDEDIRS += common/osi/include +endif +endif + endif diff --git a/components/bt/esp_ble_mesh/README.md b/components/bt/esp_ble_mesh/README.md new file mode 100644 index 000000000..e25e0b3a3 --- /dev/null +++ b/components/bt/esp_ble_mesh/README.md @@ -0,0 +1,21 @@ +# ESP-BLE-MESH Component + +This is Espressif Bluetooth Low Energy Mesh component folder. + +This component is a part of Espressif IoT Development Framework (ESP-IDF). For the latest documentation please refer to [ESP-IDF Programming Guide](https://docs.espressif.com/projects/esp-idf/en/latest/index.html). + +The ESP-BLE-MESH networking enables many-to-many (m:m) device communications and is optimized for creating large-scale device networks. + + +### [ESP-BLE-MESH Documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/index.html) + +- [Getting Started](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/index.html##getting-started-with-ble-mesh) +- [Architecture](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/arhitecture.html) +- [Feature List](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/ble_mesh-feature-list.html) +- [FAQ](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/ble_mesh_faq.html) +- [API Reference](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/bluetooth/ble_mesh.html) + + +### [ESP-BLE-MESH Examples](https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/esp_ble_mesh) + +- Refer to **ESP-BLE-MESH Examples** of [Getting Started](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/esp_ble_mesh/index.html##getting-started-with-ble-mesh) for the tutorials of ESP BLE Mesh examples. diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c new file mode 100644 index 000000000..1ba2dcd57 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_ble_api.c @@ -0,0 +1,73 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc/btc_manage.h" + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_defs.h" + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + +esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param, + const esp_ble_mesh_ble_adv_data_t *data) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (param == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_START_BLE_ADVERTISING; + + memcpy(&arg.start_ble_advertising.param, param, sizeof(esp_ble_mesh_ble_adv_param_t)); + if (data) { + memcpy(&arg.start_ble_advertising.data, data, sizeof(esp_ble_mesh_ble_adv_data_t)); + } + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (index >= CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING; + + arg.stop_ble_advertising.index = index; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c new file mode 100644 index 000000000..832e03dbb --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_common_api.c @@ -0,0 +1,92 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_defs.h" + +esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp) +{ + btc_ble_mesh_prov_args_t arg = {0}; + SemaphoreHandle_t semaphore = NULL; + btc_msg_t msg = {0}; + esp_err_t ret = ESP_OK; + + if (prov == NULL || comp == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + ret = bt_mesh_host_init(); + if (ret != ESP_OK) { + return ret; + } + + // Create a semaphore + if ((semaphore = xSemaphoreCreateCounting(1, 0)) == NULL) { + BT_ERR("%s, Failed to allocate memory for the semaphore", __func__); + return ESP_ERR_NO_MEM; + } + + arg.mesh_init.prov = prov; + arg.mesh_init.comp = comp; + /* Transport semaphore pointer to BTC layer, and will give the semaphore in the BTC task */ + arg.mesh_init.semaphore = semaphore; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_MESH_INIT; + + if (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) != BT_STATUS_SUCCESS) { + vSemaphoreDelete(semaphore); + BT_ERR("%s, BLE Mesh initialise failed", __func__); + return ESP_FAIL; + } + + /* Take the Semaphore, wait BLE Mesh initialization to finish. */ + xSemaphoreTake(semaphore, portMAX_DELAY); + /* Don't forget to delete the semaphore at the end. */ + vSemaphoreDelete(semaphore); + + return ESP_OK; +} + +esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (param == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + arg.mesh_deinit.param.erase_flash = param->erase_flash; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_DEINIT_MESH; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c new file mode 100644 index 000000000..943c91bff --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c @@ -0,0 +1,128 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_defs.h" + +int32_t esp_ble_mesh_get_model_publish_period(esp_ble_mesh_model_t *model) +{ + if (model == NULL) { + return 0; + } + return btc_ble_mesh_model_pub_period_get(model); +} + +uint16_t esp_ble_mesh_get_primary_element_address(void) +{ + return btc_ble_mesh_get_primary_addr(); +} + +uint16_t *esp_ble_mesh_is_model_subscribed_to_group(esp_ble_mesh_model_t *model, uint16_t group_addr) +{ + if (model == NULL) { + return NULL; + } + return btc_ble_mesh_model_find_group(model, group_addr); +} + +esp_ble_mesh_elem_t *esp_ble_mesh_find_element(uint16_t element_addr) +{ + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr)) { + return NULL; + } + return btc_ble_mesh_elem_find(element_addr); +} + +uint8_t esp_ble_mesh_get_element_count(void) +{ + return btc_ble_mesh_elem_count(); +} + +esp_ble_mesh_model_t *esp_ble_mesh_find_vendor_model(const esp_ble_mesh_elem_t *element, + uint16_t company_id, uint16_t model_id) +{ + if (element == NULL) { + return NULL; + } + return btc_ble_mesh_model_find_vnd(element, company_id, model_id); +} + +esp_ble_mesh_model_t *esp_ble_mesh_find_sig_model(const esp_ble_mesh_elem_t *element, uint16_t model_id) +{ + if (element == NULL) { + return NULL; + } + return btc_ble_mesh_model_find(element, model_id); +} + +const esp_ble_mesh_comp_t *esp_ble_mesh_get_composition_data(void) +{ + return btc_ble_mesh_comp_get(); +} + +esp_err_t esp_ble_mesh_model_subscribe_group_addr(uint16_t element_addr, uint16_t company_id, + uint16_t model_id, uint16_t group_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr) || + !ESP_BLE_MESH_ADDR_IS_GROUP(group_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR; + + arg.model_sub_group_addr.element_addr = element_addr; + arg.model_sub_group_addr.company_id = company_id; + arg.model_sub_group_addr.model_id = model_id; + arg.model_sub_group_addr.group_addr = group_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_model_unsubscribe_group_addr(uint16_t element_addr, uint16_t company_id, + uint16_t model_id, uint16_t group_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr) || + !ESP_BLE_MESH_ADDR_IS_GROUP(group_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR; + + arg.model_unsub_group_addr.element_addr = element_addr; + arg.model_unsub_group_addr.company_id = company_id; + arg.model_unsub_group_addr.model_id = model_id; + arg.model_unsub_group_addr.group_addr = group_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c new file mode 100644 index 000000000..23bebe595 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_low_power_api.c @@ -0,0 +1,63 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_defs.h" + +esp_err_t esp_ble_mesh_lpn_enable(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_LPN_ENABLE; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_lpn_disable(bool force) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_LPN_DISABLE; + + arg.lpn_disable.force = force; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_lpn_poll(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_LPN_POLL; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c new file mode 100644 index 000000000..db6b6a144 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_networking_api.c @@ -0,0 +1,518 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_networking_api.h" + +#define ESP_BLE_MESH_TX_SDU_MAX ((CONFIG_BLE_MESH_ADV_BUF_COUNT - 3) * 12) + +static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, + uint32_t opcode, + btc_ble_mesh_model_act_t act, + uint16_t length, uint8_t *data, + int32_t msg_timeout, bool need_rsp, + esp_ble_mesh_dev_role_t device_role) +{ + btc_ble_mesh_model_args_t arg = {0}; + uint8_t op_len = 0, mic_len = 0; + uint8_t *msg_data = NULL; + btc_msg_t msg = {0}; + esp_err_t status = ESP_OK; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + if (ctx && ctx->addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("%s, Invalid destination address 0x0000", __func__); + return ESP_ERR_INVALID_ARG; + } + + if (device_role > ROLE_FAST_PROV) { + BT_ERR("%s, Invalid device role 0x%02x", __func__, device_role); + return ESP_ERR_INVALID_ARG; + } + + /* When data is NULL, it is mandatory to set length to 0 to prevent users from misinterpreting parameters. */ + if (data == NULL) { + length = 0; + } + + if (opcode < 0x100) { + op_len = 1; + } else if (opcode < 0x10000) { + op_len = 2; + } else { + op_len = 3; + } + + if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { + if (op_len + length > model->pub->msg->size) { + BT_ERR("%s, Model publication msg size %d is too small", __func__, model->pub->msg->size); + return ESP_ERR_INVALID_ARG; + } + } + + if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { + mic_len = ESP_BLE_MESH_MIC_SHORT; + } else { + mic_len = ctx->send_rel ? ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT; + } + + if (op_len + length + mic_len > MIN(ESP_BLE_MESH_SDU_MAX_LEN, ESP_BLE_MESH_TX_SDU_MAX)) { + BT_ERR("%s, Data length %d is too large", __func__, length); + return ESP_ERR_INVALID_ARG; + } + + if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { + bt_mesh_model_msg_init(model->pub->msg, opcode); + net_buf_simple_add_mem(model->pub->msg, data, length); + } else { + msg_data = (uint8_t *)bt_mesh_malloc(op_len + length); + if (msg_data == NULL) { + return ESP_ERR_NO_MEM; + } + esp_ble_mesh_model_msg_opcode_init(msg_data, opcode); + memcpy(msg_data + op_len, data, length); + } + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MODEL; + msg.act = act; + + if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) { + arg.model_publish.model = model; + arg.model_publish.device_role = device_role; + } else { + arg.model_send.model = model; + arg.model_send.ctx = ctx; + arg.model_send.need_rsp = need_rsp; + arg.model_send.opcode = opcode; + arg.model_send.length = op_len + length; + arg.model_send.data = msg_data; + arg.model_send.device_role = device_role; + arg.model_send.msg_timeout = msg_timeout; + } + + status = (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_model_args_t), btc_ble_mesh_model_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); + + bt_mesh_free(msg_data); + + return status; +} + +esp_err_t esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_MODEL, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode) +{ + uint16_t val = 0; + + if (data == NULL) { + return ESP_ERR_INVALID_ARG; + } + + if (opcode < 0x100) { + /* 1-byte OpCode */ + data[0] = opcode & 0xff; + return ESP_OK; + } + + if (opcode < 0x10000) { + /* 2-byte OpCode, big endian */ + val = sys_cpu_to_be16 (opcode); + memcpy(data, &val, 2); + return ESP_OK; + } + + /* 3-byte OpCode, note that little endian for the least 2 bytes(Company ID) of opcode */ + data[0] = (opcode >> 16) & 0xff; + val = sys_cpu_to_le16(opcode & 0xffff); + memcpy(&data[1], &val, 2); + + return ESP_OK; +} + +esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) +{ + if (model == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return btc_ble_mesh_client_model_init(model); +} + +esp_err_t esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model) +{ + if (model == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return btc_ble_mesh_client_model_deinit(model); +} + +esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode, + uint16_t length, uint8_t *data) +{ + if (model == NULL || ctx == NULL || + ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED || + ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) { + return ESP_ERR_INVALID_ARG; + } + + return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_SERVER_MODEL_SEND, + length, data, 0, false, ROLE_NODE); +} + +esp_err_t esp_ble_mesh_client_model_send_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode, + uint16_t length, uint8_t *data, int32_t msg_timeout, + bool need_rsp, esp_ble_mesh_dev_role_t device_role) +{ + if (model == NULL || ctx == NULL || + ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED || + ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) { + return ESP_ERR_INVALID_ARG; + } + + return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND, + length, data, msg_timeout, need_rsp, device_role); +} + +esp_err_t esp_ble_mesh_model_publish(esp_ble_mesh_model_t *model, uint32_t opcode, + uint16_t length, uint8_t *data, + esp_ble_mesh_dev_role_t device_role) +{ + if (model == NULL || model->pub == NULL || model->pub->msg == NULL || + model->pub->publish_addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + return ble_mesh_model_send_msg(model, NULL, opcode, BTC_BLE_MESH_ACT_MODEL_PUBLISH, + length, data, 0, false, device_role); +} + +esp_err_t esp_ble_mesh_server_model_update_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_server_state_type_t type, + esp_ble_mesh_server_state_value_t *value) +{ + btc_ble_mesh_model_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!model || !value || type >= ESP_BLE_MESH_SERVER_MODEL_STATE_MAX) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + arg.model_update_state.model = model; + arg.model_update_state.type = type; + arg.model_update_state.value = value; + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_MODEL; + msg.act = BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_model_args_t), btc_ble_mesh_model_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_node_local_reset(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_NODE_RESET; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +#if (CONFIG_BLE_MESH_PROVISIONER) + +esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME; + + arg.set_node_name.index = index; + memset(arg.set_node_name.name, 0, sizeof(arg.set_node_name.name)); + strncpy(arg.set_node_name.name, name, ESP_BLE_MESH_NODE_NAME_MAX_LEN); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index) +{ + return bt_mesh_provisioner_get_node_name(index); +} + +uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name) +{ + if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) { + return ESP_BLE_MESH_INVALID_NODE_INDEX; + } + + return bt_mesh_provisioner_get_node_index(name); +} + +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr) || !data || length <= 14) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA; + + arg.store_node_comp_data.unicast_addr = unicast_addr; + arg.store_node_comp_data.length = length; + arg.store_node_comp_data.data = data; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) +{ + if (!uuid) { + return NULL; + } + + return btc_ble_mesh_provisioner_get_node_with_uuid(uuid); +} + +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) +{ + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return NULL; + } + + return btc_ble_mesh_provisioner_get_node_with_addr(unicast_addr); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!uuid) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID; + + memcpy(arg.delete_node_with_uuid.uuid, uuid, 16); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR; + + arg.delete_node_with_addr.unicast_addr = unicast_addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], + uint16_t net_idx, uint16_t app_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY; + + arg.add_local_app_key.net_idx = net_idx; + arg.add_local_app_key.app_idx = app_idx; + if (app_key) { + memcpy(arg.add_local_app_key.app_key, app_key, 16); + } else { + bzero(arg.add_local_app_key.app_key, 16); + } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16], + uint16_t net_idx, uint16_t app_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (app_key == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY; + + memcpy(arg.update_local_app_key.app_key, app_key, 16); + arg.update_local_app_key.net_idx = net_idx; + arg.update_local_app_key.app_idx = app_idx; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +const uint8_t *esp_ble_mesh_provisioner_get_local_app_key(uint16_t net_idx, uint16_t app_idx) +{ + return bt_mesh_provisioner_local_app_key_get(net_idx, app_idx); +} + +esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_addr, uint16_t app_idx, + uint16_t model_id, uint16_t company_id) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP; + + arg.local_mod_app_bind.elem_addr = element_addr; + arg.local_mod_app_bind.app_idx = app_idx; + arg.local_mod_app_bind.model_id = model_id; + arg.local_mod_app_bind.cid = company_id; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], uint16_t net_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (net_idx == ESP_BLE_MESH_KEY_PRIMARY) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY; + + arg.add_local_net_key.net_idx = net_idx; + if (net_key) { + memcpy(arg.add_local_net_key.net_key, net_key, 16); + } else { + bzero(arg.add_local_net_key.net_key, 16); + } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16], uint16_t net_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (net_key == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY; + + memcpy(arg.update_local_net_key.net_key, net_key, 16); + arg.update_local_net_key.net_idx = net_idx; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx) +{ + return bt_mesh_provisioner_local_net_key_get(net_idx); +} + +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void) +{ + return btc_ble_mesh_provisioner_get_prov_node_count(); +} + +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +#if (CONFIG_BLE_MESH_FAST_PROV) +const uint8_t *esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx, uint16_t app_idx) +{ + return bt_mesh_get_fast_prov_app_key(net_idx, app_idx); +} +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c new file mode 100644 index 000000000..9fbbd6b59 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c @@ -0,0 +1,501 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_provisioning_api.h" + +#define MAX_PROV_LINK_IDX (CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME) +#define MAX_OOB_INPUT_NUM 0x5F5E0FF /* Decimal: 99999999 */ + +esp_err_t esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_PROV, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +bool esp_ble_mesh_node_is_provisioned(void) +{ + return bt_mesh_is_provisioned(); +} + +esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROV_ENABLE; + arg.node_prov_enable.bearers = bearers; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROV_DISABLE; + arg.node_prov_disable.bearers = bearers; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_node_set_oob_pub_key(uint8_t pub_key_x[32], uint8_t pub_key_y[32], + uint8_t private_key[32]) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!pub_key_x || !pub_key_y || !private_key) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY; + + memcpy(arg.set_oob_pub_key.pub_key_x, pub_key_x, 32); + memcpy(arg.set_oob_pub_key.pub_key_y, pub_key_y, 32); + memcpy(arg.set_oob_pub_key.private_key, private_key, 32); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_node_input_number(uint32_t number) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (number > MAX_OOB_INPUT_NUM) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_INPUT_NUMBER; + arg.input_number.number = number; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_node_input_string(const char *string) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!string || strlen(string) > ESP_BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_INPUT_STRING; + memset(arg.input_string.string, 0, sizeof(arg.input_string.string)); + strncpy(arg.input_string.string, string, + MIN(strlen(string), sizeof(arg.input_string.string))); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_set_unprovisioned_device_name(const char *name) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!name || strlen(name) > ESP_BLE_MESH_DEVICE_NAME_MAX_LEN) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_SET_DEVICE_NAME; + + memset(arg.set_device_name.name, 0, sizeof(arg.set_device_name.name)); + strncpy(arg.set_device_name.name, name, ESP_BLE_MESH_DEVICE_NAME_MAX_LEN); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +#if (CONFIG_BLE_MESH_PROVISIONER) +esp_err_t esp_ble_mesh_provisioner_read_oob_pub_key(uint8_t link_idx, uint8_t pub_key_x[32], + uint8_t pub_key_y[32]) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!pub_key_x || !pub_key_y || link_idx >= MAX_PROV_LINK_IDX) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY; + + arg.provisioner_read_oob_pub_key.link_idx = link_idx; + memcpy(arg.provisioner_read_oob_pub_key.pub_key_x, pub_key_x, 32); + memcpy(arg.provisioner_read_oob_pub_key.pub_key_y, pub_key_y, 32); + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!string || strlen(string) > ESP_BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN || + link_idx >= MAX_PROV_LINK_IDX) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR; + + memset(arg.provisioner_input_str.string, 0, sizeof(arg.provisioner_input_str.string)); + strncpy(arg.provisioner_input_str.string, string, + MIN(strlen(string), sizeof(arg.provisioner_input_str.string))); + arg.provisioner_input_str.link_idx = link_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_input_number(uint32_t number, uint8_t link_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (number > MAX_OOB_INPUT_NUM || link_idx >= MAX_PROV_LINK_IDX) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM; + + arg.provisioner_input_num.number = number; + arg.provisioner_input_num.link_idx = link_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearers) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ENABLE; + + arg.provisioner_enable.bearers = bearers; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t bearers) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DISABLE; + + arg.provisioner_disable.bearers = bearers; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t *add_dev, + esp_ble_mesh_dev_add_flag_t flags) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (add_dev == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD; + + arg.provisioner_dev_add.add_dev.addr_type = add_dev->addr_type; + arg.provisioner_dev_add.add_dev.oob_info = add_dev->oob_info; + arg.provisioner_dev_add.add_dev.bearer = add_dev->bearer; + memcpy(arg.provisioner_dev_add.add_dev.addr, add_dev->addr, sizeof(esp_ble_mesh_bd_addr_t)); + memcpy(arg.provisioner_dev_add.add_dev.uuid, add_dev->uuid, 16); + arg.provisioner_dev_add.flags = flags; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], + esp_ble_mesh_bd_addr_t addr, esp_ble_mesh_addr_type_t addr_type, + esp_ble_mesh_prov_bearer_t bearer, uint16_t oob_info, uint16_t unicast_addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (uuid == NULL || (bearer == ESP_BLE_MESH_PROV_GATT && (addr == NULL || + addr_type > ESP_BLE_MESH_ADDR_TYPE_RANDOM)) || + (bearer != ESP_BLE_MESH_PROV_ADV && bearer != ESP_BLE_MESH_PROV_GATT) || + !ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR; + + memcpy(arg.provisioner_prov_dev_with_addr.uuid, uuid, 16); + if (addr) { + memcpy(arg.provisioner_prov_dev_with_addr.addr, addr, BD_ADDR_LEN); + arg.provisioner_prov_dev_with_addr.addr_type = addr_type; + } + arg.provisioner_prov_dev_with_addr.bearer = bearer; + arg.provisioner_prov_dev_with_addr.oob_info = oob_info; + arg.provisioner_prov_dev_with_addr.unicast_addr = unicast_addr; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_dev) +{ + uint8_t val = DEL_DEV_ADDR_FLAG | DEL_DEV_UUID_FLAG; + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (del_dev == NULL || (__builtin_popcount(del_dev->flag & val) != 1)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL; + + arg.provisioner_dev_del.del_dev.flag = del_dev->flag; + if (del_dev->flag & DEL_DEV_ADDR_FLAG) { + arg.provisioner_dev_del.del_dev.addr_type = del_dev->addr_type; + memcpy(arg.provisioner_dev_del.del_dev.addr, del_dev->addr, sizeof(esp_ble_mesh_bd_addr_t)); + } else if (del_dev->flag & DEL_DEV_UUID_FLAG) { + memcpy(arg.provisioner_dev_del.del_dev.uuid, del_dev->uuid, 16); + } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, uint8_t match_len, + uint8_t offset, bool prov_after_match) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (match_len + offset > ESP_BLE_MESH_OCTET16_LEN) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH; + + if (match_len && match_val) { + memcpy(arg.set_dev_uuid_match.match_val, match_val, match_len); + } + arg.set_dev_uuid_match.match_len = match_len; + arg.set_dev_uuid_match.offset = offset; + arg.set_dev_uuid_match.prov_after_match = prov_after_match; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_info_t *prov_data_info) +{ + uint8_t val = PROV_DATA_NET_IDX_FLAG | PROV_DATA_FLAGS_FLAG | PROV_DATA_IV_INDEX_FLAG; + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (prov_data_info == NULL || (__builtin_popcount(prov_data_info->flag & val) != 1)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO; + + arg.set_prov_data_info.prov_data.flag = prov_data_info->flag; + if (prov_data_info->flag & PROV_DATA_NET_IDX_FLAG) { + arg.set_prov_data_info.prov_data.net_idx = prov_data_info->net_idx; + } else if (prov_data_info->flag & PROV_DATA_FLAGS_FLAG) { + arg.set_prov_data_info.prov_data.flags = prov_data_info->flags; + } else if (prov_data_info->flag & PROV_DATA_IV_INDEX_FLAG) { + arg.set_prov_data_info.prov_data.iv_index = prov_data_info->iv_index; + } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (value == NULL || length == 0 || length > 16) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL; + + arg.set_static_oob_val.length = length; + memcpy(arg.set_static_oob_val.value, value, length); + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_provisioner_set_primary_elem_addr(uint16_t addr) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!ESP_BLE_MESH_ADDR_IS_UNICAST(addr)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR; + + arg.set_primary_elem_addr.addr = addr; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +/* The following APIs are for fast provisioning */ + +#if (CONFIG_BLE_MESH_FAST_PROV) + +esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_prov_info) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (fast_prov_info == NULL || (fast_prov_info->offset + + fast_prov_info->match_len > ESP_BLE_MESH_OCTET16_LEN)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO; + + arg.set_fast_prov_info.unicast_min = fast_prov_info->unicast_min; + arg.set_fast_prov_info.unicast_max = fast_prov_info->unicast_max; + arg.set_fast_prov_info.net_idx = fast_prov_info->net_idx; + arg.set_fast_prov_info.flags = fast_prov_info->flags; + arg.set_fast_prov_info.iv_index = fast_prov_info->iv_index; + arg.set_fast_prov_info.offset = fast_prov_info->offset; + arg.set_fast_prov_info.match_len = fast_prov_info->match_len; + if (fast_prov_info->match_len && fast_prov_info->match_val) { + memcpy(arg.set_fast_prov_info.match_val, fast_prov_info->match_val, fast_prov_info->match_len); + } + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_set_fast_prov_action(esp_ble_mesh_fast_prov_action_t action) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (action >= FAST_PROV_ACT_MAX) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION; + + arg.set_fast_prov_action.action = action; + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c new file mode 100644 index 000000000..31a3aa9ab --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_proxy_api.c @@ -0,0 +1,175 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_err.h" + +#include "btc_ble_mesh_prov.h" +#include "esp_ble_mesh_defs.h" + +esp_err_t esp_ble_mesh_proxy_identity_enable(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_gatt_enable(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_GATT_ENABLE; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_gatt_disable(void) +{ + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_GATT_DISABLE; + + return (btc_transfer_context(&msg, NULL, 0, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_client_connect(esp_ble_mesh_bd_addr_t addr, + esp_ble_mesh_addr_type_t addr_type, uint16_t net_idx) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!addr || addr_type > ESP_BLE_MESH_ADDR_TYPE_RANDOM) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_CLIENT_CONNECT; + + memcpy(arg.proxy_client_connect.addr, addr, BD_ADDR_LEN); + arg.proxy_client_connect.addr_type = addr_type; + arg.proxy_client_connect.net_idx = net_idx; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_client_disconnect(uint8_t conn_handle) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_CLIENT_DISCONNECT; + + arg.proxy_client_disconnect.conn_handle = conn_handle; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_client_set_filter_type(uint8_t conn_handle, + uint16_t net_idx, esp_ble_mesh_proxy_filter_type_t filter_type) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (filter_type > PROXY_FILTER_BLACKLIST) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE; + + arg.proxy_client_set_filter_type.conn_handle = conn_handle; + arg.proxy_client_set_filter_type.net_idx = net_idx; + arg.proxy_client_set_filter_type.filter_type = filter_type; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_client_add_filter_addr(uint8_t conn_handle, + uint16_t net_idx, uint16_t *addr, uint16_t addr_num) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!addr || addr_num == 0) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR; + + arg.proxy_client_add_filter_addr.conn_handle = conn_handle; + arg.proxy_client_add_filter_addr.net_idx = net_idx; + arg.proxy_client_add_filter_addr.addr_num = addr_num; + arg.proxy_client_add_filter_addr.addr = addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_proxy_client_remove_filter_addr(uint8_t conn_handle, + uint16_t net_idx, uint16_t *addr, uint16_t addr_num) +{ + btc_ble_mesh_prov_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (!addr || addr_num == 0) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_PROV; + msg.act = BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR; + + arg.proxy_client_remove_filter_addr.conn_handle = conn_handle; + arg.proxy_client_remove_filter_addr.net_idx = net_idx; + arg.proxy_client_remove_filter_addr.addr_num = addr_num; + arg.proxy_client_remove_filter_addr.addr = addr; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h new file mode 100644 index 000000000..23ebca66b --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_ble_api.h @@ -0,0 +1,65 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_BLE_API_H_ +#define _ESP_BLE_MESH_BLE_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief This function is called to start BLE advertising with the corresponding data + * and parameters while BLE Mesh is working at the same time. + * + * @note 1. When this function is called, the BLE advertising packet will be posted to + * the BLE mesh adv queue in the mesh stack and waited to be sent. + * 2. In the BLE advertising parameters, the "duration" means the time used for + * sending the BLE advertising packet each time, it shall not be smaller than the + * advertising interval. When the packet is sent successfully, it will be posted + * to the adv queue again after the "period" time if the "count" is bigger than 0. + * The "count" means how many durations the packet will be sent after it is sent + * successfully for the first time. And if the "count" is set to 0xFFFF, which + * means the packet will be sent infinitely. + * 3. The "priority" means the priority of BLE advertising packet compared with + * BLE Mesh packets. Currently two options (i.e. low/high) are provided. If the + * "priority" is high, the BLE advertising packet will be posted to the front of + * adv queue. Otherwise it will be posted to the back of adv queue. + * + * @param[in] param: Pointer to the BLE advertising parameters + * @param[in] data: Pointer to the BLE advertising data and scan response data + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_start_ble_advertising(const esp_ble_mesh_ble_adv_param_t *param, + const esp_ble_mesh_ble_adv_data_t *data); + +/** + * @brief This function is called to stop BLE advertising with the corresponding index. + * + * @param[in] index: Index of BLE advertising + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_stop_ble_advertising(uint8_t index); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_BLE_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h new file mode 100644 index 000000000..634327d3f --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h @@ -0,0 +1,57 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_COMMON_API_H_ +#define _ESP_BLE_MESH_COMMON_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Initialize BLE Mesh module. + * This API initializes provisioning capabilities and composition data information. + * + * @note After calling this API, the device needs to call esp_ble_mesh_prov_enable() + * to enable provisioning functionality again. + * + * @param[in] prov: Pointer to the device provisioning capabilities. This pointer must + * remain valid during the lifetime of the BLE Mesh device. + * @param[in] comp: Pointer to the device composition data information. This pointer + * must remain valid during the lifetime of the BLE Mesh device. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_init(esp_ble_mesh_prov_t *prov, esp_ble_mesh_comp_t *comp); + +/** + * @brief De-initialize BLE Mesh module. + * + * @note This function shall be invoked after esp_ble_mesh_client_model_deinit(). + * + * @param[in] param: Pointer to the structure of BLE Mesh deinit parameters. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_deinit(esp_ble_mesh_deinit_param_t *param); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_COMMON_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h new file mode 100644 index 000000000..0cd702e69 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h @@ -0,0 +1,147 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_LOCAL_DATA_OPERATION_API_H_ +#define _ESP_BLE_MESH_LOCAL_DATA_OPERATION_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get the model publish period, the unit is ms. + * + * @param[in] model: Model instance pointer. + * + * @return Publish period value on success, 0 or (negative) error code from errno.h on failure. + * + */ +int32_t esp_ble_mesh_get_model_publish_period(esp_ble_mesh_model_t *model); + +/** + * @brief Get the address of the primary element. + * + * @return Address of the primary element on success, or + * ESP_BLE_MESH_ADDR_UNASSIGNED on failure which means the device has not been provisioned. + * + */ +uint16_t esp_ble_mesh_get_primary_element_address(void); + +/** + * @brief Check if the model has subscribed to the given group address. + * Note: E.g., once a status message is received and the destination address + * is a group address, the model uses this API to check if it is successfully subscribed + * to the given group address. + * + * @param[in] model: Pointer to the model. + * @param[in] group_addr: Group address. + * + * @return Pointer to the group address within the Subscription List of the model on success, or + * NULL on failure which means the model has not subscribed to the given group address. + * Note: With the pointer to the group address returned, you can reset the group address + * to 0x0000 in order to unsubscribe the model from the group. + * + */ +uint16_t *esp_ble_mesh_is_model_subscribed_to_group(esp_ble_mesh_model_t *model, uint16_t group_addr); + +/** + * @brief Find the BLE Mesh element pointer via the element address. + * + * @param[in] element_addr: Element address. + * + * @return Pointer to the element on success, or NULL on failure. + * + */ +esp_ble_mesh_elem_t *esp_ble_mesh_find_element(uint16_t element_addr); + +/** + * @brief Get the number of elements that have been registered. + * + * @return Number of elements. + * + */ +uint8_t esp_ble_mesh_get_element_count(void); + +/** + * @brief Find the Vendor specific model with the given element, + * the company ID and the Vendor Model ID. + * + * @param[in] element: Element to which the model belongs. + * @param[in] company_id: A 16-bit company identifier assigned by the Bluetooth SIG. + * @param[in] model_id: A 16-bit vendor-assigned model identifier. + * + * @return Pointer to the Vendor Model on success, or NULL on failure which means the Vendor Model is not found. + * + */ +esp_ble_mesh_model_t *esp_ble_mesh_find_vendor_model(const esp_ble_mesh_elem_t *element, + uint16_t company_id, uint16_t model_id); + +/** + * @brief Find the SIG model with the given element and Model id. + * + * @param[in] element: Element to which the model belongs. + * @param[in] model_id: SIG model identifier. + * + * @return Pointer to the SIG Model on success, or NULL on failure which means the SIG Model is not found. + * + */ +esp_ble_mesh_model_t *esp_ble_mesh_find_sig_model(const esp_ble_mesh_elem_t *element, uint16_t model_id); + +/** + * @brief Get the Composition data which has been registered. + * + * @return Pointer to the Composition data on success, or NULL on failure which means the Composition data is not initialized. + * + */ +const esp_ble_mesh_comp_t *esp_ble_mesh_get_composition_data(void); + +/** + * @brief A local model of node or Provisioner subscribes a group address. + * + * @note This function shall not be invoked before node is provisioned or Provisioner is enabled. + * + * @param[in] element_addr: Unicast address of the element to which the model belongs. + * @param[in] company_id: A 16-bit company identifier. + * @param[in] model_id: A 16-bit model identifier. + * @param[in] group_addr: The group address to be subscribed. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_model_subscribe_group_addr(uint16_t element_addr, uint16_t company_id, + uint16_t model_id, uint16_t group_addr); + +/** + * @brief A local model of node or Provisioner unsubscribes a group address. + * + * @note This function shall not be invoked before node is provisioned or Provisioner is enabled. + * + * @param[in] element_addr: Unicast address of the element to which the model belongs. + * @param[in] company_id: A 16-bit company identifier. + * @param[in] model_id: A 16-bit model identifier. + * @param[in] group_addr: The subscribed group address. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_model_unsubscribe_group_addr(uint16_t element_addr, uint16_t company_id, + uint16_t model_id, uint16_t group_addr); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_LOCAL_DATA_OPERATION_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_low_power_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_low_power_api.h new file mode 100644 index 000000000..b8fea9a9e --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_low_power_api.h @@ -0,0 +1,69 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_LOW_POWER_API_H_ +#define _ESP_BLE_MESH_LOW_POWER_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enable BLE Mesh device LPN functionality. + * + * @note This API enables LPN functionality. Once called, the proper + * Friend Request will be sent. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_lpn_enable(void); + +/** + * @brief Disable BLE Mesh device LPN functionality. + * + * @param[in] force: when disabling LPN functionality, use this flag to indicate + * whether directly clear corresponding information or just + * send friend clear to disable it if friendship has already + * been established. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_lpn_disable(bool force); + +/** + * @brief LPN tries to poll messages from the Friend Node. + * + * @note The Friend Poll message is sent by a Low Power node to ask the Friend + * node to send a message that it has stored for the Low Power node. + * Users can call this API to send Friend Poll message manually. If this + * API is not invoked, the bottom layer of the Low Power node will send + * Friend Poll before the PollTimeout timer expires. + * If the corresponding Friend Update is received and MD is set to 0, + * which means there are no messages for the Low Power node, then the + * Low Power node will stop scanning. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_lpn_poll(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_LOW_POWER_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h new file mode 100644 index 000000000..c2def986f --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h @@ -0,0 +1,392 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_NETWORKING_API_H_ +#define _ESP_BLE_MESH_NETWORKING_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief: event, event code of user-defined model events; param, parameters of user-defined model events */ +typedef void (* esp_ble_mesh_model_cb_t)(esp_ble_mesh_model_cb_event_t event, + esp_ble_mesh_model_cb_param_t *param); + +/** + * @brief Register BLE Mesh callback for user-defined models' operations. + * This callback can report the following events generated for the user-defined models: + * - Call back the messages received by user-defined client and server models to the + * application layer; + * - If users call esp_ble_mesh_server/client_model_send, this callback notifies the + * application layer of the send_complete event; + * - If user-defined client model sends a message that requires response, and the response + * message is received after the timer expires, the response message will be reported + * to the application layer as published by a peer device; + * - If the user-defined client model fails to receive the response message during a specified + * period of time, a timeout event will be reported to the application layer. + * + * @note The client models (i.e. Config Client model, Health Client model, Generic + * Client models, Sensor Client model, Scene Client model and Lighting Client models) + * that have been realized internally have their specific register functions. + * For example, esp_ble_mesh_register_config_client_callback is the register + * function for Config Client Model. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t callback); + +/** + * @brief Add the message opcode to the beginning of the model message + * before sending or publishing the model message. + * + * @note This API is only used to set the opcode of the message. + * + * @param[in] data: Pointer to the message data. + * @param[in] opcode: The message opcode. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode); + +/** + * @brief Initialize the user-defined client model. All user-defined client models + * shall call this function to initialize the client model internal data. + * Node: Before calling this API, the op_pair_size and op_pair variabled within + * the user_data(defined using esp_ble_mesh_client_t_) of the client model + * need to be initialized. + * + * @param[in] model: BLE Mesh Client model to which the message belongs. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); + +/** + * @brief De-initialize the user-defined client model. + * + * @note This function shall be invoked before esp_ble_mesh_deinit() is called. + * + * @param[in] model: Pointer of the Client model. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model); + +/** + * @brief Send server model messages(such as server model status messages). + * + * @param[in] model: BLE Mesh Server Model to which the message belongs. + * @param[in] ctx: Message context, includes keys, TTL, etc. + * @param[in] opcode: Message opcode. + * @param[in] length: Message length (exclude the message opcode). + * @param[in] data: Parameters of Access Payload (exclude the message opcode) to be sent. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode, + uint16_t length, uint8_t *data); + +/** + * @brief Send client model message (such as model get, set, etc). + * + * @param[in] model: BLE Mesh Client Model to which the message belongs. + * @param[in] ctx: Message context, includes keys, TTL, etc. + * @param[in] opcode: Message opcode. + * @param[in] length: Message length (exclude the message opcode). + * @param[in] data: Parameters of the Access Payload (exclude the message opcode) to be sent. + * @param[in] msg_timeout: Time to get response to the message (in milliseconds). + * @param[in] need_rsp: TRUE if the opcode requires the peer device to reply, FALSE otherwise. + * @param[in] device_role: Role of the device (Node/Provisioner) that sends the message. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_client_model_send_msg(esp_ble_mesh_model_t *model, + esp_ble_mesh_msg_ctx_t *ctx, uint32_t opcode, + uint16_t length, uint8_t *data, int32_t msg_timeout, + bool need_rsp, esp_ble_mesh_dev_role_t device_role); + +/** + * @brief Send a model publication message. + * + * @note Before calling this function, the user needs to ensure that the model + * publication message (@ref esp_ble_mesh_model_pub_t.msg) contains a valid + * message to be sent. And if users want to update the publishing message, + * this API should be called in ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT + * with the message updated. + * + * + * @param[in] model: Mesh (client) Model publishing the message. + * @param[in] opcode: Message opcode. + * @param[in] length: Message length (exclude the message opcode). + * @param[in] data: Parameters of the Access Payload (exclude the message opcode) to be sent. + * @param[in] device_role: Role of the device (node/provisioner) publishing the message of the type esp_ble_mesh_dev_role_t. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_model_publish(esp_ble_mesh_model_t *model, uint32_t opcode, + uint16_t length, uint8_t *data, + esp_ble_mesh_dev_role_t device_role); + +/** + * @brief Update a server model state value. If the model publication + * state is set properly (e.g. publish address is set to a valid + * address), it will publish corresponding status message. + * + * @note Currently this API is used to update bound state value, not + * for all server model states. + * + * @param[in] model: Server model which is going to update the state. + * @param[in] type: Server model state type. + * @param[in] value: Server model state value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_server_model_update_state(esp_ble_mesh_model_t *model, + esp_ble_mesh_server_state_type_t type, + esp_ble_mesh_server_state_value_t *value); + +/** + * @brief Reset the provisioning procedure of the local BLE Mesh node. + * + * @note All provisioning information in this node will be deleted and the node + * needs to be reprovisioned. The API function esp_ble_mesh_node_prov_enable() + * needs to be called to start a new provisioning procedure. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_node_local_reset(void); + +/** + * @brief This function is called to set the node (provisioned device) name. + * + * @param[in] index: Index of the node in the node queue. + * @param[in] name: Name (end by '\0') to be set for the node. + * + * @note index is obtained from the parameters of ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name); + +/** + * @brief This function is called to get the node (provisioned device) name. + * + * @param[in] index: Index of the node in the node queue. + * + * @note index is obtained from the parameters of ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT. + * + * @return Node name on success, or NULL on failure. + * + */ +const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index); + +/** + * @brief This function is called to get the node (provisioned device) index. + * + * @param[in] name: Name of the node (end by '\0'). + * + * @return Node index on success, or an invalid value (0xFFFF) on failure. + * + */ +uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name); + +/** + * @brief This function is called to store the Composition Data of the node. + * + * @param[in] unicast_addr: Element address of the node + * @param[in] data: Pointer of Composition Data + * @param[in] length: Length of Composition Data + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr, uint8_t *data, uint16_t length); + +/** + * @brief This function is called to get the provisioned node information + * with the node device uuid. + * + * @param[in] uuid: Device UUID of the node + * + * @return Pointer of the node info struct or NULL on failure. + * + */ +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); + +/** + * @brief This function is called to get the provisioned node information + * with the node unicast address. + * + * @param[in] unicast_addr: Unicast address of the node + * + * @return Pointer of the node info struct or NULL on failure. + * + */ +esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); + +/** + * @brief This function is called to delete the provisioned node information + * with the node device uuid. + * + * @param[in] uuid: Device UUID of the node + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16]); + +/** + * @brief This function is called to delete the provisioned node information + * with the node unicast address. + * + * @param[in] unicast_addr: Unicast address of the node + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr); + +/** + * @brief This function is called to add a local AppKey for Provisioner. + * + * @param[in] app_key: The app key to be set for the local BLE Mesh stack. + * @param[in] net_idx: The network key index. + * @param[in] app_idx: The app key index. + * + * @note app_key: If set to NULL, app_key will be generated internally. + * net_idx: Should be an existing one. + * app_idx: If it is going to be generated internally, it should be set to + * 0xFFFF, and the new app_idx will be reported via an event. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16], uint16_t net_idx, uint16_t app_idx); + +/** + * @brief This function is used to update a local AppKey for Provisioner. + * + * @param[in] app_key: Value of the AppKey. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] app_idx: The AppKey Index + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16], + uint16_t net_idx, uint16_t app_idx); + +/** + * @brief This function is called by Provisioner to get the local app key value. + * + * @param[in] net_idx: Network key index. + * @param[in] app_idx: Application key index. + * + * @return App key on success, or NULL on failure. + * + */ +const uint8_t *esp_ble_mesh_provisioner_get_local_app_key(uint16_t net_idx, uint16_t app_idx); + +/** + * @brief This function is called by Provisioner to bind own model with proper app key. + * + * @param[in] element_addr: Provisioner local element address + * @param[in] app_idx: Provisioner local appkey index + * @param[in] model_id: Provisioner local model id + * @param[in] company_id: Provisioner local company id + * + * @note company_id: If going to bind app_key with local vendor model, company_id + * should be set to 0xFFFF. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_addr, uint16_t app_idx, + uint16_t model_id, uint16_t company_id); + +/** + * @brief This function is called by Provisioner to add local network key. + * + * @param[in] net_key: The network key to be added to the Provisioner local BLE Mesh stack. + * @param[in] net_idx: The network key index. + * + * @note net_key: If set to NULL, net_key will be generated internally. + * net_idx: If it is going to be generated internally, it should be set to + * 0xFFFF, and the new net_idx will be reported via an event. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], uint16_t net_idx); + +/** + * @brief This function is called by Provisioner to update a local network key. + * + * @param[in] net_key: Value of the NetKey. + * @param[in] net_idx: The NetKey Index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16], uint16_t net_idx); + +/** + * @brief This function is called by Provisioner to get the local network key value. + * + * @param[in] net_idx: Network key index. + * + * @return Network key on success, or NULL on failure. + * + */ +const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx); + +/** + * @brief This function is called by Provisioner to get provisioned node count. + * + * @return Number of the provisioned nodes. + * + */ +uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void); + +/** + * @brief This function is called to get fast provisioning application key. + * + * @param[in] net_idx: Network key index. + * @param[in] app_idx: Application key index. + * + * @return Application key on success, or NULL on failure. + * + */ +const uint8_t *esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx, uint16_t app_idx); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_NETWORKING_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h new file mode 100644 index 000000000..1df936390 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h @@ -0,0 +1,378 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_PROVISIONING_API_H_ +#define _ESP_BLE_MESH_PROVISIONING_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief: event, event code of provisioning events; param, parameters of provisioning events */ +typedef void (* esp_ble_mesh_prov_cb_t)(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param); + +/** + * @brief Register BLE Mesh provisioning callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_prov_callback(esp_ble_mesh_prov_cb_t callback); + +/** + * @brief Check if a device has been provisioned. + * + * @return TRUE if the device is provisioned, FALSE if the device is unprovisioned. + * + */ +bool esp_ble_mesh_node_is_provisioned(void); + +/** + * @brief Enable specific provisioning bearers to get the device ready for provisioning. + * + * @note PB-ADV: send unprovisioned device beacon. + * PB-GATT: send connectable advertising packets. + * + * @param bearers: Bit-wise OR of provisioning bearers. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers); + +/** + * @brief Disable specific provisioning bearers to make a device inaccessible for provisioning. + * + * @param bearers: Bit-wise OR of provisioning bearers. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers); + +/** + * @brief Unprovisioned device set own oob public key & private key pair. + * + * @param[in] pub_key_x: Unprovisioned device's Public Key X + * @param[in] pub_key_y: Unprovisioned device's Public Key Y + * @param[in] private_key: Unprovisioned device's Private Key + * + * @return ESP_OK on success or error code otherwise. + */ +esp_err_t esp_ble_mesh_node_set_oob_pub_key(uint8_t pub_key_x[32], uint8_t pub_key_y[32], + uint8_t private_key[32]); + +/** + * @brief Provide provisioning input OOB number. + * + * @note This is intended to be called if the user has received ESP_BLE_MESH_NODE_PROV_INPUT_EVT + * with ESP_BLE_MESH_ENTER_NUMBER as the action. + * + * @param[in] number: Number input by device. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_node_input_number(uint32_t number); + +/** + * @brief Provide provisioning input OOB string. + * + * @note This is intended to be called if the user has received ESP_BLE_MESH_NODE_PROV_INPUT_EVT + * with ESP_BLE_MESH_ENTER_STRING as the action. + * + * @param[in] string: String input by device. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_node_input_string(const char *string); + +/** + * @brief Using this function, an unprovisioned device can set its own device name, + * which will be broadcasted in its advertising data. + * + * @param[in] name: Unprovisioned device name + * + * @note This API applicable to PB-GATT mode only by setting the name to the scan response data, + * it doesn't apply to PB-ADV mode. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_set_unprovisioned_device_name(const char *name); + +/** + * @brief Provisioner inputs unprovisioned device's oob public key. + * + * @param[in] link_idx: The provisioning link index + * @param[in] pub_key_x: Unprovisioned device's Public Key X + * @param[in] pub_key_y: Unprovisioned device's Public Key Y + * + * @return ESP_OK on success or error code otherwise. + */ +esp_err_t esp_ble_mesh_provisioner_read_oob_pub_key(uint8_t link_idx, uint8_t pub_key_x[32], + uint8_t pub_key_y[32]); + +/** + * @brief Provide provisioning input OOB string. + * + * This is intended to be called after the esp_ble_mesh_prov_t prov_input_num + * callback has been called with ESP_BLE_MESH_ENTER_STRING as the action. + * + * @param[in] string: String input by Provisioner. + * @param[in] link_idx: The provisioning link index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link_idx); + +/** + * @brief Provide provisioning input OOB number. + * + * This is intended to be called after the esp_ble_mesh_prov_t prov_input_num + * callback has been called with ESP_BLE_MESH_ENTER_NUMBER as the action. + * + * @param[in] number: Number input by Provisioner. + * @param[in] link_idx: The provisioning link index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_input_number(uint32_t number, uint8_t link_idx); + +/** + * @brief Enable one or more provisioning bearers. + * + * @param[in] bearers: Bit-wise OR of provisioning bearers. + * + * @note PB-ADV: Enable BLE scan. + * PB-GATT: Initialize corresponding BLE Mesh Proxy info. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearers); + +/** + * @brief Disable one or more provisioning bearers. + * + * @param[in] bearers: Bit-wise OR of provisioning bearers. + * + * @note PB-ADV: Disable BLE scan. + * PB-GATT: Break any existing BLE Mesh Provisioning connections. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t bearers); + +/** + * @brief Add unprovisioned device info to the unprov_dev queue. + * + * @param[in] add_dev: Pointer to a struct containing the device information + * @param[in] flags: Flags indicate several operations on the device information + * - Remove device information from queue after device has been provisioned (BIT0) + * - Start provisioning immediately after device is added to queue (BIT1) + * - Device can be removed if device queue is full (BIT2) + * + * @return ESP_OK on success or error code otherwise. + * + * @note: 1. Currently address type only supports public address and static random address. + * 2. If device UUID and/or device address as well as address type already exist in the + * device queue, but the bearer is different from the existing one, add operation + * will also be successful and it will update the provision bearer supported by + * the device. + * 3. For example, if the Provisioner wants to add an unprovisioned device info before + * receiving its unprovisioned device beacon or Mesh Provisioning advertising packets, + * the Provisioner can use this API to add the device info with each one or both of + * device UUID and device address added. When the Provisioner gets the device's + * advertising packets, it will start provisioning the device internally. + * - In this situation, the Provisioner can set bearers with each one or both of + * ESP_BLE_MESH_PROV_ADV and ESP_BLE_MESH_PROV_GATT enabled, and cannot set flags + * with ADD_DEV_START_PROV_NOW_FLAG enabled. + * 4. Another example is when the Provisioner receives the unprovisioned device's beacon or + * Mesh Provisioning advertising packets, the advertising packets will be reported on to + * the application layer using the callback registered by the function + * esp_ble_mesh_register_prov_callback. And in the callback, the Provisioner + * can call this API to start provisioning the device. + * - If the Provisioner uses PB-ADV to provision, either one or both of device UUID and + * device address can be added, bearers shall be set with ESP_BLE_MESH_PROV_ADV + * enabled and the flags shall be set with ADD_DEV_START_PROV_NOW_FLAG enabled. + * - If the Provisioner uses PB-GATT to provision, both the device UUID and device + * address need to be added, bearers shall be set with ESP_BLE_MESH_PROV_GATT enabled, + * and the flags shall be set with ADD_DEV_START_PROV_NOW_FLAG enabled. + * - If the Provisioner just wants to store the unprovisioned device info when receiving + * its advertising packets and start to provision it the next time (e.g. after receiving + * its advertising packets again), then it can add the device info with either one or both + * of device UUID and device address included. Bearers can be set with either one or both + * of ESP_BLE_MESH_PROV_ADV and ESP_BLE_MESH_PROV_GATT enabled (recommend to enable the + * bearer which will receive its advertising packets, because if the other bearer is + * enabled, the Provisioner is not aware if the device supports the bearer), and flags + * cannot be set with ADD_DEV_START_PROV_NOW_FLAG enabled. + * - Note: ESP_BLE_MESH_PROV_ADV, ESP_BLE_MESH_PROV_GATT and ADD_DEV_START_PROV_NOW_FLAG + * can not be enabled at the same time. + * + */ +esp_err_t esp_ble_mesh_provisioner_add_unprov_dev(esp_ble_mesh_unprov_dev_add_t *add_dev, + esp_ble_mesh_dev_add_flag_t flags); + +/** @brief Provision an unprovisioned device with fixed unicast address. + * + * @param[in] uuid: Device UUID of the unprovisioned device + * @param[in] addr: Device address of the unprovisioned device + * @param[in] addr_type: Device address type of the unprovisioned device + * @param[in] bearer: Provisioning bearer going to be used by Provisioner + * @param[in] oob_info: OOB info of the unprovisioned device + * @param[in] unicast_addr: Unicast address going to be allocated for the unprovisioned device + * + * @return Zero on success or (negative) error code otherwise. + * + * @note: 1. Currently address type only supports public address and static random address. + * 2. Bearer must be equal to ESP_BLE_MESH_PROV_ADV or ESP_BLE_MESH_PROV_GATT, since + * Provisioner will start to provision a device immediately once this function is + * invked. And the input bearer must be identical with the one within the parameters + * of the ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT event. + * 3. If this function is used by a Provisioner to provision devices, the application + * should take care of the assigned unicast address and avoid overlap of the unicast + * addresses of different nodes. + * 4. Recommend to use only one of the functions "esp_ble_mesh_provisioner_add_unprov_dev" + * and "esp_ble_mesh_provisioner_prov_device_with_addr" by a Provisioner. + */ +esp_err_t esp_ble_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], + esp_ble_mesh_bd_addr_t addr, esp_ble_mesh_addr_type_t addr_type, + esp_ble_mesh_prov_bearer_t bearer, uint16_t oob_info, uint16_t unicast_addr); + +/** + * @brief Delete device from queue, reset current provisioning link and reset the node. + * + * @note If the device is in the queue, remove it from the queue; if the device is being + * provisioned, terminate the provisioning procedure; if the device has already + * been provisioned, reset the device. And either one of the addr or device UUID + * can be input. + * + * @param[in] del_dev: Pointer to a struct containing the device information. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_delete_dev(esp_ble_mesh_device_delete_t *del_dev); + +/** + * @brief Callback for Provisioner that received advertising packets from unprovisioned devices which are + * not in the unprovisioned device queue. + * + * Report on the unprovisioned device beacon and mesh provisioning service adv data to application. + * + * @param[in] addr: Pointer to the unprovisioned device address. + * @param[in] addr_type: Unprovisioned device address type. + * @param[in] adv_type: Adv packet type(ADV_IND or ADV_NONCONN_IND). + * @param[in] dev_uuid: Unprovisioned device UUID pointer. + * @param[in] oob_info: OOB information of the unprovisioned device. + * @param[in] bearer: Adv packet received from PB-GATT or PB-ADV bearer. + * + */ +typedef void (*esp_ble_mesh_prov_adv_cb_t)(const esp_ble_mesh_bd_addr_t addr, const esp_ble_mesh_addr_type_t addr_type, + const uint8_t adv_type, const uint8_t *dev_uuid, + uint16_t oob_info, esp_ble_mesh_prov_bearer_t bearer); + +/** + * @brief This function is called by Provisioner to set the part of the device UUID + * to be compared before starting to provision. + * + * @param[in] match_val: Value to be compared with the part of the device UUID. + * @param[in] match_len: Length of the compared match value. + * @param[in] offset: Offset of the device UUID to be compared (based on zero). + * @param[in] prov_after_match: Flag used to indicate whether provisioner should start to provision + * the device immediately if the part of the UUID matches. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val, uint8_t match_len, + uint8_t offset, bool prov_after_match); + +/** + * @brief This function is called by Provisioner to set provisioning data information + * before starting to provision. + * + * @param[in] prov_data_info: Pointer to a struct containing net_idx or flags or iv_index. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_prov_data_info(esp_ble_mesh_prov_data_info_t *prov_data_info); + +/** + * @brief This function is called by Provisioner to set static oob value used for provisioning. + * + * @param[in] value: Pointer to the static oob value. + * @param[in] length: Length of the static oob value. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length); + +/** + * @brief This function is called by Provisioner to set own Primary element address. + * + * @note This API must be invoked when BLE Mesh initialization is completed successfully, + * and can be invoked before Provisioner functionality is enabled. + * Once this API is invoked successfully, the prov_unicast_addr value in the struct + * esp_ble_mesh_prov_t will be ignored, and Provisioner will use this address as its + * own primary element address. + * And if the unicast address going to assigned for the next unprovisioned device is + * smaller than the input address + element number of Provisioner, then the address + * for the next unprovisioned device will be recalculated internally. + * + * @param[in] addr: Unicast address of the Primary element of Provisioner. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_provisioner_set_primary_elem_addr(uint16_t addr); + +/** + * @brief This function is called to set provisioning data information before starting + * fast provisioning. + * + * @param[in] fast_prov_info: Pointer to a struct containing unicast address range, net_idx, etc. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_prov_info); + +/** + * @brief This function is called to start/suspend/exit fast provisioning. + * + * @param[in] action: fast provisioning action (i.e. enter, suspend, exit). + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_set_fast_prov_action(esp_ble_mesh_fast_prov_action_t action); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_PROVISIONING_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h new file mode 100644 index 000000000..00537558c --- /dev/null +++ b/components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h @@ -0,0 +1,126 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_PROXY_API_H_ +#define _ESP_BLE_MESH_PROXY_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enable advertising with Node Identity. + * + * @note This API requires that GATT Proxy support be enabled. Once called, + * each subnet starts advertising using Node Identity for the next 60 + * seconds, and after 60s Network ID will be advertised. + * Under normal conditions, the BLE Mesh Proxy Node Identity and + * Network ID advertising will be enabled automatically by BLE Mesh + * stack after the device is provisioned. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_identity_enable(void); + +/** + * @brief Enable BLE Mesh GATT Proxy Service. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_gatt_enable(void); + +/** + * @brief Disconnect the BLE Mesh GATT Proxy connection if there is any, and + * disable the BLE Mesh GATT Proxy Service. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_gatt_disable(void); + +/** + * @brief Proxy Client creates a connection with the Proxy Server. + * + * @param[in] addr: Device address of the Proxy Server. + * @param[in] addr_type: Device address type(public or static random). + * @param[in] net_idx: NetKey Index related with Network ID in the Mesh Proxy + * advertising packet. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_connect(esp_ble_mesh_bd_addr_t addr, + esp_ble_mesh_addr_type_t addr_type, uint16_t net_idx); + +/** + * @brief Proxy Client terminates a connection with the Proxy Server. + * + * @param[in] conn_handle: Proxy connection handle. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_disconnect(uint8_t conn_handle); + +/** + * @brief Proxy Client sets the filter type of the Proxy Server. + * + * @param[in] conn_handle: Proxy connection handle. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] filter_type: whitelist or blacklist. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_set_filter_type(uint8_t conn_handle, + uint16_t net_idx, esp_ble_mesh_proxy_filter_type_t filter_type); + +/** + * @brief Proxy Client adds address to the Proxy Server filter list. + * + * @param[in] conn_handle: Proxy connection handle. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] addr: Pointer to the filter address. + * @param[in] addr_num: Number of the filter address. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_add_filter_addr(uint8_t conn_handle, + uint16_t net_idx, uint16_t *addr, uint16_t addr_num); + +/** + * @brief Proxy Client removes address from the Proxy Server filter list. + * + * @param[in] conn_handle: Proxy connection handle. + * @param[in] net_idx: Corresponding NetKey Index. + * @param[in] addr: Pointer to the filter address. + * @param[in] addr_num: Number of the filter address. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_proxy_client_remove_filter_addr(uint8_t conn_handle, + uint16_t net_idx, uint16_t *addr, uint16_t addr_num); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_PROXY_API_H_ */ + diff --git a/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h new file mode 100644 index 000000000..046e1e135 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h @@ -0,0 +1,2126 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_DEFS_H_ +#define _ESP_BLE_MESH_DEFS_H_ + +#include + +#include "mesh_common.h" +#include "proxy_server.h" +#include "provisioner_main.h" + +#ifdef CONFIG_BLUEDROID_ENABLED +#include "esp_bt_defs.h" +#include "esp_bt_main.h" +#define ESP_BLE_HOST_STATUS_ENABLED ESP_BLUEDROID_STATUS_ENABLED +#define ESP_BLE_HOST_STATUS_CHECK(status) ESP_BLUEDROID_STATUS_CHECK(status) +#else +#define ESP_BLE_HOST_STATUS_ENABLED 0 +#define ESP_BLE_HOST_STATUS_CHECK(status) do {} while (0) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/*!< The maximum length of a BLE Mesh message, including Opcode, Payload and TransMIC */ +#define ESP_BLE_MESH_SDU_MAX_LEN 384 + +/*!< Length of a short Mesh MIC. */ +#define ESP_BLE_MESH_MIC_SHORT 4 + +/*!< Length of a long Mesh MIC. */ +#define ESP_BLE_MESH_MIC_LONG 8 + +/*!< The maximum length of a BLE Mesh provisioned node name */ +#define ESP_BLE_MESH_NODE_NAME_MAX_LEN 31 + +/*!< The maximum length of a BLE Mesh unprovisioned device name */ +#define ESP_BLE_MESH_DEVICE_NAME_MAX_LEN DEVICE_NAME_SIZE + +/*!< Define the BLE Mesh octet 16 bytes size */ +#define ESP_BLE_MESH_OCTET16_LEN 16 +typedef uint8_t esp_ble_mesh_octet16_t[ESP_BLE_MESH_OCTET16_LEN]; + +/*!< Define the BLE Mesh octet 8 bytes size */ +#define ESP_BLE_MESH_OCTET8_LEN 8 +typedef uint8_t esp_ble_mesh_octet8_t[ESP_BLE_MESH_OCTET8_LEN]; + +/*!< Invalid Company ID */ +#define ESP_BLE_MESH_CID_NVAL 0xFFFF + +#define ESP_BLE_MESH_ADDR_UNASSIGNED 0x0000 +#define ESP_BLE_MESH_ADDR_ALL_NODES 0xFFFF +#define ESP_BLE_MESH_ADDR_PROXIES 0xFFFC +#define ESP_BLE_MESH_ADDR_FRIENDS 0xFFFD +#define ESP_BLE_MESH_ADDR_RELAYS 0xFFFE + +#define ESP_BLE_MESH_KEY_UNUSED 0xFFFF +#define ESP_BLE_MESH_KEY_DEV 0xFFFE + +#define ESP_BLE_MESH_KEY_PRIMARY 0x0000 +#define ESP_BLE_MESH_KEY_ANY 0xFFFF + +/*!< Primary Network Key index */ +#define ESP_BLE_MESH_NET_PRIMARY 0x000 + +/*!< Relay state value */ +#define ESP_BLE_MESH_RELAY_DISABLED 0x00 +#define ESP_BLE_MESH_RELAY_ENABLED 0x01 +#define ESP_BLE_MESH_RELAY_NOT_SUPPORTED 0x02 + +/*!< Beacon state value */ +#define ESP_BLE_MESH_BEACON_DISABLED 0x00 +#define ESP_BLE_MESH_BEACON_ENABLED 0x01 + +/*!< GATT Proxy state value */ +#define ESP_BLE_MESH_GATT_PROXY_DISABLED 0x00 +#define ESP_BLE_MESH_GATT_PROXY_ENABLED 0x01 +#define ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED 0x02 + +/*!< Friend state value */ +#define ESP_BLE_MESH_FRIEND_DISABLED 0x00 +#define ESP_BLE_MESH_FRIEND_ENABLED 0x01 +#define ESP_BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 + +/*!< Node identity state value */ +#define ESP_BLE_MESH_NODE_IDENTITY_STOPPED 0x00 +#define ESP_BLE_MESH_NODE_IDENTITY_RUNNING 0x01 +#define ESP_BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 + +/*!< Supported features */ +#define ESP_BLE_MESH_FEATURE_RELAY BIT(0) +#define ESP_BLE_MESH_FEATURE_PROXY BIT(1) +#define ESP_BLE_MESH_FEATURE_FRIEND BIT(2) +#define ESP_BLE_MESH_FEATURE_LOW_POWER BIT(3) +#define ESP_BLE_MESH_FEATURE_ALL_SUPPORTED (ESP_BLE_MESH_FEATURE_RELAY | \ + ESP_BLE_MESH_FEATURE_PROXY | \ + ESP_BLE_MESH_FEATURE_FRIEND | \ + ESP_BLE_MESH_FEATURE_LOW_POWER) + +#define ESP_BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) +#define ESP_BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xC000 && (addr) <= 0xFF00) +#define ESP_BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xC000) +#define ESP_BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xFF00 && (addr) <= 0xFFFB) + +#define ESP_BLE_MESH_INVALID_NODE_INDEX 0xFFFF + +/** @def ESP_BLE_MESH_TRANSMIT + * + * @brief Encode transmission count & interval steps. + * + * @note For example, ESP_BLE_MESH_TRANSMIT(2, 20) means that the message + * will be sent about 90ms(count is 3, step is 1, interval is 30 ms + * which includes 10ms of advertising interval random delay). + * + * @param count Number of retransmissions (first transmission is excluded). + * @param int_ms Interval steps in milliseconds. Must be greater than 0 + * and a multiple of 10. + * + * @return BLE Mesh transmit value that can be used e.g. for the default + * values of the Configuration Model data. + */ +#define ESP_BLE_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3)) + +/** @def ESP_BLE_MESH_GET_TRANSMIT_COUNT + * + * @brief Decode transmit count from a transmit value. + * + * @param transmit Encoded transmit count & interval value. + * + * @return Transmission count (actual transmissions equal to N + 1). + */ +#define ESP_BLE_MESH_GET_TRANSMIT_COUNT(transmit) (((transmit) & (uint8_t)BIT_MASK(3))) + +/** @def ESP_BLE_MESH_GET_TRANSMIT_INTERVAL + * + * @brief Decode transmit interval from a transmit value. + * + * @param transmit Encoded transmit count & interval value. + * + * @return Transmission interval in milliseconds. + */ +#define ESP_BLE_MESH_GET_TRANSMIT_INTERVAL(transmit) ((((transmit) >> 3) + 1) * 10) + +/** @def ESP_BLE_MESH_PUBLISH_TRANSMIT + * + * @brief Encode Publish Retransmit count & interval steps. + * + * @param count Number of retransmissions (first transmission is excluded). + * @param int_ms Interval steps in milliseconds. Must be greater than 0 + * and a multiple of 50. + * + * @return BLE Mesh transmit value that can be used e.g. for the default + * values of the Configuration Model data. + */ +#define ESP_BLE_MESH_PUBLISH_TRANSMIT(count, int_ms) ESP_BLE_MESH_TRANSMIT(count, (int_ms) / 5) + +/** @def ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_COUNT + * + * @brief Decode Publish Retransmit count from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Retransmission count (actual transmissions equal to N + 1). + */ +#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_COUNT(transmit) ESP_BLE_MESH_GET_TRANSMIT_COUNT(transmit) + +/** @def ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_INTERVAL + * + * @brief Decode Publish Retransmit interval from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Transmission interval in milliseconds. + */ +#define ESP_BLE_MESH_GET_PUBLISH_TRANSMIT_INTERVAL(transmit) ((((transmit) >> 3) + 1) * 50) + +/*!< Callbacks which are not needed to be initialized by users (set with 0 and will be initialized internally) */ +typedef uint32_t esp_ble_mesh_cb_t; + +typedef enum { + ESP_BLE_MESH_TYPE_PROV_CB, + ESP_BLE_MESH_TYPE_OUTPUT_NUM_CB, + ESP_BLE_MESH_TYPE_OUTPUT_STR_CB, + ESP_BLE_MESH_TYPE_INTPUT_CB, + ESP_BLE_MESH_TYPE_LINK_OPEN_CB, + ESP_BLE_MESH_TYPE_LINK_CLOSE_CB, + ESP_BLE_MESH_TYPE_COMPLETE_CB, + ESP_BLE_MESH_TYPE_RESET_CB, +} esp_ble_mesh_cb_type_t; + +/*!< This enum value is provisioning authentication oob method */ +typedef enum { + ESP_BLE_MESH_NO_OOB, + ESP_BLE_MESH_STATIC_OOB, + ESP_BLE_MESH_OUTPUT_OOB, + ESP_BLE_MESH_INPUT_OOB, +} esp_ble_mesh_oob_method_t; + +/*!< This enum value is associated with bt_mesh_output_action_t in mesh_main.h */ +typedef enum { + ESP_BLE_MESH_NO_OUTPUT = 0, + ESP_BLE_MESH_BLINK = BIT(0), + ESP_BLE_MESH_BEEP = BIT(1), + ESP_BLE_MESH_VIBRATE = BIT(2), + ESP_BLE_MESH_DISPLAY_NUMBER = BIT(3), + ESP_BLE_MESH_DISPLAY_STRING = BIT(4), +} esp_ble_mesh_output_action_t; + +/*!< This enum value is associated with bt_mesh_input_action_t in mesh_main.h */ +typedef enum { + ESP_BLE_MESH_NO_INPUT = 0, + ESP_BLE_MESH_PUSH = BIT(0), + ESP_BLE_MESH_TWIST = BIT(1), + ESP_BLE_MESH_ENTER_NUMBER = BIT(2), + ESP_BLE_MESH_ENTER_STRING = BIT(3), +} esp_ble_mesh_input_action_t; + +/*!< This enum value is associated with bt_mesh_prov_bearer_t in mesh_main.h */ +typedef enum { + ESP_BLE_MESH_PROV_ADV = BIT(0), + ESP_BLE_MESH_PROV_GATT = BIT(1), +} esp_ble_mesh_prov_bearer_t; + +/*!< This enum value is associated with bt_mesh_prov_oob_info_t in mesh_main.h */ +typedef enum { + ESP_BLE_MESH_PROV_OOB_OTHER = BIT(0), + ESP_BLE_MESH_PROV_OOB_URI = BIT(1), + ESP_BLE_MESH_PROV_OOB_2D_CODE = BIT(2), + ESP_BLE_MESH_PROV_OOB_BAR_CODE = BIT(3), + ESP_BLE_MESH_PROV_OOB_NFC = BIT(4), + ESP_BLE_MESH_PROV_OOB_NUMBER = BIT(5), + ESP_BLE_MESH_PROV_OOB_STRING = BIT(6), + /* 7 - 10 are reserved */ + ESP_BLE_MESH_PROV_OOB_ON_BOX = BIT(11), + ESP_BLE_MESH_PROV_OOB_IN_BOX = BIT(12), + ESP_BLE_MESH_PROV_OOB_ON_PAPER = BIT(13), + ESP_BLE_MESH_PROV_OOB_IN_MANUAL = BIT(14), + ESP_BLE_MESH_PROV_OOB_ON_DEV = BIT(15), +} esp_ble_mesh_prov_oob_info_t; + +/*!< Maximum length of value used by Static OOB authentication */ +#define ESP_BLE_MESH_PROV_STATIC_OOB_MAX_LEN 16 + +/*!< Maximum length of string used by Output OOB authentication */ +#define ESP_BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN 8 + +/*!< Maximum length of string used by Output OOB authentication */ +#define ESP_BLE_MESH_PROV_INPUT_OOB_MAX_LEN 8 + +/*!< Macros used to define message opcode */ +#define ESP_BLE_MESH_MODEL_OP_1(b0) (b0) +#define ESP_BLE_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1)) +#define ESP_BLE_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xC00000) | (cid)) + +/*!< This macro is associated with BLE_MESH_MODEL in mesh_access.h */ +#define ESP_BLE_MESH_SIG_MODEL(_id, _op, _pub, _user_data) \ +{ \ + .model_id = (_id), \ + .op = _op, \ + .keys = { [0 ... (CONFIG_BLE_MESH_MODEL_KEY_COUNT - 1)] = \ + ESP_BLE_MESH_KEY_UNUSED }, \ + .pub = _pub, \ + .groups = { [0 ... (CONFIG_BLE_MESH_MODEL_GROUP_COUNT - 1)] = \ + ESP_BLE_MESH_ADDR_UNASSIGNED }, \ + .user_data = _user_data, \ +} + +/*!< This macro is associated with BLE_MESH_MODEL_VND in mesh_access.h */ +#define ESP_BLE_MESH_VENDOR_MODEL(_company, _id, _op, _pub, _user_data) \ +{ \ + .vnd.company_id = (_company), \ + .vnd.model_id = (_id), \ + .op = _op, \ + .pub = _pub, \ + .keys = { [0 ... (CONFIG_BLE_MESH_MODEL_KEY_COUNT - 1)] = \ + ESP_BLE_MESH_KEY_UNUSED }, \ + .groups = { [0 ... (CONFIG_BLE_MESH_MODEL_GROUP_COUNT - 1)] = \ + ESP_BLE_MESH_ADDR_UNASSIGNED }, \ + .user_data = _user_data, \ +} + +/** @brief Helper to define a BLE Mesh element within an array. + * + * In case the element has no SIG or Vendor models, the helper + * macro ESP_BLE_MESH_MODEL_NONE can be given instead. + * + * @note This macro is associated with BLE_MESH_ELEM in mesh_access.h + * + * @param _loc Location Descriptor. + * @param _mods Array of SIG models. + * @param _vnd_mods Array of vendor models. + */ +#define ESP_BLE_MESH_ELEMENT(_loc, _mods, _vnd_mods) \ +{ \ + .location = (_loc), \ + .sig_model_count = ARRAY_SIZE(_mods), \ + .sig_models = (_mods), \ + .vnd_model_count = ARRAY_SIZE(_vnd_mods), \ + .vnd_models = (_vnd_mods), \ +} + +#define ESP_BLE_MESH_PROV(uuid, sta_val, sta_val_len, out_size, out_act, in_size, in_act) { \ + .uuid = uuid, \ + .static_val = sta_val, \ + .static_val_len = sta_val_len, \ + .output_size = out_size, \ + .output_action = out_act, \ + .input_size = in_size, \ + .input_action = in_act, \ +} + +typedef uint8_t UINT8; +typedef uint16_t UINT16; +typedef uint32_t UINT32; +typedef uint64_t UINT64; + +#define BT_OCTET32_LEN 32 +typedef UINT8 BT_OCTET32[BT_OCTET32_LEN]; /* octet array: size 32 */ + + +#ifndef BD_ADDR_LEN +#define BD_ADDR_LEN 6 +typedef uint8_t BD_ADDR[BD_ADDR_LEN]; +#endif + +typedef uint8_t esp_ble_mesh_bd_addr_t[BD_ADDR_LEN]; + +#define ESP_BLE_MESH_ADDR_TYPE_PUBLIC 0x00 +#define ESP_BLE_MESH_ADDR_TYPE_RANDOM 0x01 +#define ESP_BLE_MESH_ADDR_TYPE_RPA_PUBLIC 0x02 +#define ESP_BLE_MESH_ADDR_TYPE_RPA_RANDOM 0x03 +/// BLE device address type +typedef uint8_t esp_ble_mesh_addr_type_t; + +/** BLE Mesh deinit parameters */ +typedef struct { + bool erase_flash; /*!< Indicate if erasing flash when deinit mesh stack */ +} esp_ble_mesh_deinit_param_t; + +typedef struct esp_ble_mesh_model esp_ble_mesh_model_t; + +/** Abstraction that describes a BLE Mesh Element. + * This structure is associated with struct bt_mesh_elem in mesh_access.h + */ +typedef struct { + /** Element Address, assigned during provisioning. */ + uint16_t element_addr; + + /** Location Descriptor (GATT Bluetooth Namespace Descriptors) */ + const uint16_t location; + + const uint8_t sig_model_count; /*!< SIG Model count */ + const uint8_t vnd_model_count; /*!< Vendor Model count */ + + esp_ble_mesh_model_t *sig_models; /*!< SIG Models */ + esp_ble_mesh_model_t *vnd_models; /*!< Vendor Models */ +} esp_ble_mesh_elem_t; + +/** Abstraction that describes a model publication context. + * This structure is associated with struct bt_mesh_model_pub in mesh_access.h + */ +typedef struct { + /** Pointer to the model to which the context belongs. Initialized by the stack. */ + esp_ble_mesh_model_t *model; + + uint16_t publish_addr; /*!< Publish Address. */ + uint16_t app_idx:12, /*!< Publish AppKey Index. */ + cred:1, /*!< Friendship Credentials Flag. */ + send_rel:1; /*!< Force reliable sending (segment acks) */ + + uint8_t ttl; /*!< Publish Time to Live. */ + uint8_t retransmit; /*!< Retransmit Count & Interval Steps. */ + + uint8_t period; /*!< Publish Period. */ + uint8_t period_div:4, /*!< Divisor for the Period. */ + fast_period:1, /*!< Use FastPeriodDivisor */ + count:3; /*!< Retransmissions left. */ + + uint32_t period_start; /*!< Start of the current period. */ + + /** @brief Publication buffer, containing the publication message. + * + * This will get correctly created when the publication context + * has been defined using the ESP_BLE_MESH_MODEL_PUB_DEFINE macro. + * + * ESP_BLE_MESH_MODEL_PUB_DEFINE(name, size); + */ + struct net_buf_simple *msg; + + /** Callback used to update publish message. Initialized by the stack. */ + esp_ble_mesh_cb_t update; + + /** Publish Period Timer. Initialized by the stack. */ + struct k_delayed_work timer; + + /** Role of the device that is going to publish messages */ + uint8_t dev_role; +} esp_ble_mesh_model_pub_t; + +/** @def ESP_BLE_MESH_MODEL_PUB_DEFINE + * + * Define a model publication context. + * + * @param _name Variable name given to the context. + * @param _msg_len Length of the publication message. + * @param _role Role of the device which contains the model. + */ +#define ESP_BLE_MESH_MODEL_PUB_DEFINE(_name, _msg_len, _role) \ + NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \ + static esp_ble_mesh_model_pub_t _name = { \ + .update = (uint32_t)NULL, \ + .msg = &bt_mesh_pub_msg_##_name, \ + .dev_role = _role, \ + } + +/** @def ESP_BLE_MESH_MODEL_OP + * + * Define a model operation context. + * + * @param _opcode Message opcode. + * @param _min_len Message minimum length. + */ +#define ESP_BLE_MESH_MODEL_OP(_opcode, _min_len) \ +{ \ + .opcode = _opcode, \ + .min_len = _min_len, \ + .param_cb = (uint32_t)NULL, \ +} + +/** Abstraction that describes a model operation context. + * This structure is associated with struct bt_mesh_model_op in mesh_access.h + */ +typedef struct { + const uint32_t opcode; /*!< Message opcode */ + const size_t min_len; /*!< Message minimum length */ + esp_ble_mesh_cb_t param_cb; /*!< Callback used to handle message. Initialized by the stack. */ +} esp_ble_mesh_model_op_t; + +/** Define the terminator for the model operation table. + * Each model operation struct array must use this terminator as + * the end tag of the operation unit. + */ +#define ESP_BLE_MESH_MODEL_OP_END {0, 0, 0} + +/** Abstraction that describes a Mesh Model instance. + * This structure is associated with struct bt_mesh_model in mesh_access.h + */ +struct esp_ble_mesh_model { + /** Model ID */ + union { + const uint16_t model_id; + struct { + uint16_t company_id; + uint16_t model_id; + } vnd; + }; + + /** Internal information, mainly for persistent storage */ + uint8_t element_idx; /*!< Belongs to Nth element */ + uint8_t model_idx; /*!< Is the Nth model in the element */ + uint16_t flags; /*!< Information about what has changed */ + + /** The Element to which this Model belongs */ + esp_ble_mesh_elem_t *element; + + /** Model Publication */ + esp_ble_mesh_model_pub_t *const pub; + + /** AppKey List */ + uint16_t keys[CONFIG_BLE_MESH_MODEL_KEY_COUNT]; + + /** Subscription List (group or virtual addresses) */ + uint16_t groups[CONFIG_BLE_MESH_MODEL_GROUP_COUNT]; + + /** Model operation context */ + esp_ble_mesh_model_op_t *op; + + /** Model-specific user data */ + void *user_data; +}; + +/** Helper to define an empty model array. + * This structure is associated with BLE_MESH_MODEL_NONE in mesh_access.h + */ +#define ESP_BLE_MESH_MODEL_NONE ((esp_ble_mesh_model_t []){}) + +/** Message sending context. + * This structure is associated with struct bt_mesh_msg_ctx in mesh_access.h + */ +typedef struct { + /** NetKey Index of the subnet through which to send the message. */ + uint16_t net_idx; + + /** AppKey Index for message encryption. */ + uint16_t app_idx; + + /** Remote address. */ + uint16_t addr; + + /** Destination address of a received message. Not used for sending. */ + uint16_t recv_dst; + + /** RSSI of received packet. Not used for sending. */ + int8_t recv_rssi; + + /** Received TTL value. Not used for sending. */ + uint8_t recv_ttl: 7; + + /** Force sending reliably by using segment acknowledgement */ + uint8_t send_rel: 1; + + /** TTL, or BLE_MESH_TTL_DEFAULT for default TTL. */ + uint8_t send_ttl; + + /** Opcode of a received message. Not used for sending message. */ + uint32_t recv_op; + + /** Model corresponding to the message, no need to be initialized before sending message */ + esp_ble_mesh_model_t *model; + + /** Indicate if the message is sent by a node server model, no need to be initialized before sending message */ + bool srv_send; +} esp_ble_mesh_msg_ctx_t; + +/** Provisioning properties & capabilities. + * This structure is associated with struct bt_mesh_prov in mesh_access.h + */ +typedef struct { +#if CONFIG_BLE_MESH_NODE + /** The UUID that is used when advertising as an unprovisioned device */ + const uint8_t *uuid; + + /** Optional URI. This will be advertised separately from the + * unprovisioned beacon, however the unprovisioned beacon will + * contain a hash of it so the two can be associated by the + * provisioner. + */ + const char *uri; + + /** Out of Band information field. */ + esp_ble_mesh_prov_oob_info_t oob_info; + + /** Flag indicates whether unprovisioned devices support OOB public key */ + bool oob_pub_key; + + /** Callback used to notify to set OOB Public Key. Initialized by the stack. */ + esp_ble_mesh_cb_t oob_pub_key_cb; + + /** Static OOB value */ + const uint8_t *static_val; + /** Static OOB value length */ + uint8_t static_val_len; + + /** Maximum size of Output OOB supported */ + uint8_t output_size; + /** Supported Output OOB Actions */ + uint16_t output_actions; + + /** Maximum size of Input OOB supported */ + uint8_t input_size; + /** Supported Input OOB Actions */ + uint16_t input_actions; + + /** Callback used to output the number. Initialized by the stack. */ + esp_ble_mesh_cb_t output_num_cb; + /** Callback used to output the string. Initialized by the stack. */ + esp_ble_mesh_cb_t output_str_cb; + /** Callback used to notify to input number/string. Initialized by the stack. */ + esp_ble_mesh_cb_t input_cb; + /** Callback used to indicate that link is opened. Initialized by the stack. */ + esp_ble_mesh_cb_t link_open_cb; + /** Callback used to indicate that link is closed. Initialized by the stack. */ + esp_ble_mesh_cb_t link_close_cb; + /** Callback used to indicate that provisioning is completed. Initialized by the stack. */ + esp_ble_mesh_cb_t complete_cb; + /** Callback used to indicate that node has been reset. Initialized by the stack. */ + esp_ble_mesh_cb_t reset_cb; +#endif /* CONFIG_BLE_MESH_NODE */ + +#ifdef CONFIG_BLE_MESH_PROVISIONER + /** Provisioner device UUID */ + const uint8_t *prov_uuid; + + /** Primary element address of the provisioner */ + const uint16_t prov_unicast_addr; + + /** Pre-incremental unicast address value to be assigned to the first device */ + uint16_t prov_start_address; + + /** Attention timer contained in Provisioning Invite PDU */ + uint8_t prov_attention; + + /** Provisioning Algorithm for the Provisioner */ + uint8_t prov_algorithm; + + /** Provisioner public key oob */ + uint8_t prov_pub_key_oob; + + /** Callback used to notify to set device OOB Public Key. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_prov_read_oob_pub_key; + + /** Provisioner static oob value */ + uint8_t *prov_static_oob_val; + /** Provisioner static oob value length */ + uint8_t prov_static_oob_len; + + /** Callback used to notify to input number/string. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_prov_input; + /** Callback used to output number/string. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_prov_output; + + /** Key refresh and IV update flag */ + uint8_t flags; + + /** IV index */ + uint32_t iv_index; + + /** Callback used to indicate that link is opened. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_link_open; + /** Callback used to indicate that link is closed. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_link_close; + /** Callback used to indicate that a device is provisioned. Initialized by the stack. */ + esp_ble_mesh_cb_t provisioner_prov_comp; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +} esp_ble_mesh_prov_t; + +/** Node Composition data context. + * This structure is associated with struct bt_mesh_comp in mesh_access.h + */ +typedef struct { + uint16_t cid; /*!< 16-bit SIG-assigned company identifier */ + uint16_t pid; /*!< 16-bit vendor-assigned product identifier */ + uint16_t vid; /*!< 16-bit vendor-assigned product version identifier */ + + size_t element_count; /*!< Element count */ + esp_ble_mesh_elem_t *elements; /*!< A sequence of elements */ +} esp_ble_mesh_comp_t; + +/*!< This enum value is the role of the device */ +typedef enum { + ROLE_NODE = 0, + ROLE_PROVISIONER, + ROLE_FAST_PROV, +} esp_ble_mesh_dev_role_t; + +/*!< Flag which will be set when device is going to be added. */ +typedef uint8_t esp_ble_mesh_dev_add_flag_t; +#define ADD_DEV_RM_AFTER_PROV_FLAG BIT(0) /*!< Device will be removed from queue after provisioned successfully */ +#define ADD_DEV_START_PROV_NOW_FLAG BIT(1) /*!< Start provisioning device immediately */ +#define ADD_DEV_FLUSHABLE_DEV_FLAG BIT(2) /*!< Device can be remove when queue is full and new device is going to added */ + +/** Information of the device which is going to be added for provisioning. */ +typedef struct { + esp_ble_mesh_bd_addr_t addr; /*!< Device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint8_t uuid[16]; /*!< Device UUID */ + uint16_t oob_info; /*!< Device OOB Info */ + /*!< ADD_DEV_START_PROV_NOW_FLAG shall not be set if the bearer has both PB-ADV and PB-GATT enabled */ + esp_ble_mesh_prov_bearer_t bearer; /*!< Provisioning Bearer */ +} esp_ble_mesh_unprov_dev_add_t; + +#define DEL_DEV_ADDR_FLAG BIT(0) +#define DEL_DEV_UUID_FLAG BIT(1) +/** Information of the device which is going to be deleted. */ +typedef struct { + union { + struct { + esp_ble_mesh_bd_addr_t addr; /*!< Device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + }; + uint8_t uuid[16]; /*!< Device UUID */ + }; + uint8_t flag; /*!< BIT0: device address; BIT1: device UUID */ +} esp_ble_mesh_device_delete_t; + +#define PROV_DATA_NET_IDX_FLAG BIT(0) +#define PROV_DATA_FLAGS_FLAG BIT(1) +#define PROV_DATA_IV_INDEX_FLAG BIT(2) +/** Information of the provisioner which is going to be updated. */ +typedef struct { + union { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t flags; /*!< Flags */ + uint32_t iv_index; /*!< IV Index */ + }; + uint8_t flag; /*!< BIT0: net_idx; BIT1: flags; BIT2: iv_index */ +} esp_ble_mesh_prov_data_info_t; + +/** Information of the provisioned node */ +typedef struct { + /* Device information */ + esp_ble_mesh_bd_addr_t addr; /*!< Node device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Node device address type */ + uint8_t dev_uuid[16]; /*!< Device UUID */ + uint16_t oob_info; /*!< Node OOB information */ + + /* Provisioning information */ + uint16_t unicast_addr; /*!< Node unicast address */ + uint8_t element_num; /*!< Node element number */ + uint16_t net_idx; /*!< Node NetKey Index */ + uint8_t flags; /*!< Node key refresh flag and iv update flag */ + uint32_t iv_index; /*!< Node IV Index */ + uint8_t dev_key[16]; /*!< Node device key */ + + /* Additional information */ + char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN + 1]; /*!< Node name */ + uint16_t comp_length; /*!< Length of Composition Data */ + uint8_t *comp_data; /*!< Value of Composition Data */ +} __attribute__((packed)) esp_ble_mesh_node_t; + +/** Context of fast provisioning which need to be set. */ +typedef struct { + uint16_t unicast_min; /*!< Minimum unicast address used for fast provisioning */ + uint16_t unicast_max; /*!< Maximum unicast address used for fast provisioning */ + uint16_t net_idx; /*!< Netkey index used for fast provisioning */ + uint8_t flags; /*!< Flags used for fast provisioning */ + uint32_t iv_index; /*!< IV Index used for fast provisioning */ + uint8_t offset; /*!< Offset of the UUID to be compared */ + uint8_t match_len; /*!< Length of the UUID to be compared */ + uint8_t match_val[16]; /*!< Value of UUID to be compared */ +} esp_ble_mesh_fast_prov_info_t; + +/*!< This enum value is the action of fast provisioning */ +typedef enum { + FAST_PROV_ACT_NONE, + FAST_PROV_ACT_ENTER, + FAST_PROV_ACT_SUSPEND, + FAST_PROV_ACT_EXIT, + FAST_PROV_ACT_MAX, +} esp_ble_mesh_fast_prov_action_t; + +/*!< This enum value is the type of proxy filter */ +typedef enum { + PROXY_FILTER_WHITELIST, + PROXY_FILTER_BLACKLIST, +} esp_ble_mesh_proxy_filter_type_t; + +/** Count for sending BLE advertising packet infinitely */ +#define ESP_BLE_MESH_BLE_ADV_INFINITE 0xFFFF + +/*!< This enum value is the priority of BLE advertising packet */ +typedef enum { + ESP_BLE_MESH_BLE_ADV_PRIO_LOW, + ESP_BLE_MESH_BLE_ADV_PRIO_HIGH, +} esp_ble_mesh_ble_adv_priority_t; + +/** Context of BLE advertising parameters. */ +typedef struct { + uint16_t interval; /*!< BLE advertising interval */ + uint8_t adv_type; /*!< BLE advertising type */ + uint8_t own_addr_type; /*!< Own address type */ + uint8_t peer_addr_type; /*!< Peer address type */ + uint8_t peer_addr[BD_ADDR_LEN]; /*!< Peer address */ + uint16_t duration; /*!< Duration is milliseconds */ + uint16_t period; /*!< Period in milliseconds */ + uint16_t count; /*!< Number of advertising duration */ + uint8_t priority:2; /*!< Priority of BLE advertising packet */ +} esp_ble_mesh_ble_adv_param_t; + +/** Context of BLE advertising data. */ +typedef struct { + uint8_t adv_data_len; /*!< Advertising data length */ + uint8_t adv_data[31]; /*!< Advertising data */ + uint8_t scan_rsp_data_len; /*!< Scan response data length */ + uint8_t scan_rsp_data[31]; /*!< Scan response data */ +} esp_ble_mesh_ble_adv_data_t; + +/*!< This enum value is the event of node/provisioner/fast provisioning */ +typedef enum { + ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, /*!< Initialize BLE Mesh provisioning capabilities and internal data information completion event */ + ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT, /*!< Set the unprovisioned device name completion event */ + ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, /*!< Enable node provisioning functionality completion event */ + ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT, /*!< Disable node provisioning functionality completion event */ + ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, /*!< Establish a BLE Mesh link event */ + ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, /*!< Close a BLE Mesh link event */ + ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT, /*!< Generate Node input OOB public key event */ + ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT, /*!< Generate Node Output Number event */ + ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT, /*!< Generate Node Output String event */ + ESP_BLE_MESH_NODE_PROV_INPUT_EVT, /*!< Event requiring the user to input a number or string */ + ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT, /*!< Provisioning done event */ + ESP_BLE_MESH_NODE_PROV_RESET_EVT, /*!< Provisioning reset event */ + ESP_BLE_MESH_NODE_PROV_SET_OOB_PUB_KEY_COMP_EVT, /*!< Node set oob public key completion event */ + ESP_BLE_MESH_NODE_PROV_INPUT_NUMBER_COMP_EVT, /*!< Node input number completion event */ + ESP_BLE_MESH_NODE_PROV_INPUT_STRING_COMP_EVT, /*!< Node input string completion event */ + ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT, /*!< Enable BLE Mesh Proxy Identity advertising completion event */ + ESP_BLE_MESH_NODE_PROXY_GATT_ENABLE_COMP_EVT, /*!< Enable BLE Mesh GATT Proxy Service completion event */ + ESP_BLE_MESH_NODE_PROXY_GATT_DISABLE_COMP_EVT, /*!< Disable BLE Mesh GATT Proxy Service completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT, /*!< Provisioner enable provisioning functionality completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT, /*!< Provisioner disable provisioning functionality completion event */ + ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT, /*!< Provisioner receives unprovisioned device beacon event */ + ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT, /*!< Provisioner read unprovisioned device OOB public key event */ + ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT, /*!< Provisioner input value for provisioning procedure event */ + ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT, /*!< Provisioner output value for provisioning procedure event */ + ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT, /*!< Provisioner establish a BLE Mesh link event */ + ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT, /*!< Provisioner close a BLE Mesh link event */ + ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT, /*!< Provisioner provisioning done event */ + ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT, /*!< Provisioner add a device to the list which contains devices that are waiting/going to be provisioned completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT, /*!< Provisioner start to provision an unprovisioned device completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT, /*!< Provisioner delete a device from the list, close provisioning link with the device if it exists and remove the device from network completion event */ + ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT, /*!< Provisioner set the value to be compared with part of the unprovisioned device UUID completion event */ + ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT, /*!< Provisioner set net_idx/flags/iv_index used for provisioning completion event */ + ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT, /*!< Provisioner set static oob value used for provisioning completion event */ + ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT, /*!< Provisioner set unicast address of primary element completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT, /*!< Provisioner read unprovisioned device OOB public key completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT, /*!< Provisioner input number completion event */ + ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT, /*!< Provisioner input string completion event */ + ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT, /*!< Provisioner set node name completion event */ + ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, /*!< Provisioner add local app key completion event */ + ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT, /*!< Provisioner update local app key completion event */ + ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, /*!< Provisioner bind local model with local app key completion event */ + ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT, /*!< Provisioner add local network key completion event */ + ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT, /*!< Provisioner update local network key completion event */ + ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT, /*!< Provisioner store node composition data completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT, /*!< Provisioner delete node with uuid completion event */ + ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT, /*!< Provisioner delete node with unicast address completion event */ + ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT, /*!< Set fast provisioning information (e.g. unicast address range, net_idx, etc.) completion event */ + ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT, /*!< Set fast provisioning action completion event */ + ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT, /*!< Receive Heartbeat message event */ + ESP_BLE_MESH_LPN_ENABLE_COMP_EVT, /*!< Enable Low Power Node completion event */ + ESP_BLE_MESH_LPN_DISABLE_COMP_EVT, /*!< Disable Low Power Node completion event */ + ESP_BLE_MESH_LPN_POLL_COMP_EVT, /*!< Low Power Node send Friend Poll completion event */ + ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT, /*!< Low Power Node establishes friendship event */ + ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT, /*!< Low Power Node terminates friendship event */ + ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT, /*!< Friend Node establishes friendship event */ + ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT, /*!< Friend Node terminates friendship event */ + ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT, /*!< Proxy Client receives Network ID advertising packet event */ + ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT, /*!< Proxy Client establishes connection successfully event */ + ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT, /*!< Proxy Client terminates connection successfully event */ + ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT, /*!< Proxy Client receives Proxy Filter Status event */ + ESP_BLE_MESH_PROXY_CLIENT_CONNECT_COMP_EVT, /*!< Proxy Client connect completion event */ + ESP_BLE_MESH_PROXY_CLIENT_DISCONNECT_COMP_EVT, /*!< Proxy Client disconnect completion event */ + ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT, /*!< Proxy Client set filter type completion event */ + ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT, /*!< Proxy Client add filter address completion event */ + ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT, /*!< Proxy Client remove filter address completion event */ + ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT, /*!< Start BLE advertising completion event */ + ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT, /*!< Stop BLE advertising completion event */ + ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model subscribes group address completion event */ + ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT, /*!< Local model unsubscribes group address completion event */ + ESP_BLE_MESH_DEINIT_MESH_COMP_EVT, /*!< De-initialize BLE Mesh stack completion event */ + ESP_BLE_MESH_PROV_EVT_MAX, +} esp_ble_mesh_prov_cb_event_t; + +/** + * @brief BLE Mesh Node/Provisioner callback parameters union + */ +typedef union { + /** + * @brief ESP_BLE_MESH_PROV_REGISTER_COMP_EVT + */ + struct ble_mesh_prov_register_comp_param { + int err_code; /*!< Indicate the result of BLE Mesh initialization */ + } prov_register_comp; /*!< Event parameter of ESP_BLE_MESH_PROV_REGISTER_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT + */ + struct ble_mesh_set_unprov_dev_name_comp_param { + int err_code; /*!< Indicate the result of setting BLE Mesh device name */ + } node_set_unprov_dev_name_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT + */ + struct ble_mesh_prov_enable_comp_param { + int err_code; /*!< Indicate the result of enabling BLE Mesh device */ + } node_prov_enable_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT + */ + struct ble_mesh_prov_disable_comp_param { + int err_code; /*!< Indicate the result of disabling BLE Mesh device */ + } node_prov_disable_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT + */ + struct ble_mesh_link_open_evt_param { + esp_ble_mesh_prov_bearer_t bearer; /*!< Type of the bearer used when device link is open */ + } node_prov_link_open; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT + */ + struct ble_mesh_link_close_evt_param { + esp_ble_mesh_prov_bearer_t bearer; /*!< Type of the bearer used when device link is closed */ + } node_prov_link_close; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT + */ + struct ble_mesh_output_num_evt_param { + esp_ble_mesh_output_action_t action; /*!< Action of Output OOB Authentication */ + uint32_t number; /*!< Number of Output OOB Authentication */ + } node_prov_output_num; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT + */ + struct ble_mesh_output_str_evt_param { + char string[8]; /*!< String of Output OOB Authentication */ + } node_prov_output_str; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_INPUT_EVT + */ + struct ble_mesh_input_evt_param { + esp_ble_mesh_input_action_t action; /*!< Action of Input OOB Authentication */ + uint8_t size; /*!< Size of Input OOB Authentication */ + } node_prov_input; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_INPUT_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT + */ + struct ble_mesh_provision_complete_evt_param { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t net_key[16]; /*!< NetKey */ + uint16_t addr; /*!< Primary address */ + uint8_t flags; /*!< Flags */ + uint32_t iv_index; /*!< IV Index */ + } node_prov_complete; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_RESET_EVT + */ + struct ble_mesh_provision_reset_param { + + } node_prov_reset; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_RESET_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_SET_OOB_PUB_KEY_COMP_EVT + */ + struct ble_mesh_set_oob_pub_key_comp_param { + int err_code; /*!< Indicate the result of setting OOB Public Key */ + } node_prov_set_oob_pub_key_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_SET_OOB_PUB_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_INPUT_NUM_COMP_EVT + */ + struct ble_mesh_input_number_comp_param { + int err_code; /*!< Indicate the result of inputting number */ + } node_prov_input_num_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_INPUT_NUM_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROV_INPUT_STR_COMP_EVT + */ + struct ble_mesh_input_string_comp_param { + int err_code; /*!< Indicate the result of inputting string */ + } node_prov_input_str_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROV_INPUT_STR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT + */ + struct ble_mesh_proxy_identity_enable_comp_param { + int err_code; /*!< Indicate the result of enabling Mesh Proxy advertising */ + } node_proxy_identity_enable_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROXY_GATT_ENABLE_COMP_EVT + */ + struct ble_mesh_proxy_gatt_enable_comp_param { + int err_code; /*!< Indicate the result of enabling Mesh Proxy Service */ + } node_proxy_gatt_enable_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROXY_GATT_ENABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_NODE_PROXY_GATT_DISABLE_COMP_EVT + */ + struct ble_mesh_proxy_gatt_disable_comp_param { + int err_code; /*!< Indicate the result of disabling Mesh Proxy Service */ + } node_proxy_gatt_disable_comp; /*!< Event parameter of ESP_BLE_MESH_NODE_PROXY_GATT_DISABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT + */ + struct ble_mesh_provisioner_recv_unprov_adv_pkt_param { + uint8_t dev_uuid[16]; /*!< Device UUID of the unprovisioned device */ + esp_ble_mesh_bd_addr_t addr; /*!< Device address of the unprovisioned device */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint16_t oob_info; /*!< OOB Info of the unprovisioned device */ + uint8_t adv_type; /*!< Avertising type of the unprovisioned device */ + esp_ble_mesh_prov_bearer_t bearer; /*!< Bearer of the unprovisioned device */ + int8_t rssi; /*!< RSSI of the received advertising packet */ + } provisioner_recv_unprov_adv_pkt; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT + */ + struct ble_mesh_provisioner_prov_enable_comp_param { + int err_code; /*!< Indicate the result of enabling BLE Mesh Provisioner */ + } provisioner_prov_enable_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT + */ + struct ble_mesh_provisioner_prov_disable_comp_param { + int err_code; /*!< Indicate the result of disabling BLE Mesh Provisioner */ + } provisioner_prov_disable_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT + */ + struct ble_mesh_provisioner_link_open_evt_param { + esp_ble_mesh_prov_bearer_t bearer; /*!< Type of the bearer used when Provisioner link is opened */ + } provisioner_prov_link_open; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT + */ + struct ble_mesh_provisioner_prov_read_oob_pub_key_evt_param { + uint8_t link_idx; /*!< Index of the provisioning link */ + } provisioner_prov_read_oob_pub_key; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT + */ + struct ble_mesh_provisioner_prov_input_evt_param { + esp_ble_mesh_oob_method_t method; /*!< Method of device Output OOB Authentication */ + esp_ble_mesh_output_action_t action; /*!< Action of device Output OOB Authentication */ + uint8_t size; /*!< Size of device Output OOB Authentication */ + uint8_t link_idx; /*!< Index of the provisioning link */ + } provisioner_prov_input; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT + */ + struct ble_mesh_provisioner_prov_output_evt_param { + esp_ble_mesh_oob_method_t method; /*!< Method of device Input OOB Authentication */ + esp_ble_mesh_input_action_t action; /*!< Action of device Input OOB Authentication */ + uint8_t size; /*!< Size of device Input OOB Authentication */ + uint8_t link_idx; /*!< Index of the provisioning link */ + union { + char string[8]; /*!< String output by the Provisioner */ + uint32_t number; /*!< Number output by the Provisioner */ + }; + } provisioner_prov_output; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT + */ + struct ble_mesh_provisioner_link_close_evt_param { + esp_ble_mesh_prov_bearer_t bearer; /*!< Type of the bearer used when Provisioner link is closed */ + uint8_t reason; /*!< Reason of the closed provisioning link */ + } provisioner_prov_link_close; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT + */ + struct ble_mesh_provisioner_prov_comp_param { + uint16_t node_idx; /*!< Index of the provisioned device */ + esp_ble_mesh_octet16_t device_uuid; /*!< Device UUID of the provisioned device */ + uint16_t unicast_addr; /*!< Primary address of the provisioned device */ + uint8_t element_num; /*!< Element count of the provisioned device */ + uint16_t netkey_idx; /*!< NetKey Index of the provisioned device */ + } provisioner_prov_complete; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT + */ + struct ble_mesh_provisioner_add_unprov_dev_comp_param { + int err_code; /*!< Indicate the result of adding device into queue by the Provisioner */ + } provisioner_add_unprov_dev_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_prov_dev_with_addr_comp_param { + int err_code; /*!< Indicate the result of Provisioner starting to provision a device */ + } provisioner_prov_dev_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT + */ + struct ble_mesh_provisioner_delete_dev_comp_param { + int err_code; /*!< Indicate the result of deleting device by the Provisioner */ + } provisioner_delete_dev_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT + */ + struct ble_mesh_provisioner_set_dev_uuid_match_comp_param { + int err_code; /*!< Indicate the result of setting Device UUID match value by the Provisioner */ + } provisioner_set_dev_uuid_match_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT + */ + struct ble_mesh_provisioner_set_prov_data_info_comp_param { + int err_code; /*!< Indicate the result of setting provisioning info by the Provisioner */ + } provisioner_set_prov_data_info_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT + */ + struct ble_mesh_provisioner_set_static_oob_val_comp_param { + int err_code; /*!< Indicate the result of setting static oob value by the Provisioner */ + } provisioner_set_static_oob_val_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_set_primary_elem_addr_comp_param { + int err_code; /*!< Indicate the result of setting unicast address of primary element by the Provisioner */ + } provisioner_set_primary_elem_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_prov_read_oob_pub_key_comp_param { + int err_code; /*!< Indicate the result of setting OOB Public Key by the Provisioner */ + } provisioner_prov_read_oob_pub_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT + */ + struct ble_mesh_provisioner_prov_input_num_comp_param { + int err_code; /*!< Indicate the result of inputting number by the Provisioner */ + } provisioner_prov_input_num_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT + */ + struct ble_mesh_provisioner_prov_input_str_comp_param { + int err_code; /*!< Indicate the result of inputting string by the Provisioner */ + } provisioner_prov_input_str_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT + */ + struct ble_mesh_provisioner_set_node_name_comp_param { + int err_code; /*!< Indicate the result of setting provisioned device name by the Provisioner */ + uint16_t node_index; /*!< Index of the provisioned device */ + } provisioner_set_node_name_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_add_local_app_key_comp_param { + int err_code; /*!< Indicate the result of adding local AppKey by the Provisioner */ + uint16_t app_idx; /*!< AppKey Index */ + } provisioner_add_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_update_local_app_key_comp_param { + int err_code; /*!< Indicate the result of updating local AppKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ + uint16_t app_idx; /*!< AppKey Index */ + } provisioner_update_app_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT + */ + struct ble_mesh_provisioner_bind_local_mod_app_comp_param { + int err_code; /*!< Indicate the result of binding AppKey with model by the Provisioner */ + uint16_t element_addr; /*!< Element address */ + uint16_t app_idx; /*!< AppKey Index */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + } provisioner_bind_app_key_to_model_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_add_local_net_key_comp_param { + int err_code; /*!< Indicate the result of adding local NetKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ + } provisioner_add_net_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT + */ + struct ble_mesh_provisioner_update_local_net_key_comp_param { + int err_code; /*!< Indicate the result of updating local NetKey by the Provisioner */ + uint16_t net_idx; /*!< NetKey Index */ + } provisioner_update_net_key_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT + */ + struct ble_mesh_provisioner_store_node_comp_data_comp_param { + int err_code; /*!< Indicate the result of storing node composition data by the Provisioner */ + uint16_t addr; /*!< Node element address */ + } provisioner_store_node_comp_data_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_uuid_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with uuid by the Provisioner */ + uint8_t uuid[16]; /*!< Node device uuid */ + } provisioner_delete_node_with_uuid_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT + */ + struct ble_mesh_provisioner_delete_node_with_addr_comp_data_comp_param { + int err_code; /*!< Indicate the result of deleting node with unicast address by the Provisioner */ + uint16_t unicast_addr; /*!< Node unicast address */ + } provisioner_delete_node_with_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT + */ + struct ble_mesh_set_fast_prov_info_comp_param { + uint8_t status_unicast; /*!< Indicate the result of setting unicast address range of fast provisioning */ + uint8_t status_net_idx; /*!< Indicate the result of setting NetKey Index of fast provisioning */ + uint8_t status_match; /*!< Indicate the result of setting matching Device UUID of fast provisioning */ + } set_fast_prov_info_comp; /*!< Event parameter of ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT + */ + struct ble_mesh_set_fast_prov_action_comp_param { + uint8_t status_action; /*!< Indicate the result of setting action of fast provisioning */ + } set_fast_prov_action_comp; /*!< Event parameter of ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT + */ + struct ble_mesh_heartbeat_msg_recv_param { + uint8_t hops; /*!< Heartbeat hops (InitTTL - RxTTL + 1) */ + uint16_t feature; /*!< Bit field of currently active features of the node */ + } heartbeat_msg_recv; /*!< Event parameter of ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT */ + /** + * @brief ESP_BLE_MESH_LPN_ENABLE_COMP_EVT + */ + struct ble_mesh_lpn_enable_comp_param { + int err_code; /*!< Indicate the result of enabling LPN functionality */ + } lpn_enable_comp; /*!< Event parameter of ESP_BLE_MESH_LPN_ENABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_LPN_DISABLE_COMP_EVT + */ + struct ble_mesh_lpn_disable_comp_param { + int err_code; /*!< Indicate the result of disabling LPN functionality */ + } lpn_disable_comp; /*!< Event parameter of ESP_BLE_MESH_LPN_DISABLE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_LPN_POLL_COMP_EVT + */ + struct ble_mesh_lpn_poll_comp_param { + int err_code; /*!< Indicate the result of sending Friend Poll */ + } lpn_poll_comp; /*!< Event parameter of ESP_BLE_MESH_LPN_POLL_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT + */ + struct ble_mesh_lpn_friendship_establish_param { + uint16_t friend_addr; /*!< Friend Node unicast address */ + } lpn_friendship_establish; /*!< Event parameter of ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT */ + /** + * @brief ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT + */ + struct ble_mesh_lpn_friendship_terminate_param { + uint16_t friend_addr; /*!< Friend Node unicast address */ + } lpn_friendship_terminate; /*!< Event parameter of ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT */ + /** + * @brief ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT + */ + struct ble_mesh_friend_friendship_establish_param { + uint16_t lpn_addr; /*!< Low Power Node unicast address */ + } frnd_friendship_establish; /*!< Event parameter of ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT */ + /** + * @brief ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT + */ + struct ble_mesh_friend_friendship_terminate_param { + uint16_t lpn_addr; /*!< Low Power Node unicast address */ + /** This enum value is the reason of friendship termination on the friend node side */ + enum { + ESP_BLE_MESH_FRND_FRIENDSHIP_TERMINATE_ESTABLISH_FAIL, /*!< Friend Offer has been sent, but Friend Offer is not received within 1 second, friendship fails to be established */ + ESP_BLE_MESH_FRND_FRIENDSHIP_TERMINATE_POLL_TIMEOUT, /*!< Friendship is established, PollTimeout timer expires and no Friend Poll/Sub Add/Sub Remove is received */ + ESP_BLE_MESH_FRND_FRIENDSHIP_TERMINATE_RECV_FRND_REQ, /*!< Receive Friend Request from existing Low Power Node */ + ESP_BLE_MESH_FRND_FRIENDSHIP_TERMINATE_RECV_FRND_CLEAR, /*!< Receive Friend Clear from other friend node */ + ESP_BLE_MESH_FRND_FRIENDSHIP_TERMINATE_DISABLE, /*!< Friend feature disabled or corresponding NetKey is deleted */ + } reason; /*!< Friendship terminated reason */ + } frnd_friendship_terminate; /*!< Event parameter of ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT + */ + struct ble_mesh_proxy_client_recv_adv_pkt_param { + esp_ble_mesh_bd_addr_t addr; /*!< Device address */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint16_t net_idx; /*!< Network ID related NetKey Index */ + uint8_t net_id[8]; /*!< Network ID contained in the advertising packet */ + int8_t rssi; /*!< RSSI of the received advertising packet */ + } proxy_client_recv_adv_pkt; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT + */ + struct ble_mesh_proxy_client_connected_param { + esp_ble_mesh_bd_addr_t addr; /*!< Device address of the Proxy Server */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_connected; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT + */ + struct ble_mesh_proxy_client_disconnected_param { + esp_ble_mesh_bd_addr_t addr; /*!< Device address of the Proxy Server */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + uint8_t reason; /*!< Proxy disconnect reason */ + } proxy_client_disconnected; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT + */ + struct ble_mesh_proxy_client_recv_filter_status_param { + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t server_addr; /*!< Proxy Server primary element address */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + uint8_t filter_type; /*!< Proxy Server filter type(whitelist or blacklist) */ + uint16_t list_size; /*!< Number of addresses in the Proxy Server filter list */ + } proxy_client_recv_filter_status; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_CONNECT_COMP_EVT + */ + struct ble_mesh_proxy_client_connect_comp_param { + int err_code; /*!< Indicate the result of Proxy Client connect */ + esp_ble_mesh_bd_addr_t addr; /*!< Device address of the Proxy Server */ + esp_ble_mesh_addr_type_t addr_type; /*!< Device address type */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_connect_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_CONNECT_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_DISCONNECT_COMP_EVT + */ + struct ble_mesh_proxy_client_disconnect_comp_param { + int err_code; /*!< Indicate the result of Proxy Client disconnect */ + uint8_t conn_handle; /*!< Proxy connection handle */ + } proxy_client_disconnect_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_DISCONNECT_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT + */ + struct ble_mesh_proxy_client_set_filter_type_comp_param { + int err_code; /*!< Indicate the result of Proxy Client set filter type */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_set_filter_type_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT + */ + struct ble_mesh_proxy_client_add_filter_addr_comp_param { + int err_code; /*!< Indicate the result of Proxy Client add filter address */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_add_filter_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT + */ + struct ble_mesh_proxy_client_remove_filter_addr_comp_param { + int err_code; /*!< Indicate the result of Proxy Client remove filter address */ + uint8_t conn_handle; /*!< Proxy connection handle */ + uint16_t net_idx; /*!< Corresponding NetKey Index */ + } proxy_client_remove_filter_addr_comp; /*!< Event parameter of ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT + */ + struct ble_mesh_start_ble_advertising_comp_param { + int err_code; /*!< Indicate the result of starting BLE advertising */ + uint8_t index; /*!< Index of the BLE advertising */ + } start_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT + */ + struct ble_mesh_stop_ble_advertising_comp_param { + int err_code; /*!< Indicate the result of stopping BLE advertising */ + uint8_t index; /*!< Index of the BLE advertising */ + } stop_ble_advertising_comp; /*!< Event parameter of ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT + */ + struct ble_mesh_model_sub_group_addr_comp_param { + int err_code; /*!< Indicate the result of local model subscribing group address */ + uint16_t element_addr; /*!< Element address */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + uint16_t group_addr; /*!< Group Address */ + } model_sub_group_addr_comp; /*!< Event parameters of ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT + */ + struct ble_mesh_model_unsub_group_addr_comp_param { + int err_code; /*!< Indicate the result of local model unsubscribing group address */ + uint16_t element_addr; /*!< Element address */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + uint16_t group_addr; /*!< Group Address */ + } model_unsub_group_addr_comp; /*!< Event parameters of ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_DEINIT_MESH_COMP_EVT + */ + struct ble_mesh_deinit_mesh_comp_param { + int err_code; /*!< Indicate the result of BLE Mesh deinitialization */ + } deinit_mesh_comp; /*!< Event parameter of ESP_BLE_MESH_DEINIT_MESH_COMP_EVT */ +} esp_ble_mesh_prov_cb_param_t; + +/** + * @brief BLE Mesh models related Model ID and Opcode definitions + */ + +/*!< Foundation Models */ +#define ESP_BLE_MESH_MODEL_ID_CONFIG_SRV 0x0000 +#define ESP_BLE_MESH_MODEL_ID_CONFIG_CLI 0x0001 +#define ESP_BLE_MESH_MODEL_ID_HEALTH_SRV 0x0002 +#define ESP_BLE_MESH_MODEL_ID_HEALTH_CLI 0x0003 + +/*!< Models from the Mesh Model Specification */ +#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 +#define ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001 +#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002 +#define ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003 +#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004 +#define ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009 +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a +#define ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b +#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c +#define ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV 0x100f +#define ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010 +#define ESP_BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011 +#define ESP_BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012 +#define ESP_BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013 +#define ESP_BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014 +#define ESP_BLE_MESH_MODEL_ID_GEN_PROP_CLI 0x1015 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_SRV 0x1100 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101 +#define ESP_BLE_MESH_MODEL_ID_SENSOR_CLI 0x1102 +#define ESP_BLE_MESH_MODEL_ID_TIME_SRV 0x1200 +#define ESP_BLE_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201 +#define ESP_BLE_MESH_MODEL_ID_TIME_CLI 0x1202 +#define ESP_BLE_MESH_MODEL_ID_SCENE_SRV 0x1203 +#define ESP_BLE_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204 +#define ESP_BLE_MESH_MODEL_ID_SCENE_CLI 0x1205 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SRV 0x1206 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207 +#define ESP_BLE_MESH_MODEL_ID_SCHEDULER_CLI 0x1208 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a +#define ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d +#define ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV 0x1310 +#define ESP_BLE_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 + +/** + * esp_ble_mesh_opcode_config_client_get_t belongs to esp_ble_mesh_opcode_t, this typedef is only + * used to locate the opcodes used by esp_ble_mesh_config_client_get_state. + * The following opcodes will only be used in the esp_ble_mesh_config_client_get_state function. + */ +typedef uint32_t esp_ble_mesh_opcode_config_client_get_t; + +#define ESP_BLE_MESH_MODEL_OP_BEACON_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x09) /*!< Config Beacon Get */ +#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x08) /*!< Config Composition Data Get */ +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0C) /*!< Config Default TTL Get */ +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x12) /*!< Config GATT Proxy Get */ +#define ESP_BLE_MESH_MODEL_OP_RELAY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x26) /*!< Config Relay Get */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x18) /*!< Config Model Publication Get */ +#define ESP_BLE_MESH_MODEL_OP_FRIEND_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0F) /*!< Config Friend Get */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x38) /*!< Config Heartbeat Publication Get */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3a) /*!< Config Heartbeat Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x42) /*!< Config NetKey Get */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x01) /*!< Config AppKey Get */ +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x46) /*!< Config Node Identity Get */ +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x29) /*!< Config SIG Model Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2B) /*!< Config Vendor Model Subscription Get */ +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4B) /*!< Config SIG Model App Get */ +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4D) /*!< Config Vendor Model App Get */ +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x15) /*!< Config Key Refresh Phase Get */ +#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2D) /*!< Config Low Power Node PollTimeout Get */ +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x23) /*!< Config Network Transmit Get */ + +/** + * esp_ble_mesh_opcode_config_client_set_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by esp_ble_mesh_config_client_set_state. + * The following opcodes will only be used in the esp_ble_mesh_config_client_set_state function. + */ +typedef uint32_t esp_ble_mesh_opcode_config_client_set_t; + +#define ESP_BLE_MESH_MODEL_OP_BEACON_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0A) /*!< Config Beacon Set */ +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0D) /*!< Config Default TTL Set */ +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x13) /*!< Config GATT Proxy Set */ +#define ESP_BLE_MESH_MODEL_OP_RELAY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x27) /*!< Config Relay Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET ESP_BLE_MESH_MODEL_OP_1(0x03) /*!< Config Model Publication Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1B) /*!< Config Model Subscription Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x20) /*!< Config Model Subscription Virtual Address Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1C) /*!< Config Model Subscription Delete */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x21) /*!< Config Model Subscription Virtual Address Delete */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1E) /*!< Config Model Subscription Overwrite */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x22) /*!< Config Model Subscription Virtual Address Overwrite */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD ESP_BLE_MESH_MODEL_OP_2(0x80, 0x40) /*!< Config NetKey Add */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD ESP_BLE_MESH_MODEL_OP_1(0x00) /*!< Config AppKey Add */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3D) /*!< Config Model App Bind */ +#define ESP_BLE_MESH_MODEL_OP_NODE_RESET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x49) /*!< Config Node Reset */ +#define ESP_BLE_MESH_MODEL_OP_FRIEND_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x10) /*!< Config Friend Set */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x39) /*!< Config Heartbeat Publication Set */ +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3B) /*!< Config Heartbeat Subscription Set */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x45) /*!< Config NetKey Update */ +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x41) /*!< Config NetKey Delete */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE ESP_BLE_MESH_MODEL_OP_1(0x01) /*!< Config AppKey Update */ +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE ESP_BLE_MESH_MODEL_OP_2(0x80, 0x00) /*!< Config AppKey Delete */ +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x47) /*!< Config Node Identity Set */ +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x16) /*!< Config Key Refresh Phase Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1A) /*!< Config Model Publication Virtual Address Set */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1D) /*!< Config Model Subscription Delete All */ +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3F) /*!< Config Model App Unbind */ +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x24) /*!< Config Network Transmit Set */ + +/** + * esp_ble_mesh_opcode_config_status_t belongs to esp_ble_mesh_opcode_t, this typedef is only + * used to locate the opcodes used by the Config Model messages + * The following opcodes are used by the BLE Mesh Config Server Model internally to respond + * to the Config Client Model's request messages. + */ +typedef uint32_t esp_ble_mesh_opcode_config_status_t; + +#define ESP_BLE_MESH_MODEL_OP_BEACON_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0B) +#define ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_STATUS ESP_BLE_MESH_MODEL_OP_1(0x02) +#define ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x0E) +#define ESP_BLE_MESH_MODEL_OP_GATT_PROXY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x14) +#define ESP_BLE_MESH_MODEL_OP_RELAY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x28) +#define ESP_BLE_MESH_MODEL_OP_MODEL_PUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x19) +#define ESP_BLE_MESH_MODEL_OP_MODEL_SUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x1F) +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2A) +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2C) +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x44) +#define ESP_BLE_MESH_MODEL_OP_NET_KEY_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x43) +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x03) +#define ESP_BLE_MESH_MODEL_OP_APP_KEY_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x02) +#define ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x48) +#define ESP_BLE_MESH_MODEL_OP_MODEL_APP_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3E) +#define ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4C) +#define ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_LIST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4E) +#define ESP_BLE_MESH_MODEL_OP_NODE_RESET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x4A) +#define ESP_BLE_MESH_MODEL_OP_FRIEND_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x11) +#define ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x17) +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_STATUS ESP_BLE_MESH_MODEL_OP_1(0x06) +#define ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x3C) +#define ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2E) +#define ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x25) + +/** + * This typedef is only used to indicate the status code contained in some of + * the Configuration Server Model status message. + */ +typedef uint8_t esp_ble_mesh_cfg_status_t; + +#define ESP_BLE_MESH_CFG_STATUS_SUCCESS 0x00 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_ADDRESS 0x01 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_MODEL 0x02 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_APPKEY 0x03 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_NETKEY 0x04 +#define ESP_BLE_MESH_CFG_STATUS_INSUFFICIENT_RESOURCES 0x05 +#define ESP_BLE_MESH_CFG_STATUS_KEY_INDEX_ALREADY_STORED 0x06 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_PUBLISH_PARAMETERS 0x07 +#define ESP_BLE_MESH_CFG_STATUS_NOT_A_SUBSCRIBE_MODEL 0x08 +#define ESP_BLE_MESH_CFG_STATUS_STORAGE_FAILURE 0x09 +#define ESP_BLE_MESH_CFG_STATUS_FEATURE_NOT_SUPPORTED 0x0A +#define ESP_BLE_MESH_CFG_STATUS_CANNOT_UPDATE 0x0B +#define ESP_BLE_MESH_CFG_STATUS_CANNOT_REMOVE 0x0C +#define ESP_BLE_MESH_CFG_STATUS_CANNOT_BIND 0x0D +#define ESP_BLE_MESH_CFG_STATUS_TEMP_UNABLE_TO_CHANGE_STATE 0x0E +#define ESP_BLE_MESH_CFG_STATUS_CANNOT_SET 0x0F +#define ESP_BLE_MESH_CFG_STATUS_UNSPECIFIED_ERROR 0x10 +#define ESP_BLE_MESH_CFG_STATUS_INVALID_BINDING 0x11 + +/** + * esp_ble_mesh_opcode_health_client_get_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by esp_ble_mesh_health_client_get_state. + * The following opcodes will only be used in the esp_ble_mesh_health_client_get_state function. + */ +typedef uint32_t esp_ble_mesh_opcode_health_client_get_t; + +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x31) /*!< Health Fault Get */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x34) /*!< Health Period Get */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_GET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x04) /*!< Health Attention Get */ + +/** + * esp_ble_mesh_opcode_health_client_set_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by esp_ble_mesh_health_client_set_state. + * The following opcodes will only be used in the esp_ble_mesh_health_client_set_state function. + */ +typedef uint32_t esp_ble_mesh_opcode_health_client_set_t; + +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR ESP_BLE_MESH_MODEL_OP_2(0x80, 0x2F) /*!< Health Fault Clear */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x30) /*!< Health Fault Clear Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST ESP_BLE_MESH_MODEL_OP_2(0x80, 0x32) /*!< Health Fault Test */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x33) /*!< Health Fault Test Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x35) /*!< Health Period Set */ +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x36) /*!< Health Period Set Unacknowledged */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET ESP_BLE_MESH_MODEL_OP_2(0x80, 0x05) /*!< Health Attention Set */ +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x80, 0x06) /*!< Health Attention Set Unacknowledged */ + +/** + * esp_ble_mesh_health_model_status_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by the Health Model messages. + * The following opcodes are used by the BLE Mesh Health Server Model internally to + * respond to the Health Client Model's request messages. + */ +typedef uint32_t esp_ble_mesh_health_model_status_t; + +#define ESP_BLE_MESH_MODEL_OP_HEALTH_CURRENT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x04) +#define ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x05) +#define ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x37) +#define ESP_BLE_MESH_MODEL_OP_ATTENTION_STATUS ESP_BLE_MESH_MODEL_OP_2(0x80, 0x07) + +/** + * esp_ble_mesh_generic_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by functions esp_ble_mesh_generic_client_get_state + * & esp_ble_mesh_generic_client_set_state. + */ +typedef uint32_t esp_ble_mesh_generic_message_opcode_t; + +/*!< Generic OnOff Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x01) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x02) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x03) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x04) + +/*!< Generic Level Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x05) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x06) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x07) +#define ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x08) +#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x09) +#define ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0A) +#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0B) +#define ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0C) + +/*!< Generic Default Transition Time Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0D) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0E) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x0F) +#define ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x10) + +/*!< Generic Power OnOff Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x11) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x12) + +/*!< Generic Power OnOff Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x13) +#define ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x14) + +/*!< Generic Power Level Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x15) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x16) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x17) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x18) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x19) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1A) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1B) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1C) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1D) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1E) + +/*!< Generic Power Level Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x1F) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x20) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x21) +#define ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x22) + +/*!< Generic Battery Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x23) +#define ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x24) + +/*!< Generic Location Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x25) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS ESP_BLE_MESH_MODEL_OP_1(0x40) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x26) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x27) + +/*!< Generic Location Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET ESP_BLE_MESH_MODEL_OP_1(0x41) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x42) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x28) +#define ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x29) + +/*!< Generic Manufacturer Property Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2A) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x43) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2B) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x44) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x45) +#define ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x46) + +/*!< Generic Admin Property Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2C) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x47) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2D) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x48) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x49) +#define ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4A) + +/*!< Generic User Property Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2E) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4B) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x2F) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x4C) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x4D) +#define ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x4E) + +/*!< Generic Client Property Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET ESP_BLE_MESH_MODEL_OP_1(0x4F) +#define ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x50) + +/** + * esp_ble_mesh_sensor_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by functions esp_ble_mesh_sensor_client_get_state + * & esp_ble_mesh_sensor_client_set_state. + */ +typedef uint32_t esp_ble_mesh_sensor_message_opcode_t; + +/*!< Sensor Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x30) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS ESP_BLE_MESH_MODEL_OP_1(0x51) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x31) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS ESP_BLE_MESH_MODEL_OP_1(0x52) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x32) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS ESP_BLE_MESH_MODEL_OP_1(0x53) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x33) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS ESP_BLE_MESH_MODEL_OP_1(0x54) + +/*!< Sensor Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x34) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET ESP_BLE_MESH_MODEL_OP_1(0x55) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x56) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS ESP_BLE_MESH_MODEL_OP_1(0x57) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x35) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS ESP_BLE_MESH_MODEL_OP_1(0x58) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x36) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET ESP_BLE_MESH_MODEL_OP_1(0x59) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x5A) +#define ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5B) + +/** + * esp_ble_mesh_time_scene_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by functions esp_ble_mesh_time_scene_client_get_state + * & esp_ble_mesh_time_scene_client_set_state. + */ +typedef uint32_t esp_ble_mesh_time_scene_message_opcode_t; + +/*!< Time Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_TIME_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x37) +#define ESP_BLE_MESH_MODEL_OP_TIME_SET ESP_BLE_MESH_MODEL_OP_1(0x5C) +#define ESP_BLE_MESH_MODEL_OP_TIME_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5D) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x38) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x39) +#define ESP_BLE_MESH_MODEL_OP_TIME_ROLE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3A) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3B) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3C) +#define ESP_BLE_MESH_MODEL_OP_TIME_ZONE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3D) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3E) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x3F) +#define ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x40) + +/*!< Scene Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SCENE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x41) +#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL ESP_BLE_MESH_MODEL_OP_2(0x82, 0x42) +#define ESP_BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x43) +#define ESP_BLE_MESH_MODEL_OP_SCENE_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5E) +#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x44) +#define ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x45) + +/*!< Scene Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE ESP_BLE_MESH_MODEL_OP_2(0x82, 0x46) +#define ESP_BLE_MESH_MODEL_OP_SCENE_STORE_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x47) +#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9E) +#define ESP_BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9F) + +/*!< Scheduler Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x48) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS ESP_BLE_MESH_MODEL_OP_1(0x5F) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x49) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4A) + +/*!< Scheduler Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET ESP_BLE_MESH_MODEL_OP_1(0x60) +#define ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x61) + +/** + * esp_ble_mesh_light_message_opcode_t belongs to esp_ble_mesh_opcode_t, this typedef is + * only used to locate the opcodes used by functions esp_ble_mesh_light_client_get_state + * & esp_ble_mesh_light_client_set_state. + */ +typedef uint32_t esp_ble_mesh_light_message_opcode_t; + +/*!< Light Lightness Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x4F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x50) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x51) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x52) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x53) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x54) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x55) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x56) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x57) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x58) + +/*!< Light Lightness Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x59) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5C) + +/*!< Light CTL Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x5F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x60) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x61) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x62) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x63) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x64) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x65) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x66) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x67) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x68) + +/*!< Light CTL Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x69) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6C) + +/*!< Light HSL Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x6F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x70) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x71) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x72) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x73) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x74) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x75) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x76) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x77) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x78) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x79) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7E) + +/*!< Light HSL Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x7F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x80) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x81) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x82) + +/*!< Light xyL Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x83) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x84) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x85) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x86) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x87) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x88) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x89) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8C) + +/*!< Light xyL Setup Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8E) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x8F) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x90) + +/*!< Light Control Message Opcode */ +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x91) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x92) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x93) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x94) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x95) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x96) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x97) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x98) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x99) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9A) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9B) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9C) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET ESP_BLE_MESH_MODEL_OP_2(0x82, 0x9D) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET ESP_BLE_MESH_MODEL_OP_1(0x62) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK ESP_BLE_MESH_MODEL_OP_1(0x63) +#define ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS ESP_BLE_MESH_MODEL_OP_1(0x64) + +typedef uint32_t esp_ble_mesh_opcode_t; +/*!< End of defines of esp_ble_mesh_opcode_t */ + +/** + * This typedef is only used to indicate the status code contained in some of the + * server models (e.g. Generic Server Model) status message. + */ +typedef uint8_t esp_ble_mesh_model_status_t; + +#define ESP_BLE_MESH_MODEL_STATUS_SUCCESS 0x00 +#define ESP_BLE_MESH_MODEL_STATUS_CANNOT_SET_RANGE_MIN 0x01 +#define ESP_BLE_MESH_MODEL_STATUS_CANNOT_SET_RANGE_MAX 0x02 + +/** + * @brief BLE Mesh client models related definitions + */ + +/** Client model Get/Set message opcode and corresponding Status message opcode */ +typedef struct { + uint32_t cli_op; /*!< The client message opcode */ + uint32_t status_op; /*!< The server status opcode corresponding to the client message opcode */ +} esp_ble_mesh_client_op_pair_t; + +/** Client Model user data context. */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the client model. Initialized by the stack. */ + int op_pair_size; /*!< Size of the op_pair */ + const esp_ble_mesh_client_op_pair_t *op_pair; /*!< Table containing get/set message opcode and corresponding status message opcode */ + uint32_t publish_status; /*!< Callback used to handle the received unsolicited message. Initialized by the stack. */ + void *internal_data; /*!< Pointer to the internal data of client model */ + uint8_t msg_role; /*!< Role of the device (Node/Provisioner) that is going to send messages */ +} esp_ble_mesh_client_t; + +/** Common parameters of the messages sent by Client Model. */ +typedef struct { + esp_ble_mesh_opcode_t opcode; /*!< Message opcode */ + esp_ble_mesh_model_t *model; /*!< Pointer to the client model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< The context used to send message */ + int32_t msg_timeout; /*!< Timeout value (ms) to get response to the sent message */ + /*!< Note: if using default timeout value in menuconfig, make sure to set this value to 0 */ + uint8_t msg_role; /*!< Role of the device - Node/Provisioner */ +} esp_ble_mesh_client_common_param_t; + +/** + * @brief BLE Mesh server models related definitions + */ + +/** This enum value is the flag of transition timer operation */ +enum { + ESP_BLE_MESH_SERVER_TRANS_TIMER_START, /* Proper transition timer has been started */ + ESP_BLE_MESH_SERVER_FLAG_MAX, +}; + +/** Parameters of the server model state transition */ +typedef struct { + bool just_started; /*!< Indicate if the state transition has just started */ + + uint8_t trans_time; /*!< State transition time */ + uint8_t remain_time; /*!< Remaining time of state transition */ + uint8_t delay; /*!< Delay before starting state transition */ + uint32_t quo_tt; /*!< Duration of each divided transition step */ + uint32_t counter; /*!< Number of steps which the transition duration is divided */ + uint32_t total_duration; /*!< State transition total duration */ + int64_t start_timestamp; /*!< Time when the state transition is started */ + + /** + * Flag used to indicate if the transition timer has been started internally. + * + * If the model which contains esp_ble_mesh_state_transition_t sets "set_auto_rsp" + * to ESP_BLE_MESH_SERVER_RSP_BY_APP, the handler of the timer shall be initialized + * by the users. + * + * And users can use this flag to indicate whether the timer is started or not. + */ + BLE_MESH_ATOMIC_DEFINE(flag, ESP_BLE_MESH_SERVER_FLAG_MAX); + struct k_delayed_work timer; /*!< Timer used for state transition */ +} esp_ble_mesh_state_transition_t; + +/** Parameters of the server model received last same set message. */ +typedef struct { + uint8_t tid; /*!< Transaction number of the last message */ + uint16_t src; /*!< Source address of the last message */ + uint16_t dst; /*!< Destination address of the last message */ + int64_t timestamp; /*!< Time when the last message is received */ +} esp_ble_mesh_last_msg_info_t; + +#define ESP_BLE_MESH_SERVER_RSP_BY_APP 0 /*!< Response will be sent internally */ +#define ESP_BLE_MESH_SERVER_AUTO_RSP 1 /*!< Response need to be sent in the application */ + +/** Parameters of the Server Model response control */ +typedef struct { + /** + * @brief BLE Mesh Server Response Option + * 1. If get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, then the + * response of Client Get messages need to be replied by the application; + * 2. If get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, then the + * response of Client Get messages will be replied by the server models; + * 3. If set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, then the + * response of Client Set messages need to be replied by the application; + * 4. If set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, then the + * response of Client Set messages will be replied by the server models; + * 5. If status_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, then the + * response of Server Status messages need to be replied by the application; + * 6. If status_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, then the + * response of Server Status messages will be replied by the server models; + */ + uint8_t get_auto_rsp : 1, /*!< Response control for Client Get messages */ + set_auto_rsp : 1, /*!< Response control for Client Set messages */ + status_auto_rsp : 1; /*!< Response control for Server Status messages */ +} esp_ble_mesh_server_rsp_ctrl_t; + +/** + * @brief Server model state value union + */ +typedef union { + struct { + uint8_t onoff; /*!< The value of the Generic OnOff state */ + } gen_onoff; /*!< The Generic OnOff state */ + struct { + int16_t level; /*!< The value of the Generic Level state */ + } gen_level; /*!< The Generic Level state */ + struct { + uint8_t onpowerup; /*!< The value of the Generic OnPowerUp state */ + } gen_onpowerup; /*!< The Generic OnPowerUp state */ + struct { + uint16_t power; /*!< The value of the Generic Power Actual state */ + } gen_power_actual; /*!< The Generic Power Actual state */ + struct { + uint16_t lightness; /*!< The value of the Light Lightness Actual state */ + } light_lightness_actual; /*!< The Light Lightness Actual state */ + struct { + uint16_t lightness; /*!< The value of the Light Lightness Linear state */ + } light_lightness_linear; /*!< The Light Lightness Linear state */ + struct { + uint16_t lightness; /*!< The value of the Light CTL Lightness state */ + } light_ctl_lightness; /*!< The Light CTL Lightness state */ + struct { + uint16_t temperature; /*!< The value of the Light CTL Temperature state */ + int16_t delta_uv; /*!< The value of the Light CTL Delta UV state */ + } light_ctl_temp_delta_uv; /*!< The Light CTL Temperature & Delta UV states */ + struct { + uint16_t lightness; /*!< The value of the Light HSL Lightness state */ + } light_hsl_lightness; /*!< The Light HSL Lightness state */ + struct { + uint16_t hue; /*!< The value of the Light HSL Hue state */ + } light_hsl_hue; /*!< The Light HSL Hue state */ + struct { + uint16_t saturation; /*!< The value of the Light HSL Saturation state */ + } light_hsl_saturation; /*!< The Light HSL Saturation state */ + struct { + uint16_t lightness; /*!< The value of the Light xyL Lightness state */ + } light_xyl_lightness; /*!< The Light xyL Lightness state */ + struct { + uint8_t onoff; /*!< The value of the Light LC Light OnOff state */ + } light_lc_light_onoff; /*!< The Light LC Light OnOff state */ +} esp_ble_mesh_server_state_value_t; + +/** This enum value is the type of server model states */ +typedef enum { + ESP_BLE_MESH_GENERIC_ONOFF_STATE, + ESP_BLE_MESH_GENERIC_LEVEL_STATE, + ESP_BLE_MESH_GENERIC_ONPOWERUP_STATE, + ESP_BLE_MESH_GENERIC_POWER_ACTUAL_STATE, + ESP_BLE_MESH_LIGHT_LIGHTNESS_ACTUAL_STATE, + ESP_BLE_MESH_LIGHT_LIGHTNESS_LINEAR_STATE, + ESP_BLE_MESH_LIGHT_CTL_LIGHTNESS_STATE, + ESP_BLE_MESH_LIGHT_CTL_TEMP_DELTA_UV_STATE, + ESP_BLE_MESH_LIGHT_HSL_LIGHTNESS_STATE, + ESP_BLE_MESH_LIGHT_HSL_HUE_STATE, + ESP_BLE_MESH_LIGHT_HSL_SATURATION_STATE, + ESP_BLE_MESH_LIGHT_XYL_LIGHTNESS_STATE, + ESP_BLE_MESH_LIGHT_LC_LIGHT_ONOFF_STATE, + ESP_BLE_MESH_SERVER_MODEL_STATE_MAX, +} esp_ble_mesh_server_state_type_t; + +/*!< This enum value is the event of undefined SIG models and vendor models */ +typedef enum { + ESP_BLE_MESH_MODEL_OPERATION_EVT, /*!< User-defined models receive messages from peer devices (e.g. get, set, status, etc) event */ + ESP_BLE_MESH_MODEL_SEND_COMP_EVT, /*!< User-defined models send messages completion event */ + ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT, /*!< User-defined models publish messages completion event */ + ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT, /*!< User-defined client models receive publish messages event */ + ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT, /*!< Timeout event for the user-defined client models that failed to receive response from peer server models */ + ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT, /*!< When a model is configured to publish messages periodically, this event will occur during every publish period */ + ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT, /*!< Server models update state value completion event */ + ESP_BLE_MESH_MODEL_EVT_MAX, +} esp_ble_mesh_model_cb_event_t; + +/** + * @brief BLE Mesh model callback parameters union + */ +typedef union { + /** + * @brief ESP_BLE_MESH_MODEL_OPERATION_EVT + */ + struct ble_mesh_model_operation_evt_param { + uint32_t opcode; /*!< Opcode of the received message */ + esp_ble_mesh_model_t *model; /*!< Pointer to the model which receives the message */ + esp_ble_mesh_msg_ctx_t *ctx; /*!< Pointer to the context of the received message */ + uint16_t length; /*!< Length of the received message */ + uint8_t *msg; /*!< Value of the received message */ + } model_operation; /*!< Event parameter of ESP_BLE_MESH_MODEL_OPERATION_EVT */ + /** + * @brief ESP_BLE_MESH_MODEL_SEND_COMP_EVT + */ + struct ble_mesh_model_send_comp_param { + int err_code; /*!< Indicate the result of sending a message */ + uint32_t opcode; /*!< Opcode of the message */ + esp_ble_mesh_model_t *model; /*!< Pointer to the model which sends the message */ + esp_ble_mesh_msg_ctx_t *ctx; /*!< Context of the message */ + } model_send_comp; /*!< Event parameter of ESP_BLE_MESH_MODEL_SEND_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT + */ + struct ble_mesh_model_publish_comp_param { + int err_code; /*!< Indicate the result of publishing a message */ + esp_ble_mesh_model_t *model; /*!< Pointer to the model which publishes the message */ + } model_publish_comp; /*!< Event parameter of ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT */ + /** + * @brief ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT + */ + struct ble_mesh_mod_recv_publish_msg_param { + uint32_t opcode; /*!< Opcode of the unsolicited received message */ + esp_ble_mesh_model_t *model; /*!< Pointer to the model which receives the message */ + esp_ble_mesh_msg_ctx_t *ctx; /*!< Pointer to the context of the message */ + uint16_t length; /*!< Length of the received message */ + uint8_t *msg; /*!< Value of the received message */ + } client_recv_publish_msg; /*!< Event parameter of ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT */ + /** + * @brief ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT + */ + struct ble_mesh_client_model_send_timeout_param { + uint32_t opcode; /*!< Opcode of the previously sent message */ + esp_ble_mesh_model_t *model; /*!< Pointer to the model which sends the previous message */ + esp_ble_mesh_msg_ctx_t *ctx; /*!< Pointer to the context of the previous message */ + } client_send_timeout; /*!< Event parameter of ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT */ + /** + * @brief ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT + */ + struct ble_mesh_model_publish_update_evt_param { + esp_ble_mesh_model_t *model; /*!< Pointer to the model which is going to update its publish message */ + } model_publish_update; /*!< Event parameter of ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT */ + /** + * @brief ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT + */ + struct ble_mesh_server_model_update_state_comp_param { + int err_code; /*!< Indicate the result of updating server model state */ + esp_ble_mesh_model_t *model; /*!< Pointer to the server model which state value is updated */ + esp_ble_mesh_server_state_type_t type; /*!< Type of the updated server state */ + } server_model_update_state; /*!< Event parameter of ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT */ +} esp_ble_mesh_model_cb_param_t; + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_DEFS_H_ */ diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c new file mode 100644 index 000000000..c41933984 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_config_model_api.c @@ -0,0 +1,103 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_config_model.h" +#include "esp_ble_mesh_config_model_api.h" + +esp_err_t esp_ble_mesh_register_config_client_callback(esp_ble_mesh_cfg_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_CONFIG_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_config_server_callback(esp_ble_mesh_cfg_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_CONFIG_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool config_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: + case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_get_state_t *get_state) +{ + btc_ble_mesh_config_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (config_client_get_need_param(params->opcode) && get_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_CONFIG_CLIENT; + msg.act = BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE; + arg.cfg_client_get_state.params = params; + arg.cfg_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_config_client_args_t), btc_ble_mesh_config_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_set_state_t *set_state) +{ + btc_ble_mesh_config_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + !ESP_BLE_MESH_ADDR_IS_UNICAST(params->ctx.addr) || + (params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && set_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_CONFIG_CLIENT; + msg.act = BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE; + arg.cfg_client_set_state.params = params; + arg.cfg_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_config_client_args_t), btc_ble_mesh_config_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c new file mode 100644 index 000000000..b93a1f97f --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_generic_model_api.c @@ -0,0 +1,98 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_generic_model.h" +#include "esp_ble_mesh_generic_model_api.h" + +esp_err_t esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_GENERIC_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +static bool generic_client_get_need_param(esp_ble_mesh_opcode_t opcode) +{ + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + return true; + default: + return false; + } +} + +esp_err_t esp_ble_mesh_generic_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_generic_client_get_state_t *get_state) +{ + btc_ble_mesh_generic_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED || + (generic_client_get_need_param(params->opcode) && get_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GENERIC_CLIENT; + msg.act = BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE; + arg.generic_client_get_state.params = params; + arg.generic_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_generic_client_args_t), btc_ble_mesh_generic_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_generic_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_generic_client_set_state_t *set_state) +{ + btc_ble_mesh_generic_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_GENERIC_CLIENT; + msg.act = BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE; + arg.generic_client_set_state.params = params; + arg.generic_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_generic_client_args_t), btc_ble_mesh_generic_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_generic_server_callback(esp_ble_mesh_generic_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_GENERIC_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c new file mode 100644 index 000000000..7c3b666f6 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_health_model_api.c @@ -0,0 +1,105 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_health_model.h" +#include "esp_ble_mesh_health_model_api.h" + +esp_err_t esp_ble_mesh_register_health_client_callback(esp_ble_mesh_health_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_HEALTH_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_health_server_callback(esp_ble_mesh_health_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_HEALTH_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_get_state_t *get_state) +{ + btc_ble_mesh_health_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED || + (params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET && get_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_HEALTH_CLIENT; + msg.act = BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE; + arg.health_client_get_state.params = params; + arg.health_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_health_client_args_t), btc_ble_mesh_health_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_set_state_t *set_state) +{ + btc_ble_mesh_health_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_HEALTH_CLIENT; + msg.act = BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE; + arg.health_client_set_state.params = params; + arg.health_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_health_client_args_t), btc_ble_mesh_health_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_health_server_fault_update(esp_ble_mesh_elem_t *element) +{ + btc_ble_mesh_health_server_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (element == NULL) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_HEALTH_SERVER; + msg.act = BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE; + arg.health_fault_update.element = element; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_health_server_args_t), NULL) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c new file mode 100644 index 000000000..127941b41 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_lighting_model_api.c @@ -0,0 +1,85 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_lighting_model.h" +#include "esp_ble_mesh_lighting_model_api.h" + +esp_err_t esp_ble_mesh_register_light_client_callback(esp_ble_mesh_light_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_LIGHTING_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_light_client_get_state_t *get_state) +{ + btc_ble_mesh_lighting_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED || + (params->opcode == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET && get_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_LIGHTING_CLIENT; + msg.act = BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE; + arg.light_client_get_state.params = params; + arg.light_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_lighting_client_args_t), btc_ble_mesh_lighting_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_light_client_set_state_t *set_state) +{ + btc_ble_mesh_lighting_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_LIGHTING_CLIENT; + msg.act = BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE; + arg.light_client_set_state.params = params; + arg.light_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_lighting_client_args_t), btc_ble_mesh_lighting_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_lighting_server_callback(esp_ble_mesh_lighting_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_LIGHTING_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c new file mode 100644 index 000000000..8bf77b385 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_sensor_model_api.c @@ -0,0 +1,86 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_sensor_model.h" +#include "esp_ble_mesh_sensor_model_api.h" + +esp_err_t esp_ble_mesh_register_sensor_client_callback(esp_ble_mesh_sensor_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SENSOR_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_sensor_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sensor_client_get_state_t *get_state) +{ + btc_ble_mesh_sensor_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || get_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_SENSOR_CLIENT; + msg.act = BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE; + arg.sensor_client_get_state.params = params; + arg.sensor_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_sensor_client_args_t), btc_ble_mesh_sensor_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sensor_client_set_state_t *set_state) +{ + btc_ble_mesh_sensor_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_SENSOR_CLIENT; + msg.act = BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE; + arg.sensor_client_set_state.params = params; + arg.sensor_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_sensor_client_args_t), btc_ble_mesh_sensor_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_sensor_server_callback(esp_ble_mesh_sensor_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_SENSOR_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} + + diff --git a/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c new file mode 100644 index 000000000..bd71ea7cd --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/esp_ble_mesh_time_scene_model_api.c @@ -0,0 +1,86 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc/btc_manage.h" + +#include "btc_ble_mesh_time_scene_model.h" +#include "esp_ble_mesh_time_scene_model_api.h" + +esp_err_t esp_ble_mesh_register_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_TIME_SCENE_CLIENT, callback) == 0 ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_time_scene_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_time_scene_client_get_state_t *get_state) +{ + btc_ble_mesh_time_scene_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED || + (params->opcode == ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET && get_state == NULL)) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_TIME_SCENE_CLIENT; + msg.act = BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE; + arg.time_scene_client_get_state.params = params; + arg.time_scene_client_get_state.get_state = get_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_time_scene_client_args_t), btc_ble_mesh_time_scene_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_time_scene_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_time_scene_client_set_state_t *set_state) +{ + btc_ble_mesh_time_scene_client_args_t arg = {0}; + btc_msg_t msg = {0}; + + if (params == NULL || params->model == NULL || set_state == NULL || + params->ctx.net_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.app_idx == ESP_BLE_MESH_KEY_UNUSED || + params->ctx.addr == ESP_BLE_MESH_ADDR_UNASSIGNED) { + return ESP_ERR_INVALID_ARG; + } + + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + msg.sig = BTC_SIG_API_CALL; + msg.pid = BTC_PID_TIME_SCENE_CLIENT; + msg.act = BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE; + arg.time_scene_client_set_state.params = params; + arg.time_scene_client_set_state.set_state = set_state; + + return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_time_scene_client_args_t), btc_ble_mesh_time_scene_client_arg_deep_copy) + == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); +} + +esp_err_t esp_ble_mesh_register_time_scene_server_callback(esp_ble_mesh_time_scene_server_cb_t callback) +{ + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); + + return (btc_profile_cb_set(BTC_PID_TIME_SCENE_SERVER, callback) == 0 ? ESP_OK : ESP_FAIL); +} + diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h new file mode 100644 index 000000000..06d45e9bb --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h @@ -0,0 +1,826 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_CONFIG_MODEL_API_H_ +#define _ESP_BLE_MESH_CONFIG_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_CFG_SRV + * + * @brief Define a new Config Server Model. + * + * @note The Config Server Model can only be included by a Primary Element. + * + * @param srv_data Pointer to a unique Config Server Model user_data. + * + * @return New Config Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_CFG_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_CONFIG_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_CFG_CLI + * + * @brief Define a new Config Client Model. + * + * @note The Config Client Model can only be included by a Primary Element. + * + * @param cli_data Pointer to a unique struct esp_ble_mesh_client_t. + * + * @return New Config Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_CFG_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_CONFIG_CLI, \ + NULL, NULL, cli_data) + +/** Configuration Server Model context */ +typedef struct esp_ble_mesh_cfg_srv { + esp_ble_mesh_model_t *model; /*!< Pointer to Configuration Server Model */ + + uint8_t net_transmit; /*!< Network Transmit state */ + uint8_t relay; /*!< Relay Mode state */ + uint8_t relay_retransmit; /*!< Relay Retransmit state */ + uint8_t beacon; /*!< Secure Network Beacon state */ + uint8_t gatt_proxy; /*!< GATT Proxy state */ + uint8_t friend_state; /*!< Friend state */ + uint8_t default_ttl; /*!< Default TTL */ + + /** Heartbeat Publication */ + struct { + struct k_delayed_work timer; /*!< Heartbeat Publication timer */ + + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint16_t count; /*!< Number of Heartbeat messages to be sent */ + uint8_t period; /*!< Period for sending Heartbeat messages */ + uint8_t ttl; /*!< TTL to be used when sending Heartbeat messages */ + uint16_t feature; /*!< Bit field indicating features that trigger Heartbeat messages when changed */ + uint16_t net_idx; /*!< NetKey Index used by Heartbeat Publication */ + } heartbeat_pub; + + /** Heartbeat Subscription */ + struct { + int64_t expiry; /*!< Timestamp when Heartbeat subscription period is expired */ + + uint16_t src; /*!< Source address for Heartbeat messages */ + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint16_t count; /*!< Number of Heartbeat messages received */ + uint8_t min_hops; /*!< Minimum hops when receiving Heartbeat messages */ + uint8_t max_hops; /*!< Maximum hops when receiving Heartbeat messages */ + + /** Optional heartbeat subscription tracking function */ + esp_ble_mesh_cb_t heartbeat_recv_cb; + } heartbeat_sub; +} esp_ble_mesh_cfg_srv_t; + +/** Parameters of Config Composition Data Get. */ +typedef struct { + uint8_t page; /*!< Page number of the Composition Data. */ +} esp_ble_mesh_cfg_composition_data_get_t; + +/** Parameters of Config Model Publication Get. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_pub_get_t; + +/** Parameters of Config SIG Model Subscription Get. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ +} esp_ble_mesh_cfg_sig_model_sub_get_t; + +/** Parameters of Config Vendor Model Subscription Get. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_vnd_model_sub_get_t; + +/** Parameters of Config AppKey Get. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ +} esp_ble_mesh_cfg_app_key_get_t; + +/** Parameters of Config Node Identity Get. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ +} esp_ble_mesh_cfg_node_identity_get_t; + +/** Parameters of Config SIG Model App Get. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ +} esp_ble_mesh_cfg_sig_model_app_get_t; + +/** Parameters of Config Vendor Model App Get. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_vnd_model_app_get_t; + +/** Parameters of Config Key Refresh Phase Get. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ +} esp_ble_mesh_cfg_kr_phase_get_t; + +/** Parameters of Config Low Power Node PollTimeout Get. */ +typedef struct { + uint16_t lpn_addr; /*!< The unicast address of the Low Power node */ +} esp_ble_mesh_cfg_lpn_polltimeout_get_t; + +/** Parameters of Config Beacon Set. */ +typedef struct { + uint8_t beacon; /*!< New Secure Network Beacon state */ +} esp_ble_mesh_cfg_beacon_set_t; + +/** Parameters of Config Default TTL Set. */ +typedef struct { + uint8_t ttl; /*!< The default TTL state value */ +} esp_ble_mesh_cfg_default_ttl_set_t; + +/** Parameters of Config Friend Set. */ +typedef struct { + uint8_t friend_state; /*!< The friend state value */ +} esp_ble_mesh_cfg_friend_set_t; + +/** Parameters of Config GATT Proxy Set. */ +typedef struct { + uint8_t gatt_proxy; /*!< The GATT Proxy state value */ +} esp_ble_mesh_cfg_gatt_proxy_set_t; + +/** Parameters of Config Relay Set. */ +typedef struct { + uint8_t relay; /*!< The relay value */ + uint8_t relay_retransmit; /*!< The relay retransmit value */ +} esp_ble_mesh_cfg_relay_set_t; + +/** Parameters of Config NetKey Add. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint8_t net_key[16]; /*!< The network key value */ +} esp_ble_mesh_cfg_net_key_add_t; + +/** Parameters of Config AppKey Add. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint16_t app_idx; /*!< The app key index */ + uint8_t app_key[16]; /*!< The app key value */ +} esp_ble_mesh_cfg_app_key_add_t; + +/** Parameters of Config Model App Bind. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_app_idx; /*!< Index of the app key to bind with the model */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_app_bind_t; + +/** Parameters of Config Model Publication Set. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t publish_addr; /*!< Value of the publish address */ + uint16_t publish_app_idx; /*!< Index of the application key */ + bool cred_flag; /*!< Value of the Friendship Credential Flag */ + uint8_t publish_ttl; /*!< Default TTL value for the publishing messages */ + uint8_t publish_period; /*!< Period for periodic status publishing */ + uint8_t publish_retransmit; /*!< Number of retransmissions and number of 50-millisecond steps between retransmissions */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_pub_set_t; + +/** Parameters of Config Model Subscription Add. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t sub_addr; /*!< The address to be added to the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_add_t; + +/** Parameters of Config Model Subscription Delete. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t sub_addr; /*!< The address to be removed from the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_delete_t; + +/** Parameters of Config Model Subscription Overwrite. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t sub_addr; /*!< The address to be added to the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_overwrite_t; + +/** Parameters of Config Model Subscription Virtual Address Add. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint8_t label_uuid[16]; /*!< The Label UUID of the virtual address to be added to the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_va_add_t; + +/** Parameters of Config Model Subscription Virtual Address Delete. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint8_t label_uuid[16]; /*!< The Label UUID of the virtual address to be removed from the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_va_delete_t; + +/** Parameters of Config Model Subscription Virtual Address Overwrite. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint8_t label_uuid[16]; /*!< The Label UUID of the virtual address to be added to the Subscription List */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_va_overwrite_t; + +/** Parameters of Config Model Publication Virtual Address Set. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint8_t label_uuid[16]; /*!< Value of the Label UUID publish address */ + uint16_t publish_app_idx; /*!< Index of the application key */ + bool cred_flag; /*!< Value of the Friendship Credential Flag */ + uint8_t publish_ttl; /*!< Default TTL value for the publishing messages */ + uint8_t publish_period; /*!< Period for periodic status publishing */ + uint8_t publish_retransmit; /*!< Number of retransmissions and number of 50-millisecond steps between retransmissions */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_pub_va_set_t; + +/** Parameters of Config Model Subscription Delete All. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_sub_delete_all_t; + +/** Parameters of Config NetKey Update. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint8_t net_key[16]; /*!< The network key value */ +} esp_ble_mesh_cfg_net_key_update_t; + +/** Parameters of Config NetKey Delete. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ +} esp_ble_mesh_cfg_net_key_delete_t; + +/** Parameters of Config AppKey Update. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint16_t app_idx; /*!< The app key index */ + uint8_t app_key[16]; /*!< The app key value */ +} esp_ble_mesh_cfg_app_key_update_t; + +/** Parameters of Config AppKey Delete. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint16_t app_idx; /*!< The app key index */ +} esp_ble_mesh_cfg_app_key_delete_t; + +/** Parameters of Config Node Identity Set. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint8_t identity; /*!< New Node Identity state */ +} esp_ble_mesh_cfg_node_identity_set_t; + +/** Parameters of Config Model App Unbind. */ +typedef struct { + uint16_t element_addr; /*!< The element address */ + uint16_t model_app_idx; /*!< Index of the app key to bind with the model */ + uint16_t model_id; /*!< The model id */ + uint16_t company_id; /*!< The company id, if not a vendor model, shall set to 0xFFFF */ +} esp_ble_mesh_cfg_model_app_unbind_t; + +/** Parameters of Config Key Refresh Phase Set. */ +typedef struct { + uint16_t net_idx; /*!< The network key index */ + uint8_t transition; /*!< New Key Refresh Phase Transition */ +} esp_ble_mesh_cfg_kr_phase_set_t; + +/** Parameters of Config Network Transmit Set. */ +typedef struct { + uint8_t net_transmit; /*!< Network Transmit State */ +} esp_ble_mesh_cfg_net_transmit_set_t; + +/** Parameters of Config Model Heartbeat Publication Set. */ +typedef struct { + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint8_t count; /*!< Number of Heartbeat messages to be sent */ + uint8_t period; /*!< Period for sending Heartbeat messages */ + uint8_t ttl; /*!< TTL to be used when sending Heartbeat messages */ + uint16_t feature; /*!< Bit field indicating features that trigger Heartbeat messages when changed */ + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_cfg_heartbeat_pub_set_t; + +/** Parameters of Config Model Heartbeat Subscription Set. */ +typedef struct { + uint16_t src; /*!< Source address for Heartbeat messages */ + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint8_t period; /*!< Period for receiving Heartbeat messages */ +} esp_ble_mesh_cfg_heartbeat_sub_set_t; + +/** + * @brief For ESP_BLE_MESH_MODEL_OP_BEACON_GET + * ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET + * ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET + * ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET + * ESP_BLE_MESH_MODEL_OP_RELAY_GET + * ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET + * ESP_BLE_MESH_MODEL_OP_FRIEND_GET + * ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET + * ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET + * the get_state parameter in the esp_ble_mesh_config_client_get_state function should not be set to NULL. + */ +typedef union { + esp_ble_mesh_cfg_model_pub_get_t model_pub_get; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET. */ + esp_ble_mesh_cfg_composition_data_get_t comp_data_get; /*!< For ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET. */ + esp_ble_mesh_cfg_sig_model_sub_get_t sig_model_sub_get; /*!< For ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET */ + esp_ble_mesh_cfg_vnd_model_sub_get_t vnd_model_sub_get; /*!< For ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET */ + esp_ble_mesh_cfg_app_key_get_t app_key_get; /*!< For ESP_BLE_MESH_MODEL_OP_APP_KEY_GET. */ + esp_ble_mesh_cfg_node_identity_get_t node_identity_get; /*!< For ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET. */ + esp_ble_mesh_cfg_sig_model_app_get_t sig_model_app_get; /*!< For ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET */ + esp_ble_mesh_cfg_vnd_model_app_get_t vnd_model_app_get; /*!< For ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET */ + esp_ble_mesh_cfg_kr_phase_get_t kr_phase_get; /*!< For ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET */ + esp_ble_mesh_cfg_lpn_polltimeout_get_t lpn_pollto_get; /*!< For ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET */ +} esp_ble_mesh_cfg_client_get_state_t; + +/** + * @brief For ESP_BLE_MESH_MODEL_OP_BEACON_SET + * ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET + * ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET + * ESP_BLE_MESH_MODEL_OP_RELAY_SET + * ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE + * ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE + * ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD + * ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD + * ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND + * ESP_BLE_MESH_MODEL_OP_NODE_RESET + * ESP_BLE_MESH_MODEL_OP_FRIEND_SET + * ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET + * ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET + * the set_state parameter in the esp_ble_mesh_config_client_set_state function should not be set to NULL. + */ +typedef union { + esp_ble_mesh_cfg_beacon_set_t beacon_set; /*!< For ESP_BLE_MESH_MODEL_OP_BEACON_SET */ + esp_ble_mesh_cfg_default_ttl_set_t default_ttl_set; /*!< For ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET */ + esp_ble_mesh_cfg_friend_set_t friend_set; /*!< For ESP_BLE_MESH_MODEL_OP_FRIEND_SET */ + esp_ble_mesh_cfg_gatt_proxy_set_t gatt_proxy_set; /*!< For ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET */ + esp_ble_mesh_cfg_relay_set_t relay_set; /*!< For ESP_BLE_MESH_MODEL_OP_RELAY_SET */ + esp_ble_mesh_cfg_net_key_add_t net_key_add; /*!< For ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD */ + esp_ble_mesh_cfg_app_key_add_t app_key_add; /*!< For ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD */ + esp_ble_mesh_cfg_model_app_bind_t model_app_bind; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND */ + esp_ble_mesh_cfg_model_pub_set_t model_pub_set; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET */ + esp_ble_mesh_cfg_model_sub_add_t model_sub_add; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD */ + esp_ble_mesh_cfg_model_sub_delete_t model_sub_delete; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE */ + esp_ble_mesh_cfg_model_sub_overwrite_t model_sub_overwrite; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE */ + esp_ble_mesh_cfg_model_sub_va_add_t model_sub_va_add; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD */ + esp_ble_mesh_cfg_model_sub_va_delete_t model_sub_va_delete; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE */ + esp_ble_mesh_cfg_model_sub_va_overwrite_t model_sub_va_overwrite; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE */ + esp_ble_mesh_cfg_heartbeat_pub_set_t heartbeat_pub_set; /*!< For ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET */ + esp_ble_mesh_cfg_heartbeat_sub_set_t heartbeat_sub_set; /*!< For ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET */ + esp_ble_mesh_cfg_model_pub_va_set_t model_pub_va_set; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET */ + esp_ble_mesh_cfg_model_sub_delete_all_t model_sub_delete_all; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL */ + esp_ble_mesh_cfg_net_key_update_t net_key_update; /*!< For ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE */ + esp_ble_mesh_cfg_net_key_delete_t net_key_delete; /*!< For ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE */ + esp_ble_mesh_cfg_app_key_update_t app_key_update; /*!< For ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE */ + esp_ble_mesh_cfg_app_key_delete_t app_key_delete; /*!< For ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE */ + esp_ble_mesh_cfg_node_identity_set_t node_identity_set; /*!< For ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET */ + esp_ble_mesh_cfg_model_app_unbind_t model_app_unbind; /*!< For ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND */ + esp_ble_mesh_cfg_kr_phase_set_t kr_phase_set; /*!< For ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET */ + esp_ble_mesh_cfg_net_transmit_set_t net_transmit_set; /*!< For ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET */ +} esp_ble_mesh_cfg_client_set_state_t; + +/** Parameter of Config Beacon Status */ +typedef struct { + uint8_t beacon; /*!< Secure Network Beacon state value */ +} esp_ble_mesh_cfg_beacon_status_cb_t; + +/** Parameters of Config Composition Data Status */ +typedef struct { + uint8_t page; /*!< Page number of the Composition Data */ + struct net_buf_simple *composition_data; /*!< Pointer to Composition Data for the identified page */ +} esp_ble_mesh_cfg_comp_data_status_cb_t; + +/** Parameter of Config Default TTL Status */ +typedef struct { + uint8_t default_ttl; /*!< Default TTL state value */ +} esp_ble_mesh_cfg_default_ttl_status_cb_t; + +/** Parameter of Config GATT Proxy Status */ +typedef struct { + uint8_t gatt_proxy; /*!< GATT Proxy state value */ +} esp_ble_mesh_cfg_gatt_proxy_status_cb_t; + +/** Parameters of Config Relay Status */ +typedef struct { + uint8_t relay; /*!< Relay state value */ + uint8_t retransmit; /*!< Relay retransmit value(number of retransmissions and number of 10-millisecond steps between retransmissions) */ +} esp_ble_mesh_cfg_relay_status_cb_t; + +/** Parameters of Config Model Publication Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t publish_addr; /*!< Value of the publish address */ + uint16_t app_idx; /*!< Index of the application key */ + bool cred_flag; /*!< Value of the Friendship Credential Flag */ + uint8_t ttl; /*!< Default TTL value for the outgoing messages */ + uint8_t period; /*!< Period for periodic status publishing */ + uint8_t transmit; /*!< Number of retransmissions and number of 50-millisecond steps between retransmissions */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_cfg_model_pub_status_cb_t; + +/** Parameters of Config Model Subscription Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t sub_addr; /*!< Value of the address */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_cfg_model_sub_status_cb_t; + +/** Parameters of Config NetKey Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t net_idx; /*!< Index of the NetKey */ +} esp_ble_mesh_cfg_net_key_status_cb_t; + +/** Parameters of Config AppKey Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t net_idx; /*!< Index of the NetKey */ + uint16_t app_idx; /*!< Index of the application key */ +} esp_ble_mesh_cfg_app_key_status_cb_t; + +/** Parameters of Config Model App Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t app_idx; /*!< Index of the application key */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_cfg_mod_app_status_cb_t; + +/** Parameter of Config Friend Status */ +typedef struct { + uint8_t friend_state; /*!< Friend state value */ +} esp_ble_mesh_cfg_friend_status_cb_t; + +/** Parameters of Config Heartbeat Publication Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint8_t count; /*!< Number of Heartbeat messages remaining to be sent */ + uint8_t period; /*!< Period for sending Heartbeat messages */ + uint8_t ttl; /*!< TTL to be used when sending Heartbeat messages */ + uint16_t features; /*!< Features that trigger Heartbeat messages when changed */ + uint16_t net_idx; /*!< Index of the NetKey */ +} esp_ble_mesh_cfg_hb_pub_status_cb_t; + +/** Parameters of Config Heartbeat Subscription Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t src; /*!< Source address for Heartbeat messages */ + uint16_t dst; /*!< Destination address for Heartbeat messages */ + uint8_t period; /*!< Remaining Period for processing Heartbeat messages */ + uint8_t count; /*!< Number of Heartbeat messages received */ + uint8_t min_hops; /*!< Minimum hops when receiving Heartbeat messages */ + uint8_t max_hops; /*!< Maximum hops when receiving Heartbeat messages */ +} esp_ble_mesh_cfg_hb_sub_status_cb_t; + +/** Parameters of Config Network Transmit Status */ +typedef struct { + uint8_t net_trans_count: 3; /*!< Number of transmissions for each Network PDU originating from the node */ + uint8_t net_trans_step : 5; /*!< Maximum hops when receiving Heartbeat messages */ +} esp_ble_mesh_cfg_net_trans_status_cb_t; + +/** Parameters of Config SIG/Vendor Subscription List */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + struct net_buf_simple *sub_addr; /*!< A block of all addresses from the Subscription List */ +} esp_ble_mesh_cfg_model_sub_list_cb_t; + +/** Parameter of Config NetKey List */ +typedef struct { + struct net_buf_simple *net_idx; /*!< A list of NetKey Indexes known to the node */ +} esp_ble_mesh_cfg_net_key_list_cb_t; + +/** Parameters of Config AppKey List */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t net_idx; /*!< NetKey Index of the NetKey that the AppKeys are bound to */ + struct net_buf_simple *app_idx; /*!< A list of AppKey indexes that are bound to the NetKey identified by NetKeyIndex */ +} esp_ble_mesh_cfg_app_key_list_cb_t; + +/** Parameters of Config Node Identity Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t net_idx; /*!< Index of the NetKey */ + uint8_t identity; /*!< Node Identity state */ +} esp_ble_mesh_cfg_node_id_status_cb_t; + +/** Parameters of Config SIG/Vendor Model App List */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t element_addr; /*!< Address of the element */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ + struct net_buf_simple *app_idx; /*!< All AppKey indexes bound to the Model */ +} esp_ble_mesh_cfg_model_app_list_cb_t; + +/** Parameters of Config Key Refresh Phase Status */ +typedef struct { + uint8_t status; /*!< Status Code for the request message */ + uint16_t net_idx; /*!< Index of the NetKey */ + uint8_t phase; /*!< Key Refresh Phase state */ +} esp_ble_mesh_cfg_kr_phase_status_cb_t; + +/** Parameters of Config Low Power Node PollTimeout Status */ +typedef struct { + uint16_t lpn_addr; /*!< The unicast address of the Low Power node */ + int32_t poll_timeout; /*!< The current value of the PollTimeout timer of the Low Power node */ +} esp_ble_mesh_cfg_lpn_pollto_status_cb_t; + +/** + * @brief Configuration Client Model received message union + */ +typedef union { + esp_ble_mesh_cfg_beacon_status_cb_t beacon_status; /*!< The beacon status value */ + esp_ble_mesh_cfg_comp_data_status_cb_t comp_data_status; /*!< The composition data status value */ + esp_ble_mesh_cfg_default_ttl_status_cb_t default_ttl_status; /*!< The default_ttl status value */ + esp_ble_mesh_cfg_gatt_proxy_status_cb_t gatt_proxy_status; /*!< The gatt_proxy status value */ + esp_ble_mesh_cfg_relay_status_cb_t relay_status; /*!< The relay status value */ + esp_ble_mesh_cfg_model_pub_status_cb_t model_pub_status; /*!< The model publication status value */ + esp_ble_mesh_cfg_model_sub_status_cb_t model_sub_status; /*!< The model subscription status value */ + esp_ble_mesh_cfg_net_key_status_cb_t netkey_status; /*!< The netkey status value */ + esp_ble_mesh_cfg_app_key_status_cb_t appkey_status; /*!< The appkey status value */ + esp_ble_mesh_cfg_mod_app_status_cb_t model_app_status; /*!< The model app status value */ + esp_ble_mesh_cfg_friend_status_cb_t friend_status; /*!< The friend status value */ + esp_ble_mesh_cfg_hb_pub_status_cb_t heartbeat_pub_status; /*!< The heartbeat publication status value */ + esp_ble_mesh_cfg_hb_sub_status_cb_t heartbeat_sub_status; /*!< The heartbeat subscription status value */ + esp_ble_mesh_cfg_net_trans_status_cb_t net_transmit_status; /*!< The network transmit status value */ + esp_ble_mesh_cfg_model_sub_list_cb_t model_sub_list; /*!< The model subscription list value */ + esp_ble_mesh_cfg_net_key_list_cb_t netkey_list; /*!< The network key index list value */ + esp_ble_mesh_cfg_app_key_list_cb_t appkey_list; /*!< The application key index list value */ + esp_ble_mesh_cfg_node_id_status_cb_t node_identity_status; /*!< The node identity status value */ + esp_ble_mesh_cfg_model_app_list_cb_t model_app_list; /*!< The model application key index list value */ + esp_ble_mesh_cfg_kr_phase_status_cb_t kr_phase_status; /*!< The key refresh phase status value */ + esp_ble_mesh_cfg_lpn_pollto_status_cb_t lpn_timeout_status; /*!< The low power node poll timeout status value */ +} esp_ble_mesh_cfg_client_common_cb_param_t; + +/** Configuration Client Model callback parameters */ +typedef struct { + int error_code; /*!< Appropriate error code */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters */ + esp_ble_mesh_cfg_client_common_cb_param_t status_cb; /*!< The config status message callback values */ +} esp_ble_mesh_cfg_client_cb_param_t; + +/** This enum value is the event of Configuration Client Model */ +typedef enum { + ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_CFG_CLIENT_EVT_MAX, +} esp_ble_mesh_cfg_client_cb_event_t; + +/** + * @brief Configuration Server model related context. + */ + +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint16_t pub_addr; /*!< Publish Address */ + uint16_t app_idx; /*!< AppKey Index */ + bool cred_flag; /*!< Friendship Credential Flag */ + uint8_t pub_ttl; /*!< Publish TTL */ + uint8_t pub_period; /*!< Publish Period */ + uint8_t pub_retransmit; /*!< Publish Retransmit */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_mod_pub_set_t; + +/** Parameters of Config Model Subscription Add */ +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint16_t sub_addr; /*!< Subscription Address */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_model_sub_add_t; + +/** Parameters of Config Model Subscription Delete */ +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint16_t sub_addr; /*!< Subscription Address */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_model_sub_delete_t; + +/** Parameters of Config NetKey Add */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t net_key[16]; /*!< NetKey */ +} esp_ble_mesh_state_change_cfg_netkey_add_t; + +/** Parameters of Config NetKey Update */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t net_key[16]; /*!< NetKey */ +} esp_ble_mesh_state_change_cfg_netkey_update_t; + +/** Parameter of Config NetKey Delete */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ +} esp_ble_mesh_state_change_cfg_netkey_delete_t; + +/** Parameters of Config AppKey Add */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t app_idx; /*!< AppKey Index */ + uint8_t app_key[16]; /*!< AppKey */ +} esp_ble_mesh_state_change_cfg_appkey_add_t; + +/** Parameters of Config AppKey Update */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t app_idx; /*!< AppKey Index */ + uint8_t app_key[16]; /*!< AppKey */ +} esp_ble_mesh_state_change_cfg_appkey_update_t; + +/** Parameters of Config AppKey Delete */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint16_t app_idx; /*!< AppKey Index */ +} esp_ble_mesh_state_change_cfg_appkey_delete_t; + +/** Parameters of Config Model App Bind */ +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint16_t app_idx; /*!< AppKey Index */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_model_app_bind_t; + +/** Parameters of Config Model App Unbind */ +typedef struct { + uint16_t element_addr; /*!< Element Address */ + uint16_t app_idx; /*!< AppKey Index */ + uint16_t company_id; /*!< Company ID */ + uint16_t model_id; /*!< Model ID */ +} esp_ble_mesh_state_change_cfg_model_app_unbind_t; + +/** Parameters of Config Key Refresh Phase Set */ +typedef struct { + uint16_t net_idx; /*!< NetKey Index */ + uint8_t kr_phase; /*!< New Key Refresh Phase Transition */ +} esp_ble_mesh_state_change_cfg_kr_phase_set_t; + +/** + * @brief Configuration Server model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_cfg_mod_pub_set_t mod_pub_set; /*!< Config Model Publication Set */ + esp_ble_mesh_state_change_cfg_model_sub_add_t mod_sub_add; /*!< Config Model Subscription Add */ + esp_ble_mesh_state_change_cfg_model_sub_delete_t mod_sub_delete; /*!< Config Model Subscription Delete */ + esp_ble_mesh_state_change_cfg_netkey_add_t netkey_add; /*!< Config NetKey Add */ + esp_ble_mesh_state_change_cfg_netkey_update_t netkey_update; /*!< Config NetKey Update */ + esp_ble_mesh_state_change_cfg_netkey_delete_t netkey_delete; /*!< Config NetKey Delete */ + esp_ble_mesh_state_change_cfg_appkey_add_t appkey_add; /*!< Config AppKey Add */ + esp_ble_mesh_state_change_cfg_appkey_update_t appkey_update; /*!< Config AppKey Update */ + esp_ble_mesh_state_change_cfg_appkey_delete_t appkey_delete; /*!< Config AppKey Delete */ + esp_ble_mesh_state_change_cfg_model_app_bind_t mod_app_bind; /*!< Config Model App Bind */ + esp_ble_mesh_state_change_cfg_model_app_unbind_t mod_app_unbind; /*!< Config Model App Unbind */ + esp_ble_mesh_state_change_cfg_kr_phase_set_t kr_phase_set; /*!< Config Key Refresh Phase Set */ +} esp_ble_mesh_cfg_server_state_change_t; + +/** + * @brief Configuration Server model callback value union + */ +typedef union { + esp_ble_mesh_cfg_server_state_change_t state_change; /*!< ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT */ +} esp_ble_mesh_cfg_server_cb_value_t; + +/** Configuration Server model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the server model structure */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received message */ + esp_ble_mesh_cfg_server_cb_value_t value; /*!< Value of the received configuration messages */ +} esp_ble_mesh_cfg_server_cb_param_t; + +/** This enum value is the event of Configuration Server model */ +typedef enum { + ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT, + ESP_BLE_MESH_CFG_SERVER_EVT_MAX, +} esp_ble_mesh_cfg_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Config Client and Server Model functions. + */ + +/** + * @brief Configuration Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_cfg_client_cb_t)(esp_ble_mesh_cfg_client_cb_event_t event, + esp_ble_mesh_cfg_client_cb_param_t *param); + +/** + * @brief Configuration Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_cfg_server_cb_t)(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Config Client Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_config_client_callback(esp_ble_mesh_cfg_client_cb_t callback); + +/** + * @brief Register BLE Mesh Config Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_config_server_callback(esp_ble_mesh_cfg_server_cb_t callback); + +/** + * @brief Get the value of Config Server Model states using the Config Client Model get messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_opcode_config_client_get_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer to a union, each kind of opcode corresponds to one structure inside. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_get_state_t *get_state); + +/** + * @brief Set the value of the Configuration Server Model states using the Config Client Model set messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_opcode_config_client_set_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer to a union, each kind of opcode corresponds to one structure inside. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_set_state_t *set_state); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_CONFIG_MODEL_API_H_ */ + diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h new file mode 100644 index 000000000..e1758e179 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h @@ -0,0 +1,1305 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Generic Client Model APIs. + */ + +#ifndef _ESP_BLE_MESH_GENERIC_MODEL_API_H_ +#define _ESP_BLE_MESH_GENERIC_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI + * + * @brief Define a new Generic OnOff Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic OnOff Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic OnOff Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_LEVEL_CLI + * + * @brief Define a new Generic Level Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Level Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Level Client Model instance. + */ + +#define ESP_BLE_MESH_MODEL_GEN_LEVEL_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_CLI + * + * @brief Define a new Generic Default Transition Time Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Default Transition + * Time Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Default Transition Time Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_CLI + * + * @brief Define a new Generic Power OnOff Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Power OnOff Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Power OnOff Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_CLI + * + * @brief Define a new Generic Power Level Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Power Level Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Power Level Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_BATTERY_CLI + * + * @brief Define a new Generic Battery Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Battery Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Battery Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_BATTERY_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_LOCATION_CLI + * + * @brief Define a new Generic Location Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Location Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Location Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_LOCATION_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_PROPERTY_CLI + * + * @brief Define a new Generic Property Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Generic Property Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Generic Location Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_PROPERTY_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_PROP_CLI, \ + NULL, cli_pub, cli_data) + +/** + * @brief Bluetooth Mesh Generic Client Model Get and Set parameters structure. + */ + +/** Parameters of Generic OnOff Set. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint8_t onoff; /*!< Target value of Generic OnOff state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_gen_onoff_set_t; + +/** Parameters of Generic Level Set. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int16_t level; /*!< Target value of Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_gen_level_set_t; + +/** Parameters of Generic Delta Set. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int32_t level; /*!< Delta change of Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_gen_delta_set_t; + +/** Parameters of Generic Move Set. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int16_t delta_level; /*!< Delta Level step to calculate Move speed for Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_gen_move_set_t; + +/** Parameter of Generic Default Transition Time Set. */ +typedef struct { + uint8_t trans_time; /*!< The value of the Generic Default Transition Time state */ +} esp_ble_mesh_gen_def_trans_time_set_t; + +/** Parameter of Generic OnPowerUp Set. */ +typedef struct { + uint8_t onpowerup; /*!< The value of the Generic OnPowerUp state */ +} esp_ble_mesh_gen_onpowerup_set_t; + +/** Parameters of Generic Power Level Set. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t power; /*!< Target value of Generic Power Actual state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_gen_power_level_set_t; + +/** Parameter of Generic Power Default Set. */ +typedef struct { + uint16_t power; /*!< The value of the Generic Power Default state */ +} esp_ble_mesh_gen_power_default_set_t; + +/** Parameters of Generic Power Range Set. */ +typedef struct { + uint16_t range_min; /*!< Value of Range Min field of Generic Power Range state */ + uint16_t range_max; /*!< Value of Range Max field of Generic Power Range state */ +} esp_ble_mesh_gen_power_range_set_t; + +/** Parameters of Generic Location Global Set. */ +typedef struct { + int32_t global_latitude; /*!< Global Coordinates (Latitude) */ + int32_t global_longitude; /*!< Global Coordinates (Longitude) */ + int16_t global_altitude; /*!< Global Altitude */ +} esp_ble_mesh_gen_loc_global_set_t; + +/** Parameters of Generic Location Local Set. */ +typedef struct { + int16_t local_north; /*!< Local Coordinates (North) */ + int16_t local_east; /*!< Local Coordinates (East) */ + int16_t local_altitude; /*!< Local Altitude */ + uint8_t floor_number; /*!< Floor Number */ + uint16_t uncertainty; /*!< Uncertainty */ +} esp_ble_mesh_gen_loc_local_set_t; + +/** Parameter of Generic User Property Get. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic User Property */ +} esp_ble_mesh_gen_user_property_get_t; + +/** Parameters of Generic User Property Set. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic User Property */ + struct net_buf_simple *property_value; /*!< Raw value for the User Property */ +} esp_ble_mesh_gen_user_property_set_t; + +/** Parameter of Generic Admin Property Get. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Admin Property */ +} esp_ble_mesh_gen_admin_property_get_t; + +/** Parameters of Generic Admin Property Set. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Admin Property */ + uint8_t user_access; /*!< Enumeration indicating user access */ + struct net_buf_simple *property_value; /*!< Raw value for the Admin Property */ +} esp_ble_mesh_gen_admin_property_set_t; + +/** Parameter of Generic Manufacturer Property Get. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Manufacturer Property */ +} esp_ble_mesh_gen_manufacturer_property_get_t; + +/** Parameters of Generic Manufacturer Property Set. */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Manufacturer Property */ + uint8_t user_access; /*!< Enumeration indicating user access */ +} esp_ble_mesh_gen_manufacturer_property_set_t; + +/** Parameter of Generic Client Properties Get. */ +typedef struct { + uint16_t property_id; /*!< A starting Client Property ID present within an element */ +} esp_ble_mesh_gen_client_properties_get_t; + +/** + * @brief Generic Client Model get message union + */ +typedef union { + esp_ble_mesh_gen_user_property_get_t user_property_get; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET */ + esp_ble_mesh_gen_admin_property_get_t admin_property_get; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET*/ + esp_ble_mesh_gen_manufacturer_property_get_t manufacturer_property_get; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET */ + esp_ble_mesh_gen_client_properties_get_t client_properties_get; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET */ +} esp_ble_mesh_generic_client_get_state_t; + +/** + * @brief Generic Client Model set message union + */ +typedef union { + esp_ble_mesh_gen_onoff_set_t onoff_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET & ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK */ + esp_ble_mesh_gen_level_set_t level_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET & ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK */ + esp_ble_mesh_gen_delta_set_t delta_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET & ESP_BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK */ + esp_ble_mesh_gen_move_set_t move_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET & ESP_BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK */ + esp_ble_mesh_gen_def_trans_time_set_t def_trans_time_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET & ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK */ + esp_ble_mesh_gen_onpowerup_set_t power_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET & ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK */ + esp_ble_mesh_gen_power_level_set_t power_level_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET & ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK */ + esp_ble_mesh_gen_power_default_set_t power_default_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET & ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK */ + esp_ble_mesh_gen_power_range_set_t power_range_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET & ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK */ + esp_ble_mesh_gen_loc_global_set_t loc_global_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET & ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK */ + esp_ble_mesh_gen_loc_local_set_t loc_local_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET & ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK */ + esp_ble_mesh_gen_user_property_set_t user_property_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET & ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK */ + esp_ble_mesh_gen_admin_property_set_t admin_property_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET & ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK */ + esp_ble_mesh_gen_manufacturer_property_set_t manufacturer_property_set; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET & ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET_UNACK */ +} esp_ble_mesh_generic_client_set_state_t; + +/** + * @brief Bluetooth Mesh Generic Client Model Get and Set callback parameters structure. + */ + +/** Parameters of Generic OnOff Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint8_t present_onoff; /*!< Current value of Generic OnOff state */ + uint8_t target_onoff; /*!< Target value of Generic OnOff state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_gen_onoff_status_cb_t; + +/** Parameters of Generic Level Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int16_t present_level; /*!< Current value of Generic Level state */ + int16_t target_level; /*!< Target value of the Generic Level state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_gen_level_status_cb_t; + +/** Parameter of Generic Default Transition Time Status. */ +typedef struct { + uint8_t trans_time; /*!< The value of the Generic Default Transition Time state */ +} esp_ble_mesh_gen_def_trans_time_status_cb_t; + +/** Parameter of Generic OnPowerUp Status. */ +typedef struct { + uint8_t onpowerup; /*!< The value of the Generic OnPowerUp state */ +} esp_ble_mesh_gen_onpowerup_status_cb_t; + +/** Parameters of Generic Power Level Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_power; /*!< Current value of Generic Power Actual state */ + uint16_t target_power; /*!< Target value of Generic Power Actual state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_gen_power_level_status_cb_t; + +/** Parameter of Generic Power Last Status. */ +typedef struct { + uint16_t power; /*!< The value of the Generic Power Last state */ +} esp_ble_mesh_gen_power_last_status_cb_t; + +/** Parameter of Generic Power Default Status. */ +typedef struct { + uint16_t power; /*!< The value of the Generic Default Last state */ +} esp_ble_mesh_gen_power_default_status_cb_t; + +/** Parameters of Generic Power Range Status. */ +typedef struct { + uint8_t status_code; /*!< Status Code for the request message */ + uint16_t range_min; /*!< Value of Range Min field of Generic Power Range state */ + uint16_t range_max; /*!< Value of Range Max field of Generic Power Range state */ +} esp_ble_mesh_gen_power_range_status_cb_t; + +/** Parameters of Generic Battery Status. */ +typedef struct { + u32_t battery_level : 8; /*!< Value of Generic Battery Level state */ + u32_t time_to_discharge : 24; /*!< Value of Generic Battery Time to Discharge state */ + u32_t time_to_charge : 24; /*!< Value of Generic Battery Time to Charge state */ + u32_t flags : 8; /*!< Value of Generic Battery Flags state */ +} esp_ble_mesh_gen_battery_status_cb_t; + +/** Parameters of Generic Location Global Status. */ +typedef struct { + int32_t global_latitude; /*!< Global Coordinates (Latitude) */ + int32_t global_longitude; /*!< Global Coordinates (Longitude) */ + int16_t global_altitude; /*!< Global Altitude */ +} esp_ble_mesh_gen_loc_global_status_cb_t; + +/** Parameters of Generic Location Local Status. */ +typedef struct { + int16_t local_north; /*!< Local Coordinates (North) */ + int16_t local_east; /*!< Local Coordinates (East) */ + int16_t local_altitude; /*!< Local Altitude */ + uint8_t floor_number; /*!< Floor Number */ + uint16_t uncertainty; /*!< Uncertainty */ +} esp_ble_mesh_gen_loc_local_status_cb_t; + +/** Parameter of Generic User Properties Status. */ +typedef struct { + struct net_buf_simple *property_ids; /*!< Buffer contains a sequence of N User Property IDs */ +} esp_ble_mesh_gen_user_properties_status_cb_t; + +/** Parameters of Generic User Property Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID identifying a Generic User Property */ + uint8_t user_access; /*!< Enumeration indicating user access (optional) */ + struct net_buf_simple *property_value; /*!< Raw value for the User Property (C.1) */ +} esp_ble_mesh_gen_user_property_status_cb_t; + +/** Parameter of Generic Admin Properties Status. */ +typedef struct { + struct net_buf_simple *property_ids; /*!< Buffer contains a sequence of N Admin Property IDs */ +} esp_ble_mesh_gen_admin_properties_status_cb_t; + +/** Parameters of Generic Admin Property Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID identifying a Generic Admin Property */ + uint8_t user_access; /*!< Enumeration indicating user access (optional) */ + struct net_buf_simple *property_value; /*!< Raw value for the Admin Property (C.1) */ +} esp_ble_mesh_gen_admin_property_status_cb_t; + +/** Parameter of Generic Manufacturer Properties Status. */ +typedef struct { + struct net_buf_simple *property_ids; /*!< Buffer contains a sequence of N Manufacturer Property IDs */ +} esp_ble_mesh_gen_manufacturer_properties_status_cb_t; + +/** Parameters of Generic Manufacturer Property Status. */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID identifying a Generic Manufacturer Property */ + uint8_t user_access; /*!< Enumeration indicating user access (optional) */ + struct net_buf_simple *property_value; /*!< Raw value for the Manufacturer Property (C.1) */ +} esp_ble_mesh_gen_manufacturer_property_status_cb_t; + +/** Parameter of Generic Client Properties Status. */ +typedef struct { + struct net_buf_simple *property_ids; /*!< Buffer contains a sequence of N Client Property IDs */ +} esp_ble_mesh_gen_client_properties_status_cb_t; + +/** + * @brief Generic Client Model received message union + */ +typedef union { + esp_ble_mesh_gen_onoff_status_cb_t onoff_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS */ + esp_ble_mesh_gen_level_status_cb_t level_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS */ + esp_ble_mesh_gen_def_trans_time_status_cb_t def_trans_time_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS */ + esp_ble_mesh_gen_onpowerup_status_cb_t onpowerup_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS */ + esp_ble_mesh_gen_power_level_status_cb_t power_level_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS */ + esp_ble_mesh_gen_power_last_status_cb_t power_last_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS */ + esp_ble_mesh_gen_power_default_status_cb_t power_default_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS */ + esp_ble_mesh_gen_power_range_status_cb_t power_range_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS */ + esp_ble_mesh_gen_battery_status_cb_t battery_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS */ + esp_ble_mesh_gen_loc_global_status_cb_t location_global_status; /*!< For ESP_BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS */ + esp_ble_mesh_gen_loc_local_status_cb_t location_local_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS */ + esp_ble_mesh_gen_user_properties_status_cb_t user_properties_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS */ + esp_ble_mesh_gen_user_property_status_cb_t user_property_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS */ + esp_ble_mesh_gen_admin_properties_status_cb_t admin_properties_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS */ + esp_ble_mesh_gen_admin_property_status_cb_t admin_property_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS */ + esp_ble_mesh_gen_manufacturer_properties_status_cb_t manufacturer_properties_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS */ + esp_ble_mesh_gen_manufacturer_property_status_cb_t manufacturer_property_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS */ + esp_ble_mesh_gen_client_properties_status_cb_t client_properties_status; /*!< ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS */ +} esp_ble_mesh_gen_client_status_cb_t; + +/** Generic Client Model callback parameters */ +typedef struct { + int error_code; /*!< Appropriate error code */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + esp_ble_mesh_gen_client_status_cb_t status_cb; /*!< The generic status message callback values */ +} esp_ble_mesh_generic_client_cb_param_t; + +/** This enum value is the event of Generic Client Model */ +typedef enum { + ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX, +} esp_ble_mesh_generic_client_cb_event_t; + +/** + * @brief Bluetooth Mesh Generic Client Model function. + */ + +/** + * @brief Generic Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_generic_client_cb_t)(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Generic Client Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_generic_client_callback(esp_ble_mesh_generic_client_cb_t callback); + +/** + * @brief Get the value of Generic Server Model states using the Generic Client Model get messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_generic_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer to generic get message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_generic_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_generic_client_get_state_t *get_state); + +/** + * @brief Set the value of Generic Server Model states using the Generic Client Model set messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_generic_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer to generic set message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_generic_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_generic_client_set_state_t *set_state); + +/** + * @brief Generic Server Models related context. + */ + +/** @def ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV + * + * @brief Define a new Generic OnOff Server Model. + * + * @note 1. The Generic OnOff Server Model is a root model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_onoff_srv_t. + * + * @return New Generic OnOff Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_LEVEL_SRV + * + * @brief Define a new Generic Level Server Model. + * + * @note 1. The Generic Level Server Model is a root model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_level_srv_t. + * + * @return New Generic Level Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_LEVEL_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_LEVEL_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_SRV + * + * @brief Define a new Generic Default Transition Time Server Model. + * + * @note 1. The Generic Default Transition Time Server Model is a root model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_def_trans_time_srv_t. + * + * @return New Generic Default Transition Time Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_SRV + * + * @brief Define a new Generic Power OnOff Server Model. + * + * @note 1. The Generic Power OnOff Server model extends the Generic OnOff Server + * model. When this model is present on an element, the corresponding + * Generic Power OnOff Setup Server model shall also be present. + * 2. This model may be used to represent a variety of devices that do not + * fit any of the model descriptions that have been defined but support + * the generic properties of On/Off. + * 3. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_power_onoff_srv_t. + * + * @return New Generic Power OnOff Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_SETUP_SRV + * + * @brief Define a new Generic Power OnOff Setup Server Model. + * + * @note 1. The Generic Power OnOff Setup Server model extends the Generic Power + * OnOff Server model and the Generic Default Transition Time Server model. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_power_onoff_setup_srv_t. + * + * @return New Generic Power OnOff Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_ONOFF_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_SRV + * + * @brief Define a new Generic Power Level Server Model. + * + * @note 1. The Generic Power Level Server model extends the Generic Power OnOff + * Server model and the Generic Level Server model. When this model is + * present on an Element, the corresponding Generic Power Level Setup + * Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_power_level_srv_t. + * + * @return New Generic Power Level Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_SETUP_SRV + * + * @brief Define a new Generic Power Level Setup Server Model. + * + * @note 1. The Generic Power Level Setup Server model extends the Generic Power + * Level Server model and the Generic Power OnOff Setup Server model. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_power_level_setup_srv_t. + * + * @return New Generic Power Level Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_POWER_LEVEL_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_BATTERY_SRV + * + * @brief Define a new Generic Battery Server Model. + * + * @note 1. The Generic Battery Server Model is a root model. + * 2. This model shall support model publication and model subscription. + * 3. The model may be used to represent an element that is powered by a battery. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_battery_srv_t. + * + * @return New Generic Battery Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_BATTERY_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_BATTERY_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_LOCATION_SRV + * + * @brief Define a new Generic Location Server Model. + * + * @note 1. The Generic Location Server model is a root model. When this model + * is present on an Element, the corresponding Generic Location Setup + * Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * 3. The model may be used to represent an element that knows its + * location (global or local). + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_location_srv_t. + * + * @return New Generic Location Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_LOCATION_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_LOCATION_SETUP_SRV + * + * @brief Define a new Generic Location Setup Server Model. + * + * @note 1. The Generic Location Setup Server model extends the Generic Location + * Server model. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_location_setup_srv_t. + * + * @return New Generic Location Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_LOCATION_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_USER_PROP_SRV + * + * @brief Define a new Generic User Property Server Model. + * + * @note 1. The Generic User Property Server model is a root model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_user_prop_srv_t. + * + * @return New Generic User Property Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_USER_PROP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_ADMIN_PROP_SRV + * + * @brief Define a new Generic Admin Property Server Model. + * + * @note 1. The Generic Admin Property Server model extends the Generic User + * Property Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_admin_prop_srv_t. + * + * @return New Generic Admin Property Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_ADMIN_PROP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_MANUFACTURER_PROP_SRV + * + * @brief Define a new Generic Manufacturer Property Server Model. + * + * @note 1. The Generic Manufacturer Property Server model extends the Generic + * User Property Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_manu_prop_srv_t. + * + * @return New Generic Manufacturer Property Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_MANUFACTURER_PROP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_GEN_CLIENT_PROP_SRV + * + * @brief Define a new Generic User Property Server Model. + * + * @note 1. The Generic Client Property Server model is a root model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_gen_client_prop_srv_t. + * + * @return New Generic Client Property Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_GEN_CLIENT_PROP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV, \ + NULL, srv_pub, srv_data) + +/** Parameters of Generic OnOff state */ +typedef struct { + uint8_t onoff; /*!< The present value of the Generic OnOff state */ + uint8_t target_onoff; /*!< The target value of the Generic OnOff state */ +} esp_ble_mesh_gen_onoff_state_t; + +/** User data of Generic OnOff Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic OnOff Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_onoff_state_t state; /*!< Parameters of the Generic OnOff state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ +} esp_ble_mesh_gen_onoff_srv_t; + +/** Parameters of Generic Level state */ +typedef struct { + int16_t level; /*!< The present value of the Generic Level state */ + int16_t target_level; /*!< The target value of the Generic Level state */ + + /** + * When a new transaction starts, level should be set to last_last, and use + * "level + incoming delta" to calculate the target level. In another word, + * "last_level" is used to record "level" of the last transaction, and + * "last_delta" is used to record the previously received delta_level value. + */ + int16_t last_level; /*!< The last value of the Generic Level state */ + int32_t last_delta; /*!< The last delta change of the Generic Level state */ + + bool move_start; /*!< Indicate if the transition of the Generic Level state has been started */ + bool positive; /*!< Indicate if the transition is positive or negative */ +} esp_ble_mesh_gen_level_state_t; + +/** User data of Generic Level Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Level Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_level_state_t state; /*!< Parameters of the Generic Level state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_level; /*!< Delta change value of level state transition */ +} esp_ble_mesh_gen_level_srv_t; + +/** Parameter of Generic Default Transition Time state */ +typedef struct { + uint8_t trans_time; /*!< The value of the Generic Default Transition Time state */ +} esp_ble_mesh_gen_def_trans_time_state_t; + +/** User data of Generic Default Transition Time Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Default Transition Time Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_def_trans_time_state_t state; /*!< Parameters of the Generic Default Transition Time state */ +} esp_ble_mesh_gen_def_trans_time_srv_t; + +/** Parameter of Generic OnPowerUp state */ +typedef struct { + uint8_t onpowerup; /*!< The value of the Generic OnPowerUp state */ +} esp_ble_mesh_gen_onpowerup_state_t; + +/** User data of Generic Power OnOff Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Power OnOff Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_onpowerup_state_t *state; /*!< Parameters of the Generic OnPowerUp state */ +} esp_ble_mesh_gen_power_onoff_srv_t; + +/** User data of Generic Power OnOff Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Power OnOff Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_onpowerup_state_t *state; /*!< Parameters of the Generic OnPowerUp state */ +} esp_ble_mesh_gen_power_onoff_setup_srv_t; + +/** Parameters of Generic Power Level state */ +typedef struct { + uint16_t power_actual; /*!< The present value of the Generic Power Actual state */ + uint16_t target_power_actual; /*!< The target value of the Generic Power Actual state */ + + uint16_t power_last; /*!< The value of the Generic Power Last state */ + uint16_t power_default; /*!< The value of the Generic Power Default state */ + + uint8_t status_code; /*!< The status code of setting Generic Power Range state */ + uint16_t power_range_min; /*!< The minimum value of the Generic Power Range state */ + uint16_t power_range_max; /*!< The maximum value of the Generic Power Range state */ +} esp_ble_mesh_gen_power_level_state_t; + +/** User data of Generic Power Level Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Power Level Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_power_level_state_t *state; /*!< Parameters of the Generic Power Level state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_level; /*!< Delta change value of level state transition */ +} esp_ble_mesh_gen_power_level_srv_t; + +/** User data of Generic Power Level Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Power Level Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_power_level_state_t *state; /*!< Parameters of the Generic Power Level state */ +} esp_ble_mesh_gen_power_level_setup_srv_t; + +/** Parameters of Generic Battery state */ +typedef struct { + uint32_t battery_level : 8, /*!< The value of the Generic Battery Level state */ + time_to_discharge : 24; /*!< The value of the Generic Battery Time to Discharge state */ + uint32_t time_to_charge : 24, /*!< The value of the Generic Battery Time to Charge state */ + battery_flags : 8; /*!< The value of the Generic Battery Flags state */ +} esp_ble_mesh_gen_battery_state_t; + +/** User data of Generic Battery Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Battery Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_battery_state_t state; /*!< Parameters of the Generic Battery state */ +} esp_ble_mesh_gen_battery_srv_t; + +/** Parameters of Generic Location state */ +typedef struct { + int32_t global_latitude; /*!< The value of the Global Latitude field */ + int32_t global_longitude; /*!< The value of the Global Longitude field */ + int16_t global_altitude; /*!< The value of the Global Altitude field */ + int16_t local_north; /*!< The value of the Local North field */ + int16_t local_east; /*!< The value of the Local East field */ + int16_t local_altitude; /*!< The value of the Local Altitude field */ + uint8_t floor_number; /*!< The value of the Floor Number field */ + uint16_t uncertainty; /*!< The value of the Uncertainty field */ +} esp_ble_mesh_gen_location_state_t; + +/** User data of Generic Location Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Location Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_location_state_t *state; /*!< Parameters of the Generic Location state */ +} esp_ble_mesh_gen_location_srv_t; + +/** User data of Generic Location Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Location Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_gen_location_state_t *state; /*!< Parameters of the Generic Location state */ +} esp_ble_mesh_gen_location_setup_srv_t; + +/** This enum value is the access value of Generic User Property */ +typedef enum { + ESP_BLE_MESH_GEN_USER_ACCESS_PROHIBIT, + ESP_BLE_MESH_GEN_USER_ACCESS_READ, + ESP_BLE_MESH_GEN_USER_ACCESS_WRITE, + ESP_BLE_MESH_GEN_USER_ACCESS_READ_WRITE, +} esp_ble_mesh_gen_user_prop_access_t; + +/** This enum value is the access value of Generic Admin Property */ +typedef enum { + ESP_BLE_MESH_GEN_ADMIN_NOT_USER_PROP, + ESP_BLE_MESH_GEN_ADMIN_ACCESS_READ, + ESP_BLE_MESH_GEN_ADMIN_ACCESS_WRITE, + ESP_BLE_MESH_GEN_ADMIN_ACCESS_READ_WRITE, +} esp_ble_mesh_gen_admin_prop_access_t; + +/** This enum value is the access value of Generic Manufacturer Property */ +typedef enum { + ESP_BLE_MESH_GEN_MANU_NOT_USER_PROP, + ESP_BLE_MESH_GEN_MANU_ACCESS_READ, +} esp_ble_mesh_gen_manu_prop_access_t; + +/** Parameters of Generic Property states */ +typedef struct { + uint16_t id; /*!< The value of User/Admin/Manufacturer Property ID */ + uint8_t user_access; /*!< The value of User Access field */ + uint8_t admin_access; /*!< The value of Admin Access field */ + uint8_t manu_access; /*!< The value of Manufacturer Access field */ + struct net_buf_simple *val; /*!< The value of User/Admin/Manufacturer Property */ +} esp_ble_mesh_generic_property_t; + +/** User data of Generic User Property Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic User Property Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + uint8_t property_count; /*!< Generic User Property count */ + esp_ble_mesh_generic_property_t *properties; /*!< Parameters of the Generic User Property state */ +} esp_ble_mesh_gen_user_prop_srv_t; + +/** User data of Generic Admin Property Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Admin Property Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + uint8_t property_count; /*!< Generic Admin Property count */ + esp_ble_mesh_generic_property_t *properties; /*!< Parameters of the Generic Admin Property state */ +} esp_ble_mesh_gen_admin_prop_srv_t; + +/** User data of Generic Manufacturer Property Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Manufacturer Property Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + uint8_t property_count; /*!< Generic Manufacturer Property count */ + esp_ble_mesh_generic_property_t *properties; /*!< Parameters of the Generic Manufacturer Property state */ +} esp_ble_mesh_gen_manu_prop_srv_t; + +/** User data of Generic Client Property Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Generic Client Property Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + uint8_t id_count; /*!< Generic Client Property ID count */ + uint16_t *property_ids; /*!< Parameters of the Generic Client Property state */ +} esp_ble_mesh_gen_client_prop_srv_t; + +/** Parameter of Generic OnOff Set state change event */ +typedef struct { + uint8_t onoff; /*!< The value of Generic OnOff state */ +} esp_ble_mesh_state_change_gen_onoff_set_t; + +/** Parameter of Generic Level Set state change event */ +typedef struct { + int16_t level; /*!< The value of Generic Level state */ +} esp_ble_mesh_state_change_gen_level_set_t; + +/** Parameter of Generic Delta Set state change event */ +typedef struct { + int16_t level; /*!< The value of Generic Level state */ +} esp_ble_mesh_state_change_gen_delta_set_t; + +/** Parameter of Generic Move Set state change event */ +typedef struct { + int16_t level; /*!< The value of Generic Level state */ +} esp_ble_mesh_state_change_gen_move_set_t; + +/** Parameter of Generic Default Transition Time Set state change event */ +typedef struct { + uint8_t trans_time; /*!< The value of Generic Default Transition Time state */ +} esp_ble_mesh_state_change_gen_def_trans_time_set_t; + +/** Parameter of Generic OnPowerUp Set state change event */ +typedef struct { + uint8_t onpowerup; /*!< The value of Generic OnPowerUp state */ +} esp_ble_mesh_state_change_gen_onpowerup_set_t; + +/** Parameter of Generic Power Level Set state change event */ +typedef struct { + uint16_t power; /*!< The value of Generic Power Actual state */ +} esp_ble_mesh_state_change_gen_power_level_set_t; + +/** Parameter of Generic Power Default Set state change event */ +typedef struct { + uint16_t power; /*!< The value of Generic Power Default state */ +} esp_ble_mesh_state_change_gen_power_default_set_t; + +/** Parameters of Generic Power Range Set state change event */ +typedef struct { + uint16_t range_min; /*!< The minimum value of Generic Power Range state */ + uint16_t range_max; /*!< The maximum value of Generic Power Range state */ +} esp_ble_mesh_state_change_gen_power_range_set_t; + +/** Parameters of Generic Location Global Set state change event */ +typedef struct { + int32_t latitude; /*!< The Global Latitude value of Generic Location state */ + int32_t longitude; /*!< The Global Longitude value of Generic Location state */ + int16_t altitude; /*!< The Global Altitude value of Generic Location state */ +} esp_ble_mesh_state_change_gen_loc_global_set_t; + +/** Parameters of Generic Location Local Set state change event */ +typedef struct { + int16_t north; /*!< The Local North value of Generic Location state */ + int16_t east; /*!< The Local East value of Generic Location state */ + int16_t altitude; /*!< The Local Altitude value of Generic Location state */ + uint8_t floor_number; /*!< The Floor Number value of Generic Location state */ + uint16_t uncertainty; /*!< The Uncertainty value of Generic Location state */ +} esp_ble_mesh_state_change_gen_loc_local_set_t; + +/** Parameters of Generic User Property Set state change event */ +typedef struct { + uint16_t id; /*!< The property id of Generic User Property state */ + struct net_buf_simple *value; /*!< The property value of Generic User Property state */ +} esp_ble_mesh_state_change_gen_user_property_set_t; + +/** Parameters of Generic Admin Property Set state change event */ +typedef struct { + uint16_t id; /*!< The property id of Generic Admin Property state */ + uint8_t access; /*!< The property access of Generic Admin Property state */ + struct net_buf_simple *value; /*!< The property value of Generic Admin Property state */ +} esp_ble_mesh_state_change_gen_admin_property_set_t; + +/** Parameters of Generic Manufacturer Property Set state change event */ +typedef struct { + uint16_t id; /*!< The property id of Generic Manufacturer Property state */ + uint8_t access; /*!< The property value of Generic Manufacturer Property state */ +} esp_ble_mesh_state_change_gen_manu_property_set_t; + +/** + * @brief Generic Server Model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_gen_onoff_set_t onoff_set; /*!< Generic OnOff Set */ + esp_ble_mesh_state_change_gen_level_set_t level_set; /*!< Generic Level Set */ + esp_ble_mesh_state_change_gen_delta_set_t delta_set; /*!< Generic Delta Set */ + esp_ble_mesh_state_change_gen_move_set_t move_set; /*!< Generic Move Set */ + esp_ble_mesh_state_change_gen_def_trans_time_set_t def_trans_time_set; /*!< Generic Default Transition Time Set */ + esp_ble_mesh_state_change_gen_onpowerup_set_t onpowerup_set; /*!< Generic OnPowerUp Set */ + esp_ble_mesh_state_change_gen_power_level_set_t power_level_set; /*!< Generic Power Level Set */ + esp_ble_mesh_state_change_gen_power_default_set_t power_default_set; /*!< Generic Power Default Set */ + esp_ble_mesh_state_change_gen_power_range_set_t power_range_set; /*!< Generic Power Range Set */ + esp_ble_mesh_state_change_gen_loc_global_set_t loc_global_set; /*!< Generic Location Global Set */ + esp_ble_mesh_state_change_gen_loc_local_set_t loc_local_set; /*!< Generic Location Local Set */ + esp_ble_mesh_state_change_gen_user_property_set_t user_property_set; /*!< Generic User Property Set */ + esp_ble_mesh_state_change_gen_admin_property_set_t admin_property_set; /*!< Generic Admin Property Set */ + esp_ble_mesh_state_change_gen_manu_property_set_t manu_property_set; /*!< Generic Manufacturer Property Set */ +} esp_ble_mesh_generic_server_state_change_t; + +/** Context of the received Generic User Property Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic User Property */ +} esp_ble_mesh_server_recv_gen_user_property_get_t; + +/** Context of the received Generic Admin Property Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Admin Property */ +} esp_ble_mesh_server_recv_gen_admin_property_get_t; + +/** Context of the received Generic Manufacturer Property message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Manufacturer Property */ +} esp_ble_mesh_server_recv_gen_manufacturer_property_get_t; + +/** Context of the received Generic Client Properties Get message */ +typedef struct { + uint16_t property_id; /*!< A starting Client Property ID present within an element */ +} esp_ble_mesh_server_recv_gen_client_properties_get_t; + +/** + * @brief Generic Server Model received get message union + */ +typedef union { + esp_ble_mesh_server_recv_gen_user_property_get_t user_property; /*!< Generic User Property Get */ + esp_ble_mesh_server_recv_gen_admin_property_get_t admin_property; /*!< Generic Admin Property Get */ + esp_ble_mesh_server_recv_gen_manufacturer_property_get_t manu_property; /*!< Generic Manufacturer Property Get */ + esp_ble_mesh_server_recv_gen_client_properties_get_t client_properties; /*!< Generic Client Properties Get */ +} esp_ble_mesh_generic_server_recv_get_msg_t; + +/** Context of the received Generic OnOff Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint8_t onoff; /*!< Target value of Generic OnOff state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_gen_onoff_set_t; + +/** Context of the received Generic Level Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int16_t level; /*!< Target value of Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_gen_level_set_t; + +/** Context of the received Generic Delta Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int32_t delta_level; /*!< Delta change of Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_gen_delta_set_t; + +/** Context of the received Generic Move Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + int16_t delta_level; /*!< Delta Level step to calculate Move speed for Generic Level state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_gen_move_set_t; + +/** Context of the received Generic Default Transition Time Set message */ +typedef struct { + uint8_t trans_time; /*!< The value of the Generic Default Transition Time state */ +} esp_ble_mesh_server_recv_gen_def_trans_time_set_t; + +/** Context of the received Generic OnPowerUp Set message */ +typedef struct { + uint8_t onpowerup; /*!< The value of the Generic OnPowerUp state */ +} esp_ble_mesh_server_recv_gen_onpowerup_set_t; + +/** Context of the received Generic Power Level Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t power; /*!< Target value of Generic Power Actual state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_gen_power_level_set_t; + +/** Context of the received Generic Power Default Set message */ +typedef struct { + uint16_t power; /*!< The value of the Generic Power Default state */ +} esp_ble_mesh_server_recv_gen_power_default_set_t; + +/** Context of the received Generic Power Range Set message */ +typedef struct { + uint16_t range_min; /*!< Value of Range Min field of Generic Power Range state */ + uint16_t range_max; /*!< Value of Range Max field of Generic Power Range state */ +} esp_ble_mesh_server_recv_gen_power_range_set_t; + +/** Context of the received Generic Location Global Set message */ +typedef struct { + int32_t global_latitude; /*!< Global Coordinates (Latitude) */ + int32_t global_longitude; /*!< Global Coordinates (Longitude) */ + int16_t global_altitude; /*!< Global Altitude */ +} esp_ble_mesh_server_recv_gen_loc_global_set_t; + +/** Context of the received Generic Location Local Set message */ +typedef struct { + int16_t local_north; /*!< Local Coordinates (North) */ + int16_t local_east; /*!< Local Coordinates (East) */ + int16_t local_altitude; /*!< Local Altitude */ + uint8_t floor_number; /*!< Floor Number */ + uint16_t uncertainty; /*!< Uncertainty */ +} esp_ble_mesh_server_recv_gen_loc_local_set_t; + +/** Context of the received Generic User Property Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic User Property */ + struct net_buf_simple *property_value; /*!< Raw value for the User Property */ +} esp_ble_mesh_server_recv_gen_user_property_set_t; + +/** Context of the received Generic Admin Property Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Admin Property */ + uint8_t user_access; /*!< Enumeration indicating user access */ + struct net_buf_simple *property_value; /*!< Raw value for the Admin Property */ +} esp_ble_mesh_server_recv_gen_admin_property_set_t; + +/** Context of the received Generic Manufacturer Property Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Generic Manufacturer Property */ + uint8_t user_access; /*!< Enumeration indicating user access */ +} esp_ble_mesh_server_recv_gen_manufacturer_property_set_t; + +/** + * @brief Generic Server Model received set message union + */ +typedef union { + esp_ble_mesh_server_recv_gen_onoff_set_t onoff; /*!< Generic OnOff Set/Generic OnOff Set Unack */ + esp_ble_mesh_server_recv_gen_level_set_t level; /*!< Generic Level Set/Generic Level Set Unack */ + esp_ble_mesh_server_recv_gen_delta_set_t delta; /*!< Generic Delta Set/Generic Delta Set Unack */ + esp_ble_mesh_server_recv_gen_move_set_t move; /*!< Generic Move Set/Generic Move Set Unack */ + esp_ble_mesh_server_recv_gen_def_trans_time_set_t def_trans_time; /*!< Generic Default Transition Time Set/Generic Default Transition Time Set Unack */ + esp_ble_mesh_server_recv_gen_onpowerup_set_t onpowerup; /*!< Generic OnPowerUp Set/Generic OnPowerUp Set Unack */ + esp_ble_mesh_server_recv_gen_power_level_set_t power_level; /*!< Generic Power Level Set/Generic Power Level Set Unack */ + esp_ble_mesh_server_recv_gen_power_default_set_t power_default; /*!< Generic Power Default Set/Generic Power Default Set Unack */ + esp_ble_mesh_server_recv_gen_power_range_set_t power_range; /*!< Generic Power Range Set/Generic Power Range Set Unack */ + esp_ble_mesh_server_recv_gen_loc_global_set_t location_global; /*!< Generic Location Global Set/Generic Location Global Set Unack */ + esp_ble_mesh_server_recv_gen_loc_local_set_t location_local; /*!< Generic Location Local Set/Generic Location Local Set Unack */ + esp_ble_mesh_server_recv_gen_user_property_set_t user_property; /*!< Generic User Property Set/Generic User Property Set Unack */ + esp_ble_mesh_server_recv_gen_admin_property_set_t admin_property; /*!< Generic Admin Property Set/Generic Admin Property Set Unack */ + esp_ble_mesh_server_recv_gen_manufacturer_property_set_t manu_property; /*!< Generic Manufacturer Property Set/Generic Manufacturer Property Set Unack */ +} esp_ble_mesh_generic_server_recv_set_msg_t; + +/** + * @brief Generic Server Model callback value union + */ +typedef union { + esp_ble_mesh_generic_server_state_change_t state_change; /*!< ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT */ + esp_ble_mesh_generic_server_recv_get_msg_t get; /*!< ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT */ + esp_ble_mesh_generic_server_recv_set_msg_t set; /*!< ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT */ +} esp_ble_mesh_generic_server_cb_value_t; + +/** Generic Server Model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Generic Server Models */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received messages */ + esp_ble_mesh_generic_server_cb_value_t value; /*!< Value of the received Generic Messages */ +} esp_ble_mesh_generic_server_cb_param_t; + +/** This enum value is the event of Generic Server Model */ +typedef enum { + /** + * 1. When get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, no event will be + * callback to the application layer when Generic Get messages are received. + * 2. When set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, this event will + * be callback to the application layer when Generic Set/Set Unack messages + * are received. + */ + ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT, + /** + * When get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Generic Get messages are received. + */ + ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT, + /** + * When set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Generic Set/Set Unack messages are received. + */ + ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT, + ESP_BLE_MESH_GENERIC_SERVER_EVT_MAX, +} esp_ble_mesh_generic_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Generic Server Model function. + */ + +/** + * @brief Generic Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_generic_server_cb_t)(esp_ble_mesh_generic_server_cb_event_t event, + esp_ble_mesh_generic_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Generic Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_generic_server_callback(esp_ble_mesh_generic_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_GENERIC_MODEL_API_H_ */ + diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_health_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_health_model_api.h new file mode 100644 index 000000000..e46ae79e3 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_health_model_api.h @@ -0,0 +1,414 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_BLE_MESH_HEALTH_MODEL_API_H_ +#define _ESP_BLE_MESH_HEALTH_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_HEALTH_SRV + * + * @brief Define a new Health Server Model. + * + * @note The Health Server Model can only be included by a Primary Element. + * + * @param srv Pointer to the unique struct esp_ble_mesh_health_srv_t. + * @param pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * + * @return New Health Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_HEALTH_SRV(srv, pub) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_HEALTH_SRV, \ + NULL, pub, srv) + +/** @def ESP_BLE_MESH_MODEL_HEALTH_CLI + * + * @brief Define a new Health Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Health Client Model. + * + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Health Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_HEALTH_CLI(cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_HEALTH_CLI, \ + NULL, NULL, cli_data) + +/** @def ESP_BLE_MESH_HEALTH_PUB_DEFINE + * + * A helper to define a health publication context + * + * @param _name Name given to the publication context variable. + * @param _max Maximum number of faults the element can have. + * @param _role Role of the device which contains the model. + */ +#define ESP_BLE_MESH_HEALTH_PUB_DEFINE(_name, _max, _role) \ + ESP_BLE_MESH_MODEL_PUB_DEFINE(_name, (1 + 3 + (_max)), _role) + +/** + * SIG identifier of Health Fault Test. + * 0x01 ~ 0xFF: Vendor Specific Test. + */ +#define ESP_BLE_MESH_HEALTH_STANDARD_TEST 0x00 + +/** + * Fault values of Health Fault Test. + * 0x33 ~ 0x7F: Reserved for Future Use. + * 0x80 ~ 0xFF: Vendor Specific Warning/Error. + */ +#define ESP_BLE_MESH_NO_FAULT 0x00 +#define ESP_BLE_MESH_BATTERY_LOW_WARNING 0x01 +#define ESP_BLE_MESH_BATTERY_LOW_ERROR 0x02 +#define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_LOW_WARNING 0x03 +#define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_LOW_ERROR 0x04 +#define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_HIGH_WARNING 0x05 +#define ESP_BLE_MESH_SUPPLY_VOLTAGE_TOO_HIGH_ERROR 0x06 +#define ESP_BLE_MESH_POWER_SUPPLY_INTERRUPTED_WARNING 0x07 +#define ESP_BLE_MESH_POWER_SUPPLY_INTERRUPTED_ERROR 0x08 +#define ESP_BLE_MESH_NO_LOAD_WARNING 0x09 +#define ESP_BLE_MESH_NO_LOAD_ERROR 0x0A +#define ESP_BLE_MESH_OVERLOAD_WARNING 0x0B +#define ESP_BLE_MESH_OVERLOAD_ERROR 0x0C +#define ESP_BLE_MESH_OVERHEAT_WARNING 0x0D +#define ESP_BLE_MESH_OVERHEAT_ERROR 0x0E +#define ESP_BLE_MESH_CONDENSATION_WARNING 0x0F +#define ESP_BLE_MESH_CONDENSATION_ERROR 0x10 +#define ESP_BLE_MESH_VIBRATION_WARNING 0x11 +#define ESP_BLE_MESH_VIBRATION_ERROR 0x12 +#define ESP_BLE_MESH_CONFIGURATION_WARNING 0x13 +#define ESP_BLE_MESH_CONFIGURATION_ERROR 0x14 +#define ESP_BLE_MESH_ELEMENT_NOT_CALIBRATED_WARNING 0x15 +#define ESP_BLE_MESH_ELEMENT_NOT_CALIBRATED_ERROR 0x16 +#define ESP_BLE_MESH_MEMORY_WARNING 0x17 +#define ESP_BLE_MESH_MEMORY_ERROR 0x18 +#define ESP_BLE_MESH_SELF_TEST_WARNING 0x19 +#define ESP_BLE_MESH_SELF_TEST_ERROR 0x1A +#define ESP_BLE_MESH_INPUT_TOO_LOW_WARNING 0x1B +#define ESP_BLE_MESH_INPUT_TOO_LOW_ERROR 0x1C +#define ESP_BLE_MESH_INPUT_TOO_HIGH_WARNING 0x1D +#define ESP_BLE_MESH_INPUT_TOO_HIGH_ERROR 0x1E +#define ESP_BLE_MESH_INPUT_NO_CHANGE_WARNING 0x1F +#define ESP_BLE_MESH_INPUT_NO_CHANGE_ERROR 0x20 +#define ESP_BLE_MESH_ACTUATOR_BLOCKED_WARNING 0x21 +#define ESP_BLE_MESH_ACTUATOR_BLOCKED_ERROR 0x22 +#define ESP_BLE_MESH_HOUSING_OPENED_WARNING 0x23 +#define ESP_BLE_MESH_HOUSING_OPENED_ERROR 0x24 +#define ESP_BLE_MESH_TAMPER_WARNING 0x25 +#define ESP_BLE_MESH_TAMPER_ERROR 0x26 +#define ESP_BLE_MESH_DEVICE_MOVED_WARNING 0x27 +#define ESP_BLE_MESH_DEVICE_MOVED_ERROR 0x28 +#define ESP_BLE_MESH_DEVICE_DROPPED_WARNING 0x29 +#define ESP_BLE_MESH_DEVICE_DROPPED_ERROR 0x2A +#define ESP_BLE_MESH_OVERFLOW_WARNING 0x2B +#define ESP_BLE_MESH_OVERFLOW_ERROR 0x2C +#define ESP_BLE_MESH_EMPTY_WARNING 0x2D +#define ESP_BLE_MESH_EMPTY_ERROR 0x2E +#define ESP_BLE_MESH_INTERNAL_BUS_WARNING 0x2F +#define ESP_BLE_MESH_INTERNAL_BUS_ERROR 0x30 +#define ESP_BLE_MESH_MECHANISM_JAMMED_WARNING 0x31 +#define ESP_BLE_MESH_MECHANISM_JAMMED_ERROR 0x32 + +/** ESP BLE Mesh Health Server callback */ +typedef struct { + /** Clear health registered faults. Initialized by the stack. */ + esp_ble_mesh_cb_t fault_clear; + + /** Run a specific health test. Initialized by the stack. */ + esp_ble_mesh_cb_t fault_test; + + /** Health attention on callback. Initialized by the stack. */ + esp_ble_mesh_cb_t attention_on; + + /** Health attention off callback. Initialized by the stack. */ + esp_ble_mesh_cb_t attention_off; +} esp_ble_mesh_health_srv_cb_t; + +#define ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE 32 + +/** ESP BLE Mesh Health Server test Context */ +typedef struct { + uint8_t id_count; /*!< Number of Health self-test ID */ + const uint8_t *test_ids; /*!< Array of Health self-test IDs */ + uint16_t company_id; /*!< Company ID used to identify the Health Fault state */ + uint8_t prev_test_id; /*!< Current test ID of the health fault test */ + uint8_t current_faults[ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE]; /*!< Array of current faults */ + uint8_t registered_faults[ESP_BLE_MESH_HEALTH_FAULT_ARRAY_SIZE]; /*!< Array of registered faults */ +} __attribute__((packed)) esp_ble_mesh_health_test_t; + +/** ESP BLE Mesh Health Server Model Context */ +typedef struct { + /** Pointer to Health Server Model */ + esp_ble_mesh_model_t *model; + + /** Health callback struct */ + esp_ble_mesh_health_srv_cb_t health_cb; + + /** Attention Timer state */ + struct k_delayed_work attention_timer; + + /** Attention Timer start flag */ + bool attention_timer_start; + + /** Health Server fault test */ + esp_ble_mesh_health_test_t health_test; +} esp_ble_mesh_health_srv_t; + +/** Parameter of Health Fault Get */ +typedef struct { + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ +} esp_ble_mesh_health_fault_get_t; + +/** Parameter of Health Attention Set */ +typedef struct { + uint8_t attention; /*!< Value of the Attention Timer state */ +} esp_ble_mesh_health_attention_set_t; + +/** Parameter of Health Period Set */ +typedef struct { + uint8_t fast_period_divisor; /*!< Divider for the Publish Period */ +} esp_ble_mesh_health_period_set_t; + +/** Parameter of Health Fault Test */ +typedef struct { + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ + uint8_t test_id; /*!< ID of a specific test to be performed */ +} esp_ble_mesh_health_fault_test_t; + +/** Parameter of Health Fault Clear */ +typedef struct { + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ +} esp_ble_mesh_health_fault_clear_t; + +/** + * @brief For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET + * ESP_BLE_MESH_MODEL_OP_ATTENTION_GET + * ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET + * the get_state parameter in the esp_ble_mesh_health_client_get_state function should not be set to NULL. + */ +typedef union { + esp_ble_mesh_health_fault_get_t fault_get; /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET. */ +} esp_ble_mesh_health_client_get_state_t; + +/** + * @brief For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR + * ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK + * ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST + * ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK + * ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET + * ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK + * ESP_BLE_MESH_MODEL_OP_ATTENTION_SET + * ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK + * the set_state parameter in the esp_ble_mesh_health_client_set_state function should not be set to NULL. + */ +typedef union { + esp_ble_mesh_health_attention_set_t attention_set; /*!< For ESP_BLE_MESH_MODEL_OP_ATTENTION_SET or ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK. */ + esp_ble_mesh_health_period_set_t period_set; /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET or ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK. */ + esp_ble_mesh_health_fault_test_t fault_test; /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST or ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK. */ + esp_ble_mesh_health_fault_clear_t fault_clear; /*!< For ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR or ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK. */ +} esp_ble_mesh_health_client_set_state_t; + +/** Parameters of Health Current Status */ +typedef struct { + uint8_t test_id; /*!< ID of a most recently performed test */ + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ + struct net_buf_simple *fault_array; /*!< FaultArray field contains a sequence of 1-octet fault values */ +} esp_ble_mesh_health_current_status_cb_t; + +/** Parameters of Health Fault Status */ +typedef struct { + uint8_t test_id; /*!< ID of a most recently performed test */ + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ + struct net_buf_simple *fault_array; /*!< FaultArray field contains a sequence of 1-octet fault values */ +} esp_ble_mesh_health_fault_status_cb_t; + +/** Parameter of Health Period Status */ +typedef struct { + uint8_t fast_period_divisor; /*!< Divider for the Publish Period */ +} esp_ble_mesh_health_period_status_cb_t; + +/** Parameter of Health Attention Status */ +typedef struct { + uint8_t attention; /*!< Value of the Attention Timer state */ +} esp_ble_mesh_health_attention_status_cb_t; + +/** + * @brief Health Client Model received message union + */ +typedef union { + esp_ble_mesh_health_current_status_cb_t current_status; /*!< The health current status value */ + esp_ble_mesh_health_fault_status_cb_t fault_status; /*!< The health fault status value */ + esp_ble_mesh_health_period_status_cb_t period_status; /*!< The health period status value */ + esp_ble_mesh_health_attention_status_cb_t attention_status; /*!< The health attention status value */ +} esp_ble_mesh_health_client_common_cb_param_t; + +/** Health Client Model callback parameters */ +typedef struct { + int error_code; /*!< Appropriate error code */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + esp_ble_mesh_health_client_common_cb_param_t status_cb; /*!< The health message status callback values */ +} esp_ble_mesh_health_client_cb_param_t; + +/** This enum value is the event of Health Client Model */ +typedef enum { + ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX, +} esp_ble_mesh_health_client_cb_event_t; + +/** Parameter of publishing Health Current Status completion event */ +typedef struct { + int error_code; /*!< The result of publishing Health Current Status */ + esp_ble_mesh_elem_t *element; /*!< Pointer to the element which contains the Health Server Model */ +} esp_ble_mesh_health_fault_update_comp_cb_t; + +/** Parameters of Health Fault Clear event */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Health Server Model */ + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ +} esp_ble_mesh_health_fault_clear_cb_t; + +/** Parameters of Health Fault Test event */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Health Server Model */ + uint8_t test_id; /*!< ID of a specific test to be performed */ + uint16_t company_id; /*!< Bluetooth assigned 16-bit Company ID */ +} esp_ble_mesh_health_fault_test_cb_t; + +/** Parameter of Health Attention On event */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Health Server Model */ + uint8_t time; /*!< Duration of attention timer on (in seconds) */ +} esp_ble_mesh_health_attention_on_cb_t; + +/** Parameter of Health Attention Off event */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Health Server Model */ +} esp_ble_mesh_health_attention_off_cb_t; + +/** + * @brief Health Server Model callback parameters union + */ +typedef union { + esp_ble_mesh_health_fault_update_comp_cb_t fault_update_comp; /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT */ + esp_ble_mesh_health_fault_clear_cb_t fault_clear; /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_CLEAR_EVT */ + esp_ble_mesh_health_fault_test_cb_t fault_test; /*!< ESP_BLE_MESH_HEALTH_SERVER_FAULT_TEST_EVT */ + esp_ble_mesh_health_attention_on_cb_t attention_on; /*!< ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_ON_EVT */ + esp_ble_mesh_health_attention_off_cb_t attention_off; /*!< ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_OFF_EVT */ +} esp_ble_mesh_health_server_cb_param_t; + +/** This enum value is the event of Health Server Model */ +typedef enum { + ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT, + ESP_BLE_MESH_HEALTH_SERVER_FAULT_CLEAR_EVT, + ESP_BLE_MESH_HEALTH_SERVER_FAULT_TEST_EVT, + ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_ON_EVT, + ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_OFF_EVT, + ESP_BLE_MESH_HEALTH_SERVER_EVT_MAX, +} esp_ble_mesh_health_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Health Client and Server Model function. + */ + +/** + * @brief Health Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_health_client_cb_t)(esp_ble_mesh_health_client_cb_event_t event, + esp_ble_mesh_health_client_cb_param_t *param); + +/** + * @brief Health Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_health_server_cb_t)(esp_ble_mesh_health_server_cb_event_t event, + esp_ble_mesh_health_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Health Model callback, the callback will report Health Client & Server Model events. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_health_client_callback(esp_ble_mesh_health_client_cb_t callback); + +/** + * @brief Register BLE Mesh Health Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_health_server_callback(esp_ble_mesh_health_server_cb_t callback); + +/** + * @brief This function is called to get the Health Server states using the Health Client Model get messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_opcode_health_client_get_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer to a union, each kind of opcode corresponds to one structure inside. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_get_state_t *get_state); + +/** + * @brief This function is called to set the Health Server states using the Health Client Model set messages. + * + * @note If you want to find the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_opcode_health_client_set_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer to a union, each kind of opcode corresponds to one structure inside. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_set_state_t *set_state); + +/** + * @brief This function is called by the Health Server Model to update the context of its Health Current status. + * + * @param[in] element: The element to which the Health Server Model belongs. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_health_server_fault_update(esp_ble_mesh_elem_t *element); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_HEALTH_MODEL_API_H_ */ diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h new file mode 100644 index 000000000..4180eb914 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h @@ -0,0 +1,1683 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Light Client Model APIs. + */ + +#ifndef _ESP_BLE_MESH_LIGHTING_MODEL_API_H_ +#define _ESP_BLE_MESH_LIGHTING_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_CLI + * + * @brief Define a new Light Lightness Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Light Lightness Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Light Lightness Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_CTL_CLI + * + * @brief Define a new Light CTL Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Light CTL Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Light CTL Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_CTL_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_HSL_CLI + * + * @brief Define a new Light HSL Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Light HSL Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Light HSL Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_HSL_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_XYL_CLI + * + * @brief Define a new Light xyL Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Light xyL Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Light xyL Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_XYL_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LC_CLI + * + * @brief Define a new Light LC Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Light LC Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Light LC Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LC_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LC_CLI, \ + NULL, cli_pub, cli_data) + +/** + * @brief Bluetooth Mesh Light Lightness Client Model Get and Set parameters structure. + */ + +/** Parameters of Light Lightness Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness actual state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_lightness_set_t; + +/** Parameters of Light Lightness Linear Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness linear state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_lightness_linear_set_t; + +/** Parameter of Light Lightness Default Set */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Default state */ +} esp_ble_mesh_light_lightness_default_set_t; + +/** Parameters of Light Lightness Range Set */ +typedef struct { + uint16_t range_min; /*!< Value of range min field of light lightness range state */ + uint16_t range_max; /*!< Value of range max field of light lightness range state */ +} esp_ble_mesh_light_lightness_range_set_t; + +/** Parameters of Light CTL Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t ctl_lightness; /*!< Target value of light ctl lightness state */ + uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_ctl_set_t; + +/** Parameters of Light CTL Temperature Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t ctl_temperatrue; /*!< Target value of light ctl temperature state */ + int16_t ctl_delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_ctl_temperature_set_t; + +/** Parameters of Light CTL Temperature Range Set */ +typedef struct { + uint16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ + uint16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ +} esp_ble_mesh_light_ctl_temperature_range_set_t; + +/** Parameters of Light CTL Default Set */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t temperature; /*!< Value of light temperature default state */ + int16_t delta_uv; /*!< Value of light delta UV default state */ +} esp_ble_mesh_light_ctl_default_set_t; + +/** Parameters of Light HSL Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hsl_lightness; /*!< Target value of light hsl lightness state */ + uint16_t hsl_hue; /*!< Target value of light hsl hue state */ + uint16_t hsl_saturation; /*!< Target value of light hsl saturation state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_hsl_set_t; + +/** Parameters of Light HSL Hue Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hue; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_hsl_hue_set_t; + +/** Parameters of Light HSL Saturation Set */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t saturation; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_hsl_saturation_set_t; + +/** Parameters of Light HSL Default Set */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t hue; /*!< Value of light hue default state */ + uint16_t saturation; /*!< Value of light saturation default state */ +} esp_ble_mesh_light_hsl_default_set_t; + +/** Parameters of Light HSL Range Set */ +typedef struct { + uint16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ + uint16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ + uint16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ + uint16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ +} esp_ble_mesh_light_hsl_range_set_t; + +/** Parameters of Light xyL Set */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t xyl_lightness; /*!< The target value of the Light xyL Lightness state */ + uint16_t xyl_x; /*!< The target value of the Light xyL x state */ + uint16_t xyl_y; /*!< The target value of the Light xyL y state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_xyl_set_t; + +/** Parameters of Light xyL Default Set */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t xyl_x; /*!< The value of the Light xyL x Default state */ + uint16_t xyl_y; /*!< The value of the Light xyL y Default state */ +} esp_ble_mesh_light_xyl_default_set_t; + +/** Parameters of Light xyL Range Set */ +typedef struct { + uint16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ + uint16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ + uint16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ + uint16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ +} esp_ble_mesh_light_xyl_range_set_t; + +/** Parameter of Light LC Mode Set */ +typedef struct { + uint8_t mode; /*!< The target value of the Light LC Mode state */ +} esp_ble_mesh_light_lc_mode_set_t; + +/** Parameter of Light LC OM Set */ +typedef struct { + uint8_t mode; /*!< The target value of the Light LC Occupancy Mode state */ +} esp_ble_mesh_light_lc_om_set_t; + +/** Parameters of Light LC Light OnOff Set */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint8_t light_onoff; /*!< The target value of the Light LC Light OnOff state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_light_lc_light_onoff_set_t; + +/** Parameter of Light LC Property Get */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ +} esp_ble_mesh_light_lc_property_get_t; + +/** Parameters of Light LC Property Set */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ + struct net_buf_simple *property_value; /*!< Raw value for the Light LC Property */ +} esp_ble_mesh_light_lc_property_set_t; + +/** + * @brief Lighting Client Model get message union + */ +typedef union { + esp_ble_mesh_light_lc_property_get_t lc_property_get; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET */ +} esp_ble_mesh_light_client_get_state_t; + +/** + * @brief Lighting Client Model set message union + */ +typedef union { + esp_ble_mesh_light_lightness_set_t lightness_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK */ + esp_ble_mesh_light_lightness_linear_set_t lightness_linear_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK */ + esp_ble_mesh_light_lightness_default_set_t lightness_default_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK */ + esp_ble_mesh_light_lightness_range_set_t lightness_range_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK */ + esp_ble_mesh_light_ctl_set_t ctl_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK */ + esp_ble_mesh_light_ctl_temperature_set_t ctl_temperature_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK */ + esp_ble_mesh_light_ctl_temperature_range_set_t ctl_temperature_range_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK */ + esp_ble_mesh_light_ctl_default_set_t ctl_default_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK */ + esp_ble_mesh_light_hsl_set_t hsl_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK */ + esp_ble_mesh_light_hsl_hue_set_t hsl_hue_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK */ + esp_ble_mesh_light_hsl_saturation_set_t hsl_saturation_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK */ + esp_ble_mesh_light_hsl_default_set_t hsl_default_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK */ + esp_ble_mesh_light_hsl_range_set_t hsl_range_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK */ + esp_ble_mesh_light_xyl_set_t xyl_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK */ + esp_ble_mesh_light_xyl_default_set_t xyl_default_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK */ + esp_ble_mesh_light_xyl_range_set_t xyl_range_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK */ + esp_ble_mesh_light_lc_mode_set_t lc_mode_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK */ + esp_ble_mesh_light_lc_om_set_t lc_om_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK */ + esp_ble_mesh_light_lc_light_onoff_set_t lc_light_onoff_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK */ + esp_ble_mesh_light_lc_property_set_t lc_property_set; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET & ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK */ +} esp_ble_mesh_light_client_set_state_t; + +/** + * @brief Bluetooth Mesh Light Lightness Client Model Get and Set callback parameters structure. + */ + +/** Parameters of Light Lightness Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_lightness; /*!< Current value of light lightness actual state */ + uint16_t target_lightness; /*!< Target value of light lightness actual state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_lightness_status_cb_t; + +/** Parameters of Light Lightness Linear Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_lightness; /*!< Current value of light lightness linear state */ + uint16_t target_lightness; /*!< Target value of light lightness linear state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_lightness_linear_status_cb_t; + +/** Parameter of Light Lightness Last Status */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Last state */ +} esp_ble_mesh_light_lightness_last_status_cb_t; + +/** Parameter of Light Lightness Default Status */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness default State */ +} esp_ble_mesh_light_lightness_default_status_cb_t; + +/** Parameters of Light Lightness Range Status */ +typedef struct { + uint8_t status_code; /*!< Status Code for the request message */ + uint16_t range_min; /*!< Value of range min field of light lightness range state */ + uint16_t range_max; /*!< Value of range max field of light lightness range state */ +} esp_ble_mesh_light_lightness_range_status_cb_t; + +/** Parameters of Light CTL Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_ctl_lightness; /*!< Current value of light ctl lightness state */ + uint16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ + uint16_t target_ctl_lightness; /*!< Target value of light ctl lightness state (optional) */ + uint16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (C.1) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_ctl_status_cb_t; + +/** Parameters of Light CTL Temperature Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_ctl_temperature; /*!< Current value of light ctl temperature state */ + uint16_t present_ctl_delta_uv; /*!< Current value of light ctl delta UV state */ + uint16_t target_ctl_temperature; /*!< Target value of light ctl temperature state (optional) */ + uint16_t target_ctl_delta_uv; /*!< Target value of light ctl delta UV state (C.1) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_ctl_temperature_status_cb_t; + +/** Parameters of Light CTL Temperature Range Status */ +typedef struct { + uint8_t status_code; /*!< Status code for the request message */ + uint16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ + uint16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ +} esp_ble_mesh_light_ctl_temperature_range_status_cb_t; + +/** Parameters of Light CTL Default Status */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t temperature; /*!< Value of light temperature default state */ + int16_t delta_uv; /*!< Value of light delta UV default state */ +} esp_ble_mesh_light_ctl_default_status_cb_t; + +/** Parameters of Light HSL Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hsl_lightness; /*!< Current value of light hsl lightness state */ + uint16_t hsl_hue; /*!< Current value of light hsl hue state */ + uint16_t hsl_saturation; /*!< Current value of light hsl saturation state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ +} esp_ble_mesh_light_hsl_status_cb_t; + +/** Parameters of Light HSL Target Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hsl_lightness_target; /*!< Target value of light hsl lightness state */ + uint16_t hsl_hue_target; /*!< Target value of light hsl hue state */ + uint16_t hsl_saturation_target; /*!< Target value of light hsl saturation state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ +} esp_ble_mesh_light_hsl_target_status_cb_t; + +/** Parameters of Light HSL Hue Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_hue; /*!< Current value of light hsl hue state */ + uint16_t target_hue; /*!< Target value of light hsl hue state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_hsl_hue_status_cb_t; + +/** Parameters of Light HSL Saturation Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t present_saturation; /*!< Current value of light hsl saturation state */ + uint16_t target_saturation; /*!< Target value of light hsl saturation state (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_hsl_saturation_status_cb_t; + +/** Parameters of Light HSL Default Status */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t hue; /*!< Value of light hue default state */ + uint16_t saturation; /*!< Value of light saturation default state */ +} esp_ble_mesh_light_hsl_default_status_cb_t; + +/** Parameters of Light HSL Range Status */ +typedef struct { + uint8_t status_code; /*!< Status code for the request message */ + uint16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ + uint16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ + uint16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ + uint16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ +} esp_ble_mesh_light_hsl_range_status_cb_t; + +/** Parameters of Light xyL Status */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t xyl_lightness; /*!< The present value of the Light xyL Lightness state */ + uint16_t xyl_x; /*!< The present value of the Light xyL x state */ + uint16_t xyl_y; /*!< The present value of the Light xyL y state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ +} esp_ble_mesh_light_xyl_status_cb_t; + +/** Parameters of Light xyL Target Status */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t target_xyl_lightness; /*!< The target value of the Light xyL Lightness state */ + uint16_t target_xyl_x; /*!< The target value of the Light xyL x state */ + uint16_t target_xyl_y; /*!< The target value of the Light xyL y state */ + uint8_t remain_time; /*!< Time to complete state transition (optional) */ +} esp_ble_mesh_light_xyl_target_status_cb_t; + +/** Parameters of Light xyL Default Status */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t xyl_x; /*!< The value of the Light xyL x Default state */ + uint16_t xyl_y; /*!< The value of the Light xyL y Default state */ +} esp_ble_mesh_light_xyl_default_status_cb_t; + +/** Parameters of Light xyL Range Status */ +typedef struct { + uint8_t status_code; /*!< Status Code for the requesting message */ + uint16_t xyl_x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ + uint16_t xyl_x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ + uint16_t xyl_y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ + uint16_t xyl_y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ +} esp_ble_mesh_light_xyl_range_status_cb_t; + +/** Parameter of Light LC Mode Status */ +typedef struct { + uint8_t mode; /*!< The present value of the Light LC Mode state */ +} esp_ble_mesh_light_lc_mode_status_cb_t; + +/** Parameter of Light LC OM Status */ +typedef struct { + uint8_t mode; /*!< The present value of the Light LC Occupancy Mode state */ +} esp_ble_mesh_light_lc_om_status_cb_t; + +/** Parameters of Light LC Light OnOff Status */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint8_t present_light_onoff; /*!< The present value of the Light LC Light OnOff state */ + uint8_t target_light_onoff; /*!< The target value of the Light LC Light OnOff state (Optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_light_lc_light_onoff_status_cb_t; + +/** Parameters of Light LC Property Status */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ + struct net_buf_simple *property_value; /*!< Raw value for the Light LC Property */ +} esp_ble_mesh_light_lc_property_status_cb_t; + +/** + * @brief Lighting Client Model received message union + */ +typedef union { + esp_ble_mesh_light_lightness_status_cb_t lightness_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS */ + esp_ble_mesh_light_lightness_linear_status_cb_t lightness_linear_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS */ + esp_ble_mesh_light_lightness_last_status_cb_t lightness_last_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS */ + esp_ble_mesh_light_lightness_default_status_cb_t lightness_default_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS */ + esp_ble_mesh_light_lightness_range_status_cb_t lightness_range_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS */ + esp_ble_mesh_light_ctl_status_cb_t ctl_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS */ + esp_ble_mesh_light_ctl_temperature_status_cb_t ctl_temperature_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS */ + esp_ble_mesh_light_ctl_temperature_range_status_cb_t ctl_temperature_range_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS */ + esp_ble_mesh_light_ctl_default_status_cb_t ctl_default_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS */ + esp_ble_mesh_light_hsl_status_cb_t hsl_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS */ + esp_ble_mesh_light_hsl_target_status_cb_t hsl_target_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS */ + esp_ble_mesh_light_hsl_hue_status_cb_t hsl_hue_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS */ + esp_ble_mesh_light_hsl_saturation_status_cb_t hsl_saturation_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS */ + esp_ble_mesh_light_hsl_default_status_cb_t hsl_default_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS */ + esp_ble_mesh_light_hsl_range_status_cb_t hsl_range_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS */ + esp_ble_mesh_light_xyl_status_cb_t xyl_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS */ + esp_ble_mesh_light_xyl_target_status_cb_t xyl_target_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS */ + esp_ble_mesh_light_xyl_default_status_cb_t xyl_default_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS */ + esp_ble_mesh_light_xyl_range_status_cb_t xyl_range_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS */ + esp_ble_mesh_light_lc_mode_status_cb_t lc_mode_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS */ + esp_ble_mesh_light_lc_om_status_cb_t lc_om_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS */ + esp_ble_mesh_light_lc_light_onoff_status_cb_t lc_light_onoff_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS */ + esp_ble_mesh_light_lc_property_status_cb_t lc_property_status; /*!< For ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS */ +} esp_ble_mesh_light_client_status_cb_t; + +/** Lighting Client Model callback parameters */ +typedef struct { + int error_code; /*!< Appropriate error code */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + esp_ble_mesh_light_client_status_cb_t status_cb; /*!< The light status message callback values */ +} esp_ble_mesh_light_client_cb_param_t; + +/** This enum value is the event of Lighting Client Model */ +typedef enum { + ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_LIGHT_CLIENT_EVT_MAX, +} esp_ble_mesh_light_client_cb_event_t; + +/** + * @brief Bluetooth Mesh Light Client Model function. + */ + +/** + * @brief Lighting Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_light_client_cb_t)(esp_ble_mesh_light_client_cb_event_t event, + esp_ble_mesh_light_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Light Client Model callback. + * + * @param[in] callback: pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_light_client_callback(esp_ble_mesh_light_client_cb_t callback); + +/** + * @brief Get the value of Light Server Model states using the Light Client Model get messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_light_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer of light get message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_light_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_light_client_get_state_t *get_state); + +/** + * @brief Set the value of Light Server Model states using the Light Client Model set messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_light_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer of light set message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_light_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_light_client_set_state_t *set_state); + +/** + * @brief Lighting Server Models related context. + */ + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_SRV + * + * @brief Define a new Light Lightness Server Model. + * + * @note 1. The Light Lightness Server model extends the Generic Power OnOff + * Server model and the Generic Level Server model. When this model + * is present on an Element, the corresponding Light Lightness Setup + * Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_lightness_srv_t. + * + * @return New Light Lightness Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_SETUP_SRV + * + * @brief Define a new Light Lightness Setup Server Model. + * + * @note 1. The Light Lightness Setup Server model extends the Light Lightness + * Server model and the Generic Power OnOff Setup Server model. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_lightness_setup_srv_t. + * + * @return New Light Lightness Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LIGHTNESS_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_CTL_SRV + * + * @brief Define a new Light CTL Server Model. + * + * @note 1. The Light CTL Server model extends the Light Lightness Server model. + * When this model is present on an Element, the corresponding Light + * CTL Temperature Server model and the corresponding Light CTL Setup + * Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * 3. The model requires two elements: the main element and the Temperature + * element. The Temperature element contains the corresponding Light CTL + * Temperature Server model and an instance of a Generic Level state + * bound to the Light CTL Temperature state on the Temperature element. + * The Light CTL Temperature state on the Temperature element is bound to + * the Light CTL state on the main element. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_ctl_srv_t. + * + * @return New Light CTL Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_CTL_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_CTL_SETUP_SRV + * + * @brief Define a new Light CTL Setup Server Model. + * + * @note 1. The Light CTL Setup Server model extends the Light CTL Server and + * the Light Lightness Setup Server. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_ctl_setup_srv_t. + * + * @return New Light CTL Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_CTL_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_CTL_TEMP_SRV + * + * @brief Define a new Light CTL Temperature Server Model. + * + * @note 1. The Light CTL Temperature Server model extends the Generic Level + * Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_ctl_temp_srv_t. + * + * @return New Light CTL Temperature Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_CTL_TEMP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_HSL_SRV + * + * @brief Define a new Light HSL Server Model. + * + * @note 1. The Light HSL Server model extends the Light Lightness Server model. When + * this model is present on an Element, the corresponding Light HSL Hue + * Server model and the corresponding Light HSL Saturation Server model and + * the corresponding Light HSL Setup Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * 3. The model requires three elements: the main element and the Hue element + * and the Saturation element. The Hue element contains the corresponding + * Light HSL Hue Server model and an instance of a Generic Level state bound + * to the Light HSL Hue state on the Hue element. The Saturation element + * contains the corresponding Light HSL Saturation Server model and an + * instance of a Generic Level state bound to the Light HSL Saturation state + * on the Saturation element. The Light HSL Hue state on the Hue element is + * bound to the Light HSL state on the main element and the Light HSL + * Saturation state on the Saturation element is bound to the Light HSL state + * on the main element. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_hsl_srv_t. + * + * @return New Light HSL Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_HSL_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_HSL_SETUP_SRV + * + * @brief Define a new Light HSL Setup Server Model. + * + * @note 1. The Light HSL Setup Server model extends the Light HSL Server and + * the Light Lightness Setup Server. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_hsl_setup_srv_t. + * + * @return New Light HSL Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_HSL_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_HSL_HUE_SRV + * + * @brief Define a new Light HSL Hue Server Model. + * + * @note 1. The Light HSL Hue Server model extends the Generic Level Server model. + * This model is associated with the Light HSL Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_hsl_hue_srv_t. + * + * @return New Light HSL Hue Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_HSL_HUE_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_HSL_SAT_SRV + * + * @brief Define a new Light HSL Saturation Server Model. + * + * @note 1. The Light HSL Saturation Server model extends the Generic Level Server + * model. This model is associated with the Light HSL Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_hsl_sat_srv_t. + * + * @return New Light HSL Saturation Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_HSL_SAT_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_XYL_SRV + * + * @brief Define a new Light xyL Server Model. + * + * @note 1. The Light xyL Server model extends the Light Lightness Server model. + * When this model is present on an Element, the corresponding Light xyL + * Setup Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_xyl_srv_t. + * + * @return New Light xyL Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_XYL_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_XYL_SETUP_SRV + * + * @brief Define a new Light xyL Setup Server Model. + * + * @note 1. The Light xyL Setup Server model extends the Light xyL Server and + * the Light Lightness Setup Server. + * 2. This model shall support model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_xyl_setup_srv_t. + * + * @return New Light xyL Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_XYL_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LC_SRV + * + * @brief Define a new Light LC Server Model. + * + * @note 1. The Light LC (Lightness Control) Server model extends the Light + * Lightness Server model and the Generic OnOff Server model. When + * this model is present on an Element, the corresponding Light LC + * Setup Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * 3. This model may be used to represent an element that is a client to + * a Sensor Server model and controls the Light Lightness Actual state + * via defined state bindings. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_lc_srv_t. + * + * @return New Light LC Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LC_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_LIGHT_LC_SETUP_SRV + * + * @brief Define a new Light LC Setup Server Model. + * + * @note 1. The Light LC (Lightness Control) Setup model extends the Light LC + * Server model. + * 2. This model shall support model publication and model subscription. + * 3. This model may be used to configure setup parameters for the Light + * LC Server model. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_light_lc_setup_srv_t. + * + * @return New Light LC Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_LIGHT_LC_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** Parameters of Light Lightness state */ +typedef struct { + uint16_t lightness_linear; /*!< The present value of Light Lightness Linear state */ + uint16_t target_lightness_linear; /*!< The target value of Light Lightness Linear state */ + + uint16_t lightness_actual; /*!< The present value of Light Lightness Actual state */ + uint16_t target_lightness_actual; /*!< The target value of Light Lightness Actual state */ + + uint16_t lightness_last; /*!< The value of Light Lightness Last state */ + uint16_t lightness_default; /*!< The value of Light Lightness Default state */ + + uint8_t status_code; /*!< The status code of setting Light Lightness Range state */ + uint16_t lightness_range_min; /*!< The minimum value of Light Lightness Range state */ + uint16_t lightness_range_max; /*!< The maximum value of Light Lightness Range state */ +} esp_ble_mesh_light_lightness_state_t; + +/** User data of Light Lightness Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting Lightness Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_lightness_state_t *state; /*!< Parameters of the Light Lightness state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t actual_transition; /*!< Parameters of state transition */ + esp_ble_mesh_state_transition_t linear_transition; /*!< Parameters of state transition */ + int32_t tt_delta_lightness_actual; /*!< Delta change value of lightness actual state transition */ + int32_t tt_delta_lightness_linear; /*!< Delta change value of lightness linear state transition */ +} esp_ble_mesh_light_lightness_srv_t; + +/** User data of Light Lightness Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting Lightness Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_lightness_state_t *state; /*!< Parameters of the Light Lightness state */ +} esp_ble_mesh_light_lightness_setup_srv_t; + +/** Parameters of Light CTL state */ +typedef struct { + uint16_t lightness; /*!< The present value of Light CTL Lightness state */ + uint16_t target_lightness; /*!< The target value of Light CTL Lightness state */ + + uint16_t temperature; /*!< The present value of Light CTL Temperature state */ + uint16_t target_temperature; /*!< The target value of Light CTL Temperature state */ + + int16_t delta_uv; /*!< The present value of Light CTL Delta UV state */ + int16_t target_delta_uv; /*!< The target value of Light CTL Delta UV state */ + + uint8_t status_code; /*!< The statue code of setting Light CTL Temperature Range state */ + uint16_t temperature_range_min; /*!< The minimum value of Light CTL Temperature Range state */ + uint16_t temperature_range_max; /*!< The maximum value of Light CTL Temperature Range state */ + + uint16_t lightness_default; /*!< The value of Light Lightness Default state */ + uint16_t temperature_default; /*!< The value of Light CTL Temperature Default state */ + int16_t delta_uv_default; /*!< The value of Light CTL Delta UV Default state */ +} esp_ble_mesh_light_ctl_state_t; + +/** User data of Light CTL Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting CTL Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_ctl_state_t *state; /*!< Parameters of the Light CTL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_lightness; /*!< Delta change value of lightness state transition */ + int32_t tt_delta_temperature; /*!< Delta change value of temperature state transition */ + int32_t tt_delta_delta_uv; /*!< Delta change value of delta uv state transition */ +} esp_ble_mesh_light_ctl_srv_t; + +/** User data of Light CTL Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting CTL Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_ctl_state_t *state; /*!< Parameters of the Light CTL state */ +} esp_ble_mesh_light_ctl_setup_srv_t; + +/** User data of Light CTL Temperature Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting CTL Temperature Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_ctl_state_t *state; /*!< Parameters of the Light CTL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_temperature; /*!< Delta change value of temperature state transition */ + int32_t tt_delta_delta_uv; /*!< Delta change value of delta uv state transition */ +} esp_ble_mesh_light_ctl_temp_srv_t; + +/** Parameters of Light HSL state */ +typedef struct { + uint16_t lightness; /*!< The present value of Light HSL Lightness state */ + uint16_t target_lightness; /*!< The target value of Light HSL Lightness state */ + + uint16_t hue; /*!< The present value of Light HSL Hue state */ + uint16_t target_hue; /*!< The target value of Light HSL Hue state */ + + uint16_t saturation; /*!< The present value of Light HSL Saturation state */ + uint16_t target_saturation; /*!< The target value of Light HSL Saturation state */ + + uint16_t lightness_default; /*!< The value of Light Lightness Default state */ + uint16_t hue_default; /*!< The value of Light HSL Hue Default state */ + uint16_t saturation_default; /*!< The value of Light HSL Saturation Default state */ + + uint8_t status_code; /*!< The status code of setting Light HSL Hue & Saturation Range state */ + uint16_t hue_range_min; /*!< The minimum value of Light HSL Hue Range state */ + uint16_t hue_range_max; /*!< The maximum value of Light HSL Hue Range state */ + uint16_t saturation_range_min; /*!< The minimum value of Light HSL Saturation state */ + uint16_t saturation_range_max; /*!< The maximum value of Light HSL Saturation state */ +} esp_ble_mesh_light_hsl_state_t; + +/** User data of Light HSL Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting HSL Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_hsl_state_t *state; /*!< Parameters of the Light HSL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_lightness; /*!< Delta change value of lightness state transition */ + int32_t tt_delta_hue; /*!< Delta change value of hue state transition */ + int32_t tt_delta_saturation; /*!< Delta change value of saturation state transition */ +} esp_ble_mesh_light_hsl_srv_t; + +/** User data of Light HSL Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting HSL Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_hsl_state_t *state; /*!< Parameters of the Light HSL state */ +} esp_ble_mesh_light_hsl_setup_srv_t; + +/** User data of Light HSL Hue Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting HSL Hue Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_hsl_state_t *state; /*!< Parameters of the Light HSL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_hue; /*!< Delta change value of hue state transition */ +} esp_ble_mesh_light_hsl_hue_srv_t; + +/** User data of Light HSL Saturation Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting HSL Saturation Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_hsl_state_t *state; /*!< Parameters of the Light HSL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_saturation; /*!< Delta change value of saturation state transition */ +} esp_ble_mesh_light_hsl_sat_srv_t; + +/** Parameters of Light xyL state */ +typedef struct { + uint16_t lightness; /*!< The present value of Light xyL Lightness state */ + uint16_t target_lightness; /*!< The target value of Light xyL Lightness state */ + + uint16_t x; /*!< The present value of Light xyL x state */ + uint16_t target_x; /*!< The target value of Light xyL x state */ + + uint16_t y; /*!< The present value of Light xyL y state */ + uint16_t target_y; /*!< The target value of Light xyL y state */ + + uint16_t lightness_default; /*!< The value of Light Lightness Default state */ + uint16_t x_default; /*!< The value of Light xyL x Default state */ + uint16_t y_default; /*!< The value of Light xyL y Default state */ + + uint8_t status_code; /*!< The status code of setting Light xyL x & y Range state */ + uint16_t x_range_min; /*!< The minimum value of Light xyL x Range state */ + uint16_t x_range_max; /*!< The maximum value of Light xyL x Range state */ + uint16_t y_range_min; /*!< The minimum value of Light xyL y Range state */ + uint16_t y_range_max; /*!< The maximum value of Light xyL y Range state */ +} esp_ble_mesh_light_xyl_state_t; + +/** User data of Light xyL Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting xyL Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_xyl_state_t *state; /*!< Parameters of the Light xyL state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ + int32_t tt_delta_lightness; /*!< Delta change value of lightness state transition */ + int32_t tt_delta_x; /*!< Delta change value of x state transition */ + int32_t tt_delta_y; /*!< Delta change value of y state transition */ +} esp_ble_mesh_light_xyl_srv_t; + +/** User data of Light xyL Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting xyL Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_xyl_state_t *state; /*!< Parameters of the Light xyL state */ +} esp_ble_mesh_light_xyl_setup_srv_t; + +/** Parameters of Light LC states */ +typedef struct { + /** + * 0b0 The controller is turned off. + * - The binding with the Light Lightness state is disabled. + * 0b1 The controller is turned on. + * - The binding with the Light Lightness state is enabled. + */ + uint32_t mode : 1, /*!< The value of Light LC Mode state */ + occupancy_mode : 1, /*!< The value of Light LC Occupancy Mode state */ + light_onoff : 1, /*!< The present value of Light LC Light OnOff state */ + target_light_onoff : 1, /*!< The target value of Light LC Light OnOff state */ + occupancy : 1, /*!< The value of Light LC Occupancy state */ + ambient_luxlevel : 24; /*!< The value of Light LC Ambient LuxLevel state */ + + /** + * 1. Light LC Linear Output = max((Lightness Out)^2/65535, Regulator Output) + * 2. If the Light LC Mode state is set to 0b1, the binding is enabled and upon + * a change of the Light LC Linear Output state, the following operation + * shall be performed: + * Light Lightness Linear = Light LC Linear Output + * 3. If the Light LC Mode state is set to 0b0, the binding is disabled (i.e., + * upon a change of the Light LC Linear Output state, no operation on the + * Light Lightness Linear state is performed). + */ + uint16_t linear_output; /*!< The value of Light LC Linear Output state */ +} esp_ble_mesh_light_lc_state_t; + +/** + * Parameters of Light Property states. + * The Light LC Property states are read / write states that determine the + * configuration of a Light Lightness Controller. Each state is represented + * by a device property and is controlled by Light LC Property messages. + */ +typedef struct { + /** + * A timing state that determines the delay for changing the Light LC + * Occupancy state upon receiving a Sensor Status message from an + * occupancy sensor. + */ + uint32_t time_occupancy_delay; /*!< The value of Light LC Time Occupancy Delay state */ + /** + * A timing state that determines the time the controlled lights fade + * to the level determined by the Light LC Lightness On state. + */ + uint32_t time_fade_on; /*!< The value of Light LC Time Fade On state */ + /** + * A timing state that determines the time the controlled lights stay + * at the level determined by the Light LC Lightness On state. + */ + uint32_t time_run_on; /*!< The value of Light LC Time Run On state */ + /** + * A timing state that determines the time the controlled lights fade + * from the level determined by the Light LC Lightness On state to the + * level determined by the Light Lightness Prolong state. + */ + uint32_t time_fade; /*!< The value of Light LC Time Fade state */ + /** + * A timing state that determines the time the controlled lights stay at + * the level determined by the Light LC Lightness Prolong state. + */ + uint32_t time_prolong; /*!< The value of Light LC Time Prolong state */ + /** + * A timing state that determines the time the controlled lights fade from + * the level determined by the Light LC Lightness Prolong state to the level + * determined by the Light LC Lightness Standby state when the transition is + * automatic. + */ + uint32_t time_fade_standby_auto; /*!< The value of Light LC Time Fade Standby Auto state */ + /** + * A timing state that determines the time the controlled lights fade from + * the level determined by the Light LC Lightness Prolong state to the level + * determined by the Light LC Lightness Standby state when the transition is + * triggered by a change in the Light LC Light OnOff state. + */ + uint32_t time_fade_standby_manual; /*!< The value of Light LC Time Fade Standby Manual state */ + + /** + * A lightness state that determines the perceptive light lightness at the + * Occupancy and Run internal controller states. + */ + uint16_t lightness_on; /*!< The value of Light LC Lightness On state */ + /** + * A lightness state that determines the light lightness at the Prolong + * internal controller state. + */ + uint16_t lightness_prolong; /*!< The value of Light LC Lightness Prolong state */ + /** + * A lightness state that determines the light lightness at the Standby + * internal controller state. + */ + uint16_t lightness_standby; /*!< The value of Light LC Lightness Standby state */ + + /** + * A uint16 state representing the Ambient LuxLevel level that determines + * if the controller transitions from the Light Control Standby state. + */ + uint16_t ambient_luxlevel_on; /*!< The value of Light LC Ambient LuxLevel On state */ + /** + * A uint16 state representing the required Ambient LuxLevel level in the + * Prolong state. + */ + uint16_t ambient_luxlevel_prolong; /*!< The value of Light LC Ambient LuxLevel Prolong state */ + /** + * A uint16 state representing the required Ambient LuxLevel level in the + * Standby state. + */ + uint16_t ambient_luxlevel_standby; /*!< The value of Light LC Ambient LuxLevel Standby state */ + + /** + * A float32 state representing the integral coefficient that determines the + * integral part of the equation defining the output of the Light LC PI + * Feedback Regulator, when Light LC Ambient LuxLevel is less than LuxLevel + * Out. Valid range: 0.0 ~ 1000.0. The default value is 250.0. + */ + float regulator_kiu; /*!< The value of Light LC Regulator Kiu state */ + /** + * A float32 state representing the integral coefficient that determines the + * integral part of the equation defining the output of the Light LC PI + * Feedback Regulator, when Light LC Ambient LuxLevel is greater than or equal + * to the value of the LuxLevel Out state. Valid range: 0.0 ~ 1000.0. The + * default value is 25.0. + */ + float regulator_kid; /*!< The value of Light LC Regulator Kid state */ + /** + * A float32 state representing the proportional coefficient that determines + * the proportional part of the equation defining the output of the Light LC + * PI Feedback Regulator, when Light LC Ambient LuxLevel is less than the value + * of the LuxLevel Out state. Valid range: 0.0 ~ 1000.0. The default value is 80.0. + */ + float regulator_kpu; /*!< The value of Light LC Regulator Kpu state */ + /** + * A float32 state representing the proportional coefficient that determines + * the proportional part of the equation defining the output of the Light LC PI + * Feedback Regulator, when Light LC Ambient LuxLevel is greater than or equal + * to the value of the LuxLevel Out state. Valid range: 0.0 ~ 1000.0. The default + * value is 80.0. + */ + float regulator_kpd; /*!< The value of Light LC Regulator Kpd state */ + /** + * A int8 state representing the percentage accuracy of the Light LC PI Feedback + * Regulator. Valid range: 0.0 ~ 100.0. The default value is 2.0. + */ + int8_t regulator_accuracy; /*!< The value of Light LC Regulator Accuracy state */ + + /** + * If the message Raw field contains a Raw Value for the Time Since Motion + * Sensed device property, which represents a value less than or equal to + * the value of the Light LC Occupancy Delay state, it shall delay setting + * the Light LC Occupancy state to 0b1 by the difference between the value + * of the Light LC Occupancy Delay state and the received Time Since Motion + * value. + */ + uint32_t set_occupancy_to_1_delay; /*!< The value of the difference between value of the + Light LC Occupancy Delay state and the received + Time Since Motion value */ +} esp_ble_mesh_light_lc_property_state_t; + +/** This enum value is the Light LC State Machine states */ +typedef enum { + ESP_BLE_MESH_LC_OFF, + ESP_BLE_MESH_LC_STANDBY, + ESP_BLE_MESH_LC_FADE_ON, + ESP_BLE_MESH_LC_RUN, + ESP_BLE_MESH_LC_FADE, + ESP_BLE_MESH_LC_PROLONG, + ESP_BLE_MESH_LC_FADE_STANDBY_AUTO, + ESP_BLE_MESH_LC_FADE_STANDBY_MANUAL, +} esp_ble_mesh_lc_state_t; + +/** Parameters of Light LC state machine */ +typedef struct { + /** + * The Fade On, Fade, Fade Standby Auto, and Fade Standby Manual states are + * transition states that define the transition of the Lightness Out and + * LuxLevel Out states. This transition can be started as a result of the + * Light LC State Machine change or as a result of receiving the Light LC + * Light OnOff Set or Light LC Light Set Unacknowledged message. + */ + struct { + uint8_t fade_on; /*!< The value of transition time of Light LC Time Fade On */ + uint8_t fade; /*!< The value of transition time of Light LC Time Fade */ + uint8_t fade_standby_auto; /*!< The value of transition time of Light LC Time Fade Standby Auto */ + uint8_t fade_standby_manual; /*!< The value of transition time of Light LC Time Fade Standby Manual */ + } trans_time; /*!< The value of transition time */ + esp_ble_mesh_lc_state_t state; /*!< The value of Light LC state machine state */ + struct k_delayed_work timer; /*!< Timer of Light LC state machine */ +} esp_ble_mesh_light_lc_state_machine_t; + +/** Parameters of Light Lightness controller */ +typedef struct { + esp_ble_mesh_light_lc_state_t state; /*!< Parameters of Light LC state */ + esp_ble_mesh_light_lc_property_state_t prop_state; /*!< Parameters of Light LC Property state */ + esp_ble_mesh_light_lc_state_machine_t state_machine; /*!< Parameters of Light LC state machine */ +} esp_ble_mesh_light_control_t; + +/** User data of Light LC Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting LC Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_control_t *lc; /*!< Parameters of the Light controller */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ +} esp_ble_mesh_light_lc_srv_t; + +/** User data of Light LC Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Lighting LC Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_light_control_t *lc; /*!< Parameters of the Light controller */ +} esp_ble_mesh_light_lc_setup_srv_t; + +/** Parameter of Light Lightness Actual state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light Lightness Actual state */ +} esp_ble_mesh_state_change_light_lightness_set_t; + +/** Parameter of Light Lightness Linear state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light Lightness Linear state */ +} esp_ble_mesh_state_change_light_lightness_linear_set_t; + +/** Parameter of Light Lightness Default state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light Lightness Default state */ +} esp_ble_mesh_state_change_light_lightness_default_set_t; + +/** Parameters of Light Lightness Range state change event */ +typedef struct { + uint16_t range_min; /*!< The minimum value of Light Lightness Range state */ + uint16_t range_max; /*!< The maximum value of Light Lightness Range state */ +} esp_ble_mesh_state_change_light_lightness_range_set_t; + +/** Parameters of Light CTL state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light CTL Lightness state */ + uint16_t temperature; /*!< The value of Light CTL Temperature state */ + int16_t delta_uv; /*!< The value of Light CTL Delta UV state */ +} esp_ble_mesh_state_change_light_ctl_set_t; + +/** Parameters of Light CTL Temperature state change event */ +typedef struct { + uint16_t temperature; /*!< The value of Light CTL Temperature state */ + int16_t delta_uv; /*!< The value of Light CTL Delta UV state */ +} esp_ble_mesh_state_change_light_ctl_temperature_set_t; + +/** Parameters of Light CTL Temperature Range state change event */ +typedef struct { + uint16_t range_min; /*!< The minimum value of Light CTL Temperature Range state */ + uint16_t range_max; /*!< The maximum value of Light CTL Temperature Range state */ +} esp_ble_mesh_state_change_light_ctl_temperature_range_set_t; + +/** Parameters of Light CTL Default state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light Lightness Default state */ + uint16_t temperature; /*!< The value of Light CTL Temperature Default state */ + int16_t delta_uv; /*!< The value of Light CTL Delta UV Default state */ +} esp_ble_mesh_state_change_light_ctl_default_set_t; + +/** Parameters of Light HSL state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light HSL Lightness state */ + uint16_t hue; /*!< The value of Light HSL Hue state */ + uint16_t saturation; /*!< The value of Light HSL Saturation state */ +} esp_ble_mesh_state_change_light_hsl_set_t; + +/** Parameter of Light HSL Hue state change event */ +typedef struct { + uint16_t hue; /*!< The value of Light HSL Hue state */ +} esp_ble_mesh_state_change_light_hsl_hue_set_t; + +/** Parameter of Light HSL Saturation state change event */ +typedef struct { + uint16_t saturation; /*!< The value of Light HSL Saturation state */ +} esp_ble_mesh_state_change_light_hsl_saturation_set_t; + +/** Parameters of Light HSL Default state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light HSL Lightness Default state */ + uint16_t hue; /*!< The value of Light HSL Hue Default state */ + uint16_t saturation; /*!< The value of Light HSL Saturation Default state */ +} esp_ble_mesh_state_change_light_hsl_default_set_t; + +/** Parameters of Light HSL Range state change event */ +typedef struct { + uint16_t hue_range_min; /*!< The minimum hue value of Light HSL Range state */ + uint16_t hue_range_max; /*!< The maximum hue value of Light HSL Range state */ + uint16_t saturation_range_min; /*!< The minimum saturation value of Light HSL Range state */ + uint16_t saturation_range_max; /*!< The maximum saturation value of Light HSL Range state */ +} esp_ble_mesh_state_change_light_hsl_range_set_t; + +/** Parameters of Light xyL state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light xyL Lightness state */ + uint16_t x; /*!< The value of Light xyL x state */ + uint16_t y; /*!< The value of Light xyL y state */ +} esp_ble_mesh_state_change_light_xyl_set_t; + +/** Parameters of Light xyL Default state change event */ +typedef struct { + uint16_t lightness; /*!< The value of Light Lightness Default state */ + uint16_t x; /*!< The value of Light xyL x Default state */ + uint16_t y; /*!< The value of Light xyL y Default state */ +} esp_ble_mesh_state_change_light_xyl_default_set_t; + +/** Parameters of Light xyL Range state change event */ +typedef struct { + uint16_t x_range_min; /*!< The minimum value of Light xyL x Range state */ + uint16_t x_range_max; /*!< The maximum value of Light xyL x Range state */ + uint16_t y_range_min; /*!< The minimum value of Light xyL y Range state */ + uint16_t y_range_max; /*!< The maximum value of Light xyL y Range state */ +} esp_ble_mesh_state_change_light_xyl_range_set_t; + +/** Parameter of Light LC Mode state change event */ +typedef struct { + uint8_t mode; /*!< The value of Light LC Mode state */ +} esp_ble_mesh_state_change_light_lc_mode_set_t; + +/** Parameter of Light LC Occupancy Mode state change event */ +typedef struct { + uint8_t mode; /*!< The value of Light LC Occupancy Mode state */ +} esp_ble_mesh_state_change_light_lc_om_set_t; + +/** Parameter of Light LC Light OnOff state change event */ +typedef struct { + uint8_t onoff; /*!< The value of Light LC Light OnOff state */ +} esp_ble_mesh_state_change_light_lc_light_onoff_set_t; + +/** Parameters of Light LC Property state change event */ +typedef struct { + uint16_t property_id; /*!< The property id of Light LC Property state */ + struct net_buf_simple *property_value; /*!< The property value of Light LC Property state */ +} esp_ble_mesh_state_change_light_lc_property_set_t; + +/** Parameters of Sensor Status state change event */ +typedef struct { + uint16_t property_id; /*!< The value of Sensor Property ID */ + /** Parameters of Sensor Status related state */ + union { + uint8_t occupancy; /*!< The value of Light LC Occupancy state */ + uint32_t set_occupancy_to_1_delay; /*!< The value of Light LC Set Occupancy to 1 Delay state */ + uint32_t ambient_luxlevel; /*!< The value of Light LC Ambient Luxlevel state */ + } state; +} esp_ble_mesh_state_change_sensor_status_t; + +/** + * @brief Lighting Server Model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_light_lightness_set_t lightness_set; /*!< Light Lightness Set */ + esp_ble_mesh_state_change_light_lightness_linear_set_t lightness_linear_set; /*!< Light Lightness Linear Set */ + esp_ble_mesh_state_change_light_lightness_default_set_t lightness_default_set; /*!< Light Lightness Default Set */ + esp_ble_mesh_state_change_light_lightness_range_set_t lightness_range_set; /*!< Light Lightness Range Set */ + esp_ble_mesh_state_change_light_ctl_set_t ctl_set; /*!< Light CTL Set */ + esp_ble_mesh_state_change_light_ctl_temperature_set_t ctl_temp_set; /*!< Light CTL Temperature Set */ + esp_ble_mesh_state_change_light_ctl_temperature_range_set_t ctl_temp_range_set; /*!< Light CTL Temperature Range Set */ + esp_ble_mesh_state_change_light_ctl_default_set_t ctl_default_set; /*!< Light CTL Default Set */ + esp_ble_mesh_state_change_light_hsl_set_t hsl_set; /*!< Light HSL Set */ + esp_ble_mesh_state_change_light_hsl_hue_set_t hsl_hue_set; /*!< Light HSL Hue Set */ + esp_ble_mesh_state_change_light_hsl_saturation_set_t hsl_saturation_set; /*!< Light HSL Saturation Set */ + esp_ble_mesh_state_change_light_hsl_default_set_t hsl_default_set; /*!< Light HSL Default Set */ + esp_ble_mesh_state_change_light_hsl_range_set_t hsl_range_set; /*!< Light HSL Range Set */ + esp_ble_mesh_state_change_light_xyl_set_t xyl_set; /*!< Light xyL Set */ + esp_ble_mesh_state_change_light_xyl_default_set_t xyl_default_set; /*!< Light xyL Default Set */ + esp_ble_mesh_state_change_light_xyl_range_set_t xyl_range_set; /*!< Light xyL Range Set */ + esp_ble_mesh_state_change_light_lc_mode_set_t lc_mode_set; /*!< Light LC Mode Set */ + esp_ble_mesh_state_change_light_lc_om_set_t lc_om_set; /*!< Light LC Occupancy Mode Set */ + esp_ble_mesh_state_change_light_lc_light_onoff_set_t lc_light_onoff_set; /*!< Light LC Light OnOff Set */ + esp_ble_mesh_state_change_light_lc_property_set_t lc_property_set; /*!< Light LC Property Set */ + esp_ble_mesh_state_change_sensor_status_t sensor_status; /*!< Sensor Status */ +} esp_ble_mesh_lighting_server_state_change_t; + +/** Context of the received Light LC Property Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ +} esp_ble_mesh_server_recv_light_lc_property_get_t; + +/** + * @brief Lighting Server Model received get message union + */ +typedef union { + esp_ble_mesh_server_recv_light_lc_property_get_t lc_property; /*!< Light LC Property Get */ +} esp_ble_mesh_lighting_server_recv_get_msg_t; + +/** Context of the received Light Lightness Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness actual state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_lightness_set_t; + +/** Context of the received Light Lightness Linear Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light lightness linear state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_lightness_linear_set_t; + +/** Context of the received Light Lightness Default Set message */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Default state */ +} esp_ble_mesh_server_recv_light_lightness_default_set_t; + +/** Context of the received Light Lightness Range Set message */ +typedef struct { + uint16_t range_min; /*!< Value of range min field of light lightness range state */ + uint16_t range_max; /*!< Value of range max field of light lightness range state */ +} esp_ble_mesh_server_recv_light_lightness_range_set_t; + +/** Context of the received Light CTL Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light ctl lightness state */ + uint16_t temperature; /*!< Target value of light ctl temperature state */ + int16_t delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_ctl_set_t; + +/** Context of the received Light CTL Temperature Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t temperature; /*!< Target value of light ctl temperature state */ + int16_t delta_uv; /*!< Target value of light ctl delta UV state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_ctl_temperature_set_t; + +/** Context of the received Light CTL Temperature Range Set message */ +typedef struct { + uint16_t range_min; /*!< Value of temperature range min field of light ctl temperature range state */ + uint16_t range_max; /*!< Value of temperature range max field of light ctl temperature range state */ +} esp_ble_mesh_server_recv_light_ctl_temperature_range_set_t; + +/** Context of the received Light CTL Default Set message */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t temperature; /*!< Value of light temperature default state */ + int16_t delta_uv; /*!< Value of light delta UV default state */ +} esp_ble_mesh_server_recv_light_ctl_default_set_t; + +/** Context of the received Light HSL Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t lightness; /*!< Target value of light hsl lightness state */ + uint16_t hue; /*!< Target value of light hsl hue state */ + uint16_t saturation; /*!< Target value of light hsl saturation state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_hsl_set_t; + +/** Context of the received Light HSL Hue Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t hue; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_hsl_hue_set_t; + +/** Context of the received Light HSL Saturation Set message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t saturation; /*!< Target value of light hsl hue state */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_hsl_saturation_set_t; + +/** Context of the received Light HSL Default Set message */ +typedef struct { + uint16_t lightness; /*!< Value of light lightness default state */ + uint16_t hue; /*!< Value of light hue default state */ + uint16_t saturation; /*!< Value of light saturation default state */ +} esp_ble_mesh_server_recv_light_hsl_default_set_t; + +/** Context of the received Light HSL Range Set message */ +typedef struct { + uint16_t hue_range_min; /*!< Value of hue range min field of light hsl hue range state */ + uint16_t hue_range_max; /*!< Value of hue range max field of light hsl hue range state */ + uint16_t saturation_range_min; /*!< Value of saturation range min field of light hsl saturation range state */ + uint16_t saturation_range_max; /*!< Value of saturation range max field of light hsl saturation range state */ +} esp_ble_mesh_server_recv_light_hsl_range_set_t; + +/** Context of the received Light xyL Set message */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint16_t lightness; /*!< The target value of the Light xyL Lightness state */ + uint16_t x; /*!< The target value of the Light xyL x state */ + uint16_t y; /*!< The target value of the Light xyL y state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_xyl_set_t; + +/** Context of the received Light xyL Default Set message */ +typedef struct { + uint16_t lightness; /*!< The value of the Light Lightness Default state */ + uint16_t x; /*!< The value of the Light xyL x Default state */ + uint16_t y; /*!< The value of the Light xyL y Default state */ +} esp_ble_mesh_server_recv_light_xyl_default_set_t; + +/** Context of the received Light xyl Range Set message */ +typedef struct { + uint16_t x_range_min; /*!< The value of the xyL x Range Min field of the Light xyL x Range state */ + uint16_t x_range_max; /*!< The value of the xyL x Range Max field of the Light xyL x Range state */ + uint16_t y_range_min; /*!< The value of the xyL y Range Min field of the Light xyL y Range state */ + uint16_t y_range_max; /*!< The value of the xyL y Range Max field of the Light xyL y Range state */ +} esp_ble_mesh_server_recv_light_xyl_range_set_t; + +/** Context of the received Light LC Mode Set message */ +typedef struct { + uint8_t mode; /*!< The target value of the Light LC Mode state */ +} esp_ble_mesh_server_recv_light_lc_mode_set_t; + +/** Context of the received Light OM Set message */ +typedef struct { + uint8_t mode; /*!< The target value of the Light LC Occupancy Mode state */ +} esp_ble_mesh_server_recv_light_lc_om_set_t; + +/** Context of the received Light LC Light OnOff Set message */ +typedef struct { + bool op_en; /*!< Indicate whether optional parameters included */ + uint8_t light_onoff; /*!< The target value of the Light LC Light OnOff state */ + uint8_t tid; /*!< Transaction Identifier */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_light_lc_light_onoff_set_t; + +/** Context of the received Light LC Property Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a Light LC Property */ + struct net_buf_simple *property_value; /*!< Raw value for the Light LC Property */ +} esp_ble_mesh_server_recv_light_lc_property_set_t; + +/** + * @brief Lighting Server Model received set message union + */ +typedef union { + esp_ble_mesh_server_recv_light_lightness_set_t lightness; /*!< Light Lightness Set/Light Lightness Set Unack */ + esp_ble_mesh_server_recv_light_lightness_linear_set_t lightness_linear; /*!< Light Lightness Linear Set/Light Lightness Linear Set Unack */ + esp_ble_mesh_server_recv_light_lightness_default_set_t lightness_default; /*!< Light Lightness Default Set/Light Lightness Default Set Unack */ + esp_ble_mesh_server_recv_light_lightness_range_set_t lightness_range; /*!< Light Lightness Range Set/Light Lightness Range Set Unack */ + esp_ble_mesh_server_recv_light_ctl_set_t ctl; /*!< Light CTL Set/Light CTL Set Unack */ + esp_ble_mesh_server_recv_light_ctl_temperature_set_t ctl_temp; /*!< Light CTL Temperature Set/Light CTL Temperature Set Unack */ + esp_ble_mesh_server_recv_light_ctl_temperature_range_set_t ctl_temp_range; /*!< Light CTL Temperature Range Set/Light CTL Temperature Range Set Unack */ + esp_ble_mesh_server_recv_light_ctl_default_set_t ctl_default; /*!< Light CTL Default Set/Light CTL Default Set Unack */ + esp_ble_mesh_server_recv_light_hsl_set_t hsl; /*!< Light HSL Set/Light HSL Set Unack */ + esp_ble_mesh_server_recv_light_hsl_hue_set_t hsl_hue; /*!< Light HSL Hue Set/Light HSL Hue Set Unack */ + esp_ble_mesh_server_recv_light_hsl_saturation_set_t hsl_saturation; /*!< Light HSL Saturation Set/Light HSL Saturation Set Unack */ + esp_ble_mesh_server_recv_light_hsl_default_set_t hsl_default; /*!< Light HSL Default Set/Light HSL Default Set Unack */ + esp_ble_mesh_server_recv_light_hsl_range_set_t hsl_range; /*!< Light HSL Range Set/Light HSL Range Set Unack */ + esp_ble_mesh_server_recv_light_xyl_set_t xyl; /*!< Light xyL Set/Light xyL Set Unack */ + esp_ble_mesh_server_recv_light_xyl_default_set_t xyl_default; /*!< Light xyL Default Set/Light xyL Default Set Unack */ + esp_ble_mesh_server_recv_light_xyl_range_set_t xyl_range; /*!< Light xyL Range Set/Light xyL Range Set Unack */ + esp_ble_mesh_server_recv_light_lc_mode_set_t lc_mode; /*!< Light LC Mode Set/Light LC Mode Set Unack */ + esp_ble_mesh_server_recv_light_lc_om_set_t lc_om; /*!< Light LC OM Set/Light LC OM Set Unack */ + esp_ble_mesh_server_recv_light_lc_light_onoff_set_t lc_light_onoff; /*!< Light LC Light OnOff Set/Light LC Light OnOff Set Unack */ + esp_ble_mesh_server_recv_light_lc_property_set_t lc_property; /*!< Light LC Property Set/Light LC Property Set Unack */ +} esp_ble_mesh_lighting_server_recv_set_msg_t; + +/** Context of the received Sensor Status message */ +typedef struct { + struct net_buf_simple *data; /*!< Value of sensor data state (optional) */ +} esp_ble_mesh_server_recv_sensor_status_t; + +/** + * @brief Lighting Server Model received status message union + */ +typedef union { + esp_ble_mesh_server_recv_sensor_status_t sensor_status; /*!< Sensor Status */ +} esp_ble_mesh_lighting_server_recv_status_msg_t; + +/** + * @brief Lighting Server Model callback value union + */ +typedef union { + esp_ble_mesh_lighting_server_state_change_t state_change; /*!< ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT */ + esp_ble_mesh_lighting_server_recv_get_msg_t get; /*!< ESP_BLE_MESH_LIGHTING_SERVER_RECV_GET_MSG_EVT */ + esp_ble_mesh_lighting_server_recv_set_msg_t set; /*!< ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT */ + esp_ble_mesh_lighting_server_recv_status_msg_t status; /*!< ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT */ +} esp_ble_mesh_lighting_server_cb_value_t; + +/** Lighting Server Model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Lighting Server Models */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received messages */ + esp_ble_mesh_lighting_server_cb_value_t value; /*!< Value of the received Lighting Messages */ +} esp_ble_mesh_lighting_server_cb_param_t; + +/** This enum value is the event of Lighting Server Model */ +typedef enum { + /** + * 1. When get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, no event will be + * callback to the application layer when Lighting Get messages are received. + * 2. When set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, this event will + * be callback to the application layer when Lighting Set/Set Unack messages + * are received. + */ + ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT, + /** + * When get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Lighting Get messages are received. + */ + ESP_BLE_MESH_LIGHTING_SERVER_RECV_GET_MSG_EVT, + /** + * When set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Lighting Set/Set Unack messages are received. + */ + ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT, + /** + * When status_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will + * be callback to the application layer when Sensor Status message is received. + */ + ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT, + ESP_BLE_MESH_LIGHTING_SERVER_EVT_MAX, +} esp_ble_mesh_lighting_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Lighting Server Model function. + */ + +/** + * @brief Lighting Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_lighting_server_cb_t)(esp_ble_mesh_lighting_server_cb_event_t event, + esp_ble_mesh_lighting_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Lighting Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_lighting_server_callback(esp_ble_mesh_lighting_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_LIGHTING_MODEL_API_H_ */ + diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h new file mode 100644 index 000000000..5c43efe18 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h @@ -0,0 +1,719 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Sensor Client Model APIs. + */ + +#ifndef _ESP_BLE_MESH_SENSOR_MODEL_API_H_ +#define _ESP_BLE_MESH_SENSOR_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_SENSOR_CLI + * + * @brief Define a new Sensor Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Sensor Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Sensor Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_SENSOR_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_CLI, \ + NULL, cli_pub, cli_data) + +/** + * @brief Bluetooth Mesh Sensor Client Model Get and Set parameters structure. + */ + +/** Parameters of Sensor Descriptor Get */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID of a sensor (optional) */ +} esp_ble_mesh_sensor_descriptor_get_t; + +/** Parameter of Sensor Cadence Get */ +typedef struct { + uint16_t property_id; /*!< Property ID of a sensor */ +} esp_ble_mesh_sensor_cadence_get_t; + +/** Parameters of Sensor Cadence Set */ +typedef struct { + uint16_t property_id; /*!< Property ID for the sensor */ + uint8_t fast_cadence_period_divisor : 7, /*!< Divisor for the publish period */ + status_trigger_type : 1; /*!< The unit and format of the Status Trigger Delta fields */ + struct net_buf_simple *status_trigger_delta_down; /*!< Delta down value that triggers a status message */ + struct net_buf_simple *status_trigger_delta_up; /*!< Delta up value that triggers a status message */ + uint8_t status_min_interval; /*!< Minimum interval between two consecutive Status messages */ + struct net_buf_simple *fast_cadence_low; /*!< Low value for the fast cadence range */ + struct net_buf_simple *fast_cadence_high; /*!< Fast value for the fast cadence range */ +} esp_ble_mesh_sensor_cadence_set_t; + +/** Parameter of Sensor Settings Get */ +typedef struct { + uint16_t sensor_property_id; /*!< Property ID of a sensor */ +} esp_ble_mesh_sensor_settings_get_t; + +/** Parameters of Sensor Setting Get */ +typedef struct { + uint16_t sensor_property_id; /*!< Property ID of a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ +} esp_ble_mesh_sensor_setting_get_t; + +/** Parameters of Sensor Setting Set */ +typedef struct { + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + struct net_buf_simple *sensor_setting_raw; /*!< Raw value for the setting */ +} esp_ble_mesh_sensor_setting_set_t; + +/** Parameters of Sensor Get */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID for the sensor (optional) */ +} esp_ble_mesh_sensor_get_t; + +/** Parameters of Sensor Column Get */ +typedef struct { + uint16_t property_id; /*!< Property identifying a sensor */ + struct net_buf_simple *raw_value_x; /*!< Raw value identifying a column */ +} esp_ble_mesh_sensor_column_get_t; + +/** Parameters of Sensor Series Get */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property identifying a sensor */ + struct net_buf_simple *raw_value_x1; /*!< Raw value identifying a starting column (optional) */ + struct net_buf_simple *raw_value_x2; /*!< Raw value identifying an ending column (C.1) */ +} esp_ble_mesh_sensor_series_get_t; + +/** + * @brief Sensor Client Model get message union + */ +typedef union { + esp_ble_mesh_sensor_descriptor_get_t descriptor_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET */ + esp_ble_mesh_sensor_cadence_get_t cadence_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET */ + esp_ble_mesh_sensor_settings_get_t settings_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET */ + esp_ble_mesh_sensor_setting_get_t setting_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET */ + esp_ble_mesh_sensor_get_t sensor_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_GET */ + esp_ble_mesh_sensor_column_get_t column_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET */ + esp_ble_mesh_sensor_series_get_t series_get; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET */ +} esp_ble_mesh_sensor_client_get_state_t; + +/** + * @brief Sensor Client Model set message union + */ +typedef union { + esp_ble_mesh_sensor_cadence_set_t cadence_set; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET & ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK */ + esp_ble_mesh_sensor_setting_set_t setting_set; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET & ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK */ +} esp_ble_mesh_sensor_client_set_state_t; + +/** + * @brief Bluetooth Mesh Sensor Client Model Get and Set callback parameters structure. + */ + +/** Parameter of Sensor Descriptor Status */ +typedef struct { + struct net_buf_simple *descriptor; /*!< Sequence of 8-octet sensor descriptors (optional) */ +} esp_ble_mesh_sensor_descriptor_status_cb_t; + +/** Parameters of Sensor Cadence Status */ +typedef struct { + uint16_t property_id; /*!< Property for the sensor */ + struct net_buf_simple *sensor_cadence_value; /*!< Value of sensor cadence state */ +} esp_ble_mesh_sensor_cadence_status_cb_t; + +/** Parameters of Sensor Settings Status */ +typedef struct { + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ + struct net_buf_simple *sensor_setting_property_ids; /*!< A sequence of N sensor setting property IDs (optional) */ +} esp_ble_mesh_sensor_settings_status_cb_t; + +/** Parameters of Sensor Setting Status */ +typedef struct { + bool op_en; /*!< Indicate id optional parameters are included */ + uint16_t sensor_property_id; /*!< Property ID identifying a sensor */ + uint16_t sensor_setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + uint8_t sensor_setting_access; /*!< Read/Write access rights for the setting (optional) */ + struct net_buf_simple *sensor_setting_raw; /*!< Raw value for the setting */ +} esp_ble_mesh_sensor_setting_status_cb_t; + +/** Parameter of Sensor Status */ +typedef struct { + struct net_buf_simple *marshalled_sensor_data; /*!< Value of sensor data state (optional) */ +} esp_ble_mesh_sensor_status_cb_t; + +/** Parameters of Sensor Column Status */ +typedef struct { + uint16_t property_id; /*!< Property identifying a sensor and the Y axis */ + struct net_buf_simple *sensor_column_value; /*!< Left values of sensor column status */ +} esp_ble_mesh_sensor_column_status_cb_t; + +/** Parameters of Sensor Series Status */ +typedef struct { + uint16_t property_id; /*!< Property identifying a sensor and the Y axis */ + struct net_buf_simple *sensor_series_value; /*!< Left values of sensor series status */ +} esp_ble_mesh_sensor_series_status_cb_t; + +/** + * @brief Sensor Client Model received message union + */ +typedef union { + esp_ble_mesh_sensor_descriptor_status_cb_t descriptor_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS */ + esp_ble_mesh_sensor_cadence_status_cb_t cadence_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS */ + esp_ble_mesh_sensor_settings_status_cb_t settings_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS */ + esp_ble_mesh_sensor_setting_status_cb_t setting_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS */ + esp_ble_mesh_sensor_status_cb_t sensor_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS */ + esp_ble_mesh_sensor_column_status_cb_t column_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS */ + esp_ble_mesh_sensor_series_status_cb_t series_status; /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS */ +} esp_ble_mesh_sensor_client_status_cb_t; + +/** Sensor Client Model callback parameters */ +typedef struct { + int error_code; /*!< 0: success, + * otherwise failure. For the error code values please refer to errno.h file. + * A negative sign is added to the standard error codes in errno.h. */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + esp_ble_mesh_sensor_client_status_cb_t status_cb; /*!< The sensor status message callback values */ +} esp_ble_mesh_sensor_client_cb_param_t; + +/** This enum value is the event of Sensor Client Model */ +typedef enum { + ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_SENSOR_CLIENT_EVT_MAX, +} esp_ble_mesh_sensor_client_cb_event_t; + +/** + * @brief Bluetooth Mesh Sensor Client Model function. + */ + +/** + * @brief Sensor Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_sensor_client_cb_t)(esp_ble_mesh_sensor_client_cb_event_t event, + esp_ble_mesh_sensor_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Sensor Client Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_sensor_client_callback(esp_ble_mesh_sensor_client_cb_t callback); + +/** + * @brief Get the value of Sensor Server Model states using the Sensor Client Model get messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_sensor_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer to sensor get message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_sensor_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sensor_client_get_state_t *get_state); + +/** + * @brief Set the value of Sensor Server Model states using the Sensor Client Model set messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_sensor_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer to sensor set message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_sensor_client_set_state_t *set_state); + +/** + * @brief Sensor Server Models related context. + */ + +/** @def ESP_BLE_MESH_MODEL_SENSOR_SRV + * + * @brief Define a new Sensor Server Model. + * + * @note 1. The Sensor Server model is a root model. When this model is present + * on an element, the corresponding Sensor Setup Server model shall + * also be present. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_sensor_srv_t. + * + * @return New Sensor Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SENSOR_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SENSOR_SETUP_SRV + * + * @brief Define a new Sensor Setup Server Model. + * + * @note 1. The Sensor Setup Server model extends the Sensor Server model. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_sensor_setup_srv_t. + * + * @return New Sensor Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SENSOR_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +#define ESP_BLE_MESH_INVALID_SENSOR_PROPERTY_ID 0x0000 /*!< Invalid Sensor Property ID */ + +#define ESP_BLE_MESH_SENSOR_PROPERTY_ID_LEN 0x02 /*!< Length of Sensor Property ID */ + +#define ESP_BLE_MESH_SENSOR_DESCRIPTOR_LEN 0x08 /*!< Length of Sensor Descriptor state */ + +#define ESP_BLE_MESH_SENSOR_UNSPECIFIED_POS_TOLERANCE 0x000 /*!< Unspecified Sensor Positive Tolerance */ +#define ESP_BLE_MESH_SENSOR_UNSPECIFIED_NEG_TOLERANCE 0x000 /*!< Unspecified Sensor Negative Tolerance */ + +#define ESP_BLE_MESH_SENSOR_NOT_APPL_MEASURE_PERIOD 0x00 /*!< Not applicable Sensor Measurement Period */ + +#define ESP_BLE_MESH_SENSOR_NOT_APPL_UPDATE_INTERVAL 0x00 /*!< Not applicable Sensor Update Interval */ + +#define ESP_BLE_MESH_INVALID_SENSOR_SETTING_PROPERTY_ID 0x0000 /*!< Invalid Sensor Setting Property ID */ + +#define ESP_BLE_MESH_SENSOR_SETTING_PROPERTY_ID_LEN 0x02 /*!< Length of Sensor Setting Property ID */ +#define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_LEN 0x01 /*!< Length of Sensor Setting Access */ + +#define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_READ 0x01 /*!< Sensor Setting Access - Read */ +#define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_READ_WRITE 0x03 /*!< Sensor Setting Access - Read & Write */ + +#define ESP_BLE_MESH_SENSOR_DIVISOR_TRIGGER_TYPE_LEN 0x01 /*!< Length of Sensor Divisor Trigger Type */ +#define ESP_BLE_MESH_SENSOR_STATUS_MIN_INTERVAL_LEN 0x01 /*!< Length of Sensor Status Min Interval */ + +#define ESP_BLE_MESH_SENSOR_PERIOD_DIVISOR_MAX_VALUE 15 /*!< Maximum value of Sensor Period Divisor */ + +#define ESP_BLE_MESH_SENSOR_STATUS_MIN_INTERVAL_MAX 26 /*!< Maximum value of Sensor Status Min Interval */ + +/** + * Sensor Status Trigger Type - Format Type of the characteristic + * that the Sensor Property ID state references + */ +#define ESP_BLE_MESH_SENSOR_STATUS_TRIGGER_TYPE_CHAR 0 +/** Sensor Status Trigger Type - Format Type "uint16" */ +#define ESP_BLE_MESH_SENSOR_STATUS_TRIGGER_TYPE_UINT16 1 + +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A 0x00 /*!< Sensor Data Format A */ +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B 0x01 /*!< Sensor Data Format B */ + +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID_LEN 0x02 /*!< MPID length of Sensor Data Format A */ +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID_LEN 0x03 /*!< MPID length of Sensor Data Format B */ + +/** + * Zero length of Sensor Data. + * + * Note: + * The Length field is a 1-based uint7 value (valid range 0x0–0x7F, + * representing range of 1–127). The value 0x7F represents a length + * of zero. + */ +#define ESP_BLE_MESH_SENSOR_DATA_ZERO_LEN 0x7F + +/** @def ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT + * + * @brief Get format of the sensor data. + * + * @note Multiple sensor data may be concatenated. Make sure the _data pointer is + * updated before getting the format of the corresponding sensor data. + * + * @param _data Pointer to the start of the sensor data. + * + * @return Format of the sensor data. + */ +#define ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT(_data) (((_data)[0]) & BIT_MASK(1)) + +/** @def ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH + * + * @brief Get length of the sensor data. + * + * @note Multiple sensor data may be concatenated. Make sure the _data pointer is + * updated before getting the length of the corresponding sensor data. + * + * @param _data Pointer to the start of the sensor data. + * @param _fmt Format of the sensor data. + * + * @return Length (zero-based) of the sensor data. + */ +#define ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH(_data, _fmt) \ + (((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[0]) >> 1) & BIT_MASK(4)) : ((((_data)[0]) >> 1) & BIT_MASK(7))) + +/** @def ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID + * + * @brief Get Sensor Property ID of the sensor data. + * + * @note Multiple sensor data may be concatenated. Make sure the _data pointer is + * updated before getting Sensor Property ID of the corresponding sensor data. + * + * @param _data Pointer to the start of the sensor data. + * @param _fmt Format of the sensor data. + * + * @return Sensor Property ID of the sensor data. + */ +#define ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID(_data, _fmt) \ + (((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[1]) << 3) | (((_data)[0]) >> 5)) : ((((_data)[2]) << 8) | ((_data)[1]))) + +/** @def ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID + * + * @brief Generate a MPID value for sensor data with Format A. + * + * @note 1. The Format field is 0b0 and indicates that Format A is used. + * 2. The Length field is a 1-based uint4 value (valid range 0x0–0xF, + * representing range of 1–16). + * 3. The Property ID is an 11-bit bit field representing 11 LSb of a Property ID. + * 4. This format may be used for Property Values that are not longer than 16 + * octets and for Property IDs less than 0x0800. + * + * @param _len Length of Sensor Raw value. + * @param _id Sensor Property ID. + * + * @return 2-octet MPID value for sensor data with Format A. + * + */ +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID(_len, _id) \ + ((((_id) & BIT_MASK(11)) << 5) | (((_len) & BIT_MASK(4)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) + +/** @def ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID + * + * @brief Generate a MPID value for sensor data with Format B. + * + * @note 1. The Format field is 0b1 and indicates Format B is used. + * 2. The Length field is a 1-based uint7 value (valid range 0x0–0x7F, representing + * range of 1–127). The value 0x7F represents a length of zero. + * 3. The Property ID is a 16-bit bit field representing a Property ID. + * 4. This format may be used for Property Values not longer than 128 octets and for + * any Property IDs. Property values longer than 128 octets are not supported by + * the Sensor Status message. + * 5. Exclude the generated 1-octet value, the 2-octet Sensor Property ID + * + * @param _len Length of Sensor Raw value. + * @param _id Sensor Property ID. + * + * @return 3-octet MPID value for sensor data with Format B. + * + */ +#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID(_len, _id) \ + (((_id) << 8) | (((_len) & BIT_MASK(7)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_B) + +/** This enum value is value of Sensor Sampling Function */ +enum esp_ble_mesh_sensor_sample_func { + ESP_BLE_MESH_SAMPLE_FUNC_UNSPECIFIED, + ESP_BLE_MESH_SAMPLE_FUNC_INSTANTANEOUS, + ESP_BLE_MESH_SAMPLE_FUNC_ARITHMETIC_MEAN, + ESP_BLE_MESH_SAMPLE_FUNC_RMS, + ESP_BLE_MESH_SAMPLE_FUNC_MAXIMUM, + ESP_BLE_MESH_SAMPLE_FUNC_MINIMUM, + ESP_BLE_MESH_SAMPLE_FUNC_ACCUMULATED, + ESP_BLE_MESH_SAMPLE_FUNC_COUNT, +}; + +/** Parameters of Sensor Descriptor state */ +typedef struct { + uint32_t positive_tolerance : 12, /*!< The value of Sensor Positive Tolerance field */ + negative_tolerance : 12, /*!< The value of Sensor Negative Tolerance field */ + sampling_function : 8; /*!< The value of Sensor Sampling Function field */ + uint8_t measure_period; /*!< The value of Sensor Measurement Period field */ + uint8_t update_interval; /*!< The value of Sensor Update Interval field */ +} esp_ble_mesh_sensor_descriptor_t; + +/** Parameters of Sensor Setting state */ +typedef struct { + uint16_t property_id; /*!< The value of Sensor Setting Property ID field */ + uint8_t access; /*!< The value of Sensor Setting Access field */ + struct net_buf_simple *raw; /*!< The value of Sensor Setting Raw field */ +} esp_ble_mesh_sensor_setting_t; + +/** Parameters of Sensor Cadence state */ +typedef struct { + uint8_t period_divisor : 7, /*!< The value of Fast Cadence Period Divisor field */ + trigger_type : 1; /*!< The value of Status Trigger Type field */ + /** + * Note: + * The parameter "size" in trigger_delta_down, trigger_delta_up, fast_cadence_low & + * fast_cadence_high indicates the exact length of these four parameters, and they + * are associated with the Sensor Property ID. Users need to initialize the "size" + * precisely. + */ + struct net_buf_simple *trigger_delta_down; /*!< The value of Status Trigger Delta Down field */ + struct net_buf_simple *trigger_delta_up; /*!< The value of Status Trigger Delta Up field */ + uint8_t min_interval; /*!< The value of Status Min Interval field */ + struct net_buf_simple *fast_cadence_low; /*!< The value of Fast Cadence Low field */ + struct net_buf_simple *fast_cadence_high; /*!< The value of Fast Cadence High field */ +} esp_ble_mesh_sensor_cadence_t; + +/** Parameters of Sensor Data state */ +typedef struct { + /** + * Format A: The Length field is a 1-based uint4 value (valid range 0x0–0xF, + * representing range of 1 – 16). + * Format B: The Length field is a 1-based uint7 value (valid range 0x0–0x7F, + * representing range of 1 – 127). The value 0x7F represents a + * length of zero. + */ + uint8_t format : 1, /*!< The value of the Sensor Data format */ + length : 7; /*!< The value of the Sensor Data length */ + struct net_buf_simple *raw_value; /*!< The value of Sensor Data raw value */ +} esp_ble_mesh_sensor_data_t; + +/** Parameters of Sensor Series Column state */ +typedef struct { + struct net_buf_simple *raw_value_x; /*!< The value of Sensor Raw Value X field */ + struct net_buf_simple *column_width; /*!< The value of Sensor Column Width field */ + struct net_buf_simple *raw_value_y; /*!< The value of Sensor Raw Value Y field */ +} esp_ble_mesh_sensor_series_column_t; + +/** Parameters of Sensor states */ +typedef struct { + uint16_t sensor_property_id; /*!< The value of Sensor Property ID field */ + + /* Constant throughout the lifetime of an element */ + esp_ble_mesh_sensor_descriptor_t descriptor; /*!< Parameters of the Sensor Descriptor state */ + + /** + * Multiple Sensor Setting states may be present for each sensor. + * The Sensor Setting Property ID values shall be unique for each + * Sensor Property ID that identifies a sensor within an element. + */ + const uint8_t setting_count; /*!< */ + esp_ble_mesh_sensor_setting_t *settings; /*!< Parameters of the Sensor Setting state */ + + /** + * The Sensor Cadence state may be not supported by sensors based + * on device properties referencing "non-scalar characteristics" + * such as "histograms" or "composite characteristics". + */ + esp_ble_mesh_sensor_cadence_t *cadence; /*!< Parameters of the Sensor Cadence state */ + + esp_ble_mesh_sensor_data_t sensor_data; /*!< Parameters of the Sensor Data state */ + + esp_ble_mesh_sensor_series_column_t series_column; /*!< Parameters of the Sensor Series Column state */ +} esp_ble_mesh_sensor_state_t; + +/** User data of Sensor Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Sensor Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + const uint8_t state_count; /*!< Sensor state count */ + esp_ble_mesh_sensor_state_t *states; /*!< Parameters of the Sensor states */ +} esp_ble_mesh_sensor_srv_t; + +/** User data of Sensor Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Sensor Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + const uint8_t state_count; /*!< Sensor state count */ + esp_ble_mesh_sensor_state_t *states; /*!< Parameters of the Sensor states */ +} esp_ble_mesh_sensor_setup_srv_t; + +/** Parameters of Sensor Cadence Set state change event */ +typedef struct { + uint16_t property_id; /*!< The value of Sensor Property ID state */ + uint8_t period_divisor : 7, /*!< The value of Fast Cadence Period Divisor state */ + trigger_type : 1; /*!< The value of Status Trigger Type state */ + struct net_buf_simple *trigger_delta_down; /*!< The value of Status Trigger Delta Down state */ + struct net_buf_simple *trigger_delta_up; /*!< The value of Status Trigger Delta Up state */ + uint8_t min_interval; /*!< The value of Status Min Interval state */ + struct net_buf_simple *fast_cadence_low; /*!< The value of Fast Cadence Low state */ + struct net_buf_simple *fast_cadence_high; /*!< The value of Fast Cadence High state */ +} esp_ble_mesh_state_change_sensor_cadence_set_t; + +/** Parameters of Sensor Setting Set state change event */ +typedef struct { + uint16_t property_id; /*!< The value of Sensor Property ID state */ + uint16_t setting_property_id; /*!< The value of Sensor Setting Property ID state */ + struct net_buf_simple *setting_value; /*!< The value of Sensor Property Value state */ +} esp_ble_mesh_state_change_sensor_setting_set_t; + +/** + * @brief Sensor Server Model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_sensor_cadence_set_t sensor_cadence_set; /*!< Sensor Cadence Set */ + esp_ble_mesh_state_change_sensor_setting_set_t sensor_setting_set; /*!< Sensor Setting Set */ +} esp_ble_mesh_sensor_server_state_change_t; + +/** Context of the received Sensor Descriptor Get message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID of a sensor (optional) */ +} esp_ble_mesh_server_recv_sensor_descriptor_get_t; + +/** Context of the received Sensor Cadence Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID of a sensor */ +} esp_ble_mesh_server_recv_sensor_cadence_get_t; + +/** Context of the received Sensor Settings Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID of a sensor */ +} esp_ble_mesh_server_recv_sensor_settings_get_t; + +/** Context of the received Sensor Setting Get message */ +typedef struct { + uint16_t property_id; /*!< Property ID of a sensor */ + uint16_t setting_property_id; /*!< Setting ID identifying a setting within a sensor */ +} esp_ble_mesh_server_recv_sensor_setting_get_t; + +/** Context of the received Sensor Get message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property ID for the sensor (optional) */ +} esp_ble_mesh_server_recv_sensor_get_t; + +/** Context of the received Sensor Column Get message */ +typedef struct { + uint16_t property_id; /*!< Property identifying a sensor */ + struct net_buf_simple *raw_value_x; /*!< Raw value identifying a column */ +} esp_ble_mesh_server_recv_sensor_column_get_t; + +/** Context of the received Sensor Series Get message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t property_id; /*!< Property identifying a sensor */ + struct net_buf_simple *raw_value; /*!< Raw value containing X1 and X2 (optional) */ +} esp_ble_mesh_server_recv_sensor_series_get_t; + +/** + * @brief Sensor Server Model received get message union + */ +typedef union { + esp_ble_mesh_server_recv_sensor_descriptor_get_t sensor_descriptor; /*!< Sensor Descriptor Get */ + esp_ble_mesh_server_recv_sensor_cadence_get_t sensor_cadence; /*!< Sensor Cadence Get */ + esp_ble_mesh_server_recv_sensor_settings_get_t sensor_settings; /*!< Sensor Settings Get */ + esp_ble_mesh_server_recv_sensor_setting_get_t sensor_setting; /*!< Sensor Setting Get */ + esp_ble_mesh_server_recv_sensor_get_t sensor_data; /*!< Sensor Get */ + esp_ble_mesh_server_recv_sensor_column_get_t sensor_column; /*!< Sensor Column Get */ + esp_ble_mesh_server_recv_sensor_series_get_t sensor_series; /*!< Sensor Series Get */ +} esp_ble_mesh_sensor_server_recv_get_msg_t; + +/** Context of the received Sensor Cadence Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID for the sensor */ + struct net_buf_simple *cadence; /*!< Value of Sensor Cadence state */ +} esp_ble_mesh_server_recv_sensor_cadence_set_t; + +/** Context of the received Sensor Setting Set message */ +typedef struct { + uint16_t property_id; /*!< Property ID identifying a sensor */ + uint16_t setting_property_id; /*!< Setting ID identifying a setting within a sensor */ + struct net_buf_simple *setting_raw; /*!< Raw value for the setting */ +} esp_ble_mesh_server_recv_sensor_setting_set_t; + +/** + * @brief Sensor Server Model received set message union + */ +typedef union { + esp_ble_mesh_server_recv_sensor_cadence_set_t sensor_cadence; /*!< Sensor Cadence Set */ + esp_ble_mesh_server_recv_sensor_setting_set_t sensor_setting; /*!< Sensor Setting Set */ +} esp_ble_mesh_sensor_server_recv_set_msg_t; + +/** + * @brief Sensor Server Model callback value union + */ +typedef union { + esp_ble_mesh_sensor_server_state_change_t state_change; /*!< ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT */ + esp_ble_mesh_sensor_server_recv_get_msg_t get; /*!< ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT */ + esp_ble_mesh_sensor_server_recv_set_msg_t set; /*!< ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT */ +} esp_ble_mesh_sensor_server_cb_value_t; + +/** Sensor Server Model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Sensor Server Models */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received messages */ + esp_ble_mesh_sensor_server_cb_value_t value; /*!< Value of the received Sensor Messages */ +} esp_ble_mesh_sensor_server_cb_param_t; + +/** This enum value is the event of Sensor Server Model */ +typedef enum { + /** + * 1. When get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, no event will be + * callback to the application layer when Sensor Get messages are received. + * 2. When set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, this event will + * be callback to the application layer when Sensor Set/Set Unack messages + * are received. + */ + ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT, + /** + * When get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Sensor Get messages are received. + */ + ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT, + /** + * When set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Sensor Set/Set Unack messages are received. + */ + ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT, + ESP_BLE_MESH_SENSOR_SERVER_EVT_MAX, +} esp_ble_mesh_sensor_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Sensor Server Model function. + */ + +/** + * @brief Sensor Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_sensor_server_cb_t)(esp_ble_mesh_sensor_server_cb_event_t event, + esp_ble_mesh_sensor_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Sensor Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_sensor_server_callback(esp_ble_mesh_sensor_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_SENSOR_MODEL_API_H_ */ + + diff --git a/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h new file mode 100644 index 000000000..f6414b709 --- /dev/null +++ b/components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h @@ -0,0 +1,920 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Time and Scene Client Model APIs. + */ + +#ifndef _ESP_BLE_MESH_TIME_SCENE_MODEL_API_H_ +#define _ESP_BLE_MESH_TIME_SCENE_MODEL_API_H_ + +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @def ESP_BLE_MESH_MODEL_TIME_CLI + * + * @brief Define a new Time Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Time Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Time Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_TIME_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_TIME_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_SCENE_CLI + * + * @brief Define a new Scene Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Scene Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Scene Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCENE_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCENE_CLI, \ + NULL, cli_pub, cli_data) + +/** @def ESP_BLE_MESH_MODEL_SCHEDULER_CLI + * + * @brief Define a new Scheduler Client Model. + * + * @note This API needs to be called for each element on which + * the application needs to have a Scheduler Client Model. + * + * @param cli_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param cli_data Pointer to the unique struct esp_ble_mesh_client_t. + * + * @return New Scheduler Client Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCHEDULER_CLI(cli_pub, cli_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCHEDULER_CLI, \ + NULL, cli_pub, cli_data) + +/** + * @brief Bluetooth Mesh Time Scene Client Model Get and Set parameters structure. + */ + +/** Parameters of Time Set */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t sub_second; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_time_set_t; + +/** Parameters of Time Zone Set */ +typedef struct { + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ +} esp_ble_mesh_time_zone_set_t; + +/** Parameters of TAI-UTC Delta Set */ +typedef struct { + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint16_t padding : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ +} esp_ble_mesh_tai_utc_delta_set_t; + +/** Parameter of Time Role Set */ +typedef struct { + uint8_t time_role; /*!< The Time Role for the element */ +} esp_ble_mesh_time_role_set_t; + +/** Parameter of Scene Store */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be stored */ +} esp_ble_mesh_scene_store_t; + +/** Parameters of Scene Recall */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t scene_number; /*!< The number of scenes to be recalled */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_scene_recall_t; + +/** Parameter of Scene Delete */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be deleted */ +} esp_ble_mesh_scene_delete_t; + +/** Parameter of Scheduler Action Get */ +typedef struct { + uint8_t index; /*!< Index of the Schedule Register entry to get */ +} esp_ble_mesh_scheduler_act_get_t; + +/** Parameters of Scheduler Action Set */ +typedef struct { + uint64_t index : 4; /*!< Index of the Schedule Register entry to set */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Transition time for this action */ +} esp_ble_mesh_scheduler_act_set_t; + +/** + * @brief Time Scene Client Model get message union + */ +typedef union { + esp_ble_mesh_scheduler_act_get_t scheduler_act_get; /*!< For ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET */ +} esp_ble_mesh_time_scene_client_get_state_t; + +/** + * @brief Time Scene Client Model set message union + */ +typedef union { + esp_ble_mesh_time_set_t time_set; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_SET */ + esp_ble_mesh_time_zone_set_t time_zone_set; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_ZONE_SET */ + esp_ble_mesh_tai_utc_delta_set_t tai_utc_delta_set; /*!< For ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET */ + esp_ble_mesh_time_role_set_t time_role_set; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_ROLE_SET */ + esp_ble_mesh_scene_store_t scene_store; /*!< For ESP_BLE_MESH_MODEL_OP_SCENE_STORE & ESP_BLE_MESH_MODEL_OP_SCENE_STORE_UNACK */ + esp_ble_mesh_scene_recall_t scene_recall; /*!< For ESP_BLE_MESH_MODEL_OP_SCENE_RECALL & ESP_BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK */ + esp_ble_mesh_scene_delete_t scene_delete; /*!< For ESP_BLE_MESH_MODEL_OP_SCENE_DELETE & ESP_BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK */ + esp_ble_mesh_scheduler_act_set_t scheduler_act_set; /*!< For ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET & ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK */ +} esp_ble_mesh_time_scene_client_set_state_t; + +/** + * @brief Bluetooth Mesh Time Scene Client Model Get and Set callback parameters structure. + */ + +/** Parameters of Time Status */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t sub_second; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_time_status_cb_t; + +/** Parameters of Time Zone Status */ +typedef struct { + uint8_t time_zone_offset_curr; /*!< Current local time zone offset */ + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ +} esp_ble_mesh_time_zone_status_cb_t; + +/** Parameters of TAI-UTC Delta Status */ +typedef struct { + uint16_t tai_utc_delta_curr : 15; /*!< Current difference between TAI and UTC in seconds */ + uint16_t padding_1 : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint16_t padding_2 : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ +} esp_ble_mesh_tai_utc_delta_status_cb_t; + +/** Parameter of Time Role Status */ +typedef struct { + uint8_t time_role; /*!< The Time Role for the element */ +} esp_ble_mesh_time_role_status_cb_t; + +/** Parameters of Scene Status */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint8_t status_code; /*!< Status code of the last operation */ + uint16_t current_scene; /*!< Scene Number of the current scene */ + uint16_t target_scene; /*!< Scene Number of the target scene (optional) */ + uint8_t remain_time; /*!< Time to complete state transition (C.1) */ +} esp_ble_mesh_scene_status_cb_t; + +/** Parameters of Scene Register Status */ +typedef struct { + uint8_t status_code; /*!< Status code for the previous operation */ + uint16_t current_scene; /*!< Scene Number of the current scene */ + struct net_buf_simple *scenes; /*!< A list of scenes stored within an element */ +} esp_ble_mesh_scene_register_status_cb_t; + +/** Parameter of Scheduler Status */ +typedef struct { + uint16_t schedules; /*!< Bit field indicating defined Actions in the Schedule Register */ +} esp_ble_mesh_scheduler_status_cb_t; + +/** Parameters of Scheduler Action Status */ +typedef struct { + uint64_t index : 4; /*!< Enumerates (selects) a Schedule Register entry */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Transition time for this action */ +} esp_ble_mesh_scheduler_act_status_cb_t; + +/** + * @brief Time Scene Client Model received message union + */ +typedef union { + esp_ble_mesh_time_status_cb_t time_status; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_STATUS */ + esp_ble_mesh_time_zone_status_cb_t time_zone_status; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_ZONE_STATUS */ + esp_ble_mesh_tai_utc_delta_status_cb_t tai_utc_delta_status; /*!< For ESP_BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS */ + esp_ble_mesh_time_role_status_cb_t time_role_status; /*!< For ESP_BLE_MESH_MODEL_OP_TIME_ROLE_STATUS */ + esp_ble_mesh_scene_status_cb_t scene_status; /*!< For ESP_BLE_MESH_MODEL_OP_SCENE_STATUS */ + esp_ble_mesh_scene_register_status_cb_t scene_register_status; /*!< For ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS */ + esp_ble_mesh_scheduler_status_cb_t scheduler_status; /*!< For ESP_BLE_MESH_MODEL_OP_SCHEDULER_STATUS */ + esp_ble_mesh_scheduler_act_status_cb_t scheduler_act_status; /*!< For ESP_BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS */ +} esp_ble_mesh_time_scene_client_status_cb_t; + +/** Time Scene Client Model callback parameters */ +typedef struct { + int error_code; /*!< Appropriate error code */ + esp_ble_mesh_client_common_param_t *params; /*!< The client common parameters. */ + esp_ble_mesh_time_scene_client_status_cb_t status_cb; /*!< The scene status message callback values */ +} esp_ble_mesh_time_scene_client_cb_param_t; + +/** This enum value is the event of Time Scene Client Model */ +typedef enum { + ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT, + ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT, + ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT, + ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT, + ESP_BLE_MESH_TIME_SCENE_CLIENT_EVT_MAX, +} esp_ble_mesh_time_scene_client_cb_event_t; + +/** + * @brief Bluetooth Mesh Time Scene Client Model function. + */ + +/** + * @brief Time Scene Client Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_time_scene_client_cb_t)(esp_ble_mesh_time_scene_client_cb_event_t event, + esp_ble_mesh_time_scene_client_cb_param_t *param); + +/** + * @brief Register BLE Mesh Time Scene Client Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_t callback); + +/** + * @brief Get the value of Time Scene Server Model states using the Time Scene Client Model get messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_time_scene_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] get_state: Pointer to time scene get message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + */ +esp_err_t esp_ble_mesh_time_scene_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_time_scene_client_get_state_t *get_state); + +/** + * @brief Set the value of Time Scene Server Model states using the Time Scene Client Model set messages. + * + * @note If you want to know the opcodes and corresponding meanings accepted by this API, + * please refer to esp_ble_mesh_time_scene_message_opcode_t in esp_ble_mesh_defs.h + * + * @param[in] params: Pointer to BLE Mesh common client parameters. + * @param[in] set_state: Pointer to time scene set message value. + * Shall not be set to NULL. + * + * @return ESP_OK on success or error code otherwise. + */ +esp_err_t esp_ble_mesh_time_scene_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_time_scene_client_set_state_t *set_state); + +/** + * @brief Time Scene Server Models related context. + */ + +/** @def ESP_BLE_MESH_MODEL_TIME_SRV + * + * @brief Define a new Time Server Model. + * + * @note 1. The Time Server model is a root model. When this model is present on an + * Element, the corresponding Time Setup Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_time_srv_t. + * + * @return New Time Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_TIME_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_TIME_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_TIME_SETUP_SRV + * + * @brief Define a new Time Setup Server Model. + * + * @note 1. The Time Setup Server model extends the Time Server model. Time is + * sensitive information that is propagated across a mesh network. + * 2. Only an authorized Time Client should be allowed to change the Time + * and Time Role states. A dedicated application key Bluetooth SIG + * Proprietary should be used on the Time Setup Server to restrict + * access to the server to only authorized Time Clients. + * 3. This model does not support subscribing nor publishing. + * + * @param srv_data Pointer to the unique struct esp_ble_mesh_time_setup_srv_t. + * + * @return New Time Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_TIME_SETUP_SRV(srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_TIME_SETUP_SRV, \ + NULL, NULL, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SCENE_SRV + * + * @brief Define a new Scene Server Model. + * + * @note 1. The Scene Server model is a root model. When this model is present + * on an Element, the corresponding Scene Setup Server model shall + * also be present. + * 2. This model shall support model publication and model subscription. + * 3. The model may be present only on the Primary element of a node. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_scene_srv_t. + * + * @return New Scene Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCENE_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCENE_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SCENE_SETUP_SRV + * + * @brief Define a new Scene Setup Server Model. + * + * @note 1. The Scene Setup Server model extends the Scene Server model and + * the Generic Default Transition Time Server model. + * 2. This model shall support model subscription. + * 3. The model may be present only on the Primary element of a node. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_scene_setup_srv_t. + * + * @return New Scene Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCENE_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCENE_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SCHEDULER_SRV + * + * @brief Define a new Scheduler Server Model. + * + * @note 1. The Scheduler Server model extends the Scene Server model. When + * this model is present on an Element, the corresponding Scheduler + * Setup Server model shall also be present. + * 2. This model shall support model publication and model subscription. + * 3. The model may be present only on the Primary element of a node. + * 4. The model requires the Time Server model shall be present on the element. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_scheduler_srv_t. + * + * @return New Scheduler Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCHEDULER_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCHEDULER_SRV, \ + NULL, srv_pub, srv_data) + +/** @def ESP_BLE_MESH_MODEL_SCHEDULER_SETUP_SRV + * + * @brief Define a new Scheduler Setup Server Model. + * + * @note 1. The Scheduler Setup Server model extends the Scheduler Server and + * the Scene Setup Server models. + * 2. This model shall support model subscription. + * 3. The model may be present only on the Primary element of a node. + * + * @param srv_pub Pointer to the unique struct esp_ble_mesh_model_pub_t. + * @param srv_data Pointer to the unique struct esp_ble_mesh_scheduler_setup_srv_t. + * + * @return New Scheduler Setup Server Model instance. + */ +#define ESP_BLE_MESH_MODEL_SCHEDULER_SETUP_SRV(srv_pub, srv_data) \ + ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV, \ + NULL, srv_pub, srv_data) + +#define ESP_BLE_MESH_UNKNOWN_TAI_SECONDS 0x0000000000 /*!< Unknown TAI Seconds */ +#define ESP_BLE_MESH_UNKNOWN_TAI_ZONE_CHANGE 0x0000000000 /*!< Unknown TAI of Zone Change */ +#define ESP_BLE_MESH_UNKNOWN_TAI_DELTA_CHANGE 0x0000000000 /*!< Unknown TAI of Delta Change */ + +#define ESP_BLE_MESH_TAI_UTC_DELTA_MAX_VALUE 0x7FFF /*!< Maximum TAI-UTC Delta value */ + +#define ESP_BLE_MESH_TAI_SECONDS_LEN 0x05 /*!< Length of TAI Seconds */ +#define ESP_BLE_MESH_TAI_OF_ZONE_CHANGE_LEN 0x05 /*!< Length of TAI of Zone Change */ +#define ESP_BLE_MESH_TAI_OF_DELTA_CHANGE_LEN 0x05 /*!< Length of TAI of Delta Change */ + +#define ESP_BLE_MESH_INVALID_SCENE_NUMBER 0x0000 /*!< Invalid Scene Number */ +#define ESP_BLE_MESH_SCENE_NUMBER_LEN 0x02 /*!< Length of the Scene Number */ + +#define ESP_BLE_MESH_SCHEDULE_YEAR_ANY_YEAR 0x64 /*!< Any year of the Scheduled year */ + +#define ESP_BLE_MESH_SCHEDULE_DAY_ANY_DAY 0x00 /*!< Any day of the Scheduled day */ + +#define ESP_BLE_MESH_SCHEDULE_HOUR_ANY_HOUR 0x18 /*!< Any hour of the Scheduled hour */ +#define ESP_BLE_MESH_SCHEDULE_HOUR_ONCE_A_DAY 0x19 /*!< Any hour of the Scheduled Day */ + +#define ESP_BLE_MESH_SCHEDULE_SEC_ANY_OF_HOUR 0x3C /*!< Any minute of the Scheduled hour */ +#define ESP_BLE_MESH_SCHEDULE_SEC_EVERY_15_MIN 0x3D /*!< Every 15 minutes of the Scheduled hour */ +#define ESP_BLE_MESH_SCHEDULE_SEC_EVERY_20_MIN 0x3E /*!< Every 20 minutes of the Scheduled hour */ +#define ESP_BLE_MESH_SCHEDULE_SEC_ONCE_AN_HOUR 0x3F /*!< Once of the Scheduled hour */ + +#define ESP_BLE_MESH_SCHEDULE_SEC_ANY_OF_MIN 0x3C /*!< Any second of the Scheduled minute */ +#define ESP_BLE_MESH_SCHEDULE_SEC_EVERY_15_SEC 0x3D /*!< Every 15 seconds of the Scheduled minute */ +#define ESP_BLE_MESH_SCHEDULE_SEC_EVERY_20_SEC 0x3E /*!< Every 20 seconds of the Scheduled minute */ +#define ESP_BLE_MESH_SCHEDULE_SEC_ONCE_AN_MIN 0x3F /*!< Once of the Scheduled minute */ + +#define ESP_BLE_MESH_SCHEDULE_ACT_TURN_OFF 0x00 /*!< Scheduled Action - Turn Off */ +#define ESP_BLE_MESH_SCHEDULE_ACT_TURN_ON 0x01 /*!< Scheduled Action - Turn On */ +#define ESP_BLE_MESH_SCHEDULE_ACT_SCENE_RECALL 0x02 /*!< Scheduled Action - Scene Recall */ +#define ESP_BLE_MESH_SCHEDULE_ACT_NO_ACTION 0x0F /*!< Scheduled Action - No Action */ + +#define ESP_BLE_MESH_SCHEDULE_SCENE_NO_SCENE 0x0000 /*!< Scheduled Scene - No Scene */ + +#define ESP_BLE_MESH_SCHEDULE_ENTRY_MAX_INDEX 0x0F /*!< Maximum number of Scheduled entries */ + +#define ESP_BLE_MESH_TIME_NONE 0x00 /*!< Time Role - None */ +#define ESP_BLE_MESH_TIME_AUTHORITY 0x01 /*!< Time Role - Mesh Time Authority */ +#define ESP_BLE_MESH_TIME_RELAY 0x02 /*!< Time Role - Mesh Time Relay */ +#define ESP_BLE_MESH_TIME_CLINET 0x03 /*!< Time Role - Mesh Time Client */ + +#define ESP_BLE_MESH_SCENE_SUCCESS 0x00 /*!< Scene operation - Success */ +#define ESP_BLE_MESH_SCENE_REG_FULL 0x01 /*!< Scene operation - Scene Register Full */ +#define ESP_BLE_MESH_SCENE_NOT_FOUND 0x02 /*!< Scene operation - Scene Not Found */ + +/** Parameters of Time state */ +typedef struct { + struct { + uint8_t tai_seconds[5]; /*!< The value of the TAI Seconds state */ + uint8_t subsecond; /*!< The value of the Subsecond field */ + uint8_t uncertainty; /*!< The value of the Uncertainty field */ + uint8_t time_zone_offset_curr; /*!< The value of the Time Zone Offset Current field */ + uint8_t time_zone_offset_new; /*!< The value of the Time Zone Offset New state */ + uint8_t tai_zone_change[5]; /*!< The value of the TAI of Zone Chaneg field */ + uint16_t time_authority : 1, /*!< The value of the Time Authority bit */ + tai_utc_delta_curr : 15; /*!< The value of the TAI-UTC Delta Current state */ + uint16_t tai_utc_delta_new : 15; /*!< The value of the TAI-UTC Delta New state */ + uint8_t tai_delta_change[5]; /*!< The value of the TAI of Delta Change field */ + } time; /*!< Parameters of the Time state */ + uint8_t time_role; /*!< The value of the Time Role state */ +} esp_ble_mesh_time_state_t; + +/** User data of Time Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Time Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_time_state_t *state; /*!< Parameters of the Time state */ +} esp_ble_mesh_time_srv_t; + +/** User data of Time Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Time Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_time_state_t *state; /*!< Parameters of the Time state */ +} esp_ble_mesh_time_setup_srv_t; + +/** + * 1. Scene Store is an operation of storing values of a present state of an element. + * 2. The structure and meaning of the stored state is determined by a model. States + * to be stored are specified by each model. + * 3. The Scene Store operation shall persistently store all values of all states + * marked as Stored with Scene for all models present on all elements of a node. + * 4. If a model is extending another model, the extending model shall determine the + * Stored with Scene behavior of that model. + */ + +/** Parameters of Scene Register state */ +typedef struct { + uint16_t scene_number; /*!< The value of the Scene Number */ + uint8_t scene_type; /*!< The value of the Scene Type */ + /** + * Scene value may use a union to represent later, the union contains + * structures of all the model states which can be stored in a scene. + */ + struct net_buf_simple *scene_value; /*!< The value of the Scene Value */ +} esp_ble_mesh_scene_register_t; + +/** + * Parameters of Scenes state. + * + * Scenes serve as memory banks for storage of states (e.g., a power level + * or a light level/color). Values of states of an element can be stored + * as a scene and can be recalled later from the scene memory. + * + * A scene is represented by a Scene Number, which is a 16-bit non-zero, + * mesh-wide value. (There can be a maximum of 65535 scenes in a mesh + * network.) The meaning of a scene, as well as the state storage container + * associated with it, are determined by a model. + * + * The Scenes state change may start numerous parallel model transitions. + * In that case, each individual model handles the transition internally. + * + * The scene transition is defined as a group of individual model transitions + * started by a Scene Recall operation. The scene transition is in progress + * when at least one transition from the group of individual model transitions + * is in progress. + */ +typedef struct { + const uint16_t scene_count; /*!< The Scenes state's scene count */ + esp_ble_mesh_scene_register_t *scenes; /*!< Parameters of the Scenes state */ + + /** + * The Current Scene state is a 16-bit value that contains either the Scene + * Number of the currently active scene or a value of 0x0000 when no scene + * is active. + * + * When a Scene Store operation or a Scene Recall operation completes with + * success, the Current Scene state value shall be to the Scene Number used + * during that operation. + * + * When the Current Scene Number is deleted from a Scene Register state as a + * result of Scene Delete operation, the Current Scene state shall be set to + * 0x0000. + * + * When any of the element's state that is marked as “Stored with Scene” has + * changed not as a result of a Scene Recall operation, the value of the + * Current Scene state shall be set to 0x0000. + * + * When a scene transition is in progress, the value of the Current Scene + * state shall be set to 0x0000. + */ + uint16_t current_scene; /*!< The value of the Current Scene state */ + + /** + * The Target Scene state is a 16-bit value that contains the target Scene + * Number when a scene transition is in progress. + * + * When the scene transition is in progress and the target Scene Number is + * deleted from a Scene Register state as a result of Scene Delete operation, + * the Target Scene state shall be set to 0x0000. + * + * When the scene transition is in progress and a new Scene Number is stored + * in the Scene Register as a result of Scene Store operation, the Target + * Scene state shall be set to the new Scene Number. + * + * When the scene transition is not in progress, the value of the Target Scene + * state shall be set to 0x0000. + */ + uint16_t target_scene; /*!< The value of the Target Scene state */ + + /* Indicate the status code for the last operation */ + uint8_t status_code; /*!< The status code of the last scene operation */ + + /* Indicate if scene transition is in progress */ + bool in_progress; /*!< Indicate if the scene transition is in progress */ +} esp_ble_mesh_scenes_state_t; + +/** User data of Scene Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Scene Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_scenes_state_t *state; /*!< Parameters of the Scenes state */ + esp_ble_mesh_last_msg_info_t last; /*!< Parameters of the last received set message */ + esp_ble_mesh_state_transition_t transition; /*!< Parameters of state transition */ +} esp_ble_mesh_scene_srv_t; + +/** User data of Scene Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Scene Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_scenes_state_t *state; /*!< Parameters of the Scenes state */ +} esp_ble_mesh_scene_setup_srv_t; + +/** Parameters of Scheduler Register state */ +typedef struct { + bool in_use; /*!< Indicate if the registered schedule is in use */ + uint64_t year : 7, /*!< The value of Scheduled year for the action */ + month : 12, /*!< The value of Scheduled month for the action */ + day : 5, /*!< The value of Scheduled day of the month for the action */ + hour : 5, /*!< The value of Scheduled hour for the action */ + minute : 6, /*!< The value of Scheduled minute for the action */ + second : 6, /*!< The value of Scheduled second for the action */ + day_of_week : 7, /*!< The value of Schedule days of the week for the action */ + action : 4, /*!< The value of Action to be performed at the scheduled time */ + trans_time : 8; /*!< The value of Transition time for this action */ + uint16_t scene_number; /*!< The value of Scene Number to be used for some actions */ +} esp_ble_mesh_schedule_register_t; + +/** Parameters of Scheduler state */ +typedef struct { + const uint8_t schedule_count; /*!< Scheduler count */ + esp_ble_mesh_schedule_register_t *schedules; /*!< Up to 16 scheduled entries */ +} esp_ble_mesh_scheduler_state_t; + +/** User data of Scheduler Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Scheduler Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_scheduler_state_t *state; /*!< Parameters of the Scheduler state */ +} esp_ble_mesh_scheduler_srv_t; + +/** User data of Scheduler Setup Server Model */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to the Scheduler Setup Server Model. Initialized internally. */ + esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl; /*!< Response control of the server model received messages */ + esp_ble_mesh_scheduler_state_t *state; /*!< Parameters of the Scheduler state */ +} esp_ble_mesh_scheduler_setup_srv_t; + +/** Parameters of Time Set state change event */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t subsecond; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta_curr : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset_curr; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_state_change_time_set_t; + +/** Parameters of Time Status state change event */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t subsecond; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta_curr : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset_curr; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_state_change_time_status_t; + +/** Parameters of Time Zone Set state change event */ +typedef struct { + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ +} esp_ble_mesh_state_change_time_zone_set_t; + +/** Parameters of TAI UTC Delta Set state change event */ +typedef struct { + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ +} esp_ble_mesh_state_change_tai_utc_delta_set_t; + +/** Parameter of Time Role Set state change event */ +typedef struct { + uint8_t time_role; /*!< The Time Role for the element */ +} esp_ble_mesh_state_change_time_role_set_t; + +/** Parameter of Scene Store state change event */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be stored */ +} esp_ble_mesh_state_change_scene_store_t; + +/** Parameter of Scene Recall state change event */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be recalled */ +} esp_ble_mesh_state_change_scene_recall_t; + +/** Parameter of Scene Delete state change event */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be deleted */ +} esp_ble_mesh_state_change_scene_delete_t; + +/** Parameter of Scheduler Action Set state change event */ +typedef struct { + uint64_t index : 4; /*!< Index of the Schedule Register entry to set */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Scene number to be used for some actions */ +} esp_ble_mesh_state_change_scheduler_act_set_t; + +/** + * @brief Time Scene Server Model state change value union + */ +typedef union { + /** + * The recv_op in ctx can be used to decide which state is changed. + */ + esp_ble_mesh_state_change_time_set_t time_set; /*!< Time Set */ + esp_ble_mesh_state_change_time_status_t time_status; /*!< Time Status */ + esp_ble_mesh_state_change_time_zone_set_t time_zone_set; /*!< Time Zone Set */ + esp_ble_mesh_state_change_tai_utc_delta_set_t tai_utc_delta_set; /*!< TAI UTC Delta Set */ + esp_ble_mesh_state_change_time_role_set_t time_role_set; /*!< Time Role Set */ + esp_ble_mesh_state_change_scene_store_t scene_store; /*!< Scene Store */ + esp_ble_mesh_state_change_scene_recall_t scene_recall; /*!< Scene Recall */ + esp_ble_mesh_state_change_scene_delete_t scene_delete; /*!< Scene Delete */ + esp_ble_mesh_state_change_scheduler_act_set_t scheduler_act_set; /*!< Scheduler Action Set */ +} esp_ble_mesh_time_scene_server_state_change_t; + +/** Context of the received Scheduler Action Get message */ +typedef struct { + uint8_t index; /*!< Index of the Schedule Register entry to get */ +} esp_ble_mesh_server_recv_scheduler_act_get_t; + +/** + * @brief Time Scene Server Model received get message union + */ +typedef union { + esp_ble_mesh_server_recv_scheduler_act_get_t scheduler_act; /*!< Scheduler Action Get */ +} esp_ble_mesh_time_scene_server_recv_get_msg_t; + +/** Context of the received Time Set message */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t subsecond; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_server_recv_time_set_t; + +/** Context of the received Time Zone Set message */ +typedef struct { + uint8_t time_zone_offset_new; /*!< Upcoming local time zone offset */ + uint8_t tai_zone_change[5]; /*!< TAI Seconds time of the upcoming Time Zone Offset change */ +} esp_ble_mesh_server_recv_time_zone_set_t; + +/** Context of the received TAI UTC Delta Set message */ +typedef struct { + uint16_t tai_utc_delta_new : 15; /*!< Upcoming difference between TAI and UTC in seconds */ + uint16_t padding : 1; /*!< Always 0b0. Other values are Prohibited. */ + uint8_t tai_delta_change[5]; /*!< TAI Seconds time of the upcoming TAI-UTC Delta change */ +} esp_ble_mesh_server_recv_tai_utc_delta_set_t; + +/** Context of the received Time Role Set message */ +typedef struct { + uint8_t time_role; /*!< The Time Role for the element */ +} esp_ble_mesh_server_recv_time_role_set_t; + +/** Context of the received Scene Store message */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be stored */ +} esp_ble_mesh_server_recv_scene_store_t; + +/** Context of the received Scene Recall message */ +typedef struct { + bool op_en; /*!< Indicate if optional parameters are included */ + uint16_t scene_number; /*!< The number of scenes to be recalled */ + uint8_t tid; /*!< Transaction ID */ + uint8_t trans_time; /*!< Time to complete state transition (optional) */ + uint8_t delay; /*!< Indicate message execution delay (C.1) */ +} esp_ble_mesh_server_recv_scene_recall_t; + +/** Context of the received Scene Delete message */ +typedef struct { + uint16_t scene_number; /*!< The number of scenes to be deleted */ +} esp_ble_mesh_server_recv_scene_delete_t; + +/** Context of the received Scheduler Action Set message */ +typedef struct { + uint64_t index : 4; /*!< Index of the Schedule Register entry to set */ + uint64_t year : 7; /*!< Scheduled year for the action */ + uint64_t month : 12; /*!< Scheduled month for the action */ + uint64_t day : 5; /*!< Scheduled day of the month for the action */ + uint64_t hour : 5; /*!< Scheduled hour for the action */ + uint64_t minute : 6; /*!< Scheduled minute for the action */ + uint64_t second : 6; /*!< Scheduled second for the action */ + uint64_t day_of_week : 7; /*!< Schedule days of the week for the action */ + uint64_t action : 4; /*!< Action to be performed at the scheduled time */ + uint64_t trans_time : 8; /*!< Transition time for this action */ + uint16_t scene_number; /*!< Scene number to be used for some actions */ +} esp_ble_mesh_server_recv_scheduler_act_set_t; + +/** + * @brief Time Scene Server Model received set message union + */ +typedef union { + esp_ble_mesh_server_recv_time_set_t time; /*!< Time Set */ + esp_ble_mesh_server_recv_time_zone_set_t time_zone; /*!< Time Zone Set */ + esp_ble_mesh_server_recv_tai_utc_delta_set_t tai_utc_delta; /*!< TAI-UTC Delta Set */ + esp_ble_mesh_server_recv_time_role_set_t time_role; /*!< Time Role Set */ + esp_ble_mesh_server_recv_scene_store_t scene_store; /*!< Scene Store/Scene Store Unack */ + esp_ble_mesh_server_recv_scene_recall_t scene_recall; /*!< Scene Recall/Scene Recall Unack */ + esp_ble_mesh_server_recv_scene_delete_t scene_delete; /*!< Scene Delete/Scene Delete Unack */ + esp_ble_mesh_server_recv_scheduler_act_set_t scheduler_act; /*!< Scheduler Action Set/Scheduler Action Set Unack */ +} esp_ble_mesh_time_scene_server_recv_set_msg_t; + +/** Context of the received Time Status message */ +typedef struct { + uint8_t tai_seconds[5]; /*!< The current TAI time in seconds */ + uint8_t subsecond; /*!< The sub-second time in units of 1/256 second */ + uint8_t uncertainty; /*!< The estimated uncertainty in 10-millisecond steps */ + uint16_t time_authority : 1; /*!< 0 = No Time Authority, 1 = Time Authority */ + uint16_t tai_utc_delta : 15; /*!< Current difference between TAI and UTC in seconds */ + uint8_t time_zone_offset; /*!< The local time zone offset in 15-minute increments */ +} esp_ble_mesh_server_recv_time_status_t; + +/** + * @brief Time Scene Server Model received status message union + */ +typedef union { + esp_ble_mesh_server_recv_time_status_t time_status; /*!< Time Status */ +} esp_ble_mesh_time_scene_server_recv_status_msg_t; + +/** + * @brief Time Scene Server Model callback value union + */ +typedef union { + esp_ble_mesh_time_scene_server_state_change_t state_change; /*!< ESP_BLE_MESH_TIME_SCENE_SERVER_STATE_CHANGE_EVT */ + esp_ble_mesh_time_scene_server_recv_get_msg_t get; /*!< ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_GET_MSG_EVT */ + esp_ble_mesh_time_scene_server_recv_set_msg_t set; /*!< ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_SET_MSG_EVT */ + esp_ble_mesh_time_scene_server_recv_status_msg_t status; /*!< ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_STATUS_MSG_EVT */ +} esp_ble_mesh_time_scene_server_cb_value_t; + +/** Time Scene Server Model callback parameters */ +typedef struct { + esp_ble_mesh_model_t *model; /*!< Pointer to Time and Scenes Server Models */ + esp_ble_mesh_msg_ctx_t ctx; /*!< Context of the received messages */ + esp_ble_mesh_time_scene_server_cb_value_t value; /*!< Value of the received Time and Scenes Messages */ +} esp_ble_mesh_time_scene_server_cb_param_t; + +/** This enum value is the event of Time Scene Server Model */ +typedef enum { + /** + * 1. When get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, no event will be + * callback to the application layer when Time Scene Get messages are received. + * 2. When set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, this event will + * be callback to the application layer when Time Scene Set/Set Unack messages + * are received. + */ + ESP_BLE_MESH_TIME_SCENE_SERVER_STATE_CHANGE_EVT, + /** + * When get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Time Scene Get messages are received. + */ + ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_GET_MSG_EVT, + /** + * When set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be + * callback to the application layer when Time Scene Set/Set Unack messages are received. + */ + ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_SET_MSG_EVT, + /** + * When status_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will + * be callback to the application layer when TIme Status message is received. + */ + ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_STATUS_MSG_EVT, + ESP_BLE_MESH_TIME_SCENE_SERVER_EVT_MAX, +} esp_ble_mesh_time_scene_server_cb_event_t; + +/** + * @brief Bluetooth Mesh Time and Scenes Server Model function. + */ + +/** + * @brief Time Scene Server Model callback function type + * @param event: Event type + * @param param: Pointer to callback parameter + */ +typedef void (* esp_ble_mesh_time_scene_server_cb_t)(esp_ble_mesh_time_scene_server_cb_event_t event, + esp_ble_mesh_time_scene_server_cb_param_t *param); + +/** + * @brief Register BLE Mesh Time and Scenes Server Model callback. + * + * @param[in] callback: Pointer to the callback function. + * + * @return ESP_OK on success or error code otherwise. + * + */ +esp_err_t esp_ble_mesh_register_time_scene_server_callback(esp_ble_mesh_time_scene_server_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif /* _ESP_BLE_MESH_TIME_SCENE_MODEL_API_H_ */ + diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c new file mode 100644 index 000000000..5c15b1148 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_config_model.c @@ -0,0 +1,780 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_config_model.h" +#include "foundation.h" +#include "cfg_cli.h" +#include "esp_ble_mesh_config_model_api.h" + +extern s32_t config_msg_timeout; + +/* Configuration Client Model related functions */ + +static inline void btc_ble_mesh_config_client_cb_to_app(esp_ble_mesh_cfg_client_cb_event_t event, + esp_ble_mesh_cfg_client_cb_param_t *param) +{ + esp_ble_mesh_cfg_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_cfg_client_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_config_client_args_t *dst = (btc_ble_mesh_config_client_args_t *)p_dest; + btc_ble_mesh_config_client_args_t *src = (btc_ble_mesh_config_client_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: { + dst->cfg_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->cfg_client_get_state.params) { + memcpy(dst->cfg_client_get_state.params, src->cfg_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->cfg_client_get_state.get_state) { + dst->cfg_client_get_state.get_state = (esp_ble_mesh_cfg_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_get_state_t)); + if (dst->cfg_client_get_state.get_state) { + memcpy(dst->cfg_client_get_state.get_state, src->cfg_client_get_state.get_state, + sizeof(esp_ble_mesh_cfg_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: { + dst->cfg_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->cfg_client_set_state.params) { + memcpy(dst->cfg_client_set_state.params, src->cfg_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->cfg_client_set_state.set_state) { + dst->cfg_client_set_state.set_state = (esp_ble_mesh_cfg_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_cfg_client_set_state_t)); + if (dst->cfg_client_set_state.set_state) { + memcpy(dst->cfg_client_set_state.set_state, src->cfg_client_set_state.set_state, + sizeof(esp_ble_mesh_cfg_client_set_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_config_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_config_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_config_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: + if (arg->cfg_client_get_state.params) { + bt_mesh_free(arg->cfg_client_get_state.params); + } + if (arg->cfg_client_get_state.get_state) { + bt_mesh_free(arg->cfg_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: + if (arg->cfg_client_set_state.params) { + bt_mesh_free(arg->cfg_client_set_state.params); + } + if (arg->cfg_client_set_state.set_state) { + bt_mesh_free(arg->cfg_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_config_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_cfg_client_cb_param_t *p_dest_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_dest; + esp_ble_mesh_cfg_client_cb_param_t *p_src_data = (esp_ble_mesh_cfg_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case OP_DEV_COMP_DATA_GET: + case OP_DEV_COMP_DATA_STATUS: + if (p_src_data->status_cb.comp_data_status.composition_data) { + length = p_src_data->status_cb.comp_data_status.composition_data->len; + p_dest_data->status_cb.comp_data_status.composition_data = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.comp_data_status.composition_data) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.comp_data_status.composition_data, + p_src_data->status_cb.comp_data_status.composition_data->data, + p_src_data->status_cb.comp_data_status.composition_data->len); + } + break; + case OP_MOD_SUB_GET: + case OP_MOD_SUB_GET_VND: + case OP_MOD_SUB_LIST: + case OP_MOD_SUB_LIST_VND: + if (p_src_data->status_cb.model_sub_list.sub_addr) { + length = p_src_data->status_cb.model_sub_list.sub_addr->len; + p_dest_data->status_cb.model_sub_list.sub_addr = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.model_sub_list.sub_addr) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.model_sub_list.sub_addr, + p_src_data->status_cb.model_sub_list.sub_addr->data, + p_src_data->status_cb.model_sub_list.sub_addr->len); + } + break; + case OP_NET_KEY_GET: + case OP_NET_KEY_LIST: + if (p_src_data->status_cb.netkey_list.net_idx) { + length = p_src_data->status_cb.netkey_list.net_idx->len; + p_dest_data->status_cb.netkey_list.net_idx = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.netkey_list.net_idx) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.netkey_list.net_idx, + p_src_data->status_cb.netkey_list.net_idx->data, + p_src_data->status_cb.netkey_list.net_idx->len); + } + break; + case OP_APP_KEY_GET: + case OP_APP_KEY_LIST: + if (p_src_data->status_cb.appkey_list.app_idx) { + length = p_src_data->status_cb.appkey_list.app_idx->len; + p_dest_data->status_cb.appkey_list.app_idx = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.appkey_list.app_idx) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.appkey_list.app_idx, + p_src_data->status_cb.appkey_list.app_idx->data, + p_src_data->status_cb.appkey_list.app_idx->len); + } + break; + case OP_SIG_MOD_APP_GET: + case OP_VND_MOD_APP_GET: + case OP_SIG_MOD_APP_LIST: + case OP_VND_MOD_APP_LIST: + if (p_src_data->status_cb.model_app_list.app_idx) { + length = p_src_data->status_cb.model_app_list.app_idx->len; + p_dest_data->status_cb.model_app_list.app_idx = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.model_app_list.app_idx) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.model_app_list.app_idx, + p_src_data->status_cb.model_app_list.app_idx->data, + p_src_data->status_cb.model_app_list.app_idx->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_config_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_cfg_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case OP_DEV_COMP_DATA_GET: + case OP_DEV_COMP_DATA_STATUS: + bt_mesh_free_buf(arg->status_cb.comp_data_status.composition_data); + break; + case OP_MOD_SUB_GET: + case OP_MOD_SUB_GET_VND: + case OP_MOD_SUB_LIST: + case OP_MOD_SUB_LIST_VND: + bt_mesh_free_buf(arg->status_cb.model_sub_list.sub_addr); + break; + case OP_NET_KEY_GET: + case OP_NET_KEY_LIST: + bt_mesh_free_buf(arg->status_cb.netkey_list.net_idx); + break; + case OP_APP_KEY_GET: + case OP_APP_KEY_LIST: + bt_mesh_free_buf(arg->status_cb.appkey_list.app_idx); + break; + case OP_SIG_MOD_APP_GET: + case OP_VND_MOD_APP_GET: + case OP_SIG_MOD_APP_LIST: + case OP_VND_MOD_APP_LIST: + bt_mesh_free_buf(arg->status_cb.model_app_list.app_idx); + break; + default: + break; + } + } + case ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_config_client_callback(esp_ble_mesh_cfg_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_CONFIG_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_CONFIG_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_cfg_client_cb_param_t), btc_ble_mesh_config_client_copy_req_data); +} + +void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_cfg_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE: + act = ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE: + act = ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH: + act = ESP_BLE_MESH_CFG_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_CFG_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown config client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_config_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_config_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_config_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +static int btc_ble_mesh_config_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_get_state_t *get) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: + case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: + if (get == NULL) { + BT_ERR("%s, Invalid config client get", __func__); + return -EINVAL; + } + break; + default: + break; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = BLE_MESH_KEY_DEV; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + config_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_BEACON_GET: + return bt_mesh_cfg_beacon_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_GET: + return bt_mesh_cfg_ttl_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_FRIEND_GET: + return bt_mesh_cfg_friend_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_GET: + return bt_mesh_cfg_gatt_proxy_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_RELAY_GET: + return bt_mesh_cfg_relay_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_GET: + return bt_mesh_cfg_mod_pub_get(&ctx, get->model_pub_get.element_addr, + get->model_pub_get.model_id, + get->model_pub_get.company_id); + case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_GET: + return bt_mesh_cfg_hb_pub_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_GET: + return bt_mesh_cfg_hb_sub_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_COMPOSITION_DATA_GET: + return bt_mesh_cfg_comp_data_get(&ctx, get->comp_data_get.page); + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_SUB_GET: + return bt_mesh_cfg_mod_sub_get(&ctx, get->sig_model_sub_get.element_addr, + get->sig_model_sub_get.model_id); + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_SUB_GET: + return bt_mesh_cfg_mod_sub_get_vnd(&ctx, get->vnd_model_sub_get.element_addr, + get->vnd_model_sub_get.model_id, + get->vnd_model_sub_get.company_id); + case ESP_BLE_MESH_MODEL_OP_NET_KEY_GET: + return bt_mesh_cfg_net_key_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_APP_KEY_GET: + return bt_mesh_cfg_app_key_get(&ctx, get->app_key_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_GET: + return bt_mesh_cfg_node_identity_get(&ctx, get->node_identity_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_SIG_MODEL_APP_GET: + return bt_mesh_cfg_mod_app_get(&ctx, get->sig_model_app_get.element_addr, + get->sig_model_app_get.model_id); + case ESP_BLE_MESH_MODEL_OP_VENDOR_MODEL_APP_GET: + return bt_mesh_cfg_mod_app_get_vnd(&ctx, get->vnd_model_app_get.element_addr, + get->vnd_model_app_get.model_id, + get->vnd_model_app_get.company_id); + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_GET: + return bt_mesh_cfg_kr_phase_get(&ctx, get->kr_phase_get.net_idx); + case ESP_BLE_MESH_MODEL_OP_LPN_POLLTIMEOUT_GET: + return bt_mesh_cfg_lpn_timeout_get(&ctx, get->lpn_pollto_get.lpn_addr); + case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_GET: + return bt_mesh_cfg_net_transmit_get(&ctx); + default: + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return -EINVAL; + } + + return 0; +} + +static int btc_ble_mesh_config_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_cfg_client_set_state_t *set) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (params->opcode != ESP_BLE_MESH_MODEL_OP_NODE_RESET && set == NULL) { + BT_ERR("%s, Invalid config client set", __func__); + return -EINVAL; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = BLE_MESH_KEY_DEV; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + config_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_BEACON_SET: + return bt_mesh_cfg_beacon_set(&ctx, set->beacon_set.beacon); + case ESP_BLE_MESH_MODEL_OP_DEFAULT_TTL_SET: + return bt_mesh_cfg_ttl_set(&ctx, set->default_ttl_set.ttl); + case ESP_BLE_MESH_MODEL_OP_FRIEND_SET: + return bt_mesh_cfg_friend_set(&ctx, set->friend_set.friend_state); + case ESP_BLE_MESH_MODEL_OP_GATT_PROXY_SET: + return bt_mesh_cfg_gatt_proxy_set(&ctx, set->gatt_proxy_set.gatt_proxy); + case ESP_BLE_MESH_MODEL_OP_RELAY_SET: + return bt_mesh_cfg_relay_set(&ctx, set->relay_set.relay, + set->relay_set.relay_retransmit); + case ESP_BLE_MESH_MODEL_OP_NET_KEY_ADD: + return bt_mesh_cfg_net_key_add(&ctx, set->net_key_add.net_idx, + &set->net_key_add.net_key[0]); + case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: + return bt_mesh_cfg_app_key_add(&ctx, set->app_key_add.net_idx, + set->app_key_add.app_idx, + &set->app_key_add.app_key[0]); + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: + return bt_mesh_cfg_mod_app_bind(&ctx, set->model_app_bind.element_addr, + set->model_app_bind.model_app_idx, + set->model_app_bind.model_id, + set->model_app_bind.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_SET: { + struct bt_mesh_cfg_mod_pub model_pub = { + .addr = set->model_pub_set.publish_addr, + .app_idx = set->model_pub_set.publish_app_idx, + .cred_flag = set->model_pub_set.cred_flag, + .ttl = set->model_pub_set.publish_ttl, + .period = set->model_pub_set.publish_period, + .transmit = set->model_pub_set.publish_retransmit, + }; + return bt_mesh_cfg_mod_pub_set(&ctx, set->model_pub_set.element_addr, + set->model_pub_set.model_id, + set->model_pub_set.company_id, &model_pub); + } + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_ADD: + return bt_mesh_cfg_mod_sub_add(&ctx, set->model_sub_add.element_addr, + set->model_sub_add.sub_addr, + set->model_sub_add.model_id, + set->model_sub_add.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE: + return bt_mesh_cfg_mod_sub_del(&ctx, set->model_sub_delete.element_addr, + set->model_sub_delete.sub_addr, + set->model_sub_delete.model_id, + set->model_sub_delete.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_OVERWRITE: + return bt_mesh_cfg_mod_sub_overwrite(&ctx, set->model_sub_overwrite.element_addr, + set->model_sub_overwrite.sub_addr, + set->model_sub_overwrite.model_id, + set->model_sub_overwrite.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_ADD: + return bt_mesh_cfg_mod_sub_va_add(&ctx, set->model_sub_va_add.element_addr, + &set->model_sub_va_add.label_uuid[0], + set->model_sub_va_add.model_id, + set->model_sub_va_add.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_OVERWRITE: + return bt_mesh_cfg_mod_sub_va_overwrite(&ctx, set->model_sub_va_overwrite.element_addr, + &set->model_sub_va_overwrite.label_uuid[0], + set->model_sub_va_overwrite.model_id, + set->model_sub_va_overwrite.company_id); + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_VIRTUAL_ADDR_DELETE: + return bt_mesh_cfg_mod_sub_va_del(&ctx, set->model_sub_va_delete.element_addr, + &set->model_sub_va_delete.label_uuid[0], + set->model_sub_va_delete.model_id, + set->model_sub_va_delete.company_id); + case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_SUB_SET: + return bt_mesh_cfg_hb_sub_set(&ctx, (struct bt_mesh_cfg_hb_sub *)&set->heartbeat_sub_set); + case ESP_BLE_MESH_MODEL_OP_HEARTBEAT_PUB_SET: + return bt_mesh_cfg_hb_pub_set(&ctx, (const struct bt_mesh_cfg_hb_pub *)&set->heartbeat_pub_set); + case ESP_BLE_MESH_MODEL_OP_NODE_RESET: + return bt_mesh_cfg_node_reset(&ctx); + case ESP_BLE_MESH_MODEL_OP_MODEL_PUB_VIRTUAL_ADDR_SET: { + struct bt_mesh_cfg_mod_pub model_pub = { + .app_idx = set->model_pub_va_set.publish_app_idx, + .cred_flag = set->model_pub_va_set.cred_flag, + .ttl = set->model_pub_va_set.publish_ttl, + .period = set->model_pub_va_set.publish_period, + .transmit = set->model_pub_va_set.publish_retransmit, + }; + return bt_mesh_cfg_mod_pub_va_set(&ctx, set->model_pub_va_set.element_addr, + set->model_pub_va_set.model_id, + set->model_pub_va_set.company_id, + set->model_pub_va_set.label_uuid, &model_pub); + } + case ESP_BLE_MESH_MODEL_OP_MODEL_SUB_DELETE_ALL: + return bt_mesh_cfg_mod_sub_del_all(&ctx, set->model_sub_delete_all.element_addr, + set->model_sub_delete_all.model_id, + set->model_sub_delete_all.company_id); + case ESP_BLE_MESH_MODEL_OP_NET_KEY_UPDATE: + return bt_mesh_cfg_net_key_update(&ctx, set->net_key_update.net_idx, + set->net_key_update.net_key); + case ESP_BLE_MESH_MODEL_OP_NET_KEY_DELETE: + return bt_mesh_cfg_net_key_delete(&ctx, set->net_key_delete.net_idx); + case ESP_BLE_MESH_MODEL_OP_APP_KEY_UPDATE: + return bt_mesh_cfg_app_key_update(&ctx, set->app_key_update.net_idx, + set->app_key_update.app_idx, + set->app_key_update.app_key); + case ESP_BLE_MESH_MODEL_OP_APP_KEY_DELETE: + return bt_mesh_cfg_app_key_delete(&ctx, set->app_key_delete.net_idx, + set->app_key_delete.app_idx); + case ESP_BLE_MESH_MODEL_OP_NODE_IDENTITY_SET: + return bt_mesh_cfg_node_identity_set(&ctx, set->node_identity_set.net_idx, + set->node_identity_set.identity); + case ESP_BLE_MESH_MODEL_OP_MODEL_APP_UNBIND: + return bt_mesh_cfg_mod_app_unbind(&ctx, set->model_app_unbind.element_addr, + set->model_app_unbind.model_app_idx, + set->model_app_unbind.model_id, + set->model_app_unbind.company_id); + case ESP_BLE_MESH_MODEL_OP_KEY_REFRESH_PHASE_SET: + return bt_mesh_cfg_kr_phase_set(&ctx, set->kr_phase_set.net_idx, + set->kr_phase_set.transition); + case ESP_BLE_MESH_MODEL_OP_NETWORK_TRANSMIT_SET: + return bt_mesh_cfg_net_transmit_set(&ctx, set->net_transmit_set.net_transmit); + default: + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return -EINVAL; + } + + return 0; +} + +void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_config_client_args_t *arg = NULL; + esp_ble_mesh_cfg_client_cb_param_t cb = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_config_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE: { + cb.params = arg->cfg_client_get_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + cb.error_code = btc_ble_mesh_config_client_get_state(arg->cfg_client_get_state.params, + arg->cfg_client_get_state.get_state); + if (cb.error_code) { + btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE: { + cb.params = arg->cfg_client_set_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + cb.error_code = btc_ble_mesh_config_client_set_state(arg->cfg_client_set_state.params, + arg->cfg_client_set_state.set_state); + if (cb.error_code) { + btc_ble_mesh_config_client_callback(&cb, ESP_BLE_MESH_CFG_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_config_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_cfg_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_cfg_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_CFG_CLIENT_EVT_MAX) { + btc_ble_mesh_config_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_config_client_free_req_data(msg); + return; +} + +/* Configuration Server Model related functions */ + +static inline void btc_ble_mesh_config_server_cb_to_app(esp_ble_mesh_cfg_server_cb_event_t event, + esp_ble_mesh_cfg_server_cb_param_t *param) +{ + esp_ble_mesh_cfg_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_cfg_server_cb_t)btc_profile_cb_get(BTC_PID_CONFIG_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_config_server_callback(esp_ble_mesh_cfg_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_CONFIG_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_CONFIG_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, sizeof(esp_ble_mesh_cfg_server_cb_param_t), NULL); +} + +void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_cfg_server_cb_param_t cb_params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT; + break; + default: + BT_ERR("%s, Unknown config server event type %d", __func__, evt_type); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + + if (val && len) { + length = (len <= sizeof(cb_params.value)) ? len : sizeof(cb_params.value); + memcpy(&cb_params.value, val, length); + } + + btc_ble_mesh_config_server_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_cfg_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_cfg_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_CFG_SERVER_EVT_MAX) { + btc_ble_mesh_config_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } +} diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c new file mode 100644 index 000000000..7a9c5348f --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_generic_model.c @@ -0,0 +1,774 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_generic_model.h" +#include "generic_client.h" +#include "esp_ble_mesh_generic_model_api.h" + +/* Generic Client Models related functions */ + +static inline void btc_ble_mesh_generic_client_cb_to_app(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param) +{ + esp_ble_mesh_generic_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_generic_client_cb_t)btc_profile_cb_get(BTC_PID_GENERIC_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_generic_client_args_t *dst = (btc_ble_mesh_generic_client_args_t *)p_dest; + btc_ble_mesh_generic_client_args_t *src = (btc_ble_mesh_generic_client_args_t *)p_src; + u16_t length = 0U; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: { + dst->generic_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->generic_client_get_state.params) { + memcpy(dst->generic_client_get_state.params, src->generic_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->generic_client_get_state.get_state) { + dst->generic_client_get_state.get_state = (esp_ble_mesh_generic_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_generic_client_get_state_t)); + if (dst->generic_client_get_state.get_state) { + memcpy(dst->generic_client_get_state.get_state, src->generic_client_get_state.get_state, + sizeof(esp_ble_mesh_generic_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: { + dst->generic_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->generic_client_set_state.set_state = (esp_ble_mesh_generic_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_generic_client_set_state_t)); + if (dst->generic_client_set_state.params && dst->generic_client_set_state.set_state) { + memcpy(dst->generic_client_set_state.params, src->generic_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->generic_client_set_state.set_state, src->generic_client_set_state.set_state, + sizeof(esp_ble_mesh_generic_client_set_state_t)); + + switch (src->generic_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + if (src->generic_client_set_state.set_state->user_property_set.property_value) { + length = src->generic_client_set_state.set_state->user_property_set.property_value->len; + dst->generic_client_set_state.set_state->user_property_set.property_value = bt_mesh_alloc_buf(length); + if (!dst->generic_client_set_state.set_state->user_property_set.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->generic_client_set_state.set_state->user_property_set.property_value, + src->generic_client_set_state.set_state->user_property_set.property_value->data, + src->generic_client_set_state.set_state->user_property_set.property_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + if (src->generic_client_set_state.set_state->admin_property_set.property_value) { + length = src->generic_client_set_state.set_state->admin_property_set.property_value->len; + dst->generic_client_set_state.set_state->admin_property_set.property_value = bt_mesh_alloc_buf(length); + if (!dst->generic_client_set_state.set_state->admin_property_set.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->generic_client_set_state.set_state->admin_property_set.property_value, + src->generic_client_set_state.set_state->admin_property_set.property_value->data, + src->generic_client_set_state.set_state->admin_property_set.property_value->len); + } + break; + default: + break; + } + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_generic_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_generic_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_generic_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: + if (arg->generic_client_get_state.params) { + bt_mesh_free(arg->generic_client_get_state.params); + } + if (arg->generic_client_get_state.get_state) { + bt_mesh_free(arg->generic_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: + if (arg->generic_client_set_state.set_state) { + if (arg->generic_client_set_state.params) { + switch (arg->generic_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + bt_mesh_free_buf(arg->generic_client_set_state.set_state->user_property_set.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + bt_mesh_free_buf(arg->generic_client_set_state.set_state->admin_property_set.property_value); + break; + default: + break; + } + } + bt_mesh_free(arg->generic_client_set_state.set_state); + } + if (arg->generic_client_set_state.params) { + bt_mesh_free(arg->generic_client_set_state.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_generic_client_cb_param_t *p_dest_data = (esp_ble_mesh_generic_client_cb_param_t *)p_dest; + esp_ble_mesh_generic_client_cb_param_t *p_src_data = (esp_ble_mesh_generic_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: + if (p_src_data->status_cb.user_properties_status.property_ids) { + length = p_src_data->status_cb.user_properties_status.property_ids->len; + p_dest_data->status_cb.user_properties_status.property_ids = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.user_properties_status.property_ids) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.user_properties_status.property_ids, + p_src_data->status_cb.user_properties_status.property_ids->data, + p_src_data->status_cb.user_properties_status.property_ids->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: + if (p_src_data->status_cb.user_property_status.property_value) { + length = p_src_data->status_cb.user_property_status.property_value->len; + p_dest_data->status_cb.user_property_status.property_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.user_property_status.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.user_property_status.property_value, + p_src_data->status_cb.user_property_status.property_value->data, + p_src_data->status_cb.user_property_status.property_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: + if (p_src_data->status_cb.admin_properties_status.property_ids) { + length = p_src_data->status_cb.admin_properties_status.property_ids->len; + p_dest_data->status_cb.admin_properties_status.property_ids = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.admin_properties_status.property_ids) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.admin_properties_status.property_ids, + p_src_data->status_cb.admin_properties_status.property_ids->data, + p_src_data->status_cb.admin_properties_status.property_ids->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: + if (p_src_data->status_cb.admin_property_status.property_value) { + length = p_src_data->status_cb.admin_property_status.property_value->len; + p_dest_data->status_cb.admin_property_status.property_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.admin_property_status.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.admin_property_status.property_value, + p_src_data->status_cb.admin_property_status.property_value->data, + p_src_data->status_cb.admin_property_status.property_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS: + if (p_src_data->status_cb.manufacturer_properties_status.property_ids) { + length = p_src_data->status_cb.manufacturer_properties_status.property_ids->len; + p_dest_data->status_cb.manufacturer_properties_status.property_ids = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.manufacturer_properties_status.property_ids) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.manufacturer_properties_status.property_ids, + p_src_data->status_cb.manufacturer_properties_status.property_ids->data, + p_src_data->status_cb.manufacturer_properties_status.property_ids->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS: + if (p_src_data->status_cb.manufacturer_property_status.property_value) { + length = p_src_data->status_cb.manufacturer_property_status.property_value->len; + p_dest_data->status_cb.manufacturer_property_status.property_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.manufacturer_property_status.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.manufacturer_property_status.property_value, + p_src_data->status_cb.manufacturer_property_status.property_value->data, + p_src_data->status_cb.manufacturer_property_status.property_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: + if (p_src_data->status_cb.client_properties_status.property_ids) { + length = p_src_data->status_cb.client_properties_status.property_ids->len; + p_dest_data->status_cb.client_properties_status.property_ids = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.client_properties_status.property_ids) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.client_properties_status.property_ids, + p_src_data->status_cb.client_properties_status.property_ids->data, + p_src_data->status_cb.client_properties_status.property_ids->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_generic_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_generic_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: + bt_mesh_free_buf(arg->status_cb.user_properties_status.property_ids); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: + bt_mesh_free_buf(arg->status_cb.user_property_status.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: + bt_mesh_free_buf(arg->status_cb.admin_properties_status.property_ids); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: + bt_mesh_free_buf(arg->status_cb.admin_property_status.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTIES_STATUS: + bt_mesh_free_buf(arg->status_cb.manufacturer_properties_status.property_ids); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_MANUFACTURER_PROPERTY_STATUS: + bt_mesh_free_buf(arg->status_cb.manufacturer_property_status.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + case ESP_BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: + bt_mesh_free_buf(arg->status_cb.client_properties_status.property_ids); + break; + default: + break; + } + } + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_client_callback(esp_ble_mesh_generic_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_GENERIC_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_GENERIC_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_generic_client_cb_param_t), btc_ble_mesh_generic_client_copy_req_data); +} + +void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_generic_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE: + act = ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE: + act = ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH: + act = ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown generic client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_generic_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_generic_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_generic_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_client_common_param_t *params = NULL; + btc_ble_mesh_generic_client_args_t *arg = NULL; + esp_ble_mesh_generic_client_cb_param_t cb = {0}; + bt_mesh_client_common_param_t common = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_generic_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE: { + params = arg->generic_client_get_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->generic_client_get_state.params; + cb.error_code = bt_mesh_generic_client_get_state(&common, + (void *)arg->generic_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE: { + params = arg->generic_client_set_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->generic_client_set_state.params; + cb.error_code = bt_mesh_generic_client_set_state(&common, + (void *)arg->generic_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_generic_client_callback(&cb, ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_generic_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_generic_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_generic_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX) { + btc_ble_mesh_generic_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_generic_client_free_req_data(msg); + return; +} + +/* Generic Server Models related functions */ + +static inline void btc_ble_mesh_generic_server_cb_to_app( + esp_ble_mesh_generic_server_cb_event_t event, + esp_ble_mesh_generic_server_cb_param_t *param) +{ + esp_ble_mesh_generic_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_generic_server_cb_t)btc_profile_cb_get(BTC_PID_GENERIC_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_generic_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_generic_server_cb_param_t *p_dest_data = (esp_ble_mesh_generic_server_cb_param_t *)p_dest; + esp_ble_mesh_generic_server_cb_param_t *p_src_data = (esp_ble_mesh_generic_server_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT: + switch (p_src_data->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: + if (p_src_data->value.state_change.user_property_set.value) { + length = p_src_data->value.state_change.user_property_set.value->len; + p_dest_data->value.state_change.user_property_set.value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.user_property_set.value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.user_property_set.value, + p_src_data->value.state_change.user_property_set.value->data, + p_src_data->value.state_change.user_property_set.value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: + if (p_src_data->value.state_change.admin_property_set.value) { + length = p_src_data->value.state_change.admin_property_set.value->len; + p_dest_data->value.state_change.admin_property_set.value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.admin_property_set.value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.admin_property_set.value, + p_src_data->value.state_change.admin_property_set.value->data, + p_src_data->value.state_change.admin_property_set.value->len); + } + break; + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT: + switch (p_src_data->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: + if (p_src_data->value.set.user_property.property_value) { + length = p_src_data->value.set.user_property.property_value->len; + p_dest_data->value.set.user_property.property_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.set.user_property.property_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.set.user_property.property_value, + p_src_data->value.set.user_property.property_value->data, + p_src_data->value.set.user_property.property_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: + if (p_src_data->value.set.admin_property.property_value) { + length = p_src_data->value.set.admin_property.property_value->len; + p_dest_data->value.set.admin_property.property_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.set.admin_property.property_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.set.admin_property.property_value, + p_src_data->value.set.admin_property.property_value->data, + p_src_data->value.set.admin_property.property_value->len); + } + break; + default: + break; + } + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_server_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_generic_server_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_generic_server_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT: + switch (arg->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: + bt_mesh_free_buf(arg->value.state_change.user_property_set.value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: + bt_mesh_free_buf(arg->value.state_change.admin_property_set.value); + break; + default: + break; + } + break; + case ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT: + switch (arg->ctx.recv_op) { + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: + bt_mesh_free_buf(arg->value.set.user_property.property_value); + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: + bt_mesh_free_buf(arg->value.set.admin_property.property_value); + break; + default: + break; + } + break; + default: + break; + } +} + +static void btc_ble_mesh_generic_server_callback(esp_ble_mesh_generic_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_GENERIC_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_GENERIC_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_generic_server_cb_param_t), btc_ble_mesh_generic_server_copy_req_data); +} + +void bt_mesh_generic_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_generic_server_cb_param_t cb_params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (model == NULL || ctx == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_GENERIC_SERVER_STATE_CHANGE_EVT; + break; + case BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG: + act = ESP_BLE_MESH_GENERIC_SERVER_RECV_GET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG: + act = ESP_BLE_MESH_GENERIC_SERVER_RECV_SET_MSG_EVT; + break; + default: + BT_ERR("%s, Unknown Generic Server event type", __func__); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + + if (val && len) { + length = (len <= sizeof(cb_params.value)) ? len : sizeof(cb_params.value); + memcpy(&cb_params.value, val, length); + } + + btc_ble_mesh_generic_server_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_generic_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_generic_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_GENERIC_SERVER_EVT_MAX) { + btc_ble_mesh_generic_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_generic_server_free_req_data(msg); + return; +} diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c new file mode 100644 index 000000000..abb37bd2b --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_health_model.c @@ -0,0 +1,649 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_health_model.h" +#include "foundation.h" +#include "health_srv.h" +#include "health_cli.h" +#include "esp_ble_mesh_health_model_api.h" + +extern s32_t health_msg_timeout; + +/* Health Client Model related functions */ + +static inline void btc_ble_mesh_health_client_cb_to_app(esp_ble_mesh_health_client_cb_event_t event, + esp_ble_mesh_health_client_cb_param_t *param) +{ + esp_ble_mesh_health_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_health_client_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_health_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_health_client_args_t *dst = (btc_ble_mesh_health_client_args_t *)p_dest; + btc_ble_mesh_health_client_args_t *src = (btc_ble_mesh_health_client_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: { + dst->health_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->health_client_get_state.params) { + memcpy(dst->health_client_get_state.params, src->health_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->health_client_get_state.get_state) { + dst->health_client_get_state.get_state = (esp_ble_mesh_health_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_health_client_get_state_t)); + if (dst->health_client_get_state.get_state) { + memcpy(dst->health_client_get_state.get_state, src->health_client_get_state.get_state, + sizeof(esp_ble_mesh_health_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: { + dst->health_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->health_client_set_state.set_state = (esp_ble_mesh_health_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_health_client_set_state_t)); + if (dst->health_client_set_state.params && dst->health_client_set_state.set_state) { + memcpy(dst->health_client_set_state.params, src->health_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->health_client_set_state.set_state, src->health_client_set_state.set_state, + sizeof(esp_ble_mesh_health_client_set_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_health_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_health_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_health_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: + if (arg->health_client_get_state.params) { + bt_mesh_free(arg->health_client_get_state.params); + } + if (arg->health_client_get_state.get_state) { + bt_mesh_free(arg->health_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: + if (arg->health_client_set_state.params) { + bt_mesh_free(arg->health_client_set_state.params); + } + if (arg->health_client_set_state.set_state) { + bt_mesh_free(arg->health_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_health_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_health_client_cb_param_t *p_dest_data = (esp_ble_mesh_health_client_cb_param_t *)p_dest; + esp_ble_mesh_health_client_cb_param_t *p_src_data = (esp_ble_mesh_health_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case OP_HEALTH_CURRENT_STATUS: + if (p_src_data->status_cb.current_status.fault_array) { + length = p_src_data->status_cb.current_status.fault_array->len; + p_dest_data->status_cb.current_status.fault_array = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.current_status.fault_array) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.current_status.fault_array, + p_src_data->status_cb.current_status.fault_array->data, + p_src_data->status_cb.current_status.fault_array->len); + } + break; + case OP_HEALTH_FAULT_GET: + case OP_HEALTH_FAULT_CLEAR: + case OP_HEALTH_FAULT_TEST: + case OP_HEALTH_FAULT_STATUS: + if (p_src_data->status_cb.fault_status.fault_array) { + length = p_src_data->status_cb.fault_status.fault_array->len; + p_dest_data->status_cb.fault_status.fault_array = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.fault_status.fault_array) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.fault_status.fault_array, + p_src_data->status_cb.fault_status.fault_array->data, + p_src_data->status_cb.fault_status.fault_array->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_health_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_health_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case OP_HEALTH_CURRENT_STATUS: + bt_mesh_free_buf(arg->status_cb.current_status.fault_array); + break; + case OP_HEALTH_FAULT_GET: + case OP_HEALTH_FAULT_CLEAR: + case OP_HEALTH_FAULT_TEST: + case OP_HEALTH_FAULT_STATUS: + bt_mesh_free_buf(arg->status_cb.fault_status.fault_array); + break; + default: + break; + } + } + case ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_health_client_callback(esp_ble_mesh_health_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_HEALTH_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_HEALTH_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_health_client_cb_param_t), btc_ble_mesh_health_client_copy_req_data); +} + +void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, u16_t len) +{ + esp_ble_mesh_health_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE: + act = ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE: + act = ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH: + act = ESP_BLE_MESH_HEALTH_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_HEALTH_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown health client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_health_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_health_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_health_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +static int btc_ble_mesh_health_client_get_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_get_state_t *get) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (params == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (params->opcode == ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET && get == NULL) { + BT_ERR("%s, Invalid health client get", __func__); + return -EINVAL; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = params->ctx.app_idx; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + health_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_ATTENTION_GET: + return bt_mesh_health_attention_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_GET: + return bt_mesh_health_period_get(&ctx); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_GET: + return bt_mesh_health_fault_get(&ctx, get->fault_get.company_id); + default: + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return -EINVAL; + } + + return 0; +} + +static int btc_ble_mesh_health_client_set_state(esp_ble_mesh_client_common_param_t *params, + esp_ble_mesh_health_client_set_state_t *set) +{ + struct bt_mesh_msg_ctx ctx = {0}; + + if (params == NULL || set == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + ctx.net_idx = params->ctx.net_idx; + ctx.app_idx = params->ctx.app_idx; + ctx.addr = params->ctx.addr; + ctx.send_rel = params->ctx.send_rel; + ctx.send_ttl = params->ctx.send_ttl; + + health_msg_timeout = params->msg_timeout; + + switch (params->opcode) { + case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET: + return bt_mesh_health_attention_set(&ctx, set->attention_set.attention, true); + case ESP_BLE_MESH_MODEL_OP_ATTENTION_SET_UNACK: + return bt_mesh_health_attention_set(&ctx, set->attention_set.attention, false); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET: + return bt_mesh_health_period_set(&ctx, set->period_set.fast_period_divisor, true); + case ESP_BLE_MESH_MODEL_OP_HEALTH_PERIOD_SET_UNACK: + return bt_mesh_health_period_set(&ctx, set->period_set.fast_period_divisor, false); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST: + return bt_mesh_health_fault_test(&ctx, set->fault_test.company_id, set->fault_test.test_id, true); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_TEST_UNACK: + return bt_mesh_health_fault_test(&ctx, set->fault_test.company_id, set->fault_test.test_id, false); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR: + return bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, true); + case ESP_BLE_MESH_MODEL_OP_HEALTH_FAULT_CLEAR_UNACK: + return bt_mesh_health_fault_clear(&ctx, set->fault_clear.company_id, false); + default: + BT_ERR("%s, Invalid opcode 0x%x", __func__, params->opcode); + return -EINVAL; + } + + return 0; +} + +void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_health_client_args_t *arg = NULL; + esp_ble_mesh_health_client_cb_param_t cb = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_health_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE: { + cb.params = arg->health_client_get_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + cb.error_code = btc_ble_mesh_health_client_get_state(arg->health_client_get_state.params, + arg->health_client_get_state.get_state); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_health_client_callback(&cb, ESP_BLE_MESH_HEALTH_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE: { + cb.params = arg->health_client_set_state.params; + role_param.model = (struct bt_mesh_model *)cb.params->model; + role_param.role = cb.params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + cb.error_code = btc_ble_mesh_health_client_set_state(arg->health_client_set_state.params, + arg->health_client_set_state.set_state); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_health_client_callback(&cb, ESP_BLE_MESH_HEALTH_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_health_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_health_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_health_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_HEALTH_CLIENT_EVT_MAX) { + btc_ble_mesh_health_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_health_client_free_req_data(msg); + return; +} + +/* Health Server Model related functions */ + +static inline void btc_ble_mesh_health_server_cb_to_app(esp_ble_mesh_health_server_cb_event_t event, + esp_ble_mesh_health_server_cb_param_t *param) +{ + esp_ble_mesh_health_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_health_server_cb_t)btc_profile_cb_get(BTC_PID_HEALTH_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_server_arg_deep_free(btc_msg_t *msg) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_server_free_req_data(btc_msg_t *msg) +{ + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_health_server_callback(esp_ble_mesh_health_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_HEALTH_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_HEALTH_SERVER; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_health_server_cb_param_t), btc_ble_mesh_health_server_copy_req_data); +} + +void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_health_server_cb_param_t param = {0}; + btc_ble_mesh_health_server_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_health_server_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE: + param.fault_update_comp.element = arg->health_fault_update.element; + param.fault_update_comp.error_code = + bt_mesh_fault_update((struct bt_mesh_elem *)arg->health_fault_update.element); + btc_ble_mesh_health_server_callback(¶m, ESP_BLE_MESH_HEALTH_SERVER_FAULT_UPDATE_COMP_EVT); + break; + default: + break; + } + + btc_ble_mesh_health_server_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_health_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_health_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_HEALTH_SERVER_EVT_MAX) { + btc_ble_mesh_health_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_health_server_free_req_data(msg); + return; +} + +void btc_ble_mesh_health_server_fault_clear(struct bt_mesh_model *model, u16_t company_id) +{ + esp_ble_mesh_health_server_cb_param_t param = {0}; + + param.fault_clear.model = (esp_ble_mesh_model_t *)model; + param.fault_clear.company_id = company_id; + + btc_ble_mesh_health_server_callback(¶m, ESP_BLE_MESH_HEALTH_SERVER_FAULT_CLEAR_EVT); +} + +void btc_ble_mesh_health_server_fault_test(struct bt_mesh_model *model, u8_t test_id, u16_t company_id) +{ + esp_ble_mesh_health_server_cb_param_t param = {0}; + + param.fault_test.model = (esp_ble_mesh_model_t *)model; + param.fault_test.test_id = test_id; + param.fault_test.company_id = company_id; + + btc_ble_mesh_health_server_callback(¶m, ESP_BLE_MESH_HEALTH_SERVER_FAULT_TEST_EVT); +} + +void btc_ble_mesh_health_server_attention_on(struct bt_mesh_model *model, u8_t time) +{ + esp_ble_mesh_health_server_cb_param_t param = {0}; + + param.attention_on.model = (esp_ble_mesh_model_t *)model; + param.attention_on.time = time; + + btc_ble_mesh_health_server_callback(¶m, ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_ON_EVT); +} + +void btc_ble_mesh_health_server_attention_off(struct bt_mesh_model *model) +{ + esp_ble_mesh_health_server_cb_param_t param = {0}; + + param.attention_off.model = (esp_ble_mesh_model_t *)model; + + btc_ble_mesh_health_server_callback(¶m, ESP_BLE_MESH_HEALTH_SERVER_ATTENTION_OFF_EVT); +} diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c new file mode 100644 index 000000000..881693144 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_lighting_model.c @@ -0,0 +1,589 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_lighting_model.h" +#include "lighting_client.h" +#include "esp_ble_mesh_lighting_model_api.h" + +/* Lighting Client Models related functions */ + +static inline void btc_ble_mesh_lighting_client_cb_to_app(esp_ble_mesh_light_client_cb_event_t event, + esp_ble_mesh_light_client_cb_param_t *param) +{ + esp_ble_mesh_light_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_light_client_cb_t)btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_lighting_client_args_t *dst = (btc_ble_mesh_lighting_client_args_t *)p_dest; + btc_ble_mesh_lighting_client_args_t *src = (btc_ble_mesh_lighting_client_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { + dst->light_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->light_client_get_state.params) { + memcpy(dst->light_client_get_state.params, src->light_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->light_client_get_state.get_state) { + dst->light_client_get_state.get_state = (esp_ble_mesh_light_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_get_state_t)); + if (dst->light_client_get_state.get_state) { + memcpy(dst->light_client_get_state.get_state, src->light_client_get_state.get_state, + sizeof(esp_ble_mesh_light_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { + dst->light_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->light_client_set_state.set_state = (esp_ble_mesh_light_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_light_client_set_state_t)); + if (dst->light_client_set_state.params && dst->light_client_set_state.set_state) { + memcpy(dst->light_client_set_state.params, src->light_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->light_client_set_state.set_state, src->light_client_set_state.set_state, + sizeof(esp_ble_mesh_light_client_set_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_lighting_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_lighting_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: + if (arg->light_client_get_state.params) { + bt_mesh_free(arg->light_client_get_state.params); + } + if (arg->light_client_get_state.get_state) { + bt_mesh_free(arg->light_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: + if (arg->light_client_set_state.params) { + bt_mesh_free(arg->light_client_set_state.params); + } + if (arg->light_client_set_state.set_state) { + bt_mesh_free(arg->light_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_light_client_cb_param_t *p_dest_data = (esp_ble_mesh_light_client_cb_param_t *)p_dest; + esp_ble_mesh_light_client_cb_param_t *p_src_data = (esp_ble_mesh_light_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: + if (p_src_data->status_cb.lc_property_status.property_value) { + length = p_src_data->status_cb.lc_property_status.property_value->len; + p_dest_data->status_cb.lc_property_status.property_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.lc_property_status.property_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.lc_property_status.property_value, + p_src_data->status_cb.lc_property_status.property_value->data, + p_src_data->status_cb.lc_property_status.property_value->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_light_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_light_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: + case ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: + bt_mesh_free_buf(arg->status_cb.lc_property_status.property_value); + break; + default: + break; + } + } + case ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_client_callback(esp_ble_mesh_light_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_LIGHTING_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_LIGHTING_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_light_client_cb_param_t), btc_ble_mesh_lighting_client_copy_req_data); +} + +void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_light_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE: + act = ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE: + act = ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH: + act = ESP_BLE_MESH_LIGHT_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_LIGHT_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown lighting client event type", __func__); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_lighting_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_lighting_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_lighting_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_client_common_param_t *params = NULL; + btc_ble_mesh_lighting_client_args_t *arg = NULL; + esp_ble_mesh_light_client_cb_param_t cb = {0}; + bt_mesh_client_common_param_t common = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_lighting_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE: { + params = arg->light_client_get_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->light_client_get_state.params; + cb.error_code = bt_mesh_light_client_get_state(&common, + (void *)arg->light_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE: { + params = arg->light_client_set_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->light_client_set_state.params; + cb.error_code = bt_mesh_light_client_set_state(&common, + (void *)arg->light_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_lighting_client_callback(&cb, ESP_BLE_MESH_LIGHT_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_lighting_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_light_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_light_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_LIGHT_CLIENT_EVT_MAX) { + btc_ble_mesh_lighting_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_lighting_client_free_req_data(msg); + return; +} + +/* Lighting Server Models related functions */ + +static inline void btc_ble_mesh_lighting_server_cb_to_app( + esp_ble_mesh_lighting_server_cb_event_t event, + esp_ble_mesh_lighting_server_cb_param_t *param) +{ + esp_ble_mesh_lighting_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_lighting_server_cb_t)btc_profile_cb_get(BTC_PID_LIGHTING_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_lighting_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_lighting_server_cb_param_t *p_dest_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_dest; + esp_ble_mesh_lighting_server_cb_param_t *p_src_data = (esp_ble_mesh_lighting_server_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + if (p_src_data->value.state_change.lc_property_set.property_value) { + length = p_src_data->value.state_change.lc_property_set.property_value->len; + p_dest_data->value.state_change.lc_property_set.property_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.lc_property_set.property_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.lc_property_set.property_value, + p_src_data->value.state_change.lc_property_set.property_value->data, + p_src_data->value.state_change.lc_property_set.property_value->len); + } + } + break; + case ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + if (p_src_data->value.set.lc_property.property_value) { + length = p_src_data->value.set.lc_property.property_value->len; + p_dest_data->value.set.lc_property.property_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.set.lc_property.property_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.set.lc_property.property_value, + p_src_data->value.set.lc_property.property_value->data, + p_src_data->value.set.lc_property.property_value->len); + } + } + break; + case ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS) { + if (p_src_data->value.status.sensor_status.data) { + length = p_src_data->value.status.sensor_status.data->len; + p_dest_data->value.status.sensor_status.data = bt_mesh_alloc_buf(length); + if (p_dest_data->value.status.sensor_status.data == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.status.sensor_status.data, + p_src_data->value.status.sensor_status.data->data, + p_src_data->value.status.sensor_status.data->len); + } + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_server_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_lighting_server_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_lighting_server_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + bt_mesh_free_buf(arg->value.state_change.lc_property_set.property_value); + } + break; + case ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK) { + bt_mesh_free_buf(arg->value.set.lc_property.property_value); + } + break; + case ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS) { + bt_mesh_free_buf(arg->value.status.sensor_status.data); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_lighting_server_callback(esp_ble_mesh_lighting_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_LIGHTING_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_LIGHTING_SERVER; + msg.act = act; + + btc_transfer_context( + &msg, cb_params, sizeof(esp_ble_mesh_lighting_server_cb_param_t), btc_ble_mesh_lighting_server_copy_req_data); +} + +void bt_mesh_lighting_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_lighting_server_cb_param_t cb_params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (model == NULL || ctx == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_LIGHTING_SERVER_STATE_CHANGE_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG: + act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_GET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG: + act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_SET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG: + act = ESP_BLE_MESH_LIGHTING_SERVER_RECV_STATUS_MSG_EVT; + break; + default: + BT_ERR("%s, Unknown Lighting Server event type", __func__); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + + if (val && len) { + length = (len <= sizeof(cb_params.value)) ? len : sizeof(cb_params.value); + memcpy(&cb_params.value, val, length); + } + + btc_ble_mesh_lighting_server_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_lighting_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_lighting_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_LIGHTING_SERVER_EVT_MAX) { + btc_ble_mesh_lighting_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_lighting_server_free_req_data(msg); + return; +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c new file mode 100644 index 000000000..f0d4ea066 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_prov.c @@ -0,0 +1,2067 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_prov.h" +#include "btc_ble_mesh_config_model.h" +#include "btc_ble_mesh_health_model.h" +#include "btc_ble_mesh_generic_model.h" +#include "btc_ble_mesh_time_scene_model.h" +#include "btc_ble_mesh_sensor_model.h" +#include "btc_ble_mesh_lighting_model.h" + +#include "adv.h" +#include "mesh_kernel.h" +#include "mesh_proxy.h" +#include "mesh.h" +#include "access.h" +#include "prov.h" +#include "proxy_server.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#include "cfg_cli.h" +#include "health_cli.h" +#include "cfg_srv.h" +#include "health_srv.h" +#include "generic_client.h" +#include "lighting_client.h" +#include "sensor_client.h" +#include "time_scene_client.h" +#include "client_common.h" +#include "state_binding.h" +#include "local_operation.h" + +#include "esp_ble_mesh_common_api.h" +#include "esp_ble_mesh_provisioning_api.h" +#include "esp_ble_mesh_networking_api.h" + +static inline void btc_ble_mesh_prov_cb_to_app(esp_ble_mesh_prov_cb_event_t event, + esp_ble_mesh_prov_cb_param_t *param) +{ + esp_ble_mesh_prov_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_prov_cb_t)btc_profile_cb_get(BTC_PID_PROV); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static inline void btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event, + esp_ble_mesh_model_cb_param_t *param) +{ + esp_ble_mesh_model_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_model_cb_t)btc_profile_cb_get(BTC_PID_MODEL); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_prov_args_t *dst = (btc_ble_mesh_prov_args_t *)p_dest; + btc_ble_mesh_prov_args_t *src = (btc_ble_mesh_prov_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR", __func__); + dst->proxy_client_add_filter_addr.addr = (uint16_t *)bt_mesh_calloc(src->proxy_client_add_filter_addr.addr_num << 1); + if (dst->proxy_client_add_filter_addr.addr) { + memcpy(dst->proxy_client_add_filter_addr.addr, src->proxy_client_add_filter_addr.addr, + src->proxy_client_add_filter_addr.addr_num << 1); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR", __func__); + dst->proxy_client_remove_filter_addr.addr = bt_mesh_calloc(src->proxy_client_remove_filter_addr.addr_num << 1); + if (dst->proxy_client_remove_filter_addr.addr) { + memcpy(dst->proxy_client_remove_filter_addr.addr, src->proxy_client_remove_filter_addr.addr, + src->proxy_client_remove_filter_addr.addr_num << 1); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA", __func__); + dst->store_node_comp_data.data = bt_mesh_calloc(src->store_node_comp_data.length); + if (dst->store_node_comp_data.data) { + memcpy(dst->store_node_comp_data.data, src->store_node_comp_data.data, src->store_node_comp_data.length); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_prov_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_prov_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: + if (arg->proxy_client_add_filter_addr.addr) { + bt_mesh_free(arg->proxy_client_add_filter_addr.addr); + } + break; + case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: + if (arg->proxy_client_remove_filter_addr.addr) { + bt_mesh_free(arg->proxy_client_remove_filter_addr.addr); + } + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + if (arg->store_node_comp_data.data) { + bt_mesh_free(arg->store_node_comp_data.data); + } + break; + default: + break; + } +} + +void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_model_args_t *dst = (btc_ble_mesh_model_args_t *)p_dest; + btc_ble_mesh_model_args_t *src = (btc_ble_mesh_model_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: + case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: { + BT_DBG("%s, BTC_BLE_MESH_ACT_MODEL_SEND, src->model_send.length = %d", __func__, src->model_send.length); + dst->model_send.data = src->model_send.length ? (uint8_t *)bt_mesh_malloc(src->model_send.length) : NULL; + dst->model_send.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + if (src->model_send.length) { + if (dst->model_send.data) { + memcpy(dst->model_send.data, src->model_send.data, src->model_send.length); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + if (dst->model_send.ctx) { + memcpy(dst->model_send.ctx, src->model_send.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE: + BT_DBG("%s, BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE", __func__); + dst->model_update_state.value = bt_mesh_malloc(sizeof(esp_ble_mesh_server_state_value_t)); + if (dst->model_update_state.value) { + memcpy(dst->model_update_state.value, src->model_update_state.value, + sizeof(esp_ble_mesh_server_state_value_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +static void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_model_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_model_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: + case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: + if (arg->model_send.data) { + bt_mesh_free(arg->model_send.data); + } + if (arg->model_send.ctx) { + bt_mesh_free(arg->model_send.ctx); + } + break; + case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE: + if (arg->model_update_state.value) { + bt_mesh_free(arg->model_update_state.value); + } + break; + default: + break; + } + + return; +} + +static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_model_cb_param_t *p_dest_data = (esp_ble_mesh_model_cb_param_t *)p_dest; + esp_ble_mesh_model_cb_param_t *p_src_data = (esp_ble_mesh_model_cb_param_t *)p_src; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_MODEL_OPERATION_EVT: { + if (p_src_data->model_operation.ctx && p_src_data->model_operation.msg) { + p_dest_data->model_operation.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->model_operation.msg = p_src_data->model_operation.length ? (uint8_t *)bt_mesh_malloc(p_src_data->model_operation.length) : NULL; + if (p_dest_data->model_operation.ctx) { + memcpy(p_dest_data->model_operation.ctx, p_src_data->model_operation.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + if (p_src_data->model_operation.length) { + if (p_dest_data->model_operation.msg) { + memcpy(p_dest_data->model_operation.msg, p_src_data->model_operation.msg, p_src_data->model_operation.length); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + } + break; + } + case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: { + if (p_src_data->client_recv_publish_msg.ctx && p_src_data->client_recv_publish_msg.msg) { + p_dest_data->client_recv_publish_msg.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + p_dest_data->client_recv_publish_msg.msg = p_src_data->client_recv_publish_msg.length ? (uint8_t *)bt_mesh_malloc(p_src_data->client_recv_publish_msg.length) : NULL; + if (p_dest_data->client_recv_publish_msg.ctx) { + memcpy(p_dest_data->client_recv_publish_msg.ctx, p_src_data->client_recv_publish_msg.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + if (p_src_data->client_recv_publish_msg.length) { + if (p_dest_data->client_recv_publish_msg.msg) { + memcpy(p_dest_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.length); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + } + break; + } + case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: { + if (p_src_data->model_send_comp.ctx) { + p_dest_data->model_send_comp.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + if (p_dest_data->model_send_comp.ctx) { + memcpy(p_dest_data->model_send_comp.ctx, p_src_data->model_send_comp.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: { + if (p_src_data->client_send_timeout.ctx) { + p_dest_data->client_send_timeout.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t)); + if (p_dest_data->client_send_timeout.ctx) { + memcpy(p_dest_data->client_send_timeout.ctx, p_src_data->client_send_timeout.ctx, sizeof(esp_ble_mesh_msg_ctx_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + default: + break; + } +} + +static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_model_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_model_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_MODEL_OPERATION_EVT: { + if (arg->model_operation.msg) { + bt_mesh_free(arg->model_operation.msg); + } + if (arg->model_operation.ctx) { + bt_mesh_free(arg->model_operation.ctx); + } + break; + } + case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: { + if (arg->client_recv_publish_msg.msg) { + bt_mesh_free(arg->client_recv_publish_msg.msg); + } + if (arg->client_recv_publish_msg.ctx) { + bt_mesh_free(arg->client_recv_publish_msg.ctx); + } + break; + } + case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: { + if (arg->model_send_comp.ctx) { + bt_mesh_free(arg->model_send_comp.ctx); + } + break; + } + case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: { + if (arg->client_send_timeout.ctx) { + bt_mesh_free(arg->client_send_timeout.ctx); + } + break; + } + default: + break; + } +} + +static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *param, uint8_t act) +{ + btc_msg_t msg = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_MODEL)) { + return BT_STATUS_SUCCESS; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_MODEL; + msg.act = act; + + ret = btc_transfer_context(&msg, param, + sizeof(esp_ble_mesh_model_cb_param_t), btc_ble_mesh_model_copy_req_data); + if (ret != BT_STATUS_SUCCESS) { + BT_ERR("%s, btc_transfer_context failed", __func__); + } + return ret; +} + +static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + + mesh_param.model_operation.opcode = ctx->recv_op; + mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model; + mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; + mesh_param.model_operation.length = buf->len; + mesh_param.model_operation.msg = buf->data; + + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT); + return; +} + +static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + bt_mesh_client_node_t *node = NULL; + + if (!model || !model->user_data || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_client_model_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, false); + if (node == NULL) { + mesh_param.client_recv_publish_msg.opcode = ctx->recv_op; + mesh_param.client_recv_publish_msg.model = (esp_ble_mesh_model_t *)model; + mesh_param.client_recv_publish_msg.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; + mesh_param.client_recv_publish_msg.length = buf->len; + mesh_param.client_recv_publish_msg.msg = buf->data; + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT); + } else { + mesh_param.model_operation.opcode = ctx->recv_op; + mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model; + mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx; + mesh_param.model_operation.length = buf->len; + mesh_param.model_operation.msg = buf->data; + if (!k_delayed_work_free(&node->timer)) { + bt_mesh_client_free_node(node); + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT); + } + } + + bt_mesh_client_model_unlock(); + return; +} + +static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + + bt_mesh_client_model_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + mesh_param.client_send_timeout.opcode = node->opcode; + mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)ctx.model; + mesh_param.client_send_timeout.ctx = (esp_ble_mesh_msg_ctx_t *)&ctx; + bt_mesh_client_free_node(node); + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT); + } + } + + bt_mesh_client_model_unlock(); + return; +} + +static void btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t *model, esp_ble_mesh_msg_ctx_t *ctx, u32_t opcode, int err) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + + mesh_param.model_send_comp.err_code = err; + mesh_param.model_send_comp.opcode = opcode; + mesh_param.model_send_comp.model = model; + mesh_param.model_send_comp.ctx = ctx; + + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_SEND_COMP_EVT); + return; +} + +static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int err) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + + mesh_param.model_publish_comp.err_code = err; + mesh_param.model_publish_comp.model = model; + + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT); + return; +} + +static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.model_publish_update.model = (esp_ble_mesh_model_t *)mod; + + ret = btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static void btc_ble_mesh_server_model_update_state_comp_cb(esp_ble_mesh_model_t *model, + esp_ble_mesh_server_state_type_t type, int err) +{ + esp_ble_mesh_model_cb_param_t mesh_param = {0}; + + mesh_param.server_model_update_state.err_code = err; + mesh_param.server_model_update_state.model = model; + mesh_param.server_model_update_state.type = type; + + btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT); + return; +} + +static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) +{ + btc_msg_t msg = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_PROV)) { + return BT_STATUS_SUCCESS; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_PROV; + msg.act = act; + + ret = btc_transfer_context(&msg, param, sizeof(esp_ble_mesh_prov_cb_param_t), NULL); + if (ret != BT_STATUS_SUCCESS) { + BT_ERR("%s, btc_transfer_context failed", __func__); + } + return ret; +} + +#if CONFIG_BLE_MESH_NODE +static void btc_ble_mesh_oob_pub_key_cb(void) +{ + BT_DBG("%s", __func__); + + btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT); + return; +} + +static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, u32_t num) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.node_prov_output_num.action = (esp_ble_mesh_output_action_t)act; + mesh_param.node_prov_output_num.number = num; + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static int btc_ble_mesh_output_string_cb(const char *str) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + strncpy(mesh_param.node_prov_output_str.string, str, + MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string))); + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, u8_t size) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.node_prov_input.action = (esp_ble_mesh_input_action_t)act; + mesh_param.node_prov_input.size = size; + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_INPUT_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.node_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT); + return; +} + +static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.node_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT); + return; +} + +static void btc_ble_mesh_complete_cb(u16_t net_idx, const u8_t net_key[16], u16_t addr, u8_t flags, u32_t iv_index) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.node_prov_complete.net_idx = net_idx; + memcpy(mesh_param.node_prov_complete.net_key, net_key, 16); + mesh_param.node_prov_complete.addr = addr; + mesh_param.node_prov_complete.flags = flags; + mesh_param.node_prov_complete.iv_index = iv_index; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT); + return; +} + +static void btc_ble_mesh_reset_cb(void) +{ + BT_DBG("%s", __func__); + + btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_RESET_EVT); + return; +} +#endif /* CONFIG_BLE_MESH_NODE */ + +static void btc_ble_mesh_prov_register_complete_cb(int err_code) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.prov_register_comp.err_code = err_code; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROV_REGISTER_COMP_EVT); + return; +} + +static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act) +{ + BT_DBG("%s", __func__); + + btc_ble_mesh_prov_callback(param, act); + return; +} + +#if CONFIG_BLE_MESH_PROVISIONER +static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb( + const u8_t addr[6], const u8_t addr_type, + const u8_t adv_type, const u8_t dev_uuid[16], + u16_t oob_info, bt_mesh_prov_bearer_t bearer, s8_t rssi) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + if (addr == NULL || dev_uuid == NULL || + (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.dev_uuid, dev_uuid, 16); + memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.addr, addr, BLE_MESH_ADDR_LEN); + mesh_param.provisioner_recv_unprov_adv_pkt.addr_type = addr_type; + mesh_param.provisioner_recv_unprov_adv_pkt.oob_info = oob_info; + mesh_param.provisioner_recv_unprov_adv_pkt.adv_type = adv_type; + mesh_param.provisioner_recv_unprov_adv_pkt.bearer = bearer; + mesh_param.provisioner_recv_unprov_adv_pkt.rssi = rssi; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT); + return; +} + +static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(u8_t link_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_read_oob_pub_key.link_idx = link_idx; + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static int btc_ble_mesh_provisioner_prov_input_cb(u8_t method, + bt_mesh_output_action_t act, u8_t size, u8_t link_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_input.method = (esp_ble_mesh_oob_method_t)method; + mesh_param.provisioner_prov_input.action = (esp_ble_mesh_output_action_t)act; + mesh_param.provisioner_prov_input.size = size; + mesh_param.provisioner_prov_input.link_idx = link_idx; + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static int btc_ble_mesh_provisioner_prov_output_cb(u8_t method, + bt_mesh_input_action_t act, void *data, u8_t size, u8_t link_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + bt_status_t ret = BT_STATUS_SUCCESS; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_output.method = (esp_ble_mesh_oob_method_t)method; + mesh_param.provisioner_prov_output.action = (esp_ble_mesh_input_action_t)act; + mesh_param.provisioner_prov_output.size = size; + mesh_param.provisioner_prov_output.link_idx = link_idx; + if (act == BLE_MESH_ENTER_STRING) { + strncpy(mesh_param.provisioner_prov_output.string, (char *)data, size); + } else { + mesh_param.provisioner_prov_output.number = sys_get_le32((u8_t *)data); + } + + ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT); + return (ret == BT_STATUS_SUCCESS) ? 0 : -1; +} + +static void btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT); + return; +} + +static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, u8_t reason) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer; + mesh_param.provisioner_prov_link_close.reason = reason; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT); + return; +} + +static void btc_ble_mesh_provisioner_prov_complete_cb( + u16_t node_idx, const u8_t device_uuid[16], + u16_t unicast_addr, u8_t element_num, + u16_t netkey_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.provisioner_prov_complete.node_idx = node_idx; + mesh_param.provisioner_prov_complete.unicast_addr = unicast_addr; + mesh_param.provisioner_prov_complete.element_num = element_num; + mesh_param.provisioner_prov_complete.netkey_idx = netkey_idx; + memcpy(mesh_param.provisioner_prov_complete.device_uuid, device_uuid, sizeof(esp_ble_mesh_octet16_t)); + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT); + return; +} + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_uuid(uuid); +} + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr) +{ + return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr); +} + +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +static void btc_ble_mesh_heartbeat_msg_recv_cb(u8_t hops, u16_t feature) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + BT_DBG("%s", __func__); + + mesh_param.heartbeat_msg_recv.hops = hops; + mesh_param.heartbeat_msg_recv.feature = feature; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT); + return; +} + +#if CONFIG_BLE_MESH_LOW_POWER +static void btc_ble_mesh_lpn_cb(u16_t friend_addr, bool established) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + u8_t act = 0U; + + BT_DBG("%s", __func__); + + if (established) { + mesh_param.lpn_friendship_establish.friend_addr = friend_addr; + act = ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT; + } else { + mesh_param.lpn_friendship_terminate.friend_addr = friend_addr; + act = ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT; + } + + btc_ble_mesh_prov_callback(&mesh_param, act); + return; +} +#endif /* CONFIG_BLE_MESH_LOW_POWER */ + +#if CONFIG_BLE_MESH_FRIEND +void btc_ble_mesh_friend_cb(bool establish, u16_t lpn_addr, u8_t reason) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + u8_t act = 0U; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) { + BT_ERR("%s, Not a unicast address", __func__); + return; + } + + if (establish) { + mesh_param.frnd_friendship_establish.lpn_addr = lpn_addr; + act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT; + } else { + mesh_param.frnd_friendship_terminate.lpn_addr = lpn_addr; + mesh_param.frnd_friendship_terminate.reason = reason; + act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT; + } + + btc_ble_mesh_prov_callback(&mesh_param, act); + return; +} +#endif /* CONFIG_BLE_MESH_FRIEND */ + +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT +static void btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t *addr, + u8_t type, bt_mesh_proxy_adv_ctx_t *ctx, s8_t rssi) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + if (!addr || !ctx || type != BLE_MESH_PROXY_ADV_NET_ID) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + BT_DBG("%s", __func__); + + mesh_param.proxy_client_recv_adv_pkt.addr_type = addr->type; + memcpy(mesh_param.proxy_client_recv_adv_pkt.addr, addr->val, BD_ADDR_LEN); + mesh_param.proxy_client_recv_adv_pkt.net_idx = ctx->net_id.net_idx; + memcpy(mesh_param.proxy_client_recv_adv_pkt.net_id, ctx->net_id.net_id, 8); + mesh_param.proxy_client_recv_adv_pkt.rssi = rssi; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT); + return; +} + +static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr, + u8_t conn_handle, u16_t net_idx) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + if (!addr || conn_handle >= BLE_MESH_MAX_CONN) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + BT_DBG("%s", __func__); + + mesh_param.proxy_client_connected.addr_type = addr->type; + memcpy(mesh_param.proxy_client_connected.addr, addr->val, BD_ADDR_LEN); + mesh_param.proxy_client_connected.conn_handle = conn_handle; + mesh_param.proxy_client_connected.net_idx = net_idx; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT); + return; +} + +static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, + u8_t conn_handle, u16_t net_idx, u8_t reason) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + if (!addr || conn_handle >= BLE_MESH_MAX_CONN) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + BT_DBG("%s", __func__); + + mesh_param.proxy_client_disconnected.addr_type = addr->type; + memcpy(mesh_param.proxy_client_disconnected.addr, addr->val, BD_ADDR_LEN); + mesh_param.proxy_client_disconnected.conn_handle = conn_handle; + mesh_param.proxy_client_disconnected.net_idx = net_idx; + mesh_param.proxy_client_disconnected.reason = reason; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT); + return; +} + +static void btc_ble_mesh_proxy_client_filter_status_recv_cb(u8_t conn_handle, + u16_t src, u16_t net_idx, u8_t filter_type, u16_t list_size) +{ + esp_ble_mesh_prov_cb_param_t mesh_param = {0}; + + if (conn_handle >= BLE_MESH_MAX_CONN) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + BT_DBG("%s", __func__); + + mesh_param.proxy_client_recv_filter_status.conn_handle = conn_handle; + mesh_param.proxy_client_recv_filter_status.server_addr = src; + mesh_param.proxy_client_recv_filter_status.net_idx = net_idx; + mesh_param.proxy_client_recv_filter_status.filter_type = filter_type; + mesh_param.proxy_client_recv_filter_status.list_size = list_size; + + btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT); + return; +} +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model) +{ + __ASSERT(model && model->op, "%s, Invalid parameter", __func__); + esp_ble_mesh_model_op_t *op = model->op; + while (op != NULL && op->opcode != 0) { + op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_client_model_op_cb; + op++; + } + return bt_mesh_client_init((struct bt_mesh_model *)model); +} + +int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model) +{ + return bt_mesh_client_deinit((struct bt_mesh_model *)model); +} + +int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod) +{ + return bt_mesh_model_pub_period_get((struct bt_mesh_model *)mod); +} + +uint16_t btc_ble_mesh_get_primary_addr(void) +{ + return bt_mesh_primary_addr(); +} + +uint16_t *btc_ble_mesh_model_find_group(esp_ble_mesh_model_t *mod, uint16_t addr) +{ + return bt_mesh_model_find_group((struct bt_mesh_model *)mod, addr); +} + +esp_ble_mesh_elem_t *btc_ble_mesh_elem_find(u16_t addr) +{ + return (esp_ble_mesh_elem_t *)bt_mesh_elem_find(addr); +} + +uint8_t btc_ble_mesh_elem_count(void) +{ + return bt_mesh_elem_count(); +} + +esp_ble_mesh_model_t *btc_ble_mesh_model_find_vnd(const esp_ble_mesh_elem_t *elem, + uint16_t company, uint16_t id) +{ + return (esp_ble_mesh_model_t *)bt_mesh_model_find_vnd((struct bt_mesh_elem *)elem, company, id); +} + +esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, uint16_t id) +{ + return (esp_ble_mesh_model_t *)bt_mesh_model_find((struct bt_mesh_elem *)elem, id); +} + +const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void) +{ + return (const esp_ble_mesh_comp_t *)bt_mesh_comp_get(); +} + +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void) +{ + return bt_mesh_provisioner_get_node_count(); +} + +/* Configuration Models */ +extern const struct bt_mesh_model_op bt_mesh_cfg_srv_op[]; +extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[]; +/* Health Models */ +extern const struct bt_mesh_model_op bt_mesh_health_srv_op[]; +extern const struct bt_mesh_model_op bt_mesh_health_cli_op[]; +/* Generic Client Models */ +extern const struct bt_mesh_model_op gen_onoff_cli_op[]; +extern const struct bt_mesh_model_op gen_level_cli_op[]; +extern const struct bt_mesh_model_op gen_def_trans_time_cli_op[]; +extern const struct bt_mesh_model_op gen_power_onoff_cli_op[]; +extern const struct bt_mesh_model_op gen_power_level_cli_op[]; +extern const struct bt_mesh_model_op gen_battery_cli_op[]; +extern const struct bt_mesh_model_op gen_location_cli_op[]; +extern const struct bt_mesh_model_op gen_property_cli_op[]; +/* Lighting Client Models */ +extern const struct bt_mesh_model_op light_lightness_cli_op[]; +extern const struct bt_mesh_model_op light_ctl_cli_op[]; +extern const struct bt_mesh_model_op light_hsl_cli_op[]; +extern const struct bt_mesh_model_op light_xyl_cli_op[]; +extern const struct bt_mesh_model_op light_lc_cli_op[]; +/* Sensor Client Models */ +extern const struct bt_mesh_model_op sensor_cli_op[]; +/* Time and Scenes Client Models */ +extern const struct bt_mesh_model_op time_cli_op[]; +extern const struct bt_mesh_model_op scene_cli_op[]; +extern const struct bt_mesh_model_op scheduler_cli_op[]; +/* Generic Server Models */ +extern const struct bt_mesh_model_op gen_onoff_srv_op[]; +extern const struct bt_mesh_model_op gen_level_srv_op[]; +extern const struct bt_mesh_model_op gen_def_trans_time_srv_op[]; +extern const struct bt_mesh_model_op gen_power_onoff_srv_op[]; +extern const struct bt_mesh_model_op gen_power_onoff_setup_srv_op[]; +extern const struct bt_mesh_model_op gen_power_level_srv_op[]; +extern const struct bt_mesh_model_op gen_power_level_setup_srv_op[]; +extern const struct bt_mesh_model_op gen_battery_srv_op[]; +extern const struct bt_mesh_model_op gen_location_srv_op[]; +extern const struct bt_mesh_model_op gen_location_setup_srv_op[]; +extern const struct bt_mesh_model_op gen_user_prop_srv_op[]; +extern const struct bt_mesh_model_op gen_admin_prop_srv_op[]; +extern const struct bt_mesh_model_op gen_manu_prop_srv_op[]; +extern const struct bt_mesh_model_op gen_client_prop_srv_op[]; +/* Lighting Server Models */ +extern const struct bt_mesh_model_op light_lightness_srv_op[]; +extern const struct bt_mesh_model_op light_lightness_setup_srv_op[]; +extern const struct bt_mesh_model_op light_ctl_srv_op[]; +extern const struct bt_mesh_model_op light_ctl_setup_srv_op[]; +extern const struct bt_mesh_model_op light_ctl_temp_srv_op[]; +extern const struct bt_mesh_model_op light_hsl_srv_op[]; +extern const struct bt_mesh_model_op light_hsl_hue_srv_op[]; +extern const struct bt_mesh_model_op light_hsl_sat_srv_op[]; +extern const struct bt_mesh_model_op light_hsl_setup_srv_op[]; +extern const struct bt_mesh_model_op light_xyl_srv_op[]; +extern const struct bt_mesh_model_op light_xyl_setup_srv_op[]; +extern const struct bt_mesh_model_op light_lc_srv_op[]; +extern const struct bt_mesh_model_op light_lc_setup_srv_op[]; +/* Time and Scenes Server Models */ +extern const struct bt_mesh_model_op time_srv_op[]; +extern const struct bt_mesh_model_op time_setup_srv_op[]; +extern const struct bt_mesh_model_op scene_srv_op[]; +extern const struct bt_mesh_model_op scene_setup_srv_op[]; +extern const struct bt_mesh_model_op scheduler_srv_op[]; +extern const struct bt_mesh_model_op scheduler_setup_srv_op[]; +/* Sensor Server Models */ +extern const struct bt_mesh_model_op sensor_srv_op[]; +extern const struct bt_mesh_model_op sensor_setup_srv_op[]; + +static void btc_ble_mesh_model_op_add(esp_ble_mesh_model_t *model) +{ + esp_ble_mesh_model_op_t *op = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + /* For SIG client and server models, model->op will be NULL and initialized here. + * For vendor models whose opcode is 3 bytes, model->op will be initialized here. + */ + if ((model->op != NULL) && (model->op->opcode >= 0x10000)) { + goto add_model_op; + } + + switch (model->model_id) { + case BLE_MESH_MODEL_ID_CFG_SRV: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_srv_op; + struct bt_mesh_cfg_srv *srv = (struct bt_mesh_cfg_srv *)model->user_data; + if (srv) { + srv->hb_sub.func = btc_ble_mesh_heartbeat_msg_recv_cb; + } + break; + } + case BLE_MESH_MODEL_ID_CFG_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_cli_op; + bt_mesh_config_client_t *cli = (bt_mesh_config_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_config_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_HEALTH_SRV: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_srv_op; + struct bt_mesh_health_srv *srv = (struct bt_mesh_health_srv *)model->user_data; + if (srv) { + srv->cb.fault_clear = btc_ble_mesh_health_server_fault_clear; + srv->cb.fault_test = btc_ble_mesh_health_server_fault_test; + srv->cb.attn_on = btc_ble_mesh_health_server_attention_on; + srv->cb.attn_off = btc_ble_mesh_health_server_attention_off; + } + break; + } + case BLE_MESH_MODEL_ID_HEALTH_CLI: { + model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_cli_op; + bt_mesh_health_client_t *cli = (bt_mesh_health_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_health_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_onoff_cli_op); + bt_mesh_gen_onoff_client_t *cli = (bt_mesh_gen_onoff_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_LEVEL_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_level_cli_op); + bt_mesh_gen_level_client_t *cli = (bt_mesh_gen_level_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_def_trans_time_cli_op); + bt_mesh_gen_def_trans_time_client_t *cli = (bt_mesh_gen_def_trans_time_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_power_onoff_cli_op); + bt_mesh_gen_power_onoff_client_t *cli = (bt_mesh_gen_power_onoff_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_power_level_cli_op); + bt_mesh_gen_power_level_client_t *cli = (bt_mesh_gen_power_level_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_BATTERY_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_battery_cli_op); + bt_mesh_gen_battery_client_t *cli = (bt_mesh_gen_battery_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_LOCATION_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_location_cli_op); + bt_mesh_gen_location_client_t *cli = (bt_mesh_gen_location_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_PROP_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)gen_property_cli_op); + bt_mesh_gen_property_client_t *cli = (bt_mesh_gen_property_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_generic_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)light_lightness_cli_op); + bt_mesh_light_lightness_client_t *cli = (bt_mesh_light_lightness_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)light_ctl_cli_op); + bt_mesh_light_ctl_client_t *cli = (bt_mesh_light_ctl_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)light_hsl_cli_op); + bt_mesh_light_hsl_client_t *cli = (bt_mesh_light_hsl_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)light_xyl_cli_op); + bt_mesh_light_xyl_client_t *cli = (bt_mesh_light_xyl_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LC_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)light_lc_cli_op); + bt_mesh_light_lc_client_t *cli = (bt_mesh_light_lc_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_lighting_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_SENSOR_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)sensor_cli_op); + bt_mesh_sensor_client_t *cli = (bt_mesh_sensor_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_sensor_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_TIME_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)time_cli_op); + bt_mesh_time_client_t *cli = (bt_mesh_time_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_SCENE_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)scene_cli_op); + bt_mesh_scene_client_t *cli = (bt_mesh_scene_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_SCHEDULER_CLI: { + model->op = ((esp_ble_mesh_model_op_t *)scheduler_cli_op); + bt_mesh_scheduler_client_t *cli = (bt_mesh_scheduler_client_t *)model->user_data; + if (cli != NULL) { + cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_onoff_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_level_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_def_trans_time_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_power_onoff_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_power_onoff_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_power_level_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_power_level_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_battery_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_location_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_user_prop_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_admin_prop_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_manu_prop_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_client_prop_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)gen_location_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_lightness_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_lightness_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_ctl_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_ctl_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_ctl_temp_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_hsl_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_hsl_hue_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_hsl_sat_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_hsl_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_xyl_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_xyl_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_lc_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)light_lc_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_TIME_SRV: + model->op = (esp_ble_mesh_model_op_t *)time_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_TIME_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)time_setup_srv_op; + if (model->pub) { + /* Time Setup Server model does not support subscribing nor publishing. */ + BT_ERR("%s, Time Setup Server shall not support publication", __func__); + return; + } + break; + case BLE_MESH_MODEL_ID_SCENE_SRV: + model->op = (esp_ble_mesh_model_op_t *)scene_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_SCENE_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)scene_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_SCHEDULER_SRV: + model->op = (esp_ble_mesh_model_op_t *)scheduler_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)scheduler_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_SENSOR_SRV: + model->op = (esp_ble_mesh_model_op_t *)sensor_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: + model->op = (esp_ble_mesh_model_op_t *)sensor_setup_srv_op; + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + break; + default: + goto add_model_op; + } + return; + +add_model_op: + if (model->pub) { + model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update; + } + op = model->op; + while (op != NULL && op->opcode != 0) { + op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_server_model_op_cb; + op++; + } + return; +} + +void btc_ble_mesh_prov_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_prov_cb_param_t param = {0}; + btc_ble_mesh_prov_args_t *arg = NULL; + uint8_t act = 0U; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_prov_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MESH_INIT: { + int err_code = 0; + for (int i = 0; i < arg->mesh_init.comp->element_count; i++) { + esp_ble_mesh_elem_t *elem = &arg->mesh_init.comp->elements[i]; + /* For SIG models */ + for (int j = 0; j < elem->sig_model_count; j++) { + esp_ble_mesh_model_t *sig_model = &elem->sig_models[j]; + /* The opcode of sig model should be 1 or 2 bytes. */ + if (sig_model && sig_model->op && (sig_model->op->opcode >= 0x10000)) { + err_code = -EINVAL; + btc_ble_mesh_prov_register_complete_cb(err_code); + return; + } + btc_ble_mesh_model_op_add(sig_model); + } + /* For vendor models */ + for (int k = 0; k < elem->vnd_model_count; k++) { + esp_ble_mesh_model_t *vnd_model = &elem->vnd_models[k]; + /* The opcode of vendor model should be 3 bytes. */ + if (vnd_model && vnd_model->op && vnd_model->op->opcode < 0x10000) { + err_code = -EINVAL; + btc_ble_mesh_prov_register_complete_cb(err_code); + return; + } + btc_ble_mesh_model_op_add(vnd_model); + } + } +#if CONFIG_BLE_MESH_NODE + arg->mesh_init.prov->oob_pub_key_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_oob_pub_key_cb; + arg->mesh_init.prov->output_num_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_number_cb; + arg->mesh_init.prov->output_str_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_string_cb; + arg->mesh_init.prov->input_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_input_cb; + arg->mesh_init.prov->link_open_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_open_cb; + arg->mesh_init.prov->link_close_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_close_cb; + arg->mesh_init.prov->complete_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_complete_cb; + arg->mesh_init.prov->reset_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_reset_cb; +#endif /* CONFIG_BLE_MESH_NODE */ +#if CONFIG_BLE_MESH_PROVISIONER + arg->mesh_init.prov->provisioner_prov_read_oob_pub_key = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb; + arg->mesh_init.prov->provisioner_prov_input = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_input_cb; + arg->mesh_init.prov->provisioner_prov_output = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_output_cb; + arg->mesh_init.prov->provisioner_link_open = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_open_cb; + arg->mesh_init.prov->provisioner_link_close = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_close_cb; + arg->mesh_init.prov->provisioner_prov_comp = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_complete_cb; + bt_mesh_provisioner_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb); +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +#if CONFIG_BLE_MESH_LOW_POWER + bt_mesh_lpn_set_cb(btc_ble_mesh_lpn_cb); +#endif /* CONFIG_BLE_MESH_LOW_POWER */ +#if CONFIG_BLE_MESH_FRIEND + bt_mesh_friend_set_cb(btc_ble_mesh_friend_cb); +#endif /* CONFIG_BLE_MESH_FRIEND */ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + bt_mesh_proxy_client_set_adv_recv_cb(btc_ble_mesh_proxy_client_adv_recv_cb); + bt_mesh_proxy_client_set_conn_cb(btc_ble_mesh_proxy_client_connect_cb); + bt_mesh_proxy_client_set_disconn_cb(btc_ble_mesh_proxy_client_disconnect_cb); + bt_mesh_proxy_client_set_filter_status_cb(btc_ble_mesh_proxy_client_filter_status_recv_cb); +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + err_code = bt_mesh_init((struct bt_mesh_prov *)arg->mesh_init.prov, + (struct bt_mesh_comp *)arg->mesh_init.comp); + /* Give the semaphore when BLE Mesh initialization is finished. */ + xSemaphoreGive(arg->mesh_init.semaphore); + btc_ble_mesh_prov_register_complete_cb(err_code); + return; + } +#if CONFIG_BLE_MESH_NODE + case BTC_BLE_MESH_ACT_PROV_ENABLE: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROV_ENABLE, bearers = %d", __func__, arg->node_prov_enable.bearers); + act = ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT; + param.node_prov_enable_comp.err_code = bt_mesh_prov_enable(arg->node_prov_enable.bearers); + break; + case BTC_BLE_MESH_ACT_PROV_DISABLE: + BT_DBG("%s, BTC_BLE_MESH_ACT_PROV_DISABLE, bearers = %d", __func__, arg->node_prov_disable.bearers); + act = ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT; + param.node_prov_disable_comp.err_code = bt_mesh_prov_disable(arg->node_prov_disable.bearers); + break; + case BTC_BLE_MESH_ACT_NODE_RESET: + BT_DBG("%s, BTC_BLE_MESH_ACT_NODE_RESET", __func__); + bt_mesh_node_reset(); + return; + case BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY: + act = ESP_BLE_MESH_NODE_PROV_SET_OOB_PUB_KEY_COMP_EVT; + param.node_prov_set_oob_pub_key_comp.err_code = + bt_mesh_set_oob_pub_key(arg->set_oob_pub_key.pub_key_x, + arg->set_oob_pub_key.pub_key_y, + arg->set_oob_pub_key.private_key); + break; + case BTC_BLE_MESH_ACT_INPUT_NUMBER: + act = ESP_BLE_MESH_NODE_PROV_INPUT_NUMBER_COMP_EVT; + param.node_prov_input_num_comp.err_code = bt_mesh_input_number(arg->input_number.number); + break; + case BTC_BLE_MESH_ACT_INPUT_STRING: + act = ESP_BLE_MESH_NODE_PROV_INPUT_STRING_COMP_EVT; + param.node_prov_input_str_comp.err_code = bt_mesh_input_string(arg->input_string.string); + break; +#endif /* CONFIG_BLE_MESH_NODE */ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + case BTC_BLE_MESH_ACT_SET_DEVICE_NAME: + act = ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT; + param.node_set_unprov_dev_name_comp.err_code = bt_mesh_set_device_name(arg->set_device_name.name); + break; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + case BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE: + act = ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT; + param.node_proxy_identity_enable_comp.err_code = bt_mesh_proxy_identity_enable(); + break; + case BTC_BLE_MESH_ACT_PROXY_GATT_ENABLE: + act = ESP_BLE_MESH_NODE_PROXY_GATT_ENABLE_COMP_EVT; + param.node_proxy_gatt_enable_comp.err_code = bt_mesh_proxy_gatt_enable(); + break; + case BTC_BLE_MESH_ACT_PROXY_GATT_DISABLE: + act = ESP_BLE_MESH_NODE_PROXY_GATT_DISABLE_COMP_EVT; + param.node_proxy_gatt_disable_comp.err_code = bt_mesh_proxy_gatt_disable(); + break; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +#if CONFIG_BLE_MESH_PROVISIONER + case BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY: + act = ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT; + param.provisioner_prov_read_oob_pub_key_comp.err_code = + bt_mesh_provisioner_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx, + arg->provisioner_read_oob_pub_key.pub_key_x, + arg->provisioner_read_oob_pub_key.pub_key_y); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR: + act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT; + param.provisioner_prov_input_str_comp.err_code = + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_str.link_idx, + (const u8_t *)&arg->provisioner_input_str.string, false); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM: + act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT; + param.provisioner_prov_input_num_comp.err_code = + bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_num.link_idx, + (const u8_t *)&arg->provisioner_input_num.number, true); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_ENABLE: + act = ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT; + param.provisioner_prov_enable_comp.err_code = + bt_mesh_provisioner_enable(arg->provisioner_enable.bearers); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DISABLE: + act = ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT; + param.provisioner_prov_disable_comp.err_code = + bt_mesh_provisioner_disable(arg->provisioner_disable.bearers); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD: { + struct bt_mesh_unprov_dev_add add_dev = {0}; + add_dev.addr_type = arg->provisioner_dev_add.add_dev.addr_type; + add_dev.oob_info = arg->provisioner_dev_add.add_dev.oob_info; + add_dev.bearer = arg->provisioner_dev_add.add_dev.bearer; + memcpy(add_dev.addr, arg->provisioner_dev_add.add_dev.addr, 6); + memcpy(add_dev.uuid, arg->provisioner_dev_add.add_dev.uuid, 16); + act = ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT; + param.provisioner_add_unprov_dev_comp.err_code = + bt_mesh_provisioner_add_unprov_dev(&add_dev, arg->provisioner_dev_add.flags); + break; + } + case BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR: + act = ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT; + param.provisioner_prov_dev_with_addr_comp.err_code = + bt_mesh_provisioner_prov_device_with_addr(arg->provisioner_prov_dev_with_addr.uuid, + arg->provisioner_prov_dev_with_addr.addr, arg->provisioner_prov_dev_with_addr.addr_type, + arg->provisioner_prov_dev_with_addr.bearer, arg->provisioner_prov_dev_with_addr.oob_info, + arg->provisioner_prov_dev_with_addr.unicast_addr); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL: { + struct bt_mesh_device_delete del_dev = {0}; + if (arg->provisioner_dev_del.del_dev.flag & DEL_DEV_ADDR_FLAG) { + del_dev.addr_type = arg->provisioner_dev_del.del_dev.addr_type; + memcpy(del_dev.addr, arg->provisioner_dev_del.del_dev.addr, 6); + } else if (arg->provisioner_dev_del.del_dev.flag & DEL_DEV_UUID_FLAG) { + memcpy(del_dev.uuid, arg->provisioner_dev_del.del_dev.uuid, 16); + } + act = ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT; + param.provisioner_delete_dev_comp.err_code = bt_mesh_provisioner_delete_device(&del_dev); + break; + } + case BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH: + act = ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT; + param.provisioner_set_dev_uuid_match_comp.err_code = + bt_mesh_provisioner_set_dev_uuid_match(arg->set_dev_uuid_match.offset, + arg->set_dev_uuid_match.match_len, + arg->set_dev_uuid_match.match_val, + arg->set_dev_uuid_match.prov_after_match); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO: { + struct bt_mesh_prov_data_info info = {0}; + info.flag = arg->set_prov_data_info.prov_data.flag; + if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_NET_IDX_FLAG) { + info.net_idx = arg->set_prov_data_info.prov_data.net_idx; + } else if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_FLAGS_FLAG) { + info.flags = arg->set_prov_data_info.prov_data.flags; + } else if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_IV_INDEX_FLAG) { + info.iv_index = arg->set_prov_data_info.prov_data.iv_index; + } + act = ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT; + param.provisioner_set_prov_data_info_comp.err_code = + bt_mesh_provisioner_set_prov_data_info(&info); + break; + } + case BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL: + act = ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT; + param.provisioner_set_static_oob_val_comp.err_code = + bt_mesh_provisioner_set_static_oob_value( + arg->set_static_oob_val.value, arg->set_static_oob_val.length); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR: + act = ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT; + param.provisioner_set_primary_elem_addr_comp.err_code = + bt_mesh_provisioner_set_primary_elem_addr(arg->set_primary_elem_addr.addr); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME: + act = ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT; + param.provisioner_set_node_name_comp.node_index = arg->set_node_name.index; + param.provisioner_set_node_name_comp.err_code = + bt_mesh_provisioner_set_node_name(arg->set_node_name.index, arg->set_node_name.name); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY: { + const u8_t *app_key = NULL; + const u8_t zero[16] = {0}; + if (memcmp(arg->add_local_app_key.app_key, zero, 16)) { + app_key = arg->add_local_app_key.app_key; + } + act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT; + param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx; + param.provisioner_add_app_key_comp.err_code = + bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx, + &arg->add_local_app_key.app_idx); + break; + } + case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY: + act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT; + param.provisioner_update_app_key_comp.net_idx = arg->update_local_app_key.net_idx; + param.provisioner_update_app_key_comp.app_idx = arg->update_local_app_key.app_idx; + param.provisioner_update_app_key_comp.err_code = + bt_mesh_provisioner_local_app_key_update(arg->update_local_app_key.app_key, + arg->update_local_app_key.net_idx, arg->update_local_app_key.app_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP: + act = ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT; + param.provisioner_bind_app_key_to_model_comp.element_addr = arg->local_mod_app_bind.elem_addr; + param.provisioner_bind_app_key_to_model_comp.app_idx = arg->local_mod_app_bind.app_idx; + param.provisioner_bind_app_key_to_model_comp.company_id = arg->local_mod_app_bind.cid; + param.provisioner_bind_app_key_to_model_comp.model_id = arg->local_mod_app_bind.model_id; + param.provisioner_bind_app_key_to_model_comp.err_code = + bt_mesh_provisioner_bind_local_model_app_idx(arg->local_mod_app_bind.elem_addr, + arg->local_mod_app_bind.model_id, + arg->local_mod_app_bind.cid, + arg->local_mod_app_bind.app_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY: { + const u8_t *net_key = NULL; + const u8_t zero[16] = {0}; + if (memcmp(arg->add_local_net_key.net_key, zero, 16)) { + net_key = arg->add_local_net_key.net_key; + } + act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT; + param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx; + param.provisioner_add_net_key_comp.err_code = + bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx); + break; + } + case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY: + act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT; + param.provisioner_update_net_key_comp.net_idx = arg->update_local_net_key.net_idx; + param.provisioner_update_net_key_comp.err_code = + bt_mesh_provisioner_local_net_key_update(arg->update_local_net_key.net_key, + arg->update_local_net_key.net_idx); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA: + act = ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT; + param.provisioner_store_node_comp_data_comp.addr = arg->store_node_comp_data.unicast_addr; + param.provisioner_store_node_comp_data_comp.err_code = + bt_mesh_provisioner_store_node_comp_data(arg->store_node_comp_data.unicast_addr, + arg->store_node_comp_data.data, arg->store_node_comp_data.length); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT; + memcpy(param.provisioner_delete_node_with_uuid_comp.uuid, arg->delete_node_with_uuid.uuid, 16); + param.provisioner_delete_node_with_uuid_comp.err_code = + bt_mesh_provisioner_delete_node_with_uuid(arg->delete_node_with_uuid.uuid); + break; + case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR: + act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT; + param.provisioner_delete_node_with_addr_comp.unicast_addr = arg->delete_node_with_addr.unicast_addr; + param.provisioner_delete_node_with_addr_comp.err_code = + bt_mesh_provisioner_delete_node_with_node_addr(arg->delete_node_with_addr.unicast_addr); + break; +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +#if CONFIG_BLE_MESH_FAST_PROV + case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO: + act = ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT; + param.set_fast_prov_info_comp.status_unicast = + bt_mesh_set_fast_prov_unicast_addr_range(arg->set_fast_prov_info.unicast_min, + arg->set_fast_prov_info.unicast_max); + param.set_fast_prov_info_comp.status_net_idx = + bt_mesh_set_fast_prov_net_idx(arg->set_fast_prov_info.net_idx); + bt_mesh_set_fast_prov_flags_iv_index(arg->set_fast_prov_info.flags, + arg->set_fast_prov_info.iv_index); + param.set_fast_prov_info_comp.status_match = + bt_mesh_provisioner_set_dev_uuid_match(arg->set_fast_prov_info.offset, + arg->set_fast_prov_info.match_len, + arg->set_fast_prov_info.match_val, false); + break; + case BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION: + act = ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT; + param.set_fast_prov_action_comp.status_action = + bt_mesh_set_fast_prov_action(arg->set_fast_prov_action.action); + break; +#endif /* CONFIG_BLE_MESH_FAST_PROV */ +#if CONFIG_BLE_MESH_LOW_POWER + case BTC_BLE_MESH_ACT_LPN_ENABLE: + act = ESP_BLE_MESH_LPN_ENABLE_COMP_EVT; + param.lpn_enable_comp.err_code = bt_mesh_lpn_set(true, false); + break; + case BTC_BLE_MESH_ACT_LPN_DISABLE: + act = ESP_BLE_MESH_LPN_DISABLE_COMP_EVT; + param.lpn_disable_comp.err_code = bt_mesh_lpn_set(false, arg->lpn_disable.force); + break; + case BTC_BLE_MESH_ACT_LPN_POLL: + act = ESP_BLE_MESH_LPN_POLL_COMP_EVT; + param.lpn_poll_comp.err_code = bt_mesh_lpn_poll(); + break; +#endif /* CONFIG_BLE_MESH_LOW_POWER */ +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BTC_BLE_MESH_ACT_PROXY_CLIENT_CONNECT: + act = ESP_BLE_MESH_PROXY_CLIENT_CONNECT_COMP_EVT; + memcpy(param.proxy_client_connect_comp.addr, arg->proxy_client_connect.addr, BD_ADDR_LEN); + param.proxy_client_connect_comp.addr_type = arg->proxy_client_connect.addr_type; + param.proxy_client_connect_comp.net_idx = arg->proxy_client_connect.net_idx; + param.proxy_client_connect_comp.err_code = + bt_mesh_proxy_client_connect(arg->proxy_client_connect.addr, + arg->proxy_client_connect.addr_type, + arg->proxy_client_connect.net_idx); + break; + case BTC_BLE_MESH_ACT_PROXY_CLIENT_DISCONNECT: + act = ESP_BLE_MESH_PROXY_CLIENT_DISCONNECT_COMP_EVT; + param.proxy_client_disconnect_comp.conn_handle = arg->proxy_client_disconnect.conn_handle; + param.proxy_client_disconnect_comp.err_code = + bt_mesh_proxy_client_disconnect(arg->proxy_client_disconnect.conn_handle); + break; + case BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE: { + struct bt_mesh_proxy_cfg_pdu pdu = { + .opcode = BLE_MESH_PROXY_CFG_FILTER_SET, + .set.filter_type = arg->proxy_client_set_filter_type.filter_type, + }; + act = ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT; + param.proxy_client_set_filter_type_comp.conn_handle = arg->proxy_client_set_filter_type.conn_handle; + param.proxy_client_set_filter_type_comp.net_idx = arg->proxy_client_set_filter_type.net_idx; + param.proxy_client_set_filter_type_comp.err_code = + bt_mesh_proxy_client_send_cfg(arg->proxy_client_set_filter_type.conn_handle, + arg->proxy_client_set_filter_type.net_idx, &pdu); + break; + } + case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: { + struct bt_mesh_proxy_cfg_pdu pdu = { + .opcode = BLE_MESH_PROXY_CFG_FILTER_ADD, + .add.addr = arg->proxy_client_add_filter_addr.addr, + .add.addr_num = arg->proxy_client_add_filter_addr.addr_num, + }; + act = ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT; + param.proxy_client_add_filter_addr_comp.conn_handle = arg->proxy_client_add_filter_addr.conn_handle; + param.proxy_client_add_filter_addr_comp.net_idx = arg->proxy_client_add_filter_addr.net_idx; + param.proxy_client_add_filter_addr_comp.err_code = + bt_mesh_proxy_client_send_cfg(arg->proxy_client_add_filter_addr.conn_handle, + arg->proxy_client_add_filter_addr.net_idx, &pdu); + break; + } + case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: { + struct bt_mesh_proxy_cfg_pdu pdu = { + .opcode = BLE_MESH_PROXY_CFG_FILTER_REMOVE, + .remove.addr = arg->proxy_client_remove_filter_addr.addr, + .remove.addr_num = arg->proxy_client_remove_filter_addr.addr_num, + }; + act = ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT; + param.proxy_client_remove_filter_addr_comp.conn_handle = arg->proxy_client_remove_filter_addr.conn_handle; + param.proxy_client_remove_filter_addr_comp.net_idx = arg->proxy_client_remove_filter_addr.net_idx; + param.proxy_client_remove_filter_addr_comp.err_code = + bt_mesh_proxy_client_send_cfg(arg->proxy_client_remove_filter_addr.conn_handle, + arg->proxy_client_remove_filter_addr.net_idx, &pdu); + break; + } +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + case BTC_BLE_MESH_ACT_START_BLE_ADVERTISING: { + struct bt_mesh_ble_adv_param *set = (struct bt_mesh_ble_adv_param *)&arg->start_ble_advertising.param; + struct bt_mesh_ble_adv_data *data = NULL; + if (arg->start_ble_advertising.data.adv_data_len || + arg->start_ble_advertising.data.scan_rsp_data_len) { + data = (struct bt_mesh_ble_adv_data *)&arg->start_ble_advertising.data; + } + act = ESP_BLE_MESH_START_BLE_ADVERTISING_COMP_EVT; + param.start_ble_advertising_comp.err_code = + bt_mesh_start_ble_advertising(set, data, ¶m.start_ble_advertising_comp.index); + break; + } + case BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING: + act = ESP_BLE_MESH_STOP_BLE_ADVERTISING_COMP_EVT; + param.stop_ble_advertising_comp.index = arg->stop_ble_advertising.index; + param.stop_ble_advertising_comp.err_code = + bt_mesh_stop_ble_advertising(arg->stop_ble_advertising.index); + break; +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + case BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR: + act = ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT; + param.model_sub_group_addr_comp.element_addr = arg->model_sub_group_addr.element_addr; + param.model_sub_group_addr_comp.company_id = arg->model_sub_group_addr.company_id; + param.model_sub_group_addr_comp.model_id = arg->model_sub_group_addr.model_id; + param.model_sub_group_addr_comp.group_addr = arg->model_sub_group_addr.group_addr; + param.model_sub_group_addr_comp.err_code = + bt_mesh_model_subscribe_group_addr(arg->model_sub_group_addr.element_addr, + arg->model_sub_group_addr.company_id, + arg->model_sub_group_addr.model_id, + arg->model_sub_group_addr.group_addr); + break; + case BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR: + act = ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT; + param.model_unsub_group_addr_comp.element_addr = arg->model_unsub_group_addr.element_addr; + param.model_unsub_group_addr_comp.company_id = arg->model_unsub_group_addr.company_id; + param.model_unsub_group_addr_comp.model_id = arg->model_unsub_group_addr.model_id; + param.model_unsub_group_addr_comp.group_addr = arg->model_unsub_group_addr.group_addr; + param.model_unsub_group_addr_comp.err_code = + bt_mesh_model_unsubscribe_group_addr(arg->model_unsub_group_addr.element_addr, + arg->model_unsub_group_addr.company_id, + arg->model_unsub_group_addr.model_id, + arg->model_unsub_group_addr.group_addr); + break; + case BTC_BLE_MESH_ACT_DEINIT_MESH: + act = ESP_BLE_MESH_DEINIT_MESH_COMP_EVT; + param.deinit_mesh_comp.err_code = bt_mesh_deinit((struct bt_mesh_deinit_param *)&arg->mesh_deinit.param); + break; + default: + BT_WARN("%s, Invalid msg->act %d", __func__, msg->act); + return; + } + + /* Callback operation completion events */ + btc_ble_mesh_prov_set_complete_cb(¶m, act); + + if (msg->arg) { + btc_ble_mesh_prov_arg_deep_free(msg); + } + return; +} + +void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_prov_cb_param_t *param = NULL; + + if (!msg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_prov_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_PROV_EVT_MAX) { + btc_ble_mesh_prov_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } +} + +void btc_ble_mesh_model_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_model_args_t *arg = NULL; + int err = 0; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_model_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_MODEL_PUBLISH: { + if (arg->model_publish.device_role == PROVISIONER) { + bt_mesh_role_param_t common = {0}; + common.model = (struct bt_mesh_model *)(arg->model_publish.model); + common.role = arg->model_publish.device_role; + if (bt_mesh_set_client_model_role(&common)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + } + err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model); + btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err); + break; + } + case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: { + /* arg->model_send.length contains opcode & payload, plus extra 4-bytes TransMIC */ + struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + break; + } + net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); + arg->model_send.ctx->srv_send = true; + err = bt_mesh_model_send((struct bt_mesh_model *)arg->model_send.model, + (struct bt_mesh_msg_ctx *)arg->model_send.ctx, + buf, NULL, NULL); + bt_mesh_free_buf(buf); + btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx, + arg->model_send.opcode, err); + break; + } + case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: { + bt_mesh_role_param_t common = {0}; + /* arg->model_send.length contains opcode & message, plus extra 4-bytes TransMIC */ + struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + break; + } + net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length); + arg->model_send.ctx->srv_send = false; + common.model = (struct bt_mesh_model *)(arg->model_send.model); + common.role = arg->model_send.device_role; + if (bt_mesh_set_client_model_role(&common)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + err = bt_mesh_client_send_msg((struct bt_mesh_model *)arg->model_send.model, + arg->model_send.opcode, + (struct bt_mesh_msg_ctx *)arg->model_send.ctx, buf, + btc_ble_mesh_client_model_timeout_cb, arg->model_send.msg_timeout, + arg->model_send.need_rsp, NULL, NULL); + bt_mesh_free_buf(buf); + btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx, + arg->model_send.opcode, err); + break; + } + case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE: + err = bt_mesh_update_binding_state( + (struct bt_mesh_model *)arg->model_update_state.model, arg->model_update_state.type, + (bt_mesh_server_state_value_t *)arg->model_update_state.value); + btc_ble_mesh_server_model_update_state_comp_cb(arg->model_update_state.model, + arg->model_update_state.type, err); + break; + default: + BT_WARN("%s, Unknown msg->act %d", __func__, msg->act); + break; + } + + btc_ble_mesh_model_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_model_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_model_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_model_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_MODEL_EVT_MAX) { + btc_ble_mesh_model_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_model_free_req_data(msg); + return; +} diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c new file mode 100644 index 000000000..2021f81a0 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_sensor_model.c @@ -0,0 +1,906 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_sensor_model.h" +#include "sensor_client.h" +#include "esp_ble_mesh_sensor_model_api.h" + +/* Sensor Client Models related functions */ + +static inline void btc_ble_mesh_sensor_client_cb_to_app(esp_ble_mesh_sensor_client_cb_event_t event, + esp_ble_mesh_sensor_client_cb_param_t *param) +{ + esp_ble_mesh_sensor_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_sensor_client_cb_t)btc_profile_cb_get(BTC_PID_SENSOR_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_sensor_client_args_t *dst = (btc_ble_mesh_sensor_client_args_t *)p_dest; + btc_ble_mesh_sensor_client_args_t *src = (btc_ble_mesh_sensor_client_args_t *)p_src; + u16_t length = 0U; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: { + dst->sensor_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->sensor_client_get_state.get_state = (esp_ble_mesh_sensor_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_sensor_client_get_state_t)); + if (dst->sensor_client_get_state.params && dst->sensor_client_get_state.get_state) { + memcpy(dst->sensor_client_get_state.params, src->sensor_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->sensor_client_get_state.get_state, src->sensor_client_get_state.get_state, + sizeof(esp_ble_mesh_sensor_client_get_state_t)); + + switch (src->sensor_client_get_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + if (src->sensor_client_get_state.get_state->column_get.raw_value_x) { + length = src->sensor_client_get_state.get_state->column_get.raw_value_x->len; + dst->sensor_client_get_state.get_state->column_get.raw_value_x = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_get_state.get_state->column_get.raw_value_x) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->column_get.raw_value_x, + src->sensor_client_get_state.get_state->column_get.raw_value_x->data, + src->sensor_client_get_state.get_state->column_get.raw_value_x->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + if (src->sensor_client_get_state.get_state->series_get.raw_value_x1) { + length = src->sensor_client_get_state.get_state->series_get.raw_value_x1->len; + dst->sensor_client_get_state.get_state->series_get.raw_value_x1 = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_get_state.get_state->series_get.raw_value_x1) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->series_get.raw_value_x1, + src->sensor_client_get_state.get_state->series_get.raw_value_x1->data, + src->sensor_client_get_state.get_state->series_get.raw_value_x1->len); + } + if (src->sensor_client_get_state.get_state->series_get.raw_value_x2) { + length = src->sensor_client_get_state.get_state->series_get.raw_value_x2->len; + dst->sensor_client_get_state.get_state->series_get.raw_value_x2 = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_get_state.get_state->series_get.raw_value_x2) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_get_state.get_state->series_get.raw_value_x2, + src->sensor_client_get_state.get_state->series_get.raw_value_x2->data, + src->sensor_client_get_state.get_state->series_get.raw_value_x2->len); + } + break; + default: + break; + } + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: { + dst->sensor_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->sensor_client_set_state.set_state = (esp_ble_mesh_sensor_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_sensor_client_set_state_t)); + if (dst->sensor_client_set_state.params && dst->sensor_client_set_state.set_state) { + memcpy(dst->sensor_client_set_state.params, src->sensor_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->sensor_client_set_state.set_state, src->sensor_client_set_state.set_state, + sizeof(esp_ble_mesh_sensor_client_set_state_t)); + + switch (src->sensor_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + if (src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down) { + length = src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down->len; + dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down, + src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down->data, + src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down->len); + } + if (src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up) { + length = src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up->len; + dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up, + src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up->data, + src->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up->len); + } + if (src->sensor_client_set_state.set_state->cadence_set.fast_cadence_low) { + length = src->sensor_client_set_state.set_state->cadence_set.fast_cadence_low->len; + dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_low, + src->sensor_client_set_state.set_state->cadence_set.fast_cadence_low->data, + src->sensor_client_set_state.set_state->cadence_set.fast_cadence_low->len); + } + if (src->sensor_client_set_state.set_state->cadence_set.fast_cadence_high) { + length = src->sensor_client_set_state.set_state->cadence_set.fast_cadence_high->len; + dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->cadence_set.fast_cadence_high, + src->sensor_client_set_state.set_state->cadence_set.fast_cadence_high->data, + src->sensor_client_set_state.set_state->cadence_set.fast_cadence_high->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + if (src->sensor_client_set_state.set_state->setting_set.sensor_setting_raw) { + length = src->sensor_client_set_state.set_state->setting_set.sensor_setting_raw->len; + dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw = bt_mesh_alloc_buf(length); + if (!dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(dst->sensor_client_set_state.set_state->setting_set.sensor_setting_raw, + src->sensor_client_set_state.set_state->setting_set.sensor_setting_raw->data, + src->sensor_client_set_state.set_state->setting_set.sensor_setting_raw->len); + } + break; + default: + break; + } + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_sensor_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_sensor_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_sensor_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: + if (arg->sensor_client_get_state.get_state) { + if (arg->sensor_client_get_state.params) { + switch (arg->sensor_client_get_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->column_get.raw_value_x); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x1); + bt_mesh_free_buf(arg->sensor_client_get_state.get_state->series_get.raw_value_x2); + break; + default: + break; + } + } + bt_mesh_free(arg->sensor_client_get_state.get_state); + } + if (arg->sensor_client_get_state.params) { + bt_mesh_free(arg->sensor_client_get_state.params); + } + break; + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: + if (arg->sensor_client_set_state.set_state) { + if (arg->sensor_client_set_state.params) { + switch (arg->sensor_client_set_state.params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_down); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.status_trigger_delta_up); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_low); + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->cadence_set.fast_cadence_high); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + bt_mesh_free_buf(arg->sensor_client_set_state.set_state->setting_set.sensor_setting_raw); + break; + default: + break; + } + } + bt_mesh_free(arg->sensor_client_set_state.set_state); + } + if (arg->sensor_client_set_state.params) { + bt_mesh_free(arg->sensor_client_set_state.params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_sensor_client_cb_param_t *p_dest_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_dest; + esp_ble_mesh_sensor_client_cb_param_t *p_src_data = (esp_ble_mesh_sensor_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: + if (p_src_data->status_cb.descriptor_status.descriptor) { + length = p_src_data->status_cb.descriptor_status.descriptor->len; + p_dest_data->status_cb.descriptor_status.descriptor = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.descriptor_status.descriptor) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.descriptor_status.descriptor, + p_src_data->status_cb.descriptor_status.descriptor->data, + p_src_data->status_cb.descriptor_status.descriptor->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: + if (p_src_data->status_cb.cadence_status.sensor_cadence_value) { + length = p_src_data->status_cb.cadence_status.sensor_cadence_value->len; + p_dest_data->status_cb.cadence_status.sensor_cadence_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.cadence_status.sensor_cadence_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.cadence_status.sensor_cadence_value, + p_src_data->status_cb.cadence_status.sensor_cadence_value->data, + p_src_data->status_cb.cadence_status.sensor_cadence_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: + if (p_src_data->status_cb.settings_status.sensor_setting_property_ids) { + length = p_src_data->status_cb.settings_status.sensor_setting_property_ids->len; + p_dest_data->status_cb.settings_status.sensor_setting_property_ids = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.settings_status.sensor_setting_property_ids) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.settings_status.sensor_setting_property_ids, + p_src_data->status_cb.settings_status.sensor_setting_property_ids->data, + p_src_data->status_cb.settings_status.sensor_setting_property_ids->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: + if (p_src_data->status_cb.setting_status.sensor_setting_raw) { + length = p_src_data->status_cb.setting_status.sensor_setting_raw->len; + p_dest_data->status_cb.setting_status.sensor_setting_raw = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.setting_status.sensor_setting_raw) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.setting_status.sensor_setting_raw, + p_src_data->status_cb.setting_status.sensor_setting_raw->data, + p_src_data->status_cb.setting_status.sensor_setting_raw->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS: + if (p_src_data->status_cb.sensor_status.marshalled_sensor_data) { + length = p_src_data->status_cb.sensor_status.marshalled_sensor_data->len; + p_dest_data->status_cb.sensor_status.marshalled_sensor_data = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.sensor_status.marshalled_sensor_data) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.sensor_status.marshalled_sensor_data, + p_src_data->status_cb.sensor_status.marshalled_sensor_data->data, + p_src_data->status_cb.sensor_status.marshalled_sensor_data->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: + if (p_src_data->status_cb.column_status.sensor_column_value) { + length = p_src_data->status_cb.column_status.sensor_column_value->len; + p_dest_data->status_cb.column_status.sensor_column_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.column_status.sensor_column_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.column_status.sensor_column_value, + p_src_data->status_cb.column_status.sensor_column_value->data, + p_src_data->status_cb.column_status.sensor_column_value->len); + } + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: + if (p_src_data->status_cb.series_status.sensor_series_value) { + length = p_src_data->status_cb.series_status.sensor_series_value->len; + p_dest_data->status_cb.series_status.sensor_series_value = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.series_status.sensor_series_value) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.series_status.sensor_series_value, + p_src_data->status_cb.series_status.sensor_series_value->data, + p_src_data->status_cb.series_status.sensor_series_value->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_sensor_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_sensor_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: + bt_mesh_free_buf(arg->status_cb.descriptor_status.descriptor); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: + bt_mesh_free_buf(arg->status_cb.cadence_status.sensor_cadence_value); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: + bt_mesh_free_buf(arg->status_cb.settings_status.sensor_setting_property_ids); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: + bt_mesh_free_buf(arg->status_cb.setting_status.sensor_setting_raw); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS: + bt_mesh_free_buf(arg->status_cb.sensor_status.marshalled_sensor_data); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: + bt_mesh_free_buf(arg->status_cb.column_status.sensor_column_value); + break; + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + case ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: + bt_mesh_free_buf(arg->status_cb.series_status.sensor_series_value); + break; + default: + break; + } + } + case ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_client_callback(esp_ble_mesh_sensor_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SENSOR_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SENSOR_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_sensor_client_cb_param_t), btc_ble_mesh_sensor_client_copy_req_data); +} + +void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_sensor_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE: + act = ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE: + act = ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH: + act = ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown sensor client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_sensor_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_sensor_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_sensor_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg) +{ + esp_ble_mesh_client_common_param_t *params = NULL; + btc_ble_mesh_sensor_client_args_t *arg = NULL; + esp_ble_mesh_sensor_client_cb_param_t cb = {0}; + bt_mesh_client_common_param_t common = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_sensor_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE: { + params = arg->sensor_client_get_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->sensor_client_get_state.params; + cb.error_code = bt_mesh_sensor_client_get_state(&common, + (void *)arg->sensor_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE: { + params = arg->sensor_client_set_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->sensor_client_set_state.params; + cb.error_code = bt_mesh_sensor_client_set_state(&common, + (void *)arg->sensor_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_sensor_client_callback(&cb, ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_sensor_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_sensor_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_sensor_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_SENSOR_CLIENT_EVT_MAX) { + btc_ble_mesh_sensor_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_sensor_client_free_req_data(msg); + return; +} + +/* Sensor Server Models related functions */ + +static inline void btc_ble_mesh_sensor_server_cb_to_app( + esp_ble_mesh_sensor_server_cb_event_t event, + esp_ble_mesh_sensor_server_cb_param_t *param) +{ + esp_ble_mesh_sensor_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_sensor_server_cb_t)btc_profile_cb_get(BTC_PID_SENSOR_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_sensor_server_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_sensor_server_cb_param_t *p_dest_data = (esp_ble_mesh_sensor_server_cb_param_t *)p_dest; + esp_ble_mesh_sensor_server_cb_param_t *p_src_data = (esp_ble_mesh_sensor_server_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + if (p_src_data->value.state_change.sensor_cadence_set.trigger_delta_down) { + length = p_src_data->value.state_change.sensor_cadence_set.trigger_delta_down->len; + p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_down, + p_src_data->value.state_change.sensor_cadence_set.trigger_delta_down->data, + p_src_data->value.state_change.sensor_cadence_set.trigger_delta_down->len); + } + if (p_src_data->value.state_change.sensor_cadence_set.trigger_delta_up) { + length = p_src_data->value.state_change.sensor_cadence_set.trigger_delta_up->len; + p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.trigger_delta_up, + p_src_data->value.state_change.sensor_cadence_set.trigger_delta_up->data, + p_src_data->value.state_change.sensor_cadence_set.trigger_delta_up->len); + } + if (p_src_data->value.state_change.sensor_cadence_set.fast_cadence_low) { + length = p_src_data->value.state_change.sensor_cadence_set.fast_cadence_low->len; + p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_low, + p_src_data->value.state_change.sensor_cadence_set.fast_cadence_low->data, + p_src_data->value.state_change.sensor_cadence_set.fast_cadence_low->len); + } + if (p_src_data->value.state_change.sensor_cadence_set.fast_cadence_high) { + length = p_src_data->value.state_change.sensor_cadence_set.fast_cadence_high->len; + p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_cadence_set.fast_cadence_high, + p_src_data->value.state_change.sensor_cadence_set.fast_cadence_high->data, + p_src_data->value.state_change.sensor_cadence_set.fast_cadence_high->len); + } + } else if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK) { + if (p_src_data->value.state_change.sensor_setting_set.setting_value) { + length = p_src_data->value.state_change.sensor_setting_set.setting_value->len; + p_dest_data->value.state_change.sensor_setting_set.setting_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.state_change.sensor_setting_set.setting_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.state_change.sensor_setting_set.setting_value, + p_src_data->value.state_change.sensor_setting_set.setting_value->data, + p_src_data->value.state_change.sensor_setting_set.setting_value->len); + } + } + break; + case ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET) { + if (p_src_data->value.get.sensor_column.raw_value_x) { + length = p_src_data->value.get.sensor_column.raw_value_x->len; + p_dest_data->value.get.sensor_column.raw_value_x = bt_mesh_alloc_buf(length); + if (p_dest_data->value.get.sensor_column.raw_value_x == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.get.sensor_column.raw_value_x, + p_src_data->value.get.sensor_column.raw_value_x->data, + p_src_data->value.get.sensor_column.raw_value_x->len); + } + } else if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET) { + if (p_src_data->value.get.sensor_series.raw_value) { + length = p_src_data->value.get.sensor_series.raw_value->len; + p_dest_data->value.get.sensor_series.raw_value = bt_mesh_alloc_buf(length); + if (p_dest_data->value.get.sensor_series.raw_value == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.get.sensor_series.raw_value, + p_src_data->value.get.sensor_series.raw_value->data, + p_src_data->value.get.sensor_series.raw_value->len); + } + } + break; + case ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT: + if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + if (p_src_data->value.set.sensor_cadence.cadence) { + length = p_src_data->value.set.sensor_cadence.cadence->len; + p_dest_data->value.set.sensor_cadence.cadence = bt_mesh_alloc_buf(length); + if (p_dest_data->value.set.sensor_cadence.cadence == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.set.sensor_cadence.cadence, + p_src_data->value.set.sensor_cadence.cadence->data, + p_src_data->value.set.sensor_cadence.cadence->len); + } + } else if (p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || + p_src_data->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK) { + if (p_src_data->value.set.sensor_setting.setting_raw) { + length = p_src_data->value.set.sensor_setting.setting_raw->len; + p_dest_data->value.set.sensor_setting.setting_raw = bt_mesh_alloc_buf(length); + if (p_dest_data->value.set.sensor_setting.setting_raw == NULL) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->value.set.sensor_setting.setting_raw, + p_src_data->value.set.sensor_setting.setting_raw->data, + p_src_data->value.set.sensor_setting.setting_raw->len); + } + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_server_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_sensor_server_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_sensor_server_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.trigger_delta_down); + bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.trigger_delta_up); + bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.fast_cadence_low); + bt_mesh_free_buf(arg->value.state_change.sensor_cadence_set.fast_cadence_high); + } else if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK) { + bt_mesh_free_buf(arg->value.state_change.sensor_setting_set.setting_value); + } + break; + case ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET) { + bt_mesh_free_buf(arg->value.get.sensor_column.raw_value_x); + } else if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET) { + bt_mesh_free_buf(arg->value.get.sensor_series.raw_value); + } + break; + case ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT: + if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK) { + bt_mesh_free_buf(arg->value.set.sensor_cadence.cadence); + } else if (arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || + arg->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK) { + bt_mesh_free_buf(arg->value.set.sensor_setting.setting_raw); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_sensor_server_callback(esp_ble_mesh_sensor_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_SENSOR_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_SENSOR_SERVER; + msg.act = act; + + btc_transfer_context( + &msg, cb_params, sizeof(esp_ble_mesh_sensor_server_cb_param_t), btc_ble_mesh_sensor_server_copy_req_data); +} + +void bt_mesh_sensor_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_sensor_server_cb_param_t cb_params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (model == NULL || ctx == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT; + break; + case BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG: + act = ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG: + act = ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT; + break; + default: + BT_ERR("%s, Unknown Sensor Server event type", __func__); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + + if (val && len) { + length = (len <= sizeof(cb_params.value)) ? len : sizeof(cb_params.value); + memcpy(&cb_params.value, val, length); + } + + btc_ble_mesh_sensor_server_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_sensor_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_sensor_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_SENSOR_SERVER_EVT_MAX) { + btc_ble_mesh_sensor_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_sensor_server_free_req_data(msg); + return; +} diff --git a/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c new file mode 100644 index 000000000..6dd93b31a --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/btc_ble_mesh_time_scene_model.c @@ -0,0 +1,493 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_time_scene_model.h" +#include "time_scene_client.h" +#include "esp_ble_mesh_time_scene_model_api.h" + +/* Time and Scenes Client Models related functions */ + +static inline void btc_ble_mesh_time_scene_client_cb_to_app(esp_ble_mesh_time_scene_client_cb_event_t event, + esp_ble_mesh_time_scene_client_cb_param_t *param) +{ + esp_ble_mesh_time_scene_client_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_time_scene_client_cb_t)btc_profile_cb_get(BTC_PID_TIME_SCENE_CLIENT); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) +{ + btc_ble_mesh_time_scene_client_args_t *dst = (btc_ble_mesh_time_scene_client_args_t *)p_dest; + btc_ble_mesh_time_scene_client_args_t *src = (btc_ble_mesh_time_scene_client_args_t *)p_src; + + if (!msg || !dst || !src) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (msg->act) { + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: { + dst->time_scene_client_get_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (dst->time_scene_client_get_state.params) { + memcpy(dst->time_scene_client_get_state.params, src->time_scene_client_get_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + break; + } + if (src->time_scene_client_get_state.get_state) { + dst->time_scene_client_get_state.get_state = (esp_ble_mesh_time_scene_client_get_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_time_scene_client_get_state_t)); + if (dst->time_scene_client_get_state.get_state) { + memcpy(dst->time_scene_client_get_state.get_state, src->time_scene_client_get_state.get_state, + sizeof(esp_ble_mesh_time_scene_client_get_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + } + break; + } + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: { + dst->time_scene_client_set_state.params = (esp_ble_mesh_client_common_param_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + dst->time_scene_client_set_state.set_state = (esp_ble_mesh_time_scene_client_set_state_t *)bt_mesh_malloc(sizeof(esp_ble_mesh_time_scene_client_set_state_t)); + if (dst->time_scene_client_set_state.params && dst->time_scene_client_set_state.set_state) { + memcpy(dst->time_scene_client_set_state.params, src->time_scene_client_set_state.params, + sizeof(esp_ble_mesh_client_common_param_t)); + memcpy(dst->time_scene_client_set_state.set_state, src->time_scene_client_set_state.set_state, + sizeof(esp_ble_mesh_time_scene_client_set_state_t)); + } else { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + } + break; + } + default: + BT_DBG("%s, Unknown deep copy act %d", __func__, msg->act); + break; + } +} + +void btc_ble_mesh_time_scene_client_arg_deep_free(btc_msg_t *msg) +{ + btc_ble_mesh_time_scene_client_args_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_time_scene_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: + if (arg->time_scene_client_get_state.params) { + bt_mesh_free(arg->time_scene_client_get_state.params); + } + if (arg->time_scene_client_get_state.get_state) { + bt_mesh_free(arg->time_scene_client_get_state.get_state); + } + break; + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: + if (arg->time_scene_client_set_state.params) { + bt_mesh_free(arg->time_scene_client_set_state.params); + } + if (arg->time_scene_client_set_state.set_state) { + bt_mesh_free(arg->time_scene_client_set_state.set_state); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_time_scene_client_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src) +{ + esp_ble_mesh_time_scene_client_cb_param_t *p_dest_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_dest; + esp_ble_mesh_time_scene_client_cb_param_t *p_src_data = (esp_ble_mesh_time_scene_client_cb_param_t *)p_src; + u16_t length = 0U; + + if (!msg || !p_src_data || !p_dest_data) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (p_src_data->params) { + p_dest_data->params = bt_mesh_malloc(sizeof(esp_ble_mesh_client_common_param_t)); + if (!p_dest_data->params) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + + memcpy(p_dest_data->params, p_src_data->params, sizeof(esp_ble_mesh_client_common_param_t)); + } + + switch (msg->act) { + case ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT: + if (p_src_data->params) { + switch (p_src_data->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SCENE_STORE: + case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: + case ESP_BLE_MESH_MODEL_OP_SCENE_DELETE: + case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: + if (p_src_data->status_cb.scene_register_status.scenes) { + length = p_src_data->status_cb.scene_register_status.scenes->len; + p_dest_data->status_cb.scene_register_status.scenes = bt_mesh_alloc_buf(length); + if (!p_dest_data->status_cb.scene_register_status.scenes) { + BT_ERR("%s, Failed to allocate memory, act %d", __func__, msg->act); + return; + } + net_buf_simple_add_mem(p_dest_data->status_cb.scene_register_status.scenes, + p_src_data->status_cb.scene_register_status.scenes->data, + p_src_data->status_cb.scene_register_status.scenes->len); + } + break; + default: + break; + } + } + case ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT: + break; + default: + break; + } +} + +static void btc_ble_mesh_time_scene_client_free_req_data(btc_msg_t *msg) +{ + esp_ble_mesh_time_scene_client_cb_param_t *arg = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (esp_ble_mesh_time_scene_client_cb_param_t *)(msg->arg); + + switch (msg->act) { + case ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT: + case ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT: + case ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT: + if (arg->params) { + switch (arg->params->opcode) { + case ESP_BLE_MESH_MODEL_OP_SCENE_STORE: + case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: + case ESP_BLE_MESH_MODEL_OP_SCENE_DELETE: + case ESP_BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: + bt_mesh_free_buf(arg->status_cb.scene_register_status.scenes); + break; + default: + break; + } + } + case ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT: + if (arg->params) { + bt_mesh_free(arg->params); + } + break; + default: + break; + } +} + +static void btc_ble_mesh_time_scene_client_callback(esp_ble_mesh_time_scene_client_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_TIME_SCENE_CLIENT)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_TIME_SCENE_CLIENT; + msg.act = act; + + btc_transfer_context(&msg, cb_params, + sizeof(esp_ble_mesh_time_scene_client_cb_param_t), btc_ble_mesh_time_scene_client_copy_req_data); +} + +void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_time_scene_client_cb_param_t cb_params = {0}; + esp_ble_mesh_client_common_param_t params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE: + act = ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE: + act = ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH: + act = ESP_BLE_MESH_TIME_SCENE_CLIENT_PUBLISH_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT: + act = ESP_BLE_MESH_TIME_SCENE_CLIENT_TIMEOUT_EVT; + break; + default: + BT_ERR("%s, Unknown time scene client event type %d", __func__, evt_type); + return; + } + + params.opcode = opcode; + params.model = (esp_ble_mesh_model_t *)model; + params.ctx.net_idx = ctx->net_idx; + params.ctx.app_idx = ctx->app_idx; + params.ctx.addr = ctx->addr; + params.ctx.recv_ttl = ctx->recv_ttl; + params.ctx.recv_op = ctx->recv_op; + params.ctx.recv_dst = ctx->recv_dst; + + cb_params.error_code = 0; + cb_params.params = ¶ms; + + if (val && len) { + length = (len <= sizeof(cb_params.status_cb)) ? len : sizeof(cb_params.status_cb); + memcpy(&cb_params.status_cb, val, length); + } + + btc_ble_mesh_time_scene_client_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_time_scene_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_time_scene_client_cb_evt_to_btc(opcode, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH, model, ctx, buf->data, buf->len); + return; +} + +void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg) +{ + btc_ble_mesh_time_scene_client_args_t *arg = NULL; + esp_ble_mesh_client_common_param_t *params = NULL; + esp_ble_mesh_time_scene_client_cb_param_t cb = {0}; + bt_mesh_client_common_param_t common = {0}; + bt_mesh_role_param_t role_param = {0}; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + arg = (btc_ble_mesh_time_scene_client_args_t *)(msg->arg); + + switch (msg->act) { + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE: { + params = arg->time_scene_client_get_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->time_scene_client_get_state.params; + cb.error_code = bt_mesh_time_scene_client_get_state(&common, + (void *)arg->time_scene_client_get_state.get_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_GET_STATE_EVT); + } + break; + } + case BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE: { + params = arg->time_scene_client_set_state.params; + role_param.model = (struct bt_mesh_model *)params->model; + role_param.role = params->msg_role; + if (bt_mesh_set_client_model_role(&role_param)) { + BT_ERR("%s, Failed to set model role", __func__); + break; + } + common.opcode = params->opcode; + common.model = (struct bt_mesh_model *)params->model; + common.ctx.net_idx = params->ctx.net_idx; + common.ctx.app_idx = params->ctx.app_idx; + common.ctx.addr = params->ctx.addr; + common.ctx.send_rel = params->ctx.send_rel; + common.ctx.send_ttl = params->ctx.send_ttl; + common.msg_timeout = params->msg_timeout; + + cb.params = arg->time_scene_client_set_state.params; + cb.error_code = bt_mesh_time_scene_client_set_state(&common, + (void *)arg->time_scene_client_set_state.set_state, (void *)&cb.status_cb); + if (cb.error_code) { + /* If send failed, callback error_code to app layer immediately */ + btc_ble_mesh_time_scene_client_callback(&cb, ESP_BLE_MESH_TIME_SCENE_CLIENT_SET_STATE_EVT); + } + break; + } + default: + break; + } + + btc_ble_mesh_time_scene_client_arg_deep_free(msg); + return; +} + +void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_time_scene_client_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_time_scene_client_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_TIME_SCENE_CLIENT_EVT_MAX) { + btc_ble_mesh_time_scene_client_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + btc_ble_mesh_time_scene_client_free_req_data(msg); + return; +} + +/* Time and Scenes Server Models related functions */ + +static inline void btc_ble_mesh_time_scene_server_cb_to_app( + esp_ble_mesh_time_scene_server_cb_event_t event, + esp_ble_mesh_time_scene_server_cb_param_t *param) +{ + esp_ble_mesh_time_scene_server_cb_t btc_ble_mesh_cb = + (esp_ble_mesh_time_scene_server_cb_t)btc_profile_cb_get(BTC_PID_TIME_SCENE_SERVER); + if (btc_ble_mesh_cb) { + btc_ble_mesh_cb(event, param); + } +} + +static void btc_ble_mesh_time_scene_server_callback(esp_ble_mesh_time_scene_server_cb_param_t *cb_params, uint8_t act) +{ + btc_msg_t msg = {0}; + + BT_DBG("%s", __func__); + + /* If corresponding callback is not registered, event will not be posted. */ + if (!btc_profile_cb_get(BTC_PID_TIME_SCENE_SERVER)) { + return; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_TIME_SCENE_SERVER; + msg.act = act; + + btc_transfer_context( + &msg, cb_params, sizeof(esp_ble_mesh_time_scene_server_cb_param_t), NULL); +} + +void bt_mesh_time_scene_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len) +{ + esp_ble_mesh_time_scene_server_cb_param_t cb_params = {0}; + size_t length = 0U; + uint8_t act = 0U; + + if (model == NULL || ctx == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (evt_type) { + case BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE: + act = ESP_BLE_MESH_TIME_SCENE_SERVER_STATE_CHANGE_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG: + act = ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_GET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG: + act = ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_SET_MSG_EVT; + break; + case BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_STATUS_MSG: + act = ESP_BLE_MESH_TIME_SCENE_SERVER_RECV_STATUS_MSG_EVT; + break; + default: + BT_ERR("%s, Unknown Time Scene Server event type", __func__); + return; + } + + cb_params.model = (esp_ble_mesh_model_t *)model; + cb_params.ctx.net_idx = ctx->net_idx; + cb_params.ctx.app_idx = ctx->app_idx; + cb_params.ctx.addr = ctx->addr; + cb_params.ctx.recv_ttl = ctx->recv_ttl; + cb_params.ctx.recv_op = ctx->recv_op; + cb_params.ctx.recv_dst = ctx->recv_dst; + + if (val && len) { + length = (len <= sizeof(cb_params.value)) ? len : sizeof(cb_params.value); + memcpy(&cb_params.value, val, length); + } + + btc_ble_mesh_time_scene_server_callback(&cb_params, act); + return; +} + +void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg) +{ + esp_ble_mesh_time_scene_server_cb_param_t *param = NULL; + + if (!msg || !msg->arg) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + param = (esp_ble_mesh_time_scene_server_cb_param_t *)(msg->arg); + + if (msg->act < ESP_BLE_MESH_TIME_SCENE_SERVER_EVT_MAX) { + btc_ble_mesh_time_scene_server_cb_to_app(msg->act, param); + } else { + BT_ERR("%s, Unknown msg->act = %d", __func__, msg->act); + } + + return; +} + diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h new file mode 100644 index 000000000..db37bec69 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_config_model.h @@ -0,0 +1,82 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_CONFIG_MODEL_H_ +#define _BTC_BLE_MESH_CONFIG_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_config_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_CONFIG_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_CONFIG_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_CONFIG_CLIENT_MAX, +} btc_ble_mesh_config_client_act_t; + +typedef union { + struct ble_mesh_cfg_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_cfg_client_get_state_t *get_state; + } cfg_client_get_state; + struct ble_mesh_cfg_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_cfg_client_set_state_t *set_state; + } cfg_client_set_state; +} btc_ble_mesh_config_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_CONFIG_CLIENT_MAX, +} btc_ble_mesh_config_client_evt_t; + +void btc_ble_mesh_config_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_config_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_config_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_config_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_config_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +void btc_ble_mesh_config_server_cb_handler(btc_msg_t *msg); + +typedef enum { + BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_CONFIG_SERVER_MAX, +} btc_ble_mesh_config_server_evt_t; + +void bt_mesh_config_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_CONFIG_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h new file mode 100644 index 000000000..d87421dad --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_generic_model.h @@ -0,0 +1,84 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_GENERIC_MODEL_H_ +#define _BTC_BLE_MESH_GENERIC_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_generic_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_GENERIC_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_GENERIC_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_GENERIC_CLIENT_MAX, +} btc_ble_mesh_generic_client_act_t; + +typedef union { + struct ble_mesh_generic_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_generic_client_get_state_t *get_state; + } generic_client_get_state; + struct ble_mesh_generic_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_generic_client_set_state_t *set_state; + } generic_client_set_state; +} btc_ble_mesh_generic_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_GENERIC_CLIENT_MAX, +} btc_ble_mesh_generic_client_evt_t; + +void btc_ble_mesh_generic_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_generic_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_generic_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_generic_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_generic_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, + BTC_BLE_MESH_EVT_GENERIC_SERVER_MAX, +} btc_ble_mesh_generic_server_evt_t; + +void bt_mesh_generic_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +void btc_ble_mesh_generic_server_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_GENERIC_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h new file mode 100644 index 000000000..91a775511 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_health_model.h @@ -0,0 +1,95 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_HEALTH_MODEL_H_ +#define _BTC_BLE_MESH_HEALTH_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_health_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_HEALTH_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_HEALTH_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_HEALTH_CLIENT_MAX, +} btc_ble_mesh_health_client_act_t; + +typedef union { + struct ble_mesh_health_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_health_client_get_state_t *get_state; + } health_client_get_state; + struct ble_mesh_health_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_health_client_set_state_t *set_state; + } health_client_set_state; +} btc_ble_mesh_health_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_HEALTH_CLIENT_MAX, +} btc_ble_mesh_health_client_evt_t; + +void btc_ble_mesh_health_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_health_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_health_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, u16_t len); + +typedef enum { + BTC_BLE_MESH_ACT_HEALTH_SERVER_FAULT_UPDATE, + BTC_BLE_MESH_ACT_HEALTH_SERVER_MAX, +} btc_ble_mesh_health_server_act_t; + +typedef union { + struct ble_mesh_health_server_fault_update_args { + esp_ble_mesh_elem_t *element; + } health_fault_update; +} btc_ble_mesh_health_server_args_t; + +void btc_ble_mesh_health_server_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_server_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_health_server_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_health_server_fault_clear(struct bt_mesh_model *model, u16_t company_id); + +void btc_ble_mesh_health_server_fault_test(struct bt_mesh_model *model, u8_t test_id, u16_t company_id); + +void btc_ble_mesh_health_server_attention_on(struct bt_mesh_model *model, u8_t time); + +void btc_ble_mesh_health_server_attention_off(struct bt_mesh_model *model); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_HEALTH_MODEL_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h new file mode 100644 index 000000000..453f2ee2c --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_lighting_model.h @@ -0,0 +1,86 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_LIGHTING_MODEL_H_ +#define _BTC_BLE_MESH_LIGHTING_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_lighting_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_LIGHTING_CLIENT_MAX, +} btc_ble_mesh_lighting_client_act_t; + +typedef union { + struct ble_mesh_light_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_light_client_get_state_t *get_state; + } light_client_get_state; + struct ble_mesh_light_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_light_client_set_state_t *set_state; + } light_client_set_state; +} btc_ble_mesh_lighting_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_LIGHTING_CLIENT_MAX, +} btc_ble_mesh_lighting_client_evt_t; + +void btc_ble_mesh_lighting_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_lighting_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_lighting_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_lighting_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_lighting_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, + BTC_BLE_MESH_EVT_LIGHTING_SERVER_MAX, +} btc_ble_mesh_lighting_server_evt_t; + +void bt_mesh_lighting_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +void btc_ble_mesh_lighting_server_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_LIGHTING_MODEL_H_ */ + diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h new file mode 100644 index 000000000..e56425f15 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_prov.h @@ -0,0 +1,335 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_PROV_H_ +#define _BTC_BLE_MESH_PROV_H_ + +#include "btc/btc_manage.h" +#include "mesh_byteorder.h" +#include "mesh_main.h" +#include "provisioner_prov.h" +#include "esp_ble_mesh_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_MESH_INIT = 0, + BTC_BLE_MESH_ACT_PROV_ENABLE, + BTC_BLE_MESH_ACT_PROV_DISABLE, + BTC_BLE_MESH_ACT_NODE_RESET, + BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY, + BTC_BLE_MESH_ACT_INPUT_NUMBER, + BTC_BLE_MESH_ACT_INPUT_STRING, + BTC_BLE_MESH_ACT_SET_DEVICE_NAME, + BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE, + BTC_BLE_MESH_ACT_PROXY_GATT_ENABLE, + BTC_BLE_MESH_ACT_PROXY_GATT_DISABLE, + BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR, + BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM, + BTC_BLE_MESH_ACT_PROVISIONER_ENABLE, + BTC_BLE_MESH_ACT_PROVISIONER_DISABLE, + BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD, + BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR, + BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL, + BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH, + BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO, + BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL, + BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR, + BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME, + BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP, + BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY, + BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID, + BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR, + BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO, + BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION, + BTC_BLE_MESH_ACT_LPN_ENABLE, + BTC_BLE_MESH_ACT_LPN_DISABLE, + BTC_BLE_MESH_ACT_LPN_POLL, + BTC_BLE_MESH_ACT_PROXY_CLIENT_CONNECT, + BTC_BLE_MESH_ACT_PROXY_CLIENT_DISCONNECT, + BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE, + BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR, + BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR, + BTC_BLE_MESH_ACT_START_BLE_ADVERTISING, + BTC_BLE_MESH_ACT_STOP_BLE_ADVERTISING, + BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR, + BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR, + BTC_BLE_MESH_ACT_DEINIT_MESH, +} btc_ble_mesh_prov_act_t; + +typedef enum { + BTC_BLE_MESH_ACT_MODEL_PUBLISH, + BTC_BLE_MESH_ACT_SERVER_MODEL_SEND, + BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND, + BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE, +} btc_ble_mesh_model_act_t; + +typedef union { + struct ble_mesh_init_args { + esp_ble_mesh_prov_t *prov; + esp_ble_mesh_comp_t *comp; + SemaphoreHandle_t semaphore; + } mesh_init; + struct ble_mesh_node_prov_enable_args { + esp_ble_mesh_prov_bearer_t bearers; + } node_prov_enable; + struct ble_mesh_node_prov_disable_args { + esp_ble_mesh_prov_bearer_t bearers; + } node_prov_disable; + struct ble_mesh_set_oob_pub_key_args { + uint8_t pub_key_x[32]; + uint8_t pub_key_y[32]; + uint8_t private_key[32]; + } set_oob_pub_key; + struct ble_mesh_node_input_num_args { + uint32_t number; + } input_number; + struct ble_mesh_node_input_str_args { + char string[8]; + } input_string; + struct ble_mesh_set_device_name_args { + char name[ESP_BLE_MESH_DEVICE_NAME_MAX_LEN + 1]; + } set_device_name; + struct ble_mesh_provisioner_read_oob_pub_key_args { + uint8_t link_idx; + uint8_t pub_key_x[32]; + uint8_t pub_key_y[32]; + } provisioner_read_oob_pub_key; + struct ble_mesh_provisioner_input_str_args { + char string[8]; + uint8_t link_idx; + } provisioner_input_str; + struct ble_mesh_provisioner_input_num_args { + uint32_t number; + uint8_t link_idx; + } provisioner_input_num; + struct ble_mesh_provisioner_enable_args { + esp_ble_mesh_prov_bearer_t bearers; + } provisioner_enable; + struct ble_mesh_provisioner_disable_args { + esp_ble_mesh_prov_bearer_t bearers; + } provisioner_disable; + struct ble_mesh_provisioner_dev_add_args { + esp_ble_mesh_unprov_dev_add_t add_dev; + esp_ble_mesh_dev_add_flag_t flags; + } provisioner_dev_add; + struct ble_mesh_provisioner_prov_dev_with_addr_args { + uint8_t uuid[16]; + esp_ble_mesh_bd_addr_t addr; + esp_ble_mesh_addr_type_t addr_type; + esp_ble_mesh_prov_bearer_t bearer; + uint16_t oob_info; + uint16_t unicast_addr; + } provisioner_prov_dev_with_addr; + struct ble_mesh_provisioner_dev_del_args { + esp_ble_mesh_device_delete_t del_dev; + } provisioner_dev_del; + struct ble_mesh_provisioner_set_dev_uuid_match_args { + uint8_t offset; + uint8_t match_len; + uint8_t match_val[16]; + bool prov_after_match; + } set_dev_uuid_match; + struct ble_mesh_provisioner_set_prov_net_idx_args { + esp_ble_mesh_prov_data_info_t prov_data; + } set_prov_data_info; + struct ble_mesh_provisioner_set_static_oob_val_args { + uint8_t value[16]; + uint8_t length; + } set_static_oob_val; + struct ble_mesh_provisioner_set_primary_elem_addr_args { + uint16_t addr; + } set_primary_elem_addr; + struct ble_mesh_provisioner_set_node_name_args { + uint16_t index; + char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN + 1]; + } set_node_name; + struct ble_mesh_provisioner_add_local_app_key_args { + uint8_t app_key[16]; + uint16_t net_idx; + uint16_t app_idx; + } add_local_app_key; + struct ble_mesh_provisioner_update_local_app_key_args { + uint8_t app_key[16]; + uint16_t net_idx; + uint16_t app_idx; + } update_local_app_key; + struct ble_mesh_provisioner_bind_local_mod_app_args { + uint16_t elem_addr; + uint16_t model_id; + uint16_t cid; + uint16_t app_idx; + } local_mod_app_bind; + struct ble_mesh_provisioner_add_local_net_key_args { + uint8_t net_key[16]; + uint16_t net_idx; + } add_local_net_key; + struct ble_mesh_provisioner_update_local_net_key_args { + uint8_t net_key[16]; + uint16_t net_idx; + } update_local_net_key; + struct ble_mesh_provisioner_store_node_comp_data_args { + uint16_t unicast_addr; + uint16_t length; + uint8_t *data; + } store_node_comp_data; + struct ble_mesh_provisioner_delete_node_with_uuid_args { + uint8_t uuid[16]; + } delete_node_with_uuid; + struct ble_mesh_provisioner_delete_node_with_addr_args { + uint16_t unicast_addr; + } delete_node_with_addr; + struct ble_mesh_set_fast_prov_info_args { + uint16_t unicast_min; + uint16_t unicast_max; + uint16_t net_idx; + uint8_t flags; + uint32_t iv_index; + uint8_t offset; + uint8_t match_len; + uint8_t match_val[16]; + } set_fast_prov_info; + struct ble_mesh_set_fast_prov_action_args { + uint8_t action; + } set_fast_prov_action; + struct ble_mesh_lpn_enable_args { + /* RFU */ + } lpn_enable; + struct ble_mesh_lpn_disable_args { + bool force; + } lpn_disable; + struct ble_mesh_lpn_poll_args { + /* RFU */ + } lpn_poll; + struct ble_mesh_proxy_client_connect_args { + uint8_t addr[6]; + uint8_t addr_type; + uint16_t net_idx; + } proxy_client_connect; + struct ble_mesh_proxy_client_disconnect_args { + uint8_t conn_handle; + } proxy_client_disconnect; + struct ble_mesh_proxy_client_set_filter_type_args { + uint8_t conn_handle; + uint16_t net_idx; + uint8_t filter_type; + } proxy_client_set_filter_type; + struct ble_mesh_proxy_client_add_filter_addr_args { + uint8_t conn_handle; + uint16_t net_idx; + uint16_t addr_num; + uint16_t *addr; + } proxy_client_add_filter_addr; + struct ble_mesh_proxy_client_remove_filter_addr_args { + uint8_t conn_handle; + uint16_t net_idx; + uint16_t addr_num; + uint16_t *addr; + } proxy_client_remove_filter_addr; + struct ble_mesh_start_ble_advertising_args { + esp_ble_mesh_ble_adv_param_t param; + esp_ble_mesh_ble_adv_data_t data; + } start_ble_advertising; + struct ble_mesh_stop_ble_advertising_args { + uint8_t index; + } stop_ble_advertising; + struct ble_mesh_model_sub_group_addr_args { + uint16_t element_addr; + uint16_t company_id; + uint16_t model_id; + uint16_t group_addr; + } model_sub_group_addr; + struct ble_mesh_model_unsub_group_addr_args { + uint16_t element_addr; + uint16_t company_id; + uint16_t model_id; + uint16_t group_addr; + } model_unsub_group_addr; + struct ble_mesh_deinit_args { + esp_ble_mesh_deinit_param_t param; + } mesh_deinit; +} btc_ble_mesh_prov_args_t; + +typedef union { + struct ble_mesh_model_publish_args { + esp_ble_mesh_model_t *model; + uint8_t device_role; + } model_publish; + struct ble_mesh_model_send_args { + esp_ble_mesh_model_t *model; + esp_ble_mesh_msg_ctx_t *ctx; + uint32_t opcode; + bool need_rsp; + uint16_t length; + uint8_t *data; + uint8_t device_role; + int32_t msg_timeout; + } model_send; + struct ble_mesh_server_model_update_state_args { + esp_ble_mesh_model_t *model; + esp_ble_mesh_server_state_type_t type; + esp_ble_mesh_server_state_value_t *value; + } model_update_state; +} btc_ble_mesh_model_args_t; + +void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]); + +esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr); + +int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model); + +int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model); + +int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod); + +uint16_t btc_ble_mesh_get_primary_addr(void); + +uint16_t *btc_ble_mesh_model_find_group(esp_ble_mesh_model_t *mod, uint16_t addr); + +esp_ble_mesh_elem_t *btc_ble_mesh_elem_find(u16_t addr); + +uint8_t btc_ble_mesh_elem_count(void); + +esp_ble_mesh_model_t *btc_ble_mesh_model_find_vnd(const esp_ble_mesh_elem_t *elem, + uint16_t company, uint16_t id); + +esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, + uint16_t id); + +const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void); + +u16_t btc_ble_mesh_provisioner_get_prov_node_count(void); + +void btc_ble_mesh_model_call_handler(btc_msg_t *msg); +void btc_ble_mesh_model_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_prov_call_handler(btc_msg_t *msg); +void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_PROV_H_ */ diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h new file mode 100644 index 000000000..65a2af4f7 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_sensor_model.h @@ -0,0 +1,85 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_SENSOR_MODEL_H_ +#define _BTC_BLE_MESH_SENSOR_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_sensor_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_SENSOR_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_SENSOR_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_SENSOR_CLIENT_MAX, +} btc_ble_mesh_sensor_client_act_t; + +typedef union { + struct ble_mesh_sensor_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_sensor_client_get_state_t *get_state; + } sensor_client_get_state; + struct ble_mesh_sensor_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_sensor_client_set_state_t *set_state; + } sensor_client_set_state; +} btc_ble_mesh_sensor_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_SENSOR_CLIENT_MAX, +} btc_ble_mesh_sensor_client_evt_t; + +void btc_ble_mesh_sensor_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_sensor_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_sensor_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_sensor_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_sensor_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, + BTC_BLE_MESH_EVT_SENSOR_SERVER_MAX, +} btc_ble_mesh_sensor_server_evt_t; + +void bt_mesh_sensor_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +void btc_ble_mesh_sensor_server_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_SENSOR_MODEL_H_ */ + diff --git a/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h new file mode 100644 index 000000000..7db8764b5 --- /dev/null +++ b/components/bt/esp_ble_mesh/btc/include/btc_ble_mesh_time_scene_model.h @@ -0,0 +1,86 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BTC_BLE_MESH_TIME_SCENE_MODEL_H_ +#define _BTC_BLE_MESH_TIME_SCENE_MODEL_H_ + +#include "btc/btc_manage.h" +#include "esp_ble_mesh_time_scene_model_api.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_GET_STATE, + BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_SET_STATE, + BTC_BLE_MESH_ACT_TIME_SCENE_CLIENT_MAX, +} btc_ble_mesh_time_scene_client_act_t; + +typedef union { + struct ble_mesh_time_scene_client_get_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_time_scene_client_get_state_t *get_state; + } time_scene_client_get_state; + struct ble_mesh_time_scene_client_set_state_reg_args { + esp_ble_mesh_client_common_param_t *params; + esp_ble_mesh_time_scene_client_set_state_t *set_state; + } time_scene_client_set_state; +} btc_ble_mesh_time_scene_client_args_t; + +typedef enum { + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_PUBLISH, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, + BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_MAX, +} btc_ble_mesh_time_scene_client_evt_t; + +void btc_ble_mesh_time_scene_client_call_handler(btc_msg_t *msg); + +void btc_ble_mesh_time_scene_client_cb_handler(btc_msg_t *msg); + +void btc_ble_mesh_time_scene_client_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src); + +void btc_ble_mesh_time_scene_client_publish_callback(u32_t opcode, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); + +void bt_mesh_time_scene_client_cb_evt_to_btc(u32_t opcode, u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +typedef enum { + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_STATUS_MSG, + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_MAX, +} btc_ble_mesh_time_scene_server_evt_t; + +void bt_mesh_time_scene_server_cb_evt_to_btc(u8_t evt_type, + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + const u8_t *val, size_t len); + +void btc_ble_mesh_time_scene_server_cb_handler(btc_msg_t *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* _BTC_BLE_MESH_TIME_SCENE_MODEL_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_aes_encrypt.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_aes_encrypt.h new file mode 100644 index 000000000..afaa6b27d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_aes_encrypt.h @@ -0,0 +1,171 @@ +/* aes.h - TinyCrypt interface to an AES-128 implementation */ + +/* + * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * @brief -- Interface to an AES-128 implementation. + * + * Overview: AES-128 is a NIST approved block cipher specified in + * FIPS 197. Block ciphers are deterministic algorithms that + * perform a transformation specified by a symmetric key in fixed- + * length data sets, also called blocks. + * + * Security: AES-128 provides approximately 128 bits of security. + * + * Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key. + * + * 2) call tc_aes_encrypt/decrypt to process the data. + */ + +#ifndef _BLE_MESH_AES_ENCRYPT_H_ +#define _BLE_MESH_AES_ENCRYPT_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define Nb (4) /* number of columns (32-bit words) comprising the state */ +#define Nk (4) /* number of 32-bit words comprising the key */ +#define Nr (10) /* number of rounds */ +#define TC_AES_BLOCK_SIZE (Nb*Nk) +#define TC_AES_KEY_SIZE (Nb*Nk) + +#define TC_CRYPTO_SUCCESS 1 +#define TC_CRYPTO_FAIL 0 + +#define TC_ZERO_BYTE 0x00 + +/* padding for last message block */ +#define TC_CMAC_PADDING 0x80 + +typedef struct tc_aes_key_sched_struct { + unsigned int words[Nb * (Nr + 1)]; +} *TCAesKeySched_t; + +/* struct tc_cmac_struct represents the state of a CMAC computation */ +typedef struct tc_cmac_struct { + /* initialization vector */ + uint8_t iv[TC_AES_BLOCK_SIZE]; + /* used if message length is a multiple of block_size bytes */ + uint8_t K1[TC_AES_BLOCK_SIZE]; + /* used if message length isn't a multiple block_size bytes */ + uint8_t K2[TC_AES_BLOCK_SIZE]; + /* where to put bytes that didn't fill a block */ + uint8_t leftover[TC_AES_BLOCK_SIZE]; + /* identifies the encryption key */ + unsigned int keyid; + /* next available leftover location */ + unsigned int leftover_offset; + /* AES key schedule */ + TCAesKeySched_t sched; + /* calls to tc_cmac_update left before re-key */ + uint64_t countdown; +} *TCCmacState_t; + +/** + * @brief Set AES-128 encryption key + * Uses key k to initialize s + * @return returns TC_CRYPTO_SUCCESS (1) + * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL + * @note This implementation skips the additional steps required for keys + * larger than 128 bits, and must not be used for AES-192 or + * AES-256 key schedule -- see FIPS 197 for details + * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct + * @param k IN -- points to the AES key + */ +int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k); + +/** + * @brief AES-128 Encryption procedure + * Encrypts contents of in buffer into out buffer under key; + * schedule s + * @note Assumes s was initialized by aes_set_encrypt_key; + * out and in point to 16 byte buffers + * @return returns TC_CRYPTO_SUCCESS (1) + * returns TC_CRYPTO_FAIL (0) if: out == NULL or in == NULL or s == NULL + * @param out IN/OUT -- buffer to receive ciphertext block + * @param in IN -- a plaintext block to encrypt + * @param s IN -- initialized AES key schedule + */ +int tc_aes_encrypt(uint8_t *out, const uint8_t *in, + const TCAesKeySched_t s); + +/** + * @brief Set the AES-128 decryption key + * Uses key k to initialize s + * @return returns TC_CRYPTO_SUCCESS (1) + * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL + * @note This is the implementation of the straightforward inverse cipher + * using the cipher documented in FIPS-197 figure 12, not the + * equivalent inverse cipher presented in Figure 15 + * @warning This routine skips the additional steps required for keys larger + * than 128, and must not be used for AES-192 or AES-256 key + * schedule -- see FIPS 197 for details + * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct + * @param k IN -- points to the AES key + */ +int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k); + +/** + * @brief AES-128 Encryption procedure + * Decrypts in buffer into out buffer under key schedule s + * @return returns TC_CRYPTO_SUCCESS (1) + * returns TC_CRYPTO_FAIL (0) if: out is NULL or in is NULL or s is NULL + * @note Assumes s was initialized by aes_set_encrypt_key + * out and in point to 16 byte buffers + * @param out IN/OUT -- buffer to receive ciphertext block + * @param in IN -- a plaintext block to encrypt + * @param s IN -- initialized AES key schedule + */ +int tc_aes_decrypt(uint8_t *out, const uint8_t *in, + const TCAesKeySched_t s); + +int tc_cmac_setup(TCCmacState_t s, const uint8_t *key, TCAesKeySched_t sched); + +void gf_double(uint8_t *out, uint8_t *in); + +int tc_cmac_init(TCCmacState_t s); + +int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t data_length); + +int tc_cmac_final(uint8_t *tag, TCCmacState_t s); + +int tc_cmac_erase(TCCmacState_t s); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_AES_ENCRYPT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_atomic.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_atomic.h new file mode 100644 index 000000000..5c8bf17b8 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_atomic.h @@ -0,0 +1,305 @@ +/* atomic operations */ + +/* + * Copyright (c) 1997-2015, Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_ATOMIC_H_ +#define _BLE_MESH_ATOMIC_H_ + +#include "mesh_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef bt_mesh_atomic_t bt_mesh_atomic_val_t; + +/** + * @defgroup atomic_apis Atomic Services APIs + * @ingroup kernel_apis + * @{ + */ + +/** + * + * @brief Atomic increment. + * + * This routine performs an atomic increment by 1 on @a target. + * + * @param target Address of atomic variable. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target) +{ + return bt_mesh_atomic_add(target, 1); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target); +#endif + +/** + * + * @brief Atomic decrement. + * + * This routine performs an atomic decrement by 1 on @a target. + * + * @param target Address of atomic variable. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target) +{ + return bt_mesh_atomic_sub(target, 1); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target); +#endif + +/** + * + * @brief Atomic get. + * + * This routine performs an atomic read on @a target. + * + * @param target Address of atomic variable. + * + * @return Value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target) +{ + return __atomic_load_n(target, __ATOMIC_SEQ_CST); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target); +#endif + +/** + * + * @brief Atomic get-and-set. + * + * This routine atomically sets @a target to @a value and returns + * the previous value of @a target. + * + * @param target Address of atomic variable. + * @param value Value to write to @a target. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + /* This builtin, as described by Intel, is not a traditional + * test-and-set operation, but rather an atomic exchange operation. It + * writes value into *ptr, and returns the previous contents of *ptr. + */ + return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value); +#endif + +/** + * + * @brief Atomic bitwise inclusive OR. + * + * This routine atomically sets @a target to the bitwise inclusive OR of + * @a target and @a value. + * + * @param target Address of atomic variable. + * @param value Value to OR. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value); +#endif + +/** + * + * @brief Atomic bitwise AND. + * + * This routine atomically sets @a target to the bitwise AND of @a target + * and @a value. + * + * @param target Address of atomic variable. + * @param value Value to AND. + * + * @return Previous value of @a target. + */ +#ifdef CONFIG_ATOMIC_OPERATIONS_BUILTIN +static inline bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST); +} +#else +extern bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value); +#endif + +/** + * @cond INTERNAL_HIDDEN + */ + +#define BLE_MESH_ATOMIC_BITS (sizeof(bt_mesh_atomic_val_t) * 8) +#define BLE_MESH_ATOMIC_MASK(bit) (1 << ((bit) & (BLE_MESH_ATOMIC_BITS - 1))) +#define BLE_MESH_ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / BLE_MESH_ATOMIC_BITS)) + +/** + * INTERNAL_HIDDEN @endcond + */ + +/** + * @brief Define an array of atomic variables. + * + * This macro defines an array of atomic variables containing at least + * @a num_bits bits. + * + * @note + * If used from file scope, the bits of the array are initialized to zero; + * if used from within a function, the bits are left uninitialized. + * + * @param name Name of array of atomic variables. + * @param num_bits Number of bits needed. + */ +#define BLE_MESH_ATOMIC_DEFINE(name, num_bits) \ + bt_mesh_atomic_t name[1 + ((num_bits) - 1) / BLE_MESH_ATOMIC_BITS] + +/** + * @brief Atomically test a bit. + * + * This routine tests whether bit number @a bit of @a target is set or not. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return 1 if the bit was set, 0 if it wasn't. + */ +static inline int bt_mesh_atomic_test_bit(const bt_mesh_atomic_t *target, int bit) +{ + bt_mesh_atomic_val_t val = bt_mesh_atomic_get(BLE_MESH_ATOMIC_ELEM(target, bit)); + + return (1 & (val >> (bit & (BLE_MESH_ATOMIC_BITS - 1)))); +} + +/** + * @brief Atomically test and clear a bit. + * + * Atomically clear bit number @a bit of @a target and return its old value. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return 1 if the bit was set, 0 if it wasn't. + */ +static inline int bt_mesh_atomic_test_and_clear_bit(bt_mesh_atomic_t *target, int bit) +{ + bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit); + bt_mesh_atomic_val_t old; + + old = bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask); + + return (old & mask) != 0; +} + +/** + * @brief Atomically set a bit. + * + * Atomically set bit number @a bit of @a target and return its old value. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return 1 if the bit was set, 0 if it wasn't. + */ +static inline int bt_mesh_atomic_test_and_set_bit(bt_mesh_atomic_t *target, int bit) +{ + bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit); + bt_mesh_atomic_val_t old; + + old = bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask); + + return (old & mask) != 0; +} + +/** + * @brief Atomically clear a bit. + * + * Atomically clear bit number @a bit of @a target. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return N/A + */ +static inline void bt_mesh_atomic_clear_bit(bt_mesh_atomic_t *target, int bit) +{ + bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit); + + (void)bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask); +} + +/** + * @brief Atomically set a bit. + * + * Atomically set bit number @a bit of @a target. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return N/A + */ +static inline void bt_mesh_atomic_set_bit(bt_mesh_atomic_t *target, int bit) +{ + bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit); + + (void)bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask); +} + +/** + * @brief Atomically set a bit to a given value. + * + * Atomically set bit number @a bit of @a target to value @a val. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * @param val true for 1, false for 0. + * + * @return N/A + */ +static inline void bt_mesh_atomic_set_bit_to(bt_mesh_atomic_t *target, int bit, bool val) +{ + bt_mesh_atomic_val_t mask = BLE_MESH_ATOMIC_MASK(bit); + + if (val) { + (void)bt_mesh_atomic_or(BLE_MESH_ATOMIC_ELEM(target, bit), mask); + } else { + (void)bt_mesh_atomic_and(BLE_MESH_ATOMIC_ELEM(target, bit), ~mask); + } +} + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_ATOMIC_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h new file mode 100644 index 000000000..4a47b1c0c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_buf.h @@ -0,0 +1,1758 @@ +/** @file + * @brief Buffer management. + */ + +/* + * Copyright (c) 2015 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_BUF_H_ +#define _BLE_MESH_BUF_H_ + +#include "sdkconfig.h" +#include "mesh_slist.h" +#include "mesh_compiler.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Unaligned access */ +#define UNALIGNED_GET(p) \ +__extension__ ({ \ + struct __attribute__((__packed__)) { \ + __typeof__(*(p)) __v; \ + } *__p = (__typeof__(__p)) (p); \ + __p->__v; \ +}) + +#define BLE_MESH_NET_BUF_USER_DATA_SIZE 4 + +/** + * @brief Network buffer library + * @defgroup net_buf Network Buffer Library + * @ingroup networking + * @{ + */ + +/* Alignment needed for various parts of the buffer definition */ +#define __net_buf_align __aligned(sizeof(int)) + +/** + * @def NET_BUF_SIMPLE_DEFINE + * @brief Define a net_buf_simple stack variable. + * + * This is a helper macro which is used to define a net_buf_simple object + * on the stack. + * + * @param _name Name of the net_buf_simple object. + * @param _size Maximum data storage for the buffer. + */ +#define NET_BUF_SIMPLE_DEFINE(_name, _size) \ + u8_t net_buf_data_##_name[_size]; \ + struct net_buf_simple _name = { \ + .data = net_buf_data_##_name, \ + .len = 0, \ + .size = _size, \ + .__buf = net_buf_data_##_name, \ + } + +/** + * @def NET_BUF_SIMPLE_DEFINE_STATIC + * @brief Define a static net_buf_simple variable. + * + * This is a helper macro which is used to define a static net_buf_simple + * object. + * + * @param _name Name of the net_buf_simple object. + * @param _size Maximum data storage for the buffer. + */ +#define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \ + static u8_t net_buf_data_##_name[_size]; \ + static struct net_buf_simple _name = { \ + .data = net_buf_data_##_name, \ + .len = 0, \ + .size = _size, \ + .__buf = net_buf_data_##_name, \ + } + +/** + * @brief Simple network buffer representation. + * + * This is a simpler variant of the net_buf object (in fact net_buf uses + * net_buf_simple internally). It doesn't provide any kind of reference + * counting, user data, dynamic allocation, or in general the ability to + * pass through kernel objects such as FIFOs. + * + * The main use of this is for scenarios where the meta-data of the normal + * net_buf isn't needed and causes too much overhead. This could be e.g. + * when the buffer only needs to be allocated on the stack or when the + * access to and lifetime of the buffer is well controlled and constrained. + */ +struct net_buf_simple { + /** Pointer to the start of data in the buffer. */ + u8_t *data; + + /** Length of the data behind the data pointer. */ + u16_t len; + + /** Amount of data that this buffer can store. */ + u16_t size; + + /** Start of the data storage. Not to be accessed directly + * (the data pointer should be used instead). + */ + u8_t *__buf; +}; + +/** + * @def NET_BUF_SIMPLE + * @brief Define a net_buf_simple stack variable and get a pointer to it. + * + * This is a helper macro which is used to define a net_buf_simple object on + * the stack and the get a pointer to it as follows: + * + * struct net_buf_simple *my_buf = NET_BUF_SIMPLE(10); + * + * After creating the object it needs to be initialized by calling + * net_buf_simple_init(). + * + * @param _size Maximum data storage for the buffer. + * + * @return Pointer to stack-allocated net_buf_simple object. + */ +#define NET_BUF_SIMPLE(_size) \ + ((struct net_buf_simple *)(&(struct { \ + struct net_buf_simple buf; \ + u8_t data[_size] __net_buf_align; \ + }) { \ + .buf.size = _size, \ + .buf.__buf = NULL, \ + })) + +/** + * @brief Initialize a net_buf_simple object. + * + * This needs to be called after creating a net_buf_simple object using + * the NET_BUF_SIMPLE macro. + * + * @param buf Buffer to initialize. + * @param reserve_head Headroom to reserve. + */ +static inline void net_buf_simple_init(struct net_buf_simple *buf, + size_t reserve_head) +{ + if (!buf->__buf) { + buf->__buf = (u8_t *)buf + sizeof(*buf); + } + + buf->data = buf->__buf + reserve_head; + buf->len = 0; +} + +/** + * @brief Initialize a net_buf_simple object with data. + * + * Initialized buffer object with external data. + * + * @param buf Buffer to initialize. + * @param data External data pointer + * @param size Amount of data the pointed data buffer if able to fit. + */ +void net_buf_simple_init_with_data(struct net_buf_simple *buf, + void *data, size_t size); + +/** + * @brief Reset buffer + * + * Reset buffer data so it can be reused for other purposes. + * + * @param buf Buffer to reset. + */ +static inline void net_buf_simple_reset(struct net_buf_simple *buf) +{ + buf->len = 0; + buf->data = buf->__buf; +} + +/** + * Clone buffer state, using the same data buffer. + * + * Initializes a buffer to point to the same data as an existing buffer. + * Allows operations on the same data without altering the length and + * offset of the original. + * + * @param original Buffer to clone. + * @param clone The new clone. + */ +void net_buf_simple_clone(const struct net_buf_simple *original, + struct net_buf_simple *clone); + +/** + * @brief Prepare data to be added at the end of the buffer + * + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param len Number of bytes to increment the length with. + * + * @return The original tail of the buffer. + */ +void *net_buf_simple_add(struct net_buf_simple *buf, size_t len); + +/** + * @brief Copy given number of bytes from memory to the end of the buffer + * + * Increments the data length of the buffer to account for more data at the + * end. + * + * @param buf Buffer to update. + * @param mem Location of data to be added. + * @param len Length of data to be added + * + * @return The original tail of the buffer. + */ +void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, + size_t len); + +/** + * @brief Add (8-bit) byte at the end of the buffer + * + * Increments the data length of the buffer to account for more data at the + * end. + * + * @param buf Buffer to update. + * @param val byte value to be added. + * + * @return Pointer to the value added + */ +u8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, u8_t val); + +/** + * @brief Add 16-bit value at the end of the buffer + * + * Adds 16-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 16-bit value to be added. + */ +void net_buf_simple_add_le16(struct net_buf_simple *buf, u16_t val); + +/** + * @brief Add 16-bit value at the end of the buffer + * + * Adds 16-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 16-bit value to be added. + */ +void net_buf_simple_add_be16(struct net_buf_simple *buf, u16_t val); + +/** + * @brief Add 24-bit value at the end of the buffer + * + * Adds 24-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 24-bit value to be added. + */ +void net_buf_simple_add_le24(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Add 24-bit value at the end of the buffer + * + * Adds 24-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 24-bit value to be added. + */ +void net_buf_simple_add_be24(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Add 32-bit value at the end of the buffer + * + * Adds 32-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 32-bit value to be added. + */ +void net_buf_simple_add_le32(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Add 32-bit value at the end of the buffer + * + * Adds 32-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 32-bit value to be added. + */ +void net_buf_simple_add_be32(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Add 48-bit value at the end of the buffer + * + * Adds 48-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 48-bit value to be added. + */ +void net_buf_simple_add_le48(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Add 48-bit value at the end of the buffer + * + * Adds 48-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 48-bit value to be added. + */ +void net_buf_simple_add_be48(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Add 64-bit value at the end of the buffer + * + * Adds 64-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 64-bit value to be added. + */ +void net_buf_simple_add_le64(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Add 64-bit value at the end of the buffer + * + * Adds 64-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 64-bit value to be added. + */ +void net_buf_simple_add_be64(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Push data to the beginning of the buffer. + * + * Modifies the data pointer and buffer length to account for more data + * in the beginning of the buffer. + * + * @param buf Buffer to update. + * @param len Number of bytes to add to the beginning. + * + * @return The new beginning of the buffer data. + */ +void *net_buf_simple_push(struct net_buf_simple *buf, size_t len); + +/** + * @brief Push 16-bit value to the beginning of the buffer + * + * Adds 16-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 16-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_le16(struct net_buf_simple *buf, u16_t val); + +/** + * @brief Push 16-bit value to the beginning of the buffer + * + * Adds 16-bit value in big endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 16-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_be16(struct net_buf_simple *buf, u16_t val); + +/** + * @brief Push 8-bit value to the beginning of the buffer + * + * Adds 8-bit value the beginning of the buffer. + * + * @param buf Buffer to update. + * @param val 8-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_u8(struct net_buf_simple *buf, u8_t val); + +/** + * @brief Push 24-bit value to the beginning of the buffer + * + * Adds 24-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 24-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_le24(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Push 24-bit value to the beginning of the buffer + * + * Adds 24-bit value in big endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 24-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_be24(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Push 32-bit value to the beginning of the buffer + * + * Adds 32-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 32-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_le32(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Push 32-bit value to the beginning of the buffer + * + * Adds 32-bit value in big endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 32-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_be32(struct net_buf_simple *buf, u32_t val); + +/** + * @brief Push 48-bit value to the beginning of the buffer + * + * Adds 48-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 48-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_le48(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Push 48-bit value to the beginning of the buffer + * + * Adds 48-bit value in big endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 48-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_be48(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Push 64-bit value to the beginning of the buffer + * + * Adds 64-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 64-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_le64(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Push 64-bit value to the beginning of the buffer + * + * Adds 64-bit value in big endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 64-bit value to be pushed to the buffer. + */ +void net_buf_simple_push_be64(struct net_buf_simple *buf, u64_t val); + +/** + * @brief Remove data from the beginning of the buffer. + * + * Removes data from the beginning of the buffer by modifying the data + * pointer and buffer length. + * + * @param buf Buffer to update. + * @param len Number of bytes to remove. + * + * @return New beginning of the buffer data. + */ +void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len); + +/** + * @brief Remove data from the beginning of the buffer. + * + * Removes data from the beginning of the buffer by modifying the data + * pointer and buffer length. + * + * @param buf Buffer to update. + * @param len Number of bytes to remove. + * + * @return Pointer to the old location of the buffer data. + */ +void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len); + +/** + * @brief Remove a 8-bit value from the beginning of the buffer + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 8-bit values. + * + * @param buf A valid pointer on a buffer. + * + * @return The 8-bit removed value + */ +u8_t net_buf_simple_pull_u8(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 16 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 16-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 16-bit value converted from little endian to host endian. + */ +u16_t net_buf_simple_pull_le16(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 16 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 16-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 16-bit value converted from big endian to host endian. + */ +u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 24 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 24-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 24-bit value converted from little endian to host endian. + */ +u32_t net_buf_simple_pull_le24(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 24 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 24-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 24-bit value converted from big endian to host endian. + */ +u32_t net_buf_simple_pull_be24(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 32 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 32-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 32-bit value converted from little endian to host endian. + */ +u32_t net_buf_simple_pull_le32(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 32 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 32-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 32-bit value converted from big endian to host endian. + */ +u32_t net_buf_simple_pull_be32(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 48 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 48-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 48-bit value converted from little endian to host endian. + */ +u64_t net_buf_simple_pull_le48(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 48 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 48-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 48-bit value converted from big endian to host endian. + */ +u64_t net_buf_simple_pull_be48(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 64 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 64-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 64-bit value converted from little endian to host endian. + */ +u64_t net_buf_simple_pull_le64(struct net_buf_simple *buf); + +/** + * @brief Remove and convert 64 bits from the beginning of the buffer. + * + * Same idea as with net_buf_simple_pull(), but a helper for operating + * on 64-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 64-bit value converted from big endian to host endian. + */ +u64_t net_buf_simple_pull_be64(struct net_buf_simple *buf); + +/** + * @brief Get the tail pointer for a buffer. + * + * Get a pointer to the end of the data in a buffer. + * + * @param buf Buffer. + * + * @return Tail pointer for the buffer. + */ +static inline u8_t *net_buf_simple_tail(struct net_buf_simple *buf) +{ + return buf->data + buf->len; +} + +/** + * @brief Check buffer headroom. + * + * Check how much free space there is in the beginning of the buffer. + * + * buf A valid pointer on a buffer + * + * @return Number of bytes available in the beginning of the buffer. + */ +size_t net_buf_simple_headroom(struct net_buf_simple *buf); + +/** + * @brief Check buffer tailroom. + * + * Check how much free space there is at the end of the buffer. + * + * @param buf A valid pointer on a buffer + * + * @return Number of bytes available at the end of the buffer. + */ +size_t net_buf_simple_tailroom(struct net_buf_simple *buf); + +/** + * @brief Parsing state of a buffer. + * + * This is used for temporarily storing the parsing state of a buffer + * while giving control of the parsing to a routine which we don't + * control. + */ +struct net_buf_simple_state { + /** Offset of the data pointer from the beginning of the storage */ + u16_t offset; + /** Length of data */ + u16_t len; +}; + +/** + * @brief Save the parsing state of a buffer. + * + * Saves the parsing state of a buffer so it can be restored later. + * + * @param buf Buffer from which the state should be saved. + * @param state Storage for the state. + */ +static inline void net_buf_simple_save(struct net_buf_simple *buf, + struct net_buf_simple_state *state) +{ + state->offset = net_buf_simple_headroom(buf); + state->len = buf->len; +} + +/** + * @brief Restore the parsing state of a buffer. + * + * Restores the parsing state of a buffer from a state previously stored + * by net_buf_simple_save(). + * + * @param buf Buffer to which the state should be restored. + * @param state Stored state. + */ +static inline void net_buf_simple_restore(struct net_buf_simple *buf, + struct net_buf_simple_state *state) +{ + buf->data = buf->__buf + state->offset; + buf->len = state->len; +} + +/** + * @brief Initialize buffer with the given headroom. + * + * The buffer is not expected to contain any data when this API is called. + * + * @param buf Buffer to initialize. + * @param reserve How much headroom to reserve. + */ +void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve); + +/** + * Flag indicating that the buffer has associated fragments. Only used + * internally by the buffer handling code while the buffer is inside a + * FIFO, meaning this never needs to be explicitly set or unset by the + * net_buf API user. As long as the buffer is outside of a FIFO, i.e. + * in practice always for the user for this API, the buf->frags pointer + * should be used instead. + */ +#define NET_BUF_FRAGS BIT(0) + +/** + * @brief Network buffer representation. + * + * This struct is used to represent network buffers. Such buffers are + * normally defined through the NET_BUF_POOL_*_DEFINE() APIs and allocated + * using the net_buf_alloc() API. + */ +struct net_buf { + union { + /** Allow placing the buffer into sys_slist_t */ + sys_snode_t node; + + /** Fragments associated with this buffer. */ + struct net_buf *frags; + }; + + /** Reference count. */ + u8_t ref; + + /** Bit-field of buffer flags. */ + u8_t flags; + + /** Where the buffer should go when freed up. */ + struct net_buf_pool *pool; + + /* Union for convenience access to the net_buf_simple members, also + * preserving the old API. + */ + union { + /* The ABI of this struct must match net_buf_simple */ + struct { + /** Pointer to the start of data in the buffer. */ + u8_t *data; + + /** Length of the data behind the data pointer. */ + u16_t len; + + /** Amount of data that this buffer can store. */ + u16_t size; + + /** Start of the data storage. Not to be accessed + * directly (the data pointer should be used + * instead). + */ + u8_t *__buf; + }; + + struct net_buf_simple b; + }; + + /** System metadata for this buffer. */ + u8_t user_data[BLE_MESH_NET_BUF_USER_DATA_SIZE] __net_buf_align; +}; + +struct net_buf_data_cb { + u8_t *(*alloc)(struct net_buf *buf, size_t *size, s32_t timeout); + u8_t *(*ref)(struct net_buf *buf, u8_t *data); + void (*unref)(struct net_buf *buf, u8_t *data); +}; + +struct net_buf_data_alloc { + const struct net_buf_data_cb *cb; + void *alloc_data; +}; + +struct net_buf_pool { + /** Number of buffers in pool */ + const u16_t buf_count; + + /** Number of uninitialized buffers */ + u16_t uninit_count; + +#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) + /** Amount of available buffers in the pool. */ + s16_t avail_count; + + /** Total size of the pool. */ + const u16_t pool_size; + + /** Name of the pool. Used when printing pool information. */ + const char *name; +#endif /* CONFIG_BLE_MESH_NET_BUF_POOL_USAGE */ + + /** Optional destroy callback when buffer is freed. */ + void (*const destroy)(struct net_buf *buf); + + /** Data allocation handlers. */ + const struct net_buf_data_alloc *alloc; + + /** Helper to access the start of storage (for net_buf_pool_init) */ + struct net_buf *const __bufs; +}; + +#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) +#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \ + { \ + .alloc = _alloc, \ + .__bufs = (struct net_buf *)_bufs, \ + .buf_count = _count, \ + .uninit_count = _count, \ + .avail_count = _count, \ + .destroy = _destroy, \ + .name = STRINGIFY(_pool), \ + } +#else +#define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \ + { \ + .alloc = _alloc, \ + .__bufs = (struct net_buf *)_bufs, \ + .buf_count = _count, \ + .uninit_count = _count, \ + .destroy = _destroy, \ + } +#endif /* CONFIG_BLE_MESH_NET_BUF_POOL_USAGE */ + +struct net_buf_pool_fixed { + size_t data_size; + u8_t *data_pool; +}; + +/** @cond INTERNAL_HIDDEN */ +extern const struct net_buf_data_cb net_buf_fixed_cb; + +/** + * @def NET_BUF_POOL_FIXED_DEFINE + * @brief Define a new pool for buffers based on fixed-size data + * + * Defines a net_buf_pool struct and the necessary memory storage (array of + * structs) for the needed amount of buffers. After this, the buffers can be + * accessed from the pool through net_buf_alloc. The pool is defined as a + * static variable, so if it needs to be exported outside the current module + * this needs to happen with the help of a separate pointer rather than an + * extern declaration. + * + * The data payload of the buffers will be allocated from a byte array + * of fixed sized chunks. This kind of pool does not support blocking on + * the data allocation, so the timeout passed to net_buf_alloc will be + * always treated as K_NO_WAIT when trying to allocate the data. This means + * that allocation failures, i.e. NULL returns, must always be handled + * cleanly. + * + * If provided with a custom destroy callback, this callback is + * responsible for eventually calling net_buf_destroy() to complete the + * process of returning the buffer to the pool. + * + * @param _name Name of the pool variable. + * @param _count Number of buffers in the pool. + * @param _data_size Maximum data payload per buffer. + * @param _destroy Optional destroy callback when buffer is freed. + */ +#define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _destroy) \ + static struct net_buf net_buf_##_name[_count]; \ + static u8_t net_buf_data_##_name[_count][_data_size]; \ + static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \ + .data_size = _data_size, \ + .data_pool = (u8_t *)net_buf_data_##_name, \ + }; \ + static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \ + .cb = &net_buf_fixed_cb, \ + .alloc_data = (void *)&net_buf_fixed_##_name, \ + }; \ + struct net_buf_pool _name __net_buf_align \ + __in_section(_net_buf_pool, static, _name) = \ + NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \ + net_buf_##_name, _count, _destroy) + +/** + * @def NET_BUF_POOL_DEFINE + * @brief Define a new pool for buffers + * + * Defines a net_buf_pool struct and the necessary memory storage (array of + * structs) for the needed amount of buffers. After this,the buffers can be + * accessed from the pool through net_buf_alloc. The pool is defined as a + * static variable, so if it needs to be exported outside the current module + * this needs to happen with the help of a separate pointer rather than an + * extern declaration. + * + * If provided with a custom destroy callback this callback is + * responsible for eventually calling net_buf_destroy() to complete the + * process of returning the buffer to the pool. + * + * @param _name Name of the pool variable. + * @param _count Number of buffers in the pool. + * @param _size Maximum data size for each buffer. + * @param _ud_size Amount of user data space to reserve. + * @param _destroy Optional destroy callback when buffer is freed. + */ +#define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \ + NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _destroy) + +/** + * @brief Get a zero-based index for a buffer. + * + * This function will translate a buffer into a zero-based index, + * based on its placement in its buffer pool. This can be useful if you + * want to associate an external array of meta-data contexts with the + * buffers of a pool. + * + * @param buf Network buffer. + * + * @return Zero-based index for the buffer. + */ +int net_buf_id(struct net_buf *buf); + +/** + * @brief Allocate a new fixed buffer from a pool. + * + * @param pool Which pool to allocate the buffer from. + * @param timeout Affects the action taken should the pool be empty. + * If K_NO_WAIT, then return immediately. If K_FOREVER, then + * wait as long as necessary. Otherwise, wait up to the specified + * number of milliseconds before timing out. Note that some types + * of data allocators do not support blocking (such as the HEAP + * type). In this case it's still possible for net_buf_alloc() to + * fail (return NULL) even if it was given K_FOREVER. + * + * @return New buffer or NULL if out of buffers. + */ +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool, s32_t timeout, + const char *func, int line); +#define net_buf_alloc_fixed(_pool, _timeout) \ + net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__) +#else +struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool, s32_t timeout); +#endif + +/** + * @def net_buf_alloc + * + * @copydetails net_buf_alloc_fixed + */ +#define net_buf_alloc(pool, timeout) net_buf_alloc_fixed(pool, timeout) + +/** + * @brief Reset buffer + * + * Reset buffer data and flags so it can be reused for other purposes. + * + * @param buf Buffer to reset. + */ +void net_buf_reset(struct net_buf *buf); + +/** + * @def net_buf_reserve + * @brief Initialize buffer with the given headroom. + * + * The buffer is not expected to contain any data when this API is called. + * + * @param buf Buffer to initialize. + * @param reserve How much headroom to reserve. + */ +#define net_buf_reserve(buf, reserve) net_buf_simple_reserve(&(buf)->b, reserve) + +/** + * @brief Put a buffer into a list + * + * Put a buffer to the end of a list. If the buffer contains follow-up + * fragments this function will take care of inserting them as well + * into the list. + * + * @param list Which list to append the buffer to. + * @param buf Buffer. + */ +void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf); + +/** + * @brief Get a buffer from a list. + * + * Get buffer from a list. If the buffer had any fragments, these will + * automatically be recovered from the list as well and be placed to + * the buffer's fragment list. + * + * @param list Which list to take the buffer from. + * + * @return New buffer or NULL if the FIFO is empty. + */ +struct net_buf *net_buf_slist_get(sys_slist_t *list); + +/** + * @brief Decrements the reference count of a buffer. + * + * Decrements the reference count of a buffer and puts it back into the + * pool if the count reaches zero. + * + * @param buf A valid pointer on a buffer + */ +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +void net_buf_unref_debug(struct net_buf *buf, const char *func, int line); +#define net_buf_unref(_buf) \ + net_buf_unref_debug(_buf, __func__, __LINE__) +#else +void net_buf_unref(struct net_buf *buf); +#endif + +/** + * @brief Increment the reference count of a buffer. + * + * @param buf A valid pointer on a buffer + * + * @return the buffer newly referenced + */ +struct net_buf *net_buf_ref(struct net_buf *buf); + +/** + * @brief Get a pointer to the user data of a buffer. + * + * @param buf A valid pointer on a buffer + * + * @return Pointer to the user data of the buffer. + */ +static inline void *net_buf_user_data(struct net_buf *buf) +{ + return (void *)buf->user_data; +} + +/** + * @def net_buf_add + * @brief Prepare data to be added at the end of the buffer + * + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param len Number of bytes to increment the length with. + * + * @return The original tail of the buffer. + */ +#define net_buf_add(buf, len) net_buf_simple_add(&(buf)->b, len) + +/** + * @def net_buf_add_mem + * @brief Copy bytes from memory to the end of the buffer + * + * Copies the given number of bytes to the end of the buffer. Increments the + * data length of the buffer to account for more data at the end. + * + * @param buf Buffer to update. + * @param mem Location of data to be added. + * @param len Length of data to be added + * + * @return The original tail of the buffer. + */ +#define net_buf_add_mem(buf, mem, len) net_buf_simple_add_mem(&(buf)->b, mem, len) + +/** + * @def net_buf_add_u8 + * @brief Add (8-bit) byte at the end of the buffer + * + * Adds a byte at the end of the buffer. Increments the data length of + * the buffer to account for more data at the end. + * + * @param buf Buffer to update. + * @param val byte value to be added. + * + * @return Pointer to the value added + */ +#define net_buf_add_u8(buf, val) net_buf_simple_add_u8(&(buf)->b, val) + +/** + * @def net_buf_add_le16 + * @brief Add 16-bit value at the end of the buffer + * + * Adds 16-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 16-bit value to be added. + */ +#define net_buf_add_le16(buf, val) net_buf_simple_add_le16(&(buf)->b, val) + +/** + * @def net_buf_add_be16 + * @brief Add 16-bit value at the end of the buffer + * + * Adds 16-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 16-bit value to be added. + */ +#define net_buf_add_be16(buf, val) net_buf_simple_add_be16(&(buf)->b, val) + +/** + * @def net_buf_add_le24 + * @brief Add 24-bit value at the end of the buffer + * + * Adds 24-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 24-bit value to be added. + */ +#define net_buf_add_le24(buf, val) net_buf_simple_add_le24(&(buf)->b, val) + +/** + * @def net_buf_add_be24 + * @brief Add 24-bit value at the end of the buffer + * + * Adds 24-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 24-bit value to be added. + */ +#define net_buf_add_be24(buf, val) net_buf_simple_add_be24(&(buf)->b, val) + +/** + * @def net_buf_add_le32 + * @brief Add 32-bit value at the end of the buffer + * + * Adds 32-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 32-bit value to be added. + */ +#define net_buf_add_le32(buf, val) net_buf_simple_add_le32(&(buf)->b, val) + +/** + * @def net_buf_add_be32 + * @brief Add 32-bit value at the end of the buffer + * + * Adds 32-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 32-bit value to be added. + */ +#define net_buf_add_be32(buf, val) net_buf_simple_add_be32(&(buf)->b, val) + +/** + * @def net_buf_add_le48 + * @brief Add 48-bit value at the end of the buffer + * + * Adds 48-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 48-bit value to be added. + */ +#define net_buf_add_le48(buf, val) net_buf_simple_add_le48(&(buf)->b, val) + +/** + * @def net_buf_add_be48 + * @brief Add 48-bit value at the end of the buffer + * + * Adds 48-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 48-bit value to be added. + */ +#define net_buf_add_be48(buf, val) net_buf_simple_add_be48(&(buf)->b, val) + +/** + * @def net_buf_add_le64 + * @brief Add 64-bit value at the end of the buffer + * + * Adds 64-bit value in little endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 64-bit value to be added. + */ +#define net_buf_add_le64(buf, val) net_buf_simple_add_le64(&(buf)->b, val) + +/** + * @def net_buf_add_be64 + * @brief Add 64-bit value at the end of the buffer + * + * Adds 64-bit value in big endian format at the end of buffer. + * Increments the data length of a buffer to account for more data + * at the end. + * + * @param buf Buffer to update. + * @param val 64-bit value to be added. + */ +#define net_buf_add_be64(buf, val) net_buf_simple_add_be64(&(buf)->b, val) + +/** + * @def net_buf_push + * @brief Push data to the beginning of the buffer. + * + * Modifies the data pointer and buffer length to account for more data + * in the beginning of the buffer. + * + * @param buf Buffer to update. + * @param len Number of bytes to add to the beginning. + * + * @return The new beginning of the buffer data. + */ +#define net_buf_push(buf, len) net_buf_simple_push(&(buf)->b, len) + +/** + * @def net_buf_push_le16 + * @brief Push 16-bit value to the beginning of the buffer + * + * Adds 16-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 16-bit value to be pushed to the buffer. + */ +#define net_buf_push_le16(buf, val) net_buf_simple_push_le16(&(buf)->b, val) + +/** + * @def net_buf_push_be16 + * @brief Push 16-bit value to the beginning of the buffer + * + * Adds 16-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 16-bit value to be pushed to the buffer. + */ +#define net_buf_push_be16(buf, val) net_buf_simple_push_be16(&(buf)->b, val) + +/** + * @def net_buf_push_u8 + * @brief Push 8-bit value to the beginning of the buffer + * + * Adds 8-bit value the beginning of the buffer. + * + * @param buf Buffer to update. + * @param val 8-bit value to be pushed to the buffer. + */ +#define net_buf_push_u8(buf, val) net_buf_simple_push_u8(&(buf)->b, val) + +/** + * @def net_buf_push_le24 + * @brief Push 24-bit value to the beginning of the buffer + * + * Adds 24-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 24-bit value to be pushed to the buffer. + */ +#define net_buf_push_le24(buf, val) net_buf_simple_push_le24(&(buf)->b, val) + +/** + * @def net_buf_push_be24 + * @brief Push 24-bit value to the beginning of the buffer + * + * Adds 24-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 24-bit value to be pushed to the buffer. + */ +#define net_buf_push_be24(buf, val) net_buf_simple_push_be24(&(buf)->b, val) + +/** + * @def net_buf_push_le32 + * @brief Push 32-bit value to the beginning of the buffer + * + * Adds 32-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 32-bit value to be pushed to the buffer. + */ +#define net_buf_push_le32(buf, val) net_buf_simple_push_le32(&(buf)->b, val) + +/** + * @def net_buf_push_be32 + * @brief Push 32-bit value to the beginning of the buffer + * + * Adds 32-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 32-bit value to be pushed to the buffer. + */ +#define net_buf_push_be32(buf, val) net_buf_simple_push_be32(&(buf)->b, val) + +/** + * @def net_buf_push_le48 + * @brief Push 48-bit value to the beginning of the buffer + * + * Adds 48-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 48-bit value to be pushed to the buffer. + */ +#define net_buf_push_le48(buf, val) net_buf_simple_push_le48(&(buf)->b, val) + +/** + * @def net_buf_push_be48 + * @brief Push 48-bit value to the beginning of the buffer + * + * Adds 48-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 48-bit value to be pushed to the buffer. + */ +#define net_buf_push_be48(buf, val) net_buf_simple_push_be48(&(buf)->b, val) + +/** + * @def net_buf_push_le64 + * @brief Push 64-bit value to the beginning of the buffer + * + * Adds 64-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 64-bit value to be pushed to the buffer. + */ +#define net_buf_push_le64(buf, val) net_buf_simple_push_le64(&(buf)->b, val) + +/** + * @def net_buf_push_be64 + * @brief Push 64-bit value to the beginning of the buffer + * + * Adds 64-bit value in little endian format to the beginning of the + * buffer. + * + * @param buf Buffer to update. + * @param val 64-bit value to be pushed to the buffer. + */ +#define net_buf_push_be64(buf, val) net_buf_simple_push_be64(&(buf)->b, val) + +/** + * @def net_buf_pull + * @brief Remove data from the beginning of the buffer. + * + * Removes data from the beginning of the buffer by modifying the data + * pointer and buffer length. + * + * @param buf Buffer to update. + * @param len Number of bytes to remove. + * + * @return New beginning of the buffer data. + */ +#define net_buf_pull(buf, len) net_buf_simple_pull(&(buf)->b, len) + +/** + * @def net_buf_pull_mem + * @brief Remove data from the beginning of the buffer. + * + * Removes data from the beginning of the buffer by modifying the data + * pointer and buffer length. + * + * @param buf Buffer to update. + * @param len Number of bytes to remove. + * + * @return Pointer to the old beginning of the buffer data. + */ +#define net_buf_pull_mem(buf, len) net_buf_simple_pull_mem(&(buf)->b, len) + +/** + * @def net_buf_pull_u8 + * @brief Remove a 8-bit value from the beginning of the buffer + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 8-bit values. + * + * @param buf A valid pointer on a buffer. + * + * @return The 8-bit removed value + */ +#define net_buf_pull_u8(buf) net_buf_simple_pull_u8(&(buf)->b) + +/** + * @def net_buf_pull_le16 + * @brief Remove and convert 16 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 16-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 16-bit value converted from little endian to host endian. + */ +#define net_buf_pull_le16(buf) net_buf_simple_pull_le16(&(buf)->b) + +/** + * @def net_buf_pull_be16 + * @brief Remove and convert 16 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 16-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 16-bit value converted from big endian to host endian. + */ +#define net_buf_pull_be16(buf) net_buf_simple_pull_be16(&(buf)->b) + +/** + * @def net_buf_pull_le24 + * @brief Remove and convert 24 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 24-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 24-bit value converted from little endian to host endian. + */ +#define net_buf_pull_le24(buf) net_buf_simple_pull_le24(&(buf)->b) + +/** + * @def net_buf_pull_be24 + * @brief Remove and convert 24 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 24-bit big endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 24-bit value converted from big endian to host endian. + */ +#define net_buf_pull_be24(buf) net_buf_simple_pull_be24(&(buf)->b) + +/** + * @def net_buf_pull_le32 + * @brief Remove and convert 32 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 32-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 32-bit value converted from little endian to host endian. + */ +#define net_buf_pull_le32(buf) net_buf_simple_pull_le32(&(buf)->b) + +/** + * @def net_buf_pull_be32 + * @brief Remove and convert 32 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 32-bit big endian data. + * + * @param buf A valid pointer on a buffer + * + * @return 32-bit value converted from big endian to host endian. + */ +#define net_buf_pull_be32(buf) net_buf_simple_pull_be32(&(buf)->b) + +/** + * @def net_buf_pull_le48 + * @brief Remove and convert 48 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 48-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 48-bit value converted from little endian to host endian. + */ +#define net_buf_pull_le48(buf) net_buf_simple_pull_le48(&(buf)->b) + +/** + * @def net_buf_pull_be48 + * @brief Remove and convert 48 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 48-bit big endian data. + * + * @param buf A valid pointer on a buffer + * + * @return 48-bit value converted from big endian to host endian. + */ +#define net_buf_pull_be48(buf) net_buf_simple_pull_be48(&(buf)->b) + +/** + * @def net_buf_pull_le64 + * @brief Remove and convert 64 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 64-bit little endian data. + * + * @param buf A valid pointer on a buffer. + * + * @return 64-bit value converted from little endian to host endian. + */ +#define net_buf_pull_le64(buf) net_buf_simple_pull_le64(&(buf)->b) + +/** + * @def net_buf_pull_be64 + * @brief Remove and convert 64 bits from the beginning of the buffer. + * + * Same idea as with net_buf_pull(), but a helper for operating on + * 64-bit big endian data. + * + * @param buf A valid pointer on a buffer + * + * @return 64-bit value converted from big endian to host endian. + */ +#define net_buf_pull_be64(buf) net_buf_simple_pull_be64(&(buf)->b) + +/** + * @def net_buf_tailroom + * @brief Check buffer tailroom. + * + * Check how much free space there is at the end of the buffer. + * + * @param buf A valid pointer on a buffer + * + * @return Number of bytes available at the end of the buffer. + */ +#define net_buf_tailroom(buf) net_buf_simple_tailroom(&(buf)->b) + +/** + * @def net_buf_headroom + * @brief Check buffer headroom. + * + * Check how much free space there is in the beginning of the buffer. + * + * buf A valid pointer on a buffer + * + * @return Number of bytes available in the beginning of the buffer. + */ +#define net_buf_headroom(buf) net_buf_simple_headroom(&(buf)->b) + +/** + * @def net_buf_tail + * @brief Get the tail pointer for a buffer. + * + * Get a pointer to the end of the data in a buffer. + * + * @param buf Buffer. + * + * @return Tail pointer for the buffer. + */ +#define net_buf_tail(buf) net_buf_simple_tail(&(buf)->b) + +/** + * @brief Find the last fragment in the fragment list. + * + * @return Pointer to last fragment in the list. + */ +struct net_buf *net_buf_frag_last(struct net_buf *frags); + +/** + * @brief Insert a new fragment to a chain of bufs. + * + * Insert a new fragment into the buffer fragments list after the parent. + * + * Note: This function takes ownership of the fragment reference so the + * caller is not required to unref. + * + * @param parent Parent buffer/fragment. + * @param frag Fragment to insert. + */ +void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag); + +/** + * @brief Add a new fragment to the end of a chain of bufs. + * + * Append a new fragment into the buffer fragments list. + * + * Note: This function takes ownership of the fragment reference so the + * caller is not required to unref. + * + * @param head Head of the fragment chain. + * @param frag Fragment to add. + * + * @return New head of the fragment chain. Either head (if head + * was non-NULL) or frag (if head was NULL). + */ +struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag); + +/** + * @brief Delete existing fragment from a chain of bufs. + * + * @param parent Parent buffer/fragment, or NULL if there is no parent. + * @param frag Fragment to delete. + * + * @return Pointer to the buffer following the fragment, or NULL if it + * had no further fragments. + */ +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, + struct net_buf *frag, + const char *func, int line); +#define net_buf_frag_del(_parent, _frag) \ + net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__) +#else +struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag); +#endif + +/** + * @brief Copy bytes from net_buf chain starting at offset to linear buffer + * + * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a + * offset in it, to a linear buffer @a dst. Return number of bytes actually + * copied, which may be less than requested, if net_buf chain doesn't have + * enough data, or destination buffer is too small. + * + * @param dst Destination buffer + * @param dst_len Destination buffer length + * @param src Source net_buf chain + * @param offset Starting offset to copy from + * @param len Number of bytes to copy + * @return number of bytes actually copied + */ +size_t net_buf_linearize(void *dst, size_t dst_len, + struct net_buf *src, size_t offset, size_t len); + +/** + * @typedef net_buf_allocator_cb + * @brief Network buffer allocator callback. + * + * @details The allocator callback is called when net_buf_append_bytes + * needs to allocate a new net_buf. + * + * @param timeout Affects the action taken should the net buf pool be empty. + * If K_NO_WAIT, then return immediately. If K_FOREVER, then + * wait as long as necessary. Otherwise, wait up to the specified + * number of milliseconds before timing out. + * @param user_data The user data given in net_buf_append_bytes call. + * @return pointer to allocated net_buf or NULL on error. + */ +typedef struct net_buf *(*net_buf_allocator_cb)(s32_t timeout, void *user_data); + +/** + * @brief Append data to a list of net_buf + * + * @details Append data to a net_buf. If there is not enough space in the + * net_buf then more net_buf will be added, unless there are no free net_buf + * and timeout occurs. + * + * @param buf Network buffer. + * @param len Total length of input data + * @param value Data to be added + * @param timeout Timeout is passed to the net_buf allocator callback. + * @param allocate_cb When a new net_buf is required, use this callback. + * @param user_data A user data pointer to be supplied to the allocate_cb. + * This pointer is can be anything from a mem_pool or a net_pkt, the + * logic is left up to the allocate_cb function. + * + * @return Length of data actually added. This may be less than input + * length if other timeout than K_FOREVER was used, and there + * were no free fragments in a pool to accommodate all data. + */ +size_t net_buf_append_bytes(struct net_buf *buf, size_t len, + const void *value, s32_t timeout, + net_buf_allocator_cb allocate_cb, void *user_data); + +/** + * @brief Skip N number of bytes in a net_buf + * + * @details Skip N number of bytes starting from fragment's offset. If the total + * length of data is placed in multiple fragments, this function will skip from + * all fragments until it reaches N number of bytes. Any fully skipped buffers + * are removed from the net_buf list. + * + * @param buf Network buffer. + * @param len Total length of data to be skipped. + * + * @return Pointer to the fragment or + * NULL and pos is 0 after successful skip, + * NULL and pos is 0xffff otherwise. + */ +static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len) +{ + while (buf && len--) { + net_buf_pull_u8(buf); + if (!buf->len) { + buf = net_buf_frag_del(NULL, buf); + } + } + + return buf; +} + +/** + * @brief Calculate amount of bytes stored in fragments. + * + * Calculates the total amount of data stored in the given buffer and the + * fragments linked to it. + * + * @param buf Buffer to start off with. + * + * @return Number of bytes in the buffer and its fragments. + */ +static inline size_t net_buf_frags_len(struct net_buf *buf) +{ + size_t bytes = 0; + + while (buf) { + bytes += buf->len; + buf = buf->frags; + } + + return bytes; +} + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_BUF_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_byteorder.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_byteorder.h new file mode 100644 index 000000000..f570bd6e0 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_byteorder.h @@ -0,0 +1,599 @@ +/* + * Copyright (c) 2015-2016, Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_BYTEORDER_H_ +#define _BLE_MESH_BYTEORDER_H_ + +#include "mesh_types.h" +#include "mesh_trace.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Internal helpers only used by the sys_* APIs further below */ +#ifndef __bswap_16 +#define __bswap_16(x) ((u16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) +#endif + +#ifndef __bswap_24 +#define __bswap_24(x) ((u32_t) ((((x) >> 16) & 0xff) | \ + (((x)) & 0xff00) | \ + (((x) & 0xff) << 16))) +#endif + +#ifndef __bswap_32 +#define __bswap_32(x) ((u32_t) ((((x) >> 24) & 0xff) | \ + (((x) >> 8) & 0xff00) | \ + (((x) & 0xff00) << 8) | \ + (((x) & 0xff) << 24))) +#endif + +#ifndef __bswap_48 +#define __bswap_48(x) ((u64_t) ((((x) >> 40) & 0xff) | \ + (((x) >> 24) & 0xff00) | \ + (((x) >> 8) & 0xff0000) | \ + (((x) & 0xff0000) << 8) | \ + (((x) & 0xff00) << 24) | \ + (((x) & 0xff) << 40))) +#endif + +#ifndef __bswap_64 +#define __bswap_64(x) ((u64_t) ((((x) >> 56) & 0xff) | \ + (((x) >> 40) & 0xff00) | \ + (((x) >> 24) & 0xff0000) | \ + (((x) >> 8) & 0xff000000) | \ + (((x) & 0xff000000) << 8) | \ + (((x) & 0xff0000) << 24) | \ + (((x) & 0xff00) << 40) | \ + (((x) & 0xff) << 56))) +#endif + +/** @def sys_le16_to_cpu + * @brief Convert 16-bit integer from little-endian to host endianness. + * + * @param val 16-bit integer in little-endian format. + * + * @return 16-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le16 + * @brief Convert 16-bit integer from host endianness to little-endian. + * + * @param val 16-bit integer in host endianness. + * + * @return 16-bit integer in little-endian format. + */ + +/** @def sys_le24_to_cpu + * @brief Convert 24-bit integer from little-endian to host endianness. + * + * @param val 24-bit integer in little-endian format. + * + * @return 24-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le24 + * @brief Convert 24-bit integer from host endianness to little-endian. + * + * @param val 24-bit integer in host endianness. + * + * @return 24-bit integer in little-endian format. + */ + +/** @def sys_le32_to_cpu + * @brief Convert 32-bit integer from little-endian to host endianness. + * + * @param val 32-bit integer in little-endian format. + * + * @return 32-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le32 + * @brief Convert 32-bit integer from host endianness to little-endian. + * + * @param val 32-bit integer in host endianness. + * + * @return 32-bit integer in little-endian format. + */ + +/** @def sys_le48_to_cpu + * @brief Convert 48-bit integer from little-endian to host endianness. + * + * @param val 48-bit integer in little-endian format. + * + * @return 48-bit integer in host endianness. + */ + +/** @def sys_cpu_to_le48 + * @brief Convert 48-bit integer from host endianness to little-endian. + * + * @param val 48-bit integer in host endianness. + * + * @return 48-bit integer in little-endian format. + */ + +/** @def sys_be16_to_cpu + * @brief Convert 16-bit integer from big-endian to host endianness. + * + * @param val 16-bit integer in big-endian format. + * + * @return 16-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be16 + * @brief Convert 16-bit integer from host endianness to big-endian. + * + * @param val 16-bit integer in host endianness. + * + * @return 16-bit integer in big-endian format. + */ + +/** @def sys_be24_to_cpu + * @brief Convert 24-bit integer from big-endian to host endianness. + * + * @param val 24-bit integer in big-endian format. + * + * @return 24-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be24 + * @brief Convert 24-bit integer from host endianness to big-endian. + * + * @param val 24-bit integer in host endianness. + * + * @return 24-bit integer in big-endian format. + */ + +/** @def sys_be32_to_cpu + * @brief Convert 32-bit integer from big-endian to host endianness. + * + * @param val 32-bit integer in big-endian format. + * + * @return 32-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be32 + * @brief Convert 32-bit integer from host endianness to big-endian. + * + * @param val 32-bit integer in host endianness. + * + * @return 32-bit integer in big-endian format. + */ + +/** @def sys_be48_to_cpu + * @brief Convert 48-bit integer from big-endian to host endianness. + * + * @param val 48-bit integer in big-endian format. + * + * @return 48-bit integer in host endianness. + */ + +/** @def sys_cpu_to_be48 + * @brief Convert 48-bit integer from host endianness to big-endian. + * + * @param val 48-bit integer in host endianness. + * + * @return 48-bit integer in big-endian format. + */ + +#ifndef sys_le16_to_cpu +#define sys_le16_to_cpu(val) (val) +#endif +#ifndef sys_cpu_to_le16 +#define sys_cpu_to_le16(val) (val) +#endif +#ifndef sys_le24_to_cpu +#define sys_le24_to_cpu(val) (val) +#endif +#ifndef sys_cpu_to_le24 +#define sys_cpu_to_le24(val) (val) +#endif +#ifndef sys_le32_to_cpu +#define sys_le32_to_cpu(val) (val) +#endif +#ifndef sys_cpu_to_le32 +#define sys_cpu_to_le32(val) (val) +#endif +#ifndef sys_le48_to_cpu +#define sys_le48_to_cpu(val) (val) +#endif +#ifndef sys_cpu_to_le48 +#define sys_cpu_to_le48(val) (val) +#endif +#ifndef sys_le64_to_cpu +#define sys_le64_to_cpu(val) (val) +#endif +#ifndef sys_cpu_to_le64 +#define sys_cpu_to_le64(val) (val) +#endif +#ifndef sys_be16_to_cpu +#define sys_be16_to_cpu(val) __bswap_16(val) +#endif +#ifndef sys_cpu_to_be16 +#define sys_cpu_to_be16(val) __bswap_16(val) +#endif +#ifndef sys_be24_to_cpu +#define sys_be24_to_cpu(val) __bswap_24(val) +#endif +#ifndef sys_cpu_to_be24 +#define sys_cpu_to_be24(val) __bswap_24(val) +#endif +#ifndef sys_be32_to_cpu +#define sys_be32_to_cpu(val) __bswap_32(val) +#endif +#ifndef sys_cpu_to_be32 +#define sys_cpu_to_be32(val) __bswap_32(val) +#endif +#ifndef sys_be48_to_cpu +#define sys_be48_to_cpu(val) __bswap_48(val) +#endif +#ifndef sys_cpu_to_be48 +#define sys_cpu_to_be48(val) __bswap_48(val) +#endif +#ifndef sys_be64_to_cpu +#define sys_be64_to_cpu(val) __bswap_64(val) +#endif +#ifndef sys_cpu_to_be64 +#define sys_cpu_to_be64(val) __bswap_64(val) +#endif + +/** + * @brief Put a 16-bit integer as big-endian to arbitrary location. + * + * Put a 16-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 16-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be16(u16_t val, u8_t dst[2]) +{ + dst[0] = val >> 8; + dst[1] = val; +} + +/** + * @brief Put a 24-bit integer as big-endian to arbitrary location. + * + * Put a 24-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 24-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be24(u32_t val, u8_t dst[3]) +{ + dst[0] = val >> 16; + sys_put_be16(val, &dst[1]); +} + +/** + * @brief Put a 32-bit integer as big-endian to arbitrary location. + * + * Put a 32-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 32-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be32(u32_t val, u8_t dst[4]) +{ + sys_put_be16(val >> 16, dst); + sys_put_be16(val, &dst[2]); +} + +/** + * @brief Put a 48-bit integer as big-endian to arbitrary location. + * + * Put a 48-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 48-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be48(u64_t val, u8_t dst[6]) +{ + sys_put_be16(val >> 32, dst); + sys_put_be32(val, &dst[2]); +} + +/** + * @brief Put a 64-bit integer as big-endian to arbitrary location. + * + * Put a 64-bit integer, originally in host endianness, to a + * potentially unaligned memory location in big-endian format. + * + * @param val 64-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_be64(u64_t val, u8_t dst[8]) +{ + sys_put_be32(val >> 32, dst); + sys_put_be32(val, &dst[4]); +} + +/** + * @brief Put a 16-bit integer as little-endian to arbitrary location. + * + * Put a 16-bit integer, originally in host endianness, to a + * potentially unaligned memory location in little-endian format. + * + * @param val 16-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le16(u16_t val, u8_t dst[2]) +{ + dst[0] = val; + dst[1] = val >> 8; +} + +/** + * @brief Put a 24-bit integer as little-endian to arbitrary location. + * + * Put a 24-bit integer, originally in host endianness, to a + * potentially unaligned memory location in littel-endian format. + * + * @param val 24-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le24(u32_t val, u8_t dst[3]) +{ + sys_put_le16(val, dst); + dst[2] = val >> 16; +} + +/** + * @brief Put a 32-bit integer as little-endian to arbitrary location. + * + * Put a 32-bit integer, originally in host endianness, to a + * potentially unaligned memory location in little-endian format. + * + * @param val 32-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le32(u32_t val, u8_t dst[4]) +{ + sys_put_le16(val, dst); + sys_put_le16(val >> 16, &dst[2]); +} + +/** + * @brief Put a 48-bit integer as little-endian to arbitrary location. + * + * Put a 48-bit integer, originally in host endianness, to a + * potentially unaligned memory location in little-endian format. + * + * @param val 48-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le48(u64_t val, u8_t dst[6]) +{ + sys_put_le32(val, dst); + sys_put_le16(val >> 32, &dst[4]); +} + +/** + * @brief Put a 64-bit integer as little-endian to arbitrary location. + * + * Put a 64-bit integer, originally in host endianness, to a + * potentially unaligned memory location in little-endian format. + * + * @param val 64-bit integer in host endianness. + * @param dst Destination memory address to store the result. + */ +static inline void sys_put_le64(u64_t val, u8_t dst[8]) +{ + sys_put_le32(val, dst); + sys_put_le32(val >> 32, &dst[4]); +} + +/** + * @brief Get a 16-bit integer stored in big-endian format. + * + * Get a 16-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 16-bit integer to get. + * + * @return 16-bit integer in host endianness. + */ +static inline u16_t sys_get_be16(const u8_t src[2]) +{ + return ((u16_t)src[0] << 8) | src[1]; +} + +/** + * @brief Get a 24-bit integer stored in big-endian format. + * + * Get a 24-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 24-bit integer to get. + * + * @return 24-bit integer in host endianness. + */ +static inline u32_t sys_get_be24(const u8_t src[3]) +{ + return ((u32_t)src[0] << 16) | sys_get_be16(&src[1]); +} + +/** + * @brief Get a 32-bit integer stored in big-endian format. + * + * Get a 32-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 32-bit integer to get. + * + * @return 32-bit integer in host endianness. + */ +static inline u32_t sys_get_be32(const u8_t src[4]) +{ + return ((u32_t)sys_get_be16(&src[0]) << 16) | sys_get_be16(&src[2]); +} + +/** + * @brief Get a 48-bit integer stored in big-endian format. + * + * Get a 48-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 48-bit integer to get. + * + * @return 48-bit integer in host endianness. + */ +static inline u64_t sys_get_be48(const u8_t src[6]) +{ + return ((u64_t)sys_get_be32(&src[0]) << 32) | sys_get_be16(&src[4]); +} + +/** + * @brief Get a 64-bit integer stored in big-endian format. + * + * Get a 64-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 64-bit integer to get. + * + * @return 64-bit integer in host endianness. + */ +static inline u64_t sys_get_be64(const u8_t src[8]) +{ + return ((u64_t)sys_get_be32(&src[0]) << 32) | sys_get_be32(&src[4]); +} + +/** + * @brief Get a 16-bit integer stored in little-endian format. + * + * Get a 16-bit integer, stored in little-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the little-endian 16-bit integer to get. + * + * @return 16-bit integer in host endianness. + */ +static inline u16_t sys_get_le16(const u8_t src[2]) +{ + return ((u16_t)src[1] << 8) | src[0]; +} + +/** + * @brief Get a 24-bit integer stored in big-endian format. + * + * Get a 24-bit integer, stored in big-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the big-endian 24-bit integer to get. + * + * @return 24-bit integer in host endianness. + */ +static inline u32_t sys_get_le24(const u8_t src[3]) +{ + return ((u32_t)src[2] << 16) | sys_get_le16(&src[0]); +} + +/** + * @brief Get a 32-bit integer stored in little-endian format. + * + * Get a 32-bit integer, stored in little-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the little-endian 32-bit integer to get. + * + * @return 32-bit integer in host endianness. + */ +static inline u32_t sys_get_le32(const u8_t src[4]) +{ + return ((u32_t)sys_get_le16(&src[2]) << 16) | sys_get_le16(&src[0]); +} + +/** + * @brief Get a 48-bit integer stored in little-endian format. + * + * Get a 48-bit integer, stored in little-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the little-endian 48-bit integer to get. + * + * @return 48-bit integer in host endianness. + */ +static inline u64_t sys_get_le48(const u8_t src[6]) +{ + return ((u64_t)sys_get_le32(&src[2]) << 32) | sys_get_le16(&src[0]); +} + +/** + * @brief Get a 64-bit integer stored in little-endian format. + * + * Get a 64-bit integer, stored in little-endian format in a potentially + * unaligned memory location, and convert it to the host endianness. + * + * @param src Location of the little-endian 64-bit integer to get. + * + * @return 64-bit integer in host endianness. + */ +static inline u64_t sys_get_le64(const u8_t src[8]) +{ + return ((u64_t)sys_get_le32(&src[4]) << 32) | sys_get_le32(&src[0]); +} + +/** + * @brief Swap one buffer content into another + * + * Copy the content of src buffer into dst buffer in reversed order, + * i.e.: src[n] will be put in dst[end-n] + * Where n is an index and 'end' the last index in both arrays. + * The 2 memory pointers must be pointing to different areas, and have + * a minimum size of given length. + * + * @param dst A valid pointer on a memory area where to copy the data in + * @param src A valid pointer on a memory area where to copy the data from + * @param length Size of both dst and src memory areas + */ +static inline void sys_memcpy_swap(void *dst, const void *src, size_t length) +{ + u8_t *pdst = (u8_t *)dst; + const u8_t *psrc = (const u8_t *)src; + + __ASSERT(((psrc < pdst && (psrc + length) <= pdst) || + (psrc > pdst && (pdst + length) <= psrc)), + "Source and destination buffers must not overlap"); + + psrc += length - 1; + + for (; length > 0; length--) { + *pdst++ = *psrc--; + } +} + +/** + * @brief Swap buffer content + * + * In-place memory swap, where final content will be reversed. + * I.e.: buf[n] will be put in buf[end-n] + * Where n is an index and 'end' the last index of buf. + * + * @param buf A valid pointer on a memory area to swap + * @param length Size of buf memory area + */ +static inline void sys_mem_swap(void *buf, size_t length) +{ + size_t i; + + for (i = 0; i < (length / 2); i++) { + u8_t tmp = ((u8_t *)buf)[i]; + + ((u8_t *)buf)[i] = ((u8_t *)buf)[length - 1 - i]; + ((u8_t *)buf)[length - 1 - i] = tmp; + } +} + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_BYTEORDER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h new file mode 100644 index 000000000..ad968dd0e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h @@ -0,0 +1,82 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Model Common APIs. + */ + +#ifndef _BLE_MESH_COMMON_H_ +#define _BLE_MESH_COMMON_H_ + +#include +#include + +#include "esp_heap_caps.h" + +#include "mesh_byteorder.h" +#include "mesh_ffs.h" +#include "mesh_trace.h" +#include "mesh_mutex.h" +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST +#define bt_mesh_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL) +#define bt_mesh_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL) +#else +#define bt_mesh_malloc(size) malloc((size)) +#define bt_mesh_calloc(size) calloc(1, (size)) +#endif /* CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST */ +#define bt_mesh_free(p) free((p)) + +/** + * @brief This function allocates memory to store outgoing message. + * + * @param[in] size: Length of memory allocated to store message value + * + * @return NULL-fail, pointer of a net_buf_simple structure-success + */ +struct net_buf_simple *bt_mesh_alloc_buf(u16_t size); + +/** + * @brief This function releases the memory allocated for the outgoing message. + * + * @param[in] buf: Pointer to the net_buf_simple structure to be freed + * + * @return none + */ +void bt_mesh_free_buf(struct net_buf_simple *buf); + +/** + * @brief This function gets device role for stack internal use. + * + * @Note Currently Provisioner only support client models, Node supports + * client models and server models. Hence if srv_send is set to be + * TRUE, then role NODE will be returned. + * + * @param[in] model: Pointer to the model structure + * @param[in] srv_send: Indicate if the message is sent by a server model + * + * @return 0 - Node, 1 - Provisioner + */ +u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_COMMON_H_ */ \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_compiler.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_compiler.h new file mode 100644 index 000000000..cab93fe39 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_compiler.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010-2014,2017 Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_COMPILER_H_ +#define _BLE_MESH_COMPILER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ___in_section(a, b, c) + +#define __in_section(a, b, c) ___in_section(a, b, c) + +#define __in_section_unique(seg) ___in_section(seg, __FILE__, __COUNTER__) + +#ifndef __packed +#define __packed __attribute__((__packed__)) +#endif + +#ifndef __aligned +#define __aligned(x) __attribute__((__aligned__(x))) +#endif + +#ifndef __used +#define __used __attribute__((__used__)) +#endif + +#ifndef ARG_UNUSED +#define ARG_UNUSED(x) (void)(x) +#endif + +#ifndef popcount +#define popcount(x) __builtin_popcount(x) +#endif + +#ifndef ALWAYS_INLINE +#define ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + + +/* + * This is meant to be used in conjunction with __in_section() and similar + * where scattered structure instances are concatened together by the linker + * and walked by the code at run time just like a contiguous array of such + * structures. + * + * Assemblers and linkers may insert alignment padding by default whose + * size is larger than the natural alignment for those structures when + * gathering various section segments together, messing up the array walk. + * To prevent this, we need to provide an explicit alignment not to rely + * on the default that might just work by luck. + * + * Alignment statements in linker scripts are not sufficient as + * the assembler may add padding by itself to each segment when switching + * between sections within the same file even if it merges many such segments + * into a single section in the end. + */ +#ifndef Z_DECL_ALIGN +#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type +#endif + +/* + * Convenience helper combining __in_section() and Z_DECL_ALIGN(). + * The section name is the struct type prepended with an underscore. + * The subsection is "static" and the subsubsection is the variable name. + */ +#ifndef Z_STRUCT_SECTION_ITERABLE +#define Z_STRUCT_SECTION_ITERABLE(struct_type, name) \ + Z_DECL_ALIGN(struct struct_type) name +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_COMPILER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_dlist.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_dlist.h new file mode 100644 index 000000000..31eef746e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_dlist.h @@ -0,0 +1,496 @@ +/* + * Copyright (c) 2013-2015 Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Doubly-linked list implementation + * + * Doubly-linked list implementation using inline macros/functions. + * This API is not thread safe, and thus if a list is used across threads, + * calls to functions must be protected with synchronization primitives. + * + * The lists are expected to be initialized such that both the head and tail + * pointers point to the list itself. Initializing the lists in such a fashion + * simplifies the adding and removing of nodes to/from the list. + */ + +#ifndef _BLE_MESH_DLIST_H_ +#define _BLE_MESH_DLIST_H_ + +#include +#include "mesh_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +struct _dnode { + union { + struct _dnode *head; /* ptr to head of list (sys_dlist_t) */ + struct _dnode *next; /* ptr to next node (sys_dnode_t) */ + }; + union { + struct _dnode *tail; /* ptr to tail of list (sys_dlist_t) */ + struct _dnode *prev; /* ptr to previous node (sys_dnode_t) */ + }; +}; + +typedef struct _dnode sys_dlist_t; +typedef struct _dnode sys_dnode_t; + +/** + * @brief Provide the primitive to iterate on a list + * Note: the loop is unsafe and thus __dn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_DLIST_FOR_EACH_NODE(l, n) { + * + * } + * + * This and other SYS_DLIST_*() macros are not thread safe. + * + * @param __dl A pointer on a sys_dlist_t to iterate on + * @param __dn A sys_dnode_t pointer to peek each node of the list + */ +#define SYS_DLIST_FOR_EACH_NODE(__dl, __dn) \ + for (__dn = sys_dlist_peek_head(__dl); __dn; \ + __dn = sys_dlist_peek_next(__dl, __dn)) + +/** + * @brief Provide the primitive to iterate on a list, from a node in the list + * Note: the loop is unsafe and thus __dn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_DLIST_ITERATE_FROM_NODE(l, n) { + * + * } + * + * Like SYS_DLIST_FOR_EACH_NODE(), but __dn already contains a node in the list + * where to start searching for the next entry from. If NULL, it starts from + * the head. + * + * This and other SYS_DLIST_*() macros are not thread safe. + * + * @param __dl A pointer on a sys_dlist_t to iterate on + * @param __dn A sys_dnode_t pointer to peek each node of the list; + * it contains the starting node, or NULL to start from the head + */ +#define SYS_DLIST_ITERATE_FROM_NODE(__dl, __dn) \ + for (__dn = __dn ? sys_dlist_peek_next_no_check(__dl, __dn) \ + : sys_dlist_peek_head(__dl); \ + __dn; \ + __dn = sys_dlist_peek_next(__dl, __dn)) + +/** + * @brief Provide the primitive to safely iterate on a list + * Note: __dn can be removed, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_DLIST_FOR_EACH_NODE_SAFE(l, n, s) { + * + * } + * + * This and other SYS_DLIST_*() macros are not thread safe. + * + * @param __dl A pointer on a sys_dlist_t to iterate on + * @param __dn A sys_dnode_t pointer to peek each node of the list + * @param __dns A sys_dnode_t pointer for the loop to run safely + */ +#define SYS_DLIST_FOR_EACH_NODE_SAFE(__dl, __dn, __dns) \ + for (__dn = sys_dlist_peek_head(__dl), \ + __dns = sys_dlist_peek_next(__dl, __dn); \ + __dn; __dn = __dns, \ + __dns = sys_dlist_peek_next(__dl, __dn)) + +/* + * @brief Provide the primitive to resolve the container of a list node + * Note: it is safe to use with NULL pointer nodes + * + * @param __dn A pointer on a sys_dnode_t to get its container + * @param __cn Container struct type pointer + * @param __n The field name of sys_dnode_t within the container struct + */ +#define SYS_DLIST_CONTAINER(__dn, __cn, __n) \ + (__dn ? CONTAINER_OF(__dn, __typeof__(*__cn), __n) : NULL) +/* + * @brief Provide the primitive to peek container of the list head + * + * @param __dl A pointer on a sys_dlist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_dnode_t within the container struct + */ +#define SYS_DLIST_PEEK_HEAD_CONTAINER(__dl, __cn, __n) \ + SYS_DLIST_CONTAINER(sys_dlist_peek_head(__dl), __cn, __n) + +/* + * @brief Provide the primitive to peek the next container + * + * @param __dl A pointer on a sys_dlist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_dnode_t within the container struct + */ +#define SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n) \ + ((__cn) ? SYS_DLIST_CONTAINER(sys_dlist_peek_next(__dl, &(__cn->__n)), \ + __cn, __n) : NULL) + +/** + * @brief Provide the primitive to iterate on a list under a container + * Note: the loop is unsafe and thus __cn should not be detached + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_DLIST_FOR_EACH_CONTAINER(l, c, n) { + * + * } + * + * @param __dl A pointer on a sys_dlist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __n The field name of sys_dnode_t within the container struct + */ +#define SYS_DLIST_FOR_EACH_CONTAINER(__dl, __cn, __n) \ + for (__cn = SYS_DLIST_PEEK_HEAD_CONTAINER(__dl, __cn, __n); __cn; \ + __cn = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n)) + +/** + * @brief Provide the primitive to safely iterate on a list under a container + * Note: __cn can be detached, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_DLIST_FOR_EACH_CONTAINER_SAFE(l, c, cn, n) { + * + * } + * + * @param __dl A pointer on a sys_dlist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __cns A pointer for the loop to run safely + * @param __n The field name of sys_dnode_t within the container struct + */ +#define SYS_DLIST_FOR_EACH_CONTAINER_SAFE(__dl, __cn, __cns, __n) \ + for (__cn = SYS_DLIST_PEEK_HEAD_CONTAINER(__dl, __cn, __n), \ + __cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n); __cn; \ + __cn = __cns, \ + __cns = SYS_DLIST_PEEK_NEXT_CONTAINER(__dl, __cn, __n)) + +/** + * @brief initialize list + * + * @param list the doubly-linked list + * + * @return N/A + */ + +static inline void sys_dlist_init(sys_dlist_t *list) +{ + list->head = (sys_dnode_t *)list; + list->tail = (sys_dnode_t *)list; +} + +#define SYS_DLIST_STATIC_INIT(ptr_to_list) {{(ptr_to_list)}, {(ptr_to_list)}} + +/** + * @brief check if a node is the list's head + * + * @param list the doubly-linked list to operate on + * @param node the node to check + * + * @return 1 if node is the head, 0 otherwise + */ + +static inline int sys_dlist_is_head(sys_dlist_t *list, sys_dnode_t *node) +{ + return list->head == node; +} + +/** + * @brief check if a node is the list's tail + * + * @param list the doubly-linked list to operate on + * @param node the node to check + * + * @return 1 if node is the tail, 0 otherwise + */ + +static inline int sys_dlist_is_tail(sys_dlist_t *list, sys_dnode_t *node) +{ + return list->tail == node; +} + +/** + * @brief check if the list is empty + * + * @param list the doubly-linked list to operate on + * + * @return 1 if empty, 0 otherwise + */ + +static inline int sys_dlist_is_empty(sys_dlist_t *list) +{ + return list->head == list; +} + +/** + * @brief check if more than one node present + * + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * + * @return 1 if multiple nodes, 0 otherwise + */ + +static inline int sys_dlist_has_multiple_nodes(sys_dlist_t *list) +{ + return list->head != list->tail; +} + +/** + * @brief get a reference to the head item in the list + * + * @param list the doubly-linked list to operate on + * + * @return a pointer to the head element, NULL if list is empty + */ + +static inline sys_dnode_t *sys_dlist_peek_head(sys_dlist_t *list) +{ + return sys_dlist_is_empty(list) ? NULL : list->head; +} + +/** + * @brief get a reference to the head item in the list + * + * The list must be known to be non-empty. + * + * @param list the doubly-linked list to operate on + * + * @return a pointer to the head element + */ + +static inline sys_dnode_t *sys_dlist_peek_head_not_empty(sys_dlist_t *list) +{ + return list->head; +} + +/** + * @brief get a reference to the next item in the list, node is not NULL + * + * Faster than sys_dlist_peek_next() if node is known not to be NULL. + * + * @param list the doubly-linked list to operate on + * @param node the node from which to get the next element in the list + * + * @return a pointer to the next element from a node, NULL if node is the tail + */ + +static inline sys_dnode_t *sys_dlist_peek_next_no_check(sys_dlist_t *list, + sys_dnode_t *node) +{ + return (node == list->tail) ? NULL : node->next; +} + +/** + * @brief get a reference to the next item in the list + * + * @param list the doubly-linked list to operate on + * @param node the node from which to get the next element in the list + * + * @return a pointer to the next element from a node, NULL if node is the tail + * or NULL (when node comes from reading the head of an empty list). + */ + +static inline sys_dnode_t *sys_dlist_peek_next(sys_dlist_t *list, + sys_dnode_t *node) +{ + return node ? sys_dlist_peek_next_no_check(list, node) : NULL; +} + +/** + * @brief get a reference to the tail item in the list + * + * @param list the doubly-linked list to operate on + * + * @return a pointer to the tail element, NULL if list is empty + */ + +static inline sys_dnode_t *sys_dlist_peek_tail(sys_dlist_t *list) +{ + return sys_dlist_is_empty(list) ? NULL : list->tail; +} + +/** + * @brief add node to tail of list + * + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * @param node the element to append + * + * @return N/A + */ + +static inline void sys_dlist_append(sys_dlist_t *list, sys_dnode_t *node) +{ + node->next = list; + node->prev = list->tail; + + list->tail->next = node; + list->tail = node; +} + +/** + * @brief add node to head of list + * + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * @param node the element to append + * + * @return N/A + */ + +static inline void sys_dlist_prepend(sys_dlist_t *list, sys_dnode_t *node) +{ + node->next = list->head; + node->prev = list; + + list->head->prev = node; + list->head = node; +} + +/** + * @brief insert node after a node + * + * Insert a node after a specified node in a list. + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * @param insert_point the insert point in the list: if NULL, insert at head + * @param node the element to append + * + * @return N/A + */ + +static inline void sys_dlist_insert_after(sys_dlist_t *list, + sys_dnode_t *insert_point, sys_dnode_t *node) +{ + if (!insert_point) { + sys_dlist_prepend(list, node); + } else { + node->next = insert_point->next; + node->prev = insert_point; + insert_point->next->prev = node; + insert_point->next = node; + } +} + +/** + * @brief insert node before a node + * + * Insert a node before a specified node in a list. + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * @param insert_point the insert point in the list: if NULL, insert at tail + * @param node the element to insert + * + * @return N/A + */ + +static inline void sys_dlist_insert_before(sys_dlist_t *list, + sys_dnode_t *insert_point, sys_dnode_t *node) +{ + if (!insert_point) { + sys_dlist_append(list, node); + } else { + node->prev = insert_point->prev; + node->next = insert_point; + insert_point->prev->next = node; + insert_point->prev = node; + } +} + +/** + * @brief insert node at position + * + * Insert a node in a location depending on a external condition. The cond() + * function checks if the node is to be inserted _before_ the current node + * against which it is checked. + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * @param node the element to insert + * @param cond a function that determines if the current node is the correct + * insert point + * @param data parameter to cond() + * + * @return N/A + */ + +static inline void sys_dlist_insert_at(sys_dlist_t *list, sys_dnode_t *node, + int (*cond)(sys_dnode_t *, void *), void *data) +{ + if (sys_dlist_is_empty(list)) { + sys_dlist_append(list, node); + } else { + sys_dnode_t *pos = sys_dlist_peek_head(list); + + while (pos && !cond(pos, data)) { + pos = sys_dlist_peek_next(list, pos); + } + sys_dlist_insert_before(list, pos, node); + } +} + +/** + * @brief remove a specific node from a list + * + * The list is implicit from the node. The node must be part of a list. + * This and other sys_dlist_*() functions are not thread safe. + * + * @param node the node to remove + * + * @return N/A + */ + +static inline void sys_dlist_remove(sys_dnode_t *node) +{ + node->prev->next = node->next; + node->next->prev = node->prev; +} + +/** + * @brief get the first node in a list + * + * This and other sys_dlist_*() functions are not thread safe. + * + * @param list the doubly-linked list to operate on + * + * @return the first node in the list, NULL if list is empty + */ + +static inline sys_dnode_t *sys_dlist_get(sys_dlist_t *list) +{ + sys_dnode_t *node; + + if (sys_dlist_is_empty(list)) { + return NULL; + } + + node = list->head; + sys_dlist_remove(node); + return node; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_DLIST_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_ffs.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_ffs.h new file mode 100644 index 000000000..63c1b1b25 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_ffs.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015, Wind River Systems, Inc. + * Copyright (c) 2017, Oticon A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_FFS_H_ +#define _BLE_MESH_FFS_H_ + +#include "mesh_types.h" +#include "mesh_compiler.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * + * @brief find most significant bit set in a 32-bit word + * + * This routine finds the first bit set starting from the most significant bit + * in the argument passed in and returns the index of that bit. Bits are + * numbered starting at 1 from the least significant bit. A return value of + * zero indicates that the value passed is zero. + * + * @return most significant bit set, 0 if @a op is 0 + */ + +static ALWAYS_INLINE unsigned int find_msb_set(u32_t op) +{ + if (op == 0) { + return 0; + } + + return 32 - __builtin_clz(op); +} + +/** + * + * @brief find least significant bit set in a 32-bit word + * + * This routine finds the first bit set starting from the least significant bit + * in the argument passed in and returns the index of that bit. Bits are + * numbered starting at 1 from the least significant bit. A return value of + * zero indicates that the value passed is zero. + * + * @return least significant bit set, 0 if @a op is 0 + */ + +static ALWAYS_INLINE unsigned int find_lsb_set(u32_t op) +{ + return __builtin_ffs(op); +} + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_FFS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h new file mode 100644 index 000000000..1a6baee71 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016, Wind River Systems, Inc. + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_KERNEL_H_ +#define _BLE_MESH_KERNEL_H_ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/semphr.h" + +#include "mesh_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef CONFIG_BLUEDROID_ENABLED +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define BLE_MESH_ADV_TASK_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLE_MESH_ADV_TASK_CORE (0) +#endif +#endif + +#ifdef CONFIG_NIMBLE_ENABLED +#ifdef CONFIG_NIMBLE_PINNED_TO_CORE +#define BLE_MESH_ADV_TASK_CORE (CONFIG_NIMBLE_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_NIMBLE_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLE_MESH_ADV_TASK_CORE (0) +#endif +#endif + +#define BLE_MESH_ADV_TASK_STACK_SIZE 3072 + +/** + * @brief Put the current thread to sleep. + * + * This routine puts the current thread to sleep for @a duration + * milliseconds. + * + * @param duration Number of milliseconds to sleep. + * + * @return N/A + */ +void k_sleep(s32_t duration); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_KERNEL_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h new file mode 100644 index 000000000..488e010fa --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h @@ -0,0 +1,58 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_MUTEX_H_ +#define _BLE_MESH_MUTEX_H_ + +#include "mesh_kernel.h" +#include "mesh_slist.h" +#include "mesh_atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + SemaphoreHandle_t mutex; +#if CONFIG_SPIRAM_USE_MALLOC + StaticQueue_t *buffer; +#endif +} bt_mesh_mutex_t; + +void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); +void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); + +void bt_mesh_alarm_lock(void); +void bt_mesh_alarm_unlock(void); + +void bt_mesh_list_lock(void); +void bt_mesh_list_unlock(void); + +void bt_mesh_buf_lock(void); +void bt_mesh_buf_unlock(void); + +void bt_mesh_atomic_lock(void); +void bt_mesh_atomic_unlock(void); + +void bt_mesh_mutex_init(void); +void bt_mesh_mutex_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_MUTEX_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_slist.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_slist.h new file mode 100644 index 000000000..4ddf427e6 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_slist.h @@ -0,0 +1,468 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * + * @brief Single-linked list implementation + * + * Single-linked list implementation using inline macros/functions. + * This API is not thread safe, and thus if a list is used across threads, + * calls to functions must be protected with synchronization primitives. + */ + +#ifndef _BLE_MESH_SLIST_H_ +#define _BLE_MESH_SLIST_H_ + +#include +#include +#include "mesh_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct _snode { + struct _snode *next; +}; + +typedef struct _snode sys_snode_t; + +struct _slist { + sys_snode_t *head; + sys_snode_t *tail; +}; + +typedef struct _slist sys_slist_t; + +/** + * @brief Provide the primitive to iterate on a list + * Note: the loop is unsafe and thus __sn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE(l, n) { + * + * } + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + */ +#define SYS_SLIST_FOR_EACH_NODE(__sl, __sn) \ + for (__sn = sys_slist_peek_head(__sl); __sn; \ + __sn = sys_slist_peek_next(__sn)) + +/** + * @brief Provide the primitive to iterate on a list, from a node in the list + * Note: the loop is unsafe and thus __sn should not be removed + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_ITERATE_FROM_NODE(l, n) { + * + * } + * + * Like SYS_SLIST_FOR_EACH_NODE(), but __dn already contains a node in the list + * where to start searching for the next entry from. If NULL, it starts from + * the head. + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + * it contains the starting node, or NULL to start from the head + */ +#define SYS_SLIST_ITERATE_FROM_NODE(__sl, __sn) \ + for (__sn = __sn ? sys_slist_peek_next_no_check(__sn) \ + : sys_slist_peek_head(__sl); \ + __sn; \ + __sn = sys_slist_peek_next(__sn)) + +/** + * @brief Provide the primitive to safely iterate on a list + * Note: __sn can be removed, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE_SAFE(l, n, s) { + * + * } + * + * This and other SYS_SLIST_*() macros are not thread safe. + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __sn A sys_snode_t pointer to peek each node of the list + * @param __sns A sys_snode_t pointer for the loop to run safely + */ +#define SYS_SLIST_FOR_EACH_NODE_SAFE(__sl, __sn, __sns) \ + for (__sn = sys_slist_peek_head(__sl), \ + __sns = sys_slist_peek_next(__sn); \ + __sn; __sn = __sns, \ + __sns = sys_slist_peek_next(__sn)) + +/* + * @brief Provide the primitive to resolve the container of a list node + * Note: it is safe to use with NULL pointer nodes + * + * @param __ln A pointer on a sys_node_t to get its container + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_CONTAINER(__ln, __cn, __n) \ + ((__ln) ? CONTAINER_OF((__ln), __typeof__(*(__cn)), __n) : NULL) +/* + * @brief Provide the primitive to peek container of the list head + * + * @param __sl A pointer on a sys_slist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n) \ + SYS_SLIST_CONTAINER(sys_slist_peek_head(__sl), __cn, __n) + +/* + * @brief Provide the primitive to peek container of the list tail + * + * @param __sl A pointer on a sys_slist_t to peek + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_PEEK_TAIL_CONTAINER(__sl, __cn, __n) \ + SYS_SLIST_CONTAINER(sys_slist_peek_tail(__sl), __cn, __n) + +/* + * @brief Provide the primitive to peek the next container + * + * @param __cn Container struct type pointer + * @param __n The field name of sys_node_t within the container struct + */ + +#define SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \ + ((__cn) ? SYS_SLIST_CONTAINER(sys_slist_peek_next(&((__cn)->__n)), \ + __cn, __n) : NULL) + +/** + * @brief Provide the primitive to iterate on a list under a container + * Note: the loop is unsafe and thus __cn should not be detached + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_CONTAINER(l, c, n) { + * + * } + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_FOR_EACH_CONTAINER(__sl, __cn, __n) \ + for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n); __cn; \ + __cn = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n)) + +/** + * @brief Provide the primitive to safely iterate on a list under a container + * Note: __cn can be detached, it will not break the loop. + * + * User _MUST_ add the loop statement curly braces enclosing its own code: + * + * SYS_SLIST_FOR_EACH_NODE_SAFE(l, c, cn, n) { + * + * } + * + * @param __sl A pointer on a sys_slist_t to iterate on + * @param __cn A pointer to peek each entry of the list + * @param __cns A pointer for the loop to run safely + * @param __n The field name of sys_node_t within the container struct + */ +#define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \ + for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \ + __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n); __cn; \ + __cn = __cns, __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n)) + +/** + * @brief Initialize a list + * + * @param list A pointer on the list to initialize + */ +static inline void sys_slist_init(sys_slist_t *list) +{ + list->head = NULL; + list->tail = NULL; +} + +#define SYS_SLIST_STATIC_INIT(ptr_to_list) {NULL, NULL} + +/** + * @brief Test if the given list is empty + * + * @param list A pointer on the list to test + * + * @return a boolean, true if it's empty, false otherwise + */ +static inline bool sys_slist_is_empty(sys_slist_t *list) +{ + return (!list->head); +} + +/** + * @brief Peek the first node from the list + * + * @param list A point on the list to peek the first node from + * + * @return A pointer on the first node of the list (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_head(sys_slist_t *list) +{ + return list->head; +} + +/** + * @brief Peek the last node from the list + * + * @param list A point on the list to peek the last node from + * + * @return A pointer on the last node of the list (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_tail(sys_slist_t *list) +{ + return list->tail; +} + +/** + * @brief Peek the next node from current node, node is not NULL + * + * Faster then sys_slist_peek_next() if node is known not to be NULL. + * + * @param node A pointer on the node where to peek the next node + * + * @return a pointer on the next node (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_next_no_check(sys_snode_t *node) +{ + return node->next; +} + +/** + * @brief Peek the next node from current node + * + * @param node A pointer on the node where to peek the next node + * + * @return a pointer on the next node (or NULL if none) + */ +static inline sys_snode_t *sys_slist_peek_next(sys_snode_t *node) +{ + return node ? sys_slist_peek_next_no_check(node) : NULL; +} + +/** + * @brief Prepend a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to prepend + */ +static inline void sys_slist_prepend(sys_slist_t *list, + sys_snode_t *node) +{ + node->next = list->head; + list->head = node; + + if (!list->tail) { + list->tail = list->head; + } +} + +/** + * @brief Append a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to append + */ +static inline void sys_slist_append(sys_slist_t *list, + sys_snode_t *node) +{ + node->next = NULL; + + if (!list->tail) { + list->tail = node; + list->head = node; + } else { + list->tail->next = node; + list->tail = node; + } +} + +/** + * @brief Append a list to the given list + * + * Append a singly-linked, NULL-terminated list consisting of nodes containing + * the pointer to the next node as the first element of a node, to @a list. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param head A pointer to the first element of the list to append + * @param tail A pointer to the last element of the list to append + */ +static inline void sys_slist_append_list(sys_slist_t *list, + void *head, void *tail) +{ + if (!list->tail) { + list->head = (sys_snode_t *)head; + list->tail = (sys_snode_t *)tail; + } else { + list->tail->next = (sys_snode_t *)head; + list->tail = (sys_snode_t *)tail; + } +} + +/** + * @brief merge two slists, appending the second one to the first + * + * When the operation is completed, the appending list is empty. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param list_to_append A pointer to the list to append. + */ +static inline void sys_slist_merge_slist(sys_slist_t *list, + sys_slist_t *list_to_append) +{ + sys_slist_append_list(list, list_to_append->head, + list_to_append->tail); + sys_slist_init(list_to_append); +} + +/** + * @brief Insert a node to the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param prev A pointer on the previous node + * @param node A pointer on the node to insert + */ +static inline void sys_slist_insert(sys_slist_t *list, + sys_snode_t *prev, + sys_snode_t *node) +{ + if (!prev) { + sys_slist_prepend(list, node); + } else if (!prev->next) { + sys_slist_append(list, node); + } else { + node->next = prev->next; + prev->next = node; + } +} + +/** + * @brief Fetch and remove the first node of the given list + * + * List must be known to be non-empty. + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * + * @return A pointer to the first node of the list + */ +static inline sys_snode_t *sys_slist_get_not_empty(sys_slist_t *list) +{ + sys_snode_t *node = list->head; + + list->head = node->next; + if (list->tail == node) { + list->tail = list->head; + } + + return node; +} + +/** + * @brief Fetch and remove the first node of the given list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * + * @return A pointer to the first node of the list (or NULL if empty) + */ +static inline sys_snode_t *sys_slist_get(sys_slist_t *list) +{ + return sys_slist_is_empty(list) ? NULL : sys_slist_get_not_empty(list); +} + +/** + * @brief Remove a node + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param prev_node A pointer on the previous node + * (can be NULL, which means the node is the list's head) + * @param node A pointer on the node to remove + */ +static inline void sys_slist_remove(sys_slist_t *list, + sys_snode_t *prev_node, + sys_snode_t *node) +{ + if (!prev_node) { + list->head = node->next; + + /* Was node also the tail? */ + if (list->tail == node) { + list->tail = list->head; + } + } else { + prev_node->next = node->next; + + /* Was node the tail? */ + if (list->tail == node) { + list->tail = prev_node; + } + } + + node->next = NULL; +} + +/** + * @brief Find and remove a node from a list + * + * This and other sys_slist_*() functions are not thread safe. + * + * @param list A pointer on the list to affect + * @param node A pointer on the node to remove from the list + * + * @return true if node was removed + */ +static inline bool sys_slist_find_and_remove(sys_slist_t *list, + sys_snode_t *node) +{ + sys_snode_t *prev = NULL; + sys_snode_t *test; + + SYS_SLIST_FOR_EACH_NODE(list, test) { + if (test == node) { + sys_slist_remove(list, prev, node); + return true; + } + + prev = test; + } + + return false; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_SLIST_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h new file mode 100644 index 000000000..6f47d8184 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_timer.h @@ -0,0 +1,267 @@ +/* + * Copyright (c) 2016, Wind River Systems, Inc. + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_TIMER_H_ +#define _BLE_MESH_TIMER_H_ + +#include "mesh_types.h" +#include "mesh_slist.h" +#include "mesh_atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* number of nsec per usec */ +#define NSEC_PER_USEC 1000 + +/* number of microseconds per millisecond */ +#define USEC_PER_MSEC 1000 + +/* number of milliseconds per second */ +#define MSEC_PER_SEC 1000 + +/* number of microseconds per second */ +#define USEC_PER_SEC ((USEC_PER_MSEC) * (MSEC_PER_SEC)) + +/* number of nanoseconds per second */ +#define NSEC_PER_SEC ((NSEC_PER_USEC) * (USEC_PER_MSEC) * (MSEC_PER_SEC)) + +/* timeout is not in use */ +#define _INACTIVE (-1) + +struct k_work; + +/** + * @typedef k_work_handler_t + * @brief Work item handler function type. + * + * A work item's handler function is executed by a workqueue's thread + * when the work item is processed by the workqueue. + * + * @param work Address of the work item. + * + * @return N/A + */ +typedef void (*k_work_handler_t)(struct k_work *work); + +struct k_work { + void *_reserved; + k_work_handler_t handler; + int index; +}; + +#define _K_WORK_INITIALIZER(work_handler) \ +{ \ + ._reserved = NULL, \ + .handler = work_handler, \ +} + +/** + * @brief Generate null timeout delay. + * + * This macro generates a timeout delay that that instructs a kernel API + * not to wait if the requested operation cannot be performed immediately. + * + * @return Timeout delay value. + */ +#define K_NO_WAIT 0 + +/** + * @brief Generate timeout delay from milliseconds. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait up to @a ms milliseconds to perform the requested operation. + * + * @param ms Duration in milliseconds. + * + * @return Timeout delay value. + */ +#define K_MSEC(ms) (ms) + +/** + * @brief Generate timeout delay from seconds. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait up to @a s seconds to perform the requested operation. + * + * @param s Duration in seconds. + * + * @return Timeout delay value. + */ +#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) + +/** + * @brief Generate timeout delay from minutes. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait up to @a m minutes to perform the requested operation. + * + * @param m Duration in minutes. + * + * @return Timeout delay value. + */ +#define K_MINUTES(m) K_SECONDS((m) * 60) + +/** + * @brief Generate timeout delay from hours. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait up to @a h hours to perform the requested operation. + * + * @param h Duration in hours. + * + * @return Timeout delay value. + */ +#define K_HOURS(h) K_MINUTES((h) * 60) + +/** + * @brief Generate infinite timeout delay. + * + * This macro generates a timeout delay that that instructs a kernel API + * to wait as long as necessary to perform the requested operation. + * + * @return Timeout delay value. + */ +#define K_FOREVER (-1) + +/** + * @brief Get system uptime (32-bit version). + * + * This routine returns the lower 32-bits of the elapsed time since the system + * booted, in milliseconds. + * + * This routine can be more efficient than k_uptime_get(), as it reduces the + * need for interrupt locking and 64-bit math. However, the 32-bit result + * cannot hold a system uptime time larger than approximately 50 days, so the + * caller must handle possible rollovers. + * + * @return Current uptime. + */ +u32_t k_uptime_get_32(void); + +struct k_delayed_work { + struct k_work work; +}; + +/** + * @brief Submit a delayed work item to the system workqueue. + * + * This routine schedules work item @a work to be processed by the system + * workqueue after a delay of @a delay milliseconds. The routine initiates + * an asynchronous countdown for the work item and then returns to the caller. + * Only when the countdown completes is the work item actually submitted to + * the workqueue and becomes pending. + * + * Submitting a previously submitted delayed work item that is still + * counting down cancels the existing submission and restarts the countdown + * using the new delay. If the work item is currently pending on the + * workqueue's queue because the countdown has completed it is too late to + * resubmit the item, and resubmission fails without impacting the work item. + * If the work item has already been processed, or is currently being processed, + * its work is considered complete and the work item can be resubmitted. + * + * @warning + * Work items submitted to the system workqueue should avoid using handlers + * that block or yield since this may prevent the system workqueue from + * processing other work items in a timely manner. + * + * @note Can be called by ISRs. + * + * @param work Address of delayed work item. + * @param delay Delay before submitting the work item (in milliseconds). + * + * @retval 0 Work item countdown started. + * @retval -EINPROGRESS Work item is already pending. + * @retval -EINVAL Work item is being processed or has completed its work. + * @retval -EADDRINUSE Work item is pending on a different workqueue. + */ +int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay); + +int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period); + +/** + * @brief Get time remaining before a delayed work gets scheduled. + * + * This routine computes the (approximate) time remaining before a + * delayed work gets executed. If the delayed work is not waiting to be + * scheduled, it returns zero. + * + * @param work Delayed work item. + * + * @return Remaining time (in milliseconds). + */ +s32_t k_delayed_work_remaining_get(struct k_delayed_work *work); + +/** + * @brief Submit a work item to the system workqueue. + * + * This routine submits work item @a work to be processed by the system + * workqueue. If the work item is already pending in the workqueue's queue + * as a result of an earlier submission, this routine has no effect on the + * work item. If the work item has already been processed, or is currently + * being processed, its work is considered complete and the work item can be + * resubmitted. + * + * @warning + * Work items submitted to the system workqueue should avoid using handlers + * that block or yield since this may prevent the system workqueue from + * processing other work items in a timely manner. + * + * @note Can be called by ISRs. + * + * @param work Address of work item. + * + * @return N/A + */ +static inline void k_work_submit(struct k_work *work) +{ + if (work && work->handler) { + work->handler(work); + } +} + +/** + * @brief Initialize a work item. + * + * This routine initializes a workqueue work item, prior to its first use. + * + * @param work Address of work item. + * @param handler Function to invoke each time work item is processed. + * + * @return N/A + */ +static inline void k_work_init(struct k_work *work, k_work_handler_t handler) +{ + work->handler = handler; +} + +int k_delayed_work_cancel(struct k_delayed_work *work); + +int k_delayed_work_free(struct k_delayed_work *work); + +int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler); + +/** + * @brief Get system uptime. + * + * This routine returns the elapsed time since the system booted, + * in milliseconds. + * + * @return Current uptime. + */ +s64_t k_uptime_get(void); + +void bt_mesh_timer_init(void); +void bt_mesh_timer_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_TIMER_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h new file mode 100644 index 000000000..924e60b8d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_trace.h @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015-2016 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_TRACE_H_ +#define _BLE_MESH_TRACE_H_ + +#include "esp_log.h" +#include "mesh_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Define common tracing for all */ +#ifndef LOG_LEVEL_ERROR +#define LOG_LEVEL_ERROR 1 +#endif /* LOG_LEVEL_ERROR */ + +#ifndef LOG_LEVEL_WARN +#define LOG_LEVEL_WARN 2 +#endif /* LOG_LEVEL_WARN */ + +#ifndef LOG_LEVEL_INFO +#define LOG_LEVEL_INFO 3 +#endif /* LOG_LEVEL_INFO */ + +#ifndef LOG_LEVEL_DEBUG +#define LOG_LEVEL_DEBUG 4 +#endif /* LOG_LEVEL_DEBUG */ + +#ifndef LOG_LEVEL_VERBOSE +#define LOG_LEVEL_VERBOSE 5 +#endif /*LOG_LEVEL_VERBOSE */ + +#ifdef CONFIG_BLE_MESH_STACK_TRACE_LEVEL +#define MESH_LOG_LEVEL CONFIG_BLE_MESH_STACK_TRACE_LEVEL +#else +#define MESH_LOG_LEVEL LOG_LEVEL_WARN +#endif + +#ifdef CONFIG_BLE_MESH_NET_BUF_TRACE_LEVEL +#define NET_BUF_LOG_LEVEL CONFIG_BLE_MESH_NET_BUF_TRACE_LEVEL +#else +#define NET_BUF_LOG_LEVEL LOG_LEVEL_WARN +#endif + +#define MESH_TRACE_TAG "BLE_MESH" + +#if (LOG_LOCAL_LEVEL >= 4) +#define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING (LOG_LOCAL_LEVEL + 1) +#else +#define BLE_MESH_LOG_LOCAL_LEVEL_MAPPING LOG_LOCAL_LEVEL +#endif + +#define BLE_MESH_LOG_LEVEL_CHECK(LAYER, LEVEL) (MAX(LAYER##_LOG_LEVEL, BLE_MESH_LOG_LOCAL_LEVEL_MAPPING) >= LOG_LEVEL_##LEVEL) + +#define BLE_MESH_PRINT_E(tag, format, ...) {esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BLE_MESH_PRINT_W(tag, format, ...) {esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BLE_MESH_PRINT_I(tag, format, ...) {esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BLE_MESH_PRINT_D(tag, format, ...) {esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } +#define BLE_MESH_PRINT_V(tag, format, ...) {esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } + +#define printk ets_printf + +#define _STRINGIFY(x) #x +#define STRINGIFY(s) _STRINGIFY(s) + +#ifndef __ASSERT +#define __ASSERT(test, fmt, ...) \ + do { \ + if (!(test)) { \ + printk("ASSERTION FAIL [%s] @ %s:%d:\n\t", \ + _STRINGIFY(test), \ + __FILE__, \ + __LINE__); \ + printk(fmt, ##__VA_ARGS__); \ + for (;;); \ + } \ + } while ((0)) +#endif + +#ifndef __ASSERT_NO_MSG +#define __ASSERT_NO_MSG(x) do { if (!(x)) BLE_MESH_PRINT_E(MESH_TRACE_TAG, "error %s %u", __FILE__, __LINE__); } while (0) +#endif + +#if !CONFIG_BLE_MESH_NO_LOG +#define BT_ERR(fmt, args...) do {if ((MESH_LOG_LEVEL >= LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(MESH, ERROR)) BLE_MESH_PRINT_E(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define BT_WARN(fmt, args...) do {if ((MESH_LOG_LEVEL >= LOG_LEVEL_WARN) && BLE_MESH_LOG_LEVEL_CHECK(MESH, WARN)) BLE_MESH_PRINT_W(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define BT_INFO(fmt, args...) do {if ((MESH_LOG_LEVEL >= LOG_LEVEL_INFO) && BLE_MESH_LOG_LEVEL_CHECK(MESH, INFO)) BLE_MESH_PRINT_I(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define BT_DBG(fmt, args...) do {if ((MESH_LOG_LEVEL >= LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(MESH, DEBUG)) BLE_MESH_PRINT_D(MESH_TRACE_TAG, fmt, ## args);} while(0) +#else +#define BT_ERR(fmt, args...) +#define BT_WARN(fmt, args...) +#define BT_INFO(fmt, args...) +#define BT_DBG(fmt, args...) +#endif + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) && (!CONFIG_BLE_MESH_NO_LOG) +#define NET_BUF_ERR(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, ERROR)) BLE_MESH_PRINT_E(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_WARN(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_WARN) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, WARN)) BLE_MESH_PRINT_W(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_INFO(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_INFO) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, INFO)) BLE_MESH_PRINT_I(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_DBG(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, DEBUG)) BLE_MESH_PRINT_D(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_ASSERT(cond) __ASSERT_NO_MSG(cond) +#else +#define NET_BUF_ERR(fmt, args...) +#define NET_BUF_WARN(fmt, args...) +#define NET_BUF_INFO(fmt, args...) +#define NET_BUF_DBG(fmt, args...) +#define NET_BUF_ASSERT(cond) +#endif + +#if defined(CONFIG_BLE_MESH_NET_BUF_SIMPLE_LOG) && (!CONFIG_BLE_MESH_NO_LOG) +#define NET_BUF_SIMPLE_ERR(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_ERROR) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, ERROR)) BLE_MESH_PRINT_E(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_SIMPLE_WARN(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_WARN) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, WARN)) BLE_MESH_PRINT_W(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_SIMPLE_INFO(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_INFO) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, INFO)) BLE_MESH_PRINT_I(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_SIMPLE_DBG(fmt, args...) do {if ((NET_BUF_LOG_LEVEL >= LOG_LEVEL_DEBUG) && BLE_MESH_LOG_LEVEL_CHECK(NET_BUF, DEBUG)) BLE_MESH_PRINT_D(MESH_TRACE_TAG, fmt, ## args);} while(0) +#define NET_BUF_SIMPLE_ASSERT(cond) __ASSERT_NO_MSG(cond) +#else +#define NET_BUF_SIMPLE_ERR(fmt, args...) +#define NET_BUF_SIMPLE_WARN(fmt, args...) +#define NET_BUF_SIMPLE_INFO(fmt, args...) +#define NET_BUF_SIMPLE_DBG(fmt, args...) +#define NET_BUF_SIMPLE_ASSERT(cond) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_TRACE_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_types.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_types.h new file mode 100644 index 000000000..5a189630c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_types.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2017 Linaro Limited + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_TYPES_H_ +#define _BLE_MESH_TYPES_H_ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef signed char s8_t; +typedef signed short s16_t; +typedef signed int s32_t; +typedef signed long long s64_t; + +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned int u32_t; +typedef unsigned long long u64_t; + +typedef int bt_mesh_atomic_t; + +#ifndef bool +#define bool int8_t +#endif + +#ifndef false +#define false 0 +#endif + +#ifndef true +#define true 1 +#endif + +#ifndef PRIu64 +#define PRIu64 "llu" +#endif + +#ifndef PRIx64 +#define PRIx64 "llx" +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_TYPES_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h new file mode 100644 index 000000000..7eebe71b4 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_util.h @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2011-2014, Wind River Systems, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Misc utilities + * + * Misc utilities usable by the kernel and application code. + */ + +#ifndef _BLE_MESH_UTIL_H_ +#define _BLE_MESH_UTIL_H_ + +#include +#include "soc/soc.h" +#include "mesh_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Helper to pass a int as a pointer or vice-versa. + * Those are available for 32 bits architectures: + */ +#ifndef POINTER_TO_UINT +#define POINTER_TO_UINT(x) ((u32_t) (x)) +#endif +#ifndef UINT_TO_POINTER +#define UINT_TO_POINTER(x) ((void *) (x)) +#endif +#ifndef POINTER_TO_INT +#define POINTER_TO_INT(x) ((s32_t) (x)) +#endif +#ifndef INT_TO_POINTER +#define INT_TO_POINTER(x) ((void *) (x)) +#endif + +/* Evaluates to 0 if cond is true-ish; compile error otherwise */ +#ifndef ZERO_OR_COMPILE_ERROR +#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1) +#endif + +/* Evaluates to 0 if array is an array; compile error if not array (e.g. + * pointer) + */ +#ifndef IS_ARRAY +#define IS_ARRAY(array) \ + ZERO_OR_COMPILE_ERROR( \ + !__builtin_types_compatible_p(__typeof__(array), \ + __typeof__(&(array)[0]))) +#endif + +/* Evaluates to number of elements in an array; compile error if not + * an array (e.g. pointer) + */ +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(array) \ + ((unsigned long) (IS_ARRAY(array) + \ + (sizeof(array) / sizeof((array)[0])))) +#endif + +/* Evaluates to 1 if ptr is part of array, 0 otherwise; compile error if + * "array" argument is not an array (e.g. "ptr" and "array" mixed up) + */ +#ifndef PART_OF_ARRAY +#define PART_OF_ARRAY(array, ptr) \ + ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)])) +#endif + +#ifndef CONTAINER_OF +#define CONTAINER_OF(ptr, type, field) \ + ((type *)(((char *)(ptr)) - offsetof(type, field))) +#endif + +/* round "x" up/down to next multiple of "align" (which must be a power of 2) */ +#ifndef ROUND_UP +#define ROUND_UP(x, align) \ + (((unsigned long)(x) + ((unsigned long)align - 1)) & \ + ~((unsigned long)align - 1)) +#endif + +#ifndef ROUND_DOWN +#define ROUND_DOWN(x, align) ((unsigned long)(x) & ~((unsigned long)align - 1)) +#endif + +/* round up/down to the next word boundary */ +#ifndef WB_UP +#define WB_UP(x) ROUND_UP(x, sizeof(void *)) +#endif + +#ifndef WB_DN +#define WB_DN(x) ROUND_DOWN(x, sizeof(void *)) +#endif + +#ifndef ceiling_fraction +#define ceiling_fraction(numerator, divider) \ + (((numerator) + ((divider) - 1)) / (divider)) +#endif + +#ifndef CHECKIF +#define CHECKIF(expr) if (expr) +#endif + +/** @brief Return larger value of two provided expressions. + * + * @note Arguments are evaluated twice. See Z_MAX for GCC only, single + * evaluation version. + */ +#ifndef MAX +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#endif + +/** @brief Return smaller value of two provided expressions. + * + * @note Arguments are evaluated twice. See Z_MIN for GCC only, single + * evaluation version. + */ +#ifndef MIN +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef BIT +#define BIT(n) (1UL << (n)) +#endif + +#ifndef BIT_MASK +#define BIT_MASK(n) (BIT(n) - 1) +#endif + +/** + * @brief Check for macro definition in compiler-visible expressions + * + * This trick was pioneered in Linux as the config_enabled() macro. + * The madness has the effect of taking a macro value that may be + * defined to "1" (e.g. CONFIG_MYFEATURE), or may not be defined at + * all and turning it into a literal expression that can be used at + * "runtime". That is, it works similarly to + * "defined(CONFIG_MYFEATURE)" does except that it is an expansion + * that can exist in a standard expression and be seen by the compiler + * and optimizer. Thus much ifdef usage can be replaced with cleaner + * expressions like: + * + * if (IS_ENABLED(CONFIG_MYFEATURE)) + * myfeature_enable(); + * + * INTERNAL + * First pass just to expand any existing macros, we need the macro + * value to be e.g. a literal "1" at expansion time in the next macro, + * not "(1)", etc... Standard recursive expansion does not work. + */ +#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro) + +/* Now stick on a "_XXXX" prefix, it will now be "_XXXX1" if config_macro + * is "1", or just "_XXXX" if it's undefined. + * ENABLED: Z_IS_ENABLED2(_XXXX1) + * DISABLED Z_IS_ENABLED2(_XXXX) + */ +#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro) + +/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string + * with a trailing comma), so it has the effect of making this a + * two-argument tuple to the preprocessor only in the case where the + * value is defined to "1" + * ENABLED: _YYYY, <--- note comma! + * DISABLED: _XXXX + */ +#define _XXXX1 _YYYY, + +/* Then we append an extra argument to fool the gcc preprocessor into + * accepting it as a varargs macro. + * arg1 arg2 arg3 + * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0) + * DISABLED Z_IS_ENABLED3(_XXXX 1, 0) + */ +#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args true, false) + +/* And our second argument is thus now cooked to be 1 in the case + * where the value is defined to 1, and 0 if not: + */ +#define Z_IS_ENABLED3(ignore_this, val, ...) val + +const char *bt_hex(const void *buf, size_t len); + +void mem_rcopy(u8_t *dst, u8_t const *src, u16_t len); + +unsigned int _copy(uint8_t *to, unsigned int to_len, + const uint8_t *from, unsigned int from_len); + +void _set(void *to, uint8_t val, unsigned int len); + +uint8_t _double_byte(uint8_t a); + +int _compare(const uint8_t *a, const uint8_t *b, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_UTIL_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c b/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c new file mode 100644 index 000000000..544bbea72 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_aes_encrypt.c @@ -0,0 +1,408 @@ +/* aes_encrypt.c - TinyCrypt implementation of AES encryption procedure */ + +/* + * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Intel Corporation nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mesh_util.h" +#include "mesh_aes_encrypt.h" + +/* max number of calls until change the key (2^48).*/ +const static uint64_t MAX_CALLS = ((uint64_t)1 << 48); + +/* + * gf_wrap -- In our implementation, GF(2^128) is represented as a 16 byte + * array with byte 0 the most significant and byte 15 the least significant. + * High bit carry reduction is based on the primitive polynomial + * + * X^128 + X^7 + X^2 + X + 1, + * + * which leads to the reduction formula X^128 = X^7 + X^2 + X + 1. Indeed, + * since 0 = (X^128 + X^7 + X^2 + 1) mod (X^128 + X^7 + X^2 + X + 1) and since + * addition of polynomials with coefficients in Z/Z(2) is just XOR, we can + * add X^128 to both sides to get + * + * X^128 = (X^7 + X^2 + X + 1) mod (X^128 + X^7 + X^2 + X + 1) + * + * and the coefficients of the polynomial on the right hand side form the + * string 1000 0111 = 0x87, which is the value of gf_wrap. + * + * This gets used in the following way. Doubling in GF(2^128) is just a left + * shift by 1 bit, except when the most significant bit is 1. In the latter + * case, the relation X^128 = X^7 + X^2 + X + 1 says that the high order bit + * that overflows beyond 128 bits can be replaced by addition of + * X^7 + X^2 + X + 1 <--> 0x87 to the low order 128 bits. Since addition + * in GF(2^128) is represented by XOR, we therefore only have to XOR 0x87 + * into the low order byte after a left shift when the starting high order + * bit is 1. + */ +const unsigned char gf_wrap = 0x87; + +static const uint8_t sbox[256] = { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, + 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, + 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, + 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, + 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, + 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, + 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, + 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, + 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, + 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, + 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, + 0xb0, 0x54, 0xbb, 0x16 +}; + +static inline unsigned int rotword(unsigned int a) +{ + return (((a) >> 24) | ((a) << 8)); +} + +#define subbyte(a, o) (sbox[((a) >> (o))&0xff] << (o)) +#define subword(a) (subbyte(a, 24)|subbyte(a, 16)|subbyte(a, 8)|subbyte(a, 0)) + +int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k) +{ + const unsigned int rconst[11] = { + 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, + 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000 + }; + unsigned int i; + unsigned int t; + + if (s == (TCAesKeySched_t) 0) { + return TC_CRYPTO_FAIL; + } else if (k == (const uint8_t *) 0) { + return TC_CRYPTO_FAIL; + } + + for (i = 0; i < Nk; ++i) { + s->words[i] = (k[Nb * i] << 24) | (k[Nb * i + 1] << 16) | + (k[Nb * i + 2] << 8) | (k[Nb * i + 3]); + } + + for (; i < (Nb * (Nr + 1)); ++i) { + t = s->words[i - 1]; + if ((i % Nk) == 0) { + t = subword(rotword(t)) ^ rconst[i / Nk]; + } + s->words[i] = s->words[i - Nk] ^ t; + } + + return TC_CRYPTO_SUCCESS; +} + +static inline void add_round_key(uint8_t *s, const unsigned int *k) +{ + s[0] ^= (uint8_t)(k[0] >> 24); s[1] ^= (uint8_t)(k[0] >> 16); + s[2] ^= (uint8_t)(k[0] >> 8); s[3] ^= (uint8_t)(k[0]); + s[4] ^= (uint8_t)(k[1] >> 24); s[5] ^= (uint8_t)(k[1] >> 16); + s[6] ^= (uint8_t)(k[1] >> 8); s[7] ^= (uint8_t)(k[1]); + s[8] ^= (uint8_t)(k[2] >> 24); s[9] ^= (uint8_t)(k[2] >> 16); + s[10] ^= (uint8_t)(k[2] >> 8); s[11] ^= (uint8_t)(k[2]); + s[12] ^= (uint8_t)(k[3] >> 24); s[13] ^= (uint8_t)(k[3] >> 16); + s[14] ^= (uint8_t)(k[3] >> 8); s[15] ^= (uint8_t)(k[3]); +} + +static inline void sub_bytes(uint8_t *s) +{ + unsigned int i; + + for (i = 0; i < (Nb * Nk); ++i) { + s[i] = sbox[s[i]]; + } +} + +#define triple(a)(_double_byte(a)^(a)) + +static inline void mult_row_column(uint8_t *out, const uint8_t *in) +{ + out[0] = _double_byte(in[0]) ^ triple(in[1]) ^ in[2] ^ in[3]; + out[1] = in[0] ^ _double_byte(in[1]) ^ triple(in[2]) ^ in[3]; + out[2] = in[0] ^ in[1] ^ _double_byte(in[2]) ^ triple(in[3]); + out[3] = triple(in[0]) ^ in[1] ^ in[2] ^ _double_byte(in[3]); +} + +static inline void mix_columns(uint8_t *s) +{ + uint8_t t[Nb * Nk] = {0}; + + mult_row_column(t, s); + mult_row_column(&t[Nb], s + Nb); + mult_row_column(&t[2 * Nb], s + (2 * Nb)); + mult_row_column(&t[3 * Nb], s + (3 * Nb)); + (void) _copy(s, sizeof(t), t, sizeof(t)); +} + +/* + * This shift_rows also implements the matrix flip required for mix_columns, but + * performs it here to reduce the number of memory operations. + */ +static inline void shift_rows(uint8_t *s) +{ + uint8_t t[Nb * Nk] = {0}; + + t[0] = s[0]; t[1] = s[5]; t[2] = s[10]; t[3] = s[15]; + t[4] = s[4]; t[5] = s[9]; t[6] = s[14]; t[7] = s[3]; + t[8] = s[8]; t[9] = s[13]; t[10] = s[2]; t[11] = s[7]; + t[12] = s[12]; t[13] = s[1]; t[14] = s[6]; t[15] = s[11]; + (void) _copy(s, sizeof(t), t, sizeof(t)); +} + +int tc_aes_encrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s) +{ + uint8_t state[Nk * Nb] = {0}; + unsigned int i; + + if (out == (uint8_t *) 0) { + return TC_CRYPTO_FAIL; + } else if (in == (const uint8_t *) 0) { + return TC_CRYPTO_FAIL; + } else if (s == (TCAesKeySched_t) 0) { + return TC_CRYPTO_FAIL; + } + + (void)_copy(state, sizeof(state), in, sizeof(state)); + add_round_key(state, s->words); + + for (i = 0; i < (Nr - 1); ++i) { + sub_bytes(state); + shift_rows(state); + mix_columns(state); + add_round_key(state, s->words + Nb * (i + 1)); + } + + sub_bytes(state); + shift_rows(state); + add_round_key(state, s->words + Nb * (i + 1)); + + (void)_copy(out, sizeof(state), state, sizeof(state)); + + /* zeroing out the state buffer */ + _set(state, TC_ZERO_BYTE, sizeof(state)); + + return TC_CRYPTO_SUCCESS; +} + +int tc_cmac_setup(TCCmacState_t s, const uint8_t *key, TCAesKeySched_t sched) +{ + + /* input sanity check: */ + if (s == (TCCmacState_t) 0 || + key == (const uint8_t *) 0) { + return TC_CRYPTO_FAIL; + } + + /* put s into a known state */ + _set(s, 0, sizeof(*s)); + s->sched = sched; + + /* configure the encryption key used by the underlying block cipher */ + tc_aes128_set_encrypt_key(s->sched, key); + + /* compute s->K1 and s->K2 from s->iv using s->keyid */ + _set(s->iv, 0, TC_AES_BLOCK_SIZE); + tc_aes_encrypt(s->iv, s->iv, s->sched); + gf_double (s->K1, s->iv); + gf_double (s->K2, s->K1); + + /* reset s->iv to 0 in case someone wants to compute now */ + tc_cmac_init(s); + + return TC_CRYPTO_SUCCESS; +} + +/* + * assumes: out != NULL and points to a GF(2^n) value to receive the + * doubled value; + * in != NULL and points to a 16 byte GF(2^n) value + * to double; + * the in and out buffers do not overlap. + * effects: doubles the GF(2^n) value pointed to by "in" and places + * the result in the GF(2^n) value pointed to by "out." + */ +void gf_double(uint8_t *out, uint8_t *in) +{ + + /* start with low order byte */ + uint8_t *x = in + (TC_AES_BLOCK_SIZE - 1); + + /* if msb == 1, we need to add the gf_wrap value, otherwise add 0 */ + uint8_t carry = (in[0] >> 7) ? gf_wrap : 0; + + out += (TC_AES_BLOCK_SIZE - 1); + for (;;) { + *out-- = (*x << 1) ^ carry; + if (x == in) { + break; + } + carry = *x-- >> 7; + } +} + +int tc_cmac_init(TCCmacState_t s) +{ + /* input sanity check: */ + if (s == (TCCmacState_t) 0) { + return TC_CRYPTO_FAIL; + } + + /* CMAC starts with an all zero initialization vector */ + _set(s->iv, 0, TC_AES_BLOCK_SIZE); + + /* and the leftover buffer is empty */ + _set(s->leftover, 0, TC_AES_BLOCK_SIZE); + s->leftover_offset = 0; + + /* Set countdown to max number of calls allowed before re-keying: */ + s->countdown = MAX_CALLS; + + return TC_CRYPTO_SUCCESS; +} + +int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t data_length) +{ + unsigned int i; + + /* input sanity check: */ + if (s == (TCCmacState_t) 0) { + return TC_CRYPTO_FAIL; + } + if (data_length == 0) { + return TC_CRYPTO_SUCCESS; + } + if (data == (const uint8_t *) 0) { + return TC_CRYPTO_FAIL; + } + + if (s->countdown == 0) { + return TC_CRYPTO_FAIL; + } + + s->countdown--; + + if (s->leftover_offset > 0) { + /* last data added to s didn't end on a TC_AES_BLOCK_SIZE byte boundary */ + size_t remaining_space = TC_AES_BLOCK_SIZE - s->leftover_offset; + + if (data_length < remaining_space) { + /* still not enough data to encrypt this time either */ + _copy(&s->leftover[s->leftover_offset], data_length, data, data_length); + s->leftover_offset += data_length; + return TC_CRYPTO_SUCCESS; + } + /* leftover block is now full; encrypt it first */ + _copy(&s->leftover[s->leftover_offset], + remaining_space, + data, + remaining_space); + data_length -= remaining_space; + data += remaining_space; + s->leftover_offset = 0; + + for (i = 0; i < TC_AES_BLOCK_SIZE; ++i) { + s->iv[i] ^= s->leftover[i]; + } + tc_aes_encrypt(s->iv, s->iv, s->sched); + } + + /* CBC encrypt each (except the last) of the data blocks */ + while (data_length > TC_AES_BLOCK_SIZE) { + for (i = 0; i < TC_AES_BLOCK_SIZE; ++i) { + s->iv[i] ^= data[i]; + } + tc_aes_encrypt(s->iv, s->iv, s->sched); + data += TC_AES_BLOCK_SIZE; + data_length -= TC_AES_BLOCK_SIZE; + } + + if (data_length > 0) { + /* save leftover data for next time */ + _copy(s->leftover, data_length, data, data_length); + s->leftover_offset = data_length; + } + + return TC_CRYPTO_SUCCESS; +} + +int tc_cmac_final(uint8_t *tag, TCCmacState_t s) +{ + uint8_t *k = NULL; + unsigned int i; + + /* input sanity check: */ + if (tag == (uint8_t *) 0 || + s == (TCCmacState_t) 0) { + return TC_CRYPTO_FAIL; + } + + if (s->leftover_offset == TC_AES_BLOCK_SIZE) { + /* the last message block is a full-sized block */ + k = (uint8_t *) s->K1; + } else { + /* the final message block is not a full-sized block */ + size_t remaining = TC_AES_BLOCK_SIZE - s->leftover_offset; + + _set(&s->leftover[s->leftover_offset], 0, remaining); + s->leftover[s->leftover_offset] = TC_CMAC_PADDING; + k = (uint8_t *) s->K2; + } + for (i = 0; i < TC_AES_BLOCK_SIZE; ++i) { + s->iv[i] ^= s->leftover[i] ^ k[i]; + } + + tc_aes_encrypt(tag, s->iv, s->sched); + + /* erasing state: */ + tc_cmac_erase(s); + + return TC_CRYPTO_SUCCESS; +} + +int tc_cmac_erase(TCCmacState_t s) +{ + if (s == (TCCmacState_t) 0) { + return TC_CRYPTO_FAIL; + } + + /* destroy the current state */ + _set(s, 0, sizeof(*s)); + + return TC_CRYPTO_SUCCESS; +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c new file mode 100644 index 000000000..595b385ed --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_atomic.c @@ -0,0 +1,173 @@ +/** + * @brief Atomically set a bit. + * + * Atomically set bit number @a bit of @a target. + * The target may be a single atomic variable or an array of them. + * + * @param target Address of atomic variable or array. + * @param bit Bit number (starting from 0). + * + * @return N/A + */ + +/* + * Copyright (c) 2016 Intel Corporation + * Copyright (c) 2011-2014 Wind River Systems, Inc. + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mesh_atomic.h" +#include "mesh_mutex.h" + +#ifndef CONFIG_ATOMIC_OPERATIONS_BUILTIN + +/** +* +* @brief Atomic get primitive +* +* @param target memory location to read from +* +* This routine provides the atomic get primitive to atomically read +* a value from . It simply does an ordinary load. Note that +* is expected to be aligned to a 4-byte boundary. +* +* @return The value read from +*/ +bt_mesh_atomic_val_t bt_mesh_atomic_get(const bt_mesh_atomic_t *target) +{ + return *target; +} + +/** + * + * @brief Atomic get-and-set primitive + * + * This routine provides the atomic set operator. The is atomically + * written at and the previous value at is returned. + * + * @param target the memory location to write to + * @param value the value to write + * + * @return The previous value from + */ +bt_mesh_atomic_val_t bt_mesh_atomic_set(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + bt_mesh_atomic_val_t ret = 0; + + bt_mesh_atomic_lock(); + + ret = *target; + *target = value; + + bt_mesh_atomic_unlock(); + + return ret; +} + +/** + * + * @brief Atomic bitwise inclusive OR primitive + * + * This routine provides the atomic bitwise inclusive OR operator. The + * is atomically bitwise OR'ed with the value at , placing the result + * at , and the previous value at is returned. + * + * @param target the memory location to be modified + * @param value the value to OR + * + * @return The previous value from + */ +bt_mesh_atomic_val_t bt_mesh_atomic_or(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + bt_mesh_atomic_val_t ret = 0; + + bt_mesh_atomic_lock(); + + ret = *target; + *target |= value; + + bt_mesh_atomic_unlock(); + + return ret; +} + +/** + * + * @brief Atomic bitwise AND primitive + * + * This routine provides the atomic bitwise AND operator. The is + * atomically bitwise AND'ed with the value at , placing the result + * at , and the previous value at is returned. + * + * @param target the memory location to be modified + * @param value the value to AND + * + * @return The previous value from + */ +bt_mesh_atomic_val_t bt_mesh_atomic_and(bt_mesh_atomic_t *target, bt_mesh_atomic_val_t value) +{ + bt_mesh_atomic_val_t ret = 0; + + bt_mesh_atomic_lock(); + + ret = *target; + *target &= value; + + bt_mesh_atomic_unlock(); + + return ret; +} + +/** + * + * @brief Atomic decrement primitive + * + * @param target memory location to decrement + * + * This routine provides the atomic decrement operator. The value at + * is atomically decremented by 1, and the old value from is returned. + * + * @return The value from prior to the decrement + */ +bt_mesh_atomic_val_t bt_mesh_atomic_dec(bt_mesh_atomic_t *target) +{ + bt_mesh_atomic_val_t ret = 0; + + bt_mesh_atomic_lock(); + + ret = *target; + (*target)--; + + bt_mesh_atomic_unlock(); + + return ret; +} + +/** + * + * @brief Atomic increment primitive + * + * @param target memory location to increment + * + * This routine provides the atomic increment operator. The value at + * is atomically incremented by 1, and the old value from is returned. + * + * @return The value from before the increment + */ +bt_mesh_atomic_val_t bt_mesh_atomic_inc(bt_mesh_atomic_t *target) +{ + bt_mesh_atomic_val_t ret = 0; + + bt_mesh_atomic_lock(); + + ret = *target; + (*target)++; + + bt_mesh_atomic_unlock(); + + return ret; +} + +#endif /* #ifndef CONFIG_ATOMIC_OPERATIONS_BUILTIN */ diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c new file mode 100644 index 000000000..10a9e6566 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_buf.c @@ -0,0 +1,760 @@ +/* + * Copyright (c) 2015 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "mesh_common.h" + +int net_buf_id(struct net_buf *buf) +{ + struct net_buf_pool *pool = buf->pool; + + return buf - pool->__bufs; +} + +static inline struct net_buf *pool_get_uninit(struct net_buf_pool *pool, + u16_t uninit_count) +{ + struct net_buf *buf = NULL; + + buf = &pool->__bufs[pool->buf_count - uninit_count]; + + buf->pool = pool; + + return buf; +} + +void net_buf_simple_clone(const struct net_buf_simple *original, + struct net_buf_simple *clone) +{ + memcpy(clone, original, sizeof(struct net_buf_simple)); +} + +void *net_buf_simple_add(struct net_buf_simple *buf, size_t len) +{ + u8_t *tail = net_buf_simple_tail(buf); + + NET_BUF_SIMPLE_DBG("buf %p len %u", buf, len); + + NET_BUF_SIMPLE_ASSERT(net_buf_simple_tailroom(buf) >= len); + + buf->len += len; + return tail; +} + +void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem, + size_t len) +{ + NET_BUF_SIMPLE_DBG("buf %p len %u", buf, len); + + return memcpy(net_buf_simple_add(buf, len), mem, len); +} + +u8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, u8_t val) +{ + u8_t *u8 = NULL; + + NET_BUF_SIMPLE_DBG("buf %p val 0x%02x", buf, val); + + u8 = net_buf_simple_add(buf, 1); + *u8 = val; + + return u8; +} + +void net_buf_simple_add_le16(struct net_buf_simple *buf, u16_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le16(val, net_buf_simple_add(buf, sizeof(val))); +} + +void net_buf_simple_add_be16(struct net_buf_simple *buf, u16_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be16(val, net_buf_simple_add(buf, sizeof(val))); +} + +void net_buf_simple_add_le24(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le24(val, net_buf_simple_add(buf, 3)); +} + +void net_buf_simple_add_be24(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be24(val, net_buf_simple_add(buf, 3)); +} + +void net_buf_simple_add_le32(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le32(val, net_buf_simple_add(buf, sizeof(val))); +} + +void net_buf_simple_add_be32(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be32(val, net_buf_simple_add(buf, sizeof(val))); +} + +void net_buf_simple_add_le48(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_le48(val, net_buf_simple_add(buf, 6)); +} + +void net_buf_simple_add_be48(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_be48(val, net_buf_simple_add(buf, 6)); +} + +void net_buf_simple_add_le64(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_le64(val, net_buf_simple_add(buf, sizeof(val))); +} + +void net_buf_simple_add_be64(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_be64(val, net_buf_simple_add(buf, sizeof(val))); +} + +void *net_buf_simple_push(struct net_buf_simple *buf, size_t len) +{ + NET_BUF_SIMPLE_DBG("buf %p len %u", buf, len); + + NET_BUF_SIMPLE_ASSERT(net_buf_simple_headroom(buf) >= len); + + buf->data -= len; + buf->len += len; + return buf->data; +} + +void net_buf_simple_push_le16(struct net_buf_simple *buf, u16_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le16(val, net_buf_simple_push(buf, sizeof(val))); +} + +void net_buf_simple_push_be16(struct net_buf_simple *buf, u16_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be16(val, net_buf_simple_push(buf, sizeof(val))); +} + +void net_buf_simple_push_u8(struct net_buf_simple *buf, u8_t val) +{ + u8_t *data = net_buf_simple_push(buf, 1); + + *data = val; +} + +void net_buf_simple_push_le24(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le24(val, net_buf_simple_push(buf, 3)); +} + +void net_buf_simple_push_be24(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be24(val, net_buf_simple_push(buf, 3)); +} + +void net_buf_simple_push_le32(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_le32(val, net_buf_simple_push(buf, sizeof(val))); +} + +void net_buf_simple_push_be32(struct net_buf_simple *buf, u32_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val); + + sys_put_be32(val, net_buf_simple_push(buf, sizeof(val))); +} + +void net_buf_simple_push_le48(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_le48(val, net_buf_simple_push(buf, 6)); +} + +void net_buf_simple_push_be48(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_be48(val, net_buf_simple_push(buf, 6)); +} + +void net_buf_simple_push_le64(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_le64(val, net_buf_simple_push(buf, sizeof(val))); +} + +void net_buf_simple_push_be64(struct net_buf_simple *buf, u64_t val) +{ + NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val); + + sys_put_be64(val, net_buf_simple_push(buf, sizeof(val))); +} + +void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len) +{ + NET_BUF_SIMPLE_DBG("buf %p len %u", buf, len); + + NET_BUF_SIMPLE_ASSERT(buf->len >= len); + + buf->len -= len; + return buf->data += len; +} + +void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len) +{ + void *data = buf->data; + + NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len); + + NET_BUF_SIMPLE_ASSERT(buf->len >= len); + + buf->len -= len; + buf->data += len; + + return data; +} + +u8_t net_buf_simple_pull_u8(struct net_buf_simple *buf) +{ + u8_t val = 0U; + + val = buf->data[0]; + net_buf_simple_pull(buf, 1); + + return val; +} + +u16_t net_buf_simple_pull_le16(struct net_buf_simple *buf) +{ + u16_t val = 0U; + + val = UNALIGNED_GET((u16_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_le16_to_cpu(val); +} + +u16_t net_buf_simple_pull_be16(struct net_buf_simple *buf) +{ + u16_t val = 0U; + + val = UNALIGNED_GET((u16_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_be16_to_cpu(val); +} + +u32_t net_buf_simple_pull_le24(struct net_buf_simple *buf) +{ + struct uint24 { + u32_t u24:24; + } __packed val; + + val = UNALIGNED_GET((struct uint24 *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_le24_to_cpu(val.u24); +} + +u32_t net_buf_simple_pull_be24(struct net_buf_simple *buf) +{ + struct uint24 { + u32_t u24:24; + } __packed val; + + val = UNALIGNED_GET((struct uint24 *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_be24_to_cpu(val.u24); +} + +u32_t net_buf_simple_pull_le32(struct net_buf_simple *buf) +{ + u32_t val = 0U; + + val = UNALIGNED_GET((u32_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_le32_to_cpu(val); +} + +u32_t net_buf_simple_pull_be32(struct net_buf_simple *buf) +{ + u32_t val = 0U; + + val = UNALIGNED_GET((u32_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_be32_to_cpu(val); +} + +u64_t net_buf_simple_pull_le48(struct net_buf_simple *buf) +{ + struct uint48 { + u64_t u48:48; + } __packed val; + + val = UNALIGNED_GET((struct uint48 *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_le48_to_cpu(val.u48); +} + +u64_t net_buf_simple_pull_be48(struct net_buf_simple *buf) +{ + struct uint48 { + u64_t u48:48; + } __packed val; + + val = UNALIGNED_GET((struct uint48 *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_be48_to_cpu(val.u48); +} + +u64_t net_buf_simple_pull_le64(struct net_buf_simple *buf) +{ + u64_t val; + + val = UNALIGNED_GET((u64_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_le64_to_cpu(val); +} + +u64_t net_buf_simple_pull_be64(struct net_buf_simple *buf) +{ + u64_t val; + + val = UNALIGNED_GET((u64_t *)buf->data); + net_buf_simple_pull(buf, sizeof(val)); + + return sys_be64_to_cpu(val); +} + +size_t net_buf_simple_headroom(struct net_buf_simple *buf) +{ + return buf->data - buf->__buf; +} + +size_t net_buf_simple_tailroom(struct net_buf_simple *buf) +{ + return buf->size - net_buf_simple_headroom(buf) - buf->len; +} + +void net_buf_reset(struct net_buf *buf) +{ + NET_BUF_ASSERT(buf->flags == 0); + NET_BUF_ASSERT(buf->frags == NULL); + + net_buf_simple_reset(&buf->b); +} + +void net_buf_simple_init_with_data(struct net_buf_simple *buf, + void *data, size_t size) +{ + buf->__buf = data; + buf->data = data; + buf->size = size; + buf->len = size; +} + +void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve) +{ + NET_BUF_ASSERT(buf); + NET_BUF_ASSERT(buf->len == 0U); + NET_BUF_DBG("buf %p reserve %zu", buf, reserve); + + buf->data = buf->__buf + reserve; +} + +void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) +{ + struct net_buf *tail = NULL; + + NET_BUF_ASSERT(list); + NET_BUF_ASSERT(buf); + + for (tail = buf; tail->frags; tail = tail->frags) { + tail->flags |= NET_BUF_FRAGS; + } + + bt_mesh_list_lock(); + sys_slist_append_list(list, &buf->node, &tail->node); + bt_mesh_list_unlock(); +} + +struct net_buf *net_buf_slist_get(sys_slist_t *list) +{ + struct net_buf *buf = NULL, *frag = NULL; + + NET_BUF_ASSERT(list); + + bt_mesh_list_lock(); + buf = (void *)sys_slist_get(list); + bt_mesh_list_unlock(); + + if (!buf) { + return NULL; + } + + /* Get any fragments belonging to this buffer */ + for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) { + bt_mesh_list_lock(); + frag->frags = (void *)sys_slist_get(list); + bt_mesh_list_unlock(); + + NET_BUF_ASSERT(frag->frags); + + /* The fragments flag is only for list-internal usage */ + frag->flags &= ~NET_BUF_FRAGS; + } + + /* Mark the end of the fragment list */ + frag->frags = NULL; + + return buf; +} + +struct net_buf *net_buf_ref(struct net_buf *buf) +{ + NET_BUF_ASSERT(buf); + + NET_BUF_DBG("buf %p (old) ref %u pool %p", buf, buf->ref, buf->pool); + + buf->ref++; + return buf; +} + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +void net_buf_unref_debug(struct net_buf *buf, const char *func, int line) +#else +void net_buf_unref(struct net_buf *buf) +#endif +{ + NET_BUF_ASSERT(buf); + + while (buf) { + struct net_buf *frags = buf->frags; + struct net_buf_pool *pool = NULL; + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) + if (!buf->ref) { + NET_BUF_ERR("%s():%d: buf %p double free", func, line, + buf); + return; + } +#endif + NET_BUF_DBG("buf %p ref %u pool %p frags %p", buf, buf->ref, + buf->pool, buf->frags); + + /* Changed by Espressif. Add !buf->ref to avoid minus 0 */ + if (!buf->ref || --buf->ref > 0) { + return; + } + + buf->frags = NULL; + + pool = buf->pool; + + pool->uninit_count++; +#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) + pool->avail_count++; + NET_BUF_DBG("%s, pool %p, avail_count %d, uninit_count %d", __func__, + pool, pool->avail_count, pool->uninit_count); + NET_BUF_ASSERT(pool->avail_count <= pool->buf_count); +#endif + + if (pool->destroy) { + pool->destroy(buf); + } + + buf = frags; + } +} + +static u8_t *fixed_data_alloc(struct net_buf *buf, size_t *size, s32_t timeout) +{ + struct net_buf_pool *pool = buf->pool; + const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data; + + *size = MIN(fixed->data_size, *size); + + return fixed->data_pool + fixed->data_size * net_buf_id(buf); +} + +static void fixed_data_unref(struct net_buf *buf, u8_t *data) +{ + /* Nothing needed for fixed-size data pools */ +} + +const struct net_buf_data_cb net_buf_fixed_cb = { + .alloc = fixed_data_alloc, + .unref = fixed_data_unref, +}; + +static u8_t *data_alloc(struct net_buf *buf, size_t *size, s32_t timeout) +{ + struct net_buf_pool *pool = buf->pool; + + return pool->alloc->cb->alloc(buf, size, timeout); +} + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +struct net_buf *net_buf_alloc_len_debug(struct net_buf_pool *pool, size_t size, + s32_t timeout, const char *func, int line) +#else +struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size, + s32_t timeout) +#endif +{ + struct net_buf *buf = NULL; + int i; + + NET_BUF_ASSERT(pool); + + NET_BUF_DBG("%s, pool %p, uninit_count %d, buf_count %d", __func__, + pool, pool->uninit_count, pool->buf_count); + + /* We need to lock interrupts temporarily to prevent race conditions + * when accessing pool->uninit_count. + */ + bt_mesh_buf_lock(); + + /* If there are uninitialized buffers we're guaranteed to succeed + * with the allocation one way or another. + */ + if (pool->uninit_count) { + /* Changed by Espressif. Use buf when buf->ref is 0 */ + for (i = pool->buf_count; i > 0; i--) { + buf = pool_get_uninit(pool, i); + if (!buf->ref) { + bt_mesh_buf_unlock(); + goto success; + } + } + } + + bt_mesh_buf_unlock(); + + NET_BUF_ERR("%s, Failed to get free buffer", __func__); + return NULL; + +success: + NET_BUF_DBG("allocated buf %p", buf); + + if (size) { + buf->__buf = data_alloc(buf, &size, timeout); + if (!buf->__buf) { + NET_BUF_ERR("%s, Failed to allocate data", __func__); + return NULL; + } + } else { + NET_BUF_WARN("%s, Zero data size", __func__); + buf->__buf = NULL; + } + + buf->ref = 1; + buf->flags = 0; + buf->frags = NULL; + buf->size = size; + net_buf_reset(buf); + + pool->uninit_count--; +#if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE) + pool->avail_count--; + NET_BUF_ASSERT(pool->avail_count >= 0); +#endif + + return buf; +} + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool, + s32_t timeout, const char *func, + int line) +{ + const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data; + + return net_buf_alloc_len_debug(pool, fixed->data_size, timeout, func, line); +} +#else +struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool, s32_t timeout) +{ + const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data; + + return net_buf_alloc_len(pool, fixed->data_size, timeout); +} +#endif + +struct net_buf *net_buf_frag_last(struct net_buf *buf) +{ + NET_BUF_ASSERT(buf); + + while (buf->frags) { + buf = buf->frags; + } + + return buf; +} + +void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag) +{ + NET_BUF_ASSERT(parent); + NET_BUF_ASSERT(frag); + + if (parent->frags) { + net_buf_frag_last(frag)->frags = parent->frags; + } + /* Take ownership of the fragment reference */ + parent->frags = frag; +} + +struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag) +{ + NET_BUF_ASSERT(frag); + + if (!head) { + return net_buf_ref(frag); + } + + net_buf_frag_insert(net_buf_frag_last(head), frag); + + return head; +} + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) +struct net_buf *net_buf_frag_del_debug(struct net_buf *parent, + struct net_buf *frag, + const char *func, int line) +#else +struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag) +#endif +{ + struct net_buf *next_frag = NULL; + + NET_BUF_ASSERT(frag); + + if (parent) { + NET_BUF_ASSERT(parent->frags); + NET_BUF_ASSERT(parent->frags == frag); + parent->frags = frag->frags; + } + + next_frag = frag->frags; + + frag->frags = NULL; + +#if defined(CONFIG_BLE_MESH_NET_BUF_LOG) + net_buf_unref_debug(frag, func, line); +#else + net_buf_unref(frag); +#endif + + return next_frag; +} + +size_t net_buf_linearize(void *dst, size_t dst_len, struct net_buf *src, + size_t offset, size_t len) +{ + struct net_buf *frag = NULL; + size_t to_copy = 0U; + size_t copied = 0U; + + len = MIN(len, dst_len); + + frag = src; + + /* find the right fragment to start copying from */ + while (frag && offset >= frag->len) { + offset -= frag->len; + frag = frag->frags; + } + + /* traverse the fragment chain until len bytes are copied */ + copied = 0; + while (frag && len > 0) { + to_copy = MIN(len, frag->len - offset); + memcpy((u8_t *)dst + copied, frag->data + offset, to_copy); + + copied += to_copy; + + /* to_copy is always <= len */ + len -= to_copy; + frag = frag->frags; + + /* after the first iteration, this value will be 0 */ + offset = 0; + } + + return copied; +} + +/* This helper routine will append multiple bytes, if there is no place for + * the data in current fragment then create new fragment and add it to + * the buffer. It assumes that the buffer has at least one fragment. + */ +size_t net_buf_append_bytes(struct net_buf *buf, size_t len, + const void *value, s32_t timeout, + net_buf_allocator_cb allocate_cb, void *user_data) +{ + struct net_buf *frag = net_buf_frag_last(buf); + size_t added_len = 0U; + const u8_t *value8 = value; + + do { + u16_t count = MIN(len, net_buf_tailroom(frag)); + + net_buf_add_mem(frag, value8, count); + len -= count; + added_len += count; + value8 += count; + + if (len == 0) { + return added_len; + } + + frag = allocate_cb(timeout, user_data); + if (!frag) { + return added_len; + } + + net_buf_frag_add(buf, frag); + } while (1); + + /* Unreachable */ + return 0; +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c new file mode 100644 index 000000000..8ab86266e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c @@ -0,0 +1,67 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "mesh_main.h" +#include "client_common.h" +#include "mesh_common.h" + +struct net_buf_simple *bt_mesh_alloc_buf(u16_t size) +{ + struct net_buf_simple *buf = NULL; + u8_t *data = NULL; + + buf = (struct net_buf_simple *)bt_mesh_calloc(sizeof(struct net_buf_simple) + size); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + return NULL; + } + + data = (u8_t *)buf + sizeof(struct net_buf_simple); + + buf->data = data; + buf->len = 0; + buf->size = size; + buf->__buf = data; + + return buf; +} + +void bt_mesh_free_buf(struct net_buf_simple *buf) +{ + if (buf) { + bt_mesh_free(buf); + } +} + +u8_t bt_mesh_get_device_role(struct bt_mesh_model *model, bool srv_send) +{ + bt_mesh_client_user_data_t *client = NULL; + + if (srv_send) { + BT_DBG("%s, Message is sent by a server model", __func__); + return NODE; + } + + if (!model || !model->user_data) { + BT_ERR("%s, Invalid parameter", __func__); + return ROLE_NVAL; + } + + client = (bt_mesh_client_user_data_t *)model->user_data; + + return client->msg_role; +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c new file mode 100644 index 000000000..b288f6500 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_kernel.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2016 Intel Corporation + * Copyright (c) 2016 Wind River Systems, Inc. + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mesh_kernel.h" + +void k_sleep(s32_t duration) +{ + vTaskDelay(duration / portTICK_PERIOD_MS); +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c new file mode 100644 index 000000000..1bd6a2358 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c @@ -0,0 +1,183 @@ +// Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "mesh_common.h" + +static bt_mesh_mutex_t alarm_lock; +static bt_mesh_mutex_t list_lock; +static bt_mesh_mutex_t buf_lock; +static bt_mesh_mutex_t atomic_lock; + +void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + +#if CONFIG_SPIRAM_USE_MALLOC + mutex->buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(mutex->buffer, "%s, Failed to create queue buffer", __func__); + mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer); + __ASSERT(mutex->mutex, "%s, Failed to create static mutex", __func__); +#else + mutex->mutex = xSemaphoreCreateMutex(); + __ASSERT(mutex->mutex, "%s, Failed to create mutex", __func__); +#endif +} + +void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + vSemaphoreDelete(mutex->mutex); + mutex->mutex = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(mutex->buffer); + mutex->buffer = NULL; +#endif + } +} + +void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + xSemaphoreTake(mutex->mutex, portMAX_DELAY); + } +} + +void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("%s, Invalid mutex", __func__); + return; + } + + if (mutex->mutex) { + xSemaphoreGive(mutex->mutex); + } +} + +static void bt_mesh_alarm_mutex_new(void) +{ + if (!alarm_lock.mutex) { + bt_mesh_mutex_create(&alarm_lock); + } +} + +static void bt_mesh_alarm_mutex_free(void) +{ + bt_mesh_mutex_free(&alarm_lock); +} + +void bt_mesh_alarm_lock(void) +{ + bt_mesh_mutex_lock(&alarm_lock); +} + +void bt_mesh_alarm_unlock(void) +{ + bt_mesh_mutex_unlock(&alarm_lock); +} + +static void bt_mesh_list_mutex_new(void) +{ + if (!list_lock.mutex) { + bt_mesh_mutex_create(&list_lock); + } +} + +static void bt_mesh_list_mutex_free(void) +{ + bt_mesh_mutex_free(&list_lock); +} + +void bt_mesh_list_lock(void) +{ + bt_mesh_mutex_lock(&list_lock); +} + +void bt_mesh_list_unlock(void) +{ + bt_mesh_mutex_unlock(&list_lock); +} + +static void bt_mesh_buf_mutex_new(void) +{ + if (!buf_lock.mutex) { + bt_mesh_mutex_create(&buf_lock); + } +} + +static void bt_mesh_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&buf_lock); +} + +void bt_mesh_buf_lock(void) +{ + bt_mesh_mutex_lock(&buf_lock); +} + +void bt_mesh_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&buf_lock); +} + +static void bt_mesh_atomic_mutex_new(void) +{ + if (!atomic_lock.mutex) { + bt_mesh_mutex_create(&atomic_lock); + } +} + +static void bt_mesh_atomic_mutex_free(void) +{ + bt_mesh_mutex_free(&atomic_lock); +} + +void bt_mesh_atomic_lock(void) +{ + bt_mesh_mutex_lock(&atomic_lock); +} + +void bt_mesh_atomic_unlock(void) +{ + bt_mesh_mutex_unlock(&atomic_lock); +} + +void bt_mesh_mutex_init(void) +{ + bt_mesh_alarm_mutex_new(); + bt_mesh_list_mutex_new(); + bt_mesh_buf_mutex_new(); + bt_mesh_atomic_mutex_new(); +} + +void bt_mesh_mutex_deinit(void) +{ + bt_mesh_alarm_mutex_free(); + bt_mesh_list_mutex_free(); + bt_mesh_buf_mutex_free(); + bt_mesh_atomic_mutex_free(); +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c new file mode 100644 index 000000000..50fe7813e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_timer.c @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2016 Intel Corporation + * Copyright (c) 2016 Wind River Systems, Inc. + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "osi/hash_map.h" +#include "osi/alarm.h" +#include "osi/hash_functions.h" + +#include "mesh_common.h" +#include "provisioner_prov.h" + +static hash_map_t *bm_alarm_hash_map; +static const size_t BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE = 20 + CONFIG_BLE_MESH_PBA_SAME_TIME + \ + CONFIG_BLE_MESH_PBG_SAME_TIME; + +typedef struct alarm_t { + /* timer id point to here */ + esp_timer_handle_t alarm_hdl; + osi_alarm_callback_t cb; + void *cb_data; + int64_t deadline_us; +} osi_alarm_t; + +s64_t k_uptime_get(void) +{ + /** k_uptime_get_32 is in in milliseconds, + * but esp_timer_get_time is in microseconds + */ + return (esp_timer_get_time() / 1000); +} + +u32_t k_uptime_get_32(void) +{ + /** k_uptime_get_32 is in in milliseconds, + * but esp_timer_get_time is in microseconds + */ + return (u32_t)(esp_timer_get_time() / 1000); +} + +void bt_mesh_timer_init(void) +{ + bm_alarm_hash_map = hash_map_new(BLE_MESH_GENERAL_ALARM_HASH_MAP_SIZE, + hash_function_pointer, NULL, + (data_free_fn)osi_alarm_free, NULL); + __ASSERT(bm_alarm_hash_map, "%s, Failed to create hash map", __func__); +} + +void bt_mesh_timer_deinit(void) +{ + if (bm_alarm_hash_map) { + hash_map_free(bm_alarm_hash_map); + bm_alarm_hash_map = NULL; + } +} + +int k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler) +{ + osi_alarm_t *alarm = NULL; + + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + k_work_init(&work->work, handler); + + bt_mesh_alarm_lock(); + if (!hash_map_has_key(bm_alarm_hash_map, (void *)work)) { + alarm = osi_alarm_new("bt_mesh", (osi_alarm_callback_t)handler, (void *)&work->work, 0); + if (alarm == NULL) { + BT_ERR("%s, Alarm not created", __func__); + bt_mesh_alarm_unlock(); + return -EIO; + } + if (!hash_map_set(bm_alarm_hash_map, work, (void *)alarm)) { + BT_ERR("%s, Alarm not set", __func__); + bt_mesh_alarm_unlock(); + return -EIO; + } + } + + alarm = hash_map_get(bm_alarm_hash_map, work); + if (alarm == NULL) { + BT_ERR("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return -ENODEV; + } + + // Just init the work timer only, don't start it. + osi_alarm_cancel(alarm); + bt_mesh_alarm_unlock(); + return 0; +} + +int k_delayed_work_submit(struct k_delayed_work *work, s32_t delay) +{ + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_alarm_lock(); + osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + if (alarm == NULL) { + BT_WARN("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return -EINVAL; + } + + // Cancel the alarm first, before start the alarm. + osi_alarm_cancel(alarm); + osi_alarm_set(alarm, delay); + bt_mesh_alarm_unlock(); + return 0; +} + +int k_delayed_work_submit_periodic(struct k_delayed_work *work, s32_t period) +{ + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_alarm_lock(); + osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + if (alarm == NULL) { + BT_WARN("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return -EINVAL; + } + + /* Cancel the alarm first before starting it. */ + osi_alarm_cancel(alarm); + osi_alarm_set_periodic(alarm, period); + bt_mesh_alarm_unlock(); + return 0; +} + +int k_delayed_work_cancel(struct k_delayed_work *work) +{ + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_alarm_lock(); + osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + if (alarm == NULL) { + BT_WARN("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return -EINVAL; + } + + osi_alarm_cancel(alarm); + alarm->deadline_us = 0; + bt_mesh_alarm_unlock(); + return 0; +} + +int k_delayed_work_free(struct k_delayed_work *work) +{ + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_alarm_lock(); + osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, work); + if (alarm == NULL) { + BT_WARN("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return -EINVAL; + } + + osi_alarm_cancel(alarm); + hash_map_erase(bm_alarm_hash_map, work); + bt_mesh_alarm_unlock(); + return 0; +} + +s32_t k_delayed_work_remaining_get(struct k_delayed_work *work) +{ + s32_t time = 0; + + if (!work || !bm_alarm_hash_map) { + BT_ERR("%s, Invalid parameter", __func__); + return 0; + } + + bt_mesh_alarm_lock(); + osi_alarm_t *alarm = hash_map_get(bm_alarm_hash_map, (void *)work); + if (alarm == NULL) { + BT_WARN("%s, Alarm not found", __func__); + bt_mesh_alarm_unlock(); + return 0; + } + + time = osi_alarm_get_remaining_ms(alarm); + bt_mesh_alarm_unlock(); + return time; +} diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_util.c b/components/bt/esp_ble_mesh/mesh_common/mesh_util.c new file mode 100644 index 000000000..83949594b --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_util.c @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2016 Vinayak Kariappa Chettimada + * Copyright (c) 2015-2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "mesh_types.h" +#include "mesh_util.h" +#include "mesh_aes_encrypt.h" + +#define MASK_TWENTY_SEVEN 0x1b + +const char *bt_hex(const void *buf, size_t len) +{ + static const char hex[] = "0123456789abcdef"; + static char str[129]; + const u8_t *b = buf; + int i; + + len = MIN(len, (sizeof(str) - 1) / 2); + + for (i = 0; i < len; i++) { + str[i * 2] = hex[b[i] >> 4]; + str[i * 2 + 1] = hex[b[i] & 0xf]; + } + + str[i * 2] = '\0'; + + return str; +} + +void mem_rcopy(u8_t *dst, u8_t const *src, u16_t len) +{ + src += len; + while (len--) { + *dst++ = *--src; + } +} + +unsigned int _copy(uint8_t *to, unsigned int to_len, + const uint8_t *from, unsigned int from_len) +{ + if (from_len <= to_len) { + (void)memcpy(to, from, from_len); + return from_len; + } else { + return TC_CRYPTO_FAIL; + } +} + +void _set(void *to, uint8_t val, unsigned int len) +{ + (void)memset(to, val, len); +} + +/* + * Doubles the value of a byte for values up to 127. + */ +uint8_t _double_byte(uint8_t a) +{ + return ((a << 1) ^ ((a >> 7) * MASK_TWENTY_SEVEN)); +} + +int _compare(const uint8_t *a, const uint8_t *b, size_t size) +{ + const uint8_t *tempa = a; + const uint8_t *tempb = b; + uint8_t result = 0; + + for (unsigned int i = 0; i < size; i++) { + result |= tempa[i] ^ tempb[i]; + } + return result; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/access.c b/components/bt/esp_ble_mesh/mesh_core/access.c new file mode 100644 index 000000000..2711e724b --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/access.c @@ -0,0 +1,1337 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_ACCESS) + +#include "mesh.h" +#include "adv.h" +#include "transport.h" +#include "access.h" +#include "foundation.h" +#include "mesh_main.h" +#include "mesh_common.h" +#include "provisioner_main.h" + +#include "generic_client.h" +#include "sensor_client.h" +#include "time_scene_client.h" +#include "lighting_client.h" + +#include "generic_server.h" +#include "sensor_server.h" +#include "time_scene_server.h" +#include "lighting_server.h" + +#define BLE_MESH_SDU_MAX_LEN 384 + +static const struct bt_mesh_comp *dev_comp; +static u16_t dev_primary_addr; + +static const struct { + const u16_t id; + int (*const init)(struct bt_mesh_model *model, bool primary); +} model_init[] = { + { BLE_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_init }, + { BLE_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_init }, +#if defined(CONFIG_BLE_MESH_CFG_CLI) + { BLE_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_HEALTH_CLI) + { BLE_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, bt_mesh_gen_onoff_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, bt_mesh_gen_level_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI) + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, bt_mesh_gen_def_trans_time_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, bt_mesh_gen_pwr_onoff_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, bt_mesh_gen_pwr_level_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_BATTERY_CLI) + { BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, bt_mesh_gen_battery_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LOCATION_CLI) + { BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, bt_mesh_gen_location_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_PROPERTY_CLI) + { BLE_MESH_MODEL_ID_GEN_PROP_CLI, bt_mesh_gen_property_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_SENSOR_CLI) + { BLE_MESH_MODEL_ID_SENSOR_CLI, bt_mesh_sensor_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_TIME_CLI) + { BLE_MESH_MODEL_ID_TIME_CLI, bt_mesh_time_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_SCENE_CLI) + { BLE_MESH_MODEL_ID_SCENE_CLI, bt_mesh_scene_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_SCHEDULER_CLI) + { BLE_MESH_MODEL_ID_SCHEDULER_CLI, bt_mesh_scheduler_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LIGHTNESS_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, bt_mesh_light_lightness_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_CTL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, bt_mesh_light_ctl_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_HSL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, bt_mesh_light_hsl_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_XYL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, bt_mesh_light_xyl_cli_init }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LC_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LC_CLI, bt_mesh_light_lc_cli_init }, +#endif + { BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, bt_mesh_gen_onoff_srv_init }, + { BLE_MESH_MODEL_ID_GEN_LEVEL_SRV, bt_mesh_gen_level_srv_init }, + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV, bt_mesh_gen_def_trans_time_srv_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV, bt_mesh_gen_power_onoff_srv_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV, bt_mesh_gen_power_onoff_setup_srv_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV, bt_mesh_gen_power_level_srv_init }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV, bt_mesh_gen_power_level_setup_srv_init }, + { BLE_MESH_MODEL_ID_GEN_BATTERY_SRV, bt_mesh_gen_battery_srv_init }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SRV, bt_mesh_gen_location_srv_init }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV, bt_mesh_gen_location_setup_srv_init }, + { BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV, bt_mesh_gen_user_prop_srv_init }, + { BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV, bt_mesh_gen_admin_prop_srv_init }, + { BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV, bt_mesh_gen_manu_prop_srv_init }, + { BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV, bt_mesh_gen_client_prop_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV, bt_mesh_light_lightness_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV, bt_mesh_light_lightness_setup_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SRV, bt_mesh_light_ctl_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV, bt_mesh_light_ctl_setup_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV, bt_mesh_light_ctl_temp_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SRV, bt_mesh_light_hsl_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV, bt_mesh_light_hsl_hue_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV, bt_mesh_light_hsl_sat_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV, bt_mesh_light_hsl_setup_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SRV, bt_mesh_light_xyl_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV, bt_mesh_light_xyl_setup_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SRV, bt_mesh_light_lc_srv_init }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV, bt_mesh_light_lc_setup_srv_init }, + { BLE_MESH_MODEL_ID_TIME_SRV, bt_mesh_time_srv_init }, + { BLE_MESH_MODEL_ID_TIME_SETUP_SRV, bt_mesh_time_setup_srv_init }, + { BLE_MESH_MODEL_ID_SCENE_SRV, bt_mesh_scene_srv_init }, + { BLE_MESH_MODEL_ID_SCENE_SETUP_SRV, bt_mesh_scene_setup_srv_init }, + { BLE_MESH_MODEL_ID_SCHEDULER_SRV, bt_mesh_scheduler_srv_init }, + { BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV, bt_mesh_scheduler_setup_srv_init }, + { BLE_MESH_MODEL_ID_SENSOR_SRV, bt_mesh_sensor_srv_init }, + { BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV, bt_mesh_sensor_setup_srv_init }, +}; + +static const struct { + const u16_t id; + int (*const deinit)(struct bt_mesh_model *model, bool primary); +} model_deinit[] = { + { BLE_MESH_MODEL_ID_CFG_SRV, bt_mesh_cfg_srv_deinit }, + { BLE_MESH_MODEL_ID_HEALTH_SRV, bt_mesh_health_srv_deinit }, +#if defined(CONFIG_BLE_MESH_CFG_CLI) + { BLE_MESH_MODEL_ID_CFG_CLI, bt_mesh_cfg_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_HEALTH_CLI) + { BLE_MESH_MODEL_ID_HEALTH_CLI, bt_mesh_health_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, bt_mesh_gen_onoff_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, bt_mesh_gen_level_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_DEF_TRANS_TIME_CLI) + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, bt_mesh_gen_def_trans_time_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_ONOFF_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, bt_mesh_gen_pwr_onoff_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_POWER_LEVEL_CLI) + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, bt_mesh_gen_pwr_level_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_BATTERY_CLI) + { BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, bt_mesh_gen_battery_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_LOCATION_CLI) + { BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, bt_mesh_gen_location_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_GENERIC_PROPERTY_CLI) + { BLE_MESH_MODEL_ID_GEN_PROP_CLI, bt_mesh_gen_property_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SENSOR_CLI) + { BLE_MESH_MODEL_ID_SENSOR_CLI, bt_mesh_sensor_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_TIME_CLI) + { BLE_MESH_MODEL_ID_TIME_CLI, bt_mesh_time_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SCENE_CLI) + { BLE_MESH_MODEL_ID_SCENE_CLI, bt_mesh_scene_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_SCHEDULER_CLI) + { BLE_MESH_MODEL_ID_SCHEDULER_CLI, bt_mesh_scheduler_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LIGHTNESS_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, bt_mesh_light_lightness_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_CTL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, bt_mesh_light_ctl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_HSL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, bt_mesh_light_hsl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_XYL_CLI) + { BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, bt_mesh_light_xyl_cli_deinit }, +#endif +#if defined(CONFIG_BLE_MESH_LIGHT_LC_CLI) + { BLE_MESH_MODEL_ID_LIGHT_LC_CLI, bt_mesh_light_lc_cli_deinit }, +#endif + { BLE_MESH_MODEL_ID_GEN_ONOFF_SRV, bt_mesh_gen_onoff_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LEVEL_SRV, bt_mesh_gen_level_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV, bt_mesh_gen_def_trans_time_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV, bt_mesh_gen_power_onoff_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV, bt_mesh_gen_power_onoff_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV, bt_mesh_gen_power_level_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV, bt_mesh_gen_power_level_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_BATTERY_SRV, bt_mesh_gen_battery_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SRV, bt_mesh_gen_location_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV, bt_mesh_gen_location_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV, bt_mesh_gen_user_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV, bt_mesh_gen_admin_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV, bt_mesh_gen_manu_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV, bt_mesh_gen_client_prop_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV, bt_mesh_light_lightness_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV, bt_mesh_light_lightness_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SRV, bt_mesh_light_ctl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV, bt_mesh_light_ctl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV, bt_mesh_light_ctl_temp_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SRV, bt_mesh_light_hsl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV, bt_mesh_light_hsl_hue_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV, bt_mesh_light_hsl_sat_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV, bt_mesh_light_hsl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SRV, bt_mesh_light_xyl_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV, bt_mesh_light_xyl_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SRV, bt_mesh_light_lc_srv_deinit }, + { BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV, bt_mesh_light_lc_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_TIME_SRV, bt_mesh_time_srv_deinit }, + { BLE_MESH_MODEL_ID_TIME_SETUP_SRV, bt_mesh_time_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SCENE_SRV, bt_mesh_scene_srv_deinit }, + { BLE_MESH_MODEL_ID_SCENE_SETUP_SRV, bt_mesh_scene_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SCHEDULER_SRV, bt_mesh_scheduler_srv_deinit }, + { BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV, bt_mesh_scheduler_setup_srv_deinit }, + { BLE_MESH_MODEL_ID_SENSOR_SRV, bt_mesh_sensor_srv_deinit }, + { BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV, bt_mesh_sensor_setup_srv_deinit }, +}; + +void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, + struct bt_mesh_elem *elem, + bool vnd, bool primary, + void *user_data), + void *user_data) +{ + int i, j; + + for (i = 0; i < dev_comp->elem_count; i++) { + struct bt_mesh_elem *elem = &dev_comp->elem[i]; + + for (j = 0; j < elem->model_count; j++) { + struct bt_mesh_model *model = &elem->models[j]; + + func(model, elem, false, i == 0, user_data); + } + + for (j = 0; j < elem->vnd_model_count; j++) { + struct bt_mesh_model *model = &elem->vnd_models[j]; + + func(model, elem, true, i == 0, user_data); + } + } +} + +s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod) +{ + int period = 0; + + if (!mod->pub) { + BT_ERR("%s, Model has no publication support", __func__); + return 0; + } + + switch (mod->pub->period >> 6) { + case 0x00: + /* 1 step is 100 ms */ + period = K_MSEC((mod->pub->period & BIT_MASK(6)) * 100U); + break; + case 0x01: + /* 1 step is 1 second */ + period = K_SECONDS(mod->pub->period & BIT_MASK(6)); + break; + case 0x02: + /* 1 step is 10 seconds */ + period = K_SECONDS((mod->pub->period & BIT_MASK(6)) * 10U); + break; + case 0x03: + /* 1 step is 10 minutes */ + period = K_MINUTES((mod->pub->period & BIT_MASK(6)) * 10U); + break; + default: + BT_ERR("%s, Unknown model publication period", __func__); + return 0; + } + + if (mod->pub->fast_period) { + return period >> mod->pub->period_div; + } else { + return period; + } +} + +static s32_t next_period(struct bt_mesh_model *mod) +{ + struct bt_mesh_model_pub *pub = mod->pub; + u32_t elapsed = 0U, period = 0U; + + if (!pub) { + BT_ERR("%s, Model has no publication support", __func__); + return -ENOTSUP; + } + + period = bt_mesh_model_pub_period_get(mod); + if (!period) { + return 0; + } + + elapsed = k_uptime_get_32() - pub->period_start; + + BT_INFO("Publishing took %ums", elapsed); + + if (elapsed >= period) { + BT_WARN("Publication sending took longer than the period"); + /* Return smallest positive number since 0 means disabled */ + return K_MSEC(1); + } + + return period - elapsed; +} + +static void publish_sent(int err, void *user_data) +{ + struct bt_mesh_model *mod = user_data; + s32_t delay = 0; + + BT_DBG("err %d", err); + + if (!mod->pub) { + BT_ERR("%s, Model has no publication support", __func__); + return; + } + + if (mod->pub->count) { + delay = BLE_MESH_PUB_TRANSMIT_INT(mod->pub->retransmit); + } else { + delay = next_period(mod); + } + + if (delay) { + BT_INFO("Publishing next time in %dms", delay); + k_delayed_work_submit(&mod->pub->timer, delay); + } +} + +static void publish_start(u16_t duration, int err, void *user_data) +{ + struct bt_mesh_model *mod = user_data; + struct bt_mesh_model_pub *pub = mod->pub; + + if (err) { + BT_ERR("Failed to publish: err %d", err); + return; + } + + /* Initialize the timestamp for the beginning of a new period */ + if (pub->count == BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit)) { + pub->period_start = k_uptime_get_32(); + } +} + +static const struct bt_mesh_send_cb pub_sent_cb = { + .start = publish_start, + .end = publish_sent, +}; + +static int publish_retransmit(struct bt_mesh_model *mod) +{ + struct bt_mesh_model_pub *pub = mod->pub; + if (!pub) { + BT_ERR("%s, Model has no publication support", __func__); + return -ENOTSUP; + } + + struct bt_mesh_app_key *key = NULL; + struct net_buf_simple *sdu = NULL; + struct bt_mesh_msg_ctx ctx = { + .addr = pub->addr, + .send_ttl = pub->ttl, + .model = mod, + .srv_send = (pub->dev_role == NODE ? true : false), + }; + struct bt_mesh_net_tx tx = { + .ctx = &ctx, + .src = bt_mesh_model_elem(mod)->addr, + .xmit = bt_mesh_net_transmit_get(), + .friend_cred = pub->cred, + }; + int err = 0; + + key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key); + if (!key) { + BT_ERR("%s, AppKey 0x%03x not exists", __func__, pub->key); + return -EADDRNOTAVAIL; + } + + tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, key->net_idx); + if (!tx.sub) { + BT_ERR("%s, Subnet 0x%04x not exists", __func__, key->net_idx); + return -EADDRNOTAVAIL; + } + + ctx.net_idx = key->net_idx; + ctx.app_idx = key->app_idx; + + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_SHORT); + if (!sdu) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + net_buf_simple_add_mem(sdu, pub->msg->data, pub->msg->len); + + pub->count--; + + err = bt_mesh_trans_send(&tx, sdu, &pub_sent_cb, mod); + + bt_mesh_free_buf(sdu); + return err; +} + +static void publish_retransmit_end(int err, struct bt_mesh_model_pub *pub) +{ + /* Cancel all retransmits for this publish attempt */ + pub->count = 0U; + /* Make sure the publish timer gets reset */ + publish_sent(err, pub->mod); +} + +static void mod_publish(struct k_work *work) +{ + struct bt_mesh_model_pub *pub = CONTAINER_OF(work, + struct bt_mesh_model_pub, + timer.work); + s32_t period_ms = 0; + int err = 0; + + BT_DBG("%s", __func__); + + period_ms = bt_mesh_model_pub_period_get(pub->mod); + BT_INFO("period %u ms", period_ms); + + if (pub->count) { + err = publish_retransmit(pub->mod); + if (err) { + BT_ERR("%s, Failed to retransmit (err %d)", __func__, err); + + pub->count = 0U; + + /* Continue with normal publication */ + if (period_ms) { + k_delayed_work_submit(&pub->timer, period_ms); + } + } + + return; + } + + if (!period_ms) { + return; + } + + __ASSERT_NO_MSG(pub->update != NULL); + + /* Callback the model publish update event to the application layer. + * In the event, users can update the context of the publish message + * which will be published in the next period. + */ + err = pub->update(pub->mod); + if (err) { + /* Cancel this publish attempt. */ + BT_ERR("Update failed, skipping publish (err %d)", err); + pub->period_start = k_uptime_get_32(); + publish_retransmit_end(err, pub); + return; + } + + err = bt_mesh_model_publish(pub->mod); + if (err) { + BT_ERR("%s, Publishing failed (err %d)", __func__, err); + } +} + +struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod) +{ + return &dev_comp->elem[mod->elem_idx]; +} + +struct bt_mesh_model *bt_mesh_model_get(bool vnd, u8_t elem_idx, u8_t mod_idx) +{ + struct bt_mesh_elem *elem = NULL; + + if (!dev_comp) { + BT_ERR("%s, dev_comp is not initialized", __func__); + return NULL; + } + + if (elem_idx >= dev_comp->elem_count) { + BT_ERR("%s, Invalid element index %u", __func__, elem_idx); + return NULL; + } + + elem = &dev_comp->elem[elem_idx]; + + if (vnd) { + if (mod_idx >= elem->vnd_model_count) { + BT_ERR("%s, Invalid vendor model index %u", __func__, mod_idx); + return NULL; + } + + return &elem->vnd_models[mod_idx]; + } else { + if (mod_idx >= elem->model_count) { + BT_ERR("%s, Invalid SIG model index %u", __func__, mod_idx); + return NULL; + } + + return &elem->models[mod_idx]; + } +} + +static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + int i; + + mod->elem = elem; + + if (mod->pub) { + mod->pub->mod = mod; + k_delayed_work_init(&mod->pub->timer, mod_publish); + } + + for (i = 0; i < ARRAY_SIZE(mod->keys); i++) { + mod->keys[i] = BLE_MESH_KEY_UNUSED; + } + + mod->flags = 0; + mod->elem_idx = elem - dev_comp->elem; + if (vnd) { + mod->model_idx = mod - elem->vnd_models; + } else { + mod->model_idx = mod - elem->models; + } + + if (vnd) { + return; + } + + for (i = 0; i < ARRAY_SIZE(model_init); i++) { + if (model_init[i].id == mod->id) { + model_init[i].init(mod, primary); + } + } +} + +static void mod_deinit(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + int i; + + mod->elem = NULL; + + if (mod->pub) { + mod->pub->mod = NULL; + k_delayed_work_free(&mod->pub->timer); + } + + for (i = 0; i < ARRAY_SIZE(mod->keys); i++) { + mod->keys[i] = BLE_MESH_KEY_UNUSED; + } + + mod->flags = 0U; + mod->elem_idx = 0U; + mod->model_idx = 0U; + + if (vnd) { + return; + } + + for (i = 0; i < ARRAY_SIZE(model_deinit); i++) { + if (model_deinit[i].id == mod->id) { + model_deinit[i].deinit(mod, primary); + } + } +} + +int bt_mesh_comp_register(const struct bt_mesh_comp *comp) +{ + /* There must be at least one element */ + if (!comp->elem_count) { + return -EINVAL; + } + + dev_comp = comp; + + bt_mesh_model_foreach(mod_init, NULL); + + return 0; +} + +int bt_mesh_comp_deregister(void) +{ + if (dev_comp == NULL) { + return -EINVAL; + } + + bt_mesh_model_foreach(mod_deinit, NULL); + + dev_primary_addr = BLE_MESH_ADDR_UNASSIGNED; + dev_comp = NULL; + + return 0; +} + +void bt_mesh_comp_provision(u16_t addr) +{ + int i; + + dev_primary_addr = addr; + + BT_INFO("Primary address 0x%04x, element count %u", addr, dev_comp->elem_count); + + for (i = 0; i < dev_comp->elem_count; i++) { + struct bt_mesh_elem *elem = &dev_comp->elem[i]; + + elem->addr = addr++; + + BT_DBG("addr 0x%04x mod_count %u vnd_mod_count %u", + elem->addr, elem->model_count, elem->vnd_model_count); + } +} + +void bt_mesh_comp_unprovision(void) +{ + BT_DBG("%s", __func__); + + dev_primary_addr = BLE_MESH_ADDR_UNASSIGNED; + + bt_mesh_model_foreach(mod_init, NULL); +} + +u16_t bt_mesh_primary_addr(void) +{ + return dev_primary_addr; +} + +u16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, u16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] == addr) { + return &mod->groups[i]; + } + } + + return NULL; +} + +static struct bt_mesh_model *bt_mesh_elem_find_group(struct bt_mesh_elem *elem, + u16_t group_addr) +{ + struct bt_mesh_model *model = NULL; + u16_t *match = NULL; + int i; + + for (i = 0; i < elem->model_count; i++) { + model = &elem->models[i]; + + match = bt_mesh_model_find_group(model, group_addr); + if (match) { + return model; + } + } + + for (i = 0; i < elem->vnd_model_count; i++) { + model = &elem->vnd_models[i]; + + match = bt_mesh_model_find_group(model, group_addr); + if (match) { + return model; + } + } + + return NULL; +} + +struct bt_mesh_elem *bt_mesh_elem_find(u16_t addr) +{ + u16_t index = 0U; + + if (BLE_MESH_ADDR_IS_UNICAST(addr)) { + index = (addr - dev_comp->elem[0].addr); + if (index < dev_comp->elem_count) { + return &dev_comp->elem[index]; + } else { + return NULL; + } + } + + for (index = 0; index < dev_comp->elem_count; index++) { + struct bt_mesh_elem *elem = &dev_comp->elem[index]; + + if (bt_mesh_elem_find_group(elem, addr)) { + return elem; + } + } + + return NULL; +} + +u8_t bt_mesh_elem_count(void) +{ + return dev_comp->elem_count; +} + +static bool model_has_key(struct bt_mesh_model *mod, u16_t key) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(mod->keys); i++) { + if (mod->keys[i] == key) { + return true; + } + } + + return false; +} + +static bool model_has_dst(struct bt_mesh_model *model, u16_t dst) +{ + if (BLE_MESH_ADDR_IS_UNICAST(dst)) { + return (dev_comp->elem[model->elem_idx].addr == dst); + } else if (BLE_MESH_ADDR_IS_GROUP(dst) || BLE_MESH_ADDR_IS_VIRTUAL(dst)) { + return !!bt_mesh_model_find_group(model, dst); + } + + return (model->elem_idx == 0 && bt_mesh_fixed_group_match(dst)); +} + +static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models, + u8_t model_count, u32_t opcode, + struct bt_mesh_model **model) +{ + int i; + + for (i = 0; i < model_count; i++) { + const struct bt_mesh_model_op *op; + + *model = &models[i]; + + for (op = (*model)->op; op->func; op++) { + if (op->opcode == opcode) { + return op; + } + } + } + + *model = NULL; + return NULL; +} + +static int get_opcode(struct net_buf_simple *buf, u32_t *opcode) +{ + switch (buf->data[0] >> 6) { + case 0x00: + case 0x01: + if (buf->data[0] == 0x7f) { + BT_ERR("%s, Ignoring RFU OpCode", __func__); + return -EINVAL; + } + + *opcode = net_buf_simple_pull_u8(buf); + return 0; + case 0x02: + if (buf->len < 2) { + BT_ERR("%s, Too short payload for 2-octet OpCode", __func__); + return -EINVAL; + } + + *opcode = net_buf_simple_pull_be16(buf); + return 0; + case 0x03: + if (buf->len < 3) { + BT_ERR("%s, Too short payload for 3-octet OpCode", __func__); + return -EINVAL; + } + + *opcode = net_buf_simple_pull_u8(buf) << 16; + /* Using LE for the CID since the model layer is defined as + * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3 + * will declare the opcode in this way. + */ + *opcode |= net_buf_simple_pull_le16(buf); + return 0; + } + + return -EINVAL; +} + +bool bt_mesh_fixed_group_match(u16_t addr) +{ + /* Check for fixed group addresses */ + switch (addr) { + case BLE_MESH_ADDR_ALL_NODES: + return true; + case BLE_MESH_ADDR_PROXIES: + return (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED); + case BLE_MESH_ADDR_FRIENDS: + return (bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED); + case BLE_MESH_ADDR_RELAYS: + return (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED); + default: + return false; + } +} + +void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) +{ + struct bt_mesh_model *models = NULL, *model = NULL; + const struct bt_mesh_model_op *op = NULL; + u32_t opcode = 0U; + u8_t count = 0U; + int i; + + BT_INFO("recv, app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx, + rx->ctx.addr, rx->ctx.recv_dst); + BT_INFO("recv, len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (get_opcode(buf, &opcode) < 0) { + BT_WARN("%s, Unable to decode OpCode", __func__); + return; + } + + BT_DBG("OpCode 0x%08x", opcode); + + for (i = 0; i < dev_comp->elem_count; i++) { + struct bt_mesh_elem *elem = &dev_comp->elem[i]; + struct net_buf_simple_state state = {0}; + + /* SIG models cannot contain 3-byte (vendor) OpCodes, and + * vendor models cannot contain SIG (1- or 2-byte) OpCodes, so + * we only need to do the lookup in one of the model lists. + */ + if (BLE_MESH_MODEL_OP_LEN(opcode) < 3) { + models = elem->models; + count = elem->model_count; + } else { + models = elem->vnd_models; + count = elem->vnd_model_count; + } + + op = find_op(models, count, opcode, &model); + if (!op) { + BT_DBG("No OpCode 0x%08x for elem %d", opcode, i); + continue; + } + + if (!model_has_key(model, rx->ctx.app_idx)) { + continue; + } + + if (!model_has_dst(model, rx->ctx.recv_dst)) { + continue; + } + + if (buf->len < op->min_len) { + BT_ERR("Too short message for OpCode 0x%08x", opcode); + continue; + } + + /* The following three operations are added by Espressif. + * 1. Update the "recv_op" with the opcode got from the buf; + * 2. Update the model pointer with the found model; + * 3. Update the "srv_send" to be true when received a message. + * This flag will be used when a server model sends a status + * message, and has no impact on the client messages. + * Most of these info will be used by the application layer. + */ + rx->ctx.recv_op = opcode; + rx->ctx.model = model; + rx->ctx.srv_send = true; + + /* The callback will likely parse the buffer, so store + * the parsing state in case multiple models receive + * the message. + */ + net_buf_simple_save(buf, &state); + op->func(model, &rx->ctx, buf); + net_buf_simple_restore(buf, &state); + } +} + +void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode) +{ + net_buf_simple_init(msg, 0); + + switch (BLE_MESH_MODEL_OP_LEN(opcode)) { + case 1: + net_buf_simple_add_u8(msg, opcode); + break; + case 2: + net_buf_simple_add_be16(msg, opcode); + break; + case 3: + net_buf_simple_add_u8(msg, ((opcode >> 16) & 0xff)); + /* Using LE for the CID since the model layer is defined as + * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3 + * will declare the opcode in this way. + */ + net_buf_simple_add_le16(msg, opcode & 0xffff); + break; + default: + BT_WARN("Unknown opcode format"); + break; + } +} + +static bool ready_to_send(u8_t role, u16_t dst) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + if (!bt_mesh_provisioner_check_msg_dst(dst)) { + BT_ERR("%s, Failed to find DST 0x%04x", __func__, dst); + return false; + } + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + return true; + } + + return false; +} + +static int model_send(struct bt_mesh_model *model, + struct bt_mesh_net_tx *tx, bool implicit_bind, + struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + u8_t role = 0U; + + role = bt_mesh_get_device_role(model, tx->ctx->srv_send); + if (role == ROLE_NVAL) { + BT_ERR("%s, Failed to get model role", __func__); + return -EINVAL; + } + + BT_INFO("send, app_idx 0x%04x src 0x%04x dst 0x%04x", + tx->ctx->app_idx, tx->src, tx->ctx->addr); + BT_INFO("send, len %u: %s", msg->len, bt_hex(msg->data, msg->len)); + + if (!ready_to_send(role, tx->ctx->addr)) { + BT_ERR("%s, Not ready", __func__); + return -EINVAL; + } + + if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { + BT_ERR("%s, Not enough tailroom for TransMIC", __func__); + return -EINVAL; + } + + if (msg->len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN) - BLE_MESH_MIC_SHORT) { + BT_ERR("%s, Too big message", __func__); + return -EMSGSIZE; + } + + if (!implicit_bind && !model_has_key(model, tx->ctx->app_idx)) { + BT_ERR("%s, Model not bound to AppKey 0x%04x", __func__, tx->ctx->app_idx); + return -EINVAL; + } + + return bt_mesh_trans_send(tx, msg, cb, cb_data); +} + +int bt_mesh_model_send(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + struct bt_mesh_subnet *sub = NULL; + u8_t role = 0U; + + role = bt_mesh_get_device_role(model, ctx->srv_send); + if (role == ROLE_NVAL) { + BT_ERR("%s, Failed to get model role", __func__); + return -EINVAL; + } + + sub = bt_mesh_tx_netkey_get(role, ctx->net_idx); + if (!sub) { + BT_ERR("%s, Failed to get subnet", __func__); + return -EINVAL; + } + + ctx->model = model; + + struct bt_mesh_net_tx tx = { + .sub = sub, + .ctx = ctx, + .src = bt_mesh_model_elem(model)->addr, + .xmit = bt_mesh_net_transmit_get(), + .friend_cred = 0, + }; + + return model_send(model, &tx, false, msg, cb, cb_data); +} + +int bt_mesh_model_publish(struct bt_mesh_model *model) +{ + struct bt_mesh_model_pub *pub = model->pub; + struct bt_mesh_app_key *key = NULL; + struct net_buf_simple *sdu = NULL; + struct bt_mesh_msg_ctx ctx = { + .model = model, + }; + struct bt_mesh_net_tx tx = { + .sub = NULL, + .ctx = &ctx, + .src = bt_mesh_model_elem(model)->addr, + .xmit = bt_mesh_net_transmit_get(), + }; + int err = 0; + + BT_DBG("%s", __func__); + + if (!pub || !pub->msg) { + BT_ERR("%s, Model has no publication support", __func__); + return -ENOTSUP; + } + + if (pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_WARN("%s, Unassigned model publish address", __func__); + return -EADDRNOTAVAIL; + } + + key = bt_mesh_tx_appkey_get(pub->dev_role, pub->key); + if (!key) { + BT_ERR("%s, AppKey 0x%03x not exists", __func__, pub->key); + return -EADDRNOTAVAIL; + } + + if (pub->msg->len + BLE_MESH_MIC_SHORT > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN)) { + BT_ERR("%s, Message does not fit maximum SDU size", __func__); + return -EMSGSIZE; + } + + if (pub->count) { + BT_WARN("%s, Clearing publish retransmit timer", __func__); + k_delayed_work_cancel(&pub->timer); + } + + ctx.addr = pub->addr; + ctx.send_rel = pub->send_rel; + ctx.send_ttl = pub->ttl; + ctx.net_idx = key->net_idx; + ctx.app_idx = key->app_idx; + ctx.srv_send = pub->dev_role == NODE ? true : false; + + tx.friend_cred = pub->cred; + + tx.sub = bt_mesh_tx_netkey_get(pub->dev_role, ctx.net_idx); + if (!tx.sub) { + BT_ERR("%s, Subnet 0x%04x not exists", __func__, ctx.net_idx); + return -EADDRNOTAVAIL; + } + + pub->count = BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit); + + BT_INFO("Publish Retransmit Count %u Interval %ums", pub->count, + BLE_MESH_PUB_TRANSMIT_INT(pub->retransmit)); + + sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_SHORT); + if (!sdu) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + net_buf_simple_add_mem(sdu, pub->msg->data, pub->msg->len); + + err = model_send(model, &tx, true, sdu, &pub_sent_cb, model); + if (err) { + publish_retransmit_end(err, pub); + } + + bt_mesh_free_buf(sdu); + return err; +} + +struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, + u16_t company, u16_t id) +{ + int i; + + for (i = 0; i < elem->vnd_model_count; i++) { + if (elem->vnd_models[i].vnd.company == company && + elem->vnd_models[i].vnd.id == id) { + return &elem->vnd_models[i]; + } + } + + return NULL; +} + +struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, + u16_t id) +{ + int i; + + for (i = 0; i < elem->model_count; i++) { + if (elem->models[i].id == id) { + return &elem->models[i]; + } + } + + return NULL; +} + +const struct bt_mesh_comp *bt_mesh_comp_get(void) +{ + return dev_comp; +} + +/* APIs used by messages encryption in upper transport layer & network layer */ +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + sub = bt_mesh_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + sub = bt_mesh_provisioner_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + sub = bt_mesh_fast_prov_subnet_get(net_idx); + } + + return sub; +} + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst) +{ + const u8_t *key = NULL; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + key = bt_mesh.dev_key; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + key = bt_mesh_provisioner_dev_key_get(dst); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + key = bt_mesh_fast_prov_dev_key_get(dst); + } + + return key; +} + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && role == NODE) { + key = bt_mesh_app_key_find(app_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en() && role == PROVISIONER) { + key = bt_mesh_provisioner_app_key_find(app_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && bt_mesh_is_provisioned() && role == FAST_PROV) { + key = bt_mesh_fast_prov_app_key_find(app_idx); + } + + return key; +} + +/* APIs used by messages decryption in network layer & upper transport layer */ +size_t bt_mesh_rx_netkey_size(void) +{ + size_t size = 0U; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.sub); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.sub); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_sub); + } +#endif + + return size; +} + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index) +{ + struct bt_mesh_subnet *sub = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + sub = &bt_mesh.sub[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + sub = bt_mesh.p_sub[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.sub)) { + sub = &bt_mesh.sub[index]; + } else { + sub = bt_mesh.p_sub[index - ARRAY_SIZE(bt_mesh.sub)]; + } +#endif + + return sub; +} + +size_t bt_mesh_rx_devkey_size(void) +{ + size_t size = 0U; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = 1; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = 1; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = 1; + if (bt_mesh_is_provisioner_en()) { + size += 1; + } +#endif + + return size; +} + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src) +{ + const u8_t *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = bt_mesh.dev_key; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = bt_mesh_provisioner_dev_key_get(src); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < 1) { + key = bt_mesh.dev_key; + } else { + key = bt_mesh_provisioner_dev_key_get(src); + } +#endif + + return key; +} + +size_t bt_mesh_rx_appkey_size(void) +{ + size_t size = 0U; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + size = ARRAY_SIZE(bt_mesh.app_keys); + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + size = ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + size = ARRAY_SIZE(bt_mesh.app_keys); + if (bt_mesh_is_provisioner_en()) { + size += ARRAY_SIZE(bt_mesh.p_app_keys); + } +#endif + + return size; +} + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index) +{ + struct bt_mesh_app_key *key = NULL; + +#if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioned()) { + key = &bt_mesh.app_keys[index]; + } +#endif + +#if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + key = bt_mesh.p_app_keys[index]; + } +#endif + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER + if (index < ARRAY_SIZE(bt_mesh.app_keys)) { + key = &bt_mesh.app_keys[index]; + } else { + key = bt_mesh.p_app_keys[index - ARRAY_SIZE(bt_mesh.app_keys)]; + } +#endif + + return key; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/access.h b/components/bt/esp_ble_mesh/mesh_core/access.h new file mode 100644 index 000000000..3e002686c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/access.h @@ -0,0 +1,85 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ACCESS_H_ +#define _ACCESS_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* bt_mesh_model.flags */ +enum { + BLE_MESH_MOD_BIND_PENDING = BIT(0), + BLE_MESH_MOD_SUB_PENDING = BIT(1), + BLE_MESH_MOD_PUB_PENDING = BIT(2), +}; + +void bt_mesh_elem_register(struct bt_mesh_elem *elem, u8_t count); + +u8_t bt_mesh_elem_count(void); + +/* Find local element based on unicast or group address */ +struct bt_mesh_elem *bt_mesh_elem_find(u16_t addr); + +struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem, + u16_t company, u16_t id); +struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, + u16_t id); + +u16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, u16_t addr); + +bool bt_mesh_fixed_group_match(u16_t addr); + +void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod, + struct bt_mesh_elem *elem, + bool vnd, bool primary, + void *user_data), + void *user_data); + +s32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod); + +void bt_mesh_comp_provision(u16_t addr); +void bt_mesh_comp_unprovision(void); + +u16_t bt_mesh_primary_addr(void); + +const struct bt_mesh_comp *bt_mesh_comp_get(void); + +struct bt_mesh_model *bt_mesh_model_get(bool vnd, u8_t elem_idx, u8_t mod_idx); + +void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); + +int bt_mesh_comp_register(const struct bt_mesh_comp *comp); +int bt_mesh_comp_deregister(void); + +struct bt_mesh_subnet *bt_mesh_tx_netkey_get(u8_t role, u16_t net_idx); + +const u8_t *bt_mesh_tx_devkey_get(u8_t role, u16_t dst); + +struct bt_mesh_app_key *bt_mesh_tx_appkey_get(u8_t role, u16_t app_idx); + +size_t bt_mesh_rx_netkey_size(void); + +struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index); + +size_t bt_mesh_rx_devkey_size(void); + +const u8_t *bt_mesh_rx_devkey_get(size_t index, u16_t src); + +size_t bt_mesh_rx_appkey_size(void); + +struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index); + +#ifdef __cplusplus +} +#endif + +#endif /* _ACCESS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.c b/components/bt/esp_ble_mesh/mesh_core/adv.c new file mode 100644 index 000000000..4e1d94849 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/adv.c @@ -0,0 +1,1223 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_ADV) + +#include "mesh_kernel.h" +#include "mesh.h" +#include "mesh_hci.h" +#include "mesh_common.h" +#include "adv.h" +#include "beacon.h" +#include "prov.h" +#include "foundation.h" +#include "proxy_server.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "mesh_bearer_adapt.h" + +/* Convert from ms to 0.625ms units */ +#define ADV_SCAN_UNIT(_ms) ((_ms) * 8 / 5) +/* Convert from 0.625ms units to interval(ms) */ +#define ADV_SCAN_INT(val) ((val) * 5 / 8) + +/* Window and Interval are equal for continuous scanning */ +#define MESH_SCAN_INTERVAL 0x20 +#define MESH_SCAN_WINDOW 0x20 + +/* Pre-5.0 controllers enforce a minimum interval of 100ms + * whereas 5.0+ controllers can go down to 20ms. + */ +#define ADV_INT_DEFAULT_MS 100 +#define ADV_INT_FAST_MS 20 + +#if defined(CONFIG_BT_HOST_CRYPTO) +#define ADV_STACK_SIZE 1024 +#else +#define ADV_STACK_SIZE 768 +#endif + +static const bt_mesh_addr_t *dev_addr; + +static const u8_t adv_type[] = { + [BLE_MESH_ADV_PROV] = BLE_MESH_DATA_MESH_PROV, + [BLE_MESH_ADV_DATA] = BLE_MESH_DATA_MESH_MESSAGE, + [BLE_MESH_ADV_BEACON] = BLE_MESH_DATA_MESH_BEACON, + [BLE_MESH_ADV_URI] = BLE_MESH_DATA_URI, +}; + +NET_BUF_POOL_DEFINE(adv_buf_pool, CONFIG_BLE_MESH_ADV_BUF_COUNT, + BLE_MESH_ADV_DATA_SIZE, BLE_MESH_ADV_USER_DATA_SIZE, NULL); + +static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT]; + +struct bt_mesh_queue { + QueueHandle_t queue; +#if CONFIG_SPIRAM_USE_MALLOC + StaticQueue_t *buffer; + u8_t *storage; +#endif +}; + +static struct bt_mesh_queue xBleMeshQueue; +/* We reserve one queue for bt_mesh_adv_update() */ +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +#define BLE_MESH_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1) +#else +#define BLE_MESH_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + 1) +#endif + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +NET_BUF_POOL_DEFINE(relay_adv_buf_pool, CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT, + BLE_MESH_ADV_DATA_SIZE, BLE_MESH_ADV_USER_DATA_SIZE, NULL); + +static struct bt_mesh_adv relay_adv_pool[CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT]; + +static struct bt_mesh_queue xBleMeshRelayQueue; +#define BLE_MESH_RELAY_QUEUE_SIZE CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT + +static QueueSetHandle_t xBleMeshQueueSet; +#define BLE_MESH_QUEUE_SET_SIZE (BLE_MESH_QUEUE_SIZE + BLE_MESH_RELAY_QUEUE_SIZE) + +#define BLE_MESH_RELAY_TIME_INTERVAL K_SECONDS(6) +#define BLE_MESH_MAX_TIME_INTERVAL 0xFFFFFFFF + +static bool ignore_relay_packet(u32_t timestamp); +#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +/* length + advertising data + length + scan response data */ +NET_BUF_POOL_DEFINE(ble_adv_buf_pool, CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT, + ((BLE_MESH_ADV_DATA_SIZE + 3) << 1), BLE_MESH_ADV_USER_DATA_SIZE, NULL); + +static struct bt_mesh_adv ble_adv_pool[CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT]; + +enum { + TIMER_INIT, /* Resend timer is initialized */ + NUM_FLAGS, +}; + +static struct ble_adv_tx { + struct bt_mesh_ble_adv_param param; + struct net_buf *buf; + struct k_delayed_work resend; + BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); +} ble_adv_tx[CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT]; + +#define SEND_BLE_ADV_INFINITE 0xFFFF + +static void bt_mesh_ble_adv_deinit(void); +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +struct bt_mesh_adv_task { + TaskHandle_t handle; +#if CONFIG_SPIRAM_USE_MALLOC + StaticTask_t *task; + StackType_t *stack; +#endif +}; + +static struct bt_mesh_adv_task adv_task; + +static struct bt_mesh_adv *adv_alloc(int id) +{ + return &adv_pool[id]; +} + +static inline void adv_send_start(u16_t duration, int err, + const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + if (cb && cb->start) { + cb->start(duration, err, cb_data); + } +} + +static inline void adv_send_end(int err, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + if (cb && cb->end) { + cb->end(err, cb_data); + } +} + +static inline int adv_send(struct net_buf *buf) +{ + const s32_t adv_int_min = ((bt_mesh_dev.hci_version >= BLE_MESH_HCI_VERSION_5_0) ? + ADV_INT_FAST_MS : ADV_INT_DEFAULT_MS); + const struct bt_mesh_send_cb *cb = BLE_MESH_ADV(buf)->cb; + void *cb_data = BLE_MESH_ADV(buf)->cb_data; + struct bt_mesh_adv_param param = {0}; + u16_t duration = 0U, adv_int = 0U; + struct bt_mesh_adv_data ad = {0}; + int err = 0; + + BT_DBG("type %u len %u: %s", BLE_MESH_ADV(buf)->type, + buf->len, bt_hex(buf->data, buf->len)); + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + if (BLE_MESH_ADV(buf)->type != BLE_MESH_ADV_BLE) { +#endif + adv_int = MAX(adv_int_min, + BLE_MESH_TRANSMIT_INT(BLE_MESH_ADV(buf)->xmit)); + duration = (BLE_MESH_TRANSMIT_COUNT(BLE_MESH_ADV(buf)->xmit) + 1) * + (adv_int + 10); + + BT_DBG("count %u interval %ums duration %ums", + BLE_MESH_TRANSMIT_COUNT(BLE_MESH_ADV(buf)->xmit) + 1, adv_int, + duration); + + ad.type = adv_type[BLE_MESH_ADV(buf)->type]; + ad.data_len = buf->len; + ad.data = buf->data; + + param.options = 0U; + param.interval_min = ADV_SCAN_UNIT(adv_int); + param.interval_max = param.interval_min; + + bt_mesh_adv_buf_ref_debug(__func__, buf, 4U, BLE_MESH_BUF_REF_SMALL); + + err = bt_le_adv_start(¶m, &ad, 1, NULL, 0); +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + } else { + struct bt_mesh_ble_adv_data data = {0}; + struct ble_adv_tx *tx = cb_data; + + if (tx == NULL) { + BT_ERR("%s, Invalid adv user data", __func__); + net_buf_unref(buf); + return -EINVAL; + } + + BT_DBG("interval %dms, duration %dms, period %dms, count %d", + ADV_SCAN_INT(tx->param.interval), tx->param.duration, + tx->param.period, tx->param.count); + + data.adv_data_len = tx->buf->data[0]; + if (data.adv_data_len) { + memcpy(data.adv_data, tx->buf->data + 1, data.adv_data_len); + } + data.scan_rsp_data_len = tx->buf->data[data.adv_data_len + 1]; + if (data.scan_rsp_data_len) { + memcpy(data.scan_rsp_data, tx->buf->data + data.adv_data_len + 2, data.scan_rsp_data_len); + } + duration = tx->param.duration; + + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + + err = bt_mesh_ble_adv_start(&tx->param, &data); + } +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + + net_buf_unref(buf); + adv_send_start(duration, err, cb, cb_data); + if (err) { + BT_ERR("%s, Advertising failed: err %d", __func__, err); + return err; + } + + BT_DBG("Advertising started. Sleeping %u ms", duration); + + k_sleep(K_MSEC(duration)); + + err = bt_le_adv_stop(); + adv_send_end(err, cb, cb_data); + if (err) { + BT_ERR("%s, Stop advertising failed: err %d", __func__, err); + return 0; + } + + BT_DBG("Advertising stopped"); + return 0; +} + +static void adv_thread(void *p) +{ +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + QueueSetMemberHandle_t handle = NULL; +#endif + bt_mesh_msg_t msg = {0}; + struct net_buf **buf = NULL; + + buf = (struct net_buf **)(&msg.arg); + + BT_DBG("%s, starts", __func__); + + while (1) { + *buf = NULL; +#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + while (!(*buf)) { + s32_t timeout; + BT_DBG("Mesh Proxy Advertising start"); + timeout = bt_mesh_proxy_adv_start(); + BT_DBG("Mesh Proxy Advertising up to %d ms", timeout); + xQueueReceive(xBleMeshQueue.queue, &msg, timeout); + BT_DBG("Mesh Proxy Advertising stop"); + bt_mesh_proxy_adv_stop(); + } +#else + xQueueReceive(xBleMeshQueue.queue, &msg, portMAX_DELAY); +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +#else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); + if (handle) { + if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { + xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + } + } else { + while (!(*buf)) { + s32_t timeout = 0; + BT_DBG("Mesh Proxy Advertising start"); + timeout = bt_mesh_proxy_adv_start(); + BT_DBG("Mesh Proxy Advertising up to %d ms", timeout); + handle = xQueueSelectFromSet(xBleMeshQueueSet, timeout); + BT_DBG("Mesh Proxy Advertising stop"); + bt_mesh_proxy_adv_stop(); + if (handle) { + if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { + xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + } + } + } + } +#else + handle = xQueueSelectFromSet(xBleMeshQueueSet, portMAX_DELAY); + if (handle) { + if (uxQueueMessagesWaiting(xBleMeshQueue.queue)) { + xQueueReceive(xBleMeshQueue.queue, &msg, K_NO_WAIT); + } else if (uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + xQueueReceive(xBleMeshRelayQueue.queue, &msg, K_NO_WAIT); + } + } +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ +#endif /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + + if (*buf == NULL) { + continue; + } + + /* busy == 0 means this was canceled */ + if (BLE_MESH_ADV(*buf)->busy) { + BLE_MESH_ADV(*buf)->busy = 0U; +#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + if (adv_send(*buf)) { + BT_WARN("%s, Failed to send adv packet", __func__); + } +#else /* !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + if (msg.relay && ignore_relay_packet(msg.timestamp)) { + /* If the interval between "current time - msg.timestamp" is bigger than + * BLE_MESH_RELAY_TIME_INTERVAL, this relay packet will not be sent. + */ + BT_INFO("%s, Ignore relay packet", __func__); + net_buf_unref(*buf); + } else { + if (adv_send(*buf)) { + BT_WARN("%s, Failed to send adv packet", __func__); + } + } +#endif + } else { + bt_mesh_adv_buf_ref_debug(__func__, *buf, 1U, BLE_MESH_BUF_REF_EQUAL); + net_buf_unref(*buf); + } + + /* Give other threads a chance to run */ + taskYIELD(); + } +} + +struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, + bt_mesh_adv_alloc_t get_id, + enum bt_mesh_adv_type type, + u8_t xmit, s32_t timeout) +{ + struct bt_mesh_adv *adv = NULL; + struct net_buf *buf = NULL; + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) { + BT_WARN("Refusing to allocate buffer while suspended"); + return NULL; + } + + buf = net_buf_alloc(pool, timeout); + if (!buf) { + return NULL; + } + + BT_DBG("%s, pool = %p, buf_count = %d, uinit_count = %d", __func__, + buf->pool, pool->buf_count, pool->uninit_count); + + adv = get_id(net_buf_id(buf)); + BLE_MESH_ADV(buf) = adv; + + (void)memset(adv, 0, sizeof(*adv)); + + adv->type = type; + adv->xmit = xmit; + + return buf; +} + +void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool) +{ + int i; + + if (pool == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + for (i = 0; i < pool->buf_count; i++) { + struct net_buf *buf = &pool->__bufs[i]; + if (buf->ref > 1U) { + buf->ref = 1U; + } + net_buf_unref(buf); + } +} + +struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, u8_t xmit, + s32_t timeout) +{ + return bt_mesh_adv_create_from_pool(&adv_buf_pool, adv_alloc, type, + xmit, timeout); +} + +void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf, + u8_t ref_cmp, bt_mesh_buf_ref_flag_t flag) +{ + if (buf == NULL || func == NULL || flag >= BLE_MESH_BUF_REF_MAX) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + switch (flag) { + case BLE_MESH_BUF_REF_EQUAL: + if (buf->ref != ref_cmp) { + BT_ERR("Unexpected ref %d in %s, expect to equal to %d", buf->ref, func, ref_cmp); + } + break; + case BLE_MESH_BUF_REF_SMALL: + if (buf->ref >= ref_cmp) { + BT_ERR("Unexpected ref %d in %s, expect to smaller than %d", buf->ref, func, ref_cmp); + } + break; + default: + break; + } +} + +static void bt_mesh_unref_buf(bt_mesh_msg_t *msg) +{ + struct net_buf *buf = NULL; + + if (msg->arg) { + buf = (struct net_buf *)msg->arg; + BLE_MESH_ADV(buf)->busy = 0U; + if (buf->ref > 1U) { + buf->ref = 1U; + } + net_buf_unref(buf); + } + + return; +} + +static void bt_mesh_task_post(bt_mesh_msg_t *msg, uint32_t timeout, bool front) +{ + BT_DBG("%s", __func__); + + if (xBleMeshQueue.queue == NULL) { + BT_ERR("%s, Invalid queue", __func__); + return; + } + + if (front) { + if (xQueueSendToFront(xBleMeshQueue.queue, msg, timeout) != pdTRUE) { + BT_ERR("%s, Failed to send item to queue front", __func__); + bt_mesh_unref_buf(msg); + } + } else { + if (xQueueSend(xBleMeshQueue.queue, msg, timeout) != pdTRUE) { + BT_ERR("%s, Failed to send item to queue back", __func__); + bt_mesh_unref_buf(msg); + } + } +} + +void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + bt_mesh_msg_t msg = { + .relay = false, + }; + + BT_DBG("type 0x%02x len %u: %s", BLE_MESH_ADV(buf)->type, buf->len, + bt_hex(buf->data, buf->len)); + + BLE_MESH_ADV(buf)->cb = cb; + BLE_MESH_ADV(buf)->cb_data = cb_data; + BLE_MESH_ADV(buf)->busy = 1U; + + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + + msg.arg = (void *)net_buf_ref(buf); + bt_mesh_task_post(&msg, portMAX_DELAY, false); +} + +void bt_mesh_adv_update(void) +{ + bt_mesh_msg_t msg = { + .relay = false, + .arg = NULL, + }; + + BT_DBG("%s", __func__); + + bt_mesh_task_post(&msg, K_NO_WAIT, false); +} + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +static bool ignore_relay_packet(u32_t timestamp) +{ + u32_t now = k_uptime_get_32(); + u32_t interval = 0U; + + if (now >= timestamp) { + interval = now - timestamp; + } else { + interval = BLE_MESH_MAX_TIME_INTERVAL - (timestamp - now) + 1; + } + + return (interval >= BLE_MESH_RELAY_TIME_INTERVAL) ? true : false; +} + +static struct bt_mesh_adv *relay_adv_alloc(int id) +{ + return &relay_adv_pool[id]; +} + +struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, u8_t xmit, + s32_t timeout) +{ + return bt_mesh_adv_create_from_pool(&relay_adv_buf_pool, relay_adv_alloc, type, + xmit, timeout); +} + +static void ble_mesh_relay_task_post(bt_mesh_msg_t *msg, uint32_t timeout) +{ + QueueSetMemberHandle_t handle = NULL; + bt_mesh_msg_t old_msg = {0}; + + BT_DBG("%s", __func__); + + if (xBleMeshRelayQueue.queue == NULL) { + BT_ERR("%s, Invalid relay queue", __func__); + return; + } + + if (xQueueSend(xBleMeshRelayQueue.queue, msg, timeout) == pdTRUE) { + return; + } + + /** + * If failed to send packet to the relay queue(queue is full), we will + * remove the oldest packet in the queue and put the new one into it. + */ + handle = xQueueSelectFromSet(xBleMeshQueueSet, K_NO_WAIT); + if (handle && uxQueueMessagesWaiting(xBleMeshRelayQueue.queue)) { + BT_INFO("%s, Full queue, remove the oldest relay packet", __func__); + /* Remove the oldest relay packet from queue */ + if (xQueueReceive(xBleMeshRelayQueue.queue, &old_msg, K_NO_WAIT) != pdTRUE) { + BT_ERR("%s, Failed to remove item from queue", __func__); + bt_mesh_unref_buf(msg); + return; + } + /* Unref buf used for the oldest relay packet */ + bt_mesh_unref_buf(&old_msg); + /* Send the latest relay packet to queue */ + if (xQueueSend(xBleMeshRelayQueue.queue, msg, K_NO_WAIT) != pdTRUE) { + BT_ERR("%s, Failed to send item to relay queue", __func__); + bt_mesh_unref_buf(msg); + return; + } + } else { + BT_WARN("%s, Empty queue, but failed to send the relay packet", __func__); + bt_mesh_unref_buf(msg); + } +} + +void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, + void *cb_data, u16_t src, u16_t dst) +{ + bt_mesh_msg_t msg = { + .relay = true, + }; + + BT_DBG("type 0x%02x len %u: %s", BLE_MESH_ADV(buf)->type, buf->len, + bt_hex(buf->data, buf->len)); + + BLE_MESH_ADV(buf)->cb = cb; + BLE_MESH_ADV(buf)->cb_data = cb_data; + BLE_MESH_ADV(buf)->busy = 1U; + + msg.arg = (void *)net_buf_ref(buf); + msg.src = src; + msg.dst = dst; + msg.timestamp = k_uptime_get_32(); + /* Use K_NO_WAIT here, if xBleMeshRelayQueue is full return immediately */ + ble_mesh_relay_task_post(&msg, K_NO_WAIT); +} + +u16_t bt_mesh_get_stored_relay_count(void) +{ + return (u16_t)uxQueueMessagesWaiting(xBleMeshRelayQueue.queue); +} +#endif /* #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + +const bt_mesh_addr_t *bt_mesh_pba_get_addr(void) +{ + return dev_addr; +} + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +static bool bt_mesh_is_adv_flags_valid(struct net_buf_simple *buf) +{ + u8_t flags = 0U; + + if (buf->len != 1U) { + BT_DBG("%s, Unexpected flags length", __func__); + return false; + } + + flags = net_buf_simple_pull_u8(buf); + + BT_DBG("Received adv pkt with flags: 0x%02x", flags); + + /* Flags context will not be checked currently */ + ((void) flags); + + return true; +} + +static bool bt_mesh_is_adv_srv_uuid_valid(struct net_buf_simple *buf, u16_t *uuid) +{ + if (buf->len != 2U) { + BT_DBG("Length not match mesh service uuid"); + return false; + } + + *uuid = net_buf_simple_pull_le16(buf); + + BT_DBG("Received adv pkt with service UUID: %d", *uuid); + + if (*uuid != BLE_MESH_UUID_MESH_PROV_VAL && + *uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + return false; + } + + if (*uuid == BLE_MESH_UUID_MESH_PROV_VAL && + bt_mesh_is_provisioner_en() == false) { + return false; + } + + if (*uuid == BLE_MESH_UUID_MESH_PROXY_VAL && + !IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + return false; + } + + return true; +} + +#define BLE_MESH_PROV_SRV_DATA_LEN 0x12 +#define BLE_MESH_PROXY_SRV_DATA_LEN1 0x09 +#define BLE_MESH_PROXY_SRV_DATA_LEN2 0x11 + +static void bt_mesh_adv_srv_data_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, u16_t uuid, s8_t rssi) +{ + u16_t type = 0U; + + if (!buf || !addr) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + type = net_buf_simple_pull_le16(buf); + if (type != uuid) { + BT_DBG("%s, Invalid Mesh Service Data UUID 0x%04x", __func__, type); + return; + } + + switch (type) { +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + case BLE_MESH_UUID_MESH_PROV_VAL: + if (bt_mesh_is_provisioner_en()) { + if (buf->len != BLE_MESH_PROV_SRV_DATA_LEN) { + BT_WARN("%s, Invalid Mesh Prov Service Data length %d", __func__, buf->len); + return; + } + + BT_DBG("Start to handle Mesh Prov Service Data"); + bt_mesh_provisioner_prov_adv_ind_recv(buf, addr, rssi); + } + break; +#endif +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_MESH_UUID_MESH_PROXY_VAL: + if (buf->len != BLE_MESH_PROXY_SRV_DATA_LEN1 && + buf->len != BLE_MESH_PROXY_SRV_DATA_LEN2) { + BT_WARN("%s, Invalid Mesh Proxy Service Data length %d", __func__, buf->len); + return; + } + + BT_DBG("Start to handle Mesh Proxy Service Data"); + bt_mesh_proxy_client_adv_ind_recv(buf, addr, rssi); + break; +#endif + default: + break; + } +} +#endif + +static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi, + u8_t adv_type, struct net_buf_simple *buf) +{ +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + u16_t uuid = 0U; +#endif + + if (adv_type != BLE_MESH_ADV_NONCONN_IND && adv_type != BLE_MESH_ADV_IND) { + return; + } + + BT_DBG("%s, len %u: %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + dev_addr = addr; + + while (buf->len > 1) { + struct net_buf_simple_state state; + u8_t len, type; + + len = net_buf_simple_pull_u8(buf); + /* Check for early termination */ + if (len == 0U) { + return; + } + + if (len > buf->len) { + BT_WARN("AD malformed"); + return; + } + + net_buf_simple_save(buf, &state); + + type = net_buf_simple_pull_u8(buf); + + buf->len = len - 1; + +#if 0 + /* TODO: Check with BLE Mesh BQB test cases */ + if ((type == BLE_MESH_DATA_MESH_PROV || type == BLE_MESH_DATA_MESH_MESSAGE || + type == BLE_MESH_DATA_MESH_BEACON) && (adv_type != BLE_MESH_ADV_NONCONN_IND)) { + BT_DBG("%s, ignore BLE Mesh packet (type 0x%02x) with adv_type 0x%02x", + __func__, type, adv_type); + return; + } +#endif + + switch (type) { + case BLE_MESH_DATA_MESH_MESSAGE: + bt_mesh_net_recv(buf, rssi, BLE_MESH_NET_IF_ADV); + break; +#if CONFIG_BLE_MESH_PB_ADV + case BLE_MESH_DATA_MESH_PROV: + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { + bt_mesh_pb_adv_recv(buf); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + bt_mesh_provisioner_pb_adv_recv(buf); + } + break; +#endif /* CONFIG_BLE_MESH_PB_ADV */ + case BLE_MESH_DATA_MESH_BEACON: + bt_mesh_beacon_recv(buf, rssi); + break; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_MESH_DATA_FLAGS: + if (!bt_mesh_is_adv_flags_valid(buf)) { + BT_DBG("Adv Flags mismatch, ignore this adv pkt"); + return; + } + break; + case BLE_MESH_DATA_UUID16_ALL: + if (!bt_mesh_is_adv_srv_uuid_valid(buf, &uuid)) { + BT_DBG("Adv Service UUID mismatch, ignore this adv pkt"); + return; + } + break; + case BLE_MESH_DATA_SVC_DATA16: + bt_mesh_adv_srv_data_recv(buf, addr, uuid, rssi); + break; +#endif + default: + break; + } + + net_buf_simple_restore(buf, &state); + net_buf_simple_pull(buf, len); + } + + return; +} + +void bt_mesh_adv_init(void) +{ +#if !CONFIG_SPIRAM_USE_MALLOC + xBleMeshQueue.queue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); + __ASSERT(xBleMeshQueue.queue, "%s, Failed to create queue", __func__); +#else + xBleMeshQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(xBleMeshQueue.buffer, "%s, Failed to create queue buffer", __func__); + xBleMeshQueue.storage = heap_caps_calloc(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(xBleMeshQueue.storage, "%s, Failed to create queue storage", __func__); + xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer); + __ASSERT(xBleMeshQueue.queue, "%s, Failed to create static queue", __func__); +#endif + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#if !CONFIG_SPIRAM_USE_MALLOC + xBleMeshRelayQueue.queue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); + __ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create relay queue", __func__); +#else + xBleMeshRelayQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(xBleMeshRelayQueue.buffer, "%s, Failed to create relay queue buffer", __func__); + xBleMeshRelayQueue.storage = heap_caps_calloc(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); + __ASSERT(xBleMeshRelayQueue.storage, "%s, Failed to create relay queue storage", __func__); + xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer); + __ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create static relay queue", __func__); +#endif + + xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); + __ASSERT(xBleMeshQueueSet, "%s, Failed to create queue set", __func__); + xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet); + xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); +#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + +#if !CONFIG_SPIRAM_USE_MALLOC + int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, + configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); + __ASSERT(ret == pdTRUE, "%s, Failed to create adv thread", __func__); +#else + adv_task.task = heap_caps_calloc(1, sizeof(StaticTask_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); + __ASSERT(adv_task.task, "%s, Failed to create adv thread task", __func__); +#if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY + adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); +#else + adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#endif + __ASSERT(adv_task.stack, "%s, Failed to create adv thread stack", __func__); + adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, + configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE); + __ASSERT(adv_task.handle, "%s, Failed to create static adv thread", __func__); +#endif +} + +void bt_mesh_adv_deinit(void) +{ + if (xBleMeshQueue.queue == NULL) { + return; + } + + vTaskDelete(adv_task.handle); + adv_task.handle = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(adv_task.stack); + adv_task.stack = NULL; + heap_caps_free(adv_task.task); + adv_task.task = NULL; +#endif + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + xQueueRemoveFromSet(xBleMeshQueue.queue, xBleMeshQueueSet); + xQueueRemoveFromSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); + + vQueueDelete(xBleMeshRelayQueue.queue); + xBleMeshRelayQueue.queue = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(xBleMeshRelayQueue.buffer); + xBleMeshRelayQueue.buffer = NULL; + heap_caps_free(xBleMeshRelayQueue.storage); + xBleMeshRelayQueue.storage = NULL; +#endif + + bt_mesh_unref_buf_from_pool(&relay_adv_buf_pool); + memset(relay_adv_pool, 0, sizeof(relay_adv_pool)); + + vQueueDelete(xBleMeshQueueSet); + xBleMeshQueueSet = NULL; +#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ + + vQueueDelete(xBleMeshQueue.queue); + xBleMeshQueue.queue = NULL; +#if CONFIG_SPIRAM_USE_MALLOC + heap_caps_free(xBleMeshQueue.buffer); + xBleMeshQueue.buffer = NULL; + heap_caps_free(xBleMeshQueue.storage); + xBleMeshQueue.storage = NULL; +#endif + + bt_mesh_unref_buf_from_pool(&adv_buf_pool); + memset(adv_pool, 0, sizeof(adv_pool)); + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV + bt_mesh_ble_adv_deinit(); +#endif +} + +int bt_mesh_scan_enable(void) +{ + int err = 0; + + struct bt_mesh_scan_param scan_param = { + .type = BLE_MESH_SCAN_PASSIVE, +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, +#else + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, +#endif + .interval = MESH_SCAN_INTERVAL, + .window = MESH_SCAN_WINDOW, + .scan_fil_policy = BLE_MESH_SP_ADV_ALL, + }; + + BT_DBG("%s", __func__); + + err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + if (err && err != -EALREADY) { + BT_ERR("starting scan failed (err %d)", err); + return err; + } + + return 0; +} + +int bt_mesh_scan_disable(void) +{ + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_le_scan_stop(); + if (err && err != -EALREADY) { + BT_ERR("stopping scan failed (err %d)", err); + return err; + } + + return 0; +} + +#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST +int bt_mesh_scan_with_wl_enable(void) +{ + int err = 0; + + struct bt_mesh_scan_param scan_param = { + .type = BLE_MESH_SCAN_PASSIVE, +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_ENABLE, +#else + .filter_dup = BLE_MESH_SCAN_FILTER_DUP_DISABLE, +#endif + .interval = MESH_SCAN_INTERVAL, + .window = MESH_SCAN_WINDOW, + .scan_fil_policy = BLE_MESH_SP_ADV_WL, + }; + + BT_DBG("%s", __func__); + + err = bt_le_scan_start(&scan_param, bt_mesh_scan_cb); + if (err) { + BT_ERR("starting scan failed (err %d)", err); + return err; + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */ + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +static struct bt_mesh_adv *ble_adv_alloc(int id) +{ + return &ble_adv_pool[id]; +} + +static struct net_buf *bt_mesh_ble_adv_create(enum bt_mesh_adv_type type, u8_t xmit, s32_t timeout) +{ + return bt_mesh_adv_create_from_pool(&ble_adv_buf_pool, ble_adv_alloc, type, + xmit, timeout); +} + +static void bt_mesh_ble_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, + void *cb_data, bool front) +{ + bt_mesh_msg_t msg = { + .relay = false, + }; + + BT_DBG("type 0x%02x len %u: %s", BLE_MESH_ADV(buf)->type, buf->len, + bt_hex(buf->data, buf->len)); + + BLE_MESH_ADV(buf)->cb = cb; + BLE_MESH_ADV(buf)->cb_data = cb_data; + BLE_MESH_ADV(buf)->busy = 1U; + + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + + msg.arg = (void *)net_buf_ref(buf); + bt_mesh_task_post(&msg, portMAX_DELAY, front); +} + +static void ble_adv_tx_reset(struct ble_adv_tx *tx, bool unref) +{ + if (tx->buf == NULL) { + return; + } + + if (bt_mesh_atomic_test_bit(tx->flags, TIMER_INIT)) { + k_delayed_work_free(&tx->resend); + } + bt_mesh_atomic_set(tx->flags, 0); + memset(&tx->param, 0, sizeof(tx->param)); + BLE_MESH_ADV(tx->buf)->busy = 0U; + if (unref) { + net_buf_unref(tx->buf); + } + tx->buf = NULL; +} + +static void ble_adv_send_start(u16_t duration, int err, void *cb_data) +{ + struct ble_adv_tx *tx = cb_data; + + BT_DBG("%s, duration %d, err %d", __func__, duration, err); + + /* If failed to send BLE adv packet, and param->count is not 0 + * which means the timer has been initialized, here we need to + * free the timer. + */ + if (err) { + ble_adv_tx_reset(tx, true); + } +} + +static void ble_adv_send_end(int err, void *cb_data) +{ + struct ble_adv_tx *tx = cb_data; + + BT_DBG("%s, err %d", __func__, err); + + if (err) { + ble_adv_tx_reset(tx, true); + return; + } + + if (tx->param.count) { + if (tx->param.period) { + k_delayed_work_submit(&tx->resend, tx->param.period); + } else { + k_work_submit(&tx->resend.work); + } + } else { + ble_adv_tx_reset(tx, true); + } +} + +static struct bt_mesh_send_cb ble_adv_send_cb = { + .start = ble_adv_send_start, + .end = ble_adv_send_end, +}; + +static void ble_adv_resend(struct k_work *work) +{ + struct ble_adv_tx *tx = CONTAINER_OF(work, struct ble_adv_tx, resend.work); + bool front = false; + + if (tx->buf == NULL) { + /* The advertising has been cancelled */ + return; + } + + front = (tx->param.priority == BLE_MESH_BLE_ADV_PRIO_HIGH) ? true : false; + bt_mesh_ble_adv_send(tx->buf, &ble_adv_send_cb, tx, front); + + if (tx->param.count == SEND_BLE_ADV_INFINITE) { + /* Send the BLE advertising packet infinitely */ + return; + } + + if (tx->param.count > 0U) { + tx->param.count--; + } +} + +int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, + const struct bt_mesh_ble_adv_data *data, u8_t *index) +{ + struct ble_adv_tx *tx = NULL; + struct net_buf *buf = NULL; + bool front = false; + + if (param == NULL || index == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (param->adv_type != BLE_MESH_ADV_DIRECT_IND && + (param->interval < 0x20 || param->interval > 0x4000)) { + BT_ERR("%s, Invalid adv interval 0x%04x", __func__, param->interval); + return -EINVAL; + } + + if (param->adv_type > BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) { + BT_ERR("%s, Invalid adv type 0x%02x", __func__, param->adv_type); + return -EINVAL; + } + + if (param->own_addr_type > BLE_MESH_ADDR_RANDOM_ID) { + BT_ERR("%s, Invalid own addr type 0x%02x", __func__, param->own_addr_type); + return -EINVAL; + } + + if ((param->own_addr_type == BLE_MESH_ADDR_PUBLIC_ID || + param->own_addr_type == BLE_MESH_ADDR_RANDOM_ID || + param->adv_type == BLE_MESH_ADV_DIRECT_IND || + param->adv_type == BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) && + param->peer_addr_type > BLE_MESH_ADDR_RANDOM) { + BT_ERR("%s, Invalid peer addr type 0x%02x", __func__, param->peer_addr_type); + return -EINVAL; + } + + if (data && (data->adv_data_len > 31 || data->scan_rsp_data_len > 31)) { + BT_ERR("%s, Invalid adv data length, %d %d", __func__, + data->adv_data_len, data->scan_rsp_data_len); + return -EINVAL; + } + + if (param->priority > BLE_MESH_BLE_ADV_PRIO_HIGH) { + BT_ERR("%s, Invalid adv priority %d", __func__, param->priority); + return -EINVAL; + } + + if (param->duration < ADV_SCAN_INT(param->interval)) { + BT_ERR("%s, Too small duration %dms", __func__, param->duration); + return -EINVAL; + } + + buf = bt_mesh_ble_adv_create(BLE_MESH_ADV_BLE, 0U, K_NO_WAIT); + if (!buf) { + BT_ERR("%s, Unable to allocate buffer", __func__); + return -ENOBUFS; + } + + /* Set advertising data and scan response data */ + memset(buf->data, 0, buf->size); + if (data) { + net_buf_add_u8(buf, data->adv_data_len); + if (data->adv_data_len) { + net_buf_add_mem(buf, data->adv_data, data->adv_data_len); + } + net_buf_add_u8(buf, data->scan_rsp_data_len); + if (data->scan_rsp_data_len) { + net_buf_add_mem(buf, data->scan_rsp_data, data->scan_rsp_data_len); + } + } + + *index = net_buf_id(buf); + tx = &ble_adv_tx[*index]; + tx->buf = buf; + memcpy(&tx->param, param, sizeof(tx->param)); + + front = (tx->param.priority == BLE_MESH_BLE_ADV_PRIO_HIGH) ? true : false; + bt_mesh_ble_adv_send(buf, &ble_adv_send_cb, tx, front); + if (param->count) { + if (k_delayed_work_init(&tx->resend, ble_adv_resend)) { + /* If failed to create a timer, the BLE adv packet will be + * sent only once. Just give a warning here, and since the + * BLE adv packet can be sent, return 0 here. + */ + BT_WARN("Send BLE adv packet only once"); + tx->param.count = 0; + net_buf_unref(buf); + return 0; + } + bt_mesh_atomic_set_bit(tx->flags, TIMER_INIT); + } else { + /* Send the BLE advertising packet only once */ + net_buf_unref(buf); + } + + return 0; +} + +int bt_mesh_stop_ble_advertising(u8_t index) +{ + struct ble_adv_tx *tx = NULL; + bool unref = true; + + if (index >= ARRAY_SIZE(ble_adv_tx)) { + BT_ERR("%s, Invalid index %d", __func__, index); + return -EINVAL; + } + + tx = &ble_adv_tx[index]; + + if (tx->buf == NULL) { + BT_WARN("%s, Already stopped, index %d", __func__, index); + return 0; + } + + /* busy 1, ref 1; busy 1, ref 2; + * busy 0, ref 0; busy 0, ref 1; + */ + if (BLE_MESH_ADV(tx->buf)->busy == 1U && + tx->buf->ref == 1U) { + unref = false; + } + ble_adv_tx_reset(tx, unref); + + return 0; +} + +static void bt_mesh_ble_adv_deinit(void) +{ + for (int i = 0; i < ARRAY_SIZE(ble_adv_tx); i++) { + struct ble_adv_tx *tx = &ble_adv_tx[i]; + ble_adv_tx_reset(tx, false); + } + bt_mesh_unref_buf_from_pool(&ble_adv_buf_pool); + memset(ble_adv_pool, 0, sizeof(ble_adv_pool)); +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ diff --git a/components/bt/esp_ble_mesh/mesh_core/adv.h b/components/bt/esp_ble_mesh/mesh_core/adv.h new file mode 100644 index 000000000..e0ade0da3 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/adv.h @@ -0,0 +1,113 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _ADV_H_ +#define _ADV_H_ + +#include "mesh_access.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Maximum advertising data payload for a single data type */ +#define BLE_MESH_ADV_DATA_SIZE 29 + +/* The user data is a pointer (4 bytes) to struct bt_mesh_adv */ +#define BLE_MESH_ADV_USER_DATA_SIZE 4 + +#define BLE_MESH_ADV(buf) (*(struct bt_mesh_adv **)net_buf_user_data(buf)) + +typedef struct bt_mesh_msg { + bool relay; /* Flag indicates if the packet is a relayed one */ + void *arg; /* Pointer to the struct net_buf */ + u16_t src; /* Source address for relay packets */ + u16_t dst; /* Destination address for relay packets */ + u32_t timestamp; /* Timestamp recorded when the relay packet is posted to queue */ +} bt_mesh_msg_t; + +enum bt_mesh_adv_type { + BLE_MESH_ADV_PROV, + BLE_MESH_ADV_DATA, + BLE_MESH_ADV_BEACON, + BLE_MESH_ADV_URI, + BLE_MESH_ADV_BLE, +}; + +typedef void (*bt_mesh_adv_func_t)(struct net_buf *buf, u16_t duration, + int err, void *user_data); + +struct bt_mesh_adv { + const struct bt_mesh_send_cb *cb; + void *cb_data; + + u8_t type:3, + busy:1; + u8_t xmit; +}; + +typedef struct bt_mesh_adv *(*bt_mesh_adv_alloc_t)(int id); + +/* xmit_count: Number of retransmissions, i.e. 0 == 1 transmission */ +struct net_buf *bt_mesh_adv_create(enum bt_mesh_adv_type type, u8_t xmit, + s32_t timeout); + +typedef enum { + BLE_MESH_BUF_REF_EQUAL, + BLE_MESH_BUF_REF_SMALL, + BLE_MESH_BUF_REF_MAX, +} bt_mesh_buf_ref_flag_t; + +void bt_mesh_adv_buf_ref_debug(const char *func, struct net_buf *buf, + u8_t ref_cmp, bt_mesh_buf_ref_flag_t flag); + +struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool, + bt_mesh_adv_alloc_t get_id, + enum bt_mesh_adv_type type, + u8_t xmit, s32_t timeout); + +void bt_mesh_unref_buf_from_pool(struct net_buf_pool *pool); + +void bt_mesh_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, + void *cb_data); + +const bt_mesh_addr_t *bt_mesh_pba_get_addr(void); + +struct net_buf *bt_mesh_relay_adv_create(enum bt_mesh_adv_type type, u8_t xmit, + s32_t timeout); + +void bt_mesh_relay_adv_send(struct net_buf *buf, const struct bt_mesh_send_cb *cb, + void *cb_data, u16_t src, u16_t dst); + +u16_t bt_mesh_get_stored_relay_count(void); + +void bt_mesh_adv_update(void); + +void bt_mesh_adv_init(void); +void bt_mesh_adv_deinit(void); + +int bt_mesh_scan_enable(void); + +int bt_mesh_scan_disable(void); + +int bt_mesh_scan_with_wl_enable(void); + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +int bt_mesh_start_ble_advertising(const struct bt_mesh_ble_adv_param *param, + const struct bt_mesh_ble_adv_data *data, u8_t *index); + +int bt_mesh_stop_ble_advertising(u8_t index); +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +#ifdef __cplusplus +} +#endif + +#endif /* _ADV_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/beacon.c b/components/bt/esp_ble_mesh/mesh_core/beacon.c new file mode 100644 index 000000000..2f53aa49f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/beacon.c @@ -0,0 +1,475 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_BEACON) + +#include "adv.h" +#include "mesh.h" +#include "prov.h" +#include "crypto.h" +#include "beacon.h" +#include "access.h" +#include "foundation.h" +#include "proxy_client.h" +#include "mesh_main.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#if defined(CONFIG_BLE_MESH_FAST_PROV) +#define UNPROVISIONED_INTERVAL K_SECONDS(3) +#else +#define UNPROVISIONED_INTERVAL K_SECONDS(5) +#endif /* CONFIG_BLE_MESH_FAST_PROV */ +#define PROVISIONED_INTERVAL K_SECONDS(10) + +#define BEACON_TYPE_UNPROVISIONED 0x00 +#define BEACON_TYPE_SECURE 0x01 + +/* 3 transmissions, 20ms interval */ +#define UNPROV_XMIT BLE_MESH_TRANSMIT(2, 20) + +/* 1 transmission, 20ms interval */ +#define PROV_XMIT BLE_MESH_TRANSMIT(0, 20) + +static struct k_delayed_work beacon_timer; + +static struct bt_mesh_subnet *cache_check(u8_t data[21]) +{ + size_t subnet_size = 0U; + int i = 0; + + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (!memcmp(sub->beacon_cache, data, 21)) { + return sub; + } + } + + return NULL; +} + +static void cache_add(u8_t data[21], struct bt_mesh_subnet *sub) +{ + memcpy(sub->beacon_cache, data, 21); +} + +static void beacon_complete(int err, void *user_data) +{ + struct bt_mesh_subnet *sub = user_data; + + BT_DBG("err %d", err); + + sub->beacon_sent = k_uptime_get_32(); +} + +void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, + struct net_buf_simple *buf) +{ + u8_t flags = bt_mesh_net_flags(sub); + struct bt_mesh_subnet_keys *keys = NULL; + + net_buf_simple_add_u8(buf, BEACON_TYPE_SECURE); + + if (sub->kr_flag) { + keys = &sub->keys[1]; + } else { + keys = &sub->keys[0]; + } + + net_buf_simple_add_u8(buf, flags); + + /* Network ID */ + net_buf_simple_add_mem(buf, keys->net_id, 8); + + /* IV Index */ + net_buf_simple_add_be32(buf, bt_mesh.iv_index); + + net_buf_simple_add_mem(buf, sub->auth, 8); + + BT_INFO("net_idx 0x%03x iv_index 0x%08x flags 0x%02x", + sub->net_idx, bt_mesh.iv_index, flags); + BT_DBG("NetID %s Auth %s", bt_hex(keys->net_id, 8), + bt_hex(sub->auth, 8)); +} + +/* If the interval has passed or is within 5 seconds from now send a beacon */ +#define BEACON_THRESHOLD(sub) (K_SECONDS(10 * ((sub)->beacons_last + 1)) - \ + K_SECONDS(5)) + +static int secure_beacon_send(void) +{ + static const struct bt_mesh_send_cb send_cb = { + .end = beacon_complete, + }; + u32_t now = k_uptime_get_32(); + size_t subnet_size = 0U; + int i = 0; + + BT_DBG("%s", __func__); + + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + struct net_buf *buf; + u32_t time_diff; + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + time_diff = now - sub->beacon_sent; + if (time_diff < K_SECONDS(600) && + time_diff < BEACON_THRESHOLD(sub)) { + continue; + } + + /** + * If a node enables the Proxy Client functionality, and it + * succeeds to send Secure Network Beacon with GATT bearer, + * here we will continue to send Secure Network Beacon of + * other subnets. + */ +#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) + if (bt_mesh_proxy_client_beacon_send(sub)) { + continue; + } +#endif + + buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, PROV_XMIT, + K_NO_WAIT); + if (!buf) { + BT_ERR("%s, Unable to allocate beacon buffer", __func__); + return -ENOBUFS; + } + + bt_mesh_beacon_create(sub, &buf->b); + + bt_mesh_adv_send(buf, &send_cb, sub); + net_buf_unref(buf); + } + + return 0; +} + +#if defined(CONFIG_BLE_MESH_NODE) +static int unprovisioned_beacon_send(void) +{ +#if defined(CONFIG_BLE_MESH_PB_ADV) + const struct bt_mesh_prov *prov = NULL; + u8_t uri_hash[16] = { 0 }; + struct net_buf *buf = NULL; + u16_t oob_info = 0U; + + BT_DBG("%s", __func__); + + buf = bt_mesh_adv_create(BLE_MESH_ADV_BEACON, UNPROV_XMIT, K_NO_WAIT); + if (!buf) { + BT_ERR("%s, Unable to allocate beacon buffer", __func__); + return -ENOBUFS; + } + + prov = bt_mesh_prov_get(); + + net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED); + net_buf_add_mem(buf, prov->uuid, 16); + + if (prov->uri && bt_mesh_s1(prov->uri, uri_hash) == 0) { + oob_info = prov->oob_info | BLE_MESH_PROV_OOB_URI; + } else { + oob_info = prov->oob_info; + } + + net_buf_add_be16(buf, oob_info); + net_buf_add_mem(buf, uri_hash, 4); + + bt_mesh_adv_send(buf, NULL, NULL); + net_buf_unref(buf); + + if (prov->uri) { + size_t len; + + buf = bt_mesh_adv_create(BLE_MESH_ADV_URI, UNPROV_XMIT, + K_NO_WAIT); + if (!buf) { + BT_ERR("Unable to allocate URI buffer"); + return -ENOBUFS; + } + + len = strlen(prov->uri); + if (net_buf_tailroom(buf) < len) { + BT_WARN("Too long URI to fit advertising data"); + } else { + net_buf_add_mem(buf, prov->uri, len); + bt_mesh_adv_send(buf, NULL, NULL); + } + + net_buf_unref(buf); + } + +#endif /* CONFIG_BLE_MESH_PB_ADV */ + return 0; +} +#else /* CONFIG_BLE_MESH_NODE */ +static int unprovisioned_beacon_send(void) +{ + return 0; +} +#endif /* CONFIG_BLE_MESH_NODE */ + +static void update_beacon_observation(void) +{ + static bool first_half; + size_t subnet_size = 0U; + int i = 0; + + /* Observation period is 20 seconds, whereas the beacon timer + * runs every 10 seconds. We process what's happened during the + * window only after the second half. + */ + first_half = !first_half; + if (first_half) { + return; + } + + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + sub->beacons_last = sub->beacons_cur; + sub->beacons_cur = 0U; + } +} + +static bool ready_to_send(void) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + return true; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_get_node_count()) { + return true; + } + } + + return false; +} + +static void beacon_send(struct k_work *work) +{ + /* Don't send anything if we have an active provisioning link */ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + IS_ENABLED(CONFIG_BLE_MESH_PROV) && bt_prov_active()) { + k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); + return; + } + + BT_DBG("%s", __func__); + + if (ready_to_send()) { + update_beacon_observation(); + secure_beacon_send(); + + /* Only resubmit if beaconing is still enabled */ + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED || + bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) { + k_delayed_work_submit(&beacon_timer, + PROVISIONED_INTERVAL); + } + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node()) { + unprovisioned_beacon_send(); + k_delayed_work_submit(&beacon_timer, UNPROVISIONED_INTERVAL); + } + } + +} + +static void secure_beacon_recv(struct net_buf_simple *buf) +{ + u8_t *data = NULL, *net_id = NULL, *auth = NULL; + struct bt_mesh_subnet *sub = NULL; + u32_t iv_index = 0U; + bool new_key = false, kr_change = false, iv_change = false; + u8_t flags = 0U; + + if (buf->len < 21) { + BT_ERR("%s, Too short secure beacon (len %u)", __func__, buf->len); + return; + } + + sub = cache_check(buf->data); + if (sub) { + /* We've seen this beacon before - just update the stats */ + goto update_stats; + } + + /* So we can add to the cache if auth matches */ + data = buf->data; + + flags = net_buf_simple_pull_u8(buf); + net_id = net_buf_simple_pull_mem(buf, 8); + iv_index = net_buf_simple_pull_be32(buf); + auth = buf->data; + + BT_DBG("flags 0x%02x id %s iv_index 0x%08x", + flags, bt_hex(net_id, 8), iv_index); + + sub = bt_mesh_subnet_find(net_id, flags, iv_index, auth, &new_key); + if (!sub) { + BT_DBG("No subnet that matched beacon"); + return; + } + + if (sub->kr_phase == BLE_MESH_KR_PHASE_2 && !new_key) { + BT_WARN("Ignoring Phase 2 KR Update secured using old key"); + return; + } + + cache_add(data, sub); + + /* If we have NetKey0 accept initiation only from it */ + if (bt_mesh_primary_subnet_exist() && + sub->net_idx != BLE_MESH_KEY_PRIMARY) { + BT_WARN("Ignoring secure beacon on non-primary subnet"); + goto update_stats; + } + + BT_INFO("net_idx 0x%03x iv_index 0x%08x current iv_index 0x%08x", + sub->net_idx, iv_index, bt_mesh.iv_index); + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR) && + (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == + BLE_MESH_IV_UPDATE(flags))) { + bt_mesh_beacon_ivu_initiator(false); + } + + iv_change = bt_mesh_net_iv_update(iv_index, BLE_MESH_IV_UPDATE(flags)); + + kr_change = bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(flags), new_key); + if (kr_change) { + bt_mesh_net_beacon_update(sub); + } + + if (iv_change) { + /* Update all subnets */ + bt_mesh_net_sec_update(NULL); + } else if (kr_change) { + /* Key Refresh without IV Update only impacts one subnet */ + bt_mesh_net_sec_update(sub); + } + +update_stats: + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED && + sub->beacons_cur < 0xff) { + sub->beacons_cur++; + } +} + +void bt_mesh_beacon_recv(struct net_buf_simple *buf, s8_t rssi) +{ + u8_t type = 0U; + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (buf->len < 1) { + BT_ERR("%s, Too short beacon", __func__); + return; + } + + type = net_buf_simple_pull_u8(buf); + switch (type) { + case BEACON_TYPE_UNPROVISIONED: + BT_DBG("Unprovisioned device beacon received"); + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + bt_mesh_is_provisioner_en()) { + bt_mesh_provisioner_unprov_beacon_recv(buf, rssi); + } + break; + case BEACON_TYPE_SECURE: + secure_beacon_recv(buf); + break; + default: + BT_DBG("Unknown beacon type 0x%02x", type); + break; + } +} + +void bt_mesh_beacon_init(void) +{ + k_delayed_work_init(&beacon_timer, beacon_send); +} + +void bt_mesh_beacon_deinit(void) +{ + k_delayed_work_free(&beacon_timer); +} + +void bt_mesh_beacon_ivu_initiator(bool enable) +{ + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_INITIATOR, enable); + + if (enable) { + k_work_submit(&beacon_timer.work); + } else if (bt_mesh_beacon_get() == BLE_MESH_BEACON_DISABLED) { + k_delayed_work_cancel(&beacon_timer); + } +} + +void bt_mesh_beacon_enable(void) +{ + size_t subnet_size = 0U; + int i = 0; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + k_work_submit(&beacon_timer.work); + return; + } + + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + sub->beacons_last = 0U; + sub->beacons_cur = 0U; + + bt_mesh_net_beacon_update(sub); + } + + k_work_submit(&beacon_timer.work); +} + +void bt_mesh_beacon_disable(void) +{ + if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR)) { + k_delayed_work_cancel(&beacon_timer); + } +} diff --git a/components/bt/esp_ble_mesh/mesh_core/beacon.h b/components/bt/esp_ble_mesh/mesh_core/beacon.h new file mode 100644 index 000000000..a7dbdd81f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/beacon.h @@ -0,0 +1,35 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BEACON_H_ +#define _BEACON_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void bt_mesh_beacon_enable(void); +void bt_mesh_beacon_disable(void); + +void bt_mesh_beacon_ivu_initiator(bool enable); + +void bt_mesh_beacon_recv(struct net_buf_simple *buf, s8_t rssi); + +void bt_mesh_beacon_create(struct bt_mesh_subnet *sub, + struct net_buf_simple *buf); + +void bt_mesh_beacon_init(void); +void bt_mesh_beacon_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BEACON_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c new file mode 100644 index 000000000..801c26341 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c @@ -0,0 +1,2033 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015-2016 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "bta/bta_api.h" +#include "bta/bta_gatt_api.h" +#include "bta/bta_gatt_common.h" +#include "bta_gattc_int.h" +#include "stack/btm_ble_api.h" +#include "p_256_ecc_pp.h" +#include "osi/future.h" +#include "device/controller.h" + +#include "mbedtls/aes.h" +#include "bt_common.h" + +#include "mesh_hci.h" +#include "mesh_aes_encrypt.h" +#include "mesh_bearer_adapt.h" +#include "mesh_common.h" +#include "provisioner_prov.h" + +struct bt_mesh_dev bt_mesh_dev; + +#define BLE_MESH_BTM_CHECK_STATUS(func) do { \ + tBTM_STATUS __status = (func); \ + if ((__status != BTM_SUCCESS) && (__status != BTM_CMD_STARTED)) { \ + BT_ERR("%s, Invalid status %d", __func__, __status); \ + return -1; \ + } \ + } while(0); + +#define BLE_MESH_GATT_GET_CONN_ID(conn_id) (((u16_t)(conn_id)) >> 8) +#define BLE_MESH_GATT_CREATE_CONN_ID(gatt_if, conn_id) ((u16_t)((((u8_t)(conn_id)) << 8) | ((u8_t)(gatt_if)))) + +/* We don't need to manage the BLE_MESH_DEV_ADVERTISING flags in the version of Bluedroid, + * it will manage it in the BTM layer. + */ +#define BLE_MESH_DEV 0 + +/* P-256 Variables */ +static u8_t bt_mesh_public_key[64]; +static BT_OCTET32 bt_mesh_private_key; + +/* Scan related functions */ +static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb; +static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data); + +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER +/* Using UUID with a fixed pattern 0x96 for BLE Mesh GATT Proxy Server */ +#define BLE_MESH_GATTS_APP_UUID_BYTE 0x96 +/* the gatt database list to save the attribute table */ +static sys_slist_t bt_mesh_gatts_db; + +/* Static Variables */ +static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN]; +static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb; +static tBTA_GATTS_IF bt_mesh_gatts_if; +static BD_ADDR bt_mesh_gatts_addr; +static u16_t svc_handle, char_handle; +static future_t *future_mesh; + +/* Static Functions */ +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle); +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +/* Using UUID with a fixed pattern 0x97 for BLE Mesh GATT Proxy Client */ +#define BLE_MESH_GATTC_APP_UUID_BYTE 0x97 +static struct gattc_prov_info { + /* Service to be found depends on the type of adv pkt received */ + struct bt_mesh_conn conn; + bt_mesh_addr_t addr; + u16_t service_uuid; + u16_t mtu; + bool wr_desc_done; /* Indicate if write char descriptor event is received */ + u16_t start_handle; /* Service attribute start handle */ + u16_t end_handle; /* Service attribute end handle */ + u16_t data_in_handle; /* Data In Characteristic attribute handle */ + u16_t data_out_handle; /* Data Out Characteristic attribute handle */ + u16_t ccc_handle; /* Data Out Characteristic CCC attribute handle */ +} bt_mesh_gattc_info[BLE_MESH_MAX_CONN]; +static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb; +static tBTA_GATTC_IF bt_mesh_gattc_if; +#endif + +int bt_mesh_host_init(void) +{ + return 0; +} + +void bt_mesh_hci_init(void) +{ + const uint8_t *features = controller_get_interface()->get_features_ble()->as_array; + if (features != NULL) { + memcpy(bt_mesh_dev.features[0], features, 8); + memcpy(bt_mesh_dev.le.features, features, 8); + } + + /** + * Currently 20ms non-connectable adv interval is supported, and we need to add + * a flag to indicate this support. + */ +#ifdef CONFIG_BLE_MESH_HCI_5_0 + bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0; +#else + bt_mesh_dev.hci_version = controller_get_interface()->get_bt_version()->hci_version; +#endif + bt_mesh_dev.lmp_version = controller_get_interface()->get_bt_version()->lmp_version; + bt_mesh_dev.hci_revision = controller_get_interface()->get_bt_version()->hci_revision; + bt_mesh_dev.lmp_subversion = controller_get_interface()->get_bt_version()->lmp_subversion; + bt_mesh_dev.manufacturer = controller_get_interface()->get_bt_version()->manufacturer; + + const uint8_t *p = controller_get_interface()->get_ble_supported_states(); + uint64_t states_fh = 0, states_sh = 0; + STREAM_TO_UINT32(states_fh, p); + STREAM_TO_UINT32(states_sh, p); + bt_mesh_dev.le.states = (states_sh << 32) | states_fh; +} + +static void bt_mesh_scan_results_change_2_bta(tBTM_INQ_RESULTS *p_inq, u8_t *p_eir, + tBTA_DM_SEARCH_CBACK *p_scan_cback) +{ + tBTM_INQ_INFO *p_inq_info = NULL; + tBTA_DM_SEARCH result = {0}; + + bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr); + result.inq_res.rssi = p_inq->rssi; + result.inq_res.ble_addr_type = p_inq->ble_addr_type; + result.inq_res.inq_result_type = p_inq->inq_result_type; + result.inq_res.device_type = p_inq->device_type; + result.inq_res.flag = p_inq->flag; + result.inq_res.adv_data_len = p_inq->adv_data_len; + result.inq_res.scan_rsp_len = p_inq->scan_rsp_len; + memcpy(result.inq_res.dev_class, p_inq->dev_class, sizeof(DEV_CLASS)); + result.inq_res.ble_evt_type = p_inq->ble_evt_type; + + /* application will parse EIR to find out remote device name */ + result.inq_res.p_eir = p_eir; + + if ((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) { + /* initialize remt_name_not_required to FALSE so that we get the name by default */ + result.inq_res.remt_name_not_required = FALSE; + } + + if (p_scan_cback) { + p_scan_cback(BTA_DM_INQ_RES_EVT, &result); + } + + if (p_inq_info) { + /* application indicates if it knows the remote name, inside the callback + copy that to the inquiry data base*/ + if (result.inq_res.remt_name_not_required) { + p_inq_info->appl_knows_rem_name = TRUE; + } + } +} + +static void bt_mesh_scan_results_cb(tBTM_INQ_RESULTS *p_inq, u8_t *p_eir) +{ + bt_mesh_scan_results_change_2_bta(p_inq, p_eir, bt_mesh_scan_result_callback); +} + +static bool valid_adv_param(const struct bt_mesh_adv_param *param) +{ + if (!(param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) { +#if BLE_MESH_DEV + if (bt_mesh_dev.hci_version < BLE_MESH_HCI_VERSION_5_0 && + param->interval_min < 0x00a0) { + return false; + } +#endif + } + + if (param->interval_min > param->interval_max || + param->interval_min < 0x0010 || param->interval_max > 0x4000) { + return false; + } + + return true; +} + +static int set_adv_data(u16_t hci_op, const struct bt_mesh_adv_data *ad, size_t ad_len) +{ + struct bt_mesh_hci_cp_set_adv_data param = {0}; + int i; + + if (ad == NULL || ad_len == 0) { + return 0; + } + + for (i = 0; i < ad_len; i++) { + /* Check if ad fit in the remaining buffer */ + if (param.len + ad[i].data_len + 2 > 31) { + return -EINVAL; + } + + param.data[param.len++] = ad[i].data_len + 1; + param.data[param.len++] = ad[i].type; + + memcpy(¶m.data[param.len], ad[i].data, ad[i].data_len); + param.len += ad[i].data_len; + } + + /* Set adv data and scan rsp data. */ + if (hci_op == BLE_MESH_HCI_OP_SET_ADV_DATA) { + BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteAdvDataRaw(param.data, param.len)); + } else if (hci_op == BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA) { + BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteScanRspRaw(param.data, param.len)); + } + + return 0; +} + +static void start_adv_completed_cb(u8_t status) +{ +#if BLE_MESH_DEV + if (!status) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); + } +#endif +} + +static bool valid_scan_param(const struct bt_mesh_scan_param *param) +{ + if (param->type != BLE_MESH_SCAN_PASSIVE && + param->type != BLE_MESH_SCAN_ACTIVE) { + return false; + } + + if (param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_DISABLE && + param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_ENABLE) { + return false; + } + + if (param->interval < 0x0004 || param->interval > 0x4000) { + return false; + } + + if (param->window < 0x0004 || param->window > 0x4000) { + return false; + } + + if (param->window > param->interval) { + return false; + } + + return true; +} + +static int start_le_scan(u8_t scan_type, u16_t interval, u16_t window, u8_t filter_dup, u8_t scan_fil_policy) +{ + UINT8 addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */ + tGATT_IF client_if = 0xFF; /* Default GATT interface id */ + + BLE_MESH_BTM_CHECK_STATUS( + BTM_BleSetScanFilterParams(client_if, interval, window, scan_type, addr_type_own, + filter_dup, scan_fil_policy, NULL)); + + /* BLE Mesh scan permanently, so no duration of scan here */ + BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL)); + +#if BLE_MESH_DEV + if (scan_type == BLE_MESH_SCAN_ACTIVE) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } +#endif + + return 0; +} + +static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) +{ + bt_mesh_addr_t addr = {0}; + u8_t adv_type = 0U; + s8_t rssi = 0; + + BT_DBG("%s, event = %d", __func__, event); + + if (event == BTA_DM_INQ_RES_EVT) { + /* TODO: How to process scan response here? */ + addr.type = p_data->inq_res.ble_addr_type; + memcpy(addr.val, p_data->inq_res.bd_addr, BLE_MESH_ADDR_LEN); + rssi = p_data->inq_res.rssi; + adv_type = p_data->inq_res.ble_evt_type; + + /* scan rsp len: p_data->inq_res.scan_rsp_len */ + struct net_buf_simple *buf = bt_mesh_alloc_buf(p_data->inq_res.adv_data_len); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + net_buf_simple_add_mem(buf, p_data->inq_res.p_eir, p_data->inq_res.adv_data_len); + + if (bt_mesh_scan_dev_found_cb != NULL) { + bt_mesh_scan_dev_found_cb(&addr, rssi, adv_type, buf); + } + bt_mesh_free(buf); + } else if (event == BTA_DM_INQ_CMPL_EVT) { + BT_INFO("%s, Scan completed, number of scan response %d", __func__, p_data->inq_cmpl.num_resps); + } else { + BT_WARN("%s, Unexpected event 0x%x", __func__, event); + } +} + +/* APIs functions */ +int bt_le_adv_start(const struct bt_mesh_adv_param *param, + const struct bt_mesh_adv_data *ad, size_t ad_len, + const struct bt_mesh_adv_data *sd, size_t sd_len) +{ + tBTA_START_ADV_CMPL_CBACK *p_start_adv_cb = NULL; + tBTM_BLE_ADV_CHNL_MAP channel_map = 0U; + tBLE_ADDR_TYPE addr_type_own = 0U; + tBLE_BD_ADDR p_dir_bda = {0}; + tBTM_BLE_AFP adv_fil_pol = 0U; + u8_t adv_type = 0U; + int err = 0; + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return -EALREADY; + } +#endif + + if (!valid_adv_param(param)) { + BT_ERR("%s, Invalid adv parameters", __func__); + return -EINVAL; + } + + err = set_adv_data(BLE_MESH_HCI_OP_SET_ADV_DATA, ad, ad_len); + if (err) { + BT_ERR("%s, Failed to set adv data", __func__); + return err; + } + + /* + * We need to set SCAN_RSP when enabling advertising type that allows + * for Scan Requests. + * + * If sd was not provided but we enable connectable undirected + * advertising sd needs to be cleared from values set by previous calls. + * Clearing sd is done by calling set_adv_data() with NULL data and zero len. + * So following condition check is unusual but correct. + */ + if (sd && (param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) { + err = set_adv_data(BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA, sd, sd_len); + if (err) { + BT_ERR("%s, Failed to set scan rsp data", __func__); + return err; + } + } + + if (param->options & BLE_MESH_ADV_OPT_CONNECTABLE) { + adv_type = BLE_MESH_ADV_IND; + } else if (sd != NULL) { + adv_type = BLE_MESH_ADV_SCAN_IND; + } else { + adv_type = BLE_MESH_ADV_NONCONN_IND; + } + addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */ + channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39; + adv_fil_pol = BLE_MESH_AP_SCAN_CONN_ALL; + p_start_adv_cb = start_adv_completed_cb; + + /* Check if we can start adv using BTM_BleSetAdvParamsStartAdvCheck */ + BLE_MESH_BTM_CHECK_STATUS( + BTM_BleSetAdvParamsAll(param->interval_min, param->interval_max, adv_type, + addr_type_own, &p_dir_bda, + channel_map, adv_fil_pol, p_start_adv_cb)); + BLE_MESH_BTM_CHECK_STATUS(BTM_BleStartAdv()); + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); + + if (!(param->options & BLE_MESH_ADV_OPT_ONE_TIME)) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + } +#endif + + return 0; +} + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +int bt_mesh_ble_adv_start(const struct bt_mesh_ble_adv_param *param, + const struct bt_mesh_ble_adv_data *data) +{ + struct bt_mesh_hci_cp_set_adv_data set = {0}; + tBTM_BLE_ADV_CHNL_MAP channel_map = 0U; + tBTM_BLE_AFP adv_fil_pol = 0U; + tBLE_BD_ADDR p_dir_bda = {0}; + + if (data && param->adv_type != BLE_MESH_ADV_DIRECT_IND && + param->adv_type != BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) { + if (data->adv_data_len) { + set.len = data->adv_data_len; + memcpy(set.data, data->adv_data, data->adv_data_len); + BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteAdvDataRaw(set.data, set.len)); + } + if (data->scan_rsp_data_len && param->adv_type != BLE_MESH_ADV_NONCONN_IND) { + set.len = data->scan_rsp_data_len; + memcpy(set.data, data->scan_rsp_data, data->scan_rsp_data_len); + BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteScanRspRaw(set.data, set.len)); + } + } + + channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39; + adv_fil_pol = BLE_MESH_AP_SCAN_CONN_ALL; + if (param->own_addr_type == BLE_MESH_ADDR_PUBLIC_ID || + param->own_addr_type == BLE_MESH_ADDR_RANDOM_ID || + param->adv_type == BLE_MESH_ADV_DIRECT_IND || + param->adv_type == BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) { + p_dir_bda.type = param->peer_addr_type; + memcpy(p_dir_bda.bda, param->peer_addr, BLE_MESH_ADDR_LEN); + } + + /* Check if we can start adv using BTM_BleSetAdvParamsStartAdvCheck */ + BLE_MESH_BTM_CHECK_STATUS( + BTM_BleSetAdvParamsAll(param->interval, param->interval, param->adv_type, + param->own_addr_type, &p_dir_bda, + channel_map, adv_fil_pol, NULL)); + BLE_MESH_BTM_CHECK_STATUS(BTM_BleStartAdv()); + + return 0; +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +int bt_le_adv_stop(void) +{ +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return 0; + } +#endif + + BLE_MESH_BTM_CHECK_STATUS(BTM_BleBroadcast(false, NULL)); + +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + + return 0; +} + +int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb) +{ + int err = 0; + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + return -EALREADY; + } +#endif + + if (!valid_scan_param(param)) { + return -EINVAL; + } + +#if BLE_MESH_DEV + if (param->filter_dup) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } +#endif + + err = start_le_scan(param->type, param->interval, param->window, param->filter_dup, param->scan_fil_policy); + if (err) { + return err; + } + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); +#endif + + bt_mesh_scan_dev_found_cb = cb; + return err; +} + +int bt_le_scan_stop(void) +{ +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL)); + } +#else + BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL)); +#endif + + bt_mesh_scan_dev_found_cb = NULL; + return 0; +} + +#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST +int bt_le_update_white_list(struct bt_mesh_white_list *wl) +{ + if (wl == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (BTM_BleUpdateAdvWhitelist(wl->add_remove, wl->remote_bda, + (tBTM_ADD_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) { + return -EIO; + } + + return 0; +} +#endif + +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER +static void bt_mesh_bta_gatts_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data) +{ + switch (event) { + case BTA_GATTS_REG_EVT: + if (p_data->reg_oper.status == BTA_GATT_OK) { + bt_mesh_gatts_if = p_data->reg_oper.server_if; + } + break; + case BTA_GATTS_READ_EVT: { + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(p_data->req_data.p_data->read_req.handle); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->req_data.conn_id); + tBTA_GATTS_RSP rsp = {0}; + u8_t buf[100] = {0}; + u16_t len = 0; + + BT_DBG("%s, read: handle = %d", __func__, p_data->req_data.p_data->read_req.handle); + + if (attr != NULL && attr->read != NULL) { + if ((len = attr->read(&bt_mesh_gatts_conn[index], attr, buf, 100, + p_data->req_data.p_data->read_req.offset)) > 0) { + rsp.attr_value.handle = p_data->req_data.p_data->read_req.handle; + rsp.attr_value.len = len; + memcpy(&rsp.attr_value.value[0], buf, len); + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + p_data->req_data.status, &rsp); + BT_DBG("%s, Send gatts read response, handle = %x", __func__, attr->handle); + } else { + BT_WARN("%s, BLE Mesh gatts read failed", __func__); + } + } + break; + } + case BTA_GATTS_WRITE_EVT: { + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(p_data->req_data.p_data->write_req.handle); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->req_data.conn_id); + u16_t len = 0; + + BT_DBG("%s, write: handle = %d, len = %d, data = %s", __func__, p_data->req_data.p_data->write_req.handle, + p_data->req_data.p_data->write_req.len, + bt_hex(p_data->req_data.p_data->write_req.value, p_data->req_data.p_data->write_req.len)); + + if (attr != NULL && attr->write != NULL) { + if ((len = attr->write(&bt_mesh_gatts_conn[index], attr, + p_data->req_data.p_data->write_req.value, + p_data->req_data.p_data->write_req.len, + p_data->req_data.p_data->write_req.offset, 0)) > 0) { + if (p_data->req_data.p_data->write_req.need_rsp) { + BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id, + p_data->req_data.status, NULL); + BT_DBG("%s, send mesh write rsp, handle = %x", __func__, attr->handle); + } + } + } + break; + } + case BTA_GATTS_EXEC_WRITE_EVT: + break; + case BTA_GATTS_MTU_EVT: + break; + case BTA_GATTS_CONF_EVT: + break; + case BTA_GATTS_CREATE_EVT: + svc_handle = p_data->create.service_id; + BT_DBG("%s, svc_handle = %d, future_mesh = %p", __func__, svc_handle, future_mesh); + if (future_mesh != NULL) { + future_ready(future_mesh, FUTURE_SUCCESS); + } + break; + case BTA_GATTS_ADD_INCL_SRVC_EVT: + svc_handle = p_data->add_result.attr_id; + if (future_mesh != NULL) { + future_ready(future_mesh, FUTURE_SUCCESS); + } + break; + case BTA_GATTS_ADD_CHAR_EVT: + char_handle = p_data->add_result.attr_id; + if (future_mesh != NULL) { + future_ready(future_mesh, FUTURE_SUCCESS); + } + break; + case BTA_GATTS_ADD_CHAR_DESCR_EVT: + char_handle = p_data->add_result.attr_id; + if (future_mesh != NULL) { + future_ready(future_mesh, FUTURE_SUCCESS); + } + break; + case BTA_GATTS_DELELTE_EVT: + break; + case BTA_GATTS_START_EVT: + break; + case BTA_GATTS_STOP_EVT: + break; + case BTA_GATTS_CONNECT_EVT: +#if BLE_MESH_DEV + /* When connection is created, advertising will be stopped automatically. */ + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->connected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id); + (bt_mesh_gatts_conn_cb->connected)(&bt_mesh_gatts_conn[index], 0); + } + memcpy(bt_mesh_gatts_addr, p_data->conn.remote_bda, BLE_MESH_ADDR_LEN); + /* This is for EspBleMesh Android app. When it tries to connect with the + * device at the first time and it fails due to some reason. And after + * the second connection, the device needs to send GATT service change + * indication to the phone manually to notify it discovering service again. + */ + BTA_GATTS_SendServiceChangeIndication(bt_mesh_gatts_if, bt_mesh_gatts_addr); + } + break; + case BTA_GATTS_DISCONNECT_EVT: +#if BLE_MESH_DEV + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->disconnected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id); + (bt_mesh_gatts_conn_cb->disconnected)(&bt_mesh_gatts_conn[index], p_data->conn.reason); + } + memset(bt_mesh_gatts_addr, 0x0, BLE_MESH_ADDR_LEN); + } + break; + case BTA_GATTS_CLOSE_EVT: + break; + default: + break; + } +} + +void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb) +{ + bt_mesh_gatts_conn_cb = cb; +} + +void bt_mesh_gatts_conn_cb_deregister(void) +{ + bt_mesh_gatts_conn_cb = NULL; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle) +{ + struct bt_mesh_gatt_service *svc = NULL; + struct bt_mesh_gatt_attr *attr = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + attr = &svc->attrs[i]; + /* Check the attrs handle is equal to the handle or not */ + if (attr->handle == handle) { + return attr; + } + } + } + + return NULL; +} + +static void bt_mesh_gatts_foreach_attr(u16_t start_handle, u16_t end_handle, + bt_mesh_gatt_attr_func_t func, void *user_data) +{ + struct bt_mesh_gatt_service *svc = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + struct bt_mesh_gatt_attr *attr = &svc->attrs[i]; + + /* Check if attribute handle is within range */ + if (attr->handle < start_handle || + attr->handle > end_handle) { + continue; + } + + if (func(attr, user_data) == BLE_MESH_GATT_ITER_STOP) { + return; + } + } + } +} + +static u8_t find_next(const struct bt_mesh_gatt_attr *attr, void *user_data) +{ + struct bt_mesh_gatt_attr **next = user_data; + + *next = (struct bt_mesh_gatt_attr *)attr; + + return BLE_MESH_GATT_ITER_STOP; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_attr_next(const struct bt_mesh_gatt_attr *attr) +{ + struct bt_mesh_gatt_attr *next = NULL; + + bt_mesh_gatts_foreach_attr(attr->handle + 1, attr->handle + 1, find_next, &next); + + return next; +} + +ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t buf_len, u16_t offset, + const void *value, u16_t value_len) +{ + u16_t len = 0U; + + if (offset > value_len) { + return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET); + } + + len = MIN(buf_len, value_len - offset); + + BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset, len); + + memcpy(buf, value + offset, len); + + return len; +} + +struct gatts_incl { + u16_t start_handle; + u16_t end_handle; + u16_t uuid16; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_attr *incl = attr->user_data; + struct bt_mesh_uuid *uuid = incl->user_data; + struct gatts_incl pdu = {0}; + u8_t value_len = 0U; + + /* First attr points to the start handle */ + pdu.start_handle = sys_cpu_to_le16(incl->handle); + value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle); + + /* + * Core 4.2, Vol 3, Part G, 3.2, + * The Service UUID shall only be present when the UUID is a 16-bit Bluetooth UUID. + */ + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + value_len += sizeof(pdu.uuid16); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_uuid *uuid = attr->user_data; + + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + u16_t uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &uuid16, 2); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, + BLE_MESH_UUID_128(uuid)->val, 16); +} + +struct gatts_chrc { + u8_t properties; + u16_t value_handle; + union { + u16_t uuid16; + u8_t uuid[16]; + }; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, void *buf, + u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_char *chrc = attr->user_data; + const struct bt_mesh_gatt_attr *next = NULL; + struct gatts_chrc pdu = {0}; + u8_t value_len = 0U; + + pdu.properties = chrc->properties; + /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534: + * 3.3.2 Characteristic Value Declaration + * The Characteristic Value declaration contains the value of the + * characteristic. It is the first Attribute after the characteristic + * declaration. All characteristic definitions shall have a + * Characteristic Value declaration. + */ + next = bt_mesh_gatts_attr_next(attr); + if (!next) { + BT_WARN("%s, No value for characteristic at 0x%04x", __func__, attr->handle); + pdu.value_handle = 0x0000; + } else { + pdu.value_handle = sys_cpu_to_le16(next->handle); + } + value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle); + + if (chrc->uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(chrc->uuid)->val); + value_len += 2; + } else { + memcpy(pdu.uuid, BLE_MESH_UUID_128(chrc->uuid)->val, 16); + value_len += 16; + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +static void bta_uuid_to_bt_mesh_uuid(tBT_UUID *bta_uuid, const struct bt_mesh_uuid *uuid) +{ + assert(uuid != NULL && bta_uuid != NULL); + + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + bta_uuid->len = LEN_UUID_16; + bta_uuid->uu.uuid16 = BLE_MESH_UUID_16(uuid)->val; + } else if (uuid->type == BLE_MESH_UUID_TYPE_32) { + bta_uuid->len = LEN_UUID_32; + bta_uuid->uu.uuid32 = BLE_MESH_UUID_32(uuid)->val; + } else if (uuid->type == BLE_MESH_UUID_TYPE_128) { + bta_uuid->len = LEN_UUID_128; + memcpy(bta_uuid->uu.uuid128, BLE_MESH_UUID_128(uuid)->val, LEN_UUID_128); + } else { + BT_ERR("%s, Invalid mesh uuid type = %d", __func__, uuid->type); + } + + return; +} + +static int gatts_register(struct bt_mesh_gatt_service *svc) +{ + struct bt_mesh_gatt_service *last = NULL; + u16_t handle = 0U; + + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + handle = 0; + goto populate; + } + + last = SYS_SLIST_PEEK_TAIL_CONTAINER(&bt_mesh_gatts_db, last, node); + handle = last->attrs[last->attr_count - 1].handle; + BT_DBG("%s, handle = %d", __func__, handle); + + ((void) handle); + +populate: + sys_slist_append(&bt_mesh_gatts_db, &svc->node); + return 0; +} + +static int gatts_deregister(struct bt_mesh_gatt_service *svc) +{ + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + return 0; + } + + sys_slist_find_and_remove(&bt_mesh_gatts_db, &svc->node); + return 0; +} + +static tBTA_GATT_PERM bt_mesh_perm_to_bta_perm(u8_t perm) +{ + tBTA_GATT_PERM bta_perm = 0; + + if ((perm & BLE_MESH_GATT_PERM_READ) == BLE_MESH_GATT_PERM_READ) { + bta_perm |= BTA_GATT_PERM_READ; + } + + if ((perm & BLE_MESH_GATT_PERM_WRITE) == BLE_MESH_GATT_PERM_WRITE) { + bta_perm |= BTA_GATT_PERM_WRITE; + } + + if ((perm & BLE_MESH_GATT_PERM_READ_ENCRYPT) == BLE_MESH_GATT_PERM_READ_ENCRYPT) { + bta_perm |= BTA_GATT_PERM_READ_ENCRYPTED; + } + + if ((perm & BLE_MESH_GATT_PERM_WRITE_ENCRYPT) == BLE_MESH_GATT_PERM_WRITE_ENCRYPT) { + bta_perm |= BTA_GATT_PERM_WRITE_ENCRYPTED; + } + + if ((perm & BLE_MESH_GATT_PERM_READ_AUTHEN) == BLE_MESH_GATT_PERM_READ_AUTHEN) { + bta_perm |= BTA_GATT_PERM_READ_ENC_MITM; + } + + if ((perm & BLE_MESH_GATT_PERM_WRITE_AUTHEN) == BLE_MESH_GATT_PERM_WRITE_AUTHEN) { + bta_perm |= BTA_GATT_PERM_WRITE_ENC_MITM; + } + + return bta_perm; +} + +int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc) +{ + tBT_UUID bta_uuid = {0}; + + assert(svc != NULL); + + for (int i = 0; i < svc->attr_count; i++) { + if (svc->attrs[i].uuid->type == BLE_MESH_UUID_TYPE_16) { + switch (BLE_MESH_UUID_16(svc->attrs[i].uuid)->val) { + case BLE_MESH_UUID_GATT_PRIMARY_VAL: { + future_mesh = future_new(); + bta_uuid_to_bt_mesh_uuid(&bta_uuid, (struct bt_mesh_uuid *)svc->attrs[i].user_data); + BTA_GATTS_CreateService(bt_mesh_gatts_if, + &bta_uuid, 0, svc->attr_count, true); + if (future_await(future_mesh) == FUTURE_FAIL) { + BT_ERR("%s, Failed to add primary service", __func__); + return ESP_FAIL; + } + svc->attrs[i].handle = svc_handle; + BT_DBG("Add primary service: svc_uuid = %x, perm = %d, svc_handle = %d", bta_uuid.uu.uuid16, svc->attrs[i].perm, svc_handle); + break; + } + case BLE_MESH_UUID_GATT_SECONDARY_VAL: { + future_mesh = future_new(); + bta_uuid_to_bt_mesh_uuid(&bta_uuid, (struct bt_mesh_uuid *)svc->attrs[i].user_data); + BTA_GATTS_CreateService(bt_mesh_gatts_if, + &bta_uuid, 0, svc->attr_count, false); + if (future_await(future_mesh) == FUTURE_FAIL) { + BT_ERR("%s, Failed to add secondary service", __func__); + return ESP_FAIL; + } + svc->attrs[i].handle = svc_handle; + BT_DBG("Add secondary service: svc_uuid = %x, perm = %d, svc_handle = %d", bta_uuid.uu.uuid16, svc->attrs[i].perm, svc_handle); + break; + } + case BLE_MESH_UUID_GATT_INCLUDE_VAL: { + break; + } + case BLE_MESH_UUID_GATT_CHRC_VAL: { + future_mesh = future_new(); + struct bt_mesh_gatt_char *gatts_chrc = (struct bt_mesh_gatt_char *)svc->attrs[i].user_data; + bta_uuid_to_bt_mesh_uuid(&bta_uuid, gatts_chrc->uuid); + BTA_GATTS_AddCharacteristic(svc_handle, &bta_uuid, bt_mesh_perm_to_bta_perm(svc->attrs[i + 1].perm), gatts_chrc->properties, NULL, NULL); + if (future_await(future_mesh) == FUTURE_FAIL) { + BT_ERR("%s, Failed to add characteristic", __func__); + return ESP_FAIL; + } + /* All the characteristic should have two handles: the declaration handle and the value handle */ + svc->attrs[i].handle = char_handle - 1; + svc->attrs[i + 1].handle = char_handle; + BT_DBG("Add characteristic: char_uuid = %x, char_handle = %d, perm = %d, char_pro = %d", BLE_MESH_UUID_16(gatts_chrc->uuid)->val, char_handle, svc->attrs[i + 1].perm, gatts_chrc->properties); + break; + } + case BLE_MESH_UUID_GATT_CEP_VAL: + case BLE_MESH_UUID_GATT_CUD_VAL: + case BLE_MESH_UUID_GATT_CCC_VAL: + case BLE_MESH_UUID_GATT_SCC_VAL: + case BLE_MESH_UUID_GATT_CPF_VAL: + case BLE_MESH_UUID_VALID_RANGE_VAL: + case BLE_MESH_UUID_HIDS_EXT_REPORT_VAL: + case BLE_MESH_UUID_HIDS_REPORT_REF_VAL: + case BLE_MESH_UUID_ES_CONFIGURATION_VAL: + case BLE_MESH_UUID_ES_MEASUREMENT_VAL: + case BLE_MESH_UUID_ES_TRIGGER_SETTING_VAL: { + future_mesh = future_new(); + bta_uuid_to_bt_mesh_uuid(&bta_uuid, svc->attrs[i].uuid); + BTA_GATTS_AddCharDescriptor(svc_handle, bt_mesh_perm_to_bta_perm(svc->attrs[i].perm), &bta_uuid, NULL, NULL); + if (future_await(future_mesh) == FUTURE_FAIL) { + BT_ERR("%s, Failed to add descriptor", __func__); + return ESP_FAIL; + } + svc->attrs[i].handle = char_handle; + BT_DBG("Add descriptor: descr_uuid = %x, perm= %d, descr_handle = %d", BLE_MESH_UUID_16(svc->attrs[i].uuid)->val, svc->attrs[i].perm, char_handle); + break; + } + default: + break; + } + } + } + + if (svc_handle != 0) { + svc_handle = 0; + } + + gatts_register(svc); + return 0; +} + +int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + + gatts_deregister(svc); + + BTA_GATTS_DeleteService(svc->attrs[0].handle); + + return 0; +} + +int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason) +{ + UNUSED(reason); + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gatts_if, conn->handle); + BTA_GATTS_Close(conn_id); + return 0; +} + +int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + + BTA_GATTS_DeleteService(svc->attrs[0].handle); + return 0; +} + +int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gatts_if, conn->handle); + BTA_GATTS_HandleValueIndication(conn_id, attr->handle, len, (u8_t *)data, false); + return 0; +} + +u16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn) +{ + return BTA_GATT_GetLocalMTU(); +} + +/* APIs added by Espressif */ +int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc) +{ + if (!svc) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("Stop service:%d", svc->attrs[0].handle); + + BTA_GATTS_StopService(svc->attrs[0].handle); + return 0; +} + +int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc) +{ + struct bt_mesh_uuid_16 *uuid_16 = NULL; + struct bt_mesh_uuid *uuid = NULL; + + if (!svc) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("Start service:%d", svc->attrs[0].handle); + + BTA_GATTS_StartService(svc->attrs[0].handle, BTA_GATT_TRANSPORT_LE); + + /* For EspBleMesh Android app, it does not disconnect after provisioning + * is done, and hence we send GATT service change indication manually + * when Mesh Proxy Service is started after provisioning. + */ + uuid = (struct bt_mesh_uuid *)svc->attrs[0].user_data; + if (uuid && uuid->type == BLE_MESH_UUID_TYPE_16) { + uuid_16 = (struct bt_mesh_uuid_16 *)uuid; + BT_DBG("%s, type 0x%02x, val 0x%04x", __func__, uuid_16->uuid.type, uuid_16->val); + if (uuid_16->val == BLE_MESH_UUID_MESH_PROXY_VAL) { + BTA_GATTS_SendServiceChangeIndication(bt_mesh_gatts_if, bt_mesh_gatts_addr); + } + } + + return 0; +} + +int bt_mesh_gatts_set_local_device_name(const char *name) +{ + BLE_MESH_BTM_CHECK_STATUS(BTM_SetLocalDeviceName((char *)name)); + + return 0; +} + +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) +{ + bt_mesh_gattc_conn_cb = cb; +} + +void bt_mesh_gattc_conn_cb_deregister(void) +{ + bt_mesh_gattc_conn_cb = NULL; +} + +u8_t bt_mesh_gattc_get_free_conn_count(void) +{ + u8_t count = 0U; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == 0xFFFF && + bt_mesh_gattc_info[i].service_uuid == 0x0000) { + ++count; + } + } + + return count; +} + +u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + return bt_mesh_gattc_info[i].service_uuid; + } + } + + BT_ERR("%s, Conn is not found", __func__); + return 0; +} + +/** For provisioner acting as a GATT client, it may follow the procedures + * listed below. + * 1. Create connection with the unprovisioned device + * 2. Exchange MTU size + * 3. Find Mesh Prov Service in the device's service database + * 4. Find Mesh Prov Data In/Out characteristic within the service + * 5. Get CCC of Mesh Prov Data Out Characteristic + * 6. Set the Notification bit of CCC + */ + +int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid) +{ + u8_t zero[6] = {0}; + int i; + + if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) || + (addr->type > BLE_ADDR_RANDOM)) { + BT_ERR("%s, Invalid remote address", __func__); + return -EINVAL; + } + + if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL && + service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + BT_ERR("%s, Invalid service uuid 0x%04x", __func__, service_uuid); + return -EINVAL; + } + + /* Check if already creating connection with the device */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN)) { + BT_WARN("%s, Already create connection with %s", + __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + return -EALREADY; + } + } + + /* Find empty element in queue to store device info */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if ((bt_mesh_gattc_info[i].conn.handle == 0xFFFF) && + (bt_mesh_gattc_info[i].service_uuid == 0x0000)) { + memcpy(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + bt_mesh_gattc_info[i].addr.type = addr->type; + /* Service to be found after exchanging mtu size */ + bt_mesh_gattc_info[i].service_uuid = service_uuid; + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_WARN("%s, gattc info is full", __func__); + return -ENOMEM; + } + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL)); + } +#else + BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL)); +#endif /* BLE_MESH_DEV */ + + BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + + /* Min_interval: 250ms + * Max_interval: 250ms + * Slave_latency: 0x0 + * Supervision_timeout: 32 sec + */ + BTA_DmSetBlePrefConnParams(bt_mesh_gattc_info[i].addr.val, 0xC8, 0xC8, 0x00, 0xC80); + + BTA_GATTC_Open(bt_mesh_gattc_if, bt_mesh_gattc_info[i].addr.val, + bt_mesh_gattc_info[i].addr.type, true, BTA_GATT_TRANSPORT_LE); + + return i; +} + +void bt_mesh_gattc_exchange_mtu(u8_t index) +{ + /** Set local MTU and exchange with GATT server. + * ATT_MTU >= 69 for Mesh GATT Prov Service + * ATT_NTU >= 33 for Mesh GATT Proxy Service + */ + u16_t conn_id = 0U; + + conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_if, bt_mesh_gattc_info[index].conn.handle); + + BTA_GATTC_ConfigureMTU(conn_id); +} + +u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + return bt_mesh_gattc_info[i].mtu; + } + } + + return 0; +} + +int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + u16_t conn_id = 0U; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_if, bt_mesh_gattc_info[i].conn.handle); + BTA_GATTC_WriteCharValue(conn_id, bt_mesh_gattc_info[i].data_in_handle, + BTA_GATTC_TYPE_WRITE_NO_RSP, len, + (u8_t *)data, BTA_GATT_AUTH_REQ_NONE); + return 0; + } + } + + BT_ERR("%s, Conn is not found", __func__); + return -EEXIST; +} + +void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) +{ + /** Disconnect + * Clear proper proxy server information + * Clear proper prov_link information + * Clear proper bt_mesh_gattc_info information + * Here in adapter, we just clear proper bt_mesh_gattc_info, and + * when proxy_disconnected callback comes, the proxy server + * information and prov_link information should be cleared. + */ + u16_t conn_id = 0U; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_if, bt_mesh_gattc_info[i].conn.handle); + BTA_GATTC_Close(conn_id); + return; + } + } + + BT_ERR("%s, Conn is not found", __func__); + return; +} + +/** Mesh Provisioning Service: 0x1827 + * Mesh Provisioning Data In: 0x2ADB + * Mesh Provisioning Data Out: 0x2ADC + * Mesh Proxy Service: 0x1828 + * Mesh Proxy Data In: 0x2ADD + * Mesh PROXY Data Out: 0x2ADE + */ +static void bt_mesh_bta_gattc_cb(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) +{ + struct bt_mesh_conn *conn = NULL; + u16_t handle = 0U; + ssize_t len = 0; + int i = 0; + + switch (event) { + case BTA_GATTC_REG_EVT: + if (p_data->reg_oper.status == BTA_GATT_OK) { + u8_t uuid[16] = { [0 ... 15] = BLE_MESH_GATTC_APP_UUID_BYTE }; + + BT_DBG("BTA_GATTC_REG_EVT"); + + if (p_data->reg_oper.app_uuid.len == LEN_UUID_128 && + !memcmp(p_data->reg_oper.app_uuid.uu.uuid128, uuid, 16)) { + bt_mesh_gattc_if = p_data->reg_oper.client_if; + BT_DBG("bt_mesh_gattc_if is %d", bt_mesh_gattc_if); + } + } + break; + case BTA_GATTC_CFG_MTU_EVT: { + if (p_data->cfg_mtu.status == BTA_GATT_OK) { + BT_DBG("BTA_GATTC_CFG_MTU_EVT, cfg_mtu is %d", p_data->cfg_mtu.mtu); + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->cfg_mtu.conn_id); + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + bt_mesh_gattc_info[i].mtu = p_data->cfg_mtu.mtu; + + /* Search Mesh Provisioning Service or Mesh Proxy Service */ + tBT_UUID service_uuid = { + .len = sizeof(bt_mesh_gattc_info[i].service_uuid), + .uu.uuid16 = bt_mesh_gattc_info[i].service_uuid, + }; + BTA_GATTC_ServiceSearchRequest(p_data->cfg_mtu.conn_id, &service_uuid); + break; + } + } + } + break; + } + case BTA_GATTC_SEARCH_RES_EVT: { + BT_DBG("BTA_GATTC_SEARCH_RES_EVT"); + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->srvc_res.conn_id); + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + if (p_data->srvc_res.service_uuid.uuid.len == 2 && + p_data->srvc_res.service_uuid.uuid.uu.uuid16 == bt_mesh_gattc_info[i].service_uuid) { + bt_mesh_gattc_info[i].start_handle = p_data->srvc_res.start_handle; + bt_mesh_gattc_info[i].end_handle = p_data->srvc_res.end_handle; + } + break; + } + } + break; + } + case BTA_GATTC_SEARCH_CMPL_EVT: { + if (p_data->search_cmpl.status == BTA_GATT_OK) { + BT_DBG("BTA_GATTC_SEARCH_CMPL_EVT"); + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->search_cmpl.conn_id); + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + conn = &bt_mesh_gattc_info[i].conn; + break; + } + } + + if (conn == NULL) { + BT_ERR("%s, Conn handle is not found", __func__); + return; + } + + if (bt_mesh_gattc_info[i].start_handle == 0x00 || + bt_mesh_gattc_info[i].end_handle == 0x00 || + (bt_mesh_gattc_info[i].start_handle > bt_mesh_gattc_info[i].end_handle)) { + bt_mesh_gattc_disconnect(conn); + return; + } + + u16_t notify_en = BLE_MESH_GATT_CCC_NOTIFY; + btgatt_db_element_t *result = NULL; + tBT_UUID char_uuid = {0}; + tBTA_GATT_STATUS status = 0U; + tBTA_GATT_UNFMT write = {0}; + u16_t count = 0; + u16_t num = 0; + + /* Get the characteristic num within Mesh Provisioning/Proxy Service */ + BTA_GATTC_GetDBSizeByType(p_data->search_cmpl.conn_id, BTGATT_DB_CHARACTERISTIC, + bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle, + BTA_GATTC_INVALID_HANDLE, &count); + if (count != 2) { + bt_mesh_gattc_disconnect(conn); + return; + } + + /* Get Mesh Provisioning/Proxy Data In/Out Characteristic */ + for (int j = 0; j != 2; j++) { + /** First: find Mesh Provisioning/Proxy Data In Characteristic + * Second: find Mesh Provisioning/Proxy Data Out Characteristic + */ + char_uuid.len = 2; + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + char_uuid.uu.uuid16 = BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL + j; + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + char_uuid.uu.uuid16 = BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL + j; + } + + BTA_GATTC_GetCharByUUID(p_data->search_cmpl.conn_id, bt_mesh_gattc_info[i].start_handle, + bt_mesh_gattc_info[i].end_handle, char_uuid, &result, &num); + + if (!result) { + bt_mesh_gattc_disconnect(conn); + return; + } + + if (num != 1) { + bt_mesh_free(result); + bt_mesh_gattc_disconnect(conn); + return; + } + + if (!j) { + if (!(result[0].properties & BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP)) { + bt_mesh_free(result); + bt_mesh_gattc_disconnect(conn); + return; + } + bt_mesh_gattc_info[i].data_in_handle = result[0].attribute_handle; + } else { + if (!(result[0].properties & BLE_MESH_GATT_CHRC_NOTIFY)) { + bt_mesh_free(result); + bt_mesh_gattc_disconnect(conn); + return; + } + bt_mesh_gattc_info[i].data_out_handle = result[0].attribute_handle; + } + + bt_mesh_free(result); + result = NULL; + } + + /* Register Notification fot Mesh Provisioning/Proxy Data Out Characteristic */ + status = BTA_GATTC_RegisterForNotifications(bt_mesh_gattc_if, bt_mesh_gattc_info[i].addr.val, + bt_mesh_gattc_info[i].data_out_handle); + if (status != BTA_GATT_OK) { + bt_mesh_gattc_disconnect(conn); + return; + } + + /** After notification is registered, get descriptor number of the + * Mesh Provisioning/Proxy Data Out Characteristic + */ + BTA_GATTC_GetDBSizeByType(p_data->search_cmpl.conn_id, BTGATT_DB_DESCRIPTOR, + bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle, + bt_mesh_gattc_info[i].data_out_handle, &num); + if (!num) { + bt_mesh_gattc_disconnect(conn); + return; + } + + /* Get CCC of Mesh Provisioning/Proxy Data Out Characteristic */ + char_uuid.len = 2; + char_uuid.uu.uuid16 = BLE_MESH_UUID_GATT_CCC_VAL; + BTA_GATTC_GetDescrByCharHandle(p_data->search_cmpl.conn_id, bt_mesh_gattc_info[i].data_out_handle, + char_uuid, &result, &num); + if (!result) { + bt_mesh_gattc_disconnect(conn); + return; + } + + if (num != 1) { + bt_mesh_free(result); + bt_mesh_gattc_disconnect(conn); + return; + } + + bt_mesh_gattc_info[i].ccc_handle = result[0].attribute_handle; + + /** Enable Notification of Mesh Provisioning/Proxy Data Out + * Characteristic Descriptor. + */ + write.len = sizeof(notify_en); + write.p_value = (u8_t *)¬ify_en; + BTA_GATTC_WriteCharDescr(p_data->search_cmpl.conn_id, result[0].attribute_handle, + BTA_GATTC_TYPE_WRITE, &write, BTA_GATT_AUTH_REQ_NONE); + + bt_mesh_free(result); + result = NULL; + } + break; + } + case BTA_GATTC_READ_DESCR_EVT: + break; + case BTA_GATTC_WRITE_DESCR_EVT: { + if (p_data->write.status == BTA_GATT_OK) { + BT_DBG("BTA_GATTC_WRITE_DESCR_EVT"); + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->write.conn_id); + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + conn = &bt_mesh_gattc_info[i].conn; + break; + } + } + + if (conn == NULL) { + BT_ERR("%s, Conn handle is not found", __func__); + return; + } + + if (bt_mesh_gattc_info[i].ccc_handle != p_data->write.handle) { + BT_WARN("%s, gattc ccc_handle is not matched", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->prov_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, prov_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, proxy_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } + } + break; + } + case BTA_GATTC_NOTIF_EVT: { + BT_DBG("BTA_GATTC_NOTIF_EVT"); + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->notify.conn_id); + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + if (bt_mesh_gattc_info[i].wr_desc_done == false) { + BT_DBG("Receive notification before finishing to write ccc"); + return; + } + + conn = &bt_mesh_gattc_info[i].conn; + break; + } + } + + if (conn == NULL) { + BT_ERR("%s, Conn handle is not found", __func__); + return; + } + + if (memcmp(bt_mesh_gattc_info[i].addr.val, p_data->notify.bda, BLE_MESH_ADDR_LEN) || + bt_mesh_gattc_info[i].data_out_handle != p_data->notify.handle || + p_data->notify.is_notify == false) { + BT_ERR("%s, Notification error", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_notify != NULL) { + len = bt_mesh_gattc_conn_cb->prov_notify(&bt_mesh_gattc_info[i].conn, + p_data->notify.value, p_data->notify.len); + if (len < 0) { + BT_ERR("%s, prov_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_notify != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_notify(&bt_mesh_gattc_info[i].conn, + p_data->notify.value, p_data->notify.len); + if (len < 0) { + BT_ERR("%s, proxy_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return; + } + } + } + break; + } + case BTA_GATTC_READ_CHAR_EVT: + break; + case BTA_GATTC_WRITE_CHAR_EVT: + break; + case BTA_GATTC_PREP_WRITE_EVT: + break; + case BTA_GATTC_EXEC_EVT: + break; + case BTA_GATTC_OPEN_EVT: { + BT_DBG("BTA_GATTC_OPEN_EVT"); + /** After current connection is established, provisioner can + * use BTA_DmBleScan() to re-enable scan. + */ + tBTM_STATUS status; +#if BLE_MESH_DEV + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL); + if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) { + BT_ERR("%s, Invalid status %d", __func__, status); + break; + } + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + } +#else + status = BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL); + if (status != BTM_SUCCESS && status != BTM_CMD_STARTED) { + BT_ERR("%s, Invalid status %d", __func__, status); + break; + } +#endif /* BLE_MESH_DEV */ + break; + } + case BTA_GATTC_CLOSE_EVT: + BT_DBG("BTA_GATTC_CLOSE_EVT"); + break; + case BTA_GATTC_CONNECT_EVT: { + BT_DBG("BTA_GATTC_CONNECT_EVT"); + + if (bt_mesh_gattc_if != p_data->connect.client_if) { + BT_ERR("%s, gattc_if & connect_if don't match", __func__); + return; + } + + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->connected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, p_data->connect.remote_bda, BLE_MESH_ADDR_LEN)) { + bt_mesh_gattc_info[i].conn.handle = BLE_MESH_GATT_GET_CONN_ID(p_data->connect.conn_id); + (bt_mesh_gattc_conn_cb->connected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, i); + break; + } + } + } + break; + } + case BTA_GATTC_DISCONNECT_EVT: { + BT_DBG("BTA_GATTC_DISCONNECT_EVT"); + + if (bt_mesh_gattc_if != p_data->disconnect.client_if) { + BT_ERR("%s, gattc_if & disconnect_if don't match", __func__); + return; + } + + handle = BLE_MESH_GATT_GET_CONN_ID(p_data->disconnect.conn_id); + + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, p_data->disconnect.remote_bda, BLE_MESH_ADDR_LEN)) { + if (bt_mesh_gattc_info[i].conn.handle == handle) { + (bt_mesh_gattc_conn_cb->disconnected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, p_data->disconnect.reason); + if (!bt_mesh_gattc_info[i].wr_desc_done) { + /* Add this in case connection is established, connected event comes, but + * connection is terminated before server->filter_type is set to PROV. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } + } else { + /* Add this in case connection is failed to be established, and here we + * need to clear some provision link info, like connecting flag, device + * uuid, address info, etc. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + /* Decrease provisioner pbg_count */ + bt_mesh_provisioner_pbg_count_dec(); + } +#endif + /* Reset corresponding gattc info */ + memset(&bt_mesh_gattc_info[i], 0, sizeof(bt_mesh_gattc_info[i])); + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = GATT_DEF_BLE_MTU_SIZE; + bt_mesh_gattc_info[i].wr_desc_done = false; + break; + } + } + } + break; + } + case BTA_GATTC_CONGEST_EVT: + break; + case BTA_GATTC_SRVC_CHG_EVT: + break; + default: + break; + } +} +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_inc(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); + + return conn; +} + +void bt_mesh_conn_unref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_dec(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); +} + +void bt_mesh_gatt_init(void) +{ + BTA_GATT_SetLocalMTU(GATT_DEF_BLE_MTU_SIZE); + +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + tBT_UUID gatts_app_uuid = {LEN_UUID_128, {0}}; + memset(&gatts_app_uuid.uu.uuid128, BLE_MESH_GATTS_APP_UUID_BYTE, LEN_UUID_128); + BTA_GATTS_AppRegister(&gatts_app_uuid, bt_mesh_bta_gatts_cb); +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + tBT_UUID gattc_app_uuid = {LEN_UUID_128, {0}}; + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = GATT_DEF_BLE_MTU_SIZE; /* Default MTU_SIZE 23 */ + bt_mesh_gattc_info[i].wr_desc_done = false; + } + memset(&gattc_app_uuid.uu.uuid128, BLE_MESH_GATTC_APP_UUID_BYTE, LEN_UUID_128); + BTA_GATTC_AppRegister(&gattc_app_uuid, bt_mesh_bta_gattc_cb); +#endif +} + +void bt_mesh_gatt_deinit(void) +{ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + BTA_GATTS_AppDeregister(bt_mesh_gatts_if); + memset(bt_mesh_gatts_addr, 0, BLE_MESH_ADDR_LEN); + bt_mesh_gatts_if = 0U; + svc_handle = 0U; + char_handle = 0U; +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + BTA_GATTC_AppDeregister(bt_mesh_gattc_if); + bt_mesh_gattc_if = 0U; + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + memset(&bt_mesh_gattc_info[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_gattc_info[i].service_uuid = 0U; + bt_mesh_gattc_info[i].mtu = GATT_DEF_BLE_MTU_SIZE; /* Default MTU_SIZE 23 */ + bt_mesh_gattc_info[i].wr_desc_done = false; + bt_mesh_gattc_info[i].start_handle = 0U; + bt_mesh_gattc_info[i].end_handle = 0U; + bt_mesh_gattc_info[i].data_in_handle = 0U; + bt_mesh_gattc_info[i].data_out_handle = 0U; + bt_mesh_gattc_info[i].ccc_handle = 0U; + } +#endif +} + +void bt_mesh_adapt_init(void) +{ + BT_DBG("%s", __func__); + + /* initialization of P-256 parameters */ + p_256_init_curve(KEY_LENGTH_DWORDS_P256); + + /* Set "bt_mesh_dev.flags" to 0 (only the "BLE_MESH_DEV_HAS_PUB_KEY" + * flag is used) here, because we need to make sure each time after + * the private key is initialized, a corresponding public key must + * be generated. + */ + bt_mesh_atomic_set(bt_mesh_dev.flags, 0); + bt_mesh_rand(bt_mesh_private_key, sizeof(bt_mesh_private_key)); +} + +int bt_mesh_rand(void *buf, size_t len) +{ + int i; + + if (buf == NULL || len == 0) { + BT_ERR("%s, Invalid parameter", __func__); + return -EAGAIN; + } + + for (i = 0; i < (int)(len / sizeof(u32_t)); i++) { + u32_t rand = esp_random(); + memcpy(buf + i * sizeof(u32_t), &rand, sizeof(u32_t)); + } + + BT_DBG("%s, rand: %s", __func__, bt_hex(buf, len)); + return 0; +} + +void bt_mesh_set_private_key(const u8_t pri_key[32]) +{ + memcpy(bt_mesh_private_key, pri_key, 32); +} + +const u8_t *bt_mesh_pub_key_get(void) +{ + BT_OCTET32 private_key = {0}; + Point public_key = {0}; + + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { + return bt_mesh_public_key; + } + + /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BV-12-C requires + * different public key for each provisioning procedure. + * Note: if enabled, when Provisioner provision multiple devices + * at the same time, this may cause invalid confirmation value. + * + * Use the following code for generating different private key + * for each provisioning procedure. + * + * if (bt_mesh_rand(bt_mesh_private_key, BT_OCTET32_LEN)) { + * BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); + * return NULL; + * } + */ + + memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); + ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *)private_key, KEY_LENGTH_DWORDS_P256); + + memcpy(bt_mesh_public_key, public_key.x, BT_OCTET32_LEN); + memcpy(bt_mesh_public_key + BT_OCTET32_LEN, public_key.y, BT_OCTET32_LEN); + + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY); + + BT_DBG("Public Key %s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key))); + + return bt_mesh_public_key; +} + +bool bt_mesh_check_public_key(const u8_t key[64]) +{ + struct p256_pub_key { + u8_t x[32]; + u8_t y[32]; + } check = {0}; + + sys_memcpy_swap(check.x, key, 32); + sys_memcpy_swap(check.y, key + 32, 32); + + return ECC_CheckPointIsInElliCur_P256((Point *)&check); +} + +int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx) +{ + BT_OCTET32 private_key = {0}; + Point peer_pub_key = {0}; + Point new_pub_key = {0}; + + BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, BT_OCTET32_LEN)); + + memcpy(private_key, bt_mesh_private_key, BT_OCTET32_LEN); + memcpy(peer_pub_key.x, remote_pk, BT_OCTET32_LEN); + memcpy(peer_pub_key.y, &remote_pk[BT_OCTET32_LEN], BT_OCTET32_LEN); + + BT_DBG("remote public key x = %s", bt_hex(peer_pub_key.x, BT_OCTET32_LEN)); + BT_DBG("remote public key y = %s", bt_hex(peer_pub_key.y, BT_OCTET32_LEN)); + + ECC_PointMult(&new_pub_key, &peer_pub_key, (DWORD *)private_key, KEY_LENGTH_DWORDS_P256); + + BT_DBG("new public key x = %s", bt_hex(new_pub_key.x, 32)); + BT_DBG("new public key y = %s", bt_hex(new_pub_key.y, 32)); + + if (cb != NULL) { + cb((const u8_t *)new_pub_key.x, idx); + } + + return 0; +} + +#if CONFIG_MBEDTLS_HARDWARE_AES +static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_le, + u8_t *const cipher_text_le, u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb = {0}; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + mem_rcopy(&aes_ctx.key[0], key_le, 16); + mem_rcopy(&ecb.clear_text[0], clear_text_le, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + if (cipher_text_le) { + mem_rcopy(cipher_text_le, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } + + if (cipher_text_be) { + memcpy(cipher_text_be, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } +} + +static void ecb_encrypt_be(u8_t const *const key_be, u8_t const *const clear_text_be, + u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb = {0}; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + memcpy(&aes_ctx.key[0], key_be, 16); + memcpy(&ecb.clear_text[0], clear_text_be, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + memcpy(cipher_text_be, &ecb.cipher_text[0], sizeof(ecb.cipher_text)); +} +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ + +int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt(key, plaintext, enc_data, NULL); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s = {0}; + u8_t tmp[16] = {0}; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + sys_memcpy_swap(tmp, key, 16); + + if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_memcpy_swap(tmp, plaintext, 16); + + if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_mem_swap(enc_data, 16); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt_be(key, plaintext, enc_data); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s = {0}; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +int bt_mesh_update_exceptional_list(u8_t sub_code, u8_t type, void *info) +{ + BD_ADDR value = {0}; + + if ((sub_code > BLE_MESH_EXCEP_LIST_CLEAN) || + (type > BLE_MESH_EXCEP_INFO_MESH_PROXY_ADV)) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (type == BLE_MESH_EXCEP_INFO_MESH_LINK_ID) { + if (!info) { + BT_ERR("%s, NULL Provisioning Link ID", __func__); + return -EINVAL; + } + sys_memcpy_swap(value, info, sizeof(u32_t)); + } + + BT_DBG("%s, %s type 0x%x", __func__, sub_code ? "Remove" : "Add", type); + + /* The parameter "device_info" can't be NULL in the API */ + BLE_MESH_BTM_CHECK_STATUS(BTM_UpdateBleDuplicateExceptionalList(sub_code, type, value, NULL)); + + return 0; +} +#endif diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c new file mode 100644 index 000000000..0390f4923 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_cli.c @@ -0,0 +1,1711 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) + +#include "btc_ble_mesh_config_model.h" + +#include "mesh.h" +#include "foundation.h" +#include "mesh_common.h" +#include "cfg_cli.h" + +/* 2 byte dummy opcode for getting compile time buffer sizes. */ +#define DUMMY_2_BYTE_OP BLE_MESH_MODEL_OP_2(0xff, 0xff) + +s32_t config_msg_timeout; + +static bt_mesh_config_client_t *cli; + +static const bt_mesh_client_op_pair_t cfg_op_pair[] = { + { OP_BEACON_GET, OP_BEACON_STATUS }, + { OP_BEACON_SET, OP_BEACON_STATUS }, + { OP_DEV_COMP_DATA_GET, OP_DEV_COMP_DATA_STATUS }, + { OP_DEFAULT_TTL_GET, OP_DEFAULT_TTL_STATUS }, + { OP_DEFAULT_TTL_SET, OP_DEFAULT_TTL_STATUS }, + { OP_GATT_PROXY_GET, OP_GATT_PROXY_STATUS }, + { OP_GATT_PROXY_SET, OP_GATT_PROXY_STATUS }, + { OP_RELAY_GET, OP_RELAY_STATUS }, + { OP_RELAY_SET, OP_RELAY_STATUS }, + { OP_MOD_PUB_GET, OP_MOD_PUB_STATUS }, + { OP_MOD_PUB_SET, OP_MOD_PUB_STATUS }, + { OP_MOD_PUB_VA_SET, OP_MOD_PUB_STATUS }, + { OP_MOD_SUB_ADD, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_VA_ADD, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_DEL, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_VA_DEL, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_OVERWRITE, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_VA_OVERWRITE, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_DEL_ALL, OP_MOD_SUB_STATUS }, + { OP_MOD_SUB_GET, OP_MOD_SUB_LIST }, + { OP_MOD_SUB_GET_VND, OP_MOD_SUB_LIST_VND }, + { OP_NET_KEY_ADD, OP_NET_KEY_STATUS }, + { OP_NET_KEY_UPDATE, OP_NET_KEY_STATUS }, + { OP_NET_KEY_DEL, OP_NET_KEY_STATUS }, + { OP_NET_KEY_GET, OP_NET_KEY_LIST }, + { OP_APP_KEY_ADD, OP_APP_KEY_STATUS }, + { OP_APP_KEY_UPDATE, OP_APP_KEY_STATUS }, + { OP_APP_KEY_DEL, OP_APP_KEY_STATUS }, + { OP_APP_KEY_GET, OP_APP_KEY_LIST }, + { OP_NODE_IDENTITY_GET, OP_NODE_IDENTITY_STATUS }, + { OP_NODE_IDENTITY_SET, OP_NODE_IDENTITY_STATUS }, + { OP_MOD_APP_BIND, OP_MOD_APP_STATUS }, + { OP_MOD_APP_UNBIND, OP_MOD_APP_STATUS }, + { OP_SIG_MOD_APP_GET, OP_SIG_MOD_APP_LIST }, + { OP_VND_MOD_APP_GET, OP_VND_MOD_APP_LIST }, + { OP_NODE_RESET, OP_NODE_RESET_STATUS }, + { OP_FRIEND_GET, OP_FRIEND_STATUS }, + { OP_FRIEND_SET, OP_FRIEND_STATUS }, + { OP_KRP_GET, OP_KRP_STATUS }, + { OP_KRP_SET, OP_KRP_STATUS }, + { OP_HEARTBEAT_PUB_GET, OP_HEARTBEAT_PUB_STATUS }, + { OP_HEARTBEAT_PUB_SET, OP_HEARTBEAT_PUB_STATUS }, + { OP_HEARTBEAT_SUB_GET, OP_HEARTBEAT_SUB_STATUS }, + { OP_HEARTBEAT_SUB_SET, OP_HEARTBEAT_SUB_STATUS }, + { OP_LPN_TIMEOUT_GET, OP_LPN_TIMEOUT_STATUS }, + { OP_NET_TRANSMIT_GET, OP_NET_TRANSMIT_STATUS }, + { OP_NET_TRANSMIT_SET, OP_NET_TRANSMIT_STATUS }, +}; + +static bt_mesh_mutex_t cfg_client_lock; + +static void bt_mesh_cfg_client_mutex_new(void) +{ + if (!cfg_client_lock.mutex) { + bt_mesh_mutex_create(&cfg_client_lock); + } +} + +static void bt_mesh_cfg_client_mutex_free(void) +{ + bt_mesh_mutex_free(&cfg_client_lock); +} + +static void bt_mesh_cfg_client_lock(void) +{ + bt_mesh_mutex_lock(&cfg_client_lock); +} + +static void bt_mesh_cfg_client_unlock(void) +{ + bt_mesh_mutex_unlock(&cfg_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive configuration status message timeout"); + + bt_mesh_cfg_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_config_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_CONFIG_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_cfg_client_unlock(); + + return; +} + +static void cfg_client_cancel(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + void *status, size_t len) +{ + bt_mesh_client_node_t *node = NULL; + struct net_buf_simple buf = {0}; + u8_t evt_type = 0xFF; + + if (!model || !ctx) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + /* If it is a publish message, sent to the user directly. */ + buf.data = (u8_t *)status; + buf.len = (u16_t)len; + + bt_mesh_cfg_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); + if (!node) { + BT_DBG("Unexpected config status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case OP_BEACON_GET: + case OP_DEV_COMP_DATA_GET: + case OP_DEFAULT_TTL_GET: + case OP_GATT_PROXY_GET: + case OP_RELAY_GET: + case OP_MOD_PUB_GET: + case OP_MOD_SUB_GET: + case OP_MOD_SUB_GET_VND: + case OP_NET_KEY_GET: + case OP_APP_KEY_GET: + case OP_NODE_IDENTITY_GET: + case OP_SIG_MOD_APP_GET: + case OP_VND_MOD_APP_GET: + case OP_FRIEND_GET: + case OP_KRP_GET: + case OP_HEARTBEAT_PUB_GET: + case OP_HEARTBEAT_SUB_GET: + case OP_LPN_TIMEOUT_GET: + case OP_NET_TRANSMIT_GET: + evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_GET_STATE; + break; + case OP_BEACON_SET: + case OP_DEFAULT_TTL_SET: + case OP_GATT_PROXY_SET: + case OP_RELAY_SET: + case OP_MOD_PUB_SET: + case OP_MOD_PUB_VA_SET: + case OP_MOD_SUB_ADD: + case OP_MOD_SUB_VA_ADD: + case OP_MOD_SUB_DEL: + case OP_MOD_SUB_VA_DEL: + case OP_MOD_SUB_OVERWRITE: + case OP_MOD_SUB_VA_OVERWRITE: + case OP_MOD_SUB_DEL_ALL: + case OP_NET_KEY_ADD: + case OP_NET_KEY_UPDATE: + case OP_NET_KEY_DEL: + case OP_APP_KEY_ADD: + case OP_APP_KEY_UPDATE: + case OP_APP_KEY_DEL: + case OP_NODE_IDENTITY_SET: + case OP_MOD_APP_BIND: + case OP_MOD_APP_UNBIND: + case OP_NODE_RESET: + case OP_FRIEND_SET: + case OP_KRP_SET: + case OP_HEARTBEAT_PUB_SET: + case OP_HEARTBEAT_SUB_SET: + case OP_NET_TRANSMIT_SET: + evt_type = BTC_BLE_MESH_EVT_CONFIG_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_config_client_cb_evt_to_btc( + opcode, evt_type, model, ctx, (const u8_t *)status, len); + } + } + + bt_mesh_cfg_client_unlock(); + + switch (ctx->recv_op) { + case OP_DEV_COMP_DATA_STATUS: { + struct bt_mesh_cfg_comp_data_status *val; + val = (struct bt_mesh_cfg_comp_data_status *)status; + bt_mesh_free_buf(val->comp_data); + break; + } + case OP_MOD_SUB_LIST: + case OP_MOD_SUB_LIST_VND: { + struct bt_mesh_cfg_mod_sub_list *val = status; + bt_mesh_free_buf(val->addr); + break; + } + case OP_NET_KEY_LIST: { + struct bt_mesh_cfg_net_key_list *val = status; + bt_mesh_free_buf(val->net_idx); + break; + } + case OP_APP_KEY_LIST: { + struct bt_mesh_cfg_app_key_list *val = status; + bt_mesh_free_buf(val->app_idx); + break; + } + case OP_SIG_MOD_APP_LIST: + case OP_VND_MOD_APP_LIST: { + struct bt_mesh_cfg_mod_app_list *val = status; + bt_mesh_free_buf(val->app_idx); + break; + } + default: + break; + } +} + +static void comp_data_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_comp_data_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.page = net_buf_simple_pull_u8(buf); + status.comp_data = bt_mesh_alloc_buf(buf->len); + if (!status.comp_data) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + net_buf_simple_add_mem(status.comp_data, buf->data, buf->len); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_comp_data_status)); +} + +static void state_status_u8(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t status = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(u8_t)); +} + +static void beacon_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + state_status_u8(model, ctx, buf); +} + +static void ttl_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + state_status_u8(model, ctx, buf); +} + +static void friend_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + state_status_u8(model, ctx, buf); +} + +static void gatt_proxy_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + state_status_u8(model, ctx, buf); +} + +static void relay_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_relay_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.relay = net_buf_simple_pull_u8(buf); + status.retransmit = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_relay_status)); +} + +static void net_key_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_netkey_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.net_idx = net_buf_simple_pull_le16(buf) & 0xfff; + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_netkey_status)); +} + +static void app_key_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_appkey_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + key_idx_unpack(buf, &status.net_idx, &status.app_idx); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_appkey_status)); +} + +static void mod_app_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_mod_app_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.elem_addr = net_buf_simple_pull_le16(buf); + status.app_idx = net_buf_simple_pull_le16(buf); + if (buf->len >= 4) { + status.cid = net_buf_simple_pull_le16(buf); + } else { + status.cid = BLE_MESH_CID_NVAL; + } + status.mod_id = net_buf_simple_pull_le16(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_app_status)); +} + +static void mod_pub_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_mod_pub_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.elem_addr = net_buf_simple_pull_le16(buf); + status.addr = net_buf_simple_pull_le16(buf); + status.app_idx = net_buf_simple_pull_le16(buf); + status.cred_flag = (status.app_idx & BIT(12)); + status.app_idx &= BIT_MASK(12); + status.ttl = net_buf_simple_pull_u8(buf); + status.period = net_buf_simple_pull_u8(buf); + status.transmit = net_buf_simple_pull_u8(buf); + if (buf->len >= 4) { + status.cid = net_buf_simple_pull_le16(buf); + } else { + status.cid = BLE_MESH_CID_NVAL; + } + status.mod_id = net_buf_simple_pull_le16(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_pub_status)); +} + +static void mod_sub_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_mod_sub_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.elem_addr = net_buf_simple_pull_le16(buf); + status.sub_addr = net_buf_simple_pull_le16(buf); + if (buf->len >= 4) { + status.cid = net_buf_simple_pull_le16(buf); + } else { + status.cid = BLE_MESH_CID_NVAL; + } + status.mod_id = net_buf_simple_pull_le16(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_mod_sub_status)); +} + +static void hb_sub_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_hb_sub_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.src = net_buf_simple_pull_le16(buf); + status.dst = net_buf_simple_pull_le16(buf); + status.period = net_buf_simple_pull_u8(buf); + status.count = net_buf_simple_pull_u8(buf); + status.min = net_buf_simple_pull_u8(buf); + status.max = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_hb_sub_status)); +} + +static void hb_pub_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_hb_pub_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.dst = net_buf_simple_pull_le16(buf); + status.count = net_buf_simple_pull_u8(buf); + status.period = net_buf_simple_pull_u8(buf); + status.ttl = net_buf_simple_pull_u8(buf); + status.feat = net_buf_simple_pull_u8(buf); + status.net_idx = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_hb_sub_status)); +} + +static void node_reset_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + cfg_client_cancel(model, ctx, NULL, 0); +} + +static void mod_sub_list(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_mod_sub_list list = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + list.status = net_buf_simple_pull_u8(buf); + list.elem_addr = net_buf_simple_pull_le16(buf); + if (ctx->recv_op == OP_MOD_SUB_LIST_VND) { + list.cid = net_buf_simple_pull_le16(buf); + } else { + list.cid = BLE_MESH_CID_NVAL; + } + list.mod_id = net_buf_simple_pull_le16(buf); + + list.addr = bt_mesh_alloc_buf(buf->len); + if (!list.addr) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + net_buf_simple_add_mem(list.addr, buf->data, buf->len); + + cfg_client_cancel(model, ctx, &list, sizeof(struct bt_mesh_cfg_mod_sub_list)); +} + +static void net_key_list(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_net_key_list list = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + list.net_idx = bt_mesh_alloc_buf(buf->len); + if (!list.net_idx) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + net_buf_simple_add_mem(list.net_idx, buf->data, buf->len); + + cfg_client_cancel(model, ctx, &list, sizeof(struct bt_mesh_cfg_net_key_list)); +} + +static void app_key_list(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_app_key_list list = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + list.status = net_buf_simple_pull_u8(buf); + list.net_idx = net_buf_simple_pull_le16(buf); + list.app_idx = bt_mesh_alloc_buf(buf->len); + if (!list.app_idx) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + net_buf_simple_add_mem(list.app_idx, buf->data, buf->len); + + cfg_client_cancel(model, ctx, &list, sizeof(struct bt_mesh_cfg_app_key_list)); +} + +static void node_id_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_node_id_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.net_idx = net_buf_simple_pull_le16(buf); + status.identity = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_node_id_status)); +} + +static void mod_app_list(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_mod_app_list list = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + list.status = net_buf_simple_pull_u8(buf); + list.elem_addr = net_buf_simple_pull_le16(buf); + if (ctx->recv_op == OP_VND_MOD_APP_LIST) { + list.cid = net_buf_simple_pull_le16(buf); + } else { + list.cid = BLE_MESH_CID_NVAL; + } + list.mod_id = net_buf_simple_pull_le16(buf); + + list.app_idx = bt_mesh_alloc_buf(buf->len); + if (!list.app_idx) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + net_buf_simple_add_mem(list.app_idx, buf->data, buf->len); + + cfg_client_cancel(model, ctx, &list, sizeof(struct bt_mesh_cfg_mod_app_list)); +} + +static void kr_phase_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_key_refresh_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.status = net_buf_simple_pull_u8(buf); + status.net_idx = net_buf_simple_pull_le16(buf); + status.phase = net_buf_simple_pull_u8(buf); + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_key_refresh_status)); +} + +static void lpn_pollto_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_lpn_pollto_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.lpn_addr = net_buf_simple_pull_le16(buf); + status.timeout = net_buf_simple_pull_u8(buf); + status.timeout |= net_buf_simple_pull_u8(buf) << 8; + status.timeout |= net_buf_simple_pull_u8(buf) << 16; + + cfg_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_cfg_lpn_pollto_status)); +} + +static void net_trans_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + state_status_u8(model, ctx, buf); +} + +const struct bt_mesh_model_op bt_mesh_cfg_cli_op[] = { + { OP_DEV_COMP_DATA_STATUS, 15, comp_data_status }, + { OP_BEACON_STATUS, 1, beacon_status }, + { OP_DEFAULT_TTL_STATUS, 1, ttl_status }, + { OP_FRIEND_STATUS, 1, friend_status }, + { OP_GATT_PROXY_STATUS, 1, gatt_proxy_status }, + { OP_RELAY_STATUS, 2, relay_status }, + { OP_NET_KEY_STATUS, 3, net_key_status }, + { OP_APP_KEY_STATUS, 4, app_key_status }, + { OP_MOD_APP_STATUS, 7, mod_app_status }, + { OP_MOD_PUB_STATUS, 12, mod_pub_status }, + { OP_MOD_SUB_STATUS, 7, mod_sub_status }, + { OP_HEARTBEAT_SUB_STATUS, 9, hb_sub_status }, + { OP_HEARTBEAT_PUB_STATUS, 10, hb_pub_status }, + { OP_NODE_RESET_STATUS, 0, node_reset_status }, + { OP_MOD_SUB_LIST, 5, mod_sub_list }, + { OP_MOD_SUB_LIST_VND, 7, mod_sub_list }, + { OP_NET_KEY_LIST, 2, net_key_list }, + { OP_APP_KEY_LIST, 3, app_key_list }, + { OP_NODE_IDENTITY_STATUS, 4, node_id_status }, + { OP_SIG_MOD_APP_LIST, 5, mod_app_list }, + { OP_VND_MOD_APP_LIST, 7, mod_app_list }, + { OP_KRP_STATUS, 4, kr_phase_status }, + { OP_LPN_TIMEOUT_STATUS, 5, lpn_pollto_status }, + { OP_NET_TRANSMIT_STATUS, 1, net_trans_status }, + BLE_MESH_MODEL_OP_END, +}; + +int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEV_COMP_DATA_GET, 1); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_DEV_COMP_DATA_GET); + net_buf_simple_add_u8(&msg, page); + + err = bt_mesh_client_send_msg(cli->model, OP_DEV_COMP_DATA_GET, ctx, + &msg, timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int get_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 0); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int set_state_u8(struct bt_mesh_msg_ctx *ctx, u32_t op, u8_t new_val) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 1); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_u8(&msg, new_val); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_beacon_get(struct bt_mesh_msg_ctx *ctx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return get_state_u8(ctx, OP_BEACON_GET); +} + +int bt_mesh_cfg_beacon_set(struct bt_mesh_msg_ctx *ctx, u8_t val) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return set_state_u8(ctx, OP_BEACON_SET, val); +} + +int bt_mesh_cfg_ttl_get(struct bt_mesh_msg_ctx *ctx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return get_state_u8(ctx, OP_DEFAULT_TTL_GET); +} + +int bt_mesh_cfg_ttl_set(struct bt_mesh_msg_ctx *ctx, u8_t val) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return set_state_u8(ctx, OP_DEFAULT_TTL_SET, val); +} + +int bt_mesh_cfg_friend_get(struct bt_mesh_msg_ctx *ctx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return get_state_u8(ctx, OP_FRIEND_GET); +} + +int bt_mesh_cfg_friend_set(struct bt_mesh_msg_ctx *ctx, u8_t val) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return set_state_u8(ctx, OP_FRIEND_SET, val); +} + +int bt_mesh_cfg_gatt_proxy_get(struct bt_mesh_msg_ctx *ctx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return get_state_u8(ctx, OP_GATT_PROXY_GET); +} + +int bt_mesh_cfg_gatt_proxy_set(struct bt_mesh_msg_ctx *ctx, u8_t val) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return set_state_u8(ctx, OP_GATT_PROXY_SET, val); +} + +int bt_mesh_cfg_relay_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_RELAY_GET); + + err = bt_mesh_client_send_msg(cli->model, OP_RELAY_GET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_relay_set(struct bt_mesh_msg_ctx *ctx, u8_t new_relay, + u8_t new_transmit) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_SET, 2); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_RELAY_SET); + net_buf_simple_add_u8(&msg, new_relay); + net_buf_simple_add_u8(&msg, new_transmit); + + err = bt_mesh_client_send_msg(cli->model, OP_RELAY_SET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_net_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, + const u8_t net_key[16]) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_ADD, 18); + int err = 0; + + if (!ctx || !ctx->addr || !net_key) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_ADD); + net_buf_simple_add_le16(&msg, key_net_idx); + net_buf_simple_add_mem(&msg, net_key, 16); + + err = bt_mesh_client_send_msg(cli->model, OP_NET_KEY_ADD, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_app_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, + u16_t key_app_idx, const u8_t app_key[16]) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_ADD, 19); + int err = 0; + + if (!ctx || !ctx->addr || !app_key) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_ADD); + key_idx_pack(&msg, key_net_idx, key_app_idx); + net_buf_simple_add_mem(&msg, app_key, 16); + + err = bt_mesh_client_send_msg(cli->model, OP_APP_KEY_ADD, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_app_bind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_app_idx, u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_BIND, 8); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_APP_BIND); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_le16(&msg, mod_app_idx); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_APP_BIND, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int mod_sub(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 8); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_le16(&msg, sub_addr); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_sub_add(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return mod_sub(OP_MOD_SUB_ADD, ctx, elem_addr, sub_addr, mod_id, cid); +} + +int bt_mesh_cfg_mod_sub_del(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return mod_sub(OP_MOD_SUB_DEL, ctx, elem_addr, sub_addr, mod_id, cid); +} + +int bt_mesh_cfg_mod_sub_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return mod_sub(OP_MOD_SUB_OVERWRITE, ctx, elem_addr, sub_addr, mod_id, cid); +} + +static int mod_sub_va(u32_t op, struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 22); + int err = 0; + + BT_DBG("net_idx 0x%04x addr 0x%04x elem_addr 0x%04x label %s", + ctx->net_idx, ctx->addr, elem_addr, label); + BT_DBG("mod_id 0x%04x cid 0x%04x", mod_id, cid); + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_mem(&msg, label, 16); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_sub_va_add(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr || !label) { + return -EINVAL; + } + return mod_sub_va(OP_MOD_SUB_VA_ADD, ctx, elem_addr, label, mod_id, cid); +} + +int bt_mesh_cfg_mod_sub_va_del(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr || !label) { + return -EINVAL; + } + return mod_sub_va(OP_MOD_SUB_VA_DEL, ctx, elem_addr, label, mod_id, cid); +} + +int bt_mesh_cfg_mod_sub_va_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr || !label) { + return -EINVAL; + } + return mod_sub_va(OP_MOD_SUB_VA_OVERWRITE, ctx, elem_addr, label, mod_id, cid); +} + +int bt_mesh_cfg_mod_pub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_GET, 6); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_PUB_GET); + net_buf_simple_add_le16(&msg, elem_addr); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_PUB_GET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_pub_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid, + struct bt_mesh_cfg_mod_pub *pub) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_SET, 13); + int err = 0; + + if (!ctx || !ctx->addr || !pub) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_PUB_SET); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_le16(&msg, pub->addr); + net_buf_simple_add_le16(&msg, (pub->app_idx | (pub->cred_flag << 12))); + net_buf_simple_add_u8(&msg, pub->ttl); + net_buf_simple_add_u8(&msg, pub->period); + net_buf_simple_add_u8(&msg, pub->transmit); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_PUB_SET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, + struct bt_mesh_cfg_hb_sub *sub) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_SET, 5); + int err = 0; + + if (!ctx || !ctx->addr || !sub) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_SET); + net_buf_simple_add_le16(&msg, sub->src); + net_buf_simple_add_le16(&msg, sub->dst); + net_buf_simple_add_u8(&msg, sub->period); + + err = bt_mesh_client_send_msg(cli->model, OP_HEARTBEAT_SUB_SET, ctx, + &msg, timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_hb_sub_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_GET); + + err = bt_mesh_client_send_msg(cli->model, OP_HEARTBEAT_SUB_GET, ctx, + &msg, timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, + const struct bt_mesh_cfg_hb_pub *pub) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_SET, 9); + int err = 0; + + if (!ctx || !ctx->addr || !pub) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_SET); + net_buf_simple_add_le16(&msg, pub->dst); + net_buf_simple_add_u8(&msg, pub->count); + net_buf_simple_add_u8(&msg, pub->period); + net_buf_simple_add_u8(&msg, pub->ttl); + net_buf_simple_add_le16(&msg, pub->feat); + net_buf_simple_add_le16(&msg, pub->net_idx); + + err = bt_mesh_client_send_msg(cli->model, OP_HEARTBEAT_PUB_SET, ctx, + &msg, timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_GET); + + err = bt_mesh_client_send_msg(cli->model, OP_HEARTBEAT_PUB_GET, ctx, + &msg, timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_node_reset(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_NODE_RESET); + + err = bt_mesh_client_send_msg(cli->model, OP_NODE_RESET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_pub_va_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid, const u8_t label[16], + struct bt_mesh_cfg_mod_pub *pub) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_VA_SET, 27); + int err = 0; + + if (!ctx || !ctx->addr || !label || !pub) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_PUB_VA_SET); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_mem(&msg, label, 16); + net_buf_simple_add_le16(&msg, (pub->app_idx | (pub->cred_flag << 12))); + net_buf_simple_add_u8(&msg, pub->ttl); + net_buf_simple_add_u8(&msg, pub->period); + net_buf_simple_add_u8(&msg, pub->transmit); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_PUB_VA_SET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_sub_del_all(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_DEL_ALL, 6); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_SUB_DEL_ALL); + net_buf_simple_add_le16(&msg, elem_addr); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_SUB_DEL_ALL, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int mod_sub_get(u32_t op, struct bt_mesh_msg_ctx *ctx, + u16_t elem_addr, u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, elem_addr); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_sub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return mod_sub_get(OP_MOD_SUB_GET, ctx, elem_addr, mod_id, BLE_MESH_CID_NVAL); +} + +int bt_mesh_cfg_mod_sub_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr || cid == BLE_MESH_CID_NVAL) { + return -EINVAL; + } + return mod_sub_get(OP_MOD_SUB_GET_VND, ctx, elem_addr, mod_id, cid); +} + +int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, + const u8_t net_key[16]) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_UPDATE, 18); + int err = 0; + + if (!ctx || !ctx->addr || !net_key) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_UPDATE); + net_buf_simple_add_le16(&msg, net_idx); + net_buf_simple_add_mem(&msg, net_key, 16); + + err = bt_mesh_client_send_msg(cli->model, OP_NET_KEY_UPDATE, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_DEL, 2); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_DEL); + net_buf_simple_add_le16(&msg, net_idx); + + err = bt_mesh_client_send_msg(cli->model, OP_NET_KEY_DEL, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_net_key_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_GET); + + err = bt_mesh_client_send_msg(cli->model, OP_NET_KEY_GET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, + u16_t app_idx, const u8_t app_key[16]) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_UPDATE, 19); + int err = 0; + + if (!ctx || !ctx->addr || !app_key) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_UPDATE); + key_idx_pack(&msg, net_idx, app_idx); + net_buf_simple_add_mem(&msg, app_key, 16); + + err = bt_mesh_client_send_msg(cli->model, OP_APP_KEY_UPDATE, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_DEL, 3); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_DEL); + key_idx_pack(&msg, net_idx, app_idx); + + err = bt_mesh_client_send_msg(cli->model, OP_APP_KEY_DEL, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_app_key_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_GET, 2); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_GET); + net_buf_simple_add_le16(&msg, net_idx); + + err = bt_mesh_client_send_msg(cli->model, OP_APP_KEY_GET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int node_identity_op(u32_t op, struct bt_mesh_msg_ctx *ctx, + u16_t net_idx, u8_t identity) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, net_idx); + if (op == OP_NODE_IDENTITY_SET) { + net_buf_simple_add_u8(&msg, identity); + } + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_node_identity_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return node_identity_op(OP_NODE_IDENTITY_GET, ctx, net_idx, 0xFF); +} + +int bt_mesh_cfg_node_identity_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t identity) +{ + if (!ctx || !ctx->addr || identity > 0x01) { + return -EINVAL; + } + return node_identity_op(OP_NODE_IDENTITY_SET, ctx, net_idx, identity); +} + +int bt_mesh_cfg_mod_app_unbind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t app_idx, u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_UNBIND, 8); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_MOD_APP_UNBIND); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_le16(&msg, app_idx); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, OP_MOD_APP_UNBIND, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +static int mod_app_get(u32_t op, struct bt_mesh_msg_ctx *ctx, + u16_t elem_addr, u16_t mod_id, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 6); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, elem_addr); + if (cid != BLE_MESH_CID_NVAL) { + net_buf_simple_add_le16(&msg, cid); + } + net_buf_simple_add_le16(&msg, mod_id); + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_mod_app_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return mod_app_get(OP_SIG_MOD_APP_GET, ctx, elem_addr, mod_id, BLE_MESH_CID_NVAL); +} + +int bt_mesh_cfg_mod_app_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid) +{ + if (!ctx || !ctx->addr || cid == BLE_MESH_CID_NVAL) { + return -EINVAL; + } + return mod_app_get(OP_VND_MOD_APP_GET, ctx, elem_addr, mod_id, cid); +} + +static int kr_phase_op(u32_t op, struct bt_mesh_msg_ctx *ctx, + u16_t net_idx, u8_t transition) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, DUMMY_2_BYTE_OP, 3); + int err = 0; + + bt_mesh_model_msg_init(&msg, op); + net_buf_simple_add_le16(&msg, net_idx); + if (op == OP_KRP_SET) { + net_buf_simple_add_u8(&msg, transition); + } + + err = bt_mesh_client_send_msg(cli->model, op, ctx, &msg, timeout_handler, + config_msg_timeout, true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_kr_phase_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return kr_phase_op(OP_KRP_GET, ctx, net_idx, 0xFF); +} + +int bt_mesh_cfg_kr_phase_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t transition) +{ + if (!ctx || !ctx->addr || transition > 0x03) { + return -EINVAL; + } + return kr_phase_op(OP_KRP_SET, ctx, net_idx, transition);; +} + +int bt_mesh_cfg_lpn_timeout_get(struct bt_mesh_msg_ctx *ctx, u16_t lpn_addr) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_GET, 2); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_LPN_TIMEOUT_GET); + net_buf_simple_add_le16(&msg, lpn_addr); + + err = bt_mesh_client_send_msg(cli->model, OP_LPN_TIMEOUT_GET, ctx, &msg, + timeout_handler, config_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_cfg_net_transmit_get(struct bt_mesh_msg_ctx *ctx) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return get_state_u8(ctx, OP_NET_TRANSMIT_GET); +} + +int bt_mesh_cfg_net_transmit_set(struct bt_mesh_msg_ctx *ctx, u8_t transmit) +{ + if (!ctx || !ctx->addr) { + return -EINVAL; + } + return set_state_u8(ctx, OP_NET_TRANSMIT_SET, transmit); +} + +s32_t bt_mesh_cfg_cli_timeout_get(void) +{ + return config_msg_timeout; +} + +void bt_mesh_cfg_cli_timeout_set(s32_t timeout) +{ + config_msg_timeout = timeout; +} + +int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary) +{ + config_internal_data_t *internal = NULL; + bt_mesh_config_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!primary) { + BT_ERR("Configuration Client only allowed in primary element"); + return -EINVAL; + } + + if (!model) { + BT_ERR("Configuration Client model is NULL"); + return -EINVAL; + } + + client = (bt_mesh_config_client_t *)model->user_data; + if (!client) { + BT_ERR("No Configuration Client context provided"); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(config_internal_data_t)); + if (!internal) { + BT_ERR("Allocate memory for Configuration Client internal data fail"); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(cfg_op_pair); + client->op_pair = cfg_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + cli = client; + + /* Configuration Model security is device-key based */ + model->keys[0] = BLE_MESH_KEY_DEV; + + bt_mesh_cfg_client_mutex_new(); + + return 0; +} + +int bt_mesh_cfg_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_config_client_t *client = NULL; + + if (!primary) { + BT_ERR("Configuration Client only allowed in primary element"); + return -EINVAL; + } + + if (!model) { + BT_ERR("Configuration Client model is NULL"); + return -EINVAL; + } + + client = (bt_mesh_config_client_t *)model->user_data; + if (!client) { + BT_ERR("No Configuration Client context provided"); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + cli->internal_data = NULL; + } + + client = NULL; + + bt_mesh_cfg_client_mutex_free(); + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c new file mode 100644 index 000000000..46b4a524e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/cfg_srv.c @@ -0,0 +1,3629 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) + +#include "btc_ble_mesh_config_model.h" + +#include "mesh.h" +#include "adv.h" +#include "lpn.h" +#include "transport.h" +#include "crypto.h" +#include "access.h" +#include "beacon.h" +#include "foundation.h" +#include "friend.h" +#include "settings.h" +#include "cfg_srv.h" +#include "proxy_server.h" +#include "mesh_main.h" +#include "mesh_common.h" + +#define DEFAULT_TTL 7 + +/* Maximum message length is 384 in BLE Mesh. Here for composition data, + * due to 1 octet opcode and 4 octets TransMIC, 379 octets can be used to + * store device composition data. + */ +#define COMP_DATA_MAX_LEN 379 + +static struct bt_mesh_cfg_srv *conf; + +static struct label labels[CONFIG_BLE_MESH_LABEL_COUNT]; + +static int comp_add_elem(struct net_buf_simple *buf, struct bt_mesh_elem *elem, + bool primary) +{ + struct bt_mesh_model *mod = NULL; + int i; + + if (net_buf_simple_tailroom(buf) < + 4 + (elem->model_count * 2U) + (elem->vnd_model_count * 4U)) { + BT_ERR("%s, Too large device composition", __func__); + return -E2BIG; + } + + net_buf_simple_add_le16(buf, elem->loc); + + net_buf_simple_add_u8(buf, elem->model_count); + net_buf_simple_add_u8(buf, elem->vnd_model_count); + + for (i = 0; i < elem->model_count; i++) { + mod = &elem->models[i]; + net_buf_simple_add_le16(buf, mod->id); + } + + for (i = 0; i < elem->vnd_model_count; i++) { + mod = &elem->vnd_models[i]; + net_buf_simple_add_le16(buf, mod->vnd.company); + net_buf_simple_add_le16(buf, mod->vnd.id); + } + + return 0; +} + +static int comp_get_page_0(struct net_buf_simple *buf) +{ + u16_t feat = 0U; + const struct bt_mesh_comp *comp = NULL; + int i; + + comp = bt_mesh_comp_get(); + + if (IS_ENABLED(CONFIG_BLE_MESH_RELAY)) { + feat |= BLE_MESH_FEAT_RELAY; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + feat |= BLE_MESH_FEAT_PROXY; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + feat |= BLE_MESH_FEAT_FRIEND; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + feat |= BLE_MESH_FEAT_LOW_POWER; + } + + net_buf_simple_add_le16(buf, comp->cid); + net_buf_simple_add_le16(buf, comp->pid); + net_buf_simple_add_le16(buf, comp->vid); + net_buf_simple_add_le16(buf, CONFIG_BLE_MESH_CRPL); + net_buf_simple_add_le16(buf, feat); + + for (i = 0; i < comp->elem_count; i++) { + int err; + + err = comp_add_elem(buf, &comp->elem[i], i == 0); + if (err) { + return err; + } + } + + return 0; +} + +static void dev_comp_data_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct net_buf_simple *sdu = NULL; + u8_t page = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + page = net_buf_simple_pull_u8(buf); + if (page != 0U) { + BT_WARN("Composition page %u not available", page); + page = 0U; + } + + sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, COMP_DATA_MAX_LEN)); + if (!sdu) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + bt_mesh_model_msg_init(sdu, OP_DEV_COMP_DATA_STATUS); + + net_buf_simple_add_u8(sdu, page); + if (comp_get_page_0(sdu) < 0) { + BT_ERR("%s, Unable to get composition page 0", __func__); + bt_mesh_free_buf(sdu); + return; + } + + if (bt_mesh_model_send(model, ctx, sdu, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Composition Data Status", __func__); + } + + bt_mesh_free_buf(sdu); + return; +} + +static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem, + struct net_buf_simple *buf, bool *vnd) +{ + if (buf->len < 4) { + u16_t id = 0U; + + id = net_buf_simple_pull_le16(buf); + + BT_DBG("ID 0x%04x addr 0x%04x", id, elem->addr); + + *vnd = false; + + return bt_mesh_model_find(elem, id); + } else { + u16_t company = 0U, id = 0U; + + company = net_buf_simple_pull_le16(buf); + id = net_buf_simple_pull_le16(buf); + + BT_DBG("Company 0x%04x ID 0x%04x addr 0x%04x", company, id, + elem->addr); + + *vnd = true; + + return bt_mesh_model_find_vnd(elem, company, id); + } +} + +static bool app_key_is_valid(u16_t app_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return true; + } + } + + return false; +} + +static u8_t _mod_pub_set(struct bt_mesh_model *model, u16_t pub_addr, + u16_t app_idx, u8_t cred_flag, u8_t ttl, u8_t period, + u8_t retransmit, bool store) +{ + if (!model->pub) { + return STATUS_NVAL_PUB_PARAM; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && cred_flag) { + return STATUS_FEAT_NOT_SUPP; + } + + if (!model->pub->update && period) { + return STATUS_NVAL_PUB_PARAM; + } + + if (pub_addr == BLE_MESH_ADDR_UNASSIGNED) { + if (model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + return STATUS_SUCCESS; + } + + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->key = 0U; + model->pub->cred = 0U; + model->pub->ttl = 0U; + model->pub->period = 0U; + model->pub->retransmit = 0U; + model->pub->count = 0U; + + if (model->pub->update) { + k_delayed_work_cancel(&model->pub->timer); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_mod_pub(model); + } + + return STATUS_SUCCESS; + } + + if (!bt_mesh_app_key_find(app_idx)) { + return STATUS_INVALID_APPKEY; + } + + model->pub->addr = pub_addr; + model->pub->key = app_idx; + model->pub->cred = cred_flag; + model->pub->ttl = ttl; + model->pub->period = period; + model->pub->retransmit = retransmit; + + if (model->pub->update) { + s32_t period_ms; + + period_ms = bt_mesh_model_pub_period_get(model); + BT_DBG("period %u ms", period_ms); + + if (period_ms) { + k_delayed_work_submit(&model->pub->timer, period_ms); + } else { + k_delayed_work_cancel(&model->pub->timer); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_mod_pub(model); + } + + return STATUS_SUCCESS; +} + +static u8_t mod_bind(struct bt_mesh_model *model, u16_t key_idx) +{ + int i; + + BT_DBG("model %p key_idx 0x%03x", model, key_idx); + + if (!app_key_is_valid(key_idx)) { + return STATUS_INVALID_APPKEY; + } + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + /* Treat existing binding as success */ + if (model->keys[i] == key_idx) { + return STATUS_SUCCESS; + } + } + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] == BLE_MESH_KEY_UNUSED) { + model->keys[i] = key_idx; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_bind(model); + } + + return STATUS_SUCCESS; + } + } + + return STATUS_INSUFF_RESOURCES; +} + +static u8_t mod_unbind(struct bt_mesh_model *model, u16_t key_idx, bool store) +{ + int i; + + BT_DBG("model %p key_idx 0x%03x store %u", model, key_idx, store); + + if (!app_key_is_valid(key_idx)) { + return STATUS_INVALID_APPKEY; + } + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] != key_idx) { + continue; + } + + model->keys[i] = BLE_MESH_KEY_UNUSED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_mod_bind(model); + } + + if (model->pub && model->pub->key == key_idx) { + _mod_pub_set(model, BLE_MESH_ADDR_UNASSIGNED, + 0, 0, 0, 0, 0, store); + } + } + + return STATUS_SUCCESS; +} + +struct bt_mesh_app_key *bt_mesh_app_key_alloc(u16_t app_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx == BLE_MESH_KEY_UNUSED) { + return key; + } + } + + return NULL; +} + +static u8_t app_key_set(u16_t net_idx, u16_t app_idx, const u8_t val[16], + bool update) +{ + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + struct bt_mesh_subnet *sub = NULL; + + BT_DBG("net_idx 0x%04x app_idx %04x update %u val %s", + net_idx, app_idx, update, bt_hex(val, 16)); + + sub = bt_mesh_subnet_get(net_idx); + if (!sub) { + return STATUS_INVALID_NETKEY; + } + + key = bt_mesh_app_key_find(app_idx); + if (update) { + if (!key) { + return STATUS_INVALID_APPKEY; + } + + if (key->net_idx != net_idx) { + return STATUS_INVALID_BINDING; + } + + keys = &key->keys[1]; + + /* The AppKey Update message shall generate an error when node + * is in normal operation, Phase 2, or Phase 3 or in Phase 1 + * when the AppKey Update message on a valid AppKeyIndex when + * the AppKey value is different. + */ + if (sub->kr_phase != BLE_MESH_KR_PHASE_1) { + return STATUS_CANNOT_UPDATE; + } + + if (key->updated) { + if (memcmp(keys->val, val, 16)) { + return STATUS_CANNOT_UPDATE; + } else { + return STATUS_SUCCESS; + } + } + + key->updated = true; + } else { + if (key) { + if (key->net_idx == net_idx && + !memcmp(key->keys[0].val, val, 16)) { + return STATUS_SUCCESS; + } + + if (key->net_idx == net_idx) { + return STATUS_IDX_ALREADY_STORED; + } else { + return STATUS_INVALID_NETKEY; + } + } + + key = bt_mesh_app_key_alloc(app_idx); + if (!key) { + return STATUS_INSUFF_RESOURCES; + } + + keys = &key->keys[0]; + } + + if (bt_mesh_app_id(val, &keys->id)) { + if (update) { + key->updated = false; + } + + return STATUS_STORAGE_FAIL; + } + + BT_DBG("app_idx 0x%04x AID 0x%02x", app_idx, keys->id); + + key->net_idx = net_idx; + key->app_idx = app_idx; + memcpy(keys->val, val, 16); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Storing AppKey persistently"); + bt_mesh_store_app_key(key); + } + + return STATUS_SUCCESS; +} + +static void app_key_add(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); + u16_t key_net_idx = 0U, key_app_idx = 0U; + u8_t status = 0U; + + key_idx_unpack(buf, &key_net_idx, &key_app_idx); + + BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx); + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS); + + status = app_key_set(key_net_idx, key_app_idx, buf->data, false); + BT_DBG("status 0x%02x", status); + net_buf_simple_add_u8(&msg, status); + + key_idx_pack(&msg, key_net_idx, key_app_idx); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config AppKey Status", __func__); + return; + } + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_appkey_add.net_idx = key_net_idx; + change.cfg_appkey_add.app_idx = key_app_idx; + memcpy(change.cfg_appkey_add.app_key, buf->data, 16); + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +static void app_key_update(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); + u16_t key_net_idx = 0U, key_app_idx = 0U; + u8_t status = 0U; + + key_idx_unpack(buf, &key_net_idx, &key_app_idx); + + BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx); + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS); + + status = app_key_set(key_net_idx, key_app_idx, buf->data, true); + BT_DBG("status 0x%02x", status); + net_buf_simple_add_u8(&msg, status); + + key_idx_pack(&msg, key_net_idx, key_app_idx); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config AppKey Status", __func__); + } + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_appkey_update.net_idx = key_net_idx; + change.cfg_appkey_update.app_idx = key_app_idx; + memcpy(change.cfg_appkey_update.app_key, buf->data, 16); + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +struct unbind_data { + u16_t app_idx; + bool store; +}; + +static void _mod_unbind(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + struct unbind_data *data = user_data; + + mod_unbind(mod, data->app_idx, data->store); +} + +void bt_mesh_app_key_del(struct bt_mesh_app_key *key, bool store) +{ + struct unbind_data data = { .app_idx = key->app_idx, .store = store }; + + BT_DBG("AppIdx 0x%03x store %u", key->app_idx, store); + + bt_mesh_model_foreach(_mod_unbind, &data); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_clear_app_key(key); + } + + key->net_idx = BLE_MESH_KEY_UNUSED; + (void)memset(key->keys, 0, sizeof(key->keys)); +} + +static void app_key_del(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4); + u16_t key_net_idx = 0U, key_app_idx = 0U; + struct bt_mesh_app_key *key = NULL; + u8_t status = 0U; + + key_idx_unpack(buf, &key_net_idx, &key_app_idx); + + BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx); + + if (!bt_mesh_subnet_get(key_net_idx)) { + status = STATUS_INVALID_NETKEY; + goto send_status; + } + + key = bt_mesh_app_key_find(key_app_idx); + if (!key) { + /* Treat as success since the client might have missed a + * previous response and is resending the request. + */ + status = STATUS_SUCCESS; + goto send_status; + } + + if (key->net_idx != key_net_idx) { + status = STATUS_INVALID_BINDING; + goto send_status; + } + + bt_mesh_app_key_del(key, true); + status = STATUS_SUCCESS; + +send_status: + bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS); + + net_buf_simple_add_u8(&msg, status); + + key_idx_pack(&msg, key_net_idx, key_app_idx); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config AppKey Status", __func__); + } + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_appkey_delete.net_idx = key_net_idx; + change.cfg_appkey_delete.app_idx = key_app_idx; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +/* Index list length: 3 bytes for every pair and 2 bytes for an odd idx */ +#define IDX_LEN(num) (((num) / 2) * 3 + ((num) % 2) * 2) + +static void app_key_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_LIST, + 3 + IDX_LEN(CONFIG_BLE_MESH_APP_KEY_COUNT)); + u16_t get_idx = 0U, i = 0U, prev = 0U; + u8_t status = 0U; + + get_idx = net_buf_simple_pull_le16(buf); + if (get_idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, get_idx); + return; + } + + BT_DBG("idx 0x%04x", get_idx); + + bt_mesh_model_msg_init(&msg, OP_APP_KEY_LIST); + + if (!bt_mesh_subnet_get(get_idx)) { + status = STATUS_INVALID_NETKEY; + } else { + status = STATUS_SUCCESS; + } + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, get_idx); + + if (status != STATUS_SUCCESS) { + goto send_status; + } + + prev = BLE_MESH_KEY_UNUSED; + for (i = 0U; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx != get_idx) { + continue; + } + + if (prev == BLE_MESH_KEY_UNUSED) { + prev = key->app_idx; + continue; + } + + key_idx_pack(&msg, prev, key->app_idx); + prev = BLE_MESH_KEY_UNUSED; + } + + if (prev != BLE_MESH_KEY_UNUSED) { + net_buf_simple_add_le16(&msg, prev); + } + +send_status: + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config AppKey List", __func__); + } +} + +static void beacon_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_beacon_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Beacon Status", __func__); + } +} + +static void beacon_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + } else if (buf->data[0] == 0x00 || buf->data[0] == 0x01) { + if (buf->data[0] != cfg->beacon) { + cfg->beacon = buf->data[0]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + + if (cfg->beacon) { + bt_mesh_beacon_enable(); + } else { + bt_mesh_beacon_disable(); + } + } + } else { + BT_WARN("Invalid Config Beacon value 0x%02x", buf->data[0]); + return; + } + + bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_beacon_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Beacon Status", __func__); + } +} + +static void default_ttl_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_default_ttl_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Default TTL Status", __func__); + } +} + +static void default_ttl_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + } else if (buf->data[0] <= BLE_MESH_TTL_MAX && buf->data[0] != 0x01) { + if (cfg->default_ttl != buf->data[0]) { + cfg->default_ttl = buf->data[0]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + } + } else { + BT_WARN("Prohibited Default TTL value 0x%02x", buf->data[0]); + return; + } + + bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_default_ttl_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Default TTL Status", __func__); + } +} + +static void send_gatt_proxy_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_GATT_PROXY_STATUS, 1); + + bt_mesh_model_msg_init(&msg, OP_GATT_PROXY_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_gatt_proxy_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config GATT Proxy Status", __func__); + } +} + +static void gatt_proxy_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + send_gatt_proxy_status(model, ctx); +} + +static void gatt_proxy_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + if (buf->data[0] != 0x00 && buf->data[0] != 0x01) { + BT_WARN("Invalid GATT Proxy value 0x%02x", buf->data[0]); + return; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) || + bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { + goto send_status; + } + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + goto send_status; + } + + BT_DBG("GATT Proxy 0x%02x -> 0x%02x", cfg->gatt_proxy, buf->data[0]); + + if (cfg->gatt_proxy == buf->data[0]) { + goto send_status; + } + + cfg->gatt_proxy = buf->data[0]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + + if (cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) { + bt_mesh_heartbeat_send(); + } + +send_status: + send_gatt_proxy_status(model, ctx); +} + +static void net_transmit_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_net_transmit_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Network Transmit Status", __func__); + } +} + +static void net_transmit_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + BT_DBG("Transmit 0x%02x (count %u interval %ums)", buf->data[0], + BLE_MESH_TRANSMIT_COUNT(buf->data[0]), + BLE_MESH_TRANSMIT_INT(buf->data[0])); + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + } else { + cfg->net_transmit = buf->data[0]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + } + + bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_net_transmit_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Network Transmit Status", __func__); + } +} + +static void relay_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_relay_get()); + net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Relay Status", __func__); + } +} + +static void relay_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + } else if (buf->data[0] == 0x00 || buf->data[0] == 0x01) { + bool change; + + if (cfg->relay == BLE_MESH_RELAY_NOT_SUPPORTED) { + change = false; + } else { + change = (cfg->relay != buf->data[0]); + cfg->relay = buf->data[0]; + cfg->relay_retransmit = buf->data[1]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + } + + BT_DBG("Relay 0x%02x (%s) xmit 0x%02x (count %u interval %u)", + cfg->relay, change ? "changed" : "not changed", + cfg->relay_retransmit, + BLE_MESH_TRANSMIT_COUNT(cfg->relay_retransmit), + BLE_MESH_TRANSMIT_INT(cfg->relay_retransmit)); + + if ((cfg->hb_pub.feat & BLE_MESH_FEAT_RELAY) && change) { + bt_mesh_heartbeat_send(); + } + } else { + BT_WARN("Invalid Relay value 0x%02x", buf->data[0]); + return; + } + + bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS); + net_buf_simple_add_u8(&msg, bt_mesh_relay_get()); + net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get()); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Relay Status", __func__); + } +} + +static void send_mod_pub_status(struct bt_mesh_model *cfg_mod, + struct bt_mesh_msg_ctx *ctx, + u16_t elem_addr, u16_t pub_addr, + bool vnd, struct bt_mesh_model *mod, + u8_t status, u8_t *mod_id) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_STATUS, 14); + + bt_mesh_model_msg_init(&msg, OP_MOD_PUB_STATUS); + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, elem_addr); + + if (status != STATUS_SUCCESS) { + (void)memset(net_buf_simple_add(&msg, 7), 0, 7); + } else { + u16_t idx_cred; + + net_buf_simple_add_le16(&msg, pub_addr); + + idx_cred = mod->pub->key | (u16_t)mod->pub->cred << 12; + net_buf_simple_add_le16(&msg, idx_cred); + net_buf_simple_add_u8(&msg, mod->pub->ttl); + net_buf_simple_add_u8(&msg, mod->pub->period); + net_buf_simple_add_u8(&msg, mod->pub->retransmit); + } + + if (vnd) { + memcpy(net_buf_simple_add(&msg, 4), mod_id, 4); + } else { + memcpy(net_buf_simple_add(&msg, 2), mod_id, 2); + } + + if (bt_mesh_model_send(cfg_mod, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model Publication Status", __func__); + } +} + +static void mod_pub_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, pub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + mod_id = buf->data; + + BT_DBG("elem_addr 0x%04x", elem_addr); + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (!mod->pub) { + status = STATUS_NVAL_PUB_PARAM; + goto send_status; + } + + pub_addr = mod->pub->addr; + status = STATUS_SUCCESS; + +send_status: + send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod, + status, mod_id); +} + +static void mod_pub_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U; + u16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + pub_addr = net_buf_simple_pull_le16(buf); + pub_app_idx = net_buf_simple_pull_le16(buf); + cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1)); + pub_app_idx &= BIT_MASK(12); + + pub_ttl = net_buf_simple_pull_u8(buf); + if (pub_ttl > BLE_MESH_TTL_MAX && pub_ttl != BLE_MESH_TTL_DEFAULT) { + BT_ERR("%s, Invalid TTL value 0x%02x", __func__, pub_ttl); + return; + } + + pub_period = net_buf_simple_pull_u8(buf); + retransmit = net_buf_simple_pull_u8(buf); + mod_id = buf->data; + + BT_DBG("elem_addr 0x%04x pub_addr 0x%04x cred_flag %u", + elem_addr, pub_addr, cred_flag); + BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", + pub_app_idx, pub_ttl, pub_period); + BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit, + BLE_MESH_PUB_TRANSMIT_COUNT(retransmit), + BLE_MESH_PUB_TRANSMIT_INT(retransmit)); + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = _mod_pub_set(mod, pub_addr, pub_app_idx, cred_flag, pub_ttl, + pub_period, retransmit, true); + +send_status: + send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod, + status, mod_id); + + if (status == STATUS_SUCCESS && mod->pub) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_mod_pub_set.elem_addr = elem_addr; + change.cfg_mod_pub_set.pub_addr = mod->pub->addr; + change.cfg_mod_pub_set.app_idx = mod->pub->key; + change.cfg_mod_pub_set.cred_flag = mod->pub->cred; + change.cfg_mod_pub_set.ttl = mod->pub->ttl; + change.cfg_mod_pub_set.period = mod->pub->period; + change.cfg_mod_pub_set.transmit = mod->pub->retransmit; + change.cfg_mod_pub_set.cid = vnd ? mod->vnd.company : 0xFFFF; + change.cfg_mod_pub_set.mod_id = vnd ? mod->vnd.id : mod->id; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +struct label *get_label(u16_t index) +{ + if (index >= ARRAY_SIZE(labels)) { + return NULL; + } + + return &labels[index]; +} + +#if CONFIG_BLE_MESH_LABEL_COUNT > 0 +static inline void va_store(struct label *store) +{ + bt_mesh_atomic_set_bit(store->flags, BLE_MESH_VA_CHANGED); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_label(); + } +} + +static struct label *va_find(const u8_t *label_uuid, + struct label **free_slot) +{ + struct label *match = NULL; + int i; + + if (free_slot != NULL) { + *free_slot = NULL; + } + + for (i = 0; i < ARRAY_SIZE(labels); i++) { + if (labels[i].ref == 0) { + if (free_slot != NULL) { + *free_slot = &labels[i]; + } + continue; + } + + if (!memcmp(labels[i].uuid, label_uuid, 16)) { + match = &labels[i]; + } + } + + return match; +} + +static u8_t va_add(u8_t *label_uuid, u16_t *addr) +{ + struct label *update = NULL, *free_slot = NULL; + + update = va_find(label_uuid, &free_slot); + if (update) { + update->ref++; + va_store(update); + return STATUS_SUCCESS; + } + + if (!free_slot) { + return STATUS_INSUFF_RESOURCES; + } + + if (bt_mesh_virtual_addr(label_uuid, addr) < 0) { + return STATUS_UNSPECIFIED; + } + + free_slot->ref = 1U; + free_slot->addr = *addr; + memcpy(free_slot->uuid, label_uuid, 16); + va_store(free_slot); + + return STATUS_SUCCESS; +} + +static u8_t va_del(u8_t *label_uuid, u16_t *addr) +{ + struct label *update = NULL; + + update = va_find(label_uuid, NULL); + if (update) { + update->ref--; + + if (addr) { + *addr = update->addr; + } + + va_store(update); + return STATUS_SUCCESS; + } + + if (addr) { + *addr = BLE_MESH_ADDR_UNASSIGNED; + } + + return STATUS_CANNOT_REMOVE; +} + +static size_t mod_sub_list_clear(struct bt_mesh_model *mod) +{ + u8_t *label_uuid = NULL; + size_t clear_count = 0U; + int i; + + /* Unref stored labels related to this model */ + for (i = 0, clear_count = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (!BLE_MESH_ADDR_IS_VIRTUAL(mod->groups[i])) { + if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) { + mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + clear_count++; + } + + continue; + } + + label_uuid = bt_mesh_label_uuid_get(mod->groups[i]); + + mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + clear_count++; + + if (label_uuid) { + va_del(label_uuid, NULL); + } else { + BT_ERR("%s, Label UUID not found", __func__); + } + } + + return clear_count; +} + +static void mod_pub_va_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U; + u16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + label_uuid = net_buf_simple_pull_mem(buf, 16); + pub_app_idx = net_buf_simple_pull_le16(buf); + cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1)); + pub_app_idx &= BIT_MASK(12); + pub_ttl = net_buf_simple_pull_u8(buf); + if (pub_ttl > BLE_MESH_TTL_MAX && pub_ttl != BLE_MESH_TTL_DEFAULT) { + BT_ERR("%s, Invalid TTL value 0x%02x", __func__, pub_ttl); + return; + } + + pub_period = net_buf_simple_pull_u8(buf); + retransmit = net_buf_simple_pull_u8(buf); + mod_id = buf->data; + + BT_DBG("elem_addr 0x%04x cred_flag %u", elem_addr, cred_flag); + BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x", + pub_app_idx, pub_ttl, pub_period); + BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit, + BLE_MESH_PUB_TRANSMIT_COUNT(retransmit), + BLE_MESH_PUB_TRANSMIT_INT(retransmit)); + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + pub_addr = 0U; + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + pub_addr = 0U; + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = va_add(label_uuid, &pub_addr); + if (status == STATUS_SUCCESS) { + status = _mod_pub_set(mod, pub_addr, pub_app_idx, cred_flag, + pub_ttl, pub_period, retransmit, true); + } + +send_status: + send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod, + status, mod_id); +} +#else +static size_t mod_sub_list_clear(struct bt_mesh_model *mod) +{ + size_t clear_count = 0U; + int i; + + /* Unref stored labels related to this model */ + for (i = 0, clear_count = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) { + mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + clear_count++; + } + } + + return clear_count; +} + +static void mod_pub_va_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t *mod_id = NULL, status = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U, pub_addr = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + net_buf_simple_pull(buf, 16); + mod_id = net_buf_simple_pull(buf, 4); + + BT_DBG("elem_addr 0x%04x", elem_addr); + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (!mod->pub) { + status = STATUS_NVAL_PUB_PARAM; + goto send_status; + } + + pub_addr = mod->pub->addr; + status = STATUS_INSUFF_RESOURCES; + +send_status: + send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod, + status, mod_id); +} +#endif /* CONFIG_BLE_MESH_LABEL_COUNT > 0 */ + +static void send_mod_sub_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, u8_t status, + u16_t elem_addr, u16_t sub_addr, u8_t *mod_id, + bool vnd) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_STATUS, 9); + + BT_DBG("status 0x%02x elem_addr 0x%04x sub_addr 0x%04x", status, + elem_addr, sub_addr); + + bt_mesh_model_msg_init(&msg, OP_MOD_SUB_STATUS); + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, elem_addr); + net_buf_simple_add_le16(&msg, sub_addr); + + if (vnd) { + memcpy(net_buf_simple_add(&msg, 4), mod_id, 4); + } else { + memcpy(net_buf_simple_add(&msg, 2), mod_id, 2); + } + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model Subscription Status", __func__); + } +} + +static void mod_sub_add(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + int i; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + sub_addr = net_buf_simple_pull_le16(buf); + + BT_DBG("elem_addr 0x%04x, sub_addr 0x%04x", elem_addr, sub_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) { + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + if (bt_mesh_model_find_group(mod, sub_addr)) { + /* Tried to add existing subscription */ + BT_DBG("found existing subscription"); + status = STATUS_SUCCESS; + goto send_status; + } + + for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { + mod->groups[i] = sub_addr; + break; + } + } + + if (i == ARRAY_SIZE(mod->groups)) { + status = STATUS_INSUFF_RESOURCES; + } else { + status = STATUS_SUCCESS; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_add(sub_addr); + } + } + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_mod_sub_add.elem_addr = elem_addr; + change.cfg_mod_sub_add.sub_addr = sub_addr; + change.cfg_mod_sub_add.cid = vnd ? mod->vnd.company : 0xFFFF; + change.cfg_mod_sub_add.mod_id = vnd ? mod->vnd.id : mod->id; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +static void mod_sub_del(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u16_t *match = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + sub_addr = net_buf_simple_pull_le16(buf); + + BT_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) { + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + /* An attempt to remove a non-existing address shall be treated + * as a success. + */ + status = STATUS_SUCCESS; + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(&sub_addr, 1); + } + + match = bt_mesh_model_find_group(mod, sub_addr); + if (match) { + *match = BLE_MESH_ADDR_UNASSIGNED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + } + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_mod_sub_delete.elem_addr = elem_addr; + change.cfg_mod_sub_delete.sub_addr = sub_addr; + change.cfg_mod_sub_delete.cid = vnd ? mod->vnd.company : 0xFFFF; + change.cfg_mod_sub_delete.mod_id = vnd ? mod->vnd.id : mod->id; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +static void mod_sub_overwrite(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + sub_addr = net_buf_simple_pull_le16(buf); + + BT_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) { + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups)); + } + + mod_sub_list_clear(mod); + + if (ARRAY_SIZE(mod->groups) > 0) { + mod->groups[0] = sub_addr; + status = STATUS_SUCCESS; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_add(sub_addr); + } + } else { + status = STATUS_INSUFF_RESOURCES; + } + + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); +} + +static void mod_sub_del_all(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + BT_DBG("elem_addr 0x%04x", elem_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups)); + } + + mod_sub_list_clear(mod); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + status = STATUS_SUCCESS; + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, + BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd); +} + +static void mod_sub_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST, + 5 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t addr = 0U, id = 0U; + int i; + + addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, addr); + return; + } + + id = net_buf_simple_pull_le16(buf); + + BT_DBG("addr 0x%04x id 0x%04x", addr, id); + + bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST); + + elem = bt_mesh_elem_find(addr); + if (!elem) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS); + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, id); + goto send_list; + } + + mod = bt_mesh_model_find(elem, id); + if (!mod) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL); + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, id); + goto send_list; + } + + net_buf_simple_add_u8(&msg, STATUS_SUCCESS); + + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, id); + + for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) { + net_buf_simple_add_le16(&msg, mod->groups[i]); + } + } + +send_list: + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model Subscription List", __func__); + } +} + +static void mod_sub_get_vnd(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST_VND, + 7 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2); + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t company = 0U, addr = 0U, id = 0U; + int i; + + addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, addr); + return; + } + + company = net_buf_simple_pull_le16(buf); + id = net_buf_simple_pull_le16(buf); + + BT_DBG("addr 0x%04x company 0x%04x id 0x%04x", addr, company, id); + + bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST_VND); + + elem = bt_mesh_elem_find(addr); + if (!elem) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS); + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, company); + net_buf_simple_add_le16(&msg, id); + goto send_list; + } + + mod = bt_mesh_model_find_vnd(elem, company, id); + if (!mod) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL); + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, company); + net_buf_simple_add_le16(&msg, id); + goto send_list; + } + + net_buf_simple_add_u8(&msg, STATUS_SUCCESS); + + net_buf_simple_add_le16(&msg, addr); + net_buf_simple_add_le16(&msg, company); + net_buf_simple_add_le16(&msg, id); + + for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) { + net_buf_simple_add_le16(&msg, mod->groups[i]); + } + } + +send_list: + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Vendor Model Subscription List", __func__); + } +} + +#if CONFIG_BLE_MESH_LABEL_COUNT > 0 +static void mod_sub_va_add(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + int i; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + label_uuid = net_buf_simple_pull_mem(buf, 16); + + BT_DBG("elem_addr 0x%04x", elem_addr); + + mod_id = buf->data; + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + sub_addr = BLE_MESH_ADDR_UNASSIGNED; + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + sub_addr = BLE_MESH_ADDR_UNASSIGNED; + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = va_add(label_uuid, &sub_addr); + if (status != STATUS_SUCCESS) { + goto send_status; + } + + if (bt_mesh_model_find_group(mod, sub_addr)) { + /* Tried to add existing subscription */ + status = STATUS_SUCCESS; + goto send_status; + } + + for (i = 0; i < ARRAY_SIZE(mod->groups); i++) { + if (mod->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { + mod->groups[i] = sub_addr; + break; + } + } + + if (i == ARRAY_SIZE(mod->groups)) { + status = STATUS_INSUFF_RESOURCES; + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_add(sub_addr); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + status = STATUS_SUCCESS; + } + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); +} + +static void mod_sub_va_del(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u16_t *match = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + label_uuid = net_buf_simple_pull_mem(buf, 16); + + BT_DBG("elem_addr 0x%04x", elem_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + sub_addr = BLE_MESH_ADDR_UNASSIGNED; + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + sub_addr = BLE_MESH_ADDR_UNASSIGNED; + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = va_del(label_uuid, &sub_addr); + if (sub_addr == BLE_MESH_ADDR_UNASSIGNED) { + goto send_status; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(&sub_addr, 1); + } + + match = bt_mesh_model_find_group(mod, sub_addr); + if (match) { + *match = BLE_MESH_ADDR_UNASSIGNED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + status = STATUS_SUCCESS; + } else { + status = STATUS_CANNOT_REMOVE; + } + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); +} + +static void mod_sub_va_overwrite(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t elem_addr = 0U, sub_addr = BLE_MESH_ADDR_UNASSIGNED; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *label_uuid = NULL; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + label_uuid = net_buf_simple_pull_mem(buf, 16); + + BT_DBG("elem_addr 0x%04x", elem_addr); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups)); + } + + mod_sub_list_clear(mod); + + if (ARRAY_SIZE(mod->groups) > 0) { + status = va_add(label_uuid, &sub_addr); + if (status == STATUS_SUCCESS) { + mod->groups[0] = sub_addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(mod); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_add(sub_addr); + } + } + } else { + status = STATUS_INSUFF_RESOURCES; + } + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, sub_addr, + mod_id, vnd); +} +#else +static void mod_sub_va_add(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + net_buf_simple_pull(buf, 16); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = STATUS_INSUFF_RESOURCES; + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, + BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd); +} + +static void mod_sub_va_del(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + net_buf_simple_pull(buf, 16); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + if (!get_model(elem, buf, &vnd)) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = STATUS_INSUFF_RESOURCES; + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, + BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd); +} + +static void mod_sub_va_overwrite(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_elem *elem = NULL; + u16_t elem_addr = 0U; + u8_t *mod_id = NULL; + u8_t status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + net_buf_simple_pull(buf, 18); + + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + if (!get_model(elem, buf, &vnd)) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = STATUS_INSUFF_RESOURCES; + +send_status: + send_mod_sub_status(model, ctx, status, elem_addr, + BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd); +} +#endif /* CONFIG_BLE_MESH_LABEL_COUNT > 0 */ + +static void send_net_key_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t idx, u8_t status) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_STATUS, 3); + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_STATUS); + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, idx); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config NetKey Status", __func__); + } +} + +static void net_key_add(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; + int err = 0; + + idx = net_buf_simple_pull_le16(buf); + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + BT_DBG("idx 0x%04x", idx); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx == BLE_MESH_KEY_UNUSED) { + sub = &bt_mesh.sub[i]; + break; + } + } + + if (!sub) { + send_net_key_status(model, ctx, idx, + STATUS_INSUFF_RESOURCES); + return; + } + } + + /* Check for already existing subnet */ + if (sub->net_idx == idx) { + u8_t status = 0U; + + if (memcmp(buf->data, sub->keys[0].net, 16)) { + status = STATUS_IDX_ALREADY_STORED; + } else { + status = STATUS_SUCCESS; + } + + send_net_key_status(model, ctx, idx, status); + return; + } + + err = bt_mesh_net_keys_create(&sub->keys[0], buf->data); + if (err) { + send_net_key_status(model, ctx, idx, STATUS_UNSPECIFIED); + return; + } + + sub->net_idx = idx; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Storing NetKey persistently"); + bt_mesh_store_subnet(sub); + } + + /* Make sure we have valid beacon data to be sent */ + bt_mesh_net_beacon_update(sub); + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; + bt_mesh_proxy_beacon_send(sub); + bt_mesh_adv_update(); + } else { + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + } + + send_net_key_status(model, ctx, idx, STATUS_SUCCESS); + + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_netkey_add.net_idx = sub->net_idx; + memcpy(change.cfg_netkey_add.net_key, sub->keys[0].net, 16); + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); +} + +static void net_key_update(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; + int err = 0; + + idx = net_buf_simple_pull_le16(buf); + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + BT_DBG("idx 0x%04x", idx); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + send_net_key_status(model, ctx, idx, STATUS_INVALID_NETKEY); + return; + } + + /* The node shall successfully process a NetKey Update message on a + * valid NetKeyIndex when the NetKey value is different and the Key + * Refresh procedure has not been started, or when the NetKey value is + * the same in Phase 1. The NetKey Update message shall generate an + * error when the node is in Phase 2, or Phase 3. + */ + switch (sub->kr_phase) { + case BLE_MESH_KR_NORMAL: + if (!memcmp(buf->data, sub->keys[0].net, 16)) { + return; + } + break; + case BLE_MESH_KR_PHASE_1: + if (!memcmp(buf->data, sub->keys[1].net, 16)) { + send_net_key_status(model, ctx, idx, STATUS_SUCCESS); + return; + } + /* fall through */ + case BLE_MESH_KR_PHASE_2: + case BLE_MESH_KR_PHASE_3: + send_net_key_status(model, ctx, idx, STATUS_CANNOT_UPDATE); + return; + } + + err = bt_mesh_net_keys_create(&sub->keys[1], buf->data); + if (!err && (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || + IS_ENABLED(CONFIG_BLE_MESH_FRIEND))) { + err = friend_cred_update(sub); + } + + if (err) { + send_net_key_status(model, ctx, idx, STATUS_UNSPECIFIED); + return; + } + + sub->kr_phase = BLE_MESH_KR_PHASE_1; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Storing NetKey persistently"); + bt_mesh_store_subnet(sub); + } + + bt_mesh_net_beacon_update(sub); + + send_net_key_status(model, ctx, idx, STATUS_SUCCESS); + + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_netkey_update.net_idx = sub->net_idx; + memcpy(change.cfg_netkey_update.net_key, sub->keys[1].net, 16); + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); +} + +static void hb_pub_disable(struct bt_mesh_cfg_srv *cfg) +{ + BT_DBG("%s", __func__); + + cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_pub.count = 0U; + cfg->hb_pub.ttl = 0U; + cfg->hb_pub.period = 0U; + + k_delayed_work_cancel(&cfg->hb_pub.timer); +} + +static void net_key_del(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + u16_t del_idx = 0U; + u8_t status = 0U; + + del_idx = net_buf_simple_pull_le16(buf); + if (del_idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, del_idx); + return; + } + + BT_DBG("idx 0x%04x", del_idx); + + sub = bt_mesh_subnet_get(del_idx); + if (!sub) { + /* This could be a retry of a previous attempt that had its + * response lost, so pretend that it was a success. + */ + status = STATUS_SUCCESS; + goto send_status; + } + + /* The key that the message was encrypted with cannot be removed. + * The NetKey List must contain a minimum of one NetKey. + */ + if (ctx->net_idx == del_idx) { + status = STATUS_CANNOT_REMOVE; + goto send_status; + } + + bt_mesh_subnet_del(sub, true); + status = STATUS_SUCCESS; + +send_status: + send_net_key_status(model, ctx, del_idx, status); + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_netkey_delete.net_idx = sub->net_idx; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +static void net_key_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_LIST, + IDX_LEN(CONFIG_BLE_MESH_SUBNET_COUNT)); + u16_t prev = 0U, i = 0U; + + bt_mesh_model_msg_init(&msg, OP_NET_KEY_LIST); + + prev = BLE_MESH_KEY_UNUSED; + for (i = 0U; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (prev == BLE_MESH_KEY_UNUSED) { + prev = sub->net_idx; + continue; + } + + key_idx_pack(&msg, prev, sub->net_idx); + prev = BLE_MESH_KEY_UNUSED; + } + + if (prev != BLE_MESH_KEY_UNUSED) { + net_buf_simple_add_le16(&msg, prev); + } + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config NetKey List", __func__); + } +} + +static void node_identity_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); + struct bt_mesh_subnet *sub = NULL; + u8_t node_id = 0U; + u16_t idx = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + idx = net_buf_simple_pull_le16(buf); + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + bt_mesh_model_msg_init(&msg, OP_NODE_IDENTITY_STATUS); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_NETKEY); + node_id = 0x00; + } else { + net_buf_simple_add_u8(&msg, STATUS_SUCCESS); + node_id = sub->node_id; + } + + net_buf_simple_add_le16(&msg, idx); + net_buf_simple_add_u8(&msg, node_id); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Node Identity Status", __func__); + } +} + +static void node_identity_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4); + struct bt_mesh_subnet *sub = NULL; + u8_t node_id = 0U; + u16_t idx = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + idx = net_buf_simple_pull_le16(buf); + if (idx > 0xfff) { + BT_WARN("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + node_id = net_buf_simple_pull_u8(buf); + if (node_id != 0x00 && node_id != 0x01) { + BT_WARN("%s, Invalid Node ID value 0x%02x", __func__, node_id); + return; + } + + bt_mesh_model_msg_init(&msg, OP_NODE_IDENTITY_STATUS); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + net_buf_simple_add_u8(&msg, STATUS_INVALID_NETKEY); + net_buf_simple_add_le16(&msg, idx); + net_buf_simple_add_u8(&msg, node_id); + } else { + net_buf_simple_add_u8(&msg, STATUS_SUCCESS); + net_buf_simple_add_le16(&msg, idx); + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + if (node_id) { + bt_mesh_proxy_identity_start(sub); + } else { + bt_mesh_proxy_identity_stop(sub); + } + bt_mesh_adv_update(); + } + net_buf_simple_add_u8(&msg, sub->node_id); + } + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Node Identity Status", __func__); + } +} + +static void create_mod_app_status(struct net_buf_simple *msg, + struct bt_mesh_model *mod, bool vnd, + u16_t elem_addr, u16_t app_idx, + u8_t status, u8_t *mod_id) +{ + bt_mesh_model_msg_init(msg, OP_MOD_APP_STATUS); + + net_buf_simple_add_u8(msg, status); + net_buf_simple_add_le16(msg, elem_addr); + net_buf_simple_add_le16(msg, app_idx); + + if (vnd) { + memcpy(net_buf_simple_add(msg, 4), mod_id, 4); + } else { + memcpy(net_buf_simple_add(msg, 2), mod_id, 2); + } +} + +static void mod_app_bind(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); + u16_t elem_addr = 0U, key_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + key_app_idx = net_buf_simple_pull_le16(buf); + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + /* Configuration Server only allows device key based access */ + if (model == mod) { + BT_ERR("%s, Client tried to bind AppKey to Configuration Model", __func__); + status = STATUS_CANNOT_BIND; + goto send_status; + } + + status = mod_bind(mod, key_app_idx); + +send_status: + BT_DBG("status 0x%02x", status); + create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status, + mod_id); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model App Bind Status", __func__); + } + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_mod_app_bind.elem_addr = elem_addr; + change.cfg_mod_app_bind.app_idx = key_app_idx; + change.cfg_mod_app_bind.cid = vnd ? mod->vnd.company : 0xFFFF; + change.cfg_mod_app_bind.mod_id = vnd ? mod->vnd.id : mod->id; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +static void mod_app_unbind(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9); + u16_t elem_addr = 0U, key_app_idx = 0U; + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + key_app_idx = net_buf_simple_pull_le16(buf); + mod_id = buf->data; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_status; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_status; + } + + status = mod_unbind(mod, key_app_idx, true); + +send_status: + BT_DBG("status 0x%02x", status); + create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status, + mod_id); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model App Unbind Status", __func__); + } + + if (status == STATUS_SUCCESS) { + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_mod_app_unbind.elem_addr = elem_addr; + change.cfg_mod_app_unbind.app_idx = key_app_idx; + change.cfg_mod_app_unbind.cid = vnd ? mod->vnd.company : 0xFFFF; + change.cfg_mod_app_unbind.mod_id = vnd ? mod->vnd.id : mod->id; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); + } +} + +#define KEY_LIST_LEN (CONFIG_BLE_MESH_MODEL_KEY_COUNT * 2) + +static void mod_app_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + NET_BUF_SIMPLE_DEFINE(msg, + MAX(BLE_MESH_MODEL_BUF_LEN(OP_VND_MOD_APP_LIST, + 9 + KEY_LIST_LEN), + BLE_MESH_MODEL_BUF_LEN(OP_SIG_MOD_APP_LIST, + 9 + KEY_LIST_LEN))); + struct bt_mesh_model *mod = NULL; + struct bt_mesh_elem *elem = NULL; + u8_t *mod_id = NULL, status = 0U; + u16_t elem_addr = 0U; + bool vnd = false; + + elem_addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Prohibited element address 0x%04x", __func__, elem_addr); + return; + } + + mod_id = buf->data; + + BT_DBG("elem_addr 0x%04x", elem_addr); + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + mod = NULL; + vnd = (buf->len == 4U); + status = STATUS_INVALID_ADDRESS; + goto send_list; + } + + mod = get_model(elem, buf, &vnd); + if (!mod) { + status = STATUS_INVALID_MODEL; + goto send_list; + } + + status = STATUS_SUCCESS; + +send_list: + if (vnd) { + bt_mesh_model_msg_init(&msg, OP_VND_MOD_APP_LIST); + } else { + bt_mesh_model_msg_init(&msg, OP_SIG_MOD_APP_LIST); + } + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, elem_addr); + + if (vnd) { + net_buf_simple_add_mem(&msg, mod_id, 4); + } else { + net_buf_simple_add_mem(&msg, mod_id, 2); + } + + if (mod) { + int i; + + for (i = 0; i < ARRAY_SIZE(mod->keys); i++) { + if (mod->keys[i] != BLE_MESH_KEY_UNUSED) { + net_buf_simple_add_le16(&msg, mod->keys[i]); + } + } + } + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Model Application List", __func__); + } +} + +static void node_reset(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET_STATUS, 0); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + + bt_mesh_model_msg_init(&msg, OP_NODE_RESET_STATUS); + + /* Send the response first since we wont have any keys left to + * send it later. + */ + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Node Reset Status", __func__); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + bt_mesh_node_reset(); + } +} + +static void send_friend_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_FRIEND_STATUS, 1); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + bt_mesh_model_msg_init(&msg, OP_FRIEND_STATUS); + net_buf_simple_add_u8(&msg, cfg->frnd); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Friend Status", __func__); + } +} + +static void friend_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + send_friend_status(model, ctx); +} + +static void friend_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + if (buf->data[0] != 0x00 && buf->data[0] != 0x01) { + BT_WARN("Invalid Friend value 0x%02x", buf->data[0]); + return; + } + + if (!cfg) { + BT_WARN("No Configuration Server context available"); + goto send_status; + } + + BT_DBG("Friend 0x%02x -> 0x%02x", cfg->frnd, buf->data[0]); + + if (cfg->frnd == buf->data[0]) { + goto send_status; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + cfg->frnd = buf->data[0]; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_cfg(); + } + + if (cfg->frnd == BLE_MESH_FRIEND_DISABLED) { + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + } + } + + if (cfg->hb_pub.feat & BLE_MESH_FEAT_FRIEND) { + bt_mesh_heartbeat_send(); + } + +send_status: + send_friend_status(model, ctx); +} + +static void lpn_timeout_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5); + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U; + s32_t timeout = 0; + + lpn_addr = net_buf_simple_pull_le16(buf); + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x lpn_addr 0x%02x", + ctx->net_idx, ctx->app_idx, ctx->addr, lpn_addr); + + if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) { + BT_WARN("Invalid LPNAddress; ignoring msg"); + return; + } + + bt_mesh_model_msg_init(&msg, OP_LPN_TIMEOUT_STATUS); + net_buf_simple_add_le16(&msg, lpn_addr); + + if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + timeout = 0; + goto send_rsp; + } + + frnd = bt_mesh_friend_find(BLE_MESH_KEY_ANY, lpn_addr, true, true); + if (!frnd) { + timeout = 0; + goto send_rsp; + } + + timeout = k_delayed_work_remaining_get(&frnd->timer) / 100; + +send_rsp: + net_buf_simple_add_le24(&msg, timeout); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config LPN PollTimeout Status", __func__); + } +} + +static void send_krp_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t idx, u8_t phase, u8_t status) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_KRP_STATUS, 4); + + bt_mesh_model_msg_init(&msg, OP_KRP_STATUS); + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, idx); + net_buf_simple_add_u8(&msg, phase); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Key Refresh Phase Status", __func__); + } +} + +static void krp_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + u16_t idx = 0U; + + idx = net_buf_simple_pull_le16(buf); + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + BT_DBG("idx 0x%04x", idx); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + send_krp_status(model, ctx, idx, 0x00, STATUS_INVALID_NETKEY); + } else { + send_krp_status(model, ctx, idx, sub->kr_phase, + STATUS_SUCCESS); + } +} + +static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + u8_t phase = 0U; + u16_t idx = 0U; + + idx = net_buf_simple_pull_le16(buf); + phase = net_buf_simple_pull_u8(buf); + + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + BT_DBG("idx 0x%04x transition 0x%02x", idx, phase); + + sub = bt_mesh_subnet_get(idx); + if (!sub) { + send_krp_status(model, ctx, idx, 0x00, STATUS_INVALID_NETKEY); + return; + } + + BT_DBG("%u -> %u", sub->kr_phase, phase); + + if (phase < BLE_MESH_KR_PHASE_2 || phase > BLE_MESH_KR_PHASE_3 || + (sub->kr_phase == BLE_MESH_KR_NORMAL && + phase == BLE_MESH_KR_PHASE_2)) { + BT_WARN("%s, Prohibited transition %u -> %u", __func__, sub->kr_phase, phase); + return; + } + + if (sub->kr_phase == BLE_MESH_KR_PHASE_1 && + phase == BLE_MESH_KR_PHASE_2) { + sub->kr_phase = BLE_MESH_KR_PHASE_2; + sub->kr_flag = 1; + bt_mesh_net_beacon_update(sub); + } else if ((sub->kr_phase == BLE_MESH_KR_PHASE_1 || + sub->kr_phase == BLE_MESH_KR_PHASE_2) && + phase == BLE_MESH_KR_PHASE_3) { + bt_mesh_net_revoke_keys(sub); + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || + IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + friend_cred_refresh(ctx->net_idx); + } + sub->kr_phase = BLE_MESH_KR_NORMAL; + sub->kr_flag = 0; + bt_mesh_net_beacon_update(sub); + } + + send_krp_status(model, ctx, idx, sub->kr_phase, STATUS_SUCCESS); + + bt_mesh_cfg_server_state_change_t change = {0}; + change.cfg_kr_phase_set.net_idx = idx; + change.cfg_kr_phase_set.kr_phase = phase; + bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE, + model, ctx, (const u8_t *)&change, sizeof(change)); +} + +static u8_t hb_log(u16_t val) +{ + if (!val) { + return 0x00; + } else if (val == 0xffff) { + return 0xff; + } else { + return 32 - __builtin_clz(val); + } +} + +static u8_t hb_pub_count_log(u16_t val) +{ + if (!val) { + return 0x00; + } else if (val == 0x01) { + return 0x01; + } else if (val == 0xffff) { + return 0xff; + } else { + return 32 - __builtin_clz(val - 1) + 1; + } +} + +static u16_t hb_pwr2(u8_t val, u8_t sub) +{ + if (!val) { + return 0x0000; + } else if (val == 0xff || val == 0x11) { + return 0xffff; + } else { + return (1 << (val - sub)); + } +} + +struct hb_pub_param { + u16_t dst; + u8_t count_log; + u8_t period_log; + u8_t ttl; + u16_t feat; + u16_t net_idx; +} __packed; + +static void hb_pub_send_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, u8_t status, + struct hb_pub_param *orig_msg) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_STATUS, 10); + struct bt_mesh_cfg_srv *cfg = model->user_data; + + BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status); + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_STATUS); + + net_buf_simple_add_u8(&msg, status); + + if (orig_msg) { + memcpy(net_buf_simple_add(&msg, sizeof(*orig_msg)), orig_msg, + sizeof(*orig_msg)); + goto send; + } + + net_buf_simple_add_le16(&msg, cfg->hb_pub.dst); + net_buf_simple_add_u8(&msg, hb_pub_count_log(cfg->hb_pub.count)); + net_buf_simple_add_u8(&msg, cfg->hb_pub.period); + net_buf_simple_add_u8(&msg, cfg->hb_pub.ttl); + net_buf_simple_add_le16(&msg, cfg->hb_pub.feat); + net_buf_simple_add_le16(&msg, cfg->hb_pub.net_idx); + +send: + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Heartbeat Publication Status", __func__); + } +} + +static void heartbeat_pub_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("src 0x%04x", ctx->addr); + + hb_pub_send_status(model, ctx, STATUS_SUCCESS, NULL); +} + +static void heartbeat_pub_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct hb_pub_param *param = (void *)buf->data; + struct bt_mesh_cfg_srv *cfg = model->user_data; + u16_t dst = 0U, feat = 0U, idx = 0U; + u8_t status = 0U; + + BT_DBG("src 0x%04x", ctx->addr); + + dst = sys_le16_to_cpu(param->dst); + /* All other address types but virtual are valid */ + if (BLE_MESH_ADDR_IS_VIRTUAL(dst)) { + status = STATUS_INVALID_ADDRESS; + goto failed; + } + + if (param->count_log > 0x11 && param->count_log != 0xff) { + status = STATUS_CANNOT_SET; + goto failed; + } + + if (param->period_log > 0x10) { + status = STATUS_CANNOT_SET; + goto failed; + } + + if (param->ttl > BLE_MESH_TTL_MAX && param->ttl != BLE_MESH_TTL_DEFAULT) { + BT_ERR("%s, Invalid TTL value 0x%02x", __func__, param->ttl); + return; + } + + feat = sys_le16_to_cpu(param->feat); + + idx = sys_le16_to_cpu(param->net_idx); + if (idx > 0xfff) { + BT_ERR("%s, Invalid NetKeyIndex 0x%04x", __func__, idx); + return; + } + + if (!bt_mesh_subnet_get(idx)) { + status = STATUS_INVALID_NETKEY; + goto failed; + } + + cfg->hb_pub.dst = dst; + cfg->hb_pub.period = param->period_log; + cfg->hb_pub.feat = feat & BLE_MESH_FEAT_SUPPORTED; + cfg->hb_pub.net_idx = idx; + + if (dst == BLE_MESH_ADDR_UNASSIGNED) { + hb_pub_disable(cfg); + } else { + /* 2^(n-1) */ + cfg->hb_pub.count = hb_pwr2(param->count_log, 1); + cfg->hb_pub.ttl = param->ttl; + + BT_DBG("period %u ms", hb_pwr2(param->period_log, 1) * 1000U); + + /* Note: Send heartbeat message here will cause wrong heartbeat status message */ +#if 0 + /* The first Heartbeat message shall be published as soon + * as possible after the Heartbeat Publication Period state + * has been configured for periodic publishing. + */ + if (param->period_log && param->count_log) { + k_work_submit(&cfg->hb_pub.timer.work); + } else { + k_delayed_work_cancel(&cfg->hb_pub.timer); + } +#endif + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_hb_pub(); + } + + hb_pub_send_status(model, ctx, STATUS_SUCCESS, NULL); + + /* The first Heartbeat message shall be published as soon + * as possible after the Heartbeat Publication Period state + * has been configured for periodic publishing. + */ + if (dst != BLE_MESH_ADDR_UNASSIGNED) { + if (param->period_log && param->count_log) { + k_work_submit(&cfg->hb_pub.timer.work); + } else { + k_delayed_work_cancel(&cfg->hb_pub.timer); + } + } + + return; + +failed: + hb_pub_send_status(model, ctx, status, param); +} + +static void hb_sub_send_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, u8_t status) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_STATUS, 9); + struct bt_mesh_cfg_srv *cfg = model->user_data; + u16_t period = 0U; + s64_t uptime = 0; + + BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status); + + uptime = k_uptime_get(); + if (uptime > cfg->hb_sub.expiry) { + period = 0U; + } else { + period = (cfg->hb_sub.expiry - uptime) / 1000; + } + + bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_STATUS); + + net_buf_simple_add_u8(&msg, status); + net_buf_simple_add_le16(&msg, cfg->hb_sub.src); + net_buf_simple_add_le16(&msg, cfg->hb_sub.dst); + net_buf_simple_add_u8(&msg, hb_log(period)); + net_buf_simple_add_u8(&msg, hb_log(cfg->hb_sub.count)); + net_buf_simple_add_u8(&msg, cfg->hb_sub.min_hops); + net_buf_simple_add_u8(&msg, cfg->hb_sub.max_hops); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Config Heartbeat Subscription Status", __func__); + } +} + +static void heartbeat_sub_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("src 0x%04x", ctx->addr); + + hb_sub_send_status(model, ctx, STATUS_SUCCESS); +} + +static void heartbeat_sub_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + u16_t sub_src = 0U, sub_dst = 0U; + u8_t sub_period = 0U; + s32_t period_ms = 0; + + BT_DBG("src 0x%04x", ctx->addr); + + sub_src = net_buf_simple_pull_le16(buf); + sub_dst = net_buf_simple_pull_le16(buf); + sub_period = net_buf_simple_pull_u8(buf); + + BT_DBG("sub_src 0x%04x sub_dst 0x%04x period 0x%02x", + sub_src, sub_dst, sub_period); + + if (sub_src != BLE_MESH_ADDR_UNASSIGNED && + !BLE_MESH_ADDR_IS_UNICAST(sub_src)) { + BT_WARN("Prohibited source address"); + return; + } + + if (BLE_MESH_ADDR_IS_VIRTUAL(sub_dst) || BLE_MESH_ADDR_IS_RFU(sub_dst) || + (BLE_MESH_ADDR_IS_UNICAST(sub_dst) && + sub_dst != bt_mesh_primary_addr())) { + BT_WARN("Prohibited destination address"); + return; + } + + if (sub_period > 0x11) { + BT_WARN("Prohibited subscription period 0x%02x", sub_period); + return; + } + + if (sub_src == BLE_MESH_ADDR_UNASSIGNED || + sub_dst == BLE_MESH_ADDR_UNASSIGNED || + sub_period == 0x00) { + /* Only an explicit address change to unassigned should + * trigger clearing of the values according to + * MESH/NODE/CFG/HBS/BV-02-C. + */ + if (sub_src == BLE_MESH_ADDR_UNASSIGNED || + sub_dst == BLE_MESH_ADDR_UNASSIGNED) { + cfg->hb_sub.src = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.dst = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.min_hops = BLE_MESH_TTL_MAX; + cfg->hb_sub.max_hops = 0U; + cfg->hb_sub.count = 0U; + } + + period_ms = 0; + } else { + cfg->hb_sub.src = sub_src; + cfg->hb_sub.dst = sub_dst; + cfg->hb_sub.min_hops = BLE_MESH_TTL_MAX; + cfg->hb_sub.max_hops = 0U; + cfg->hb_sub.count = 0U; + period_ms = hb_pwr2(sub_period, 1) * 1000U; + } + + /* Let the transport layer know it needs to handle this address */ + bt_mesh_set_hb_sub_dst(cfg->hb_sub.dst); + + BT_DBG("period_ms %u", period_ms); + + if (period_ms) { + cfg->hb_sub.expiry = k_uptime_get() + period_ms; + } else { + cfg->hb_sub.expiry = 0; + } + + hb_sub_send_status(model, ctx, STATUS_SUCCESS); + + /* MESH/NODE/CFG/HBS/BV-01-C expects the MinHops to be 0x7f after + * disabling subscription, but 0x00 for subsequent Get requests. + */ + if (!period_ms) { + cfg->hb_sub.min_hops = 0U; + } +} + +const struct bt_mesh_model_op bt_mesh_cfg_srv_op[] = { + { OP_DEV_COMP_DATA_GET, 1, dev_comp_data_get }, + { OP_APP_KEY_ADD, 19, app_key_add }, + { OP_APP_KEY_UPDATE, 19, app_key_update }, + { OP_APP_KEY_DEL, 3, app_key_del }, + { OP_APP_KEY_GET, 2, app_key_get }, + { OP_BEACON_GET, 0, beacon_get }, + { OP_BEACON_SET, 1, beacon_set }, + { OP_DEFAULT_TTL_GET, 0, default_ttl_get }, + { OP_DEFAULT_TTL_SET, 1, default_ttl_set }, + { OP_GATT_PROXY_GET, 0, gatt_proxy_get }, + { OP_GATT_PROXY_SET, 1, gatt_proxy_set }, + { OP_NET_TRANSMIT_GET, 0, net_transmit_get }, + { OP_NET_TRANSMIT_SET, 1, net_transmit_set }, + { OP_RELAY_GET, 0, relay_get }, + { OP_RELAY_SET, 2, relay_set }, + { OP_MOD_PUB_GET, 4, mod_pub_get }, + { OP_MOD_PUB_SET, 11, mod_pub_set }, + { OP_MOD_PUB_VA_SET, 24, mod_pub_va_set }, + { OP_MOD_SUB_ADD, 6, mod_sub_add }, + { OP_MOD_SUB_VA_ADD, 20, mod_sub_va_add }, + { OP_MOD_SUB_DEL, 6, mod_sub_del }, + { OP_MOD_SUB_VA_DEL, 20, mod_sub_va_del }, + { OP_MOD_SUB_OVERWRITE, 6, mod_sub_overwrite }, + { OP_MOD_SUB_VA_OVERWRITE, 20, mod_sub_va_overwrite }, + { OP_MOD_SUB_DEL_ALL, 4, mod_sub_del_all }, + { OP_MOD_SUB_GET, 4, mod_sub_get }, + { OP_MOD_SUB_GET_VND, 6, mod_sub_get_vnd }, + { OP_NET_KEY_ADD, 18, net_key_add }, + { OP_NET_KEY_UPDATE, 18, net_key_update }, + { OP_NET_KEY_DEL, 2, net_key_del }, + { OP_NET_KEY_GET, 0, net_key_get }, + { OP_NODE_IDENTITY_GET, 2, node_identity_get }, + { OP_NODE_IDENTITY_SET, 3, node_identity_set }, + { OP_MOD_APP_BIND, 6, mod_app_bind }, + { OP_MOD_APP_UNBIND, 6, mod_app_unbind }, + { OP_SIG_MOD_APP_GET, 4, mod_app_get }, + { OP_VND_MOD_APP_GET, 6, mod_app_get }, + { OP_NODE_RESET, 0, node_reset }, + { OP_FRIEND_GET, 0, friend_get }, + { OP_FRIEND_SET, 1, friend_set }, + { OP_LPN_TIMEOUT_GET, 2, lpn_timeout_get }, + { OP_KRP_GET, 2, krp_get }, + { OP_KRP_SET, 3, krp_set }, + { OP_HEARTBEAT_PUB_GET, 0, heartbeat_pub_get }, + { OP_HEARTBEAT_PUB_SET, 9, heartbeat_pub_set }, + { OP_HEARTBEAT_SUB_GET, 0, heartbeat_sub_get }, + { OP_HEARTBEAT_SUB_SET, 5, heartbeat_sub_set }, + BLE_MESH_MODEL_OP_END, +}; + +static void hb_publish(struct k_work *work) +{ + struct bt_mesh_cfg_srv *cfg = CONTAINER_OF(work, + struct bt_mesh_cfg_srv, + hb_pub.timer.work); + struct bt_mesh_subnet *sub = NULL; + u16_t period_ms = 0U; + + BT_DBG("hb_pub.count: %u", cfg->hb_pub.count); + + sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx); + if (!sub) { + BT_ERR("%s, No matching subnet for idx 0x%02x", + __func__, cfg->hb_pub.net_idx); + cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED; + return; + } + + if (cfg->hb_pub.count == 0U) { + return; + } + + period_ms = hb_pwr2(cfg->hb_pub.period, 1) * 1000U; + if (period_ms && cfg->hb_pub.count > 1) { + k_delayed_work_submit(&cfg->hb_pub.timer, period_ms); + } + + bt_mesh_heartbeat_send(); + + if (cfg->hb_pub.count != 0xffff) { + cfg->hb_pub.count--; + } +} + +static bool conf_is_valid(struct bt_mesh_cfg_srv *cfg) +{ + if (cfg->relay > 0x02) { + return false; + } + + if (cfg->beacon > 0x01) { + return false; + } + + if (cfg->default_ttl > BLE_MESH_TTL_MAX) { + return false; + } + + return true; +} + +int bt_mesh_cfg_srv_init(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + + if (!cfg) { + BT_ERR("%s, No Configuration Server context provided", __func__); + return -EINVAL; + } + + if (!conf_is_valid(cfg)) { + BT_ERR("%s, Invalid values in configuration", __func__); + return -EINVAL; + } + + /* Configuration Model security is device-key based */ + model->keys[0] = BLE_MESH_KEY_DEV; + + if (!IS_ENABLED(CONFIG_BLE_MESH_RELAY)) { + cfg->relay = BLE_MESH_RELAY_NOT_SUPPORTED; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + cfg->frnd = BLE_MESH_FRIEND_NOT_SUPPORTED; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + cfg->gatt_proxy = BLE_MESH_GATT_PROXY_NOT_SUPPORTED; + } + + k_delayed_work_init(&cfg->hb_pub.timer, hb_publish); + cfg->hb_pub.net_idx = BLE_MESH_KEY_UNUSED; + cfg->hb_sub.expiry = 0; + + cfg->model = model; + + conf = cfg; + + return 0; +} + +int bt_mesh_cfg_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_cfg_srv *cfg = model->user_data; + + if (!cfg) { + BT_ERR("%s, No Configuration Server context provided", __func__); + return -EINVAL; + } + + bt_mesh_cfg_reset(); + + k_delayed_work_free(&cfg->hb_pub.timer); + cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED; + + conf = NULL; + + return 0; +} + +static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + size_t clear_count = 0U; + + /* Clear model state that isn't otherwise cleared. E.g. AppKey + * binding and model publication is cleared as a consequence + * of removing all app keys, however model subscription clearing + * must be taken care of here. + */ + + clear_count = mod_sub_list_clear(mod); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && clear_count) { + bt_mesh_store_mod_sub(mod); + } +} + +void bt_mesh_cfg_reset(void) +{ + struct bt_mesh_cfg_srv *cfg = conf; + int i; + + BT_DBG("%s", __func__); + + if (!cfg) { + return; + } + + bt_mesh_set_hb_sub_dst(BLE_MESH_ADDR_UNASSIGNED); + + cfg->hb_sub.src = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.dst = BLE_MESH_ADDR_UNASSIGNED; + cfg->hb_sub.expiry = 0; + + /* Delete all net keys, which also takes care of all app keys which + * are associated with each net key. + */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx != BLE_MESH_KEY_UNUSED) { + bt_mesh_subnet_del(sub, true); + } + } + + bt_mesh_model_foreach(mod_reset, NULL); + + (void)memset(labels, 0, sizeof(labels)); +} + +void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat) +{ + struct bt_mesh_cfg_srv *cfg = conf; + + if (!cfg) { + BT_WARN("No configuration server context available"); + return; + } + + if (src != cfg->hb_sub.src || dst != cfg->hb_sub.dst) { + BT_WARN("No subscription for received heartbeat"); + return; + } + + if (k_uptime_get() > cfg->hb_sub.expiry) { + BT_WARN("Heartbeat subscription period expired"); + return; + } + + cfg->hb_sub.min_hops = MIN(cfg->hb_sub.min_hops, hops); + cfg->hb_sub.max_hops = MAX(cfg->hb_sub.max_hops, hops); + + if (cfg->hb_sub.count < 0xffff) { + cfg->hb_sub.count++; + } + + BT_DBG("src 0x%04x dst 0x%04x hops %u min %u max %u count %u", src, + dst, hops, cfg->hb_sub.min_hops, cfg->hb_sub.max_hops, + cfg->hb_sub.count); + + if (cfg->hb_sub.func) { + cfg->hb_sub.func(hops, feat); + } +} + +u8_t bt_mesh_net_transmit_get(void) +{ + if (conf) { + return conf->net_transmit; + } + + return 0; +} + +u8_t bt_mesh_relay_get(void) +{ + if (conf) { + return conf->relay; + } + + return BLE_MESH_RELAY_NOT_SUPPORTED; +} + +u8_t bt_mesh_friend_get(void) +{ + if (conf) { + BT_DBG("conf %p conf->frnd 0x%02x", conf, conf->frnd); + return conf->frnd; + } + + return BLE_MESH_FRIEND_NOT_SUPPORTED; +} + +u8_t bt_mesh_relay_retransmit_get(void) +{ + if (conf) { + return conf->relay_retransmit; + } + + return 0; +} + +u8_t bt_mesh_beacon_get(void) +{ + if (conf) { + return conf->beacon; + } + + return BLE_MESH_BEACON_DISABLED; +} + +u8_t bt_mesh_gatt_proxy_get(void) +{ + if (conf) { + return conf->gatt_proxy; + } + + return BLE_MESH_GATT_PROXY_NOT_SUPPORTED; +} + +u8_t bt_mesh_default_ttl_get(void) +{ + if (conf) { + return conf->default_ttl; + } + + return DEFAULT_TTL; +} + +u8_t *bt_mesh_label_uuid_get(u16_t addr) +{ + int i; + + BT_DBG("addr 0x%04x", addr); + + for (i = 0; i < ARRAY_SIZE(labels); i++) { + if (labels[i].addr == addr) { + BT_DBG("Found Label UUID for 0x%04x: %s", addr, + bt_hex(labels[i].uuid, 16)); + return labels[i].uuid; + } + } + + BT_WARN("No matching Label UUID for 0x%04x", addr); + + return NULL; +} + +struct bt_mesh_hb_pub *bt_mesh_hb_pub_get(void) +{ + if (!conf) { + return NULL; + } + + return &conf->hb_pub; +} + +void bt_mesh_hb_pub_disable(void) +{ + if (conf) { + hb_pub_disable(conf); + } +} + +struct bt_mesh_cfg_srv *bt_mesh_cfg_get(void) +{ + return conf; +} + +void bt_mesh_subnet_del(struct bt_mesh_subnet *sub, bool store) +{ + int i; + + BT_DBG("NetIdx 0x%03x store %u", sub->net_idx, store); + + if (conf && conf->hb_pub.net_idx == sub->net_idx) { + hb_pub_disable(conf); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_hb_pub(); + } + } + + /* Delete any app keys bound to this NetKey index */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx == sub->net_idx) { + bt_mesh_app_key_del(key, store); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_clear_net_idx(sub->net_idx); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_clear_subnet(sub); + } + + (void)memset(sub, 0, sizeof(*sub)); + sub->net_idx = BLE_MESH_KEY_UNUSED; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/crypto.c b/components/bt/esp_ble_mesh/mesh_core/crypto.c new file mode 100644 index 000000000..d1d94e167 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/crypto.c @@ -0,0 +1,874 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_CRYPTO) + +#include "mesh_common.h" +#include "crypto.h" +#include "mesh_aes_encrypt.h" +#include "mesh_bearer_adapt.h" + +#define NET_MIC_LEN(pdu) (((pdu)[1] & 0x80) ? 8 : 4) +#define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4) + +int bt_mesh_aes_cmac(const u8_t key[16], struct bt_mesh_sg *sg, + size_t sg_len, u8_t mac[16]) +{ + struct tc_aes_key_sched_struct sched = {0}; + struct tc_cmac_struct state = {0}; + + if (tc_cmac_setup(&state, key, &sched) == TC_CRYPTO_FAIL) { + return -EIO; + } + + for (; sg_len; sg_len--, sg++) { + if (tc_cmac_update(&state, sg->data, + sg->len) == TC_CRYPTO_FAIL) { + return -EIO; + } + } + + if (tc_cmac_final(mac, &state) == TC_CRYPTO_FAIL) { + return -EIO; + } + + return 0; +} + +int bt_mesh_k1(const u8_t *ikm, size_t ikm_len, const u8_t salt[16], + const char *info, u8_t okm[16]) +{ + int err = 0; + + err = bt_mesh_aes_cmac_one(salt, ikm, ikm_len, okm); + if (err < 0) { + return err; + } + + return bt_mesh_aes_cmac_one(okm, info, strlen(info), okm); +} + +int bt_mesh_k2(const u8_t n[16], const u8_t *p, size_t p_len, + u8_t net_id[1], u8_t enc_key[16], u8_t priv_key[16]) +{ + struct bt_mesh_sg sg[3] = {0}; + u8_t salt[16] = {0}; + u8_t out[16] = {0}; + u8_t t[16] = {0}; + u8_t pad = 0U; + int err = 0; + + BT_DBG("n %s", bt_hex(n, 16)); + BT_DBG("p %s", bt_hex(p, p_len)); + + err = bt_mesh_s1("smk2", salt); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(salt, n, 16, t); + if (err) { + return err; + } + + pad = 0x01; + + sg[0].data = NULL; + sg[0].len = 0; + sg[1].data = p; + sg[1].len = p_len; + sg[2].data = &pad; + sg[2].len = sizeof(pad); + + err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out); + if (err) { + return err; + } + + net_id[0] = out[15] & 0x7f; + + sg[0].data = out; + sg[0].len = sizeof(out); + pad = 0x02; + + err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out); + if (err) { + return err; + } + + memcpy(enc_key, out, 16); + + pad = 0x03; + + err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out); + if (err) { + return err; + } + + memcpy(priv_key, out, 16); + + BT_DBG("NID 0x%02x enc_key %s", net_id[0], bt_hex(enc_key, 16)); + BT_DBG("priv_key %s", bt_hex(priv_key, 16)); + + return 0; +} + +int bt_mesh_k3(const u8_t n[16], u8_t out[8]) +{ + u8_t id64[] = { 'i', 'd', '6', '4', 0x01 }; + u8_t tmp[16] = {0}; + u8_t t[16] = {0}; + int err = 0; + + err = bt_mesh_s1("smk3", tmp); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(tmp, n, 16, t); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(t, id64, sizeof(id64), tmp); + if (err) { + return err; + } + + memcpy(out, tmp + 8, 8); + + return 0; +} + +int bt_mesh_k4(const u8_t n[16], u8_t out[1]) +{ + u8_t id6[] = { 'i', 'd', '6', 0x01 }; + u8_t tmp[16] = {0}; + u8_t t[16] = {0}; + int err = 0; + + err = bt_mesh_s1("smk4", tmp); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(tmp, n, 16, t); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(t, id6, sizeof(id6), tmp); + if (err) { + return err; + } + + out[0] = tmp[15] & BIT_MASK(6); + + return 0; +} + +int bt_mesh_id128(const u8_t n[16], const char *s, u8_t out[16]) +{ + const char *id128 = "id128\x01"; + u8_t salt[16] = {0}; + int err = 0; + + err = bt_mesh_s1(s, salt); + if (err) { + return err; + } + + return bt_mesh_k1(n, 16, salt, id128, out); +} + +static int bt_mesh_ccm_decrypt(const u8_t key[16], u8_t nonce[13], + const u8_t *enc_msg, size_t msg_len, + const u8_t *aad, size_t aad_len, + u8_t *out_msg, size_t mic_size) +{ + u8_t msg[16] = {0}, pmsg[16] = {0}, cmic[16] = {0}, + cmsg[16] = {0}, Xn[16] = {0}, mic[16] = {0}; + u16_t last_blk = 0U, blk_cnt = 0U; + size_t i = 0U, j = 0U; + int err = 0; + + if (msg_len < 1 || aad_len >= 0xff00) { + return -EINVAL; + } + + /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(0x0000, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmic); + if (err) { + return err; + } + + /* X_0 = e(AppKey, 0x09 || nonce || length) */ + if (mic_size == sizeof(u64_t)) { + pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00); + } else { + pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00); + } + + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(msg_len, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + + /* If AAD is being used to authenticate, include it here */ + if (aad_len) { + sys_put_be16(aad_len, pmsg); + + for (i = 0; i < sizeof(u16_t); i++) { + pmsg[i] = Xn[i] ^ pmsg[i]; + } + + j = 0; + aad_len += sizeof(u16_t); + while (aad_len > 16) { + do { + pmsg[i] = Xn[i] ^ aad[j]; + i++, j++; + } while (i < 16); + + aad_len -= 16; + i = 0; + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + } + + for (; i < aad_len; i++, j++) { + pmsg[i] = Xn[i] ^ aad[j]; + } + + for (i = aad_len; i < 16; i++) { + pmsg[i] = Xn[i]; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + } + + last_blk = msg_len % 16; + blk_cnt = (msg_len + 15) / 16; + if (!last_blk) { + last_blk = 16U; + } + + for (j = 0; j < blk_cnt; j++) { + if (j + 1 == blk_cnt) { + /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(j + 1, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmsg); + if (err) { + return err; + } + + /* Encrypted = Payload[0-15] ^ C_1 */ + for (i = 0; i < last_blk; i++) { + msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i]; + } + + memcpy(out_msg + (j * 16), msg, last_blk); + + /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ + for (i = 0; i < last_blk; i++) { + pmsg[i] = Xn[i] ^ msg[i]; + } + + for (i = last_blk; i < 16; i++) { + pmsg[i] = Xn[i] ^ 0x00; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + + /* MIC = C_mic ^ X_1 */ + for (i = 0; i < sizeof(mic); i++) { + mic[i] = cmic[i] ^ Xn[i]; + } + } else { + /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(j + 1, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmsg); + if (err) { + return err; + } + + /* Encrypted = Payload[0-15] ^ C_1 */ + for (i = 0; i < 16; i++) { + msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i]; + } + + memcpy(out_msg + (j * 16), msg, 16); + + /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ + for (i = 0; i < 16; i++) { + pmsg[i] = Xn[i] ^ msg[i]; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + } + } + + if (memcmp(mic, enc_msg + msg_len, mic_size)) { + return -EBADMSG; + } + + return 0; +} + +static int bt_mesh_ccm_encrypt(const u8_t key[16], u8_t nonce[13], + const u8_t *msg, size_t msg_len, + const u8_t *aad, size_t aad_len, + u8_t *out_msg, size_t mic_size) +{ + u8_t pmsg[16] = {0}, cmic[16] = {0}, cmsg[16] = {0}, + mic[16] = {0}, Xn[16] = {0}; + u16_t blk_cnt = 0U, last_blk = 0U; + size_t i = 0U, j = 0U; + int err = 0; + + BT_DBG("key %s", bt_hex(key, 16)); + BT_DBG("nonce %s", bt_hex(nonce, 13)); + BT_DBG("msg (len %u) %s", msg_len, bt_hex(msg, msg_len)); + BT_DBG("aad_len %u mic_size %u", aad_len, mic_size); + + /* Unsupported AAD size */ + if (aad_len >= 0xff00) { + return -EINVAL; + } + + /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(0x0000, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmic); + if (err) { + return err; + } + + /* X_0 = e(AppKey, 0x09 || nonce || length) */ + if (mic_size == sizeof(u64_t)) { + pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00); + } else { + pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00); + } + + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(msg_len, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + + /* If AAD is being used to authenticate, include it here */ + if (aad_len) { + sys_put_be16(aad_len, pmsg); + + for (i = 0; i < sizeof(u16_t); i++) { + pmsg[i] = Xn[i] ^ pmsg[i]; + } + + j = 0; + aad_len += sizeof(u16_t); + while (aad_len > 16) { + do { + pmsg[i] = Xn[i] ^ aad[j]; + i++, j++; + } while (i < 16); + + aad_len -= 16; + i = 0; + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + } + + for (; i < aad_len; i++, j++) { + pmsg[i] = Xn[i] ^ aad[j]; + } + + for (i = aad_len; i < 16; i++) { + pmsg[i] = Xn[i]; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + } + + last_blk = msg_len % 16; + blk_cnt = (msg_len + 15) / 16; + if (!last_blk) { + last_blk = 16U; + } + + for (j = 0; j < blk_cnt; j++) { + if (j + 1 == blk_cnt) { + /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ + for (i = 0; i < last_blk; i++) { + pmsg[i] = Xn[i] ^ msg[(j * 16) + i]; + } + for (i = last_blk; i < 16; i++) { + pmsg[i] = Xn[i] ^ 0x00; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + + /* MIC = C_mic ^ X_1 */ + for (i = 0; i < sizeof(mic); i++) { + mic[i] = cmic[i] ^ Xn[i]; + } + + /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(j + 1, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmsg); + if (err) { + return err; + } + + /* Encrypted = Payload[0-15] ^ C_1 */ + for (i = 0; i < last_blk; i++) { + out_msg[(j * 16) + i] = + msg[(j * 16) + i] ^ cmsg[i]; + } + } else { + /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ + for (i = 0; i < 16; i++) { + pmsg[i] = Xn[i] ^ msg[(j * 16) + i]; + } + + err = bt_mesh_encrypt_be(key, pmsg, Xn); + if (err) { + return err; + } + + /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0] = 0x01; + memcpy(pmsg + 1, nonce, 13); + sys_put_be16(j + 1, pmsg + 14); + + err = bt_mesh_encrypt_be(key, pmsg, cmsg); + if (err) { + return err; + } + + /* Encrypted = Payload[0-15] ^ C_N */ + for (i = 0; i < 16; i++) { + out_msg[(j * 16) + i] = + msg[(j * 16) + i] ^ cmsg[i]; + } + + } + } + + memcpy(out_msg + msg_len, mic, mic_size); + + return 0; +} + +#if defined(CONFIG_BLE_MESH_PROXY) +static void create_proxy_nonce(u8_t nonce[13], const u8_t *pdu, + u32_t iv_index) +{ + /* Nonce Type */ + nonce[0] = 0x03; + + /* Pad */ + nonce[1] = 0x00; + + /* Sequence Number */ + nonce[2] = pdu[2]; + nonce[3] = pdu[3]; + nonce[4] = pdu[4]; + + /* Source Address */ + nonce[5] = pdu[5]; + nonce[6] = pdu[6]; + + /* Pad */ + nonce[7] = 0U; + nonce[8] = 0U; + + /* IV Index */ + sys_put_be32(iv_index, &nonce[9]); +} +#endif /* PROXY */ + +static void create_net_nonce(u8_t nonce[13], const u8_t *pdu, + u32_t iv_index) +{ + /* Nonce Type */ + nonce[0] = 0x00; + + /* FRND + TTL */ + nonce[1] = pdu[1]; + + /* Sequence Number */ + nonce[2] = pdu[2]; + nonce[3] = pdu[3]; + nonce[4] = pdu[4]; + + /* Source Address */ + nonce[5] = pdu[5]; + nonce[6] = pdu[6]; + + /* Pad */ + nonce[7] = 0U; + nonce[8] = 0U; + + /* IV Index */ + sys_put_be32(iv_index, &nonce[9]); +} + +int bt_mesh_net_obfuscate(u8_t *pdu, u32_t iv_index, + const u8_t privacy_key[16]) +{ + u8_t priv_rand[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, }; + u8_t tmp[16] = {0}; + int err = 0, i; + + BT_DBG("IVIndex %u, PrivacyKey %s", iv_index, bt_hex(privacy_key, 16)); + + sys_put_be32(iv_index, &priv_rand[5]); + memcpy(&priv_rand[9], &pdu[7], 7); + + BT_DBG("PrivacyRandom %s", bt_hex(priv_rand, 16)); + + err = bt_mesh_encrypt_be(privacy_key, priv_rand, tmp); + if (err) { + return err; + } + + for (i = 0; i < 6; i++) { + pdu[1 + i] ^= tmp[i]; + } + + return 0; +} + +int bt_mesh_net_encrypt(const u8_t key[16], struct net_buf_simple *buf, + u32_t iv_index, bool proxy) +{ + u8_t mic_len = NET_MIC_LEN(buf->data); + u8_t nonce[13] = {0}; + int err = 0; + + BT_DBG("IVIndex %u EncKey %s mic_len %u", iv_index, bt_hex(key, 16), + mic_len); + BT_DBG("PDU (len %u) %s", buf->len, bt_hex(buf->data, buf->len)); + +#if defined(CONFIG_BLE_MESH_PROXY) + if (proxy) { + create_proxy_nonce(nonce, buf->data, iv_index); + } else { + create_net_nonce(nonce, buf->data, iv_index); + } +#else + create_net_nonce(nonce, buf->data, iv_index); +#endif + + BT_DBG("Nonce %s", bt_hex(nonce, 13)); + + err = bt_mesh_ccm_encrypt(key, nonce, &buf->data[7], buf->len - 7, + NULL, 0, &buf->data[7], mic_len); + if (!err) { + net_buf_simple_add(buf, mic_len); + } + + return err; +} + +int bt_mesh_net_decrypt(const u8_t key[16], struct net_buf_simple *buf, + u32_t iv_index, bool proxy) +{ + u8_t mic_len = NET_MIC_LEN(buf->data); + u8_t nonce[13] = {0}; + + BT_DBG("PDU (%u bytes) %s", buf->len, bt_hex(buf->data, buf->len)); + BT_DBG("iv_index %u, key %s mic_len %u", iv_index, bt_hex(key, 16), + mic_len); + +#if defined(CONFIG_BLE_MESH_PROXY) + if (proxy) { + create_proxy_nonce(nonce, buf->data, iv_index); + } else { + create_net_nonce(nonce, buf->data, iv_index); + } +#else + create_net_nonce(nonce, buf->data, iv_index); +#endif + + BT_DBG("Nonce %s", bt_hex(nonce, 13)); + + buf->len -= mic_len; + + return bt_mesh_ccm_decrypt(key, nonce, &buf->data[7], buf->len - 7, + NULL, 0, &buf->data[7], mic_len); +} + +static void create_app_nonce(u8_t nonce[13], bool dev_key, u8_t aszmic, + u16_t src, u16_t dst, u32_t seq_num, + u32_t iv_index) +{ + if (dev_key) { + nonce[0] = 0x02; + } else { + nonce[0] = 0x01; + } + + sys_put_be32((seq_num | ((u32_t)aszmic << 31)), &nonce[1]); + + sys_put_be16(src, &nonce[5]); + sys_put_be16(dst, &nonce[7]); + + sys_put_be32(iv_index, &nonce[9]); +} + +int bt_mesh_app_encrypt(const u8_t key[16], bool dev_key, u8_t aszmic, + struct net_buf_simple *buf, const u8_t *ad, + u16_t src, u16_t dst, u32_t seq_num, u32_t iv_index) +{ + u8_t nonce[13] = {0}; + int err = 0; + + BT_DBG("AppKey %s", bt_hex(key, 16)); + BT_DBG("dev_key %u src 0x%04x dst 0x%04x", dev_key, src, dst); + BT_DBG("seq_num 0x%08x iv_index 0x%08x", seq_num, iv_index); + BT_DBG("Clear: %s", bt_hex(buf->data, buf->len)); + + create_app_nonce(nonce, dev_key, aszmic, src, dst, seq_num, iv_index); + + BT_DBG("Nonce %s", bt_hex(nonce, 13)); + + err = bt_mesh_ccm_encrypt(key, nonce, buf->data, buf->len, ad, + ad ? 16 : 0, buf->data, APP_MIC_LEN(aszmic)); + if (!err) { + net_buf_simple_add(buf, APP_MIC_LEN(aszmic)); + BT_DBG("Encr: %s", bt_hex(buf->data, buf->len)); + } + + return err; +} + +int bt_mesh_app_decrypt(const u8_t key[16], bool dev_key, u8_t aszmic, + struct net_buf_simple *buf, struct net_buf_simple *out, + const u8_t *ad, u16_t src, u16_t dst, u32_t seq_num, + u32_t iv_index) +{ + u8_t nonce[13] = {0}; + int err = 0; + + BT_DBG("EncData (len %u) %s", buf->len, bt_hex(buf->data, buf->len)); + + create_app_nonce(nonce, dev_key, aszmic, src, dst, seq_num, iv_index); + + BT_DBG("AppKey %s", bt_hex(key, 16)); + BT_DBG("Nonce %s", bt_hex(nonce, 13)); + + err = bt_mesh_ccm_decrypt(key, nonce, buf->data, buf->len, ad, + ad ? 16 : 0, out->data, APP_MIC_LEN(aszmic)); + if (!err) { + net_buf_simple_add(out, buf->len); + } + + return err; +} + +/* reversed, 8-bit, poly=0x07 */ +static const u8_t crc_table[256] = { + 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75, + 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b, + 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69, + 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67, + + 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d, + 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43, + 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51, + 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f, + + 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05, + 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b, + 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19, + 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17, + + 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d, + 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33, + 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21, + 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f, + + 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95, + 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b, + 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89, + 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87, + + 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad, + 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3, + 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1, + 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf, + + 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5, + 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb, + 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9, + 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7, + + 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd, + 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3, + 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1, + 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf +}; + +u8_t bt_mesh_fcs_calc(const u8_t *data, u8_t data_len) +{ + u8_t fcs = 0xff; + + while (data_len--) { + fcs = crc_table[fcs ^ *data++]; + } + + BT_DBG("fcs 0x%02x", 0xff - fcs); + + return 0xff - fcs; +} + +bool bt_mesh_fcs_check(struct net_buf_simple *buf, u8_t received_fcs) +{ + const u8_t *data = buf->data; + u16_t data_len = buf->len; + u8_t fcs = 0xff; + + while (data_len--) { + fcs = crc_table[fcs ^ *data++]; + } + + return crc_table[fcs ^ received_fcs] == 0xcf; +} + +int bt_mesh_virtual_addr(const u8_t virtual_label[16], u16_t *addr) +{ + u8_t salt[16] = {0}; + u8_t tmp[16] = {0}; + int err = 0; + + err = bt_mesh_s1("vtad", salt); + if (err) { + return err; + } + + err = bt_mesh_aes_cmac_one(salt, virtual_label, 16, tmp); + if (err) { + return err; + } + + *addr = (sys_get_be16(&tmp[14]) & 0x3fff) | 0x8000; + + return 0; +} + +int bt_mesh_prov_conf_salt(const u8_t conf_inputs[145], u8_t salt[16]) +{ + const u8_t conf_salt_key[16] = { 0 }; + + return bt_mesh_aes_cmac_one(conf_salt_key, conf_inputs, 145, salt); +} + +int bt_mesh_prov_conf_key(const u8_t dhkey[32], const u8_t conf_salt[16], + u8_t conf_key[16]) +{ + return bt_mesh_k1(dhkey, 32, conf_salt, "prck", conf_key); +} + +int bt_mesh_prov_conf(const u8_t conf_key[16], const u8_t rand[16], + const u8_t auth[16], u8_t conf[16]) +{ + struct bt_mesh_sg sg[] = { { rand, 16 }, { auth, 16 } }; + + BT_DBG("ConfirmationKey %s", bt_hex(conf_key, 16)); + BT_DBG("RandomDevice %s", bt_hex(rand, 16)); + BT_DBG("AuthValue %s", bt_hex(auth, 16)); + + return bt_mesh_aes_cmac(conf_key, sg, ARRAY_SIZE(sg), conf); +} + +int bt_mesh_prov_decrypt(const u8_t key[16], u8_t nonce[13], + const u8_t data[25 + 8], u8_t out[25]) +{ + return bt_mesh_ccm_decrypt(key, nonce, data, 25, NULL, 0, out, 8); +} + +#if CONFIG_BLE_MESH_PROVISIONER +int bt_mesh_prov_encrypt(const u8_t key[16], u8_t nonce[13], + const u8_t data[25], u8_t out[33]) +{ + return bt_mesh_ccm_encrypt(key, nonce, data, 25, NULL, 0, out, 8); +} +#endif + +int bt_mesh_beacon_auth(const u8_t beacon_key[16], u8_t flags, + const u8_t net_id[8], u32_t iv_index, + u8_t auth[8]) +{ + u8_t msg[13] = {0}, tmp[16] = {0}; + int err = 0; + + BT_DBG("BeaconKey %s", bt_hex(beacon_key, 16)); + BT_DBG("NetId %s", bt_hex(net_id, 8)); + BT_DBG("IV Index 0x%08x", iv_index); + + msg[0] = flags; + memcpy(&msg[1], net_id, 8); + sys_put_be32(iv_index, &msg[9]); + + BT_DBG("BeaconMsg %s", bt_hex(msg, sizeof(msg))); + + err = bt_mesh_aes_cmac_one(beacon_key, msg, sizeof(msg), tmp); + if (!err) { + memcpy(auth, tmp, 8); + } + + return err; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/crypto.h b/components/bt/esp_ble_mesh/mesh_core/crypto.h new file mode 100644 index 000000000..feed22613 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/crypto.h @@ -0,0 +1,174 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _CRYPTO_H_ +#define _CRYPTO_H_ + +#include +#include "mesh_buf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_mesh_sg { + const void *data; + size_t len; +}; + +int bt_mesh_aes_cmac(const u8_t key[16], struct bt_mesh_sg *sg, + size_t sg_len, u8_t mac[16]); + +static inline int bt_mesh_aes_cmac_one(const u8_t key[16], const void *m, + size_t len, u8_t mac[16]) +{ + struct bt_mesh_sg sg = { m, len }; + + return bt_mesh_aes_cmac(key, &sg, 1, mac); +} + +static inline bool bt_mesh_s1(const char *m, u8_t salt[16]) +{ + const u8_t zero[16] = { 0 }; + + return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt); +} + +int bt_mesh_k1(const u8_t *ikm, size_t ikm_len, const u8_t salt[16], + const char *info, u8_t okm[16]); + +#define bt_mesh_k1_str(ikm, ikm_len, salt_str, info, okm) \ +({ \ + const u8_t salt[16] = salt_str; \ + bt_mesh_k1(ikm, ikm_len, salt, info, okm); \ +}) + +int bt_mesh_k2(const u8_t n[16], const u8_t *p, size_t p_len, + u8_t net_id[1], u8_t enc_key[16], u8_t priv_key[16]); + +int bt_mesh_k3(const u8_t n[16], u8_t out[8]); + +int bt_mesh_k4(const u8_t n[16], u8_t out[1]); + +int bt_mesh_id128(const u8_t n[16], const char *s, u8_t out[16]); + +static inline int bt_mesh_id_resolving_key(const u8_t net_key[16], + u8_t resolving_key[16]) +{ + return bt_mesh_k1_str(net_key, 16, "smbt", "smbi", resolving_key); +} + +static inline int bt_mesh_identity_key(const u8_t net_key[16], + u8_t identity_key[16]) +{ + return bt_mesh_id128(net_key, "nkik", identity_key); +} + +static inline int bt_mesh_beacon_key(const u8_t net_key[16], + u8_t beacon_key[16]) +{ + return bt_mesh_id128(net_key, "nkbk", beacon_key); +} + +int bt_mesh_beacon_auth(const u8_t beacon_key[16], u8_t flags, + const u8_t net_id[16], u32_t iv_index, + u8_t auth[8]); + +static inline int bt_mesh_app_id(const u8_t app_key[16], u8_t app_id[1]) +{ + return bt_mesh_k4(app_key, app_id); +} + +static inline int bt_mesh_session_key(const u8_t dhkey[32], + const u8_t prov_salt[16], + u8_t session_key[16]) +{ + return bt_mesh_k1(dhkey, 32, prov_salt, "prsk", session_key); +} + +static inline int bt_mesh_prov_nonce(const u8_t dhkey[32], + const u8_t prov_salt[16], + u8_t nonce[13]) +{ + u8_t tmp[16]; + int err; + + err = bt_mesh_k1(dhkey, 32, prov_salt, "prsn", tmp); + if (!err) { + memcpy(nonce, tmp + 3, 13); + } + + return err; +} + +static inline int bt_mesh_dev_key(const u8_t dhkey[32], + const u8_t prov_salt[16], + u8_t dev_key[16]) +{ + return bt_mesh_k1(dhkey, 32, prov_salt, "prdk", dev_key); +} + +static inline int bt_mesh_prov_salt(const u8_t conf_salt[16], + const u8_t prov_rand[16], + const u8_t dev_rand[16], + u8_t prov_salt[16]) +{ + const u8_t prov_salt_key[16] = { 0 }; + struct bt_mesh_sg sg[] = { + { conf_salt, 16 }, + { prov_rand, 16 }, + { dev_rand, 16 }, + }; + + return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt); +} + +int bt_mesh_net_obfuscate(u8_t *pdu, u32_t iv_index, + const u8_t privacy_key[16]); + +int bt_mesh_net_encrypt(const u8_t key[16], struct net_buf_simple *buf, + u32_t iv_index, bool proxy); + +int bt_mesh_net_decrypt(const u8_t key[16], struct net_buf_simple *buf, + u32_t iv_index, bool proxy); + +int bt_mesh_app_encrypt(const u8_t key[16], bool dev_key, u8_t aszmic, + struct net_buf_simple *buf, const u8_t *ad, + u16_t src, u16_t dst, u32_t seq_num, u32_t iv_index); + +int bt_mesh_app_decrypt(const u8_t key[16], bool dev_key, u8_t aszmic, + struct net_buf_simple *buf, struct net_buf_simple *out, + const u8_t *ad, u16_t src, u16_t dst, u32_t seq_num, + u32_t iv_index); + +u8_t bt_mesh_fcs_calc(const u8_t *data, u8_t data_len); + +bool bt_mesh_fcs_check(struct net_buf_simple *buf, u8_t received_fcs); + +int bt_mesh_virtual_addr(const u8_t virtual_label[16], u16_t *addr); + +int bt_mesh_prov_conf_salt(const u8_t conf_inputs[145], u8_t salt[16]); + +int bt_mesh_prov_conf_key(const u8_t dhkey[32], const u8_t conf_salt[16], + u8_t conf_key[16]); + +int bt_mesh_prov_conf(const u8_t conf_key[16], const u8_t rand[16], + const u8_t auth[16], u8_t conf[16]); + +int bt_mesh_prov_decrypt(const u8_t key[16], u8_t nonce[13], + const u8_t data[25 + 8], u8_t out[25]); + +int bt_mesh_prov_encrypt(const u8_t key[16], u8_t nonce[13], + const u8_t data[25], u8_t out[33]); + +#ifdef __cplusplus +} +#endif + +#endif /* _CRYPTO_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/foundation.h b/components/bt/esp_ble_mesh/mesh_core/foundation.h new file mode 100644 index 000000000..76634079e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/foundation.h @@ -0,0 +1,193 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _FOUNDATION_H_ +#define _FOUNDATION_H_ + +#include "mesh_byteorder.h" +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define OP_APP_KEY_ADD BLE_MESH_MODEL_OP_1(0x00) +#define OP_APP_KEY_UPDATE BLE_MESH_MODEL_OP_1(0x01) +#define OP_DEV_COMP_DATA_STATUS BLE_MESH_MODEL_OP_1(0x02) +#define OP_MOD_PUB_SET BLE_MESH_MODEL_OP_1(0x03) +#define OP_HEALTH_CURRENT_STATUS BLE_MESH_MODEL_OP_1(0x04) +#define OP_HEALTH_FAULT_STATUS BLE_MESH_MODEL_OP_1(0x05) +#define OP_HEARTBEAT_PUB_STATUS BLE_MESH_MODEL_OP_1(0x06) +#define OP_APP_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x00) +#define OP_APP_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x01) +#define OP_APP_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x02) +#define OP_APP_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x03) +#define OP_ATTENTION_GET BLE_MESH_MODEL_OP_2(0x80, 0x04) +#define OP_ATTENTION_SET BLE_MESH_MODEL_OP_2(0x80, 0x05) +#define OP_ATTENTION_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x06) +#define OP_ATTENTION_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x07) +#define OP_DEV_COMP_DATA_GET BLE_MESH_MODEL_OP_2(0x80, 0x08) +#define OP_BEACON_GET BLE_MESH_MODEL_OP_2(0x80, 0x09) +#define OP_BEACON_SET BLE_MESH_MODEL_OP_2(0x80, 0x0a) +#define OP_BEACON_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0b) +#define OP_DEFAULT_TTL_GET BLE_MESH_MODEL_OP_2(0x80, 0x0c) +#define OP_DEFAULT_TTL_SET BLE_MESH_MODEL_OP_2(0x80, 0x0d) +#define OP_DEFAULT_TTL_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x0e) +#define OP_FRIEND_GET BLE_MESH_MODEL_OP_2(0x80, 0x0f) +#define OP_FRIEND_SET BLE_MESH_MODEL_OP_2(0x80, 0x10) +#define OP_FRIEND_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x11) +#define OP_GATT_PROXY_GET BLE_MESH_MODEL_OP_2(0x80, 0x12) +#define OP_GATT_PROXY_SET BLE_MESH_MODEL_OP_2(0x80, 0x13) +#define OP_GATT_PROXY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x14) +#define OP_KRP_GET BLE_MESH_MODEL_OP_2(0x80, 0x15) +#define OP_KRP_SET BLE_MESH_MODEL_OP_2(0x80, 0x16) +#define OP_KRP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x17) +#define OP_MOD_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x18) +#define OP_MOD_PUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x19) +#define OP_MOD_PUB_VA_SET BLE_MESH_MODEL_OP_2(0x80, 0x1a) +#define OP_MOD_SUB_ADD BLE_MESH_MODEL_OP_2(0x80, 0x1b) +#define OP_MOD_SUB_DEL BLE_MESH_MODEL_OP_2(0x80, 0x1c) +#define OP_MOD_SUB_DEL_ALL BLE_MESH_MODEL_OP_2(0x80, 0x1d) +#define OP_MOD_SUB_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x1e) +#define OP_MOD_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x1f) +#define OP_MOD_SUB_VA_ADD BLE_MESH_MODEL_OP_2(0x80, 0x20) +#define OP_MOD_SUB_VA_DEL BLE_MESH_MODEL_OP_2(0x80, 0x21) +#define OP_MOD_SUB_VA_OVERWRITE BLE_MESH_MODEL_OP_2(0x80, 0x22) +#define OP_NET_TRANSMIT_GET BLE_MESH_MODEL_OP_2(0x80, 0x23) +#define OP_NET_TRANSMIT_SET BLE_MESH_MODEL_OP_2(0x80, 0x24) +#define OP_NET_TRANSMIT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x25) +#define OP_RELAY_GET BLE_MESH_MODEL_OP_2(0x80, 0x26) +#define OP_RELAY_SET BLE_MESH_MODEL_OP_2(0x80, 0x27) +#define OP_RELAY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x28) +#define OP_MOD_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x29) +#define OP_MOD_SUB_LIST BLE_MESH_MODEL_OP_2(0x80, 0x2a) +#define OP_MOD_SUB_GET_VND BLE_MESH_MODEL_OP_2(0x80, 0x2b) +#define OP_MOD_SUB_LIST_VND BLE_MESH_MODEL_OP_2(0x80, 0x2c) +#define OP_LPN_TIMEOUT_GET BLE_MESH_MODEL_OP_2(0x80, 0x2d) +#define OP_LPN_TIMEOUT_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x2e) +#define OP_HEALTH_FAULT_CLEAR BLE_MESH_MODEL_OP_2(0x80, 0x2f) +#define OP_HEALTH_FAULT_CLEAR_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x30) +#define OP_HEALTH_FAULT_GET BLE_MESH_MODEL_OP_2(0x80, 0x31) +#define OP_HEALTH_FAULT_TEST BLE_MESH_MODEL_OP_2(0x80, 0x32) +#define OP_HEALTH_FAULT_TEST_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x33) +#define OP_HEALTH_PERIOD_GET BLE_MESH_MODEL_OP_2(0x80, 0x34) +#define OP_HEALTH_PERIOD_SET BLE_MESH_MODEL_OP_2(0x80, 0x35) +#define OP_HEALTH_PERIOD_SET_UNREL BLE_MESH_MODEL_OP_2(0x80, 0x36) +#define OP_HEALTH_PERIOD_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x37) +#define OP_HEARTBEAT_PUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x38) +#define OP_HEARTBEAT_PUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x39) +#define OP_HEARTBEAT_SUB_GET BLE_MESH_MODEL_OP_2(0x80, 0x3a) +#define OP_HEARTBEAT_SUB_SET BLE_MESH_MODEL_OP_2(0x80, 0x3b) +#define OP_HEARTBEAT_SUB_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3c) +#define OP_MOD_APP_BIND BLE_MESH_MODEL_OP_2(0x80, 0x3d) +#define OP_MOD_APP_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x3e) +#define OP_MOD_APP_UNBIND BLE_MESH_MODEL_OP_2(0x80, 0x3f) +#define OP_NET_KEY_ADD BLE_MESH_MODEL_OP_2(0x80, 0x40) +#define OP_NET_KEY_DEL BLE_MESH_MODEL_OP_2(0x80, 0x41) +#define OP_NET_KEY_GET BLE_MESH_MODEL_OP_2(0x80, 0x42) +#define OP_NET_KEY_LIST BLE_MESH_MODEL_OP_2(0x80, 0x43) +#define OP_NET_KEY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x44) +#define OP_NET_KEY_UPDATE BLE_MESH_MODEL_OP_2(0x80, 0x45) +#define OP_NODE_IDENTITY_GET BLE_MESH_MODEL_OP_2(0x80, 0x46) +#define OP_NODE_IDENTITY_SET BLE_MESH_MODEL_OP_2(0x80, 0x47) +#define OP_NODE_IDENTITY_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x48) +#define OP_NODE_RESET BLE_MESH_MODEL_OP_2(0x80, 0x49) +#define OP_NODE_RESET_STATUS BLE_MESH_MODEL_OP_2(0x80, 0x4a) +#define OP_SIG_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4b) +#define OP_SIG_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4c) +#define OP_VND_MOD_APP_GET BLE_MESH_MODEL_OP_2(0x80, 0x4d) +#define OP_VND_MOD_APP_LIST BLE_MESH_MODEL_OP_2(0x80, 0x4e) + +#define STATUS_SUCCESS 0x00 +#define STATUS_INVALID_ADDRESS 0x01 +#define STATUS_INVALID_MODEL 0x02 +#define STATUS_INVALID_APPKEY 0x03 +#define STATUS_INVALID_NETKEY 0x04 +#define STATUS_INSUFF_RESOURCES 0x05 +#define STATUS_IDX_ALREADY_STORED 0x06 +#define STATUS_NVAL_PUB_PARAM 0x07 +#define STATUS_NOT_SUB_MOD 0x08 +#define STATUS_STORAGE_FAIL 0x09 +#define STATUS_FEAT_NOT_SUPP 0x0a +#define STATUS_CANNOT_UPDATE 0x0b +#define STATUS_CANNOT_REMOVE 0x0c +#define STATUS_CANNOT_BIND 0x0d +#define STATUS_TEMP_STATE_CHG_FAIL 0x0e +#define STATUS_CANNOT_SET 0x0f +#define STATUS_UNSPECIFIED 0x10 +#define STATUS_INVALID_BINDING 0x11 + +enum { + BLE_MESH_VA_CHANGED, /* Label information changed */ +}; + +struct label { + u16_t ref; + u16_t addr; + u8_t uuid[16]; + bt_mesh_atomic_t flags[1]; +}; + +int bt_mesh_cfg_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_srv_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_cfg_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_srv_deinit(struct bt_mesh_model *model, bool primary); + +int bt_mesh_cfg_cli_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_cfg_cli_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_health_cli_deinit(struct bt_mesh_model *model, bool primary); + +void bt_mesh_cfg_reset(void); + +void bt_mesh_heartbeat(u16_t src, u16_t dst, u8_t hops, u16_t feat); + +void bt_mesh_attention(struct bt_mesh_model *model, u8_t time); + +struct label *get_label(u16_t index); + +u8_t *bt_mesh_label_uuid_get(u16_t addr); + +struct bt_mesh_hb_pub *bt_mesh_hb_pub_get(void); +void bt_mesh_hb_pub_disable(void); +struct bt_mesh_cfg_srv *bt_mesh_cfg_get(void); + +u8_t bt_mesh_net_transmit_get(void); +u8_t bt_mesh_relay_get(void); +u8_t bt_mesh_friend_get(void); +u8_t bt_mesh_relay_retransmit_get(void); +u8_t bt_mesh_beacon_get(void); +u8_t bt_mesh_gatt_proxy_get(void); +u8_t bt_mesh_default_ttl_get(void); + +void bt_mesh_subnet_del(struct bt_mesh_subnet *sub, bool store); + +struct bt_mesh_app_key *bt_mesh_app_key_alloc(u16_t app_idx); +void bt_mesh_app_key_del(struct bt_mesh_app_key *key, bool store); + +static inline void key_idx_pack(struct net_buf_simple *buf, + u16_t idx1, u16_t idx2) +{ + net_buf_simple_add_le16(buf, idx1 | ((idx2 & 0x00f) << 12)); + net_buf_simple_add_u8(buf, idx2 >> 4); +} + +static inline void key_idx_unpack(struct net_buf_simple *buf, + u16_t *idx1, u16_t *idx2) +{ + *idx1 = sys_get_le16(&buf->data[0]) & 0xfff; + *idx2 = sys_get_le16(&buf->data[1]) >> 4; + net_buf_simple_pull(buf, 3); +} + +#ifdef __cplusplus +} +#endif + +#endif /* _FOUNDATION_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.c b/components/bt/esp_ble_mesh/mesh_core/friend.c new file mode 100644 index 000000000..0c714bc9b --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/friend.c @@ -0,0 +1,1717 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_FRIEND) + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "transport.h" +#include "access.h" +#include "friend.h" +#include "foundation.h" +#include "mesh_main.h" +#include "provisioner_main.h" + +#ifdef CONFIG_BLE_MESH_FRIEND + +/* We reserve one extra buffer for each friendship, since we need to be able + * to resend the last sent PDU, which sits separately outside of the queue. + */ +#define FRIEND_BUF_COUNT ((CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE + 1) * \ + CONFIG_BLE_MESH_FRIEND_LPN_COUNT) + +#define FRIEND_ADV(buf) CONTAINER_OF(BLE_MESH_ADV(buf), struct friend_adv, adv) + +/* PDUs from Friend to the LPN should only be transmitted once with the + * smallest possible interval (20ms). + */ +#define FRIEND_XMIT BLE_MESH_TRANSMIT(0, 20) + +struct friend_pdu_info { + u16_t src; + u16_t dst; + + u8_t seq[3]; + + u8_t ttl: 7, + ctl: 1; + + u32_t iv_index; +}; + +NET_BUF_POOL_FIXED_DEFINE(friend_buf_pool, FRIEND_BUF_COUNT, + BLE_MESH_ADV_DATA_SIZE, NULL); + +static struct friend_adv { + struct bt_mesh_adv adv; + u16_t app_idx; +} adv_pool[FRIEND_BUF_COUNT]; + +enum { + BLE_MESH_FRIENDSHIP_TERMINATE_ESTABLISH_FAIL, + BLE_MESH_FRIENDSHIP_TERMINATE_POLL_TIMEOUT, + BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_REQ, + BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_CLEAR, + BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE, +}; + +static void (*friend_cb)(bool establish, u16_t lpn_addr, u8_t reason); + +static bool friend_init = false; + +static struct bt_mesh_subnet *friend_subnet_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + sub = bt_mesh_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + sub = bt_mesh_provisioner_subnet_get(net_idx); + } + + return sub; +} + +static struct bt_mesh_adv *adv_alloc(int id) +{ + adv_pool[id].app_idx = BLE_MESH_KEY_UNUSED; + return &adv_pool[id].adv; +} + +static bool is_lpn_unicast(struct bt_mesh_friend *frnd, u16_t addr) +{ + if (frnd->lpn == BLE_MESH_ADDR_UNASSIGNED) { + return false; + } + + return (addr >= frnd->lpn && addr < (frnd->lpn + frnd->num_elem)); +} + +struct bt_mesh_friend *bt_mesh_friend_find(u16_t net_idx, u16_t lpn_addr, + bool valid, bool established) +{ + int i; + + BT_DBG("net_idx 0x%04x lpn_addr 0x%04x", net_idx, lpn_addr); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (valid && !frnd->valid) { + continue; + } + + if (established && !frnd->established) { + continue; + } + + if (net_idx != BLE_MESH_KEY_ANY && frnd->net_idx != net_idx) { + continue; + } + + if (is_lpn_unicast(frnd, lpn_addr)) { + return frnd; + } + } + + return NULL; +} + +static void purge_buffers(sys_slist_t *list) +{ + while (!sys_slist_is_empty(list)) { + struct net_buf *buf = NULL; + + buf = (void *)sys_slist_get_not_empty(list); + + buf->frags = NULL; + buf->flags &= ~NET_BUF_FRAGS; + + net_buf_unref(buf); + } +} + +/* Intentionally start a little bit late into the ReceiveWindow when + * it's large enough. This may improve reliability with some platforms, + * like the PTS, where the receiver might not have sufficiently compensated + * for internal latencies required to start scanning. + */ +static s32_t recv_delay(struct bt_mesh_friend *frnd) +{ +#if CONFIG_BLE_MESH_FRIEND_RECV_WIN > 50 + return (s32_t)frnd->recv_delay + (CONFIG_BLE_MESH_FRIEND_RECV_WIN / 5); +#else + return frnd->recv_delay; +#endif +} + +static void friend_clear(struct bt_mesh_friend *frnd, u8_t reason) +{ + int i; + + BT_DBG("LPN 0x%04x", frnd->lpn); + + k_delayed_work_cancel(&frnd->timer); + + if (frnd->established) { + if (friend_cb) { + friend_cb(false, frnd->lpn, reason); + } + } + + friend_cred_del(frnd->net_idx, frnd->lpn); + + if (frnd->last) { + /* Cancel the sending if necessary */ + if (frnd->pending_buf) { + bt_mesh_adv_buf_ref_debug(__func__, frnd->last, 2U, BLE_MESH_BUF_REF_EQUAL); + BLE_MESH_ADV(frnd->last)->busy = 0U; + } else { + bt_mesh_adv_buf_ref_debug(__func__, frnd->last, 1U, BLE_MESH_BUF_REF_EQUAL); + } + + net_buf_unref(frnd->last); + frnd->last = NULL; + } + + purge_buffers(&frnd->queue); + + for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { + struct bt_mesh_friend_seg *seg = &frnd->seg[i]; + + purge_buffers(&seg->queue); + seg->seg_count = 0U; + } + + frnd->valid = 0U; + frnd->established = 0U; + frnd->pending_buf = 0U; + frnd->fsn = 0U; + frnd->queue_size = 0U; + frnd->pending_req = 0U; + (void)memset(frnd->sub_list, 0, sizeof(frnd->sub_list)); +} + +void bt_mesh_friend_clear_net_idx(u16_t net_idx) +{ + int i; + + BT_DBG("net_idx 0x%04x", net_idx); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (frnd->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (net_idx == BLE_MESH_KEY_ANY || frnd->net_idx == net_idx) { + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE); + } + } +} + +void bt_mesh_friend_sec_update(u16_t net_idx) +{ + int i; + + BT_DBG("net_idx 0x%04x", net_idx); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (frnd->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (net_idx == BLE_MESH_KEY_ANY || frnd->net_idx == net_idx) { + frnd->sec_update = 1U; + } + } +} + +int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_clear *msg = (void *)buf->data; + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U, lpn_counter = 0U; + struct bt_mesh_net_tx tx = { + .sub = rx->sub, + .ctx = &rx->ctx, + .src = bt_mesh_primary_addr(), + .xmit = bt_mesh_net_transmit_get(), + }; + struct bt_mesh_ctl_friend_clear_confirm cfm = {0}; + + if (buf->len < sizeof(*msg)) { + BT_WARN("%s, Too short Friend Clear", __func__); + return -EINVAL; + } + + lpn_addr = sys_be16_to_cpu(msg->lpn_addr); + lpn_counter = sys_be16_to_cpu(msg->lpn_counter); + + BT_DBG("LPN addr 0x%04x counter 0x%04x", lpn_addr, lpn_counter); + + frnd = bt_mesh_friend_find(rx->sub->net_idx, lpn_addr, false, false); + if (!frnd) { + BT_WARN("%s, No matching LPN addr 0x%04x", __func__, lpn_addr); + return 0; + } + + /* A Friend Clear message is considered valid if the result of the + * subtraction of the value of the LPNCounter field of the Friend + * Request message (the one that initiated the friendship) from the + * value of the LPNCounter field of the Friend Clear message, modulo + * 65536, is in the range 0 to 255 inclusive. + */ + if (lpn_counter - frnd->lpn_counter > 255) { + BT_WARN("%s, LPN Counter out of range (old %u new %u)", + __func__, frnd->lpn_counter, lpn_counter); + return 0; + } + + tx.ctx->send_ttl = BLE_MESH_TTL_MAX; + + cfm.lpn_addr = msg->lpn_addr; + cfm.lpn_counter = msg->lpn_counter; + + bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR_CFM, &cfm, + sizeof(cfm), NULL, NULL); + + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_CLEAR); + + return 0; +} + +static void friend_sub_add(struct bt_mesh_friend *frnd, u16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { + if (frnd->sub_list[i] == BLE_MESH_ADDR_UNASSIGNED) { + frnd->sub_list[i] = addr; + return; + } + } + + BT_WARN("%s, No space in friend subscription list", __func__); +} + +static void friend_sub_rem(struct bt_mesh_friend *frnd, u16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { + if (frnd->sub_list[i] == addr) { + frnd->sub_list[i] = BLE_MESH_ADDR_UNASSIGNED; + return; + } + } +} + +static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd, + struct friend_pdu_info *info, + struct net_buf_simple *sdu) +{ + struct net_buf *buf = NULL; + + buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc, + BLE_MESH_ADV_DATA, + FRIEND_XMIT, K_NO_WAIT); + if (!buf) { + return NULL; + } + + net_buf_add_u8(buf, (info->iv_index & 1) << 7); /* Will be reset in encryption */ + + if (info->ctl) { + net_buf_add_u8(buf, info->ttl | 0x80); + } else { + net_buf_add_u8(buf, info->ttl); + } + + net_buf_add_mem(buf, info->seq, sizeof(info->seq)); + + net_buf_add_be16(buf, info->src); + net_buf_add_be16(buf, info->dst); + + net_buf_add_mem(buf, sdu->data, sdu->len); + + return buf; +} + +struct unseg_app_sdu_meta { + struct bt_mesh_net_rx net; + const u8_t *key; + struct bt_mesh_subnet *subnet; + bool is_dev_key; + u8_t aid; + u8_t *ad; +}; + +static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd, + struct net_buf *buf, + struct unseg_app_sdu_meta *meta) +{ + u16_t app_idx = FRIEND_ADV(buf)->app_idx; + u8_t role = 0U; + int err = 0; + + meta->subnet = friend_subnet_get(frnd->net_idx); + if (!meta->subnet) { + BT_ERR("Invalid subnet for unseg app sdu"); + return -EINVAL; + } + + role = (IS_ENABLED(CONFIG_BLE_MESH_NODE) && + bt_mesh_is_provisioned()) ? NODE : PROVISIONER; + + meta->is_dev_key = (app_idx == BLE_MESH_KEY_DEV); + bt_mesh_net_header_parse(&buf->b, &meta->net); + err = bt_mesh_app_key_get(meta->subnet, app_idx, &meta->key, + &meta->aid, role, meta->net.ctx.addr); + if (err) { + BT_ERR("Failed to get AppKey"); + return err; + } + + if (BLE_MESH_ADDR_IS_VIRTUAL(meta->net.ctx.recv_dst)) { + meta->ad = bt_mesh_label_uuid_get(meta->net.ctx.recv_dst); + if (!meta->ad) { + BT_ERR("Failed to get label uuid"); + return -ENOENT; + } + } else { + meta->ad = NULL; + } + + return 0; +} + +static int unseg_app_sdu_decrypt(struct bt_mesh_friend *frnd, + struct net_buf *buf, + const struct unseg_app_sdu_meta *meta) +{ + struct net_buf_simple sdu = {0}; + + net_buf_simple_clone(&buf->b, &sdu); + net_buf_simple_pull(&sdu, 10); + sdu.len -= 4; + + return bt_mesh_app_decrypt(meta->key, meta->is_dev_key, 0, &sdu, &sdu, + meta->ad, meta->net.ctx.addr, + meta->net.ctx.recv_dst, meta->net.seq, + BLE_MESH_NET_IVI_TX); +} + +static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd, + struct net_buf *buf, + const struct unseg_app_sdu_meta *meta) +{ + struct net_buf_simple sdu = {0}; + + net_buf_simple_clone(&buf->b, &sdu); + net_buf_simple_pull(&sdu, 10); + sdu.len -= 4; + + return bt_mesh_app_encrypt(meta->key, meta->is_dev_key, 0, &sdu, + meta->ad, meta->net.ctx.addr, + meta->net.ctx.recv_dst, bt_mesh.seq, + BLE_MESH_NET_IVI_TX); +} + +static int unseg_app_sdu_prepare(struct bt_mesh_friend *frnd, + struct net_buf *buf) +{ + struct unseg_app_sdu_meta meta = {0}; + int err = 0; + + if (FRIEND_ADV(buf)->app_idx == BLE_MESH_KEY_UNUSED) { + return 0; + } + + err = unseg_app_sdu_unpack(frnd, buf, &meta); + if (err) { + return err; + } + + /* No need to re-encrypt the message if the sequence number is + * unchanged. + */ + if (meta.net.seq == bt_mesh.seq) { + return 0; + } + + err = unseg_app_sdu_decrypt(frnd, buf, &meta); + if (err) { + BT_WARN("Decryption failed! %d", err); + return err; + } + + err = unseg_app_sdu_encrypt(frnd, buf, &meta); + if (err) { + BT_WARN("Re-encryption failed! %d", err); + } + + return err; +} + +static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf, + bool master_cred) +{ + struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx); + const u8_t *enc = NULL, *priv = NULL; + u32_t iv_index = 0U; + u16_t src = 0U; + u8_t nid = 0U; + int err = 0; + + if (!sub) { + BT_ERR("Invalid subnet to encrypt friend pdu"); + return -EINVAL; + } + + if (master_cred) { + enc = sub->keys[sub->kr_flag].enc; + priv = sub->keys[sub->kr_flag].privacy; + nid = sub->keys[sub->kr_flag].nid; + } else { + if (friend_cred_get(sub, frnd->lpn, &nid, &enc, &priv)) { + BT_ERR("friend_cred_get failed"); + return -ENOENT; + } + } + + src = sys_get_be16(&buf->data[5]); + + if (bt_mesh_elem_find(src)) { + u32_t seq; + + if (FRIEND_ADV(buf)->app_idx != BLE_MESH_KEY_UNUSED) { + err = unseg_app_sdu_prepare(frnd, buf); + if (err) { + return err; + } + } + + seq = bt_mesh_next_seq(); + sys_put_be24(seq, &buf->data[2]); + + iv_index = BLE_MESH_NET_IVI_TX; + FRIEND_ADV(buf)->app_idx = BLE_MESH_KEY_UNUSED; + } else { + u8_t ivi = (buf->data[0] >> 7); + iv_index = (bt_mesh.iv_index - ((bt_mesh.iv_index & 1) != ivi)); + } + + buf->data[0] = (nid | (iv_index & 1) << 7); + + if (bt_mesh_net_encrypt(enc, &buf->b, iv_index, false)) { + BT_ERR("Encrypting failed"); + return -EINVAL; + } + + if (bt_mesh_net_obfuscate(buf->data, iv_index, priv)) { + BT_ERR("Obfuscating failed"); + return -EINVAL; + } + + return 0; +} + +static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd, + u8_t ctl_op, + struct net_buf_simple *sdu) +{ + struct friend_pdu_info info = {0}; + + BT_DBG("LPN 0x%04x", frnd->lpn); + + net_buf_simple_push_u8(sdu, TRANS_CTL_HDR(ctl_op, 0)); + + info.src = bt_mesh_primary_addr(); + info.dst = frnd->lpn; + + info.ctl = 1U; + info.ttl = 0U; + + memset(info.seq, 0, sizeof(info.seq)); + + info.iv_index = BLE_MESH_NET_IVI_TX; + + return create_friend_pdu(frnd, &info, sdu); +} + +static struct net_buf *encode_update(struct bt_mesh_friend *frnd, u8_t md) +{ + struct bt_mesh_ctl_friend_update *upd = NULL; + NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*upd)); + struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx); + + __ASSERT_NO_MSG(sub != NULL); + + BT_DBG("lpn 0x%04x md 0x%02x", frnd->lpn, md); + + net_buf_simple_reserve(&sdu, 1); + + upd = net_buf_simple_add(&sdu, sizeof(*upd)); + upd->flags = bt_mesh_net_flags(sub); + upd->iv_index = sys_cpu_to_be32(bt_mesh.iv_index); + upd->md = md; + + return encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_UPDATE, &sdu); +} + +static void enqueue_sub_cfm(struct bt_mesh_friend *frnd, u8_t xact) +{ + struct bt_mesh_ctl_friend_sub_confirm *cfm = NULL; + NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*cfm)); + struct net_buf *buf = NULL; + + BT_DBG("lpn 0x%04x xact 0x%02x", frnd->lpn, xact); + + net_buf_simple_reserve(&sdu, 1); + + cfm = net_buf_simple_add(&sdu, sizeof(*cfm)); + cfm->xact = xact; + + buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_SUB_CFM, &sdu); + if (!buf) { + BT_ERR("%s, Unable to encode Subscription List Confirmation", __func__); + return; + } + + if (encrypt_friend_pdu(frnd, buf, false)) { + return; + } + + if (frnd->last) { + BT_DBG("Discarding last PDU"); + net_buf_unref(frnd->last); + } + + frnd->last = buf; + frnd->send_last = 1U; +} + +static void friend_recv_delay(struct bt_mesh_friend *frnd) +{ + frnd->pending_req = 1U; + k_delayed_work_submit(&frnd->timer, recv_delay(frnd)); + BT_INFO("Waiting RecvDelay of %d ms", recv_delay(frnd)); +} + +int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_friend *frnd = NULL; + u8_t xact = 0U; + + if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) { + BT_WARN("%s, Too short Friend Subscription Add", __func__); + return -EINVAL; + } + + frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true); + if (!frnd) { + BT_WARN("%s, No matching LPN addr 0x%04x", __func__, rx->ctx.addr); + return 0; + } + + if (frnd->pending_buf) { + BT_WARN("%s, Previous buffer not yet sent!", __func__); + return 0; + } + + friend_recv_delay(frnd); + + xact = net_buf_simple_pull_u8(buf); + + while (buf->len >= 2U) { + friend_sub_add(frnd, net_buf_simple_pull_be16(buf)); + } + + enqueue_sub_cfm(frnd, xact); + + return 0; +} + +int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_friend *frnd = NULL; + u8_t xact = 0U; + + if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) { + BT_WARN("%s, Too short Friend Subscription Remove", __func__); + return -EINVAL; + } + + frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true); + if (!frnd) { + BT_WARN("%s, No matching LPN addr 0x%04x", __func__, rx->ctx.addr); + return 0; + } + + if (frnd->pending_buf) { + BT_WARN("%s, Previous buffer not yet sent!", __func__); + return 0; + } + + friend_recv_delay(frnd); + + xact = net_buf_simple_pull_u8(buf); + + while (buf->len >= 2U) { + friend_sub_rem(frnd, net_buf_simple_pull_be16(buf)); + } + + enqueue_sub_cfm(frnd, xact); + + return 0; +} + +static void enqueue_buf(struct bt_mesh_friend *frnd, struct net_buf *buf) +{ + net_buf_slist_put(&frnd->queue, buf); + frnd->queue_size++; +} + +static void enqueue_update(struct bt_mesh_friend *frnd, u8_t md) +{ + struct net_buf *buf = NULL; + + buf = encode_update(frnd, md); + if (!buf) { + BT_ERR("%s, Unable to encode Friend Update", __func__); + return; + } + + frnd->sec_update = 0U; + enqueue_buf(frnd, buf); +} + +int bt_mesh_friend_poll(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_poll *msg = (void *)buf->data; + struct bt_mesh_friend *frnd = NULL; + + if (buf->len < sizeof(*msg)) { + BT_WARN("%s, Too short Friend Poll", __func__); + return -EINVAL; + } + + frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false); + if (!frnd) { + BT_WARN("%s, No matching LPN addr 0x%04x", __func__, rx->ctx.addr); + return 0; + } + + if (msg->fsn & ~1) { + BT_WARN("%s, Prohibited (non-zero) padding bits", __func__); + return -EINVAL; + } + + if (frnd->pending_buf) { + BT_WARN("%s, Previous buffer not yet sent!", __func__); + return 0; + } + + BT_DBG("msg->fsn %u frnd->fsn %u", (msg->fsn & 1), frnd->fsn); + + friend_recv_delay(frnd); + + if (!frnd->established) { + BT_INFO("Friendship established with 0x%04x", frnd->lpn); + frnd->established = 1U; + if (friend_cb) { + friend_cb(true, frnd->lpn, 0); + } + } + + if (msg->fsn == frnd->fsn && frnd->last) { + BT_DBG("Re-sending last PDU"); + frnd->send_last = 1U; + } else { + if (frnd->last) { + net_buf_unref(frnd->last); + frnd->last = NULL; + } + + frnd->fsn = msg->fsn; + + if (sys_slist_is_empty(&frnd->queue)) { + enqueue_update(frnd, 0); + BT_DBG("Enqueued Friend Update to empty queue"); + } + } + + return 0; +} + +static struct bt_mesh_friend *find_clear(u16_t prev_friend) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (frnd->clear.frnd == prev_friend) { + return frnd; + } + } + + return NULL; +} + +static void friend_clear_sent(int err, void *user_data) +{ + struct bt_mesh_friend *frnd = user_data; + + k_delayed_work_submit(&frnd->clear.timer, + K_SECONDS(frnd->clear.repeat_sec)); + frnd->clear.repeat_sec *= 2U; +} + +static const struct bt_mesh_send_cb clear_sent_cb = { + .end = friend_clear_sent, +}; + +static void send_friend_clear(struct bt_mesh_friend *frnd) +{ + struct bt_mesh_msg_ctx ctx = { + .net_idx = frnd->net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = frnd->clear.frnd, + .send_ttl = BLE_MESH_TTL_MAX, + }; + struct bt_mesh_net_tx tx = { + .sub = friend_subnet_get(frnd->net_idx), + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + .xmit = bt_mesh_net_transmit_get(), + }; + struct bt_mesh_ctl_friend_clear req = { + .lpn_addr = sys_cpu_to_be16(frnd->lpn), + .lpn_counter = sys_cpu_to_be16(frnd->lpn_counter), + }; + + BT_DBG("%s", __func__); + + if (!tx.sub) { + BT_ERR("Invalid subnet for Friend Clear"); + return; + } + + bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR, &req, + sizeof(req), &clear_sent_cb, frnd); +} + +static void clear_timeout(struct k_work *work) +{ + struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend, + clear.timer.work); + u32_t duration = 0U; + + BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); + + duration = k_uptime_get_32() - frnd->clear.start; + if (duration > 2 * frnd->poll_to) { + BT_DBG("Clear Procedure timer expired"); + frnd->clear.frnd = BLE_MESH_ADDR_UNASSIGNED; + return; + } + + send_friend_clear(frnd); +} + +static void clear_procedure_start(struct bt_mesh_friend *frnd) +{ + BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd); + + frnd->clear.start = k_uptime_get_32(); + frnd->clear.repeat_sec = 1U; + + send_friend_clear(frnd); +} + +int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_clear_confirm *msg = (void *)buf->data; + struct bt_mesh_friend *frnd = NULL; + u16_t lpn_addr = 0U, lpn_counter = 0U; + + BT_DBG("%s", __func__); + + if (buf->len < sizeof(*msg)) { + BT_WARN("%s, Too short Friend Clear Confirm", __func__); + return -EINVAL; + } + + frnd = find_clear(rx->ctx.addr); + if (!frnd) { + BT_WARN("%s, No pending clear procedure for 0x%02x", __func__, rx->ctx.addr); + return 0; + } + + lpn_addr = sys_be16_to_cpu(msg->lpn_addr); + if (lpn_addr != frnd->lpn) { + BT_WARN("%s, LPN address mismatch (0x%04x != 0x%04x)", + __func__, lpn_addr, frnd->lpn); + return 0; + } + + lpn_counter = sys_be16_to_cpu(msg->lpn_counter); + if (lpn_counter != frnd->lpn_counter) { + BT_WARN("%s, LPN counter mismatch (0x%04x != 0x%04x)", + __func__, lpn_counter, frnd->lpn_counter); + return 0; + } + + k_delayed_work_cancel(&frnd->clear.timer); + frnd->clear.frnd = BLE_MESH_ADDR_UNASSIGNED; + + return 0; +} + +static void enqueue_offer(struct bt_mesh_friend *frnd, s8_t rssi) +{ + struct bt_mesh_ctl_friend_offer *off = NULL; + NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*off)); + struct net_buf *buf = NULL; + + BT_DBG("%s", __func__); + + net_buf_simple_reserve(&sdu, 1); + + off = net_buf_simple_add(&sdu, sizeof(*off)); + + off->recv_win = CONFIG_BLE_MESH_FRIEND_RECV_WIN, + off->queue_size = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE, + off->sub_list_size = ARRAY_SIZE(frnd->sub_list), + off->rssi = rssi, + off->frnd_counter = sys_cpu_to_be16(frnd->counter); + + buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_OFFER, &sdu); + if (!buf) { + BT_ERR("%s, Unable to encode Friend Offer", __func__); + return; + } + + if (encrypt_friend_pdu(frnd, buf, true)) { + return; + } + + frnd->counter++; + + if (frnd->last) { + net_buf_unref(frnd->last); + } + + frnd->last = buf; + frnd->send_last = 1U; +} + +#define RECV_WIN CONFIG_BLE_MESH_FRIEND_RECV_WIN +#define RSSI_FACT(crit) (((crit) >> 5) & (u8_t)BIT_MASK(2)) +#define RECV_WIN_FACT(crit) (((crit) >> 3) & (u8_t)BIT_MASK(2)) +#define MIN_QUEUE_SIZE_LOG(crit) ((crit) & (u8_t)BIT_MASK(3)) +#define MIN_QUEUE_SIZE(crit) ((u32_t)BIT(MIN_QUEUE_SIZE_LOG(crit))) + +static s32_t offer_delay(struct bt_mesh_friend *frnd, s8_t rssi, u8_t crit) +{ + /* Scaling factors. The actual values are 1, 1.5, 2 & 2.5, but we + * want to avoid floating-point arithmetic. + */ + static const u8_t fact[] = { 10, 15, 20, 25 }; + s32_t delay = 0; + + BT_INFO("ReceiveWindowFactor %u ReceiveWindow %u RSSIFactor %u RSSI %d", + fact[RECV_WIN_FACT(crit)], RECV_WIN, + fact[RSSI_FACT(crit)], rssi); + + /* Delay = ReceiveWindowFactor * ReceiveWindow - RSSIFactor * RSSI */ + delay = (s32_t)fact[RECV_WIN_FACT(crit)] * RECV_WIN; + delay -= (s32_t)fact[RSSI_FACT(crit)] * rssi; + delay /= 10; + + BT_DBG("Local Delay calculated as %d ms", delay); + + if (delay < 100) { + return K_MSEC(100); + } + + return K_MSEC(delay); +} + +int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_req *msg = (void *)buf->data; + struct bt_mesh_friend *frnd = NULL; + u32_t poll_to = 0U; + int i; + + if (buf->len < sizeof(*msg)) { + BT_WARN("%s, Too short Friend Request", __func__); + return -EINVAL; + } + + if (msg->recv_delay <= 0x09) { + BT_WARN("%s, Prohibited ReceiveDelay (0x%02x)", __func__, msg->recv_delay); + return -EINVAL; + } + + poll_to = sys_get_be24(msg->poll_to); + + if (poll_to <= 0x000009 || poll_to >= 0x34bc00) { + BT_WARN("%s, Prohibited PollTimeout (0x%06x)", __func__, poll_to); + return -EINVAL; + } + + if (msg->num_elem == 0x00) { + BT_WARN("%s, Prohibited NumElements value (0x00)", __func__); + return -EINVAL; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr + msg->num_elem - 1)) { + BT_WARN("%s, LPN elements stretch outside of unicast range", __func__); + return -EINVAL; + } + + if (!MIN_QUEUE_SIZE_LOG(msg->criteria)) { + BT_WARN("%s, Prohibited Minimum Queue Size in Friend Request", __func__); + return -EINVAL; + } + + if (CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE < MIN_QUEUE_SIZE(msg->criteria)) { + BT_WARN("%s, We have a too small Friend Queue size (%u < %u)", + __func__, CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE, + MIN_QUEUE_SIZE(msg->criteria)); + return 0; + } + + frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false); + if (frnd) { + BT_WARN("%s, Existing LPN re-requesting Friendship", __func__); + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_REQ); + goto init_friend; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + if (!bt_mesh.frnd[i].valid) { + frnd = &bt_mesh.frnd[i]; + frnd->valid = 1U; + break; + } + } + + if (!frnd) { + BT_WARN("%s, No free Friend contexts for new LPN", __func__); + return -ENOMEM; + } + +init_friend: + frnd->lpn = rx->ctx.addr; + frnd->num_elem = msg->num_elem; + frnd->net_idx = rx->sub->net_idx; + frnd->recv_delay = msg->recv_delay; + frnd->poll_to = poll_to * 100U; + frnd->lpn_counter = sys_be16_to_cpu(msg->lpn_counter); + frnd->clear.frnd = sys_be16_to_cpu(msg->prev_addr); + + BT_INFO("LPN 0x%04x rssi %d recv_delay %u poll_to %ums", + frnd->lpn, rx->ctx.recv_rssi, frnd->recv_delay, frnd->poll_to); + + /** + * Spec says: + * After a friendship has been established, if the PreviousAddress field + * of the Friend Request message contains a valid unicast address that is + * not the Friend node’s own unicast address, then the Friend node shall + * begin sending Friend Clear messages to that unicast address. + */ + if (BLE_MESH_ADDR_IS_UNICAST(frnd->clear.frnd) && + !bt_mesh_elem_find(frnd->clear.frnd)) { + clear_procedure_start(frnd); + } + + k_delayed_work_submit(&frnd->timer, + offer_delay(frnd, rx->ctx.recv_rssi, + msg->criteria)); + + friend_cred_create(rx->sub, frnd->lpn, frnd->lpn_counter, + frnd->counter); + + enqueue_offer(frnd, rx->ctx.recv_rssi); + + return 0; +} + +static bool is_seg(struct bt_mesh_friend_seg *seg, u16_t src, u16_t seq_zero) +{ + struct net_buf *buf = (void *)sys_slist_peek_head(&seg->queue); + struct net_buf_simple_state state = {0}; + u16_t buf_seq_zero = 0U; + u16_t buf_src = 0U; + + if (!buf) { + return false; + } + + net_buf_simple_save(&buf->b, &state); + net_buf_skip(buf, 5); /* skip IVI, NID, CTL, TTL, SEQ */ + buf_src = net_buf_pull_be16(buf); + net_buf_skip(buf, 3); /* skip DST, OP/AID */ + buf_seq_zero = ((net_buf_pull_be16(buf) >> 2) & TRANS_SEQ_ZERO_MASK); + net_buf_simple_restore(&buf->b, &state); + + return ((src == buf_src) && (seq_zero == buf_seq_zero)); +} + +static struct bt_mesh_friend_seg *get_seg(struct bt_mesh_friend *frnd, + u16_t src, u16_t seq_zero, + u8_t seg_count) +{ + struct bt_mesh_friend_seg *unassigned = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { + struct bt_mesh_friend_seg *seg = &frnd->seg[i]; + + if (is_seg(seg, src, seq_zero)) { + return seg; + } + + if (!unassigned && !sys_slist_peek_head(&seg->queue)) { + unassigned = seg; + } + } + + if (unassigned) { + unassigned->seg_count = seg_count; + } + + return unassigned; +} + +static void enqueue_friend_pdu(struct bt_mesh_friend *frnd, + enum bt_mesh_friend_pdu_type type, + u16_t src, u8_t seg_count, + struct net_buf *buf) +{ + struct bt_mesh_friend_seg *seg = NULL; + + BT_DBG("type %u", type); + + if (type == BLE_MESH_FRIEND_PDU_SINGLE) { + if (frnd->sec_update) { + enqueue_update(frnd, 1); + } + + enqueue_buf(frnd, buf); + return; + } + + u16_t seq_zero = (((buf->data[10] << 8 | buf->data[11]) >> 2) & TRANS_SEQ_ZERO_MASK); + + seg = get_seg(frnd, src, seq_zero, seg_count); + if (!seg) { + BT_ERR("No free friend segment RX contexts for 0x%04x", src); + net_buf_unref(buf); + return; + } + + net_buf_slist_put(&seg->queue, buf); + + if (type == BLE_MESH_FRIEND_PDU_COMPLETE) { + if (frnd->sec_update) { + enqueue_update(frnd, 1); + } + + sys_slist_merge_slist(&frnd->queue, &seg->queue); + + frnd->queue_size += seg->seg_count; + seg->seg_count = 0U; + } else { + /* Mark the buffer as having more to come after it */ + buf->flags |= NET_BUF_FRAGS; + } +} + +static void buf_send_start(u16_t duration, int err, void *user_data) +{ + struct bt_mesh_friend *frnd = user_data; + + BT_DBG("err %d", err); + + frnd->pending_buf = 0U; + + /* Friend Offer doesn't follow the re-sending semantics */ + if (!frnd->established) { + net_buf_unref(frnd->last); + frnd->last = NULL; + } +} + +static void buf_send_end(int err, void *user_data) +{ + struct bt_mesh_friend *frnd = user_data; + + BT_DBG("err %d", err); + + if (frnd->pending_req) { + BT_WARN("Another request before previous completed sending"); + return; + } + + if (frnd->established) { + k_delayed_work_submit(&frnd->timer, frnd->poll_to); + BT_DBG("Waiting %u ms for next poll", frnd->poll_to); + } else { + /* Friend offer timeout is 1 second */ + k_delayed_work_submit(&frnd->timer, K_SECONDS(1)); + BT_DBG("Waiting for first poll"); + } +} + +static void friend_timeout(struct k_work *work) +{ + struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend, + timer.work); + static const struct bt_mesh_send_cb buf_sent_cb = { + .start = buf_send_start, + .end = buf_send_end, + }; + + __ASSERT_NO_MSG(frnd->pending_buf == 0U); + + BT_DBG("lpn 0x%04x send_last %u last %p", frnd->lpn, + frnd->send_last, frnd->last); + + if (frnd->send_last && frnd->last) { + BT_DBG("Sending frnd->last %p", frnd->last); + frnd->send_last = 0U; + goto send_last; + } + + if (frnd->established && !frnd->pending_req) { + BT_WARN("%s, Friendship lost with 0x%04x", __func__, frnd->lpn); + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_POLL_TIMEOUT); + return; + } + + frnd->last = (void *)sys_slist_get(&frnd->queue); + if (!frnd->last) { + BT_WARN("%s, Friendship not established with 0x%04x", __func__, frnd->lpn); + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_ESTABLISH_FAIL); + return; + } + + if (encrypt_friend_pdu(frnd, frnd->last, false)) { + return; + } + + /* Clear the flag we use for segment tracking */ + frnd->last->flags &= ~NET_BUF_FRAGS; + frnd->last->frags = NULL; + + BT_DBG("Sending buf %p from Friend Queue of LPN 0x%04x", + frnd->last, frnd->lpn); + frnd->queue_size--; + +send_last: + frnd->pending_req = 0U; + frnd->pending_buf = 1U; + bt_mesh_adv_send(frnd->last, &buf_sent_cb, frnd); +} + +void bt_mesh_friend_set_cb(void (*cb)(bool establish, u16_t lpn_addr, u8_t reason)) +{ + friend_cb = cb; +} + +int bt_mesh_friend_init(void) +{ + int i; + + if (friend_init == true) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + int j; + + frnd->net_idx = BLE_MESH_KEY_UNUSED; + + sys_slist_init(&frnd->queue); + + k_delayed_work_init(&frnd->timer, friend_timeout); + k_delayed_work_init(&frnd->clear.timer, clear_timeout); + + for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) { + sys_slist_init(&frnd->seg[j].queue); + } + } + + friend_init = true; + + return 0; +} + +int bt_mesh_friend_deinit(void) +{ + int i; + + if (friend_init == false) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + frnd->net_idx = BLE_MESH_KEY_UNUSED; + + k_delayed_work_free(&frnd->timer); + k_delayed_work_free(&frnd->clear.timer); + } + + bt_mesh_unref_buf_from_pool(&friend_buf_pool); + memset(adv_pool, 0, sizeof(adv_pool)); + + friend_init = false; + + return 0; +} + +static bool is_segack(struct net_buf *buf, const u64_t *seqauth, u16_t src) +{ + struct net_buf_simple_state state = {0}; + bool found = false; + + if (buf->len != 16) { + return false; + } + + net_buf_simple_save(&buf->b, &state); + + net_buf_skip(buf, 1); /* skip IVI, NID */ + + if (!(net_buf_pull_u8(buf) >> 7)) { + goto end; + } + + net_buf_pull(buf, 3); /* skip SEQNUM */ + + if (src != net_buf_pull_be16(buf)) { + goto end; + } + + net_buf_skip(buf, 2); /* skip dst */ + + if (TRANS_CTL_OP((u8_t *) net_buf_pull_mem(buf, 1)) != TRANS_CTL_OP_ACK) { + goto end; + } + + found = ((net_buf_pull_be16(buf) >> 2) & TRANS_SEQ_ZERO_MASK) == + (*seqauth & TRANS_SEQ_ZERO_MASK); +end: + net_buf_simple_restore(&buf->b, &state); + return found; +} + +static void friend_purge_old_ack(struct bt_mesh_friend *frnd, + const u64_t *seq_auth, u16_t src) +{ + sys_snode_t *cur = NULL, *prev = NULL; + + BT_DBG("SeqAuth %llx src 0x%04x", *seq_auth, src); + + for (cur = sys_slist_peek_head(&frnd->queue); + cur != NULL; prev = cur, cur = sys_slist_peek_next(cur)) { + struct net_buf *buf = (void *)cur; + + if (is_segack(buf, seq_auth, src)) { + BT_DBG("Removing old ack from Friend Queue"); + + sys_slist_remove(&frnd->queue, prev, cur); + frnd->queue_size--; + /* Make sure old slist entry state doesn't remain */ + buf->frags = NULL; + + net_buf_unref(buf); + break; + } + } +} + +static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd, + struct bt_mesh_net_rx *rx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) +{ + struct friend_pdu_info info = {0}; + struct net_buf *buf = NULL; + + /* Because of network loopback, tx packets will also be passed into + * this rx function. These packets have already been added to the + * queue, and should be ignored. + */ + if (bt_mesh_elem_find(rx->ctx.addr)) { + return; + } + + BT_DBG("LPN 0x%04x queue_size %u", frnd->lpn, frnd->queue_size); + + if (type == BLE_MESH_FRIEND_PDU_SINGLE && seq_auth) { + friend_purge_old_ack(frnd, seq_auth, rx->ctx.addr); + } + + info.src = rx->ctx.addr; + info.dst = rx->ctx.recv_dst; + + if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { + info.ttl = rx->ctx.recv_ttl; + } else { + info.ttl = rx->ctx.recv_ttl - 1U; + } + + info.ctl = rx->ctl; + + sys_put_be24(rx->seq, info.seq); + + info.iv_index = BLE_MESH_NET_IVI_RX(rx); + + buf = create_friend_pdu(frnd, &info, sbuf); + if (!buf) { + BT_ERR("%s, Failed to encode Friend buffer", __func__); + return; + } + + enqueue_friend_pdu(frnd, type, info.src, seg_count, buf); + + BT_DBG("Queued message for LPN 0x%04x, queue_size %u", + frnd->lpn, frnd->queue_size); +} + +static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd, + struct bt_mesh_net_tx *tx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) +{ + struct friend_pdu_info info = {0}; + struct net_buf *buf = NULL; + + BT_DBG("LPN 0x%04x", frnd->lpn); + + if (type == BLE_MESH_FRIEND_PDU_SINGLE && seq_auth) { + friend_purge_old_ack(frnd, seq_auth, tx->src); + } + + info.src = tx->src; + info.dst = tx->ctx->addr; + + info.ttl = tx->ctx->send_ttl; + info.ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED); + + sys_put_be24(bt_mesh.seq, info.seq); + + info.iv_index = BLE_MESH_NET_IVI_TX; + + buf = create_friend_pdu(frnd, &info, sbuf); + if (!buf) { + BT_ERR("%s, Failed to encode Friend buffer", __func__); + return; + } + + if (type == BLE_MESH_FRIEND_PDU_SINGLE && !info.ctl) { + /* Unsegmented application packets may be re-encrypted later, + * as they depend on the the sequence number being the same + * when encrypting in transport and network. + */ + FRIEND_ADV(buf)->app_idx = tx->ctx->app_idx; + } + + enqueue_friend_pdu(frnd, type, info.src, seg_count, buf); + + BT_DBG("Queued message for LPN 0x%04x", frnd->lpn); +} + +static bool friend_lpn_matches(struct bt_mesh_friend *frnd, u16_t net_idx, + u16_t addr) +{ + int i; + + if (!frnd->established) { + return false; + } + + if (net_idx != frnd->net_idx) { + return false; + } + + if (BLE_MESH_ADDR_IS_UNICAST(addr)) { + return is_lpn_unicast(frnd, addr); + } + + for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) { + if (frnd->sub_list[i] == addr) { + return true; + } + } + + return false; +} + +bool bt_mesh_friend_match(u16_t net_idx, u16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (friend_lpn_matches(frnd, net_idx, addr)) { + BT_DBG("LPN 0x%04x matched address 0x%04x", + frnd->lpn, addr); + return true; + } + } + + BT_DBG("No matching LPN for address 0x%04x", addr); + + return false; +} + +static bool friend_queue_has_space(struct bt_mesh_friend *frnd, u16_t addr, + const u64_t *seq_auth, u8_t seg_count) +{ + u32_t total = 0U; + int i; + + if (seg_count > CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE) { + return false; + } + + for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) { + struct bt_mesh_friend_seg *seg = &frnd->seg[i]; + + if (seq_auth && is_seg(seg, addr, *seq_auth & TRANS_SEQ_ZERO_MASK)) { + /* If there's a segment queue for this message then the + * space verification has already happened. + */ + return true; + } + + total += seg->seg_count; + } + + /* If currently pending segments combined with this segmented message + * are more than the Friend Queue Size, then there's no space. This + * is because we don't have a mechanism of aborting already pending + * segmented messages to free up buffers. + */ + return (CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - total) > seg_count; +} + +bool bt_mesh_friend_queue_has_space(u16_t net_idx, u16_t src, u16_t dst, + const u64_t *seq_auth, u8_t seg_count) +{ + bool someone_has_space = false, friend_match = false; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (!friend_lpn_matches(frnd, net_idx, dst)) { + continue; + } + + friend_match = true; + + if (friend_queue_has_space(frnd, src, seq_auth, seg_count)) { + someone_has_space = true; + } + } + + /* If there were no matched LPNs treat this as success, so the + * transport layer can continue its work. + */ + if (!friend_match) { + return true; + } + + /* From the transport layers perspective it's good enough that at + * least one Friend Queue has space. If there were multiple Friend + * matches then the destination must be a group address, in which + * case e.g. segment acks are not sent. + */ + return someone_has_space; +} + +static bool friend_queue_prepare_space(struct bt_mesh_friend *frnd, u16_t addr, + const u64_t *seq_auth, u8_t seg_count) +{ + bool pending_segments = false; + u8_t avail_space = 0U; + + if (!friend_queue_has_space(frnd, addr, seq_auth, seg_count)) { + return false; + } + + avail_space = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - frnd->queue_size; + pending_segments = false; + + while (pending_segments || avail_space < seg_count) { + struct net_buf *buf = (void *)sys_slist_get(&frnd->queue); + + if (!buf) { + BT_ERR("Unable to free up enough buffers"); + return false; + } + + frnd->queue_size--; + avail_space++; + + pending_segments = (buf->flags & NET_BUF_FRAGS); + + /* Make sure old slist entry state doesn't remain */ + buf->frags = NULL; + buf->flags &= ~NET_BUF_FRAGS; + + net_buf_unref(buf); + } + + return true; +} + +void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) +{ + int i; + + if (!rx->friend_match || + (rx->ctx.recv_ttl <= 1U && rx->net_if != BLE_MESH_NET_IF_LOCAL) || + bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { + return; + } + + BT_DBG("recv_ttl %u net_idx 0x%04x src 0x%04x dst 0x%04x", + rx->ctx.recv_ttl, rx->sub->net_idx, rx->ctx.addr, + rx->ctx.recv_dst); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (!friend_lpn_matches(frnd, rx->sub->net_idx, + rx->ctx.recv_dst)) { + continue; + } + + if (!friend_queue_prepare_space(frnd, rx->ctx.addr, seq_auth, + seg_count)) { + continue; + } + + friend_lpn_enqueue_rx(frnd, rx, type, seq_auth, seg_count, + sbuf); + } +} + +bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf) +{ + bool matched = false; + int i; + + if (!bt_mesh_friend_match(tx->sub->net_idx, tx->ctx->addr) || + bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) { + return matched; + } + + BT_DBG("net_idx 0x%04x dst 0x%04x src 0x%04x", tx->sub->net_idx, + tx->ctx->addr, tx->src); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + + if (!friend_lpn_matches(frnd, tx->sub->net_idx, + tx->ctx->addr)) { + continue; + } + + if (!friend_queue_prepare_space(frnd, tx->src, seq_auth, + seg_count)) { + continue; + } + + friend_lpn_enqueue_tx(frnd, tx, type, seq_auth, seg_count, + sbuf); + matched = true; + } + + return matched; +} + +void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, u16_t src, + u16_t dst, const u64_t *seq_auth) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) { + struct bt_mesh_friend *frnd = &bt_mesh.frnd[i]; + int j; + + if (!friend_lpn_matches(frnd, sub->net_idx, dst)) { + continue; + } + + for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) { + struct bt_mesh_friend_seg *seg = &frnd->seg[j]; + + if (!is_seg(seg, src, *seq_auth & TRANS_SEQ_ZERO_MASK)) { + continue; + } + + BT_WARN("%s, Clearing incomplete segments for 0x%04x", __func__, src); + + purge_buffers(&seg->queue); + seg->seg_count = 0U; + break; + } + } +} + +void bt_mesh_friend_remove_lpn(u16_t lpn_addr) +{ + struct bt_mesh_friend *frnd = NULL; + + frnd = bt_mesh_friend_find(BLE_MESH_KEY_ANY, lpn_addr, false, false); + if (frnd) { + friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE); + } +} + +#endif /* CONFIG_BLE_MESH_FRIEND */ diff --git a/components/bt/esp_ble_mesh/mesh_core/friend.h b/components/bt/esp_ble_mesh/mesh_core/friend.h new file mode 100644 index 000000000..1f47c1bd5 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/friend.h @@ -0,0 +1,67 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _FRIEND_H_ +#define _FRIEND_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum bt_mesh_friend_pdu_type { + BLE_MESH_FRIEND_PDU_SINGLE, + BLE_MESH_FRIEND_PDU_PARTIAL, + BLE_MESH_FRIEND_PDU_COMPLETE, +}; + +bool bt_mesh_friend_match(u16_t net_idx, u16_t addr); + +struct bt_mesh_friend *bt_mesh_friend_find(u16_t net_idx, u16_t lpn_addr, + bool valid, bool established); + +bool bt_mesh_friend_queue_has_space(u16_t net_idx, u16_t src, u16_t dst, + const u64_t *seq_auth, u8_t seg_count); + +void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf); +bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx, + enum bt_mesh_friend_pdu_type type, + const u64_t *seq_auth, u8_t seg_count, + struct net_buf_simple *sbuf); + +void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, u16_t src, + u16_t dst, const u64_t *seq_auth); + +void bt_mesh_friend_sec_update(u16_t net_idx); + +void bt_mesh_friend_clear_net_idx(u16_t net_idx); + +int bt_mesh_friend_poll(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); +int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); +int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); +int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); +int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); +int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); + +int bt_mesh_friend_init(void); +int bt_mesh_friend_deinit(void); + +void bt_mesh_friend_remove_lpn(u16_t lpn_addr); + +#ifdef __cplusplus +} +#endif + +#endif /* _FRIEND_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/health_cli.c b/components/bt/esp_ble_mesh/mesh_core/health_cli.c new file mode 100644 index 000000000..bceb45047 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/health_cli.c @@ -0,0 +1,516 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) + +#include "btc_ble_mesh_health_model.h" + +#include "foundation.h" +#include "mesh_common.h" +#include "health_cli.h" + +s32_t health_msg_timeout; + +static bt_mesh_health_client_t *health_cli; + +static const bt_mesh_client_op_pair_t health_op_pair[] = { + { OP_HEALTH_FAULT_GET, OP_HEALTH_FAULT_STATUS }, + { OP_HEALTH_FAULT_CLEAR, OP_HEALTH_FAULT_STATUS }, + { OP_HEALTH_FAULT_TEST, OP_HEALTH_FAULT_STATUS }, + { OP_HEALTH_PERIOD_GET, OP_HEALTH_PERIOD_STATUS }, + { OP_HEALTH_PERIOD_SET, OP_HEALTH_PERIOD_STATUS }, + { OP_ATTENTION_GET, OP_ATTENTION_STATUS }, + { OP_ATTENTION_SET, OP_ATTENTION_STATUS }, +}; + +static bt_mesh_mutex_t health_client_lock; + +static void bt_mesh_health_client_mutex_new(void) +{ + if (!health_client_lock.mutex) { + bt_mesh_mutex_create(&health_client_lock); + } +} + +static void bt_mesh_health_client_mutex_free(void) +{ + bt_mesh_mutex_free(&health_client_lock); +} + +static void bt_mesh_health_client_lock(void) +{ + bt_mesh_mutex_lock(&health_client_lock); +} + +static void bt_mesh_health_client_unlock(void) +{ + bt_mesh_mutex_unlock(&health_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive health status message timeout"); + + bt_mesh_health_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_health_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_HEALTH_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_health_client_unlock(); + + return; +} + +static void health_client_cancel(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + void *status, size_t len) +{ + bt_mesh_client_node_t *node = NULL; + struct net_buf_simple buf = {0}; + u8_t evt_type = 0xFF; + + if (!model || !ctx || !status || !len) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + /* If it is a publish message, sent to the user directly. */ + buf.data = (u8_t *)status; + buf.len = (u16_t)len; + + bt_mesh_health_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, &buf, true); + if (!node) { + BT_DBG("Unexpected health status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case OP_HEALTH_FAULT_GET: + case OP_HEALTH_PERIOD_GET: + case OP_ATTENTION_GET: + evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_GET_STATE; + break; + case OP_HEALTH_FAULT_CLEAR: + case OP_HEALTH_FAULT_TEST: + case OP_HEALTH_PERIOD_SET: + case OP_ATTENTION_SET: + evt_type = BTC_BLE_MESH_EVT_HEALTH_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_health_client_cb_evt_to_btc( + opcode, evt_type, model, ctx, (const u8_t *)status, len); + } + } + + bt_mesh_health_client_unlock(); + + switch (ctx->recv_op) { + case OP_HEALTH_FAULT_STATUS: { + struct bt_mesh_health_fault_status *val; + val = (struct bt_mesh_health_fault_status *)status; + bt_mesh_free_buf(val->fault_array); + break; + } + default: + break; + } +} + +static void health_fault_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_health_fault_status status = {0}; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status.test_id = net_buf_simple_pull_u8(buf); + status.cid = net_buf_simple_pull_le16(buf); + status.fault_array = bt_mesh_alloc_buf(buf->len); + if (!status.fault_array) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + net_buf_simple_add_mem(status.fault_array, buf->data, buf->len); + + health_client_cancel(model, ctx, &status, sizeof(struct bt_mesh_health_fault_status)); +} + +static void health_current_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + bt_mesh_client_node_t *node = NULL; + u8_t test_id = 0U; + u16_t cid = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + /* Health current status is a publish message, sent to the user directly. */ + if (!(node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true))) { + return; + } + + test_id = net_buf_simple_pull_u8(buf); + cid = net_buf_simple_pull_le16(buf); + + BT_DBG("Test ID 0x%02x Company ID 0x%04x Fault Count %u", + test_id, cid, buf->len); + + ((void) test_id); + ((void) cid); +} + +static void health_period_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t status = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status = net_buf_simple_pull_u8(buf); + + health_client_cancel(model, ctx, &status, sizeof(u8_t)); +} + +static void health_attention_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t status = 0U; + + BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s", + ctx->net_idx, ctx->app_idx, ctx->addr, buf->len, + bt_hex(buf->data, buf->len)); + + status = net_buf_simple_pull_u8(buf); + + health_client_cancel(model, ctx, &status, sizeof(u8_t)); +} + +const struct bt_mesh_model_op bt_mesh_health_cli_op[] = { + { OP_HEALTH_FAULT_STATUS, 3, health_fault_status }, + { OP_HEALTH_CURRENT_STATUS, 3, health_current_status }, + { OP_HEALTH_PERIOD_STATUS, 1, health_period_status }, + { OP_ATTENTION_STATUS, 1, health_attention_status }, + BLE_MESH_MODEL_OP_END, +}; + +int bt_mesh_health_attention_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_ATTENTION_GET); + + err = bt_mesh_client_send_msg(health_cli->model, OP_ATTENTION_GET, ctx, + &msg, timeout_handler, health_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, + u8_t attention, bool need_ack) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_SET, 1); + u32_t opcode = 0U; + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + if (need_ack) { + opcode = OP_ATTENTION_SET; + } else { + opcode = OP_ATTENTION_SET_UNREL; + } + bt_mesh_model_msg_init(&msg, opcode); + net_buf_simple_add_u8(&msg, attention); + + err = bt_mesh_client_send_msg(health_cli->model, opcode, ctx, &msg, + timeout_handler, health_msg_timeout, + need_ack, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_period_get(struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_GET, 0); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEALTH_PERIOD_GET); + + err = bt_mesh_client_send_msg(health_cli->model, OP_HEALTH_PERIOD_GET, + ctx, &msg, timeout_handler, health_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_period_set(struct bt_mesh_msg_ctx *ctx, + u8_t divisor, bool need_ack) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_SET, 1); + u32_t opcode = 0U; + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + if (need_ack) { + opcode = OP_HEALTH_PERIOD_SET; + } else { + opcode = OP_HEALTH_PERIOD_SET_UNREL; + } + bt_mesh_model_msg_init(&msg, opcode); + net_buf_simple_add_u8(&msg, divisor); + + err = bt_mesh_client_send_msg(health_cli->model, opcode, ctx, &msg, + timeout_handler, health_msg_timeout, + need_ack, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_fault_test(struct bt_mesh_msg_ctx *ctx, + u16_t cid, u8_t test_id, bool need_ack) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_TEST, 3); + u32_t opcode = 0U; + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + if (need_ack) { + opcode = OP_HEALTH_FAULT_TEST; + } else { + opcode = OP_HEALTH_FAULT_TEST_UNREL; + } + bt_mesh_model_msg_init(&msg, opcode); + net_buf_simple_add_u8(&msg, test_id); + net_buf_simple_add_le16(&msg, cid); + + err = bt_mesh_client_send_msg(health_cli->model, opcode, ctx, &msg, + timeout_handler, health_msg_timeout, + need_ack, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, + u16_t cid, bool need_ack) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_CLEAR, 2); + u32_t opcode = 0U; + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + if (need_ack) { + opcode = OP_HEALTH_FAULT_CLEAR; + } else { + opcode = OP_HEALTH_FAULT_CLEAR_UNREL; + } + bt_mesh_model_msg_init(&msg, opcode); + net_buf_simple_add_le16(&msg, cid); + + err = bt_mesh_client_send_msg(health_cli->model, opcode, ctx, &msg, + timeout_handler, health_msg_timeout, + need_ack, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +int bt_mesh_health_fault_get(struct bt_mesh_msg_ctx *ctx, u16_t cid) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_FAULT_GET, 2); + int err = 0; + + if (!ctx || !ctx->addr) { + return -EINVAL; + } + + bt_mesh_model_msg_init(&msg, OP_HEALTH_FAULT_GET); + net_buf_simple_add_le16(&msg, cid); + + err = bt_mesh_client_send_msg(health_cli->model, OP_HEALTH_FAULT_GET, ctx, + &msg, timeout_handler, health_msg_timeout, + true, NULL, NULL); + if (err) { + BT_ERR("%s, send failed (err %d)", __func__, err); + } + + return err; +} + +s32_t bt_mesh_health_cli_timeout_get(void) +{ + return health_msg_timeout; +} + +void bt_mesh_health_cli_timeout_set(s32_t timeout) +{ + health_msg_timeout = timeout; +} + +int bt_mesh_health_cli_set(struct bt_mesh_model *model) +{ + if (!model || !model->user_data) { + BT_ERR("%s, No Health Client context for given model", __func__); + return -EINVAL; + } + + health_cli = model->user_data; + + return 0; +} + +int bt_mesh_health_cli_init(struct bt_mesh_model *model, bool primary) +{ + health_internal_data_t *internal = NULL; + bt_mesh_health_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_health_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, No Health Client context provided", __func__); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(health_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(health_op_pair); + client->op_pair = health_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + bt_mesh_health_client_mutex_new(); + + /* Set the default health client pointer */ + if (!health_cli) { + health_cli = client; + } + + return 0; +} + +int bt_mesh_health_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_health_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_health_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, No Health Client context provided", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_health_client_mutex_free(); + + if (health_cli) { + health_cli = NULL; + } + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/health_srv.c b/components/bt/esp_ble_mesh/mesh_core/health_srv.c new file mode 100644 index 000000000..ccf18b368 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/health_srv.c @@ -0,0 +1,550 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_MODEL) + +#include "btc_ble_mesh_health_model.h" + +#include "access.h" +#include "foundation.h" +#include "mesh_common.h" +#include "health_srv.h" + +#define HEALTH_TEST_STANDARD 0x00 + +#define HEALTH_NO_FAULT 0x00 + +/* Health Server context of the primary element */ +struct bt_mesh_health_srv *health_srv; + +/** + * When an Element receives a Health Fault Get, or a Health Fault Test, or + * a Health Fault Test Unacknowledged, or a Health Fault Clear, or a Health + * Fault Clear Unacknowledged message that is not successfully processed + * (i.e. the Company ID field that does not identify any Health Fault state + * present in the node), it shall ignore the message. + * The Health Fault state is identified by Company ID and may be present in + * the node for more than one Company ID. + */ + +static u8_t health_get_curr_fault_count(struct bt_mesh_model *model) +{ + struct bt_mesh_health_srv *srv = model->user_data; + u8_t count = 0U; + size_t i = 0U; + + for (i = 0U; i < ARRAY_SIZE(srv->test.curr_faults); i++) { + if (srv->test.curr_faults[i] != HEALTH_NO_FAULT) { + count++; + } + } + + return count; +} + +static void health_get_fault_value(struct bt_mesh_model *model, + struct net_buf_simple *msg, + bool current) +{ + struct bt_mesh_health_srv *srv = model->user_data; + size_t array_size = 0U; + size_t i = 0U; + + array_size = current ? ARRAY_SIZE(srv->test.curr_faults) : ARRAY_SIZE(srv->test.reg_faults); + + for (i = 0U; i < array_size; i++) { + if (net_buf_simple_tailroom(msg) == 0) { + return; + } + + u8_t fault = current ? srv->test.curr_faults[i] : srv->test.reg_faults[i]; + if (fault != HEALTH_NO_FAULT) { + net_buf_simple_add_u8(msg, fault); + } + } +} + +static bool health_is_test_id_exist(struct bt_mesh_model *model, u8_t test_id) +{ + struct bt_mesh_health_srv *srv = model->user_data; + int i; + + for (i = 0; i < srv->test.id_count; i++) { + if (srv->test.test_ids[i] == test_id) { + return true; + } + } + + return false; +} + +static int health_send_fault_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + struct bt_mesh_health_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + int err = 0; + + msg = bt_mesh_alloc_buf(4 + ARRAY_SIZE(srv->test.reg_faults) + 4); + if (!msg) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + bt_mesh_model_msg_init(msg, OP_HEALTH_FAULT_STATUS); + net_buf_simple_add_u8(msg, srv->test.prev_test_id); + net_buf_simple_add_le16(msg, srv->test.company_id); + if (ctx->recv_op != OP_HEALTH_FAULT_CLEAR) { + /** + * For Health Fault Clear, the FaultArray field in Health Fault Status + * shall be empty. + */ + health_get_fault_value(model, msg, false); + } + + err = bt_mesh_model_send(model, ctx, msg, NULL, NULL); + if (err) { + BT_ERR("%s, Failed to send Health Fault Status response", __func__); + } + + bt_mesh_free_buf(msg); + return err; +} + +static void health_fault_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_health_srv *srv = model->user_data; + u16_t company_id = 0U; + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return; + } + + company_id = net_buf_simple_pull_le16(buf); + if (company_id != srv->test.company_id) { + BT_ERR("%s, Unknown Company ID 0x%04x", __func__, company_id); + return; + } + + BT_DBG("company_id 0x%04x", company_id); + + health_send_fault_status(model, ctx); +} + +static void health_fault_clear(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_health_srv *srv = model->user_data; + u16_t company_id = 0U; + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return; + } + + company_id = net_buf_simple_pull_le16(buf); + if (company_id != srv->test.company_id) { + BT_ERR("%s, Unknown Company ID 0x%04x", __func__, company_id); + return; + } + + BT_DBG("company_id 0x%04x", company_id); + + memset(srv->test.reg_faults, HEALTH_NO_FAULT, ARRAY_SIZE(srv->test.reg_faults)); + + if (srv->cb.fault_clear) { + srv->cb.fault_clear(model, company_id); + } + + if (ctx->recv_op == OP_HEALTH_FAULT_CLEAR) { + health_send_fault_status(model, ctx); + } +} + +static void health_fault_test(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_health_srv *srv = model->user_data; + u16_t company_id = 0U; + u8_t test_id = 0U; + + BT_DBG("%s", __func__); + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return; + } + + test_id = net_buf_simple_pull_u8(buf); + if (health_is_test_id_exist(model, test_id) == false) { + BT_ERR("%s, Unknown Test ID 0x%02x", __func__, test_id); + return; + } + + company_id = net_buf_simple_pull_le16(buf); + if (company_id != srv->test.company_id) { + BT_ERR("%s, Unknown Company ID 0x%04x", __func__, company_id); + return; + } + + BT_DBG("test 0x%02x company 0x%04x", test_id, company_id); + + srv->test.prev_test_id = test_id; + + if (srv->cb.fault_test) { + srv->cb.fault_test(model, test_id, company_id); + } + + if (ctx->recv_op == OP_HEALTH_FAULT_TEST) { + health_send_fault_status(model, ctx); + } +} + +static void send_attention_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_ATTENTION_STATUS, 1); + struct bt_mesh_health_srv *srv = model->user_data; + u8_t time = 0U; + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return; + } + + time = k_delayed_work_remaining_get(&srv->attn_timer) / 1000; + BT_DBG("%u second%s", time, (time == 1U) ? "" : "s"); + + bt_mesh_model_msg_init(&msg, OP_ATTENTION_STATUS); + net_buf_simple_add_u8(&msg, time); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Health Attention Status", __func__); + } +} + +static void attention_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("%s", __func__); + + send_attention_status(model, ctx); +} + +static void health_set_attention(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t time = 0U; + + time = net_buf_simple_pull_u8(buf); + + BT_DBG("%u second%s", time, (time == 1U) ? "" : "s"); + + bt_mesh_attention(model, time); +} + +static void attention_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("%s", __func__); + + health_set_attention(model, ctx, buf); + + if (ctx->recv_op == OP_ATTENTION_SET) { + send_attention_status(model, ctx); + } +} + +static void send_health_period_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx) +{ + BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEALTH_PERIOD_STATUS, 1); + + bt_mesh_model_msg_init(&msg, OP_HEALTH_PERIOD_STATUS); + net_buf_simple_add_u8(&msg, model->pub->period_div); + + if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) { + BT_ERR("%s, Unable to send Health Period Status", __func__); + } +} + +static void health_period_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("%s", __func__); + + send_health_period_status(model, ctx); +} + +static void health_set_period(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u8_t period = 0U; + + period = net_buf_simple_pull_u8(buf); + if (period > 15) { + BT_WARN("%s, Prohibited period value %u", __func__, period); + return; + } + + BT_DBG("period %u", period); + + model->pub->period_div = period; +} + +static void health_period_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + BT_DBG("%s", __func__); + + health_set_period(model, ctx, buf); + + if (ctx->recv_op == OP_HEALTH_PERIOD_SET) { + send_health_period_status(model, ctx); + } +} + +const struct bt_mesh_model_op bt_mesh_health_srv_op[] = { + { OP_HEALTH_FAULT_GET, 2, health_fault_get }, + { OP_HEALTH_FAULT_CLEAR, 2, health_fault_clear }, + { OP_HEALTH_FAULT_CLEAR_UNREL, 2, health_fault_clear }, + { OP_HEALTH_FAULT_TEST, 3, health_fault_test }, + { OP_HEALTH_FAULT_TEST_UNREL, 3, health_fault_test }, + { OP_HEALTH_PERIOD_GET, 0, health_period_get }, + { OP_HEALTH_PERIOD_SET, 1, health_period_set }, + { OP_HEALTH_PERIOD_SET_UNREL, 1, health_period_set }, + { OP_ATTENTION_GET, 0, attention_get }, + { OP_ATTENTION_SET, 1, attention_set }, + { OP_ATTENTION_SET_UNREL, 1, attention_set }, + BLE_MESH_MODEL_OP_END, +}; + +static size_t health_get_current(struct bt_mesh_model *model, + struct net_buf_simple *msg) +{ + struct bt_mesh_health_srv *srv = model->user_data; + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return 0; + } + + if (msg->size < 4) { + BT_ERR("%s, Too small health publication msg size %d", __func__, msg->size); + return 0; + } + + bt_mesh_model_msg_init(msg, OP_HEALTH_CURRENT_STATUS); + net_buf_simple_add_u8(msg, srv->test.prev_test_id); + net_buf_simple_add_le16(msg, srv->test.company_id); + health_get_fault_value(model, msg, true); + + return health_get_curr_fault_count(model); +} + +static int health_pub_update(struct bt_mesh_model *model) +{ + struct bt_mesh_model_pub *pub = model->pub; + size_t count = 0U; + + BT_DBG("%s", __func__); + + if (!pub || !pub->msg) { + BT_ERR("%s, Invalid health publication context", __func__); + return -EINVAL; + } + + count = health_get_current(model, pub->msg); + if (count) { + pub->fast_period = 1U; + } else { + pub->fast_period = 0U; + } + + return 0; +} + +int bt_mesh_fault_update(struct bt_mesh_elem *elem) +{ + struct bt_mesh_model *model = NULL; + + model = bt_mesh_model_find(elem, BLE_MESH_MODEL_ID_HEALTH_SRV); + if (!model) { + BT_ERR("%s, Health Server does not exist", __func__); + return -EINVAL; + } + + if (!model->pub) { + BT_ERR("%s, Health Server has no publication support", __func__); + return -EINVAL; + } + + /* Let periodic publishing, if enabled, take care of sending the + * Health Current Status. + */ + if (bt_mesh_model_pub_period_get(model)) { + return 0; + } + + health_pub_update(model); + + return bt_mesh_model_publish(model); +} + +static void attention_off(struct k_work *work) +{ + struct bt_mesh_health_srv *srv = CONTAINER_OF(work, + struct bt_mesh_health_srv, + attn_timer.work); + BT_DBG("%s", __func__); + + if (!srv) { + BT_ERR("%s, No Health Server context provided", __func__); + return; + } + + if (srv->cb.attn_off) { + srv->cb.attn_off(srv->model); + } + srv->attn_timer_start = false; +} + +int bt_mesh_health_srv_init(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_health_srv *srv = model->user_data; + + /* Health Server Model shall be supported by a primary element and may be + * supported by any secondary elements. + */ + + if (!srv) { + if (!primary) { + /* If Health Server is in the secondary element with NULL user_data. */ + return 0; + } + + BT_ERR("%s, No Health Server context provided", __func__); + return -EINVAL; + } + + if (srv->test.id_count == 0 || !srv->test.test_ids) { + BT_ERR("%s, No Health Test ID provided", __func__); + return -EINVAL; + } + + if (!model->pub) { + BT_ERR("%s, Health Server has no publication support", __func__); + return -EINVAL; + } + + model->pub->update = health_pub_update; + + k_delayed_work_init(&srv->attn_timer, attention_off); + + srv->model = model; + srv->attn_timer_start = false; + + memset(srv->test.curr_faults, HEALTH_NO_FAULT, ARRAY_SIZE(srv->test.curr_faults)); + memset(srv->test.reg_faults, HEALTH_NO_FAULT, ARRAY_SIZE(srv->test.reg_faults)); + + if (primary) { + health_srv = srv; + } + + return 0; +} + +int bt_mesh_health_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + struct bt_mesh_health_srv *srv = model->user_data; + + if (!srv) { + if (!primary) { + /* If Health Server is in the secondary element with NULL user_data. */ + return 0; + } + + BT_ERR("%s, No Health Server context provided", __func__); + return -EINVAL; + } + + if (srv->test.id_count == 0 || !srv->test.test_ids) { + BT_ERR("%s, No Health Test ID provided", __func__); + return -EINVAL; + } + + if (!model->pub) { + BT_ERR("%s, Health Server has no publication support", __func__); + return -EINVAL; + } + + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->update = NULL; + + k_delayed_work_free(&srv->attn_timer); + + if (primary) { + health_srv = NULL; + } + + return 0; +} + +void bt_mesh_attention(struct bt_mesh_model *model, u8_t time) +{ + struct bt_mesh_health_srv *srv = NULL; + + if (!model) { + srv = health_srv; + if (!srv) { + BT_WARN("%s, No Health Server context provided", __func__); + return; + } + + model = srv->model; + } else { + srv = model->user_data; + if (!srv) { + BT_WARN("%s, No Health Server context provided", __func__); + return; + } + } + + if (time) { + if (srv->cb.attn_on) { + srv->cb.attn_on(model, time); + } + + k_delayed_work_submit(&srv->attn_timer, time * 1000U); + srv->attn_timer_start = true; + } else { + k_delayed_work_cancel(&srv->attn_timer); + + if (srv->attn_timer_start == true) { + if (srv->cb.attn_off) { + srv->cb.attn_off(model); + } + srv->attn_timer_start = false; + } + } +} diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h new file mode 100644 index 000000000..873d98985 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_cli.h @@ -0,0 +1,303 @@ +/** @file + * @brief Bluetooth Mesh Configuration Client Model APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_CFG_CLI_H_ +#define _BLE_MESH_CFG_CLI_H_ + +#include "client_common.h" + +/** + * @brief Bluetooth Mesh + * @defgroup bt_mesh_cfg_cli Bluetooth Mesh Configuration Client Model + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Config client model common structure */ +typedef bt_mesh_client_user_data_t bt_mesh_config_client_t; +typedef bt_mesh_client_internal_data_t config_internal_data_t; + +extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[]; + +#define BLE_MESH_MODEL_CFG_CLI(cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_CFG_CLI, \ + bt_mesh_cfg_cli_op, NULL, cli_data) + +int bt_mesh_cfg_comp_data_get(struct bt_mesh_msg_ctx *ctx, u8_t page); + +int bt_mesh_cfg_beacon_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_beacon_set(struct bt_mesh_msg_ctx *ctx, u8_t val); + +int bt_mesh_cfg_ttl_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_ttl_set(struct bt_mesh_msg_ctx *ctx, u8_t val); + +int bt_mesh_cfg_friend_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_friend_set(struct bt_mesh_msg_ctx *ctx, u8_t val); + +int bt_mesh_cfg_gatt_proxy_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_gatt_proxy_set(struct bt_mesh_msg_ctx *ctx, u8_t val); + +int bt_mesh_cfg_relay_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_relay_set(struct bt_mesh_msg_ctx *ctx, u8_t new_relay, u8_t new_transmit); + +int bt_mesh_cfg_net_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, + const u8_t net_key[16]); + +int bt_mesh_cfg_app_key_add(struct bt_mesh_msg_ctx *ctx, u16_t key_net_idx, + u16_t key_app_idx, const u8_t app_key[16]); + +int bt_mesh_cfg_mod_app_bind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_app_idx, u16_t mod_id, u16_t cid); + +struct bt_mesh_cfg_mod_pub { + u16_t addr; + u16_t app_idx; + bool cred_flag; + u8_t ttl; + u8_t period; + u8_t transmit; +}; + +int bt_mesh_cfg_mod_pub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_pub_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid, + struct bt_mesh_cfg_mod_pub *pub); + +int bt_mesh_cfg_mod_sub_add(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_del(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t sub_addr, u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_va_add(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_va_del(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_va_overwrite(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + const u8_t label[16], u16_t mod_id, u16_t cid); + +struct bt_mesh_cfg_hb_sub { + u16_t src; + u16_t dst; + u8_t period; +}; + +int bt_mesh_cfg_hb_sub_set(struct bt_mesh_msg_ctx *ctx, + struct bt_mesh_cfg_hb_sub *sub); + +int bt_mesh_cfg_hb_sub_get(struct bt_mesh_msg_ctx *ctx); + +struct bt_mesh_cfg_hb_pub { + u16_t dst; + u8_t count; + u8_t period; + u8_t ttl; + u16_t feat; + u16_t net_idx; +}; + +int bt_mesh_cfg_hb_pub_set(struct bt_mesh_msg_ctx *ctx, + const struct bt_mesh_cfg_hb_pub *pub); + +int bt_mesh_cfg_hb_pub_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_node_reset(struct bt_mesh_msg_ctx *ctx); + +s32_t bt_mesh_cfg_cli_timeout_get(void); +void bt_mesh_cfg_cli_timeout_set(s32_t timeout); + +/* Configuration Client Status Message Context */ + +struct bt_mesh_cfg_comp_data_status { + u8_t page; + struct net_buf_simple *comp_data; +}; + +struct bt_mesh_cfg_relay_status { + u8_t relay; + u8_t retransmit; +}; + +struct bt_mesh_cfg_netkey_status { + u8_t status; + u16_t net_idx; +}; + +struct bt_mesh_cfg_appkey_status { + u8_t status; + u16_t net_idx; + u16_t app_idx; +}; + +struct bt_mesh_cfg_mod_app_status { + u8_t status; + u16_t elem_addr; + u16_t app_idx; + u16_t cid; + u16_t mod_id; +}; + +struct bt_mesh_cfg_mod_pub_status { + u8_t status; + u16_t elem_addr; + u16_t addr; + u16_t app_idx; + bool cred_flag; + u8_t ttl; + u8_t period; + u8_t transmit; + u16_t cid; + u16_t mod_id; +}; + +struct bt_mesh_cfg_mod_sub_status { + u8_t status; + u16_t elem_addr; + u16_t sub_addr; + u16_t cid; + u16_t mod_id; +}; + +struct bt_mesh_cfg_hb_sub_status { + u8_t status; + u16_t src; + u16_t dst; + u8_t period; + u8_t count; + u8_t min; + u8_t max; +}; + +struct bt_mesh_cfg_hb_pub_status { + u8_t status; + u16_t dst; + u8_t count; + u8_t period; + u8_t ttl; + u16_t feat; + u16_t net_idx; +}; + +struct bt_mesh_cfg_mod_sub_list { + u8_t status; + u16_t elem_addr; + u16_t cid; + u16_t mod_id; + struct net_buf_simple *addr; +}; + +struct bt_mesh_cfg_net_key_list { + struct net_buf_simple *net_idx; +}; + +struct bt_mesh_cfg_app_key_list { + u8_t status; + u16_t net_idx; + struct net_buf_simple *app_idx; +}; + +struct bt_mesh_cfg_node_id_status { + u8_t status; + u16_t net_idx; + u8_t identity; +}; + +struct bt_mesh_cfg_mod_app_list { + u8_t status; + u16_t elem_addr; + u16_t cid; + u16_t mod_id; + struct net_buf_simple *app_idx; +}; + +struct bt_mesh_cfg_key_refresh_status { + u8_t status; + u16_t net_idx; + u8_t phase; +}; + +struct bt_mesh_cfg_lpn_pollto_status { + u16_t lpn_addr; + s32_t timeout; +}; + +int bt_mesh_cfg_mod_pub_va_set(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid, const u8_t label[16], + struct bt_mesh_cfg_mod_pub *pub); + +int bt_mesh_cfg_mod_sub_del_all(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_sub_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id); + +int bt_mesh_cfg_mod_sub_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_net_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, + const u8_t net_key[16]); + +int bt_mesh_cfg_net_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx); + +int bt_mesh_cfg_net_key_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_app_key_update(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, + u16_t app_idx, const u8_t app_key[16]); + +int bt_mesh_cfg_app_key_delete(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u16_t app_idx); + +int bt_mesh_cfg_app_key_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx); + +int bt_mesh_cfg_node_identity_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx); + +int bt_mesh_cfg_node_identity_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t identity); + +int bt_mesh_cfg_mod_app_unbind(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t app_idx, u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_mod_app_get(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, u16_t mod_id); + +int bt_mesh_cfg_mod_app_get_vnd(struct bt_mesh_msg_ctx *ctx, u16_t elem_addr, + u16_t mod_id, u16_t cid); + +int bt_mesh_cfg_kr_phase_get(struct bt_mesh_msg_ctx *ctx, u16_t net_idx); + +int bt_mesh_cfg_kr_phase_set(struct bt_mesh_msg_ctx *ctx, u16_t net_idx, u8_t transition); + +int bt_mesh_cfg_lpn_timeout_get(struct bt_mesh_msg_ctx *ctx, u16_t lpn_addr); + +int bt_mesh_cfg_net_transmit_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_cfg_net_transmit_set(struct bt_mesh_msg_ctx *ctx, u8_t transmit); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __BLE_MESH_CFG_CLI_H */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h b/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h new file mode 100644 index 000000000..fe9843239 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/cfg_srv.h @@ -0,0 +1,222 @@ +/** @file + * @brief Bluetooth Mesh Configuration Server Model APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_CFG_SRV_H_ +#define _BLE_MESH_CFG_SRV_H_ + +#include "mesh_access.h" + +/** + * @brief Bluetooth Mesh + * @defgroup bt_mesh_cfg_srv Bluetooth Mesh Configuration Server Model + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** Mesh Configuration Server Model Context */ +struct bt_mesh_cfg_srv { + struct bt_mesh_model *model; + + u8_t net_transmit; /* Network Transmit state */ + u8_t relay; /* Relay Mode state */ + u8_t relay_retransmit; /* Relay Retransmit state */ + u8_t beacon; /* Secure Network Beacon state */ + u8_t gatt_proxy; /* GATT Proxy state */ + u8_t frnd; /* Friend state */ + u8_t default_ttl; /* Default TTL */ + + /* Heartbeat Publication */ + struct bt_mesh_hb_pub { + struct k_delayed_work timer; + + u16_t dst; + u16_t count; + u8_t period; + u8_t ttl; + u16_t feat; + u16_t net_idx; + } hb_pub; + + /* Heartbeat Subscription */ + struct bt_mesh_hb_sub { + s64_t expiry; + + u16_t src; + u16_t dst; + u16_t count; + u8_t min_hops; + u8_t max_hops; + + /* Optional subscription tracking function */ + void (*func)(u8_t hops, u16_t feat); + } hb_sub; +}; + +extern const struct bt_mesh_model_op bt_mesh_cfg_srv_op[]; + +#define BLE_MESH_MODEL_CFG_SRV(srv_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_CFG_SRV, \ + bt_mesh_cfg_srv_op, NULL, srv_data) + +typedef union { + struct { + u8_t beacon; + } cfg_beacon_set; + struct { + u8_t ttl; + } cfg_default_ttl_set; + struct { + u8_t gatt_proxy; + } cfg_gatt_proxy_set; + struct { + u8_t relay; + u8_t retransmit; + } cfg_relay_set; + struct { + u16_t elem_addr; + u16_t pub_addr; + u16_t app_idx; + bool cred_flag; + u8_t ttl; + u8_t period; + u8_t transmit; + u16_t cid; + u16_t mod_id; + } cfg_mod_pub_set; + struct { + u16_t elem_addr; + u8_t pub_addr[16]; + u16_t app_idx; + bool cred_flag; + u8_t ttl; + u8_t period; + u8_t transmit; + u16_t cid; + u16_t mod_id; + } cfg_mod_pub_va_set; + struct { + u16_t elem_addr; + u16_t sub_addr; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_add; + struct { + u16_t elem_addr; + u8_t sub_addr[16]; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_va_add; + struct { + u16_t elem_addr; + u16_t sub_addr; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_delete; + struct { + u16_t elem_addr; + u8_t sub_addr[16]; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_va_delete; + struct { + u16_t elem_addr; + u16_t sub_addr; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_overwrite; + struct { + u16_t elem_addr; + u8_t sub_addr[16]; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_va_overwrite; + struct { + u16_t elem_addr; + u16_t cid; + u16_t mod_id; + } cfg_mod_sub_delete_all; + struct { + u16_t net_idx; + u8_t net_key[16]; + } cfg_netkey_add; + struct { + u16_t net_idx; + u8_t net_key[16]; + } cfg_netkey_update; + struct { + u16_t net_idx; + } cfg_netkey_delete; + struct { + u16_t net_idx; + u16_t app_idx; + u8_t app_key[16]; + } cfg_appkey_add; + struct { + u16_t net_idx; + u16_t app_idx; + u8_t app_key[16]; + } cfg_appkey_update; + struct { + u16_t net_idx; + u16_t app_idx; + } cfg_appkey_delete; + struct { + u16_t net_idx; + u8_t identity; + } cfg_node_identity_set; + struct { + u16_t elem_addr; + u16_t app_idx; + u16_t cid; + u16_t mod_id; + } cfg_mod_app_bind; + struct { + u16_t elem_addr; + u16_t app_idx; + u16_t cid; + u16_t mod_id; + } cfg_mod_app_unbind; + struct { + u8_t frnd; + } cfg_friend_set; + struct { + u16_t net_idx; + u8_t kr_phase; + } cfg_kr_phase_set; + struct { + u16_t dst; + u8_t count; + u8_t period; + u8_t ttl; + u16_t feat; + u16_t net_idx; + } cfg_hb_pub_set; + struct { + u16_t src; + u16_t dst; + u8_t period; + } cfg_hb_sub_set; + struct { + u8_t transmit; + } cfg_net_transmit_set; +} bt_mesh_cfg_server_state_change_t; + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __BLE_MESH_CFG_SRV_H */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h new file mode 100644 index 000000000..52982eced --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_cli.h @@ -0,0 +1,82 @@ +/** @file + * @brief Bluetooth Mesh Health Client Model APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_HEALTH_CLI_H_ +#define _BLE_MESH_HEALTH_CLI_H_ + +#include "client_common.h" + +/** + * @brief Bluetooth Mesh + * @defgroup bt_mesh_health_cli Bluetooth Mesh Health Client Model + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Health client model common structure */ +typedef bt_mesh_client_user_data_t bt_mesh_health_client_t; +typedef bt_mesh_client_internal_data_t health_internal_data_t; + +extern const struct bt_mesh_model_op bt_mesh_health_cli_op[]; + +#define BLE_MESH_MODEL_HEALTH_CLI(cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_HEALTH_CLI, \ + bt_mesh_health_cli_op, NULL, cli_data) + +int bt_mesh_health_cli_set(struct bt_mesh_model *model); + +int bt_mesh_health_fault_get(struct bt_mesh_msg_ctx *ctx, u16_t cid); + +int bt_mesh_health_fault_clear(struct bt_mesh_msg_ctx *ctx, u16_t cid, + bool need_ack); + +int bt_mesh_health_fault_test(struct bt_mesh_msg_ctx *ctx, + u16_t cid, u8_t test_id, bool need_ack); + +int bt_mesh_health_period_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_health_period_set(struct bt_mesh_msg_ctx *ctx, + u8_t divisor, bool need_ack); + +int bt_mesh_health_attention_get(struct bt_mesh_msg_ctx *ctx); + +int bt_mesh_health_attention_set(struct bt_mesh_msg_ctx *ctx, + u8_t attention, bool need_ack); + +s32_t bt_mesh_health_cli_timeout_get(void); +void bt_mesh_health_cli_timeout_set(s32_t timeout); + +/* Health Client Status Message Context */ + +struct bt_mesh_health_current_status { + u8_t test_id; + u16_t cid; + struct net_buf_simple *fault_array; +}; + +struct bt_mesh_health_fault_status { + u8_t test_id; + u16_t cid; + struct net_buf_simple *fault_array; +}; + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __BLE_MESH_HEALTH_CLI_H */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h b/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h new file mode 100644 index 000000000..02a21e295 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/health_srv.h @@ -0,0 +1,105 @@ +/** @file + * @brief Bluetooth Mesh Health Server Model APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_HEALTH_SRV_H_ +#define _BLE_MESH_HEALTH_SRV_H_ + +#include "mesh_access.h" + +/** + * @brief Bluetooth Mesh Health Server Model + * @defgroup bt_mesh_health_srv Bluetooth Mesh Health Server Model + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_mesh_health_srv_cb { + /* Clear registered faults */ + void (*fault_clear)(struct bt_mesh_model *model, u16_t company_id); + + /* Run a specific test */ + void (*fault_test)(struct bt_mesh_model *model, u8_t test_id, + u16_t company_id); + + /* Attention on */ + void (*attn_on)(struct bt_mesh_model *model, u8_t time); + + /* Attention off */ + void (*attn_off)(struct bt_mesh_model *model); +}; + +/** @def BLE_MESH_HEALTH_PUB_DEFINE + * + * A helper to define a health publication context + * + * @param _name Name given to the publication context variable. + * @param _max_faults Maximum number of faults the element can have. + */ +#define BLE_MESH_HEALTH_PUB_DEFINE(_name, _max_faults) \ + BLE_MESH_MODEL_PUB_DEFINE(_name, NULL, (1 + 3 + (_max_faults))) + +struct bt_mesh_health_test { + u8_t id_count; /* Number of Health self-test ID */ + const u8_t *test_ids; /* Array of Health self-test IDs */ + u16_t company_id; /* Company ID used to identify the Health Fault state */ + u8_t prev_test_id; /* Most currently performed test id */ + u8_t curr_faults[32]; /* Array of current faults */ + u8_t reg_faults[32]; /* Array of registered faults */ +} __attribute__((packed)); + +/** Mesh Health Server Model Context */ +struct bt_mesh_health_srv { + struct bt_mesh_model *model; + + /* Optional callback struct */ + struct bt_mesh_health_srv_cb cb; + + /* Attention Timer state */ + struct k_delayed_work attn_timer; + + /* Attention Timer start flag */ + bool attn_timer_start; + + /* Health Server fault test */ + struct bt_mesh_health_test test; +}; + +extern const struct bt_mesh_model_op bt_mesh_health_srv_op[]; + +/** @def BLE_MESH_MODEL_HEALTH_SRV + * + * Define a new health server model. Note that this API needs to be + * repeated for each element which the application wants to have a + * health server model on. Each instance also needs a unique + * bt_mesh_health_srv and bt_mesh_model_pub context. + * + * @param srv Pointer to a unique struct bt_mesh_health_srv. + * @param pub Pointer to a unique struct bt_mesh_model_pub. + * + * @return New mesh model instance. + */ +#define BLE_MESH_MODEL_HEALTH_SRV(srv, pub) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_HEALTH_SRV, \ + bt_mesh_health_srv_op, pub, srv) + +int bt_mesh_fault_update(struct bt_mesh_elem *elem); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __BLE_MESH_HEALTH_SRV_H */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h new file mode 100644 index 000000000..dd3985a8d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_access.h @@ -0,0 +1,509 @@ +/** @file + * @brief Bluetooth Mesh Access Layer APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_ACCESS_H_ +#define _BLE_MESH_ACCESS_H_ + +#include "sdkconfig.h" +#include "mesh_buf.h" +#include "mesh_timer.h" + +/** + * @brief Bluetooth Mesh Access Layer + * @defgroup bt_mesh_access Bluetooth Mesh Access Layer + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_CID_NVAL 0xFFFF + +#define BLE_MESH_ADDR_UNASSIGNED 0x0000 +#define BLE_MESH_ADDR_ALL_NODES 0xffff +#define BLE_MESH_ADDR_PROXIES 0xfffc +#define BLE_MESH_ADDR_FRIENDS 0xfffd +#define BLE_MESH_ADDR_RELAYS 0xfffe + +#define BLE_MESH_KEY_UNUSED 0xffff +#define BLE_MESH_KEY_DEV 0xfffe + +/** Helper to define a mesh element within an array. + * + * In case the element has no SIG or Vendor models the helper + * macro BLE_MESH_MODEL_NONE can be given instead. + * + * @param _loc Location Descriptor. + * @param _mods Array of models. + * @param _vnd_mods Array of vendor models. + */ +#define BLE_MESH_ELEM(_loc, _mods, _vnd_mods) \ +{ \ + .loc = (_loc), \ + .model_count = ARRAY_SIZE(_mods), \ + .models = (_mods), \ + .vnd_model_count = ARRAY_SIZE(_vnd_mods), \ + .vnd_models = (_vnd_mods), \ +} + +/** Abstraction that describes a Mesh Element */ +struct bt_mesh_elem { + /* Unicast Address. Set at runtime during provisioning. */ + u16_t addr; + + /* Location Descriptor (GATT Bluetooth Namespace Descriptors) */ + const u16_t loc; + + const u8_t model_count; + const u8_t vnd_model_count; + + struct bt_mesh_model *const models; + struct bt_mesh_model *const vnd_models; +}; + +/* Foundation Models */ +#define BLE_MESH_MODEL_ID_CFG_SRV 0x0000 +#define BLE_MESH_MODEL_ID_CFG_CLI 0x0001 +#define BLE_MESH_MODEL_ID_HEALTH_SRV 0x0002 +#define BLE_MESH_MODEL_ID_HEALTH_CLI 0x0003 + +/* Models from the Mesh Model Specification */ +#define BLE_MESH_MODEL_ID_GEN_ONOFF_SRV 0x1000 +#define BLE_MESH_MODEL_ID_GEN_ONOFF_CLI 0x1001 +#define BLE_MESH_MODEL_ID_GEN_LEVEL_SRV 0x1002 +#define BLE_MESH_MODEL_ID_GEN_LEVEL_CLI 0x1003 +#define BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV 0x1004 +#define BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI 0x1005 +#define BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV 0x1006 +#define BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV 0x1007 +#define BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI 0x1008 +#define BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV 0x1009 +#define BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV 0x100a +#define BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI 0x100b +#define BLE_MESH_MODEL_ID_GEN_BATTERY_SRV 0x100c +#define BLE_MESH_MODEL_ID_GEN_BATTERY_CLI 0x100d +#define BLE_MESH_MODEL_ID_GEN_LOCATION_SRV 0x100e +#define BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV 0x100f +#define BLE_MESH_MODEL_ID_GEN_LOCATION_CLI 0x1010 +#define BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV 0x1011 +#define BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV 0x1012 +#define BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV 0x1013 +#define BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV 0x1014 +#define BLE_MESH_MODEL_ID_GEN_PROP_CLI 0x1015 +#define BLE_MESH_MODEL_ID_SENSOR_SRV 0x1100 +#define BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV 0x1101 +#define BLE_MESH_MODEL_ID_SENSOR_CLI 0x1102 +#define BLE_MESH_MODEL_ID_TIME_SRV 0x1200 +#define BLE_MESH_MODEL_ID_TIME_SETUP_SRV 0x1201 +#define BLE_MESH_MODEL_ID_TIME_CLI 0x1202 +#define BLE_MESH_MODEL_ID_SCENE_SRV 0x1203 +#define BLE_MESH_MODEL_ID_SCENE_SETUP_SRV 0x1204 +#define BLE_MESH_MODEL_ID_SCENE_CLI 0x1205 +#define BLE_MESH_MODEL_ID_SCHEDULER_SRV 0x1206 +#define BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV 0x1207 +#define BLE_MESH_MODEL_ID_SCHEDULER_CLI 0x1208 +#define BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV 0x1300 +#define BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV 0x1301 +#define BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI 0x1302 +#define BLE_MESH_MODEL_ID_LIGHT_CTL_SRV 0x1303 +#define BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV 0x1304 +#define BLE_MESH_MODEL_ID_LIGHT_CTL_CLI 0x1305 +#define BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV 0x1306 +#define BLE_MESH_MODEL_ID_LIGHT_HSL_SRV 0x1307 +#define BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV 0x1308 +#define BLE_MESH_MODEL_ID_LIGHT_HSL_CLI 0x1309 +#define BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV 0x130a +#define BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV 0x130b +#define BLE_MESH_MODEL_ID_LIGHT_XYL_SRV 0x130c +#define BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV 0x130d +#define BLE_MESH_MODEL_ID_LIGHT_XYL_CLI 0x130e +#define BLE_MESH_MODEL_ID_LIGHT_LC_SRV 0x130f +#define BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV 0x1310 +#define BLE_MESH_MODEL_ID_LIGHT_LC_CLI 0x1311 + +/** Message sending context. */ +struct bt_mesh_msg_ctx { + /** NetKey Index of the subnet to send the message on. */ + u16_t net_idx; + + /** AppKey Index to encrypt the message with. */ + u16_t app_idx; + + /** Remote address. */ + u16_t addr; + + /** Destination address of a received message. Not used for sending. */ + u16_t recv_dst; + + /** RSSI of received packet. Not used for sending. */ + s8_t recv_rssi; + + /** Received TTL value. Not used for sending. */ + u8_t recv_ttl: 7; + + /** Force sending reliably by using segment acknowledgement */ + u8_t send_rel: 1; + + /** TTL, or BLE_MESH_TTL_DEFAULT for default TTL. */ + u8_t send_ttl; + + /** Change by Espressif, opcode of a received message. + * Not used for sending message. */ + u32_t recv_op; + + /** Change by Espressif, model corresponds to the message */ + struct bt_mesh_model *model; + + /** Change by Espressif, if the message is sent by a server + * model. Not used for receiving message. */ + bool srv_send; +}; + +struct bt_mesh_model_op { + /* OpCode encoded using the BLE_MESH_MODEL_OP_* macros */ + const u32_t opcode; + + /* Minimum required message length */ + const size_t min_len; + + /* Message handler for the opcode */ + void (*const func)(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf); +}; + +#define BLE_MESH_MODEL_OP_1(b0) (b0) +#define BLE_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1)) +#define BLE_MESH_MODEL_OP_3(b0, cid) ((((b0) << 16) | 0xc00000) | (cid)) + +#define BLE_MESH_MODEL_OP_END { 0, 0, NULL } +#define BLE_MESH_MODEL_NO_OPS ((struct bt_mesh_model_op []) \ + { BLE_MESH_MODEL_OP_END }) + +/** Helper to define an empty model array */ +#define BLE_MESH_MODEL_NONE ((struct bt_mesh_model []){}) + +/** Length of a short Mesh MIC. */ +#define BLE_MESH_MIC_SHORT 4 +/** Length of a long Mesh MIC. */ +#define BLE_MESH_MIC_LONG 8 + +/** @def BLE_MESH_MODEL_OP_LEN + * + * @brief Helper to determine the length of an opcode. + * + * @param _op Opcode. + */ +#define BLE_MESH_MODEL_OP_LEN(_op) ((_op) <= 0xff ? 1 : (_op) <= 0xffff ? 2 : 3) + +/** @def BLE_MESH_MODEL_BUF_LEN + * + * @brief Helper for model message buffer length. + * + * Returns the length of a Mesh model message buffer, including the opcode + * length and a short MIC. + * + * @param _op Opcode of the message. + * @param _payload_len Length of the model payload. + */ +#define BLE_MESH_MODEL_BUF_LEN(_op, _payload_len) \ + (BLE_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BLE_MESH_MIC_SHORT) + +/** @def BLE_MESH_MODEL_BUF_LEN_LONG_MIC + * + * @brief Helper for model message buffer length. + * + * Returns the length of a Mesh model message buffer, including the opcode + * length and a long MIC. + * + * @param _op Opcode of the message. + * @param _payload_len Length of the model payload. + */ +#define BLE_MESH_MODEL_BUF_LEN_LONG_MIC(_op, _payload_len) \ + (BLE_MESH_MODEL_OP_LEN(_op) + (_payload_len) + BLE_MESH_MIC_LONG) + +/** @def BLE_MESH_MODEL_BUF_DEFINE + * + * @brief Define a Mesh model message buffer using @ref NET_BUF_SIMPLE_DEFINE. + * + * @param _buf Buffer name. + * @param _op Opcode of the message. + * @param _payload_len Length of the model message payload. + */ +#define BLE_MESH_MODEL_BUF_DEFINE(_buf, _op, _payload_len) \ + NET_BUF_SIMPLE_DEFINE(_buf, BLE_MESH_MODEL_BUF_LEN(_op, (_payload_len))) + +#define BLE_MESH_MODEL(_id, _op, _pub, _user_data) \ +{ \ + .id = (_id), \ + .op = _op, \ + .keys = { [0 ... (CONFIG_BLE_MESH_MODEL_KEY_COUNT - 1)] = \ + BLE_MESH_KEY_UNUSED }, \ + .pub = _pub, \ + .groups = { [0 ... (CONFIG_BLE_MESH_MODEL_GROUP_COUNT - 1)] = \ + BLE_MESH_ADDR_UNASSIGNED }, \ + .user_data = _user_data, \ +} + +#define BLE_MESH_MODEL_VND(_company, _id, _op, _pub, _user_data) \ +{ \ + .vnd.company = (_company), \ + .vnd.id = (_id), \ + .op = _op, \ + .pub = _pub, \ + .keys = { [0 ... (CONFIG_BLE_MESH_MODEL_KEY_COUNT - 1)] = \ + BLE_MESH_KEY_UNUSED }, \ + .groups = { [0 ... (CONFIG_BLE_MESH_MODEL_GROUP_COUNT - 1)] = \ + BLE_MESH_ADDR_UNASSIGNED }, \ + .user_data = _user_data, \ +} + +/** @def BLE_MESH_TRANSMIT + * + * @brief Encode transmission count & interval steps. + * + * @param count Number of retransmissions (first transmission is excluded). + * @param int_ms Interval steps in milliseconds. Must be greater than 0 + * and a multiple of 10. + * + * @return Mesh transmit value that can be used e.g. for the default + * values of the configuration model data. + */ +#define BLE_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3)) + +/** @def BLE_MESH_TRANSMIT_COUNT + * + * @brief Decode transmit count from a transmit value. + * + * @param transmit Encoded transmit count & interval value. + * + * @return Transmission count (actual transmissions is N + 1). + */ +#define BLE_MESH_TRANSMIT_COUNT(transmit) (((transmit) & (u8_t)BIT_MASK(3))) + +/** @def BLE_MESH_TRANSMIT_INT + * + * @brief Decode transmit interval from a transmit value. + * + * @param transmit Encoded transmit count & interval value. + * + * @return Transmission interval in milliseconds. + */ +#define BLE_MESH_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 10) + +/** @def BLE_MESH_PUB_TRANSMIT + * + * @brief Encode Publish Retransmit count & interval steps. + * + * @param count Number of retransmissions (first transmission is excluded). + * @param int_ms Interval steps in milliseconds. Must be greater than 0 + * and a multiple of 50. + * + * @return Mesh transmit value that can be used e.g. for the default + * values of the configuration model data. + */ +#define BLE_MESH_PUB_TRANSMIT(count, int_ms) BLE_MESH_TRANSMIT(count, (int_ms) / 5) + +/** @def BLE_MESH_PUB_TRANSMIT_COUNT + * + * @brief Decode Publish Retransmit count from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Retransmission count (actual transmissions is N + 1). + */ +#define BLE_MESH_PUB_TRANSMIT_COUNT(transmit) BLE_MESH_TRANSMIT_COUNT(transmit) + +/** @def BLE_MESH_PUB_TRANSMIT_INT + * + * @brief Decode Publish Retransmit interval from a given value. + * + * @param transmit Encoded Publish Retransmit count & interval value. + * + * @return Transmission interval in milliseconds. + */ +#define BLE_MESH_PUB_TRANSMIT_INT(transmit) ((((transmit) >> 3) + 1) * 50) + +/** Model publication context. */ +struct bt_mesh_model_pub { + /** The model the context belongs to. Initialized by the stack. */ + struct bt_mesh_model *mod; + + u16_t addr; /**< Publish Address. */ + u16_t key:12, /**< Publish AppKey Index. */ + cred:1, /**< Friendship Credentials Flag. */ + send_rel:1; /**< Force reliable sending (segment acks) */ + + u8_t ttl; /**< Publish Time to Live. */ + u8_t retransmit; /**< Retransmit Count & Interval Steps. */ + u8_t period; /**< Publish Period. */ + u8_t period_div:4, /**< Divisor for the Period. */ + fast_period:1,/**< Use FastPeriodDivisor */ + count:3; /**< Retransmissions left. */ + + u32_t period_start; /**< Start of the current period. */ + + /** @brief Publication buffer, containing the publication message. + * + * This will get correctly created when the publication context + * has been defined using the BLE_MESH_MODEL_PUB_DEFINE macro. + * + * BLE_MESH_MODEL_PUB_DEFINE(name, update, size); + */ + struct net_buf_simple *msg; + + /** @brief Callback for updating the publication buffer. + * + * When set to NULL, the model is assumed not to support + * periodic publishing. When set to non-NULL the callback + * will be called periodically and is expected to update + * @ref bt_mesh_model_pub.msg with a valid publication + * message. + * + * If the callback returns non-zero, the publication is skipped + * and will resume on the next periodic publishing interval. + * + * @param mod The Model the Publication Context belongs to. + * + * @return Zero on success or (negative) error code otherwise. + */ + int (*update)(struct bt_mesh_model *mod); + + /** Publish Period Timer. Only for stack-internal use. */ + struct k_delayed_work timer; + + /* Change by Espressif, role of the device going to publish messages */ + u8_t dev_role; +}; + +/** @def BLE_MESH_MODEL_PUB_DEFINE + * + * Define a model publication context. + * + * @param _name Variable name given to the context. + * @param _update Optional message update callback (may be NULL). + * @param _msg_len Length of the publication message. + */ +#define BLE_MESH_MODEL_PUB_DEFINE(_name, _update, _msg_len) \ + NET_BUF_SIMPLE_DEFINE_STATIC(bt_mesh_pub_msg_##_name, _msg_len); \ + static struct bt_mesh_model_pub _name = { \ + .update = _update, \ + .msg = &bt_mesh_pub_msg_##_name, \ + } + +/** Abstraction that describes a Mesh Model instance */ +struct bt_mesh_model { + union { + const u16_t id; + struct { + u16_t company; + u16_t id; + } vnd; + }; + + /* Internal information, mainly for persistent storage */ + u8_t elem_idx; /* Belongs to Nth element */ + u8_t model_idx; /* Is the Nth model in the element */ + u16_t flags; /* Information about what has changed */ + + /* The Element this Model belongs to */ + struct bt_mesh_elem *elem; + + /* Model Publication */ + struct bt_mesh_model_pub *const pub; + + /* AppKey List */ + u16_t keys[CONFIG_BLE_MESH_MODEL_KEY_COUNT]; + + /* Subscription List (group or virtual addresses) */ + u16_t groups[CONFIG_BLE_MESH_MODEL_GROUP_COUNT]; + + const struct bt_mesh_model_op *const op; + + /* Model-specific user data */ + void *user_data; +}; + +struct bt_mesh_send_cb { + void (*start)(u16_t duration, int err, void *cb_data); + void (*end)(int err, void *cb_data); +}; + +void bt_mesh_model_msg_init(struct net_buf_simple *msg, u32_t opcode); + +/** Special TTL value to request using configured default TTL */ +#define BLE_MESH_TTL_DEFAULT 0xff + +/** Maximum allowed TTL value */ +#define BLE_MESH_TTL_MAX 0x7f + +/** + * @brief Send an Access Layer message. + * + * @param model Mesh (client) Model that the message belongs to. + * @param ctx Message context, includes keys, TTL, etc. + * @param msg Access Layer payload (the actual message to be sent). + * @param cb Optional "message sent" callback. + * @param cb_data User data to be passed to the callback. + * + * @return 0 on success, or (negative) error code on failure. + */ +int bt_mesh_model_send(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, + void *cb_data); + +/** + * @brief Send a model publication message. + * + * Before calling this function, the user needs to ensure that the model + * publication message (@ref bt_mesh_model_pub.msg) contains a valid + * message to be sent. Note that this API is only to be used for + * non-period publishing. For periodic publishing the app only needs + * to make sure that @ref bt_mesh_model_pub.msg contains a valid message + * whenever the @ref bt_mesh_model_pub.update callback is called. + * + * @param model Mesh (client) Model that's publishing the message. + * + * @return 0 on success, or (negative) error code on failure. + */ +int bt_mesh_model_publish(struct bt_mesh_model *model); + +/** + * @brief Get the element that a model belongs to. + * + * @param mod Mesh model. + * + * @return Pointer to the element that the given model belongs to. + */ +struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod); + +/** Node Composition */ +struct bt_mesh_comp { + u16_t cid; + u16_t pid; + u16_t vid; + + size_t elem_count; + struct bt_mesh_elem *elem; +}; + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __BLE_MESH_ACCESS_H */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h new file mode 100644 index 000000000..ea4ba202b --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h @@ -0,0 +1,813 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015-2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_BEARER_ADAPT_H_ +#define _BLE_MESH_BEARER_ADAPT_H_ + +#include +#include "sdkconfig.h" +#include "mesh_types.h" +#include "mesh_util.h" +#include "mesh_uuid.h" +#include "mesh_buf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* BLE Mesh Max Connection Count */ +#ifdef CONFIG_BLUEDROID_ENABLED +#define BLE_MESH_MAX_CONN MIN(CONFIG_BT_ACL_CONNECTIONS, CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN) +#endif + +#ifdef CONFIG_NIMBLE_ENABLED +#define BLE_MESH_MAX_CONN CONFIG_NIMBLE_MAX_CONNECTIONS +#endif + +#define BLE_MESH_GAP_ADV_MAX_LEN 31 + +#define BLE_MESH_GATT_DEF_MTU_SIZE 23 + +/* BD ADDR types */ +#define BLE_MESH_ADDR_PUBLIC 0x00 +#define BLE_MESH_ADDR_RANDOM 0x01 +#define BLE_MESH_ADDR_PUBLIC_ID 0x02 +#define BLE_MESH_ADDR_RANDOM_ID 0x03 + +/* BD ADDR length */ +#define BLE_MESH_ADDR_LEN 0x06 + +/* Advertising types */ +#define BLE_MESH_ADV_IND 0x00 +#define BLE_MESH_ADV_DIRECT_IND 0x01 +#define BLE_MESH_ADV_SCAN_IND 0x02 +#define BLE_MESH_ADV_NONCONN_IND 0x03 +#define BLE_MESH_ADV_DIRECT_IND_LOW_DUTY 0x04 + +/* advertising channel map */ +#define BLE_MESH_ADV_CHNL_37 BIT(0) +#define BLE_MESH_ADV_CHNL_38 BIT(1) +#define BLE_MESH_ADV_CHNL_39 BIT(2) + +/* Advertising filter policy */ +#define BLE_MESH_AP_SCAN_CONN_ALL 0x00 +#define BLE_MESH_AP_SCAN_WL_CONN_ALL 0x01 +#define BLE_MESH_AP_SCAN_ALL_CONN_WL 0x02 +#define BLE_MESH_AP_SCAN_CONN_WL 0x03 + +/* Scan types */ +#define BLE_MESH_SCAN_PASSIVE 0x00 +#define BLE_MESH_SCAN_ACTIVE 0x01 + +/* Scan operation */ +#define BLE_MESH_SCAN_DISABLE 0x00 +#define BLE_MESH_SCAN_ENABLE 0x01 + +/* Scan duplicate operation */ +#define BLE_MESH_SCAN_FILTER_DUP_DISABLE 0x00 +#define BLE_MESH_SCAN_FILTER_DUP_ENABLE 0x01 + +/* Scan filter policy */ +#define BLE_MESH_SP_ADV_ALL 0x00 +#define BLE_MESH_SP_ADV_WL 0x01 +#define BLE_MESH_SP_ADV_ALL_RPA_DIR_ADV 0x02 +#define BLE_MESH_SP_ADV_WL_RPA_DIR_ADV 0x03 + +/* Error codes for Error response PDU */ +#define BLE_MESH_ATT_ERR_INVALID_HANDLE 0x01 +#define BLE_MESH_ATT_ERR_READ_NOT_PERMITTED 0x02 +#define BLE_MESH_ATT_ERR_WRITE_NOT_PERMITTED 0x03 +#define BLE_MESH_ATT_ERR_INVALID_PDU 0x04 +#define BLE_MESH_ATT_ERR_AUTHENTICATION 0x05 +#define BLE_MESH_ATT_ERR_NOT_SUPPORTED 0x06 +#define BLE_MESH_ATT_ERR_INVALID_OFFSET 0x07 +#define BLE_MESH_ATT_ERR_AUTHORIZATION 0x08 +#define BLE_MESH_ATT_ERR_PREPARE_QUEUE_FULL 0x09 +#define BLE_MESH_ATT_ERR_ATTRIBUTE_NOT_FOUND 0x0a +#define BLE_MESH_ATT_ERR_ATTRIBUTE_NOT_LONG 0x0b +#define BLE_MESH_ATT_ERR_ENCRYPTION_KEY_SIZE 0x0c +#define BLE_MESH_ATT_ERR_INVALID_ATTRIBUTE_LEN 0x0d +#define BLE_MESH_ATT_ERR_UNLIKELY 0x0e +#define BLE_MESH_ATT_ERR_INSUFFICIENT_ENCRYPTION 0x0f +#define BLE_MESH_ATT_ERR_UNSUPPORTED_GROUP_TYPE 0x10 +#define BLE_MESH_ATT_ERR_INSUFFICIENT_RESOURCES 0x11 + +/* Common Profile Error Codes (from CSS) */ +#define BLE_MESH_ATT_ERR_WRITE_REQ_REJECTED 0xfc +#define BLE_MESH_ATT_ERR_CCC_IMPROPER_CONF 0xfd +#define BLE_MESH_ATT_ERR_PROCEDURE_IN_PROGRESS 0xfe +#define BLE_MESH_ATT_ERR_OUT_OF_RANGE 0xff + +/* EIR/AD data type definitions */ +#define BLE_MESH_DATA_FLAGS 0x01 /* AD flags */ +#define BLE_MESH_DATA_UUID16_SOME 0x02 /* 16-bit UUID, more available */ +#define BLE_MESH_DATA_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ +#define BLE_MESH_DATA_UUID32_SOME 0x04 /* 32-bit UUID, more available */ +#define BLE_MESH_DATA_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ +#define BLE_MESH_DATA_UUID128_SOME 0x06 /* 128-bit UUID, more available */ +#define BLE_MESH_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ +#define BLE_MESH_DATA_NAME_SHORTENED 0x08 /* Shortened name */ +#define BLE_MESH_DATA_NAME_COMPLETE 0x09 /* Complete name */ +#define BLE_MESH_DATA_TX_POWER 0x0a /* Tx Power */ +#define BLE_MESH_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */ +#define BLE_MESH_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */ +#define BLE_MESH_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */ +#define BLE_MESH_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */ +#define BLE_MESH_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */ +#define BLE_MESH_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */ +#define BLE_MESH_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */ +#define BLE_MESH_DATA_URI 0x24 /* URI */ +#define BLE_MESH_DATA_MESH_PROV 0x29 /* Mesh Provisioning PDU */ +#define BLE_MESH_DATA_MESH_MESSAGE 0x2a /* Mesh Networking PDU */ +#define BLE_MESH_DATA_MESH_BEACON 0x2b /* Mesh Beacon */ + +#define BLE_MESH_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */ + +#define BLE_MESH_AD_LIMITED 0x01 /* Limited Discoverable */ +#define BLE_MESH_AD_GENERAL 0x02 /* General Discoverable */ +#define BLE_MESH_AD_NO_BREDR 0x04 /* BR/EDR not supported */ + +/* Client Characteristic Configuration Values */ + +/** @def BLE_MESH_GATT_CCC_NOTIFY + * @brief Client Characteristic Configuration Notification. + * + * If set, changes to Characteristic Value shall be notified. + */ +#define BLE_MESH_GATT_CCC_NOTIFY 0x0001 + +/** @def BLE_MESH_GATT_CCC_INDICATE + * @brief Client Characteristic Configuration Indication. + * + * If set, changes to Characteristic Value shall be indicated. + */ +#define BLE_MESH_GATT_CCC_INDICATE 0x0002 + +/** @def BLE_MESH_GATT_ERR + * @brief Construct error return value for attribute read and write callbacks. + * + * @param _att_err ATT error code + * + * @return Appropriate error code for the attribute callbacks. + * + */ +#define BLE_MESH_GATT_ERR(_att_err) (-(_att_err)) + +enum { + BLE_MESH_GATT_ITER_STOP = 0, + BLE_MESH_GATT_ITER_CONTINUE, +}; + +/* GATT attribute permission bit field values */ +enum { + /** No operations supported, e.g. for notify-only */ + BLE_MESH_GATT_PERM_NONE = 0, + + /** Attribute read permission. */ + BLE_MESH_GATT_PERM_READ = BIT(0), + + /** Attribute write permission. */ + BLE_MESH_GATT_PERM_WRITE = BIT(1), + + /** Attribute read permission with encryption. + * + * If set, requires encryption for read access. + */ + BLE_MESH_GATT_PERM_READ_ENCRYPT = BIT(2), + + /** Attribute write permission with encryption. + * + * If set, requires encryption for write access. + */ + BLE_MESH_GATT_PERM_WRITE_ENCRYPT = BIT(3), + + /** Attribute read permission with authentication. + * + * If set, requires encryption using authenticated link-key for read + * access. + */ + BLE_MESH_GATT_PERM_READ_AUTHEN = BIT(4), + + /** Attribute write permission with authentication. + * + * If set, requires encryption using authenticated link-key for write + * access. + */ + BLE_MESH_GATT_PERM_WRITE_AUTHEN = BIT(5), + + /** Attribute prepare write permission. + * + * If set, allows prepare writes with use of BT_GATT_WRITE_FLAG_PREPARE + * passed to write callback. + */ + BLE_MESH_GATT_PERM_PREPARE_WRITE = BIT(6), +}; + +/** Advertising options */ +enum { + /** Convenience value when no options are specified. */ + BLE_MESH_ADV_OPT_NONE = 0, + + /** Advertise as connectable. Type of advertising is determined by + * providing SCAN_RSP data and/or enabling local privacy support. + */ + BLE_MESH_ADV_OPT_CONNECTABLE = BIT(0), + + /** Don't try to resume connectable advertising after a connection. + * This option is only meaningful when used together with + * BLE_MESH_ADV_OPT_CONNECTABLE. If set the advertising will be stopped + * when bt_le_adv_stop() is called or when an incoming (slave) + * connection happens. If this option is not set the stack will + * take care of keeping advertising enabled even as connections + * occur. + */ + BLE_MESH_ADV_OPT_ONE_TIME = BIT(1), +}; + +/* Defined GAP timers */ +#define BLE_MESH_GAP_SCAN_FAST_INTERVAL 0x0060 /* 60 ms */ +#define BLE_MESH_GAP_SCAN_FAST_WINDOW 0x0030 /* 30 ms */ +#define BLE_MESH_GAP_SCAN_SLOW_INTERVAL_1 0x0800 /* 1.28 s */ +#define BLE_MESH_GAP_SCAN_SLOW_WINDOW_1 0x0012 /* 11.25 ms */ +#define BLE_MESH_GAP_SCAN_SLOW_INTERVAL_2 0x1000 /* 2.56 s */ +#define BLE_MESH_GAP_SCAN_SLOW_WINDOW_2 0x0012 /* 11.25 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MIN_0 0x0020 /* 20 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MAX_0 0x0020 /* 20 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MIN_1 0x0030 /* 30 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MAX_1 0x0060 /* 60 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MIN_2 0x00a0 /* 100 ms */ +#define BLE_MESH_GAP_ADV_FAST_INT_MAX_2 0x00f0 /* 150 ms */ +#define BLE_MESH_GAP_ADV_SLOW_INT_MIN 0x0320 /* 500 ms */ +#define BLE_MESH_GAP_ADV_SLOW_INT_MAX 0x0320 /* 500 ms */ +#define BLE_MESH_GAP_INIT_CONN_INT_MIN 0x0018 /* 30 ms */ +#define BLE_MESH_GAP_INIT_CONN_INT_MAX 0x0028 /* 50 ms */ + +/* Characteristic Properties Bit field values */ + +/** @def BLE_MESH_GATT_CHRC_BROADCAST + * @brief Characteristic broadcast property. + * + * If set, permits broadcasts of the Characteristic Value using Server + * Characteristic Configuration Descriptor. + */ +#define BLE_MESH_GATT_CHRC_BROADCAST 0x01 + +/** @def BLE_MESH_GATT_CHRC_READ + * @brief Characteristic read property. + * + * If set, permits reads of the Characteristic Value. + */ +#define BLE_MESH_GATT_CHRC_READ 0x02 + +/** @def BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP + * @brief Characteristic write without response property. + * + * If set, permits write of the Characteristic Value without response. + */ +#define BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP 0x04 + +/** @def BLE_MESH_GATT_CHRC_WRITE + * @brief Characteristic write with response property. + * + * If set, permits write of the Characteristic Value with response. + */ +#define BLE_MESH_GATT_CHRC_WRITE 0x08 + +/** @def BLE_MESH_GATT_CHRC_NOTIFY + * @brief Characteristic notify property. + * + * If set, permits notifications of a Characteristic Value without + * acknowledgment. + */ +#define BLE_MESH_GATT_CHRC_NOTIFY 0x10 + +/** @def BLE_MESH_GATT_CHRC_INDICATE + * @brief Characteristic indicate property. + * + * If set, permits indications of a Characteristic Value with acknowledgment. + */ +#define BLE_MESH_GATT_CHRC_INDICATE 0x20 + +/** @def BLE_MESH_GATT_CHRC_AUTH + * @brief Characteristic Authenticated Signed Writes property. + * + * If set, permits signed writes to the Characteristic Value. + */ +#define BLE_MESH_GATT_CHRC_AUTH 0x40 + +/** @def BLE_MESH_GATT_CHRC_EXT_PROP + * @brief Characteristic Extended Properties property. + * + * If set, additional characteristic properties are defined in the + * Characteristic Extended Properties Descriptor. + */ +#define BLE_MESH_GATT_CHRC_EXT_PROP 0x80 + +/** @brief Characteristic Attribute Value. */ +struct bt_mesh_gatt_char { + /** Characteristic UUID. */ + const struct bt_mesh_uuid *uuid; + /** Characteristic properties. */ + u8_t properties; +}; + +/** @brief GATT Service structure */ +struct bt_mesh_gatt_service { + /** Service Attributes */ + struct bt_mesh_gatt_attr *attrs; + /** Service Attribute count */ + u16_t attr_count; + sys_snode_t node; +}; + +struct bt_mesh_ecb_param { + u8_t key[16]; + u8_t clear_text[16]; + u8_t cipher_text[16]; +} __packed; + +typedef struct { + u8_t type; + u8_t val[6]; +} bt_mesh_addr_t; + +/** Description of different data types that can be encoded into + * advertising data. Used to form arrays that are passed to the + * bt_le_adv_start() function. + */ +struct bt_mesh_adv_data { + u8_t type; + u8_t data_len; + const u8_t *data; +}; + +/** @brief Helper to declare elements of bt_data arrays + * + * This macro is mainly for creating an array of struct + * bt_mesh_adv_data elements which is then passed to + * bt_le_adv_start(). + * + * @param _type Type of advertising data field + * @param _data Pointer to the data field payload + * @param _data_len Number of bytes behind the _data pointer + */ +#define BLE_MESH_ADV_DATA(_type, _data, _data_len) \ + { \ + .type = (_type), \ + .data_len = (_data_len), \ + .data = (const u8_t *)(_data), \ + } + +/** @brief Helper to declare elements of bt_data arrays + * + * This macro is mainly for creating an array of struct bt_mesh_adv_data + * elements which is then passed to bt_le_adv_start(). + * + * @param _type Type of advertising data field + * @param _bytes Variable number of single-byte parameters + */ +#define BLE_MESH_ADV_DATA_BYTES(_type, _bytes...) \ + BLE_MESH_ADV_DATA(_type, ((u8_t []) { _bytes }), \ + sizeof((u8_t []) { _bytes })) + +/* BLE Mesh Advertising Parameters */ +struct bt_mesh_adv_param { + /** Bit-field of advertising options */ + u8_t options; + + /** Minimum Advertising Interval (N * 0.625) */ + u16_t interval_min; + + /** Maximum Advertising Interval (N * 0.625) */ + u16_t interval_max; +}; + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +enum bt_mesh_ble_adv_priority { + BLE_MESH_BLE_ADV_PRIO_LOW, + BLE_MESH_BLE_ADV_PRIO_HIGH, +}; + +struct bt_mesh_ble_adv_param { + u16_t interval; /* Advertising interval */ + u8_t adv_type; /* Advertising type */ + u8_t own_addr_type; /* Own address type */ + u8_t peer_addr_type; /* Peer address type */ + u8_t peer_addr[6]; /* Peer address */ + u16_t duration; /* Duration is milliseconds */ + u16_t period; /* Period in milliseconds */ + u16_t count; /* Number of advertising duration */ + u8_t priority:2; /* Priority of BLE advertising packet */ +}; + +struct bt_mesh_ble_adv_data { + u8_t adv_data_len; /* Advertising data length */ + u8_t adv_data[31]; /* Advertising data */ + u8_t scan_rsp_data_len; /* Scan response data length */ + u8_t scan_rsp_data[31]; /* Scan response data */ +}; +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +/* BLE Mesh scan parameters */ +struct bt_mesh_scan_param { + /** Scan type (BLE_MESH_SCAN_ACTIVE or BLE_MESH_SCAN_PASSIVE) */ + u8_t type; + + /** Duplicate filtering (BLE_MESH_SCAN_FILTER_DUP_ENABLE or + * BLE_MESH_SCAN_FILTER_DUP_DISABLE) + */ + u8_t filter_dup; + + /** Scan interval (N * 0.625 ms) */ + u16_t interval; + + /** Scan window (N * 0.625 ms) */ + u16_t window; + + /** BLE scan filter policy */ + u8_t scan_fil_policy; +}; + +struct bt_mesh_conn { + u16_t handle; + bt_mesh_atomic_t ref; +}; + +/** @typedef bt_mesh_scan_cb_t + * @brief Callback type for reporting LE scan results. + * + * A function of this type is given to the bt_le_scan_start() function + * and will be called for any discovered LE device. + * + * @param addr Advertiser LE address and type. + * @param rssi Strength of advertiser signal. + * @param adv_type Type of advertising response from advertiser. + * @param data Buffer containing advertiser data. + */ +typedef void bt_mesh_scan_cb_t(const bt_mesh_addr_t *addr, s8_t rssi, + u8_t adv_type, struct net_buf_simple *buf); + +/* @typedef bt_mesh_dh_key_cb_t + * @brief Callback type for DH Key calculation. + * + * Used to notify of the calculated DH Key. + * + * @param key Public key. + * @param idx Provisioning link index, only used by Provisioner. + * + * @return The DH Key, or NULL in case of failure. + */ +typedef void (*bt_mesh_dh_key_cb_t)(const u8_t key[32], const u8_t idx); + +/** @typedef bt_mesh_gatt_attr_func_t + * @brief Attribute iterator callback. + * + * @param attr Attribute found. + * @param user_data Data given. + * + * @return BLE_MESH_GATT_ITER_CONTINUE if should continue to the next attribute + * or BLE_MESH_GATT_ITER_STOP to stop. + */ +typedef u8_t (*bt_mesh_gatt_attr_func_t)(const struct bt_mesh_gatt_attr *attr, + void *user_data); + +/** @brief Connection callback structure. + * + * This structure is used for tracking the state of a connection. + * It is registered with the help of the bt_mesh_gatts_conn_cb_register() API. + * It's permissible to register multiple instances of this @ref bt_conn_cb + * type, in case different modules of an application are interested in + * tracking the connection state. If a callback is not of interest for + * an instance, it may be set to NULL and will as a consequence not be + * used for that instance. + */ +struct bt_mesh_conn_cb { + /** @brief A new connection has been established. + * + * This callback notifies the application of a new connection. + * In case the err parameter is non-zero it means that the + * connection establishment failed. + * + * @param conn New connection object. + * @param err HCI error. Zero for success, non-zero otherwise. + */ + void (*connected)(struct bt_mesh_conn *conn, u8_t err); + + /** @brief A connection has been disconnected. + * + * This callback notifies the application that a connection + * has been disconnected. + * + * @param conn Connection object. + * @param reason HCI reason for the disconnection. + */ + void (*disconnected)(struct bt_mesh_conn *conn, u8_t reason); +}; + +struct bt_mesh_prov_conn_cb { + void (*connected)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, int id); + + void (*disconnected)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, u8_t reason); + + ssize_t (*prov_write_descr)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn); + + ssize_t (*prov_notify)(struct bt_mesh_conn *conn, u8_t *data, u16_t len); + + ssize_t (*proxy_write_descr)(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn); + + ssize_t (*proxy_notify)(struct bt_mesh_conn *conn, u8_t *data, u16_t len); +}; + +/** @brief GATT Attribute structure. */ +struct bt_mesh_gatt_attr { + /** Attribute UUID */ + const struct bt_mesh_uuid *uuid; + + /** Attribute read callback + * + * @param conn The connection that is requesting to read + * @param attr The attribute that's being read + * @param buf Buffer to place the read result in + * @param len Length of data to read + * @param offset Offset to start reading from + * + * @return Number fo bytes read, or in case of an error + * BLE_MESH_GATT_ERR() with a specific ATT error code. + */ + ssize_t (*read)(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, + u16_t offset); + + /** Attribute write callback + * + * @param conn The connection that is requesting to write + * @param attr The attribute that's being written + * @param buf Buffer with the data to write + * @param len Number of bytes in the buffer + * @param offset Offset to start writing from + * @param flags Flags (BT_GATT_WRITE_*) + * + * @return Number of bytes written, or in case of an error + * BLE_MESH_GATT_ERR() with a specific ATT error code. + */ + ssize_t (*write)(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + const void *buf, u16_t len, + u16_t offset, u8_t flags); + + /** Attribute user data */ + void *user_data; + /** Attribute handle */ + u16_t handle; + /** Attribute permissions */ + u8_t perm; +}; + +/** @def BLE_MESH_GATT_PRIMARY_SERVICE + * @brief Primary Service Declaration Macro. + * + * Helper macro to declare a primary service attribute. + * + * @param _service Service attribute value. + */ +#define BLE_MESH_GATT_PRIMARY_SERVICE(_service) \ +{ \ + .uuid = BLE_MESH_UUID_GATT_PRIMARY, \ + .perm = BLE_MESH_GATT_PERM_READ, \ + .read = bt_mesh_gatts_attr_read_service, \ + .user_data = _service, \ +} + +/** @def BLE_MESH_GATT_SECONDARY_SERVICE + * @brief Secondary Service Declaration Macro. + * + * Helper macro to declare a secondary service attribute. + * + * @param _service Service attribute value. + */ +#define BLE_MESH_GATT_SECONDARY_SERVICE(_service) \ +{ \ + .uuid = BLE_MESH_UUID_GATT_SECONDARY, \ + .perm = BLE_MESH_GATT_PERM_READ, \ + .read = bt_mesh_gatts_attr_read_service, \ + .user_data = _service, \ +} + +/** @def BLE_MESH_GATT_INCLUDE_SERVICE + * @brief Include Service Declaration Macro. + * + * Helper macro to declare database internal include service attribute. + * + * @param _service_incl the first service attribute of service to include + */ +#define BLE_MESH_GATT_INCLUDE_SERVICE(_service_incl) \ +{ \ + .uuid = BLE_MESH_UUID_GATT_INCLUDE, \ + .perm = BLE_MESH_GATT_PERM_READ, \ + .read = bt_mesh_gatts_attr_read_included, \ + .user_data = _service_incl, \ +} + +/** @def BLE_MESH_GATT_CHARACTERISTIC + * @brief Characteristic Declaration Macro. + * + * Helper macro to declare a characteristic attribute. + * + * @param _uuid Characteristic attribute uuid. + * @param _props Characteristic attribute properties. + */ +#define BLE_MESH_GATT_CHARACTERISTIC(_uuid, _props) \ +{ \ + .uuid = BLE_MESH_UUID_GATT_CHRC, \ + .perm = BLE_MESH_GATT_PERM_READ, \ + .read = bt_mesh_gatts_attr_read_chrc, \ + .user_data = (&(struct bt_mesh_gatt_char) { .uuid = _uuid, \ + .properties = _props, }), \ +} + +/** @def BLE_MESH_GATT_DESCRIPTOR + * @brief Descriptor Declaration Macro. + * + * Helper macro to declare a descriptor attribute. + * + * @param _uuid Descriptor attribute uuid. + * @param _perm Descriptor attribute access permissions. + * @param _read Descriptor attribute read callback. + * @param _write Descriptor attribute write callback. + * @param _value Descriptor attribute value. + */ +#define BLE_MESH_GATT_DESCRIPTOR(_uuid, _perm, _read, _write, _value) \ +{ \ + .uuid = _uuid, \ + .perm = _perm, \ + .read = _read, \ + .write = _write, \ + .user_data = _value, \ +} + +/** @def BLE_MESH_GATT_SERVICE + * @brief Service Structure Declaration Macro. + * + * Helper macro to declare a service structure. + * + * @param _attrs Service attributes. + */ +#define BLE_MESH_GATT_SERVICE(_attrs) \ +{ \ + .attrs = _attrs, \ + .attr_count = ARRAY_SIZE(_attrs), \ +} + +int bt_mesh_host_init(void); + +int bt_le_adv_start(const struct bt_mesh_adv_param *param, + const struct bt_mesh_adv_data *ad, size_t ad_len, + const struct bt_mesh_adv_data *sd, size_t sd_len); + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +int bt_mesh_ble_adv_start(const struct bt_mesh_ble_adv_param *param, + const struct bt_mesh_ble_adv_data *data); +#endif + +int bt_le_adv_stop(void); + +int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb); + +int bt_le_scan_stop(void); + +typedef enum { + BLE_MESH_WHITELIST_REMOVE, + BLE_MESH_WHITELIST_ADD, +} bt_mesh_wl_operation; + +struct bt_mesh_white_list { + bool add_remove; + u8_t remote_bda[BLE_MESH_ADDR_LEN]; + u8_t addr_type; + /* For Bluedroid host, this callback is used to notify the + * result of updating white list. + */ + void (*update_wl_comp_cb)(u8_t status, bt_mesh_wl_operation wl_operation); +}; + +int bt_le_update_white_list(struct bt_mesh_white_list *wl); + +void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb); +void bt_mesh_gatts_conn_cb_deregister(void); + +int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason); + +int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc); +int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc); + +int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc); + +ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset); + +ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t buf_len, u16_t offset, + const void *value, u16_t value_len); + +ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset); + +ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, void *buf, + u16_t len, u16_t offset); + +int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len); + +u16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn); + +/** APIs added by Espressif */ +int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc); +int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc); + +int bt_mesh_gatts_set_local_device_name(const char *name); + +void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb); +void bt_mesh_gattc_conn_cb_deregister(void); + +u8_t bt_mesh_gattc_get_free_conn_count(void); + +u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn); + +int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid); + +void bt_gattc_conn_close(struct bt_mesh_conn *conn); + +void bt_mesh_gattc_exchange_mtu(u8_t index); + +u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn); + +int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len); + +void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn); + +struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn); + +void bt_mesh_conn_unref(struct bt_mesh_conn *conn); + +void bt_mesh_gatt_init(void); +void bt_mesh_gatt_deinit(void); + +void bt_mesh_adapt_init(void); + +int bt_mesh_rand(void *buf, size_t len); + +void bt_mesh_set_private_key(const u8_t pri_key[32]); + +const u8_t *bt_mesh_pub_key_get(void); + +bool bt_mesh_check_public_key(const uint8_t key[64]); + +int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx); + +int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]); + +int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]); + +enum { + BLE_MESH_EXCEP_LIST_ADD = 0, + BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_LIST_CLEAN, +}; + +enum { + BLE_MESH_EXCEP_INFO_ADV_ADDR = 0, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, + BLE_MESH_EXCEP_INFO_MESH_BEACON, + BLE_MESH_EXCEP_INFO_MESH_PROV_ADV, + BLE_MESH_EXCEP_INFO_MESH_PROXY_ADV, +}; + +enum { + BLE_MESH_EXCEP_CLEAN_ADDR_LIST = BIT(0), + BLE_MESH_EXCEP_CLEAN_MESH_LINK_ID_LIST = BIT(1), + BLE_MESH_EXCEP_CLEAN_MESH_BEACON_LIST = BIT(2), + BLE_MESH_EXCEP_CLEAN_MESH_PROV_ADV_LIST = BIT(3), + BLE_MESH_EXCEP_CLEAN_MESH_PROXY_ADV_LIST = BIT(4), + BLE_MESH_EXCEP_CLEAN_ALL_LIST = 0xFFFF, +}; + +int bt_mesh_update_exceptional_list(u8_t sub_code, u8_t type, void *info); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_BEARER_ADAPT_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h new file mode 100644 index 000000000..75ff8ccf3 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_hci.h @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2015-2016 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_HCI_H_ +#define _BLE_MESH_HCI_H_ + +#include "mesh_atomic.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Porting form zephyr/subsys/bluetooth/host/hci_core.h */ + +#define BLE_MESH_LMP_FEAT_PAGES_COUNT 1 + +/* bt_mesh_dev flags: the flags defined here represent BT controller state */ +enum { + BLE_MESH_DEV_ENABLE, + BLE_MESH_DEV_READY, + BLE_MESH_DEV_ID_STATIC_RANDOM, + BLE_MESH_DEV_HAS_PUB_KEY, + BLE_MESH_DEV_PUB_KEY_BUSY, + + BLE_MESH_DEV_ADVERTISING, + BLE_MESH_DEV_KEEP_ADVERTISING, + BLE_MESH_DEV_SCANNING, + BLE_MESH_DEV_EXPLICIT_SCAN, + BLE_MESH_DEV_ACTIVE_SCAN, + BLE_MESH_DEV_SCAN_FILTER_DUP, + + BLE_MESH_DEV_RPA_VALID, + + BLE_MESH_DEV_ID_PENDING, + + /* Total number of flags - must be at the end of the enum */ + BLE_MESH_DEV_NUM_FLAGS, +}; + +struct bt_mesh_dev_le { + /* LE features */ + u8_t features[8]; + + /* LE states */ + u64_t states; +}; + +/* State tracking for the local Bluetooth controller */ +struct bt_mesh_dev { + /* Flags indicate which functionality is enabled */ + BLE_MESH_ATOMIC_DEFINE(flags, BLE_MESH_DEV_NUM_FLAGS); + + /* Controller version & manufacturer information */ + u8_t hci_version; + u8_t lmp_version; + u16_t hci_revision; + u16_t lmp_subversion; + u16_t manufacturer; + + /* LMP features (pages 0, 1, 2) */ + u8_t features[BLE_MESH_LMP_FEAT_PAGES_COUNT][8]; + + /* LE controller specific features */ + struct bt_mesh_dev_le le; +}; + +/*Porting from zephyr/subsys/bluetooth/host/hci_core.h */ +/* HCI version from Assigned Numbers */ +#define BLE_MESH_HCI_VERSION_1_0B 0 +#define BLE_MESH_HCI_VERSION_1_1 1 +#define BLE_MESH_HCI_VERSION_1_2 2 +#define BLE_MESH_HCI_VERSION_2_0 3 +#define BLE_MESH_HCI_VERSION_2_1 4 +#define BLE_MESH_HCI_VERSION_3_0 5 +#define BLE_MESH_HCI_VERSION_4_0 6 +#define BLE_MESH_HCI_VERSION_4_1 7 +#define BLE_MESH_HCI_VERSION_4_2 8 +#define BLE_MESH_HCI_VERSION_5_0 9 + +/* OpCode Group Fields */ +#define BLE_MESH_OGF_LINK_CTRL 0x01 +#define BLE_MESH_OGF_BASEBAND 0x03 +#define BLE_MESH_OGF_INFO 0x04 +#define BLE_MESH_OGF_STATUS 0x05 +#define BLE_MESH_OGF_LE 0x08 +#define BLE_MESH_OGF_VS 0x3f + +/* Construct OpCode from OGF and OCF */ +#define BLE_MESH_OP(ogf, ocf) ((ocf) | ((ogf) << 10)) + +/* Obtain OGF from OpCode */ +#define BLE_MESH_OGF(opcode) (((opcode) >> 10) & BIT_MASK(6)) + +/* Obtain OCF from OpCode */ +#define BLE_MESH_OCF(opcode) ((opcode) & BIT_MASK(10)) + +#define BLE_MESH_HCI_OP_SET_ADV_PARAM BLE_MESH_OP(BLE_MESH_OGF_LE, 0x0006) +struct bt_mesh_hci_cp_set_adv_param { + u16_t min_interval; + u16_t max_interval; + u8_t type; + u8_t own_addr_type; + bt_mesh_addr_t direct_addr; + u8_t channel_map; + u8_t filter_policy; +} __packed; + +#define BLE_MESH_HCI_OP_SET_ADV_DATA BLE_MESH_OP(BLE_MESH_OGF_LE, 0x0008) +struct bt_mesh_hci_cp_set_adv_data { + u8_t len; + u8_t data[31]; +} __packed; + +#define BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA BLE_MESH_OP(BLE_MESH_OGF_LE, 0x0009) +struct bt_mesh_hci_cp_set_scan_rsp_data { + u8_t len; + u8_t data[31]; +} __packed; + +/* Added by Espressif */ +extern struct bt_mesh_dev bt_mesh_dev; + +void bt_mesh_hci_init(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_HCI_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h new file mode 100644 index 000000000..362c0bec6 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h @@ -0,0 +1,648 @@ +/** @file + * @brief Bluetooth Mesh Profile APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_MAIN_H_ +#define _BLE_MESH_MAIN_H_ + +#include "mesh_access.h" + +/** + * @brief Bluetooth Mesh Provisioning + * @defgroup bt_mesh_prov Bluetooth Mesh Provisioning + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + BLE_MESH_NO_OUTPUT = 0, + BLE_MESH_BLINK = BIT(0), + BLE_MESH_BEEP = BIT(1), + BLE_MESH_VIBRATE = BIT(2), + BLE_MESH_DISPLAY_NUMBER = BIT(3), + BLE_MESH_DISPLAY_STRING = BIT(4), +} bt_mesh_output_action_t; + +typedef enum { + BLE_MESH_NO_INPUT = 0, + BLE_MESH_PUSH = BIT(0), + BLE_MESH_TWIST = BIT(1), + BLE_MESH_ENTER_NUMBER = BIT(2), + BLE_MESH_ENTER_STRING = BIT(3), +} bt_mesh_input_action_t; + +typedef enum { + BLE_MESH_PROV_ADV = BIT(0), + BLE_MESH_PROV_GATT = BIT(1), +} bt_mesh_prov_bearer_t; + +typedef enum { + BLE_MESH_PROV_OOB_OTHER = BIT(0), + BLE_MESH_PROV_OOB_URI = BIT(1), + BLE_MESH_PROV_OOB_2D_CODE = BIT(2), + BLE_MESH_PROV_OOB_BAR_CODE = BIT(3), + BLE_MESH_PROV_OOB_NFC = BIT(4), + BLE_MESH_PROV_OOB_NUMBER = BIT(5), + BLE_MESH_PROV_OOB_STRING = BIT(6), + /* 7 - 10 are reserved */ + BLE_MESH_PROV_OOB_ON_BOX = BIT(11), + BLE_MESH_PROV_OOB_IN_BOX = BIT(12), + BLE_MESH_PROV_OOB_ON_PAPER = BIT(13), + BLE_MESH_PROV_OOB_IN_MANUAL = BIT(14), + BLE_MESH_PROV_OOB_ON_DEV = BIT(15), +} bt_mesh_prov_oob_info_t; + +#define BLE_MESH_PROV_STATIC_OOB_MAX_LEN 16 +#define BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN 8 +#define BLE_MESH_PROV_INPUT_OOB_MAX_LEN 8 + +/** Provisioning properties & capabilities. */ +struct bt_mesh_prov { +#if CONFIG_BLE_MESH_NODE + /** The UUID that's used when advertising as unprovisioned */ + const u8_t *uuid; + + /** Optional URI. This will be advertised separately from the + * unprovisioned beacon, however the unprovisioned beacon will + * contain a hash of it so the two can be associated by the + * provisioner. + */ + const char *uri; + + /** Out of Band information field. */ + bt_mesh_prov_oob_info_t oob_info; + + /** Flag indicates whether unprovisioned devices support OOB public key */ + bool oob_pub_key; + + /** @brief Set device OOB public key. + * + * This callback notifies the application to + * set OOB public key & private key pair. + */ + void (*oob_pub_key_cb)(void); + + /** Static OOB value */ + const u8_t *static_val; + /** Static OOB value length */ + u8_t static_val_len; + + /** Maximum size of Output OOB supported */ + u8_t output_size; + /** Supported Output OOB Actions */ + u16_t output_actions; + + /* Maximum size of Input OOB supported */ + u8_t input_size; + /** Supported Input OOB Actions */ + u16_t input_actions; + + /** @brief Output of a number is requested. + * + * This callback notifies the application to + * output the given number using the given action. + * + * @param act Action for outputting the number. + * @param num Number to be out-put. + * + * @return Zero on success or negative error code otherwise + */ + int (*output_number)(bt_mesh_output_action_t act, u32_t num); + + /** @brief Output of a string is requested. + * + * This callback notifies the application to + * display the given string to the user. + * + * @param str String to be displayed. + * + * @return Zero on success or negative error code otherwise + */ + int (*output_string)(const char *str); + + /** @brief Input is requested. + * + * This callback notifies the application to request + * input from the user using the given action. The + * requested input will either be a string or a number, and + * the application needs to consequently call the + * bt_mesh_input_string() or bt_mesh_input_number() functions + * once the data has been acquired from the user. + * + * @param act Action for inputting data. + * @param num Maximum size of the in-put data. + * + * @return Zero on success or negative error code otherwise + */ + int (*input)(bt_mesh_input_action_t act, u8_t size); + + /** @brief Provisioning link has been opened. + * + * This callback notifies the application that a provisioning + * link has been opened on the given provisioning bearer. + * + * @param bearer Provisioning bearer. + */ + void (*link_open)(bt_mesh_prov_bearer_t bearer); + + /** @brief Provisioning link has been closed. + * + * This callback notifies the application that a provisioning + * link has been closed on the given provisioning bearer. + * + * @param bearer Provisioning bearer. + */ + void (*link_close)(bt_mesh_prov_bearer_t bearer); + + /** @brief Provisioning is complete. + * + * This callback notifies the application that provisioning has + * been successfully completed, and that the local node has been + * assigned the specified NetKeyIndex and primary element address. + * + * @param net_idx NetKeyIndex given during provisioning. + * @param net_key NetKey given during provisioning. + * @param addr Primary element address. + * @param flags Key Refresh & IV Update flags + * @param iv_index IV Index. + */ + void (*complete)(u16_t net_idx, const u8_t net_key[16], u16_t addr, u8_t flags, u32_t iv_index); + + /** @brief Node has been reset. + * + * This callback notifies the application that the local node + * has been reset and needs to be reprovisioned. The node will + * not automatically advertise as unprovisioned, rather the + * bt_mesh_prov_enable() API needs to be called to enable + * unprovisioned advertising on one or more provisioning bearers. + */ + void (*reset)(void); +#endif /* CONFIG_BLE_MESH_NODE */ + +#if CONFIG_BLE_MESH_PROVISIONER + /* Provisioner device uuid */ + const u8_t *prov_uuid; + + /* + * Primary element address of the provisioner. + * No need to initialize it for fast provisioning. + */ + const u16_t prov_unicast_addr; + + /* + * Starting unicast address going to assigned. + * No need to initialize it for fast provisioning. + */ + u16_t prov_start_address; + + /* Attention timer contained in Provisioning Invite */ + u8_t prov_attention; + + /* Provisioner provisioning Algorithm */ + u8_t prov_algorithm; + + /* Provisioner public key oob */ + u8_t prov_pub_key_oob; + + /** @brief Input is requested. + * + * This callback notifies the application that it should + * read device's public key with OOB + * + * @param link_idx: The provisioning link index + * + * @return Zero on success or negative error code otherwise + */ + int (*prov_pub_key_oob_cb)(u8_t link_idx); + + /* Provisioner static oob value */ + u8_t *prov_static_oob_val; + + /* Provisioner static oob value length */ + u8_t prov_static_oob_len; + + /** @brief Provisioner input a number read from device output + * + * This callback notifies the application that it should + * input the number given by the device. + * + * @param method: The OOB authentication method + * @param act: The output action of the device + * @param size: The output size of the device + * @param link_idx: The provisioning link index + * + * @return Zero on success or negative error code otherwise + */ + int (*prov_input_num)(u8_t method, bt_mesh_output_action_t act, u8_t size, u8_t link_idx); + + /** @brief Provisioner output a number to the device + * + * This callback notifies the application that it should + * output the number to the device. + * + * @param method: The OOB authentication method + * @param act: The input action of the device + * @param data: The input number/string of the device + * @param size: The input size of the device + * @param link_idx: The provisioning link index + * + * @return Zero on success or negative error code otherwise + */ + int (*prov_output_num)(u8_t method, bt_mesh_input_action_t act, void *data, u8_t size, u8_t link_idx); + + /* + * Key refresh and IV update flag. + * No need to initialize it for fast provisioning. + */ + u8_t flags; + + /* + * IV index. No need to initialize it for fast provisioning. + */ + u32_t iv_index; + + /** @brief Provisioner has opened a provisioning link. + * + * This callback notifies the application that a provisioning + * link has been opened on the given provisioning bearer. + * + * @param bearer Provisioning bearer. + */ + void (*prov_link_open)(bt_mesh_prov_bearer_t bearer); + + /** @brief Provisioner has closed a provisioning link. + * + * This callback notifies the application that a provisioning + * link has been closed on the given provisioning bearer. + * + * @param bearer Provisioning bearer. + * @param reason Provisioning link close reason(disconnect reason) + */ + void (*prov_link_close)(bt_mesh_prov_bearer_t bearer, u8_t reason); + + /** @brief Provision one device is complete. + * + * This callback notifies the application that provisioner has + * successfully provisioned a device, and the node has been assigned + * the specified NetKeyIndex and primary element address. + * + * @param node_idx Node index within the node(provisioned device) queue. + * @param device_uuid Provisioned device uuid pointer. + * @param unicast_addr Provisioned device assigned unicast address. + * @param element_num Provisioned device element number. + * @param netkey_idx Provisioned device assigned netkey index. + */ + void (*prov_complete)(u16_t node_idx, const u8_t device_uuid[16], + u16_t unicast_addr, u8_t element_num, + u16_t netkey_idx); +#endif /* CONFIG_BLE_MESH_PROVISIONER */ +}; + +enum ble_mesh_role { + NODE = 0, + PROVISIONER, + FAST_PROV, + ROLE_NVAL, +}; + +/* The following APIs are for BLE Mesh Node */ + +/** @brief Provide provisioning input OOB string. + * + * This is intended to be called after the bt_mesh_prov input callback + * has been called with BLE_MESH_ENTER_STRING as the action. + * + * @param str String. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_input_string(const char *str); + +/** @brief Provide provisioning input OOB number. + * + * This is intended to be called after the bt_mesh_prov input callback + * has been called with BLE_MESH_ENTER_NUMBER as the action. + * + * @param num Number. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_input_number(u32_t num); + +/** @brief Enable specific provisioning bearers + * + * Enable one or more provisioning bearers. + * + * @param Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers); + +/** @brief Disable specific provisioning bearers + * + * Disable one or more provisioning bearers. + * + * @param Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers); + +/* The following API is for BLE Mesh Fast Provisioning */ + +/** @brief Change the device action + * + * @param[IN] action: role of device to be set + * 0x01 - enter, 0x02 - suspend, 0x03 - exit + * + * @return status + */ +u8_t bt_mesh_set_fast_prov_action(u8_t action); + +/* The following APIs are for BLE Mesh Provisioner */ + +/** @brief Provide provisioning input OOB string. + * + * This is intended to be called after the bt_mesh_prov input callback + * has been called with BLE_MESH_ENTER_STRING as the action. + * + * @param str String. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_prov_input_string(const char *str); + +/** @brief Provide provisioning input OOB number. + * + * This is intended to be called after the bt_mesh_prov input callback + * has been called with BLE_MESH_ENTER_NUMBER as the action. + * + * @param num Number. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_prov_input_number(u32_t num); + +/** @brief Enable Provisioner corresponding functionalities, e.g. scan, etc. + * + * @param bearers Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers); + +/** @brief Enable specific provisioning bearers + * + * Enable one or more provisioning bearers. + * + * @param bearers Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers); + +/** @brief Disable specific provisioning bearers + * + * Disable one or more provisioning bearers. + * + * @param bearers Bit-wise OR of provisioning bearers. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers); + +/** + * @} + */ + +/** + * @brief Bluetooth Mesh + * @defgroup bt_mesh Bluetooth Mesh + * @ingroup bluetooth + * @{ + */ + +/* Primary Network Key index */ +#define BLE_MESH_NET_PRIMARY 0x000 + +#define BLE_MESH_RELAY_DISABLED 0x00 +#define BLE_MESH_RELAY_ENABLED 0x01 +#define BLE_MESH_RELAY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_BEACON_DISABLED 0x00 +#define BLE_MESH_BEACON_ENABLED 0x01 + +#define BLE_MESH_GATT_PROXY_DISABLED 0x00 +#define BLE_MESH_GATT_PROXY_ENABLED 0x01 +#define BLE_MESH_GATT_PROXY_NOT_SUPPORTED 0x02 + +#define BLE_MESH_FRIEND_DISABLED 0x00 +#define BLE_MESH_FRIEND_ENABLED 0x01 +#define BLE_MESH_FRIEND_NOT_SUPPORTED 0x02 + +#define BLE_MESH_NODE_IDENTITY_STOPPED 0x00 +#define BLE_MESH_NODE_IDENTITY_RUNNING 0x01 +#define BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02 + +/* Features */ +#define BLE_MESH_FEAT_RELAY BIT(0) +#define BLE_MESH_FEAT_PROXY BIT(1) +#define BLE_MESH_FEAT_FRIEND BIT(2) +#define BLE_MESH_FEAT_LOW_POWER BIT(3) +#define BLE_MESH_FEAT_SUPPORTED (BLE_MESH_FEAT_RELAY | \ + BLE_MESH_FEAT_PROXY | \ + BLE_MESH_FEAT_FRIEND | \ + BLE_MESH_FEAT_LOW_POWER) + +/** @brief Initialize Mesh support + * + * After calling this API, the node will not automatically advertise as + * unprovisioned, rather the bt_mesh_prov_enable() API needs to be called + * to enable unprovisioned advertising on one or more provisioning bearers. + * + * @param prov Node provisioning information. + * @param comp Node Composition. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_init(const struct bt_mesh_prov *prov, + const struct bt_mesh_comp *comp); + +/* BLE Mesh deinit parameters */ +struct bt_mesh_deinit_param { + bool erase; /* Indicate if erasing flash when deinit mesh stack */ +}; + +/** @brief De-initialize Mesh support + * + * @param param BLE Mesh deinit parameters. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_deinit(struct bt_mesh_deinit_param *param); + +/** @brief Reset the state of the local Mesh node. + * + * Resets the state of the node, which means that it needs to be + * reprovisioned to become an active node in a Mesh network again. + * + * After calling this API, the node will not automatically advertise as + * unprovisioned, rather the bt_mesh_prov_enable() API needs to be called + * to enable unprovisioned advertising on one or more provisioning bearers. + * + */ +void bt_mesh_node_reset(void); + +/** @brief Suspend the Mesh network temporarily. + * + * This API can be used for power saving purposes, but the user should be + * aware that leaving the local node suspended for a long period of time + * may cause it to become permanently disconnected from the Mesh network. + * If at all possible, the Friendship feature should be used instead, to + * make the node into a Low Power Node. + * + * @return 0 on success, or (negative) error code on failure. + */ +int bt_mesh_suspend(void); + +/** @brief Resume a suspended Mesh network. + * + * This API resumes the local node, after it has been suspended using the + * bt_mesh_suspend() API. + * + * @return 0 on success, or (negative) error code on failure. + */ +int bt_mesh_resume(void); + +/** @brief Provision the local Mesh Node. + * + * This API should normally not be used directly by the application. The + * only exception is for testing purposes where manual provisioning is + * desired without an actual external provisioner. + * + * @param net_key Network Key + * @param net_idx Network Key Index + * @param flags Provisioning Flags + * @param iv_index IV Index + * @param addr Primary element address + * @param dev_key Device Key + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, + u8_t flags, u32_t iv_index, u16_t addr, + const u8_t dev_key[16]); + +/** @brief Check if the device is an unprovisioned device + * and will act as a node once provisioned. + * + * @return true - yes, false - no. + */ +bool bt_mesh_is_node(void); + +/** @brief Check if the local node has been provisioned. + * + * This API can be used to check if the local node has been provisioned + * or not. It can e.g. be helpful to determine if there was a stored + * network in flash, i.e. if the network was restored after calling + * settings_load(). + * + * @return True if the node is provisioned. False otherwise. + */ +bool bt_mesh_is_provisioned(void); + +/** @brief Check if the device is a Provisioner. + * + * @return true - yes, false - no. + */ +bool bt_mesh_is_provisioner(void); + +/** @brief Check if the Provisioner is enabled + * + * @return true - enabled, false - disabled. + */ +bool bt_mesh_is_provisioner_en(void); + +/** @brief Toggle the IV Update test mode + * + * This API is only available if the IV Update test mode has been enabled + * in Kconfig. It is needed for passing most of the IV Update qualification + * test cases. + * + * @param enable true to enable IV Update test mode, false to disable it. + */ +void bt_mesh_iv_update_test(bool enable); + +/** @brief Toggle the IV Update state + * + * This API is only available if the IV Update test mode has been enabled + * in Kconfig. It is needed for passing most of the IV Update qualification + * test cases. + * + * @return true if IV Update In Progress state was entered, false otherwise. + */ +bool bt_mesh_iv_update(void); + +/** @brief Toggle the Low Power feature of the local device + * + * Enables or disables the Low Power feature of the local device. This is + * exposed as a run-time feature, since the device might want to change + * this e.g. based on being plugged into a stable power source or running + * from a battery power source. + * + * @param enable true to enable LPN functionality, false to disable it. + * @param force when disable LPN functionality, use this flag to indicate + * whether directly clear corresponding information or sending + * friend clear to disable it. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_lpn_set(bool enable, bool force); + +/** @brief Send out a Friend Poll message. + * + * Send a Friend Poll message to the Friend of this node. If there is no + * established Friendship the function will return an error. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_lpn_poll(void); + +/** @brief Register a callback for Friendship changes. + * + * Registers a callback that will be called whenever Friendship gets + * established or is lost. + * + * @param cb Function to call when the Friendship status changes. + */ +void bt_mesh_lpn_set_cb(void (*cb)(u16_t friend_addr, bool established)); + +/** @brief Register a callback for Friendship changes of friend node. + * + * Registers a callback that will be called whenever Friendship gets + * established or is terminated. + * + * @param cb Function to call when the Friendship status of friend node changes. + */ +void bt_mesh_friend_set_cb(void (*cb)(bool establish, u16_t lpn_addr, u8_t reason)); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* _BLE_MESH_MAIN_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_proxy.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_proxy.h new file mode 100644 index 000000000..5450aa328 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_proxy.h @@ -0,0 +1,45 @@ +/** @file + * @brief Bluetooth Mesh Proxy APIs. + */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_PROXY_H_ +#define _BLE_MESH_PROXY_H_ + +#include +/** + * @brief Bluetooth Mesh Proxy + * @defgroup bt_mesh_proxy Bluetooth Mesh Proxy + * @ingroup bt_mesh + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enable advertising with Node Identity. + * + * This API requires that GATT Proxy support has been enabled. Once called + * each subnet will start advertising using Node Identity for the next + * 60 seconds. + * + * @return 0 on success, or (negative) error code on failure. + */ +int bt_mesh_proxy_identity_enable(void); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* _BLE_MESH_PROXY_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/include/mesh_uuid.h b/components/bt/esp_ble_mesh/mesh_core/include/mesh_uuid.h new file mode 100644 index 000000000..de03df5c3 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/include/mesh_uuid.h @@ -0,0 +1,530 @@ +/** @file + * @brief Bluetooth UUID handling + */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _BLE_MESH_UUID_H_ +#define _BLE_MESH_UUID_H_ + +/** + * @brief UUIDs + * @defgroup bt_uuid UUIDs + * @ingroup bluetooth + * @{ + */ + +#include "mesh_util.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Bluetooth UUID types */ +enum { + BLE_MESH_UUID_TYPE_16, + BLE_MESH_UUID_TYPE_32, + BLE_MESH_UUID_TYPE_128, +}; + +/** @brief This is a 'tentative' type and should be used as a pointer only */ +struct bt_mesh_uuid { + u8_t type; +}; + +struct bt_mesh_uuid_16 { + struct bt_mesh_uuid uuid; + u16_t val; +}; + +struct bt_mesh_uuid_32 { + struct bt_mesh_uuid uuid; + u32_t val; +}; + +struct bt_mesh_uuid_128 { + struct bt_mesh_uuid uuid; + u8_t val[16]; +}; + +#define BLE_MESH_UUID_INIT_16(value) \ +{ \ + .uuid.type = BLE_MESH_UUID_TYPE_16, \ + .val = (value), \ +} + +#define BLE_MESH_UUID_INIT_32(value) \ +{ \ + .uuid.type = BLE_MESH_UUID_TYPE_32, \ + .val = (value), \ +} + +#define BLE_MESH_UUID_INIT_128(value...) \ +{ \ + .uuid.type = BLE_MESH_UUID_TYPE_128, \ + .val = { value }, \ +} + +#define BLE_MESH_UUID_DECLARE_16(value) \ + ((struct bt_mesh_uuid *) (&(struct bt_mesh_uuid_16) BLE_MESH_UUID_INIT_16(value))) + +#define BLE_MESH_UUID_DECLARE_32(value) \ + ((struct bt_mesh_uuid *) (&(struct bt_mesh_uuid_32) BLE_MESH_UUID_INIT_32(value))) + +#define BLE_MESH_UUID_DECLARE_128(value...) \ + ((struct bt_mesh_uuid *) (&(struct bt_mesh_uuid_128) BLE_MESH_UUID_INIT_128(value))) + +#define BLE_MESH_UUID_16(__u) CONTAINER_OF(__u, struct bt_mesh_uuid_16, uuid) +#define BLE_MESH_UUID_32(__u) CONTAINER_OF(__u, struct bt_mesh_uuid_32, uuid) +#define BLE_MESH_UUID_128(__u) CONTAINER_OF(__u, struct bt_mesh_uuid_128, uuid) + +/** @def BLE_MESH_UUID_GAP + * @brief Generic Access + */ +#define BLE_MESH_UUID_GAP BLE_MESH_UUID_DECLARE_16(0x1800) +#define BLE_MESH_UUID_GAP_VAL 0x1800 +/** @def BLE_MESH_UUID_GATT + * @brief Generic Attribute + */ +#define BLE_MESH_UUID_GATT BLE_MESH_UUID_DECLARE_16(0x1801) +#define BLE_MESH_UUID_GATT_VAL 0x1801 +/** @def BLE_MESH_UUID_CTS + * @brief Current Time Service + */ +#define BLE_MESH_UUID_CTS BLE_MESH_UUID_DECLARE_16(0x1805) +#define BLE_MESH_UUID_CTS_VAL 0x1805 +/** @def BLE_MESH_UUID_DIS + * @brief Device Information Service + */ +#define BLE_MESH_UUID_DIS BLE_MESH_UUID_DECLARE_16(0x180a) +#define BLE_MESH_UUID_DIS_VAL 0x180a +/** @def BLE_MESH_UUID_HRS + * @brief Heart Rate Service + */ +#define BLE_MESH_UUID_HRS BLE_MESH_UUID_DECLARE_16(0x180d) +#define BLE_MESH_UUID_HRS_VAL 0x180d +/** @def BLE_MESH_UUID_BAS + * @brief Battery Service + */ +#define BLE_MESH_UUID_BAS BLE_MESH_UUID_DECLARE_16(0x180f) +#define BLE_MESH_UUID_BAS_VAL 0x180f +/** @def BLE_MESH_UUID_HIDS + * @brief HID Service + */ +#define BLE_MESH_UUID_HIDS BLE_MESH_UUID_DECLARE_16(0x1812) +#define BLE_MESH_UUID_HIDS_VAL 0x1812 +/** @def BLE_MESH_UUID_CSC + * @brief Cycling Speed and Cadence Service + */ +#define BLE_MESH_UUID_CSC BLE_MESH_UUID_DECLARE_16(0x1816) +#define BLE_MESH_UUID_CSC_VAL 0x1816 +/** @def BLE_MESH_UUID_ESS + * @brief Environmental Sensing Service + */ +#define BLE_MESH_UUID_ESS BLE_MESH_UUID_DECLARE_16(0x181a) +#define BLE_MESH_UUID_ESS_VAL 0x181a +/** @def BLE_MESH_UUID_IPSS + * @brief IP Support Service + */ +#define BLE_MESH_UUID_IPSS BLE_MESH_UUID_DECLARE_16(0x1820) +#define BLE_MESH_UUID_IPSS_VAL 0x1820 +/** @def BLE_MESH_UUID_MESH_PROV + * @brief Mesh Provisioning Service + */ +#define BLE_MESH_UUID_MESH_PROV BLE_MESH_UUID_DECLARE_16(0x1827) +#define BLE_MESH_UUID_MESH_PROV_VAL 0x1827 +/** @def BLE_MESH_UUID_MESH_PROXY + * @brief Mesh Proxy Service + */ +#define BLE_MESH_UUID_MESH_PROXY BLE_MESH_UUID_DECLARE_16(0x1828) +#define BLE_MESH_UUID_MESH_PROXY_VAL 0x1828 +/** @def BLE_MESH_UUID_GATT_PRIMARY + * @brief GATT Primary Service + */ +#define BLE_MESH_UUID_GATT_PRIMARY BLE_MESH_UUID_DECLARE_16(0x2800) +#define BLE_MESH_UUID_GATT_PRIMARY_VAL 0x2800 +/** @def BLE_MESH_UUID_GATT_SECONDARY + * @brief GATT Secondary Service + */ +#define BLE_MESH_UUID_GATT_SECONDARY BLE_MESH_UUID_DECLARE_16(0x2801) +#define BLE_MESH_UUID_GATT_SECONDARY_VAL 0x2801 +/** @def BLE_MESH_UUID_GATT_INCLUDE + * @brief GATT Include Service + */ +#define BLE_MESH_UUID_GATT_INCLUDE BLE_MESH_UUID_DECLARE_16(0x2802) +#define BLE_MESH_UUID_GATT_INCLUDE_VAL 0x2802 +/** @def BLE_MESH_UUID_GATT_CHRC + * @brief GATT Characteristic + */ +#define BLE_MESH_UUID_GATT_CHRC BLE_MESH_UUID_DECLARE_16(0x2803) +#define BLE_MESH_UUID_GATT_CHRC_VAL 0x2803 +/** @def BLE_MESH_UUID_GATT_CEP + * @brief GATT Characteristic Extended Properties + */ +#define BLE_MESH_UUID_GATT_CEP BLE_MESH_UUID_DECLARE_16(0x2900) +#define BLE_MESH_UUID_GATT_CEP_VAL 0x2900 +/** @def BLE_MESH_UUID_GATT_CUD + * @brief GATT Characteristic User Description + */ +#define BLE_MESH_UUID_GATT_CUD BLE_MESH_UUID_DECLARE_16(0x2901) +#define BLE_MESH_UUID_GATT_CUD_VAL 0x2901 +/** @def BLE_MESH_UUID_GATT_CCC + * @brief GATT Client Characteristic Configuration + */ +#define BLE_MESH_UUID_GATT_CCC BLE_MESH_UUID_DECLARE_16(0x2902) +#define BLE_MESH_UUID_GATT_CCC_VAL 0x2902 +/** @def BLE_MESH_UUID_GATT_SCC + * @brief GATT Server Characteristic Configuration + */ +#define BLE_MESH_UUID_GATT_SCC BLE_MESH_UUID_DECLARE_16(0x2903) +#define BLE_MESH_UUID_GATT_SCC_VAL 0x2903 +/** @def BLE_MESH_UUID_GATT_CPF + * @brief GATT Characteristic Presentation Format + */ +#define BLE_MESH_UUID_GATT_CPF BLE_MESH_UUID_DECLARE_16(0x2904) +#define BLE_MESH_UUID_GATT_CPF_VAL 0x2904 +/** @def BLE_MESH_UUID_VALID_RANGE + * @brief Valid Range Descriptor + */ +#define BLE_MESH_UUID_VALID_RANGE BLE_MESH_UUID_DECLARE_16(0x2906) +#define BLE_MESH_UUID_VALID_RANGE_VAL 0x2906 +/** @def BLE_MESH_UUID_HIDS_EXT_REPORT + * @brief HID External Report Descriptor + */ +#define BLE_MESH_UUID_HIDS_EXT_REPORT BLE_MESH_UUID_DECLARE_16(0x2907) +#define BLE_MESH_UUID_HIDS_EXT_REPORT_VAL 0x2907 +/** @def BLE_MESH_UUID_HIDS_REPORT_REF + * @brief HID Report Reference Descriptor + */ +#define BLE_MESH_UUID_HIDS_REPORT_REF BLE_MESH_UUID_DECLARE_16(0x2908) +#define BLE_MESH_UUID_HIDS_REPORT_REF_VAL 0x2908 +/** @def BLE_MESH_UUID_ES_CONFIGURATION + * @brief Environmental Sensing Configuration Descriptor + */ +#define BLE_MESH_UUID_ES_CONFIGURATION BLE_MESH_UUID_DECLARE_16(0x290b) +#define BLE_MESH_UUID_ES_CONFIGURATION_VAL 0x290b +/** @def BLE_MESH_UUID_ES_MEASUREMENT + * @brief Environmental Sensing Measurement Descriptor + */ +#define BLE_MESH_UUID_ES_MEASUREMENT BLE_MESH_UUID_DECLARE_16(0x290c) +#define BLE_MESH_UUID_ES_MEASUREMENT_VAL 0x290c +/** @def BLE_MESH_UUID_ES_TRIGGER_SETTING + * @brief Environmental Sensing Trigger Setting Descriptor + */ +#define BLE_MESH_UUID_ES_TRIGGER_SETTING BLE_MESH_UUID_DECLARE_16(0x290d) +#define BLE_MESH_UUID_ES_TRIGGER_SETTING_VAL 0x290d +/** @def BLE_MESH_UUID_GAP_DEVICE_NAME + * @brief GAP Characteristic Device Name + */ +#define BLE_MESH_UUID_GAP_DEVICE_NAME BLE_MESH_UUID_DECLARE_16(0x2a00) +#define BLE_MESH_UUID_GAP_DEVICE_NAME_VAL 0x2a00 +/** @def BLE_MESH_UUID_GAP_APPEARANCE + * @brief GAP Characteristic Appearance + */ +#define BLE_MESH_UUID_GAP_APPEARANCE BLE_MESH_UUID_DECLARE_16(0x2a01) +#define BLE_MESH_UUID_GAP_APPEARANCE_VAL 0x2a01 +/** @def BLE_MESH_UUID_GAP_PPCP + * @brief GAP Characteristic Peripheral Preferred Connection Parameters + */ +#define BLE_MESH_UUID_GAP_PPCP BLE_MESH_UUID_DECLARE_16(0x2a04) +#define BLE_MESH_UUID_GAP_PPCP_VAL 0x2a04 +/** @def BLE_MESH_UUID_GATT_SC + * @brief GATT Characteristic Service Changed + */ +#define BLE_MESH_UUID_GATT_SC BLE_MESH_UUID_DECLARE_16(0x2a05) +#define BLE_MESH_UUID_GATT_SC_VAL 0x2a05 +/** @def BLE_MESH_UUID_BAS_BATTERY_LEVEL + * @brief BAS Characteristic Battery Level + */ +#define BLE_MESH_UUID_BAS_BATTERY_LEVEL BLE_MESH_UUID_DECLARE_16(0x2a19) +#define BLE_MESH_UUID_BAS_BATTERY_LEVEL_VAL 0x2a19 +/** @def BLE_MESH_UUID_DIS_SYSTEM_ID + * @brief DIS Characteristic System ID + */ +#define BLE_MESH_UUID_DIS_SYSTEM_ID BLE_MESH_UUID_DECLARE_16(0x2a23) +#define BLE_MESH_UUID_DIS_SYSTEM_ID_VAL 0x2a23 +/** @def BLE_MESH_UUID_DIS_MODEL_NUMBER + * @brief DIS Characteristic Model Number String + */ +#define BLE_MESH_UUID_DIS_MODEL_NUMBER BLE_MESH_UUID_DECLARE_16(0x2a24) +#define BLE_MESH_UUID_DIS_MODEL_NUMBER_VAL 0x2a24 +/** @def BLE_MESH_UUID_DIS_SERIAL_NUMBER + * @brief DIS Characteristic Serial Number String + */ +#define BLE_MESH_UUID_DIS_SERIAL_NUMBER BLE_MESH_UUID_DECLARE_16(0x2a25) +#define BLE_MESH_UUID_DIS_SERIAL_NUMBER_VAL 0x2a25 +/** @def BLE_MESH_UUID_DIS_FIRMWARE_REVISION + * @brief DIS Characteristic Firmware Revision String + */ +#define BLE_MESH_UUID_DIS_FIRMWARE_REVISION BLE_MESH_UUID_DECLARE_16(0x2a26) +#define BLE_MESH_UUID_DIS_FIRMWARE_REVISION_VAL 0x2a26 +/** @def BLE_MESH_UUID_DIS_HARDWARE_REVISION + * @brief DIS Characteristic Hardware Revision String + */ +#define BLE_MESH_UUID_DIS_HARDWARE_REVISION BLE_MESH_UUID_DECLARE_16(0x2a27) +#define BLE_MESH_UUID_DIS_HARDWARE_REVISION_VAL 0x2a27 +/** @def BLE_MESH_UUID_DIS_SOFTWARE_REVISION + * @brief DIS Characteristic Software Revision String + */ +#define BLE_MESH_UUID_DIS_SOFTWARE_REVISION BLE_MESH_UUID_DECLARE_16(0x2a28) +#define BLE_MESH_UUID_DIS_SOFTWARE_REVISION_VAL 0x2a28 +/** @def BLE_MESH_UUID_DIS_MANUFACTURER_NAME + * @brief DIS Characteristic Manufacturer Name String + */ +#define BLE_MESH_UUID_DIS_MANUFACTURER_NAME BLE_MESH_UUID_DECLARE_16(0x2a29) +#define BLE_MESH_UUID_DIS_MANUFACTURER_NAME_VAL 0x2a29 +/** @def BLE_MESH_UUID_DIS_PNP_ID + * @brief DIS Characteristic PnP ID + */ +#define BLE_MESH_UUID_DIS_PNP_ID BLE_MESH_UUID_DECLARE_16(0x2a50) +#define BLE_MESH_UUID_DIS_PNP_ID_VAL 0x2a50 +/** @def BLE_MESH_UUID_CTS_CURRENT_TIME + * @brief CTS Characteristic Current Time + */ +#define BLE_MESH_UUID_CTS_CURRENT_TIME BLE_MESH_UUID_DECLARE_16(0x2a2b) +#define BLE_MESH_UUID_CTS_CURRENT_TIME_VAL 0x2a2b +/** @def BLE_MESH_UUID_MAGN_DECLINATION + * @brief Magnetic Declination Characteristic + */ +#define BLE_MESH_UUID_MAGN_DECLINATION BLE_MESH_UUID_DECLARE_16(0x2a2c) +#define BLE_MESH_UUID_MAGN_DECLINATION_VAL 0x2a2c +/** @def BLE_MESH_UUID_HRS_MEASUREMENT + * @brief HRS Characteristic Measurement Interval + */ +#define BLE_MESH_UUID_HRS_MEASUREMENT BLE_MESH_UUID_DECLARE_16(0x2a37) +#define BLE_MESH_UUID_HRS_MEASUREMENT_VAL 0x2a37 +/** @def BLE_MESH_UUID_HRS_BODY_SENSOR + * @brief HRS Characteristic Body Sensor Location + */ +#define BLE_MESH_UUID_HRS_BODY_SENSOR BLE_MESH_UUID_DECLARE_16(0x2a38) +#define BLE_MESH_UUID_HRS_BODY_SENSOR_VAL 0x2a38 +/** @def BLE_MESH_UUID_HRS_CONTROL_POINT + * @brief HRS Characteristic Control Point + */ +#define BLE_MESH_UUID_HRS_CONTROL_POINT BLE_MESH_UUID_DECLARE_16(0x2a39) +#define BLE_MESH_UUID_HRS_CONTROL_POINT_VAL 0x2a39 +/** @def BLE_MESH_UUID_HIDS_INFO + * @brief HID Information Characteristic + */ +#define BLE_MESH_UUID_HIDS_INFO BLE_MESH_UUID_DECLARE_16(0x2a4a) +#define BLE_MESH_UUID_HIDS_INFO_VAL 0x2a4a +/** @def BLE_MESH_UUID_HIDS_REPORT_MAP + * @brief HID Report Map Characteristic + */ +#define BLE_MESH_UUID_HIDS_REPORT_MAP BLE_MESH_UUID_DECLARE_16(0x2a4b) +#define BLE_MESH_UUID_HIDS_REPORT_MAP_VAL 0x2a4b +/** @def BLE_MESH_UUID_HIDS_CTRL_POINT + * @brief HID Control Point Characteristic + */ +#define BLE_MESH_UUID_HIDS_CTRL_POINT BLE_MESH_UUID_DECLARE_16(0x2a4c) +#define BLE_MESH_UUID_HIDS_CTRL_POINT_VAL 0x2a4c +/** @def BLE_MESH_UUID_HIDS_REPORT + * @brief HID Report Characteristic + */ +#define BLE_MESH_UUID_HIDS_REPORT BLE_MESH_UUID_DECLARE_16(0x2a4d) +#define BLE_MESH_UUID_HIDS_REPORT_VAL 0x2a4d +/** @def BLE_MESH_UUID_CSC_MEASUREMENT + * @brief CSC Measurement Characteristic + */ +#define BLE_MESH_UUID_CSC_MEASUREMENT BLE_MESH_UUID_DECLARE_16(0x2a5b) +#define BLE_MESH_UUID_CSC_MEASUREMENT_VAL 0x2a5b +/** @def BLE_MESH_UUID_CSC_FEATURE + * @brief CSC Feature Characteristic + */ +#define BLE_MESH_UUID_CSC_FEATURE BLE_MESH_UUID_DECLARE_16(0x2a5c) +#define BLE_MESH_UUID_CSC_FEATURE_VAL 0x2a5c +/** @def BLE_MESH_UUID_SENSOR_LOCATION + * @brief Sensor Location Characteristic + */ +#define BLE_MESH_UUID_SENSOR_LOCATION BLE_MESH_UUID_DECLARE_16(0x2a5d) +#define BLE_MESH_UUID_SENSOR_LOCATION_VAL 0x2a5d +/** @def BLE_MESH_UUID_SC_CONTROL_POINT + * @brief SC Control Point Characteristic + */ +#define BLE_MESH_UUID_SC_CONTROL_POINT BLE_MESH_UUID_DECLARE_16(0x2a55) +#define BLE_MESH_UUID_SC_CONTROL_POINT_VAl 0x2a55 +/** @def BLE_MESH_UUID_ELEVATION + * @brief Elevation Characteristic + */ +#define BLE_MESH_UUID_ELEVATION BLE_MESH_UUID_DECLARE_16(0x2a6c) +#define BLE_MESH_UUID_ELEVATION_VAL 0x2a6c +/** @def BLE_MESH_UUID_PRESSURE + * @brief Pressure Characteristic + */ +#define BLE_MESH_UUID_PRESSURE BLE_MESH_UUID_DECLARE_16(0x2a6d) +#define BLE_MESH_UUID_PRESSURE_VAL 0x2a6d +/** @def BLE_MESH_UUID_TEMPERATURE + * @brief Temperature Characteristic + */ +#define BLE_MESH_UUID_TEMPERATURE BLE_MESH_UUID_DECLARE_16(0x2a6e) +#define BLE_MESH_UUID_TEMPERATURE_VAL 0x2a6e +/** @def BLE_MESH_UUID_HUMIDITY + * @brief Humidity Characteristic + */ +#define BLE_MESH_UUID_HUMIDITY BLE_MESH_UUID_DECLARE_16(0x2a6f) +#define BLE_MESH_UUID_HUMIDITY_VAL 0x2a6f +/** @def BLE_MESH_UUID_TRUE_WIND_SPEED + * @brief True Wind Speed Characteristic + */ +#define BLE_MESH_UUID_TRUE_WIND_SPEED BLE_MESH_UUID_DECLARE_16(0x2a70) +#define BLE_MESH_UUID_TRUE_WIND_SPEED_VAL 0x2a70 +/** @def BLE_MESH_UUID_TRUE_WIND_DIR + * @brief True Wind Direction Characteristic + */ +#define BLE_MESH_UUID_TRUE_WIND_DIR BLE_MESH_UUID_DECLARE_16(0x2a71) +#define BLE_MESH_UUID_TRUE_WIND_DIR_VAL 0x2a71 +/** @def BLE_MESH_UUID_APPARENT_WIND_SPEED + * @brief Apparent Wind Speed Characteristic + */ +#define BLE_MESH_UUID_APPARENT_WIND_SPEED BLE_MESH_UUID_DECLARE_16(0x2a72) +#define BLE_MESH_UUID_APPARENT_WIND_SPEED_VAL 0x2a72 +/** @def BLE_MESH_UUID_APPARENT_WIND_DIR + * @brief Apparent Wind Direction Characteristic + */ +#define BLE_MESH_UUID_APPARENT_WIND_DIR BLE_MESH_UUID_DECLARE_16(0x2a73) +#define BLE_MESH_UUID_APPARENT_WIND_DIR_VAL 0x2a73 +/** @def BLE_MESH_UUID_GUST_FACTOR + * @brief Gust Factor Characteristic + */ +#define BLE_MESH_UUID_GUST_FACTOR BLE_MESH_UUID_DECLARE_16(0x2a74) +#define BLE_MESH_UUID_GUST_FACTOR_VAL 0x2a74 +/** @def BLE_MESH_UUID_POLLEN_CONCENTRATION + * @brief Pollen Concentration Characteristic + */ +#define BLE_MESH_UUID_POLLEN_CONCENTRATION BLE_MESH_UUID_DECLARE_16(0x2a75) +#define BLE_MESH_UUID_POLLEN_CONCENTRATION_VAL 0x2a75 +/** @def BLE_MESH_UUID_UV_INDEX + * @brief UV Index Characteristic + */ +#define BLE_MESH_UUID_UV_INDEX BLE_MESH_UUID_DECLARE_16(0x2a76) +#define BLE_MESH_UUID_UV_INDEX_VAL 0x2a76 +/** @def BLE_MESH_UUID_IRRADIANCE + * @brief Irradiance Characteristic + */ +#define BLE_MESH_UUID_IRRADIANCE BLE_MESH_UUID_DECLARE_16(0x2a77) +#define BLE_MESH_UUID_IRRADIANCE_VAL 0x2a77 +/** @def BLE_MESH_UUID_RAINFALL + * @brief Rainfall Characteristic + */ +#define BLE_MESH_UUID_RAINFALL BLE_MESH_UUID_DECLARE_16(0x2a78) +#define BLE_MESH_UUID_RAINFALL_VAL 0x2a78 +/** @def BLE_MESH_UUID_WIND_CHILL + * @brief Wind Chill Characteristic + */ +#define BLE_MESH_UUID_WIND_CHILL BLE_MESH_UUID_DECLARE_16(0x2a79) +#define BLE_MESH_UUID_WIND_CHILL_VAL 0x2a79 +/** @def BLE_MESH_UUID_HEAT_INDEX + * @brief Heat Index Characteristic + */ +#define BLE_MESH_UUID_HEAT_INDEX BLE_MESH_UUID_DECLARE_16(0x2a7a) +#define BLE_MESH_UUID_HEAT_INDEX_VAL 0x2a7a +/** @def BLE_MESH_UUID_DEW_POINT + * @brief Dew Point Characteristic + */ +#define BLE_MESH_UUID_DEW_POINT BLE_MESH_UUID_DECLARE_16(0x2a7b) +#define BLE_MESH_UUID_DEW_POINT_VAL 0x2a7b +/** @def BLE_MESH_UUID_DESC_VALUE_CHANGED + * @brief Descriptor Value Changed Characteristic + */ +#define BLE_MESH_UUID_DESC_VALUE_CHANGED BLE_MESH_UUID_DECLARE_16(0x2a7d) +#define BLE_MESH_UUID_DESC_VALUE_CHANGED_VAL 0x2a7d +/** @def BLE_MESH_UUID_MAGN_FLUX_DENSITY_2D + * @brief Magnetic Flux Density - 2D Characteristic + */ +#define BLE_MESH_UUID_MAGN_FLUX_DENSITY_2D BLE_MESH_UUID_DECLARE_16(0x2aa0) +#define BLE_MESH_UUID_MAGN_FLUX_DENSITY_2D_VAL 0x2aa0 +/** @def BLE_MESH_UUID_MAGN_FLUX_DENSITY_3D + * @brief Magnetic Flux Density - 3D Characteristic + */ +#define BLE_MESH_UUID_MAGN_FLUX_DENSITY_3D BLE_MESH_UUID_DECLARE_16(0x2aa1) +#define BLE_MESH_UUID_MAGN_FLUX_DENSITY_3D_VAL 0x2aa1 +/** @def BLE_MESH_UUID_BAR_PRESSURE_TREND + * @brief Barometric Pressure Trend Characteristic + */ +#define BLE_MESH_UUID_BAR_PRESSURE_TREND BLE_MESH_UUID_DECLARE_16(0x2aa3) +#define BLE_MESH_UUID_BAR_PRESSURE_TREND_VAL 0x2aa3 +/** @def BLE_MESH_UUID_MESH_PROV_DATA_IN + * @brief Mesh Provisioning Data In + */ +#define BLE_MESH_UUID_MESH_PROV_DATA_IN BLE_MESH_UUID_DECLARE_16(0x2adb) +#define BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL 0x2adb +/** @def BLE_MESH_UUID_MESH_PROV_DATA_OUT + * @brief Mesh Provisioning Data Out + */ +#define BLE_MESH_UUID_MESH_PROV_DATA_OUT BLE_MESH_UUID_DECLARE_16(0x2adc) +#define BLE_MESH_UUID_MESH_PROV_DATA_OUT_VAL 0x2adc +/** @def BLE_MESH_UUID_MESH_PROXY_DATA_IN + * @brief Mesh Proxy Data In + */ +#define BLE_MESH_UUID_MESH_PROXY_DATA_IN BLE_MESH_UUID_DECLARE_16(0x2add) +#define BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL 0x2add +/** @def BLE_MESH_UUID_MESH_PROXY_DATA_OUT + * @brief Mesh Proxy Data Out + */ +#define BLE_MESH_UUID_MESH_PROXY_DATA_OUT BLE_MESH_UUID_DECLARE_16(0x2ade) +#define BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL 0x2ade + +/* + * Protocol UUIDs + */ +#define BLE_MESH_UUID_SDP BLE_MESH_UUID_DECLARE_16(0x0001) +#define BLE_MESH_UUID_SDP_VAL 0x0001 +#define BLE_MESH_UUID_UDP BLE_MESH_UUID_DECLARE_16(0x0002) +#define BLE_MESH_UUID_UDP_VAL 0x0002 +#define BLE_MESH_UUID_RFCOMM BLE_MESH_UUID_DECLARE_16(0x0003) +#define BLE_MESH_UUID_RFCOMM_VAL 0x0003 +#define BLE_MESH_UUID_TCP BLE_MESH_UUID_DECLARE_16(0x0004) +#define BLE_MESH_UUID_TCP_VAL 0x0004 +#define BLE_MESH_UUID_TCS_BIN BLE_MESH_UUID_DECLARE_16(0x0005) +#define BLE_MESH_UUID_TCS_BIN_VAL 0x0005 +#define BLE_MESH_UUID_TCS_AT BLE_MESH_UUID_DECLARE_16(0x0006) +#define BLE_MESH_UUID_TCS_AT_VAL 0x0006 +#define BLE_MESH_UUID_ATT BLE_MESH_UUID_DECLARE_16(0x0007) +#define BLE_MESH_UUID_ATT_VAL 0x0007 +#define BLE_MESH_UUID_OBEX BLE_MESH_UUID_DECLARE_16(0x0008) +#define BLE_MESH_UUID_OBEX_VAL 0x0008 +#define BLE_MESH_UUID_IP BLE_MESH_UUID_DECLARE_16(0x0009) +#define BLE_MESH_UUID_IP_VAL 0x0009 +#define BLE_MESH_UUID_FTP BLE_MESH_UUID_DECLARE_16(0x000a) +#define BLE_MESH_UUID_FTP_VAL 0x000a +#define BLE_MESH_UUID_HTTP BLE_MESH_UUID_DECLARE_16(0x000c) +#define BLE_MESH_UUID_HTTP_VAL 0x000c +#define BLE_MESH_UUID_BNEP BLE_MESH_UUID_DECLARE_16(0x000f) +#define BLE_MESH_UUID_BNEP_VAL 0x000f +#define BLE_MESH_UUID_UPNP BLE_MESH_UUID_DECLARE_16(0x0010) +#define BLE_MESH_UUID_UPNP_VAL 0x0010 +#define BLE_MESH_UUID_HIDP BLE_MESH_UUID_DECLARE_16(0x0011) +#define BLE_MESH_UUID_HIDP_VAL 0x0011 +#define BLE_MESH_UUID_HCRP_CTRL BLE_MESH_UUID_DECLARE_16(0x0012) +#define BLE_MESH_UUID_HCRP_CTRL_VAL 0x0012 +#define BLE_MESH_UUID_HCRP_DATA BLE_MESH_UUID_DECLARE_16(0x0014) +#define BLE_MESH_UUID_HCRP_DATA_VAL 0x0014 +#define BLE_MESH_UUID_HCRP_NOTE BLE_MESH_UUID_DECLARE_16(0x0016) +#define BLE_MESH_UUID_HCRP_NOTE_VAL 0x0016 +#define BLE_MESH_UUID_AVCTP BLE_MESH_UUID_DECLARE_16(0x0017) +#define BLE_MESH_UUID_AVCTP_VAL 0x0017 +#define BLE_MESH_UUID_AVDTP BLE_MESH_UUID_DECLARE_16(0x0019) +#define BLE_MESH_UUID_AVDTP_VAL 0x0019 +#define BLE_MESH_UUID_CMTP BLE_MESH_UUID_DECLARE_16(0x001b) +#define BLE_MESH_UUID_CMTP_VAL 0x001b +#define BLE_MESH_UUID_UDI BLE_MESH_UUID_DECLARE_16(0x001d) +#define BLE_MESH_UUID_UDI_VAL 0x001d +#define BLE_MESH_UUID_MCAP_CTRL BLE_MESH_UUID_DECLARE_16(0x001e) +#define BLE_MESH_UUID_MCAP_CTRL_VAL 0x001e +#define BLE_MESH_UUID_MCAP_DATA BLE_MESH_UUID_DECLARE_16(0x001f) +#define BLE_MESH_UUID_MCAP_DATA_VAL 0x001f +#define BLE_MESH_UUID_L2CAP BLE_MESH_UUID_DECLARE_16(0x0100) +#define BLE_MESH_UUID_L2CAP_VAL 0x0100 + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* _BLE_MESH_UUID_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/local_operation.c b/components/bt/esp_ble_mesh/mesh_core/local_operation.c new file mode 100644 index 000000000..9e2ab3f5e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/local_operation.c @@ -0,0 +1,121 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#include "mesh.h" +#include "lpn.h" +#include "crypto.h" +#include "access.h" +#include "foundation.h" +#include "transport.h" +#include "mesh_main.h" +#include "settings.h" + +static struct bt_mesh_model *find_model(u16_t elem_addr, u16_t cid, u16_t mod_id) +{ + struct bt_mesh_elem *elem = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, elem_addr); + return NULL; + } + + elem = bt_mesh_elem_find(elem_addr); + if (elem == NULL) { + BT_ERR("%s, No element found, addr 0x%04x", __func__, elem_addr); + return NULL; + } + + if (cid == BLE_MESH_CID_NVAL) { + return bt_mesh_model_find(elem, mod_id); + } else { + return bt_mesh_model_find_vnd(elem, cid, mod_id); + } +} + +int bt_mesh_model_subscribe_group_addr(u16_t elem_addr, u16_t cid, + u16_t mod_id, u16_t group_addr) +{ + struct bt_mesh_model *model = NULL; + int i; + + model = find_model(elem_addr, cid, mod_id); + if (model == NULL) { + BT_ERR("Subscribe, model not found, cid 0x%04x, mod_id 0x%04x", cid, mod_id); + return -ENODEV; + } + + if (!BLE_MESH_ADDR_IS_GROUP(group_addr)) { + BT_ERR("Subscribe, not a group address 0x%04x", group_addr); + return -EINVAL; + } + + if (bt_mesh_model_find_group(model, group_addr)) { + BT_INFO("Group address 0x%04x already exists", group_addr); + return 0; + } + + for (i = 0; i < ARRAY_SIZE(model->groups); i++) { + if (model->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { + model->groups[i] = group_addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(model); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_add(group_addr); + } + + BT_INFO("Subscribe group address 0x%04x", group_addr); + return 0; + } + } + + BT_ERR("Subscribe, model sub is full!"); + return -ENOMEM; +} + +int bt_mesh_model_unsubscribe_group_addr(u16_t elem_addr, u16_t cid, + u16_t mod_id, u16_t group_addr) +{ + struct bt_mesh_model *model = NULL; + u16_t *match = NULL; + + model = find_model(elem_addr, cid, mod_id); + if (model == NULL) { + BT_ERR("Unsubscribe, model not found, cid 0x%04x, mod_id 0x%04x", cid, mod_id); + return -ENODEV; + } + + if (!BLE_MESH_ADDR_IS_GROUP(group_addr)) { + BT_ERR("Unsubscribe, not a group address 0x%04x", group_addr); + return -EINVAL; + } + + match = bt_mesh_model_find_group(model, group_addr); + if (match == NULL) { + BT_WARN("Group address 0x%04x not exists", group_addr); + return -EEXIST; + } + + *match = BLE_MESH_ADDR_UNASSIGNED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_sub(model); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_group_del(&group_addr, 1); + } + + BT_INFO("Unsubscribe group address 0x%04x", group_addr); + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/local_operation.h b/components/bt/esp_ble_mesh/mesh_core/local_operation.h new file mode 100644 index 000000000..3c51ec502 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/local_operation.h @@ -0,0 +1,29 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2020 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _LOCAL_OPERATION_H_ +#define _LOCAL_OPERATION_H_ + +#include "mesh_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int bt_mesh_model_subscribe_group_addr(u16_t elem_addr, u16_t mod_id, + u16_t cid, u16_t group_addr); + +int bt_mesh_model_unsubscribe_group_addr(u16_t elem_addr, u16_t cid, + u16_t mod_id, u16_t group_addr); + +#ifdef __cplusplus +} +#endif + +#endif /* _LOCAL_OPERATION_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.c b/components/bt/esp_ble_mesh/mesh_core/lpn.c new file mode 100644 index 000000000..6aa5136e8 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.c @@ -0,0 +1,1104 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_LOW_POWER) + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "transport.h" +#include "access.h" +#include "beacon.h" +#include "lpn.h" +#include "foundation.h" +#include "mesh_main.h" +#include "cfg_srv.h" + +#ifdef CONFIG_BLE_MESH_LOW_POWER + +#if defined(CONFIG_BLE_MESH_LPN_AUTO) +#define LPN_AUTO_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_LPN_AUTO_TIMEOUT) +#else +#define LPN_AUTO_TIMEOUT 0 +#endif + +#define LPN_RECV_DELAY CONFIG_BLE_MESH_LPN_RECV_DELAY +#define SCAN_LATENCY MIN(CONFIG_BLE_MESH_LPN_SCAN_LATENCY, \ + LPN_RECV_DELAY) + +#define FRIEND_REQ_RETRY_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_LPN_RETRY_TIMEOUT) + +#define FRIEND_REQ_WAIT K_MSEC(100) +#define FRIEND_REQ_SCAN K_SECONDS(1) +#define FRIEND_REQ_TIMEOUT (FRIEND_REQ_WAIT + FRIEND_REQ_SCAN) + +#define POLL_RETRY_TIMEOUT K_MSEC(100) + +#define REQ_RETRY_DURATION(lpn) (4 * (LPN_RECV_DELAY + (lpn)->adv_duration + \ + (lpn)->recv_win + POLL_RETRY_TIMEOUT)) + +#define POLL_TIMEOUT_INIT (CONFIG_BLE_MESH_LPN_INIT_POLL_TIMEOUT * 100) +#define POLL_TIMEOUT_MAX(lpn) ((CONFIG_BLE_MESH_LPN_POLL_TIMEOUT * 100) - \ + REQ_RETRY_DURATION(lpn)) + +/** + * 1. Should use 20 attempts for BQB test case MESH/NODE/FRND/LPM/BI-02-C. + * 2. We should use more specific value for each PollTimeout range. + */ +#define REQ_ATTEMPTS(lpn) (POLL_TIMEOUT_MAX(lpn) < K_SECONDS(3) ? 2 : 6) + +#define CLEAR_ATTEMPTS 2 + +#define LPN_CRITERIA ((CONFIG_BLE_MESH_LPN_MIN_QUEUE_SIZE) | \ + (CONFIG_BLE_MESH_LPN_RSSI_FACTOR << 3) | \ + (CONFIG_BLE_MESH_LPN_RECV_WIN_FACTOR << 5)) + +#define POLL_TO(to) { (u8_t)((to) >> 16), (u8_t)((to) >> 8), (u8_t)(to) } +#define LPN_POLL_TO POLL_TO(CONFIG_BLE_MESH_LPN_POLL_TIMEOUT) + +/* 2 transmissions, 20ms interval */ +#define POLL_XMIT BLE_MESH_TRANSMIT(1, 20) + +#define FIRST_POLL_ATTEMPTS 6 + +static void (*lpn_cb)(u16_t friend_addr, bool established); + +#if !CONFIG_BLE_MESH_NO_LOG +static const char *state2str(int state) +{ + switch (state) { + case BLE_MESH_LPN_DISABLED: + return "disabled"; + case BLE_MESH_LPN_CLEAR: + return "clear"; + case BLE_MESH_LPN_TIMER: + return "timer"; + case BLE_MESH_LPN_ENABLED: + return "enabled"; + case BLE_MESH_LPN_REQ_WAIT: + return "req wait"; + case BLE_MESH_LPN_WAIT_OFFER: + return "wait offer"; + case BLE_MESH_LPN_ESTABLISHED: + return "established"; + case BLE_MESH_LPN_RECV_DELAY: + return "recv delay"; + case BLE_MESH_LPN_WAIT_UPDATE: + return "wait update"; + case BLE_MESH_LPN_OFFER_RECV: + return "offer recv"; + default: + return "(unknown)"; + } +} +#endif + +static inline void lpn_set_state(int state) +{ + BT_DBG("%s -> %s", state2str(bt_mesh.lpn.state), state2str(state)); + bt_mesh.lpn.state = state; +} + +static inline void group_zero(bt_mesh_atomic_t *target) +{ +#if CONFIG_BLE_MESH_LPN_GROUPS > 32 + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.lpn.added); i++) { + bt_mesh_atomic_set(&target[i], 0); + } +#else + bt_mesh_atomic_set(target, 0); +#endif +} + +static inline void group_set(bt_mesh_atomic_t *target, bt_mesh_atomic_t *source) +{ +#if CONFIG_BLE_MESH_LPN_GROUPS > 32 + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.lpn.added); i++) { + (void)bt_mesh_atomic_or(&target[i], bt_mesh_atomic_get(&source[i])); + } +#else + (void)bt_mesh_atomic_or(target, bt_mesh_atomic_get(source)); +#endif +} + +static inline void group_clear(bt_mesh_atomic_t *target, bt_mesh_atomic_t *source) +{ +#if CONFIG_BLE_MESH_LPN_GROUPS > 32 + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.lpn.added); i++) { + (void)bt_mesh_atomic_and(&target[i], ~bt_mesh_atomic_get(&source[i])); + } +#else + (void)bt_mesh_atomic_and(target, ~bt_mesh_atomic_get(source)); +#endif +} + +static void clear_friendship(bool force, bool disable); + +static bool scan_after_clear; + +static void friend_clear_sent(int err, void *user_data) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + /* We're switching away from Low Power behavior, so permanently + * enable scanning. + */ + if (scan_after_clear == false) { + bt_mesh_scan_enable(); + scan_after_clear = true; + } + + lpn->req_attempts++; + + if (err) { + BT_ERR("%s, Sending Friend Request failed (err %d)", __func__, err); + lpn_set_state(BLE_MESH_LPN_ENABLED); + clear_friendship(false, lpn->disable); + return; + } + + lpn_set_state(BLE_MESH_LPN_CLEAR); + k_delayed_work_submit(&lpn->timer, FRIEND_REQ_TIMEOUT); +} + +static const struct bt_mesh_send_cb clear_sent_cb = { + .end = friend_clear_sent, +}; + +static int send_friend_clear(void) +{ + struct bt_mesh_msg_ctx ctx = { + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = bt_mesh.lpn.frnd, + .send_ttl = 0, + }; + struct bt_mesh_net_tx tx = { + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + .xmit = bt_mesh_net_transmit_get(), + }; + struct bt_mesh_ctl_friend_clear req = { + .lpn_addr = sys_cpu_to_be16(tx.src), + .lpn_counter = sys_cpu_to_be16(bt_mesh.lpn.counter), + }; + + BT_DBG("%s", __func__); + + return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR, &req, + sizeof(req), &clear_sent_cb, NULL); +} + +static void clear_friendship(bool force, bool disable) +{ + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + BT_DBG("force %u disable %u", force, disable); + + if (!force && lpn->established && !lpn->clear_success && + lpn->req_attempts < CLEAR_ATTEMPTS) { + send_friend_clear(); + lpn->disable = disable; + return; + } + + bt_mesh_rx_reset(); + + k_delayed_work_cancel(&lpn->timer); + + friend_cred_del(bt_mesh.sub[0].net_idx, lpn->frnd); + + if (lpn->clear_success) { + lpn->old_friend = BLE_MESH_ADDR_UNASSIGNED; + } else { + lpn->old_friend = lpn->frnd; + } + + if (lpn_cb && lpn->frnd != BLE_MESH_ADDR_UNASSIGNED) { + lpn_cb(lpn->frnd, false); + } + + lpn->frnd = BLE_MESH_ADDR_UNASSIGNED; + lpn->fsn = 0U; + lpn->req_attempts = 0U; + lpn->recv_win = 0U; + lpn->queue_size = 0U; + lpn->disable = 0U; + lpn->sent_req = 0U; + lpn->established = 0U; + lpn->clear_success = 0U; + + group_zero(lpn->added); + group_zero(lpn->pending); + group_zero(lpn->to_remove); + + /* Set this to 1 to force group subscription when the next + * Friendship is created, in case lpn->groups doesn't get + * modified meanwhile. + */ + lpn->groups_changed = 1U; + + if (cfg->hb_pub.feat & BLE_MESH_FEAT_LOW_POWER) { + bt_mesh_heartbeat_send(); + } + + if (disable) { + lpn_set_state(BLE_MESH_LPN_DISABLED); + return; + } + + lpn_set_state(BLE_MESH_LPN_ENABLED); + k_delayed_work_submit(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); + + scan_after_clear = false; + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } +} + +static void friend_req_sent(u16_t duration, int err, void *user_data) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + if (err) { + BT_ERR("%s, Sending Friend Request failed (err %d)", __func__, err); + + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_enable(); + } + return; + } + + lpn->adv_duration = duration; + + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + k_delayed_work_submit(&lpn->timer, FRIEND_REQ_WAIT); + lpn_set_state(BLE_MESH_LPN_REQ_WAIT); + } else { + k_delayed_work_submit(&lpn->timer, + duration + FRIEND_REQ_TIMEOUT); + lpn_set_state(BLE_MESH_LPN_WAIT_OFFER); + } +} + +static const struct bt_mesh_send_cb friend_req_sent_cb = { + .start = friend_req_sent, +}; + +static int send_friend_req(struct bt_mesh_lpn *lpn) +{ + const struct bt_mesh_comp *comp = bt_mesh_comp_get(); + struct bt_mesh_msg_ctx ctx = { + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = BLE_MESH_ADDR_FRIENDS, + .send_ttl = 0, + }; + struct bt_mesh_net_tx tx = { + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + .xmit = POLL_XMIT, + }; + struct bt_mesh_ctl_friend_req req = { + .criteria = LPN_CRITERIA, + .recv_delay = LPN_RECV_DELAY, + .poll_to = LPN_POLL_TO, + .prev_addr = sys_cpu_to_be16(lpn->old_friend), + .num_elem = comp->elem_count, + .lpn_counter = sys_cpu_to_be16(lpn->counter), + }; + + BT_DBG("%s", __func__); + + return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_REQ, &req, + sizeof(req), &friend_req_sent_cb, NULL); +} + +static void req_sent(u16_t duration, int err, void *user_data) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + BT_DBG("req 0x%02x duration %u err %d state %s", + lpn->sent_req, duration, err, state2str(lpn->state)); + + if (err) { + BT_ERR("%s, Sending request failed (err %d)", __func__, err); + lpn->sent_req = 0U; + group_zero(lpn->pending); + return; + } + + lpn->req_attempts++; + lpn->adv_duration = duration; + + if (lpn->established || IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + lpn_set_state(BLE_MESH_LPN_RECV_DELAY); + /* We start scanning a bit early to elimitate risk of missing + * response data due to HCI and other latencies. + */ + k_delayed_work_submit(&lpn->timer, + LPN_RECV_DELAY - SCAN_LATENCY); + } else { + lpn_set_state(BLE_MESH_LPN_OFFER_RECV); + /** + * Friend Update is replied by Friend Node with TTL set to 0 and Network + * Transmit set to 30ms which will cause the packet easy to be missed. + * Regarding this situation, here we can reduce the duration of receiving + * the first Friend Update. + */ + k_delayed_work_submit(&lpn->timer, + LPN_RECV_DELAY + duration + + lpn->recv_win); + } +} + +static const struct bt_mesh_send_cb req_sent_cb = { + .start = req_sent, +}; + +static int send_friend_poll(void) +{ + struct bt_mesh_msg_ctx ctx = { + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = bt_mesh.lpn.frnd, + .send_ttl = 0, + }; + struct bt_mesh_net_tx tx = { + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + .xmit = POLL_XMIT, + .friend_cred = true, + }; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + u8_t fsn = lpn->fsn; + int err = 0; + + BT_DBG("lpn->sent_req 0x%02x", lpn->sent_req); + + if (lpn->sent_req) { + if (lpn->sent_req != TRANS_CTL_OP_FRIEND_POLL) { + lpn->pending_poll = 1U; + } + + return 0; + } + + err = bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_POLL, &fsn, 1, + &req_sent_cb, NULL); + if (err == 0) { + lpn->pending_poll = 0U; + lpn->sent_req = TRANS_CTL_OP_FRIEND_POLL; + } + + return err; +} + +void bt_mesh_lpn_disable(bool force) +{ + if (bt_mesh.lpn.state == BLE_MESH_LPN_DISABLED) { + return; + } + + clear_friendship(force, true); +} + +int bt_mesh_lpn_set(bool enable, bool force) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + if (enable) { + if (lpn->state != BLE_MESH_LPN_DISABLED) { + return 0; + } + } else { + if (lpn->state == BLE_MESH_LPN_DISABLED) { + return 0; + } + } + + if (!bt_mesh_is_provisioned()) { + if (enable) { + lpn_set_state(BLE_MESH_LPN_ENABLED); + } else { + lpn_set_state(BLE_MESH_LPN_DISABLED); + } + + return 0; + } + + if (enable) { + lpn_set_state(BLE_MESH_LPN_ENABLED); + + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } + + send_friend_req(lpn); + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_AUTO) && + lpn->state == BLE_MESH_LPN_TIMER) { + k_delayed_work_cancel(&lpn->timer); + lpn_set_state(BLE_MESH_LPN_DISABLED); + } else { + bt_mesh_lpn_disable(force); + } + } + + return 0; +} + +static void friend_response_received(struct bt_mesh_lpn *lpn) +{ + BT_DBG("lpn->sent_req 0x%02x", lpn->sent_req); + + if (lpn->sent_req == TRANS_CTL_OP_FRIEND_POLL) { + lpn->fsn++; + } + + k_delayed_work_cancel(&lpn->timer); + bt_mesh_scan_disable(); + lpn_set_state(BLE_MESH_LPN_ESTABLISHED); + lpn->req_attempts = 0U; + lpn->sent_req = 0U; +} + +void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + if (lpn->state == BLE_MESH_LPN_TIMER) { + BT_DBG("Restarting establishment timer"); + k_delayed_work_submit(&lpn->timer, LPN_AUTO_TIMEOUT); + return; + } + + if (lpn->sent_req != TRANS_CTL_OP_FRIEND_POLL) { + BT_WARN("Unexpected message without a preceding Poll"); + return; + } + + friend_response_received(lpn); + + BT_DBG("Requesting more messages from Friend"); + + send_friend_poll(); +} + +int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_offer *msg = (void *)buf->data; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + struct bt_mesh_subnet *sub = rx->sub; + struct friend_cred *cred = NULL; + u16_t frnd_counter = 0U; + int err = 0; + + if (buf->len < sizeof(*msg)) { + BT_WARN("Too short Friend Offer"); + return -EINVAL; + } + + if (lpn->state != BLE_MESH_LPN_WAIT_OFFER) { + BT_WARN("Ignoring unexpected Friend Offer"); + return 0; + } + + if (!msg->recv_win) { + BT_WARN("Prohibited ReceiveWindow value"); + return -EINVAL; + } + + frnd_counter = sys_be16_to_cpu(msg->frnd_counter); + + BT_INFO("recv_win %u queue_size %u sub_list_size %u rssi %d counter %u", + msg->recv_win, msg->queue_size, msg->sub_list_size, msg->rssi, + frnd_counter); + + lpn->frnd = rx->ctx.addr; + + cred = friend_cred_create(sub, lpn->frnd, lpn->counter, frnd_counter); + if (!cred) { + lpn->frnd = BLE_MESH_ADDR_UNASSIGNED; + return -ENOMEM; + } + + /* TODO: Add offer acceptance criteria check */ + + k_delayed_work_cancel(&lpn->timer); + + lpn->recv_win = msg->recv_win; + lpn->queue_size = msg->queue_size; + + err = send_friend_poll(); + if (err) { + friend_cred_clear(cred); + lpn->frnd = BLE_MESH_ADDR_UNASSIGNED; + lpn->recv_win = 0U; + lpn->queue_size = 0U; + return err; + } + + lpn->counter++; + + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } + + return 0; +} + +int bt_mesh_lpn_friend_clear_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_clear_confirm *msg = (void *)buf->data; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + u16_t addr = 0U, counter = 0U; + + if (buf->len < sizeof(*msg)) { + BT_WARN("Too short Friend Clear Confirm"); + return -EINVAL; + } + + if (lpn->state != BLE_MESH_LPN_CLEAR) { + BT_WARN("Ignoring unexpected Friend Clear Confirm"); + return 0; + } + + addr = sys_be16_to_cpu(msg->lpn_addr); + counter = sys_be16_to_cpu(msg->lpn_counter); + + BT_DBG("LPNAddress 0x%04x LPNCounter 0x%04x", addr, counter); + + if (addr != bt_mesh_primary_addr() || counter != lpn->counter) { + BT_WARN("Invalid parameters in Friend Clear Confirm"); + return 0; + } + + lpn->clear_success = 1U; + clear_friendship(false, lpn->disable); + + return 0; +} + +static void lpn_group_add(u16_t group) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + u16_t *free_slot = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(lpn->groups); i++) { + if (lpn->groups[i] == group) { + bt_mesh_atomic_clear_bit(lpn->to_remove, i); + return; + } + + if (!free_slot && lpn->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { + free_slot = &lpn->groups[i]; + } + } + + if (!free_slot) { + BT_WARN("Friend Subscription List exceeded!"); + return; + } + + *free_slot = group; + lpn->groups_changed = 1U; +} + +static void lpn_group_del(u16_t group) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + int i; + + for (i = 0; i < ARRAY_SIZE(lpn->groups); i++) { + if (lpn->groups[i] == group) { + if (bt_mesh_atomic_test_bit(lpn->added, i) || + bt_mesh_atomic_test_bit(lpn->pending, i)) { + bt_mesh_atomic_set_bit(lpn->to_remove, i); + lpn->groups_changed = 1U; + } else { + lpn->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + } + } + } +} + +static inline int group_popcount(bt_mesh_atomic_t *target) +{ +#if CONFIG_BLE_MESH_LPN_GROUPS > 32 + int i, count = 0; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.lpn.added); i++) { + count += popcount(bt_mesh_atomic_get(&target[i])); + } +#else + return popcount(bt_mesh_atomic_get(target)); +#endif +} + +static bool sub_update(u8_t op) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + int added_count = group_popcount(lpn->added); + struct bt_mesh_msg_ctx ctx = { + .net_idx = bt_mesh.sub[0].net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = lpn->frnd, + .send_ttl = 0, + }; + struct bt_mesh_net_tx tx = { + .sub = &bt_mesh.sub[0], + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + .xmit = POLL_XMIT, + .friend_cred = true, + }; + struct bt_mesh_ctl_friend_sub req = {0}; + size_t i = 0U, g = 0U; + + BT_DBG("op 0x%02x sent_req 0x%02x", op, lpn->sent_req); + + if (lpn->sent_req) { + return false; + } + + for (i = 0U, g = 0U; i < ARRAY_SIZE(lpn->groups); i++) { + if (lpn->groups[i] == BLE_MESH_ADDR_UNASSIGNED) { + continue; + } + + if (op == TRANS_CTL_OP_FRIEND_SUB_ADD) { + if (bt_mesh_atomic_test_bit(lpn->added, i)) { + continue; + } + } else { + if (!bt_mesh_atomic_test_bit(lpn->to_remove, i)) { + continue; + } + } + + if (added_count + g >= lpn->queue_size) { + BT_WARN("%s, Friend Queue Size exceeded", __func__); + break; + } + + req.addr_list[g++] = sys_cpu_to_be16(lpn->groups[i]); + bt_mesh_atomic_set_bit(lpn->pending, i); + + if (g == ARRAY_SIZE(req.addr_list)) { + break; + } + } + + if (g == 0U) { + group_zero(lpn->pending); + return false; + } + + req.xact = lpn->xact_next++; + + if (bt_mesh_ctl_send(&tx, op, &req, 1 + g * 2, + &req_sent_cb, NULL) < 0) { + group_zero(lpn->pending); + return false; + } + + lpn->xact_pending = req.xact; + lpn->sent_req = op; + return true; +} + +static void update_timeout(struct bt_mesh_lpn *lpn) +{ + if (lpn->established) { + BT_WARN("No response from Friend during ReceiveWindow"); + bt_mesh_scan_disable(); + lpn_set_state(BLE_MESH_LPN_ESTABLISHED); + k_delayed_work_submit(&lpn->timer, POLL_RETRY_TIMEOUT); + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } + + if (lpn->req_attempts < FIRST_POLL_ATTEMPTS) { + BT_WARN("Retrying first Friend Poll"); + lpn->sent_req = 0U; + if (send_friend_poll() == 0) { + return; + } + } + + BT_ERR("Timed out waiting for first Friend Update"); + clear_friendship(false, false); + } +} + +static void lpn_timeout(struct k_work *work) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + BT_DBG("state: %s", state2str(lpn->state)); + + switch (lpn->state) { + case BLE_MESH_LPN_DISABLED: + break; + case BLE_MESH_LPN_CLEAR: + clear_friendship(false, bt_mesh.lpn.disable); + break; + case BLE_MESH_LPN_TIMER: + BT_DBG("Starting to look for Friend nodes"); + lpn_set_state(BLE_MESH_LPN_ENABLED); + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } + /* fall through */ + case BLE_MESH_LPN_ENABLED: + send_friend_req(lpn); + break; + case BLE_MESH_LPN_REQ_WAIT: + bt_mesh_scan_enable(); + k_delayed_work_submit(&lpn->timer, + lpn->adv_duration + FRIEND_REQ_SCAN); + lpn_set_state(BLE_MESH_LPN_WAIT_OFFER); + break; + case BLE_MESH_LPN_WAIT_OFFER: + BT_WARN("No acceptable Friend Offers received"); + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_disable(); + } + lpn->counter++; + lpn_set_state(BLE_MESH_LPN_ENABLED); + lpn->sent_req = 0U; + k_delayed_work_submit(&lpn->timer, FRIEND_REQ_RETRY_TIMEOUT); + break; + case BLE_MESH_LPN_OFFER_RECV: + if (lpn->req_attempts < FIRST_POLL_ATTEMPTS) { + BT_WARN("Retrying the first Friend Poll, %d attempts", lpn->req_attempts); + lpn->sent_req = 0U; + send_friend_poll(); + break; + } + + BT_ERR("Timeout waiting for the first Friend Update"); + clear_friendship(true, false); + break; + case BLE_MESH_LPN_ESTABLISHED: + if (lpn->req_attempts < REQ_ATTEMPTS(lpn)) { + u8_t req = lpn->sent_req; + + lpn->sent_req = 0U; + + if (!req || req == TRANS_CTL_OP_FRIEND_POLL) { + send_friend_poll(); + } else { + sub_update(req); + } + + break; + } + + BT_ERR("No response from Friend after %u retries", + lpn->req_attempts); + lpn->req_attempts = 0U; + clear_friendship(false, false); + break; + case BLE_MESH_LPN_RECV_DELAY: + k_delayed_work_submit(&lpn->timer, + lpn->adv_duration + SCAN_LATENCY + + lpn->recv_win); + bt_mesh_scan_enable(); + lpn_set_state(BLE_MESH_LPN_WAIT_UPDATE); + break; + case BLE_MESH_LPN_WAIT_UPDATE: + update_timeout(lpn); + break; + default: + __ASSERT(0, "Unhandled LPN state"); + break; + } +} + +void bt_mesh_lpn_group_add(u16_t group) +{ + BT_DBG("group 0x%04x", group); + + lpn_group_add(group); + + if (!bt_mesh_lpn_established() || bt_mesh.lpn.sent_req) { + return; + } + + sub_update(TRANS_CTL_OP_FRIEND_SUB_ADD); +} + +void bt_mesh_lpn_group_del(u16_t *groups, size_t group_count) +{ + int i; + + for (i = 0; i < group_count; i++) { + if (groups[i] != BLE_MESH_ADDR_UNASSIGNED) { + BT_DBG("group 0x%04x", groups[i]); + lpn_group_del(groups[i]); + } + } + + if (!bt_mesh_lpn_established() || bt_mesh.lpn.sent_req) { + return; + } + + sub_update(TRANS_CTL_OP_FRIEND_SUB_REM); +} + +static s32_t poll_timeout(struct bt_mesh_lpn *lpn) +{ + /* If we're waiting for segment acks keep polling at high freq */ + if (bt_mesh_tx_in_progress()) { + return MIN(POLL_TIMEOUT_MAX(lpn), K_SECONDS(1)); + } + + if (lpn->poll_timeout < POLL_TIMEOUT_MAX(lpn)) { + lpn->poll_timeout *= 2; + lpn->poll_timeout = MIN(lpn->poll_timeout, + POLL_TIMEOUT_MAX(lpn)); + } + + BT_DBG("Poll Timeout is %ums", lpn->poll_timeout); + + return lpn->poll_timeout; +} + +int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_sub_confirm *msg = (void *)buf->data; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + if (buf->len < sizeof(*msg)) { + BT_WARN("Too short Friend Subscription Confirm"); + return -EINVAL; + } + + BT_DBG("xact 0x%02x", msg->xact); + + if (!lpn->sent_req) { + BT_WARN("No pending subscription list message"); + return 0; + } + + if (msg->xact != lpn->xact_pending) { + BT_WARN("Transaction mismatch (0x%02x != 0x%02x)", + msg->xact, lpn->xact_pending); + return 0; + } + + if (lpn->sent_req == TRANS_CTL_OP_FRIEND_SUB_ADD) { + group_set(lpn->added, lpn->pending); + group_zero(lpn->pending); + } else if (lpn->sent_req == TRANS_CTL_OP_FRIEND_SUB_REM) { + int i; + + group_clear(lpn->added, lpn->pending); + + for (i = 0; i < ARRAY_SIZE(lpn->groups); i++) { + if (bt_mesh_atomic_test_and_clear_bit(lpn->pending, i) && + bt_mesh_atomic_test_and_clear_bit(lpn->to_remove, i)) { + lpn->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + } + } + } else { + BT_WARN("Unexpected Friend Subscription Confirm"); + return 0; + } + + friend_response_received(lpn); + + if (lpn->groups_changed) { + sub_update(TRANS_CTL_OP_FRIEND_SUB_ADD); + sub_update(TRANS_CTL_OP_FRIEND_SUB_REM); + + if (!lpn->sent_req) { + lpn->groups_changed = 0U; + } + } + + if (lpn->pending_poll) { + send_friend_poll(); + } + + if (!lpn->sent_req) { + k_delayed_work_submit(&lpn->timer, poll_timeout(lpn)); + } + + return 0; +} + +int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_ctl_friend_update *msg = (void *)buf->data; + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + struct bt_mesh_subnet *sub = rx->sub; + u32_t iv_index = 0U; + + if (buf->len < sizeof(*msg)) { + BT_WARN("Too short Friend Update"); + return -EINVAL; + } + + if (lpn->sent_req != TRANS_CTL_OP_FRIEND_POLL) { + BT_WARN("Unexpected friend update"); + return 0; + } + + if (sub->kr_phase == BLE_MESH_KR_PHASE_2 && !rx->new_key) { + BT_WARN("Ignoring Phase 2 KR Update secured using old key"); + return 0; + } + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_INITIATOR) && + (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) == + BLE_MESH_IV_UPDATE(msg->flags))) { + bt_mesh_beacon_ivu_initiator(false); + } + + if (!lpn->established) { + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + + /* This is normally checked on the transport layer, however + * in this state we're also still accepting master + * credentials so we need to ensure the right ones (Friend + * Credentials) were used for this message. + */ + if (!rx->friend_cred) { + BT_WARN("Friend Update with wrong credentials"); + return -EINVAL; + } + + lpn->established = 1U; + + BT_INFO("Friendship established with 0x%04x", lpn->frnd); + + if (cfg->hb_pub.feat & BLE_MESH_FEAT_LOW_POWER) { + bt_mesh_heartbeat_send(); + } + + if (lpn_cb) { + lpn_cb(lpn->frnd, true); + } + + /* Set initial poll timeout */ + lpn->poll_timeout = MIN(POLL_TIMEOUT_MAX(lpn), + POLL_TIMEOUT_INIT); + } + + friend_response_received(lpn); + + iv_index = sys_be32_to_cpu(msg->iv_index); + + BT_INFO("flags 0x%02x iv_index 0x%08x md %u", msg->flags, iv_index, + msg->md); + + if (bt_mesh_kr_update(sub, BLE_MESH_KEY_REFRESH(msg->flags), + rx->new_key)) { + bt_mesh_net_beacon_update(sub); + } + + bt_mesh_net_iv_update(iv_index, BLE_MESH_IV_UPDATE(msg->flags)); + + if (lpn->groups_changed) { + sub_update(TRANS_CTL_OP_FRIEND_SUB_ADD); + sub_update(TRANS_CTL_OP_FRIEND_SUB_REM); + + if (!lpn->sent_req) { + lpn->groups_changed = 0U; + } + } + + if (msg->md) { + BT_DBG("Requesting for more messages"); + send_friend_poll(); + } + + if (!lpn->sent_req) { + k_delayed_work_submit(&lpn->timer, poll_timeout(lpn)); + } + + return 0; +} + +int bt_mesh_lpn_poll(void) +{ + if (!bt_mesh.lpn.established) { + return -EAGAIN; + } + + BT_DBG("Requesting more messages"); + + return send_friend_poll(); +} + +void bt_mesh_lpn_set_cb(void (*cb)(u16_t friend_addr, bool established)) +{ + lpn_cb = cb; +} + +int bt_mesh_lpn_init(void) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + BT_DBG("%s", __func__); + + k_delayed_work_init(&lpn->timer, lpn_timeout); + + if (lpn->state == BLE_MESH_LPN_ENABLED) { + if (!IS_ENABLED(CONFIG_BLE_MESH_LPN_ESTABLISHMENT)) { + bt_mesh_scan_enable(); + } + + send_friend_req(lpn); + } else { + bt_mesh_scan_enable(); + + if (IS_ENABLED(CONFIG_BLE_MESH_LPN_AUTO)) { + BT_DBG("Waiting %u ms for messages", LPN_AUTO_TIMEOUT); + lpn_set_state(BLE_MESH_LPN_TIMER); + k_delayed_work_submit(&lpn->timer, LPN_AUTO_TIMEOUT); + } + } + + return 0; +} + +int bt_mesh_lpn_deinit(void) +{ + struct bt_mesh_lpn *lpn = &bt_mesh.lpn; + + bt_mesh_lpn_disable(true); + + k_delayed_work_free(&lpn->timer); + + return 0; +} + +#endif /* CONFIG_BLE_MESH_LOW_POWER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/lpn.h b/components/bt/esp_ble_mesh/mesh_core/lpn.h new file mode 100644 index 000000000..97ba92955 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/lpn.h @@ -0,0 +1,78 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _LPN_H_ +#define _LPN_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int bt_mesh_lpn_friend_update(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); +int bt_mesh_lpn_friend_offer(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); +int bt_mesh_lpn_friend_clear_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); +int bt_mesh_lpn_friend_sub_cfm(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf); + +static inline bool bt_mesh_lpn_established(void) +{ +#if defined(CONFIG_BLE_MESH_LOW_POWER) + return bt_mesh.lpn.established; +#else + return false; +#endif +} + +static inline bool bt_mesh_lpn_match(u16_t addr) +{ +#if defined(CONFIG_BLE_MESH_LOW_POWER) + if (bt_mesh_lpn_established()) { + return (addr == bt_mesh.lpn.frnd); + } +#endif + return false; +} + +static inline bool bt_mesh_lpn_waiting_update(void) +{ +#if defined(CONFIG_BLE_MESH_LOW_POWER) + return (bt_mesh.lpn.state == BLE_MESH_LPN_WAIT_UPDATE); +#else + return false; +#endif +} + +static inline bool bt_mesh_lpn_timer(void) +{ +#if defined(CONFIG_BLE_MESH_LPN_AUTO) + return (bt_mesh.lpn.state == BLE_MESH_LPN_TIMER); +#else + return false; +#endif +} + +void bt_mesh_lpn_msg_received(struct bt_mesh_net_rx *rx); + +void bt_mesh_lpn_group_add(u16_t group); +void bt_mesh_lpn_group_del(u16_t *groups, size_t group_count); + +void bt_mesh_lpn_disable(bool force); + +int bt_mesh_lpn_init(void); +int bt_mesh_lpn_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _LPN_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c new file mode 100644 index 000000000..64710ddc1 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -0,0 +1,692 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG) + +#include "adv.h" +#include "prov.h" +#include "beacon.h" +#include "lpn.h" +#include "friend.h" +#include "transport.h" +#include "access.h" +#include "foundation.h" +#include "settings.h" +#include "mesh.h" +#include "mesh_hci.h" +#include "mesh_common.h" +#include "proxy_client.h" +#include "proxy_server.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#define ACTION_ENTER 0x01 +#define ACTION_SUSPEND 0x02 +#define ACTION_EXIT 0x03 + +static bool mesh_init = false; + +int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx, + u8_t flags, u32_t iv_index, u16_t addr, + const u8_t dev_key[16]) +{ + bool pb_gatt_enabled = false; + int err = 0; + + BT_INFO("Primary Element: 0x%04x", addr); + BT_INFO("net_idx 0x%04x flags 0x%02x iv_index 0x%04x", + net_idx, flags, iv_index); + BT_INFO("dev_key %s", bt_hex(dev_key, 16)); + + if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_VALID)) { + return -EALREADY; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + if (bt_mesh_proxy_prov_disable(false) == 0) { + pb_gatt_enabled = true; + } else { + pb_gatt_enabled = false; + } + } else { + pb_gatt_enabled = false; + } + + err = bt_mesh_net_create(net_idx, flags, net_key, iv_index); + if (err) { + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID); + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && pb_gatt_enabled) { + bt_mesh_proxy_prov_enable(); + } + + return err; + } + + bt_mesh.seq = 0U; + + bt_mesh_comp_provision(addr); + + memcpy(bt_mesh.dev_key, dev_key, 16); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Storing network information persistently"); + bt_mesh_store_net(); + bt_mesh_store_subnet(&bt_mesh.sub[0]); + bt_mesh_store_iv(false); + } + + /* Add this to avoid "already active status" for bt_mesh_scan_enable() */ + bt_mesh_scan_disable(); + + bt_mesh_net_start(); + + return 0; +} + +void bt_mesh_node_reset(void) +{ + if (!bt_mesh_is_provisioned()) { + BT_WARN("%s, Not provisioned", __func__); + return; + } + + bt_mesh.iv_index = 0U; + bt_mesh.seq = 0U; + + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID); + + k_delayed_work_cancel(&bt_mesh.ivu_timer); + + bt_mesh_cfg_reset(); + + bt_mesh_rx_reset(); + bt_mesh_tx_reset(); + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_disable(true); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + bt_mesh_proxy_gatt_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_net(); + } + + (void)memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key)); + + bt_mesh_scan_disable(); + bt_mesh_beacon_disable(); + + bt_mesh_comp_unprovision(); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_seq(); + bt_mesh_clear_role(); + } + + memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags)); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + bt_mesh_prov_reset(); + } +} + +bool bt_mesh_is_node(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_NODE); +} + +bool bt_mesh_is_provisioned(void) +{ + if (bt_mesh_is_node()) { + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID); + } else { + return false; + } +} + +bool bt_mesh_is_provisioner(void) +{ + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); +} + +bool bt_mesh_is_provisioner_en(void) +{ + if (bt_mesh_is_provisioner()) { + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + } else { + return false; + } +} + +int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) +{ + if (bt_mesh_is_provisioned()) { + return -EALREADY; + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (bearers & BLE_MESH_PROV_ADV)) { + /* Make sure we're scanning for provisioning invitations */ + bt_mesh_scan_enable(); + /* Enable unprovisioned beacon sending */ + bt_mesh_beacon_enable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { + bt_mesh_proxy_prov_enable(); + bt_mesh_adv_update(); + } + + return 0; +} + +int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) +{ + if (bt_mesh_is_provisioned()) { + return -EALREADY; + } + + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_role(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (bearers & BLE_MESH_PROV_ADV)) { + bt_mesh_beacon_disable(); + bt_mesh_scan_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { + bt_mesh_proxy_prov_disable(true); + } + + return 0; +} + +static void model_suspend(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + if (mod->pub && mod->pub->update) { + mod->pub->count = 0U; + k_delayed_work_cancel(&mod->pub->timer); + } +} + +int bt_mesh_suspend(void) +{ + int err = 0; + + if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + return -EINVAL; + } + + if (bt_mesh_atomic_test_and_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) { + return -EALREADY; + } + + err = bt_mesh_scan_disable(); + if (err) { + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED); + BT_WARN("%s, Disabling scanning failed (err %d)", __func__, err); + return err; + } + + bt_mesh_hb_pub_disable(); + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_disable(); + } + + bt_mesh_model_foreach(model_suspend, NULL); + + return 0; +} + +static void model_resume(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + if (mod->pub && mod->pub->update) { + s32_t period_ms = bt_mesh_model_pub_period_get(mod); + + if (period_ms) { + k_delayed_work_submit(&mod->pub->timer, period_ms); + } + } +} + +int bt_mesh_resume(void) +{ + int err = 0; + + if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_VALID)) { + return -EINVAL; + } + + if (!bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SUSPENDED)) { + return -EALREADY; + } + + err = bt_mesh_scan_enable(); + if (err) { + BT_WARN("%s, Re-enabling scanning failed (err %d)", __func__, err); + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_SUSPENDED); + return err; + } + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); + } + + bt_mesh_model_foreach(model_resume, NULL); + + return err; +} + +int bt_mesh_init(const struct bt_mesh_prov *prov, + const struct bt_mesh_comp *comp) +{ + int err = 0; + + if (mesh_init == true) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + bt_mesh_mutex_init(); + + bt_mesh_timer_init(); + + bt_mesh_hci_init(); + + bt_mesh_adapt_init(); + + err = bt_mesh_comp_register(comp); + if (err) { + return err; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { + bt_mesh_gatt_init(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { + if ((IS_ENABLED(CONFIG_BLE_MESH_NODE) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + bt_mesh_proxy_init(); + } + if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + bt_mesh_proxy_prov_client_init(); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + err = bt_mesh_prov_init(prov); + if (err) { + return err; + } + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_prov_init(prov); + if (err) { + return err; + } + } + } + + bt_mesh_net_init(); + bt_mesh_trans_init(); + + /* Changed by Espressif, add a random delay (0 ~ 3s) */ + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + u32_t delay = 0; + bt_mesh_rand(&delay, sizeof(u32_t)); + vTaskDelay((delay % 3000) / portTICK_PERIOD_MS); + } + + bt_mesh_beacon_init(); + + bt_mesh_adv_init(); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + bt_mesh_provisioner_init(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_settings_init(); + } + + mesh_init = true; + return 0; +} + +int bt_mesh_deinit(struct bt_mesh_deinit_param *param) +{ + int err = 0; + + if (param == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (mesh_init == false) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV)) { + bt_mesh_beacon_disable(); + bt_mesh_scan_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_proxy_prov_disable(true); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_disable(); + } + + bt_mesh_scan_disable(); + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + err = bt_mesh_prov_deinit(); + if (err) { + return err; + } + } + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_prov_deinit(param->erase); + if (err) { + return err; + } + } + } + + bt_mesh_trans_deinit(param->erase); + bt_mesh_net_deinit(param->erase); + + bt_mesh_beacon_deinit(); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY)) { + if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) { + bt_mesh_proxy_deinit(); + } + } + + if ((IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) || + IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)) { + bt_mesh_proxy_prov_client_deinit(); + } + + bt_mesh_gatt_deinit(); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + err = bt_mesh_provisioner_deinit(param->erase); + if (err) { + return err; + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_deinit(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + bt_mesh_lpn_deinit(); + } + + bt_mesh_adv_deinit(); + + err = bt_mesh_comp_deregister(); + if (err) { + return err; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + if (param->erase) { + bt_mesh_clear_role(); + } + bt_mesh_settings_deinit(); + } + + bt_mesh_timer_deinit(); + + bt_mesh_mutex_deinit(); + + mesh_init = false; + return 0; +} + +#if defined(CONFIG_BLE_MESH_PROVISIONER) +int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) +{ + bt_mesh_provisioner_set_prov_bearer(bearers, false); + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (bearers & BLE_MESH_PROV_ADV)) { + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_BEACON, NULL); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_PROV_ADV, NULL); + } +#endif + + if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (bearers & BLE_MESH_PROV_ADV)) || + (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (bearers & BLE_MESH_PROV_GATT))) { + bt_mesh_scan_enable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { + bt_mesh_provisioner_pb_gatt_enable(); + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_init(); + } + + return 0; +} + +int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) +{ + int err = 0; + + if (bt_mesh_is_provisioner_en()) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + err = bt_mesh_provisioner_set_prov_info(); + if (err) { + BT_ERR("%s, Failed to set provisioning info", __func__); + return err; + } + + err = bt_mesh_provisioner_net_create(); + if (err) { + BT_ERR("%s, Failed to create network", __func__); + return err; + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_PROVISIONER); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_role(); + } + + return bt_mesh_provisioner_net_start(bearers); +} + +int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) +{ + bt_mesh_prov_bearer_t enable = 0U; + + if (!bt_mesh_is_provisioner_en()) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + enable = bt_mesh_provisioner_get_prov_bearer(); + if (!(enable & bearers)) { + BT_ERR("%s, Bearers mismatch", __func__); + return -EINVAL; + } + + bt_mesh_provisioner_set_prov_bearer(bearers, true); + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + (enable & BLE_MESH_PROV_GATT) && + (bearers & BLE_MESH_PROV_GATT)) { + bt_mesh_provisioner_pb_gatt_disable(); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_PROV_ADV, NULL); +#endif + } + + if (!(enable & (~bearers))) { + /* Provisioner is disabled completely, disable scan here */ + bt_mesh_scan_disable(); + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + if (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + (enable & BLE_MESH_PROV_ADV)) { + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_BEACON, NULL); + } +#endif + + /* Clear corresponding flags */ + bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); + + /* When Provisioner is disabled, the device role indicated by bt_mesh.flags + * will not be cleared, because when Provisioner is restarted after disabled, + * its previous information can be recovered from flash properly. + */ + + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY); + } + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +/* The following API is for fast provisioning */ + +#if CONFIG_BLE_MESH_FAST_PROV +u8_t bt_mesh_set_fast_prov_action(u8_t action) +{ + if (!action || action > ACTION_EXIT) { + return 0x01; + } + + if ((!bt_mesh_is_provisioner_en() && (action == ACTION_SUSPEND || action == ACTION_EXIT)) || + (bt_mesh_is_provisioner_en() && (action == ACTION_ENTER))) { + BT_WARN("%s, Already", __func__); + return 0x0; + } + + if (action == ACTION_ENTER) { +#if 0 + /* If the device is provisioned using PB-GATT and connected to + * the phone with proxy service, proxy_gatt shall not be disabled + * here. The node needs to send some status messages to the phone + * while it is connected. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + bt_mesh_proxy_gatt_disable(); + } +#endif + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_disable(); + } + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_enable(); + } + bt_mesh_provisioner_set_primary_elem_addr(bt_mesh_primary_addr()); + bt_mesh_provisioner_set_prov_bearer(BLE_MESH_PROV_ADV, false); + bt_mesh_provisioner_fast_prov_enable(true); + bt_mesh_atomic_or(bt_mesh.flags, BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV)); + } else { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_provisioner_pb_gatt_disable(); + } + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); + } +#if 0 + /* Mesh Proxy GATT will be re-enabled on application layer */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { + bt_mesh_proxy_gatt_enable(); + bt_mesh_adv_update(); + } +#endif + bt_mesh_atomic_and(bt_mesh.flags, ~(BIT(BLE_MESH_PROVISIONER) | BIT(BLE_MESH_VALID_PROV))); + bt_mesh_provisioner_fast_prov_enable(false); + if (action == ACTION_EXIT) { + bt_mesh_provisioner_remove_node(NULL); + } + } + + return 0x0; +} +#endif /* CONFIG_BLE_MESH_FAST_PROV */ diff --git a/components/bt/esp_ble_mesh/mesh_core/mesh.h b/components/bt/esp_ble_mesh/mesh_core/mesh.h new file mode 100644 index 000000000..5a409a891 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/mesh.h @@ -0,0 +1,32 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _MESH_H_ +#define _MESH_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_KEY_PRIMARY 0x0000 +#define BLE_MESH_KEY_ANY 0xffff + +#define BLE_MESH_ADDR_IS_UNICAST(addr) ((addr) && (addr) < 0x8000) +#define BLE_MESH_ADDR_IS_GROUP(addr) ((addr) >= 0xc000 && (addr) <= 0xff00) +#define BLE_MESH_ADDR_IS_VIRTUAL(addr) ((addr) >= 0x8000 && (addr) < 0xc000) +#define BLE_MESH_ADDR_IS_RFU(addr) ((addr) >= 0xff00 && (addr) <= 0xfffb) + +struct bt_mesh_net; + +#ifdef __cplusplus +} +#endif + +#endif /* _MESH_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/net.c b/components/bt/esp_ble_mesh/mesh_core/net.c new file mode 100644 index 000000000..548182e31 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/net.c @@ -0,0 +1,1590 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_NET) + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "lpn.h" +#include "friend.h" +#include "transport.h" +#include "access.h" +#include "foundation.h" +#include "beacon.h" +#include "settings.h" +#include "prov.h" +#include "proxy_client.h" +#include "proxy_server.h" +#include "provisioner_main.h" + +/* Minimum valid Mesh Network PDU length. The Network headers + * themselves take up 9 bytes. After that there is a minumum of 1 byte + * payload for both CTL=1 and CTL=0 PDUs (smallest OpCode is 1 byte). CTL=1 + * PDUs must use a 64-bit (8 byte) NetMIC, whereas CTL=0 PDUs have at least + * a 32-bit (4 byte) NetMIC and AppMIC giving again a total of 8 bytes. + */ +#define BLE_MESH_NET_MIN_PDU_LEN (BLE_MESH_NET_HDR_LEN + 1 + 8) + +/* Seq limit after IV Update is triggered */ +#define IV_UPDATE_SEQ_LIMIT 8000000 + +#define IVI(pdu) ((pdu)[0] >> 7) +#define NID(pdu) ((pdu)[0] & 0x7f) +#define CTL(pdu) ((pdu)[1] >> 7) +#define TTL(pdu) ((pdu)[1] & 0x7f) +#define SEQ(pdu) (sys_get_be24(&(pdu)[2])) +#define SRC(pdu) (sys_get_be16(&(pdu)[5])) +#define DST(pdu) (sys_get_be16(&(pdu)[7])) + +/* Determine how many friendship credentials we need */ +#if defined(CONFIG_BLE_MESH_FRIEND) +#define FRIEND_CRED_COUNT CONFIG_BLE_MESH_FRIEND_LPN_COUNT +#elif defined(CONFIG_BLE_MESH_LOW_POWER) +#define FRIEND_CRED_COUNT CONFIG_BLE_MESH_SUBNET_COUNT +#else +#define FRIEND_CRED_COUNT 0 +#endif + +#if FRIEND_CRED_COUNT > 0 +static struct friend_cred friend_cred[FRIEND_CRED_COUNT]; +#endif + +static struct { + u32_t src:15, /* MSB of source address is always 0 */ + seq:17; +} msg_cache[CONFIG_BLE_MESH_MSG_CACHE_SIZE]; +static u16_t msg_cache_next; + +/* Singleton network context (the implementation only supports one) */ +struct bt_mesh_net bt_mesh = { + .local_queue = SYS_SLIST_STATIC_INIT(&bt_mesh.local_queue), + .sub = { + [0 ... (CONFIG_BLE_MESH_SUBNET_COUNT - 1)] = { + .net_idx = BLE_MESH_KEY_UNUSED, + } + }, + .app_keys = { + [0 ... (CONFIG_BLE_MESH_APP_KEY_COUNT - 1)] = { + .net_idx = BLE_MESH_KEY_UNUSED, + } + }, +}; + +static u32_t dup_cache[4]; +static int dup_cache_next; + +#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) +#define BLE_MESH_MAX_STORED_RELAY_COUNT (CONFIG_BLE_MESH_RELAY_ADV_BUF_COUNT / 2) +#endif + +static bool check_dup(struct net_buf_simple *data) +{ + const u8_t *tail = net_buf_simple_tail(data); + u32_t val = 0U; + int i; + + val = sys_get_be32(tail - 4) ^ sys_get_be32(tail - 8); + + for (i = 0; i < ARRAY_SIZE(dup_cache); i++) { + if (dup_cache[i] == val) { + return true; + } + } + + dup_cache[dup_cache_next++] = val; + dup_cache_next %= ARRAY_SIZE(dup_cache); + + return false; +} + +static bool msg_cache_match(struct bt_mesh_net_rx *rx, + struct net_buf_simple *pdu) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(msg_cache); i++) { + if (msg_cache[i].src == SRC(pdu->data) && + msg_cache[i].seq == (SEQ(pdu->data) & BIT_MASK(17))) { + return true; + } + } + + return false; +} + +static void msg_cache_add(struct bt_mesh_net_rx *rx) +{ + rx->msg_cache_idx = msg_cache_next++; + msg_cache[rx->msg_cache_idx].src = rx->ctx.addr; + msg_cache[rx->msg_cache_idx].seq = rx->seq; + msg_cache_next %= ARRAY_SIZE(msg_cache); +} + +#if CONFIG_BLE_MESH_PROVISIONER +void bt_mesh_msg_cache_clear(u16_t unicast_addr, u8_t elem_num) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(msg_cache); i++) { + if (msg_cache[i].src >= unicast_addr && + msg_cache[i].src < unicast_addr + elem_num) { + memset(&msg_cache[i], 0, sizeof(msg_cache[i])); + } + } +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +struct bt_mesh_subnet *bt_mesh_subnet_get(u16_t net_idx) +{ + int i; + + if (net_idx == BLE_MESH_KEY_ANY) { + return &bt_mesh.sub[0]; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx == net_idx) { + return &bt_mesh.sub[i]; + } + } + + return NULL; +} + +int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, + const u8_t key[16]) +{ + u8_t p[] = { 0 }; + u8_t nid = 0U; + int err = 0; + + err = bt_mesh_k2(key, p, sizeof(p), &nid, keys->enc, keys->privacy); + if (err) { + BT_ERR("%s, Unable to generate NID, EncKey & PrivacyKey", __func__); + return err; + } + + memcpy(keys->net, key, 16); + + keys->nid = nid; + + BT_DBG("NID 0x%02x EncKey %s", keys->nid, bt_hex(keys->enc, 16)); + BT_DBG("PrivacyKey %s", bt_hex(keys->privacy, 16)); + + err = bt_mesh_k3(key, keys->net_id); + if (err) { + BT_ERR("%s, Unable to generate Net ID", __func__); + return err; + } + + BT_DBG("NetID %s", bt_hex(keys->net_id, 8)); + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + err = bt_mesh_identity_key(key, keys->identity); + if (err) { + BT_ERR("%s, Unable to generate IdentityKey", __func__); + return err; + } + + BT_DBG("IdentityKey %s", bt_hex(keys->identity, 16)); +#endif /* GATT_PROXY */ + + err = bt_mesh_beacon_key(key, keys->beacon); + if (err) { + BT_ERR("%s, Unable to generate beacon key", __func__); + return err; + } + + BT_DBG("BeaconKey %s", bt_hex(keys->beacon, 16)); + + return 0; +} + +#if (defined(CONFIG_BLE_MESH_LOW_POWER) || \ + defined(CONFIG_BLE_MESH_FRIEND)) +int friend_cred_set(struct friend_cred *cred, u8_t idx, const u8_t net_key[16]) +{ + u16_t lpn_addr = 0U, frnd_addr = 0U; + int err = 0; + u8_t p[9] = {0}; + +#if defined(CONFIG_BLE_MESH_LOW_POWER) + if (cred->addr == bt_mesh.lpn.frnd) { + lpn_addr = bt_mesh_primary_addr(); + frnd_addr = cred->addr; + } else { + lpn_addr = cred->addr; + frnd_addr = bt_mesh_primary_addr(); + } +#else + lpn_addr = cred->addr; + frnd_addr = bt_mesh_primary_addr(); +#endif + + BT_DBG("LPNAddress 0x%04x FriendAddress 0x%04x", lpn_addr, frnd_addr); + BT_DBG("LPNCounter 0x%04x FriendCounter 0x%04x", cred->lpn_counter, + cred->frnd_counter); + + p[0] = 0x01; + sys_put_be16(lpn_addr, p + 1); + sys_put_be16(frnd_addr, p + 3); + sys_put_be16(cred->lpn_counter, p + 5); + sys_put_be16(cred->frnd_counter, p + 7); + + err = bt_mesh_k2(net_key, p, sizeof(p), &cred->cred[idx].nid, + cred->cred[idx].enc, cred->cred[idx].privacy); + if (err) { + BT_ERR("%s, Unable to generate NID, EncKey & PrivacyKey", __func__); + return err; + } + + BT_DBG("Friend NID 0x%02x EncKey %s", cred->cred[idx].nid, + bt_hex(cred->cred[idx].enc, 16)); + BT_DBG("Friend PrivacyKey %s", bt_hex(cred->cred[idx].privacy, 16)); + + return 0; +} + +void friend_cred_refresh(u16_t net_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { + struct friend_cred *cred = &friend_cred[i]; + + if (cred->addr != BLE_MESH_ADDR_UNASSIGNED && + cred->net_idx == net_idx) { + memcpy(&cred->cred[0], &cred->cred[1], + sizeof(cred->cred[0])); + } + } +} + +int friend_cred_update(struct bt_mesh_subnet *sub) +{ + int err = 0, i; + + BT_DBG("net_idx 0x%04x", sub->net_idx); + + for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { + struct friend_cred *cred = &friend_cred[i]; + + if (cred->addr == BLE_MESH_ADDR_UNASSIGNED || + cred->net_idx != sub->net_idx) { + continue; + } + + err = friend_cred_set(cred, 1, sub->keys[1].net); + if (err) { + return err; + } + } + + return 0; +} + +struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr, + u16_t lpn_counter, u16_t frnd_counter) +{ + struct friend_cred *cred = NULL; + int i, err = 0; + + BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr); + + for (cred = NULL, i = 0; i < ARRAY_SIZE(friend_cred); i++) { + if ((friend_cred[i].addr == BLE_MESH_ADDR_UNASSIGNED) || + (friend_cred[i].addr == addr && + friend_cred[i].net_idx == sub->net_idx)) { + cred = &friend_cred[i]; + break; + } + } + + if (!cred) { + BT_WARN("No free friend credential slots"); + return NULL; + } + + cred->net_idx = sub->net_idx; + cred->addr = addr; + cred->lpn_counter = lpn_counter; + cred->frnd_counter = frnd_counter; + + err = friend_cred_set(cred, 0, sub->keys[0].net); + if (err) { + friend_cred_clear(cred); + return NULL; + } + + if (sub->kr_flag) { + err = friend_cred_set(cred, 1, sub->keys[1].net); + if (err) { + friend_cred_clear(cred); + return NULL; + } + } + + return cred; +} + +void friend_cred_clear(struct friend_cred *cred) +{ + cred->net_idx = BLE_MESH_KEY_UNUSED; + cred->addr = BLE_MESH_ADDR_UNASSIGNED; + cred->lpn_counter = 0U; + cred->frnd_counter = 0U; + (void)memset(cred->cred, 0, sizeof(cred->cred)); +} + +int friend_cred_del(u16_t net_idx, u16_t addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { + struct friend_cred *cred = &friend_cred[i]; + + if (cred->addr == addr && cred->net_idx == net_idx) { + friend_cred_clear(cred); + return 0; + } + } + + return -ENOENT; +} + +int friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid, + const u8_t **enc, const u8_t **priv) +{ + int i; + + BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr); + + for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { + struct friend_cred *cred = &friend_cred[i]; + + if (cred->net_idx != sub->net_idx) { + continue; + } + + if (addr != BLE_MESH_ADDR_UNASSIGNED && cred->addr != addr) { + continue; + } + + if (nid) { + *nid = cred->cred[sub->kr_flag].nid; + } + + if (enc) { + *enc = cred->cred[sub->kr_flag].enc; + } + + if (priv) { + *priv = cred->cred[sub->kr_flag].privacy; + } + + return 0; + } + + return -ENOENT; +} +#else +int friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid, + const u8_t **enc, const u8_t **priv) +{ + return -ENOENT; +} +#endif /* FRIEND || LOW_POWER */ + +u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub) +{ + u8_t flags = 0x00; + + if (sub && sub->kr_flag) { + flags |= BLE_MESH_NET_FLAG_KR; + } + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { + flags |= BLE_MESH_NET_FLAG_IVU; + } + + return flags; +} + +int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub) +{ + u8_t flags = bt_mesh_net_flags(sub); + struct bt_mesh_subnet_keys *keys = NULL; + + if (sub->kr_flag) { + BT_DBG("NetIndex %u Using new key", sub->net_idx); + keys = &sub->keys[1]; + } else { + BT_DBG("NetIndex %u Using current key", sub->net_idx); + keys = &sub->keys[0]; + } + + BT_DBG("flags 0x%02x, IVI 0x%08x", flags, bt_mesh.iv_index); + + return bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id, + bt_mesh.iv_index, sub->auth); +} + +int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], + u32_t iv_index) +{ + struct bt_mesh_subnet *sub = NULL; + int err = 0; + + BT_DBG("idx %u flags 0x%02x iv_index %u", idx, flags, iv_index); + + BT_DBG("NetKey %s", bt_hex(key, 16)); + + (void)memset(msg_cache, 0, sizeof(msg_cache)); + msg_cache_next = 0U; + + sub = &bt_mesh.sub[0]; + + sub->kr_flag = BLE_MESH_KEY_REFRESH(flags); + if (sub->kr_flag) { + err = bt_mesh_net_keys_create(&sub->keys[1], key); + if (err) { + return -EIO; + } + + sub->kr_phase = BLE_MESH_KR_PHASE_2; + } else { + err = bt_mesh_net_keys_create(&sub->keys[0], key); + if (err) { + return -EIO; + } + } + + sub->net_idx = idx; + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; + } else { + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + } + + bt_mesh.iv_index = iv_index; + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, + BLE_MESH_IV_UPDATE(flags)); + + /* Set minimum required hours, since the 96-hour minimum requirement + * doesn't apply straight after provisioning (since we can't know how + * long has actually passed since the network changed its state). + */ + bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS; + + /* Make sure we have valid beacon data to be sent */ + bt_mesh_net_beacon_update(sub); + + return 0; +} + +void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub) +{ + int i; + + BT_DBG("idx 0x%04x", sub->net_idx); + + memcpy(&sub->keys[0], &sub->keys[1], sizeof(sub->keys[0])); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Store updated NetKey persistently"); + bt_mesh_store_subnet(sub); + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx != sub->net_idx || !key->updated) { + continue; + } + + memcpy(&key->keys[0], &key->keys[1], sizeof(key->keys[0])); + key->updated = false; + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + BT_DBG("Store updated AppKey persistently"); + bt_mesh_store_app_key(key); + } + } +} + +bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key) +{ + if (new_kr != sub->kr_flag && sub->kr_phase == BLE_MESH_KR_NORMAL) { + BT_WARN("KR change in normal operation. Are we blacklisted?"); + return false; + } + + sub->kr_flag = new_kr; + + if (sub->kr_flag) { + if (sub->kr_phase == BLE_MESH_KR_PHASE_1) { + BT_INFO("Phase 1 -> Phase 2"); + sub->kr_phase = BLE_MESH_KR_PHASE_2; + return true; + } + } else { + switch (sub->kr_phase) { + case BLE_MESH_KR_PHASE_1: + if (!new_key) { + /* Ignore */ + break; + } + /* Upon receiving a Secure Network beacon with the KR flag set + * to 0 using the new NetKey in Phase 1, the node shall + * immediately transition to Phase 3, which effectively skips + * Phase 2. + * + * Intentional fall-through. + */ + case BLE_MESH_KR_PHASE_2: + BT_INFO("KR Phase 0x%02x -> Normal", sub->kr_phase); + bt_mesh_net_revoke_keys(sub); + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) || + IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + friend_cred_refresh(sub->net_idx); + } + sub->kr_phase = BLE_MESH_KR_NORMAL; + return true; + } + } + + return false; +} + +void bt_mesh_rpl_reset(void) +{ + int i; + + /* Discard "old old" IV Index entries from RPL and flag + * any other ones (which are valid) as old. + */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + + if (rpl->src) { + if (rpl->old_iv) { + (void)memset(rpl, 0, sizeof(*rpl)); + } else { + rpl->old_iv = true; + } + } + } +} + +#if defined(CONFIG_BLE_MESH_IV_UPDATE_TEST) +void bt_mesh_iv_update_test(bool enable) +{ + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_TEST, enable); + /* Reset the duration variable - needed for some PTS tests */ + bt_mesh.ivu_duration = 0U; +} + +bool bt_mesh_iv_update(void) +{ + if (!bt_mesh_is_provisioned()) { + BT_ERR("%s, Not yet provisioned", __func__); + return false; + } + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { + bt_mesh_net_iv_update(bt_mesh.iv_index, false); + } else { + bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); + } + + bt_mesh_net_sec_update(NULL); + + return bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS); +} +#endif /* CONFIG_BLE_MESH_IV_UPDATE_TEST */ + +/* Used for sending immediate beacons to Friend queues and GATT clients */ +void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_sec_update(sub ? sub->net_idx : BLE_MESH_KEY_ANY); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { + bt_mesh_proxy_beacon_send(sub); + } +} + +bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update) +{ + int i; + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { + /* We're currently in IV Update mode */ + + if (iv_index != bt_mesh.iv_index) { + BT_WARN("IV Index mismatch: 0x%08x != 0x%08x", + iv_index, bt_mesh.iv_index); + return false; + } + + if (iv_update) { + /* Nothing to do */ + BT_DBG("Already in IV Update in Progress state"); + return false; + } + } else { + /* We're currently in Normal mode */ + + if (iv_index == bt_mesh.iv_index) { + BT_DBG("Same IV Index in normal mode"); + return false; + } + + if (iv_index < bt_mesh.iv_index || + iv_index > bt_mesh.iv_index + 42) { + BT_ERR("IV Index out of sync: 0x%08x != 0x%08x", + iv_index, bt_mesh.iv_index); + return false; + } + + if (iv_index > bt_mesh.iv_index + 1) { + BT_WARN("Performing IV Index Recovery"); + (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); + bt_mesh.iv_index = iv_index; + bt_mesh.seq = 0U; + goto do_update; + } + + if (iv_index == bt_mesh.iv_index + 1 && !iv_update) { + BT_WARN("Ignoring new index in normal mode"); + return false; + } + + if (!iv_update) { + /* Nothing to do */ + BT_DBG("Already in Normal state"); + return false; + } + } + + if (!(IS_ENABLED(CONFIG_BLE_MESH_IV_UPDATE_TEST) && + bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_TEST))) { + if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { + BT_WARN("IV Update before minimum duration"); + return false; + } + } + + /* Defer change to Normal Operation if there are pending acks */ + if (!iv_update && bt_mesh_tx_in_progress()) { + BT_WARN("IV Update deferred because of pending transfer"); + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING); + return false; + } + +do_update: + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv_update); + bt_mesh.ivu_duration = 0U; + + if (iv_update) { + bt_mesh.iv_index = iv_index; + BT_INFO("IV Update state entered. New index 0x%08x", + bt_mesh.iv_index); + + bt_mesh_rpl_reset(); + } else { + BT_INFO("Normal mode entered"); + bt_mesh.seq = 0U; + } + + k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); + + size_t subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + if (sub && sub->net_idx != BLE_MESH_KEY_UNUSED) { + bt_mesh_net_beacon_update(sub); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_iv(false); + } + + return true; +} + +bool bt_mesh_primary_subnet_exist(void) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + if (bt_mesh_subnet_get(BLE_MESH_KEY_PRIMARY)) { + return true; + } + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY)) { + return true; + } + } + + return false; +} + +u32_t bt_mesh_next_seq(void) +{ + u32_t seq = bt_mesh.seq++; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_seq(); + } + + if (!bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) && + bt_mesh.seq > IV_UPDATE_SEQ_LIMIT && + bt_mesh_primary_subnet_exist()) { + bt_mesh_beacon_ivu_initiator(true); + bt_mesh_net_iv_update(bt_mesh.iv_index + 1, true); + bt_mesh_net_sec_update(NULL); + } + + return seq; +} + +int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, + bool new_key, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + const u8_t *enc = NULL, *priv = NULL; + u32_t seq = 0U; + u16_t dst = 0U; + int err = 0; + + BT_DBG("net_idx 0x%04x new_key %u len %u", sub->net_idx, new_key, + buf->len); + + enc = sub->keys[new_key].enc; + priv = sub->keys[new_key].privacy; + + err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv); + if (err) { + BT_ERR("%s, De-obfuscate failed (err %d)", __func__, err); + return err; + } + + err = bt_mesh_net_decrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false); + if (err) { + BT_ERR("%s, Decrypt failed (err %d)", __func__, err); + return err; + } + + /* Update with a new sequence number */ + seq = bt_mesh_next_seq(); + sys_put_be24(seq, &buf->data[2]); + + /* Get destination, in case it's a proxy client */ + dst = DST(buf->data); + + err = bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_TX, false); + if (err) { + BT_ERR("%s, Encrypt failed (err %d)", __func__, err); + return err; + } + + err = bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv); + if (err) { + BT_ERR("%s, Obfuscate failed (err %d)", __func__, err); + return err; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + bt_mesh_proxy_relay(&buf->b, dst) && + BLE_MESH_ADDR_IS_UNICAST(dst)) { + send_cb_finalize(cb, cb_data); + return 0; + } + + bt_mesh_adv_send(buf, cb, cb_data); + return 0; +} + +static void bt_mesh_net_local(struct k_work *work) +{ + struct net_buf *buf = NULL; + + while ((buf = net_buf_slist_get(&bt_mesh.local_queue))) { + BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + bt_mesh_net_recv(&buf->b, 0, BLE_MESH_NET_IF_LOCAL); + net_buf_unref(buf); + } +} + +int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, + bool proxy) +{ + const bool ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED); + const u8_t *enc = NULL, *priv = NULL; + u8_t nid = 0U; + int err = 0; + + if (ctl && net_buf_simple_tailroom(buf) < BLE_MESH_MIC_LONG) { + BT_ERR("%s, Insufficient MIC space for CTL PDU", __func__); + return -EINVAL; + } else if (net_buf_simple_tailroom(buf) < BLE_MESH_MIC_SHORT) { + BT_ERR("%s, Insufficient MIC space for PDU", __func__); + return -EINVAL; + } + + BT_DBG("src 0x%04x dst 0x%04x ctl %u seq 0x%06x", + tx->src, tx->ctx->addr, ctl, bt_mesh.seq); + + net_buf_simple_push_be16(buf, tx->ctx->addr); + net_buf_simple_push_be16(buf, tx->src); + + net_buf_simple_push_be24(buf, bt_mesh_next_seq()); + + if (ctl) { + net_buf_simple_push_u8(buf, tx->ctx->send_ttl | 0x80); + } else { + net_buf_simple_push_u8(buf, tx->ctx->send_ttl); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && tx->friend_cred) { + if (friend_cred_get(tx->sub, BLE_MESH_ADDR_UNASSIGNED, + &nid, &enc, &priv)) { + BT_WARN("Falling back to master credentials"); + + tx->friend_cred = 0U; + + nid = tx->sub->keys[tx->sub->kr_flag].nid; + enc = tx->sub->keys[tx->sub->kr_flag].enc; + priv = tx->sub->keys[tx->sub->kr_flag].privacy; + } + } else { + tx->friend_cred = 0U; + nid = tx->sub->keys[tx->sub->kr_flag].nid; + enc = tx->sub->keys[tx->sub->kr_flag].enc; + priv = tx->sub->keys[tx->sub->kr_flag].privacy; + } + + net_buf_simple_push_u8(buf, (nid | (BLE_MESH_NET_IVI_TX & 1) << 7)); + + err = bt_mesh_net_encrypt(enc, buf, BLE_MESH_NET_IVI_TX, proxy); + if (err) { + return err; + } + + return bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_TX, priv); +} + +int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + int err = 0; + + BT_DBG("src 0x%04x dst 0x%04x len %u headroom %u tailroom %u", + tx->src, tx->ctx->addr, buf->len, net_buf_headroom(buf), + net_buf_tailroom(buf)); + BT_DBG("Payload len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + BT_DBG("Seq 0x%06x", bt_mesh.seq); + + if (tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) { + tx->ctx->send_ttl = bt_mesh_default_ttl_get(); + } + + err = bt_mesh_net_encode(tx, &buf->b, false); + if (err) { + goto done; + } + + /* Deliver to GATT Proxy Clients if necessary. Mesh spec 3.4.5.2: + * "The output filter of the interface connected to advertising or + * GATT bearers shall drop all messages with TTL value set to 1." + */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + tx->ctx->send_ttl != 1U) { + if (bt_mesh_proxy_relay(&buf->b, tx->ctx->addr) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + /* Notify completion if this only went + * through the Mesh Proxy. + */ + send_cb_finalize(cb, cb_data); + + err = 0; + goto done; + } + } + +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + if (tx->ctx->send_ttl != 1U) { + if (bt_mesh_proxy_client_send(&buf->b, tx->ctx->addr)) { + /* If Proxy Client succeeds to send messages with GATT bearer, + * we can directly finish here. And if not, which means no + * connection has been created with Proxy Client, here we will + * use advertising bearer for the messages. + */ + send_cb_finalize(cb, cb_data); + + err = 0; + goto done; + } + } +#endif + + /* Deliver to local network interface if necessary */ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() && + (bt_mesh_fixed_group_match(tx->ctx->addr) || + bt_mesh_elem_find(tx->ctx->addr))) { + if (cb && cb->start) { + cb->start(0, 0, cb_data); + } + net_buf_slist_put(&bt_mesh.local_queue, net_buf_ref(buf)); + if (cb && cb->end) { + cb->end(0, cb_data); + } + k_work_submit(&bt_mesh.local_work); + } else if (tx->ctx->send_ttl != 1U) { + /* Deliver to the advertising network interface. Mesh spec + * 3.4.5.2: "The output filter of the interface connected to + * advertising or GATT bearers shall drop all messages with + * TTL value set to 1." + */ + bt_mesh_adv_send(buf, cb, cb_data); + } + +done: + net_buf_unref(buf); + return err; +} + +static bool auth_match(struct bt_mesh_subnet_keys *keys, + const u8_t net_id[8], u8_t flags, + u32_t iv_index, const u8_t auth[8]) +{ + u8_t net_auth[8] = {0}; + + if (memcmp(net_id, keys->net_id, 8)) { + return false; + } + + bt_mesh_beacon_auth(keys->beacon, flags, keys->net_id, iv_index, + net_auth); + + if (memcmp(auth, net_auth, 8)) { + BT_WARN("Authentication Value %s != %s", + bt_hex(auth, 8), bt_hex(net_auth, 8)); + return false; + } + + return true; +} + +struct bt_mesh_subnet *bt_mesh_subnet_find(const u8_t net_id[8], u8_t flags, + u32_t iv_index, const u8_t auth[8], + bool *new_key) +{ + size_t subnet_size = 0U; + int i; + + subnet_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < subnet_size; i++) { + struct bt_mesh_subnet *sub = bt_mesh_rx_netkey_get(i); + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (auth_match(&sub->keys[0], net_id, flags, iv_index, auth)) { + *new_key = false; + return sub; + } + + if (sub->kr_phase == BLE_MESH_KR_NORMAL) { + continue; + } + + if (auth_match(&sub->keys[1], net_id, flags, iv_index, auth)) { + *new_key = true; + return sub; + } + } + + return NULL; +} + +static int net_decrypt(struct bt_mesh_subnet *sub, const u8_t *enc, + const u8_t *priv, const u8_t *data, + size_t data_len, struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + BT_DBG("NID 0x%02x net_idx 0x%04x", NID(data), sub->net_idx); + BT_DBG("IVI %u net->iv_index 0x%08x", IVI(data), bt_mesh.iv_index); + + rx->old_iv = (IVI(data) != (bt_mesh.iv_index & 0x01)); + + net_buf_simple_reset(buf); + memcpy(net_buf_simple_add(buf, data_len), data, data_len); + + if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) { + return -ENOENT; + } + + rx->ctx.addr = SRC(buf->data); + if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr)) { + BT_INFO("Ignoring non-unicast src addr 0x%04x", rx->ctx.addr); + return -EINVAL; + } + + if (rx->net_if == BLE_MESH_NET_IF_ADV && msg_cache_match(rx, buf)) { + BT_DBG("Duplicate found in Network Message Cache"); + return -EALREADY; + } + + BT_DBG("src 0x%04x", rx->ctx.addr); + + if (IS_ENABLED(CONFIG_BLE_MESH_PROXY) && + rx->net_if == BLE_MESH_NET_IF_PROXY_CFG) { + return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), + true); + } + + return bt_mesh_net_decrypt(enc, buf, BLE_MESH_NET_IVI_RX(rx), false); +} + +#if (defined(CONFIG_BLE_MESH_LOW_POWER) || \ + defined(CONFIG_BLE_MESH_FRIEND)) +static int friend_decrypt(struct bt_mesh_subnet *sub, const u8_t *data, + size_t data_len, struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + int i; + + BT_DBG("NID 0x%02x net_idx 0x%04x", NID(data), sub->net_idx); + + for (i = 0; i < ARRAY_SIZE(friend_cred); i++) { + struct friend_cred *cred = &friend_cred[i]; + + if (cred->net_idx != sub->net_idx) { + continue; + } + + if (NID(data) == cred->cred[0].nid && + !net_decrypt(sub, cred->cred[0].enc, cred->cred[0].privacy, + data, data_len, rx, buf)) { + return 0; + } + + if (sub->kr_phase == BLE_MESH_KR_NORMAL) { + continue; + } + + if (NID(data) == cred->cred[1].nid && + !net_decrypt(sub, cred->cred[1].enc, cred->cred[1].privacy, + data, data_len, rx, buf)) { + rx->new_key = 1U; + return 0; + } + } + + return -ENOENT; +} +#endif + +static bool net_find_and_decrypt(const u8_t *data, size_t data_len, + struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_subnet *sub = NULL; + size_t array_size = 0U; + int i; + + BT_DBG("%s", __func__); + + array_size = bt_mesh_rx_netkey_size(); + + for (i = 0; i < array_size; i++) { + sub = bt_mesh_rx_netkey_get(i); + if (!sub) { + BT_DBG("%s, NULL subnet", __func__); + continue; + } + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + +#if (defined(CONFIG_BLE_MESH_LOW_POWER) || defined(CONFIG_BLE_MESH_FRIEND)) + if (!friend_decrypt(sub, data, data_len, rx, buf)) { + rx->friend_cred = 1; + rx->ctx.net_idx = sub->net_idx; + rx->sub = sub; + return true; + } +#endif + + if (NID(data) == sub->keys[0].nid && + !net_decrypt(sub, sub->keys[0].enc, sub->keys[0].privacy, + data, data_len, rx, buf)) { + rx->ctx.net_idx = sub->net_idx; + rx->sub = sub; + return true; + } + + if (sub->kr_phase == BLE_MESH_KR_NORMAL) { + continue; + } + + if (NID(data) == sub->keys[1].nid && + !net_decrypt(sub, sub->keys[1].enc, sub->keys[1].privacy, + data, data_len, rx, buf)) { + rx->new_key = 1U; + rx->ctx.net_idx = sub->net_idx; + rx->sub = sub; + return true; + } + } + + return false; +} + +/* Relaying from advertising to the advertising bearer should only happen + * if the Relay state is set to enabled. Locally originated packets always + * get sent to the advertising bearer. If the packet came in through GATT, + * then we should only relay it if the GATT Proxy state is enabled. + */ +static bool relay_to_adv(enum bt_mesh_net_if net_if) +{ + switch (net_if) { + case BLE_MESH_NET_IF_LOCAL: + return true; + case BLE_MESH_NET_IF_ADV: + return (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED); + case BLE_MESH_NET_IF_PROXY: + return (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED); + default: + return false; + } +} + +static void bt_mesh_net_relay(struct net_buf_simple *sbuf, + struct bt_mesh_net_rx *rx) +{ + const u8_t *enc = NULL, *priv = NULL; + struct net_buf *buf = NULL; + u8_t nid = 0U, transmit = 0U; + + if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { + /* Locally originated PDUs with TTL=1 will only be delivered + * to local elements as per Mesh Profile 1.0 section 3.4.5.2: + * "The output filter of the interface connected to + * advertising or GATT bearers shall drop all messages with + * TTL value set to 1." + */ + if (rx->ctx.recv_ttl == 1U) { + return; + } + } else { + if (rx->ctx.recv_ttl <= 1U) { + return; + } + } + + if (rx->net_if == BLE_MESH_NET_IF_ADV && + bt_mesh_relay_get() != BLE_MESH_RELAY_ENABLED && + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_ENABLED) { + return; + } + + BT_DBG("TTL %u CTL %u dst 0x%04x", rx->ctx.recv_ttl, rx->ctl, + rx->ctx.recv_dst); + + /* The Relay Retransmit state is only applied to adv-adv relaying. + * Anything else (like GATT to adv, or locally originated packets) + * use the Network Transmit state. + */ + if (rx->net_if == BLE_MESH_NET_IF_ADV) { + transmit = bt_mesh_relay_retransmit_get(); + } else { + transmit = bt_mesh_net_transmit_get(); + } + + /** + * When the node tries to relay a Segment ACK message, in this case + * the corresponding segment packets (if exist) can be removed from + * the relay queue. + */ + +#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, transmit, K_NO_WAIT); +#else + /** + * Check if the number of relay packets in queue is too large, if so + * use minimum relay retransmit value for later relay packets. + */ + if (bt_mesh_get_stored_relay_count() >= BLE_MESH_MAX_STORED_RELAY_COUNT) { + transmit = BLE_MESH_TRANSMIT(0, 20); + } + buf = bt_mesh_relay_adv_create(BLE_MESH_ADV_DATA, transmit, K_NO_WAIT); +#endif + + if (!buf) { + BT_ERR("%s, Out of relay buffers", __func__); + return; + } + + /* Only decrement TTL for non-locally originated packets */ + if (rx->net_if != BLE_MESH_NET_IF_LOCAL) { + /* Leave CTL bit intact */ + sbuf->data[1] &= 0x80; + sbuf->data[1] |= rx->ctx.recv_ttl - 1U; + } + + net_buf_add_mem(buf, sbuf->data, sbuf->len); + + enc = rx->sub->keys[rx->sub->kr_flag].enc; + priv = rx->sub->keys[rx->sub->kr_flag].privacy; + nid = rx->sub->keys[rx->sub->kr_flag].nid; + + BT_DBG("Relaying packet. TTL is now %u", TTL(buf->data)); + + /* Update NID if RX or RX was with friend credentials */ + if (rx->friend_cred) { + buf->data[0] &= 0x80; /* Clear everything except IVI */ + buf->data[0] |= nid; + } + + /* We re-encrypt and obfuscate using the received IVI rather than + * the normal TX IVI (which may be different) since the transport + * layer nonce includes the IVI. + */ + if (bt_mesh_net_encrypt(enc, &buf->b, BLE_MESH_NET_IVI_RX(rx), false)) { + BT_ERR("%s, Re-encrypting failed", __func__); + goto done; + } + + if (bt_mesh_net_obfuscate(buf->data, BLE_MESH_NET_IVI_RX(rx), priv)) { + BT_ERR("%s, Re-obfuscating failed", __func__); + goto done; + } + + /* Sending to the GATT bearer should only happen if GATT Proxy + * is enabled or the message originates from the local node. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED || + rx->net_if == BLE_MESH_NET_IF_LOCAL)) { + if (bt_mesh_proxy_relay(&buf->b, rx->ctx.recv_dst) && + BLE_MESH_ADDR_IS_UNICAST(rx->ctx.recv_dst)) { + goto done; + } + } + + if (relay_to_adv(rx->net_if)) { +#if !defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) + bt_mesh_adv_send(buf, NULL, NULL); +#else + bt_mesh_relay_adv_send(buf, NULL, NULL, rx->ctx.addr, rx->ctx.recv_dst); +#endif + } + +done: + net_buf_unref(buf); +} + +void bt_mesh_net_header_parse(struct net_buf_simple *buf, + struct bt_mesh_net_rx *rx) +{ + rx->old_iv = (IVI(buf->data) != (bt_mesh.iv_index & 0x01)); + rx->ctl = CTL(buf->data); + rx->ctx.recv_ttl = TTL(buf->data); + rx->seq = SEQ(buf->data); + rx->ctx.addr = SRC(buf->data); + rx->ctx.recv_dst = DST(buf->data); +} + +int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, + struct bt_mesh_net_rx *rx, struct net_buf_simple *buf) +{ + if (data->len < BLE_MESH_NET_MIN_PDU_LEN) { + BT_WARN("Dropping too short mesh packet (len %u)", data->len); + BT_WARN("%s", bt_hex(data->data, data->len)); + return -EINVAL; + } + + if (net_if == BLE_MESH_NET_IF_ADV && check_dup(data)) { + return -EINVAL; + } + + BT_DBG("%u bytes: %s", data->len, bt_hex(data->data, data->len)); + + rx->net_if = net_if; + + if (!net_find_and_decrypt(data->data, data->len, rx, buf)) { + BT_DBG("Unable to find matching net for packet"); + return -ENOENT; + } + + /* Initialize AppIdx to a sane value */ + rx->ctx.app_idx = BLE_MESH_KEY_UNUSED; + + rx->ctx.recv_ttl = TTL(buf->data); + + /* Default to responding with TTL 0 for non-routed messages */ + if (rx->ctx.recv_ttl == 0U) { + rx->ctx.send_ttl = 0U; + } else { + rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT; + } + + rx->ctl = CTL(buf->data); + rx->seq = SEQ(buf->data); + rx->ctx.recv_dst = DST(buf->data); + + BT_DBG("Decryption successful. Payload len %u", buf->len); + + if (net_if != BLE_MESH_NET_IF_PROXY_CFG && + rx->ctx.recv_dst == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("%s, Destination address is unassigned; dropping packet", __func__); + return -EBADMSG; + } + + if (BLE_MESH_ADDR_IS_RFU(rx->ctx.recv_dst)) { + BT_ERR("%s, Destination address is RFU; dropping packet", __func__); + return -EBADMSG; + } + + if (net_if != BLE_MESH_NET_IF_LOCAL && bt_mesh_elem_find(rx->ctx.addr)) { + BT_DBG("Dropping locally originated packet"); + return -EBADMSG; + } + + BT_DBG("src 0x%04x dst 0x%04x ttl %u", rx->ctx.addr, rx->ctx.recv_dst, + rx->ctx.recv_ttl); + BT_DBG("PDU: %s", bt_hex(buf->data, buf->len)); + + msg_cache_add(rx); + + return 0; +} + +static bool ready_to_recv(void) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + return true; + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + if (bt_mesh_provisioner_get_node_count()) { + return true; + } + } + + return false; +} + +static bool ignore_net_msg(u16_t src, u16_t dst) +{ + if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) { + /* When fast provisioning is enabled, the node addr + * message will be sent to the Primary Provisioner, + * which shall not be ignored here. + */ + return false; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && + bt_mesh_is_provisioner_en() && + BLE_MESH_ADDR_IS_UNICAST(dst) && + bt_mesh_elem_find(dst)) { + /* If the destination address of the message is the element + * address of Provisioner, but Provisioner fails to find the + * node in its provisioning database, then this message will + * be ignored. + */ + if (!bt_mesh_provisioner_get_node_with_addr(src)) { + BT_INFO("Not found node address 0x%04x", src); + return true; + } + } + return false; +} + +void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, + enum bt_mesh_net_if net_if) +{ + NET_BUF_SIMPLE_DEFINE(buf, 29); + struct bt_mesh_net_rx rx = { .ctx.recv_rssi = rssi }; + struct net_buf_simple_state state = {0}; + + BT_DBG("rssi %d net_if %u", rssi, net_if); + + if (!ready_to_recv()) { + return; + } + + if (bt_mesh_net_decode(data, net_if, &rx, &buf)) { + return; + } + + if (ignore_net_msg(rx.ctx.addr, rx.ctx.recv_dst)) { + return; + } + + /* Save the state so the buffer can later be relayed */ + net_buf_simple_save(&buf, &state); + + rx.local_match = (bt_mesh_fixed_group_match(rx.ctx.recv_dst) || + bt_mesh_elem_find(rx.ctx.recv_dst)); + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + net_if == BLE_MESH_NET_IF_PROXY) { + bt_mesh_proxy_addr_add(data, rx.ctx.addr); + + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_DISABLED && + !rx.local_match) { + BT_INFO("Proxy is disabled; ignoring message"); + return; + } + } + + /* The transport layer has indicated that it has rejected the message, + * but would like to see it again if it is received in the future. + * This can happen if a message is received when the device is in + * Low Power mode, but the message was not encrypted with the friend + * credentials. Remove it from the message cache so that we accept + * it again in the future. + */ + if (bt_mesh_trans_recv(&buf, &rx) == -EAGAIN) { + BT_WARN("Removing rejected message from Network Message Cache"); + msg_cache[rx.msg_cache_idx].src = BLE_MESH_ADDR_UNASSIGNED; + /* Rewind the next index now that we're not using this entry */ + msg_cache_next = rx.msg_cache_idx; + } + + /* Relay if this was a group/virtual address, or if the destination + * was neither a local element nor an LPN we're Friends for. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.recv_dst) || + (!rx.local_match && !rx.friend_match)) { + net_buf_simple_restore(&buf, &state); + bt_mesh_net_relay(&buf, &rx); + } +} + +static void ivu_refresh(struct k_work *work) +{ + bt_mesh.ivu_duration += BLE_MESH_IVU_HOURS; + + BT_INFO("%s for %u hour%s", + bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS) ? + "IVU in Progress" : "IVU Normal mode", + bt_mesh.ivu_duration, bt_mesh.ivu_duration == 1U ? "" : "s"); + + if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_iv(true); + } + + k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); + return; + } + + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { + bt_mesh_beacon_ivu_initiator(true); + bt_mesh_net_iv_update(bt_mesh.iv_index, false); + } else if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_iv(true); + } +} + +void bt_mesh_net_start(void) +{ + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { + bt_mesh_beacon_enable(); + } else { + bt_mesh_beacon_disable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED) { + bt_mesh_proxy_gatt_enable(); + bt_mesh_adv_update(); + } + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Add Mesh beacon type (Secure Network Beacon) to the exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_BEACON, NULL); +#endif + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) { + /* TODO: Enable duplicate scan in Low Power Mode */ + bt_mesh_lpn_init(); + } else { + bt_mesh_scan_enable(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_init(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PROV)) { + u16_t net_idx = bt_mesh.sub[0].net_idx; + u16_t addr = bt_mesh_primary_addr(); + u32_t iv_index = bt_mesh.iv_index; + u8_t flags = (u8_t)bt_mesh.sub[0].kr_flag; + const u8_t *net_key = bt_mesh.sub[0].keys[flags].net; + if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS)) { + flags |= BLE_MESH_NET_FLAG_IVU; + } + + bt_mesh_prov_complete(net_idx, net_key, addr, flags, iv_index); + } +} + +void bt_mesh_net_init(void) +{ + k_delayed_work_init(&bt_mesh.ivu_timer, ivu_refresh); + + k_work_init(&bt_mesh.local_work, bt_mesh_net_local); +} + +void bt_mesh_net_deinit(bool erase) +{ + k_delayed_work_free(&bt_mesh.ivu_timer); + + k_work_init(&bt_mesh.local_work, NULL); + + /* Local queue uses a while loop, currently no need + * to handle this. + */ + +#if FRIEND_CRED_COUNT > 0 + memset(friend_cred, 0, sizeof(friend_cred)); +#endif + + memset(msg_cache, 0, sizeof(msg_cache)); + msg_cache_next = 0U; + + memset(dup_cache, 0, sizeof(dup_cache)); + dup_cache_next = 0U; + + bt_mesh.iv_index = 0U; + bt_mesh.seq = 0U; + + memset(bt_mesh.flags, 0, sizeof(bt_mesh.flags)); + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_seq(); + bt_mesh_clear_iv(); + } +} diff --git a/components/bt/esp_ble_mesh/mesh_core/net.h b/components/bt/esp_ble_mesh/mesh_core/net.h new file mode 100644 index 000000000..2349403b6 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/net.h @@ -0,0 +1,428 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _NET_H_ +#define _NET_H_ + +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_NET_FLAG_KR BIT(0) +#define BLE_MESH_NET_FLAG_IVU BIT(1) + +#define BLE_MESH_KR_NORMAL 0x00 +#define BLE_MESH_KR_PHASE_1 0x01 +#define BLE_MESH_KR_PHASE_2 0x02 +#define BLE_MESH_KR_PHASE_3 0x03 + +#define BLE_MESH_IV_UPDATE(flags) ((flags >> 1) & 0x01) +#define BLE_MESH_KEY_REFRESH(flags) (flags & 0x01) + +/* How many hours in between updating IVU duration */ +#define BLE_MESH_IVU_MIN_HOURS 96 +#define BLE_MESH_IVU_HOURS (BLE_MESH_IVU_MIN_HOURS / \ + CONFIG_BLE_MESH_IVU_DIVIDER) +#define BLE_MESH_IVU_TIMEOUT K_HOURS(BLE_MESH_IVU_HOURS) + +struct bt_mesh_app_key { + u16_t net_idx; + u16_t app_idx; + bool updated; + struct bt_mesh_app_keys { + u8_t id; + u8_t val[16]; + } keys[2]; +}; + +struct bt_mesh_subnet { + u32_t beacon_sent; /* Timestamp of last sent beacon */ + u8_t beacons_last; /* Number of beacons during last + * observation window + */ + u8_t beacons_cur; /* Number of beacons observed during + * currently ongoing window. + */ + + u8_t beacon_cache[21]; /* Cached last authenticated beacon */ + + u16_t net_idx; /* NetKeyIndex */ + + bool kr_flag; /* Key Refresh Flag */ + u8_t kr_phase; /* Key Refresh Phase */ + + u8_t node_id; /* Node Identity State */ + u32_t node_id_start; /* Node Identity started timestamp */ + + u8_t auth[8]; /* Beacon Authentication Value */ + + struct bt_mesh_subnet_keys { + u8_t net[16]; /* NetKey */ + u8_t nid; /* NID */ + u8_t enc[16]; /* EncKey */ + u8_t net_id[8]; /* Network ID */ +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + u8_t identity[16]; /* IdentityKey */ +#endif + u8_t privacy[16]; /* PrivacyKey */ + u8_t beacon[16]; /* BeaconKey */ + } keys[2]; +}; + +struct bt_mesh_rpl { + u16_t src; + bool old_iv; +#if defined(CONFIG_BLE_MESH_SETTINGS) + bool store; +#endif + u32_t seq; +}; + +#if defined(CONFIG_BLE_MESH_FRIEND) +#define FRIEND_SEG_RX CONFIG_BLE_MESH_FRIEND_SEG_RX +#define FRIEND_SUB_LIST_SIZE CONFIG_BLE_MESH_FRIEND_SUB_LIST_SIZE +#else +#define FRIEND_SEG_RX 0 +#define FRIEND_SUB_LIST_SIZE 0 +#endif + +struct bt_mesh_friend { + u16_t lpn; + u8_t recv_delay; + u8_t fsn: 1, + send_last: 1, + pending_req: 1, + sec_update: 1, + pending_buf: 1, + valid: 1, + established: 1; + s32_t poll_to; + u8_t num_elem; + u16_t lpn_counter; + u16_t counter; + + u16_t net_idx; + + u16_t sub_list[FRIEND_SUB_LIST_SIZE]; + + struct k_delayed_work timer; + + struct bt_mesh_friend_seg { + sys_slist_t queue; + + /* The target number of segments, i.e. not necessarily + * the current number of segments, in the queue. This is + * used for Friend Queue free space calculations. + */ + u8_t seg_count; + } seg[FRIEND_SEG_RX]; + + struct net_buf *last; + + sys_slist_t queue; + u32_t queue_size; + + /* Friend Clear Procedure */ + struct { + u32_t start; /* Clear Procedure start */ + u16_t frnd; /* Previous Friend's address */ + u16_t repeat_sec; /* Repeat timeout in seconds */ + struct k_delayed_work timer; /* Repeat timer */ + } clear; +}; + +#if defined(CONFIG_BLE_MESH_LOW_POWER) +#define LPN_GROUPS CONFIG_BLE_MESH_LPN_GROUPS +#else +#define LPN_GROUPS 0 +#endif + +/* Low Power Node state */ +struct bt_mesh_lpn { + enum __packed { + BLE_MESH_LPN_DISABLED, /* LPN feature is disabled */ + BLE_MESH_LPN_CLEAR, /* Clear in progress */ + BLE_MESH_LPN_TIMER, /* Waiting for auto timer expiry */ + BLE_MESH_LPN_ENABLED, /* LPN enabled, but no Friend */ + BLE_MESH_LPN_REQ_WAIT, /* Wait before scanning for offers */ + BLE_MESH_LPN_WAIT_OFFER, /* Friend Req sent */ + BLE_MESH_LPN_ESTABLISHED, /* Friendship established */ + BLE_MESH_LPN_RECV_DELAY, /* Poll sent, waiting ReceiveDelay */ + BLE_MESH_LPN_WAIT_UPDATE, /* Waiting for Update or message */ + BLE_MESH_LPN_OFFER_RECV, /* Friend offer received */ + } state; + + /* Transaction Number (used for subscription list) */ + u8_t xact_next; + u8_t xact_pending; + u8_t sent_req; + + /* Address of our Friend when we're a LPN. Unassigned if we don't + * have a friend yet. + */ + u16_t frnd; + + /* Value from the friend offer */ + u8_t recv_win; + + u8_t req_attempts; /* Number of Request attempts */ + + s32_t poll_timeout; + + u8_t groups_changed: 1, /* Friend Subscription List needs updating */ + pending_poll: 1, /* Poll to be sent after subscription */ + disable: 1, /* Disable LPN after clearing */ + fsn: 1, /* Friend Sequence Number */ + established: 1, /* Friendship established */ + clear_success: 1; /* Friend Clear Confirm received */ + + /* Friend Queue Size */ + u8_t queue_size; + + /* LPNCounter */ + u16_t counter; + + /* Previous Friend of this LPN */ + u16_t old_friend; + + /* Duration reported for last advertising packet */ + u16_t adv_duration; + + /* Next LPN related action timer */ + struct k_delayed_work timer; + + /* Subscribed groups */ + u16_t groups[LPN_GROUPS]; + + /* Bit fields for tracking which groups the Friend knows about */ + BLE_MESH_ATOMIC_DEFINE(added, LPN_GROUPS); + BLE_MESH_ATOMIC_DEFINE(pending, LPN_GROUPS); + BLE_MESH_ATOMIC_DEFINE(to_remove, LPN_GROUPS); +}; + +/* bt_mesh_net.flags */ +enum { + BLE_MESH_NODE, /* Device is a node */ + BLE_MESH_PROVISIONER, /* Device is a Provisioner */ + BLE_MESH_VALID, /* We have been provisioned */ + BLE_MESH_VALID_PROV, /* Provisioner has been enabled */ + BLE_MESH_SUSPENDED, /* Network is temporarily suspended */ + BLE_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */ + BLE_MESH_IVU_INITIATOR, /* IV Update initiated by us */ + BLE_MESH_IVU_TEST, /* IV Update test mode */ + BLE_MESH_IVU_PENDING, /* Update blocked by SDU in progress */ + + /* pending storage actions, must reside within first 32 flags */ + BLE_MESH_RPL_PENDING, + BLE_MESH_KEYS_PENDING, + BLE_MESH_NET_PENDING, + BLE_MESH_IV_PENDING, + BLE_MESH_SEQ_PENDING, + BLE_MESH_HB_PUB_PENDING, + BLE_MESH_CFG_PENDING, + BLE_MESH_MOD_PENDING, + BLE_MESH_VA_PENDING, + + /* Don't touch - intentionally last */ + BLE_MESH_FLAG_COUNT, +}; + +struct bt_mesh_net { + u32_t iv_index; /* Current IV Index */ + u32_t seq; /* Next outgoing sequence number (24 bits) */ + + BLE_MESH_ATOMIC_DEFINE(flags, BLE_MESH_FLAG_COUNT); + + /* Local network interface */ + struct k_work local_work; + sys_slist_t local_queue; + +#if defined(CONFIG_BLE_MESH_FRIEND) + /* Friend state, unique for each LPN that we're Friends for */ + struct bt_mesh_friend frnd[CONFIG_BLE_MESH_FRIEND_LPN_COUNT]; +#endif + +#if defined(CONFIG_BLE_MESH_LOW_POWER) + struct bt_mesh_lpn lpn; /* Low Power Node state */ +#endif + + /* Number of hours in current IV Update state */ + u8_t ivu_duration; + + /* Timer to track duration in current IV Update state */ + struct k_delayed_work ivu_timer; + + u8_t dev_key[16]; + + struct bt_mesh_app_key app_keys[CONFIG_BLE_MESH_APP_KEY_COUNT]; + + struct bt_mesh_subnet sub[CONFIG_BLE_MESH_SUBNET_COUNT]; + + struct bt_mesh_rpl rpl[CONFIG_BLE_MESH_CRPL]; + +#if defined(CONFIG_BLE_MESH_PROVISIONER) + /* Application keys stored by provisioner */ + struct bt_mesh_app_key *p_app_keys[CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT]; + /* Next app_idx can be assigned */ + u16_t p_app_idx_next; + + /* Network keys stored by provisioner */ + struct bt_mesh_subnet *p_sub[CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT]; + /* Next net_idx can be assigned */ + u16_t p_net_idx_next; +#endif +}; + +/* Network interface */ +enum bt_mesh_net_if { + BLE_MESH_NET_IF_ADV, + BLE_MESH_NET_IF_LOCAL, + BLE_MESH_NET_IF_PROXY, + BLE_MESH_NET_IF_PROXY_CFG, +}; + +/* Decoding context for Network/Transport data */ +struct bt_mesh_net_rx { + struct bt_mesh_subnet *sub; + struct bt_mesh_msg_ctx ctx; + u32_t seq; /* Sequence Number */ + u8_t old_iv: 1, /* iv_index - 1 was used */ + new_key: 1, /* Data was encrypted with updated key */ + friend_cred: 1, /* Data was encrypted with friend cred */ + ctl: 1, /* Network Control */ + net_if: 2, /* Network interface */ + local_match: 1, /* Matched a local element */ + friend_match: 1; /* Matched an LPN we're friends for */ + u16_t msg_cache_idx; /* Index of entry in message cache */ +}; + +/* Encoding context for Network/Transport data */ +struct bt_mesh_net_tx { + struct bt_mesh_subnet *sub; + struct bt_mesh_msg_ctx *ctx; + u16_t src; + u8_t xmit; + u8_t friend_cred: 1, + aszmic: 1, + aid: 6; +}; + +extern struct bt_mesh_net bt_mesh; + +#define BLE_MESH_NET_IVI_TX (bt_mesh.iv_index - \ + bt_mesh_atomic_test_bit(bt_mesh.flags, \ + BLE_MESH_IVU_IN_PROGRESS)) +#define BLE_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv) + +#define BLE_MESH_NET_HDR_LEN 9 + +void bt_mesh_msg_cache_clear(u16_t unicast_addr, u8_t elem_num); + +int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys, + const u8_t key[16]); + +int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16], + u32_t iv_index); + +u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub); + +bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key); + +void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub); + +int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub); + +void bt_mesh_rpl_reset(void); + +bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update); + +void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub); + +struct bt_mesh_subnet *bt_mesh_subnet_get(u16_t net_idx); + +struct bt_mesh_subnet *bt_mesh_subnet_find(const u8_t net_id[8], u8_t flags, + u32_t iv_index, const u8_t auth[8], + bool *new_key); + +int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf, + bool proxy); + +int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf, + const struct bt_mesh_send_cb *cb, void *cb_data); + +int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf, + bool new_key, const struct bt_mesh_send_cb *cb, + void *cb_data); + +int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if, + struct bt_mesh_net_rx *rx, struct net_buf_simple *buf); + +void bt_mesh_net_recv(struct net_buf_simple *data, s8_t rssi, + enum bt_mesh_net_if net_if); + +bool bt_mesh_primary_subnet_exist(void); + +u32_t bt_mesh_next_seq(void); + +void bt_mesh_net_start(void); + +void bt_mesh_net_init(void); +void bt_mesh_net_deinit(bool erase); + +void bt_mesh_net_header_parse(struct net_buf_simple *buf, + struct bt_mesh_net_rx *rx); + +/* Friendship Credential Management */ +struct friend_cred { + u16_t net_idx; + u16_t addr; + + u16_t lpn_counter; + u16_t frnd_counter; + + struct { + u8_t nid; /* NID */ + u8_t enc[16]; /* EncKey */ + u8_t privacy[16]; /* PrivacyKey */ + } cred[2]; +}; + +int friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid, + const u8_t **enc, const u8_t **priv); +int friend_cred_set(struct friend_cred *cred, u8_t idx, const u8_t net_key[16]); +void friend_cred_refresh(u16_t net_idx); +int friend_cred_update(struct bt_mesh_subnet *sub); +struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr, + u16_t lpn_counter, u16_t frnd_counter); +void friend_cred_clear(struct friend_cred *cred); +int friend_cred_del(u16_t net_idx, u16_t addr); + +static inline void send_cb_finalize(const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + if (!cb) { + return; + } + + if (cb->start) { + cb->start(0, 0, cb_data); + } + + if (cb->end) { + cb->end(0, cb_data); + } +} + +#ifdef __cplusplus +} +#endif + +#endif /* _NET_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c new file mode 100644 index 000000000..037296f6c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c @@ -0,0 +1,1992 @@ +/* + * Copyright (c) 2017 Nordic Semiconductor ASA + * Copyright (c) 2015-2016 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc/btc_task.h" +#include "osi/alarm.h" + +#include "mbedtls/aes.h" +#include "mbedtls/ecp.h" + +#include "host/ble_hs.h" +#include "host/ble_uuid.h" +#include "host/ble_att.h" +#include "host/ble_gatt.h" +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" + +#include "mesh_hci.h" +#include "mesh_common.h" +#include "mesh_aes_encrypt.h" +#include "provisioner_prov.h" + +/** @def BT_UUID_MESH_PROV + * @brief Mesh Provisioning Service + */ +#define BT_UUID_MESH_PROV_VAL 0x1827 +/** @def BT_UUID_MESH_PROXY + * @brief Mesh Proxy Service + */ +#define BT_UUID_MESH_PROXY_VAL 0x1828 +/** @def BT_UUID_GATT_CCC + * @brief GATT Client Characteristic Configuration + */ +#define BT_UUID_GATT_CCC_VAL 0x2902 +/** @def BT_UUID_MESH_PROV_DATA_IN + * @brief Mesh Provisioning Data In + */ +#define BT_UUID_MESH_PROV_DATA_IN_VAL 0x2adb +/** @def BT_UUID_MESH_PROV_DATA_OUT + * @brief Mesh Provisioning Data Out + */ +#define BT_UUID_MESH_PROV_DATA_OUT_VAL 0x2adc +/** @def BT_UUID_MESH_PROXY_DATA_IN + * @brief Mesh Proxy Data In + */ +#define BT_UUID_MESH_PROXY_DATA_IN_VAL 0x2add +/** @def BT_UUID_MESH_PROXY_DATA_OUT + * @brief Mesh Proxy Data Out + */ +#define BT_UUID_MESH_PROXY_DATA_OUT_VAL 0x2ade + +#define BLE_MESH_GATT_GET_CONN_ID(conn_id) ((u16_t)(conn_id)) +#define BLE_MESH_GATT_CREATE_CONN_ID(conn_id) ((u16_t)(conn_id)) + +static uint16_t proxy_svc_start_handle, prov_svc_start_handle; +struct bt_mesh_dev bt_mesh_dev; + +/* P-256 Variables */ +static u8_t bt_mesh_public_key[64]; +static u8_t bt_mesh_private_key[32]; + +/* Scan related functions */ +static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb; + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +/* the gatt database list to save the attribute table */ +static sys_slist_t bt_mesh_gatts_db; + +/* Static Variables */ +static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN]; +static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb; + +static u8_t bt_mesh_gatts_addr[6]; + +#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ + +int bt_mesh_host_init(void) +{ + static bool init = false; + int rc; + + if (init == true) { + return 0; + } + + rc = btc_init(); + if (rc != 0) { + return -1; + } + + rc = osi_alarm_create_mux(); + if (rc != 0) { + return -1; + } + + osi_alarm_init(); + init = true; + return 0; +} + +uint8_t ble_hs_hci_get_hci_version(void); + +void bt_mesh_hci_init(void) +{ + /** + * Currently 20ms non-connectable adv interval is supported, and we need to add + * a flag to indicate this support. + */ +#ifdef CONFIG_BLE_MESH_HCI_5_0 + bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0; +#else + bt_mesh_dev.hci_version = ble_hs_hci_get_hci_version(); +#endif + return; +} + +static struct ble_gap_disc_params scan_param; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +static struct gattc_prov_info { + /* Service to be found depends on the type of adv pkt received */ + struct bt_mesh_conn conn; + bt_mesh_addr_t addr; + u16_t service_uuid; + u16_t mtu; + bool wr_desc_done; /* Indicate if write char descriptor event is received */ + u16_t start_handle; /* Service attribute start handle */ + u16_t end_handle; /* Service attribute end handle */ + u16_t data_in_handle; /* Data In Characteristic attribute handle */ + u16_t data_out_handle; /* Data Out Characteristic attribute handle */ + u16_t ccc_handle; /* Data Out Characteristic CCC attribute handle */ +} bt_mesh_gattc_info[BLE_MESH_MAX_CONN]; + +static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb; + +static int ble_on_subscribe(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) +{ + struct bt_mesh_conn *conn = NULL; + uint8_t value[2] = {0x01, 0x00}; + int i = (int)arg, j, len; + MODLOG_DFLT(INFO, "Subscribe complete; status=%d conn_handle=%d " + "attr_handle=%d\n", + error->status, conn_handle, attr->handle); + + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + + conn = &bt_mesh_gattc_info[i].conn; + + if (bt_mesh_gattc_info[i].ccc_handle != attr->handle) { + BT_WARN("%s, gattc ccc_handle is not matched", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->prov_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, prov_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_write_descr != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_write_descr(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn); + if (len < 0) { + BT_ERR("%s, proxy_write_descr failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + bt_mesh_gattc_info[i].wr_desc_done = true; + } + } + + + return 0; + } + + ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j); + return 0; +} + +static int dsc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, + void *arg) +{ + int rc = 0, j, i = (int)arg; /* char index */ + uint8_t value[2] = {0x01, 0x00}; + + switch (error->status) { + case 0: + bt_mesh_gattc_info[i].ccc_handle = dsc->handle; + break; + + case BLE_HS_EDONE: + /* All descriptors in this characteristic discovered; start discovering + * descriptors in the next characteristic. + */ + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + /* Register Notification for Mesh Provisioning/Proxy Data Out Characteristic */ + for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].ccc_handle) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + return 0; + } + ble_gattc_write_flat(conn_handle, bt_mesh_gattc_info[i].ccc_handle, value, sizeof(value), ble_on_subscribe, (void *)j); + } else { + ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j); + } + rc = 0; + break; + + default: + /* Error; abort discovery. */ + rc = error->status; + break; + } + + return rc; +} + + +static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) +{ + int rc = 0, j; + uint16_t uuid16 = 0; + int i = (int)arg; /* service index */ + struct bt_mesh_conn *conn = &bt_mesh_gattc_info[i].conn; + const ble_uuid_any_t *uuid = &chr->uuid; + if (chr) { + uuid16 = (uint16_t) BLE_UUID16(uuid)->value; + } + switch (error->status) { + case 0: + /* Get Mesh Provisioning/Proxy Data In/Out Characteristic */ + if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL)) { + if (!(chr->properties & BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP)) { + bt_mesh_gattc_disconnect(conn); + BT_ERR("Write without response is not set for Data In characteristic"); + return 0; + } + bt_mesh_gattc_info[i].data_in_handle = chr->val_handle; + } else if ((uuid16 == BLE_MESH_UUID_MESH_PROV_DATA_OUT_VAL) || (uuid16 == BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL)) { + if (!(chr->properties & BLE_MESH_GATT_CHRC_NOTIFY)) { + bt_mesh_gattc_disconnect(conn); + BT_ERR("Notify is not set for Data Out characteristic"); + return 0; + } + bt_mesh_gattc_info[i].data_out_handle = chr->val_handle; + } + break; + case BLE_HS_EDONE: + /* All characteristics in this service discovered; start discovering + * characteristics in the next service. + */ + for (j = i + 1; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && (bt_mesh_gattc_info[j].start_handle > bt_mesh_gattc_info[j].end_handle)) { + break; + } + } + if (j == ARRAY_SIZE(bt_mesh_gattc_info)) { + for (j = 0; j < ARRAY_SIZE(bt_mesh_gattc_info); j++) { + if ((bt_mesh_gattc_info[j].conn.handle == conn_handle) && bt_mesh_gattc_info[j].data_out_handle) { + break; + } + } + ble_gattc_disc_all_dscs(conn_handle, bt_mesh_gattc_info[j].data_out_handle, 0xffff, dsc_disced, (void *)j); + } else { + ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[j].start_handle, bt_mesh_gattc_info[j].end_handle, + chr_disced, (void *)j); + } + break; + + default: + rc = error->status; + break; + } + + return rc; +} + + +static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) +{ + struct bt_mesh_conn *conn = NULL; + int rc = 0, i; + const ble_uuid_any_t *uuid; + uint8_t uuid_length; + switch (error->status) { + case 0: + if (!service) { + return 0; + } + uuid = &service->uuid; + uuid_length = (uint8_t) (uuid->u.type == BLE_UUID_TYPE_16 ? 2 : 16); + if (uuid_length != 2) { + return 0; + } + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].service_uuid == (uint16_t)BLE_UUID16(uuid)->value) { + bt_mesh_gattc_info[i].start_handle = service->start_handle; + bt_mesh_gattc_info[i].end_handle = service->end_handle; + break; + } + } + + break; + case BLE_HS_EDONE: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn handle is not found", __func__); + return 0; + } + conn = &bt_mesh_gattc_info[i].conn; + if (bt_mesh_gattc_info[i].start_handle == 0x00 || + bt_mesh_gattc_info[i].end_handle == 0x00 || + (bt_mesh_gattc_info[i].start_handle > bt_mesh_gattc_info[i].end_handle)) { + bt_mesh_gattc_disconnect(conn); + return 0; + } + + /* Get the characteristic num within Mesh Provisioning/Proxy Service */ + ble_gattc_disc_all_chrs(conn_handle, bt_mesh_gattc_info[i].start_handle, bt_mesh_gattc_info[i].end_handle, + chr_disced, (void *)i); + break; + + default: + rc = error->status; + break; + } + + return rc; +} + + +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +static int disc_cb(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_disc_desc *desc; + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + int rc, i; + uint8_t notif_data[100]; + uint16_t notif_len; + ssize_t len; + struct ble_gap_conn_desc conn_desc; + struct bt_mesh_conn *conn = NULL; +#endif + + switch (event->type) { + case BLE_GAP_EVENT_DISC: + desc = &event->disc; + + struct net_buf_simple *buf = bt_mesh_alloc_buf(desc->length_data); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + return 0; + } + net_buf_simple_add_mem(buf, desc->data, desc->length_data); + + if (bt_mesh_scan_dev_found_cb) { + bt_mesh_scan_dev_found_cb((bt_mesh_addr_t *)&desc->addr, desc->rssi, desc->event_type, buf); + } + bt_mesh_free(buf); + break; +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_GAP_EVENT_CONNECT: + if (event->connect.status == 0) { + /* Connection successfully established. */ + MODLOG_DFLT(INFO, "Connection established "); + + rc = ble_gap_conn_find(event->connect.conn_handle, &conn_desc); + assert(rc == 0); + + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->connected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN)) { + bt_mesh_gattc_info[i].conn.handle = event->connect.conn_handle; + (bt_mesh_gattc_conn_cb->connected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, i); + break; + } + } + } + } +#if BLE_MESH_DEV + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + if (rc != 0) { + BT_ERR("%s, Invalid status %d", __func__, rc); + break; + } + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + } +#else + rc = ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + if (rc != 0) { + BT_ERR("%s, Invalid status %d", __func__, rc); + break; + } +#endif /* BLE_MESH_DEV */ + break; + case BLE_GAP_EVENT_DISCONNECT: + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->disconnected != NULL) { + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + memcpy(&conn_desc, &event->disconnect.conn, sizeof(conn_desc)); + if (!memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_ota_addr.val, BLE_MESH_ADDR_LEN)) { + if (bt_mesh_gattc_info[i].conn.handle == event->disconnect.conn.conn_handle) { + (bt_mesh_gattc_conn_cb->disconnected)(&bt_mesh_gattc_info[i].addr, &bt_mesh_gattc_info[i].conn, event->disconnect.reason); + if (!bt_mesh_gattc_info[i].wr_desc_done) { + /* Add this in case connection is established, connected event comes, but + * connection is terminated before server->filter_type is set to PROV. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } + } else { + /* Add this in case connection is failed to be established, and here we + * need to clear some provision link info, like connecting flag, device + * uuid, address info, etc. + */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + bt_mesh_provisioner_clear_link_info(bt_mesh_gattc_info[i].addr.val); + } +#endif + } +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + /* Decrease prov pbg_count */ + bt_mesh_provisioner_pbg_count_dec(); + } +#endif + /* Reset corresponding gattc info */ + memset(&bt_mesh_gattc_info[i], 0, sizeof(bt_mesh_gattc_info[i])); + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT; + bt_mesh_gattc_info[i].wr_desc_done = false; + break; + } + } + } + break; + case BLE_GAP_EVENT_MTU: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == event->mtu.conn_handle) { + bt_mesh_gattc_info[i].mtu = event->mtu.value; + break; + } + } + /** Once mtu exchanged accomplished, start to find services, and here + * need a flag to indicate which service to find(Mesh Prov Service or + * Mesh Proxy Service) + */ + ble_uuid_any_t bt_uuid; + if (i != ARRAY_SIZE(bt_mesh_gattc_info)) { + //service_uuid.len = sizeof(bt_mesh_gattc_info[i].service_uuid); + if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x02) { + bt_uuid.u16.u.type = BLE_UUID_TYPE_16; + bt_uuid.u16.value = bt_mesh_gattc_info[i].service_uuid; + + } else if (sizeof(bt_mesh_gattc_info[i].service_uuid) == 0x10) { + bt_uuid.u128.u.type = BLE_UUID_TYPE_128; + memcpy(bt_uuid.u128.value, &bt_mesh_gattc_info[i].service_uuid, 16); + } + /* Search Mesh Provisioning Service or Mesh Proxy Service */ + ble_gattc_disc_all_svcs(bt_mesh_gattc_info[i].conn.handle, svc_disced, NULL); + } + break; + case BLE_GAP_EVENT_NOTIFY_RX: + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == event->notify_rx.conn_handle) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn handle is not found", __func__); + return 0; + } + + conn = &bt_mesh_gattc_info[i].conn; + ble_gap_conn_find(event->notify_rx.conn_handle, &conn_desc); + + if (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) { + /* Data isn't populated yet */ + return 0; + } + + if (memcmp(bt_mesh_gattc_info[i].addr.val, conn_desc.peer_id_addr.val, BLE_MESH_ADDR_LEN) || + (bt_mesh_gattc_info[i].data_out_handle != event->notify_rx.attr_handle) || + (event->notify_rx.indication != 0)) { + BT_ERR("%s, Notification error", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + + notif_len = OS_MBUF_PKTLEN(event->notify_rx.om); + rc = os_mbuf_copydata(event->notify_rx.om, 0, notif_len, notif_data); + + if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROV_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->prov_notify != NULL) { + len = bt_mesh_gattc_conn_cb->prov_notify(&bt_mesh_gattc_info[i].conn, + notif_data, notif_len); + if (len < 0) { + BT_ERR("%s, prov_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + } + } else if (bt_mesh_gattc_info[i].service_uuid == BLE_MESH_UUID_MESH_PROXY_VAL) { + if (bt_mesh_gattc_conn_cb != NULL && bt_mesh_gattc_conn_cb->proxy_notify != NULL) { + len = bt_mesh_gattc_conn_cb->proxy_notify(&bt_mesh_gattc_info[i].conn, + notif_data, notif_len); + if (len < 0) { + BT_ERR("%s, proxy_notify failed", __func__); + bt_mesh_gattc_disconnect(conn); + return 0; + } + } + } + break; +#endif + default: + break; + } + + return 0; +} + +static int start_le_scan(u8_t scan_type, u16_t interval, u16_t window, u8_t filter_dup) +{ + + scan_param.filter_duplicates = filter_dup; + scan_param.itvl = interval; + scan_param.window = window; + + if (scan_type == BLE_MESH_SCAN_PASSIVE) { + scan_param.passive = 1; + } else { + scan_param.passive = 0; + } + ble_gap_disc(BLE_OWN_ADDR_PUBLIC, BLE_HS_FOREVER, &scan_param, disc_cb, NULL); + +#if BLE_MESH_DEV + if (scan_type == BLE_MESH_SCAN_ACTIVE) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN); + } +#endif + + return 0; +} + +static int set_ad(const struct bt_mesh_adv_data *ad, size_t ad_len, u8_t *buf, u8_t *buf_len) +{ + int i; + + for (i = 0; i < ad_len; i++) { + buf[(*buf_len)++] = ad[i].data_len + 1; + buf[(*buf_len)++] = ad[i].type; + + memcpy(&buf[*buf_len], ad[i].data, + ad[i].data_len); + *buf_len += ad[i].data_len; + } + + return 0; +} + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle); + +static int gap_event_cb(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int rc; + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + /* A new connection was established or a connection attempt failed. */ + MODLOG_DFLT(INFO, "connection %s; status=%d ", + event->connect.status == 0 ? "established" : "failed", + event->connect.status); + if (event->connect.status == 0) { + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + } + MODLOG_DFLT(INFO, "\n"); +#if BLE_MESH_DEV + /* When connection is created, advertising will be stopped automatically. */ + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->connected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->connect.conn_handle); + (bt_mesh_gatts_conn_cb->connected)(&bt_mesh_gatts_conn[index], 0); + } + memcpy(bt_mesh_gatts_addr, desc.peer_id_addr.val, BLE_MESH_ADDR_LEN); + /* This is for EspBleMesh Android app. When it tries to connect with the + * device at the first time and it fails due to some reason. And after + * the second connection, the device needs to send GATT service change + * indication to the phone manually to notify it discovering service again. + */ + ble_svc_gatt_changed(prov_svc_start_handle, 0xffff); + + } + + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason); + MODLOG_DFLT(INFO, "\n"); +#if BLE_MESH_DEV + bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->disconnected != NULL) { + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle); + if (index < BLE_MESH_MAX_CONN) { + bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(event->disconnect.conn.conn_handle); + (bt_mesh_gatts_conn_cb->disconnected)(&bt_mesh_gatts_conn[index], event->disconnect.reason); + } + memset(bt_mesh_gatts_addr, 0x0, BLE_MESH_ADDR_LEN); + } + + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE: + /* The central has updated the connection parameters. */ + MODLOG_DFLT(INFO, "connection updated; status=%d ", + event->conn_update.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + MODLOG_DFLT(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_ADV_COMPLETE: + MODLOG_DFLT(INFO, "advertise complete; reason=%d", + event->adv_complete.reason); + return 0; + + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + MODLOG_DFLT(INFO, "encryption change event; status=%d ", + event->enc_change.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + MODLOG_DFLT(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + MODLOG_DFLT(INFO, "subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=%d\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate, + event->subscribe.cur_indicate); + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(event->subscribe.attr_handle + 1); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(event->subscribe.conn_handle); + u16_t len = 0; + uint16_t ccc_val = 0; + + if (event->subscribe.prev_notify != event->subscribe.cur_notify) { + ccc_val = event->subscribe.cur_notify; + } else if (event->subscribe.prev_indicate != event->subscribe.cur_indicate) { + if (event->subscribe.cur_indicate) { + ccc_val = 2; + } else { + ccc_val = 0; + } + } + + if (attr != NULL && attr->write != NULL) { + if ((len = attr->write(&bt_mesh_gatts_conn[index], attr, + &ccc_val, + sizeof(ccc_val), + 0 /* offset */, 0)) > 0) { + } + } + + return 0; + + case BLE_GAP_EVENT_MTU: + MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + + case BLE_GAP_EVENT_REPEAT_PAIRING: + /* We already have a bond with the peer, but it is attempting to + * establish a new secure link. This app sacrifices security for + * convenience: just throw away the old bond and accept the new link. + */ + + /* Delete the old bond. */ + rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + assert(rc == 0); + ble_store_util_delete_peer(&desc.peer_id_addr); + + /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should + * continue with the pairing operation. + */ + return BLE_GAP_REPEAT_PAIRING_RETRY; + + case BLE_GAP_EVENT_PASSKEY_ACTION: + MODLOG_DFLT(INFO, "PASSKEY_ACTION_EVENT started \n"); + return 0; + } + + return 0; +} +#else + +static int gap_event_cb(struct ble_gap_event *event, void *arg) +{ + return 0; +} +#endif + +/* APIs functions */ +int bt_le_adv_start(const struct bt_mesh_adv_param *param, + const struct bt_mesh_adv_data *ad, size_t ad_len, + const struct bt_mesh_adv_data *sd, size_t sd_len) +{ +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return -EALREADY; + } +#endif + uint8_t buf[BLE_HS_ADV_MAX_SZ]; + uint8_t buf_len = 0; + int err; + struct ble_gap_adv_params adv_params; + + err = set_ad(ad, ad_len, buf, &buf_len); + if (err) { + BT_ERR("set_ad failed: err %d", err); + return err; + } + + err = ble_gap_adv_set_data(buf, buf_len); + if (err != 0) { + BT_ERR("Advertising set failed: err %d", err); + return err; + } + + if (sd && (param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) { + buf_len = 0; + + err = set_ad(sd, sd_len, buf, &buf_len); + if (err) { + BT_ERR("set_ad failed: err %d", err); + return err; + } + + err = ble_gap_adv_rsp_set_data(buf, buf_len); + if (err != 0) { + BT_ERR("Scan rsp failed: err %d", err); + return err; + } + } + + memset(&adv_params, 0, sizeof adv_params); + adv_params.itvl_min = param->interval_min; + adv_params.itvl_max = param->interval_max; + + if (param->options & BLE_MESH_ADV_OPT_CONNECTABLE) { + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + } else if (sd != NULL) { + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + } else { + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_NON; + } + +again: + err = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, + gap_event_cb, NULL); + if (err) { + if (err == BLE_HS_EALREADY) { + ble_gap_adv_stop(); + goto again; + } + + BT_ERR("Advertising start failed: err %d", err); + return err; + } + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); + + if (!(param->options & BLE_MESH_ADV_OPT_ONE_TIME)) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + } +#endif + + return 0; +} + +#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV +int bt_mesh_ble_adv_start(const struct bt_mesh_ble_adv_param *param, + const struct bt_mesh_ble_adv_data *data) +{ + struct ble_gap_adv_params adv_params = {0}; + ble_addr_t p_dir_bda = {0}; + int err = 0; + + if (data && param->adv_type != BLE_MESH_ADV_DIRECT_IND && + param->adv_type != BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) { + if (data->adv_data_len) { + err = ble_gap_adv_set_data(data->adv_data, data->adv_data_len); + if (err) { + BT_ERR("Failed to set advertising data, err %d", err); + return err; + } + } + if (data->scan_rsp_data_len && param->adv_type != BLE_MESH_ADV_NONCONN_IND) { + err = ble_gap_adv_rsp_set_data(data->scan_rsp_data, data->scan_rsp_data_len); + if (err) { + BT_ERR("Failed to set scan rsp data, err %d", err); + return err; + } + } + } + + switch (param->adv_type) { + case BLE_MESH_ADV_IND: + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + break; + case BLE_MESH_ADV_DIRECT_IND: + adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + break; + case BLE_MESH_ADV_SCAN_IND: + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + break; + case BLE_MESH_ADV_NONCONN_IND: + adv_params.conn_mode = BLE_GAP_CONN_MODE_NON; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + break; + case BLE_MESH_ADV_DIRECT_IND_LOW_DUTY: + adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + break; + } + adv_params.itvl_min = param->interval; + adv_params.itvl_max = param->interval; + adv_params.channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39; + adv_params.filter_policy = BLE_MESH_AP_SCAN_CONN_ALL; + adv_params.high_duty_cycle = (param->adv_type == BLE_MESH_ADV_DIRECT_IND) ? true : false; + + if (param->own_addr_type == BLE_MESH_ADDR_PUBLIC_ID || + param->own_addr_type == BLE_MESH_ADDR_RANDOM_ID || + param->adv_type == BLE_MESH_ADV_DIRECT_IND || + param->adv_type == BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) { + p_dir_bda.type = param->peer_addr_type; + memcpy(p_dir_bda.val, param->peer_addr, BLE_MESH_ADDR_LEN); + } + + err = ble_gap_adv_start(param->own_addr_type, &p_dir_bda, BLE_HS_FOREVER, &adv_params, + gap_event_cb, NULL); + if (err) { + BT_ERR("Failed to start advertising, err %d", err); + return err; + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */ + +int bt_le_adv_stop(void) +{ +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING); + if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) { + return 0; + } +#endif + ble_gap_adv_stop(); + +#if BLE_MESH_DEV + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING); +#endif + + return 0; +} + +int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb) +{ + int err; + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + return -EALREADY; + } +#endif + +#if BLE_MESH_DEV + if (param->filter_dup) { + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } else { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP); + } +#endif + + err = start_le_scan(param->type, param->interval, param->window, param->filter_dup); + if (err) { + return err; + } + +#if BLE_MESH_DEV + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); +#endif + + bt_mesh_scan_dev_found_cb = cb; + return err; +} + +int bt_le_scan_stop(void) +{ +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING); + ble_gap_disc_cancel(); + } +#else + ble_gap_disc_cancel(); +#endif + + bt_mesh_scan_dev_found_cb = NULL; + return 0; +} + +#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST +int bt_le_update_white_list(struct bt_mesh_white_list *wl) +{ + ble_addr_t addr = {0}; + + if (wl == NULL || wl->add_remove == false) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + addr.type = wl->addr_type; + memcpy(addr.val, wl->remote_bda, BLE_MESH_ADDR_LEN); + + return ble_gap_wl_set(&addr, 1); +} +#endif + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE + +void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb) +{ + bt_mesh_gatts_conn_cb = cb; +} + +void bt_mesh_gatts_conn_cb_deregister(void) +{ + bt_mesh_gatts_conn_cb = NULL; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(u16_t handle) +{ + struct bt_mesh_gatt_service *svc = NULL; + struct bt_mesh_gatt_attr *attr = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + attr = &svc->attrs[i]; + /* Check the attrs handle is equal to the handle or not */ + if (attr->handle == handle) { + return attr; + } + } + } + + return NULL; +} + +static void bt_mesh_gatts_foreach_attr(u16_t start_handle, u16_t end_handle, + bt_mesh_gatt_attr_func_t func, void *user_data) +{ + struct bt_mesh_gatt_service *svc = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) { + int i; + + for (i = 0; i < svc->attr_count; i++) { + struct bt_mesh_gatt_attr *attr = &svc->attrs[i]; + + /* Check if attribute handle is within range */ + if (attr->handle < start_handle || + attr->handle > end_handle) { + continue; + } + + if (func(attr, user_data) == BLE_MESH_GATT_ITER_STOP) { + return; + } + } + } +} + +static u8_t find_next(const struct bt_mesh_gatt_attr *attr, void *user_data) +{ + struct bt_mesh_gatt_attr **next = user_data; + + *next = (struct bt_mesh_gatt_attr *)attr; + + return BLE_MESH_GATT_ITER_STOP; +} + +static struct bt_mesh_gatt_attr *bt_mesh_gatts_attr_next(const struct bt_mesh_gatt_attr *attr) +{ + struct bt_mesh_gatt_attr *next = NULL; + + bt_mesh_gatts_foreach_attr(attr->handle + 1, attr->handle + 1, find_next, &next); + + return next; +} + +ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t buf_len, u16_t offset, + const void *value, u16_t value_len) +{ + u16_t len; + + if (offset > value_len) { + return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET); + } + + len = MIN(buf_len, value_len - offset); + + BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset, len); + + memcpy(buf, value + offset, len); + + return len; +} + +struct gatts_incl { + u16_t start_handle; + u16_t end_handle; + u16_t uuid16; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_attr *incl = attr->user_data; + struct bt_mesh_uuid *uuid = incl->user_data; + struct gatts_incl pdu = {0}; + u8_t value_len; + + /* First attr points to the start handle */ + pdu.start_handle = sys_cpu_to_le16(incl->handle); + value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle); + + /* + * Core 4.2, Vol 3, Part G, 3.2, + * The Service UUID shall only be present when the UUID is a 16-bit Bluetooth UUID. + */ + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + value_len += sizeof(pdu.uuid16); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + struct bt_mesh_uuid *uuid = attr->user_data; + + if (uuid->type == BLE_MESH_UUID_TYPE_16) { + u16_t uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val); + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &uuid16, 2); + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, + BLE_MESH_UUID_128(uuid)->val, 16); +} + +struct gatts_chrc { + u8_t properties; + u16_t value_handle; + union { + u16_t uuid16; + u8_t uuid[16]; + }; +} __packed; + +ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, void *buf, + u16_t len, u16_t offset) +{ + struct bt_mesh_gatt_char *chrc = attr->user_data; + const struct bt_mesh_gatt_attr *next = NULL; + struct gatts_chrc pdu = {0}; + u8_t value_len; + + pdu.properties = chrc->properties; + /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534: + * 3.3.2 Characteristic Value Declaration + * The Characteristic Value declaration contains the value of the + * characteristic. It is the first Attribute after the characteristic + * declaration. All characteristic definitions shall have a + * Characteristic Value declaration. + */ + next = bt_mesh_gatts_attr_next(attr); + if (!next) { + BT_WARN("%s, No value for characteristic at 0x%04x", __func__, attr->handle); + pdu.value_handle = 0x0000; + } else { + pdu.value_handle = sys_cpu_to_le16(next->handle); + } + value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle); + + if (chrc->uuid->type == BLE_MESH_UUID_TYPE_16) { + pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(chrc->uuid)->val); + value_len += 2; + } else { + memcpy(pdu.uuid, BLE_MESH_UUID_128(chrc->uuid)->val, 16); + value_len += 16; + } + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len); +} + +static int gatts_register(struct bt_mesh_gatt_service *svc) +{ + struct bt_mesh_gatt_service *last; + u16_t handle; + + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + handle = 0; + goto populate; + } + + last = SYS_SLIST_PEEK_TAIL_CONTAINER(&bt_mesh_gatts_db, last, node); + handle = last->attrs[last->attr_count - 1].handle; + BT_DBG("%s, handle = %d", __func__, handle); + +populate: + sys_slist_append(&bt_mesh_gatts_db, &svc->node); + return 0; +} + +static int gatts_deregister(struct bt_mesh_gatt_service *svc) +{ + if (sys_slist_is_empty(&bt_mesh_gatts_db)) { + return 0; + } + + sys_slist_find_and_remove(&bt_mesh_gatts_db, &svc->node); + return 0; +} + +int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc) +{ + uint16_t offset = 0; + int i; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + offset = proxy_svc_start_handle; + } else if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROV_VAL) { + offset = prov_svc_start_handle; + } + + for (i = 0; i < svc->attr_count; i++) { + svc->attrs[i].handle = offset + i; + } + gatts_register(svc); + return 0; +} + +int bt_mesh_gatts_service_deregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + + gatts_deregister(svc); + + return 0; +} + +int bt_mesh_gatts_disconnect(struct bt_mesh_conn *conn, u8_t reason) +{ + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle); + ble_gap_terminate(conn_id, reason); + return 0; +} + +int bt_mesh_gatts_service_unregister(struct bt_mesh_gatt_service *svc) +{ + assert(svc != NULL); + BT_ERR("%s, Unsupported for NimBLE host", __func__); + return 0; +} + +int bt_mesh_gatts_notify(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + struct os_mbuf *om; + u16_t conn_id = BLE_MESH_GATT_CREATE_CONN_ID(conn->handle); + + om = ble_hs_mbuf_from_flat(data, len); + assert(om); + ble_gattc_notify_custom(conn_id, attr->handle, om); + + return 0; +} + +u16_t bt_mesh_gatt_get_mtu(struct bt_mesh_conn *conn) +{ + return ble_att_preferred_mtu(); +} + +/* APIs added by Espressif */ +int bt_mesh_gatts_service_stop(struct bt_mesh_gatt_service *svc) +{ + int rc; + uint16_t handle; + if (!svc) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); + } else { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL); + } + + rc = ble_gatts_find_svc(uuid, &handle); + assert(rc == 0); + ble_gatts_svc_set_visibility(handle, 0); + + /* FIXME: figure out end handle */ + ble_svc_gatt_changed(handle, 0xffff); + + return 0; +} + +int bt_mesh_gatts_service_start(struct bt_mesh_gatt_service *svc) +{ + int rc; + uint16_t handle; + const ble_uuid_t *uuid; + if (BLE_MESH_UUID_16(svc->attrs[0].user_data)->val == BT_UUID_MESH_PROXY_VAL) { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL); + } else { + uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL); + } + + rc = ble_gatts_find_svc(uuid, &handle); + assert(rc == 0); + ble_gatts_svc_set_visibility(handle, 1); + + /* FIXME: figure out end handle */ + ble_svc_gatt_changed(handle, 0xffff); + + return 0; +} + +int bt_mesh_gatts_set_local_device_name(const char *name) +{ + return ble_svc_gap_device_name_set(name); +} +#endif /* defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE */ + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT +void bt_mesh_gattc_conn_cb_register(struct bt_mesh_prov_conn_cb *cb) +{ + bt_mesh_gattc_conn_cb = cb; +} + +void bt_mesh_gattc_conn_cb_deregister(void) +{ + bt_mesh_gattc_conn_cb = NULL; +} + +u8_t bt_mesh_gattc_get_free_conn_count(void) +{ + u8_t count = 0; + u8_t i; + + for (i = 0U; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == 0xFFFF && + bt_mesh_gattc_info[i].service_uuid == 0x0000) { + ++count; + } + } + + return count; +} + +u16_t bt_mesh_gattc_get_service_uuid(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + return 0; + } + + return bt_mesh_gattc_info[i].service_uuid; +} + +/** For provisioner acting as a GATT client, it may follow the procedures + * listed below. + * 1. Create connection with the unprovisioned device + * 2. Exchange MTU size + * 3. Find Mesh Prov Service in the device's service database + * 4. Find Mesh Prov Data In/Out characteristic within the service + * 5. Get CCC of Mesh Prov Data Out Characteristic + * 6. Set the Notification bit of CCC + */ + +int bt_mesh_gattc_conn_create(const bt_mesh_addr_t *addr, u16_t service_uuid) +{ + u8_t zero[6] = {0}; + int i, rc; + + if (!addr || !memcmp(addr->val, zero, BLE_MESH_ADDR_LEN) || + (addr->type > BLE_ADDR_RANDOM)) { + BT_ERR("%s, Invalid remote address", __func__); + return -EINVAL; + } + + if (service_uuid != BLE_MESH_UUID_MESH_PROV_VAL && + service_uuid != BLE_MESH_UUID_MESH_PROXY_VAL) { + BT_ERR("%s, Invalid service uuid 0x%04x", __func__, service_uuid); + return -EINVAL; + } + + /* Check if already creating connection with the device */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (!memcmp(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN)) { + BT_WARN("%s, Already create connection with %s", + __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + return -EALREADY; + } + } + + /* Find empty element in queue to store device info */ + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if ((bt_mesh_gattc_info[i].conn.handle == 0xFFFF) && + (bt_mesh_gattc_info[i].service_uuid == 0x0000)) { + memcpy(bt_mesh_gattc_info[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + bt_mesh_gattc_info[i].addr.type = addr->type; + /* Service to be found after exchanging mtu size */ + bt_mesh_gattc_info[i].service_uuid = service_uuid; + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_WARN("%s, gattc info is full", __func__); + return -ENOMEM; + } + +#if BLE_MESH_DEV + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) { + rc = ble_gap_disc_cancel(); + if (rc != 0) { + return -1; + } + } +#else + rc = ble_gap_disc_cancel(); + if (rc != 0) { + return -1; + } +#endif /* BLE_MESH_DEV */ + + BT_DBG("%s, create conn with %s", __func__, bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + + /* Min_interval: 250ms + * Max_interval: 250ms + * Slave_latency: 0x0 + * Supervision_timeout: 32 sec + */ + struct ble_gap_conn_params conn_params = {0}; + conn_params.itvl_min = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */ + conn_params.itvl_max = 0xC8; /* (250 * 1000) / 1250 = 200 = 0xC8 */ + conn_params.latency = 0; + conn_params.supervision_timeout = 0xC80; + conn_params.scan_itvl = 0x0020; //0x0010 + conn_params.scan_window = 0x0020; //0x0010 + conn_params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; + conn_params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; + + + ble_addr_t peer_addr; + memcpy(peer_addr.val, addr->val, 6); + peer_addr.type = addr->type; + + rc = ble_gap_connect(BLE_OWN_ADDR_PUBLIC, &peer_addr, BLE_HS_FOREVER, &conn_params, + disc_cb, NULL); + + return i; +} + +static int mtu_cb(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t mtu, void *arg) +{ + int i; + if (error->status == 0) { + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (bt_mesh_gattc_info[i].conn.handle == conn_handle) { + bt_mesh_gattc_info[i].mtu = mtu; + break; + } + } + } + return 0; +} + + + +void bt_mesh_gattc_exchange_mtu(u8_t index) +{ + /** Set local MTU and exchange with GATT server. + * ATT_MTU >= 69 for Mesh GATT Prov Service + * ATT_NTU >= 33 for Mesh GATT Proxy Service + */ + + ble_gattc_exchange_mtu(bt_mesh_gattc_info[index].conn.handle, mtu_cb, NULL); +} + +u16_t bt_mesh_gattc_get_mtu_info(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + return bt_mesh_gattc_info[i].mtu; + } + } + + return 0; +} + +int bt_mesh_gattc_write_no_rsp(struct bt_mesh_conn *conn, const struct bt_mesh_gatt_attr *attr, + const void *data, u16_t len) +{ + u16_t conn_id; + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn is not found", __func__); + /** Here we return 0 for prov_send() return value check in provisioner.c + */ + return 0; + } + + conn_id = BLE_MESH_GATT_CREATE_CONN_ID(bt_mesh_gattc_info[i].conn.handle); + + struct os_mbuf *om; + int rc; + + om = ble_hs_mbuf_from_flat(data, len); + if (om == NULL) { + return -1; + } + + rc = ble_gattc_write_no_rsp(conn_id, bt_mesh_gattc_info[i].data_in_handle, om); + if (rc != 0) { + return -1; + } + + return 0; +} + +void bt_mesh_gattc_disconnect(struct bt_mesh_conn *conn) +{ + /** Disconnect + * Clear proper proxy server information + * Clear proper prov_link information + * Clear proper bt_mesh_gattc_info information + * Here in adapter, we just clear proper bt_mesh_gattc_info, and + * when proxy_disconnected callback comes, the proxy server + * information and prov_link information should be cleared. + */ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + if (conn == &bt_mesh_gattc_info[i].conn) { + break; + } + } + + if (i == ARRAY_SIZE(bt_mesh_gattc_info)) { + BT_ERR("%s, Conn is not found", __func__); + return; + } + ble_gap_terminate(bt_mesh_gattc_info[i].conn.handle, BLE_ERR_REM_USER_CONN_TERM); +} + +/** Mesh Provisioning Service: 0x1827 + * Mesh Provisioning Data In: 0x2ADB + * Mesh Provisioning Data Out: 0x2ADC + * Mesh Proxy Service: 0x1828 + * Mesh Proxy Data In: 0x2ADD + * Mesh PROXY Data Out: 0x2ADE + */ +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +struct bt_mesh_conn *bt_mesh_conn_ref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_inc(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); + + return conn; +} + +void bt_mesh_conn_unref(struct bt_mesh_conn *conn) +{ + bt_mesh_atomic_dec(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, bt_mesh_atomic_get(&conn->ref)); +} + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE +static int proxy_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR || ctxt->op == BLE_GATT_ACCESS_OP_WRITE_DSC) { + struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(attr_handle); + u8_t index = BLE_MESH_GATT_GET_CONN_ID(conn_handle); + u16_t len = 0; + + BT_DBG("%s, write: handle = %d, len = %d, data = %s", __func__, attr_handle, + ctxt->om->om_len, + bt_hex(ctxt->om->om_data, ctxt->om->om_len)); + + if (attr != NULL && attr->write != NULL) { + if ((len = attr->write(&bt_mesh_gatts_conn[index], attr, + ctxt->om->om_data, + ctxt->om->om_len, + 0 /* offset */, 0)) > 0) { + } + } + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR || ctxt->op == BLE_GATT_ACCESS_OP_READ_DSC) { + BT_ERR("%s, Unhandled read request for chr and dsc: opcode - %d", __func__, ctxt->op); + } + return 0; +} + +static int dummy_access_cb(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + /* + * We should never never enter this callback - it's attached to notify-only + * characteristic which are notified directly from mbuf. And we can't pass + * NULL as access_cb because gatts will assert on init... + */ + assert(0); + return 0; +} + +static const struct ble_gatt_svc_def svc_defs [] = { +#ifdef CONFIG_BLE_MESH_GATT_PROXY_SERVER + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL), + .includes = NULL, + .characteristics = (struct ble_gatt_chr_def[]) + { { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_IN_VAL), + .access_cb = proxy_char_access_cb, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_DATA_OUT_VAL), + .access_cb = dummy_access_cb, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + 0, /* No more characteristics in this service. */ + } + }, + }, +#endif +#ifdef CONFIG_BLE_MESH_PB_GATT + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL), + .includes = NULL, + .characteristics = (struct ble_gatt_chr_def[]) + { { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_IN_VAL), + .access_cb = proxy_char_access_cb, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + .uuid = BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_DATA_OUT_VAL), + .access_cb = dummy_access_cb, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + 0, /* No more characteristics in this service. */ + } + }, + }, +#endif + { + 0, /* No more services. */ + }, +}; +#endif + +void gatt_register_cb(struct ble_gatt_register_ctxt *ctxt, + void *arg ) +{ + if (ctxt->op == BLE_GATT_REGISTER_OP_SVC) { + if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROXY_VAL)) == 0) { + proxy_svc_start_handle = ctxt->svc.handle; + } else if (ble_uuid_cmp(ctxt->svc.svc_def->uuid, BLE_UUID16_DECLARE(BT_UUID_MESH_PROV_VAL)) == 0) { + prov_svc_start_handle = ctxt->svc.handle; + } + } +} + +void bt_mesh_gatt_init(void) +{ + ble_att_set_preferred_mtu(BLE_ATT_MTU_DFLT); + + ble_hs_cfg.gatts_register_cb = gatt_register_cb; + +#if defined(CONFIG_BLE_MESH_NODE) && CONFIG_BLE_MESH_NODE + static bool init = false; + int rc; + + if (init == false) { + ble_svc_gap_init(); + ble_svc_gatt_init(); + + rc = ble_gatts_count_cfg(svc_defs); + assert(rc == 0); + + rc = ble_gatts_add_svcs(svc_defs); + assert(rc == 0); + + ble_gatts_start(); + + ble_gatts_svc_set_visibility(prov_svc_start_handle, 1); + ble_gatts_svc_set_visibility(proxy_svc_start_handle, 0); + + init = true; + } +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT; + bt_mesh_gattc_info[i].wr_desc_done = false; + } +#endif +} + +void bt_mesh_gatt_deinit(void) +{ +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + memset(bt_mesh_gatts_addr, 0, BLE_MESH_ADDR_LEN); +#endif + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + for (int i = 0; i < ARRAY_SIZE(bt_mesh_gattc_info); i++) { + bt_mesh_gattc_info[i].conn.handle = 0xFFFF; + memset(&bt_mesh_gattc_info[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_gattc_info[i].service_uuid = 0U; + bt_mesh_gattc_info[i].mtu = BLE_ATT_MTU_DFLT; + bt_mesh_gattc_info[i].wr_desc_done = false; + bt_mesh_gattc_info[i].start_handle = 0U; + bt_mesh_gattc_info[i].end_handle = 0U; + bt_mesh_gattc_info[i].data_in_handle = 0U; + bt_mesh_gattc_info[i].data_out_handle = 0U; + bt_mesh_gattc_info[i].ccc_handle = 0U; + } +#endif +} + +void ble_sm_alg_ecc_init(void); + +void bt_mesh_adapt_init(void) +{ + BT_DBG("%s", __func__); + + /* initialization of P-256 parameters */ + ble_sm_alg_ecc_init(); + + /* Set "bt_mesh_dev.flags" to 0 (only the "BLE_MESH_DEV_HAS_PUB_KEY" + * flag is used) here, because we need to make sure each time after + * the private key is initialized, a corresponding public key must + * be generated. + */ + bt_mesh_atomic_set(bt_mesh_dev.flags, 0); + bt_mesh_rand(bt_mesh_private_key, sizeof(bt_mesh_private_key)); +} + +int bt_mesh_rand(void *buf, size_t len) +{ + int i; + + if (buf == NULL || len == 0) { + BT_ERR("%s, Invalid parameter", __func__); + return -EAGAIN; + } + + for (i = 0; i < (int)(len / sizeof(u32_t)); i++) { + u32_t rand = esp_random(); + memcpy(buf + i * sizeof(u32_t), &rand, sizeof(u32_t)); + } + + BT_DBG("%s, rand: %s", __func__, bt_hex(buf, len)); + return 0; +} + +void bt_mesh_set_private_key(const u8_t pri_key[32]) +{ + memcpy(bt_mesh_private_key, pri_key, 32); +} + +int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv); + +const u8_t *bt_mesh_pub_key_get(void) +{ + uint8_t pri_key[32] = {0}; + +#if 1 + if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY)) { + return bt_mesh_public_key; + } +#else + /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BV-12-C requires + * different public key for each provisioning procedure. + * Note: if enabled, when Provisioner provision multiple devices + * at the same time, this may cause invalid confirmation value. + */ + if (bt_mesh_rand(bt_mesh_private_key, 32)) { + BT_ERR("%s, Unable to generate bt_mesh_private_key", __func__); + return NULL; + } +#endif + + int rc = ble_sm_alg_gen_key_pair(bt_mesh_public_key, pri_key); + if (rc != 0) { + BT_ERR("%s, Failed to generate the key pair", __func__); + return NULL; + } + memcpy(bt_mesh_private_key, pri_key, 32); + + bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY); + + BT_DBG("Public Key %s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key))); + + return bt_mesh_public_key; +} + +bool bt_mesh_check_public_key(const u8_t key[64]) +{ + struct mbedtls_ecp_point pt = {0}; + mbedtls_ecp_group grp = {0}; + bool rc = false; + + uint8_t pub[65] = {0}; + /* Hardcoded first byte of pub key for MBEDTLS_ECP_PF_UNCOMPRESSED */ + pub[0] = 0x04; + memcpy(&pub[1], key, 64); + + /* Initialize the required structures here */ + mbedtls_ecp_point_init(&pt); + mbedtls_ecp_group_init(&grp); + + /* Below 3 steps are to validate public key on curve secp256r1 */ + if (mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1) != 0) { + goto exit; + } + + if (mbedtls_ecp_point_read_binary(&grp, &pt, pub, 65) != 0) { + goto exit; + } + + if (mbedtls_ecp_check_pubkey(&grp, &pt) != 0) { + goto exit; + } + + rc = true; + +exit: + mbedtls_ecp_point_free(&pt); + mbedtls_ecp_group_free(&grp); + return rc; + +} + +int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, + uint8_t *our_priv_key, uint8_t *out_dhkey); + +int bt_mesh_dh_key_gen(const u8_t remote_pk[64], bt_mesh_dh_key_cb_t cb, const u8_t idx) +{ + uint8_t dhkey[32]; + + BT_DBG("private key = %s", bt_hex(bt_mesh_private_key, 32)); + + ble_sm_alg_gen_dhkey((uint8_t *)&remote_pk[0], (uint8_t *)&remote_pk[32], bt_mesh_private_key, dhkey); + + if (cb != NULL) { + cb((const u8_t *)dhkey, idx); + } + return 0; +} + +#if CONFIG_MBEDTLS_HARDWARE_AES +static void ecb_encrypt(u8_t const *const key_le, u8_t const *const clear_text_le, + u8_t *const cipher_text_le, u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + mem_rcopy(&aes_ctx.key[0], key_le, 16); + mem_rcopy(&ecb.clear_text[0], clear_text_le, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + if (cipher_text_le) { + mem_rcopy(cipher_text_le, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } + + if (cipher_text_be) { + memcpy(cipher_text_be, &ecb.cipher_text[0], + sizeof(ecb.cipher_text)); + } +} + +static void ecb_encrypt_be(u8_t const *const key_be, u8_t const *const clear_text_be, + u8_t *const cipher_text_be) +{ + struct bt_mesh_ecb_param ecb; + mbedtls_aes_context aes_ctx = {0}; + + aes_ctx.key_bytes = 16; + memcpy(&aes_ctx.key[0], key_be, 16); + memcpy(&ecb.clear_text[0], clear_text_be, sizeof(ecb.clear_text)); + mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, &ecb.clear_text[0], &ecb.cipher_text[0]); + + memcpy(cipher_text_be, &ecb.cipher_text[0], sizeof(ecb.cipher_text)); +} +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ + +int bt_mesh_encrypt_le(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt(key, plaintext, enc_data, NULL); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s; + u8_t tmp[16]; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + sys_memcpy_swap(tmp, key, 16); + + if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_memcpy_swap(tmp, plaintext, 16); + + if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + sys_mem_swap(enc_data, 16); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +int bt_mesh_encrypt_be(const u8_t key[16], const u8_t plaintext[16], + u8_t enc_data[16]) +{ +#if CONFIG_MBEDTLS_HARDWARE_AES + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + ecb_encrypt_be(key, plaintext, enc_data); + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#else /* CONFIG_MBEDTLS_HARDWARE_AES */ + struct tc_aes_key_sched_struct s; + + BT_DBG("key %s plaintext %s", bt_hex(key, 16), bt_hex(plaintext, 16)); + + if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) { + return -EINVAL; + } + + BT_DBG("enc_data %s", bt_hex(enc_data, 16)); + + return 0; +#endif /* CONFIG_MBEDTLS_HARDWARE_AES */ +} + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) +int bt_mesh_update_exceptional_list(u8_t sub_code, u8_t type, void *info) +{ + BT_ERR("%s, Unsupported for NimBLE host", __func__); + return 0; +} +#endif diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.c b/components/bt/esp_ble_mesh/mesh_core/prov.c new file mode 100644 index 000000000..dcd990e29 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/prov.c @@ -0,0 +1,1823 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_PROV) + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "access.h" +#include "foundation.h" +#include "mesh_common.h" +#include "mesh_proxy.h" +#include "proxy_server.h" +#include "prov.h" + +#if CONFIG_BLE_MESH_NODE + +/* 3 transmissions, 20ms interval */ +#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20) + +#define AUTH_METHOD_NO_OOB 0x00 +#define AUTH_METHOD_STATIC 0x01 +#define AUTH_METHOD_OUTPUT 0x02 +#define AUTH_METHOD_INPUT 0x03 + +#define OUTPUT_OOB_BLINK 0x00 +#define OUTPUT_OOB_BEEP 0x01 +#define OUTPUT_OOB_VIBRATE 0x02 +#define OUTPUT_OOB_NUMBER 0x03 +#define OUTPUT_OOB_STRING 0x04 + +#define INPUT_OOB_PUSH 0x00 +#define INPUT_OOB_TWIST 0x01 +#define INPUT_OOB_NUMBER 0x02 +#define INPUT_OOB_STRING 0x03 + +#define PUB_KEY_NO_OOB 0x00 +#define PUB_KEY_OOB 0x01 + +#define PROV_ERR_NONE 0x00 +#define PROV_ERR_NVAL_PDU 0x01 +#define PROV_ERR_NVAL_FMT 0x02 +#define PROV_ERR_UNEXP_PDU 0x03 +#define PROV_ERR_CFM_FAILED 0x04 +#define PROV_ERR_RESOURCES 0x05 +#define PROV_ERR_DECRYPT 0x06 +#define PROV_ERR_UNEXP_ERR 0x07 +#define PROV_ERR_ADDR 0x08 + +#define PROV_INVITE 0x00 +#define PROV_CAPABILITIES 0x01 +#define PROV_START 0x02 +#define PROV_PUB_KEY 0x03 +#define PROV_INPUT_COMPLETE 0x04 +#define PROV_CONFIRM 0x05 +#define PROV_RANDOM 0x06 +#define PROV_DATA 0x07 +#define PROV_COMPLETE 0x08 +#define PROV_FAILED 0x09 + +#define PROV_ALG_P256 0x00 + +#define GPCF(gpc) (gpc & 0x03) +#define GPC_START(last_seg) (((last_seg) << 2) | 0x00) +#define GPC_ACK 0x01 +#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02) +#define GPC_CTL(op) (((op) << 2) | 0x03) + +#define START_PAYLOAD_MAX 20 +#define CONT_PAYLOAD_MAX 23 + +#define START_LAST_SEG(gpc) (gpc >> 2) +#define CONT_SEG_INDEX(gpc) (gpc >> 2) + +#define BEARER_CTL(gpc) (gpc >> 2) +#define LINK_OPEN 0x00 +#define LINK_ACK 0x01 +#define LINK_CLOSE 0x02 + +#define CLOSE_REASON_SUCCESS 0x00 +#define CLOSE_REASON_TIMEOUT 0x01 +#define CLOSE_REASON_FAILED 0x02 + +#define XACT_SEG_DATA(_seg) (&link.rx.buf->data[20 + ((_seg - 1) * 23)]) +#define XACT_SEG_RECV(_seg) (link.rx.seg &= ~(1 << (_seg))) + +#define XACT_NVAL 0xff + +enum { + REMOTE_PUB_KEY, /* Remote key has been received */ + OOB_PUB_KEY, /* OOB public key is available */ + LINK_ACTIVE, /* Link has been opened */ + HAVE_DHKEY, /* DHKey has been calculated */ + SEND_CONFIRM, /* Waiting to send Confirm value */ + WAIT_NUMBER, /* Waiting for number input from user */ + WAIT_STRING, /* Waiting for string input from user */ + LINK_INVALID, /* Error occurred during provisioning */ + + NUM_FLAGS, +}; + +struct prov_link { + BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); +#if defined(CONFIG_BLE_MESH_PB_GATT) + struct bt_mesh_conn *conn; /* GATT connection */ +#endif + u8_t dhkey[32]; /* Calculated DHKey */ + u8_t expect; /* Next expected PDU */ + + bool oob_pk_flag; /* Flag indicates whether using OOB public key */ + + u8_t oob_method; + u8_t oob_action; + u8_t oob_size; + + u8_t conf[16]; /* Remote Confirmation */ + u8_t rand[16]; /* Local Random */ + u8_t auth[16]; /* Authentication Value */ + + u8_t conf_salt[16]; /* ConfirmationSalt */ + u8_t conf_key[16]; /* ConfirmationKey */ + u8_t conf_inputs[145]; /* ConfirmationInputs */ + u8_t prov_salt[16]; /* Provisioning Salt */ + +#if defined(CONFIG_BLE_MESH_PB_ADV) + u32_t id; /* Link ID */ + u8_t tx_pdu_type; /* The previously transmitted Provisioning PDU type */ + + struct { + u8_t id; /* Transaction ID */ + u8_t prev_id; /* Previous Transaction ID */ + u8_t seg; /* Bit-field of unreceived segments */ + u8_t last_seg; /* Last segment (to check length) */ + u8_t fcs; /* Expected FCS value */ + struct net_buf_simple *buf; + } rx; + + struct { + /* Start timestamp of the transaction */ + s64_t start; + + /* Transaction id*/ + u8_t id; + + /* Pending outgoing buffer(s) */ + struct net_buf *buf[3]; + + /* Retransmit timer */ + struct k_delayed_work retransmit; + } tx; +#endif + + struct k_delayed_work prot_timer; +}; + +struct prov_rx { + u32_t link_id; + u8_t xact_id; + u8_t gpc; +}; + +#define BUF_TIMEOUT K_MSEC(400) + +#if defined(CONFIG_BLE_MESH_FAST_PROV) +#define RETRANSMIT_TIMEOUT K_MSEC(360) +#define TRANSACTION_TIMEOUT K_SECONDS(3) +#define PROTOCOL_TIMEOUT K_SECONDS(6) +#else +#define RETRANSMIT_TIMEOUT K_MSEC(500) +#define TRANSACTION_TIMEOUT K_SECONDS(30) +#define PROTOCOL_TIMEOUT K_SECONDS(60) +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +#define PROV_BUF_HEADROOM 5 +#else +#define PROV_BUF_HEADROOM 0 +NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, 65); +#endif + +#define PROV_BUF(name, len) \ + NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len) + +static struct prov_link link; + +static const struct bt_mesh_prov *prov; + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static bt_mesh_mutex_t pb_buf_lock; + +static void bt_mesh_pb_buf_mutex_new(void) +{ + if (!pb_buf_lock.mutex) { + bt_mesh_mutex_create(&pb_buf_lock); + } +} + +static void bt_mesh_pb_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&pb_buf_lock); +} + +static void bt_mesh_pb_buf_lock(void) +{ + bt_mesh_mutex_lock(&pb_buf_lock); +} + +static void bt_mesh_pb_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&pb_buf_lock); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +static void reset_state(void) +{ + k_delayed_work_cancel(&link.prot_timer); + + /* Disable Attention Timer if it was set */ + if (link.conf_inputs[0]) { + bt_mesh_attention(NULL, 0); + } + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (link.conn) { + bt_mesh_conn_unref(link.conn); + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_ADV) + /* Clear everything except the retransmit and protocol timer + * delayed work objects. + */ + (void)memset(&link, 0, offsetof(struct prov_link, tx.retransmit)); + link.rx.prev_id = XACT_NVAL; + +#if defined(CONFIG_BLE_MESH_PB_GATT) + link.rx.buf = bt_mesh_proxy_get_buf(); +#else + net_buf_simple_reset(&rx_buf); + link.rx.buf = &rx_buf; +#endif /* PB_GATT */ + +#else /* !PB_ADV */ + /* Clear everything except the protocol timer (k_delayed_work) */ + (void)memset(&link, 0, offsetof(struct prov_link, prot_timer)); +#endif /* PB_ADV */ +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void buf_sent(int err, void *user_data) +{ + if (!link.tx.buf[0]) { + return; + } + + k_delayed_work_submit(&link.tx.retransmit, RETRANSMIT_TIMEOUT); +} + +static struct bt_mesh_send_cb buf_sent_cb = { + .end = buf_sent, +}; + +static void free_segments(void) +{ + int i; + + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { + struct net_buf *buf = link.tx.buf[i]; + + if (!buf) { + break; + } + + link.tx.buf[i] = NULL; + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + /* Mark as canceled */ + BLE_MESH_ADV(buf)->busy = 0U; + net_buf_unref(buf); + } + + bt_mesh_pb_buf_unlock(); +} + +static void prov_clear_tx(void) +{ + BT_DBG("%s", __func__); + + k_delayed_work_cancel(&link.tx.retransmit); + + free_segments(); +} + +static void reset_adv_link(void) +{ + prov_clear_tx(); + + if (prov->link_close) { + prov->link_close(BLE_MESH_PROV_ADV); + } + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link.id); +#endif + + reset_state(); +} + +static struct net_buf *adv_buf_create(void) +{ + struct net_buf *buf = NULL; + + buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); + if (!buf) { + BT_ERR("%s, Out of provisioning buffers", __func__); + return NULL; + } + + return buf; +} + +static u8_t pending_ack = XACT_NVAL; + +static void ack_complete(u16_t duration, int err, void *user_data) +{ + BT_DBG("xact %u complete", (u8_t)pending_ack); + pending_ack = XACT_NVAL; +} + +static void gen_prov_ack_send(u8_t xact_id) +{ + static const struct bt_mesh_send_cb cb = { + .start = ack_complete, + }; + const struct bt_mesh_send_cb *complete = NULL; + struct net_buf *buf = NULL; + + BT_DBG("xact_id %u", xact_id); + + if (pending_ack == xact_id) { + BT_DBG("Not sending duplicate ack"); + return; + } + + buf = adv_buf_create(); + if (!buf) { + return; + } + + if (pending_ack == XACT_NVAL) { + pending_ack = xact_id; + complete = &cb; + } else { + complete = NULL; + } + + net_buf_add_be32(buf, link.id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_ACK); + + bt_mesh_adv_send(buf, complete, NULL); + net_buf_unref(buf); +} + +static void send_reliable(void) +{ + int i; + + link.tx.start = k_uptime_get(); + + for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { + struct net_buf *buf = link.tx.buf[i]; + + if (!buf) { + break; + } + + if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) { + bt_mesh_adv_send(buf, NULL, NULL); + } else { + bt_mesh_adv_send(buf, &buf_sent_cb, NULL); + } + } +} + +static int bearer_ctl_send(u8_t op, void *data, u8_t data_len) +{ + struct net_buf *buf = NULL; + + BT_DBG("op 0x%02x data_len %u", op, data_len); + + prov_clear_tx(); + + buf = adv_buf_create(); + if (!buf) { + return -ENOBUFS; + } + + net_buf_add_be32(buf, link.id); + /* Transaction ID, always 0 for Bearer messages */ + net_buf_add_u8(buf, 0x00); + net_buf_add_u8(buf, GPC_CTL(op)); + net_buf_add_mem(buf, data, data_len); + + link.tx.buf[0] = buf; + send_reliable(); + + return 0; +} + +static u8_t last_seg(u8_t len) +{ + if (len <= START_PAYLOAD_MAX) { + return 0; + } + + len -= START_PAYLOAD_MAX; + + return 1 + (len / CONT_PAYLOAD_MAX); +} + +static inline u8_t next_transaction_id(void) +{ + if (link.tx.id != 0U && link.tx.id != 0xFF) { + return ++link.tx.id; + } + + link.tx.id = 0x80; + return link.tx.id; +} + +static int prov_send_adv(struct net_buf_simple *msg) +{ + struct net_buf *start = NULL, *buf = NULL; + u8_t seg_len = 0U, seg_id = 0U; + u8_t xact_id = 0U; + s32_t timeout = PROTOCOL_TIMEOUT; + + BT_DBG("%s, len %u: %s", __func__, msg->len, bt_hex(msg->data, msg->len)); + + prov_clear_tx(); + + start = adv_buf_create(); + if (!start) { + return -ENOBUFS; + } + + xact_id = next_transaction_id(); + net_buf_add_be32(start, link.id); + net_buf_add_u8(start, xact_id); + + net_buf_add_u8(start, GPC_START(last_seg(msg->len))); + net_buf_add_be16(start, msg->len); + net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); + + link.tx.buf[0] = start; + /* Changed by Espressif, get message type */ + link.tx_pdu_type = msg->data[0]; + + seg_len = MIN(msg->len, START_PAYLOAD_MAX); + BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); + net_buf_add_mem(start, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + + buf = start; + for (seg_id = 1U; msg->len > 0; seg_id++) { + if (seg_id >= ARRAY_SIZE(link.tx.buf)) { + BT_ERR("%s, Too big message", __func__); + free_segments(); + return -E2BIG; + } + + buf = adv_buf_create(); + if (!buf) { + free_segments(); + return -ENOBUFS; + } + + link.tx.buf[seg_id] = buf; + + seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); + + BT_DBG("seg_id %u len %u: %s", seg_id, seg_len, + bt_hex(msg->data, seg_len)); + + net_buf_add_be32(buf, link.id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_CONT(seg_id)); + net_buf_add_mem(buf, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + } + + send_reliable(); + + /* Changed by Espressif, add provisioning timeout timer operations. + * When sending a provisioning PDU successfully, restart the 60s timer. + */ +#if defined(CONFIG_BLE_MESH_FAST_PROV) + if (link.tx_pdu_type >= PROV_COMPLETE) { + timeout = K_SECONDS(60); + } +#endif + k_delayed_work_submit(&link.prot_timer, timeout); + + return 0; +} + +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static int prov_send_gatt(struct net_buf_simple *msg) +{ + int err = 0; + + if (!link.conn) { + return -ENOTCONN; + } + + /* Changed by Espressif, add provisioning timeout timer operations. + * When sending a provisioning PDU successfully, restart the 60s timer. + */ + err = bt_mesh_proxy_send(link.conn, BLE_MESH_PROXY_PROV, msg); + if (err) { + BT_ERR("%s, Failed to send provisioning PDU", __func__); + return err; + } + + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +static inline int prov_send(struct net_buf_simple *buf) +{ +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (link.conn) { + return prov_send_gatt(buf); + } +#endif +#if defined(CONFIG_BLE_MESH_PB_ADV) + return prov_send_adv(buf); +#else + return 0; +#endif +} + +static void prov_buf_init(struct net_buf_simple *buf, u8_t type) +{ + net_buf_simple_reserve(buf, PROV_BUF_HEADROOM); + net_buf_simple_add_u8(buf, type); +} + +static void prov_send_fail_msg(u8_t err) +{ + PROV_BUF(buf, 2); + + prov_buf_init(&buf, PROV_FAILED); + net_buf_simple_add_u8(&buf, err); + + if (prov_send(&buf)) { + BT_ERR("Failed to send Provisioning Failed message"); + } + + bt_mesh_atomic_set_bit(link.flags, LINK_INVALID); +} + +static void prov_invite(const u8_t *data) +{ + PROV_BUF(buf, 12); + + BT_DBG("Attention Duration: %u seconds", data[0]); + + if (data[0]) { + bt_mesh_attention(NULL, data[0]); + } + + link.conf_inputs[0] = data[0]; + + prov_buf_init(&buf, PROV_CAPABILITIES); + + /* Number of Elements supported */ + net_buf_simple_add_u8(&buf, bt_mesh_elem_count()); + + /* Supported algorithms - FIPS P-256 Eliptic Curve */ + net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256)); + + /* Public Key Type */ + net_buf_simple_add_u8(&buf, prov->oob_pub_key); + + /* Static OOB Type */ + net_buf_simple_add_u8(&buf, prov->static_val ? BIT(0) : 0x00); + + /* Output OOB Size */ + net_buf_simple_add_u8(&buf, prov->output_size); + + /* Output OOB Action */ + net_buf_simple_add_be16(&buf, prov->output_actions); + + /* Input OOB Size */ + net_buf_simple_add_u8(&buf, prov->input_size); + + /* Input OOB Action */ + net_buf_simple_add_be16(&buf, prov->input_actions); + + memcpy(&link.conf_inputs[1], &buf.data[1], 11); + + if (prov_send(&buf)) { + BT_ERR("%s, Failed to send capabilities", __func__); + return; + } + + link.expect = PROV_START; +} + +static void prov_capabilities(const u8_t *data) +{ + u16_t algorithms = 0U, output_action = 0U, input_action = 0U; + + BT_DBG("Elements: %u", data[0]); + + algorithms = sys_get_be16(&data[1]); + BT_DBG("Algorithms: %u", algorithms); + + BT_DBG("Public Key Type: 0x%02x", data[3]); + BT_DBG("Static OOB Type: 0x%02x", data[4]); + BT_DBG("Output OOB Size: %u", data[5]); + + output_action = sys_get_be16(&data[6]); + BT_DBG("Output OOB Action: 0x%04x", output_action); + + BT_DBG("Input OOB Size: %u", data[8]); + + input_action = sys_get_be16(&data[9]); + BT_DBG("Input OOB Action: 0x%04x", input_action); + + ((void) algorithms); + ((void) output_action); + ((void) input_action); +} + +static bt_mesh_output_action_t output_action(u8_t action) +{ + switch (action) { + case OUTPUT_OOB_BLINK: + return BLE_MESH_BLINK; + case OUTPUT_OOB_BEEP: + return BLE_MESH_BEEP; + case OUTPUT_OOB_VIBRATE: + return BLE_MESH_VIBRATE; + case OUTPUT_OOB_NUMBER: + return BLE_MESH_DISPLAY_NUMBER; + case OUTPUT_OOB_STRING: + return BLE_MESH_DISPLAY_STRING; + default: + return BLE_MESH_NO_OUTPUT; + } +} + +static bt_mesh_input_action_t input_action(u8_t action) +{ + switch (action) { + case INPUT_OOB_PUSH: + return BLE_MESH_PUSH; + case INPUT_OOB_TWIST: + return BLE_MESH_TWIST; + case INPUT_OOB_NUMBER: + return BLE_MESH_ENTER_NUMBER; + case INPUT_OOB_STRING: + return BLE_MESH_ENTER_STRING; + default: + return BLE_MESH_NO_INPUT; + } +} + +static int prov_auth(u8_t method, u8_t action, u8_t size) +{ + bt_mesh_output_action_t output = 0U; + bt_mesh_input_action_t input = 0U; + + switch (method) { + case AUTH_METHOD_NO_OOB: + if (action || size) { + return -EINVAL; + } + + (void)memset(link.auth, 0, sizeof(link.auth)); + return 0; + case AUTH_METHOD_STATIC: + if (action || size) { + return -EINVAL; + } + + memcpy(link.auth + 16 - prov->static_val_len, + prov->static_val, prov->static_val_len); + (void)memset(link.auth, 0, + sizeof(link.auth) - prov->static_val_len); + return 0; + + case AUTH_METHOD_OUTPUT: + output = output_action(action); + if (!output) { + return -EINVAL; + } + + if (!(prov->output_actions & output)) { + return -EINVAL; + } + + if (size > prov->output_size) { + return -EINVAL; + } + + if (output == BLE_MESH_DISPLAY_STRING) { + unsigned char str[9] = {'\0'}; + u8_t i = 0U; + + bt_mesh_rand(str, size); + + /* Normalize to '0' .. '9' & 'A' .. 'Z' */ + for (i = 0U; i < size; i++) { + str[i] %= 36; + if (str[i] < 10) { + str[i] += '0'; + } else { + str[i] += 'A' - 10; + } + } + str[size] = '\0'; + + memcpy(link.auth, str, size); + (void)memset(link.auth + size, 0, + sizeof(link.auth) - size); + + return prov->output_string((char *)str); + } else { + u32_t div[8] = { 10, 100, 1000, 10000, 100000, + 1000000, 10000000, 100000000 + }; + u32_t num = 0U; + + bt_mesh_rand(&num, sizeof(num)); + num %= div[size - 1]; + + sys_put_be32(num, &link.auth[12]); + (void)memset(link.auth, 0, 12); + + return prov->output_number(output, num); + } + + case AUTH_METHOD_INPUT: + input = input_action(action); + if (!input) { + return -EINVAL; + } + + if (!(prov->input_actions & input)) { + return -EINVAL; + } + + if (size > prov->input_size) { + return -EINVAL; + } + + if (input == BLE_MESH_ENTER_STRING) { + bt_mesh_atomic_set_bit(link.flags, WAIT_STRING); + } else { + bt_mesh_atomic_set_bit(link.flags, WAIT_NUMBER); + } + + return prov->input(input, size); + + default: + return -EINVAL; + } +} + +static void prov_start(const u8_t *data) +{ + BT_INFO("Algorithm: 0x%02x", data[0]); + BT_INFO("Public Key: 0x%02x", data[1]); + BT_INFO("Auth Method: 0x%02x", data[2]); + BT_INFO("Auth Action: 0x%02x", data[3]); + BT_INFO("Auth Size: 0x%02x", data[4]); + + if (data[0] != PROV_ALG_P256) { + BT_ERR("%s, Unknown algorithm 0x%02x", __func__, data[0]); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + if (data[1] != prov->oob_pub_key) { + BT_ERR("%s, Invalid public key type: 0x%02x", __func__, data[1]); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + memcpy(&link.conf_inputs[12], data, 5); + + link.expect = PROV_PUB_KEY; + + /* If Provisioning Start PDU indicates that provisioner chooses + * OOB public key, then callback to the application layer to let + * users input public & private key pair. + */ + link.oob_pk_flag = data[1] ? true : false; + if (link.oob_pk_flag) { + prov->oob_pub_key_cb(); + } + + if (prov_auth(data[2], data[3], data[4]) < 0) { + BT_ERR("%s, Invalid authentication method: 0x%02x; " + "action: 0x%02x; size: 0x%02x", + __func__, data[2], data[3], data[4]); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + } +} + +static void send_confirm(void) +{ + PROV_BUF(cfm, 17); + + BT_DBG("ConfInputs[0] %s", bt_hex(link.conf_inputs, 64)); + BT_DBG("ConfInputs[64] %s", bt_hex(&link.conf_inputs[64], 64)); + BT_DBG("ConfInputs[128] %s", bt_hex(&link.conf_inputs[128], 17)); + + if (bt_mesh_prov_conf_salt(link.conf_inputs, link.conf_salt)) { + BT_ERR("%s, Unable to generate confirmation salt", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("ConfirmationSalt: %s", bt_hex(link.conf_salt, 16)); + + if (bt_mesh_prov_conf_key(link.dhkey, link.conf_salt, link.conf_key)) { + BT_ERR("%s, Unable to generate confirmation key", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("ConfirmationKey: %s", bt_hex(link.conf_key, 16)); + + if (bt_mesh_rand(link.rand, 16)) { + BT_ERR("%s, Unable to generate random number", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("LocalRandom: %s", bt_hex(link.rand, 16)); + + prov_buf_init(&cfm, PROV_CONFIRM); + + if (bt_mesh_prov_conf(link.conf_key, link.rand, link.auth, + net_buf_simple_add(&cfm, 16))) { + BT_ERR("%s, Unable to generate confirmation value", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + if (prov_send(&cfm)) { + BT_ERR("%s, Unable to send Provisioning Confirm", __func__); + return; + } + + link.expect = PROV_RANDOM; +} + +static void send_input_complete(void) +{ + PROV_BUF(buf, 1); + + prov_buf_init(&buf, PROV_INPUT_COMPLETE); + if (prov_send(&buf)) { + BT_ERR("Failed to send Provisioning Input Complete"); + } +} + +int bt_mesh_input_number(u32_t num) +{ + BT_INFO("%u", num); + + if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_NUMBER)) { + return -EINVAL; + } + + sys_put_be32(num, &link.auth[12]); + + send_input_complete(); + + if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { + return 0; + } + + if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { + send_confirm(); + } + + return 0; +} + +int bt_mesh_input_string(const char *str) +{ + BT_INFO("%s", str); + + if (!bt_mesh_atomic_test_and_clear_bit(link.flags, WAIT_STRING)) { + return -EINVAL; + } + + (void)memcpy(link.auth, str, prov->input_size); + + send_input_complete(); + + if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { + return 0; + } + + if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { + send_confirm(); + } + + return 0; +} + +static void prov_dh_key_cb(const u8_t key[32], const u8_t idx) +{ + BT_DBG("%p", key); + + if (!key) { + BT_ERR("%s, DHKey generation failed", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + sys_memcpy_swap(link.dhkey, key, 32); + + BT_DBG("DHkey: %s", bt_hex(link.dhkey, 32)); + + bt_mesh_atomic_set_bit(link.flags, HAVE_DHKEY); + + if (bt_mesh_atomic_test_bit(link.flags, WAIT_NUMBER) || + bt_mesh_atomic_test_bit(link.flags, WAIT_STRING)) { + return; + } + + if (bt_mesh_atomic_test_and_clear_bit(link.flags, SEND_CONFIRM)) { + send_confirm(); + } +} + +static void send_pub_key(void) +{ + PROV_BUF(buf, 65); + const u8_t *key = NULL; + + /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). + * X and Y halves are swapped independently. Use response + * buffer as a temporary storage location. The validating of + * the remote public key is finished when it is received. + */ + sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); + sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); + + if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { + BT_ERR("%s, Unable to generate DHKey", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + key = bt_mesh_pub_key_get(); + if (!key) { + BT_ERR("%s, No public key available", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("Local Public Key: %s", bt_hex(key, 64)); + + prov_buf_init(&buf, PROV_PUB_KEY); + + /* Swap X and Y halves independently to big-endian */ + sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32); + sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32); + + memcpy(&link.conf_inputs[81], &buf.data[1], 64); + + if (prov_send(&buf)) { + BT_ERR("Failed to send Public Key"); + return; + } + + link.expect = PROV_CONFIRM; +} + +static int bt_mesh_calc_dh_key(void) +{ + NET_BUF_SIMPLE_DEFINE(buf, 64); + + /* Copy remote key in little-endian for bt_mesh_dh_key_gen(). + * X and Y halves are swapped independently. + */ + net_buf_simple_reset(&buf); + sys_memcpy_swap(buf.data, &link.conf_inputs[17], 32); + sys_memcpy_swap(&buf.data[32], &link.conf_inputs[49], 32); + + if (bt_mesh_dh_key_gen(buf.data, prov_dh_key_cb, 0)) { + BT_ERR("%s, Unable to generate DHKey", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return -EIO; + } + + return 0; +} + +int bt_mesh_set_oob_pub_key(const u8_t pub_key_x[32], const u8_t pub_key_y[32], + const u8_t pri_key[32]) +{ + if (!pub_key_x || !pub_key_y || !pri_key) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Copy OOB public key in big-endian to Provisioning ConfirmationInputs, + * X and Y halves are swapped independently. + * And set input private key to mesh_bearer_adapt.c + */ + sys_memcpy_swap(&link.conf_inputs[81], pub_key_x, 32); + sys_memcpy_swap(&link.conf_inputs[81] + 32, pub_key_y, 32); + bt_mesh_set_private_key(pri_key); + + bt_mesh_atomic_set_bit(link.flags, OOB_PUB_KEY); + + /* If remote public key is not got, just return */ + if (!bt_mesh_atomic_test_bit(link.flags, REMOTE_PUB_KEY)) { + return 0; + } + + return bt_mesh_calc_dh_key(); +} + +static void prov_pub_key(const u8_t *data) +{ + BT_DBG("Remote Public Key: %s", bt_hex(data, 64)); + + /* BLE Mesh BQB test case MESH/NODE/PROV/UPD/BI-13-C needs to + * check the public key using the following rules: + * (1) X > 0, Y > 0 + * (2) X > 0, Y = 0 + * (3) X = 0, Y = 0 + */ + if (!bt_mesh_check_public_key(data)) { + BT_ERR("%s, Invalid public key", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + return; + } + + memcpy(&link.conf_inputs[17], data, 64); + bt_mesh_atomic_set_bit(link.flags, REMOTE_PUB_KEY); + + if (!bt_mesh_pub_key_get()) { + /* Clear retransmit timer */ +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(); +#endif + BT_WARN("Waiting for a local public key"); + return; + } + + if (!link.oob_pk_flag) { + send_pub_key(); + } else { + link.expect = PROV_CONFIRM; + } +} + +static void prov_input_complete(const u8_t *data) +{ + BT_DBG("%s", __func__); +} + +static void prov_confirm(const u8_t *data) +{ + BT_DBG("Remote Confirm: %s", bt_hex(data, 16)); + + memcpy(link.conf, data, 16); + + if (!bt_mesh_atomic_test_bit(link.flags, HAVE_DHKEY)) { +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(); +#endif + bt_mesh_atomic_set_bit(link.flags, SEND_CONFIRM); + /* If using OOB public key and it has already got, calculates dhkey */ + if (link.oob_pk_flag && bt_mesh_atomic_test_bit(link.flags, OOB_PUB_KEY)) { + bt_mesh_calc_dh_key(); + } + } else { + send_confirm(); + } +} + +static void prov_random(const u8_t *data) +{ + PROV_BUF(rnd, 17); + u8_t conf_verify[16] = {0}; + + BT_DBG("Remote Random: %s", bt_hex(data, 16)); + + if (bt_mesh_prov_conf(link.conf_key, data, link.auth, conf_verify)) { + BT_ERR("%s, Unable to calculate confirmation verification", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + if (memcmp(conf_verify, link.conf, 16)) { + BT_ERR("%s, Invalid confirmation value", __func__); + BT_DBG("Received: %s", bt_hex(link.conf, 16)); + BT_DBG("Calculated: %s", bt_hex(conf_verify, 16)); + prov_send_fail_msg(PROV_ERR_CFM_FAILED); + return; + } + + prov_buf_init(&rnd, PROV_RANDOM); + net_buf_simple_add_mem(&rnd, link.rand, 16); + + if (prov_send(&rnd)) { + BT_ERR("%s, Failed to send Provisioning Random", __func__); + return; + } + + if (bt_mesh_prov_salt(link.conf_salt, data, link.rand, + link.prov_salt)) { + BT_ERR("%s, Failed to generate provisioning salt", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("ProvisioningSalt: %s", bt_hex(link.prov_salt, 16)); + + link.expect = PROV_DATA; +} + +static inline bool is_pb_gatt(void) +{ +#if defined(CONFIG_BLE_MESH_PB_GATT) + return !!link.conn; +#else + return false; +#endif +} + +static void prov_data(const u8_t *data) +{ + PROV_BUF(msg, 1); + u8_t session_key[16] = {0}; + u8_t nonce[13] = {0}; + u8_t dev_key[16] = {0}; + u8_t pdu[25] = {0}; + u8_t flags = 0U; + u32_t iv_index = 0U; + u16_t addr = 0U; + u16_t net_idx = 0U; + int err = 0; + bool identity_enable = false; + + BT_DBG("%s", __func__); + + err = bt_mesh_session_key(link.dhkey, link.prov_salt, session_key); + if (err) { + BT_ERR("%s, Unable to generate session key", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("SessionKey: %s", bt_hex(session_key, 16)); + + err = bt_mesh_prov_nonce(link.dhkey, link.prov_salt, nonce); + if (err) { + BT_ERR("%s, Unable to generate session nonce", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("Nonce: %s", bt_hex(nonce, 13)); + + err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu); + if (err) { + BT_ERR("%s, Unable to decrypt provisioning data", __func__); + prov_send_fail_msg(PROV_ERR_DECRYPT); + return; + } + + err = bt_mesh_dev_key(link.dhkey, link.prov_salt, dev_key); + if (err) { + BT_ERR("%s, Unable to generate device key", __func__); + prov_send_fail_msg(PROV_ERR_UNEXP_ERR); + return; + } + + BT_DBG("DevKey: %s", bt_hex(dev_key, 16)); + + net_idx = sys_get_be16(&pdu[16]); + flags = pdu[18]; + iv_index = sys_get_be32(&pdu[19]); + addr = sys_get_be16(&pdu[23]); + + BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x", + net_idx, iv_index, addr); + + prov_buf_init(&msg, PROV_COMPLETE); + if (prov_send(&msg)) { + BT_ERR("Failed to send Provisioning Complete"); + return; + } + + /* Ignore any further PDUs on this link */ + link.expect = 0U; + + /* Store info, since bt_mesh_provision() will end up clearing it */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + identity_enable = is_pb_gatt(); + } else { + identity_enable = false; + } + + err = bt_mesh_provision(pdu, net_idx, flags, iv_index, addr, dev_key); + if (err) { + BT_ERR("Failed to provision (err %d)", err); + return; + } + + /* After PB-GATT provisioning we should start advertising + * using Node Identity. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && identity_enable) { + bt_mesh_proxy_identity_enable(); + } +} + +static void prov_complete(const u8_t *data) +{ + BT_DBG("%s", __func__); +} + +static void prov_failed(const u8_t *data) +{ + BT_WARN("Error: 0x%02x", data[0]); +} + +static const struct { + void (*func)(const u8_t *data); + u16_t len; +} prov_handlers[] = { + { prov_invite, 1 }, + { prov_capabilities, 11 }, + { prov_start, 5, }, + { prov_pub_key, 64 }, + { prov_input_complete, 0 }, + { prov_confirm, 16 }, + { prov_random, 16 }, + { prov_data, 33 }, + { prov_complete, 0 }, + { prov_failed, 1 }, +}; + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void prov_retransmit(struct k_work *work) +{ + s64_t timeout = TRANSACTION_TIMEOUT; + int i; + + BT_DBG("%s", __func__); + + if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + BT_WARN("Link not active"); + return; + } + +#if defined(CONFIG_BLE_MESH_FAST_PROV) + /* When Provisioning Failed PDU is sent, 3s may be used here. */ + if (link.tx_pdu_type >= PROV_COMPLETE) { + timeout = K_SECONDS(30); + } +#endif + if (k_uptime_get() - link.tx.start > timeout) { + BT_WARN("Node timeout, giving up transaction"); + reset_adv_link(); + return; + } + + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { + struct net_buf *buf = link.tx.buf[i]; + + if (!buf) { + break; + } + + if (BLE_MESH_ADV(buf)->busy) { + continue; + } + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) { + bt_mesh_adv_send(buf, NULL, NULL); + } else { + bt_mesh_adv_send(buf, &buf_sent_cb, NULL); + } + + } + + bt_mesh_pb_buf_unlock(); +} + +static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("len %u", buf->len); + + if (buf->len < 16) { + BT_ERR("%s, Too short bearer open message (len %u)", __func__, buf->len); + return; + } + + if (bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + /* Send another link ack if the provisioner missed the last */ + if (link.id == rx->link_id && link.expect == PROV_INVITE) { + BT_DBG("Resending link ack"); + bearer_ctl_send(LINK_ACK, NULL, 0); + } else { + BT_INFO("Ignoring bearer open: link already active"); + } + + return; + } + + if (memcmp(buf->data, prov->uuid, 16)) { + BT_DBG("Bearer open message not for us"); + return; + } + + if (prov->link_open) { + prov->link_open(BLE_MESH_PROV_ADV); + } + + link.id = rx->link_id; + bt_mesh_atomic_set_bit(link.flags, LINK_ACTIVE); + net_buf_simple_reset(link.rx.buf); + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Add the link id into exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link.id); +#endif + + bearer_ctl_send(LINK_ACK, NULL, 0); + + link.expect = PROV_INVITE; +} + +static void link_ack(struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("len %u", buf->len); +} + +static void link_close(struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("len %u", buf->len); + + reset_adv_link(); +} + +static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len); + + switch (BEARER_CTL(rx->gpc)) { + case LINK_OPEN: + link_open(rx, buf); + break; + case LINK_ACK: + if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + return; + } + + link_ack(rx, buf); + break; + case LINK_CLOSE: + if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE)) { + return; + } + + link_close(rx, buf); + break; + default: + BT_ERR("%s, Unknown bearer opcode: 0x%02x", __func__, BEARER_CTL(rx->gpc)); + return; + } +} + +static void prov_msg_recv(void) +{ + u8_t type = link.rx.buf->data[0]; + + BT_DBG("type 0x%02x len %u", type, link.rx.buf->len); + + if (!bt_mesh_fcs_check(link.rx.buf, link.rx.fcs)) { + BT_ERR("%s, Incorrect FCS", __func__); + return; + } + + gen_prov_ack_send(link.rx.id); + link.rx.prev_id = link.rx.id; + link.rx.id = 0U; + + if (bt_mesh_atomic_test_bit(link.flags, LINK_INVALID)) { + BT_WARN("Unexpected msg 0x%02x on invalidated link", type); + prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + return; + } + + if (type != PROV_FAILED && type != link.expect) { + BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); + prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + return; + } + + if (type >= ARRAY_SIZE(prov_handlers)) { + BT_ERR("%s, Unknown provisioning PDU type 0x%02x", __func__, type); + prov_send_fail_msg(PROV_ERR_NVAL_PDU); + return; + } + + if (1 + prov_handlers[type].len != link.rx.buf->len) { + BT_ERR("%s, Invalid length %u for type 0x%02x", + __func__, link.rx.buf->len, type); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + /* Changed by Espressif, add provisioning timeout timer operations. + * When received a provisioning PDU, restart the 60s timer. + */ + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + + prov_handlers[type].func(&link.rx.buf->data[1]); +} + +static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) +{ + u8_t seg = CONT_SEG_INDEX(rx->gpc); + + BT_DBG("len %u, seg_index %u", buf->len, seg); + + if (!link.rx.seg && link.rx.prev_id == rx->xact_id) { + BT_INFO("Resending ack"); + gen_prov_ack_send(rx->xact_id); + return; + } + + if (rx->xact_id != link.rx.id) { + BT_WARN("Data for unknown transaction (%u != %u)", + rx->xact_id, link.rx.id); + return; + } + + if (seg > link.rx.last_seg) { + BT_ERR("%s, Invalid segment index %u", __func__, seg); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } else if (seg == link.rx.last_seg) { + u8_t expect_len = 0U; + + expect_len = (link.rx.buf->len - 20U - + ((link.rx.last_seg - 1) * 23U)); + if (expect_len != buf->len) { + BT_ERR("%s, Incorrect last seg len: %u != %u", + __func__, expect_len, buf->len); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + } + + if (!(link.rx.seg & BIT(seg))) { + BT_INFO("Ignoring already received segment"); + return; + } + + memcpy(XACT_SEG_DATA(seg), buf->data, buf->len); + XACT_SEG_RECV(seg); + + if (!link.rx.seg) { + prov_msg_recv(); + } +} + +static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("len %u", buf->len); + + if (!link.tx.buf[0]) { + return; + } + + if (rx->xact_id == link.tx.id) { + prov_clear_tx(); + } +} + +static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) +{ + if (link.rx.seg) { + BT_INFO("Got Start while there are unreceived segments"); + return; + } + + if (link.rx.prev_id == rx->xact_id) { + BT_INFO("Resending ack"); + gen_prov_ack_send(rx->xact_id); + return; + } + + link.rx.buf->len = net_buf_simple_pull_be16(buf); + link.rx.id = rx->xact_id; + link.rx.fcs = net_buf_simple_pull_u8(buf); + + BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, + START_LAST_SEG(rx->gpc), link.rx.buf->len, link.rx.fcs); + + if (link.rx.buf->len < 1) { + BT_ERR("%s, Ignoring zero-length provisioning PDU", __func__); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + if (link.rx.buf->len > link.rx.buf->size) { + BT_ERR("%s, Too large provisioning PDU (%u bytes)", + __func__, link.rx.buf->len); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + if (START_LAST_SEG(rx->gpc) > 0 && link.rx.buf->len <= 20U) { + BT_ERR("%s, Too small total length for multi-segment PDU", __func__); + prov_send_fail_msg(PROV_ERR_NVAL_FMT); + return; + } + + link.rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; + link.rx.last_seg = START_LAST_SEG(rx->gpc); + memcpy(link.rx.buf->data, buf->data, buf->len); + XACT_SEG_RECV(0); + + if (!link.rx.seg) { + prov_msg_recv(); + } +} + +static const struct { + void (*func)(struct prov_rx *rx, struct net_buf_simple *buf); + bool require_link; + u8_t min_len; +} gen_prov[] = { + { gen_prov_start, true, 3 }, + { gen_prov_ack, true, 0 }, + { gen_prov_cont, true, 0 }, + { gen_prov_ctl, false, 0 }, +}; + +static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf) +{ + if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) { + BT_ERR("%s, Too short GPC message type %u", __func__, GPCF(rx->gpc)); + return; + } + + if (!bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE) && + gen_prov[GPCF(rx->gpc)].require_link) { + BT_DBG("Ignoring message that requires active link"); + return; + } + + gen_prov[GPCF(rx->gpc)].func(rx, buf); +} + +void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) +{ + struct prov_rx rx = {0}; + + if (!bt_prov_active() && bt_mesh_is_provisioned()) { + BT_DBG("Ignoring provisioning PDU - already provisioned"); + return; + } + + if (buf->len < 6) { + BT_WARN("Too short provisioning packet (len %u)", buf->len); + return; + } + + rx.link_id = net_buf_simple_pull_be32(buf); + rx.xact_id = net_buf_simple_pull_u8(buf); + rx.gpc = net_buf_simple_pull_u8(buf); + + BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id); + + if (bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE) && link.id != rx.link_id) { + BT_DBG("Ignoring mesh beacon for unknown link"); + return; + } + + gen_prov_recv(&rx, buf); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) +{ + u8_t type = 0U; + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (link.conn != conn) { + BT_WARN("Data for unexpected connection"); + return -ENOTCONN; + } + + if (buf->len < 1) { + BT_WARN("Too short provisioning packet (len %u)", buf->len); + return -EINVAL; + } + + type = net_buf_simple_pull_u8(buf); + if (type != PROV_FAILED && type != link.expect) { + BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); + prov_send_fail_msg(PROV_ERR_UNEXP_PDU); + return -EINVAL; + } + + if (type >= ARRAY_SIZE(prov_handlers)) { + BT_ERR("%s, Unknown provisioning PDU type 0x%02x", __func__, type); + return -EINVAL; + } + + if (prov_handlers[type].len != buf->len) { + BT_ERR("%s, Invalid length %u for type 0x%02x", __func__, buf->len, type); + return -EINVAL; + } + + /* Changed by Espressif, add provisioning timeout timer operations. + * When received a provisioning PDU, restart the 60s timer. + */ + k_delayed_work_submit(&link.prot_timer, PROTOCOL_TIMEOUT); + + prov_handlers[type].func(buf->data); + + return 0; +} + +int bt_mesh_pb_gatt_open(struct bt_mesh_conn *conn) +{ + BT_DBG("conn %p", conn); + + if (bt_mesh_atomic_test_and_set_bit(link.flags, LINK_ACTIVE)) { + return -EBUSY; + } + + link.conn = bt_mesh_conn_ref(conn); + link.expect = PROV_INVITE; + + if (prov->link_open) { + prov->link_open(BLE_MESH_PROV_GATT); + } + + return 0; +} + +int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn) +{ + BT_DBG("conn %p", conn); + + if (link.conn != conn) { + BT_ERR("%s, Not connected", __func__); + return -ENOTCONN; + } + + if (prov->link_close) { + prov->link_close(BLE_MESH_PROV_GATT); + } + + reset_state(); + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +const struct bt_mesh_prov *bt_mesh_prov_get(void) +{ + return prov; +} + +bool bt_prov_active(void) +{ + return bt_mesh_atomic_test_bit(link.flags, LINK_ACTIVE); +} + +static void protocol_timeout(struct k_work *work) +{ + BT_WARN("Protocol timeout"); + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (link.conn) { + bt_mesh_pb_gatt_close(link.conn); + return; + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_ADV) + u8_t reason = CLOSE_REASON_TIMEOUT; + + link.rx.seg = 0U; + bearer_ctl_send(LINK_CLOSE, &reason, sizeof(reason)); + + reset_state(); +#endif +} + +int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info) +{ + const u8_t *key = NULL; + + if (!prov_info) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + + if (prov_info->static_val_len > BLE_MESH_PROV_STATIC_OOB_MAX_LEN || + prov_info->output_size > BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN || + prov_info->input_size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { + BT_ERR("%s, Invalid auth oob length", __func__); + return -EINVAL; + } + + __ASSERT(prov_info->uuid, "%s, Device UUID is not initialized", __func__); + + /* Changed by Espressif. Use micro-ecc to generate public key now. */ + key = bt_mesh_pub_key_get(); + if (!key) { + BT_ERR("%s, Failed to generate public key", __func__); + return -EIO; + } + + k_delayed_work_init(&link.prot_timer, protocol_timeout); + + prov = prov_info; + +#if defined(CONFIG_BLE_MESH_PB_ADV) + k_delayed_work_init(&link.tx.retransmit, prov_retransmit); +#endif + + reset_state(); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_buf_mutex_new(); +#endif + + return 0; +} + +int bt_mesh_prov_deinit(void) +{ + if (prov == NULL) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + + k_delayed_work_free(&link.prot_timer); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(); + k_delayed_work_free(&link.tx.retransmit); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link.id); +#endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + (void)memset(&link, 0, sizeof(link)); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_buf_mutex_free(); +#endif + + prov = NULL; + + return 0; +} + +void bt_mesh_prov_complete(u16_t net_idx, const u8_t net_key[16], u16_t addr, u8_t flags, u32_t iv_index) +{ + if (prov->complete) { + prov->complete(net_idx, net_key, addr, flags, iv_index); + } +} + +void bt_mesh_prov_reset(void) +{ + if (prov->reset) { + prov->reset(); + } +} + +#endif /* CONFIG_BLE_MESH_NODE */ diff --git a/components/bt/esp_ble_mesh/mesh_core/prov.h b/components/bt/esp_ble_mesh/mesh_core/prov.h new file mode 100644 index 000000000..a1872bd84 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/prov.h @@ -0,0 +1,42 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef _PROV_H_ +#define _PROV_H_ + +#include "mesh_main.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void bt_mesh_pb_adv_recv(struct net_buf_simple *buf); + +bool bt_prov_active(void); + +int bt_mesh_pb_gatt_open(struct bt_mesh_conn *conn); +int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn); +int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); + +int bt_mesh_set_oob_pub_key(const u8_t pub_key_x[32], const u8_t pub_key_y[32], + const u8_t pri_key[32]); + +const struct bt_mesh_prov *bt_mesh_prov_get(void); + +int bt_mesh_prov_init(const struct bt_mesh_prov *prov); +int bt_mesh_prov_deinit(void); + +void bt_mesh_prov_complete(u16_t net_idx, const u8_t net_key[16], u16_t addr, u8_t flags, u32_t iv_index); +void bt_mesh_prov_reset(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROV_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c new file mode 100644 index 000000000..58feece1f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.c @@ -0,0 +1,1670 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "mesh.h" +#include "crypto.h" +#include "adv.h" +#include "access.h" +#include "settings.h" +#include "friend.h" +#include "transport.h" +#include "mesh_common.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#if CONFIG_BLE_MESH_PROVISIONER + +static struct bt_mesh_node *mesh_nodes[CONFIG_BLE_MESH_MAX_PROV_NODES]; +static bt_mesh_mutex_t provisioner_lock; +static u16_t node_count; + +static int provisioner_remove_node(u16_t index, bool erase); + +static void bt_mesh_provisioner_mutex_new(void) +{ + if (!provisioner_lock.mutex) { + bt_mesh_mutex_create(&provisioner_lock); + } +} + +static void bt_mesh_provisioner_mutex_free(void) +{ + bt_mesh_mutex_free(&provisioner_lock); +} + +static void bt_mesh_provisioner_lock(void) +{ + bt_mesh_mutex_lock(&provisioner_lock); +} + +static void bt_mesh_provisioner_unlock(void) +{ + bt_mesh_mutex_unlock(&provisioner_lock); +} + +int bt_mesh_provisioner_init(void) +{ + bt_mesh_provisioner_mutex_new(); + + return 0; +} + +/** + * When a Provisioner tries to create a network, it will check the + * status of the restored network keys firstly, and try to create + * one if they are not existed. + */ +int bt_mesh_provisioner_net_create(void) +{ + const struct bt_mesh_prov *prov = NULL; + struct bt_mesh_subnet *sub = NULL; + u8_t p_key[16] = {0}; + + BT_DBG("%s", __func__); + + prov = bt_mesh_provisioner_get_prov_info(); + if (!prov) { + BT_ERR("%s, NULL provisioning context", __func__); + return -EINVAL; + } + + /* If the device only acts as a Provisioner, need to initialize + * each element's address. + */ + bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); + + if (bt_mesh.p_sub[0]) { + /* Provisioner is already enabled (enable -> disable -> enable), + * or Provisioner is restored from flash. + */ + BT_INFO("Provisioner already created network"); + sub = bt_mesh.p_sub[0]; + goto done; + } + + /* Generate the primary netkey */ + if (bt_mesh_rand(p_key, 16)) { + BT_ERR("%s, Failed to generate Primary NetKey", __func__); + return -EIO; + } + + sub = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); + if (!sub) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sub->kr_flag = BLE_MESH_KEY_REFRESH(prov->flags); + if (sub->kr_flag) { + if (bt_mesh_net_keys_create(&sub->keys[1], p_key)) { + BT_ERR("%s, Failed to generate net-related keys", __func__); + bt_mesh_free(sub); + return -EIO; + } + sub->kr_phase = BLE_MESH_KR_PHASE_2; + } else { + /* Currently provisioner only use keys[0] */ + if (bt_mesh_net_keys_create(&sub->keys[0], p_key)) { + BT_ERR("%s, Failed to create net-related keys", __func__); + bt_mesh_free(sub); + return -EIO; + } + sub->kr_phase = BLE_MESH_KR_NORMAL; + } + sub->net_idx = BLE_MESH_KEY_PRIMARY; + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + + bt_mesh.p_sub[0] = sub; + + /* Dynamically added appkey & netkey will use these key_idx */ + bt_mesh.p_app_idx_next = 0x0000; + bt_mesh.p_net_idx_next = 0x0001; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_net_idx(); + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_subnet(bt_mesh.p_sub[0]); + } + + bt_mesh.iv_index = prov->iv_index; + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, + BLE_MESH_IV_UPDATE(prov->flags)); + + /* Set minimum required hours, since the 96-hour minimum requirement + * doesn't apply straight after provisioning (since we can't know how + * long has actually passed since the network changed its state). + * This operation is the same with node initialization. + */ + bt_mesh.ivu_duration = BLE_MESH_IVU_MIN_HOURS; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_iv(true); + } + +done: + BT_INFO("NetKey Index 0x%03x, NID 0x%02x", sub->net_idx, sub->keys[0].nid); + BT_INFO("NetKey %s", bt_hex(sub->keys[0].net, 16)); + + return 0; +} + +int bt_mesh_provisioner_deinit(bool erase) +{ + int i; + + for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT; i++) { + if (bt_mesh.p_sub[i]) { + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_subnet(bt_mesh.p_sub[i]); + } + bt_mesh_free(bt_mesh.p_sub[i]); + bt_mesh.p_sub[i] = NULL; + } + } + + for (i = 0; i < CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT; i++) { + if (bt_mesh.p_app_keys[i]) { + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_app_key(bt_mesh.p_app_keys[i]); + } + bt_mesh_free(bt_mesh.p_app_keys[i]); + bt_mesh.p_app_keys[i] = NULL; + } + } + + bt_mesh.p_net_idx_next = 0U; + bt_mesh.p_app_idx_next = 0U; + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_net_idx(); + bt_mesh_clear_p_app_idx(); + } + + for (i = 0; i < CONFIG_BLE_MESH_MAX_PROV_NODES; i++) { + provisioner_remove_node(i, erase); + } + + node_count = 0U; + + bt_mesh_provisioner_mutex_free(); + + return 0; +} + +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own) +{ + const struct bt_mesh_comp *comp = NULL; + struct bt_mesh_node *node = NULL; + u16_t primary_addr = BLE_MESH_ADDR_UNASSIGNED; + u16_t comp_addr = BLE_MESH_ADDR_UNASSIGNED; + int i; + + if (comp_with_own) { + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("NULL composition data"); + return true; + } + + primary_addr = bt_mesh_provisioner_get_primary_elem_addr(); + if (!BLE_MESH_ADDR_IS_UNICAST(primary_addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, primary_addr); + return true; + } + } + + for (comp_addr = addr; comp_addr < addr + elem_num; comp_addr++) { + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && comp_addr >= node->unicast_addr && + comp_addr < node->unicast_addr + node->element_num) { + BT_ERR("Duplicate with node address 0x%04x", comp_addr); + return true; + } + + if (comp_with_own && comp_addr >= primary_addr && + comp_addr < primary_addr + comp->elem_count) { + BT_ERR("Duplicate with Provisioner address 0x%04x", comp_addr); + return true; + } + } + } + + return false; +} + +static void provisioner_node_count_inc(void) +{ + node_count++; +} + +static void provisioner_node_count_dec(void) +{ + if (node_count) { + node_count--; + } +} + +u16_t bt_mesh_provisioner_get_node_count(void) +{ + return node_count; +} + +static int provisioner_store_node(struct bt_mesh_node *node, bool store, u16_t *index) +{ + int i; + + bt_mesh_provisioner_lock(); + + /* Check if the node already exists */ + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, node->dev_uuid, 16)) { + BT_WARN("Node already exists, uuid %s", bt_hex(node->dev_uuid, 16)); + bt_mesh_provisioner_unlock(); + return -EEXIST; + } + } + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] == NULL) { + mesh_nodes[i] = bt_mesh_calloc(sizeof(struct bt_mesh_node)); + if (!mesh_nodes[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_provisioner_unlock(); + return -ENOMEM; + } + + memcpy(mesh_nodes[i], node, sizeof(struct bt_mesh_node)); + provisioner_node_count_inc(); + if (index) { + *index = i; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_node_info(mesh_nodes[i]); + } + + bt_mesh_provisioner_unlock(); + return 0; + } + } + + BT_ERR("Node is full!"); + bt_mesh_provisioner_unlock(); + return -ENOMEM; +} + +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node) +{ + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + return provisioner_store_node(node, false, NULL); +} + +int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, + u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, + u32_t iv_index, const u8_t dev_key[16], u16_t *index) +{ + struct bt_mesh_node node = {0}; + + BT_DBG("%s", __func__); + + if (!addr || !uuid || !dev_key || !index) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_INFO("unicast_addr 0x%04x, elem_num %d, net_idx 0x%04x", + unicast_addr, element_num, net_idx); + BT_INFO("dev_uuid %s", bt_hex(uuid, 16)); + BT_INFO("dev_key %s", bt_hex(dev_key, 16)); + + memcpy(node.addr, addr->val, BLE_MESH_ADDR_LEN); + node.addr_type = addr->type; + memcpy(node.dev_uuid, uuid, 16); + node.oob_info = oob_info; + node.unicast_addr = unicast_addr; + node.element_num = element_num; + node.net_idx = net_idx; + node.flags = flags; + node.iv_index = iv_index; + memcpy(node.dev_key, dev_key, 16); + + return provisioner_store_node(&node, true, index); +} + +static int provisioner_remove_node(u16_t index, bool erase) +{ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("Remove node %d", index); + + bt_mesh_provisioner_lock(); + + if (mesh_nodes[index] == NULL) { + bt_mesh_provisioner_unlock(); + return 0; + } + + node = mesh_nodes[index]; + + /* Reset corresponding network cache when reset the node */ + bt_mesh_msg_cache_clear(node->unicast_addr, node->element_num); + + /* Reset corresponding transport info when removing the node */ + for (i = 0; i < node->element_num; i++) { + bt_mesh_rx_reset_single(node->unicast_addr + i); + } + for (i = 0; i < node->element_num; i++) { + bt_mesh_tx_reset_single(node->unicast_addr + i); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_remove_lpn(node->unicast_addr); + } + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_node_info(node->unicast_addr); + } + + if (mesh_nodes[index]->comp_data) { + bt_mesh_free(mesh_nodes[index]->comp_data); + } + bt_mesh_free(mesh_nodes[index]); + mesh_nodes[index] = NULL; + + provisioner_node_count_dec(); + + bt_mesh_provisioner_unlock(); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_uuid(const u8_t uuid[16], u16_t *index) +{ + int i; + + BT_DBG("%s", __func__); + + if (uuid == NULL) { + BT_ERR("%s, Invalid device uuid", __func__); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && !memcmp(mesh_nodes[i]->dev_uuid, uuid, 16)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return mesh_nodes[i]; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +int bt_mesh_provisioner_remove_node(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + int i; + + if (uuid == NULL) { + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + provisioner_remove_node(i, true); + } + } + return 0; + } + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("%s, Node not exists, uuid %s", __func__, bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +static struct bt_mesh_node *provisioner_find_node_with_addr(u16_t addr, u16_t *index) +{ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); + return NULL; + } + + bt_mesh_provisioner_lock(); + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && addr >= node->unicast_addr && + addr < (node->unicast_addr + node->element_num)) { + if (index) { + *index = i; + } + bt_mesh_provisioner_unlock(); + return node; + } + } + + bt_mesh_provisioner_unlock(); + return NULL; +} + +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name) +{ + struct bt_mesh_node *node = NULL; + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node not exists, addr 0x%04x", __func__, addr); + return -ENODEV; + } + + strncpy(node->name, name, BLE_MESH_NODE_NAME_SIZE); + node->name[BLE_MESH_NODE_NAME_SIZE] = 0; + + return 0; +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]) +{ + return provisioner_find_node_with_uuid(uuid, NULL); +} + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr) +{ + return provisioner_find_node_with_addr(unicast_addr, NULL); +} + +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + node = provisioner_find_node_with_uuid(uuid, &index); + if (!node) { + BT_WARN("%s, Node not exists, uuid %s", __func__, bt_hex(uuid, 16)); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +int bt_mesh_provisioner_delete_node_with_node_addr(u16_t unicast_addr) +{ + struct bt_mesh_node *node = NULL; + u16_t index = 0U; + + node = provisioner_find_node_with_addr(unicast_addr, &index); + if (!node) { + BT_WARN("%s, Node not exists, addr 0x%04x", __func__, unicast_addr); + return -ENODEV; + } + + provisioner_remove_node(index, true); + return 0; +} + +int bt_mesh_provisioner_delete_node_with_dev_addr(const bt_mesh_addr_t *addr) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i] && mesh_nodes[i]->addr_type == addr->type && + !memcmp(mesh_nodes[i]->addr, addr->val, BLE_MESH_ADDR_LEN)) { + return provisioner_remove_node(i, true); + } + } + + BT_WARN("Node not exist, device address %s", bt_hex(addr->val, BLE_MESH_ADDR_LEN)); + return -ENODEV; +} + +static int provisioner_check_node_index(u16_t index) +{ + BT_DBG("%s", __func__); + + if (index >= ARRAY_SIZE(mesh_nodes)) { + BT_ERR("%s, Too big node index %d", __func__, index); + return -EINVAL; + } + + if (mesh_nodes[index] == NULL) { + BT_ERR("%s, Node not exists, index %d", __func__, index); + return -ENODEV; + } + + return 0; +} + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name) +{ + size_t length = 0U, name_len = 0U; + int i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (provisioner_check_node_index(index)) { + return -EINVAL; + } + + BT_DBG("len %d, name %s", strlen(name), name); + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + BT_WARN("Node name %s exists", name); + return -EEXIST; + } + } + } + + memset(mesh_nodes[index]->name, 0, sizeof(mesh_nodes[index]->name)); + strncpy(mesh_nodes[index]->name, name, length); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_node_name(mesh_nodes[index]); + } + + return 0; +} + +const char *bt_mesh_provisioner_get_node_name(u16_t index) +{ + BT_DBG("%s", __func__); + + if (provisioner_check_node_index(index)) { + return NULL; + } + + return mesh_nodes[index]->name; +} + +u16_t bt_mesh_provisioner_get_node_index(const char *name) +{ + size_t length = 0U, name_len = 0U; + int i; + + BT_DBG("%s", __func__); + + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return BLE_MESH_INVALID_NODE_INDEX; + } + + length = (strlen(name) <= BLE_MESH_NODE_NAME_SIZE) ? strlen(name) : BLE_MESH_NODE_NAME_SIZE; + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + if (mesh_nodes[i]) { + name_len = strlen(mesh_nodes[i]->name); + if (length != name_len) { + continue; + } + if (!strncmp(mesh_nodes[i]->name, name, length)) { + return (u16_t)i; + } + } + } + + BT_ERR("Node name %s not exists", name); + return BLE_MESH_INVALID_NODE_INDEX; +} + +#define COMP_DATA_PAGE_0_MIN_LEN 16 + +static int store_node_comp_data(u16_t addr, const u8_t *data, u16_t length, bool store) +{ + struct bt_mesh_node *node = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, addr); + return -EINVAL; + } + + if (data == NULL || (length % 2) || length < COMP_DATA_PAGE_0_MIN_LEN) { + BT_ERR("%s, Invalid composition data", __func__); + return -EINVAL; + } + + node = provisioner_find_node_with_addr(addr, NULL); + if (node == NULL) { + BT_ERR("%s, Node not exists, addr 0x%04x", __func__, addr); + return -ENODEV; + } + + node->comp_data = bt_mesh_calloc(length); + if (node->comp_data == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + memcpy(node->comp_data, data, length); + node->comp_length = length; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) { + bt_mesh_store_node_comp_data(node); + } + + return 0; +} + +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +{ + return store_node_comp_data(addr, data, length, true); +} + +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length) +{ + return store_node_comp_data(addr, data, length, false); +} + +/* Provisioner DevKey, NetKey and AppKey related functions */ + +const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + if (sub->kr_flag) { + return sub->keys[1].net; + } else { + return sub->keys[0].net; + } + } + } + + return NULL; +} + +struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + BT_DBG("%s", __func__); + + if (net_idx == BLE_MESH_KEY_ANY) { + return bt_mesh.p_sub[0]; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + return sub; + } + } + + return NULL; +} + +bool bt_mesh_provisioner_check_msg_dst(u16_t dst) +{ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + return true; + } + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && dst >= node->unicast_addr && + dst < node->unicast_addr + node->element_num) { + return true; + } + } + + return false; +} + +const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst) +{ + /* Device key is only used to encrypt configuration messages. + * Configuration model shall only be supported by the primary + * element which uses the primary unicast address. + */ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && node->unicast_addr == dst) { + return node->dev_key; + } + } + + return NULL; +} + +struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + return NULL; +} + +static int provisioner_check_app_key(const u8_t app_key[16], u16_t *app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + if (!app_key) { + return 0; + } + + /* Check if app_key is already existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && (!memcmp(key->keys[0].val, app_key, 16) || + !memcmp(key->keys[1].val, app_key, 16))) { + *app_idx = key->app_idx; + return -EEXIST; + } + } + + return 0; +} + +static int provisioner_check_app_idx(u16_t app_idx, bool exist) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + if (exist) { + /* Check if app_idx is already existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && (key->app_idx == app_idx)) { + return -EEXIST; + } + } + return 0; + } + + /* Check if app_idx is not existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && (key->app_idx == app_idx)) { + return 0; + } + } + + return -ENODEV; +} + +static int provisioner_check_app_key_full(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + if (!bt_mesh.p_app_keys[i]) { + return i; + } + } + + return -ENOMEM; +} + +static int provisioner_check_net_key(const u8_t net_key[16], u16_t *net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + if (!net_key) { + return 0; + } + + /* Check if net_key is already existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && (!memcmp(sub->keys[0].net, net_key, 16) || + !memcmp(sub->keys[1].net, net_key, 16))) { + *net_idx = sub->net_idx; + return -EEXIST; + } + } + + return 0; +} + +static int provisioner_check_net_idx(u16_t net_idx, bool exist) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + if (exist) { + /* Check if net_idx is already existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && (sub->net_idx == net_idx)) { + return -EEXIST; + } + } + return 0; + } + + /* Check if net_idx is not existed */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && (sub->net_idx == net_idx)) { + return 0; + } + } + + return -ENODEV; +} + +static int provisioner_check_net_key_full(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (!bt_mesh.p_sub[i]) { + return i; + } + } + + return -ENOMEM; +} + +int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx) +{ + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + u8_t p_key[16] = {0}; + int add = -1; + + if (bt_mesh.p_app_idx_next >= 0x1000) { + BT_ERR("No AppKey Index available"); + return -EIO; + } + + if (!app_idx || (*app_idx != 0xFFFF && *app_idx >= 0x1000)) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Check if the same application key already exists */ + if (provisioner_check_app_key(app_key, app_idx)) { + BT_WARN("AppKey exists, AppKey Index updated"); + return 0; + } + + /* Check if the net_idx exists */ + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return -ENODEV; + } + + /* Check if the same app_idx already exists */ + if (provisioner_check_app_idx(*app_idx, true)) { + BT_ERR("%s, AppKey Index 0x%03x exists", __func__, *app_idx); + return -EEXIST; + } + + add = provisioner_check_app_key_full(); + if (add < 0) { + BT_ERR("AppKey is full!"); + return -ENOMEM; + } + + if (!app_key) { + if (bt_mesh_rand(p_key, 16)) { + BT_ERR("Failed to generate AppKey"); + return -EIO; + } + } else { + memcpy(p_key, app_key, 16); + } + + key = bt_mesh_calloc(sizeof(struct bt_mesh_app_key)); + if (!key) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + keys = &key->keys[0]; + if (bt_mesh_app_id(p_key, &keys->id)) { + BT_ERR("%s, Failed to generate AID", __func__); + bt_mesh_free(key); + return -EIO; + } + + memcpy(keys->val, p_key, 16); + key->net_idx = net_idx; + if (*app_idx != 0xFFFF) { + key->app_idx = *app_idx; + } else { + key->app_idx = bt_mesh.p_app_idx_next; + while (1) { + if (provisioner_check_app_idx(key->app_idx, true)) { + key->app_idx = (++bt_mesh.p_app_idx_next); + if (key->app_idx >= 0x1000) { + BT_ERR("No AppKey Index available"); + bt_mesh_free(key); + return -EIO; + } + } else { + break; + } + } + *app_idx = key->app_idx; + } + key->updated = false; + + bt_mesh.p_app_keys[add] = key; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_app_key(key); + } + + return 0; +} + +int bt_mesh_provisioner_local_app_key_update(const u8_t app_key[16], u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + + if (app_key == NULL) { + BT_ERR("%s, Invalid AppKey", __func__); + return -EINVAL; + } + + BT_INFO("AppKey %s, net_idx 0x%03x, app_idx 0x%03x", bt_hex(app_key, 16), net_idx, app_idx); + + /* Check if the net_idx exists */ + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return -ENODEV; + } + + key = bt_mesh_provisioner_app_key_find(app_idx); + if (key == NULL) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return -ENODEV; + } + + keys = &key->keys[0]; + if (bt_mesh_app_id(app_key, &keys->id)) { + BT_ERR("%s, Failed to generate AID", __func__); + return -EIO; + } + + memset(keys->val, 0, 16); + memcpy(keys->val, app_key, 16); + + key->updated = false; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_app_idx(); + bt_mesh_store_p_app_key(key); + } + + return 0; +} + +const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + BT_DBG("%s", __func__); + + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return NULL; + } + + if (provisioner_check_app_idx(app_idx, false)) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && key->net_idx == net_idx && + key->app_idx == app_idx) { + if (key->updated) { + return key->keys[1].val; + } + return key->keys[0].val; + } + } + + return NULL; +} + +static void model_pub_clear(struct bt_mesh_model *model) +{ + if (!model->pub) { + return; + } + + if (model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->key = 0U; + model->pub->cred = 0U; + model->pub->ttl = 0U; + model->pub->period = 0U; + model->pub->retransmit = 0U; + model->pub->count = 0U; + + if (model->pub->update) { + k_delayed_work_cancel(&model->pub->timer); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_pub(model); + } + + return; +} + +static void model_unbind(struct bt_mesh_model *model, u16_t app_idx) +{ + int i; + + BT_DBG("model %p app_idx 0x%03x", model, app_idx); + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] != app_idx) { + continue; + } + + model->keys[i] = BLE_MESH_KEY_UNUSED; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_bind(model); + } + + model_pub_clear(model); + } +} + +static void _model_unbind(struct bt_mesh_model *mod, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + u16_t app_idx = *(u16_t *)user_data; + + model_unbind(mod, app_idx); +} + +int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + BT_DBG("%s", __func__); + + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return -ENODEV; + } + + if (provisioner_check_app_idx(app_idx, false)) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return -ENODEV; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && key->net_idx == net_idx && + key->app_idx == app_idx) { + /* Remove the AppKey from the models if they are bound with it */ + bt_mesh_model_foreach(_model_unbind, &app_idx); + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_app_key(key); + } + + bt_mesh_free(bt_mesh.p_app_keys[i]); + bt_mesh.p_app_keys[i] = NULL; + return 0; + } + } + + /* Shall never reach here */ + return -ENODEV; +} + +int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + u8_t p_key[16] = {0}; + int add = -1; + + if (bt_mesh.p_net_idx_next >= 0x1000) { + BT_ERR("No NetKey Index available"); + return -EIO; + } + + if (!net_idx || (*net_idx != 0xFFFF && *net_idx >= 0x1000)) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Check if the same network key already exists */ + if (provisioner_check_net_key(net_key, net_idx)) { + BT_WARN("NetKey exists, NetKey Index updated"); + return 0; + } + + /* Check if the same net_idx already exists */ + if (provisioner_check_net_idx(*net_idx, true)) { + BT_ERR("%s, NetKey Index 0x%03x exists", __func__, *net_idx); + return -EEXIST; + } + + add = provisioner_check_net_key_full(); + if (add < 0) { + BT_ERR("NetKey is full!"); + return -ENOMEM; + } + + if (!net_key) { + if (bt_mesh_rand(p_key, 16)) { + BT_ERR("Failed to generate NetKey"); + return -EIO; + } + } else { + memcpy(p_key, net_key, 16); + } + + sub = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); + if (!sub) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + if (bt_mesh_net_keys_create(&sub->keys[0], p_key)) { + BT_ERR("%s, Failed to generate NID", __func__); + bt_mesh_free(sub); + return -EIO; + } + + if (*net_idx != 0xFFFF) { + sub->net_idx = *net_idx; + } else { + sub->net_idx = bt_mesh.p_net_idx_next; + while (1) { + if (provisioner_check_net_idx(sub->net_idx, true)) { + sub->net_idx = (++bt_mesh.p_net_idx_next); + if (sub->net_idx >= 0x1000) { + BT_ERR("No NetKey Index available"); + bt_mesh_free(sub); + return -EIO; + } + } else { + break; + } + } + *net_idx = sub->net_idx; + } + sub->kr_phase = BLE_MESH_KR_NORMAL; + sub->kr_flag = false; + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + + bt_mesh.p_sub[add] = sub; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_net_idx(); + bt_mesh_store_p_subnet(sub); + } + + return 0; +} + +int bt_mesh_provisioner_local_net_key_update(const u8_t net_key[16], u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int err = 0; + + if (net_key == NULL) { + BT_ERR("%s, Invalid NetKey", __func__); + return -EINVAL; + } + + BT_INFO("NetKey %s, net_idx 0x%03x", bt_hex(net_key, 16), net_idx); + + sub = bt_mesh_provisioner_subnet_get(net_idx); + if (sub == NULL) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return -ENODEV; + } + + err = bt_mesh_net_keys_create(&sub->keys[0], net_key); + if (err) { + BT_ERR("%s, Failed to generate NID", __func__); + return -EIO; + } + + memset(sub->keys[0].net, 0, 16); + memcpy(sub->keys[0].net, net_key, 16); + + sub->kr_phase = BLE_MESH_KR_NORMAL; + sub->kr_flag = false; + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + + err = bt_mesh_net_beacon_update(sub); + if (err) { + BT_ERR("%s, Failed to update secure beacon", __func__); + return -EIO; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_p_subnet(sub); + } + + return 0; +} + +const u8_t *bt_mesh_provisioner_local_net_key_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + BT_DBG("%s", __func__); + + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return NULL; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + if (sub->kr_flag) { + return sub->keys[1].net; + } + return sub->keys[0].net; + } + } + + return NULL; +} + +int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i, j; + + BT_DBG("%s", __func__); + + if (provisioner_check_net_idx(net_idx, false)) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return -ENODEV; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + /* Delete any app keys bound to this NetKey index */ + for (j = 0; j < ARRAY_SIZE(bt_mesh.p_app_keys); j++) { + struct bt_mesh_app_key *key = bt_mesh.p_app_keys[j]; + if (key && key->net_idx == sub->net_idx) { + bt_mesh_provisioner_local_app_key_delete(key->net_idx, key->app_idx); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_p_subnet(sub); + } + + bt_mesh_free(bt_mesh.p_sub[i]); + bt_mesh.p_sub[i] = NULL; + return 0; + } + } + + /* Shall never reach here */ + return -ENODEV; +} + +int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, + u16_t cid, u16_t app_idx) +{ + struct bt_mesh_model *model = NULL; + struct bt_mesh_elem *elem = NULL; + int i; + + elem = bt_mesh_elem_find(elem_addr); + if (!elem) { + BT_ERR("%s, No element found, addr 0x%04x", __func__, elem_addr); + return -ENODEV; + } + + if (cid == 0xFFFF) { + model = bt_mesh_model_find(elem, mod_id); + } else { + model = bt_mesh_model_find_vnd(elem, cid, mod_id); + } + if (!model) { + BT_ERR("%s, No model is found", __func__); + return -ENODEV; + } + + if (provisioner_check_app_idx(app_idx, false)) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return -ENODEV; + } + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] == app_idx) { + BT_INFO("AppKey 0x%03x already bound to model", app_idx); + return 0; + } + } + + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + if (model->keys[i] == BLE_MESH_KEY_UNUSED) { + model->keys[i] = app_idx; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_mod_bind(model); + } + return 0; + } + } + + BT_ERR("Model bound is full!"); + return -ENOMEM; +} + +int bt_mesh_print_local_composition_data(void) +{ + const struct bt_mesh_comp *comp = NULL; + struct bt_mesh_model *model = NULL; + struct bt_mesh_elem *elem = NULL; + int i, j; + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("%s, NULL composition data", __func__); + return -EINVAL; + } + + BT_INFO("************************************************"); + BT_INFO("* cid: 0x%04x pid: 0x%04x vid: 0x%04x *", comp->cid, comp->pid, comp->vid); + BT_INFO("* Element Number: 0x%02x *", comp->elem_count); + for (i = 0; i < comp->elem_count; i++) { + elem = &comp->elem[i]; + BT_INFO("* Element %d: 0x%04x *", i, elem->addr); + BT_INFO("* Loc: 0x%04x NumS: 0x%02x NumV: 0x%02x *", elem->loc, elem->model_count, elem->vnd_model_count); + for (j = 0; j < elem->model_count; j++) { + model = &elem->models[j]; + BT_INFO("* sig_model %d: id - 0x%04x *", j, model->id); + } + for (j = 0; j < elem->vnd_model_count; j++) { + model = &elem->vnd_models[j]; + BT_INFO("* vnd_model %d: id - 0x%04x, cid - 0x%04x *", j, model->vnd.id, model->vnd.company); + } + } + BT_INFO("************************************************"); + + ((void) model); + + return 0; +} + +#if CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node) +{ + int err = 0; + + if (!node) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(node->unicast_addr)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, node->unicast_addr); + return -EINVAL; + } + + if (node->element_num == 0) { + BT_ERR("%s, Invalid element count %d", __func__, node->element_num); + return -EINVAL; + } + + if (bt_mesh_provisioner_check_is_addr_dup(node->unicast_addr, node->element_num, true)) { + BT_ERR("%s, Unicast address 0x%04x is duplicated", __func__, node->unicast_addr); + return -EINVAL; + } + + if (node->unicast_addr + node->element_num - 1 > 0x7FFF) { + BT_ERR("%s, Not enough unicast address for the node", __func__); + return -EIO; + } + + if (bt_mesh_provisioner_net_key_get(node->net_idx) == NULL) { + BT_ERR("%s, Invalid NetKey Index 0x%03x", __func__, node->net_idx); + return -EINVAL; + } + + err = provisioner_store_node(node, true, NULL); + if (err) { + BT_ERR("%s, Failed to store node info", __func__); + return err; + } + + bt_mesh_test_provisioner_update_alloc_addr(node->unicast_addr, node->element_num); + return 0; +} +#endif /* CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK */ + +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +/* The following APIs are for fast provisioning */ + +#if CONFIG_BLE_MESH_FAST_PROV + +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst) +{ + struct bt_mesh_node *node = NULL; + int i; + + BT_DBG("%s", __func__); + + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + BT_ERR("%s, Not a unicast address 0x%04x", __func__, dst); + return NULL; + } + + if (dst == bt_mesh_primary_addr()) { + return bt_mesh.dev_key; + } + + for (i = 0; i < ARRAY_SIZE(mesh_nodes); i++) { + node = mesh_nodes[i]; + if (node && node->unicast_addr == dst) { + return node->dev_key; + } + } + + return NULL; +} + +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; + if (sub->net_idx == net_idx) { + return sub; + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + if (sub && sub->net_idx == net_idx) { + return sub; + } + } + + return NULL; +} + +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + key = &bt_mesh.app_keys[i]; + if (key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + key = bt_mesh.p_app_keys[i]; + if (key && key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + return NULL; +} + +u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx) +{ + struct bt_mesh_subnet_keys *key = NULL; + struct bt_mesh_subnet *sub = NULL; + + sub = bt_mesh_fast_prov_subnet_get(net_idx); + if (sub) { + key = BLE_MESH_KEY_REFRESH(sub->kr_flag) ? &sub->keys[1] : &sub->keys[0]; + return bt_mesh_provisioner_set_fast_prov_net_idx(key->net, net_idx); + } + + /* If net_idx is not found, set net_idx to fast_prov first, + * and wait for primary provisioner to add net_key */ + return bt_mesh_provisioner_set_fast_prov_net_idx(NULL, net_idx); +} + +u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]) +{ + const u8_t *keys = NULL; + u16_t net_idx = 0U; + int err = 0; + + net_idx = bt_mesh_provisioner_get_fast_prov_net_idx(); + bt_mesh.p_net_idx_next = net_idx; + + err = bt_mesh_provisioner_local_net_key_add(net_key, &net_idx); + if (err) { + return 0x01; /* status: add net_key fail */ + }; + + keys = bt_mesh_provisioner_local_net_key_get(net_idx); + if (!keys) { + return 0x01; /* status: add net_key fail */ + } + + return bt_mesh_provisioner_set_fast_prov_net_idx(keys, net_idx); +} + +const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx) +{ + struct bt_mesh_subnet *sub = NULL; + + sub = bt_mesh_fast_prov_subnet_get(net_idx); + if (!sub) { + BT_ERR("%s, NetKey Index 0x%03x not exists", __func__, net_idx); + return NULL; + } + + return (sub->kr_flag ? sub->keys[1].net : sub->keys[0].net); +} + +const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx) +{ + struct bt_mesh_app_key *key = NULL; + + key = bt_mesh_fast_prov_app_key_find(app_idx); + if (!key) { + BT_ERR("%s, AppKey Index 0x%03x not exists", __func__, app_idx); + return NULL; + } + + return (key->updated ? key->keys[1].val : key->keys[0].val); +} + +#endif /* CONFIG_BLE_MESH_FAST_PROV */ diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h new file mode 100644 index 000000000..f5552cd24 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_main.h @@ -0,0 +1,145 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _PROVISIONER_MAIN_H_ +#define _PROVISIONER_MAIN_H_ + +#include "net.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_INVALID_NODE_INDEX 0xFFFF +#define BLE_MESH_NODE_NAME_SIZE 31 + +/* Each node information stored by provisioner */ +struct bt_mesh_node { + /* Device information */ + u8_t addr[6]; /* Node device address */ + u8_t addr_type; /* Node device address type */ + u8_t dev_uuid[16]; /* Node Device UUID */ + u16_t oob_info; /* Node OOB information */ + + /* Provisioning information */ + u16_t unicast_addr; /* Node unicast address */ + u8_t element_num; /* Node element number */ + u16_t net_idx; /* Node NetKey Index */ + u8_t flags; /* Node key refresh flag and iv update flag */ + u32_t iv_index; /* Node IV Index */ + u8_t dev_key[16]; /* Node device key */ + + /* Additional information */ + char name[BLE_MESH_NODE_NAME_SIZE + 1]; /* Node name */ + u16_t comp_length; /* Length of Composition Data */ + u8_t *comp_data; /* Value of Composition Data */ +} __packed; + +int bt_mesh_provisioner_init(void); + +int bt_mesh_provisioner_net_create(void); + +int bt_mesh_provisioner_deinit(bool erase); + +bool bt_mesh_provisioner_check_is_addr_dup(u16_t addr, u8_t elem_num, bool comp_with_own); + +u16_t bt_mesh_provisioner_get_node_count(void); + +int bt_mesh_provisioner_restore_node_info(struct bt_mesh_node *node); + +int bt_mesh_provisioner_provision(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t oob_info, + u16_t unicast_addr, u8_t element_num, u16_t net_idx, u8_t flags, + u32_t iv_index, const u8_t dev_key[16], u16_t *index); + +int bt_mesh_provisioner_remove_node(const u8_t uuid[16]); + +int bt_mesh_provisioner_restore_node_name(u16_t addr, const char *name); + +int bt_mesh_provisioner_restore_node_comp_data(u16_t addr, const u8_t *data, u16_t length); + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_uuid(const u8_t uuid[16]); + +struct bt_mesh_node *bt_mesh_provisioner_get_node_with_addr(u16_t unicast_addr); + +int bt_mesh_provisioner_delete_node_with_uuid(const u8_t uuid[16]); + +int bt_mesh_provisioner_delete_node_with_node_addr(u16_t unicast_addr); + +int bt_mesh_provisioner_delete_node_with_dev_addr(const bt_mesh_addr_t *addr); + +int bt_mesh_provisioner_set_node_name(u16_t index, const char *name); + +const char *bt_mesh_provisioner_get_node_name(u16_t index); + +u16_t bt_mesh_provisioner_get_node_index(const char *name); + +int bt_mesh_provisioner_store_node_comp_data(u16_t addr, const u8_t *data, u16_t length); + +const u8_t *bt_mesh_provisioner_net_key_get(u16_t net_idx); + +struct bt_mesh_subnet *bt_mesh_provisioner_subnet_get(u16_t net_idx); + +bool bt_mesh_provisioner_check_msg_dst(u16_t dst); + +const u8_t *bt_mesh_provisioner_dev_key_get(u16_t dst); + +struct bt_mesh_app_key *bt_mesh_provisioner_app_key_find(u16_t app_idx); + +int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx); + +int bt_mesh_provisioner_local_app_key_update(const u8_t app_key[16], u16_t net_idx, u16_t app_idx); + +const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx); + +int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx); + +int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx); + +int bt_mesh_provisioner_local_net_key_update(const u8_t net_key[16], u16_t net_idx); + +const u8_t *bt_mesh_provisioner_local_net_key_get(u16_t net_idx); + +int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx); + +/* Provisioner bind local client model with proper appkey index */ +int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id, + u16_t cid, u16_t app_idx); + +/* Provisioner print own element information */ +int bt_mesh_print_local_composition_data(void); + +int bt_mesh_provisioner_store_node_info(struct bt_mesh_node *node); + +/* The following APIs are for fast provisioning */ + +const u8_t *bt_mesh_fast_prov_dev_key_get(u16_t dst); + +struct bt_mesh_subnet *bt_mesh_fast_prov_subnet_get(u16_t net_idx); + +struct bt_mesh_app_key *bt_mesh_fast_prov_app_key_find(u16_t app_idx); + +u8_t bt_mesh_set_fast_prov_net_idx(u16_t net_idx); + +u8_t bt_mesh_add_fast_prov_net_key(const u8_t net_key[16]); + +const u8_t *bt_mesh_get_fast_prov_net_key(u16_t net_idx); + +const u8_t *bt_mesh_get_fast_prov_app_key(u16_t net_idx, u16_t app_idx); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROVISIONER_MAIN_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c new file mode 100644 index 000000000..d385eb701 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c @@ -0,0 +1,3529 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "access.h" +#include "settings.h" +#include "mesh_common.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" + +#if CONFIG_BLE_MESH_PROVISIONER + +_Static_assert(BLE_MESH_MAX_CONN >= CONFIG_BLE_MESH_PBG_SAME_TIME, + "Too large BLE Mesh PB-GATT count"); + +/* 3 transmissions, 20ms interval */ +#define PROV_XMIT BLE_MESH_TRANSMIT(2, 20) + +#define AUTH_METHOD_NO_OOB 0x00 +#define AUTH_METHOD_STATIC 0x01 +#define AUTH_METHOD_OUTPUT 0x02 +#define AUTH_METHOD_INPUT 0x03 + +#define OUTPUT_OOB_BLINK 0x00 +#define OUTPUT_OOB_BEEP 0x01 +#define OUTPUT_OOB_VIBRATE 0x02 +#define OUTPUT_OOB_NUMBER 0x03 +#define OUTPUT_OOB_STRING 0x04 + +#define INPUT_OOB_PUSH 0x00 +#define INPUT_OOB_TWIST 0x01 +#define INPUT_OOB_NUMBER 0x02 +#define INPUT_OOB_STRING 0x03 + +#define PROV_ERR_NONE 0x00 +#define PROV_ERR_NVAL_PDU 0x01 +#define PROV_ERR_NVAL_FMT 0x02 +#define PROV_ERR_UNEXP_PDU 0x03 +#define PROV_ERR_CFM_FAILED 0x04 +#define PROV_ERR_RESOURCES 0x05 +#define PROV_ERR_DECRYPT 0x06 +#define PROV_ERR_UNEXP_ERR 0x07 +#define PROV_ERR_ADDR 0x08 + +#define PROV_INVITE 0x00 +#define PROV_CAPABILITIES 0x01 +#define PROV_START 0x02 +#define PROV_PUB_KEY 0x03 +#define PROV_INPUT_COMPLETE 0x04 +#define PROV_CONFIRM 0x05 +#define PROV_RANDOM 0x06 +#define PROV_DATA 0x07 +#define PROV_COMPLETE 0x08 +#define PROV_FAILED 0x09 + +#define PROV_ALG_P256 0x00 + +#define GPCF(gpc) (gpc & 0x03) +#define GPC_START(last_seg) (((last_seg) << 2) | 0x00) +#define GPC_ACK 0x01 +#define GPC_CONT(seg_id) (((seg_id) << 2) | 0x02) +#define GPC_CTL(op) (((op) << 2) | 0x03) + +#define START_PAYLOAD_MAX 20 +#define CONT_PAYLOAD_MAX 23 + +#define START_LAST_SEG(gpc) (gpc >> 2) +#define CONT_SEG_INDEX(gpc) (gpc >> 2) + +#define BEARER_CTL(gpc) (gpc >> 2) +#define LINK_OPEN 0x00 +#define LINK_ACK 0x01 +#define LINK_CLOSE 0x02 + +#define CLOSE_REASON_SUCCESS 0x00 +#define CLOSE_REASON_TIMEOUT 0x01 +#define CLOSE_REASON_FAILED 0x02 + +#define PROV_AUTH_VAL_SIZE 0x10 +#define PROV_CONF_SALT_SIZE 0x10 +#define PROV_CONF_KEY_SIZE 0x10 +#define PROV_DH_KEY_SIZE 0x20 +#define PROV_CONFIRM_SIZE 0x10 +#define PROV_PROV_SALT_SIZE 0x10 +#define PROV_CONF_INPUTS_SIZE 0x91 + +#define XACT_SEG_DATA(_idx, _seg) (&link[_idx].rx.buf->data[20 + ((_seg - 1) * 23)]) +#define XACT_SEG_RECV(_idx, _seg) (link[_idx].rx.seg &= ~(1 << (_seg))) + +#define XACT_NVAL 0xff + +enum { + REMOTE_PUB_KEY, /* Remote key has been received */ + LOCAL_PUB_KEY, /* Local public key is available */ + LINK_ACTIVE, /* Link has been opened */ + WAIT_GEN_DHKEY, /* Waiting for remote public key to generate DHKey */ + HAVE_DHKEY, /* DHKey has been calculated */ + SEND_CONFIRM, /* Waiting to send Confirm value */ + WAIT_NUMBER, /* Waiting for number input from user */ + WAIT_STRING, /* Waiting for string input from user */ + TIMEOUT_START, /* Provision timeout timer has started */ + NUM_FLAGS, +}; + +/** Provisioner link structure allocation + * |--------------------------------------------------------| + * | Link(PB-ADV) | Link(PB-GATT) | + * |--------------------------------------------------------| + * |<----------------------Total Link---------------------->| + */ +struct prov_link { + BLE_MESH_ATOMIC_DEFINE(flags, NUM_FLAGS); + u8_t uuid[16]; /* check if device is being provisioned*/ + u16_t oob_info; /* oob info of this device */ + u8_t element_num; /* element num of device */ + u8_t ki_flags; /* Key refresh flag and iv update flag */ + u32_t iv_index; /* IV Index */ + u8_t auth_method; /* Choosen authentication method */ + u8_t auth_action; /* Choosen authentication action */ + u8_t auth_size; /* Choosen authentication size */ + u16_t assign_addr; /* Application assigned address for the device */ + u16_t unicast_addr; /* unicast address allocated for device */ + bt_mesh_addr_t addr; /* Device address */ +#if defined(CONFIG_BLE_MESH_PB_GATT) + bool connecting; /* start connecting with device */ + struct bt_mesh_conn *conn; /* GATT connection */ +#endif + u8_t expect; /* Next expected PDU */ + + u8_t *dhkey; /* Calculated DHKey */ + u8_t *auth; /* Authentication Value */ + + u8_t *conf_salt; /* ConfirmationSalt */ + u8_t *conf_key; /* ConfirmationKey */ + u8_t *conf_inputs; /* ConfirmationInputs */ + + u8_t *rand; /* Local Random */ + u8_t *conf; /* Remote Confirmation */ + + u8_t *prov_salt; /* Provisioning Salt */ + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bool linking; /* Linking is being establishing */ + u16_t send_link_close; /* Link close is being sent flag */ + u32_t link_id; /* Link ID */ + u8_t pending_ack; /* Decide which transaction id ack is pending */ + u8_t expect_ack_for; /* Transaction ACK expected for provisioning pdu */ + u8_t tx_pdu_type; /* The current transmitted Provisioning PDU type */ + + struct { + u8_t trans_id; /* Transaction ID */ + u8_t prev_id; /* Previous Transaction ID */ + u8_t seg; /* Bit-field of unreceived segments */ + u8_t last_seg; /* Last segment (to check length) */ + u8_t fcs; /* Expected FCS value */ + u8_t adv_buf_id; /* index of buf allocated in adv_buf_data */ + struct net_buf_simple *buf; + } rx; + + struct { + /* Start timestamp of the transaction */ + s64_t start; + + /* Transaction id*/ + u8_t trans_id; + + /* Pending outgoing buffer(s) */ + struct net_buf *buf[3]; + + /* Retransmit timer */ + struct k_delayed_work retransmit; + } tx; +#endif + + /** Provision timeout timer. Spec P259 says: The provisioning protocol + * shall have a minimum timeout of 60 seconds that is reset each time + * a provisioning protocol PDU is sent or received. + */ + struct k_delayed_work timeout; +}; + +/* Number of devices can be provisioned at the same time equals to PB-ADV + PB-GATT */ +#define BLE_MESH_PROV_SAME_TIME \ + (CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME) + +#define PROV_MAX_ADDR_TO_ASSIGN 0x7FFF + +static struct prov_link link[BLE_MESH_PROV_SAME_TIME]; + +struct prov_rx { + u32_t link_id; + u8_t xact_id; + u8_t gpc; +}; + +struct bt_mesh_prov_ctx { + /* Primary element address of Provisioner */ + u16_t primary_addr; + + /* Provisioning bearers used by Provisioner */ + bt_mesh_prov_bearer_t bearers; + + /* If provisioning random have been generated, set BIT0 to 1 */ + u8_t rand_gen_done; + + /* Provisioner random */ + u8_t random[16]; + + /* Current number of PB-ADV provisioned devices simultaneously */ + u8_t pba_count; + + /* Current number of PB-GATT provisioned devices simultaneously */ + u8_t pbg_count; + + /* Current unicast address going to allocated */ + u16_t curr_alloc_addr; + + /* Current net_idx going to be used in provisioning data */ + u16_t curr_net_idx; + + /* Current flags going to be used in provisioning data */ + u16_t curr_flags; + + /* Current iv_index going to be used in provisioning data */ + u16_t curr_iv_index; + + /* Length of Static OOB value */ + u8_t static_oob_len; + + /* Static OOB value */ + u8_t static_oob_val[16]; + + /* Offset of the device uuid to be matched, based on zero */ + u8_t match_offset; + + /* Length of the device uuid to be matched (start from the match_offset) */ + u8_t match_length; + + /* Value of the device uuid to be matched */ + u8_t match_value[16]; + + /* Indicate when received uuid_match adv_pkts, can provision it at once */ + bool prov_after_match; + +#if defined(CONFIG_BLE_MESH_PB_ADV) + /* Mutex used to protect the PB-ADV procedure */ + bt_mesh_mutex_t pb_adv_lock; + + /* Mutex used to protect the adv buf during PB-ADV procedure */ + bt_mesh_mutex_t pb_buf_lock; +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + /* Mutex used to protect the PB-GATT procedure */ + bt_mesh_mutex_t pb_gatt_lock; +#endif + + /* Fast provisioning related information */ + struct { + bool enable; + u16_t net_idx; + const u8_t *net_key; + u8_t flags; + u32_t iv_index; + u16_t unicast_addr_min; + u16_t unicast_addr_max; + } fast_prov; +}; + +static struct bt_mesh_prov_ctx prov_ctx; + +#define FAST_PROV_ENABLE() (prov_ctx.fast_prov.enable) + +struct unprov_dev_queue { + bt_mesh_addr_t addr; + u8_t uuid[16]; + u16_t oob_info; + u8_t bearer; + u8_t flags; +} __packed unprov_dev[CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM] = { + [0 ... (CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM - 1)] = { + .addr.type = 0xff, + .bearer = 0, + .flags = false, + }, +}; + +static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb; + +#define BUF_TIMEOUT K_MSEC(400) + +#if defined(CONFIG_BLE_MESH_FAST_PROV) +#define RETRANSMIT_TIMEOUT K_MSEC(360) +#define TRANSACTION_TIMEOUT K_SECONDS(3) +#define PROVISION_TIMEOUT K_SECONDS(6) +#else +#define RETRANSMIT_TIMEOUT K_MSEC(500) +#define TRANSACTION_TIMEOUT K_SECONDS(30) +#define PROVISION_TIMEOUT K_SECONDS(60) +#endif /* CONFIG_BLE_MESH_FAST_PROV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +#define PROV_BUF_HEADROOM 5 +#else +#define PROV_BUF_HEADROOM 0 +#endif + +#define PROV_BUF(name, len) \ + NET_BUF_SIMPLE_DEFINE(name, PROV_BUF_HEADROOM + len) + +static const struct bt_mesh_prov *prov; + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void send_link_open(const u8_t idx); +#endif + +static void prov_gen_dh_key(const u8_t idx); + +static void send_pub_key(const u8_t idx, u8_t oob); + +static void close_link(const u8_t idx, u8_t reason); + +#if defined(CONFIG_BLE_MESH_PB_ADV) +#define ADV_BUF_SIZE 65 + +static struct prov_adv_buf { + struct net_buf_simple buf; +} adv_buf[CONFIG_BLE_MESH_PBA_SAME_TIME]; + +static u8_t adv_buf_data[ADV_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME]; +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#define PROV_FREE_MEM(_idx, member) \ +{ \ + if (link[_idx].member) { \ + bt_mesh_free(link[_idx].member); \ + link[_idx].member = NULL; \ + } \ +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void bt_mesh_pb_adv_mutex_new(void) +{ + if (!prov_ctx.pb_adv_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_adv_lock); + } +} + +static void bt_mesh_pb_adv_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_adv_lock); +} + +static void bt_mesh_pb_adv_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock); +} + +static void bt_mesh_pb_adv_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock); +} + +static void bt_mesh_pb_buf_mutex_new(void) +{ + if (!prov_ctx.pb_buf_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_buf_lock); + } +} + +static void bt_mesh_pb_buf_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_buf_lock); +} + +static void bt_mesh_pb_buf_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_buf_lock); +} + +static void bt_mesh_pb_buf_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_buf_lock); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static void bt_mesh_pb_gatt_mutex_new(void) +{ + if (!prov_ctx.pb_gatt_lock.mutex) { + bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock); + } +} + +static void bt_mesh_pb_gatt_mutex_free(void) +{ + bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock); +} + +static void bt_mesh_pb_gatt_lock(void) +{ + bt_mesh_mutex_lock(&prov_ctx.pb_gatt_lock); +} + +static void bt_mesh_pb_gatt_unlock(void) +{ + bt_mesh_mutex_unlock(&prov_ctx.pb_gatt_lock); +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +void bt_mesh_provisioner_pbg_count_dec(void) +{ + if (prov_ctx.pbg_count) { + prov_ctx.pbg_count--; + } +} + +static inline void provisioner_pbg_count_inc(void) +{ + prov_ctx.pbg_count++; +} + +#if defined(CONFIG_BLE_MESH_PB_GATT) +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]) +{ + int i; + + if (!addr) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + BT_DBG("%s, Clear device %s info", __func__, bt_hex(addr, BLE_MESH_ADDR_LEN)); + + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (!memcmp(link[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { + link[i].connecting = false; + link[i].conn = NULL; + link[i].oob_info = 0x0; + memset(link[i].uuid, 0, 16); + memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_atomic_test_and_clear_bit(link[i].flags, LINK_ACTIVE); + if (bt_mesh_atomic_test_and_clear_bit(link[i].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[i].timeout); + } + return; + } + } + + BT_WARN("Device address %s is not found", bt_hex(addr, BLE_MESH_ADDR_LEN)); + return; +} +#endif + +const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void) +{ + return prov; +} + +void bt_mesh_provisioner_restore_prov_info(u16_t primary_addr, u16_t alloc_addr) +{ + prov_ctx.primary_addr = primary_addr; + prov_ctx.curr_alloc_addr = alloc_addr; +} + +static int provisioner_dev_find(const bt_mesh_addr_t *addr, const u8_t uuid[16], u16_t *index) +{ + bool uuid_match = false; + bool addr_match = false; + u8_t zero[16] = {0}; + u16_t i = 0U, j = 0U; + int comp = 0; + + if (addr) { + comp = memcmp(addr->val, zero, BLE_MESH_ADDR_LEN); + } + + if ((!uuid && (!addr || (comp == 0) || (addr->type > BLE_MESH_ADDR_RANDOM))) || !index) { + return -EINVAL; + } + + /** Note: user may add a device into two unprov_dev array elements, + * one with device address, address type and another only + * with device UUID. We need to take this into consideration. + */ + if (uuid && memcmp(uuid, zero, 16)) { + for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) { + if (!memcmp(unprov_dev[i].uuid, uuid, 16)) { + uuid_match = true; + break; + } + } + } + + if (addr && comp && (addr->type <= BLE_MESH_ADDR_RANDOM)) { + for (j = 0; j < ARRAY_SIZE(unprov_dev); j++) { + if (!memcmp(unprov_dev[j].addr.val, addr->val, BLE_MESH_ADDR_LEN) && + unprov_dev[j].addr.type == addr->type) { + addr_match = true; + break; + } + } + } + + if (!uuid_match && !addr_match) { + BT_DBG("%s, Device does not exist in queue", __func__); + return -ENODEV; + } + + if (uuid_match && addr_match && (i != j)) { + /** + * In this situation, copy address & type into device uuid + * array element, reset another element, rm_flag will be + * decided by uuid element. + */ + unprov_dev[i].addr.type = unprov_dev[j].addr.type; + memcpy(unprov_dev[i].addr.val, unprov_dev[j].addr.val, BLE_MESH_ADDR_LEN); + unprov_dev[i].bearer |= unprov_dev[j].bearer; + memset(&unprov_dev[j], 0x0, sizeof(struct unprov_dev_queue)); + } + + *index = uuid_match ? i : j; + return 0; +} + +static bool is_unprov_dev_being_provision(const u8_t uuid[16]) +{ + int i; + +#if defined(CONFIG_BLE_MESH_FAST_PROV) + /** + * During Fast Provisioning test, we found that if a device has already being + * provisioned, there is still a chance that the Provisioner can receive the + * Unprovisioned Device Beacon from the device (because the device will stop + * Unprovisioned Device Beacon when Transaction ACK for Provisioning Complete + * is received). So in Fast Provisioning the Provisioner should ignore this. + */ + if (bt_mesh_provisioner_get_node_with_uuid(uuid)) { + BT_WARN("Device has already been provisioned"); + return true; + } +#endif + + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { +#if defined(CONFIG_BLE_MESH_PB_ADV) && defined(CONFIG_BLE_MESH_PB_GATT) + if (link[i].linking || link[i].connecting || + bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { +#elif defined(CONFIG_BLE_MESH_PB_ADV) && !defined(CONFIG_BLE_MESH_PB_GATT) + if (link[i].linking || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { +#else + if (link[i].connecting || bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { +#endif + if (!memcmp(link[i].uuid, uuid, 16)) { + BT_DBG("%s, Device is being provisioned", __func__); + return true; + } + } + } + + return false; +} + +static bool is_unprov_dev_uuid_match(const u8_t uuid[16]) +{ + if (prov_ctx.match_length) { + if (memcmp(uuid + prov_ctx.match_offset, + prov_ctx.match_value, prov_ctx.match_length)) { + return false; + } + } + + return true; +} + +static int provisioner_check_unprov_dev_info(const u8_t uuid[16], bt_mesh_prov_bearer_t bearer) +{ + if (!uuid) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Check if the device uuid matches configured value */ + if (is_unprov_dev_uuid_match(uuid) == false) { + BT_DBG("%s, Device uuid mismatch", __func__); + return -EIO; + } + + /* Check if this device is currently being provisioned. + * According to Zephyr's device code, if we connect with + * one device and start to provision it, we may still can + * receive the connectable prov adv pkt from this device. + * Here we check both PB-GATT and PB-ADV link status. + */ + if (is_unprov_dev_being_provision(uuid)) { + return -EALREADY; + } + + /* Check if the current PB-ADV link is full */ + if (bearer == BLE_MESH_PROV_ADV && prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME) { + BT_INFO("Current PB-ADV links reach max limit"); + return -ENOMEM; + } + + /* Check if the current PB-GATT link is full */ + if (bearer == BLE_MESH_PROV_GATT && prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME) { + BT_INFO("Current PB-GATT links reach max limit"); + return -ENOMEM; + } + + /* Check if the device has already been provisioned */ + if (bt_mesh_provisioner_get_node_with_uuid(uuid)) { + BT_INFO("Provisioned before, start to provision again"); + return 0; + } + + return 0; +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static int provisioner_start_prov_pb_adv(const u8_t uuid[16], const bt_mesh_addr_t *addr, + u16_t oob_info, u16_t assign_addr) +{ + u8_t zero[6] = {0}; + int addr_cmp = 0; + int i; + + if (!uuid || !addr) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_pb_adv_lock(); + + /* If the unicast address of the node is going to be allocated internally, + * then we need to check if there are addresses can be allocated. + */ + if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && + prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("No available unicast address to assign"); + bt_mesh_pb_adv_unlock(); + return -EIO; + } + + if (is_unprov_dev_being_provision(uuid)) { + bt_mesh_pb_adv_unlock(); + return -EALREADY; + } + + addr_cmp = memcmp(addr->val, zero, BLE_MESH_ADDR_LEN); + + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + if (!bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE) && !link[i].linking) { + memcpy(link[i].uuid, uuid, 16); + link[i].oob_info = oob_info; + if (addr_cmp && (addr->type <= BLE_MESH_ADDR_RANDOM)) { + link[i].addr.type = addr->type; + memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + } + send_link_open(i); + /* If the application layer assigned a specific unicast address for the device, + * then Provisioner will use this address in the Provisioning Data PDU. + */ + if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { + link[i].assign_addr = assign_addr; + } + bt_mesh_pb_adv_unlock(); + return 0; + } + } + + BT_ERR("%s, No PB-ADV link is available", __func__); + bt_mesh_pb_adv_unlock(); + + return -ENOMEM; +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static int provisioner_start_prov_pb_gatt(const u8_t uuid[16], const bt_mesh_addr_t *addr, + u16_t oob_info, u16_t assign_addr) +{ + u8_t zero[6] = {0}; + int addr_cmp = 0; + int i; + + if (!uuid || !addr) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + bt_mesh_pb_gatt_lock(); + + /* If the unicast address of the node is going to be allocated internally, + * then we need to check if there are addresses can be allocated. + */ + if (assign_addr == BLE_MESH_ADDR_UNASSIGNED && + prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("No available unicast address to assign"); + bt_mesh_pb_gatt_unlock(); + return -EIO; + } + + if (is_unprov_dev_being_provision(uuid)) { + bt_mesh_pb_gatt_unlock(); + return -EALREADY; + } + + addr_cmp = memcmp(addr->val, zero, BLE_MESH_ADDR_LEN); + + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (!link[i].connecting && !bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + memcpy(link[i].uuid, uuid, 16); + link[i].oob_info = oob_info; + if (addr_cmp && (addr->type <= BLE_MESH_ADDR_RANDOM)) { + link[i].addr.type = addr->type; + memcpy(link[i].addr.val, addr->val, BLE_MESH_ADDR_LEN); + } + if (bt_mesh_gattc_conn_create(&link[i].addr, BLE_MESH_UUID_MESH_PROV_VAL) < 0) { + memset(link[i].uuid, 0, 16); + link[i].oob_info = 0x0; + memset(&link[i].addr, 0, sizeof(bt_mesh_addr_t)); + bt_mesh_pb_gatt_unlock(); + return -EIO; + } + /* If the application layer assigned a specific unicast address for the device, + * then Provisioner will use this address in the Provisioning Data PDU. + */ + if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) { + link[i].assign_addr = assign_addr; + } + /* If creating connection successfully, set connecting flag to 1 */ + link[i].connecting = true; + provisioner_pbg_count_inc(); + bt_mesh_pb_gatt_unlock(); + return 0; + } + } + + BT_ERR("%s, No PB-GATT link is available", __func__); + bt_mesh_pb_gatt_unlock(); + + return -ENOMEM; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u8_t flags) +{ + bt_mesh_addr_t add_addr = {0}; + u8_t zero[16] = {0}; + int addr_cmp = 0; + int uuid_cmp = 0; + u16_t i = 0U; + int err = 0; + + if (!add_dev) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + addr_cmp = memcmp(add_dev->addr, zero, BLE_MESH_ADDR_LEN); + uuid_cmp = memcmp(add_dev->uuid, zero, 16); + + if (add_dev->bearer == 0x0 || ((uuid_cmp == 0) && + ((addr_cmp == 0) || add_dev->addr_type > BLE_MESH_ADDR_RANDOM))) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if ((add_dev->bearer & BLE_MESH_PROV_ADV) && (add_dev->bearer & BLE_MESH_PROV_GATT) && + (flags & START_PROV_NOW)) { + BT_ERR("%s, Can not start PB-ADV & PB-GATT simultaneously", __func__); + return -EINVAL; + } + + if ((uuid_cmp == 0) && (flags & START_PROV_NOW)) { + BT_ERR("%s, Can not start provisioning with zero uuid", __func__); + return -EINVAL; + } + + if ((add_dev->bearer & BLE_MESH_PROV_GATT) && (flags & START_PROV_NOW) && + ((addr_cmp == 0) || add_dev->addr_type > BLE_MESH_ADDR_RANDOM)) { + BT_ERR("%s, Invalid device address for PB-GATT", __func__); + return -EINVAL; + } + + if (add_dev->bearer & BLE_MESH_PROV_GATT) { +#if !CONFIG_BLE_MESH_PB_GATT + BT_ERR("%s, Not support PB-GATT", __func__); + return -EINVAL; +#endif + } + + if (add_dev->bearer & BLE_MESH_PROV_ADV) { +#if !CONFIG_BLE_MESH_PB_ADV + BT_ERR("%s, Not support PB-ADV", __func__); + return -EINVAL; +#endif + } + + /* Check if the provisioned nodes array is full */ + if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) { + if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_WARN("Current provisioned devices reach max limit"); + return -ENOMEM; + } + } + + add_addr.type = add_dev->addr_type; + memcpy(add_addr.val, add_dev->addr, BLE_MESH_ADDR_LEN); + + err = provisioner_dev_find(&add_addr, add_dev->uuid, &i); + if (err == -EINVAL) { + BT_ERR("%s, Invalid parameter", __func__); + return err; + } else if (err == 0) { + if (!(add_dev->bearer & unprov_dev[i].bearer)) { + BT_WARN("Add device with only bearer updated"); + unprov_dev[i].bearer |= add_dev->bearer; + } else { + BT_WARN("Device already exists in queue"); + } + goto start; + } + + for (i = 0U; i < ARRAY_SIZE(unprov_dev); i++) { + if (unprov_dev[i].bearer) { + continue; + } + if (addr_cmp && (add_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { + unprov_dev[i].addr.type = add_dev->addr_type; + memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN); + } + if (uuid_cmp) { + memcpy(unprov_dev[i].uuid, add_dev->uuid, 16); + } + unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2); + unprov_dev[i].flags = flags & BIT_MASK(3); + goto start; + } + + /* If queue is full, find flushable device and replace it */ + for (i = 0U; i < ARRAY_SIZE(unprov_dev); i++) { + if (unprov_dev[i].flags & FLUSHABLE_DEV) { + memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue)); + if (addr_cmp && (add_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { + unprov_dev[i].addr.type = add_dev->addr_type; + memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN); + } + if (uuid_cmp) { + memcpy(unprov_dev[i].uuid, add_dev->uuid, 16); + } + unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2); + unprov_dev[i].flags = flags & BIT_MASK(3); + goto start; + } + } + + BT_ERR("%s, Unprovisioned device queue is full", __func__); + return -ENOMEM; + +start: + if (!(flags & START_PROV_NOW)) { + return 0; + } + + /* Check if current provisioned node count + active link reach max limit */ + if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) { + if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \ + prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_WARN("Node count + active link count reach max limit"); + return -EIO; + } + } + + if ((err = provisioner_check_unprov_dev_info(add_dev->uuid, add_dev->bearer))) { + return err; + } + + if (add_dev->bearer == BLE_MESH_PROV_ADV) { +#if defined(CONFIG_BLE_MESH_PB_ADV) + if ((err = provisioner_start_prov_pb_adv( + add_dev->uuid, &add_addr, add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) { + return err; + } +#endif + } else if (add_dev->bearer == BLE_MESH_PROV_GATT) { +#if defined(CONFIG_BLE_MESH_PB_GATT) + if ((err = provisioner_start_prov_pb_gatt( + add_dev->uuid, &add_addr, add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED))) { + return err; + } +#endif + } + + return 0; +} + +int bt_mesh_provisioner_prov_device_with_addr(const u8_t uuid[16], const u8_t addr[6], + u8_t addr_type, bt_mesh_prov_bearer_t bearer, + u16_t oob_info, u16_t unicast_addr) +{ + bt_mesh_addr_t dev_addr = {0}; + int err = 0; + + if (uuid == NULL) { + BT_ERR("%s, NULL device uuid", __func__); + return -EINVAL; + } + + if (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT) { + BT_ERR("%s, Invalid provisioning bearer 0x%02x", __func__, bearer); + return -EINVAL; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && bearer == BLE_MESH_PROV_ADV) { + BT_ERR("%s, Not support PB-ADV", __func__); + return -ENOTSUP; + } + + if (!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && bearer == BLE_MESH_PROV_GATT) { + BT_ERR("%s, Not support PB-GATT", __func__); + return -ENOTSUP; + } + + if (bearer == BLE_MESH_PROV_GATT && (addr == NULL || addr_type > BLE_MESH_ADDR_RANDOM)) { + BT_ERR("%s, Invalid device address info", __func__); + return -EINVAL; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + BT_ERR("%s, Invalid unicast address 0x%04x", __func__, unicast_addr); + return -EINVAL; + } + + /* Here we will not check if the assigned unicast address is overlapped + * with the unicast addresses of other nodes or Provisioner, because: + * 1. At this moment, the element number of the device is unknown + * 2. If the node is a reprovisioned device, then the original allocated + * unicast address will be used. + * 3. Some other devices may be just being provisioning, and currently we + * can not know the exactly allocated addresses of them. + */ + + if (bt_mesh_provisioner_get_node_with_uuid(uuid) == NULL) { + /* Check if the provisioned nodes array is full */ + if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_WARN("Current provisioned devices reach max limit"); + return -ENOMEM; + } + + /* Check if current provisioned node count + active link reach max limit */ + if (bt_mesh_provisioner_get_node_count() + prov_ctx.pba_count + \ + prov_ctx.pbg_count >= CONFIG_BLE_MESH_MAX_PROV_NODES) { + BT_WARN("Node count + active link count reach max limit"); + return -EIO; + } + } + + if ((err = provisioner_check_unprov_dev_info(uuid, bearer))) { + return err; + } + + if (addr) { + dev_addr.type = addr_type; + memcpy(dev_addr.val, addr, BLE_MESH_ADDR_LEN); + } + + if (bearer == BLE_MESH_PROV_ADV) { +#if defined(CONFIG_BLE_MESH_PB_ADV) + if ((err = provisioner_start_prov_pb_adv(uuid, &dev_addr, oob_info, unicast_addr))) { + return err; + } +#endif + } else if (bearer == BLE_MESH_PROV_GATT) { +#if defined(CONFIG_BLE_MESH_PB_GATT) + if ((err = provisioner_start_prov_pb_gatt(uuid, &dev_addr, oob_info, unicast_addr))) { + return err; + } +#endif + } + + return 0; +} + +int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev) +{ + /** + * Three Situations: + * 1. device is not being/been provisioned, just remove from device queue. + * 2. device is being provisioned, need to close link & remove from device queue. + * 3. device is been provisioned, need to send config_node_reset and may need to + * remove from device queue. config _node_reset can be added in function + * provisioner_reset_node() in provisioner_main.c. + */ + bt_mesh_addr_t del_addr = {0}; + u8_t zero[16] = {0}; + bool addr_match = false; + bool uuid_match = false; + int addr_cmp = 0; + int uuid_cmp = 0; + u16_t i = 0U; + int err = 0; + + if (!del_dev) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + addr_cmp = memcmp(del_dev->addr, zero, BLE_MESH_ADDR_LEN); + uuid_cmp = memcmp(del_dev->uuid, zero, 16); + + if ((uuid_cmp == 0) && ((addr_cmp == 0) || del_dev->addr_type > BLE_MESH_ADDR_RANDOM)) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + del_addr.type = del_dev->addr_type; + memcpy(del_addr.val, del_dev->addr, BLE_MESH_ADDR_LEN); + + /* First: find if the device is in the device queue */ + err = provisioner_dev_find(&del_addr, del_dev->uuid, &i); + if (err) { + BT_DBG("%s, Device is not in the queue", __func__); + } else { + memset(&unprov_dev[i], 0x0, sizeof(struct unprov_dev_queue)); + } + + /* Second: find if the device is being provisioned */ + for (i = 0U; i < ARRAY_SIZE(link); i++) { + if (addr_cmp && (del_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { + if (!memcmp(link[i].addr.val, del_dev->addr, BLE_MESH_ADDR_LEN) && + link[i].addr.type == del_dev->addr_type) { + addr_match = true; + } + } + if (uuid_cmp) { + if (!memcmp(link[i].uuid, del_dev->uuid, 16)) { + uuid_match = true; + } + } + if (addr_match || uuid_match) { + close_link(i, CLOSE_REASON_FAILED); + break; + } + } + + /* Third: find if the device is been provisioned */ + if (addr_cmp && (del_dev->addr_type <= BLE_MESH_ADDR_RANDOM)) { + bt_mesh_provisioner_delete_node_with_dev_addr(&del_addr); + } + + if (uuid_cmp) { + bt_mesh_provisioner_delete_node_with_uuid(del_dev->uuid); + } + + return 0; +} + +int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, + const u8_t *match, bool prov_flag) +{ + if (length && (!match || (offset + length > 16))) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + (void)memset(prov_ctx.match_value, 0, 16); + + prov_ctx.match_offset = offset; + prov_ctx.match_length = length; + if (length) { + memcpy(prov_ctx.match_value, match, length); + } + prov_ctx.prov_after_match = prov_flag; + + return 0; +} + +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb) +{ + if (!cb) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + notify_unprov_adv_pkt_cb = cb; + return 0; +} + +int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info) +{ + const u8_t *key = NULL; + + if (!info || info->flag == 0) { + return -EINVAL; + } + + if (info->flag & NET_IDX_FLAG) { + key = bt_mesh_provisioner_net_key_get(info->net_idx); + if (!key) { + BT_ERR("%s, Failed to get NetKey", __func__); + return -EINVAL; + } + prov_ctx.curr_net_idx = info->net_idx; + } else if (info->flag & FLAGS_FLAG) { + prov_ctx.curr_flags = info->flags; + } else if (info->flag & IV_INDEX_FLAG) { + prov_ctx.curr_iv_index = info->iv_index; + } + + return 0; +} + +int bt_mesh_provisioner_set_prov_info(void) +{ + const struct bt_mesh_comp *comp = NULL; + + if (prov_ctx.primary_addr == BLE_MESH_ADDR_UNASSIGNED) { + /* If unicast address of primary element of Provisioner has not been set + * before, then the following initialization procedure will be used. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(prov->prov_unicast_addr) || + !BLE_MESH_ADDR_IS_UNICAST(prov->prov_start_address)) { + BT_ERR("%s, Invalid address, own 0x%04x, start 0x%04x", + __func__, prov->prov_unicast_addr, prov->prov_start_address); + return -EINVAL; + } + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("%s, NULL composition data", __func__); + return -EINVAL; + } + + if (prov->prov_unicast_addr + comp->elem_count > prov->prov_start_address) { + BT_WARN("Too small start address 0x%04x, update to 0x%04x", + prov->prov_start_address, prov->prov_unicast_addr + comp->elem_count); + prov_ctx.curr_alloc_addr = prov->prov_unicast_addr + comp->elem_count; + } else { + prov_ctx.curr_alloc_addr = prov->prov_start_address; + } + prov_ctx.primary_addr = prov->prov_unicast_addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + } + prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY; + prov_ctx.curr_flags = prov->flags; + prov_ctx.curr_iv_index = prov->iv_index; + + return 0; +} + +void bt_mesh_provisioner_set_prov_bearer(bt_mesh_prov_bearer_t bearers, bool clear) +{ + if (clear == false) { + prov_ctx.bearers |= bearers; + } else { + prov_ctx.bearers &= ~bearers; + } +} + +bt_mesh_prov_bearer_t bt_mesh_provisioner_get_prov_bearer(void) +{ + return prov_ctx.bearers; +} + +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length) +{ + int i; + + if (value == NULL || length == 0U || length > 16U) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + /* Make sure Static OOB is not being used. */ + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (link[i].auth_method == AUTH_METHOD_STATIC) { + BT_ERR("%s, Static OOB is being used", __func__); + return -EINVAL; + } + } + + (void)memset(prov_ctx.static_oob_val, 0, 16); + + prov_ctx.static_oob_len = MIN(16, length); + memcpy(prov_ctx.static_oob_val, value, prov_ctx.static_oob_len); + + return 0; +} + +u16_t bt_mesh_provisioner_get_primary_elem_addr(void) +{ + return prov_ctx.primary_addr; +} + +int bt_mesh_provisioner_set_primary_elem_addr(u16_t addr) +{ + const struct bt_mesh_comp *comp = NULL; + + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("Invalid primary address 0x%04x", addr); + return -EINVAL; + } + + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("NULL composition data"); + return -EINVAL; + } + + /* Make sure Provisioner address is not identical with the addresses of nodes */ + if (bt_mesh_provisioner_check_is_addr_dup(addr, comp->elem_count, false)) { + BT_ERR("Address 0x%04x is duplicated with node address", addr); + return -EINVAL; + } + + /* If the current can-be allocated address is bigger than primary address + + * element number, then the curr_alloc_addr will not be changed, and only + * the Provisioner related addresses will be updated. + */ + if (addr + comp->elem_count > prov_ctx.curr_alloc_addr) { + prov_ctx.curr_alloc_addr = addr + comp->elem_count; + } + BT_INFO("Provisioner primary address updated, old 0x%04x, new 0x%04x", prov_ctx.primary_addr, addr); + prov_ctx.primary_addr = addr; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + + bt_mesh_comp_provision(addr); + + return 0; +} + +#if CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK +int bt_mesh_test_provisioner_update_alloc_addr(u16_t unicast_addr, u16_t element_num) +{ + u16_t max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : PROV_MAX_ADDR_TO_ASSIGN; + + if (unicast_addr + element_num > max_addr) { + BT_WARN("%s, Not enough unicast address to allocate", __func__); + prov_ctx.curr_alloc_addr = BLE_MESH_ADDR_UNASSIGNED; + } else { + prov_ctx.curr_alloc_addr = unicast_addr + element_num; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK */ + +/* The following APIs are for fast provisioning */ + +void bt_mesh_provisioner_fast_prov_enable(bool enable) +{ + prov_ctx.fast_prov.enable = enable; +} + +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx) +{ + prov_ctx.fast_prov.net_idx = net_idx; + prov_ctx.fast_prov.net_key = net_key; + + if (!net_key) { + BT_WARN("Wait for NetKey for fast provisioning"); + return 0x01; /*status: wait for net_key */ + } + + return 0x0; /* status: success */ +} + +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void) +{ + return prov_ctx.fast_prov.net_idx; +} + +u8_t bt_mesh_set_fast_prov_unicast_addr_range(u16_t min, u16_t max) +{ + if (!BLE_MESH_ADDR_IS_UNICAST(min) || !BLE_MESH_ADDR_IS_UNICAST(max)) { + BT_ERR("%s, Not a unicast address", __func__); + return 0x01; /* status: not a unicast address */ + } + + if (min > max) { + BT_ERR("%s, Min bigger than max", __func__); + return 0x02; /* status: min is bigger than max */ + } + + if (min <= prov_ctx.fast_prov.unicast_addr_max) { + BT_ERR("%s, Address overlap", __func__); + return 0x03; /* status: address overlaps with current value */ + } + + prov_ctx.fast_prov.unicast_addr_min = min; + prov_ctx.fast_prov.unicast_addr_max = max; + + prov_ctx.curr_alloc_addr = prov_ctx.fast_prov.unicast_addr_min; + + return 0x0; /* status: success */ +} + +void bt_mesh_set_fast_prov_flags_iv_index(u8_t flags, u32_t iv_index) +{ + /* BIT0: Key Refresh flag, BIT1: IV Update flag */ + prov_ctx.fast_prov.flags = flags & BIT_MASK(2); + prov_ctx.fast_prov.iv_index = iv_index; +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static struct net_buf_simple *bt_mesh_pba_get_buf(const u8_t idx) +{ + struct net_buf_simple *buf = &(adv_buf[idx].buf); + + net_buf_simple_reset(buf); + + return buf; +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +static void prov_memory_free(const u8_t idx) +{ + PROV_FREE_MEM(idx, dhkey); + PROV_FREE_MEM(idx, auth); + PROV_FREE_MEM(idx, conf); + PROV_FREE_MEM(idx, conf_salt); + PROV_FREE_MEM(idx, conf_key); + PROV_FREE_MEM(idx, conf_inputs); + PROV_FREE_MEM(idx, prov_salt); +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void buf_sent(int err, void *user_data) +{ + u8_t idx = (int)user_data; + + if (!link[idx].tx.buf[0]) { + return; + } + + k_delayed_work_submit(&link[idx].tx.retransmit, RETRANSMIT_TIMEOUT); +} + +static struct bt_mesh_send_cb buf_sent_cb = { + .end = buf_sent, +}; + +static void free_segments(const u8_t idx) +{ + int i; + + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + struct net_buf *buf = link[idx].tx.buf[i]; + + if (!buf) { + break; + } + + link[idx].tx.buf[i] = NULL; + bt_mesh_adv_buf_ref_debug(__func__, buf, 3U, BLE_MESH_BUF_REF_SMALL); + /* Mark as canceled */ + BLE_MESH_ADV(buf)->busy = 0U; + net_buf_unref(buf); + } + + bt_mesh_pb_buf_unlock(); +} + +static void prov_clear_tx(const u8_t idx) +{ + BT_DBG("%s", __func__); + + k_delayed_work_cancel(&link[idx].tx.retransmit); + + free_segments(idx); +} + +static void reset_link(const u8_t idx, u8_t reason) +{ + prov_clear_tx(idx); + + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + if (prov->prov_link_close) { + prov->prov_link_close(BLE_MESH_PROV_ADV, reason); + } + + prov_memory_free(idx); + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link[idx].link_id); +#endif + + /* Clear everything except the retransmit delayed work config */ + memset(&link[idx], 0, offsetof(struct prov_link, tx.retransmit)); + + link[idx].pending_ack = XACT_NVAL; + link[idx].rx.prev_id = XACT_NVAL; + + if (bt_mesh_pub_key_get()) { + bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); + } + + link[idx].rx.buf = bt_mesh_pba_get_buf(idx); + + if (prov_ctx.pba_count) { + prov_ctx.pba_count--; + } +} + +static struct net_buf *adv_buf_create(void) +{ + struct net_buf *buf = NULL; + + buf = bt_mesh_adv_create(BLE_MESH_ADV_PROV, PROV_XMIT, BUF_TIMEOUT); + if (!buf) { + BT_ERR("Out of provisioning buffers"); + return NULL; + } + + return buf; +} + +static void ack_complete(u16_t duration, int err, void *user_data) +{ + u8_t idx = (int)user_data; + + BT_DBG("xact %u complete", link[idx].pending_ack); + + link[idx].pending_ack = XACT_NVAL; +} + +static void gen_prov_ack_send(const u8_t idx, u8_t xact_id) +{ + static const struct bt_mesh_send_cb cb = { + .start = ack_complete, + }; + const struct bt_mesh_send_cb *complete = NULL; + struct net_buf *buf = NULL; + + BT_DBG("xact_id %u", xact_id); + + if (link[idx].pending_ack == xact_id) { + BT_DBG("Not sending duplicate ack"); + return; + } + + buf = adv_buf_create(); + if (!buf) { + return; + } + + if (link[idx].pending_ack == XACT_NVAL) { + link[idx].pending_ack = xact_id; + complete = &cb; + } else { + complete = NULL; + } + + net_buf_add_be32(buf, link[idx].link_id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_ACK); + + bt_mesh_adv_send(buf, complete, (void *)(int)idx); + net_buf_unref(buf); +} + +static void send_reliable(const u8_t idx) +{ + int i; + + link[idx].tx.start = k_uptime_get(); + + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + struct net_buf *buf = link[idx].tx.buf[i]; + + if (!buf) { + break; + } + + if (i + 1 < ARRAY_SIZE(link[idx].tx.buf) && link[idx].tx.buf[i + 1]) { + bt_mesh_adv_send(buf, NULL, NULL); + } else { + bt_mesh_adv_send(buf, &buf_sent_cb, (void *)(int)idx); + } + } +} + +static int bearer_ctl_send(const u8_t idx, u8_t op, void *data, u8_t data_len) +{ + struct net_buf *buf = NULL; + + BT_DBG("op 0x%02x data_len %u", op, data_len); + + prov_clear_tx(idx); + + buf = adv_buf_create(); + if (!buf) { + return -ENOBUFS; + } + + net_buf_add_be32(buf, link[idx].link_id); + /* Transaction ID, always 0 for Bearer messages */ + net_buf_add_u8(buf, 0x00); + net_buf_add_u8(buf, GPC_CTL(op)); + net_buf_add_mem(buf, data, data_len); + + link[idx].tx.buf[0] = buf; + send_reliable(idx); + + /** We can also use buf->ref and a flag to decide that + * link close has been sent 3 times. + * Here we use another way: use retransmit timer and need + * to make sure the timer is not cancelled during sending + * link close pdu, so we add link[i].tx.id = 0 + */ + if (op == LINK_CLOSE) { + u8_t reason = *(u8_t *)data; + link[idx].send_link_close = ((reason & BIT_MASK(2)) << 1) | BIT(0); + link[idx].tx.trans_id = 0; + } + + return 0; +} + +static void send_link_open(const u8_t idx) +{ + int j; + + /** Generate link ID, and may need to check if this id is + * currently being used, which may will not happen ever. + */ + bt_mesh_rand(&link[idx].link_id, sizeof(u32_t)); + while (1) { + for (j = 0; j < CONFIG_BLE_MESH_PBA_SAME_TIME; j++) { + if (bt_mesh_atomic_test_bit(link[j].flags, LINK_ACTIVE) || link[j].linking) { + if (link[idx].link_id == link[j].link_id) { + bt_mesh_rand(&link[idx].link_id, sizeof(u32_t)); + break; + } + } + } + if (j == CONFIG_BLE_MESH_PBA_SAME_TIME) { + break; + } + } + +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Add the link id into exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link[idx].link_id); +#endif + + bearer_ctl_send(idx, LINK_OPEN, link[idx].uuid, 16); + + /* If Provisioner sets LINK_ACTIVE flag once Link Open is sent, we have + * no need to use linking flag (like PB-GATT connecting) to prevent the + * stored device info (UUID, oob_info) being replaced by other received + * unprovisioned device beacons. + * But if Provisioner sets LINK_ACTIVE flag after Link ACK is received, + * we need to use linking flag to prevent device info being replaced. + * Currently we set LINK_ACTIVE flag after sending Link Open. + */ + link[idx].linking = true; + + /* Set LINK_ACTIVE just to be in compatibility with current Zephyr code */ + bt_mesh_atomic_set_bit(link[idx].flags, LINK_ACTIVE); + + if (prov->prov_link_open) { + prov->prov_link_open(BLE_MESH_PROV_ADV); + } + + prov_ctx.pba_count++; +} + +static u8_t last_seg(u8_t len) +{ + if (len <= START_PAYLOAD_MAX) { + return 0; + } + + len -= START_PAYLOAD_MAX; + + return 1 + (len / CONT_PAYLOAD_MAX); +} + +static inline u8_t next_transaction_id(const u8_t idx) +{ + if (link[idx].tx.trans_id > 0x7F) { + link[idx].tx.trans_id = 0x0; + } + return link[idx].tx.trans_id++; +} + +static int prov_send_adv(const u8_t idx, struct net_buf_simple *msg) +{ + struct net_buf *start = NULL, *buf = NULL; + u8_t seg_len = 0U, seg_id = 0U; + u8_t xact_id = 0U; + s32_t timeout = PROVISION_TIMEOUT; + + BT_DBG("%s, len %u: %s", __func__, msg->len, bt_hex(msg->data, msg->len)); + + prov_clear_tx(idx); + + start = adv_buf_create(); + if (!start) { + return -ENOBUFS; + } + + xact_id = next_transaction_id(idx); + net_buf_add_be32(start, link[idx].link_id); + net_buf_add_u8(start, xact_id); + + net_buf_add_u8(start, GPC_START(last_seg(msg->len))); + net_buf_add_be16(start, msg->len); + net_buf_add_u8(start, bt_mesh_fcs_calc(msg->data, msg->len)); + + link[idx].tx.buf[0] = start; + /* Changed by Espressif, get message type */ + link[idx].tx_pdu_type = msg->data[0]; + + seg_len = MIN(msg->len, START_PAYLOAD_MAX); + BT_DBG("seg 0 len %u: %s", seg_len, bt_hex(msg->data, seg_len)); + net_buf_add_mem(start, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + + buf = start; + for (seg_id = 1; msg->len > 0; seg_id++) { + if (seg_id >= ARRAY_SIZE(link[idx].tx.buf)) { + BT_ERR("%s, Too big message", __func__); + free_segments(idx); + return -E2BIG; + } + + buf = adv_buf_create(); + if (!buf) { + free_segments(idx); + return -ENOBUFS; + } + + link[idx].tx.buf[seg_id] = buf; + + seg_len = MIN(msg->len, CONT_PAYLOAD_MAX); + + BT_DBG("seg_id %u len %u: %s", seg_id, seg_len, + bt_hex(msg->data, seg_len)); + + net_buf_add_be32(buf, link[idx].link_id); + net_buf_add_u8(buf, xact_id); + net_buf_add_u8(buf, GPC_CONT(seg_id)); + net_buf_add_mem(buf, msg->data, seg_len); + net_buf_simple_pull(msg, seg_len); + } + + send_reliable(idx); + +#if defined(CONFIG_BLE_MESH_FAST_PROV) + if (link[idx].tx_pdu_type >= PROV_DATA) { + timeout = K_SECONDS(60); + } +#endif + if (!bt_mesh_atomic_test_and_set_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_submit(&link[idx].timeout, timeout); + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static int prov_send_gatt(const u8_t idx, struct net_buf_simple *msg) +{ + int err = 0; + + if (!link[idx].conn) { + return -ENOTCONN; + } + + err = bt_mesh_proxy_prov_client_send(link[idx].conn, BLE_MESH_PROXY_PROV, msg); + if (err) { + BT_ERR("%s, Failed to send PB-GATT pdu", __func__); + return err; + } + + if (!bt_mesh_atomic_test_and_set_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_submit(&link[idx].timeout, PROVISION_TIMEOUT); + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +static inline int prov_send(const u8_t idx, struct net_buf_simple *buf) +{ +#if defined(CONFIG_BLE_MESH_PB_ADV) + if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { + return prov_send_adv(idx, buf); + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (idx < BLE_MESH_PROV_SAME_TIME +#if defined(CONFIG_BLE_MESH_PB_ADV) + && idx >= CONFIG_BLE_MESH_PBA_SAME_TIME +#endif + ) { + return prov_send_gatt(idx, buf); + } +#endif + + BT_ERR("%s, Invalid link index %d", __func__, idx); + return -EINVAL; +} + +static void prov_buf_init(struct net_buf_simple *buf, u8_t type) +{ + net_buf_simple_reserve(buf, PROV_BUF_HEADROOM); + net_buf_simple_add_u8(buf, type); +} + +static void prov_invite(const u8_t idx, const u8_t *data) +{ + BT_DBG("%s", __func__); +} + +static void prov_start(const u8_t idx, const u8_t *data) +{ + BT_DBG("%s", __func__); +} + +static void prov_data(const u8_t idx, const u8_t *data) +{ + BT_DBG("%s", __func__); +} + +static void send_invite(const u8_t idx) +{ + PROV_BUF(buf, 2); + + prov_buf_init(&buf, PROV_INVITE); + + net_buf_simple_add_u8(&buf, prov->prov_attention); + + link[idx].conf_inputs[0] = prov->prov_attention; + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Invite", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + link[idx].expect = PROV_CAPABILITIES; +} + +static void prov_capabilities(const u8_t idx, const u8_t *data) +{ + PROV_BUF(buf, 6); + u16_t algorithms = 0U, output_action = 0U, input_action = 0U; + u8_t element_num = 0U, pub_key_oob = 0U, static_oob = 0U, + output_size = 0U, input_size = 0U; + u8_t auth_method = 0U, auth_action = 0U, auth_size = 0U; + + element_num = data[0]; + BT_INFO("Elements: 0x%02x", element_num); + if (!element_num) { + BT_ERR("%s, Invalid element number", __func__); + goto fail; + } + link[idx].element_num = element_num; + + algorithms = sys_get_be16(&data[1]); + BT_INFO("Algorithms: 0x%04x", algorithms); + if (algorithms != BIT(PROV_ALG_P256)) { + BT_ERR("%s, Invalid algorithms", __func__); + goto fail; + } + + pub_key_oob = data[3]; + BT_INFO("Public Key Type: 0x%02x", pub_key_oob); + if (pub_key_oob > 0x01) { + BT_ERR("%s, Invalid public key type", __func__); + goto fail; + } + pub_key_oob = ((prov->prov_pub_key_oob && + prov->prov_pub_key_oob_cb) ? pub_key_oob : 0x00); + + static_oob = data[4]; + BT_INFO("Static OOB Type: 0x%02x", static_oob); + if (static_oob > 0x01) { + BT_ERR("%s, Invalid Static OOB type", __func__); + goto fail; + } + static_oob = (prov_ctx.static_oob_len ? static_oob : 0x00); + + output_size = data[5]; + BT_INFO("Output OOB Size: 0x%02x", output_size); + if (output_size > 0x08) { + BT_ERR("%s, Invalid Output OOB size", __func__); + goto fail; + } + + output_action = sys_get_be16(&data[6]); + BT_INFO("Output OOB Action: 0x%04x", output_action); + if (output_action > 0x1f) { + BT_ERR("%s, Invalid Output OOB action", __func__); + goto fail; + } + + /* Provisioner select output action */ + if (prov->prov_input_num && output_size) { + output_action = __builtin_ctz(output_action); + } else { + output_size = 0x0; + output_action = 0x0; + } + + input_size = data[8]; + BT_INFO("Input OOB Size: 0x%02x", input_size); + if (input_size > 0x08) { + BT_ERR("%s, Invalid Input OOB size", __func__); + goto fail; + } + + input_action = sys_get_be16(&data[9]); + BT_INFO("Input OOB Action: 0x%04x", input_action); + if (input_action > 0x0f) { + BT_ERR("%s, Invalid Input OOB action", __func__); + goto fail; + } + + /* Make sure received pdu is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + /* Provisioner select input action */ + if (prov->prov_output_num && input_size) { + input_action = __builtin_ctz(input_action); + } else { + input_size = 0x0; + input_action = 0x0; + } + + if (static_oob) { + /* if static oob is valid, just use static oob */ + auth_method = AUTH_METHOD_STATIC; + auth_action = 0x00; + auth_size = 0x00; + } else { + if (!output_size && !input_size) { + auth_method = AUTH_METHOD_NO_OOB; + auth_action = 0x00; + auth_size = 0x00; + } else if (!output_size && input_size) { + auth_method = AUTH_METHOD_INPUT; + auth_action = (u8_t)input_action; + auth_size = input_size; + } else { + auth_method = AUTH_METHOD_OUTPUT; + auth_action = (u8_t)output_action; + auth_size = output_size; + } + } + + /* Store provisioning capabilities value in conf_inputs */ + memcpy(&link[idx].conf_inputs[1], data, 11); + + prov_buf_init(&buf, PROV_START); + net_buf_simple_add_u8(&buf, prov->prov_algorithm); + net_buf_simple_add_u8(&buf, pub_key_oob); + net_buf_simple_add_u8(&buf, auth_method); + net_buf_simple_add_u8(&buf, auth_action); + net_buf_simple_add_u8(&buf, auth_size); + + memcpy(&link[idx].conf_inputs[12], &buf.data[1], 5); + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Start", __func__); + goto fail; + } + + link[idx].auth_method = auth_method; + link[idx].auth_action = auth_action; + link[idx].auth_size = auth_size; + + /** After prov start sent, use OOB to get remote public key. + * And we just follow the procedure in Figure 5.15 of Section + * 5.4.2.3 of Mesh Profile Spec. + */ + if (pub_key_oob) { + if (prov->prov_pub_key_oob_cb(idx)) { + BT_ERR("%s, Failed to notify input OOB Public Key", __func__); + goto fail; + } + } + + /** If using PB-ADV, need to listen for transaction ack, + * after ack is received, provisioner can send public key. + */ +#if defined(CONFIG_BLE_MESH_PB_ADV) + if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { + link[idx].expect_ack_for = PROV_START; + return; + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + send_pub_key(idx, pub_key_oob); + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static bt_mesh_output_action_t output_action(u8_t action) +{ + switch (action) { + case OUTPUT_OOB_BLINK: + return BLE_MESH_BLINK; + case OUTPUT_OOB_BEEP: + return BLE_MESH_BEEP; + case OUTPUT_OOB_VIBRATE: + return BLE_MESH_VIBRATE; + case OUTPUT_OOB_NUMBER: + return BLE_MESH_DISPLAY_NUMBER; + case OUTPUT_OOB_STRING: + return BLE_MESH_DISPLAY_STRING; + default: + return BLE_MESH_NO_OUTPUT; + } +} + +static bt_mesh_input_action_t input_action(u8_t action) +{ + switch (action) { + case INPUT_OOB_PUSH: + return BLE_MESH_PUSH; + case INPUT_OOB_TWIST: + return BLE_MESH_TWIST; + case INPUT_OOB_NUMBER: + return BLE_MESH_ENTER_NUMBER; + case INPUT_OOB_STRING: + return BLE_MESH_ENTER_STRING; + default: + return BLE_MESH_NO_INPUT; + } +} + +static int prov_auth(const u8_t idx, u8_t method, u8_t action, u8_t size) +{ + bt_mesh_output_action_t output = 0U; + bt_mesh_input_action_t input = 0U; + + link[idx].auth = (u8_t *)bt_mesh_calloc(PROV_AUTH_VAL_SIZE); + if (!link[idx].auth) { + BT_ERR("%s, Failed to allocate memory", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return -ENOMEM; + } + + switch (method) { + case AUTH_METHOD_NO_OOB: + if (action || size) { + return -EINVAL; + } + memset(link[idx].auth, 0, 16); + return 0; + + case AUTH_METHOD_STATIC: + if (action || size) { + return -EINVAL; + } + memcpy(link[idx].auth + 16 - prov_ctx.static_oob_len, + prov_ctx.static_oob_val, prov_ctx.static_oob_len); + memset(link[idx].auth, 0, 16 - prov_ctx.static_oob_len); + return 0; + + case AUTH_METHOD_OUTPUT: + /* Use auth_action to get device output action */ + output = output_action(action); + if (!output) { + return -EINVAL; + } + return prov->prov_input_num(AUTH_METHOD_OUTPUT, output, size, idx); + + case AUTH_METHOD_INPUT: + /* Use auth_action to get device input action */ + input = input_action(action); + if (!input) { + return -EINVAL; + } + + /* Provisioner ouput number/string and wait for device's Provisioning Input Complete PDU */ + link[idx].expect = PROV_INPUT_COMPLETE; + + if (input == BLE_MESH_ENTER_STRING) { + unsigned char str[9] = {'\0'}; + u8_t j = 0U; + + bt_mesh_rand(str, size); + + /* Normalize to '0' .. '9' & 'A' .. 'Z' */ + for (j = 0U; j < size; j++) { + str[j] %= 36; + if (str[j] < 10) { + str[j] += '0'; + } else { + str[j] += 'A' - 10; + } + } + str[size] = '\0'; + + memcpy(link[idx].auth, str, size); + memset(link[idx].auth + size, 0, PROV_AUTH_VAL_SIZE - size); + + return prov->prov_output_num(AUTH_METHOD_INPUT, input, str, size, idx); + } else { + u32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; + u32_t num = 0U; + + bt_mesh_rand(&num, sizeof(num)); + num %= div[size - 1]; + + sys_put_be32(num, &link[idx].auth[12]); + memset(link[idx].auth, 0, 12); + + return prov->prov_output_num(AUTH_METHOD_INPUT, input, &num, size, idx); + } + + default: + return -EINVAL; + } +} + +static void send_confirm(const u8_t idx) +{ + PROV_BUF(buf, 17); + + BT_DBG("ConfInputs[0] %s", bt_hex(link[idx].conf_inputs, 64)); + BT_DBG("ConfInputs[64] %s", bt_hex(link[idx].conf_inputs + 64, 64)); + BT_DBG("ConfInputs[128] %s", bt_hex(link[idx].conf_inputs + 128, 17)); + + link[idx].conf_salt = (u8_t *)bt_mesh_calloc(PROV_CONF_SALT_SIZE); + if (!link[idx].conf_salt) { + BT_ERR("%s, Failed to allocate memory", __func__); + goto fail; + } + + link[idx].conf_key = (u8_t *)bt_mesh_calloc(PROV_CONF_KEY_SIZE); + if (!link[idx].conf_key) { + BT_ERR("%s, Failed to allocate memory", __func__); + goto fail; + } + + if (bt_mesh_prov_conf_salt(link[idx].conf_inputs, link[idx].conf_salt)) { + BT_ERR("%s, Failed to generate confirmation salt", __func__); + goto fail; + } + + BT_DBG("ConfirmationSalt: %s", bt_hex(link[idx].conf_salt, 16)); + + if (bt_mesh_prov_conf_key(link[idx].dhkey, link[idx].conf_salt, link[idx].conf_key)) { + BT_ERR("%s, Failed to generate confirmation key", __func__); + goto fail; + } + + BT_DBG("ConfirmationKey: %s", bt_hex(link[idx].conf_key, 16)); + + /** Provisioner use the same random number for each provisioning + * device, if different random need to be used, here provisioner + * should allocate memory for rand and call bt_mesh_rand() every time. + */ + if (!(prov_ctx.rand_gen_done & BIT(0))) { + if (bt_mesh_rand(prov_ctx.random, 16)) { + BT_ERR("%s, Failed to generate random number", __func__); + goto fail; + } + link[idx].rand = prov_ctx.random; + prov_ctx.rand_gen_done |= BIT(0); + } else { + /* Provisioner random has already been generated. */ + link[idx].rand = prov_ctx.random; + } + + BT_DBG("LocalRandom: %s", bt_hex(link[idx].rand, 16)); + + prov_buf_init(&buf, PROV_CONFIRM); + + if (bt_mesh_prov_conf(link[idx].conf_key, link[idx].rand, link[idx].auth, + net_buf_simple_add(&buf, 16))) { + BT_ERR("%s, Failed to generate confirmation value", __func__); + goto fail; + } + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Confirm", __func__); + goto fail; + } + + link[idx].expect = PROV_CONFIRM; + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag) +{ + /** This function should be called in the prov_input_num + * callback, after the data output by device has been + * input by provisioner. + * Paramter size is used to indicate the length of data + * indicated by Pointer val, for example, if device output + * data is 12345678(decimal), the data in auth value will + * be 0xBC614E. + * Parameter num_flag is used to indicate whether the value + * input by provisioner is number or string. + */ + if (!link[idx].auth) { + BT_ERR("%s, Link auth is NULL", __func__); + return -EINVAL; + } + + BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + + memset(link[idx].auth, 0, 16); + if (num_flag) { + /* Provisioner inputs number */ + memcpy(link[idx].auth + 12, val, sizeof(u32_t)); + } else { + /* Provisioner inputs string */ + memcpy(link[idx].auth, val, link[idx].auth_size); + } + + send_confirm(idx); + return 0; +} + +int bt_mesh_provisioner_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag) +{ + /** This function should be called in the prov_output_num + * callback, after the data has been output by provisioner. + * Parameter size is used to indicate the length of data + * indicated by Pointer num, for example, if provisioner + * output data is 12345678(decimal), the data in auth value + * will be 0xBC614E. + * Parameter num_flag is used to indicate whether the value + * output by provisioner is number or string. + */ + if (num == NULL || size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (!link[idx].auth) { + BT_ERR("%s, link auth is NULL", __func__); + return -EINVAL; + } + + BT_INFO("Link idx %d, type %s", idx, num_flag ? "number" : "string"); + + if (num_flag) { + /* Provisioner output number */ + memset(link[idx].auth, 0, 16); + memcpy(link[idx].auth + 16 - size, num, size); + } else { + /* Provisioner output string */ + memset(link[idx].auth, 0, 16); + memcpy(link[idx].auth, num, size); + } + + link[idx].expect = PROV_INPUT_COMPLETE; + + return 0; +} + +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]) +{ + if (!link[idx].conf_inputs) { + BT_ERR("%s, Link conf_inputs is NULL", __func__); + return -EINVAL; + } + + /* Swap X and Y halves independently to big-endian */ + sys_memcpy_swap(&link[idx].conf_inputs[81], pub_key_x, 32); + sys_memcpy_swap(&link[idx].conf_inputs[81] + 32, pub_key_y, 32); + + bt_mesh_atomic_set_bit(link[idx].flags, REMOTE_PUB_KEY); + + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, WAIT_GEN_DHKEY)) { + prov_gen_dh_key(idx); + } + + return 0; +} + +static void prov_dh_key_cb(const u8_t key[32], const u8_t idx) +{ + BT_DBG("%p", key); + + if (!key) { + BT_ERR("%s, Failed to generate DHKey", __func__); + goto fail; + } + + link[idx].dhkey = (u8_t *)bt_mesh_calloc(PROV_DH_KEY_SIZE); + if (!link[idx].dhkey) { + BT_ERR("%s, Failed to allocate memory", __func__); + goto fail; + } + sys_memcpy_swap(link[idx].dhkey, key, 32); + + BT_DBG("DHkey: %s", bt_hex(link[idx].dhkey, 32)); + + bt_mesh_atomic_set_bit(link[idx].flags, HAVE_DHKEY); + + /** After dhkey is generated, if auth_method is No OOB or + * Static OOB, provisioner can start to send confirmation. + * If output OOB is used by the device, provisioner need + * to watch out the output number and input it as auth_val. + * If input OOB is used by the device, provisioner need + * to output a value, and wait for prov input complete pdu. + */ + if (prov_auth(idx, link[idx].auth_method, + link[idx].auth_action, link[idx].auth_size) < 0) { + BT_ERR("%s, Failed to authenticate", __func__); + goto fail; + } + if (link[idx].auth_method == AUTH_METHOD_OUTPUT || + link[idx].auth_method == AUTH_METHOD_INPUT) { + return; + } + + if (link[idx].expect != PROV_INPUT_COMPLETE) { + send_confirm(idx); + } + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static void prov_gen_dh_key(const u8_t idx) +{ + u8_t pub_key[64] = {0}; + + /* Copy device public key in little-endian for bt_mesh_dh_key_gen(). + * X and Y halves are swapped independently. + */ + sys_memcpy_swap(&pub_key[0], &link[idx].conf_inputs[81], 32); + sys_memcpy_swap(&pub_key[32], &link[idx].conf_inputs[113], 32); + + if (bt_mesh_dh_key_gen(pub_key, prov_dh_key_cb, idx)) { + BT_ERR("%s, Failed to generate DHKey", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } +} + +static void send_pub_key(const u8_t idx, u8_t oob) +{ + PROV_BUF(buf, 65); + const u8_t *key = NULL; + + key = bt_mesh_pub_key_get(); + if (!key) { + BT_ERR("%s, No public key available", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + BT_DBG("Local Public Key: %s", bt_hex(key, 64)); + + bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); + + prov_buf_init(&buf, PROV_PUB_KEY); + + /* Swap X and Y halves independently to big-endian */ + sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32); + sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32); + + /* Store provisioner public key value in conf_inputs */ + memcpy(&link[idx].conf_inputs[17], &buf.data[1], 64); + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Public Key", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (!oob) { + link[idx].expect = PROV_PUB_KEY; + } else { + /** Have already got device public key. If next is to + * send confirm(not wait for input complete), need to + * wait for transactiona ack for public key then send + * provisioning confirm pdu. + */ +#if defined(CONFIG_BLE_MESH_PB_ADV) + if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { + link[idx].expect_ack_for = PROV_PUB_KEY; + return; + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + /* If remote public key has been read, then start to generate DHkey, + * otherwise wait for device oob public key. + */ + if (bt_mesh_atomic_test_bit(link[idx].flags, REMOTE_PUB_KEY)) { + prov_gen_dh_key(idx); + } else { + bt_mesh_atomic_set_bit(link[idx].flags, WAIT_GEN_DHKEY); + } + } +} + +static void prov_pub_key(const u8_t idx, const u8_t *data) +{ + BT_DBG("Remote Public Key: %s", bt_hex(data, 64)); + + /* Make sure received pdu is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + memcpy(&link[idx].conf_inputs[81], data, 64); + + if (!bt_mesh_atomic_test_bit(link[idx].flags, LOCAL_PUB_KEY)) { + /* Clear retransmit timer */ +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(idx); +#endif + bt_mesh_atomic_set_bit(link[idx].flags, REMOTE_PUB_KEY); + BT_WARN("Waiting for local public key"); + return; + } + + prov_gen_dh_key(idx); +} + +static void prov_input_complete(const u8_t idx, const u8_t *data) +{ + /* Make sure received pdu is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + /* Provisioner receives input complete and send confirm */ + send_confirm(idx); +} + +static void prov_confirm(const u8_t idx, const u8_t *data) +{ + /** + * Zephyr uses PROV_BUF(16). Currently test with PROV_BUF(16) + * and PROV_BUF(17) on branch feature/btdm_ble_mesh_debug both + * work fine. + */ + PROV_BUF(buf, 17); + + BT_DBG("Remote Confirm: %s", bt_hex(data, 16)); + + /* Make sure received pdu is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + link[idx].conf = (u8_t *)bt_mesh_calloc(PROV_CONFIRM_SIZE); + if (!link[idx].conf) { + BT_ERR("%s, Failed to allocate memory", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + memcpy(link[idx].conf, data, 16); + + if (!bt_mesh_atomic_test_bit(link[idx].flags, HAVE_DHKEY)) { +#if defined(CONFIG_BLE_MESH_PB_ADV) + prov_clear_tx(idx); +#endif + bt_mesh_atomic_set_bit(link[idx].flags, SEND_CONFIRM); + } + + prov_buf_init(&buf, PROV_RANDOM); + + net_buf_simple_add_mem(&buf, link[idx].rand, 16); + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Random", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + link[idx].expect = PROV_RANDOM; +} + +static void send_prov_data(const u8_t idx) +{ + PROV_BUF(buf, 34); + u16_t prev_addr = BLE_MESH_ADDR_UNASSIGNED; + u16_t max_addr = BLE_MESH_ADDR_UNASSIGNED; + struct bt_mesh_node *node = NULL; + const u8_t *netkey = NULL; + u8_t session_key[16] = {0}; + u8_t nonce[13] = {0}; + u8_t pdu[25] = {0}; + int err = 0; + + err = bt_mesh_session_key(link[idx].dhkey, link[idx].prov_salt, session_key); + if (err) { + BT_ERR("%s, Failed to generate session key", __func__); + goto fail; + } + BT_DBG("SessionKey: %s", bt_hex(session_key, 16)); + + err = bt_mesh_prov_nonce(link[idx].dhkey, link[idx].prov_salt, nonce); + if (err) { + BT_ERR("%s, Failed to generate session nonce", __func__); + goto fail; + } + BT_DBG("Nonce: %s", bt_hex(nonce, 13)); + + /* Assign provisioning data for the device. Currently all provisioned devices + * will be added to the primary subnet, and may add an API to choose to which + * subnet will the device be provisioned later. + */ + if (FAST_PROV_ENABLE()) { + netkey = prov_ctx.fast_prov.net_key; + if (!netkey) { + BT_ERR("%s, Failed to get NetKey for fast provisioning", __func__); + goto fail; + } + memcpy(pdu, netkey, 16); + sys_put_be16(prov_ctx.fast_prov.net_idx, &pdu[16]); + pdu[18] = prov_ctx.fast_prov.flags; + sys_put_be32(prov_ctx.fast_prov.iv_index, &pdu[19]); + } else { + netkey = bt_mesh_provisioner_net_key_get(prov_ctx.curr_net_idx); + if (!netkey) { + BT_ERR("%s, Failed to get NetKey for provisioning data", __func__); + goto fail; + } + memcpy(pdu, netkey, 16); + sys_put_be16(prov_ctx.curr_net_idx, &pdu[16]); + pdu[18] = prov_ctx.curr_flags; + sys_put_be32(prov_ctx.curr_iv_index, &pdu[19]); + } + + /** + * The Provisioner must not reuse unicast addresses that have been + * allocated to a device and sent in a Provisioning Data PDU until + * the Provisioner receives an Unprovisioned Device beacon or + * Service Data for the Mesh Provisioning Service from that same + * device, identified using the Device UUID of the device. + */ + + /* Check if this device is a re-provisioned device */ + node = bt_mesh_provisioner_get_node_with_uuid(link[idx].uuid); + if (node) { + if (link[idx].element_num <= node->element_num) { + /** + * If the device is provisioned before, but the element number of + * the device is bigger now, then we treat it as a new device. + */ + prev_addr = node->unicast_addr; + } + bt_mesh_provisioner_remove_node(link[idx].uuid); + } + + max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : PROV_MAX_ADDR_TO_ASSIGN; + + if (BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { + sys_put_be16(prev_addr, &pdu[23]); + link[idx].unicast_addr = prev_addr; + } else { + u16_t alloc_addr = BLE_MESH_ADDR_UNASSIGNED; + + if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { + alloc_addr = link[idx].assign_addr; + } else { + /* If this device to be provisioned is a new device */ + if (prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("%s, No unicast address can be allocated", __func__); + goto fail; + } + alloc_addr = prov_ctx.curr_alloc_addr; + } + + if (alloc_addr + link[idx].element_num - 1 > max_addr) { + BT_ERR("%s, Not enough unicast address for the device", __func__); + goto fail; + } + + /* Make sure the assigned unicast address is not identical with any unicast + * address of other nodes. And make sure the address is not identical with + * any unicast address of Provisioner. + */ + if (bt_mesh_provisioner_check_is_addr_dup(alloc_addr, link[idx].element_num, true)) { + BT_ERR("%s, Assigned address 0x%04x is duplicated", __func__, alloc_addr); + goto fail; + } + + sys_put_be16(alloc_addr, &pdu[23]); + link[idx].unicast_addr = alloc_addr; + } + + prov_buf_init(&buf, PROV_DATA); + + err = bt_mesh_prov_encrypt(session_key, nonce, pdu, net_buf_simple_add(&buf, 33)); + if (err) { + BT_ERR("%s, Failed to encrypt provisioning data", __func__); + goto fail; + } + + if (prov_send(idx, &buf)) { + BT_ERR("%s, Failed to send Provisioning Data", __func__); + goto fail; + } + + /** + * We update the next unicast address to be allocated here because if + * Provisioner is provisioning two devices at the same time, we need + * to assign the unicast address for them correctly. Hence we should + * not update the prov_ctx.curr_alloc_addr after the proper provisioning + * complete pdu is received. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(prev_addr)) { + if (BLE_MESH_ADDR_IS_UNICAST(link[idx].assign_addr)) { + /* Even if the unicast address of the node is assigned by the + * application, we will also update the prov_ctx.curr_alloc_addr + * here, in case Users use the two methods together (i.e. allocate + * the unicast address for the node internally and assign the + * unicast address for the node from application). + */ + if (prov_ctx.curr_alloc_addr < link[idx].assign_addr + link[idx].element_num) { + prov_ctx.curr_alloc_addr = link[idx].assign_addr + link[idx].element_num; + } + } else { + prov_ctx.curr_alloc_addr += link[idx].element_num; + if (prov_ctx.curr_alloc_addr > max_addr) { + /* No unicast address will be used for further provisioning */ + prov_ctx.curr_alloc_addr = BLE_MESH_ADDR_UNASSIGNED; + } + } + /* Store the available unicast address range to flash */ + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr); + } + } + + if (FAST_PROV_ENABLE()) { + link[idx].ki_flags = prov_ctx.fast_prov.flags; + link[idx].iv_index = prov_ctx.fast_prov.iv_index; + } else { + link[idx].ki_flags = prov_ctx.curr_flags; + link[idx].iv_index = prov_ctx.curr_iv_index; + } + + link[idx].expect = PROV_COMPLETE; + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static void prov_random(const u8_t idx, const u8_t *data) +{ + u8_t conf_verify[16] = {0}; + + BT_DBG("Remote Random: %s", bt_hex(data, 16)); + + if (bt_mesh_prov_conf(link[idx].conf_key, data, link[idx].auth, conf_verify)) { + BT_ERR("%s, Failed to calculate confirmation verification", __func__); + goto fail; + } + + if (memcmp(conf_verify, link[idx].conf, 16)) { + BT_ERR("%s, Invalid confirmation value", __func__); + BT_DBG("Received: %s", bt_hex(link[idx].conf, 16)); + BT_DBG("Calculated: %s", bt_hex(conf_verify, 16)); + goto fail; + } + + /*Verify received confirm is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + /** After provisioner receives provisioning random from device, + * and successfully check the confirmation, the following + * should be done: + * 1. bt_mesh_calloc memory for prov_salt + * 2. calculate prov_salt + * 3. prepare provisioning data and send + */ + link[idx].prov_salt = (u8_t *)bt_mesh_calloc(PROV_PROV_SALT_SIZE); + if (!link[idx].prov_salt) { + BT_ERR("%s, Failed to allocate memory", __func__); + goto fail; + } + + if (bt_mesh_prov_salt(link[idx].conf_salt, link[idx].rand, data, + link[idx].prov_salt)) { + BT_ERR("%s, Failed to generate ProvisioningSalt", __func__); + goto fail; + } + + BT_DBG("ProvisioningSalt: %s", bt_hex(link[idx].prov_salt, 16)); + + send_prov_data(idx); + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static void prov_complete(const u8_t idx, const u8_t *data) +{ + u8_t device_key[16] = {0}; + u16_t net_idx = 0U; + u16_t index = 0U; + u16_t rm = 0U; + int err = 0; + + /* Make sure received pdu is ok and cancel the timeout timer */ + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + err = bt_mesh_dev_key(link[idx].dhkey, link[idx].prov_salt, device_key); + if (err) { + BT_ERR("%s, Failed to generate device key", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (FAST_PROV_ENABLE()) { + net_idx = prov_ctx.fast_prov.net_idx; + } else { + net_idx = prov_ctx.curr_net_idx; + } + err = bt_mesh_provisioner_provision(&link[idx].addr, link[idx].uuid, link[idx].oob_info, + link[idx].unicast_addr, link[idx].element_num, net_idx, + link[idx].ki_flags, link[idx].iv_index, device_key, &index); + if (err) { + BT_ERR("%s, Failed to store node info", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (prov->prov_complete) { + prov->prov_complete(index, link[idx].uuid, link[idx].unicast_addr, + link[idx].element_num, net_idx); + } + + err = provisioner_dev_find(&link[idx].addr, link[idx].uuid, &rm); + if (!err) { + if (unprov_dev[rm].flags & RM_AFTER_PROV) { + memset(&unprov_dev[rm], 0, sizeof(struct unprov_dev_queue)); + } + } else if (err == -ENODEV) { + BT_DBG("Device is not found in queue"); + } else { + BT_ERR("Failed to remove device from queue"); + } + + close_link(idx, CLOSE_REASON_SUCCESS); +} + +static void prov_failed(const u8_t idx, const u8_t *data) +{ + BT_WARN("%s, Error 0x%02x", __func__, data[0]); + + close_link(idx, CLOSE_REASON_FAILED); +} + +static const struct { + void (*func)(const u8_t idx, const u8_t *data); + u16_t len; +} prov_handlers[] = { + { prov_invite, 1 }, + { prov_capabilities, 11 }, + { prov_start, 5 }, + { prov_pub_key, 64 }, + { prov_input_complete, 0 }, + { prov_confirm, 16 }, + { prov_random, 16 }, + { prov_data, 33 }, + { prov_complete, 0 }, + { prov_failed, 1 }, +}; + +static void close_link(const u8_t idx, u8_t reason) +{ +#if defined(CONFIG_BLE_MESH_PB_ADV) + if (idx < CONFIG_BLE_MESH_PBA_SAME_TIME) { + bearer_ctl_send(idx, LINK_CLOSE, &reason, sizeof(reason)); + return; + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (idx < BLE_MESH_PROV_SAME_TIME +#if defined(CONFIG_BLE_MESH_PB_ADV) + && idx >= CONFIG_BLE_MESH_PBA_SAME_TIME +#endif + ) { + if (link[idx].conn) { + bt_mesh_gattc_disconnect(link[idx].conn); + } + return; + } +#endif + + BT_ERR("%s, Invalid link index %d", __func__, idx); + return; +} + +static void prov_timeout(struct k_work *work) +{ + u8_t idx = (u8_t)work->index; + + BT_WARN("%s", __func__); + + close_link(idx, CLOSE_REASON_TIMEOUT); +} + +#if defined(CONFIG_BLE_MESH_PB_ADV) +static void prov_retransmit(struct k_work *work) +{ + s64_t timeout = TRANSACTION_TIMEOUT; + u8_t idx = (u8_t)work->index; + int i; + + BT_DBG("%s", __func__); + + if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { + BT_WARN("Link is not active"); + return; + } + +#if defined(CONFIG_BLE_MESH_FAST_PROV) + if (link[idx].tx_pdu_type >= PROV_DATA) { + timeout = K_SECONDS(30); + } +#endif + if (k_uptime_get() - link[idx].tx.start > timeout) { + BT_WARN("Provisioner timeout, giving up transaction"); + reset_link(idx, CLOSE_REASON_TIMEOUT); + return; + } + + if (link[idx].send_link_close & BIT(0)) { + u8_t reason = (link[idx].send_link_close >> 1) & BIT_MASK(2); + u16_t count = (link[idx].send_link_close >> 3); + if (count >= 2) { + reset_link(idx, reason); + return; + } + link[idx].send_link_close += BIT(3); + } + + bt_mesh_pb_buf_lock(); + + for (i = 0; i < ARRAY_SIZE(link[idx].tx.buf); i++) { + struct net_buf *buf = link[idx].tx.buf[i]; + + if (!buf) { + break; + } + + if (BLE_MESH_ADV(buf)->busy) { + continue; + } + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (i + 1 < ARRAY_SIZE(link[idx].tx.buf) && link[idx].tx.buf[i + 1]) { + bt_mesh_adv_send(buf, NULL, NULL); + } else { + bt_mesh_adv_send(buf, &buf_sent_cb, (void *)(int)idx); + } + } + + bt_mesh_pb_buf_unlock(); +} + +static void link_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("len %u", buf->len); + + if (buf->len) { + BT_ERR("%s, Invalid Link ACK length", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (link[idx].expect == PROV_CAPABILITIES) { + BT_INFO("%s, Link ACK is already received", __func__); + return; + } + + link[idx].conf_inputs = (u8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); + if (!link[idx].conf_inputs) { + BT_ERR("%s, Failed to allocate memory", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + send_invite(idx); +} + +static void link_close(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + u8_t reason = 0U; + + BT_DBG("len %u", buf->len); + + reason = net_buf_simple_pull_u8(buf); + + reset_link(idx, reason); +} + +static void gen_prov_ctl(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len); + + switch (BEARER_CTL(rx->gpc)) { + case LINK_OPEN: + break; + + case LINK_ACK: + if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { + return; + } + link_ack(idx, rx, buf); + break; + + case LINK_CLOSE: + if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE)) { + return; + } + link_close(idx, rx, buf); + break; + + default: + BT_ERR("%s, Unknown bearer opcode 0x%02x", __func__, BEARER_CTL(rx->gpc)); + return; + } +} + +static void prov_msg_recv(const u8_t idx) +{ + u8_t type = link[idx].rx.buf->data[0]; + + BT_DBG("type 0x%02x len %u", type, link[idx].rx.buf->len); + + /** + * Provisioner first checks information within the received + * Provisioning PDU. If the check succeeds then check fcs. + */ + if (type != PROV_FAILED && type != link[idx].expect) { + BT_ERR("%s, Unexpected msg 0x%02x != 0x%02x", __func__, type, link[idx].expect); + goto fail; + } + + if (type >= 0x0A) { + BT_ERR("%s, Unknown provisioning PDU type 0x%02x", __func__, type); + goto fail; + } + + if (1 + prov_handlers[type].len != link[idx].rx.buf->len) { + BT_ERR("%s, Invalid length %u for type 0x%02x", __func__, link[idx].rx.buf->len, type); + goto fail; + } + + if (!bt_mesh_fcs_check(link[idx].rx.buf, link[idx].rx.fcs)) { + BT_ERR("%s, Incorrect FCS", __func__); + goto fail; + } + + gen_prov_ack_send(idx, link[idx].rx.trans_id); + link[idx].rx.prev_id = link[idx].rx.trans_id; + link[idx].rx.trans_id = 0; + + prov_handlers[type].func(idx, &link[idx].rx.buf->data[1]); + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static void gen_prov_cont(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + u8_t seg = CONT_SEG_INDEX(rx->gpc); + + BT_DBG("len %u, seg_index %u", buf->len, seg); + + if (!link[idx].rx.seg && link[idx].rx.prev_id == rx->xact_id) { + BT_INFO("%s, Resending ack", __func__); + gen_prov_ack_send(idx, rx->xact_id); + return; + } + + if (rx->xact_id != link[idx].rx.trans_id) { + BT_WARN("Data for unknown transaction (%u != %u)", + rx->xact_id, link[idx].rx.trans_id); + return; + } + + if (seg > link[idx].rx.last_seg) { + BT_ERR("%s, Invalid segment index %u", __func__, seg); + goto fail; + } else if (seg == link[idx].rx.last_seg) { + u8_t expect_len = 0U; + + expect_len = (link[idx].rx.buf->len - 20 - + (23 * (link[idx].rx.last_seg - 1))); + if (expect_len != buf->len) { + BT_ERR("%s, Incorrect last seg len: %u != %u", + __func__, expect_len, buf->len); + goto fail; + } + } + + if (!(link[idx].rx.seg & BIT(seg))) { + BT_INFO("%s, Ignore already received segment", __func__); + return; + } + + memcpy(XACT_SEG_DATA(idx, seg), buf->data, buf->len); + XACT_SEG_RECV(idx, seg); + + if (!link[idx].rx.seg) { + prov_msg_recv(idx); + } + return; + +fail: + close_link(idx, CLOSE_REASON_FAILED); + return; +} + +static void gen_prov_ack(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + u8_t ack_type = 0U, pub_key_oob = 0U; + + BT_DBG("len %u", buf->len); + + if (!link[idx].tx.buf[0]) { + return; + } + + if (!link[idx].tx.trans_id) { + return; + } + + if (rx->xact_id == (link[idx].tx.trans_id - 1)) { + prov_clear_tx(idx); + + ack_type = link[idx].expect_ack_for; + switch (ack_type) { + case PROV_START: + pub_key_oob = link[idx].conf_inputs[13]; + send_pub_key(idx, pub_key_oob); + break; + case PROV_PUB_KEY: + prov_gen_dh_key(idx); + break; + default: + break; + } + link[idx].expect_ack_for = 0x00; + } +} + +static void gen_prov_start(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + if (link[idx].rx.seg) { + BT_INFO("%s, Get Start while there are unreceived segments", __func__); + return; + } + + if (link[idx].rx.prev_id == rx->xact_id) { + BT_INFO("%s, Resending ack", __func__); + gen_prov_ack_send(idx, rx->xact_id); + return; + } + + link[idx].rx.buf->len = net_buf_simple_pull_be16(buf); + link[idx].rx.trans_id = rx->xact_id; + link[idx].rx.fcs = net_buf_simple_pull_u8(buf); + + BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, + START_LAST_SEG(rx->gpc), link[idx].rx.buf->len, link[idx].rx.fcs); + + /* Provisioner can not receive zero-length provisioning pdu */ + if (link[idx].rx.buf->len < 1) { + BT_ERR("%s, Ignoring zero-length provisioning PDU", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (link[idx].rx.buf->len > link[idx].rx.buf->size) { + BT_ERR("%s, Too large provisioning PDU (%u bytes)", + __func__, link[idx].rx.buf->len); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + if (START_LAST_SEG(rx->gpc) > 0 && link[idx].rx.buf->len <= 20) { + BT_ERR("%s, Too small total length for multi-segment PDU", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + link[idx].rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; + link[idx].rx.last_seg = START_LAST_SEG(rx->gpc); + memcpy(link[idx].rx.buf->data, buf->data, buf->len); + XACT_SEG_RECV(idx, 0); + + if (!link[idx].rx.seg) { + prov_msg_recv(idx); + } +} + +static const struct { + void (*const func)(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf); + const u8_t require_link; + const u8_t min_len; +} gen_prov[] = { + { gen_prov_start, true, 3 }, + { gen_prov_ack, true, 0 }, + { gen_prov_cont, true, 0 }, + { gen_prov_ctl, true, 0 }, +}; + +static void gen_prov_recv(const u8_t idx, struct prov_rx *rx, struct net_buf_simple *buf) +{ + if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) { + BT_ERR("%s, Too short GPC message type %u", __func__, GPCF(rx->gpc)); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + /** + * require_link can be used combining with link[].linking flag to + * set LINK_ACTIVE status after Link ACK is received. In this case + * there is no need to check LINK_ACTIVE status in find_link(). + */ + if (!bt_mesh_atomic_test_bit(link[idx].flags, LINK_ACTIVE) && + gen_prov[GPCF(rx->gpc)].require_link) { + BT_DBG("Ignoring message that requires active link"); + return; + } + + gen_prov[GPCF(rx->gpc)].func(idx, rx, buf); +} + +static int find_link(u32_t link_id, u8_t *idx) +{ + int i; + + /* link for PB-ADV is from 0 to CONFIG_BLE_MESH_PBA_SAME_TIME */ + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + if (link[i].link_id == link_id) { + if (idx) { + *idx = i; + } + return 0; + } + } + } + + return -1; +} + +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf) +{ + struct prov_rx rx = {0}; + u8_t idx = 0U; + + rx.link_id = net_buf_simple_pull_be32(buf); + if (find_link(rx.link_id, &idx) < 0) { + BT_DBG("%s, Data for unexpected link", __func__); + return; + } + + if (buf->len < 2) { + BT_ERR("%s, Too short provisioning packet (len %u)", __func__, buf->len); + close_link(idx, CLOSE_REASON_FAILED); + return; + } + + rx.xact_id = net_buf_simple_pull_u8(buf); + rx.gpc = net_buf_simple_pull_u8(buf); + + BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id); + + gen_prov_recv(idx, &rx, buf); +} +#endif /* CONFIG_BLE_MESH_PB_ADV */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static struct bt_mesh_conn *find_conn(struct bt_mesh_conn *conn, u8_t *idx) +{ + int i; + + /* link for PB-GATT is from CONFIG_BLE_MESH_PBA_SAME_TIME to BLE_MESH_PROV_SAME_TIME */ + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + if (link[i].conn == conn) { + if (idx) { + *idx = i; + } + return conn; + } + } + } + + return NULL; +} + +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf) +{ + u8_t type = 0U; + u8_t idx = 0U; + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (!find_conn(conn, &idx)) { + BT_ERR("%s, Data for unexpected connection", __func__); + return -ENOTCONN; + } + + if (buf->len < 1) { + BT_ERR("%s, Too short provisioning packet (len %u)", __func__, buf->len); + goto fail; + } + + type = net_buf_simple_pull_u8(buf); + if (type != PROV_FAILED && type != link[idx].expect) { + BT_ERR("%s, Unexpected msg 0x%02x != 0x%02x", __func__, type, link[idx].expect); + goto fail; + } + + if (type >= 0x0A) { + BT_ERR("%s, Unknown provisioning PDU type 0x%02x", __func__, type); + goto fail; + } + + if (prov_handlers[type].len != buf->len) { + BT_ERR("%s, Invalid length %u for type 0x%02x", __func__, buf->len, type); + goto fail; + } + + prov_handlers[type].func(idx, buf->data); + + return 0; + +fail: + /* Mesh Spec Section 5.4.4 Provisioning errors */ + close_link(idx, CLOSE_REASON_FAILED); + return -EINVAL; +} + +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn) +{ + int i; + + if (!addr || !conn) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (!memcmp(link[i].addr.val, addr, BLE_MESH_ADDR_LEN)) { + link[i].conn = bt_mesh_conn_ref(conn); + return 0; + } + } + + BT_ERR("%s, Address %s is not found", __func__, bt_hex(addr, BLE_MESH_ADDR_LEN)); + return -ENOMEM; +} + +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr) +{ + u8_t idx = 0U; + int i; + + BT_DBG("conn %p", conn); + + /** + * Double check if the device is currently being provisioned using PB-ADV. + * Provisioner binds conn with proper device when proxy_prov_connected() + * is invoked, and here after proper GATT procedures are completed, we just + * check if this conn already exists in the proxy servers array. + */ + for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) { + if (link[i].conn == conn) { + idx = i; + break; + } + } + + if (i == BLE_MESH_PROV_SAME_TIME) { + BT_ERR("%s, Link is not found", __func__); + return -ENOTCONN; + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + if (bt_mesh_atomic_test_bit(link[i].flags, LINK_ACTIVE)) { + if (!memcmp(link[i].uuid, link[idx].uuid, 16)) { + BT_WARN("Provision using PB-GATT & PB-ADV same time"); + close_link(idx, CLOSE_REASON_FAILED); + return -EALREADY; + } + } + } +#endif + + bt_mesh_atomic_set_bit(link[idx].flags, LINK_ACTIVE); + link[idx].conn = bt_mesh_conn_ref(conn); + + /* May use lcd to indicate starting provisioning each device */ + if (prov->prov_link_open) { + prov->prov_link_open(BLE_MESH_PROV_GATT); + } + + link[idx].conf_inputs = (u8_t *)bt_mesh_calloc(PROV_CONF_INPUTS_SIZE); + if (!link[idx].conf_inputs) { + /* Disconnect this connection, clear corresponding informations */ + BT_ERR("%s, Failed to allocate memory", __func__); + close_link(idx, CLOSE_REASON_FAILED); + return -ENOMEM; + } + + send_invite(idx); + return 0; +} + +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason) +{ + u8_t idx = 0U; + + BT_DBG("conn %p", conn); + + if (!find_conn(conn, &idx)) { + BT_ERR("%s, Conn %p is not found", __func__, conn); + return -ENOTCONN; + } + + if (bt_mesh_atomic_test_and_clear_bit(link[idx].flags, TIMEOUT_START)) { + k_delayed_work_cancel(&link[idx].timeout); + } + + if (prov->prov_link_close) { + prov->prov_link_close(BLE_MESH_PROV_GATT, reason); + } + + prov_memory_free(idx); + + memset(&link[idx], 0, offsetof(struct prov_link, timeout)); + + if (bt_mesh_pub_key_get()) { + bt_mesh_atomic_set_bit(link[idx].flags, LOCAL_PUB_KEY); + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info) +{ + const u8_t *key = NULL; + int i; + + if (!prov_info) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + + key = bt_mesh_pub_key_get(); + if (!key) { + BT_ERR("%s, Failed to generate Public Key", __func__); + return -EIO; + } + + prov = prov_info; + + prov_ctx.primary_addr = BLE_MESH_ADDR_UNASSIGNED; + + if (prov->prov_static_oob_val && prov->prov_static_oob_len) { + prov_ctx.static_oob_len = MIN(16, prov->prov_static_oob_len); + memcpy(prov_ctx.static_oob_val, prov->prov_static_oob_val, prov_ctx.static_oob_len); + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + struct prov_adv_buf *adv = &adv_buf[i]; + adv->buf.size = ADV_BUF_SIZE; + adv->buf.__buf = adv_buf_data + (i * ADV_BUF_SIZE); + + link[i].pending_ack = XACT_NVAL; + k_delayed_work_init(&link[i].tx.retransmit, prov_retransmit); + link[i].tx.retransmit.work.index = (int)i; + link[i].rx.prev_id = XACT_NVAL; + link[i].rx.buf = bt_mesh_pba_get_buf(i); + } +#endif + + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { + k_delayed_work_init(&link[i].timeout, prov_timeout); + link[i].timeout.work.index = (int)i; + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_adv_mutex_new(); + bt_mesh_pb_buf_mutex_new(); +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_pb_gatt_mutex_new(); +#endif + + return 0; +} + +int bt_mesh_provisioner_prov_deinit(bool erase) +{ + int i; + + if (prov == NULL) { + BT_ERR("%s, No provisioning context provided", __func__); + return -EINVAL; + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) { + prov_clear_tx(i); + k_delayed_work_free(&link[i].tx.retransmit); +#if defined(CONFIG_BLE_MESH_USE_DUPLICATE_SCAN) + /* Remove the link id from exceptional list */ + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_REMOVE, + BLE_MESH_EXCEP_INFO_MESH_LINK_ID, &link[i].link_id); +#endif /* CONFIG_BLE_MESH_USE_DUPLICATE_SCAN */ + } +#endif /* CONFIG_BLE_MESH_PB_ADV */ + + for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) { + prov_memory_free(i); + k_delayed_work_free(&link[i].timeout); + memset(&link[i], 0, sizeof(link[i])); + } + +#if defined(CONFIG_BLE_MESH_PB_ADV) + bt_mesh_pb_adv_mutex_free(); + bt_mesh_pb_buf_mutex_free(); +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_pb_gatt_mutex_free(); +#endif + memset(&prov_ctx, 0, sizeof(prov_ctx)); + +#if defined(CONFIG_BLE_MESH_PB_ADV) + memset(adv_buf, 0, sizeof(adv_buf)); + memset(adv_buf_data, 0, sizeof(adv_buf_data)); +#endif + memset(unprov_dev, 0, sizeof(unprov_dev)); + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_prov_info(); + } + + prov = NULL; + + return 0; +} + +static bool is_unprov_dev_info_callback_to_app(bt_mesh_prov_bearer_t bearer, + const u8_t uuid[16], const bt_mesh_addr_t *addr, u16_t oob_info, s8_t rssi) +{ + u16_t index = 0U; + + if (prov_ctx.prov_after_match == false) { + u8_t adv_type = (bearer == BLE_MESH_PROV_ADV) ? + BLE_MESH_ADV_NONCONN_IND : BLE_MESH_ADV_IND; + + if (provisioner_dev_find(addr, uuid, &index)) { + BT_DBG("%s, Device is not in queue, notify to upper layer", __func__); + if (notify_unprov_adv_pkt_cb) { + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer, rssi); + } + return true; + } + + if (!(unprov_dev[index].bearer & bearer)) { + BT_WARN("Device in queue not support PB-%s", + (bearer == BLE_MESH_PROV_ADV) ? "ADV" : "GATT"); + if (notify_unprov_adv_pkt_cb) { + notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type, uuid, oob_info, bearer, rssi); + } + return true; + } + } + + return false; +} + +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, s8_t rssi) +{ +#if defined(CONFIG_BLE_MESH_PB_ADV) + const bt_mesh_addr_t *addr = NULL; + const u8_t *uuid = NULL; + u16_t oob_info = 0U; + + if (!(prov_ctx.bearers & BLE_MESH_PROV_ADV)) { + BT_WARN("Provisioner not support PB-ADV bearer"); + return; + } + + if (buf->len != 0x12 && buf->len != 0x16) { + BT_ERR("%s, Invalid Unprovisioned Device Beacon length", __func__); + return; + } + + addr = bt_mesh_pba_get_addr(); + uuid = buf->data; + net_buf_simple_pull(buf, 16); + /* Mesh beacon uses big-endian to send beacon data */ + oob_info = net_buf_simple_pull_be16(buf); + + if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_ADV)) { + return; + } + + if (is_unprov_dev_info_callback_to_app( + BLE_MESH_PROV_ADV, uuid, addr, oob_info, rssi)) { + return; + } + + provisioner_start_prov_pb_adv(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED); +#endif /* CONFIG_BLE_MESH_PB_ADV */ +} + +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi) +{ +#if defined(CONFIG_BLE_MESH_PB_GATT) + const u8_t *uuid = NULL; + u16_t oob_info = 0U; + + if (!(prov_ctx.bearers & BLE_MESH_PROV_GATT)) { + BT_WARN("Provisioner not support PB-GATT bearer"); + return; + } + + if (bt_mesh_gattc_get_free_conn_count() == 0) { + BT_INFO("BLE connections for mesh reach max limit"); + return; + } + + uuid = buf->data; + net_buf_simple_pull(buf, 16); + /* Mesh beacon uses big-endian to send beacon data */ + oob_info = net_buf_simple_pull_be16(buf); + + if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_GATT)) { + return; + } + + if (is_unprov_dev_info_callback_to_app( + BLE_MESH_PROV_GATT, uuid, addr, oob_info, rssi)) { + return; + } + + /* Provisioner will copy the device uuid, oob info, etc. into an unused link + * struct, and at this moment the link has not been activated. Even if we + * receive an Unprovisioned Device Beacon and a Connectable Provisioning adv + * pkt from the same device, and store the device info received within each + * adv pkt into two link structs which will has no impact on the provisioning + * of this device, because no matter which link among PB-GATT and PB-ADV is + * activated first, the other one will be dropped finally and the link struct + * occupied by the dropped link will be used by other devices (because the link + * is not activated). + * Use connecting flag to prevent if two devices's adv pkts are both received, + * the previous one info will be replaced by the second one. + */ + provisioner_start_prov_pb_gatt(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED); +#endif /* CONFIG_BLE_MESH_PB_GATT */ +} + +#endif /* CONFIG_BLE_MESH_PROVISIONER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h new file mode 100644 index 000000000..4b74b425e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/provisioner_prov.h @@ -0,0 +1,427 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _PROVISIONER_PROV_H_ +#define _PROVISIONER_PROV_H_ + +#include "mesh_main.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef CONFIG_BLE_MESH_PBA_SAME_TIME +#define CONFIG_BLE_MESH_PBA_SAME_TIME 0 +#endif + +#ifndef CONFIG_BLE_MESH_PBG_SAME_TIME +#define CONFIG_BLE_MESH_PBG_SAME_TIME 0 +#endif + +#define RM_AFTER_PROV BIT(0) +#define START_PROV_NOW BIT(1) +#define FLUSHABLE_DEV BIT(2) + +struct bt_mesh_unprov_dev_add { + u8_t addr[6]; + u8_t addr_type; + u8_t uuid[16]; + u16_t oob_info; + u8_t bearer; +}; + +struct bt_mesh_device_delete { + u8_t addr[6]; + u8_t addr_type; + u8_t uuid[16]; +}; + +#define NET_IDX_FLAG BIT(0) +#define FLAGS_FLAG BIT(1) +#define IV_INDEX_FLAG BIT(2) + +struct bt_mesh_prov_data_info { + union { + u16_t net_idx; + u8_t flags; + u32_t iv_index; + }; + u8_t flag; +}; + +/* The following APIs are for primary provisioner internal use */ + +/** + * @brief This function decrements the current PB-GATT count. + * + * @return None + */ +void bt_mesh_provisioner_pbg_count_dec(void); + +/** + * @brief This function clears the part of the link info of the proper device. + * + * @param[in] addr: Remote device address + * + * @return None + */ +void bt_mesh_provisioner_clear_link_info(const u8_t addr[6]); + +/** + * @brief This function handles the received PB-ADV PDUs. + * + * @param[in] buf: Pointer to the buffer containing generic provisioning PDUs + * + * @return Zero - success, otherwise - fail + */ +void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf); + +/** + * @brief This function sends provisioning invite to start + * provisioning this unprovisioned device. + * + * @param[in] addr: Remote device address + * @param[in] conn: Pointer to the bt_conn structure + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_prov_conn(const u8_t addr[6], struct bt_mesh_conn *conn); + +/** + * @brief This function sends provisioning invite to start + * provisioning this unprovisioned device. + * + * @param[in] conn: Pointer to the bt_conn structure + * @param[in] addr: Address of the connected device + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, u8_t *addr); + +/** + * @brief This function resets the used information when + * related connection is terminated. + * + * @param[in] conn: Pointer to the bt_conn structure + * @param[in] reason: Connection terminated reason + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, u8_t reason); + +/** + * @brief This function handles the received PB-GATT provision + * PDUs. + * + * @param[in] conn: Pointer to the bt_conn structure + * @param[in] buf: Pointer to the buffer containing provision PDUs + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf); + +/** + * @brief This function initializes provisioner's PB-GATT and PB-ADV + * related information. + * + * @param[in] prov_info: Pointer to the application-initialized provisioner info. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_prov_init(const struct bt_mesh_prov *prov_info); + +/** + * @brief This function de-initializes provisioner's PB-GATT and PB-ADV + * related information. + * + * @param[in] erase: Indicate if erasing provisioning information from flash. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_prov_deinit(bool erase); + +/** + * @brief This function parses the received unprovisioned device + * beacon advertising packets, and if checked, starts to provision this device + * using PB-ADV bearer. + * + * @param[in] buf: Pointer to the buffer containing unprovisioned device beacon + * @param[in] rssi: RSSI of the received unprovisioned device beacon + * + * @return None + */ +void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, s8_t rssi); + +void bt_mesh_provisioner_prov_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi); + +/** + * @brief This function gets the bt_mesh_prov pointer. + * + * @return bt_mesh_prov pointer(prov) + */ +const struct bt_mesh_prov *bt_mesh_provisioner_get_prov_info(void); + +void bt_mesh_provisioner_restore_prov_info(u16_t primary_addr, u16_t alloc_addr); + +/* The following APIs are for primary provisioner application use */ + +/** @brief Add unprovisioned device info to unprov_dev queue + * + * @param[in] add_dev: Pointer to the structure containing the device information + * @param[in] flags: Flags indicate several operations of the device information + * - Remove device information from queue after it is provisioned (BIT0) + * - Start provisioning as soon as device is added to queue (BIT1) + * - Device can be flushed when device queue is full (BIT2) + * + * @return Zero on success or (negative) error code otherwise. + * + * @note 1. Currently address type only supports public address and static random address. + * 2. If device UUID and/or device address and address type already exist in the + * device queue, but the bearer differs from the existing one, add operation + * will also be successful and it will update the provision bearer supported by + * the device. + */ +int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u8_t flags); + +/** @brief Provision an unprovisioned device with fixed unicast address. + * + * @param[in] uuid: Device UUID of the unprovisioned device + * @param[in] addr: Device address of the unprovisioned device + * @param[in] addr_type: Device address type of the unprovisioned device + * @param[in] bearer: Provisioning bearer going to be used + * @param[in] oob_info: OOB info of the unprovisioned device + * @param[in] unicast_addr: Unicast address going to be allocated for the unprovisioned device + * + * @return Zero on success or (negative) error code otherwise. + * + * @note 1. Currently address type only supports public address and static random address. + * 2. Bearer must be equal to BLE_MESH_PROV_ADV or BLE_MESH_PROV_GATT + */ +int bt_mesh_provisioner_prov_device_with_addr(const u8_t uuid[16], const u8_t addr[6], + u8_t addr_type, bt_mesh_prov_bearer_t bearer, + u16_t oob_info, u16_t unicast_addr); + +/** @brief Delete device from queue, reset current provisioning link and reset the node + * + * @param[in] del_dev: Pointer to the structure containing the device information + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev); + +/** + * @brief This function sets a part of the device UUID for comparison before + * starting to provision the device. + * + * @param[in] offset: offset of the device UUID to be compared + * @param[in] length: length of the device UUID to be compared + * @param[in] match: value to be compared + * @param[in] prov_flag: flags indicate if uuid_match advertising packets are received, after that + * the device will be provisioned at once or reported to the application layer + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, + const u8_t *match, bool prov_flag); + +/** @brief Callback for provisioner receiving advertising packet from unprovisioned devices which are + * not in the unprovisioned device queue. + * + * Report on the unprovisioned device beacon and mesh provisioning service advertising data to application layer + * + * @param addr Unprovisioned device address pointer + * @param addr_type Unprovisioned device address type + * @param dev_uuid Unprovisioned device device UUID pointer + * @param bearer Advertising packet received from PB-GATT or PB-ADV bearer + * @param adv_type Adv packet type, currently this is not used and we can use bearer to device + * the adv_type(ADV_IND or ADV_NONCONN_IND). This parameter will be used, when + * scan response data will be supported. + * @param rssi RSSI of the received advertising packet + * + */ +typedef void (*unprov_adv_pkt_cb_t)(const u8_t addr[6], const u8_t addr_type, + const u8_t adv_type, const u8_t dev_uuid[16], + u16_t oob_info, bt_mesh_prov_bearer_t bearer, s8_t rssi); + +/** + * @brief This function registers the callback which notifies the application + * layer of the received mesh provisioning or unprovisioned device + * beacon advertizing packets (from devices not in the unprov device queue). + * + * @param[in] cb: Callback of the notifying adv pkts function + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb); + +/** + * @brief This function changes net_idx or flags or iv_index used in provisioning data. + * + * @param[in] info: Pointer of structure containing net_idx or flags or iv_index + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info); + +/** + * @brief This function sets the provisioning information needed by Provisioner, + * including unicast address, IV Index, etc. + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_prov_info(void); + +/** + * @brief This function sets the provisioning bearer type used by Provisioner. + * + * @param[in] bearers: Provisioning bearer type + * @param[in] clear: Indicate if the corresponding bearer type will be cleared + * + * @return None + */ +void bt_mesh_provisioner_set_prov_bearer(bt_mesh_prov_bearer_t bearers, bool clear); + +/** + * @brief This function gets the provisioning bearer type used by Provisioner. + * + * @return Currently supported provisioning bearer type + */ +bt_mesh_prov_bearer_t bt_mesh_provisioner_get_prov_bearer(void); + +/** + * @brief This function sets the Static OOB value used by Provisioner. + * + * @param[in] value: Static OOB value + * @param[in] length: Static OOB value length + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_static_oob_value(const u8_t *value, u8_t length); + +/** + * @brief This function gets the unicast address of primary element of Provisioner. + * + * @return Unicast address of primary element of Provisioner. + */ +u16_t bt_mesh_provisioner_get_primary_elem_addr(void); + +/** + * @brief This function sets the unicast address of primary element of Provisioner. + * + * @param[in] addr: unicast address of primary element + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_primary_elem_addr(u16_t addr); + +/** + * @brief This function is used to update next allocated address by Provisioner. + * + * @note This function is used for mesh internal test. + * + * @param[in] unicast_addr: unicast address of the node + * @param[in] element_num: element count of the node + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_test_provisioner_update_alloc_addr(u16_t unicast_addr, u16_t element_num); + +/** + * @brief This function is called to input number/string out-put by unprovisioned device. + * + * @param[in] idx The provisioning link index + * @param[in] val Pointer of the input number/string + * @param[in] num_flag Flag indicates if it is a number or string + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_oob_input_data(const u8_t idx, const u8_t *val, bool num_flag); + +/** + * @brief This function is called to output number/string which will be input by unprovisioned device. + * + * @param[in] idx The provisioning link index + * @param[in] num Pointer of the output number/string + * @param[in] size Size of the output number/string + * @param[in] num_flag Flag indicates if it is a number or string + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_set_oob_output_data(const u8_t idx, const u8_t *num, u8_t size, bool num_flag); + +/** + * @brief This function is called to read unprovisioned device's oob public key. + * + * @param[in] idx The provisioning link index + * @param[in] pub_key_x Unprovisioned device's Public Key X + * @param[in] pub_key_y Unprovisioned device's Public Key Y + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_provisioner_read_oob_pub_key(const u8_t idx, const u8_t pub_key_x[32], const u8_t pub_key_y[32]); + +/* The following APIs are for fast provisioning */ + +/** + * @brief This function is called to set fast_prov_flag. + * + * @param[in] enable: Enable or disable fast provisioning + * + * @return None + */ +void bt_mesh_provisioner_fast_prov_enable(bool enable); + +/** + * @brief This function is called to set netkey index used for fast provisioning. + * + * @param[in] net_key: Netkey value + * @param[in] net_idx: Netkey index + * + * @return status for set netkey index msg + */ +u8_t bt_mesh_provisioner_set_fast_prov_net_idx(const u8_t *net_key, u16_t net_idx); + +/** + * @brief This function is called to get netkey index used for fast provisioning. + * + * @return net_idx of fast provisioning + */ +u16_t bt_mesh_provisioner_get_fast_prov_net_idx(void); + +/** + * @brief This function is called to set unicast address range used for fast provisioning. + * + * @param[in] min: Minimum unicast address + * @param[in] max: Maximum unicast address + * + * @return status for set unicast address range message + */ +u8_t bt_mesh_set_fast_prov_unicast_addr_range(u16_t min, u16_t max); + +/** + * @brief This function is called to set flags & iv_index used for fast provisioning. + * + * @param[in] flags: Key refresh flag and iv update flag + * @param[in] iv_index: IV index + * + * @return None + */ +void bt_mesh_set_fast_prov_flags_iv_index(u8_t flags, u32_t iv_index); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROVISIONER_PROV_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.c b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c new file mode 100644 index 000000000..215b443ce --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.c @@ -0,0 +1,1004 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "mesh.h" +#include "access.h" +#include "beacon.h" +#include "mesh_common.h" +#include "foundation.h" +#include "proxy_client.h" +#include "provisioner_prov.h" +#include "provisioner_main.h" +#include "mesh_bearer_adapt.h" + +#define PDU_TYPE(data) (data[0] & BIT_MASK(6)) +#define PDU_SAR(data) (data[0] >> 6) + +#define PROXY_SAR_TIMEOUT K_SECONDS(20) + +#define SAR_COMPLETE 0x00 +#define SAR_FIRST 0x01 +#define SAR_CONT 0x02 +#define SAR_LAST 0x03 + +#define PDU_HDR(sar, type) (sar << 6 | (type & BIT_MASK(6))) + +#define SERVER_BUF_SIZE 68 + +#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_CLIENT + +static struct bt_mesh_proxy_server { + struct bt_mesh_conn *conn; + enum __packed { + NONE, + PROV, + PROXY, + } conn_type; +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + u16_t net_idx; +#endif + u8_t msg_type; + struct k_delayed_work sar_timer; + struct net_buf_simple buf; +} servers[BLE_MESH_MAX_CONN]; + +static u8_t server_buf_data[SERVER_BUF_SIZE * BLE_MESH_MAX_CONN]; + +static struct bt_mesh_proxy_server *find_server(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + if (servers[i].conn == conn) { + return &servers[i]; + } + } + + return NULL; +} + +static void proxy_sar_timeout(struct k_work *work) +{ + struct bt_mesh_proxy_server *server = NULL; + + BT_WARN("%s", __func__); + + server = CONTAINER_OF(work, struct bt_mesh_proxy_server, sar_timer.work); + if (!server || !server->conn) { + BT_ERR("%s, Invalid proxy server parameter", __func__); + return; + } + + net_buf_simple_reset(&server->buf); + bt_mesh_gattc_disconnect(server->conn); +} + +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT +/** + * The following callbacks are used to notify proper information + * to the application layer. + */ +static proxy_client_recv_adv_cb_t proxy_client_adv_recv_cb; +static proxy_client_connect_cb_t proxy_client_connect_cb; +static proxy_client_disconnect_cb_t proxy_client_disconnect_cb; +static proxy_client_recv_filter_status_cb_t proxy_client_filter_status_recv_cb; + +void bt_mesh_proxy_client_set_adv_recv_cb(proxy_client_recv_adv_cb_t cb) +{ + proxy_client_adv_recv_cb = cb; +} + +void bt_mesh_proxy_client_set_conn_cb(proxy_client_connect_cb_t cb) +{ + proxy_client_connect_cb = cb; +} + +void bt_mesh_proxy_client_set_disconn_cb(proxy_client_disconnect_cb_t cb) +{ + proxy_client_disconnect_cb = cb; +} + +void bt_mesh_proxy_client_set_filter_status_cb(proxy_client_recv_filter_status_cb_t cb) +{ + proxy_client_filter_status_recv_cb = cb; +} + +static void filter_status(struct bt_mesh_proxy_server *server, + struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + u8_t filter_type = 0U; + u16_t list_size = 0U; + + if (buf->len != 3) { + BT_ERR("%s, Invalid Proxy Filter Status length %d", __func__, buf->len); + return; + } + + filter_type = net_buf_simple_pull_u8(buf); + if (filter_type > 0x01) { + BT_ERR("%s, Invalid filter type 0x%02x", __func__, filter_type); + return; + } + + list_size = net_buf_simple_pull_be16(buf); + + BT_INFO("%s, filter_type 0x%02x list_size %d", __func__, filter_type, list_size); + + if (proxy_client_filter_status_recv_cb) { + proxy_client_filter_status_recv_cb(server - servers, rx->ctx.addr, server->net_idx, filter_type, list_size); + } + + return; +} + +static void proxy_cfg(struct bt_mesh_proxy_server *server) +{ + NET_BUF_SIMPLE_DEFINE(buf, 29); + struct bt_mesh_net_rx rx = {0}; + u8_t opcode = 0U; + int err = 0; + + err = bt_mesh_net_decode(&server->buf, BLE_MESH_NET_IF_PROXY_CFG, + &rx, &buf); + if (err) { + BT_ERR("%s, Failed to decode Proxy Configuration (err %d)", __func__, err); + return; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(rx.ctx.addr)) { + BT_ERR("%s, Proxy Configuration from non-unicast addr 0x%04x", __func__, rx.ctx.addr); + return; + } + + /* Remove network headers */ + net_buf_simple_pull(&buf, BLE_MESH_NET_HDR_LEN); + + BT_DBG("%u bytes: %s", buf.len, bt_hex(buf.data, buf.len)); + + if (buf.len < 3) { + BT_WARN("Too short proxy configuration PDU"); + return; + } + + opcode = net_buf_simple_pull_u8(&buf); + + switch (opcode) { + case BLE_MESH_PROXY_CFG_FILTER_STATUS: + filter_status(server, &rx, &buf); + break; + default: + BT_WARN("Unknown Proxy Configuration OpCode 0x%02x", opcode); + break; + } +} +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +static void proxy_complete_pdu(struct bt_mesh_proxy_server *server) +{ + switch (server->msg_type) { +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + case BLE_MESH_PROXY_NET_PDU: + BT_DBG("Mesh Network PDU"); + bt_mesh_net_recv(&server->buf, 0, BLE_MESH_NET_IF_PROXY); + break; + case BLE_MESH_PROXY_BEACON: + BT_DBG("Mesh Beacon PDU"); + bt_mesh_beacon_recv(&server->buf, 0); + break; + case BLE_MESH_PROXY_CONFIG: + BT_DBG("Mesh Configuration PDU"); + proxy_cfg(server); + break; +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + case BLE_MESH_PROXY_PROV: + BT_DBG("Mesh Provisioning PDU"); + bt_mesh_provisioner_pb_gatt_recv(server->conn, &server->buf); + break; +#endif + default: + BT_WARN("Unhandled Message Type 0x%02x", server->msg_type); + break; + } + + net_buf_simple_reset(&server->buf); +} + +#define ATTR_IS_PROV(uuid) (uuid == BLE_MESH_UUID_MESH_PROV_VAL) + +static ssize_t proxy_recv(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, const void *buf, + u16_t len, u16_t offset, u8_t flags) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + const u8_t *data = buf; + u16_t srvc_uuid = 0U; + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if (len < 1) { + BT_WARN("Too small Proxy PDU"); + return -EINVAL; + } + + srvc_uuid = bt_mesh_gattc_get_service_uuid(conn); + if (!srvc_uuid) { + BT_ERR("%s, No service uuid found", __func__); + return -ENOTCONN; + } + + if (ATTR_IS_PROV(srvc_uuid) != (PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { + BT_WARN("Proxy PDU type doesn't match GATT service uuid"); + return -EINVAL; + } + + if (len - 1 > net_buf_simple_tailroom(&server->buf)) { + BT_WARN("Too big proxy PDU"); + return -EINVAL; + } + + switch (PDU_SAR(data)) { + case SAR_COMPLETE: + if (server->buf.len) { + BT_WARN("Complete PDU while a pending incomplete one"); + return -EINVAL; + } + + server->msg_type = PDU_TYPE(data); + net_buf_simple_add_mem(&server->buf, data + 1, len - 1); + proxy_complete_pdu(server); + break; + + case SAR_FIRST: + if (server->buf.len) { + BT_WARN("First PDU while a pending incomplete one"); + return -EINVAL; + } + + k_delayed_work_submit(&server->sar_timer, PROXY_SAR_TIMEOUT); + server->msg_type = PDU_TYPE(data); + net_buf_simple_add_mem(&server->buf, data + 1, len - 1); + break; + + case SAR_CONT: + if (!server->buf.len) { + BT_WARN("Continuation with no prior data"); + return -EINVAL; + } + + if (server->msg_type != PDU_TYPE(data)) { + BT_WARN("Unexpected message type in continuation"); + return -EINVAL; + } + + k_delayed_work_submit(&server->sar_timer, PROXY_SAR_TIMEOUT); + net_buf_simple_add_mem(&server->buf, data + 1, len - 1); + break; + + case SAR_LAST: + if (!server->buf.len) { + BT_WARN("Last SAR PDU with no prior data"); + return -EINVAL; + } + + if (server->msg_type != PDU_TYPE(data)) { + BT_WARN("Unexpected message type in last SAR PDU"); + return -EINVAL; + } + + k_delayed_work_cancel(&server->sar_timer); + net_buf_simple_add_mem(&server->buf, data + 1, len - 1); + proxy_complete_pdu(server); + break; + } + + return len; +} + + +static int proxy_send(struct bt_mesh_conn *conn, const void *data, u16_t len) +{ + BT_DBG("%u bytes: %s", len, bt_hex(data, len)); + + return bt_mesh_gattc_write_no_rsp(conn, NULL, data, len); +} + +static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg) +{ + u16_t mtu = 0U; + int err = 0; + + if (conn == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("conn %p type 0x%02x len %u: %s", conn, type, msg->len, + bt_hex(msg->data, msg->len)); + + mtu = bt_mesh_gattc_get_mtu_info(conn); + if (!mtu) { + BT_ERR("%s, Conn used to get mtu does not exist", __func__); + return -ENOTCONN; + } + + /* ATT_MTU - OpCode (1 byte) - Handle (2 bytes) */ + mtu -= 3; + if (mtu > msg->len) { + net_buf_simple_push_u8(msg, PDU_HDR(SAR_COMPLETE, type)); + return proxy_send(conn, msg->data, msg->len); + } + + net_buf_simple_push_u8(msg, PDU_HDR(SAR_FIRST, type)); + err = proxy_send(conn, msg->data, mtu); + net_buf_simple_pull(msg, mtu); + + while (msg->len) { + if (msg->len + 1 < mtu) { + net_buf_simple_push_u8(msg, PDU_HDR(SAR_LAST, type)); + err = proxy_send(conn, msg->data, msg->len); + break; + } + + net_buf_simple_push_u8(msg, PDU_HDR(SAR_CONT, type)); + err = proxy_send(conn, msg->data, mtu); + net_buf_simple_pull(msg, mtu); + } + + return err; +} + +int bt_mesh_proxy_prov_client_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + if (!server) { + BT_ERR("$%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if ((server->conn_type == PROV) != (type == BLE_MESH_PROXY_PROV)) { + BT_ERR("%s, Invalid PDU type for Proxy Server", __func__); + return -EINVAL; + } + + return proxy_segment_and_send(conn, type, msg); +} + +static void proxy_connected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, int id) +{ + struct bt_mesh_proxy_server *server = NULL; + + if (!servers[id].conn) { + server = &servers[id]; + } + + if (!server) { + BT_ERR("%s, No free Proxy Server objects", __func__); + /** Disconnect current connection, clear part of prov_link + * information, like uuid, dev_addr, linking flag, etc. + */ + bt_mesh_gattc_disconnect(conn); + return; + } + + server->conn = bt_mesh_conn_ref(conn); + server->conn_type = NONE; + net_buf_simple_reset(&server->buf); + + bt_mesh_gattc_exchange_mtu(id); + return; +} + +static void proxy_disconnected(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn, u8_t reason) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + BT_DBG("conn %p, handle is %d, reason 0x%02x", conn, conn->handle, reason); + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return; + } + +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + if (server->conn_type == PROV) { + bt_mesh_provisioner_pb_gatt_close(conn, reason); + } +#endif + +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + if (server->conn_type == PROXY) { + if (proxy_client_disconnect_cb) { + proxy_client_disconnect_cb(addr, server - servers, server->net_idx, reason); + } + } +#endif + + k_delayed_work_cancel(&server->sar_timer); + server->conn = NULL; + server->conn_type = NONE; +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + server->net_idx = BLE_MESH_KEY_UNUSED; +#endif + + return; +} + +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT +static ssize_t prov_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if (server->conn_type == NONE) { + server->conn_type = PROV; + + if (bt_mesh_provisioner_set_prov_conn(addr->val, server->conn)) { + BT_ERR("%s, bt_mesh_provisioner_set_prov_conn failed", __func__); + bt_mesh_gattc_disconnect(server->conn); + return -EIO; + } + return bt_mesh_provisioner_pb_gatt_open(conn, addr->val); + } + + return -ENOMEM; +} + +static ssize_t prov_recv_ntf(struct bt_mesh_conn *conn, u8_t *data, u16_t len) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if (server->conn_type == PROV) { + return proxy_recv(conn, NULL, data, len, 0, 0); + } + + return -EINVAL; +} + +int bt_mesh_provisioner_pb_gatt_enable(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + if (servers[i].conn) { + servers[i].conn_type = PROV; + } + } + + return 0; +} + +int bt_mesh_provisioner_pb_gatt_disable(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + + if (server->conn && server->conn_type == PROV) { + bt_mesh_gattc_disconnect(server->conn); + server->conn_type = NONE; + } + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT */ + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) +static ssize_t proxy_write_ccc(bt_mesh_addr_t *addr, struct bt_mesh_conn *conn) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if (server->conn_type == NONE) { + server->conn_type = PROXY; + + if (proxy_client_connect_cb) { + proxy_client_connect_cb(addr, server - servers, server->net_idx); + } + return 0; + } + + return -EINVAL; +} + +static ssize_t proxy_recv_ntf(struct bt_mesh_conn *conn, u8_t *data, u16_t len) +{ + struct bt_mesh_proxy_server *server = find_server(conn); + + if (!server) { + BT_ERR("%s, No Proxy Server object found", __func__); + return -ENOTCONN; + } + + if (server->conn_type == PROXY) { + return proxy_recv(conn, NULL, data, len, 0, 0); + } + + return -EINVAL; +} + +/** + * Currently proxy client doesn't need bt_mesh_proxy_client_enable() and + * bt_mesh_proxy_client_disable() functions, and once they are used, + * proxy client can be enabled to parse node_id_adv and net_id_adv in + * order to support proxy client role. + * And if gatt proxy is disabled, proxy client can stop handling these + * two kinds of connectable advertising packets. + */ +int bt_mesh_proxy_client_enable(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + if (servers[i].conn) { + servers[i].conn_type = PROXY; + } + } + + /** + * TODO: + * Once at least one device has been provisioned, proxy client can be + * set to allow receiving and parsing node_id & net_id adv packets, + * and we may use a global flag to indicate this. + */ + + return 0; +} + +int bt_mesh_proxy_client_disable(void) +{ + int i; + + BT_DBG("%s", __func__); + + /** + * TODO: + * Once this function is invoked, proxy client shall stop handling + * node_id & net_id adv packets, and if proxy connection exists, + * it should be disconnected. + */ + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + + if (server->conn && server->conn_type == PROXY) { + bt_mesh_gattc_disconnect(server->conn); + server->conn_type = NONE; + } + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +static struct bt_mesh_prov_conn_cb conn_callbacks = { + .connected = proxy_connected, + .disconnected = proxy_disconnected, +#if CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT + .prov_write_descr = prov_write_ccc, + .prov_notify = prov_recv_ntf, +#endif /* CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT */ +#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) + .proxy_write_descr = proxy_write_ccc, + .proxy_notify = proxy_recv_ntf, +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ +}; + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_CLIENT) +static struct bt_mesh_subnet *bt_mesh_is_net_id_exist(const u8_t net_id[8]) +{ + struct bt_mesh_subnet *sub = NULL; + size_t size = 0U, i = 0U; + + size = bt_mesh_rx_netkey_size(); + + for (i = 0U; i < size; i++) { + sub = bt_mesh_rx_netkey_get(i); + if (sub && !memcmp(sub->keys[sub->kr_flag].net_id, net_id, 8)) { + return sub; + } + } + + return NULL; +} + +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi) +{ + bt_mesh_proxy_adv_ctx_t ctx = {0}; + u8_t type = 0U; + + /* Check if connection reaches the maximum limitation */ + if (bt_mesh_gattc_get_free_conn_count() == 0) { + BT_INFO("BLE connections for mesh reach max limit"); + return; + } + + type = net_buf_simple_pull_u8(buf); + + switch (type) { + case BLE_MESH_PROXY_ADV_NET_ID: { + if (buf->len != sizeof(ctx.net_id.net_id)) { + BT_WARN("Malformed Network ID"); + return; + } + + struct bt_mesh_subnet *sub = NULL; + sub = bt_mesh_is_net_id_exist(buf->data); + if (!sub) { + return; + } + + memcpy(ctx.net_id.net_id, buf->data, buf->len); + ctx.net_id.net_idx = sub->net_idx; + break; + } + case BLE_MESH_PROXY_ADV_NODE_ID: + /* Gets node identity information. + * hash = aes-ecb(identity key, 16 octets(padding + random + src)) mod 2^64, + * If Proxy Client wants to get src, it may encrypts multiple times and compare + * the hash value (8 octets) with the received one. + */ + return; + default: + BT_DBG("%s, Unknown Mesh Proxy adv type 0x%02x", __func__, type); + return; + } + + if (proxy_client_adv_recv_cb) { + proxy_client_adv_recv_cb(addr, type, &ctx, rssi); + } +} + +int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_idx) +{ + bt_mesh_addr_t remote_addr = {0}; + int result = 0; + + if (!addr || addr_type > BLE_MESH_ADDR_RANDOM) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + memcpy(remote_addr.val, addr, BLE_MESH_ADDR_LEN); + remote_addr.type = addr_type; + + result = bt_mesh_gattc_conn_create(&remote_addr, BLE_MESH_UUID_MESH_PROXY_VAL); + if (result < 0) { + return result; + } + + /* Store corresponding net_idx which can be used for sending Proxy Configuration */ + servers[result].net_idx = net_idx; + return 0; +} + +int bt_mesh_proxy_client_disconnect(u8_t conn_handle) +{ + struct bt_mesh_conn *conn = NULL; + + if (conn_handle >= BLE_MESH_MAX_CONN) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("conn_handle %d", conn_handle); + + conn = servers[conn_handle].conn; + if (!conn) { + BT_ERR("%s, Not connected, conn_handle %d", __func__, conn_handle); + return -ENOTCONN; + } + + bt_mesh_gattc_disconnect(conn); + return 0; +} + +bool bt_mesh_proxy_client_send(struct net_buf_simple *buf, u16_t dst) +{ + bool send = false; + int err = 0; + int i; + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + NET_BUF_SIMPLE_DEFINE(msg, 32); + + if (!server->conn || server->conn_type != PROXY) { + continue; + } + + /* Proxy PDU sending modifies the original buffer, + * so we need to make a copy. + */ + net_buf_simple_init(&msg, 1); + net_buf_simple_add_mem(&msg, buf->data, buf->len); + + err = bt_mesh_proxy_prov_client_send(server->conn, BLE_MESH_PROXY_NET_PDU, &msg); + if (err) { + BT_ERR("%s, Failed to send proxy net message (err %d)", __func__, err); + } else { + BT_INFO("%u bytes to dst 0x%04x", buf->len, dst); + send = true; + } + } + + return send; +} + +static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) +{ + NET_BUF_SIMPLE_DEFINE(buf, 23); + + net_buf_simple_init(&buf, 1); + bt_mesh_beacon_create(sub, &buf); + + return bt_mesh_proxy_prov_client_send(conn, BLE_MESH_PROXY_BEACON, &buf); +} + +bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub) +{ + bool send = false; + int err = 0; + int i; + + /* NULL means we send Secure Network Beacon on all subnets */ + if (!sub) { +#if CONFIG_BLE_MESH_NODE + if (bt_mesh_is_provisioned()) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx != BLE_MESH_KEY_UNUSED) { + send = bt_mesh_proxy_client_beacon_send(&bt_mesh.sub[i]); + } + } + return send; + } +#endif /* CONFIG_BLE_MESH_NODE */ +#if CONFIG_BLE_MESH_PROVISIONER + if (bt_mesh_is_provisioner_en()) { + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] && bt_mesh.p_sub[i]->net_idx != BLE_MESH_KEY_UNUSED) { + send = bt_mesh_proxy_client_beacon_send(bt_mesh.p_sub[i]); + } + } + return send; + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + return send; + } + + for (i = 0; i < ARRAY_SIZE(servers); i++) { + if (servers[i].conn && servers[i].conn_type == PROXY) { + err = beacon_send(servers[i].conn, sub); + if (err) { + BT_ERR("%s, Failed to send proxy beacon message (err %d)", __func__, err); + } else { + send = true; + } + } + } + + return send; +} + +static int send_proxy_cfg(struct bt_mesh_conn *conn, u16_t net_idx, struct bt_mesh_proxy_cfg_pdu *cfg) +{ + struct bt_mesh_msg_ctx ctx = { + .net_idx = net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, /* CTL shall be set to 1 */ + .addr = BLE_MESH_ADDR_UNASSIGNED, /* DST shall be set to the unassigned address */ + .send_ttl = 0U, /* TTL shall be set to 0 */ + }; + struct bt_mesh_net_tx tx = { + .ctx = &ctx, + .src = bt_mesh_primary_addr(), + }; + struct net_buf_simple *buf = NULL; + u16_t alloc_len = 0U; + int err = 0; + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) { + tx.sub = bt_mesh_subnet_get(net_idx); + } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) { + tx.sub = bt_mesh_provisioner_subnet_get(net_idx); + } + if (!tx.sub) { + BT_ERR("%s, Failed to find subnet", __func__); + return -EIO; + } + + switch (cfg->opcode) { + case BLE_MESH_PROXY_CFG_FILTER_SET: + if (cfg->set.filter_type > 0x01) { + BT_ERR("%s, Invalid filter type 0x%02x", __func__, cfg->set.filter_type); + return -EINVAL; + } + + alloc_len = sizeof(cfg->opcode) + sizeof(cfg->set.filter_type); + break; + case BLE_MESH_PROXY_CFG_FILTER_ADD: + if (cfg->add.addr == NULL || cfg->add.addr_num == 0) { + BT_ERR("%s, Add address list is NULL", __func__); + return -EINVAL; + } + + alloc_len = sizeof(cfg->opcode) + (cfg->add.addr_num << 1); + break; + case BLE_MESH_PROXY_CFG_FILTER_REMOVE: + if (cfg->remove.addr == NULL || cfg->remove.addr_num == 0) { + BT_ERR("%s, Remove address list is NULL", __func__); + return -EINVAL; + } + + alloc_len = sizeof(cfg->opcode) + (cfg->remove.addr_num << 1); + break; + default: + BT_ERR("%s, Unknown Proxy Configuration opcode 0x%02x", __func__, cfg->opcode); + return -EINVAL; + } + + /** + * For Proxy Configuration PDU: + * 1 octet Proxy PDU type + 9 octets network pdu header + Tranport PDU + 8 octets NetMIC + */ + buf = bt_mesh_alloc_buf(1 + BLE_MESH_NET_HDR_LEN + alloc_len + 8); + if (!buf) { + return -ENOMEM; + } + + net_buf_simple_reset(buf); + net_buf_simple_reserve(buf, 10); + + net_buf_simple_add_u8(buf, cfg->opcode); + switch (cfg->opcode) { + case BLE_MESH_PROXY_CFG_FILTER_SET: + net_buf_simple_add_u8(buf, cfg->set.filter_type); + break; + case BLE_MESH_PROXY_CFG_FILTER_ADD: + for (u16_t i = 0U; i < cfg->add.addr_num; i++) { + net_buf_simple_add_le16(buf, cfg->add.addr[i]); + } + break; + case BLE_MESH_PROXY_CFG_FILTER_REMOVE: + for (u16_t i = 0U; i < cfg->remove.addr_num; i++) { + net_buf_simple_add_le16(buf, cfg->remove.addr[i]); + } + break; + } + + BT_DBG("%s, len %u bytes: %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + err = bt_mesh_net_encode(&tx, buf, true); + if (err) { + BT_ERR("%s, Encoding Proxy message failed (err %d)", __func__, err); + bt_mesh_free_buf(buf); + return err; + } + + err = bt_mesh_proxy_prov_client_send(conn, BLE_MESH_PROXY_CONFIG, buf); + if (err) { + BT_ERR("%s, Failed to send proxy cfg message (err %d)", __func__, err); + } + + bt_mesh_free_buf(buf); + return err; +} + +int bt_mesh_proxy_client_send_cfg(u8_t conn_handle, u16_t net_idx, struct bt_mesh_proxy_cfg_pdu *pdu) +{ + struct bt_mesh_conn *conn = NULL; + + if (conn_handle >= BLE_MESH_MAX_CONN || !pdu || pdu->opcode > BLE_MESH_PROXY_CFG_FILTER_REMOVE) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("conn_handle %d, net_idx 0x%04x", conn_handle, net_idx); + + conn = servers[conn_handle].conn; + if (!conn) { + BT_ERR("%s, Not connected, conn_handle %d", __func__, conn_handle); + return -ENOTCONN; + } + + /** + * Check if net_idx used to encrypt Proxy Configuration are the same + * with the one added when creating proxy connection. + */ + if (servers[conn_handle].net_idx != net_idx) { + BT_ERR("%s, NetKey Index 0x%04x mismatch, expect 0x%04x", + __func__, net_idx, servers[conn_handle].net_idx); + return -EIO; + } + + return send_proxy_cfg(conn, net_idx, pdu); +} +#endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ + +int bt_mesh_proxy_prov_client_init(void) +{ + int i; + + /* Initialize the server receive buffers */ + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + + k_delayed_work_init(&server->sar_timer, proxy_sar_timeout); + server->buf.size = SERVER_BUF_SIZE; + server->buf.__buf = server_buf_data + (i * SERVER_BUF_SIZE); +#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT + server->net_idx = BLE_MESH_KEY_UNUSED; +#endif + } + + bt_mesh_gattc_conn_cb_register(&conn_callbacks); + +#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN && CONFIG_BLE_MESH_GATT_PROXY_CLIENT + bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_ADD, + BLE_MESH_EXCEP_INFO_MESH_PROXY_ADV, NULL); +#endif + + return 0; +} + +int bt_mesh_proxy_prov_client_deinit(void) +{ + int i; + + /* Initialize the server receive buffers */ + for (i = 0; i < ARRAY_SIZE(servers); i++) { + struct bt_mesh_proxy_server *server = &servers[i]; + k_delayed_work_free(&server->sar_timer); + memset(server, 0, sizeof(struct bt_mesh_proxy_server)); + } + + memset(server_buf_data, 0, sizeof(server_buf_data)); + + bt_mesh_gattc_conn_cb_deregister(); + + return 0; +} + +#endif /* (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_CLIENT */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_client.h b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h new file mode 100644 index 000000000..5ca3ea703 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_client.h @@ -0,0 +1,111 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _PROXY_CLIENT_H_ +#define _PROXY_CLIENT_H_ + +#include "net.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_PROXY_ADV_NET_ID 0x00 +#define BLE_MESH_PROXY_ADV_NODE_ID 0x01 + +#define BLE_MESH_PROXY_NET_PDU 0x00 +#define BLE_MESH_PROXY_BEACON 0x01 +#define BLE_MESH_PROXY_CONFIG 0x02 +#define BLE_MESH_PROXY_PROV 0x03 + +#define BLE_MESH_PROXY_CFG_FILTER_SET 0x00 +#define BLE_MESH_PROXY_CFG_FILTER_ADD 0x01 +#define BLE_MESH_PROXY_CFG_FILTER_REMOVE 0x02 +#define BLE_MESH_PROXY_CFG_FILTER_STATUS 0x03 + +typedef union { + struct { + u8_t net_id[8]; + u16_t net_idx; + } net_id; + struct { + u16_t src; + } node_id; +} bt_mesh_proxy_adv_ctx_t; + +struct bt_mesh_proxy_net_pdu { + struct net_buf_simple *val; +}; + +struct bt_mesh_proxy_cfg_pdu { + u8_t opcode; + union { + struct cfg_filter_set { + u8_t filter_type; + } set; + struct cfg_addr_add { + u16_t *addr; + u16_t addr_num; + } add; + struct cfg_addr_remove { + u16_t *addr; + u16_t addr_num; + } remove; + }; +}; + +typedef struct { + u8_t type; + union { + struct bt_mesh_proxy_net_pdu net; + struct bt_mesh_proxy_cfg_pdu cfg; + }; +} bt_mesh_proxy_client_pdu_t; + +int bt_mesh_proxy_prov_client_send(struct bt_mesh_conn *conn, u8_t type, struct net_buf_simple *msg); + +int bt_mesh_provisioner_pb_gatt_enable(void); +int bt_mesh_provisioner_pb_gatt_disable(void); + +int bt_mesh_proxy_client_enable(void); +int bt_mesh_proxy_client_disable(void); + +typedef void (*proxy_client_recv_adv_cb_t)(const bt_mesh_addr_t *addr, u8_t type, bt_mesh_proxy_adv_ctx_t *ctx, s8_t rssi); +typedef void (*proxy_client_connect_cb_t)(const bt_mesh_addr_t *addr, u8_t conn_handle, u16_t net_idx); +typedef void (*proxy_client_disconnect_cb_t)(const bt_mesh_addr_t *addr, u8_t conn_handle, u16_t net_idx, u8_t reason); +typedef void (*proxy_client_recv_filter_status_cb_t)(u8_t conn_handle, u16_t src, u16_t net_idx, u8_t filter_type, u16_t list_size); + +void bt_mesh_proxy_client_set_adv_recv_cb(proxy_client_recv_adv_cb_t cb); +void bt_mesh_proxy_client_set_conn_cb(proxy_client_connect_cb_t cb); +void bt_mesh_proxy_client_set_disconn_cb(proxy_client_disconnect_cb_t cb); +void bt_mesh_proxy_client_set_filter_status_cb(proxy_client_recv_filter_status_cb_t cb); + +void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh_addr_t *addr, s8_t rssi); + +int bt_mesh_proxy_client_connect(const u8_t addr[6], u8_t addr_type, u16_t net_idx); +int bt_mesh_proxy_client_disconnect(u8_t conn_handle); + +bool bt_mesh_proxy_client_beacon_send(struct bt_mesh_subnet *sub); +bool bt_mesh_proxy_client_send(struct net_buf_simple *buf, u16_t dst); +int bt_mesh_proxy_client_send_cfg(u8_t conn_handle, u16_t net_idx, struct bt_mesh_proxy_cfg_pdu *pdu); + +int bt_mesh_proxy_prov_client_init(void); +int bt_mesh_proxy_prov_client_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROXY_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.c b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c new file mode 100644 index 000000000..5f1ef084f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.c @@ -0,0 +1,1454 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_PROXY) + +#include "mesh.h" +#include "adv.h" +#include "prov.h" +#include "beacon.h" +#include "access.h" +#include "foundation.h" +#include "proxy_server.h" + +#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \ + CONFIG_BLE_MESH_GATT_PROXY_SERVER + +/* Not support enabling Proxy Client and Proxy Server simultaneously */ +_Static_assert(!(IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) &&IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_CLIENT)), + "Not support Proxy Server and Proxy Client simultaneously"); + +#define PDU_TYPE(data) (data[0] & BIT_MASK(6)) +#define PDU_SAR(data) (data[0] >> 6) + +/* Mesh Profile 1.0 Section 6.6: + * "The timeout for the SAR transfer is 20 seconds. When the timeout + * expires, the Proxy Server shall disconnect." + */ +#define PROXY_SAR_TIMEOUT K_SECONDS(20) + +#define SAR_COMPLETE 0x00 +#define SAR_FIRST 0x01 +#define SAR_CONT 0x02 +#define SAR_LAST 0x03 + +#define CFG_FILTER_SET 0x00 +#define CFG_FILTER_ADD 0x01 +#define CFG_FILTER_REMOVE 0x02 +#define CFG_FILTER_STATUS 0x03 + +#define PDU_HDR(sar, type) (sar << 6 | (type & BIT_MASK(6))) + +#define CLIENT_BUF_SIZE 68 + +#define ADV_OPT (BLE_MESH_ADV_OPT_CONNECTABLE | BLE_MESH_ADV_OPT_ONE_TIME) + +static const struct bt_mesh_adv_param slow_adv_param = { + .options = ADV_OPT, + .interval_min = BLE_MESH_GAP_ADV_SLOW_INT_MIN, + .interval_max = BLE_MESH_GAP_ADV_SLOW_INT_MAX, +}; + +static const struct bt_mesh_adv_param fast_adv_param = { + .options = ADV_OPT, + .interval_min = BLE_MESH_GAP_ADV_FAST_INT_MIN_0, + .interval_max = BLE_MESH_GAP_ADV_FAST_INT_MAX_0, +}; + +static bool proxy_adv_enabled; + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +static void proxy_send_beacons(struct k_work *work); +static u16_t proxy_ccc_val; +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static u16_t prov_ccc_val; +static bool prov_fast_adv; +#endif + +static struct bt_mesh_proxy_client { + struct bt_mesh_conn *conn; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + u16_t filter[CONFIG_BLE_MESH_PROXY_FILTER_SIZE]; +#endif + enum __packed { + NONE, + WHITELIST, + BLACKLIST, + PROV, + } filter_type; + u8_t msg_type; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + struct k_work send_beacons; +#endif + struct k_delayed_work sar_timer; + struct net_buf_simple buf; +} clients[BLE_MESH_MAX_CONN] = { + [0 ... (BLE_MESH_MAX_CONN - 1)] = { +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .send_beacons = _K_WORK_INITIALIZER(proxy_send_beacons), +#endif + }, +}; + +static u8_t client_buf_data[CLIENT_BUF_SIZE * BLE_MESH_MAX_CONN]; + +/* Track which service is enabled */ +static enum { + MESH_GATT_NONE, + MESH_GATT_PROV, + MESH_GATT_PROXY, +} gatt_svc = MESH_GATT_NONE; + +static char device_name[DEVICE_NAME_SIZE + 1]; + +int bt_mesh_set_device_name(const char *name) +{ + if (!name) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (strlen(name) > DEVICE_NAME_SIZE) { + BT_ERR("%s, Too long device name", __func__); + return -EINVAL; + } + + memset(device_name, 0x0, sizeof(device_name)); + strncpy(device_name, name, DEVICE_NAME_SIZE); + + return bt_mesh_gatts_set_local_device_name(device_name); +} + +static struct bt_mesh_proxy_client *find_client(struct bt_mesh_conn *conn) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn == conn) { + return &clients[i]; + } + } + + return NULL; +} + +static void proxy_sar_timeout(struct k_work *work) +{ + struct bt_mesh_proxy_client *client = NULL; + + BT_WARN("Proxy SAR timeout"); + + client = CONTAINER_OF(work, struct bt_mesh_proxy_client, sar_timer.work); + if (!client || !client->conn) { + BT_ERR("%s, Invalid proxy client parameter", __func__); + return; + } + + net_buf_simple_reset(&client->buf); + bt_mesh_gatts_disconnect(client->conn, 0x13); +} + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +/* Next subnet in queue to be advertised */ +static int next_idx; + +static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg); + +static int filter_set(struct bt_mesh_proxy_client *client, + struct net_buf_simple *buf) +{ + u8_t type = 0U; + + if (buf->len < 1) { + BT_WARN("Too short Filter Set message"); + return -EINVAL; + } + + type = net_buf_simple_pull_u8(buf); + BT_DBG("type 0x%02x", type); + + switch (type) { + case 0x00: + (void)memset(client->filter, 0, sizeof(client->filter)); + client->filter_type = WHITELIST; + break; + case 0x01: + (void)memset(client->filter, 0, sizeof(client->filter)); + client->filter_type = BLACKLIST; + break; + default: + BT_WARN("Prohibited Filter Type 0x%02x", type); + return -EINVAL; + } + + return 0; +} + +static void filter_add(struct bt_mesh_proxy_client *client, u16_t addr) +{ + int i; + + BT_DBG("addr 0x%04x", addr); + + if (addr == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] == addr) { + return; + } + } + + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] == BLE_MESH_ADDR_UNASSIGNED) { + client->filter[i] = addr; + return; + } + } +} + +static void filter_remove(struct bt_mesh_proxy_client *client, u16_t addr) +{ + int i; + + BT_DBG("addr 0x%04x", addr); + + if (addr == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] == addr) { + client->filter[i] = BLE_MESH_ADDR_UNASSIGNED; + return; + } + } +} + +static void send_filter_status(struct bt_mesh_proxy_client *client, + struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + struct bt_mesh_net_tx tx = { + .sub = rx->sub, + .ctx = &rx->ctx, + .src = bt_mesh_primary_addr(), + }; + u16_t filter_size = 0U; + int i, err = 0; + + /* Configuration messages always have dst unassigned */ + tx.ctx->addr = BLE_MESH_ADDR_UNASSIGNED; + + net_buf_simple_reset(buf); + net_buf_simple_reserve(buf, 10); + + net_buf_simple_add_u8(buf, CFG_FILTER_STATUS); + + if (client->filter_type == WHITELIST) { + net_buf_simple_add_u8(buf, 0x00); + } else { + net_buf_simple_add_u8(buf, 0x01); + } + + for (filter_size = 0U, i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] != BLE_MESH_ADDR_UNASSIGNED) { + filter_size++; + } + } + + net_buf_simple_add_be16(buf, filter_size); + + BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len)); + + err = bt_mesh_net_encode(&tx, buf, true); + if (err) { + BT_ERR("%s, Encoding Proxy cfg message failed (err %d)", __func__, err); + return; + } + + err = proxy_segment_and_send(client->conn, BLE_MESH_PROXY_CONFIG, buf); + if (err) { + BT_ERR("%s, Failed to send proxy cfg message (err %d)", __func__, err); + } +} + +static void proxy_cfg(struct bt_mesh_proxy_client *client) +{ + NET_BUF_SIMPLE_DEFINE(buf, 29); + struct bt_mesh_net_rx rx = {0}; + u8_t opcode = 0U; + int err = 0; + + err = bt_mesh_net_decode(&client->buf, BLE_MESH_NET_IF_PROXY_CFG, + &rx, &buf); + if (err) { + BT_ERR("%s, Failed to decode Proxy Configuration (err %d)", __func__, err); + return; + } + + /* Remove network headers */ + net_buf_simple_pull(&buf, BLE_MESH_NET_HDR_LEN); + + BT_DBG("%u bytes: %s", buf.len, bt_hex(buf.data, buf.len)); + + if (buf.len < 1) { + BT_WARN("Too short proxy configuration PDU"); + return; + } + + opcode = net_buf_simple_pull_u8(&buf); + switch (opcode) { + case CFG_FILTER_SET: + filter_set(client, &buf); + send_filter_status(client, &rx, &buf); + break; + case CFG_FILTER_ADD: + while (buf.len >= 2) { + u16_t addr = 0U; + + addr = net_buf_simple_pull_be16(&buf); + filter_add(client, addr); + } + send_filter_status(client, &rx, &buf); + break; + case CFG_FILTER_REMOVE: + while (buf.len >= 2) { + u16_t addr = 0U; + + addr = net_buf_simple_pull_be16(&buf); + filter_remove(client, addr); + } + send_filter_status(client, &rx, &buf); + break; + default: + BT_WARN("Unhandled configuration OpCode 0x%02x", opcode); + break; + } +} + +static int beacon_send(struct bt_mesh_conn *conn, struct bt_mesh_subnet *sub) +{ + NET_BUF_SIMPLE_DEFINE(buf, 23); + + net_buf_simple_reserve(&buf, 1); + bt_mesh_beacon_create(sub, &buf); + + return proxy_segment_and_send(conn, BLE_MESH_PROXY_BEACON, &buf); +} + +static void proxy_send_beacons(struct k_work *work) +{ + struct bt_mesh_proxy_client *client = NULL; + int i; + + client = CONTAINER_OF(work, struct bt_mesh_proxy_client, send_beacons); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx != BLE_MESH_KEY_UNUSED) { + beacon_send(client->conn, sub); + } + } +} + +void bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub) +{ + int i; + + if (!sub) { + /* NULL means we send on all subnets */ + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx != BLE_MESH_KEY_UNUSED) { + bt_mesh_proxy_beacon_send(&bt_mesh.sub[i]); + } + } + + return; + } + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn) { + beacon_send(clients[i].conn, sub); + } + } +} + +void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub) +{ + sub->node_id = BLE_MESH_NODE_IDENTITY_RUNNING; + sub->node_id_start = k_uptime_get_32(); + + /* Prioritize the recently enabled subnet */ + next_idx = sub - bt_mesh.sub; +} + +void bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub) +{ + sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; + sub->node_id_start = 0U; +} + +int bt_mesh_proxy_identity_enable(void) +{ + int i, count = 0; + + BT_DBG("%s", __func__); + + if (!bt_mesh_is_provisioned()) { + return -EAGAIN; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + if (sub->node_id == BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED) { + continue; + } + + bt_mesh_proxy_identity_start(sub); + count++; + } + + if (count) { + bt_mesh_adv_update(); + } + + return 0; +} + +#endif /* GATT_PROXY */ + +static void proxy_complete_pdu(struct bt_mesh_proxy_client *client) +{ + switch (client->msg_type) { +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + case BLE_MESH_PROXY_NET_PDU: + BT_DBG("Mesh Network PDU"); + bt_mesh_net_recv(&client->buf, 0, BLE_MESH_NET_IF_PROXY); + break; + case BLE_MESH_PROXY_BEACON: + BT_DBG("Mesh Beacon PDU"); + bt_mesh_beacon_recv(&client->buf, 0); + break; + case BLE_MESH_PROXY_CONFIG: + BT_DBG("Mesh Configuration PDU"); + proxy_cfg(client); + break; +#endif +#if defined(CONFIG_BLE_MESH_PB_GATT) + case BLE_MESH_PROXY_PROV: + BT_DBG("Mesh Provisioning PDU"); + bt_mesh_pb_gatt_recv(client->conn, &client->buf); + break; +#endif + default: + BT_WARN("Unhandled Message Type 0x%02x", client->msg_type); + break; + } + + net_buf_simple_reset(&client->buf); +} + +#define ATTR_IS_PROV(attr) (attr->user_data != NULL) + +static ssize_t proxy_recv(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, const void *buf, + u16_t len, u16_t offset, u8_t flags) +{ + struct bt_mesh_proxy_client *client = find_client(conn); + const u8_t *data = buf; + + if (!client) { + return -ENOTCONN; + } + + if (len < 1) { + BT_WARN("Too small Proxy PDU"); + return -EINVAL; + } + + if (ATTR_IS_PROV(attr) != (PDU_TYPE(data) == BLE_MESH_PROXY_PROV)) { + BT_WARN("Proxy PDU type doesn't match GATT service"); + return -EINVAL; + } + + if (len - 1 > net_buf_simple_tailroom(&client->buf)) { + BT_WARN("Too big proxy PDU"); + return -EINVAL; + } + + switch (PDU_SAR(data)) { + case SAR_COMPLETE: + if (client->buf.len) { + BT_WARN("Complete PDU while a pending incomplete one"); + return -EINVAL; + } + + client->msg_type = PDU_TYPE(data); + net_buf_simple_add_mem(&client->buf, data + 1, len - 1); + proxy_complete_pdu(client); + break; + + case SAR_FIRST: + if (client->buf.len) { + BT_WARN("First PDU while a pending incomplete one"); + return -EINVAL; + } + + k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); + client->msg_type = PDU_TYPE(data); + net_buf_simple_add_mem(&client->buf, data + 1, len - 1); + break; + + case SAR_CONT: + if (!client->buf.len) { + BT_WARN("Continuation with no prior data"); + return -EINVAL; + } + + if (client->msg_type != PDU_TYPE(data)) { + BT_WARN("Unexpected message type in continuation"); + return -EINVAL; + } + + k_delayed_work_submit(&client->sar_timer, PROXY_SAR_TIMEOUT); + net_buf_simple_add_mem(&client->buf, data + 1, len - 1); + break; + + case SAR_LAST: + if (!client->buf.len) { + BT_WARN("Last SAR PDU with no prior data"); + return -EINVAL; + } + + if (client->msg_type != PDU_TYPE(data)) { + BT_WARN("Unexpected message type in last SAR PDU"); + return -EINVAL; + } + + k_delayed_work_cancel(&client->sar_timer); + net_buf_simple_add_mem(&client->buf, data + 1, len - 1); + proxy_complete_pdu(client); + break; + } + + return len; +} + +static int conn_count; + +static void proxy_connected(struct bt_mesh_conn *conn, u8_t err) +{ + struct bt_mesh_proxy_client *client = NULL; + int i; + + BT_DBG("conn %p err 0x%02x", conn, err); + + conn_count++; + + /* Since we use ADV_OPT_ONE_TIME */ + proxy_adv_enabled = false; + + /* Try to re-enable advertising in case it's possible */ + if (conn_count < BLE_MESH_MAX_CONN) { + bt_mesh_adv_update(); + } + + for (client = NULL, i = 0; i < ARRAY_SIZE(clients); i++) { + if (!clients[i].conn) { + client = &clients[i]; + break; + } + } + + if (!client) { + BT_ERR("%s, No free Proxy Client objects", __func__); + return; + } + + client->conn = bt_mesh_conn_ref(conn); + client->filter_type = NONE; +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + (void)memset(client->filter, 0, sizeof(client->filter)); +#endif + net_buf_simple_reset(&client->buf); +} + +static void proxy_disconnected(struct bt_mesh_conn *conn, u8_t reason) +{ + int i; + + BT_DBG("conn %p reason 0x%02x", conn, reason); + + conn_count--; + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + + if (client->conn == conn) { + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + client->filter_type == PROV) { + bt_mesh_pb_gatt_close(conn); + } + + k_delayed_work_cancel(&client->sar_timer); + bt_mesh_conn_unref(client->conn); + client->conn = NULL; + break; + } + } + + bt_mesh_adv_update(); +} + +struct net_buf_simple *bt_mesh_proxy_get_buf(void) +{ + struct net_buf_simple *buf = &clients[0].buf; + + net_buf_simple_reset(buf); + + return buf; +} + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static ssize_t prov_ccc_write(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + const void *buf, u16_t len, + u16_t offset, u8_t flags) +{ + struct bt_mesh_proxy_client *client = NULL; + u16_t *value = attr->user_data; + + BT_DBG("len %u: %s", len, bt_hex(buf, len)); + + if (len != sizeof(*value)) { + return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_ATTRIBUTE_LEN); + } + + *value = sys_get_le16(buf); + if (*value != BLE_MESH_GATT_CCC_NOTIFY) { + BT_WARN("Client wrote 0x%04x instead enabling notify", *value); + return len; + } + + /* If a connection exists there must be a client */ + client = find_client(conn); + __ASSERT(client, "No client for connection"); + + if (client->filter_type == NONE) { + client->filter_type = PROV; + bt_mesh_pb_gatt_open(conn); + } + + return len; +} + +static ssize_t prov_ccc_read(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + u16_t *value = attr->user_data; + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, value, + sizeof(*value)); +} + +/* Mesh Provisioning Service Declaration */ +static struct bt_mesh_gatt_attr prov_attrs[] = { + BLE_MESH_GATT_PRIMARY_SERVICE(BLE_MESH_UUID_MESH_PROV), + + BLE_MESH_GATT_CHARACTERISTIC(BLE_MESH_UUID_MESH_PROV_DATA_IN, + BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP), + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_MESH_PROV_DATA_IN, BLE_MESH_GATT_PERM_WRITE, + NULL, proxy_recv, (void *)1), + + BLE_MESH_GATT_CHARACTERISTIC(BLE_MESH_UUID_MESH_PROV_DATA_OUT, + BLE_MESH_GATT_CHRC_NOTIFY), + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_MESH_PROV_DATA_OUT, BLE_MESH_GATT_PERM_NONE, + NULL, NULL, NULL), + /* Add custom CCC as clients need to be tracked individually */ + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_GATT_CCC, + BLE_MESH_GATT_PERM_WRITE | BLE_MESH_GATT_PERM_READ, + prov_ccc_read, prov_ccc_write, &prov_ccc_val), +}; + +struct bt_mesh_gatt_service prov_svc = BLE_MESH_GATT_SERVICE(prov_attrs); + +int bt_mesh_proxy_prov_enable(void) +{ + int i; + + BT_DBG("%s", __func__); + + if (gatt_svc == MESH_GATT_PROV) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + if (gatt_svc != MESH_GATT_NONE) { + BT_WARN("%s, Busy", __func__); + return -EBUSY; + } + + bt_mesh_gatts_service_start(&prov_svc); + gatt_svc = MESH_GATT_PROV; + prov_fast_adv = true; + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn) { + clients[i].filter_type = PROV; + } + } + + + return 0; +} + +int bt_mesh_proxy_prov_disable(bool disconnect) +{ + int i; + + BT_DBG("%s", __func__); + + if (gatt_svc == MESH_GATT_NONE) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + if (gatt_svc != MESH_GATT_PROV) { + BT_WARN("%s, Busy", __func__); + return -EBUSY; + } + + bt_mesh_gatts_service_stop(&prov_svc); + gatt_svc = MESH_GATT_NONE; + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + + if (!client->conn || client->filter_type != PROV) { + continue; + } + + if (disconnect) { + bt_mesh_gatts_disconnect(client->conn, 0x13); + } else { + bt_mesh_pb_gatt_close(client->conn); + client->filter_type = NONE; + } + } + + bt_mesh_adv_update(); + + return 0; +} + +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) +static ssize_t proxy_ccc_write(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + const void *buf, u16_t len, + u16_t offset, u8_t flags) +{ + struct bt_mesh_proxy_client *client = NULL; + u16_t value = 0U; + + BT_DBG("len %u: %s", len, bt_hex(buf, len)); + + if (len != sizeof(value)) { + return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_ATTRIBUTE_LEN); + } + + value = sys_get_le16(buf); + if (value != BLE_MESH_GATT_CCC_NOTIFY) { + BT_WARN("Client wrote 0x%04x instead enabling notify", value); + return len; + } + + /* If a connection exists there must be a client */ + client = find_client(conn); + __ASSERT(client, "No client for connection"); + + if (client->filter_type == NONE) { + client->filter_type = WHITELIST; + k_work_submit(&client->send_beacons); + } + + return len; +} + +static ssize_t proxy_ccc_read(struct bt_mesh_conn *conn, + const struct bt_mesh_gatt_attr *attr, + void *buf, u16_t len, u16_t offset) +{ + u16_t *value = attr->user_data; + + return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, value, + sizeof(*value)); +} + +/* Mesh Proxy Service Declaration */ +static struct bt_mesh_gatt_attr proxy_attrs[] = { + BLE_MESH_GATT_PRIMARY_SERVICE(BLE_MESH_UUID_MESH_PROXY), + + BLE_MESH_GATT_CHARACTERISTIC(BLE_MESH_UUID_MESH_PROXY_DATA_IN, + BLE_MESH_GATT_CHRC_WRITE_WITHOUT_RESP), + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_MESH_PROXY_DATA_IN, BLE_MESH_GATT_PERM_WRITE, + NULL, proxy_recv, NULL), + + BLE_MESH_GATT_CHARACTERISTIC(BLE_MESH_UUID_MESH_PROXY_DATA_OUT, + BLE_MESH_GATT_CHRC_NOTIFY), + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_MESH_PROXY_DATA_OUT, BLE_MESH_GATT_PERM_NONE, + NULL, NULL, NULL), + /* Add custom CCC as clients need to be tracked individually */ + BLE_MESH_GATT_DESCRIPTOR(BLE_MESH_UUID_GATT_CCC, + BLE_MESH_GATT_PERM_READ | BLE_MESH_GATT_PERM_WRITE, + proxy_ccc_read, proxy_ccc_write, &proxy_ccc_val), +}; + +struct bt_mesh_gatt_service proxy_svc = BLE_MESH_GATT_SERVICE(proxy_attrs); + +int bt_mesh_proxy_gatt_enable(void) +{ + int i; + + BT_DBG("%s", __func__); + + if (gatt_svc == MESH_GATT_PROXY) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + if (gatt_svc != MESH_GATT_NONE) { + BT_WARN("%s, Busy", __func__); + return -EBUSY; + } + + bt_mesh_gatts_service_start(&proxy_svc); + gatt_svc = MESH_GATT_PROXY; + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].conn) { + clients[i].filter_type = WHITELIST; + } + } + + return 0; +} + +void bt_mesh_proxy_gatt_disconnect(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + + if (client->conn && (client->filter_type == WHITELIST || + client->filter_type == BLACKLIST)) { + client->filter_type = NONE; + bt_mesh_gatts_disconnect(client->conn, 0x13); + } + } +} + +int bt_mesh_proxy_gatt_disable(void) +{ + BT_DBG("%s", __func__); + + if (gatt_svc == MESH_GATT_NONE) { + BT_WARN("%s, Already", __func__); + return -EALREADY; + } + + if (gatt_svc != MESH_GATT_PROXY) { + BT_WARN("%s, Busy", __func__); + return -EBUSY; + } + + bt_mesh_proxy_gatt_disconnect(); + + bt_mesh_gatts_service_stop(&proxy_svc); + gatt_svc = MESH_GATT_NONE; + + return 0; +} + +void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, u16_t addr) +{ + struct bt_mesh_proxy_client *client = + CONTAINER_OF(buf, struct bt_mesh_proxy_client, buf); + + BT_INFO("filter_type %u addr 0x%04x", client->filter_type, addr); + + if (client->filter_type == WHITELIST) { + filter_add(client, addr); + } else if (client->filter_type == BLACKLIST) { + filter_remove(client, addr); + } +} + +static bool client_filter_match(struct bt_mesh_proxy_client *client, + u16_t addr) +{ + int i; + + BT_DBG("filter_type %u addr 0x%04x", client->filter_type, addr); + + if (client->filter_type == BLACKLIST) { + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] == addr) { + return false; + } + } + + return true; + } + + if (addr == BLE_MESH_ADDR_ALL_NODES) { + return true; + } + + if (client->filter_type == WHITELIST) { + for (i = 0; i < ARRAY_SIZE(client->filter); i++) { + if (client->filter[i] == addr) { + return true; + } + } + } + + return false; +} + +bool bt_mesh_proxy_relay(struct net_buf_simple *buf, u16_t dst) +{ + bool relayed = false; + int i; + + BT_DBG("%u bytes to dst 0x%04x", buf->len, dst); + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + NET_BUF_SIMPLE_DEFINE(msg, 32); + + if (!client->conn) { + continue; + } + + if (!client_filter_match(client, dst)) { + continue; + } + + /* Proxy PDU sending modifies the original buffer, + * so we need to make a copy. + */ + net_buf_simple_reserve(&msg, 1); + net_buf_simple_add_mem(&msg, buf->data, buf->len); + + bt_mesh_proxy_send(client->conn, BLE_MESH_PROXY_NET_PDU, &msg); + relayed = true; + } + + return relayed; +} + +#endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */ + +static int proxy_send(struct bt_mesh_conn *conn, const void *data, u16_t len) +{ + BT_DBG("%u bytes: %s", len, bt_hex(data, len)); + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + if (gatt_svc == MESH_GATT_PROXY) { + return bt_mesh_gatts_notify(conn, &proxy_attrs[4], data, len); + } +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (gatt_svc == MESH_GATT_PROV) { + return bt_mesh_gatts_notify(conn, &prov_attrs[4], data, len); + } +#endif + + return 0; +} + +static int proxy_segment_and_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg) +{ + u16_t mtu = 0U; + + BT_DBG("conn %p type 0x%02x len %u: %s", conn, type, msg->len, + bt_hex(msg->data, msg->len)); + + /* ATT_MTU - OpCode (1 byte) - Handle (2 bytes) */ + mtu = bt_mesh_gatt_get_mtu(conn) - 3; + if (mtu > msg->len) { + net_buf_simple_push_u8(msg, PDU_HDR(SAR_COMPLETE, type)); + return proxy_send(conn, msg->data, msg->len); + } + + net_buf_simple_push_u8(msg, PDU_HDR(SAR_FIRST, type)); + proxy_send(conn, msg->data, mtu); + net_buf_simple_pull(msg, mtu); + + while (msg->len) { + if (msg->len + 1 < mtu) { + net_buf_simple_push_u8(msg, PDU_HDR(SAR_LAST, type)); + proxy_send(conn, msg->data, msg->len); + break; + } + + net_buf_simple_push_u8(msg, PDU_HDR(SAR_CONT, type)); + proxy_send(conn, msg->data, mtu); + net_buf_simple_pull(msg, mtu); + } + + return 0; +} + +int bt_mesh_proxy_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg) +{ + struct bt_mesh_proxy_client *client = find_client(conn); + + if (!client) { + BT_ERR("%s, No Proxy Client found", __func__); + return -ENOTCONN; + } + + if ((client->filter_type == PROV) != (type == BLE_MESH_PROXY_PROV)) { + BT_ERR("%s, Invalid PDU type for Proxy Client", __func__); + return -EINVAL; + } + + return proxy_segment_and_send(conn, type, msg); +} + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static u8_t prov_svc_data[20] = { 0x27, 0x18, }; + +static const struct bt_mesh_adv_data prov_ad[] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x27, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, prov_svc_data, sizeof(prov_svc_data)), +}; +#endif /* PB_GATT */ + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + +#define ID_TYPE_NET 0x00 +#define ID_TYPE_NODE 0x01 + +#define NODE_ID_LEN 19 +#define NET_ID_LEN 11 + +#define NODE_ID_TIMEOUT K_SECONDS(CONFIG_BLE_MESH_NODE_ID_TIMEOUT) + +static u8_t proxy_svc_data[NODE_ID_LEN] = { 0x28, 0x18, }; + +static const struct bt_mesh_adv_data node_id_ad[] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x28, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, proxy_svc_data, NODE_ID_LEN), +}; + +static const struct bt_mesh_adv_data net_id_ad[] = { + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_FLAGS, (BLE_MESH_AD_GENERAL | BLE_MESH_AD_NO_BREDR)), + BLE_MESH_ADV_DATA_BYTES(BLE_MESH_DATA_UUID16_ALL, 0x28, 0x18), + BLE_MESH_ADV_DATA(BLE_MESH_DATA_SVC_DATA16, proxy_svc_data, NET_ID_LEN), +}; + +static size_t gatt_proxy_adv_create(struct bt_mesh_adv_data *proxy_sd) +{ + const char *name = device_name; + size_t name_len = strlen(name); + /* One octet for Length, and another octet for AD type */ + size_t sd_space = 29; + + if (name_len > sd_space) { + proxy_sd->type = BLE_MESH_DATA_NAME_SHORTENED; + proxy_sd->data_len = sd_space; + } else { + proxy_sd->type = BLE_MESH_DATA_NAME_COMPLETE; + proxy_sd->data_len = name_len; + } + + proxy_sd->data = (const u8_t *)name; + + return 1; +} + +static int node_id_adv(struct bt_mesh_subnet *sub) +{ + struct bt_mesh_adv_data proxy_sd = {0}; + size_t proxy_sd_len = 0U; + u8_t tmp[16] = {0}; + int err = 0; + + BT_DBG("%s", __func__); + + proxy_svc_data[2] = ID_TYPE_NODE; + + err = bt_mesh_rand(proxy_svc_data + 11, 8); + if (err) { + return err; + } + + (void)memset(tmp, 0, 6); + memcpy(tmp + 6, proxy_svc_data + 11, 8); + sys_put_be16(bt_mesh_primary_addr(), tmp + 14); + + err = bt_mesh_encrypt_be(sub->keys[sub->kr_flag].identity, tmp, tmp); + if (err) { + return err; + } + + memcpy(proxy_svc_data + 3, tmp + 8, 8); + proxy_sd_len = gatt_proxy_adv_create(&proxy_sd); + + err = bt_le_adv_start(&fast_adv_param, node_id_ad, + ARRAY_SIZE(node_id_ad), &proxy_sd, proxy_sd_len); + if (err) { + BT_WARN("Failed to advertise using Node ID (err %d)", err); + return err; + } + + proxy_adv_enabled = true; + + return 0; +} + +static int net_id_adv(struct bt_mesh_subnet *sub) +{ + struct bt_mesh_adv_data proxy_sd = {0}; + size_t proxy_sd_len = 0U; + int err = 0; + + BT_DBG("%s", __func__); + + proxy_svc_data[2] = ID_TYPE_NET; + + BT_DBG("Advertising with NetId %s", + bt_hex(sub->keys[sub->kr_flag].net_id, 8)); + + memcpy(proxy_svc_data + 3, sub->keys[sub->kr_flag].net_id, 8); + proxy_sd_len = gatt_proxy_adv_create(&proxy_sd); + + err = bt_le_adv_start(&slow_adv_param, net_id_ad, + ARRAY_SIZE(net_id_ad), &proxy_sd, proxy_sd_len); + if (err) { + BT_WARN("Failed to advertise using Network ID (err %d)", err); + return err; + } + + proxy_adv_enabled = true; + + return 0; +} + +static bool advertise_subnet(struct bt_mesh_subnet *sub) +{ + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + return false; + } + + return (sub->node_id == BLE_MESH_NODE_IDENTITY_RUNNING || + bt_mesh_gatt_proxy_get() != BLE_MESH_GATT_PROXY_NOT_SUPPORTED); +} + +static struct bt_mesh_subnet *next_sub(void) +{ + struct bt_mesh_subnet *sub = NULL; + int i; + + for (i = next_idx; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; + if (advertise_subnet(sub)) { + next_idx = (i + 1) % ARRAY_SIZE(bt_mesh.sub); + return sub; + } + } + + /** + * If among [next_idx, ARRAY_SIZE(bt_mesh.sub)], there is no subnet + * to advertise, then try to start advertising from Primary subnet. + */ + for (i = 0; i < next_idx; i++) { + sub = &bt_mesh.sub[i]; + if (advertise_subnet(sub)) { + next_idx = (i + 1) % ARRAY_SIZE(bt_mesh.sub); + return sub; + } + } + + return NULL; +} + +static int sub_count(void) +{ + int i, count = 0; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + struct bt_mesh_subnet *sub = &bt_mesh.sub[i]; + + if (advertise_subnet(sub)) { + count++; + } + } + + return count; +} + +static s32_t gatt_proxy_advertise(struct bt_mesh_subnet *sub) +{ + s32_t remaining = K_FOREVER; + int subnet_count = 0; + + BT_DBG("%s", __func__); + + if (conn_count == BLE_MESH_MAX_CONN) { + BT_WARN("Connectable advertising deferred (max connections)"); + return remaining; + } + + if (!sub) { + BT_WARN("No subnets to advertise on"); + return remaining; + } + + if (sub->node_id == BLE_MESH_NODE_IDENTITY_RUNNING) { + u32_t active = k_uptime_get_32() - sub->node_id_start; + + if (active < NODE_ID_TIMEOUT) { + remaining = NODE_ID_TIMEOUT - active; + BT_DBG("Node ID active for %u ms, %d ms remaining", + active, remaining); + node_id_adv(sub); + } else { + bt_mesh_proxy_identity_stop(sub); + BT_DBG("Node ID stopped"); + } + } + + if (sub->node_id == BLE_MESH_NODE_IDENTITY_STOPPED) { + net_id_adv(sub); + } + + subnet_count = sub_count(); + BT_DBG("sub_count %u", subnet_count); + if (subnet_count > 1) { + s32_t max_timeout = 0; + + /* We use NODE_ID_TIMEOUT as a starting point since it may + * be less than 60 seconds. Divide this period into at least + * 6 slices, but make sure that a slice is at least one + * second long (to avoid excessive rotation). + */ + max_timeout = NODE_ID_TIMEOUT / MAX(subnet_count, 6); + max_timeout = MAX(max_timeout, K_SECONDS(1)); + + if (remaining > max_timeout || remaining < 0) { + remaining = max_timeout; + } + } + + BT_DBG("Advertising %d ms for net_idx 0x%04x", remaining, sub->net_idx); + + return remaining; +} +#endif /* GATT_PROXY */ + +#if defined(CONFIG_BLE_MESH_PB_GATT) +static size_t gatt_prov_adv_create(struct bt_mesh_adv_data prov_sd[2]) +{ + const struct bt_mesh_prov *prov = bt_mesh_prov_get(); + const char *name = device_name; + size_t name_len = strlen(name); + size_t prov_sd_len = 0U; + size_t sd_space = 31U; + + memcpy(prov_svc_data + 2, prov->uuid, 16); + sys_put_be16(prov->oob_info, prov_svc_data + 18); + + if (prov->uri) { + size_t uri_len = strlen(prov->uri); + + if (uri_len > 29) { + /* There's no way to shorten an URI */ + BT_WARN("Too long URI to fit advertising packet"); + } else { + prov_sd[0].type = BLE_MESH_DATA_URI; + prov_sd[0].data_len = uri_len; + prov_sd[0].data = (const u8_t *)prov->uri; + sd_space -= 2 + uri_len; + prov_sd_len++; + } + } + + if (sd_space > 2 && name_len > 0) { + sd_space -= 2; + + if (sd_space < name_len) { + prov_sd[prov_sd_len].type = BLE_MESH_DATA_NAME_SHORTENED; + prov_sd[prov_sd_len].data_len = sd_space; + } else { + prov_sd[prov_sd_len].type = BLE_MESH_DATA_NAME_COMPLETE; + prov_sd[prov_sd_len].data_len = name_len; + } + + prov_sd[prov_sd_len].data = (const u8_t *)name; + prov_sd_len++; + } + + return prov_sd_len; +} +#endif /* CONFIG_BLE_MESH_PB_GATT */ + +s32_t bt_mesh_proxy_adv_start(void) +{ + BT_DBG("%s", __func__); + + if (gatt_svc == MESH_GATT_NONE) { + return K_FOREVER; + } + +#if defined(CONFIG_BLE_MESH_PB_GATT) + if (!bt_mesh_is_provisioned()) { + const struct bt_mesh_adv_param *param; + struct bt_mesh_adv_data prov_sd[2]; + size_t prov_sd_len; + + if (prov_fast_adv) { + param = &fast_adv_param; + } else { + param = &slow_adv_param; + } + + prov_sd_len = gatt_prov_adv_create(prov_sd); + + if (bt_le_adv_start(param, prov_ad, ARRAY_SIZE(prov_ad), + prov_sd, prov_sd_len) == 0) { + proxy_adv_enabled = true; + + /* Advertise 60 seconds using fast interval */ + if (prov_fast_adv) { + prov_fast_adv = false; + return K_SECONDS(60); + } + } + } +#endif /* PB_GATT */ + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + if (bt_mesh_is_provisioned()) { + return gatt_proxy_advertise(next_sub()); + } +#endif /* GATT_PROXY */ + + return K_FOREVER; +} + +void bt_mesh_proxy_adv_stop(void) +{ + int err = 0; + + BT_DBG("adv_enabled %u", proxy_adv_enabled); + + if (!proxy_adv_enabled) { + return; + } + + err = bt_le_adv_stop(); + if (err) { + BT_ERR("%s, Failed to stop advertising (err %d)", __func__, err); + } else { + proxy_adv_enabled = false; + } +} + +static struct bt_mesh_conn_cb conn_callbacks = { + .connected = proxy_connected, + .disconnected = proxy_disconnected, +}; + +int bt_mesh_proxy_init(void) +{ + int i; + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + bt_mesh_gatts_service_register(&proxy_svc); +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_gatts_service_register(&prov_svc); +#endif + + /* Initialize the client receive buffers */ + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + + client->buf.size = CLIENT_BUF_SIZE; + client->buf.__buf = client_buf_data + (i * CLIENT_BUF_SIZE); + + k_delayed_work_init(&client->sar_timer, proxy_sar_timeout); + } + + bt_mesh_gatts_conn_cb_register(&conn_callbacks); + + strncpy(device_name, "ESP-BLE-MESH", DEVICE_NAME_SIZE); + return bt_mesh_gatts_set_local_device_name(device_name); +} + +int bt_mesh_proxy_deinit(void) +{ + int i; + + proxy_adv_enabled = false; + gatt_svc = MESH_GATT_NONE; + +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + bt_mesh_gatts_service_deregister(&proxy_svc); + next_idx = 0; +#endif + +#if defined(CONFIG_BLE_MESH_PB_GATT) + bt_mesh_gatts_service_deregister(&prov_svc); +#endif + + for (i = 0; i < ARRAY_SIZE(clients); i++) { + struct bt_mesh_proxy_client *client = &clients[i]; + k_delayed_work_free(&client->sar_timer); + memset(client, 0, sizeof(struct bt_mesh_proxy_client)); + } + + memset(client_buf_data, 0, sizeof(client_buf_data)); + memset(device_name, 0, sizeof(device_name)); + + bt_mesh_gatts_conn_cb_deregister(); + conn_count = 0; + + return 0; +} + +#endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */ diff --git a/components/bt/esp_ble_mesh/mesh_core/proxy_server.h b/components/bt/esp_ble_mesh/mesh_core/proxy_server.h new file mode 100644 index 000000000..724561f41 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/proxy_server.h @@ -0,0 +1,73 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _PROXY_H_ +#define _PROXY_H_ + +#include "net.h" +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_PROXY_NET_PDU 0x00 +#define BLE_MESH_PROXY_BEACON 0x01 +#define BLE_MESH_PROXY_CONFIG 0x02 +#define BLE_MESH_PROXY_PROV 0x03 + +#if CONFIG_BLE_MESH_PROXY +/** + * Device Name Characteristic: + * 1. For iOS, when it tries to get the value of Device Name Characteristic, the PDU + * "Read By Type Request" will be used, and the valid length of corresponding + * response is 19 (23 - 1 - 1 - 2). + * 2. For Android, when it tries to get the value of Device Name Characteristic, the + * PDU "Read Request" will be used, and the valid length of corresponding response + * is 22 (23 - 1). + */ +#define DEVICE_NAME_SIZE MIN((BLE_MESH_GATT_DEF_MTU_SIZE - 4), (BLE_MESH_GAP_ADV_MAX_LEN - 2)) +#else +/* For Scan Response Data, the maximum length is 29 (31 - 1 - 1) currently. */ +#define DEVICE_NAME_SIZE (BLE_MESH_GAP_ADV_MAX_LEN - 2) +#endif + +int bt_mesh_set_device_name(const char *name); + +int bt_mesh_proxy_send(struct bt_mesh_conn *conn, u8_t type, + struct net_buf_simple *msg); + +int bt_mesh_proxy_prov_enable(void); +int bt_mesh_proxy_prov_disable(bool disconnect); + +int bt_mesh_proxy_gatt_enable(void); +int bt_mesh_proxy_gatt_disable(void); +void bt_mesh_proxy_gatt_disconnect(void); + +void bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub); + +struct net_buf_simple *bt_mesh_proxy_get_buf(void); + +s32_t bt_mesh_proxy_adv_start(void); +void bt_mesh_proxy_adv_stop(void); + +void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub); +void bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub); + +bool bt_mesh_proxy_relay(struct net_buf_simple *buf, u16_t dst); +void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, u16_t addr); + +int bt_mesh_proxy_init(void); +int bt_mesh_proxy_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _PROXY_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.c b/components/bt/esp_ble_mesh/mesh_core/settings.c new file mode 100644 index 000000000..ea15e02a9 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/settings.c @@ -0,0 +1,2589 @@ +/* + * Copyright (c) 2018 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_SETTINGS) + +#include "mesh.h" +#include "crypto.h" +#include "transport.h" +#include "access.h" +#include "foundation.h" +#include "proxy_server.h" +#include "cfg_srv.h" +#include "mesh_common.h" +#include "settings_nvs.h" +#include "provisioner_main.h" +#include "provisioner_prov.h" + +/* BLE Mesh NVS Key and corresponding data struct. + * Note: The length of nvs key must be <= 15. + * "xxxx" (2 octet) means the rpl_src, net_idx, app_idx, model_key, etc. + * Model model_key is a combination "elem_idx << 8 | model_idx". + * key: "mesh/net" -> write/read to set/get NET data + * key: "mesh/iv" -> write/read to set/get IV data + * key: "mesh/seq" -> write/read to set/get SEQ data + * key: "mesh/hb_pub" -> write/read to set/get CFG HB_PUB data + * key: "mesh/cfg" -> write/read to set/get CFG data + * key: "mesh/rpl" -> write/read to set/get all RPL src. + * key: "mesh/rpl/xxxx" -> write/read to set/get the "xxxx" RPL data + * key: "mesh/netkey" -> write/read to set/get all NetKey Indexes + * key: "mesh/nk/xxxx" -> write/read to set/get the "xxxx" NetKey data + * key: "mesh/appkey" -> write/read to set/get all AppKey Indexes + * key: "mesh/ak/xxxx" -> write/read to set/get the "xxxx" AppKey data + * key: "mesh/sig" -> write/read to set/get all SIG MODEL model_keys. + * key: "mesh/s/xxxx/b" -> write/read to set/get SIG MODEL Bind AppKey List + * key: "mesh/s/xxxx/s" -> write/read to set/get SIG MODEL Subscription List + * key: "mesh/s/xxxx/p" -> write/read to set/get SIG MODEL Publication + * key: "mesh/vnd" -> write/read to set/get all VENDOR MODEL model_keys. + * key: "mesh/v/xxxx/b" -> write/read to set/get VENDOR MODEL Bind AppKey List + * key: "mesh/v/xxxx/s" -> write/read to set/get VENDOR MODEL Subscription List + * key: "mesh/v/xxxx/p" -> write/read to set/get VENDOR MODEL Publication + * key: "mesh/vaddr" -> write/read to set/get all virtual addresses + * key: "mesh/va/xxxx" -> write/read to set/get the "xxxx" virtual address + */ + +#if CONFIG_BLE_MESH_SETTINGS + +/* Tracking of what storage changes are pending for App and Net Keys. We + * track this in a separate array here instead of within the respective + * bt_mesh_app_key and bt_mesh_subnet structs themselves, since once a key + * gets deleted its struct becomes invalid and may be reused for other keys. + */ +static struct key_update { + u16_t key_idx: 12, /* AppKey or NetKey Index */ + valid: 1, /* 1 if this entry is valid, 0 if not */ + app_key: 1, /* 1 if this is an AppKey, 0 if a NetKey */ + clear: 1; /* 1 if key needs clearing, 0 if storing */ +} key_updates[CONFIG_BLE_MESH_APP_KEY_COUNT + CONFIG_BLE_MESH_SUBNET_COUNT]; + +static struct k_delayed_work pending_store; + +/* Mesh network storage information */ +struct net_val { + u16_t primary_addr; + u8_t dev_key[16]; +} __packed; + +/* Sequence number storage */ +struct seq_val { + u8_t val[3]; +} __packed; + +/* Heartbeat Publication storage */ +struct hb_pub_val { + u16_t dst; + u8_t period; + u8_t ttl; + u16_t feat; + u16_t net_idx: 12, + indefinite: 1; +}; + +/* Miscellaneous configuration server model states */ +struct cfg_val { + u8_t net_transmit; + u8_t relay; + u8_t relay_retransmit; + u8_t beacon; + u8_t gatt_proxy; + u8_t frnd; + u8_t default_ttl; +}; + +/* IV Index & IV Update storage */ +struct iv_val { + u32_t iv_index; + u8_t iv_update: 1, + iv_duration: 7; +} __packed; + +/* Replay Protection List storage */ +struct rpl_val { + u32_t seq: 24, + old_iv: 1; +}; + +/* NetKey storage information */ +struct net_key_val { + u8_t kr_flag: 1, + kr_phase: 7; + u8_t val[2][16]; +} __packed; + +/* AppKey storage information */ +struct app_key_val { + u16_t net_idx; + bool updated; + u8_t val[2][16]; +} __packed; + +struct mod_pub_val { + u16_t addr; + u16_t key; + u8_t ttl; + u8_t retransmit; + u8_t period; + u8_t period_div: 4, + cred: 1; +}; + +/* Virtual Address information */ +struct va_val { + u16_t ref; + u16_t addr; + u8_t uuid[16]; +} __packed; + +/* We need this so we don't overwrite app-hardcoded values in case FCB + * contains a history of changes but then has a NULL at the end. + */ +static struct { + bool valid; + struct cfg_val cfg; +} stored_cfg; + +struct prov_info { + u16_t primary_addr; + u16_t alloc_addr; +}; + +struct node_info { + u8_t addr[6]; + u8_t addr_type; + u8_t dev_uuid[16]; + u16_t oob_info; + u16_t unicast_addr; + u8_t element_num; + u16_t net_idx; + u8_t flags; + u32_t iv_index; + u8_t dev_key[16]; +} __packed; + +#define DEVICE_ROLE_BITS (BIT(BLE_MESH_NODE) | BIT(BLE_MESH_PROVISIONER)) + +static int role_set(const char *name) +{ + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags), &exist); + if (err) { + BT_ERR("Failed to load mesh device role"); + return err; + } + + if (exist == false) { +#if CONFIG_BLE_MESH_SETTINGS_BACKWARD_COMPATIBILITY + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && + !IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER)) { + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); + } +#else + return 0; +#endif + } + + return 0; +} + +static int net_set(const char *name) +{ + struct net_val net = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&net, sizeof(net), &exist); + if (err) { + BT_ERR("Failed to load node net info"); + memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key)); + bt_mesh_comp_unprovision(); + return 0; + } + + if (exist == false) { + return 0; + } + + memcpy(bt_mesh.dev_key, net.dev_key, sizeof(bt_mesh.dev_key)); + bt_mesh_comp_provision(net.primary_addr); + + BT_INFO("Restored Primary Address 0x%04x", net.primary_addr); + BT_INFO("Restored DevKey %s", bt_hex(bt_mesh.dev_key, 16)); + + return 0; +} + +static int iv_set(const char *name) +{ + struct iv_val iv = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&iv, sizeof(iv), &exist); + if (err) { + BT_ERR("Failed to load iv_index"); + bt_mesh.iv_index = 0U; + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.iv_index = iv.iv_index; + bt_mesh_atomic_set_bit_to(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS, iv.iv_update); + bt_mesh.ivu_duration = iv.iv_duration; + + BT_INFO("Restored IV Index 0x%04x (IV Update Flag %u) duration %u hours", + iv.iv_index, iv.iv_update, iv.iv_duration); + + return 0; +} + +static int seq_set(const char *name) +{ + struct seq_val seq = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&seq, sizeof(seq), &exist); + if (err) { + BT_ERR("Failed to load sequence number"); + bt_mesh.seq = 0U; + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.seq = sys_get_le24(seq.val); + +#if CONFIG_BLE_MESH_SEQ_STORE_RATE > 0 + /* Make sure we have a large enough sequence number. We + * subtract 1 so that the first transmission causes a write + * to the settings storage. + */ + bt_mesh.seq += (CONFIG_BLE_MESH_SEQ_STORE_RATE - + (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)); + bt_mesh.seq--; +#endif + + BT_INFO("Restored Sequence Number 0x%06x", bt_mesh.seq); + + return 0; +} + +static struct bt_mesh_rpl *rpl_find(u16_t src) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + if (bt_mesh.rpl[i].src == src) { + return &bt_mesh.rpl[i]; + } + } + + return NULL; +} + +static struct bt_mesh_rpl *rpl_alloc(u16_t src) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + if (bt_mesh.rpl[i].src == BLE_MESH_ADDR_UNASSIGNED) { + bt_mesh.rpl[i].src = src; + return &bt_mesh.rpl[i]; + } + } + + return NULL; +} + +static int rpl_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + struct bt_mesh_rpl *entry = NULL; + struct rpl_val rpl = {0}; + char get[16] = {'\0'}; + bool exist = false; + size_t length = 0U; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t src = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/rpl/%04x", src); + + err = bt_mesh_load_core_settings(get, (u8_t *)&rpl, sizeof(rpl), &exist); + if (err) { + BT_ERR("Failed to load RPL entry 0x%04x", src); + bt_mesh_rpl_reset(); + goto free; + } + + if (exist == false) { + continue; + } + + entry = rpl_find(src); + if (!entry) { + entry = rpl_alloc(src); + if (!entry) { + BT_ERR("%s, No space for a new RPL 0x%04x", __func__, src); + err = -ENOMEM; + goto free; + } + } + + BT_INFO("Restored RPL entry 0x%04x: seq 0x%06x, old_iv %u", src, rpl.seq, rpl.old_iv); + entry->src = src; + entry->seq = rpl.seq; + entry->old_iv = rpl.old_iv; + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static struct bt_mesh_subnet *subnet_alloc(u16_t net_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + if (bt_mesh.sub[i].net_idx == BLE_MESH_KEY_UNUSED) { + bt_mesh.sub[i].net_idx = net_idx; + return &bt_mesh.sub[i]; + } + } + + return NULL; +} + +static int net_key_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_key_val key = {0}; + char get[16] = {'\0'}; + bool exist = false; + size_t length = 0U; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t net_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/nk/%04x", net_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load NetKey 0x%03x", __func__, net_idx); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_subnet_get(net_idx); + if (!sub) { + sub = subnet_alloc(net_idx); + if (!sub) { + BT_ERR("%s, No space for a new subnet 0x%03x", __func__, net_idx); + err = -ENOMEM; + goto free; + } + } + + sub->net_idx = net_idx; + sub->kr_flag = key.kr_flag; + sub->kr_phase = key.kr_phase; + memcpy(sub->keys[0].net, &key.val[0], 16); + memcpy(sub->keys[1].net, &key.val[1], 16); + + BT_INFO("Restored NetKey Index 0x%03x", sub->net_idx); + BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int app_key_set(const char *name) +{ + struct bt_mesh_app_key *app = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_buf_simple *buf = NULL; + struct app_key_val key = {0}; + char get[16] = {'\0'}; + bool exist = false; + size_t length = 0U; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t app_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/ak/%04x", app_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load AppKey 0x%03x", __func__, app_idx); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_subnet_get(key.net_idx); + if (!sub) { + BT_ERR("%s, Failed to find subnet 0x%03x", __func__, key.net_idx); + err = -ENOENT; + goto free; + } + + app = bt_mesh_app_key_find(app_idx); + if (!app) { + app = bt_mesh_app_key_alloc(app_idx); + if (!app) { + BT_ERR("%s, No space for a new app key 0x%03x", __func__, app_idx); + err = -ENOMEM; + goto free; + } + } + + app->net_idx = key.net_idx; + app->app_idx = app_idx; + app->updated = key.updated; + memcpy(app->keys[0].val, key.val[0], 16); + memcpy(app->keys[1].val, key.val[1], 16); + bt_mesh_app_id(app->keys[0].val, &app->keys[0].id); + bt_mesh_app_id(app->keys[1].val, &app->keys[1].id); + + BT_INFO("Restored AppKey Index 0x%03x, NetKey Index 0x%03x", + app->app_idx, app->net_idx); + BT_INFO("Restored AppKey %s", bt_hex(app->keys[0].val, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int hb_pub_set(const char *name) +{ + struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get(); + struct hb_pub_val hb_val = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + if (!hb_pub) { + BT_ERR("%s, NULL cfg hb pub", __func__); + return -EINVAL; + } + + err = bt_mesh_load_core_settings(name, (u8_t *)&hb_val, sizeof(hb_val), &exist); + if (err) { + BT_ERR("Failed to load heartbeat publication"); + hb_pub->dst = BLE_MESH_ADDR_UNASSIGNED; + hb_pub->count = 0U; + hb_pub->ttl = 0U; + hb_pub->period = 0U; + hb_pub->feat = 0U; + return 0; + } + + if (exist == false) { + return 0; + } + + hb_pub->dst = hb_val.dst; + hb_pub->period = hb_val.period; + hb_pub->ttl = hb_val.ttl; + hb_pub->feat = hb_val.feat; + hb_pub->net_idx = hb_val.net_idx; + if (hb_val.indefinite) { + hb_pub->count = 0xffff; + } else { + hb_pub->count = 0U; + } + + BT_INFO("Restored Heartbeat Publication, dst 0x%04x", hb_pub->dst); + + return 0; +} + +static int cfg_set(const char *name) +{ + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + struct cfg_val val = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + if (!cfg) { + BT_ERR("%s, NULL cfg", __func__); + stored_cfg.valid = false; + return -EINVAL; + } + + err = bt_mesh_load_core_settings(name, (u8_t *)&val, sizeof(val), &exist); + if (err) { + BT_ERR("Failed to load configuration state"); + stored_cfg.valid = false; + return 0; + } + + if (exist == false) { + return 0; + } + + memcpy(&stored_cfg.cfg, &val, sizeof(val)); + stored_cfg.valid = true; + BT_INFO("Restored Configuration State"); + return 0; +} + +static int model_set_bind(bool vnd, struct bt_mesh_model *model, u16_t model_key) +{ + char name[16] = {'\0'}; + bool exist = false; + int err = 0; + int i; + + /* Start with empty array regardless of cleared or set value */ + for (i = 0; i < ARRAY_SIZE(model->keys); i++) { + model->keys[i] = BLE_MESH_KEY_UNUSED; + } + + sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); + err = bt_mesh_load_core_settings(name, (u8_t *)model->keys, sizeof(model->keys), &exist); + if (err) { + BT_ERR("Failed to load model bound keys"); + return -EIO; + } + + return 0; +} + +static int model_set_sub(bool vnd, struct bt_mesh_model *model, u16_t model_key) +{ + char name[16] = {'\0'}; + bool exist = false; + int err = 0; + int i; + + /* Start with empty array regardless of cleared or set value */ + for (i = 0; i < ARRAY_SIZE(model->groups); i++) { + model->groups[i] = BLE_MESH_ADDR_UNASSIGNED; + } + + sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); + err = bt_mesh_load_core_settings(name, (u8_t *)model->groups, sizeof(model->groups), &exist); + if (err) { + BT_ERR("Failed to load model subscriptions"); + return -EIO; + } + + return 0; +} + +static int model_set_pub(bool vnd, struct bt_mesh_model *model, u16_t model_key) +{ + struct mod_pub_val pub = {0}; + char name[16] = {'\0'}; + bool exist = false; + int err = 0; + + if (!model->pub) { + BT_INFO("Not support publication, model_id 0x%04x, cid 0x%04x", + vnd ? model->vnd.id : model->id, vnd ? model->vnd.company : 0xFFFF); + return 0; + } + + sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); + err = bt_mesh_load_core_settings(name, (u8_t *)&pub, sizeof(pub), &exist); + if (err) { + BT_ERR("Failed to load model publication"); + model->pub->addr = BLE_MESH_ADDR_UNASSIGNED; + model->pub->key = 0U; + model->pub->cred = 0U; + model->pub->ttl = 0U; + model->pub->period = 0U; + model->pub->retransmit = 0U; + model->pub->count = 0U; + return 0; + } + + if (exist == false) { + return 0; + } + + model->pub->addr = pub.addr; + model->pub->key = pub.key; + model->pub->cred = pub.cred; + model->pub->ttl = pub.ttl; + model->pub->period = pub.period; + model->pub->retransmit = pub.retransmit; + model->pub->count = 0U; + + BT_INFO("Restored Model Publication, address 0x%04x, app_idx 0x%03x", + pub.addr, pub.key); + + return 0; +} + +static int model_set(bool vnd, const char *name) +{ + struct bt_mesh_model *model = NULL; + struct net_buf_simple *buf = NULL; + u8_t elem_idx = 0U, model_idx = 0U; + size_t length = 0U; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t model_key = net_buf_simple_pull_le16(buf); + elem_idx = BLE_MESH_GET_ELEM_IDX(model_key); + model_idx = BLE_MESH_GET_MODEL_IDX(model_key); + + model = bt_mesh_model_get(vnd, elem_idx, model_idx); + if (!model) { + BT_ERR("%s model not found, elem_idx %u, model_idx %u", + vnd ? "vnd" : "sig", elem_idx, model_idx); + err = -ENOENT; + goto free; + } + + err = model_set_bind(vnd, model, model_key); + if (err) { + goto free; + } + + err = model_set_sub(vnd, model, model_key); + if (err) { + goto free; + } + + err = model_set_pub(vnd, model, model_key); + if (err) { + goto free; + } + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int sig_mod_set(const char *name) +{ + return model_set(false, name); +} + +static int vnd_mod_set(const char *name) +{ + return model_set(true, name); +} + +#if CONFIG_BLE_MESH_LABEL_COUNT > 0 +static int va_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + struct va_val va = {0}; + char get[16] = {'\0'}; + struct label *lab = NULL; + size_t length = 0U; + bool exist = false; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t index = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/va/%04x", index); + + err = bt_mesh_load_core_settings(get, (u8_t *)&va, sizeof(va), &exist); + if (err) { + BT_ERR("Failed to load virtual address 0x%04x", index); + goto free; + } + + if (exist == false) { + continue; + } + + if (va.ref == 0) { + BT_DBG("Ignore virtual address %s with ref = 0", get); + goto free; + } + + lab = get_label(index); + if (lab == NULL) { + BT_WARN("%s, Out of labels buffers", __func__); + err = -ENOBUFS; + goto free; + } + + memcpy(lab->uuid, va.uuid, 16); + lab->addr = va.addr; + lab->ref = va.ref; + + BT_INFO("Restored Virtual Address 0x%04x, ref 0x%04x", index, lab->ref); + } + +free: + bt_mesh_free_buf(buf); + return err; +} +#endif + +#if CONFIG_BLE_MESH_PROVISIONER +static int p_prov_set(const char *name) +{ + struct prov_info val = {0}; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&val, sizeof(val), &exist); + if (err) { + BT_ERR("Failed to load next address allocation"); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh_provisioner_restore_prov_info(val.primary_addr, val.alloc_addr); + + BT_INFO("Restored Primary Address 0x%04x, next address allocation 0x%04x", + val.primary_addr, val.alloc_addr); + + return 0; +} + +static int p_net_idx_set(const char *name) +{ + u16_t net_idx = 0U; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&net_idx, sizeof(net_idx), &exist); + if (err) { + BT_ERR("Failed to load next net_idx allocation"); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.p_net_idx_next = net_idx; + + BT_INFO("Restored next NetKey Index allocation 0x%03x", bt_mesh.p_net_idx_next); + + return 0; +} + +static int p_app_idx_set(const char *name) +{ + u16_t app_idx = 0U; + bool exist = false; + int err = 0; + + BT_DBG("%s", __func__); + + err = bt_mesh_load_core_settings(name, (u8_t *)&app_idx, sizeof(app_idx), &exist); + if (err) { + BT_ERR("Failed to load next app_idx allocation"); + return 0; + } + + if (exist == false) { + return 0; + } + + bt_mesh.p_app_idx_next = app_idx; + + BT_INFO("Restored next AppKey Index allocation 0x%03x", bt_mesh.p_app_idx_next); + + return 0; +} + +static struct bt_mesh_subnet *p_subnet_alloc(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + if (bt_mesh.p_sub[i] == NULL) { + bt_mesh.p_sub[i] = bt_mesh_calloc(sizeof(struct bt_mesh_subnet)); + if (!bt_mesh.p_sub[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + return NULL; + } + + return bt_mesh.p_sub[i]; + } + } + + return NULL; +} + +static struct bt_mesh_app_key *p_appkey_alloc(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) { + if (bt_mesh.p_app_keys[i] == NULL) { + bt_mesh.p_app_keys[i] = bt_mesh_calloc(sizeof(struct bt_mesh_app_key)); + if (!bt_mesh.p_app_keys[i]) { + BT_ERR("%s, Failed to allocate memory", __func__); + return NULL; + } + + return bt_mesh.p_app_keys[i]; + } + } + + return NULL; +} + +static int p_net_key_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_key_val key = {0}; + char get[16] = {'\0'}; + size_t length = 0U; + bool exist = false; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t net_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/pnk/%04x", net_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load NetKey 0x%03x", __func__, net_idx); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_provisioner_subnet_get(net_idx); + if (!sub) { + sub = p_subnet_alloc(); + if (!sub) { + BT_ERR("%s, No space for a new subnet 0x%03x", __func__, net_idx); + err = -ENOMEM; + goto free; + } + } + + sub->net_idx = net_idx; + sub->kr_flag = key.kr_flag; + sub->kr_phase = key.kr_phase; + memcpy(sub->keys[0].net, &key.val[0], 16); + memcpy(sub->keys[1].net, &key.val[1], 16); + + BT_INFO("Restored NetKey Index 0x%03x", sub->net_idx); + BT_INFO("Restored NetKey %s", bt_hex(sub->keys[0].net, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int p_app_key_set(const char *name) +{ + struct bt_mesh_app_key *app = NULL; + struct bt_mesh_subnet *sub = NULL; + struct net_buf_simple *buf = NULL; + struct app_key_val key = {0}; + char get[16] = {'\0'}; + size_t length = 0U; + bool exist = false; + int err = 0; + int i; + + BT_DBG("%s", __func__); + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t app_idx = net_buf_simple_pull_le16(buf); + sprintf(get, "mesh/pak/%04x", app_idx); + + err = bt_mesh_load_core_settings(get, (u8_t *)&key, sizeof(key), &exist); + if (err) { + BT_ERR("%s, Failed to load AppKey 0x%03x", __func__, app_idx); + goto free; + } + + if (exist == false) { + continue; + } + + sub = bt_mesh_provisioner_subnet_get(key.net_idx); + if (!sub) { + BT_ERR("%s, Failed to find subnet 0x%03x", __func__, key.net_idx); + err = -ENOENT; + goto free; + } + + app = bt_mesh_provisioner_app_key_find(app_idx); + if (!app) { + app = p_appkey_alloc(); + if (!app) { + BT_ERR("%s, No space for a new app key 0x%03x", __func__, app_idx); + err = -ENOMEM; + goto free; + } + } + + app->net_idx = key.net_idx; + app->app_idx = app_idx; + app->updated = key.updated; + memcpy(app->keys[0].val, key.val[0], 16); + memcpy(app->keys[1].val, key.val[1], 16); + bt_mesh_app_id(app->keys[0].val, &app->keys[0].id); + bt_mesh_app_id(app->keys[1].val, &app->keys[1].id); + + BT_INFO("Restored AppKey Index 0x%03x, NetKey Index 0x%03x", + app->app_idx, app->net_idx); + BT_INFO("Restored AppKey %s", bt_hex(app->keys[0].val, 16)); + } + +free: + bt_mesh_free_buf(buf); + return err; +} + +static int node_info_set(u16_t addr, bool *exist) +{ + struct bt_mesh_node node = {0}; + struct node_info info = {0}; + char get[16] = {'\0'}; + int err = 0; + + sprintf(get, "mesh/pn/%04x/i", addr); + err = bt_mesh_load_core_settings(get, (u8_t *)&info, sizeof(info), exist); + if (err) { + BT_ERR("Failed to load node 0x%04x info", addr); + return -EIO; + } + + if (*exist == false) { + return 0; + } + + memcpy(node.addr, info.addr, BLE_MESH_ADDR_LEN); + node.addr_type = info.addr_type; + memcpy(node.dev_uuid, info.dev_uuid, 16); + node.oob_info = info.oob_info; + node.unicast_addr = info.unicast_addr; + node.element_num = info.element_num; + node.net_idx = info.net_idx; + node.flags = info.flags; + node.iv_index = info.iv_index; + memcpy(node.dev_key, info.dev_key, 16); + + err = bt_mesh_provisioner_restore_node_info(&node); + if (err) { + BT_ERR("Failed to restore node 0x%04x info", addr); + return -EIO; + } + + BT_INFO("Restored Node 0x%04x, UUID %s", addr, bt_hex(node.dev_uuid, 16)); + + return 0; +} + +static int node_name_set(u16_t addr) +{ + char name[BLE_MESH_NODE_NAME_SIZE + 1] = {0}; + char get[16] = {'\0'}; + bool exist = false; + int err = 0; + + sprintf(get, "mesh/pn/%04x/n", addr); + err = bt_mesh_load_core_settings(get, (u8_t *)name, BLE_MESH_NODE_NAME_SIZE, &exist); + if (err) { + BT_ERR("Failed to load node 0x%04x name", addr); + return -EIO; + } + + if (exist == false) { + return 0; + } + + err = bt_mesh_provisioner_restore_node_name(addr, name); + if (err) { + BT_ERR("Failed to restore node 0x%04x name", addr); + return -EIO; + } + + BT_INFO("Restored Node 0x%04x, Name %s", addr, name); + + return 0; +} + +static int node_comp_data_set(u16_t addr) +{ + struct net_buf_simple *buf = NULL; + char get[16] = {'\0'}; + int err = 0; + + sprintf(get, "mesh/pn/%04x/c", addr); + buf = bt_mesh_get_core_settings_item(get); + if (!buf) { + return 0; + } + + err = bt_mesh_provisioner_restore_node_comp_data(addr, buf->data, buf->len); + if (err) { + BT_ERR("Failed to restore node 0x%04x comp data", addr); + } else { + BT_INFO("Restored Node 0x%04x, Composition Data %s", addr, bt_hex(buf->data, buf->len)); + } + + bt_mesh_free_buf(buf); + return err; +} + +static int p_node_set(const char *name) +{ + struct net_buf_simple *buf = NULL; + bool exist = false; + size_t length = 0U; + int i; + + buf = bt_mesh_get_core_settings_item(name); + if (!buf) { + return 0; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t addr = net_buf_simple_pull_le16(buf); + if (!BLE_MESH_ADDR_IS_UNICAST(addr)) { + BT_ERR("%s, 0x%04x is not a unicast address", __func__, addr); + continue; + } + + if (node_info_set(addr, &exist)) { + continue; + } + + if (exist == false) { + continue; + } + + if (node_name_set(addr)) { + continue; + } + + if (node_comp_data_set(addr)) { + continue; + } + } + + bt_mesh_free_buf(buf); + return 0; +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +const struct bt_mesh_setting { + const char *name; + int (*func)(const char *name); +} settings[] = { + { "mesh/role", role_set }, + { "mesh/net", net_set }, + { "mesh/iv", iv_set }, + { "mesh/seq", seq_set }, + { "mesh/rpl", rpl_set }, + { "mesh/netkey", net_key_set }, + { "mesh/appkey", app_key_set }, + { "mesh/hb_pub", hb_pub_set }, + { "mesh/cfg", cfg_set }, + { "mesh/sig", sig_mod_set }, + { "mesh/vnd", vnd_mod_set }, +#if CONFIG_BLE_MESH_LABEL_COUNT > 0 + { "mesh/vaddr", va_set }, +#endif +#if CONFIG_BLE_MESH_PROVISIONER + { "mesh/p_prov", p_prov_set }, + { "mesh/p_netidx", p_net_idx_set }, + { "mesh/p_appidx", p_app_idx_set }, + { "mesh/p_netkey", p_net_key_set }, + { "mesh/p_appkey", p_app_key_set }, + { "mesh/p_node", p_node_set }, +#endif +}; + +/** + * For Provisioner, the load operation needs the following actions: + * net_set: Not needed + * iv_set: Need, although Provisioner will do some initialization of IV Index + * during startup, but we need to restore the last IV Index status + * seq_set: Need, restore the previous sequence number + * rpl_set: Need, restore the previous Replay Protection List + * net_key_set: Need, restore the previous network keys + * app_key_set: Need, restore the previous application keys + * hb_pub_set: Not needed currently + * cfg_set: Not needed currently + * sig_mod_set: Need, restore SIG models related info (app, sub, pub) + * vnd_mod_set: Need, restore vendor models related info (app, sub, pub) + */ +int settings_core_load(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(settings); i++) { + if ((!strcmp(settings[i].name, "mesh/net") || + !strcmp(settings[i].name, "mesh/netkey") || + !strcmp(settings[i].name, "mesh/appkey") || + !strcmp(settings[i].name, "mesh/hb_pub") || + !strcmp(settings[i].name, "mesh/cfg")) && + (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioner())) { + BT_DBG("Not restoring %s for Provisioner", settings[i].name); + continue; + } + + if ((!strcmp(settings[i].name, "mesh/p_prov") || + !strcmp(settings[i].name, "mesh/p_netidx") || + !strcmp(settings[i].name, "mesh/p_appidx") || + !strcmp(settings[i].name, "mesh/p_netkey") || + !strcmp(settings[i].name, "mesh/p_appkey") || + !strcmp(settings[i].name, "mesh/p_node")) && + (!IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) || bt_mesh_is_node())) { + BT_DBG("Not restoring %s for Node", settings[i].name); + continue; + } + + settings[i].func(settings[i].name); + + if (!strcmp(settings[i].name, "mesh/role")) { + u8_t role = bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS; + switch (role) { + case 0U: + BT_INFO("Mesh device just starts up, no restore"); + return 0; + case BIT(BLE_MESH_NODE): + BT_INFO("Restored mesh device role: Node"); + break; + case BIT(BLE_MESH_PROVISIONER): + BT_INFO("Restored mesh device role: Provisioner"); + break; + default: + BT_ERR("Restored mesh device role: Unknown"); + return 0; + } + } + } + + return 0; +} + +static int subnet_init(struct bt_mesh_subnet *sub) +{ + int err = 0; + + err = bt_mesh_net_keys_create(&sub->keys[0], sub->keys[0].net); + if (err) { + BT_ERR("%s, Unable to generate keys for subnet", __func__); + return -EIO; + } + + if (sub->kr_phase != BLE_MESH_KR_NORMAL) { + err = bt_mesh_net_keys_create(&sub->keys[1], sub->keys[1].net); + if (err) { + BT_ERR("%s, Unable to generate keys for subnet", __func__); + (void)memset(&sub->keys[0], 0, sizeof(sub->keys[0])); + return -EIO; + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) { + sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED; + } else { + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + } + + /* Make sure we have valid beacon data to be sent */ + bt_mesh_net_beacon_update(sub); + + return 0; +} + +static void commit_model(struct bt_mesh_model *model, struct bt_mesh_elem *elem, + bool vnd, bool primary, void *user_data) +{ + if (model->pub && model->pub->update && + model->pub->addr != BLE_MESH_ADDR_UNASSIGNED) { + s32_t ms = bt_mesh_model_pub_period_get(model); + if (ms) { + BT_DBG("Starting publish timer (period %u ms)", ms); + k_delayed_work_submit(&model->pub->timer, ms); + } + } +} + +int settings_core_commit(void) +{ + struct bt_mesh_subnet *sub = NULL; + int err = 0; + int i; + +#if defined(CONFIG_BLE_MESH_NODE) + if (bt_mesh_is_node()) { + BT_INFO("sub[0].net_idx 0x%03x", bt_mesh.sub[0].net_idx); + + if (bt_mesh.sub[0].net_idx == BLE_MESH_KEY_UNUSED) { + /* Nothing to do since we're not yet provisioned */ + return 0; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT)) { + bt_mesh_proxy_prov_disable(true); + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) { + sub = &bt_mesh.sub[i]; + + if (sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + err = subnet_init(sub); + if (err) { + BT_ERR("%s, Failed to init subnet 0x%03x", __func__, sub->net_idx); + } + } + } +#endif /* CONFIG_BLE_MESH_NODE */ + +#if defined(CONFIG_BLE_MESH_PROVISIONER) + if (bt_mesh_is_provisioner()) { + if (bt_mesh.p_sub[0] == NULL || + bt_mesh.p_sub[0]->net_idx == BLE_MESH_KEY_UNUSED) { + return 0; + } + + BT_INFO("p_sub[0]->net_idx 0x%03x", bt_mesh.p_sub[0]->net_idx); + + bt_mesh_comp_provision(bt_mesh_provisioner_get_primary_elem_addr()); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.p_sub); i++) { + sub = bt_mesh.p_sub[i]; + + if (sub == NULL || sub->net_idx == BLE_MESH_KEY_UNUSED) { + continue; + } + + err = subnet_init(sub); + if (err) { + BT_ERR("%s, Failed to init subnet 0x%03x", __func__, sub->net_idx); + } + sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED; + } + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + + if (bt_mesh.ivu_duration < BLE_MESH_IVU_MIN_HOURS) { + k_delayed_work_submit(&bt_mesh.ivu_timer, BLE_MESH_IVU_TIMEOUT); + } + + bt_mesh_model_foreach(commit_model, NULL); + +#if defined(CONFIG_BLE_MESH_NODE) + if (bt_mesh_is_node()) { + struct bt_mesh_hb_pub *hb_pub = NULL; + struct bt_mesh_cfg_srv *cfg = NULL; + + hb_pub = bt_mesh_hb_pub_get(); + if (hb_pub && hb_pub->dst != BLE_MESH_ADDR_UNASSIGNED && + hb_pub->count && hb_pub->period) { + BT_DBG("Starting heartbeat publication"); + k_work_submit(&hb_pub->timer.work); + } + + cfg = bt_mesh_cfg_get(); + if (cfg && stored_cfg.valid) { + cfg->net_transmit = stored_cfg.cfg.net_transmit; + cfg->relay = stored_cfg.cfg.relay; + cfg->relay_retransmit = stored_cfg.cfg.relay_retransmit; + cfg->beacon = stored_cfg.cfg.beacon; + cfg->gatt_proxy = stored_cfg.cfg.gatt_proxy; + cfg->frnd = stored_cfg.cfg.frnd; + cfg->default_ttl = stored_cfg.cfg.default_ttl; + } + + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID); + bt_mesh_net_start(); + } +#endif /* CONFIG_BLE_MESH_NODE */ + +#if defined(CONFIG_BLE_MESH_PROVISIONER) + if (bt_mesh_is_provisioner()) { + bt_mesh_provisioner_net_start(BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT); + } +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + + return 0; +} + +/* Pending flags that use K_NO_WAIT as the storage timeout */ +#define NO_WAIT_PENDING_BITS (BIT(BLE_MESH_NET_PENDING) | \ + BIT(BLE_MESH_IV_PENDING) | \ + BIT(BLE_MESH_SEQ_PENDING)) + +/* Pending flags that use CONFIG_BLE_MESH_STORE_TIMEOUT */ +#define GENERIC_PENDING_BITS (BIT(BLE_MESH_KEYS_PENDING) | \ + BIT(BLE_MESH_HB_PUB_PENDING) | \ + BIT(BLE_MESH_CFG_PENDING) | \ + BIT(BLE_MESH_MOD_PENDING)) + +static void schedule_store(int flag) +{ + s32_t timeout = 0, remaining = 0; + + bt_mesh_atomic_set_bit(bt_mesh.flags, flag); + + if (bt_mesh_atomic_get(bt_mesh.flags) & NO_WAIT_PENDING_BITS) { + timeout = K_NO_WAIT; + } else if (bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING) && + (!(bt_mesh_atomic_get(bt_mesh.flags) & GENERIC_PENDING_BITS) || + (CONFIG_BLE_MESH_RPL_STORE_TIMEOUT < CONFIG_BLE_MESH_STORE_TIMEOUT))) { + timeout = K_SECONDS(CONFIG_BLE_MESH_RPL_STORE_TIMEOUT); + } else { + timeout = K_SECONDS(CONFIG_BLE_MESH_STORE_TIMEOUT); + } + + remaining = k_delayed_work_remaining_get(&pending_store); + if (remaining && remaining < timeout) { + BT_DBG("Not rescheduling due to existing earlier deadline"); + return; + } + + BT_INFO("Waiting %d seconds", timeout / MSEC_PER_SEC); + + if (timeout) { + k_delayed_work_submit(&pending_store, timeout); + } else { + k_work_submit(&pending_store.work); + } +} + +static void clear_iv(void) +{ + BT_DBG("Clearing IV"); + bt_mesh_save_core_settings("mesh/iv", NULL, 0); +} + +static void clear_net(void) +{ + BT_DBG("Clearing Network"); + bt_mesh_save_core_settings("mesh/net", NULL, 0); +} + +static void store_pending_net(void) +{ + struct net_val net = {0}; + + BT_DBG("Primary address 0x%04x DevKey %s", bt_mesh_primary_addr(), + bt_hex(bt_mesh.dev_key, 16)); + + net.primary_addr = bt_mesh_primary_addr(); + memcpy(net.dev_key, bt_mesh.dev_key, 16); + + bt_mesh_save_core_settings("mesh/net", (const u8_t *)&net, sizeof(net)); +} + +void bt_mesh_store_role(void) +{ + BT_DBG("Store, device role %lu", bt_mesh_atomic_get(bt_mesh.flags) & DEVICE_ROLE_BITS); + + bt_mesh_save_core_settings("mesh/role", (const u8_t *)bt_mesh.flags, sizeof(bt_mesh.flags)); +} + +void bt_mesh_store_net(void) +{ + schedule_store(BLE_MESH_NET_PENDING); +} + +static void store_pending_iv(void) +{ + struct iv_val iv = {0}; + + iv.iv_index = bt_mesh.iv_index; + iv.iv_update = bt_mesh_atomic_test_bit(bt_mesh.flags, BLE_MESH_IVU_IN_PROGRESS); + iv.iv_duration = bt_mesh.ivu_duration; + + bt_mesh_save_core_settings("mesh/iv", (const u8_t *)&iv, sizeof(iv)); +} + +void bt_mesh_store_iv(bool only_duration) +{ + schedule_store(BLE_MESH_IV_PENDING); + + if (!only_duration) { + /* Always update Seq whenever IV changes */ + schedule_store(BLE_MESH_SEQ_PENDING); + } +} + +void bt_mesh_clear_iv(void) +{ + clear_iv(); +} + +static void store_pending_seq(void) +{ + struct seq_val seq = {0}; + + sys_put_le24(bt_mesh.seq, seq.val); + + bt_mesh_save_core_settings("mesh/seq", (const u8_t *)&seq, sizeof(seq)); +} + +void bt_mesh_store_seq(void) +{ + if (CONFIG_BLE_MESH_SEQ_STORE_RATE && + (bt_mesh.seq % CONFIG_BLE_MESH_SEQ_STORE_RATE)) { + return; + } + + schedule_store(BLE_MESH_SEQ_PENDING); +} + +void bt_mesh_clear_seq(void) +{ + bt_mesh_save_core_settings("mesh/seq", NULL, 0); +} + +static void store_rpl(struct bt_mesh_rpl *entry) +{ + struct rpl_val rpl = {0}; + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("src 0x%04x seq 0x%06x old_iv %u", entry->src, entry->seq, entry->old_iv); + + rpl.seq = entry->seq; + rpl.old_iv = entry->old_iv; + + sprintf(name, "mesh/rpl/%04x", entry->src); + err = bt_mesh_save_core_settings(name, (const u8_t *)&rpl, sizeof(rpl)); + if (err) { + BT_ERR("Failed to store RPL entry 0x%04x", entry->src); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/rpl", entry->src); + if (err) { + BT_ERR("Failed to add 0x%04x to mesh/rpl", entry->src); + } + + return; +} + +static void clear_rpl(void) +{ + struct net_buf_simple *buf = NULL; + char name[16] = {'\0'}; + size_t length = 0U; + u16_t src = 0U; + int i; + + BT_DBG("%s", __func__); + + bt_mesh_rpl_clear(); + + buf = bt_mesh_get_core_settings_item("mesh/rpl"); + if (!buf) { + bt_mesh_save_core_settings("mesh/rpl", NULL, 0); + return; + } + + length = buf->len; + + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + src = net_buf_simple_pull_le16(buf); + sprintf(name, "mesh/rpl/%04x", src); + bt_mesh_save_core_settings(name, NULL, 0); + } + + bt_mesh_save_core_settings("mesh/rpl", NULL, 0); + + bt_mesh_free_buf(buf); + return; +} + +static void store_pending_rpl(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + + if (rpl->store) { + rpl->store = false; + store_rpl(rpl); + } + } +} + +static void store_pending_hb_pub(void) +{ + struct bt_mesh_hb_pub *hb_pub = bt_mesh_hb_pub_get(); + struct hb_pub_val val = {0}; + + if (!hb_pub) { + BT_WARN("NULL heartbeat publication"); + return; + } + + val.indefinite = (hb_pub->count = 0xffff); + val.dst = hb_pub->dst; + val.period = hb_pub->period; + val.ttl = hb_pub->ttl; + val.feat = hb_pub->feat; + val.net_idx = hb_pub->net_idx; + + bt_mesh_save_core_settings("mesh/hb_pub", (const u8_t *)&val, sizeof(val)); +} + +static void store_pending_cfg(void) +{ + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + struct cfg_val val = {0}; + + if (!cfg) { + BT_WARN("NULL configuration state"); + return; + } + + val.net_transmit = cfg->net_transmit; + val.relay = cfg->relay; + val.relay_retransmit = cfg->relay_retransmit; + val.beacon = cfg->beacon; + val.gatt_proxy = cfg->gatt_proxy; + val.frnd = cfg->frnd; + val.default_ttl = cfg->default_ttl; + + bt_mesh_save_core_settings("mesh/cfg", (const u8_t *)&val, sizeof(val)); +} + +static void clear_cfg(void) +{ + BT_DBG("Clearing configuration"); + bt_mesh_save_core_settings("mesh/cfg", NULL, 0); +} + +static void clear_app_key(u16_t app_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("AppKeyIndex 0x%03x", app_idx); + + sprintf(name, "mesh/ak/%04x", app_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/appkey", app_idx); + if (err) { + BT_ERR("%s, Failed to remove 0x%03x from mesh/appkey", __func__, app_idx); + } + + return; +} + +static void clear_net_key(u16_t net_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("NetKeyIndex 0x%03x", net_idx); + + sprintf(name, "mesh/nk/%04x", net_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/netkey", net_idx); + if (err) { + BT_ERR("%s, Failed to remove 0x%03x from mesh/netkey", __func__, net_idx); + } + + return; +} + +static void store_net_key(struct bt_mesh_subnet *sub) +{ + struct net_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx, + bt_hex(sub->keys[0].net, 16)); + + memcpy(&key.val[0], sub->keys[0].net, 16); + memcpy(&key.val[1], sub->keys[1].net, 16); + key.kr_flag = sub->kr_flag; + key.kr_phase = sub->kr_phase; + + sprintf(name, "mesh/nk/%04x", sub->net_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to store NetKey 0x%03x", __func__, sub->net_idx); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/netkey", sub->net_idx); + if (err) { + BT_ERR("Failed to add 0x%03x to mesh/netkey", sub->net_idx); + } + + return; +} + +static void store_app_key(struct bt_mesh_app_key *app) +{ + struct app_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + key.net_idx = app->net_idx; + key.updated = app->updated; + memcpy(key.val[0], app->keys[0].val, 16); + memcpy(key.val[1], app->keys[1].val, 16); + + sprintf(name, "mesh/ak/%04x", app->app_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to store AppKey 0x%03x", __func__, app->app_idx); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/appkey", app->app_idx); + if (err) { + BT_ERR("Failed to add 0x%03x to mesh/appkey", app->app_idx); + } + + return; +} + +static void store_pending_keys(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(key_updates); i++) { + struct key_update *update = &key_updates[i]; + + if (!update->valid) { + continue; + } + + if (update->clear) { + if (update->app_key) { + clear_app_key(update->key_idx); + } else { + clear_net_key(update->key_idx); + } + } else { + if (update->app_key) { + struct bt_mesh_app_key *key = NULL; + key = bt_mesh_app_key_find(update->key_idx); + if (key) { + store_app_key(key); + } else { + BT_WARN("AppKeyIndex 0x%03x not found", update->key_idx); + } + } else { + struct bt_mesh_subnet *sub = NULL; + sub = bt_mesh_subnet_get(update->key_idx); + if (sub) { + store_net_key(sub); + } else { + BT_WARN("NetKeyIndex 0x%03x not found", update->key_idx); + } + } + } + + update->valid = 0U; + } +} + +static void store_pending_mod_bind(struct bt_mesh_model *model, bool vnd) +{ + char name[16] = {'\0'}; + u16_t model_key = 0U; + int err = 0; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + + sprintf(name, "mesh/%s/%04x/b", vnd ? "v" : "s", model_key); + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + bt_mesh_save_core_settings(name, NULL, 0); + return; + } + + err = bt_mesh_save_core_settings(name, (const u8_t *)model->keys, sizeof(model->keys)); + if (err) { + BT_ERR("Failed to store %s", name); + return; + } + + err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); + if (err) { + BT_ERR("Failed to add bound key to %s, model_key 0x%04x", + vnd ? "mesh/vnd" : "mesh/sig", model_key); + } + + return; +} + +static void store_pending_mod_sub(struct bt_mesh_model *model, bool vnd) +{ + char name[16] = {'\0'}; + u16_t model_key = 0U; + int err = 0; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + + sprintf(name, "mesh/%s/%04x/s", vnd ? "v" : "s", model_key); + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + bt_mesh_save_core_settings(name, NULL, 0); + return; + } + + err = bt_mesh_save_core_settings(name, (const u8_t *)model->groups, sizeof(model->groups)); + if (err) { + BT_ERR("Failed to store %s", name); + return; + } + + err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); + if (err) { + BT_ERR("Failed to add subscription to %s, model_key 0x%04x", + vnd ? "mesh/vnd" : "mesh/sig", model_key); + } + + return; +} + +static void store_pending_mod_pub(struct bt_mesh_model *model, bool vnd) +{ + struct mod_pub_val pub = {0}; + char name[16] = {'\0'}; + u16_t model_key = 0U; + int err = 0; + + if (!model->pub) { + BT_WARN("Model has no publication support"); + return; + } + + pub.addr = model->pub->addr; + pub.key = model->pub->key; + pub.ttl = model->pub->ttl; + pub.retransmit = model->pub->retransmit; + pub.period = model->pub->period; + pub.period_div = model->pub->period_div; + pub.cred = model->pub->cred; + + model_key = BLE_MESH_GET_MODEL_KEY(model->elem_idx, model->model_idx); + + sprintf(name, "mesh/%s/%04x/p", vnd ? "v" : "s", model_key); + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + bt_mesh_save_core_settings(name, NULL, 0); + return; + } + + err = bt_mesh_save_core_settings(name, (const u8_t *)&pub, sizeof(pub)); + if (err) { + BT_ERR("Failed to store %s", name); + return; + } + + err = bt_mesh_add_core_settings_item(vnd ? "mesh/vnd" : "mesh/sig", model_key); + if (err) { + BT_ERR("Failed to add publication to %s, model_key 0x%04x", + vnd ? "mesh/vnd" : "mesh/sig", model_key); + } + + return; +} + +static void store_pending_mod(struct bt_mesh_model *model, + struct bt_mesh_elem *elem, bool vnd, + bool primary, void *user_data) +{ + if (!model->flags) { + return; + } + + if (model->flags & BLE_MESH_MOD_BIND_PENDING) { + model->flags &= ~BLE_MESH_MOD_BIND_PENDING; + store_pending_mod_bind(model, vnd); + } + + if (model->flags & BLE_MESH_MOD_SUB_PENDING) { + model->flags &= ~BLE_MESH_MOD_SUB_PENDING; + store_pending_mod_sub(model, vnd); + } + + if (model->flags & BLE_MESH_MOD_PUB_PENDING) { + model->flags &= ~BLE_MESH_MOD_PUB_PENDING; + store_pending_mod_pub(model, vnd); + } +} + +#define IS_VA_DEL(_label) ((_label)->ref == 0) +static void store_pending_va(void) +{ + struct va_val va = {0}; + char name[16] = {'\0'}; + struct label *lab = NULL; + u16_t i = 0U; + int err = 0; + + for (i = 0U; (lab = get_label(i)) != NULL; i++) { + if (!bt_mesh_atomic_test_and_clear_bit(lab->flags, + BLE_MESH_VA_CHANGED)) { + continue; + } + + sprintf(name, "mesh/va/%04x", i); + + if (IS_VA_DEL(lab)) { + err = bt_mesh_save_core_settings(name, NULL, 0); + } else { + va.ref = lab->ref; + va.addr = lab->addr; + memcpy(va.uuid, lab->uuid, 16); + err = bt_mesh_save_core_settings(name, (const u8_t *)&va, sizeof(va)); + } + if (err) { + BT_ERR("Failed to %s virtual address %s", + IS_VA_DEL(lab) ? "delete" : "store", name); + return; + } + + if (IS_VA_DEL(lab)) { + err = bt_mesh_remove_core_settings_item("mesh/vaddr", i); + } else { + err = bt_mesh_add_core_settings_item("mesh/vaddr", i); + } + if (err) { + BT_ERR("Failed to %s 0x%04x in mesh/vaddr", + IS_VA_DEL(lab) ? "delete" : "store", i); + return; + } + + BT_DBG("%s virtual address 0x%04x", IS_VA_DEL(lab) ? "Delete" : "Store", i); + } +} + +static void store_pending(struct k_work *work) +{ + BT_DBG("%s", __func__); + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_RPL_PENDING)) { + if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { + store_pending_rpl(); + } else { + clear_rpl(); + } + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_KEYS_PENDING)) { + store_pending_keys(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_NET_PENDING)) { + if (bt_mesh_is_provisioned()) { + store_pending_net(); + } else { + clear_net(); + } + } + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IV_PENDING)) { + if (!IS_ENABLED(CONFIG_BLE_MESH_NODE) || bt_mesh_is_provisioned()) { + store_pending_iv(); + } else { + clear_iv(); + } + } + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_SEQ_PENDING)) { + store_pending_seq(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_HB_PUB_PENDING)) { + store_pending_hb_pub(); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_CFG_PENDING)) { + if (bt_mesh_is_provisioned()) { + store_pending_cfg(); + } else { + clear_cfg(); + } + } + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_MOD_PENDING)) { + bt_mesh_model_foreach(store_pending_mod, NULL); + if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_node() && + !bt_mesh_is_provisioned()) { + bt_mesh_save_core_settings("mesh/sig", NULL, 0); + bt_mesh_save_core_settings("mesh/vnd", NULL, 0); + } + } + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_VA_PENDING)) { + store_pending_va(); + } +} + +void bt_mesh_store_rpl(struct bt_mesh_rpl *entry) +{ + entry->store = true; + schedule_store(BLE_MESH_RPL_PENDING); +} + +static struct key_update *key_update_find(bool app_key, u16_t key_idx, + struct key_update **free_slot) +{ + struct key_update *match = NULL; + int i; + + *free_slot = NULL; + + for (i = 0; i < ARRAY_SIZE(key_updates); i++) { + struct key_update *update = &key_updates[i]; + + if (!update->valid) { + *free_slot = update; + continue; + } + + if (update->app_key != app_key) { + continue; + } + + if (update->key_idx == key_idx) { + match = update; + } + } + + return match; +} + +void bt_mesh_store_subnet(struct bt_mesh_subnet *sub) +{ + struct key_update *free_slot = NULL; + struct key_update *update = NULL; + + BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); + + update = key_update_find(false, sub->net_idx, &free_slot); + if (update) { + update->clear = 0U; + schedule_store(BLE_MESH_KEYS_PENDING); + return; + } + + if (!free_slot) { + store_net_key(sub); + return; + } + + free_slot->valid = 1U; + free_slot->key_idx = sub->net_idx; + free_slot->app_key = 0U; + free_slot->clear = 0U; + + schedule_store(BLE_MESH_KEYS_PENDING); +} + +void bt_mesh_store_app_key(struct bt_mesh_app_key *key) +{ + struct key_update *free_slot = NULL; + struct key_update *update = NULL; + + BT_DBG("AppKeyIndex 0x%03x", key->app_idx); + + update = key_update_find(true, key->app_idx, &free_slot); + if (update) { + update->clear = 0U; + schedule_store(BLE_MESH_KEYS_PENDING); + return; + } + + if (!free_slot) { + store_app_key(key); + return; + } + + free_slot->valid = 1U; + free_slot->key_idx = key->app_idx; + free_slot->app_key = 1U; + free_slot->clear = 0U; + + schedule_store(BLE_MESH_KEYS_PENDING); +} + +void bt_mesh_store_hb_pub(void) +{ + schedule_store(BLE_MESH_HB_PUB_PENDING); +} + +void bt_mesh_store_cfg(void) +{ + schedule_store(BLE_MESH_CFG_PENDING); +} + +void bt_mesh_clear_role(void) +{ + BT_DBG("Clear device role"); + bt_mesh_save_core_settings("mesh/role", NULL, 0); +} + +void bt_mesh_clear_net(void) +{ + schedule_store(BLE_MESH_NET_PENDING); + schedule_store(BLE_MESH_IV_PENDING); + schedule_store(BLE_MESH_CFG_PENDING); +} + +void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub) +{ + struct key_update *free_slot = NULL; + struct key_update *update = NULL; + + BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); + + update = key_update_find(false, sub->net_idx, &free_slot); + if (update) { + update->clear = 1U; + schedule_store(BLE_MESH_KEYS_PENDING); + return; + } + + if (!free_slot) { + clear_net_key(sub->net_idx); + return; + } + + free_slot->valid = 1U; + free_slot->key_idx = sub->net_idx; + free_slot->app_key = 0U; + free_slot->clear = 1U; + + schedule_store(BLE_MESH_KEYS_PENDING); +} + +void bt_mesh_clear_app_key(struct bt_mesh_app_key *key) +{ + struct key_update *free_slot = NULL; + struct key_update *update = NULL; + + BT_DBG("AppKeyIndex 0x%03x", key->app_idx); + + update = key_update_find(true, key->app_idx, &free_slot); + if (update) { + update->clear = 1U; + schedule_store(BLE_MESH_KEYS_PENDING); + return; + } + + if (!free_slot) { + clear_app_key(key->app_idx); + return; + } + + free_slot->valid = 1U; + free_slot->key_idx = key->app_idx; + free_slot->app_key = 1U; + free_slot->clear = 1U; + + schedule_store(BLE_MESH_KEYS_PENDING); +} + +void bt_mesh_clear_rpl(void) +{ + schedule_store(BLE_MESH_RPL_PENDING); +} + +void bt_mesh_store_mod_bind(struct bt_mesh_model *model) +{ + model->flags |= BLE_MESH_MOD_BIND_PENDING; + schedule_store(BLE_MESH_MOD_PENDING); +} + +void bt_mesh_store_mod_sub(struct bt_mesh_model *model) +{ + model->flags |= BLE_MESH_MOD_SUB_PENDING; + schedule_store(BLE_MESH_MOD_PENDING); +} + +void bt_mesh_store_mod_pub(struct bt_mesh_model *model) +{ + model->flags |= BLE_MESH_MOD_PUB_PENDING; + schedule_store(BLE_MESH_MOD_PENDING); +} + +void bt_mesh_store_label(void) +{ + schedule_store(BLE_MESH_VA_PENDING); +} + +#if CONFIG_BLE_MESH_PROVISIONER +/** + * key: "mesh/p_prov" -> write/read to set/get prov_ctx.curr_addr + * key: "mesh/p_netidx" -> write/read to set/get bt_mesh.p_net_idx_next + * key: "mesh/p_appidx" -> write/read to set/get bt_mesh.p_app_idx_next + * key: "mesh/p_netkey" -> write/read to set/get all Provisioner NetKey Index + * key: "mesh/pnk/xxxx" -> write/read to set/get the "xxxx" NetKey + * key: "mesh/p_appkey" -> write/read to set/get all Provisioner AppKey Index + * key: "mesh/pak/xxxx" -> write/read to set/get the "xxxx" AppKey + * key: "mesh/p_node" -> write/read to set/get all self-provisioned nodes info + * key: "mesh/pn/xxxx/i" -> write/read to set/get the "xxxx" provisioned node info + * key: "mesh/pn/xxxx/n" -> write/read to set/get the "xxxx" provisioned node name + * key: "mesh/pn/xxxx/c" -> write/read to set/get the "xxxx" provisioned node composition data + */ +void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr) +{ + struct prov_info val = {0}; + + BT_DBG("Primary address 0x%04x, next address allocation 0x%04x", primary_addr, alloc_addr); + + val.primary_addr = primary_addr; + val.alloc_addr = alloc_addr; + + bt_mesh_save_core_settings("mesh/p_prov", (const u8_t *)&val, sizeof(val)); +} + +void bt_mesh_clear_prov_info(void) +{ + bt_mesh_save_core_settings("mesh/p_prov", NULL, 0); +} + +static void clear_p_net_key(u16_t net_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + sprintf(name, "mesh/pnk/%04x", net_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/p_netkey", net_idx); + if (err) { + BT_ERR("Failed to remove 0x%03x from mesh/p_netkey", net_idx); + } +} + +static void clear_p_app_key(u16_t app_idx) +{ + char name[16] = {'\0'}; + int err = 0; + + sprintf(name, "mesh/pak/%04x", app_idx); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/p_appkey", app_idx); + if (err) { + BT_ERR("Failed to remove 0x%03x from mesh/p_appkey", app_idx); + } +} + +static void store_p_net_key(struct bt_mesh_subnet *sub) +{ + struct net_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + memcpy(&key.val[0], sub->keys[0].net, 16); + memcpy(&key.val[1], sub->keys[1].net, 16); + key.kr_flag = sub->kr_flag; + key.kr_phase = sub->kr_phase; + + sprintf(name, "mesh/pnk/%04x", sub->net_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to store NetKey 0x%03x", __func__, sub->net_idx); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/p_netkey", sub->net_idx); + if (err) { + BT_ERR("Failed to add 0x%03x to mesh/p_netkey", sub->net_idx); + } +} + +static void store_p_app_key(struct bt_mesh_app_key *app) +{ + struct app_key_val key = {0}; + char name[16] = {'\0'}; + int err = 0; + + key.net_idx = app->net_idx; + key.updated = app->updated; + memcpy(key.val[0], app->keys[0].val, 16); + memcpy(key.val[1], app->keys[1].val, 16); + + sprintf(name, "mesh/pak/%04x", app->app_idx); + err = bt_mesh_save_core_settings(name, (const u8_t *)&key, sizeof(key)); + if (err) { + BT_ERR("%s, Failed to store AppKey 0x%03x", __func__, app->app_idx); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/p_appkey", app->app_idx); + if (err) { + BT_ERR("Failed to add 0x%03x to mesh/p_appkey", app->app_idx); + } +} + +void bt_mesh_store_p_net_idx(void) +{ + BT_DBG("p_net_idx_next 0x%03x", bt_mesh.p_net_idx_next); + + bt_mesh_save_core_settings("mesh/p_netidx", + (const u8_t *)&bt_mesh.p_net_idx_next, sizeof(bt_mesh.p_net_idx_next)); +} + +void bt_mesh_clear_p_net_idx(void) +{ + bt_mesh_save_core_settings("mesh/p_netidx", NULL, 0); +} + +void bt_mesh_store_p_app_idx(void) +{ + BT_DBG("p_app_idx_next 0x%03x", bt_mesh.p_app_idx_next); + + bt_mesh_save_core_settings("mesh/p_appidx", + (const u8_t *)&bt_mesh.p_app_idx_next, sizeof(bt_mesh.p_app_idx_next)); +} + +void bt_mesh_clear_p_app_idx(void) +{ + bt_mesh_save_core_settings("mesh/p_appidx", NULL, 0); +} + +void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub) +{ + if (sub == NULL) { + BT_ERR("%s, Invalid subnet",__func__); + return; + } + + BT_DBG("NetKeyIndex 0x%03x NetKey %s", sub->net_idx, + bt_hex(sub->keys[0].net, 16)); + + store_p_net_key(sub); +} + +void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key) +{ + if (key == NULL) { + BT_ERR("%s, Invalid AppKey",__func__); + return; + } + + BT_DBG("AppKeyIndex 0x%03x AppKey %s", key->app_idx, + bt_hex(key->keys[0].val, 16)); + + store_p_app_key(key); +} + +void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub) +{ + if (sub == NULL) { + BT_ERR("%s, Invalid subnet",__func__); + return; + } + + BT_DBG("NetKeyIndex 0x%03x", sub->net_idx); + + clear_p_net_key(sub->net_idx); +} + +void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key) +{ + if (key == NULL) { + BT_ERR("%s, Invalid AppKey",__func__); + return; + } + + BT_DBG("AppKeyIndex 0x%03x", key->app_idx); + + clear_p_app_key(key->app_idx); +} + +void bt_mesh_clear_rpl_single(u16_t src) +{ + char name[16] = {'\0'}; + int err = 0; + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_ERR("%s, Invalid source address 0x%04x", __func__, src); + return; + } + + sprintf(name, "mesh/rpl/%04x", src); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/rpl", src); + if (err) { + BT_ERR("Failed to remove 0x%04x from mesh/rpl", src); + } +} + +void bt_mesh_store_node_info(struct bt_mesh_node *node) +{ + struct node_info val = {0}; + char name[16] = {'\0'}; + int err = 0; + + if (node == NULL) { + BT_ERR("%s, Invalid node", __func__); + return; + } + + memcpy(val.addr, node->addr, BLE_MESH_ADDR_LEN); + val.addr_type = node->addr_type; + memcpy(val.dev_uuid, node->dev_uuid, 16); + val.oob_info = node->oob_info; + val.unicast_addr = node->unicast_addr; + val.element_num = node->element_num; + val.net_idx = node->net_idx; + val.flags = node->flags; + val.iv_index = node->iv_index; + memcpy(val.dev_key, node->dev_key, 16); + + sprintf(name, "mesh/pn/%04x/i", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)&val, sizeof(val)); + if (err) { + BT_ERR("Failed to store node 0x%04x info", node->unicast_addr); + return; + } + + err = bt_mesh_add_core_settings_item("mesh/p_node", node->unicast_addr); + if (err) { + BT_ERR("Failed to add node 0x%04x info", node->unicast_addr); + } +} + +static void clear_node(u16_t addr) +{ + char name[16] = {'\0'}; + int err = 0; + + /* Clear node information */ + sprintf(name, "mesh/pn/%04x/i", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + /* Clear node name */ + sprintf(name, "mesh/pn/%04x/n", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + /* Clear node composition data */ + sprintf(name, "mesh/pn/%04x/c", addr); + bt_mesh_save_core_settings(name, NULL, 0); + + err = bt_mesh_remove_core_settings_item("mesh/p_node", addr); + if (err) { + BT_ERR("Failed to remove node 0x%04x", addr); + } +} + +void bt_mesh_clear_node_info(u16_t unicast_addr) +{ + if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) { + BT_ERR("%s, Invalid unicast address 0x%04x", __func__, unicast_addr); + return; + } + + BT_DBG("Unicast address 0x%04x", unicast_addr); + + clear_node(unicast_addr); +} + +void bt_mesh_store_node_name(struct bt_mesh_node *node) +{ + char node_name[BLE_MESH_NODE_NAME_SIZE + 1] = {0}; + char name[16] = {'\0'}; + int err = 0; + + if (node == NULL) { + BT_ERR("%s, Invalid node", __func__); + return; + } + + strncpy(node_name, node->name, BLE_MESH_NODE_NAME_SIZE + 1); + + sprintf(name, "mesh/pn/%04x/n", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)node_name, BLE_MESH_NODE_NAME_SIZE); + if (err) { + BT_ERR("Failed to store node 0x%04x name", node->unicast_addr); + } +} + +void bt_mesh_store_node_comp_data(struct bt_mesh_node *node) +{ + char name[16] = {'\0'}; + int err = 0; + + if (!node || !node->comp_data || node->comp_length == 0U) { + BT_ERR("%s, Invalid node info", __func__); + return; + } + + sprintf(name, "mesh/pn/%04x/c", node->unicast_addr); + err = bt_mesh_save_core_settings(name, (const u8_t *)node->comp_data, node->comp_length); + if (err) { + BT_ERR("Failed to store node 0x%04x comp data", node->unicast_addr); + } +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +int settings_core_init(void) +{ + BT_DBG("%s", __func__); + + k_delayed_work_init(&pending_store, store_pending); + + return 0; +} + +int bt_mesh_settings_init(void) +{ + BT_DBG("%s", __func__); + + bt_mesh_settings_foreach(); + + return 0; +} + +int settings_core_deinit(void) +{ + k_delayed_work_free(&pending_store); + + return 0; +} + +int bt_mesh_settings_deinit(void) +{ + bt_mesh_settings_deforeach(); + + return 0; +} + +#endif /* CONFIG_BLE_MESH_SETTINGS */ diff --git a/components/bt/esp_ble_mesh/mesh_core/settings.h b/components/bt/esp_ble_mesh/mesh_core/settings.h new file mode 100644 index 000000000..52a7b2cd0 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/settings.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2018 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include "net.h" +#include "provisioner_main.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int settings_core_init(void); +int settings_core_load(void); +int settings_core_commit(void); +int settings_core_deinit(void); + +void bt_mesh_store_role(void); +void bt_mesh_store_net(void); +void bt_mesh_store_iv(bool only_duration); +void bt_mesh_clear_iv(void); +void bt_mesh_store_seq(void); +void bt_mesh_clear_seq(void); +void bt_mesh_store_rpl(struct bt_mesh_rpl *rpl); +void bt_mesh_store_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_store_app_key(struct bt_mesh_app_key *key); +void bt_mesh_store_hb_pub(void); +void bt_mesh_store_cfg(void); +void bt_mesh_store_mod_bind(struct bt_mesh_model *mod); +void bt_mesh_store_mod_sub(struct bt_mesh_model *mod); +void bt_mesh_store_mod_pub(struct bt_mesh_model *mod); +void bt_mesh_store_label(void); + +void bt_mesh_clear_role(void); +void bt_mesh_clear_net(void); +void bt_mesh_clear_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_clear_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_rpl(void); + +#if CONFIG_BLE_MESH_PROVISIONER +void bt_mesh_store_prov_info(u16_t primary_addr, u16_t alloc_addr); +void bt_mesh_clear_prov_info(void); +void bt_mesh_store_p_net_idx(void); +void bt_mesh_clear_p_net_idx(void); +void bt_mesh_store_p_app_idx(void); +void bt_mesh_clear_p_app_idx(void); +void bt_mesh_store_p_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_store_p_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_p_subnet(struct bt_mesh_subnet *sub); +void bt_mesh_clear_p_app_key(struct bt_mesh_app_key *key); +void bt_mesh_clear_rpl_single(u16_t src); +void bt_mesh_store_node_info(struct bt_mesh_node *node); +void bt_mesh_clear_node_info(u16_t unicast_addr); +void bt_mesh_store_node_name(struct bt_mesh_node *node); +void bt_mesh_store_node_comp_data(struct bt_mesh_node *node); +#endif + +int bt_mesh_settings_init(void); +int bt_mesh_settings_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _SETTINGS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c new file mode 100644 index 000000000..400eac9ce --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.c @@ -0,0 +1,406 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "nvs.h" +#include "nvs_flash.h" + +#include "mesh_common.h" +#include "settings_nvs.h" +#include "settings.h" + +#if CONFIG_BLE_MESH_SETTINGS + +enum settings_type { + SETTINGS_CORE, + SETTINGS_SERVER, +}; + +struct settings_context { + char *nvs_name; + nvs_handle handle; + + int (*settings_init)(void); + int (*settings_load)(void); + int (*settings_commit)(void); + int (*settings_deinit)(void); + int (*settings_erase)(void); +}; + +static struct settings_context settings_ctx[] = { + [SETTINGS_CORE] = { + .nvs_name = "mesh_core", + .settings_init = settings_core_init, + .settings_load = settings_core_load, + .settings_commit = settings_core_commit, + .settings_deinit = settings_core_deinit, + }, + [SETTINGS_SERVER] = { + .nvs_name = "mesh_server", + .settings_init = NULL, + .settings_load = NULL, + .settings_commit = NULL, + }, +}; + +/* API used to initialize, load and commit BLE Mesh related settings */ + +void bt_mesh_settings_foreach(void) +{ + int err = 0; + int i; + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + err = nvs_flash_init_partition(CONFIG_BLE_MESH_PARTITION_NAME); + if (err != ESP_OK) { + BT_ERR("Failed to init mesh partition, name %s, err %d", CONFIG_BLE_MESH_PARTITION_NAME, err); + return; + } +#endif + + for (i = 0; i < ARRAY_SIZE(settings_ctx); i++) { + struct settings_context *ctx = &settings_ctx[i]; + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + err = nvs_open_from_partition(CONFIG_BLE_MESH_PARTITION_NAME, ctx->nvs_name, NVS_READWRITE, &ctx->handle); +#else + err = nvs_open(ctx->nvs_name, NVS_READWRITE, &ctx->handle); +#endif + if (err != ESP_OK) { + BT_ERR("%s, Open nvs failed, name %s, err %d", __func__, ctx->nvs_name, err); + continue; + } + + if (ctx->settings_init && ctx->settings_init()) { + BT_ERR("%s, Init settings failed, name %s", __func__, ctx->nvs_name); + continue; + } + + if (ctx->settings_load && ctx->settings_load()) { + BT_ERR("%s, Load settings failed, name %s", __func__, ctx->nvs_name); + continue; + } + + if (ctx->settings_commit && ctx->settings_commit()) { + BT_ERR("%s, Commit settings failed, name %s", __func__, ctx->nvs_name); + continue; + } + } +} + +void bt_mesh_settings_deforeach(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(settings_ctx); i++) { + struct settings_context *ctx = &settings_ctx[i]; + + if (ctx->settings_deinit && ctx->settings_deinit()) { + BT_ERR("%s, Deinit settings failed, name %s", __func__, ctx->nvs_name); + continue; + } + + nvs_close(ctx->handle); + } + +#if CONFIG_BLE_MESH_SPECIFIC_PARTITION + nvs_flash_deinit_partition(CONFIG_BLE_MESH_PARTITION_NAME); +#endif +} + +/* API used to get BLE Mesh related nvs handle */ + +static inline nvs_handle settings_get_nvs_handle(enum settings_type type) +{ + return settings_ctx[type].handle; +} + +/* API used to store/erase BLE Mesh related settings */ + +static int settings_save(nvs_handle handle, const char *key, const u8_t *val, size_t len) +{ + int err = 0; + + if (key == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_DBG("%s, nvs %s, key %s", __func__, val ? "set" : "erase", key); + + if (val) { + err = nvs_set_blob(handle, key, val, len); + } else { + err = nvs_erase_key(handle, key); + if (err == ESP_ERR_NVS_NOT_FOUND) { + BT_DBG("%s, %s does not exist", __func__, key); + return 0; + } + } + if (err != ESP_OK) { + BT_ERR("%s, Failed to %s %s data (err %d)", __func__, + val ? "set" : "erase", key, err); + return -EIO; + } + + err = nvs_commit(handle); + if (err != ESP_OK) { + BT_ERR("%s, Failed to commit settings (err %d)", __func__, err); + return -EIO; + } + + return 0; +} + +int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len) +{ + nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + return settings_save(handle, key, val, len); +} + +/* API used to load BLE Mesh related settings */ + +static int settings_load(nvs_handle handle, const char *key, + u8_t *buf, size_t buf_len, bool *exist) +{ + int err = 0; + + if (key == NULL || buf == NULL || exist == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + err = nvs_get_blob(handle, key, buf, &buf_len); + if (err != ESP_OK) { + if (err == ESP_ERR_NVS_NOT_FOUND) { + BT_DBG("%s, Settings %s is not found", __func__, key); + *exist = false; + return 0; + } + + BT_ERR("%s, Failed to get %s data (err %d)", __func__, key, err); + return -EIO; + } + + *exist = true; + return 0; +} + +int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist) +{ + nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + return settings_load(handle, key, buf, buf_len, exist); +} + +/* API used to get length of BLE Mesh related settings */ + +static size_t settings_get_length(nvs_handle handle, const char *key) +{ + size_t len = 0U; + int err = 0; + + if (key == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return 0; + } + + err = nvs_get_blob(handle, key, NULL, &len); + if (err != ESP_OK) { + if (err != ESP_ERR_NVS_NOT_FOUND) { + BT_ERR("%s, Failed to get %s length (err %d)", __func__, key, err); + } + return 0; + } + + return len; +} + +/* API used to get BLE Mesh related items. Here items mean model key, NetKey/AppKey + * Index, etc. which are going to be used as the prefix of the nvs keys of the BLE + * Mesh settings. + */ + +static struct net_buf_simple *settings_get_item(nvs_handle handle, const char *key) +{ + struct net_buf_simple *buf = NULL; + size_t length = 0U; + bool exist = false; + int err = 0; + + length = settings_get_length(handle, key); + if (!length) { + BT_DBG("%s, Empty %s", __func__, key); + return NULL; + } + + buf = bt_mesh_alloc_buf(length); + if (!buf) { + BT_ERR("%s, Failed to allocate memory", __func__); + /* TODO: in this case, erase all related settings? */ + return NULL; + } + + err = settings_load(handle, key, buf->data, length, &exist); + if (err) { + BT_ERR("%s, Failed to load %s", __func__, key); + /* TODO: in this case, erase all related settings? */ + bt_mesh_free_buf(buf); + return NULL; + } + + if (exist == false) { + bt_mesh_free_buf(buf); + return NULL; + } + + buf->len = length; + return buf; +} + +struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key) +{ + nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + return settings_get_item(handle, key); +} + +/* API used to check if the settings item exists */ + +static bool is_settings_item_exist(struct net_buf_simple *buf, const u16_t val) +{ + struct net_buf_simple_state state = {0}; + size_t length = 0U; + int i; + + if (!buf) { + return false; + } + + net_buf_simple_save(buf, &state); + + length = buf->len; + for (i = 0; i < length / SETTINGS_ITEM_SIZE; i++) { + u16_t item = net_buf_simple_pull_le16(buf); + if (item == val) { + net_buf_simple_restore(buf, &state); + return true; + } + } + + net_buf_simple_restore(buf, &state); + return false; +} + +/* API used to add the settings item */ + +static int settings_add_item(nvs_handle handle, const char *key, const u16_t val) +{ + struct net_buf_simple *store = NULL; + struct net_buf_simple *buf = NULL; + size_t length = 0U; + int err = 0; + + buf = settings_get_item(handle, key); + + /* Check if val already exists */ + if (is_settings_item_exist(buf, val) == true) { + BT_DBG("%s, 0x%04x already exists", __func__, val); + bt_mesh_free_buf(buf); + return 0; + } + + length = (buf ? buf->len : 0) + sizeof(val); + + store = bt_mesh_alloc_buf(length); + if (!store) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free_buf(buf); + return -ENOMEM; + } + + if (buf) { + net_buf_simple_add_mem(store, buf->data, buf->len); + } + net_buf_simple_add_mem(store, &val, sizeof(val)); + + err = settings_save(handle, key, store->data, store->len); + + bt_mesh_free_buf(store); + bt_mesh_free_buf(buf); + return err; +} + +int bt_mesh_add_core_settings_item(const char *key, const u16_t val) +{ + nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + return settings_add_item(handle, key, val); +} + +/* API used to remove the settings item */ + +static int settings_remove_item(nvs_handle handle, const char *key, const u16_t val) +{ + struct net_buf_simple *store = NULL; + struct net_buf_simple *buf = NULL; + size_t length = 0U; + size_t buf_len = 0U; + int err = 0; + int i; + + buf = settings_get_item(handle, key); + + /* Check if val does exist */ + if (is_settings_item_exist(buf, val) == false) { + BT_DBG("%s, 0x%04x does not exist", __func__, val); + bt_mesh_free_buf(buf); + return 0; + } + + length = buf->len - sizeof(val); + if (!length) { + settings_save(handle, key, NULL, 0); + bt_mesh_free_buf(buf); + return 0; + } + + store = bt_mesh_alloc_buf(length); + if (!store) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free_buf(buf); + return -ENOMEM; + } + + buf_len = buf->len; + for (i = 0; i < buf_len / SETTINGS_ITEM_SIZE; i++) { + u16_t item = net_buf_simple_pull_le16(buf); + if (item != val) { + net_buf_simple_add_le16(store, item); + } + } + + err = settings_save(handle, key, store->data, store->len); + + bt_mesh_free_buf(store); + bt_mesh_free_buf(buf); + return err; +} + +int bt_mesh_remove_core_settings_item(const char *key, const u16_t val) +{ + nvs_handle handle = settings_get_nvs_handle(SETTINGS_CORE); + return settings_remove_item(handle, key, val); +} + +#endif /* CONFIG_BLE_MESH_SETTINGS */ diff --git a/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h new file mode 100644 index 000000000..099efa54d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/storage/settings_nvs.h @@ -0,0 +1,47 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_SETTINGS_NVS_H_ +#define _BLE_MESH_SETTINGS_NVS_H_ + +#include "mesh_buf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define SETTINGS_ITEM_SIZE sizeof(u16_t) + +#define BLE_MESH_GET_ELEM_IDX(x) ((u8_t)((x) >> 8)) +#define BLE_MESH_GET_MODEL_IDX(x) ((u8_t)(x)) +#define BLE_MESH_GET_MODEL_KEY(a, b) ((u16_t)(((u16_t)((a) << 8)) | b)) + +void bt_mesh_settings_foreach(void); +void bt_mesh_settings_deforeach(void); + +int bt_mesh_save_core_settings(const char *key, const u8_t *val, size_t len); + +int bt_mesh_load_core_settings(const char *key, u8_t *buf, size_t buf_len, bool *exist); + +struct net_buf_simple *bt_mesh_get_core_settings_item(const char *key); + +int bt_mesh_add_core_settings_item(const char *key, const u16_t val); + +int bt_mesh_remove_core_settings_item(const char *key, const u16_t val); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_SETTINGS_NVS_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/test.c b/components/bt/esp_ble_mesh/mesh_core/test.c new file mode 100644 index 000000000..616c20708 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/test.c @@ -0,0 +1,169 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include "adv.h" +#include "mesh.h" +#include "test.h" +#include "crypto.h" +#include "access.h" +#include "foundation.h" +#include "mesh_main.h" + +#if defined(CONFIG_BLE_MESH_SELF_TEST) + +int bt_mesh_test(void) +{ + return 0; +} + +#if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK +int bt_mesh_device_auto_enter_network(struct bt_mesh_device_network_info *info) +{ + const struct bt_mesh_comp *comp = NULL; + struct bt_mesh_model *model = NULL; + struct bt_mesh_elem *elem = NULL; + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + struct bt_mesh_subnet *sub = NULL; + int i, j, k; + int err = 0; + + if (info == NULL || !BLE_MESH_ADDR_IS_UNICAST(info->unicast_addr) || + !BLE_MESH_ADDR_IS_GROUP(info->group_addr)) { + return -EINVAL; + } + + /* The device becomes a node and enters the network */ + err = bt_mesh_provision(info->net_key, info->net_idx, info->flags, info->iv_index, + info->unicast_addr, info->dev_key); + if (err) { + BT_ERR("%s, bt_mesh_provision() failed (err %d)", __func__, err); + return err; + } + + /* Adds application key to device */ + sub = bt_mesh_subnet_get(info->net_idx); + if (!sub) { + BT_ERR("%s, Failed to find subnet 0x%04x", __func__, info->net_idx); + return -ENODEV; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + key = &bt_mesh.app_keys[i]; + if (key->net_idx == BLE_MESH_KEY_UNUSED) { + break; + } + } + if (i == ARRAY_SIZE(bt_mesh.app_keys)) { + BT_ERR("%s, Failed to allocate memory, AppKeyIndex 0x%04x", __func__, info->app_idx); + return -ENOMEM; + } + + keys = sub->kr_flag ? &key->keys[1] : &key->keys[0]; + + if (bt_mesh_app_id(info->app_key, &keys->id)) { + BT_ERR("%s, Failed to calculate AID, AppKeyIndex 0x%04x", __func__, info->app_idx); + return -EIO; + } + + key->net_idx = info->net_idx; + key->app_idx = info->app_idx; + memcpy(keys->val, info->app_key, 16); + + /* Binds AppKey with all non-config models, adds group address to all these models */ + comp = bt_mesh_comp_get(); + if (!comp) { + BT_ERR("%s, Composition data is NULL", __func__); + return -ENODEV; + } + + for (i = 0; i < comp->elem_count; i++) { + elem = &comp->elem[i]; + for (j = 0; j < elem->model_count; j++) { + model = &elem->models[j]; + if (model->id == BLE_MESH_MODEL_ID_CFG_SRV || + model->id == BLE_MESH_MODEL_ID_CFG_CLI) { + continue; + } + for (k = 0; k < ARRAY_SIZE(model->keys); k++) { + if (model->keys[k] == BLE_MESH_KEY_UNUSED) { + model->keys[k] = info->app_idx; + break; + } + } + for (k = 0; k < ARRAY_SIZE(model->groups); k++) { + if (model->groups[k] == BLE_MESH_ADDR_UNASSIGNED) { + model->groups[k] = info->group_addr; + break; + } + } + } + for (j = 0; j < elem->vnd_model_count; j++) { + model = &elem->vnd_models[j]; + for (k = 0; k < ARRAY_SIZE(model->keys); k++) { + if (model->keys[k] == BLE_MESH_KEY_UNUSED) { + model->keys[k] = info->app_idx; + break; + } + } + for (k = 0; k < ARRAY_SIZE(model->groups); k++) { + if (model->groups[k] == BLE_MESH_ADDR_UNASSIGNED) { + model->groups[k] = info->group_addr; + break; + } + } + } + } + + return 0; +} +#endif /* CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK */ + +#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST +int bt_mesh_test_update_white_list(struct bt_mesh_white_list *wl) +{ + int err = 0; + + if (wl == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + BT_INFO("%s, addr %s, addr_type 0x%02x", wl->add_remove ? "Add" : "Remove", + bt_hex(wl->remote_bda, BLE_MESH_ADDR_LEN), wl->addr_type); + + err = bt_le_update_white_list(wl); + if (err) { + BT_ERR("Failed to update white list"); + } + + return err; +} + +int bt_mesh_test_start_scanning(bool wl_en) +{ + BT_INFO("Scan with filter policy %s", wl_en ? "enabled" : "disabled"); + + if (wl_en) { + return bt_mesh_scan_with_wl_enable(); + } else { + return bt_mesh_scan_enable(); + } +} + +int bt_mesh_test_stop_scanning(void) +{ + return bt_mesh_scan_disable(); +} +#endif /* CONFIG_BLE_MESH_TEST_USE_WHITE_LIST */ + +#endif /* CONFIG_BLE_MESH_SELF_TEST */ diff --git a/components/bt/esp_ble_mesh/mesh_core/test.h b/components/bt/esp_ble_mesh/mesh_core/test.h new file mode 100644 index 000000000..ffb054f3c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/test.h @@ -0,0 +1,52 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _BLE_MESH_TEST_H_ +#define _BLE_MESH_TEST_H_ + +#include "mesh_bearer_adapt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int bt_mesh_test(void); + +struct bt_mesh_device_network_info { + u8_t net_key[16]; + u16_t net_idx; + u8_t flags; + u32_t iv_index; + u16_t unicast_addr; + u8_t dev_key[16]; + u8_t app_key[16]; + u16_t app_idx; + u16_t group_addr; +}; + +int bt_mesh_device_auto_enter_network(struct bt_mesh_device_network_info *info); + +/* Before trying to update the white list, users need to make sure that + * one of the following conditions is satisfied: + * 1. BLE scanning is disabled; + * 2. BLE scanning is enabled with scan filter policy disabled; + * If BLE scanning is enabled with scan filter policy enabled, users need + * to stop BLE scanning firstly, then the white list can be updated. + */ +int bt_mesh_test_update_white_list(struct bt_mesh_white_list *wl); + +int bt_mesh_test_start_scanning(bool wl_en); + +int bt_mesh_test_stop_scanning(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _BLE_MESH_TEST_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.c b/components/bt/esp_ble_mesh/mesh_core/transport.c new file mode 100644 index 000000000..1ffbf9839 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/transport.c @@ -0,0 +1,1984 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLE_MESH_DEBUG_TRANS) + +#include "crypto.h" +#include "adv.h" +#include "mesh.h" +#include "lpn.h" +#include "friend.h" +#include "access.h" +#include "foundation.h" +#include "settings.h" +#include "transport.h" +#include "mesh_main.h" +#include "mesh_common.h" +#include "cfg_srv.h" + +/* The transport layer needs at least three buffers for itself to avoid + * deadlocks. Ensure that there are a sufficient number of advertising + * buffers available compared to the maximum supported outgoing segment + * count. + */ +_Static_assert(CONFIG_BLE_MESH_ADV_BUF_COUNT >= (CONFIG_BLE_MESH_TX_SEG_MAX + 3), + "Too small BLE Mesh adv buffer count"); + +#define AID_MASK ((u8_t)(BIT_MASK(6))) + +#define SEG(data) ((data)[0] >> 7) +#define AKF(data) (((data)[0] >> 6) & 0x01) +#define AID(data) ((data)[0] & AID_MASK) +#define ASZMIC(data) (((data)[1] >> 7) & 1) + +#define APP_MIC_LEN(aszmic) ((aszmic) ? BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT) + +#define UNSEG_HDR(akf, aid) ((akf << 6) | (aid & AID_MASK)) +#define SEG_HDR(akf, aid) (UNSEG_HDR(akf, aid) | 0x80) + +#define BLOCK_COMPLETE(seg_n) (u32_t)(((u64_t)1 << (seg_n + 1)) - 1) + +#define SEQ_AUTH(iv_index, seq) (((u64_t)iv_index) << 24 | (u64_t)seq) + +/* Number of retransmit attempts (after the initial transmit) per segment */ +#define SEG_RETRANSMIT_ATTEMPTS 4 + +/* "This timer shall be set to a minimum of 200 + 50 * TTL milliseconds.". + * We use 400 since 300 is a common send duration for standard HCI, and we + * need to have a timeout that's bigger than that. + */ +#define SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) (K_MSEC(400) + 50 * (tx)->ttl) +/* When sending to a group, the messages are not acknowledged, and there's no + * reason to delay the repetitions significantly. Delaying by more than 0 ms + * to avoid flooding the network. + */ +#define SEG_RETRANSMIT_TIMEOUT_GROUP K_MSEC(50) + +#define SEG_RETRANSMIT_TIMEOUT(tx) \ + (BLE_MESH_ADDR_IS_UNICAST((tx)->dst) ? \ + SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) : \ + SEG_RETRANSMIT_TIMEOUT_GROUP) + +/* How long to wait for available buffers before giving up */ +#define BUF_TIMEOUT K_NO_WAIT + +static struct seg_tx { + struct bt_mesh_subnet *sub; + struct net_buf *seg[CONFIG_BLE_MESH_TX_SEG_MAX]; + u64_t seq_auth; + u16_t dst; + u8_t seg_n:5, /* Last segment index */ + new_key:1; /* New/old key */ + u8_t nack_count; /* Number of unacked segs */ + u8_t ttl; + u8_t seg_pending; /* Number of segments pending */ + u8_t attempts; /* Transmit attempts */ + const struct bt_mesh_send_cb *cb; + void *cb_data; + struct k_delayed_work retransmit; /* Retransmit timer */ +} seg_tx[CONFIG_BLE_MESH_TX_SEG_MSG_COUNT]; + +static struct seg_rx { + struct bt_mesh_subnet *sub; + u64_t seq_auth; + u8_t seg_n: 5, + ctl: 1, + in_use: 1, + obo: 1; + u8_t hdr; + u8_t ttl; + u16_t src; + u16_t dst; + u32_t block; + u32_t last; + struct k_delayed_work ack; + struct net_buf_simple buf; +} seg_rx[CONFIG_BLE_MESH_RX_SEG_MSG_COUNT] = { + [0 ... (CONFIG_BLE_MESH_RX_SEG_MSG_COUNT - 1)] = { + .buf.size = CONFIG_BLE_MESH_RX_SDU_MAX, + }, +}; + +static u8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT * + CONFIG_BLE_MESH_RX_SDU_MAX)]; + +static u16_t hb_sub_dst = BLE_MESH_ADDR_UNASSIGNED; + +static bt_mesh_mutex_t tx_seg_lock; +static bt_mesh_mutex_t rx_seg_lock; + +static void bt_mesh_tx_seg_mutex_new(void) +{ + if (!tx_seg_lock.mutex) { + bt_mesh_mutex_create(&tx_seg_lock); + } +} + +static void bt_mesh_tx_seg_mutex_free(void) +{ + bt_mesh_mutex_free(&tx_seg_lock); +} + +static void bt_mesh_tx_seg_lock(void) +{ + bt_mesh_mutex_lock(&tx_seg_lock); +} + +static void bt_mesh_tx_seg_unlock(void) +{ + bt_mesh_mutex_unlock(&tx_seg_lock); +} + +static void bt_mesh_rx_seg_mutex_new(void) +{ + if (!rx_seg_lock.mutex) { + bt_mesh_mutex_create(&rx_seg_lock); + } +} + +static void bt_mesh_rx_seg_mutex_free(void) +{ + bt_mesh_mutex_free(&rx_seg_lock); +} + +static void bt_mesh_rx_seg_lock(void) +{ + bt_mesh_mutex_lock(&rx_seg_lock); +} + +static void bt_mesh_rx_seg_unlock(void) +{ + bt_mesh_mutex_unlock(&rx_seg_lock); +} + +u8_t bt_mesh_get_seg_retrans_num(void) +{ + return SEG_RETRANSMIT_ATTEMPTS; +} + +s32_t bt_mesh_get_seg_retrans_timeout(u8_t ttl) +{ + /* This function will be used when a client model sending an + * acknowledged message. And if the dst of a message is not + * a unicast address, the function will not be invoked. + * So we can directly use the SEG_RETRANSMIT_TIMEOUT_UNICAST + * macro here. + */ + struct seg_tx tx = { + .ttl = ttl, + }; + return SEG_RETRANSMIT_TIMEOUT_UNICAST(&tx); +} + +void bt_mesh_set_hb_sub_dst(u16_t addr) +{ + hb_sub_dst = addr; +} + +static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + struct net_buf *buf = NULL; + + BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x sdu_len %u", + tx->src, tx->ctx->addr, tx->ctx->app_idx, sdu->len); + + buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, BUF_TIMEOUT); + if (!buf) { + BT_ERR("%s, Out of network buffers", __func__); + return -ENOBUFS; + } + + net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN); + + if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) { + net_buf_add_u8(buf, UNSEG_HDR(0, 0)); + } else { + net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid)); + } + + net_buf_add_mem(buf, sdu->data, sdu->len); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx, + tx->src, tx->ctx->addr, + NULL, 1)) { + if (BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + BT_ERR("Not enough space in Friend Queue"); + net_buf_unref(buf); + return -ENOBUFS; + } else { + BT_WARN("No space in Friend Queue"); + goto send; + } + } + + if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, + NULL, 1, &buf->b) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + /* PDUs for a specific Friend should only go + * out through the Friend Queue. + */ + net_buf_unref(buf); + send_cb_finalize(cb, cb_data); + return 0; + } + } + +send: + return bt_mesh_net_send(tx, buf, cb, cb_data); +} + +bool bt_mesh_tx_in_progress(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + if (seg_tx[i].nack_count) { + return true; + } + } + + return false; +} + +static void seg_tx_done(struct seg_tx *tx, u8_t seg_idx) +{ + bt_mesh_adv_buf_ref_debug(__func__, tx->seg[seg_idx], 3U, BLE_MESH_BUF_REF_SMALL); + + BLE_MESH_ADV(tx->seg[seg_idx])->busy = 0U; + net_buf_unref(tx->seg[seg_idx]); + tx->seg[seg_idx] = NULL; + tx->nack_count--; +} + +static void seg_tx_reset(struct seg_tx *tx) +{ + int i; + + bt_mesh_tx_seg_lock(); + + k_delayed_work_cancel(&tx->retransmit); + + tx->cb = NULL; + tx->cb_data = NULL; + tx->seq_auth = 0U; + tx->sub = NULL; + tx->dst = BLE_MESH_ADDR_UNASSIGNED; + + for (i = 0; i <= tx->seg_n && tx->nack_count; i++) { + if (!tx->seg[i]) { + continue; + } + + seg_tx_done(tx, i); + } + + tx->nack_count = 0U; + + bt_mesh_tx_seg_unlock(); + + if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) { + BT_DBG("Proceding with pending IV Update"); + /* bt_mesh_net_iv_update() will re-enable the flag if this + * wasn't the only transfer. + */ + if (bt_mesh_net_iv_update(bt_mesh.iv_index, false)) { + bt_mesh_net_sec_update(NULL); + } + } +} + +static inline void seg_tx_complete(struct seg_tx *tx, int err) +{ + const struct bt_mesh_send_cb *cb = tx->cb; + void *cb_data = tx->cb_data; + + seg_tx_reset(tx); + + if (cb && cb->end) { + cb->end(err, cb_data); + } +} + +static void schedule_retransmit(struct seg_tx *tx) +{ + if (--tx->seg_pending) { + return; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst) && !tx->attempts) { + BT_INFO("Complete tx sdu to group"); + seg_tx_complete(tx, 0); + return; + } + + k_delayed_work_submit(&tx->retransmit, SEG_RETRANSMIT_TIMEOUT(tx)); +} + +static void seg_first_send_start(u16_t duration, int err, void *user_data) +{ + struct seg_tx *tx = user_data; + + if (tx->cb && tx->cb->start) { + tx->cb->start(duration, err, tx->cb_data); + } +} + +static void seg_send_start(u16_t duration, int err, void *user_data) +{ + struct seg_tx *tx = user_data; + + /* If there's an error in transmitting the 'sent' callback will never + * be called. Make sure that we kick the retransmit timer also in this + * case since otherwise we risk the transmission of becoming stale. + */ + if (err) { + schedule_retransmit(tx); + } +} + +static void seg_sent(int err, void *user_data) +{ + struct seg_tx *tx = user_data; + + schedule_retransmit(tx); +} + +static const struct bt_mesh_send_cb first_sent_cb = { + .start = seg_first_send_start, + .end = seg_sent, +}; + +static const struct bt_mesh_send_cb seg_sent_cb = { + .start = seg_send_start, + .end = seg_sent, +}; + +static void seg_tx_send_unacked(struct seg_tx *tx) +{ + int i, err = 0; + + bt_mesh_tx_seg_lock(); + + if (!(tx->attempts--)) { + BT_WARN("Ran out of retransmit attempts"); + bt_mesh_tx_seg_unlock(); + seg_tx_complete(tx, -ETIMEDOUT); + return; + } + + BT_INFO("Attempts: %u", tx->attempts); + + for (i = 0; i <= tx->seg_n; i++) { + struct net_buf *seg = tx->seg[i]; + + if (!seg) { + continue; + } + + if (BLE_MESH_ADV(seg)->busy) { + BT_DBG("Skipping segment that's still advertising"); + continue; + } + + tx->seg_pending++; + + BT_DBG("resending %u/%u", i, tx->seg_n); + + err = bt_mesh_net_resend(tx->sub, seg, tx->new_key, + &seg_sent_cb, tx); + if (err) { + BT_ERR("%s, Sending segment failed", __func__); + bt_mesh_tx_seg_unlock(); + seg_tx_complete(tx, -EIO); + return; + } + } + + bt_mesh_tx_seg_unlock(); +} + +static void seg_retransmit(struct k_work *work) +{ + struct seg_tx *tx = CONTAINER_OF(work, struct seg_tx, retransmit); + + seg_tx_send_unacked(tx); +} + +static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + u8_t seg_hdr = 0U, seg_o = 0U; + u16_t seq_zero = 0U; + struct seg_tx *tx = NULL; + int i; + + BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u", + net_tx->src, net_tx->ctx->addr, net_tx->ctx->app_idx, + net_tx->aszmic, sdu->len); + + if (sdu->len < 1) { + BT_ERR("%s, Zero-length SDU not allowed", __func__); + return -EINVAL; + } + + if (sdu->len > BLE_MESH_TX_SDU_MAX) { + BT_ERR("%s, Not enough segment buffers for length %u", __func__, sdu->len); + return -EMSGSIZE; + } + + for (tx = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) { + if (!seg_tx[i].nack_count) { + tx = &seg_tx[i]; + break; + } + } + + if (!tx) { + BT_ERR("%s, No multi-segment message contexts available", __func__); + return -EBUSY; + } + + if (net_tx->ctx->app_idx == BLE_MESH_KEY_DEV) { + seg_hdr = SEG_HDR(0, 0); + } else { + seg_hdr = SEG_HDR(1, net_tx->aid); + } + + seg_o = 0U; + tx->dst = net_tx->ctx->addr; + tx->seg_n = (sdu->len - 1) / BLE_MESH_APP_SEG_SDU_MAX; + tx->nack_count = tx->seg_n + 1; + tx->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); + tx->sub = net_tx->sub; + tx->new_key = net_tx->sub->kr_flag; + tx->attempts = SEG_RETRANSMIT_ATTEMPTS; + tx->seg_pending = 0; + tx->cb = cb; + tx->cb_data = cb_data; + + if (net_tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) { + tx->ttl = bt_mesh_default_ttl_get(); + } else { + tx->ttl = net_tx->ctx->send_ttl; + } + + seq_zero = tx->seq_auth & TRANS_SEQ_ZERO_MASK; + + BT_DBG("SeqZero 0x%04x", seq_zero); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && + !bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src, + tx->dst, &tx->seq_auth, + tx->seg_n + 1) && + BLE_MESH_ADDR_IS_UNICAST(tx->dst)) { + BT_ERR("Not enough space in Friend Queue for %u segments", + tx->seg_n + 1); + seg_tx_reset(tx); + return -ENOBUFS; + } + + for (seg_o = 0U; sdu->len; seg_o++) { + struct net_buf *seg = NULL; + u16_t len = 0U; + int err = 0; + + seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, net_tx->xmit, + BUF_TIMEOUT); + if (!seg) { + BT_ERR("%s, Out of segment buffers", __func__); + seg_tx_reset(tx); + return -ENOBUFS; + } + + net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN); + + net_buf_add_u8(seg, seg_hdr); + net_buf_add_u8(seg, (net_tx->aszmic << 7) | seq_zero >> 6); + net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | + (seg_o >> 3))); + net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx->seg_n); + + len = MIN(sdu->len, BLE_MESH_APP_SEG_SDU_MAX); + net_buf_add_mem(seg, sdu->data, len); + net_buf_simple_pull(sdu, len); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + enum bt_mesh_friend_pdu_type type = BLE_MESH_FRIEND_PDU_PARTIAL; + + if (seg_o == tx->seg_n) { + type = BLE_MESH_FRIEND_PDU_COMPLETE; + } else { + type = BLE_MESH_FRIEND_PDU_PARTIAL; + } + + if (bt_mesh_friend_enqueue_tx(net_tx, type, + &tx->seq_auth, + tx->seg_n + 1, + &seg->b) && + BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) { + /* PDUs for a specific Friend should only go + * out through the Friend Queue. + */ + net_buf_unref(seg); + continue; + } + } + + tx->seg[seg_o] = net_buf_ref(seg); + + BT_DBG("Sending %u/%u", seg_o, tx->seg_n); + tx->seg_pending++; + + err = bt_mesh_net_send(net_tx, seg, + seg_o ? &seg_sent_cb : &first_sent_cb, + tx); + if (err) { + BT_ERR("%s, Sending segment failed", __func__); + seg_tx_reset(tx); + return err; + } + } + + /* This can happen if segments only went into the Friend Queue */ + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) { + seg_tx_reset(tx); + /* If there was a callback notify sending immediately since + * there's no other way to track this (at least currently) + * with the Friend Queue. + */ + send_cb_finalize(cb, cb_data); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + bt_mesh_lpn_established()) { + bt_mesh_lpn_poll(); + } + + return 0; +} + +struct bt_mesh_app_key *bt_mesh_app_key_find(u16_t app_idx) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) { + struct bt_mesh_app_key *key = &bt_mesh.app_keys[i]; + + if (key->net_idx != BLE_MESH_KEY_UNUSED && + key->app_idx == app_idx) { + return key; + } + } + + return NULL; +} + +int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ + const u8_t *key = NULL; + u8_t *ad = NULL, role = 0U; + u8_t aid = 0U; + int err = 0; + + if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) { + BT_ERR("%s, Insufficient tailroom for Transport MIC", __func__); + return -EINVAL; + } + + if (msg->len > BLE_MESH_SDU_UNSEG_MAX) { + tx->ctx->send_rel = 1U; + } + + BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->sub->net_idx, + tx->ctx->app_idx, tx->ctx->addr); + BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len)); + + role = bt_mesh_get_device_role(tx->ctx->model, tx->ctx->srv_send); + if (role == ROLE_NVAL) { + BT_ERR("%s, Failed to get model role", __func__); + return -EINVAL; + } + + err = bt_mesh_app_key_get(tx->sub, tx->ctx->app_idx, &key, + &aid, role, tx->ctx->addr); + if (err) { + return err; + } + + tx->aid = aid; + + if (!tx->ctx->send_rel || net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) { + tx->aszmic = 0U; + } else { + tx->aszmic = 1U; + } + + if (BLE_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) { + ad = bt_mesh_label_uuid_get(tx->ctx->addr); + } else { + ad = NULL; + } + + err = bt_mesh_app_encrypt(key, tx->ctx->app_idx == BLE_MESH_KEY_DEV, + tx->aszmic, msg, ad, tx->src, + tx->ctx->addr, bt_mesh.seq, + BLE_MESH_NET_IVI_TX); + if (err) { + BT_ERR("%s, Encrypt failed", __func__); + return err; + } + + if (tx->ctx->send_rel) { + err = send_seg(tx, msg, cb, cb_data); + } else { + err = send_unseg(tx, msg, cb, cb_data); + } + + return err; +} + +static void update_rpl(struct bt_mesh_rpl *rpl, struct bt_mesh_net_rx *rx) +{ + rpl->src = rx->ctx.addr; + rpl->seq = rx->seq; + rpl->old_iv = rx->old_iv; + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_store_rpl(rpl); + } +} + +/* Check the Replay Protection List for a replay attempt. If non-NULL match + * parameter is given the RPL slot is returned but it is not immediately + * updated (needed for segmented messages), whereas if a NULL match is given + * the RPL is immediately updated (used for unsegmented messages). + */ +static bool is_replay(struct bt_mesh_net_rx *rx, struct bt_mesh_rpl **match) +{ + int i; + + /* Don't bother checking messages from ourselves */ + if (rx->net_if == BLE_MESH_NET_IF_LOCAL) { + return false; + } + + /* The RPL is used only for the local node */ + if (!rx->local_match) { + return false; + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + + /* Empty slot */ + if (!rpl->src) { + if (match) { + *match = rpl; + } else { + update_rpl(rpl, rx); + } + + return false; + } + + /* Existing slot for given address */ + if (rpl->src == rx->ctx.addr) { + if (rx->old_iv && !rpl->old_iv) { + return true; + } + + if ((!rx->old_iv && rpl->old_iv) || + rpl->seq < rx->seq) { + if (match) { + *match = rpl; + } else { + update_rpl(rpl, rx); + } + + return false; + } else { + return true; + } + } + } + + BT_ERR("%s, RPL is full!", __func__); + return true; +} + +static int sdu_recv(struct bt_mesh_net_rx *rx, u32_t seq, u8_t hdr, + u8_t aszmic, struct net_buf_simple *buf) +{ + struct net_buf_simple *sdu = NULL; + size_t array_size = 0U; + size_t i = 0U; + u8_t *ad = NULL; + int err = 0; + + BT_DBG("ASZMIC %u AKF %u AID 0x%02x", aszmic, AKF(&hdr), AID(&hdr)); + BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len)); + + if (buf->len < 1 + APP_MIC_LEN(aszmic)) { + BT_ERR("%s, Too short SDU + MIC", __func__); + return -EINVAL; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !rx->local_match) { + BT_DBG("Ignoring PDU for LPN 0x%04x of this Friend", + rx->ctx.recv_dst); + return 0; + } + + if (BLE_MESH_ADDR_IS_VIRTUAL(rx->ctx.recv_dst)) { + ad = bt_mesh_label_uuid_get(rx->ctx.recv_dst); + } else { + ad = NULL; + } + + /* Adjust the length to not contain the MIC at the end */ + buf->len -= APP_MIC_LEN(aszmic); + + /* Use bt_mesh_alloc_buf() instead of NET_BUF_SIMPLE_DEFINE to avoid + * causing btu task stackoverflow. + */ + sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - BLE_MESH_MIC_SHORT); + if (!sdu) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + if (!AKF(&hdr)) { + array_size = bt_mesh_rx_devkey_size(); + + for (i = 0U; i < array_size; i++) { + const u8_t *dev_key = NULL; + + dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr); + if (!dev_key) { + BT_DBG("%s, NULL Device Key", __func__); + continue; + } + + net_buf_simple_reset(sdu); + err = bt_mesh_app_decrypt(dev_key, true, aszmic, buf, + sdu, ad, rx->ctx.addr, + rx->ctx.recv_dst, seq, + BLE_MESH_NET_IVI_RX(rx)); + if (err) { + continue; + } + + rx->ctx.app_idx = BLE_MESH_KEY_DEV; + bt_mesh_model_recv(rx, sdu); + + bt_mesh_free_buf(sdu); + return 0; + } + + BT_WARN("%s, Unable to decrypt with DevKey", __func__); + bt_mesh_free_buf(sdu); + return -ENODEV; + } + + array_size = bt_mesh_rx_appkey_size(); + + for (i = 0U; i < array_size; i++) { + struct bt_mesh_app_keys *keys = NULL; + struct bt_mesh_app_key *key = NULL; + + key = bt_mesh_rx_appkey_get(i); + if (!key) { + BT_DBG("%s, NULL AppKey", __func__); + continue; + } + + /* Make sure that this AppKey matches received net_idx */ + if (key->net_idx != rx->sub->net_idx) { + continue; + } + + if (rx->new_key && key->updated) { + keys = &key->keys[1]; + } else { + keys = &key->keys[0]; + } + + /* Check that the AppKey ID matches */ + if (AID(&hdr) != keys->id) { + continue; + } + + net_buf_simple_reset(sdu); + err = bt_mesh_app_decrypt(keys->val, false, aszmic, buf, + sdu, ad, rx->ctx.addr, + rx->ctx.recv_dst, seq, + BLE_MESH_NET_IVI_RX(rx)); + if (err) { + BT_DBG("Unable to decrypt with AppKey 0x%03x", + key->app_idx); + continue; + } + + rx->ctx.app_idx = key->app_idx; + bt_mesh_model_recv(rx, sdu); + + bt_mesh_free_buf(sdu); + return 0; + } + + if (rx->local_match) { + BT_WARN("%s, No matching AppKey", __func__); + } + bt_mesh_free_buf(sdu); + return 0; +} + +static struct seg_tx *seg_tx_lookup(u16_t seq_zero, u8_t obo, u16_t addr) +{ + struct seg_tx *tx = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + tx = &seg_tx[i]; + + if ((tx->seq_auth & TRANS_SEQ_ZERO_MASK) != seq_zero) { + continue; + } + + if (tx->dst == addr) { + return tx; + } + + /* If the expected remote address doesn't match, + * but the OBO flag is set and this is the first + * acknowledgement, assume it's a Friend that's + * responding and therefore accept the message. + */ + if (obo && tx->nack_count == tx->seg_n + 1) { + tx->dst = addr; + return tx; + } + } + + return NULL; +} + +static int trans_ack(struct bt_mesh_net_rx *rx, u8_t hdr, + struct net_buf_simple *buf, u64_t *seq_auth) +{ + struct seg_tx *tx = NULL; + unsigned int bit = 0; + u32_t ack = 0U; + u16_t seq_zero = 0U; + u8_t obo = 0U; + + if (buf->len < 6) { + BT_ERR("%s, Too short ack message", __func__); + return -EINVAL; + } + + seq_zero = net_buf_simple_pull_be16(buf); + obo = seq_zero >> 15; + seq_zero = (seq_zero >> 2) & TRANS_SEQ_ZERO_MASK; + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match) { + BT_DBG("Ack for LPN 0x%04x of this Friend", rx->ctx.recv_dst); + /* Best effort - we don't have enough info for true SeqAuth */ + *seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(rx), seq_zero); + return 0; + } + + ack = net_buf_simple_pull_be32(buf); + + BT_DBG("OBO %u seq_zero 0x%04x ack 0x%08x", obo, seq_zero, ack); + + tx = seg_tx_lookup(seq_zero, obo, rx->ctx.addr); + if (!tx) { + BT_WARN("No matching TX context for ack"); + return -EINVAL; + } + + if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst)) { + BT_WARN("%s, Received ack for segments to group", __func__); + return -EINVAL; + } + + *seq_auth = tx->seq_auth; + + if (!ack) { + BT_WARN("SDU canceled"); + seg_tx_complete(tx, -ECANCELED); + return 0; + } + + if (find_msb_set(ack) - 1 > tx->seg_n) { + BT_ERR("%s, Too large segment number in ack", __func__); + return -EINVAL; + } + + k_delayed_work_cancel(&tx->retransmit); + + while ((bit = find_lsb_set(ack))) { + if (tx->seg[bit - 1]) { + BT_DBG("seg %u/%u acked", bit - 1, tx->seg_n); + bt_mesh_tx_seg_lock(); + seg_tx_done(tx, bit - 1); + bt_mesh_tx_seg_unlock(); + } + + ack &= ~BIT(bit - 1); + } + + if (tx->nack_count) { + seg_tx_send_unacked(tx); + } else { + BT_DBG("SDU TX complete"); + seg_tx_complete(tx, 0); + } + + return 0; +} + +static int trans_heartbeat(struct bt_mesh_net_rx *rx, + struct net_buf_simple *buf) +{ + u8_t init_ttl = 0U, hops = 0U; + u16_t feat = 0U; + + if (buf->len < 3) { + BT_ERR("%s, Too short heartbeat message", __func__); + return -EINVAL; + } + + if (rx->ctx.recv_dst != hb_sub_dst) { + BT_WARN("Ignoring heartbeat to non-subscribed destination"); + return 0; + } + + init_ttl = (net_buf_simple_pull_u8(buf) & 0x7f); + feat = net_buf_simple_pull_be16(buf); + + hops = (init_ttl - rx->ctx.recv_ttl + 1); + + BT_INFO("src 0x%04x TTL %u InitTTL %u (%u hop%s) feat 0x%04x", + rx->ctx.addr, rx->ctx.recv_ttl, init_ttl, hops, + (hops == 1U) ? "" : "s", feat); + + bt_mesh_heartbeat(rx->ctx.addr, rx->ctx.recv_dst, hops, feat); + + return 0; +} + +static int ctl_recv(struct bt_mesh_net_rx *rx, u8_t hdr, + struct net_buf_simple *buf, u64_t *seq_auth) +{ + u8_t ctl_op = TRANS_CTL_OP(&hdr); + + BT_DBG("OpCode 0x%02x len %u", ctl_op, buf->len); + + switch (ctl_op) { + case TRANS_CTL_OP_ACK: + return trans_ack(rx, hdr, buf, seq_auth); + case TRANS_CTL_OP_HEARTBEAT: + return trans_heartbeat(rx, buf); + } + + /* Only acks and heartbeats may need processing without local_match */ + if (!rx->local_match) { + return 0; + } + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !bt_mesh_lpn_established()) { + switch (ctl_op) { + case TRANS_CTL_OP_FRIEND_POLL: + return bt_mesh_friend_poll(rx, buf); + case TRANS_CTL_OP_FRIEND_REQ: + return bt_mesh_friend_req(rx, buf); + case TRANS_CTL_OP_FRIEND_CLEAR: + return bt_mesh_friend_clear(rx, buf); + case TRANS_CTL_OP_FRIEND_CLEAR_CFM: + return bt_mesh_friend_clear_cfm(rx, buf); + case TRANS_CTL_OP_FRIEND_SUB_ADD: + return bt_mesh_friend_sub_add(rx, buf); + case TRANS_CTL_OP_FRIEND_SUB_REM: + return bt_mesh_friend_sub_rem(rx, buf); + } + } + +#if defined(CONFIG_BLE_MESH_LOW_POWER) + if (ctl_op == TRANS_CTL_OP_FRIEND_OFFER) { + return bt_mesh_lpn_friend_offer(rx, buf); + } + + if (rx->ctx.addr == bt_mesh.lpn.frnd) { + if (ctl_op == TRANS_CTL_OP_FRIEND_CLEAR_CFM) { + return bt_mesh_lpn_friend_clear_cfm(rx, buf); + } + + if (!rx->friend_cred) { + BT_WARN("Message from friend with wrong credentials"); + return -EINVAL; + } + + switch (ctl_op) { + case TRANS_CTL_OP_FRIEND_UPDATE: + return bt_mesh_lpn_friend_update(rx, buf); + case TRANS_CTL_OP_FRIEND_SUB_CFM: + return bt_mesh_lpn_friend_sub_cfm(rx, buf); + } + } +#endif /* CONFIG_BLE_MESH_LOW_POWER */ + + BT_WARN("Unhandled TransOpCode 0x%02x", ctl_op); + + return -ENOENT; +} + +static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx, + u64_t *seq_auth) +{ + u8_t hdr = 0U; + + BT_DBG("AFK %u AID 0x%02x", AKF(buf->data), AID(buf->data)); + + if (buf->len < 1) { + BT_ERR("%s, Too small unsegmented PDU", __func__); + return -EINVAL; + } + + if (is_replay(rx, NULL)) { + BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", + rx->ctx.addr, rx->ctx.recv_dst, rx->seq); + return -EINVAL; + } + + hdr = net_buf_simple_pull_u8(buf); + + if (rx->ctl) { + return ctl_recv(rx, hdr, buf, seq_auth); + } else { + /* SDUs must match a local element or an LPN of this Friend. */ + if (!rx->local_match && !rx->friend_match) { + return 0; + } + + return sdu_recv(rx, rx->seq, hdr, 0, buf); + } +} + +static inline s32_t ack_timeout(struct seg_rx *rx) +{ + s32_t to = 0; + u8_t ttl = 0U; + + if (rx->ttl == BLE_MESH_TTL_DEFAULT) { + ttl = bt_mesh_default_ttl_get(); + } else { + ttl = rx->ttl; + } + + /* The acknowledgment timer shall be set to a minimum of + * 150 + 50 * TTL milliseconds. + */ + to = K_MSEC(150 + (ttl * 50U)); + + /* 100 ms for every not yet received segment */ + to += K_MSEC(((rx->seg_n + 1) - popcount(rx->block)) * 100U); + + /* Make sure we don't send more frequently than the duration for + * each packet (default is 300ms). + */ + return MAX(to, K_MSEC(400)); +} + +static int ctl_send_unseg(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, + size_t data_len, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + struct net_buf *buf = NULL; + + buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, BUF_TIMEOUT); + if (!buf) { + BT_ERR("%s, Out of transport buffers", __func__); + return -ENOBUFS; + } + + net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN); + + net_buf_add_u8(buf, TRANS_CTL_HDR(ctl_op, 0)); + + net_buf_add_mem(buf, data, data_len); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE, + NULL, 1, &buf->b) && + BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) { + /* PDUs for a specific Friend should only go + * out through the Friend Queue. + */ + net_buf_unref(buf); + return 0; + } + } + + return bt_mesh_net_send(tx, buf, cb, cb_data); +} + +static int ctl_send_seg(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, + size_t data_len, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + struct seg_tx *tx_seg = NULL; + u16_t unsent = data_len; + u16_t seq_zero = 0; + u8_t seg_o = 0; + int i; + + for (tx_seg = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) { + if (!seg_tx[i].nack_count) { + tx_seg = &seg_tx[i]; + break; + } + } + + if (!tx_seg) { + BT_ERR("%s, No multi-segment message contexts available", __func__); + return -EBUSY; + } + + tx_seg->dst = tx->ctx->addr; + tx_seg->seg_n = (data_len - 1) / BLE_MESH_CTL_SEG_SDU_MAX; + tx_seg->nack_count = tx_seg->seg_n + 1; + tx_seg->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq); + tx_seg->sub = tx->sub; + tx_seg->new_key = tx->sub->kr_flag; + tx_seg->attempts = SEG_RETRANSMIT_ATTEMPTS; + tx_seg->seg_pending = 0; + tx_seg->cb = cb; + tx_seg->cb_data = cb_data; + + if (tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) { + tx_seg->ttl = bt_mesh_default_ttl_get(); + } else { + tx_seg->ttl = tx->ctx->send_ttl; + } + + seq_zero = tx_seg->seq_auth & TRANS_SEQ_ZERO_MASK; + + BT_DBG("SeqZero 0x%04x", seq_zero); + + for (seg_o = 0; seg_o <= tx_seg->seg_n; seg_o++) { + struct net_buf *seg = NULL; + u16_t len = 0; + int err = 0; + + seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, tx->xmit, + BUF_TIMEOUT); + if (!seg) { + BT_ERR("%s, Out of segment buffers", __func__); + seg_tx_reset(tx_seg); + return -ENOBUFS; + } + + net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN); + + net_buf_add_u8(seg, TRANS_CTL_HDR(ctl_op, 1)); + net_buf_add_u8(seg, (tx->aszmic << 7) | seq_zero >> 6); + net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | (seg_o >> 3))); + net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx_seg->seg_n); + + len = MIN(unsent, BLE_MESH_CTL_SEG_SDU_MAX); + net_buf_add_mem(seg, (u8_t *)data + (data_len - unsent), len); + unsent -= len; + + tx_seg->seg[seg_o] = net_buf_ref(seg); + + BT_DBG("Sending %u/%u", seg_o, tx_seg->seg_n); + tx_seg->seg_pending++; + + err = bt_mesh_net_send(tx, seg, + seg_o ? &seg_sent_cb : &first_sent_cb, + tx_seg); + if (err) { + BT_ERR("%s, Sending segment failed", __func__); + seg_tx_reset(tx_seg); + return err; + } + } + + return 0; +} + +int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, + size_t data_len, const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + BT_DBG("src 0x%04x dst 0x%04x ttl 0x%02x ctl 0x%02x", tx->src, + tx->ctx->addr, tx->ctx->send_ttl, ctl_op); + BT_DBG("len %zu: %s", data_len, bt_hex(data, data_len)); + + if (data_len <= BLE_MESH_SDU_UNSEG_MAX) { + return ctl_send_unseg(tx, ctl_op, data, data_len, + cb, cb_data); + } else { + return ctl_send_seg(tx, ctl_op, data, data_len, + cb, cb_data); + } +} + +static int send_ack(struct bt_mesh_subnet *sub, u16_t src, u16_t dst, + u8_t ttl, u64_t *seq_auth, u32_t block, u8_t obo) +{ + struct bt_mesh_msg_ctx ctx = { + .net_idx = sub->net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = dst, + .send_ttl = ttl, + }; + struct bt_mesh_net_tx tx = { + .sub = sub, + .ctx = &ctx, + .src = obo ? bt_mesh_primary_addr() : src, + .xmit = bt_mesh_net_transmit_get(), + }; + u16_t seq_zero = *seq_auth & TRANS_SEQ_ZERO_MASK; + u8_t buf[6] = {0}; + + BT_DBG("SeqZero 0x%04x Block 0x%08x OBO %u", seq_zero, block, obo); + + if (bt_mesh_lpn_established()) { + BT_WARN("Not sending ack when LPN is enabled"); + return 0; + } + + /* This can happen if the segmented message was destined for a group + * or virtual address. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + BT_INFO("Not sending ack for non-unicast address"); + return 0; + } + + sys_put_be16(((seq_zero << 2) & 0x7ffc) | (obo << 15), buf); + sys_put_be32(block, &buf[2]); + + return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_ACK, buf, sizeof(buf), + NULL, NULL); +} + +static void seg_rx_reset(struct seg_rx *rx, bool full_reset) +{ + BT_DBG("rx %p", rx); + + bt_mesh_rx_seg_lock(); + + k_delayed_work_cancel(&rx->ack); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->obo && + rx->block != BLOCK_COMPLETE(rx->seg_n)) { + BT_WARN("Clearing incomplete buffers from Friend queue"); + bt_mesh_friend_clear_incomplete(rx->sub, rx->src, rx->dst, + &rx->seq_auth); + } + + rx->in_use = 0U; + + /* We don't always reset these values since we need to be able to + * send an ack if we receive a segment after we've already received + * the full SDU. + */ + if (full_reset) { + rx->seq_auth = 0U; + rx->sub = NULL; + rx->src = BLE_MESH_ADDR_UNASSIGNED; + rx->dst = BLE_MESH_ADDR_UNASSIGNED; + } + + bt_mesh_rx_seg_unlock(); +} + +static u32_t incomplete_timeout(struct seg_rx *rx) +{ + u32_t timeout = 0U; + u8_t ttl = 0U; + + if (rx->ttl == BLE_MESH_TTL_DEFAULT) { + ttl = bt_mesh_default_ttl_get(); + } else { + ttl = rx->ttl; + } + + /* "The incomplete timer shall be set to a minimum of 10 seconds." */ + timeout = K_SECONDS(10); + + /* The less segments being received, the shorter timeout will be used. */ + timeout += K_MSEC(ttl * popcount(rx->block) * 100U); + + return MIN(timeout, K_SECONDS(60)); +} + +static void seg_ack(struct k_work *work) +{ + struct seg_rx *rx = CONTAINER_OF(work, struct seg_rx, ack); + + BT_DBG("rx %p", rx); + + bt_mesh_rx_seg_lock(); + + if (k_uptime_get_32() - rx->last > incomplete_timeout(rx)) { + BT_WARN("Incomplete timer expired"); + bt_mesh_rx_seg_unlock(); + seg_rx_reset(rx, false); + return; + } + + /* Add this check in case the timeout handler is just executed + * after the seg_rx_reset() which may reset rx->sub to NULL. + */ + if (rx->sub == NULL) { + bt_mesh_rx_seg_unlock(); + return; + } + + send_ack(rx->sub, rx->dst, rx->src, rx->ttl, &rx->seq_auth, + rx->block, rx->obo); + + k_delayed_work_submit(&rx->ack, ack_timeout(rx)); + + bt_mesh_rx_seg_unlock(); +} + +static inline u8_t seg_len(bool ctl) +{ + if (ctl) { + return BLE_MESH_CTL_SEG_SDU_MAX; + } else { + return BLE_MESH_APP_SEG_SDU_MAX; + } +} + +static inline bool sdu_len_is_ok(bool ctl, u8_t seg_n) +{ + return ((seg_n * seg_len(ctl) + 1) <= CONFIG_BLE_MESH_RX_SDU_MAX); +} + +static struct seg_rx *seg_rx_find(struct bt_mesh_net_rx *net_rx, + const u64_t *seq_auth) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + struct seg_rx *rx = &seg_rx[i]; + + if (rx->src != net_rx->ctx.addr || + rx->dst != net_rx->ctx.recv_dst) { + continue; + } + + /* Return newer RX context in addition to an exact match, so + * the calling function can properly discard an old SeqAuth. + * Note: in Zephyr v1.14.0, ">=" is used here which does not + * seem to be a right operation, hence we still use the original + * "==" here. + */ + if (rx->seq_auth == *seq_auth) { + return rx; + } + + if (rx->in_use) { + BT_WARN("Duplicate SDU from src 0x%04x", + net_rx->ctx.addr); + + /* Clear out the old context since the sender + * has apparently started sending a new SDU. + */ + seg_rx_reset(rx, true); + + /* Return non-match so caller can re-allocate */ + return NULL; + } + } + + return NULL; +} + +static bool seg_rx_is_valid(struct seg_rx *rx, struct bt_mesh_net_rx *net_rx, + const u8_t *hdr, u8_t seg_n) +{ + if (rx->hdr != *hdr || rx->seg_n != seg_n) { + BT_ERR("%s, Invalid segment for ongoing session", __func__); + return false; + } + + if (rx->src != net_rx->ctx.addr || rx->dst != net_rx->ctx.recv_dst) { + BT_ERR("%s, Invalid source or destination for segment", __func__); + return false; + } + + if (rx->ctl != net_rx->ctl) { + BT_ERR("%s, Inconsistent CTL in segment", __func__); + return false; + } + + return true; +} + +static struct seg_rx *seg_rx_alloc(struct bt_mesh_net_rx *net_rx, + const u8_t *hdr, const u64_t *seq_auth, + u8_t seg_n) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + struct seg_rx *rx = &seg_rx[i]; + + if (rx->in_use) { + continue; + } + + rx->in_use = 1U; + net_buf_simple_reset(&rx->buf); + rx->sub = net_rx->sub; + rx->ctl = net_rx->ctl; + rx->seq_auth = *seq_auth; + rx->seg_n = seg_n; + rx->hdr = *hdr; + rx->ttl = net_rx->ctx.send_ttl; + rx->src = net_rx->ctx.addr; + rx->dst = net_rx->ctx.recv_dst; + rx->block = 0U; + + BT_DBG("New RX context. Block Complete 0x%08x", + BLOCK_COMPLETE(seg_n)); + + return rx; + } + + return NULL; +} + +static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx, + enum bt_mesh_friend_pdu_type *pdu_type, u64_t *seq_auth, + u8_t *seg_count) +{ + struct bt_mesh_rpl *rpl = NULL; + struct seg_rx *rx = NULL; + u8_t *hdr = buf->data; + u16_t seq_zero = 0U; + u8_t seg_n = 0U; + u8_t seg_o = 0U; + int err = 0; + + if (buf->len < 5) { + BT_ERR("%s, Too short segmented message (len %u)", __func__, buf->len); + return -EINVAL; + } + + if (is_replay(net_rx, &rpl)) { + BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", + net_rx->ctx.addr, net_rx->ctx.recv_dst, net_rx->seq); + return -EINVAL; + } + + BT_DBG("ASZMIC %u AKF %u AID 0x%02x", ASZMIC(hdr), AKF(hdr), AID(hdr)); + + net_buf_simple_pull(buf, 1); + + seq_zero = net_buf_simple_pull_be16(buf); + seg_o = (seq_zero & 0x03) << 3; + seq_zero = (seq_zero >> 2) & TRANS_SEQ_ZERO_MASK; + seg_n = net_buf_simple_pull_u8(buf); + seg_o |= seg_n >> 5; + seg_n &= 0x1f; + + BT_DBG("SeqZero 0x%04x SegO %u SegN %u", seq_zero, seg_o, seg_n); + + if (seg_o > seg_n) { + BT_ERR("%s, SegO greater than SegN (%u > %u)", __func__, seg_o, seg_n); + return -EINVAL; + } + + /* According to Mesh 1.0 specification: + * "The SeqAuth is composed of the IV Index and the sequence number + * (SEQ) of the first segment" + * + * Therefore we need to calculate very first SEQ in order to find + * seqAuth. We can calculate as below: + * + * SEQ(0) = SEQ(n) - (delta between seqZero and SEQ(n) by looking into + * 14 least significant bits of SEQ(n)) + * + * Mentioned delta shall be >= 0, if it is not then seq_auth will + * be broken and it will be verified by the code below. + */ + *seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(net_rx), + (net_rx->seq - + ((((net_rx->seq & BIT_MASK(14)) - seq_zero)) & + BIT_MASK(13)))); + + *seg_count = seg_n + 1; + + /* Look for old RX sessions */ + rx = seg_rx_find(net_rx, seq_auth); + if (rx) { + /* Discard old SeqAuth packet */ + if (rx->seq_auth > *seq_auth) { + BT_WARN("Ignoring old SeqAuth"); + return -EINVAL; + } + + if (!seg_rx_is_valid(rx, net_rx, hdr, seg_n)) { + return -EINVAL; + } + + if (rx->in_use) { + BT_DBG("Existing RX context. Block 0x%08x", rx->block); + goto found_rx; + } + + if (rx->block == BLOCK_COMPLETE(rx->seg_n)) { + BT_INFO("Got segment for already complete SDU"); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, + net_rx->ctx.addr, net_rx->ctx.send_ttl, + seq_auth, rx->block, rx->obo); + + if (rpl) { + update_rpl(rpl, net_rx); + } + + return -EALREADY; + } + + /* We ignore instead of sending block ack 0 since the + * ack timer is always smaller than the incomplete + * timer, i.e. the sender is misbehaving. + */ + BT_WARN("Got segment for canceled SDU"); + return -EINVAL; + } + + /* Bail out early if we're not ready to receive such a large SDU */ + if (!sdu_len_is_ok(net_rx->ctl, seg_n)) { + BT_ERR("%s, Too big incoming SDU length", __func__); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, + net_rx->ctx.send_ttl, seq_auth, 0, + net_rx->friend_match); + return -EMSGSIZE; + } + + /* Verify early that there will be space in the Friend Queue(s) in + * case this message is destined to an LPN of ours. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && + net_rx->friend_match && !net_rx->local_match && + !bt_mesh_friend_queue_has_space(net_rx->sub->net_idx, + net_rx->ctx.addr, + net_rx->ctx.recv_dst, seq_auth, + *seg_count)) { + BT_ERR("No space in Friend Queue for %u segments", *seg_count); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, + net_rx->ctx.send_ttl, seq_auth, 0, + net_rx->friend_match); + return -ENOBUFS; + } + + /* Look for free slot for a new RX session */ + rx = seg_rx_alloc(net_rx, hdr, seq_auth, seg_n); + if (!rx) { + /* Warn but don't cancel since the existing slots will + * eventually be freed up and we'll be able to process + * this one. + */ + BT_WARN("No free slots for new incoming segmented messages"); + return -ENOMEM; + } + + rx->obo = net_rx->friend_match; + +found_rx: + if (BIT(seg_o) & rx->block) { + BT_WARN("Received already received fragment"); + return -EALREADY; + } + + /* All segments, except the last one, must either have 8 bytes of + * payload (for 64bit Net MIC) or 12 bytes of payload (for 32bit + * Net MIC). + */ + if (seg_o == seg_n) { + /* Set the expected final buffer length */ + rx->buf.len = seg_n * seg_len(rx->ctl) + buf->len; + BT_DBG("Target len %u * %u + %u = %u", seg_n, seg_len(rx->ctl), + buf->len, rx->buf.len); + + if (rx->buf.len > CONFIG_BLE_MESH_RX_SDU_MAX) { + BT_ERR("Too large SDU len"); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, + net_rx->ctx.addr, net_rx->ctx.send_ttl, + seq_auth, 0, rx->obo); + seg_rx_reset(rx, true); + return -EMSGSIZE; + } + } else { + if (buf->len != seg_len(rx->ctl)) { + BT_ERR("%s, Incorrect segment size for message type", __func__); + return -EINVAL; + } + } + + /* Reset the Incomplete Timer */ + rx->last = k_uptime_get_32(); + + if (!k_delayed_work_remaining_get(&rx->ack) && + !bt_mesh_lpn_established()) { + k_delayed_work_submit(&rx->ack, ack_timeout(rx)); + } + + /* Location in buffer can be calculated based on seg_o & rx->ctl */ + memcpy(rx->buf.data + (seg_o * seg_len(rx->ctl)), buf->data, buf->len); + + BT_INFO("Received %u/%u", seg_o, seg_n); + + /* Mark segment as received */ + rx->block |= BIT(seg_o); + + if (rx->block != BLOCK_COMPLETE(seg_n)) { + *pdu_type = BLE_MESH_FRIEND_PDU_PARTIAL; + return 0; + } + + BT_DBG("Complete SDU"); + + if (rpl) { + update_rpl(rpl, net_rx); + } + + *pdu_type = BLE_MESH_FRIEND_PDU_COMPLETE; + + k_delayed_work_cancel(&rx->ack); + send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr, + net_rx->ctx.send_ttl, seq_auth, rx->block, rx->obo); + + if (net_rx->ctl) { + err = ctl_recv(net_rx, *hdr, &rx->buf, seq_auth); + } else { + err = sdu_recv(net_rx, (rx->seq_auth & 0xffffff), *hdr, + ASZMIC(hdr), &rx->buf); + } + + seg_rx_reset(rx, false); + + return err; +} + +int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx) +{ + u64_t seq_auth = TRANS_SEQ_AUTH_NVAL; + enum bt_mesh_friend_pdu_type pdu_type = BLE_MESH_FRIEND_PDU_SINGLE; + struct net_buf_simple_state state = {0}; + u8_t seg_count = 0U; + int err = 0; + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + rx->friend_match = bt_mesh_friend_match(rx->sub->net_idx, + rx->ctx.recv_dst); + } else { + rx->friend_match = false; + } + + BT_DBG("src 0x%04x dst 0x%04x seq 0x%08x friend_match %u", + rx->ctx.addr, rx->ctx.recv_dst, rx->seq, rx->friend_match); + + /* Remove network headers */ + net_buf_simple_pull(buf, BLE_MESH_NET_HDR_LEN); + + BT_DBG("Payload %s", bt_hex(buf->data, buf->len)); + + /* If LPN mode is enabled messages are only accepted when we've + * requested the Friend to send them. The messages must also + * be encrypted using the Friend Credentials. + */ + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + bt_mesh_lpn_established() && rx->net_if == BLE_MESH_NET_IF_ADV && + (!bt_mesh_lpn_waiting_update() || !rx->friend_cred)) { + BT_WARN("Ignoring unexpected message in Low Power mode"); + return -EAGAIN; + } + + /* Save the app-level state so the buffer can later be placed in + * the Friend Queue. + */ + net_buf_simple_save(buf, &state); + + if (SEG(buf->data)) { + /* Segmented messages must match a local element or an + * LPN of this Friend. + */ + if (!rx->local_match && !rx->friend_match) { + return 0; + } + + err = trans_seg(buf, rx, &pdu_type, &seq_auth, &seg_count); + } else { + seg_count = 1U; + err = trans_unseg(buf, rx, &seq_auth); + } + + /* Notify LPN state machine so a Friend Poll will be sent. If the + * message was a Friend Update it's possible that a Poll was already + * queued for sending, however that's fine since then the + * bt_mesh_lpn_waiting_update() function will return false: + * we still need to go through the actual sending to the bearer and + * wait for ReceiveDelay before transitioning to WAIT_UPDATE state. + * Another situation where we want to notify the LPN state machine + * is if it's configured to use an automatic Friendship establishment + * timer, in which case we want to reset the timer at this point. + * + */ + if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && + (bt_mesh_lpn_timer() || + (bt_mesh_lpn_established() && bt_mesh_lpn_waiting_update()))) { + bt_mesh_lpn_msg_received(rx); + } + + net_buf_simple_restore(buf, &state); + + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match && !err) { + if (seq_auth == TRANS_SEQ_AUTH_NVAL) { + bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL, + seg_count, buf); + } else { + bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth, + seg_count, buf); + } + } + + return err; +} + +void bt_mesh_rx_reset(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + seg_rx_reset(&seg_rx[i], true); + } + + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl(); + } else { + (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); + } +} + +void bt_mesh_tx_reset(void) +{ + int i; + + BT_DBG("%s", __func__); + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + seg_tx_reset(&seg_tx[i]); + } +} + +#if CONFIG_BLE_MESH_PROVISIONER +void bt_mesh_rx_reset_single(u16_t src) +{ + int i; + + if (!BLE_MESH_ADDR_IS_UNICAST(src)) { + return; + } + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + struct seg_rx *rx = &seg_rx[i]; + if (src == rx->src) { + seg_rx_reset(rx, true); + } + } + + for (i = 0; i < ARRAY_SIZE(bt_mesh.rpl); i++) { + struct bt_mesh_rpl *rpl = &bt_mesh.rpl[i]; + if (src == rpl->src) { + memset(rpl, 0, sizeof(struct bt_mesh_rpl)); + if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl_single(src); + } + } + } +} + +void bt_mesh_tx_reset_single(u16_t dst) +{ + int i; + + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + return; + } + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + struct seg_tx *tx = &seg_tx[i]; + if (dst == tx->dst) { + seg_tx_reset(tx); + } + } +} +#endif /* CONFIG_BLE_MESH_PROVISIONER */ + +void bt_mesh_trans_init(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + k_delayed_work_init(&seg_tx[i].retransmit, seg_retransmit); + } + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + k_delayed_work_init(&seg_rx[i].ack, seg_ack); + seg_rx[i].buf.__buf = (seg_rx_buf_data + + (i * CONFIG_BLE_MESH_RX_SDU_MAX)); + seg_rx[i].buf.data = seg_rx[i].buf.__buf; + } + + bt_mesh_tx_seg_mutex_new(); + bt_mesh_rx_seg_mutex_new(); +} + +void bt_mesh_trans_deinit(bool erase) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + seg_rx_reset(&seg_rx[i], true); + } + + if (erase && IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { + bt_mesh_clear_rpl(); + } else { + bt_mesh_rpl_clear(); + } + + bt_mesh_tx_reset(); + + for (i = 0; i < ARRAY_SIZE(seg_tx); i++) { + k_delayed_work_free(&seg_tx[i].retransmit); + } + + for (i = 0; i < ARRAY_SIZE(seg_rx); i++) { + k_delayed_work_free(&seg_rx[i].ack); + } + + bt_mesh_tx_seg_mutex_free(); + bt_mesh_rx_seg_mutex_free(); +} + +void bt_mesh_rpl_clear(void) +{ + BT_DBG("%s", __func__); + (void)memset(bt_mesh.rpl, 0, sizeof(bt_mesh.rpl)); +} + +void bt_mesh_heartbeat_send(void) +{ + struct bt_mesh_cfg_srv *cfg = bt_mesh_cfg_get(); + u16_t feat = 0U; + struct __packed { + u8_t init_ttl; + u16_t feat; + } hb; + struct bt_mesh_msg_ctx ctx = { + .net_idx = cfg->hb_pub.net_idx, + .app_idx = BLE_MESH_KEY_UNUSED, + .addr = cfg->hb_pub.dst, + .send_ttl = cfg->hb_pub.ttl, + }; + struct bt_mesh_net_tx tx = { + .sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx), + .ctx = &ctx, + .src = bt_mesh_model_elem(cfg->model)->addr, + .xmit = bt_mesh_net_transmit_get(), + }; + + /* Do nothing if heartbeat publication is not enabled */ + if (cfg->hb_pub.dst == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + + hb.init_ttl = cfg->hb_pub.ttl; + + if (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED) { + feat |= BLE_MESH_FEAT_RELAY; + } + + if (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED) { + feat |= BLE_MESH_FEAT_PROXY; + } + + if (bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED) { + feat |= BLE_MESH_FEAT_FRIEND; + } + + if (bt_mesh_lpn_established()) { + feat |= BLE_MESH_FEAT_LOW_POWER; + } + + hb.feat = sys_cpu_to_be16(feat); + + BT_INFO("InitTTL %u feat 0x%04x", cfg->hb_pub.ttl, feat); + + bt_mesh_ctl_send(&tx, TRANS_CTL_OP_HEARTBEAT, &hb, sizeof(hb), + NULL, NULL); +} + +int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, + const u8_t **key, u8_t *aid, u8_t role, u16_t dst) +{ + struct bt_mesh_app_key *app_key = NULL; + + if (app_idx == BLE_MESH_KEY_DEV) { + *key = bt_mesh_tx_devkey_get(role, dst); + if (!*key) { + BT_ERR("%s, Failed to get Device Key", __func__); + return -EINVAL; + } + + *aid = 0U; + return 0; + } + + if (!subnet) { + BT_ERR("%s, Invalid subnet", __func__); + return -EINVAL; + } + + app_key = bt_mesh_tx_appkey_get(role, app_idx); + if (!app_key) { + BT_ERR("%s, AppKey 0x%04x not exists", __func__, app_idx); + return -ENOENT; + } + + if (subnet->kr_phase == BLE_MESH_KR_PHASE_2 && app_key->updated) { + *key = app_key->keys[1].val; + *aid = app_key->keys[1].id; + } else { + *key = app_key->keys[0].val; + *aid = app_key->keys[0].id; + } + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_core/transport.h b/components/bt/esp_ble_mesh/mesh_core/transport.h new file mode 100644 index 000000000..ac80273b0 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_core/transport.h @@ -0,0 +1,128 @@ +/* Bluetooth Mesh */ + +/* + * Copyright (c) 2017 Intel Corporation + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _TRANSPORT_H_ +#define _TRANSPORT_H_ + +#include "net.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define TRANS_SEQ_AUTH_NVAL 0xffffffffffffffff + +#define BLE_MESH_SDU_UNSEG_MAX 11 +#define BLE_MESH_CTL_SEG_SDU_MAX 8 +#define BLE_MESH_APP_SEG_SDU_MAX 12 +#define BLE_MESH_TX_SDU_MAX (CONFIG_BLE_MESH_TX_SEG_MAX * 12) + +#define TRANS_SEQ_ZERO_MASK ((u16_t)BIT_MASK(13)) +#define TRANS_CTL_OP_MASK ((u8_t)BIT_MASK(7)) +#define TRANS_CTL_OP(data) ((data)[0] & TRANS_CTL_OP_MASK) +#define TRANS_CTL_HDR(op, seg) ((op & TRANS_CTL_OP_MASK) | (seg << 7)) + +#define TRANS_CTL_OP_ACK 0x00 +#define TRANS_CTL_OP_FRIEND_POLL 0x01 +#define TRANS_CTL_OP_FRIEND_UPDATE 0x02 +#define TRANS_CTL_OP_FRIEND_REQ 0x03 +#define TRANS_CTL_OP_FRIEND_OFFER 0x04 +#define TRANS_CTL_OP_FRIEND_CLEAR 0x05 +#define TRANS_CTL_OP_FRIEND_CLEAR_CFM 0x06 +#define TRANS_CTL_OP_FRIEND_SUB_ADD 0x07 +#define TRANS_CTL_OP_FRIEND_SUB_REM 0x08 +#define TRANS_CTL_OP_FRIEND_SUB_CFM 0x09 +#define TRANS_CTL_OP_HEARTBEAT 0x0a + +struct bt_mesh_ctl_friend_poll { + u8_t fsn; +} __packed; + +struct bt_mesh_ctl_friend_update { + u8_t flags; + u32_t iv_index; + u8_t md; +} __packed; + +struct bt_mesh_ctl_friend_req { + u8_t criteria; + u8_t recv_delay; + u8_t poll_to[3]; + u16_t prev_addr; + u8_t num_elem; + u16_t lpn_counter; +} __packed; + +struct bt_mesh_ctl_friend_offer { + u8_t recv_win; + u8_t queue_size; + u8_t sub_list_size; + s8_t rssi; + u16_t frnd_counter; +} __packed; + +struct bt_mesh_ctl_friend_clear { + u16_t lpn_addr; + u16_t lpn_counter; +} __packed; + +struct bt_mesh_ctl_friend_clear_confirm { + u16_t lpn_addr; + u16_t lpn_counter; +} __packed; + +#define BLE_MESH_FRIEND_SUB_MIN_LEN (1 + 2) +struct bt_mesh_ctl_friend_sub { + u8_t xact; + u16_t addr_list[5]; +} __packed; + +struct bt_mesh_ctl_friend_sub_confirm { + u8_t xact; +} __packed; + +u8_t bt_mesh_get_seg_retrans_num(void); + +s32_t bt_mesh_get_seg_retrans_timeout(u8_t ttl); + +void bt_mesh_set_hb_sub_dst(u16_t addr); + +struct bt_mesh_app_key *bt_mesh_app_key_find(u16_t app_idx); + +bool bt_mesh_tx_in_progress(void); + +void bt_mesh_rx_reset(void); +void bt_mesh_tx_reset(void); +void bt_mesh_rx_reset_single(u16_t src); +void bt_mesh_tx_reset_single(u16_t dst); + +int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, u8_t ctl_op, void *data, + size_t data_len, const struct bt_mesh_send_cb *cb, + void *cb_data); + +int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data); + +int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx); + +void bt_mesh_trans_init(void); +void bt_mesh_trans_deinit(bool erase); + +void bt_mesh_rpl_clear(void); + +void bt_mesh_heartbeat_send(void); + +int bt_mesh_app_key_get(const struct bt_mesh_subnet *subnet, u16_t app_idx, + const u8_t **key, u8_t *aid, u8_t role, u16_t dst); + +#ifdef __cplusplus +} +#endif + +#endif /* _TRANSPORT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/client_common.c b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c new file mode 100644 index 000000000..c976c2737 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/client_common.c @@ -0,0 +1,546 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "mesh.h" +#include "mesh_main.h" +#include "transport.h" +#include "foundation.h" +#include "client_common.h" +#include "mesh_common.h" + +#define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */ + +static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, u16_t tx_dst) +{ + bt_mesh_client_node_t *node = NULL; + sys_snode_t *cur = NULL; + + bt_mesh_list_lock(); + if (sys_slist_is_empty(list)) { + bt_mesh_list_unlock(); + return NULL; + } + + for (cur = sys_slist_peek_head(list); + cur != NULL; cur = sys_slist_peek_next(cur)) { + node = (bt_mesh_client_node_t *)cur; + if (node->ctx.addr == tx_dst) { + bt_mesh_list_unlock(); + return node; + } + } + + bt_mesh_list_unlock(); + return NULL; +} + +bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg( + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, bool need_pub) +{ + bt_mesh_client_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *cli = NULL; + bt_mesh_client_node_t *node = NULL; + + if (!model || !ctx || !buf) { + BT_ERR("%s, Invalid parameter", __func__); + return NULL; + } + + cli = (bt_mesh_client_user_data_t *)model->user_data; + if (!cli) { + BT_ERR("%s, Clinet user_data is NULL", __func__); + return NULL; + } + + /** If the received message address is not a unicast address, + * the address may be a group/virtual address, and we push + * this message to the application layer. + */ + if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + BT_DBG("Unexpected status message 0x%x", ctx->recv_op); + if (cli->publish_status && need_pub) { + cli->publish_status(ctx->recv_op, model, ctx, buf); + } + return NULL; + } + + /** If the source address of the received status message is + * different with the destination address of the sending + * message, then the message is from another element and + * push it to application layer. + */ + data = (bt_mesh_client_internal_data_t *)cli->internal_data; + if (!data) { + BT_ERR("%s, Client internal_data is NULL", __func__); + return NULL; + } + + if ((node = bt_mesh_client_pick_node(&data->queue, ctx->addr)) == NULL) { + BT_DBG("Unexpected status message 0x%x", ctx->recv_op); + if (cli->publish_status && need_pub) { + cli->publish_status(ctx->recv_op, model, ctx, buf); + } + return NULL; + } + + if (node->op_pending != ctx->recv_op) { + BT_DBG("Unexpected status message 0x%x", ctx->recv_op); + if (cli->publish_status && need_pub) { + cli->publish_status(ctx->recv_op, model, ctx, buf); + } + return NULL; + } + + if (k_delayed_work_remaining_get(&node->timer) == 0) { + BT_DBG("Unexpected status message 0x%x", ctx->recv_op); + if (cli->publish_status && need_pub) { + cli->publish_status(ctx->recv_op, model, ctx, buf); + } + return NULL; + } + + return node; +} + +static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, u16_t tx_dst) +{ + bt_mesh_client_node_t *node = NULL; + sys_snode_t *cur = NULL; + + bt_mesh_list_lock(); + if (sys_slist_is_empty(list)) { + bt_mesh_list_unlock(); + return false; + } + + for (cur = sys_slist_peek_head(list); + cur != NULL; cur = sys_slist_peek_next(cur)) { + node = (bt_mesh_client_node_t *)cur; + if (node->ctx.addr == tx_dst) { + bt_mesh_list_unlock(); + return true; + } + } + + bt_mesh_list_unlock(); + return false; +} + +static u32_t bt_mesh_client_get_status_op(const bt_mesh_client_op_pair_t *op_pair, + int size, u32_t opcode) +{ + if (!op_pair || size == 0) { + return 0; + } + + const bt_mesh_client_op_pair_t *op = op_pair; + for (int i = 0; i < size; i++) { + if (op->cli_op == opcode) { + return op->status_op; + } + op++; + } + + return 0; +} + +static s32_t bt_mesh_get_adv_duration(void) +{ + u16_t duration, adv_int; + u8_t xmit; + + xmit = bt_mesh_net_transmit_get(); /* Network transmit */ + adv_int = BLE_MESH_TRANSMIT_INT(xmit); + duration = (BLE_MESH_TRANSMIT_COUNT(xmit) + 1) * (adv_int + 10); + + return (s32_t)duration; +} + +static s32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *msg, + u32_t opcode, s32_t timeout) +{ + s32_t seg_retrans_to = 0, duration = 0, time = 0; + u8_t seg_count = 0, seg_retrans_num = 0; + bool need_seg = false; + u8_t mic_size = 0; + + if (msg->len > BLE_MESH_SDU_UNSEG_MAX || ctx->send_rel) { + need_seg = true; /* Needs segmentation */ + } + + mic_size = (need_seg && net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ? + BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT; + + if (need_seg) { + /* Based on the message length, calculate how many segments are needed. + * All the messages sent from here are access messages. + */ + seg_retrans_num = bt_mesh_get_seg_retrans_num(); + seg_retrans_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl); + seg_count = (msg->len + mic_size - 1) / 12U + 1U; + + duration = bt_mesh_get_adv_duration(); + + /* Currenlty only consider the time consumption of the same segmented + * messages, but if there are other messages between any two retrans- + * missions of the same segmented messages, then the whole time will + * be longer. + * + * Since the transport behavior has been changed, i.e. start retransmit + * timer after the last segment is sent, so we can simplify the timeout + * calculation here. And the retransmit timer will be started event if + * the attempts reaches ZERO when the dst is a unicast address. + */ + s32_t seg_duration = seg_count * (duration + HCI_TIME_FOR_START_ADV); + time = (seg_duration + seg_retrans_to) * seg_retrans_num; + + BT_INFO("Original timeout %dms, calculated timeout %dms", timeout, time); + + if (time < timeout) { + /* If the calculated time is smaller than the input timeout value, + * then use the original timeout value. + */ + time = timeout; + } + } else { + /* For unsegmented access messages, directly use the timeout + * value from the application layer. + */ + time = timeout; + } + + BT_INFO("Client message 0x%08x with timeout %dms", opcode, time); + + return time; +} + +static void msg_send_start(u16_t duration, int err, void *cb_data) +{ + bt_mesh_client_node_t *node = cb_data; + + BT_DBG("%s, duration %ums", __func__, duration); + + if (err) { + if (!k_delayed_work_free(&node->timer)) { + bt_mesh_client_free_node(node); + } + return; + } + + k_delayed_work_submit(&node->timer, node->timeout); +} + +static const struct bt_mesh_send_cb send_cb = { + .start = msg_send_start, + .end = NULL, +}; + +int bt_mesh_client_send_msg(struct bt_mesh_model *model, + u32_t opcode, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *msg, + k_work_handler_t timer_handler, + s32_t timeout, bool need_ack, + const struct bt_mesh_send_cb *cb, + void *cb_data) +{ + bt_mesh_client_internal_data_t *internal = NULL; + bt_mesh_client_user_data_t *client = NULL; + bt_mesh_client_node_t *node = NULL; + int err = 0; + + if (!model || !ctx || !msg) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_client_user_data_t *)model->user_data; + if (!client) { + BT_ERR("%s, Invalid client user data", __func__); + return -EINVAL; + } + + internal = (bt_mesh_client_internal_data_t *)client->internal_data; + if (!internal) { + BT_ERR("%s, Invalid client internal data", __func__); + return -EINVAL; + } + + if (ctx->addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_ERR("%s, Invalid DST 0x%04x", __func__, ctx->addr); + return -EINVAL; + } + + if (!need_ack) { + /* If this is an unack message, send it directly. */ + return bt_mesh_model_send(model, ctx, msg, cb, cb_data); + } + + if (!BLE_MESH_ADDR_IS_UNICAST(ctx->addr)) { + /* If an acknowledged message is not sent to a unicast address, + * for example to a group/virtual address, then all the + * corresponding responses will be treated as publish messages. + * And no timeout will be used for the message. + */ + return bt_mesh_model_send(model, ctx, msg, cb, cb_data); + } + + if (!timer_handler) { + BT_ERR("%s, Invalid timeout handler", __func__); + return -EINVAL; + } + + if (bt_mesh_client_check_node_in_list(&internal->queue, ctx->addr)) { + BT_ERR("%s, Busy sending message to DST 0x%04x", __func__, ctx->addr); + return -EBUSY; + } + + /* Don't forget to free the node in the timeout (timer_handler) function. */ + node = (bt_mesh_client_node_t *)bt_mesh_calloc(sizeof(bt_mesh_client_node_t)); + if (!node) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + memcpy(&node->ctx, ctx, sizeof(struct bt_mesh_msg_ctx)); + node->ctx.model = model; + node->opcode = opcode; + node->op_pending = bt_mesh_client_get_status_op(client->op_pair, client->op_pair_size, opcode); + if (node->op_pending == 0U) { + BT_ERR("Not found the status opcode in op_pair list"); + bt_mesh_free(node); + return -EINVAL; + } + node->timeout = bt_mesh_client_calc_timeout(ctx, msg, opcode, timeout ? timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT); + + if (k_delayed_work_init(&node->timer, timer_handler)) { + BT_ERR("%s, Failed to create a timer", __func__); + bt_mesh_free(node); + return -EIO; + } + + bt_mesh_list_lock(); + sys_slist_append(&internal->queue, &node->client_node); + bt_mesh_list_unlock(); + + /* "bt_mesh_model_send" will post the mesh packet to the mesh adv queue. + * Due to the higher priority of adv_thread (than btc task), we need to + * send the packet after the list item "node" is initialized properly. + */ + err = bt_mesh_model_send(model, ctx, msg, &send_cb, node); + if (err) { + BT_ERR("Failed to send client message 0x%08x", node->opcode); + k_delayed_work_free(&node->timer); + bt_mesh_client_free_node(node); + } + + return err; +} + +static bt_mesh_mutex_t client_model_lock; + +static void bt_mesh_client_model_mutex_new(void) +{ + if (!client_model_lock.mutex) { + bt_mesh_mutex_create(&client_model_lock); + } +} + +static void bt_mesh_client_model_mutex_free(void) +{ + bt_mesh_mutex_free(&client_model_lock); +} + +void bt_mesh_client_model_lock(void) +{ + bt_mesh_mutex_lock(&client_model_lock); +} + +void bt_mesh_client_model_unlock(void) +{ + bt_mesh_mutex_unlock(&client_model_lock); +} + +int bt_mesh_client_init(struct bt_mesh_model *model) +{ + bt_mesh_client_internal_data_t *data = NULL; + bt_mesh_client_user_data_t *cli = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (!model->op) { + BT_ERR("%s, Client model op is NULL", __func__); + return -EINVAL; + } + + cli = model->user_data; + if (!cli) { + BT_ERR("%s, Client user_data is NULL", __func__); + return -EINVAL; + } + + if (!cli->internal_data) { + data = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t)); + if (!data) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + /* Init the client data queue */ + sys_slist_init(&data->queue); + + cli->model = model; + cli->internal_data = data; + } else { + bt_mesh_client_clear_list(cli->internal_data); + } + + bt_mesh_client_model_mutex_new(); + + return 0; +} + +int bt_mesh_client_deinit(struct bt_mesh_model *model) +{ + bt_mesh_client_user_data_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_client_user_data_t *)model->user_data; + if (!client) { + BT_ERR("%s, Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_client_model_mutex_free(); + + return 0; +} + +int bt_mesh_client_free_node(bt_mesh_client_node_t *node) +{ + bt_mesh_client_internal_data_t *internal = NULL; + bt_mesh_client_user_data_t *client = NULL; + + if (!node || !node->ctx.model) { + BT_ERR("%s, Client model list item is NULL", __func__); + return -EINVAL; + } + + client = (bt_mesh_client_user_data_t *)node->ctx.model->user_data; + if (!client) { + BT_ERR("%s, Client model user data is NULL", __func__); + return -EINVAL; + } + + internal = (bt_mesh_client_internal_data_t *)client->internal_data; + if (!internal) { + BT_ERR("%s, Client model internal data is NULL", __func__); + return -EINVAL; + } + + // Release the client node from the queue + bt_mesh_list_lock(); + sys_slist_find_and_remove(&internal->queue, &node->client_node); + bt_mesh_list_unlock(); + // Free the node + bt_mesh_free(node); + + return 0; +} + +int bt_mesh_client_clear_list(void *data) +{ + bt_mesh_client_internal_data_t *internal = NULL; + bt_mesh_client_node_t *node = NULL; + + if (!data) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + internal = (bt_mesh_client_internal_data_t *)data; + + bt_mesh_list_lock(); + while (!sys_slist_is_empty(&internal->queue)) { + node = (void *)sys_slist_get_not_empty(&internal->queue); + k_delayed_work_free(&node->timer); + bt_mesh_free(node); + } + bt_mesh_list_unlock(); + + return 0; +} + +int bt_mesh_set_client_model_role(bt_mesh_role_param_t *common) +{ + bt_mesh_client_user_data_t *client = NULL; + + if (!common || !common->model || !common->model->user_data) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_client_user_data_t *)common->model->user_data; + + switch (common->role) { +#if CONFIG_BLE_MESH_NODE + case NODE: + /* no matter if provisioner is enabled/disabled , node role can be used to send messages */ + client->msg_role = NODE; + break; +#endif +#if CONFIG_BLE_MESH_PROVISIONER + case PROVISIONER: + /* if provisioner is not enabled, provisioner role can't be used to send messages */ + if (!bt_mesh_is_provisioner_en()) { + BT_ERR("%s, Provisioner is disabled", __func__); + return -EINVAL; + } + client->msg_role = PROVISIONER; + break; +#endif +#if CONFIG_BLE_MESH_FAST_PROV + case FAST_PROV: + client->msg_role = FAST_PROV; + break; +#endif + default: + BT_WARN("%s, Unknown model role %x", __func__, common->role); + return -EINVAL; + } + + return 0; +} diff --git a/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c new file mode 100644 index 000000000..0aff82eb6 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/generic_client.c @@ -0,0 +1,1298 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_generic_model.h" + +#include "model_opcode.h" +#include "generic_client.h" + +/** The following are the macro definitions of generic client + * model messages length, and a message is composed of three + * parts: Opcode + msg_value + MIC + */ +/* Generic onoff client messages length */ +#define BLE_MESH_GEN_ONOFF_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_ONOFF_SET_MSG_LEN (2 + 4 + 4) + +/* Generic level client messages length */ +#define BLE_MESH_GEN_LEVEL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_LEVEL_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_GEN_DELTA_SET_MSG_LEN (2 + 7 + 4) +#define BLE_MESH_GEN_MOVE_SET_MSG_LEN (2 + 5 + 4) + +/* Generic default transition time client messages length */ +#define BLE_MESH_GEN_DEF_TRANS_TIME_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_DEF_TRANS_TIME_SET_MSG_LEN (2 + 1 + 4) + +/* Generic power onoff client messages length */ +#define BLE_MESH_GEN_ONPOWERUP_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_ONPOWERUP_SET_MSG_LEN (2 + 1 + 4) + +/* Generic power level client messages length */ +#define BLE_MESH_GEN_POWER_LEVEL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_POWER_LEVEL_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_GEN_POWER_LAST_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_POWER_DEFAULT_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_POWER_DEFAULT_SET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_GEN_POWER_RANGE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_POWER_RANGE_SET_MSG_LEN (2 + 4 + 4) + +/* Generic battery client messages length */ +#define BLE_MESH_GEN_BATTERY_GET_MSG_LEN (2 + 0 + 4) + +/* Generic location client messages length */ +#define BLE_MESH_GEN_LOC_GLOBAL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_LOC_GLOBAL_SET_MSG_LEN (1 + 10 + 4) +#define BLE_MESH_GEN_LOC_LOCAL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_LOC_LOCAL_SET_MSG_LEN (2 + 9 + 4) + +/* Generic property client messages length */ +#define BLE_MESH_GEN_USER_PROPERTIES_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_USER_PROPERTY_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_GEN_USER_PROPERTY_SET_MSG_LEN /* variable */ +#define BLE_MESH_GEN_ADMIN_PROPERTIES_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_ADMIN_PROPERTY_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_GEN_ADMIN_PROPERTY_SET_MSG_LEN /* variable */ +#define BLE_MESH_GEN_MANU_PROPERTIES_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_GEN_MANU_PROPERTY_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_GEN_MANU_PROPERTY_SET_MSG_LEN (1 + 3 + 4) +#define BLE_MESH_GEN_CLINET_PROPERTIES_GET_MSG_LEN (1 + 2 + 4) + +#define BLE_MESH_GEN_GET_STATE_MSG_LEN (2 + 2 + 4) + +static const bt_mesh_client_op_pair_t gen_op_pair[] = { + { BLE_MESH_MODEL_OP_GEN_ONOFF_GET, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ONOFF_SET, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LEVEL_GET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LEVEL_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_DELTA_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_MOVE_SET, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS }, + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET, BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_GEN_BATTERY_GET, BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET, BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET, BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET, BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET, BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS }, +}; + +static bt_mesh_mutex_t generic_client_lock; + +static void bt_mesh_generic_client_mutex_new(void) +{ + if (!generic_client_lock.mutex) { + bt_mesh_mutex_create(&generic_client_lock); + } +} + +static void bt_mesh_generic_client_mutex_free(void) +{ + bt_mesh_mutex_free(&generic_client_lock); +} + +static void bt_mesh_generic_client_lock(void) +{ + bt_mesh_mutex_lock(&generic_client_lock); +} + +static void bt_mesh_generic_client_unlock(void) +{ + bt_mesh_mutex_unlock(&generic_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive generic status message timeout"); + + bt_mesh_generic_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_generic_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_GENERIC_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_generic_client_unlock(); + + return; +} + +static void generic_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + bt_mesh_client_node_t *node = NULL; + u8_t *val = NULL; + u8_t evt = 0xFF; + size_t len = 0U; + + BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS: { + struct bt_mesh_gen_onoff_status *status = NULL; + if (buf->len != 1 && buf->len != 3) { + BT_ERR("Invalid Generic OnOff Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onoff_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_onoff = net_buf_simple_pull_u8(buf); + if (buf->len) { + status->op_en = true; + status->target_onoff = net_buf_simple_pull_u8(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_onoff_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS: { + struct bt_mesh_gen_level_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("Invalid Generic Level Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_level_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_level = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_level = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_level_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS: { + struct bt_mesh_gen_def_trans_time_status *status = NULL; + if (buf->len != 1) { + BT_ERR("Invalid Generic Default Trans Time Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_def_trans_time_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->trans_time = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_def_trans_time_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS: { + struct bt_mesh_gen_onpowerup_status *status = NULL; + if (buf->len != 1) { + BT_ERR("Invalid Generic OnPowerUp Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_onpowerup_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->onpowerup = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_onpowerup_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS: { + struct bt_mesh_gen_power_level_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("Invalid Generic Power Level Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_level_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_power = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_power = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_power_level_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS: { + struct bt_mesh_gen_power_last_status *status = NULL; + if (buf->len != 2) { + BT_ERR("Invalid Generic Power Last Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_last_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->power = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_power_last_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS: { + struct bt_mesh_gen_power_default_status *status = NULL; + if (buf->len != 2) { + BT_ERR("Invalid Generic Power Default Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_default_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->power = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_power_default_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS: { + struct bt_mesh_gen_power_range_status *status = NULL; + if (buf->len != 5) { + BT_ERR("Invalid Generic Power Range Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_power_range_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->range_min = net_buf_simple_pull_le16(buf); + status->range_max = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_power_range_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS: { + struct bt_mesh_gen_battery_status *status = NULL; + if (buf->len != 8) { + BT_ERR("Invalid Generic Battery Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_battery_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + u32_t value = 0; + value = net_buf_simple_pull_le32(buf); + status->battery_level = (u8_t)value; + status->time_to_discharge = (value >> 8); + value = net_buf_simple_pull_le32(buf); + status->time_to_charge = (value & 0xffffff); + status->flags = (u8_t)(value >> 24); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_battery_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS: { + struct bt_mesh_gen_loc_global_status *status = NULL; + if (buf->len != 10) { + BT_ERR("Invalid Generic Location Global Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_global_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->global_latitude = net_buf_simple_pull_le32(buf); + status->global_longitude = net_buf_simple_pull_le32(buf); + status->global_altitude = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_loc_global_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS: { + struct bt_mesh_gen_loc_local_status *status = NULL; + if (buf->len != 9) { + BT_ERR("Invalid Generic Location Local Status length %d", buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_loc_local_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->local_north = net_buf_simple_pull_le16(buf); + status->local_east = net_buf_simple_pull_le16(buf); + status->local_altitude = net_buf_simple_pull_le16(buf); + status->floor_number = net_buf_simple_pull_u8(buf); + status->uncertainty = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_loc_local_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: { + struct bt_mesh_gen_user_properties_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_properties_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->user_property_ids = bt_mesh_alloc_buf(buf->len); + if (!status->user_property_ids) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->user_property_ids, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_user_properties_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: { + struct bt_mesh_gen_user_property_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_user_property_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->user_property_id = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->user_access = net_buf_simple_pull_u8(buf); + status->user_property_value = bt_mesh_alloc_buf(buf->len); + if (!status->user_property_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->user_property_value, buf->data, buf->len); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_user_property_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: { + struct bt_mesh_gen_admin_properties_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_properties_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->admin_property_ids = bt_mesh_alloc_buf(buf->len); + if (!status->admin_property_ids) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->admin_property_ids, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_admin_properties_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: { + struct bt_mesh_gen_admin_property_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_admin_property_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->admin_property_id = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->admin_user_access = net_buf_simple_pull_u8(buf); + status->admin_property_value = bt_mesh_alloc_buf(buf->len); + if (!status->admin_property_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->admin_property_value, buf->data, buf->len); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_admin_property_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS: { + struct bt_mesh_gen_manu_properties_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_properties_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->manu_property_ids = bt_mesh_alloc_buf(buf->len); + if (!status->manu_property_ids) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->manu_property_ids, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_manu_properties_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS: { + struct bt_mesh_gen_manu_property_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_manu_property_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->manu_property_id = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->manu_user_access = net_buf_simple_pull_u8(buf); + status->manu_property_value = bt_mesh_alloc_buf(buf->len); + if (!status->manu_property_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->manu_property_value, buf->data, buf->len); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_manu_property_status); + break; + } + case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: { + struct bt_mesh_gen_client_properties_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_gen_client_properties_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->client_property_ids = bt_mesh_alloc_buf(buf->len); + if (!status->client_property_ids) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->client_property_ids, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_gen_client_properties_status); + break; + } + default: + BT_ERR("%s, Not a Generic Status message opcode", __func__); + return; + } + + buf->data = val; + buf->len = len; + + bt_mesh_generic_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); + if (!node) { + BT_DBG("Unexpected generic status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_GET: + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET: + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET: + case BLE_MESH_MODEL_OP_GEN_BATTERY_GET: + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET: + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET: + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: + case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_GET_STATE; + break; + case BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_DELTA_SET: + case BLE_MESH_MODEL_OP_GEN_MOVE_SET: + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET: + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET: + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET: + evt = BTC_BLE_MESH_EVT_GENERIC_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_generic_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); + } + } + + bt_mesh_generic_client_unlock(); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS: { + struct bt_mesh_gen_user_properties_status *status; + status = (struct bt_mesh_gen_user_properties_status *)val; + bt_mesh_free_buf(status->user_property_ids); + break; + } + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS: { + struct bt_mesh_gen_user_property_status *status; + status = (struct bt_mesh_gen_user_property_status *)val; + bt_mesh_free_buf(status->user_property_value); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS: { + struct bt_mesh_gen_admin_properties_status *status; + status = (struct bt_mesh_gen_admin_properties_status *)val; + bt_mesh_free_buf(status->admin_property_ids); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS: { + struct bt_mesh_gen_admin_property_status *status; + status = (struct bt_mesh_gen_admin_property_status *)val; + bt_mesh_free_buf(status->admin_property_value); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS: { + struct bt_mesh_gen_manu_properties_status *status; + status = (struct bt_mesh_gen_manu_properties_status *)val; + bt_mesh_free_buf(status->manu_property_ids); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS: { + struct bt_mesh_gen_manu_property_status *status; + status = (struct bt_mesh_gen_manu_property_status *)val; + bt_mesh_free_buf(status->manu_property_value); + break; + } + case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS: { + struct bt_mesh_gen_client_properties_status *status; + status = (struct bt_mesh_gen_client_properties_status *)val; + bt_mesh_free_buf(status->client_property_ids); + break; + } + default: + break; + } + + bt_mesh_free(val); + + return; +} + +const struct bt_mesh_model_op gen_onoff_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS, 1, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_level_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS, 2, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_def_trans_time_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS, 1, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_power_onoff_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS, 1, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_power_level_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS, 5, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_battery_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS, 8, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_location_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS, 10, generic_status }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS, 9, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op gen_property_cli_op[] = { + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS, 2, generic_status }, + { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS, 2, generic_status }, + BLE_MESH_MODEL_OP_END, +}; + +static int gen_get_state(bt_mesh_client_common_param_t *common, void *value) +{ + NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_GEN_GET_STATE_MSG_LEN); + int err = 0; + + bt_mesh_model_msg_init(&msg, common->opcode); + + if (value) { + switch (common->opcode) { + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: { + struct bt_mesh_gen_user_property_get *get; + get = (struct bt_mesh_gen_user_property_get *)value; + net_buf_simple_add_le16(&msg, get->user_property_id); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: { + struct bt_mesh_gen_admin_property_get *get; + get = (struct bt_mesh_gen_admin_property_get *)value; + net_buf_simple_add_le16(&msg, get->admin_property_id); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: { + struct bt_mesh_gen_manu_property_get *get; + get = (struct bt_mesh_gen_manu_property_get *)value; + net_buf_simple_add_le16(&msg, get->manu_property_id); + break; + } + case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: { + struct bt_mesh_gen_client_properties_get *get; + get = (struct bt_mesh_gen_client_properties_get *)value; + net_buf_simple_add_le16(&msg, get->client_property_id); + break; + } + default: + BT_DBG("This generic message should be sent with NULL get pointer"); + break; + } + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, &msg, + timeout_handler, common->msg_timeout, true, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Generic Get message (err %d)", __func__, err); + } + + return err; +} + +static int gen_set_state(bt_mesh_client_common_param_t *common, + void *value, u16_t value_len, bool need_ack) +{ + struct net_buf_simple *msg = NULL; + int err = 0; + + msg = bt_mesh_alloc_buf(value_len); + if (!msg) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + bt_mesh_model_msg_init(msg, common->opcode); + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + case BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: { + struct bt_mesh_gen_onoff_set *set; + set = (struct bt_mesh_gen_onoff_set *)value; + net_buf_simple_add_u8(msg, set->onoff); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: { + struct bt_mesh_gen_level_set *set; + set = (struct bt_mesh_gen_level_set *)value; + net_buf_simple_add_le16(msg, set->level); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + + case BLE_MESH_MODEL_OP_GEN_DELTA_SET: + case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: { + struct bt_mesh_gen_delta_set *set; + set = (struct bt_mesh_gen_delta_set *)value; + net_buf_simple_add_le32(msg, set->delta_level); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + + case BLE_MESH_MODEL_OP_GEN_MOVE_SET: + case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: { + struct bt_mesh_gen_move_set *set; + set = (struct bt_mesh_gen_move_set *)value; + net_buf_simple_add_le16(msg, set->delta_level); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET: + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK: { + struct bt_mesh_gen_def_trans_time_set *set; + set = (struct bt_mesh_gen_def_trans_time_set *)value; + net_buf_simple_add_u8(msg, set->trans_time); + break; + } + + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET: + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK: { + struct bt_mesh_gen_onpowerup_set *set; + set = (struct bt_mesh_gen_onpowerup_set *)value; + net_buf_simple_add_u8(msg, set->onpowerup); + break; + } + + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK: { + struct bt_mesh_gen_power_level_set *set; + set = (struct bt_mesh_gen_power_level_set *)value; + net_buf_simple_add_le16(msg, set->power); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK: { + struct bt_mesh_gen_power_default_set *set; + set = (struct bt_mesh_gen_power_default_set *)value; + net_buf_simple_add_le16(msg, set->power); + break; + } + + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET: + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK: { + struct bt_mesh_gen_power_range_set *set; + set = (struct bt_mesh_gen_power_range_set *)value; + net_buf_simple_add_le16(msg, set->range_min); + net_buf_simple_add_le16(msg, set->range_max); + break; + } + + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: { + struct bt_mesh_gen_loc_global_set *set; + set = (struct bt_mesh_gen_loc_global_set *)value; + net_buf_simple_add_le32(msg, set->global_latitude); + net_buf_simple_add_le32(msg, set->global_longitude); + net_buf_simple_add_le16(msg, set->global_altitude); + break; + } + + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: { + struct bt_mesh_gen_loc_local_set *set; + set = (struct bt_mesh_gen_loc_local_set *)value; + net_buf_simple_add_le16(msg, set->local_north); + net_buf_simple_add_le16(msg, set->local_east); + net_buf_simple_add_le16(msg, set->local_altitude); + net_buf_simple_add_u8(msg, set->floor_number); + net_buf_simple_add_le16(msg, set->uncertainty); + break; + } + + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: { + struct bt_mesh_gen_user_property_set *set; + set = (struct bt_mesh_gen_user_property_set *)value; + net_buf_simple_add_le16(msg, set->user_property_id); + net_buf_simple_add_mem(msg, set->user_property_value->data, set->user_property_value->len); + break; + } + + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: { + struct bt_mesh_gen_admin_property_set *set; + set = (struct bt_mesh_gen_admin_property_set *)value; + net_buf_simple_add_le16(msg, set->admin_property_id); + net_buf_simple_add_u8(msg, set->admin_user_access); + net_buf_simple_add_mem(msg, set->admin_property_value->data, set->admin_property_value->len); + break; + } + + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET: + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK: { + struct bt_mesh_gen_manu_property_set *set; + set = (struct bt_mesh_gen_manu_property_set *)value; + net_buf_simple_add_le16(msg, set->manu_property_id); + net_buf_simple_add_u8(msg, set->manu_user_access); + break; + } + + default: + BT_ERR("%s, Not a Generic Client Set message opcode", __func__); + err = -EINVAL; + goto end; + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, msg, + timeout_handler, common->msg_timeout, need_ack, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Generic Set message (err %d)", __func__, err); + } + +end: + bt_mesh_free_buf(msg); + + return err; +} + +int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) +{ + bt_mesh_generic_client_t *client = NULL; + + if (!common || !common->model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_generic_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Generic Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_GET: + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET: + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET: + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET: + case BLE_MESH_MODEL_OP_GEN_BATTERY_GET: + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET: + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET: + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: + break; + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: + if (!get) { + BT_ERR("%s, Generic user_property_get is NULL", __func__); + return -EINVAL; + } + break; + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: + if (!get) { + BT_ERR("%s, Generic admin_property_get is NULL", __func__); + return -EINVAL; + } + break; + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: + if (!get) { + BT_ERR("%s, Generic manu_property_get is NULL", __func__); + return -EINVAL; + } + break; + case BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET: + if (!get) { + BT_ERR("%s, Generic client_properties_get is NULL", __func__); + return -EINVAL; + } + break; + default: + BT_ERR("%s, Not a Generic Client Get message opcode", __func__); + return -EINVAL; + } + + return gen_get_state(common, get); +} + +int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) +{ + bt_mesh_generic_client_t *client = NULL; + u16_t length = 0U; + bool need_ack = false; + + if (!common || !common->model || !set) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_generic_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Generic Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: { + struct bt_mesh_gen_onoff_set *value; + value = (struct bt_mesh_gen_onoff_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic OnOff Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_GEN_ONOFF_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: { + struct bt_mesh_gen_level_set *value; + value = (struct bt_mesh_gen_level_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic Level Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_GEN_LEVEL_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_DELTA_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: { + struct bt_mesh_gen_delta_set *value; + value = (struct bt_mesh_gen_delta_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic Delta Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_GEN_DELTA_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_MOVE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: { + struct bt_mesh_gen_move_set *value; + value = (struct bt_mesh_gen_move_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic Move Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_GEN_MOVE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK: { + u8_t value = *(u8_t *)set; + if ((value & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic Default Trans Time Set transition time", __func__); + return -EINVAL; + } + length = BLE_MESH_GEN_DEF_TRANS_TIME_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK: + length = BLE_MESH_GEN_ONPOWERUP_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK: { + struct bt_mesh_gen_power_level_set *value; + value = (struct bt_mesh_gen_power_level_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Generic Power Level Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_GEN_POWER_LEVEL_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK: + length = BLE_MESH_GEN_POWER_DEFAULT_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK: { + struct bt_mesh_gen_power_range_set *value; + value = (struct bt_mesh_gen_power_range_set *)set; + if (value->range_min > value->range_max) { + BT_ERR("%s, Generic Power Level Set range min is greater than range max", __func__); + return -EINVAL; + } + length = BLE_MESH_GEN_POWER_RANGE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: + length = BLE_MESH_GEN_LOC_GLOBAL_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: + length = BLE_MESH_GEN_LOC_LOCAL_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK: { + struct bt_mesh_gen_user_property_set *value; + value = (struct bt_mesh_gen_user_property_set *)set; + if (!value->user_property_value) { + BT_ERR("%s, Generic user_property_value is NULL", __func__); + return -EINVAL; + } + length = (1 + 2 + value->user_property_value->len + 4); + break; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK: { + struct bt_mesh_gen_admin_property_set *value; + value = (struct bt_mesh_gen_admin_property_set *)set; + if (!value->admin_property_value) { + BT_ERR("%s, Generic admin_property_value is NULL", __func__); + return -EINVAL; + } + length = (1 + 2 + 1 + value->admin_property_value->len + 4); + break; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK: + length = BLE_MESH_GEN_MANU_PROPERTY_SET_MSG_LEN; + break; + default: + BT_ERR("%s, Not a Generic Client Set message opcode", __func__); + return -EINVAL; + } + + return gen_set_state(common, set, length, need_ack); +} + +static int generic_client_init(struct bt_mesh_model *model, bool primary) +{ + generic_internal_data_t *internal = NULL; + bt_mesh_generic_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_generic_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Generic Client user_data is NULL", __func__); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(generic_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(gen_op_pair); + client->op_pair = gen_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + bt_mesh_generic_client_mutex_new(); + + return 0; +} + +int bt_mesh_gen_onoff_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_level_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_def_trans_time_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_pwr_onoff_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_pwr_level_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_battery_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_location_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +int bt_mesh_gen_property_cli_init(struct bt_mesh_model *model, bool primary) +{ + return generic_client_init(model, primary); +} + +static int generic_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_generic_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_generic_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Generic Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_generic_client_mutex_free(); + + return 0; +} + +int bt_mesh_gen_onoff_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_level_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_def_trans_time_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_pwr_onoff_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_pwr_level_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_battery_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_location_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} + +int bt_mesh_gen_property_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_client_deinit(model, primary); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h new file mode 100644 index 000000000..a39bcb542 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/client_common.h @@ -0,0 +1,139 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _CLIENT_COMMON_H_ +#define _CLIENT_COMMON_H_ + +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Client model opcode pair table */ +typedef struct { + u32_t cli_op; /* Client message opcode */ + u32_t status_op; /* Corresponding status message opcode */ +} bt_mesh_client_op_pair_t; + +/** Client model user data context */ +typedef struct { + /** Pointer to the client model */ + struct bt_mesh_model *model; + + /** Size of the opcode pair table */ + int op_pair_size; + + /** Pointer to the opcode pair table */ + const bt_mesh_client_op_pair_t *op_pair; + + /** + * @brief This function is a callback function used to push the received unsolicited + * messages to the application layer. + * + * @param[in] opcode: Opcode of received status message + * @param[in] model: Model associated with the status message + * @param[in] ctx: Context information of the status message + * @param[in] buf: Buffer contains the status message value + * + * @return None + */ + void (*publish_status)(u32_t opcode, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf); + + /** Pointer to the internal data of client model */ + void *internal_data; + + /** Role of the device to which the client model belongs */ + u8_t msg_role; +} bt_mesh_client_user_data_t; + +/** Client model internal data context */ +typedef struct { + sys_slist_t queue; +} bt_mesh_client_internal_data_t; + +/** Client model sending message related context */ +typedef struct { + sys_snode_t client_node; + struct bt_mesh_msg_ctx ctx; /* Message context */ + u32_t opcode; /* Message opcode */ + u32_t op_pending; /* Expected status message opcode */ + s32_t timeout; /* Calculated message timeout value */ + struct k_delayed_work timer; /* Time used to get response. Only for internal use. */ +} bt_mesh_client_node_t; + +/** Client model sending message parameters */ +typedef struct { + u32_t opcode; /* Message opcode */ + struct bt_mesh_model *model; /* Pointer to the client model */ + struct bt_mesh_msg_ctx ctx; /* Message context */ + s32_t msg_timeout; /* Time to get corresponding response */ + const struct bt_mesh_send_cb *cb; /* User defined callback function */ + void *cb_data; /* User defined callback value */ +} bt_mesh_client_common_param_t; + +void bt_mesh_client_model_lock(void); + +void bt_mesh_client_model_unlock(void); + +int bt_mesh_client_init(struct bt_mesh_model *model); + +int bt_mesh_client_deinit(struct bt_mesh_model *model); + +/** + * @brief Check if the msg received by client model is a publish msg or not + * + * @param model Mesh (client) Model that the message belongs to. + * @param ctx Message context, includes keys, TTL, etc. + * @param buf The message buffer + * @param need_pub Indicate if the msg sent to app layer as a publish msg + * @return 0 on success, or (negative) error code on failure. + */ +bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg( + struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, bool need_pub); + +int bt_mesh_client_send_msg(struct bt_mesh_model *model, + u32_t opcode, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *msg, + k_work_handler_t timer_handler, + s32_t timeout, bool need_ack, + const struct bt_mesh_send_cb *cb, void *cb_data); + +int bt_mesh_client_free_node(bt_mesh_client_node_t *node); + +int bt_mesh_client_clear_list(void *data); + +typedef struct { + struct bt_mesh_model *model; /* The client model structure */ + u8_t role; /* Role of the device - Node/Provisioner */ +} bt_mesh_role_param_t; + +/** + * @brief This function copies node_index for stack internal use. + * + * @param[in] common: Pointer to the bt_mesh_role_param_t structure + * + * @return Zero - success, otherwise - fail + */ +int bt_mesh_set_client_model_role(bt_mesh_role_param_t *common); + +#ifdef __cplusplus +} +#endif + +#endif /* _CLIENT_COMMON_H_ */ + diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h new file mode 100644 index 000000000..eeda99fc3 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/generic_client.h @@ -0,0 +1,577 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Generic Client Model APIs. + */ + +#ifndef _GENERIC_CLIENT_H_ +#define _GENERIC_CLIENT_H_ + +#include "client_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Generic client model common structure */ +typedef bt_mesh_client_user_data_t bt_mesh_generic_client_t; +typedef bt_mesh_client_internal_data_t generic_internal_data_t; + +/* Generic OnOff Client Model Context */ +extern const struct bt_mesh_model_op gen_onoff_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_ONOFF_CLI + * + * Define a new generic onoff client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a generic onoff client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_onoff_cli. + * + * @return New generic onoff client model instance. + */ +#define BLE_MESH_MODEL_GEN_ONOFF_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_ONOFF_CLI, \ + gen_onoff_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_onoff_client_t; + +struct bt_mesh_gen_onoff_status { + bool op_en; /* Indicate whether optional parameters included */ + u8_t present_onoff; /* Present value of Generic OnOff state */ + u8_t target_onoff; /* Target value of Generic OnOff state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_gen_onoff_set { + bool op_en; /* Indicate whether optional parameters included */ + u8_t onoff; /* Target value of Generic OnOff state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +/* Generic Level Client Model Context */ +extern const struct bt_mesh_model_op gen_level_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_LEVEL_CLI + * + * Define a new generic level client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a generic level client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_level_cli. + * + * @return New generic level client model instance. + */ +#define BLE_MESH_MODEL_GEN_LEVEL_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_LEVEL_CLI, \ + gen_level_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_level_client_t; + +struct bt_mesh_gen_level_status { + bool op_en; /* Indicate whether optional parameters included */ + s16_t present_level; /* Present value of Generic Level state */ + s16_t target_level; /* Target value of the Generic Level state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_gen_level_set { + bool op_en; /* Indicate whether optional parameters included */ + s16_t level; /* Target value of Generic Level state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_gen_delta_set { + bool op_en; /* Indicate whether optional parameters included */ + s32_t delta_level; /* Delta change of Generic Level state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_gen_move_set { + bool op_en; /* Indicate whether optional parameters included */ + s16_t delta_level; /* Delta Level step to calculate Move speed for Generic Level state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +/* Generic Default Transition Time Client Model Context */ +extern const struct bt_mesh_model_op gen_def_trans_time_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_CLI + * + * Define a new generic default transition time client model. Note + * that this API needs to be repeated for each element that the + * application wants to have a generic default transition client + * model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_def_trans_time_cli. + * + * @return New generic default transition time client model instance. + */ +#define BLE_MESH_MODEL_GEN_DEF_TRANS_TIME_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI, \ + gen_def_trans_time_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_def_trans_time_client_t; + +struct bt_mesh_gen_def_trans_time_set { + u8_t trans_time; /* The value of the Generic Default Transition Time state */ +}; + +struct bt_mesh_gen_def_trans_time_status { + u8_t trans_time; /* The value of the Generic Default Transition Time state */ +}; + +/* Generic Power OnOff Client Model Context */ +extern const struct bt_mesh_model_op gen_power_onoff_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_POWER_ONOFF_CLI + * + * Define a new generic power onoff client model. Note that this API + * needs to be repeated for each element which the application wants + * to have a generic power onoff client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_power_onoff_cli. + * + * @return New generic power onoff client model instance. + */ +#define BLE_MESH_MODEL_GEN_POWER_ONOFF_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI, \ + gen_power_onoff_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_power_onoff_client_t; + +struct bt_mesh_gen_onpowerup_set { + u8_t onpowerup; /* The value of the Generic OnPowerUp state */ +}; + +struct bt_mesh_gen_onpowerup_status { + u8_t onpowerup; /* The value of the Generic OnPowerUp state */ +}; + +/* Generic Power Level Client Model Context */ +extern const struct bt_mesh_model_op gen_power_level_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_POWER_LEVEL_CLI + * + * Define a new generic power level client model. Note that this API + * needs to be repeated for each element which the application wants + * to have a generic power level client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_power_level_cli. + * + * @return New generic power level client model instance. + */ +#define BLE_MESH_MODEL_GEN_POWER_LEVEL_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI, \ + gen_power_level_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_power_level_client_t; + +struct bt_mesh_gen_power_level_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_power; /* Present value of Generic Power Actual state */ + u16_t target_power; /* Target value of Generic Power Actual state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_gen_power_last_status { + u16_t power; /* The value of the Generic Power Last state */ +}; + +struct bt_mesh_gen_power_default_status { + u16_t power; /* The value of the Generic Default Last state */ +}; + +struct bt_mesh_gen_power_range_status { + u8_t status_code; /* Status Code for the requesting message */ + u16_t range_min; /* Value of Range Min field of Generic Power Range state */ + u16_t range_max; /* Value of Range Max field of Generic Power Range state */ +}; + +struct bt_mesh_gen_power_level_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t power; /* Target value of Generic Power Actual state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_gen_power_default_set { + u16_t power; /* The value of the Generic Power Default state */ +}; + +struct bt_mesh_gen_power_range_set { + u16_t range_min; /* Value of Range Min field of Generic Power Range state */ + u16_t range_max; /* Value of Range Max field of Generic Power Range state */ +}; + +/* Generic Battery Client Model Context */ +extern const struct bt_mesh_model_op gen_battery_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_BATTERY_CLI + * + * Define a new generic battery client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a generic battery client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_battery_cli. + * + * @return New generic battery client model instance. + */ +#define BLE_MESH_MODEL_GEN_BATTERY_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_BATTERY_CLI, \ + gen_battery_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_battery_client_t; + +struct bt_mesh_gen_battery_status { + u32_t battery_level : 8; /* Value of Generic Battery Level state */ + u32_t time_to_discharge : 24; /* Value of Generic Battery Time to Discharge state */ + u32_t time_to_charge : 24; /* Value of Generic Battery Time to Charge state */ + u32_t flags : 8; /* Value of Generic Battery Flags state */ +}; + +/* Generic Location Client Model Context */ +extern const struct bt_mesh_model_op gen_location_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_LOCATION_CLI + * + * Define a new generic location client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a generic location client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_location_cli. + * + * @return New generic location client model instance. + */ +#define BLE_MESH_MODEL_GEN_LOCATION_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_LOCATION_CLI, \ + gen_location_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_location_client_t; + +struct bt_mesh_gen_loc_global_status { + s32_t global_latitude; /* Global Coordinates (Latitude) */ + s32_t global_longitude; /* Global Coordinates (Longitude) */ + s16_t global_altitude; /* Global Altitude */ +}; + +struct bt_mesh_gen_loc_local_status { + s16_t local_north; /* Local Coordinates (North) */ + s16_t local_east; /* Local Coordinates (East) */ + s16_t local_altitude; /* Local Altitude */ + u8_t floor_number; /* Floor Number */ + u16_t uncertainty; /* Uncertainty */ +}; + +struct bt_mesh_gen_loc_global_set { + s32_t global_latitude; /* Global Coordinates (Latitude) */ + s32_t global_longitude; /* Global Coordinates (Longitude) */ + s16_t global_altitude; /* Global Altitude */ +}; + +struct bt_mesh_gen_loc_local_set { + s16_t local_north; /* Local Coordinates (North) */ + s16_t local_east; /* Local Coordinates (East) */ + s16_t local_altitude; /* Local Altitude */ + u8_t floor_number; /* Floor Number */ + u16_t uncertainty; /* Uncertainty */ +}; + +/* Generic Property Client Model Context */ +extern const struct bt_mesh_model_op gen_property_cli_op[]; + +/** @def BLE_MESH_MODEL_GEN_LOCATION_CLI + * + * Define a new generic location client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a generic location client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_gen_location_cli. + * + * @return New generic location client model instance. + */ +#define BLE_MESH_MODEL_GEN_PROPERTY_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_GEN_PROP_CLI, \ + gen_property_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_gen_property_client_t; + +struct bt_mesh_gen_user_properties_status { + struct net_buf_simple *user_property_ids; /* Buffer contains a sequence of N User Property IDs */ +}; + +struct bt_mesh_gen_user_property_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t user_property_id; /* Property ID identifying a Generic User Property */ + u8_t user_access; /* Enumeration indicating user access (optional) */ + struct net_buf_simple *user_property_value; /* Raw value for the User Property (C.1) */ +}; + +struct bt_mesh_gen_admin_properties_status { + struct net_buf_simple *admin_property_ids; /* Buffer contains a sequence of N Admin Property IDs */ +}; + +struct bt_mesh_gen_admin_property_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t admin_property_id; /* Property ID identifying a Generic Admin Property */ + u8_t admin_user_access; /* Enumeration indicating user access (optional) */ + struct net_buf_simple *admin_property_value; /* Raw value for the Admin Property (C.1) */ +}; + +struct bt_mesh_gen_manu_properties_status { + struct net_buf_simple *manu_property_ids; /* Buffer contains a sequence of N Manufacturer Property IDs */ +}; + +struct bt_mesh_gen_manu_property_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t manu_property_id; /* Property ID identifying a Generic Manufacturer Property */ + u8_t manu_user_access; /* Enumeration indicating user access (optional) */ + struct net_buf_simple *manu_property_value; /* Raw value for the Manufacturer Property (C.1) */ +}; + +struct bt_mesh_gen_client_properties_status { + struct net_buf_simple *client_property_ids; /* Buffer contains a sequence of N Client Property IDs */ +}; + +struct bt_mesh_gen_user_property_get { + u16_t user_property_id; /* Property ID identifying a Generic User Property */ +}; + +struct bt_mesh_gen_user_property_set { + u16_t user_property_id; /* Property ID identifying a Generic User Property */ + struct net_buf_simple *user_property_value; /* Raw value for the User Property */ +}; + +struct bt_mesh_gen_admin_property_get { + u16_t admin_property_id; /* Property ID identifying a Generic Admin Property */ +}; + +struct bt_mesh_gen_admin_property_set { + u16_t admin_property_id; /* Property ID identifying a Generic Admin Property */ + u8_t admin_user_access; /* Enumeration indicating user access */ + struct net_buf_simple *admin_property_value; /* Raw value for the Admin Property */ +}; + +struct bt_mesh_gen_manu_property_get { + u16_t manu_property_id; /* Property ID identifying a Generic Manufacturer Property */ +}; + +struct bt_mesh_gen_manu_property_set { + u16_t manu_property_id; /* Property ID identifying a Generic Manufacturer Property */ + u8_t manu_user_access; /* Enumeration indicating user access */ +}; + +struct bt_mesh_gen_client_properties_get { + u16_t client_property_id; /* A starting Client Property ID present within an element */ +}; + +/** + * @brief This function is called to initialize generic onoff client model user_data. + * + * @param[in] model: Pointer to generic onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_onoff_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic level client model user_data. + * + * @param[in] model: Pointer to generic level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_level_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic default transition time + * client model user_data. + * + * @param[in] model: Pointer to generic default transition time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_def_trans_time_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic power onoff client model user_data. + * + * @param[in] model: Pointer to generic power onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_onoff_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic power level client model user_data. + * + * @param[in] model: Pointer to generic power level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_level_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic battery client model user_data. + * + * @param[in] model: Pointer to generic battery client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_battery_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic location client model user_data. + * + * @param[in] model: Pointer to generic location client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_location_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize generic property client model user_data. + * + * @param[in] model: Pointer to generic property client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_property_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic onoff client model user_data. + * + * @param[in] model: Pointer to generic onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_onoff_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic level client model user_data. + * + * @param[in] model: Pointer to generic level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_level_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic default transition time + * client model user_data. + * + * @param[in] model: Pointer to generic default transition time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_def_trans_time_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic power onoff client model user_data. + * + * @param[in] model: Pointer to generic power onoff client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_onoff_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic power level client model user_data. + * + * @param[in] model: Pointer to generic power level client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_pwr_level_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic battery client model user_data. + * + * @param[in] model: Pointer to generic battery client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_battery_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic location client model user_data. + * + * @param[in] model: Pointer to generic location client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_location_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize generic property client model user_data. + * + * @param[in] model: Pointer to generic property client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_gen_property_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to get generic states. + * + * @param[in] common: Message common information structure + * @param[in] get: Pointer of generic get message value + * @param[out] status: Pointer of generic status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_generic_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); + +/** + * @brief This function is called to set generic states. + * + * @param[in] common: Message common information structure + * @param[in] set: Pointer of generic set message value + * @param[out] status: Pointer of generic status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_generic_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); + +#ifdef __cplusplus +} +#endif + +#endif /* _GENERIC_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h new file mode 100644 index 000000000..7867f10ad --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/lighting_client.h @@ -0,0 +1,547 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Lighting Client Model APIs. + */ + +#ifndef _LIGHTING_CLIENT_H_ +#define _LIGHTING_CLIENT_H_ + +#include "client_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Light client model common structure */ +typedef bt_mesh_client_user_data_t bt_mesh_light_client_t; +typedef bt_mesh_client_internal_data_t light_internal_data_t; + +/* Light Lightness Client Model Context */ +extern const struct bt_mesh_model_op light_lightness_cli_op[]; + +/** @def BLE_MESH_MODEL_LIGHT_LIGHTNESS_CLI + * + * Define a new light lightness client model. Note that this API + * needs to be repeated for each element which the application + * wants to have a light lightness client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_light_lightness_cli. + * + * @return New light lightness client model instance. + */ +#define BLE_MESH_MODEL_LIGHT_LIGHTNESS_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI, \ + light_lightness_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_light_lightness_client_t; + +struct bt_mesh_light_lightness_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_lightness; /* Present value of light lightness actual state */ + u16_t target_lightness; /* Target value of light lightness actual state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_lightness_linear_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_lightness; /* Present value of light lightness linear state */ + u16_t target_lightness; /* Target value of light lightness linear state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_lightness_last_status { + u16_t lightness; /* The value of the Light Lightness Last state */ +}; + +struct bt_mesh_light_lightness_default_status { + u16_t lightness; /* The value of the Light Lightness default state */ +}; + +struct bt_mesh_light_lightness_range_status { + u8_t status_code; /* Status Code for the requesting message */ + u16_t range_min; /* Value of range min field of light lightness range state */ + u16_t range_max; /* Value of range max field of light lightness range state */ +}; + +struct bt_mesh_light_lightness_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t lightness; /* Target value of light lightness actual state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_lightness_linear_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t lightness; /* Target value of light lightness linear state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_lightness_default_set { + u16_t lightness; /* The value of the Light Lightness Default state */ +}; + +struct bt_mesh_light_lightness_range_set { + u16_t range_min; /* Value of range min field of light lightness range state */ + u16_t range_max; /* Value of range max field of light lightness range state */ +}; + +/* Light CTL Client Model Context */ +extern const struct bt_mesh_model_op light_ctl_cli_op[]; + +/** @def BLE_MESH_MODEL_LIGHT_CTL_CLI + * + * Define a new light CTL client model. Note that this API needs + * to be repeated for each element which the application wants to + * have a light CTL client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_light_ctl_cli. + * + * @return New light CTL client model instance. + */ +#define BLE_MESH_MODEL_LIGHT_CTL_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_CTL_CLI, \ + light_ctl_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_light_ctl_client_t; + +struct bt_mesh_light_ctl_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_ctl_lightness; /* Present value of light ctl lightness state */ + u16_t present_ctl_temperature; /* Present value of light ctl temperature state */ + u16_t target_ctl_lightness; /* Target value of light ctl lightness state (optional) */ + u16_t target_ctl_temperature; /* Target value of light ctl temperature state (C.1) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_ctl_temperature_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_ctl_temperature; /* Present value of light ctl temperature state */ + u16_t present_ctl_delta_uv; /* Present value of light ctl delta UV state */ + u16_t target_ctl_temperature; /* Target value of light ctl temperature state (optional) */ + u16_t target_ctl_delta_uv; /* Target value of light ctl delta UV state (C.1) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_ctl_temperature_range_status { + u8_t status_code; /* Status code for the requesting message */ + u16_t range_min; /* Value of temperature range min field of light ctl temperature range state */ + u16_t range_max; /* Value of temperature range max field of light ctl temperature range state */ +}; + +struct bt_mesh_light_ctl_default_status { + u16_t lightness; /* Value of light lightness default state */ + u16_t temperature; /* Value of light temperature default state */ + s16_t delta_uv; /* Value of light delta UV default state */ +}; + +struct bt_mesh_light_ctl_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t ctl_lightness; /* Target value of light ctl lightness state */ + u16_t ctl_temperature; /* Target value of light ctl temperature state */ + s16_t ctl_delta_uv; /* Target value of light ctl delta UV state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_ctl_temperature_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t ctl_temperature; /* Target value of light ctl temperature state */ + s16_t ctl_delta_uv; /* Target value of light ctl delta UV state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_ctl_temperature_range_set { + u16_t range_min; /* Value of temperature range min field of light ctl temperature range state */ + u16_t range_max; /* Value of temperature range max field of light ctl temperature range state */ +}; + +struct bt_mesh_light_ctl_default_set { + u16_t lightness; /* Value of light lightness default state */ + u16_t temperature; /* Value of light temperature default state */ + s16_t delta_uv; /* Value of light delta UV default state */ +}; + +/* Light HSL Client Model Context */ +extern const struct bt_mesh_model_op light_hsl_cli_op[]; + +/** @def BLE_MESH_MODEL_LIGHT_HSL_CLI + * + * Define a new light HSL client model. Note that this API needs + * to be repeated for each element which the application wants to + * have a light HSL client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_light_hsl_cli. + * + * @return New light HSL client model instance. + */ +#define BLE_MESH_MODEL_LIGHT_HSL_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_HSL_CLI, \ + light_hsl_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_light_hsl_client_t; + +struct bt_mesh_light_hsl_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t hsl_lightness; /* Present value of light hsl lightness state */ + u16_t hsl_hue; /* Present value of light hsl hue state */ + u16_t hsl_saturation; /* Present value of light hsl saturation state */ + u8_t remain_time; /* Time to complete state transition (optional) */ +}; + +struct bt_mesh_light_hsl_target_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t hsl_lightness_target; /* Target value of light hsl lightness state */ + u16_t hsl_hue_target; /* Target value of light hsl hue state */ + u16_t hsl_saturation_target; /* Target value of light hsl saturation state */ + u8_t remain_time; /* Time to complete state transition (optional) */ +}; + +struct bt_mesh_light_hsl_hue_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_hue; /* Present value of light hsl hue state */ + u16_t target_hue; /* Target value of light hsl hue state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_hsl_saturation_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t present_saturation; /* Present value of light hsl saturation state */ + u16_t target_saturation; /* Target value of light hsl saturation state (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_hsl_default_status { + u16_t lightness; /* Value of light lightness default state */ + u16_t hue; /* Value of light hue default state */ + u16_t saturation; /* Value of light saturation default state */ +}; + +struct bt_mesh_light_hsl_range_status { + u8_t status_code; /* Status code for the requesting message */ + u16_t hue_range_min; /* Value of hue range min field of light hsl hue range state */ + u16_t hue_range_max; /* Value of hue range max field of light hsl hue range state */ + u16_t saturation_range_min; /* Value of saturation range min field of light hsl saturation range state */ + u16_t saturation_range_max; /* Value of saturation range max field of light hsl saturation range state */ +}; + +struct bt_mesh_light_hsl_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t hsl_lightness; /* Target value of light hsl lightness state */ + u16_t hsl_hue; /* Target value of light hsl hue state */ + u16_t hsl_saturation; /* Target value of light hsl saturation state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_hsl_hue_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t hue; /* Target value of light hsl hue state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_hsl_saturation_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t saturation; /* Target value of light hsl hue state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_hsl_default_set { + u16_t lightness; /* Value of light lightness default state */ + u16_t hue; /* Value of light hue default state */ + u16_t saturation; /* Value of light saturation default state */ +}; + +struct bt_mesh_light_hsl_range_set { + u16_t hue_range_min; /* Value of hue range min field of light hsl hue range state */ + u16_t hue_range_max; /* Value of hue range max field of light hsl hue range state */ + u16_t saturation_range_min; /* Value of saturation range min field of light hsl saturation range state */ + u16_t saturation_range_max; /* Value of saturation range max field of light hsl saturation range state */ +}; + +/* Light xyL Client Model Context */ +extern const struct bt_mesh_model_op light_xyl_cli_op[]; + +/** @def BLE_MESH_MODEL_LIGHT_XYL_CLI + * + * Define a new light xyL client model. Note that this API needs + * to be repeated for each element which the application wants + * to have a light xyL client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_light_xyl_cli. + * + * @return New light xyL client model instance. + */ +#define BLE_MESH_MODEL_LIGHT_XYL_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_XYL_CLI, \ + light_xyl_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_light_xyl_client_t; + +struct bt_mesh_light_xyl_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t xyl_lightness; /* The present value of the Light xyL Lightness state */ + u16_t xyl_x; /* The present value of the Light xyL x state */ + u16_t xyl_y; /* The present value of the Light xyL y state */ + u8_t remain_time; /* Time to complete state transition (optional) */ +}; + +struct bt_mesh_light_xyl_target_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t target_xyl_lightness; /* The target value of the Light xyL Lightness state */ + u16_t target_xyl_x; /* The target value of the Light xyL x state */ + u16_t target_xyl_y; /* The target value of the Light xyL y state */ + u8_t remain_time; /* Time to complete state transition (optional) */ +}; + +struct bt_mesh_light_xyl_default_status { + u16_t lightness; /* The value of the Light Lightness Default state */ + u16_t xyl_x; /* The value of the Light xyL x Default state */ + u16_t xyl_y; /* The value of the Light xyL y Default state */ +}; + +struct bt_mesh_light_xyl_range_status { + u8_t status_code; /* Status Code for the requesting message */ + u16_t xyl_x_range_min; /* The value of the xyL x Range Min field of the Light xyL x Range state */ + u16_t xyl_x_range_max; /* The value of the xyL x Range Max field of the Light xyL x Range state */ + u16_t xyl_y_range_min; /* The value of the xyL y Range Min field of the Light xyL y Range state */ + u16_t xyl_y_range_max; /* The value of the xyL y Range Max field of the Light xyL y Range state */ +}; + +struct bt_mesh_light_xyl_set { + bool op_en; /* Indicate whether optional parameters included */ + u16_t xyl_lightness; /* The target value of the Light xyL Lightness state */ + u16_t xyl_x; /* The target value of the Light xyL x state */ + u16_t xyl_y; /* The target value of the Light xyL y state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_xyl_default_set { + u16_t lightness; /* The value of the Light Lightness Default state */ + u16_t xyl_x; /* The value of the Light xyL x Default state */ + u16_t xyl_y; /* The value of the Light xyL y Default state */ +}; + +struct bt_mesh_light_xyl_range_set { + u16_t xyl_x_range_min; /* The value of the xyL x Range Min field of the Light xyL x Range state */ + u16_t xyl_x_range_max; /* The value of the xyL x Range Max field of the Light xyL x Range state */ + u16_t xyl_y_range_min; /* The value of the xyL y Range Min field of the Light xyL y Range state */ + u16_t xyl_y_range_max; /* The value of the xyL y Range Max field of the Light xyL y Range state */ +}; + +/* Light LC Client Model Context */ +extern const struct bt_mesh_model_op light_lc_cli_op[]; + +/** @def BLE_MESH_MODEL_LIGHT_LC_CLI + * + * Define a new light lc client model. Note that this API needs + * to be repeated for each element which the application wants + * to have a light lc client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_light_lc_cli. + * + * @return New light lc client model instance. + */ +#define BLE_MESH_MODEL_LIGHT_LC_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_LIGHT_LC_CLI, \ + light_lc_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_light_lc_client_t; + +struct bt_mesh_light_lc_mode_status { + u8_t mode; /* The present value of the Light LC Mode state */ +}; + +struct bt_mesh_light_lc_om_status { + u8_t mode; /* The present value of the Light LC Occupancy Mode state */ +}; + +struct bt_mesh_light_lc_light_onoff_status { + bool op_en; /* Indicate whether optional parameters included */ + u8_t present_light_onoff; /* The present value of the Light LC Light OnOff state */ + u8_t target_light_onoff; /* The target value of the Light LC Light OnOff state (Optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_light_lc_property_status { + u16_t light_lc_property_id; /* Property ID identifying a Light LC Property */ + struct net_buf_simple *light_lc_property_value; /* Raw value for the Light LC Property */ +}; + +struct bt_mesh_light_lc_mode_set { + u8_t mode; /* The target value of the Light LC Mode state */ +}; + +struct bt_mesh_light_lc_om_set { + u8_t mode; /* The target value of the Light LC Occupancy Mode state */ +}; + +struct bt_mesh_light_lc_light_onoff_set { + bool op_en; /* Indicate whether optional parameters included */ + u8_t light_onoff; /* The target value of the Light LC Light OnOff state */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_light_lc_property_get { + u16_t light_lc_property_id; /* Property ID identifying a Light LC Property */ +}; + +struct bt_mesh_light_lc_property_set { + u16_t light_lc_property_id; /* Property ID identifying a Light LC Property */ + struct net_buf_simple *light_lc_property_value; /* Raw value for the Light LC Property */ +}; + +/** + * @brief This function is called to initialize light lightness client model user_data. + * + * @param[in] model: Pointer to light lightness client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lightness_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize light ctl client model user_data. + * + * @param[in] model: Pointer to light ctl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_ctl_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize light hsl client model user_data. + * + * @param[in] model: Pointer to light hsl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_hsl_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize light xyl client model user_data. + * + * @param[in] model: Pointer to light xyl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_xyl_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize light lc client model user_data. + * + * @param[in] model: Pointer to light lc client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lc_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light lightness client model user_data. + * + * @param[in] model: Pointer to light lightness client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lightness_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light ctl client model user_data. + * + * @param[in] model: Pointer to light ctl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_ctl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light hsl client model user_data. + * + * @param[in] model: Pointer to light hsl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_hsl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light xyl client model user_data. + * + * @param[in] model: Pointer to light xyl client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_xyl_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize light lc client model user_data. + * + * @param[in] model: Pointer to light lc client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_lc_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to get light states. + * + * @param[in] common: Message common information structure + * @param[in] get: Pointer of light get message value + * @param[out] status: Pointer of light status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); + +/** + * @brief This function is called to set light states. + * + * @param[in] common: Message common information structure + * @param[in] set: Pointer of light set message value + * @param[out] status: Pointer of light status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_light_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); + +#ifdef __cplusplus +} +#endif + +#endif /* _LIGHTING_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h new file mode 100644 index 000000000..135eaf799 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/sensor_client.h @@ -0,0 +1,182 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Sensor Client Model APIs. + */ + +#ifndef _SENSOR_CLIENT_H_ +#define _SENSOR_CLIENT_H_ + +#include "client_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Sensor Client Model Context */ +extern const struct bt_mesh_model_op sensor_cli_op[]; + +/** @def BLE_MESH_MODEL_SENSOR_CLI + * + * Define a new sensor client model. Note that this API needs to + * be repeated for each element which the application wants to + * have a sensor client model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_sensor_cli. + * + * @return New sensor client model instance. + */ +#define BLE_MESH_MODEL_SENSOR_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SENSOR_CLI, \ + sensor_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_sensor_client_t; +typedef bt_mesh_client_internal_data_t sensor_internal_data_t; + +struct bt_mesh_sensor_descriptor_status { + struct net_buf_simple *descriptor; /* Sequence of 8-octet sensor descriptors (optional) */ +}; + +struct bt_mesh_sensor_cadence_status { + u16_t property_id; /* Property for the sensor */ + struct net_buf_simple *sensor_cadence_value; /* Value of sensor cadence state */ +}; + +struct bt_mesh_sensor_settings_status { + u16_t sensor_property_id; /* Property ID identifying a sensor */ + struct net_buf_simple *sensor_setting_property_ids; /* A sequence of N sensor setting property IDs (optional) */ +}; + +struct bt_mesh_sensor_setting_status { + bool op_en; /* Indicate whether optional parameters included */ + u16_t sensor_property_id; /* Property ID identifying a sensor */ + u16_t sensor_setting_property_id; /* Setting ID identifying a setting within a sensor */ + u8_t sensor_setting_access; /* Read/Write access rights for the setting (optional) */ + struct net_buf_simple *sensor_setting_raw; /* Raw value for the setting */ +}; + +struct bt_mesh_sensor_status { + struct net_buf_simple *marshalled_sensor_data; /* Value of sensor data state (optional) */ +}; + +struct bt_mesh_sensor_column_status { + u16_t property_id; /* Property identifying a sensor and the Y axis */ + struct net_buf_simple *sensor_column_value; /* Left values of sensor column status */ +}; + +struct bt_mesh_sensor_series_status { + u16_t property_id; /* Property identifying a sensor and the Y axis */ + struct net_buf_simple *sensor_series_value; /* Left values of sensor series status */ +}; + +struct bt_mesh_sensor_descriptor_get { + bool op_en; /* Indicate whether optional parameters included */ + u16_t property_id; /* Property ID for the sensor (optional) */ +}; + +struct bt_mesh_sensor_cadence_get { + u16_t property_id; /* Property ID for the sensor */ +}; + +struct bt_mesh_sensor_cadence_set { + u16_t property_id; /* Property ID for the sensor */ + u8_t fast_cadence_period_divisor : 7, /* Divisor for the publish period */ + status_trigger_type : 1; /* The unit and format of the Status Trigger Delta fields */ + struct net_buf_simple *status_trigger_delta_down; /* Delta down value that triggers a status message */ + struct net_buf_simple *status_trigger_delta_up; /* Delta up value that triggers a status message */ + u8_t status_min_interval; /* Minimum interval between two consecutive Status messages */ + struct net_buf_simple *fast_cadence_low; /* Low value for the fast cadence range */ + struct net_buf_simple *fast_cadence_high; /* Fast value for the fast cadence range */ +}; + +struct bt_mesh_sensor_settings_get { + u16_t sensor_property_id; /* Property ID for the sensor */ +}; + +struct bt_mesh_sensor_setting_get { + u16_t sensor_property_id; /* Property ID identifying a sensor */ + u16_t sensor_setting_property_id; /* Setting ID identifying a setting within a sensor */ +}; + +struct bt_mesh_sensor_setting_set { + u16_t sensor_property_id; /* Property ID identifying a sensor */ + u16_t sensor_setting_property_id; /* Setting ID identifying a setting within a sensor */ + struct net_buf_simple *sensor_setting_raw; /* Raw value for the setting */ +}; + +struct bt_mesh_sensor_get { + bool op_en; /* Indicate whether optional parameters included */ + u16_t property_id; /* Property ID for the sensor (optional) */ +}; + +struct bt_mesh_sensor_column_get { + u16_t property_id; /* Property identifying a sensor */ + struct net_buf_simple *raw_value_x; /* Raw value identifying a column */ +}; + +struct bt_mesh_sensor_series_get { + bool op_en; /* Indicate whether optional parameters included */ + u16_t property_id; /* Property identifying a sensor */ + struct net_buf_simple *raw_value_x1; /* Raw value identifying a starting column (optional) */ + struct net_buf_simple *raw_value_x2; /* Raw value identifying a ending column (C.1) */ +}; + +/** + * @brief This function is called to initialize sensor client model user_data. + * + * @param[in] model: Pointer to sensor client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize sensor client model user_data. + * + * @param[in] model: Pointer to sensor client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_sensor_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to get sensor states. + * + * @param[in] common: Message common information structure + * @param[in] get: Pointer of sensor get message value + * @param[out] status: Pointer of sensor status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); + +/** + * @brief This function is called to set sensor states. + * + * @param[in] common: Message common information structure + * @param[in] set: Pointer of sensor set message value + * @param[out] status: Pointer of sensor status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); + +#ifdef __cplusplus +} +#endif + +#endif /* _SENSOR_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h new file mode 100644 index 000000000..11a917f41 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/include/time_scene_client.h @@ -0,0 +1,292 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** @file + * @brief Bluetooth Mesh Time and Scene Client Model APIs. + */ + +#ifndef _TIME_SCENE_CLIENT_H_ +#define _TIME_SCENE_CLIENT_H_ + +#include "client_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Time scene client model common structure */ +typedef bt_mesh_client_user_data_t bt_mesh_time_scene_client_t; +typedef bt_mesh_client_internal_data_t time_scene_internal_data_t; + +/* Time Client Model Context */ +extern const struct bt_mesh_model_op time_cli_op[]; + +/** @def BLE_MESH_MODEL_TIME_CLI + * + * Define a new time client model. Note that this API needs to + * be repeated for each element which the application wants to + * have a time model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_time_cli. + * + * @return New time client model instance. + */ +#define BLE_MESH_MODEL_TIME_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_TIME_CLI, \ + time_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_time_client_t; + +struct bt_mesh_time_status { + u8_t tai_seconds[5]; /* The current TAI time in seconds */ + u8_t sub_second; /* The sub-second time in units of 1/256 second */ + u8_t uncertainty; /* The estimated uncertainty in 10-millisecond steps */ + u16_t time_authority : 1; /* 0 = No Time Authority, 1 = Time Authority */ + u16_t tai_utc_delta : 15; /* Current difference between TAI and UTC in seconds */ + u8_t time_zone_offset; /* The local time zone offset in 15-minute increments */ +}; + +struct bt_mesh_time_zone_status { + u8_t time_zone_offset_curr; /* Current local time zone offset */ + u8_t time_zone_offset_new; /* Upcoming local time zone offset */ + u8_t tai_zone_change[5]; /* TAI Seconds time of the upcoming Time Zone Offset change */ +}; + +struct bt_mesh_tai_utc_delta_status { + u16_t tai_utc_delta_curr : 15; /* Current difference between TAI and UTC in seconds */ + u16_t padding_1 : 1; /* Always 0b0. Other values are Prohibited. */ + u16_t tai_utc_delta_new : 15; /* Upcoming difference between TAI and UTC in seconds */ + u16_t padding_2 : 1; /* Always 0b0. Other values are Prohibited. */ + u8_t tai_delta_change[5]; /* TAI Seconds time of the upcoming TAI-UTC Delta change */ +}; + +struct bt_mesh_time_role_status { + u8_t time_role; /* The Time Role for the element */ +}; + +struct bt_mesh_time_set { + u8_t tai_seconds[5]; /* The current TAI time in seconds */ + u8_t sub_second; /* The sub-second time in units of 1/256 second */ + u8_t uncertainty; /* The estimated uncertainty in 10-millisecond steps */ + u16_t time_authority : 1; /* 0 = No Time Authority, 1 = Time Authority */ + u16_t tai_utc_delta : 15; /* Current difference between TAI and UTC in seconds */ + u8_t time_zone_offset; /* The local time zone offset in 15-minute increments */ +}; + +struct bt_mesh_time_zone_set { + u8_t time_zone_offset_new; /* Upcoming local time zone offset */ + u8_t tai_zone_change[5]; /* TAI Seconds time of the upcoming Time Zone Offset change */ +}; + +struct bt_mesh_tai_utc_delta_set { + u16_t tai_utc_delta_new : 15; /* Upcoming difference between TAI and UTC in seconds */ + u16_t padding : 1; /* Always 0b0. Other values are Prohibited. */ + u8_t tai_delta_change[5]; /* TAI Seconds time of the upcoming TAI-UTC Delta change */ +}; + +struct bt_mesh_time_role_set { + u8_t time_role; /* The Time Role for the element */ +}; + +/* Scene Client Model Context */ +extern const struct bt_mesh_model_op scene_cli_op[]; + +/** @def BLE_MESH_MODEL_SCENE_CLI + * + * Define a new scene client model. Note that this API needs to + * be repeated for each element which the application wants to + * have a scene model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_scene_cli. + * + * @return New scene client model instance. + */ +#define BLE_MESH_MODEL_SCENE_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SCENE_CLI, \ + scene_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_scene_client_t; + +struct bt_mesh_scene_status { + bool op_en; /* Indicate whether optional parameters included */ + u8_t status_code; /* Status code for the last operation */ + u16_t current_scene; /* Scene Number of a current scene */ + u16_t target_scene; /* Scene Number of a target scene (optional) */ + u8_t remain_time; /* Time to complete state transition (C.1) */ +}; + +struct bt_mesh_scene_register_status { + u8_t status_code; /* Status code for the previous operation */ + u16_t current_scene; /* Scene Number of a current scene */ + struct net_buf_simple *scenes; /* A list of scenes stored within an element */ +}; + +struct bt_mesh_scene_store { + u16_t scene_number; /* The number of the scene to be stored */ +}; + +struct bt_mesh_scene_recall { + bool op_en; /* Indicate whether optional parameters included */ + u16_t scene_number; /* The number of the scene to be recalled */ + u8_t tid; /* Transaction Identifier */ + u8_t trans_time; /* Time to complete state transition (optional) */ + u8_t delay; /* Indicate message execution delay (C.1) */ +}; + +struct bt_mesh_scene_delete { + u16_t scene_number; /* The number of the scene to be deleted */ +}; + +/* Scheduler Client Model Context */ +extern const struct bt_mesh_model_op scheduler_cli_op[]; + +/** @def BLE_MESH_MODEL_SCHEDULER_CLI + * + * Define a new scheduler client model. Note that this API needs to + * be repeated for each element which the application wants to + * have a scheduler model on. + * @param cli_pub Pointer to a unique struct bt_mesh_model_pub. + * @param cli_data Pointer to a unique struct bt_mesh_scheduler_cli. + * + * @return New scheduler client model instance. + */ +#define BLE_MESH_MODEL_SCHEDULER_CLI(cli_pub, cli_data) \ + BLE_MESH_MODEL(BLE_MESH_MODEL_ID_SCHEDULER_CLI, \ + scheduler_cli_op, cli_pub, cli_data) + +typedef bt_mesh_client_user_data_t bt_mesh_scheduler_client_t; + +struct bt_mesh_scheduler_status { + u16_t schedules; /* Bit field indicating defined Actions in the Schedule Register */ +}; + +struct bt_mesh_scheduler_act_status { + u64_t index : 4; /* Enumerates (selects) a Schedule Register entry */ + u64_t year : 7; /* Scheduled year for the action */ + u64_t month : 12; /* Scheduled month for the action */ + u64_t day : 5; /* Scheduled day of the month for the action */ + u64_t hour : 5; /* Scheduled hour for the action */ + u64_t minute : 6; /* Scheduled minute for the action */ + u64_t second : 6; /* Scheduled second for the action */ + u64_t day_of_week : 7; /* Schedule days of the week for the action */ + u64_t action : 4; /* Action to be performed at the scheduled time */ + u64_t trans_time : 8; /* Transition time for this action */ + u16_t scene_number; /* Transition time for this action */ +}; + +struct bt_mesh_scheduler_act_get { + u8_t index; /* Index of the Schedule Register entry to get */ +}; + +struct bt_mesh_scheduler_act_set { + u64_t index : 4; /* Index of the Schedule Register entry to set */ + u64_t year : 7; /* Scheduled year for the action */ + u64_t month : 12; /* Scheduled month for the action */ + u64_t day : 5; /* Scheduled day of the month for the action */ + u64_t hour : 5; /* Scheduled hour for the action */ + u64_t minute : 6; /* Scheduled minute for the action */ + u64_t second : 6; /* Scheduled second for the action */ + u64_t day_of_week : 7; /* Schedule days of the week for the action */ + u64_t action : 4; /* Action to be performed at the scheduled time */ + u64_t trans_time : 8; /* Transition time for this action */ + u16_t scene_number; /* Transition time for this action */ +}; + +/** + * @brief This function is called to initialize time client model user_data. + * + * @param[in] model: Pointer to time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_time_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize scene client model user_data. + * + * @param[in] model: Pointer to scene client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scene_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to initialize scheduler client model user_data. + * + * @param[in] model: Pointer to scheduler client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scheduler_cli_init(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize time client model user_data. + * + * @param[in] model: Pointer to time client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_time_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize scene client model user_data. + * + * @param[in] model: Pointer to scene client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scene_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to de-initialize scheduler client model user_data. + * + * @param[in] model: Pointer to scheduler client model + * @param[in] primary: Whether belongs to primary element + * + * @return Zero-success, other-fail + */ +int bt_mesh_scheduler_cli_deinit(struct bt_mesh_model *model, bool primary); + +/** + * @brief This function is called to get scene states. + * + * @param[in] common: Message common information structure + * @param[in] get: Pointer of time scene get message value + * @param[out] status: Pointer of time scene status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_time_scene_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status); + +/** + * @brief This function is called to set scene states. + * + * @param[in] common: Message common information structure + * @param[in] set: Pointer of time scene set message value + * @param[out] status: Pointer of time scene status message value + * + * @return Zero-success, other-fail + */ +int bt_mesh_time_scene_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status); + +#ifdef __cplusplus +} +#endif + +#endif /* _TIME_SCENE_CLIENT_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c new file mode 100644 index 000000000..5ab225ab5 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/lighting_client.c @@ -0,0 +1,1458 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_lighting_model.h" + +#include "model_opcode.h" +#include "lighting_client.h" + +/** The following are the macro definitions of lighting client + * model messages length, and a message is composed of three + * parts: Opcode + msg_value + MIC + */ +/* Light lightness client messages length */ +#define BLE_MESH_LIGHT_LIGHTNESS_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_LINEAR_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_LINEAR_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_LAST_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_DEFAULT_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_DEFAULT_SET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_RANGE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_LIGHTNESS_RANGE_SET_MSG_LEN (2 + 4 + 4) + +/* Light CTL client messages length */ +#define BLE_MESH_LIGHT_CTL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_CTL_SET_MSG_LEN (2 + 9 + 4) +#define BLE_MESH_LIGHT_CTL_TEMPERATURE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_CTL_TEMPERATURE_SET_MSG_LEN (2 + 7 + 4) +#define BLE_MESH_LIGHT_CTL_TEMPERATURE_RANGE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_CTL_TEMPERATURE_RANGE_SET_MSG_LEN (2 + 4 + 4) +#define BLE_MESH_LIGHT_CTL_DEFAULT_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_CTL_DEFAULT_SET_MSG_LEN (2 + 6 + 4) + +/* Light HSL client messages length */ +#define BLE_MESH_LIGHT_HSL_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_SET_MSG_LEN (2 + 9 + 4) +#define BLE_MESH_LIGHT_HSL_TARGET_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_HUE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_HUE_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_LIGHT_HSL_SATURATION_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_SATURATION_SET_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_LIGHT_HSL_DEFAULT_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_DEFAULT_SET_MSG_LEN (2 + 6 + 4) +#define BLE_MESH_LIGHT_HSL_RANGE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_LIGHT_HSL_RANGE_SET_MSG_LEN (2 + 8 + 4) + +/* Light xyL client messages length */ +#define BLE_MESH_LIGHT_XYL_SET_MSG_LEN (2 + 9 + 4) +#define BLE_MESH_LIGHT_XYL_DEFAULT_SET_MSG_LEN (2 + 6 + 4) +#define BLE_MESH_LIGHT_XYL_RANGE_SET_MSG_LEN (2 + 8 + 4) + +/* Light LC client messages length */ +#define BLE_MESH_LIGHT_LC_MODE_SET_MSG_LEN (2 + 1 + 4) +#define BLE_MESH_LIGHT_LC_OM_SET_MSG_LEN (2 + 1 + 4) +#define BLE_MESH_LIGHT_LC_LIGHT_ONOFF_SET_MSG_LEN (2 + 4 + 4) +#define BLE_MESH_LIGHT_LC_PROPERTY_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_LIGHT_LC_PROPERTY_SET_MSG_LEN /* variable */ + +#define BLE_MESH_LIGHT_GET_STATE_MSG_LEN (2 + 2 + 4) + +static const bt_mesh_client_op_pair_t light_op_pair[] = { + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_GET, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_SET, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SET, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_GET, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_SET, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET, BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS }, + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS }, +}; + +static bt_mesh_mutex_t light_client_lock; + +static void bt_mesh_light_client_mutex_new(void) +{ + if (!light_client_lock.mutex) { + bt_mesh_mutex_create(&light_client_lock); + } +} + +static void bt_mesh_light_client_mutex_free(void) +{ + bt_mesh_mutex_free(&light_client_lock); +} + +static void bt_mesh_light_client_lock(void) +{ + bt_mesh_mutex_lock(&light_client_lock); +} + +static void bt_mesh_light_client_unlock(void) +{ + bt_mesh_mutex_unlock(&light_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive light status message timeout"); + + bt_mesh_light_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_lighting_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_LIGHTING_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_light_client_unlock(); + + return; +} + +static void light_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + bt_mesh_client_node_t *node = NULL; + u8_t *val = NULL; + u8_t evt = 0xFF; + size_t len = 0U; + + BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS: { + struct bt_mesh_light_lightness_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("%s, Invalid Light Lightness Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_lightness = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_lightness = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lightness_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS: { + struct bt_mesh_light_lightness_linear_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("%s, Invalid Light Lightness Linear Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_linear_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_lightness = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_lightness = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lightness_linear_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS: { + struct bt_mesh_light_lightness_last_status *status = NULL; + if (buf->len != 2) { + BT_ERR("%s, Invalid Light Lightness Last Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_last_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->lightness = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lightness_last_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS: { + struct bt_mesh_light_lightness_default_status *status = NULL; + if (buf->len != 2) { + BT_ERR("%s, Invalid Light Lightness Default Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_default_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->lightness = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lightness_default_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS: { + struct bt_mesh_light_lightness_range_status *status = NULL; + if (buf->len != 5) { + BT_ERR("%s, Invalid Light Lightness Range Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lightness_range_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->range_min = net_buf_simple_pull_le16(buf); + status->range_max = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lightness_range_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS: { + struct bt_mesh_light_ctl_status *status = NULL; + if (buf->len != 4 && buf->len != 9) { + BT_ERR("%s, Invalid Light CTL Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_ctl_lightness = net_buf_simple_pull_le16(buf); + status->present_ctl_temperature = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_ctl_lightness = net_buf_simple_pull_le16(buf); + status->target_ctl_temperature = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_ctl_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS: { + struct bt_mesh_light_ctl_temperature_status *status = NULL; + if (buf->len != 4 && buf->len != 9) { + BT_ERR("%s, Invalid Light CTL Temperature Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_temperature_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_ctl_temperature = net_buf_simple_pull_le16(buf); + status->present_ctl_delta_uv = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_ctl_temperature = net_buf_simple_pull_le16(buf); + status->target_ctl_delta_uv = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_ctl_temperature_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS: { + struct bt_mesh_light_ctl_temperature_range_status *status = NULL; + if (buf->len != 5) { + BT_ERR("%s, Invalid Light CTL Temperature Range Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_temperature_range_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->range_min = net_buf_simple_pull_le16(buf); + status->range_max = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_ctl_temperature_range_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS: { + struct bt_mesh_light_ctl_default_status *status = NULL; + if (buf->len != 6) { + BT_ERR("%s, Invalid Light CTL Default Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_ctl_default_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->lightness = net_buf_simple_pull_le16(buf); + status->temperature = net_buf_simple_pull_le16(buf); + status->delta_uv = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_ctl_default_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS: { + struct bt_mesh_light_hsl_status *status = NULL; + if (buf->len != 6 && buf->len != 7) { + BT_ERR("%s, Invalid Light HSL Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->hsl_lightness = net_buf_simple_pull_le16(buf); + status->hsl_hue = net_buf_simple_pull_le16(buf); + status->hsl_saturation = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS: { + struct bt_mesh_light_hsl_target_status *status = NULL; + if (buf->len != 6 && buf->len != 7) { + BT_ERR("%s, Invalid Light HSL Target Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_target_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->hsl_lightness_target = net_buf_simple_pull_le16(buf); + status->hsl_hue_target = net_buf_simple_pull_le16(buf); + status->hsl_saturation_target = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_target_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS: { + struct bt_mesh_light_hsl_hue_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("%s, Invalid Light HSL Hue Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_hue_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_hue = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_hue = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_hue_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS: { + struct bt_mesh_light_hsl_saturation_status *status = NULL; + if (buf->len != 2 && buf->len != 5) { + BT_ERR("%s, Invalid Light HSL Saturation Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_saturation_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_saturation = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_saturation = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_saturation_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS: { + struct bt_mesh_light_hsl_default_status *status = NULL; + if (buf->len != 6) { + BT_ERR("%s, Invalid Light HSL Default Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_default_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->lightness = net_buf_simple_pull_le16(buf); + status->hue = net_buf_simple_pull_le16(buf); + status->saturation = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_default_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS: { + struct bt_mesh_light_hsl_range_status *status = NULL; + if (buf->len != 9) { + BT_ERR("%s, Invalid Light HSL Range Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_hsl_range_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->hue_range_min = net_buf_simple_pull_le16(buf); + status->hue_range_max = net_buf_simple_pull_le16(buf); + status->saturation_range_min = net_buf_simple_pull_le16(buf); + status->saturation_range_max = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_hsl_range_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS: { + struct bt_mesh_light_xyl_status *status = NULL; + if (buf->len != 6 && buf->len != 7) { + BT_ERR("%s, Invalid Light xyL Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->xyl_lightness = net_buf_simple_pull_le16(buf); + status->xyl_x = net_buf_simple_pull_le16(buf); + status->xyl_y = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_xyl_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS: { + struct bt_mesh_light_xyl_target_status *status = NULL; + if (buf->len != 6 && buf->len != 7) { + BT_ERR("%s, Invalid Light xyL Target Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_target_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->target_xyl_lightness = net_buf_simple_pull_le16(buf); + status->target_xyl_x = net_buf_simple_pull_le16(buf); + status->target_xyl_y = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_xyl_target_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS: { + struct bt_mesh_light_xyl_default_status *status = NULL; + if (buf->len != 6) { + BT_ERR("%s, Invalid Light xyL Default Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_default_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->lightness = net_buf_simple_pull_le16(buf); + status->xyl_x = net_buf_simple_pull_le16(buf); + status->xyl_y = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_xyl_default_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS: { + struct bt_mesh_light_xyl_range_status *status = NULL; + if (buf->len != 9) { + BT_ERR("%s, Invalid Light xyL Range Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_xyl_range_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->xyl_x_range_min = net_buf_simple_pull_le16(buf); + status->xyl_x_range_max = net_buf_simple_pull_le16(buf); + status->xyl_y_range_min = net_buf_simple_pull_le16(buf); + status->xyl_y_range_max = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_xyl_range_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS: { + struct bt_mesh_light_lc_mode_status *status = NULL; + if (buf->len != 1) { + BT_ERR("%s, Invalid Light LC Mode Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_mode_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->mode = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lc_mode_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS: { + struct bt_mesh_light_lc_om_status *status = NULL; + if (buf->len != 1) { + BT_ERR("%s, Invalid Light LC OM Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_om_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->mode = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lc_om_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS: { + struct bt_mesh_light_lc_light_onoff_status *status = NULL; + if (buf->len != 1 && buf->len != 3) { + BT_ERR("%s, Invalid Light LC Light OnOff Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_light_onoff_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->present_light_onoff = net_buf_simple_pull_u8(buf); + if (buf->len) { + status->op_en = true; + status->target_light_onoff = net_buf_simple_pull_u8(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lc_light_onoff_status); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: { + struct bt_mesh_light_lc_property_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_light_lc_property_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->light_lc_property_id = net_buf_simple_pull_le16(buf); + status->light_lc_property_value = bt_mesh_alloc_buf(buf->len); + if (!status->light_lc_property_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->light_lc_property_value, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_light_lc_property_status); + break; + } + default: + BT_ERR("%s, Not a Lighting Status message opcode", __func__); + return; + } + + buf->data = val; + buf->len = len; + + bt_mesh_light_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); + if (!node) { + BT_DBG("Unexpected light status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: + evt = BTC_BLE_MESH_EVT_LIGHTING_CLIENT_GET_STATE; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: + evt = BTC_BLE_MESH_EVT_LIGHTING_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_lighting_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); + } + } + + bt_mesh_light_client_unlock(); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS: { + struct bt_mesh_light_lc_property_status *status; + status = (struct bt_mesh_light_lc_property_status *)val; + bt_mesh_free_buf(status->light_lc_property_value); + break; + } + default: + break; + } + + bt_mesh_free(val); + + return; +} + +const struct bt_mesh_model_op light_lightness_cli_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS, 5, light_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op light_ctl_cli_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS, 4, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS, 4, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS, 5, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS, 6, light_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op light_hsl_cli_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS, 2, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS, 9, light_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op light_xyl_cli_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS, 6, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS, 9, light_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op light_lc_cli_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS, 1, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS, 1, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS, 1, light_status }, + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS, 2, light_status }, + BLE_MESH_MODEL_OP_END, +}; + +static int light_get_state(bt_mesh_client_common_param_t *common, void *value) +{ + NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_LIGHT_GET_STATE_MSG_LEN); + int err = 0; + + bt_mesh_model_msg_init(&msg, common->opcode); + + if (value) { + switch (common->opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: { + struct bt_mesh_light_lc_property_get *get; + get = (struct bt_mesh_light_lc_property_get *)value; + net_buf_simple_add_le16(&msg, get->light_lc_property_id); + break; + } + default: + BT_DBG("This lighting message should be sent with NULL get pointer"); + break; + } + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, &msg, + timeout_handler, common->msg_timeout, true, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Lighting Client Get message (err %d)", __func__, err); + } + + return err; +} + +static int light_set_state(bt_mesh_client_common_param_t *common, + void *value, u16_t value_len, bool need_ack) +{ + struct net_buf_simple *msg = NULL; + int err = 0; + + msg = bt_mesh_alloc_buf(value_len); + if (!msg) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + bt_mesh_model_msg_init(msg, common->opcode); + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK: { + struct bt_mesh_light_lightness_set *set; + set = (struct bt_mesh_light_lightness_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK: { + struct bt_mesh_light_lightness_linear_set *set; + set = (struct bt_mesh_light_lightness_linear_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK: { + struct bt_mesh_light_lightness_default_set *set; + set = (struct bt_mesh_light_lightness_default_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK: { + struct bt_mesh_light_lightness_range_set *set; + set = (struct bt_mesh_light_lightness_range_set *)value; + net_buf_simple_add_le16(msg, set->range_min); + net_buf_simple_add_le16(msg, set->range_max); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK: { + struct bt_mesh_light_ctl_set *set; + set = (struct bt_mesh_light_ctl_set *)value; + net_buf_simple_add_le16(msg, set->ctl_lightness); + net_buf_simple_add_le16(msg, set->ctl_temperature); + net_buf_simple_add_le16(msg, set->ctl_delta_uv); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK: { + struct bt_mesh_light_ctl_temperature_set *set; + set = (struct bt_mesh_light_ctl_temperature_set *)value; + net_buf_simple_add_le16(msg, set->ctl_temperature); + net_buf_simple_add_le16(msg, set->ctl_delta_uv); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK: { + struct bt_mesh_light_ctl_temperature_range_set *set; + set = (struct bt_mesh_light_ctl_temperature_range_set *)value; + net_buf_simple_add_le16(msg, set->range_min); + net_buf_simple_add_le16(msg, set->range_max); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK: { + struct bt_mesh_light_ctl_default_set *set; + set = (struct bt_mesh_light_ctl_default_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + net_buf_simple_add_le16(msg, set->temperature); + net_buf_simple_add_le16(msg, set->delta_uv); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK: { + struct bt_mesh_light_hsl_set *set; + set = (struct bt_mesh_light_hsl_set *)value; + net_buf_simple_add_le16(msg, set->hsl_lightness); + net_buf_simple_add_le16(msg, set->hsl_hue); + net_buf_simple_add_le16(msg, set->hsl_saturation); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK: { + struct bt_mesh_light_hsl_hue_set *set; + set = (struct bt_mesh_light_hsl_hue_set *)value; + net_buf_simple_add_le16(msg, set->hue); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK: { + struct bt_mesh_light_hsl_saturation_set *set; + set = (struct bt_mesh_light_hsl_saturation_set *)value; + net_buf_simple_add_le16(msg, set->saturation); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK: { + struct bt_mesh_light_hsl_default_set *set; + set = (struct bt_mesh_light_hsl_default_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + net_buf_simple_add_le16(msg, set->hue); + net_buf_simple_add_le16(msg, set->saturation); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK: { + struct bt_mesh_light_hsl_range_set *set; + set = (struct bt_mesh_light_hsl_range_set *)value; + net_buf_simple_add_le16(msg, set->hue_range_min); + net_buf_simple_add_le16(msg, set->hue_range_max); + net_buf_simple_add_le16(msg, set->saturation_range_min); + net_buf_simple_add_le16(msg, set->saturation_range_max); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK: { + struct bt_mesh_light_xyl_set *set; + set = (struct bt_mesh_light_xyl_set *)value; + net_buf_simple_add_le16(msg, set->xyl_lightness); + net_buf_simple_add_le16(msg, set->xyl_x); + net_buf_simple_add_le16(msg, set->xyl_y); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK: { + struct bt_mesh_light_xyl_default_set *set; + set = (struct bt_mesh_light_xyl_default_set *)value; + net_buf_simple_add_le16(msg, set->lightness); + net_buf_simple_add_le16(msg, set->xyl_x); + net_buf_simple_add_le16(msg, set->xyl_y); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK: { + struct bt_mesh_light_xyl_range_set *set; + set = (struct bt_mesh_light_xyl_range_set *)value; + net_buf_simple_add_le16(msg, set->xyl_x_range_min); + net_buf_simple_add_le16(msg, set->xyl_x_range_max); + net_buf_simple_add_le16(msg, set->xyl_y_range_min); + net_buf_simple_add_le16(msg, set->xyl_y_range_max); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK: { + struct bt_mesh_light_lc_mode_set *set; + set = (struct bt_mesh_light_lc_mode_set *)value; + net_buf_simple_add_u8(msg, set->mode); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK: { + struct bt_mesh_light_lc_om_set *set; + set = (struct bt_mesh_light_lc_om_set *)value; + net_buf_simple_add_u8(msg, set->mode); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK: { + struct bt_mesh_light_lc_light_onoff_set *set; + set = (struct bt_mesh_light_lc_light_onoff_set *)value; + net_buf_simple_add_u8(msg, set->light_onoff); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK: { + struct bt_mesh_light_lc_property_set *set; + set = (struct bt_mesh_light_lc_property_set *)value; + net_buf_simple_add_le16(msg, set->light_lc_property_id); + net_buf_simple_add_mem(msg, set->light_lc_property_value->data, set->light_lc_property_value->len); + break; + } + default: + BT_ERR("%s, Not a Lighting Client Set message opcode", __func__); + err = -EINVAL; + goto end; + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, msg, + timeout_handler, common->msg_timeout, need_ack, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Lighting Client Set message (err %d)", __func__, err); + } + +end: + bt_mesh_free_buf(msg); + + return err; +} + +int bt_mesh_light_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) +{ + bt_mesh_light_client_t *client = NULL; + + if (!common || !common->model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_light_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Lighting Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET: + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET: + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET: + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET: + if (!get) { + BT_ERR("%s, Lighting lc_property_get is NULL", __func__); + return -EINVAL; + } + break; + default: + BT_ERR("%s, Not a Lighting Client Get message opcode", __func__); + return -EINVAL; + } + + return light_get_state(common, get); +} + +int bt_mesh_light_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) +{ + bt_mesh_light_client_t *client = NULL; + u16_t length = 0U; + bool need_ack = false; + + if (!common || !common->model || !set) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_light_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Lighting Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK: { + struct bt_mesh_light_lightness_set *value; + value = (struct bt_mesh_light_lightness_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light Lightness Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_LIGHTNESS_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK: { + struct bt_mesh_light_lightness_linear_set *value; + value = (struct bt_mesh_light_lightness_linear_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light Lightness Linear Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_LIGHTNESS_LINEAR_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK: + length = BLE_MESH_LIGHT_LIGHTNESS_DEFAULT_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK: { + struct bt_mesh_light_lightness_range_set *value; + value = (struct bt_mesh_light_lightness_range_set *)set; + if (value->range_min > value->range_max) { + BT_ERR("%s, Light Lightness Range Set range min is greater than range max", __func__); + return -EINVAL; + } + length = BLE_MESH_LIGHT_LIGHTNESS_RANGE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK: { + struct bt_mesh_light_ctl_set *value; + value = (struct bt_mesh_light_ctl_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light CTL Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_CTL_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK: { + struct bt_mesh_light_ctl_temperature_set *value; + value = (struct bt_mesh_light_ctl_temperature_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light CTL Temperature Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_CTL_TEMPERATURE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK: { + struct bt_mesh_light_ctl_temperature_range_set *value; + value = (struct bt_mesh_light_ctl_temperature_range_set *)set; + if (value->range_min > value->range_max) { + BT_ERR("%s, Light CTL Temperature Range Set range min is greater than range max", __func__); + return -EINVAL; + } + length = BLE_MESH_LIGHT_CTL_TEMPERATURE_RANGE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK: + length = BLE_MESH_LIGHT_CTL_DEFAULT_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK: { + struct bt_mesh_light_hsl_set *value; + value = (struct bt_mesh_light_hsl_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light HSL Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_HSL_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK: { + struct bt_mesh_light_hsl_hue_set *value; + value = (struct bt_mesh_light_hsl_hue_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light HSL Hue Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_HSL_HUE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK: { + struct bt_mesh_light_hsl_saturation_set *value; + value = (struct bt_mesh_light_hsl_saturation_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light HSL Saturation Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_HSL_SATURATION_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK: + length = BLE_MESH_LIGHT_HSL_DEFAULT_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK: { + struct bt_mesh_light_hsl_range_set *value; + value = (struct bt_mesh_light_hsl_range_set *)set; + if (value->hue_range_min > value->hue_range_max || + value->saturation_range_min > value->saturation_range_max) { + BT_ERR("%s, Light HSL Range Set range min is greater than range max", __func__); + return -EINVAL; + } + length = BLE_MESH_LIGHT_HSL_RANGE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK: { + struct bt_mesh_light_xyl_set *value; + value = (struct bt_mesh_light_xyl_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light xyL Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_XYL_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK: + length = BLE_MESH_LIGHT_XYL_DEFAULT_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK: { + struct bt_mesh_light_xyl_range_set *value; + value = (struct bt_mesh_light_xyl_range_set *)set; + if (value->xyl_x_range_min > value->xyl_x_range_max || + value->xyl_y_range_min > value->xyl_y_range_max) { + BT_ERR("%s, Light xyL Range Set range min is greater than range max", __func__); + return -EINVAL; + } + length = BLE_MESH_LIGHT_XYL_RANGE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK: + length = BLE_MESH_LIGHT_LC_MODE_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK: + length = BLE_MESH_LIGHT_LC_OM_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK: { + struct bt_mesh_light_lc_light_onoff_set *value; + value = (struct bt_mesh_light_lc_light_onoff_set *)set; + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Light LC Light OnOff Set transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_LIGHT_LC_LIGHT_ONOFF_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK: { + struct bt_mesh_light_lc_property_set *value; + value = (struct bt_mesh_light_lc_property_set *)set; + if (!value->light_lc_property_value) { + BT_ERR("%s, Lighting light_lc_property_value is NULL", __func__); + return -EINVAL; + } + length = (1 + 2 + value->light_lc_property_value->len + 4); + break; + } + default: + BT_ERR("%s, Not a Lighting Client Set message opcode", __func__); + return -EINVAL; + } + + return light_set_state(common, set, length, need_ack); +} + +static int light_client_init(struct bt_mesh_model *model, bool primary) +{ + light_internal_data_t *internal = NULL; + bt_mesh_light_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_light_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Lighting Client user_data is NULL", __func__); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(light_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(light_op_pair); + client->op_pair = light_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + bt_mesh_light_client_mutex_new(); + + return 0; +} + +int bt_mesh_light_lightness_cli_init(struct bt_mesh_model *model, bool primary) +{ + return light_client_init(model, primary); +} + +int bt_mesh_light_ctl_cli_init(struct bt_mesh_model *model, bool primary) +{ + return light_client_init(model, primary); +} + +int bt_mesh_light_hsl_cli_init(struct bt_mesh_model *model, bool primary) +{ + return light_client_init(model, primary); +} + +int bt_mesh_light_xyl_cli_init(struct bt_mesh_model *model, bool primary) +{ + return light_client_init(model, primary); +} + +int bt_mesh_light_lc_cli_init(struct bt_mesh_model *model, bool primary) +{ + return light_client_init(model, primary); +} + +static int light_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_light_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_light_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Lighting Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_light_client_mutex_free(); + + return 0; +} + +int bt_mesh_light_lightness_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_ctl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_hsl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_xyl_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} + +int bt_mesh_light_lc_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_client_deinit(model, primary); +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c new file mode 100644 index 000000000..18e1e389e --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/sensor_client.c @@ -0,0 +1,650 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_sensor_model.h" + +#include "model_opcode.h" +#include "sensor_client.h" + +/** The following are the macro definitions of sensor client + * model messages length, and a message is composed of three + * parts: Opcode + msg_value + MIC + */ +/* Sensor client messages length */ +#define BLE_MESH_SENSOR_DESCRIPTOR_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_SENSOR_CADENCE_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_SENSOR_CADENCE_SET_MSG_LEN /* variable */ +#define BLE_MESH_SENSOR_SETTINGS_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_SENSOR_SETTING_GET_MSG_LEN (2 + 4 + 4) +#define BLE_MESH_SENSOR_SETTING_SET_MSG_LEN /* variable */ +#define BLE_MESH_SENSOR_GET_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_SENSOR_COLUMN_GET_MSG_LEN /* variable */ +#define BLE_MESH_SENSOR_SERIES_GET_MSG_LEN /* variable */ + +static const bt_mesh_client_op_pair_t sensor_op_pair[] = { + { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_GET, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_GET, BLE_MESH_MODEL_OP_SENSOR_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS }, + { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS }, +}; + +static bt_mesh_mutex_t sensor_client_lock; + +static void bt_mesh_sensor_client_mutex_new(void) +{ + if (!sensor_client_lock.mutex) { + bt_mesh_mutex_create(&sensor_client_lock); + } +} + +static void bt_mesh_sensor_client_mutex_free(void) +{ + bt_mesh_mutex_free(&sensor_client_lock); +} + +static void bt_mesh_sensor_client_lock(void) +{ + bt_mesh_mutex_lock(&sensor_client_lock); +} + +static void bt_mesh_sensor_client_unlock(void) +{ + bt_mesh_mutex_unlock(&sensor_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive sensor status message timeout"); + + bt_mesh_sensor_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_sensor_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_sensor_client_unlock(); + + return; +} + +static void sensor_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + bt_mesh_client_node_t *node = NULL; + u8_t *val = NULL; + u8_t evt = 0xFF; + size_t len = 0U; + + BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: { + struct bt_mesh_sensor_descriptor_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_descriptor_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->descriptor = bt_mesh_alloc_buf(buf->len); + if (!status->descriptor) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->descriptor, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_descriptor_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: { + struct bt_mesh_sensor_cadence_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_cadence_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->property_id = net_buf_simple_pull_le16(buf); + status->sensor_cadence_value = bt_mesh_alloc_buf(buf->len); + if (!status->sensor_cadence_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->sensor_cadence_value, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_cadence_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: { + struct bt_mesh_sensor_settings_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_settings_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->sensor_property_id = net_buf_simple_pull_le16(buf); + status->sensor_setting_property_ids = bt_mesh_alloc_buf(buf->len); + if (!status->sensor_setting_property_ids) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->sensor_setting_property_ids, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_settings_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: { + struct bt_mesh_sensor_setting_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_setting_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->sensor_property_id = net_buf_simple_pull_le16(buf); + status->sensor_setting_property_id = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->sensor_setting_access = net_buf_simple_pull_u8(buf); + status->sensor_setting_raw = bt_mesh_alloc_buf(buf->len); + if (!status->sensor_setting_raw) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->sensor_setting_raw, buf->data, buf->len); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_setting_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_STATUS: { + struct bt_mesh_sensor_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->marshalled_sensor_data = bt_mesh_alloc_buf(buf->len); + if (!status->marshalled_sensor_data) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->marshalled_sensor_data, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: { + struct bt_mesh_sensor_column_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_column_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->property_id = net_buf_simple_pull_le16(buf); + status->sensor_column_value = bt_mesh_alloc_buf(buf->len); + if (!status->sensor_column_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->sensor_column_value, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_column_status); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: { + struct bt_mesh_sensor_series_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_series_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->property_id = net_buf_simple_pull_le16(buf); + status->sensor_series_value = bt_mesh_alloc_buf(buf->len); + if (!status->sensor_series_value) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->sensor_series_value, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_sensor_series_status); + break; + } + default: + BT_ERR("%s, Not a Sensor Status message opcode", __func__); + return; + } + + buf->data = val; + buf->len = len; + + bt_mesh_sensor_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); + if (!node) { + BT_DBG("Unexpected sensor status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: + case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: + case BLE_MESH_MODEL_OP_SENSOR_GET: + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: + evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE; + break; + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_sensor_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); + } + } + + bt_mesh_sensor_client_unlock(); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: { + struct bt_mesh_sensor_descriptor_status *status; + status = (struct bt_mesh_sensor_descriptor_status *)val; + bt_mesh_free_buf(status->descriptor); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: { + struct bt_mesh_sensor_cadence_status *status; + status = (struct bt_mesh_sensor_cadence_status *)val; + bt_mesh_free_buf(status->sensor_cadence_value); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: { + struct bt_mesh_sensor_settings_status *status; + status = (struct bt_mesh_sensor_settings_status *)val; + bt_mesh_free_buf(status->sensor_setting_property_ids); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: { + struct bt_mesh_sensor_setting_status *status; + status = (struct bt_mesh_sensor_setting_status *)val; + bt_mesh_free_buf(status->sensor_setting_raw); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_STATUS: { + struct bt_mesh_sensor_status *status; + status = (struct bt_mesh_sensor_status *)val; + bt_mesh_free_buf(status->marshalled_sensor_data); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: { + struct bt_mesh_sensor_column_status *status; + status = (struct bt_mesh_sensor_column_status *)val; + bt_mesh_free_buf(status->sensor_column_value); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: { + struct bt_mesh_sensor_series_status *status; + status = (struct bt_mesh_sensor_series_status *)val; + bt_mesh_free_buf(status->sensor_series_value); + break; + } + default: + break; + } + + bt_mesh_free(val); + + return; +} + +const struct bt_mesh_model_op sensor_cli_op[] = { + { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS, 0, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS, 2, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS, 2, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS, 4, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_STATUS, 0, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS, 2, sensor_status }, + { BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS, 2, sensor_status }, + BLE_MESH_MODEL_OP_END, +}; + +static int sensor_act_state(bt_mesh_client_common_param_t *common, + void *value, u16_t value_len, bool need_ack) +{ + struct net_buf_simple *msg = NULL; + int err = 0; + + msg = bt_mesh_alloc_buf(value_len); + if (!msg) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + bt_mesh_model_msg_init(msg, common->opcode); + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: { + struct bt_mesh_sensor_descriptor_get *act; + act = (struct bt_mesh_sensor_descriptor_get *)value; + if (act->op_en) { + net_buf_simple_add_le16(msg, act->property_id); + } + break; + } + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: { + struct bt_mesh_sensor_cadence_get *act; + act = (struct bt_mesh_sensor_cadence_get *)value; + net_buf_simple_add_le16(msg, act->property_id); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK: { + struct bt_mesh_sensor_cadence_set *act; + act = (struct bt_mesh_sensor_cadence_set *)value; + net_buf_simple_add_le16(msg, act->property_id); + net_buf_simple_add_u8(msg, act->status_trigger_type << 7 | act->fast_cadence_period_divisor); + net_buf_simple_add_mem(msg, act->status_trigger_delta_down->data, act->status_trigger_delta_down->len); + net_buf_simple_add_mem(msg, act->status_trigger_delta_up->data, act->status_trigger_delta_up->len); + net_buf_simple_add_u8(msg, act->status_min_interval); + net_buf_simple_add_mem(msg, act->fast_cadence_low->data, act->fast_cadence_low->len); + net_buf_simple_add_mem(msg, act->fast_cadence_high->data, act->fast_cadence_high->len); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: { + struct bt_mesh_sensor_settings_get *act; + act = (struct bt_mesh_sensor_settings_get *)value; + net_buf_simple_add_le16(msg, act->sensor_property_id); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: { + struct bt_mesh_sensor_setting_get *act; + act = (struct bt_mesh_sensor_setting_get *)value; + net_buf_simple_add_le16(msg, act->sensor_property_id); + net_buf_simple_add_le16(msg, act->sensor_setting_property_id); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK: { + struct bt_mesh_sensor_setting_set *act; + act = (struct bt_mesh_sensor_setting_set *)value; + net_buf_simple_add_le16(msg, act->sensor_property_id); + net_buf_simple_add_le16(msg, act->sensor_setting_property_id); + net_buf_simple_add_mem(msg, act->sensor_setting_raw->data, act->sensor_setting_raw->len); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_GET: { + struct bt_mesh_sensor_get *act; + act = (struct bt_mesh_sensor_get *)value; + if (act->op_en) { + net_buf_simple_add_le16(msg, act->property_id); + } + break; + } + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: { + struct bt_mesh_sensor_column_get *act; + act = (struct bt_mesh_sensor_column_get *)value; + net_buf_simple_add_le16(msg, act->property_id); + net_buf_simple_add_mem(msg, act->raw_value_x->data, act->raw_value_x->len); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: { + struct bt_mesh_sensor_series_get *act; + act = (struct bt_mesh_sensor_series_get *)value; + net_buf_simple_add_le16(msg, act->property_id); + if (act->op_en) { + net_buf_simple_add_mem(msg, act->raw_value_x1->data, act->raw_value_x1->len); + net_buf_simple_add_mem(msg, act->raw_value_x2->data, act->raw_value_x2->len); + } + break; + } + default: + BT_ERR("%s, Not a Sensor Client message opcode", __func__); + err = -EINVAL; + goto end; + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, msg, + timeout_handler, common->msg_timeout, need_ack, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Sensor Client message (err %d)", __func__, err); + } + +end: + bt_mesh_free_buf(msg); + + return err; +} + +int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) +{ + bt_mesh_sensor_client_t *client = NULL; + u16_t length = 0U; + + if (!common || !common->model || !get) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_sensor_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Sensor Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: + length = BLE_MESH_SENSOR_DESCRIPTOR_GET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: + length = BLE_MESH_SENSOR_CADENCE_GET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: + length = BLE_MESH_SENSOR_SETTINGS_GET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: + length = BLE_MESH_SENSOR_SETTING_GET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_SENSOR_GET: + length = BLE_MESH_SENSOR_GET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: { + struct bt_mesh_sensor_column_get *value; + value = (struct bt_mesh_sensor_column_get *)get; + if (!value->raw_value_x) { + BT_ERR("%s, Sensor column_get is NULL", __func__); + return -EINVAL; + } + length = (2 + 2 + value->raw_value_x->len + 4); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: { + struct bt_mesh_sensor_series_get *value; + value = (struct bt_mesh_sensor_series_get *)get; + if (value->op_en) { + if (!value->raw_value_x1 || !value->raw_value_x2) { + BT_ERR("%s, Sensor series_get is NULL", __func__); + return -EINVAL; + } + } + if (value->op_en) { + length = value->raw_value_x1->len + value->raw_value_x2->len; + } + length += (2 + 2 + 4); + break; + } + default: + BT_ERR("%s, Not a Sensor Client Get message opcode", __func__); + return -EINVAL; + } + + return sensor_act_state(common, get, length, true); +} + +int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) +{ + bt_mesh_sensor_client_t *client = NULL; + u16_t length = 0U; + bool need_ack = false; + + if (!common || !common->model || !set) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_sensor_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Sensor Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK: { + struct bt_mesh_sensor_cadence_set *value; + value = (struct bt_mesh_sensor_cadence_set *)set; + if (!value->status_trigger_delta_down || !value->status_trigger_delta_up || + !value->fast_cadence_low || !value->fast_cadence_high) { + BT_ERR("%s, Sensor cadence_set is NULL", __func__); + return -EINVAL; + } + length = value->status_trigger_delta_down->len + \ + value->status_trigger_delta_up->len + \ + value->fast_cadence_low->len + \ + value->fast_cadence_high->len; + length += (1 + 2 + 1 + 1 + 4); + break; + } + case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK: { + struct bt_mesh_sensor_setting_set *value; + value = (struct bt_mesh_sensor_setting_set *)set; + if (!value->sensor_setting_raw) { + BT_ERR("%s, Sensor setting_raw is NULL", __func__); + return -EINVAL; + } + length = (1 + 2 + 2 + value->sensor_setting_raw->len + 4); + break; + } + default: + BT_ERR("%s, Not a Sensor Client Set message opcode", __func__); + return -EINVAL; + } + + return sensor_act_state(common, set, length, need_ack); +} + +int bt_mesh_sensor_cli_init(struct bt_mesh_model *model, bool primary) +{ + sensor_internal_data_t *internal = NULL; + bt_mesh_sensor_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_sensor_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Sensor Client user_data is NULL", __func__); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(sensor_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(sensor_op_pair); + client->op_pair = sensor_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + bt_mesh_sensor_client_mutex_new(); + + return 0; +} + +int bt_mesh_sensor_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_sensor_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_sensor_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Sensor Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_sensor_client_mutex_free(); + + return 0; +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c new file mode 100644 index 000000000..11162ae4f --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/client/time_scene_client.c @@ -0,0 +1,743 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "btc_ble_mesh_time_scene_model.h" + +#include "model_opcode.h" +#include "time_scene_client.h" + +/** The following are the macro definitions of time and client + * scene model messages length, and a message is composed of + * three parts: Opcode + msg_value + MIC + */ +/* Time client messages length */ +#define BLE_MESH_TIME_SET_MSG_LEN (1 + 10 + 4) +#define BLE_MESH_TIME_ZONE_SET_MSG_LEN (2 + 6 + 4) +#define BLE_MESH_TAI_UTC_DELTA_SET_MSG_LEN (2 + 7 + 4) +#define BLE_MESH_TIME_ROLE_SET_MSG_LEN (2 + 1 + 4) + +/* Scene client messages length */ +#define BLE_MESH_SCENE_STORE_MSG_LEN (2 + 2 + 4) +#define BLE_MESH_SCENE_RECALL_MSG_LEN (2 + 5 + 4) +#define BLE_MESH_SCENE_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_SCENE_REGISTER_GET_MSG_LEN (2 + 0 + 4) +#define BLE_MESH_SCENE_DELETE_MSG_LEN (2 + 2 + 4) + +/* Scheduler client messages length */ +#define BLE_MESH_SCHEDULER_ACT_GET_MSG_LEN (2 + 1 + 4) +#define BLE_MESH_SCHEDULER_ACT_SET_MSG_LEN (1 + 10 + 4) + +#define BLE_MESH_SCENE_GET_STATE_MSG_LEN (2 + 1 + 4) +#define BLE_MESH_SCENE_ACT_STATE_MSG_LEN (2 + 2 + 4) + +static const bt_mesh_client_op_pair_t time_scene_op_pair[] = { + { BLE_MESH_MODEL_OP_TIME_GET, BLE_MESH_MODEL_OP_TIME_STATUS }, + { BLE_MESH_MODEL_OP_TIME_SET, BLE_MESH_MODEL_OP_TIME_STATUS }, + { BLE_MESH_MODEL_OP_TIME_ZONE_GET, BLE_MESH_MODEL_OP_TIME_ZONE_STATUS }, + { BLE_MESH_MODEL_OP_TIME_ZONE_SET, BLE_MESH_MODEL_OP_TIME_ZONE_STATUS }, + { BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET, BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS }, + { BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET, BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS }, + { BLE_MESH_MODEL_OP_TIME_ROLE_GET, BLE_MESH_MODEL_OP_TIME_ROLE_STATUS }, + { BLE_MESH_MODEL_OP_TIME_ROLE_SET, BLE_MESH_MODEL_OP_TIME_ROLE_STATUS }, + { BLE_MESH_MODEL_OP_SCENE_STORE, BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS }, + { BLE_MESH_MODEL_OP_SCENE_RECALL, BLE_MESH_MODEL_OP_SCENE_STATUS }, + { BLE_MESH_MODEL_OP_SCENE_GET, BLE_MESH_MODEL_OP_SCENE_STATUS }, + { BLE_MESH_MODEL_OP_SCENE_REGISTER_GET, BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS }, + { BLE_MESH_MODEL_OP_SCENE_DELETE, BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS }, + { BLE_MESH_MODEL_OP_SCHEDULER_GET, BLE_MESH_MODEL_OP_SCHEDULER_STATUS }, + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS }, + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS }, +}; + +static bt_mesh_mutex_t time_scene_client_lock; + +static void bt_mesh_time_scene_client_mutex_new(void) +{ + if (!time_scene_client_lock.mutex) { + bt_mesh_mutex_create(&time_scene_client_lock); + } +} + +static void bt_mesh_time_scene_client_mutex_free(void) +{ + bt_mesh_mutex_free(&time_scene_client_lock); +} + +static void bt_mesh_time_scene_client_lock(void) +{ + bt_mesh_mutex_lock(&time_scene_client_lock); +} + +static void bt_mesh_time_scene_client_unlock(void) +{ + bt_mesh_mutex_unlock(&time_scene_client_lock); +} + +static void timeout_handler(struct k_work *work) +{ + struct k_delayed_work *timer = NULL; + bt_mesh_client_node_t *node = NULL; + struct bt_mesh_msg_ctx ctx = {0}; + u32_t opcode = 0U; + + BT_WARN("Receive time scene status message timeout"); + + bt_mesh_time_scene_client_lock(); + + timer = CONTAINER_OF(work, struct k_delayed_work, work); + + if (timer && !k_delayed_work_free(timer)) { + node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work); + if (node) { + memcpy(&ctx, &node->ctx, sizeof(ctx)); + opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_time_scene_client_cb_evt_to_btc( + opcode, BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_TIMEOUT, ctx.model, &ctx, NULL, 0); + } + } + + bt_mesh_time_scene_client_unlock(); + + return; +} + +static void time_scene_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + bt_mesh_client_node_t *node = NULL; + u8_t *val = NULL; + u8_t evt = 0xFF; + size_t len = 0U; + + BT_DBG("%s, len %d, bytes %s", __func__, buf->len, bt_hex(buf->data, buf->len)); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_TIME_STATUS: { + struct bt_mesh_time_status *status = NULL; + if (buf->len != 5 && buf->len != 10) { + BT_ERR("%s, Invalid Time Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + memcpy(status->tai_seconds, buf->data, 5); + net_buf_simple_pull(buf, 5); + status->sub_second = net_buf_simple_pull_u8(buf); + status->uncertainty = net_buf_simple_pull_u8(buf); + u16_t temp = net_buf_simple_pull_le16(buf); + status->time_authority = temp & BIT(0); + status->tai_utc_delta = temp >> 15; + status->time_zone_offset = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_time_status); + break; + } + case BLE_MESH_MODEL_OP_TIME_ZONE_STATUS: { + struct bt_mesh_time_zone_status *status = NULL; + if (buf->len != 7) { + BT_ERR("%s, Invalid Time Zone Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_zone_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->time_zone_offset_curr = net_buf_simple_pull_u8(buf); + status->time_zone_offset_new = net_buf_simple_pull_u8(buf); + memcpy(status->tai_zone_change, buf->data, 5); + net_buf_simple_pull(buf, 5); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_time_zone_status); + break; + } + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS: { + struct bt_mesh_tai_utc_delta_status *status = NULL; + if (buf->len != 9) { + BT_ERR("%s, Invalid TAI UTC Delta Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_tai_utc_delta_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + u16_t temp = net_buf_simple_pull_le16(buf); + status->tai_utc_delta_curr = temp & BIT_MASK(15); + status->padding_1 = (temp >> 15) & BIT(0); + temp = net_buf_simple_pull_le16(buf); + status->tai_utc_delta_new = temp & BIT_MASK(15); + status->padding_2 = (temp >> 15) & BIT(0); + memcpy(status->tai_delta_change, buf->data, 5); + net_buf_simple_pull(buf, 5); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_tai_utc_delta_status); + break; + } + case BLE_MESH_MODEL_OP_TIME_ROLE_STATUS: { + struct bt_mesh_time_role_status *status = NULL; + if (buf->len != 1) { + BT_ERR("%s, Invalid Time Role Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_time_role_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->time_role = net_buf_simple_pull_u8(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_time_role_status); + break; + } + case BLE_MESH_MODEL_OP_SCENE_STATUS: { + struct bt_mesh_scene_status *status = NULL; + if (buf->len != 3 && buf->len != 6) { + BT_ERR("%s, Invalid Scene Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_scene_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->current_scene = net_buf_simple_pull_le16(buf); + if (buf->len) { + status->op_en = true; + status->target_scene = net_buf_simple_pull_le16(buf); + status->remain_time = net_buf_simple_pull_u8(buf); + } + val = (u8_t *)status; + len = sizeof(struct bt_mesh_scene_status); + break; + } + case BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: { + struct bt_mesh_scene_register_status *status = NULL; + status = bt_mesh_calloc(sizeof(struct bt_mesh_scene_register_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->status_code = net_buf_simple_pull_u8(buf); + status->current_scene = net_buf_simple_pull_le16(buf); + status->scenes = bt_mesh_alloc_buf(buf->len); + if (!status->scenes) { + BT_ERR("%s, Failed to allocate memory", __func__); + bt_mesh_free(status); + return; + } + net_buf_simple_add_mem(status->scenes, buf->data, buf->len); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_scene_register_status); + break; + } + case BLE_MESH_MODEL_OP_SCHEDULER_STATUS: { + struct bt_mesh_scheduler_status *status = NULL; + if (buf->len != 2) { + BT_ERR("%s, Invalid Scheduler Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_scheduler_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + status->schedules = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_scheduler_status); + break; + } + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS: { + struct bt_mesh_scheduler_act_status *status = NULL; + if (buf->len != 10) { + BT_ERR("%s, Invalid Scheduler Action Status length %d", __func__, buf->len); + return; + } + status = bt_mesh_calloc(sizeof(struct bt_mesh_scheduler_act_status)); + if (!status) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + memcpy(status, buf->data, offsetof(struct bt_mesh_scheduler_act_status, scene_number)); + net_buf_simple_pull(buf, offsetof(struct bt_mesh_scheduler_act_status, scene_number)); + status->scene_number = net_buf_simple_pull_le16(buf); + val = (u8_t *)status; + len = sizeof(struct bt_mesh_scheduler_act_status); + break; + } + default: + BT_ERR("%s, Not a Time Scene Status message opcode", __func__); + return; + } + + buf->data = val; + buf->len = len; + + bt_mesh_time_scene_client_lock(); + + node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true); + if (!node) { + BT_DBG("Unexpected time scene status message 0x%x", ctx->recv_op); + } else { + switch (node->opcode) { + case BLE_MESH_MODEL_OP_TIME_GET: + case BLE_MESH_MODEL_OP_TIME_ZONE_GET: + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET: + case BLE_MESH_MODEL_OP_TIME_ROLE_GET: + case BLE_MESH_MODEL_OP_SCENE_GET: + case BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: + case BLE_MESH_MODEL_OP_SCHEDULER_GET: + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET: + evt = BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_GET_STATE; + break; + case BLE_MESH_MODEL_OP_TIME_SET: + case BLE_MESH_MODEL_OP_TIME_ZONE_SET: + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET: + case BLE_MESH_MODEL_OP_TIME_ROLE_SET: + case BLE_MESH_MODEL_OP_SCENE_STORE: + case BLE_MESH_MODEL_OP_SCENE_RECALL: + case BLE_MESH_MODEL_OP_SCENE_DELETE: + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET: + evt = BTC_BLE_MESH_EVT_TIME_SCENE_CLIENT_SET_STATE; + break; + default: + break; + } + + if (!k_delayed_work_free(&node->timer)) { + u32_t opcode = node->opcode; + bt_mesh_client_free_node(node); + bt_mesh_time_scene_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len); + } + } + + bt_mesh_time_scene_client_unlock(); + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS: { + struct bt_mesh_scene_register_status *status; + status = (struct bt_mesh_scene_register_status *)val; + bt_mesh_free_buf(status->scenes); + break; + } + default: + break; + } + + bt_mesh_free(val); + + return; +} + +const struct bt_mesh_model_op time_cli_op[] = { + { BLE_MESH_MODEL_OP_TIME_STATUS, 5, time_scene_status }, + { BLE_MESH_MODEL_OP_TIME_ZONE_STATUS, 7, time_scene_status }, + { BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS, 9, time_scene_status }, + { BLE_MESH_MODEL_OP_TIME_ROLE_STATUS, 1, time_scene_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op scene_cli_op[] = { + { BLE_MESH_MODEL_OP_SCENE_STATUS, 3, time_scene_status }, + { BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS, 3, time_scene_status }, + BLE_MESH_MODEL_OP_END, +}; + +const struct bt_mesh_model_op scheduler_cli_op[] = { + { BLE_MESH_MODEL_OP_SCHEDULER_STATUS, 2, time_scene_status }, + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS, 10, time_scene_status }, + BLE_MESH_MODEL_OP_END, +}; + +static int time_scene_get_state(bt_mesh_client_common_param_t *common, void *value) +{ + NET_BUF_SIMPLE_DEFINE(msg, BLE_MESH_SCENE_GET_STATE_MSG_LEN); + int err = 0; + + bt_mesh_model_msg_init(&msg, common->opcode); + + if (value) { + switch (common->opcode) { + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET: { + struct bt_mesh_scheduler_act_get *get; + get = (struct bt_mesh_scheduler_act_get *)value; + net_buf_simple_add_u8(&msg, get->index); + break; + } + default: + BT_DBG("This time scene message should be sent with NULL get pointer"); + break; + } + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, &msg, + timeout_handler, common->msg_timeout, true, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Time Scene Get message (err %d)", __func__, err); + } + + return err; +} + +static int time_scene_set_state(bt_mesh_client_common_param_t *common, + void *value, u16_t value_len, bool need_ack) +{ + struct net_buf_simple *msg = NULL; + int err = 0; + + msg = bt_mesh_alloc_buf(value_len); + if (!msg) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + bt_mesh_model_msg_init(msg, common->opcode); + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_TIME_SET: { + struct bt_mesh_time_set *set; + set = (struct bt_mesh_time_set *)value; + net_buf_simple_add_mem(msg, set->tai_seconds, 5); + net_buf_simple_add_u8(msg, set->sub_second); + net_buf_simple_add_u8(msg, set->uncertainty); + net_buf_simple_add_le16(msg, set->tai_utc_delta << 1 | set->time_authority); + net_buf_simple_add_u8(msg, set->time_zone_offset); + break; + } + case BLE_MESH_MODEL_OP_TIME_ZONE_SET: { + struct bt_mesh_time_zone_set *set; + set = (struct bt_mesh_time_zone_set *)value; + net_buf_simple_add_u8(msg, set->time_zone_offset_new); + net_buf_simple_add_mem(msg, set->tai_zone_change, 5); + break; + } + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET: { + struct bt_mesh_tai_utc_delta_set *set; + set = (struct bt_mesh_tai_utc_delta_set *)value; + net_buf_simple_add_le16(msg, set->padding << 15 | set->tai_utc_delta_new); + net_buf_simple_add_mem(msg, set->tai_delta_change, 5); + break; + } + case BLE_MESH_MODEL_OP_TIME_ROLE_SET: { + struct bt_mesh_time_role_set *set; + set = (struct bt_mesh_time_role_set *)value; + net_buf_simple_add_u8(msg, set->time_role); + break; + } + case BLE_MESH_MODEL_OP_SCENE_STORE: + case BLE_MESH_MODEL_OP_SCENE_STORE_UNACK: { + struct bt_mesh_scene_store *set; + set = (struct bt_mesh_scene_store *)value; + net_buf_simple_add_le16(msg, set->scene_number); + break; + } + case BLE_MESH_MODEL_OP_SCENE_RECALL: + case BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK: { + struct bt_mesh_scene_recall *set; + set = (struct bt_mesh_scene_recall *)value; + net_buf_simple_add_le16(msg, set->scene_number); + net_buf_simple_add_u8(msg, set->tid); + if (set->op_en) { + net_buf_simple_add_u8(msg, set->trans_time); + net_buf_simple_add_u8(msg, set->delay); + } + break; + } + case BLE_MESH_MODEL_OP_SCENE_DELETE: + case BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK: { + struct bt_mesh_scene_delete *set; + set = (struct bt_mesh_scene_delete *)value; + net_buf_simple_add_le16(msg, set->scene_number); + break; + } + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET: + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK: { + struct bt_mesh_scheduler_act_set *set; + set = (struct bt_mesh_scheduler_act_set *)value; + net_buf_simple_add_mem(msg, set, offsetof(struct bt_mesh_scheduler_act_set, scene_number)); + net_buf_simple_add_le16(msg, set->scene_number); + break; + } + default: + BT_ERR("%s, Not a Time Scene Client set message opcode", __func__); + err = -EINVAL; + goto end; + } + + err = bt_mesh_client_send_msg(common->model, common->opcode, &common->ctx, msg, + timeout_handler, common->msg_timeout, need_ack, + common->cb, common->cb_data); + if (err) { + BT_ERR("%s, Failed to send Time Scene Set message (err %d)", __func__, err); + } + +end: + bt_mesh_free_buf(msg); + return err; +} + +int bt_mesh_time_scene_client_get_state(bt_mesh_client_common_param_t *common, void *get, void *status) +{ + bt_mesh_time_scene_client_t *client = NULL; + + if (!common || !common->model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_time_scene_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Time Scene Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_TIME_GET: + case BLE_MESH_MODEL_OP_TIME_ZONE_GET: + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET: + case BLE_MESH_MODEL_OP_TIME_ROLE_GET: + case BLE_MESH_MODEL_OP_SCENE_GET: + case BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: + case BLE_MESH_MODEL_OP_SCHEDULER_GET: + break; + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET: + if (!get) { + BT_ERR("%s, Scheduler action index is NULL", __func__); + return -EINVAL; + } + break; + default: + BT_ERR("%s, Not a Time Scene Client Get message opcode", __func__); + return -EINVAL; + } + + return time_scene_get_state(common, get); +} + +int bt_mesh_time_scene_client_set_state(bt_mesh_client_common_param_t *common, void *set, void *status) +{ + bt_mesh_time_scene_client_t *client = NULL; + u16_t length = 0U; + bool need_ack = false; + + if (!common || !common->model || !set) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_time_scene_client_t *)common->model->user_data; + if (!client || !client->internal_data) { + BT_ERR("%s, Time Scene Client user data is NULL", __func__); + return -EINVAL; + } + + switch (common->opcode) { + case BLE_MESH_MODEL_OP_TIME_SET: + need_ack = true; + length = BLE_MESH_TIME_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_TIME_ZONE_SET: + need_ack = true; + length = BLE_MESH_TIME_ZONE_SET_MSG_LEN; + break; + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET: { + struct bt_mesh_tai_utc_delta_set *value; + value = (struct bt_mesh_tai_utc_delta_set *)set; + if (value->padding) { + BT_ERR("%s, Non-zero padding value is prohibited", __func__); + return -EINVAL; + } + need_ack = true; + length = BLE_MESH_TAI_UTC_DELTA_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_TIME_ROLE_SET: { + struct bt_mesh_time_role_set *value; + value = (struct bt_mesh_time_role_set *)set; + if (value->time_role > 0x03) { + BT_ERR("%s, Time role value is prohibited", __func__); + return -EINVAL; + } + need_ack = true; + length = BLE_MESH_TIME_ROLE_SET_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_SCENE_STORE: + need_ack = true; + case BLE_MESH_MODEL_OP_SCENE_STORE_UNACK: { + struct bt_mesh_scene_store *value; + value = (struct bt_mesh_scene_store *)set; + if (!value->scene_number) { + BT_ERR("%s, Scene store scene_number 0x0000 is prohibited", __func__); + return -EINVAL; + } + length = BLE_MESH_SCENE_STORE_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_SCENE_RECALL: + need_ack = true; + case BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK: { + struct bt_mesh_scene_recall *value; + value = (struct bt_mesh_scene_recall *)set; + if (!value->scene_number) { + BT_ERR("%s, Scene recall scene_number 0x0000 is prohibited", __func__); + return -EINVAL; + } + if (value->op_en) { + if ((value->trans_time & 0x3F) > 0x3E) { + BT_ERR("%s, Invalid Scene Recall transition time", __func__); + return -EINVAL; + } + } + length = BLE_MESH_SCENE_RECALL_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_SCENE_DELETE: + need_ack = true; + case BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK: { + length = BLE_MESH_SCENE_DELETE_MSG_LEN; + break; + } + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET: + need_ack = true; + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK: { + struct bt_mesh_scheduler_act_set *value; + value = (struct bt_mesh_scheduler_act_set *)set; + if (value->year > 0x64) { + BT_ERR("%s, Scheduler register year value is prohibited", __func__); + return -EINVAL; + } + if (value->hour > 0x19) { + BT_ERR("%s, Scheduler register hour value is prohibited", __func__); + return -EINVAL; + } + length = BLE_MESH_SCHEDULER_ACT_SET_MSG_LEN; + break; + } + default: + BT_ERR("%s, Not a Time Scene Set message opcode", __func__); + return -EINVAL; + } + + return time_scene_set_state(common, set, length, need_ack); +} + +static int time_scene_client_init(struct bt_mesh_model *model, bool primary) +{ + time_scene_internal_data_t *internal = NULL; + bt_mesh_time_scene_client_t *client = NULL; + + BT_DBG("primary %u", primary); + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_time_scene_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Time Scene Client user_data is NULL", __func__); + return -EINVAL; + } + + if (!client->internal_data) { + internal = bt_mesh_calloc(sizeof(time_scene_internal_data_t)); + if (!internal) { + BT_ERR("%s, Failed to allocate memory", __func__); + return -ENOMEM; + } + + sys_slist_init(&internal->queue); + + client->model = model; + client->op_pair_size = ARRAY_SIZE(time_scene_op_pair); + client->op_pair = time_scene_op_pair; + client->internal_data = internal; + } else { + bt_mesh_client_clear_list(client->internal_data); + } + + bt_mesh_time_scene_client_mutex_new(); + + return 0; +} + +int bt_mesh_time_cli_init(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_init(model, primary); +} + +int bt_mesh_scene_cli_init(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_init(model, primary); +} + +int bt_mesh_scheduler_cli_init(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_init(model, primary); +} + +static int time_scene_client_deinit(struct bt_mesh_model *model, bool primary) +{ + bt_mesh_time_scene_client_t *client = NULL; + + if (!model) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + client = (bt_mesh_time_scene_client_t *)model->user_data; + if (!client) { + BT_ERR("%s, Time Scene Client user_data is NULL", __func__); + return -EINVAL; + } + + if (client->internal_data) { + /* Remove items from the list */ + bt_mesh_client_clear_list(client->internal_data); + + /* Free the allocated internal data */ + bt_mesh_free(client->internal_data); + client->internal_data = NULL; + } + + bt_mesh_time_scene_client_mutex_free(); + + return 0; +} + +int bt_mesh_time_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); +} + +int bt_mesh_scene_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); +} + +int bt_mesh_scheduler_cli_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_client_deinit(model, primary); +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h b/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h new file mode 100644 index 000000000..00ac913fb --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/common/include/model_opcode.h @@ -0,0 +1,284 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _MODEL_OPCODE_H_ +#define _MODEL_OPCODE_H_ + +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Generic OnOff Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x01) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x02) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x03) +#define BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x04) + +/* Generic Level Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x05) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x06) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x07) +#define BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x08) +#define BLE_MESH_MODEL_OP_GEN_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x09) +#define BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0A) +#define BLE_MESH_MODEL_OP_GEN_MOVE_SET BLE_MESH_MODEL_OP_2(0x82, 0x0B) +#define BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0C) + +/* Generic Default Transition Time Message Opcode*/ +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x0D) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET BLE_MESH_MODEL_OP_2(0x82, 0x0E) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x0F) +#define BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x10) + +/* Generic Power OnOff Message Opcode*/ +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET BLE_MESH_MODEL_OP_2(0x82, 0x11) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x12) + +/* Generic Power OnOff Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET BLE_MESH_MODEL_OP_2(0x82, 0x13) +#define BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x14) + +/* Generic Power Level Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET BLE_MESH_MODEL_OP_2(0x82, 0x15) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET BLE_MESH_MODEL_OP_2(0x82, 0x16) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x17) +#define BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x18) +#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x19) +#define BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1A) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x1B) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1C) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x1D) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x1E) + +/* Generic Power Level Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x1F) +#define BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x20) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x21) +#define BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x22) + +/* Generic Battery Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_BATTERY_GET BLE_MESH_MODEL_OP_2(0x82, 0x23) +#define BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x24) + +/* Generic Location Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x25) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS BLE_MESH_MODEL_OP_1(0x40) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET BLE_MESH_MODEL_OP_2(0x82, 0x26) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x27) + +/* Generic Location Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET BLE_MESH_MODEL_OP_1(0x41) +#define BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK BLE_MESH_MODEL_OP_1(0x42) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET BLE_MESH_MODEL_OP_2(0x82, 0x28) +#define BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x29) + +/* Generic Manufacturer Property Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2A) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x43) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2B) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x44) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x45) +#define BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x46) + +/* Generic Admin Property Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2C) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x47) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2D) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x48) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x49) +#define BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4A) + +/* Generic User Property Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x2E) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x4B) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x2F) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x4C) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x4D) +#define BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x4E) + +/* Generic Client Property Message Opcode */ +#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET BLE_MESH_MODEL_OP_1(0x4F) +#define BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS BLE_MESH_MODEL_OP_1(0x50) + +/* Sensor Message Opcode */ +#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x30) +#define BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS BLE_MESH_MODEL_OP_1(0x51) +#define BLE_MESH_MODEL_OP_SENSOR_GET BLE_MESH_MODEL_OP_2(0x82, 0x31) +#define BLE_MESH_MODEL_OP_SENSOR_STATUS BLE_MESH_MODEL_OP_1(0x52) +#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET BLE_MESH_MODEL_OP_2(0x82, 0x32) +#define BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS BLE_MESH_MODEL_OP_1(0x53) +#define BLE_MESH_MODEL_OP_SENSOR_SERIES_GET BLE_MESH_MODEL_OP_2(0x82, 0x33) +#define BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS BLE_MESH_MODEL_OP_1(0x54) + +/* Sensor Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET BLE_MESH_MODEL_OP_2(0x82, 0x34) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET BLE_MESH_MODEL_OP_1(0x55) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK BLE_MESH_MODEL_OP_1(0x56) +#define BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS BLE_MESH_MODEL_OP_1(0x57) +#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET BLE_MESH_MODEL_OP_2(0x82, 0x35) +#define BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS BLE_MESH_MODEL_OP_1(0x58) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_GET BLE_MESH_MODEL_OP_2(0x82, 0x36) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET BLE_MESH_MODEL_OP_1(0x59) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK BLE_MESH_MODEL_OP_1(0x5A) +#define BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS BLE_MESH_MODEL_OP_1(0x5B) + +/* Time Message Opcode */ +#define BLE_MESH_MODEL_OP_TIME_GET BLE_MESH_MODEL_OP_2(0x82, 0x37) +#define BLE_MESH_MODEL_OP_TIME_SET BLE_MESH_MODEL_OP_1(0x5C) +#define BLE_MESH_MODEL_OP_TIME_STATUS BLE_MESH_MODEL_OP_1(0x5D) +#define BLE_MESH_MODEL_OP_TIME_ROLE_GET BLE_MESH_MODEL_OP_2(0x82, 0x38) +#define BLE_MESH_MODEL_OP_TIME_ROLE_SET BLE_MESH_MODEL_OP_2(0x82, 0x39) +#define BLE_MESH_MODEL_OP_TIME_ROLE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3A) +#define BLE_MESH_MODEL_OP_TIME_ZONE_GET BLE_MESH_MODEL_OP_2(0x82, 0x3B) +#define BLE_MESH_MODEL_OP_TIME_ZONE_SET BLE_MESH_MODEL_OP_2(0x82, 0x3C) +#define BLE_MESH_MODEL_OP_TIME_ZONE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x3D) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET BLE_MESH_MODEL_OP_2(0x82, 0x3E) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET BLE_MESH_MODEL_OP_2(0x82, 0x3F) +#define BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x40) + +/* Scene Message Opcode */ +#define BLE_MESH_MODEL_OP_SCENE_GET BLE_MESH_MODEL_OP_2(0x82, 0x41) +#define BLE_MESH_MODEL_OP_SCENE_RECALL BLE_MESH_MODEL_OP_2(0x82, 0x42) +#define BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x43) +#define BLE_MESH_MODEL_OP_SCENE_STATUS BLE_MESH_MODEL_OP_1(0x5E) +#define BLE_MESH_MODEL_OP_SCENE_REGISTER_GET BLE_MESH_MODEL_OP_2(0x82, 0x44) +#define BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x45) + +/* Scene Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_SCENE_STORE BLE_MESH_MODEL_OP_2(0x82, 0x46) +#define BLE_MESH_MODEL_OP_SCENE_STORE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x47) +#define BLE_MESH_MODEL_OP_SCENE_DELETE BLE_MESH_MODEL_OP_2(0x82, 0x9E) +#define BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9F) + +/* Scheduler Message Opcode */ +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET BLE_MESH_MODEL_OP_2(0x82, 0x48) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS BLE_MESH_MODEL_OP_1(0x5F) +#define BLE_MESH_MODEL_OP_SCHEDULER_GET BLE_MESH_MODEL_OP_2(0x82, 0x49) +#define BLE_MESH_MODEL_OP_SCHEDULER_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4A) + +/* Scheduler Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET BLE_MESH_MODEL_OP_1(0x60) +#define BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK BLE_MESH_MODEL_OP_1(0x61) + +/* Light Lightness Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET BLE_MESH_MODEL_OP_2(0x82, 0x4B) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET BLE_MESH_MODEL_OP_2(0x82, 0x4C) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x4D) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x4E) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET BLE_MESH_MODEL_OP_2(0x82, 0x4F) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET BLE_MESH_MODEL_OP_2(0x82, 0x50) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x51) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x52) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET BLE_MESH_MODEL_OP_2(0x82, 0x53) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x54) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x55) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x56) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x57) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x58) + +/* Light Lightness Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x59) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5A) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x5B) +#define BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5C) + +/* Light CTL Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_CTL_GET BLE_MESH_MODEL_OP_2(0x82, 0x5D) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET BLE_MESH_MODEL_OP_2(0x82, 0x5E) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x5F) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x60) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET BLE_MESH_MODEL_OP_2(0x82, 0x61) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x62) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x63) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET BLE_MESH_MODEL_OP_2(0x82, 0x64) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x65) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x66) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x67) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x68) + +/* Light CTL Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x69) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6A) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6B) +#define BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x6C) + +/* Light HSL Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_HSL_GET BLE_MESH_MODEL_OP_2(0x82, 0x6D) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET BLE_MESH_MODEL_OP_2(0x82, 0x6E) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET BLE_MESH_MODEL_OP_2(0x82, 0x6F) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x70) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x71) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET BLE_MESH_MODEL_OP_2(0x82, 0x72) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET BLE_MESH_MODEL_OP_2(0x82, 0x73) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x74) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x75) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET BLE_MESH_MODEL_OP_2(0x82, 0x76) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x77) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x78) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x79) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7A) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x7B) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7C) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x7D) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x7E) + +/* Light HSL Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x7F) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x80) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x81) +#define BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x82) + +/* Light xyL Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_XYL_GET BLE_MESH_MODEL_OP_2(0x82, 0x83) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET BLE_MESH_MODEL_OP_2(0x82, 0x84) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x85) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x86) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET BLE_MESH_MODEL_OP_2(0x82, 0x87) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x88) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET BLE_MESH_MODEL_OP_2(0x82, 0x89) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8A) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET BLE_MESH_MODEL_OP_2(0x82, 0x8B) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x8C) + +/* Light xyL Setup Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET BLE_MESH_MODEL_OP_2(0x82, 0x8D) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x8E) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET BLE_MESH_MODEL_OP_2(0x82, 0x8F) +#define BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x90) + +/* Light Control Message Opcode */ +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET BLE_MESH_MODEL_OP_2(0x82, 0x91) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET BLE_MESH_MODEL_OP_2(0x82, 0x92) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x93) +#define BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x94) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET BLE_MESH_MODEL_OP_2(0x82, 0x95) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET BLE_MESH_MODEL_OP_2(0x82, 0x96) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x97) +#define BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x98) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET BLE_MESH_MODEL_OP_2(0x82, 0x99) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET BLE_MESH_MODEL_OP_2(0x82, 0x9A) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK BLE_MESH_MODEL_OP_2(0x82, 0x9B) +#define BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS BLE_MESH_MODEL_OP_2(0x82, 0x9C) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET BLE_MESH_MODEL_OP_2(0x82, 0x9D) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET BLE_MESH_MODEL_OP_1(0x62) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK BLE_MESH_MODEL_OP_1(0x63) +#define BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS BLE_MESH_MODEL_OP_1(0x64) + +#ifdef __cplusplus +} +#endif + +#endif /* _MODEL_OPCODE_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/device_property.c b/components/bt/esp_ble_mesh/mesh_models/server/device_property.c new file mode 100644 index 000000000..5ef9d3968 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/device_property.c @@ -0,0 +1,146 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "mesh_common.h" +#include "server_common.h" +#include "device_property.h" + +static struct bt_mesh_dev_prop { + u16_t prop_id; + u8_t len; +} device_properties [] = { + { BLE_MESH_INVALID_DEVICE_PROPERTY_ID, 0xFF }, + { BLE_MESH_AVERAGE_AMBIENT_TEMPERATURE_IN_A_PERIOD_OF_DAY, 0x03 }, + { BLE_MESH_AVERAGE_INPUT_CURRENT, 0x03 }, + { BLE_MESH_AVERAGE_INPUT_VOLTAGE, 0x03 }, + { BLE_MESH_AVERAGE_OUTPUT_CURRENT, 0x03 }, + { BLE_MESH_AVERAGE_OUTPUT_VOLTAGE, 0x03 }, + { BLE_MESH_CENTER_BEAM_INTENSITY_AT_FULL_POWER, 0x02 }, + { BLE_MESH_CHROMATICITY_TOLERANCE, 0x01 }, + { BLE_MESH_COLOR_RENDERING_INDEX_R9, 0x01 }, + { BLE_MESH_COLOR_RENDERING_INDEX_RA, 0x01 }, + { BLE_MESH_DEVICE_APPEARANCE, 0x02 }, + { BLE_MESH_DEVICE_COUNTRY_OF_ORIGIN, 0x02 }, + { BLE_MESH_DEVICE_DATE_OF_MANUFACTURE, 0x04 }, + { BLE_MESH_DEVICE_ENERGY_USE_SINCE_TURN_ON, 0x04 }, + { BLE_MESH_DEVICE_FIRMWARE_REVISION, 0x08 }, + { BLE_MESH_DEVICE_GLOBAL_TRADE_ITEM_NUMBER, 0x08 }, + { BLE_MESH_DEVICE_HARDWARE_REVISION, 0x16 }, + { BLE_MESH_DEVICE_MANUFACTURER_NAME, 0x36 }, + { BLE_MESH_DEVICE_MODEL_NUMBER, 0x24 }, + { BLE_MESH_DEVICE_OPERATING_TEMPERATURE_RANGE_SPECIFICATION, 0x04 }, + { BLE_MESH_DEVICE_OPERATING_TEMPERATURE_STATISTICAL_VALUES, 0x09 }, + { BLE_MESH_DEVICE_OVER_TEMPERATURE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_DEVICE_POWER_RANGE_SPECIFICATION, 0x12 }, + { BLE_MESH_DEVICE_RUNTIME_SINCE_TURN_ON, 0x04 }, + { BLE_MESH_DEVICE_RUNTIME_WARRANTY, 0x04 }, + { BLE_MESH_DEVICE_SERIAL_NUMBER, 0x16 }, + { BLE_MESH_DEVICE_SOFTWARE_REVISION, 0x08 }, + { BLE_MESH_DEVICE_UNDER_TEMPERATURE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INDOOR_AMBIENT_TEMPERATURE_STATISTICAL_VALUES, 0x05 }, + { BLE_MESH_INITIAL_CIE_1931_CHROMATICITY_COORDINATES, 0x04 }, + { BLE_MESH_INITIAL_CORRELATED_COLOR_TEMPERATURE, 0x02 }, + { BLE_MESH_INITIAL_LUMINOUS_FLUX, 0x02 }, + { BLE_MESH_INITIAL_PLANCKIAN_DISTANCE, 0x02 }, + { BLE_MESH_INPUT_CURRENT_RANGE_SPECIFICATION, 0x06 }, + { BLE_MESH_INPUT_CURRENT_STATISTICS, 0x09 }, + { BLE_MESH_INPUT_OVER_CURRENT_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INPUT_OVER_RIPPLE_VOLTAGE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INPUT_OVER_VOLTAGE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INPUT_UNDER_CURRENT_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INPUT_UNDER_VOLTAGE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_INPUT_VOLTAGE_RANGE_SPECIFICATION, 0x06 }, + { BLE_MESH_INPUT_VOLTAGE_RIPPLE_SPECIFICATION, 0x01 }, + { BLE_MESH_INPUT_VOLTAGE_STATISTICS, 0x09 }, + { BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON, 0x02 }, + { BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG, 0x02 }, + { BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY, 0x02 }, + { BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY, 0x01 }, + { BLE_MESH_LIGHT_CONTROL_REGULATOR_KID, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU, 0x04 }, + { BLE_MESH_LIGHT_CONTROL_TIME_FADE, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_PROLONG, 0x03 }, + { BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON, 0x03 }, + { BLE_MESH_LUMEN_MAINTENANCE_FACTOR, 0x01 }, + { BLE_MESH_LUMINOUS_EFFICACY, 0x02 }, + { BLE_MESH_LUMINOUS_ENERGY_SINCE_TURN_ON, 0x04 }, + { BLE_MESH_LUMINOUS_EXPOSURE, 0x04 }, + { BLE_MESH_LUMINOUS_FLUX_RANGE, 0x04 }, + { BLE_MESH_MOTION_SENSED, 0x01 }, + { BLE_MESH_MOTION_THRESHOLD, 0x01 }, + { BLE_MESH_OPEN_CIRCUIT_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_OUTDOOR_STATISTICAL_VALUES, 0x05 }, + { BLE_MESH_OUTPUT_CURRENT_RANGE, 0x04 }, + { BLE_MESH_OUTPUT_CURRENT_STATISTICS, 0x09 }, + { BLE_MESH_OUTPUT_RIPPLE_VOLTAGE_SPECIFICATION, 0x01 }, + { BLE_MESH_OUTPUT_VOLTAGE_RANGE, 0x06 }, + { BLE_MESH_OUTPUT_VOLTAGE_STATISTICS, 0x09 }, + { BLE_MESH_OVER_OUTPUT_RIPPLE_VOLTAGE_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_PEOPLE_COUNT, 0x02 }, + { BLE_MESH_PRESENCE_DETECTED, 0x01 }, + { BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL, 0x04 }, + { BLE_MESH_PRESENT_AMBIENT_TEMPERATURE, 0x01 }, + { BLE_MESH_PRESENT_CIE_1931_CHROMATICITY, 0x04 }, + { BLE_MESH_PRESENT_CORRELATED_COLOR_TEMPERATURE, 0x02 }, + { BLE_MESH_PRESENT_DEVICE_INPUT_POWER, 0x04 }, + { BLE_MESH_PRESENT_DEVICE_OPERATING_EFFICIENCY, 0x01 }, + { BLE_MESH_PRESENT_DEVICE_OPERATING_TEMPERATURE, 0x02 }, + { BLE_MESH_PRESENT_ILLUMINANCE, 0x04 }, + { BLE_MESH_PRESENT_INDOOR_AMBIENT_TEMPERATURE, 0x01 }, + { BLE_MESH_PRESENT_INPUT_CURRENT, 0x02 }, + { BLE_MESH_PRESENT_INPUT_RIPPLE_VOLTAGE, 0x01 }, + { BLE_MESH_PRESENT_INPUT_VOLTAGE, 0x02 }, + { BLE_MESH_PRESENT_LUMINOUS_FLUX, 0x02 }, + { BLE_MESH_PRESENT_OUTDOOR_AMBIENT_TEMPERATURE, 0x01 }, + { BLE_MESH_PRESENT_OUTPUT_CURRENT, 0x02 }, + { BLE_MESH_PRESENT_OUTPUT_VOLTAGE, 0x02 }, + { BLE_MESH_PRESENT_PLANCKIAN_DISTANCE, 0x02 }, + { BLE_MESH_PRESENT_RELATIVE_OUTPUT_RIPPLE_VOLTAGE, 0x01 }, + { BLE_MESH_RELATIVE_DEVICE_ENERGY_USE_IN_A_PERIOD_OF_DAY, 0x06 }, + { BLE_MESH_RELATIVE_DEVICE_RUNTIME_IN_A_GENERIC_LEVEL_RANGE, 0x05 }, + { BLE_MESH_RELATIVE_EXPOSURE_TIME_IN_AN_ILLUMINANCE_RANGE, 0x09 }, + { BLE_MESH_RELATIVE_RUNTIME_IN_A_CORRELATED_COLOR_TEMPERATURE_RANGE, 0x04 }, + { BLE_MESH_RELATIVE_RUNTIME_IN_A_DEVICE_OPERATING_TEMPERATURE_RANGE, 0x05 }, + { BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_CURRENT_RANGE, 0x05 }, + { BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_VOLTAGE_RANGE, 0x05 }, + { BLE_MESH_SHORT_CIRCUIT_EVENT_STATISTICS, 0x06 }, + { BLE_MESH_TIME_SINCE_MOTION_SENSED, 0x02 }, + { BLE_MESH_TIME_SINCE_PRESENCE_DETECTED, 0x02 }, + { BLE_MESH_TOTAL_DEVICE_ENERGY_USE, 0x04 }, + { BLE_MESH_TOTAL_DEVICE_OFF_ON_CYCLES, 0x04 }, + { BLE_MESH_TOTAL_DEVICE_POWER_ON_CYCLES, 0x04 }, + { BLE_MESH_TOTAL_DEVICE_POWER_ON_TIME, 0x04 }, + { BLE_MESH_TOTAL_DEVICE_RUNTIME, 0x04 }, + { BLE_MESH_TOTAL_LIGHT_EXPOSURE_TIME, 0x04 }, + { BLE_MESH_TOTAL_LUMINOUS_ENERGY, 0x04 }, +}; + +u8_t bt_mesh_get_dev_prop_len(u16_t prop_id) +{ + if (prop_id > BLE_MESH_TOTAL_LUMINOUS_ENERGY) { + BT_ERR("%s, Unknown Device Property ID 0x%04x", __func__, prop_id); + return UINT8_MAX; + } + + return device_properties[prop_id].len; +} diff --git a/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c new file mode 100644 index 000000000..ab74f633d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/generic_server.c @@ -0,0 +1,2795 @@ +/* Bluetooth: Mesh Generic Server Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc_ble_mesh_generic_model.h" + +#include "access.h" +#include "transport.h" +#include "model_opcode.h" +#include "state_transition.h" +#include "device_property.h" + +static bt_mesh_mutex_t generic_server_lock; + +static void bt_mesh_generic_server_mutex_new(void) +{ + if (!generic_server_lock.mutex) { + bt_mesh_mutex_create(&generic_server_lock); + } +} + +static void bt_mesh_generic_server_mutex_free(void) +{ + bt_mesh_mutex_free(&generic_server_lock); +} + +void bt_mesh_generic_server_lock(void) +{ + bt_mesh_mutex_lock(&generic_server_lock); +} + +void bt_mesh_generic_server_unlock(void) +{ + bt_mesh_mutex_unlock(&generic_server_lock); +} + +/* message handlers (Start) */ + +/* Generic OnOff Server message handlers */ +static void send_gen_onoff_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish) +{ + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 3; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS); + net_buf_simple_add_u8(msg, srv->state.onoff); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->state.target_onoff); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_onoff_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + send_gen_onoff_status(model, ctx, false); + return; +} + +void gen_onoff_publish(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + send_gen_onoff_status(model, NULL, true); + return; +} + +static void gen_onoff_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + u8_t tid = 0U, onoff = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s64_t now = 0; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + onoff = net_buf_simple_pull_u8(buf); + if (onoff > BLE_MESH_STATE_ON) { + BT_ERR("%s, Invalid OnOff value 0x%02x", __func__, onoff); + return; + } + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .onoff_set.op_en = optional, + .onoff_set.onoff = onoff, + .onoff_set.tid = tid, + .onoff_set.trans_time = trans_time, + .onoff_set.delay = delay, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + send_gen_onoff_status(model, ctx, false); + } + send_gen_onoff_status(model, NULL, true); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_generic_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state.target_onoff = onoff; + + if (srv->state.target_onoff != srv->state.onoff) { + generic_onoff_tt_values(srv, trans_time, delay); + } else { + bt_mesh_gen_server_state_change_t change = { + .gen_onoff_set.onoff = srv->state.onoff, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + send_gen_onoff_status(model, ctx, false); + } + send_gen_onoff_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state.onoff = srv->state.target_onoff; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { + send_gen_onoff_status(model, ctx, false); + } + send_gen_onoff_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +/* Generic Level Server message handlers */ +static void send_gen_level_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish) +{ + struct bt_mesh_gen_level_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 5; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS); + net_buf_simple_add_le16(msg, srv->state.level); + if (srv->transition.counter) { + if (srv->state.move_start) { + if (srv->state.positive) { + net_buf_simple_add_le16(msg, INT16_MAX); + } else { /* 0 should not be possible */ + net_buf_simple_add_le16(msg, INT16_MIN); + } + net_buf_simple_add_u8(msg, BLE_MESH_UNKNOWN_REMAIN_TIME); + } else { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state.target_level); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_level_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_level_srv *srv = model->user_data; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + send_gen_level_status(model, ctx, false); + return; +} + +void gen_level_publish(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + send_gen_level_status(model, NULL, true); + return; +} + +static void gen_level_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_level_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s16_t level = 0; + s64_t now = 0; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + level = (s16_t) net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .level_set.op_en = optional, + .level_set.level = level, + .level_set.tid = tid, + .level_set.trans_time = trans_time, + .level_set.delay = delay, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_generic_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state.target_level = level; + + /** + * If the target state is equal to the current state, the transition + * shall not be started and is considered complete. + */ + if (srv->state.target_level != srv->state.level) { + generic_level_tt_values(srv, trans_time, delay); + } else { + bt_mesh_gen_server_state_change_t change = { + .gen_level_set.level = srv->state.level, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state.level = srv->state.target_level; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void gen_delta_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_level_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + s32_t tmp32 = 0, delta = 0; + bool optional = false; + s64_t now = 0; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + delta = (s32_t)net_buf_simple_pull_le32(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .delta_set.op_en = optional, + .delta_set.delta_level = delta, + .delta_set.tid = tid, + .delta_set.trans_time = trans_time, + .delta_set.delay = delay, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + /** + * A number of Generic Delta Set and Generic Delta Set Unacknowledged + * messages with the same transaction identifier set in the TID field + * may be sent. + * + * A new transaction starts when the TID field value in the received + * message is different from the TID field value in the previously + * received message that was using the same source and destination + * addresses or from the most recently received message with the same + * TID field value that was received 6 or more seconds earlier. + */ + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (srv->state.last_delta == delta) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + /* In this condition, no event will be callback to application layer */ + return; + } + + tmp32 = srv->state.last_level + delta; + } else { + /* Starts a new transaction */ + srv->state.last_level = srv->state.level; + tmp32 = srv->state.level + delta; + } + + bt_mesh_generic_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state.last_delta = delta; + if (tmp32 < INT16_MIN) { + tmp32 = INT16_MIN; + } else if (tmp32 > INT16_MAX) { + tmp32 = INT16_MAX; + } + srv->state.target_level = tmp32; + + /** + * If the target state is equal to the current state, the transition + * shall not be started and is considered complete. + */ + if (srv->state.target_level != srv->state.level) { + generic_level_tt_values(srv, trans_time, delay); + } else { + bt_mesh_gen_server_state_change_t change = { + .gen_delta_set.level = srv->state.level, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state.level = srv->state.target_level; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void gen_move_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_level_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s16_t delta = 0; + s32_t tmp32 = 0; + s64_t now = 0; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + delta = (s16_t) net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .move_set.op_en = optional, + .move_set.delta_level = delta, + .move_set.tid = tid, + .move_set.trans_time = trans_time, + .move_set.delay = delay, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_generic_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state.last_delta = delta; + + tmp32 = srv->state.level + delta; + if (tmp32 < INT16_MIN) { + tmp32 = INT16_MIN; + } else if (tmp32 > INT16_MAX) { + tmp32 = INT16_MAX; + } + srv->state.target_level = tmp32; + + /** + * If the target state is equal to the current state, the transition + * shall not be started and is considered complete. + */ + if (srv->state.target_level != srv->state.level) { + generic_level_tt_values(srv, trans_time, delay); + } else { + bt_mesh_gen_server_state_change_t change = { + .gen_move_set.level = srv->state.level, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + srv->state.move_start = false; + + bt_mesh_generic_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + if (delta) { + srv->state.move_start = true; + srv->state.positive = (delta > 0) ? true : false; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) { + send_gen_level_status(model, ctx, false); + } + send_gen_level_status(model, NULL, true); + + bt_mesh_generic_server_unlock(); + + /** + * If (trans_time == 0) OR (delta == 0) + * 1. If the resulting Transition Time is equal to 0 or is undefined, + * the Generic Move Set command will not initiate any Generic Level + * state change. + * 2. When a Generic Level Server receives the message with a value of + * the Delta Level field equal to 0, it shall stop changing the + * Generic Level state. (if delta == 0, srv->state.target_level will + * equal to srv->state.level) + */ + if (srv->transition.counter == 0U) { + srv->state.move_start = false; + bt_mesh_gen_server_state_change_t change = { + .gen_move_set.level = srv->state.level, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + return; + } + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +/* Generic Default Transition Time Server message handlers */ +static void send_gen_def_trans_time_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish) +{ + struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 1; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS); + net_buf_simple_add_u8(msg, srv->state.trans_time); + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_def_trans_time_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + send_gen_def_trans_time_status(model, ctx, false); + return; +} + +static void gen_def_trans_time_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data; + u8_t trans_time = 0U; + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + trans_time = net_buf_simple_pull_u8(buf); + if ((trans_time & 0x3F) == 0x3F) { + BT_WARN("%s, Invalid Transaction Number of Steps 0x3F", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .def_trans_time_set.trans_time = trans_time, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state.trans_time != trans_time) { + srv->state.trans_time = trans_time; + } + + bt_mesh_gen_server_state_change_t change = { + .gen_def_trans_time_set.trans_time = trans_time, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET) { + send_gen_def_trans_time_status(model, ctx, false); + } + send_gen_def_trans_time_status(model, NULL, true); + + return; +} + +/* Generic Power OnOff Server message handlers */ +static void send_gen_onpowerup_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 1; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS); + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: { + struct bt_mesh_gen_power_onoff_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->onpowerup); + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: { + struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->onpowerup); + break; + } + default: + BT_ERR("%s, Invalid Generic Power OnOff Server 0x%04x", __func__, model->id); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_onpowerup_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_onoff_srv *srv = model->user_data; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + send_gen_onpowerup_status(model, ctx, false); + return; +} + +/* Generic Power OnOff Setup Server message handlers */ +void gen_onpowerup_publish(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: { + struct bt_mesh_gen_power_onoff_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power OnOff Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: { + struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power OnOff Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Generic Power OnOff Server 0x%04x", __func__, model->id); + return; + } + + send_gen_onpowerup_status(model, NULL, true); + return; +} + +static void gen_onpowerup_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data; + u8_t onpowerup = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + onpowerup = net_buf_simple_pull_u8(buf); + if (onpowerup > BLE_MESH_STATE_RESTORE) { + BT_WARN("%s, Invalid OnPowerUp value 0x%02x", __func__, onpowerup); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .onpowerup_set.onpowerup = onpowerup, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state->onpowerup != onpowerup) { + srv->state->onpowerup = onpowerup; + } + + bt_mesh_gen_server_state_change_t change = { + .gen_onpowerup_set.onpowerup = onpowerup, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET) { + send_gen_onpowerup_status(model, ctx, false); + } + send_gen_onpowerup_status(model, NULL, true); + + return; +} + +/* Generic Power Level Server message handlers */ +static void send_gen_power_level_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 5; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS: + case BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS: { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS) { + net_buf_simple_add_le16(msg, srv->state->power_actual); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_power_actual); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } else if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS) { + net_buf_simple_add_le16(msg, srv->state->power_last); + } + break; + } + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS: + if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->power_default); + } else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) { + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->power_default); + } + break; + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->power_range_min); + net_buf_simple_add_le16(msg, srv->state->power_range_max); + } else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) { + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->power_range_min); + net_buf_simple_add_le16(msg, srv->state->power_range_max); + } + break; + default: + BT_WARN("%s, Unknown Generic Power status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_power_level_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET: + opcode = BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS; + break; + case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET: + opcode = BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS; + break; + case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET: + opcode = BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS; + break; + case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET: + opcode = BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS; + break; + default: + BT_WARN("%s, Unknown Generic Power Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_gen_power_level_status(model, ctx, false, opcode); + return; +} + +void gen_power_level_publish(struct bt_mesh_model *model, u16_t opcode) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power Level Server state", __func__); + return; + } + break; + } + case ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: { + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power Level Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Generic Power Level Server 0x%04x", __func__, model->id); + return; + } + + send_gen_power_level_status(model, NULL, true, opcode); + return; +} + +static void gen_power_level_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t power = 0U; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + power = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .power_level_set.op_en = optional, + .power_level_set.power = power, + .power_level_set.tid = tid, + .power_level_set.trans_time = trans_time, + .power_level_set.delay = delay, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) { + send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + } + send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_generic_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + if (power) { + if (srv->state->power_range_min && power < srv->state->power_range_min) { + power = srv->state->power_range_min; + } else if (srv->state->power_range_max && power > srv->state->power_range_max) { + power = srv->state->power_range_max; + } + } + srv->state->target_power_actual = power; + + /* If the target state is equal to the current state, the transition + * shall not be started and is considered complete. + */ + if (srv->state->target_power_actual != srv->state->power_actual) { + generic_power_level_tt_values(srv, trans_time, delay); + } else { + bt_mesh_gen_server_state_change_t change = { + .gen_power_level_set.power = srv->state->power_actual, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) { + send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + } + send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + + bt_mesh_generic_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->power_actual = srv->state->target_power_actual; + /* Whenever the Generic Power Actual state is changed to a non-zero value + * as a result of a non-transactional message or a completed sequence of + * transactional messages, the value of the Generic Power Last state shall + * be set to the value of the Generic Power Actual state. + */ + if (srv->state->power_actual) { + srv->state->power_last = srv->state->power_actual; + } + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) { + send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + } + send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + + bt_mesh_generic_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +/* Generic Power Level Setup Server message handlers */ +static void gen_power_default_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + u16_t power = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + power = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .power_default_set.power = power, /* Just callback the actual received value */ + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + /** + * Value 0x0000 has a special meaning defined: use the value of the + * Generic Power Last state as the default value. + */ + if (power == 0x0000) { + power = srv->state->power_last; + } + + if (srv->state->power_default != power) { + srv->state->power_default = power; + } + + bt_mesh_gen_server_state_change_t change = { + .gen_power_default_set.power = power, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET) { + send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS); + } + send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS); + + return; +} + +static void gen_power_range_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + u16_t range_min = 0U, range_max = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + range_min = net_buf_simple_pull_le16(buf); + range_max = net_buf_simple_pull_le16(buf); + + if (range_min > range_max) { + BT_ERR("%s, Range Min 0x%04x is greater than Range Max 0x%04x", + __func__, range_min, range_max); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .power_range_set.range_min = range_min, + .power_range_set.range_max = range_max, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (range_min == 0x0000) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN; + } else if (range_max == 0x0000) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX; + } else { + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; + } + + if (range_min && srv->state->power_range_min != range_min) { + srv->state->power_range_min = range_min; + } + + if (range_max && srv->state->power_range_max != range_max) { + srv->state->power_range_max = range_max; + } + + bt_mesh_gen_server_state_change_t change = { + .gen_power_range_set.range_min = srv->state->power_range_min, + .gen_power_range_set.range_max = srv->state->power_range_max, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET) { + send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS); + } + send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS); + + return; +} + +/* Generic Battery Server message handlers */ +static void gen_battery_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_battery_srv *srv = model->user_data; + NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + + if (srv == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS); + net_buf_simple_add_le32(&msg, srv->state.time_to_discharge << 8 | srv->state.battery_level); + net_buf_simple_add_le32(&msg, srv->state.battery_flags << 24 | srv->state.time_to_charge); + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); + return; +} + +/* Generic Location Server message handlers */ +static void send_gen_location_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 1 + 10; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS: + if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) { + struct bt_mesh_gen_location_srv *srv = model->user_data; + net_buf_simple_add_le32(msg, srv->state->global_latitude); + net_buf_simple_add_le32(msg, srv->state->global_longitude); + net_buf_simple_add_le16(msg, srv->state->global_altitude); + } else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) { + struct bt_mesh_gen_location_setup_srv *srv = model->user_data; + net_buf_simple_add_le32(msg, srv->state->global_latitude); + net_buf_simple_add_le32(msg, srv->state->global_longitude); + net_buf_simple_add_le16(msg, srv->state->global_altitude); + } + break; + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS: + if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) { + struct bt_mesh_gen_location_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->local_north); + net_buf_simple_add_le16(msg, srv->state->local_east); + net_buf_simple_add_le16(msg, srv->state->local_altitude); + net_buf_simple_add_u8(msg, srv->state->floor_number); + net_buf_simple_add_le16(msg, srv->state->uncertainty); + } else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) { + struct bt_mesh_gen_location_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->local_north); + net_buf_simple_add_le16(msg, srv->state->local_east); + net_buf_simple_add_le16(msg, srv->state->local_altitude); + net_buf_simple_add_u8(msg, srv->state->floor_number); + net_buf_simple_add_le16(msg, srv->state->uncertainty); + } + break; + default: + BT_WARN("%s, Unknown Generic Location status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_location_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_location_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET: + opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS; + break; + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET: + opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS; + break; + default: + BT_WARN("%s, Unknown Generic Location Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_gen_location_status(model, ctx, false, opcode); + return; +} + +/* Generic Location Setup Server message handlers */ +static void gen_location_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_location_setup_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: { + opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS; + s32_t latitude = net_buf_simple_pull_le32(buf); + s32_t longitude = net_buf_simple_pull_le32(buf); + s16_t altitude = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .loc_global_set.latitude = latitude, + .loc_global_set.longitude = longitude, + .loc_global_set.altitude = altitude, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (latitude != 0x80000000) { + srv->state->global_latitude = latitude; + } + if (longitude != 0x80000000) { + srv->state->global_longitude = longitude; + } + if (altitude != 0x7FFF) { + srv->state->global_altitude = altitude; + } + + bt_mesh_gen_server_state_change_t change = { + .gen_loc_global_set.latitude = srv->state->global_latitude, + .gen_loc_global_set.longitude = srv->state->global_longitude, + .gen_loc_global_set.altitude = srv->state->global_altitude, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + break; + } + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET: + case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: { + opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS; + u16_t north = net_buf_simple_pull_le16(buf); + u16_t east = net_buf_simple_pull_le16(buf); + u16_t altitude = net_buf_simple_pull_le16(buf); + u8_t floor = net_buf_simple_pull_u8(buf); + u16_t uncertainty = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .loc_local_set.north = north, + .loc_local_set.east = east, + .loc_local_set.altitude = altitude, + .loc_local_set.floor_number = floor, + .loc_local_set.uncertainty = uncertainty, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (north != 0x8000) { + srv->state->local_north = north; + } + if (east != 0x8000) { + srv->state->local_east = east; + } + if (altitude != 0x7FFF) { + srv->state->local_altitude = altitude; + } + if (floor != 0xFF) { + srv->state->floor_number = floor; + } + srv->state->uncertainty = uncertainty; + + bt_mesh_gen_server_state_change_t change = { + .gen_loc_local_set.north = srv->state->local_north, + .gen_loc_local_set.east = srv->state->local_east, + .gen_loc_local_set.altitude = srv->state->local_altitude, + .gen_loc_local_set.floor_number = srv->state->floor_number, + .gen_loc_local_set.uncertainty = srv->state->uncertainty, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + break; + } + default: + BT_WARN("%s, Unknown Generic Location Set opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET || + ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET) { + send_gen_location_status(model, ctx, false, opcode); + } + send_gen_location_status(model, NULL, true, opcode); + + return; +} + +/* Generic User Property Server message handlers */ +struct bt_mesh_generic_property *gen_get_user_property(struct bt_mesh_model *model, + u16_t property_id) +{ + struct bt_mesh_gen_user_prop_srv *srv = model->user_data; + int i; + + for (i = 0; i < srv->property_count; i++) { + if (srv->properties[i].id == property_id) { + return &srv->properties[i]; + } + } + + return NULL; +} + +static void send_gen_user_prop_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t property_id, bool publish) +{ + struct bt_mesh_generic_property *property = NULL; + struct net_buf_simple *msg = NULL; + u16_t length = 0U; + + if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { + BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); + return; + } + + property = gen_get_user_property(model, property_id); + if (property == NULL) { + BT_WARN("%s, User property 0x%04x not exist", __func__, property_id); + length = sizeof(property_id); + } else { + length = sizeof(property->id) + sizeof(property->user_access) + property->val->len; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 1 + length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS); + if (property == NULL) { + net_buf_simple_add_le16(msg, property_id); + } else { + net_buf_simple_add_le16(msg, property->id); + net_buf_simple_add_u8(msg, property->user_access); + if ((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET && + property->user_access != USER_ACCESS_PROHIBIT && + property->user_access != USER_ACCESS_WRITE) || + ((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET || + ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK) && + property->user_access != USER_ACCESS_PROHIBIT && + property->user_access != USER_ACCESS_READ)) { + net_buf_simple_add_mem(msg, property->val->data, property->val->len); + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_user_prop_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_user_prop_srv *srv = model->user_data; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + /** + * Also we can use __packed for esp_ble_mesh_gen_user_property_get_t, + * and directly callback with buf->data & buf->len. + */ + bt_mesh_gen_server_recv_get_msg_t get = {0}; + const u8_t *param = NULL; + size_t len = 0; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET) { + get.user_property_get.id = net_buf_simple_pull_le16(buf); + param = (const u8_t *)&get; + len = sizeof(get); + } + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: { + struct net_buf_simple *msg = NULL; + u8_t i; + msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS); + for (i = 0U; i < srv->property_count; i++) { + if (srv->properties[i].admin_access != ADMIN_NOT_USER_PROP && + srv->properties[i].manu_access != MANU_NOT_USER_PROP) { + net_buf_simple_add_le16(msg, srv->properties[i].id); + } + } + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; + } + case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: { + u16_t property_id = net_buf_simple_pull_le16(buf); + send_gen_user_prop_status(model, ctx, property_id, false); + return; + } + default: + BT_WARN("%s, Unknown Generic User Property Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +static void gen_user_prop_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_user_prop_srv *srv = model->user_data; + struct bt_mesh_generic_property *property = NULL; + u16_t property_id = 0U; + u8_t expect_len = 0U; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + property_id = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .user_property_set.id = property_id, + .user_property_set.value = buf, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + property = gen_get_user_property(model, property_id); + if (property == NULL || property->user_access == USER_ACCESS_PROHIBIT || + property->user_access == USER_ACCESS_READ) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) { + send_gen_user_prop_status(model, ctx, property_id, false); + } + send_gen_user_prop_status(model, ctx, property_id, true); + return; + } + + /* For BLE Mesh Model BQB test: + * PTS will send Generic User Property Set/Set Unack messages with + * invalid device property value length, these messages need to be + * ignored, otherwise the test case will fail. + */ + expect_len = bt_mesh_get_dev_prop_len(property_id); + if (buf->len != expect_len) { + BT_ERR("%s, Invalid User Property length, ID 0x%04x, expect %d, actual %d", + __func__, property_id, expect_len, buf->len); + return; + } + + net_buf_simple_reset(property->val); + net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size)); + + bt_mesh_gen_server_state_change_t change = { + .gen_user_prop_set.id = property_id, + .gen_user_prop_set.value = property->val, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) { + send_gen_user_prop_status(model, ctx, property_id, false); + } + send_gen_user_prop_status(model, ctx, property_id, true); + + return; +} + +/* Generic Admin Property Server message handlers */ +struct bt_mesh_generic_property *gen_get_admin_property(struct bt_mesh_model *model, + u16_t property_id) +{ + struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; + int i; + + for (i = 0; i < srv->property_count; i++) { + if (srv->properties[i].id == property_id) { + return &srv->properties[i]; + } + } + + return NULL; +} + +static void send_gen_admin_prop_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t property_id, bool publish) +{ + struct bt_mesh_generic_property *property = NULL; + struct net_buf_simple *msg = NULL; + u16_t length = 0U; + + if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { + BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); + return; + } + + property = gen_get_admin_property(model, property_id); + if (property == NULL) { + BT_WARN("%s, Admin property 0x%04x not exist", __func__, property_id); + length = sizeof(property_id); + } else { + length = sizeof(property->id) + sizeof(property->admin_access) + property->val->len; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 1 + length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS); + if (property == NULL) { + net_buf_simple_add_le16(msg, property_id); + } else { + net_buf_simple_add_le16(msg, property->id); + net_buf_simple_add_u8(msg, property->admin_access); + if (ctx->recv_op != BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET || + property->admin_access != ADMIN_ACCESS_WRITE) { + net_buf_simple_add_mem(msg, property->val->data, property->val->len); + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_admin_prop_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_get_msg_t get = {0}; + const u8_t *param = NULL; + size_t len = 0U; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET) { + get.admin_property_get.id = net_buf_simple_pull_le16(buf); + param = (const u8_t *)&get; + len = sizeof(get); + } + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: { + struct net_buf_simple *msg = NULL; + u8_t i = 0U; + msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS); + for (i = 0U; i < srv->property_count; i++) { + net_buf_simple_add_le16(msg, srv->properties[i].id); + } + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; + } + case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: { + u16_t property_id = net_buf_simple_pull_le16(buf); + send_gen_admin_prop_status(model, ctx, property_id, false); + return; + } + default: + BT_WARN("%s, Unknown Generic Admin Property Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +static void gen_admin_prop_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; + struct bt_mesh_generic_property *property = NULL; + u16_t property_id = 0U; + u8_t access = 0U; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + property_id = net_buf_simple_pull_le16(buf); + access = net_buf_simple_pull_u8(buf); + if (access > ADMIN_ACCESS_READ_WRITE) { + BT_ERR("%s, Invalid Admin Access 0x%02x", __func__, access); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .admin_property_set.id = property_id, + .admin_property_set.access = access, + .admin_property_set.value = buf, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + property = gen_get_admin_property(model, property_id); + if (property == NULL) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) { + send_gen_admin_prop_status(model, ctx, property_id, false); + } + send_gen_admin_prop_status(model, ctx, property_id, true); + return; + } + + property->admin_access = access; + + net_buf_simple_reset(property->val); + net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size)); + + bt_mesh_gen_server_state_change_t change = { + .gen_admin_prop_set.id = property_id, + .gen_admin_prop_set.access = property->admin_access, + .gen_admin_prop_set.value = property->val, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) { + send_gen_admin_prop_status(model, ctx, property_id, false); + } + send_gen_admin_prop_status(model, ctx, property_id, true); + + return; +} + +/* Generic Manufacturer Property Server message handlers */ +struct bt_mesh_generic_property *gen_get_manu_property(struct bt_mesh_model *model, + u16_t property_id) +{ + struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; + int i; + + for (i = 0; i < srv->property_count; i++) { + if (srv->properties[i].id == property_id) { + return &srv->properties[i]; + } + } + + return NULL; +} + +static void send_gen_manu_prop_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t property_id, bool publish) +{ + struct bt_mesh_generic_property *property = NULL; + struct net_buf_simple *msg = NULL; + u16_t length = 0U; + + if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) { + BT_ERR("%s, Invalid User Property ID 0x%04x", __func__, property_id); + return; + } + + property = gen_get_manu_property(model, property_id); + if (property == NULL) { + BT_WARN("%s, Manufacturer property 0x%04x not exist", __func__, property_id); + length = sizeof(property_id); + } else { + length = sizeof(property->id) + sizeof(property->manu_access) + property->val->len; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 1 + length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS); + if (property == NULL) { + net_buf_simple_add_le16(msg, property_id); + } else { + net_buf_simple_add_le16(msg, property->id); + net_buf_simple_add_u8(msg, property->manu_access); + net_buf_simple_add_mem(msg, property->val->data, property->val->len); + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void gen_manu_prop_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_get_msg_t get = {0}; + const u8_t *param = NULL; + size_t len = 0U; + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET) { + get.manu_property_get.id = net_buf_simple_pull_le16(buf); + param = (const u8_t *)&get; + len = sizeof(get); + } + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, param, len); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: { + struct net_buf_simple *msg = NULL; + u8_t i = 0U; + msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS); + for (i = 0U; i < srv->property_count; i++) { + net_buf_simple_add_le16(msg, srv->properties[i].id); + } + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; + } + case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: { + u16_t property_id = net_buf_simple_pull_le16(buf); + send_gen_manu_prop_status(model, ctx, property_id, false); + return; + } + default: + BT_WARN("%s, Unknown Generic Manufacturer Property Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +static void gen_manu_prop_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; + struct bt_mesh_generic_property *property = NULL; + u16_t property_id = 0U; + u8_t access = 0U; + + if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + property_id = net_buf_simple_pull_le16(buf); + access = net_buf_simple_pull_u8(buf); + if (access > MANU_ACCESS_READ) { + BT_ERR("%s, Invalid Manufacturer Access 0x%02x", __func__, access); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_set_msg_t set = { + .manu_property_set.id = property_id, + .manu_property_set.access = access, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + property = gen_get_manu_property(model, property_id); + if (property == NULL) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) { + send_gen_manu_prop_status(model, ctx, property_id, false); + } + send_gen_manu_prop_status(model, ctx, property_id, true); + return; + } + + property->manu_access = access; + + bt_mesh_gen_server_state_change_t change = { + .gen_manu_prop_set.id = property_id, + .gen_manu_prop_set.access = property->manu_access, + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) { + send_gen_manu_prop_status(model, ctx, property_id, false); + } + send_gen_manu_prop_status(model, ctx, property_id, true); + + return; +} + +/* Generic Client Property Server message handlers */ +static int search_prop_id_index(const u16_t *array, u8_t array_idx, u16_t id) +{ + static const u16_t *start = NULL; + u8_t index = 0U; + u16_t temp = 0U; + + if (start == NULL) { + start = array; + } + + if (array_idx == 0U) { + if (*array >= id) { + return array - start; + } else { + return -1; + } + } + + index = array_idx / 2; + temp = array[index]; + + if (temp == id) { + return array + index - start; + } else if (temp > id) { + return search_prop_id_index(array, index, id); + } else { + return search_prop_id_index(array + index + 1, array_idx - 1 - index, id); + } +} + +static void gen_client_prop_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_gen_client_prop_srv *srv = model->user_data; + struct net_buf_simple *sdu = NULL; + u16_t total_len = 5U; + u16_t property_id = 0U; + int i, index = 0; + + if (srv == NULL || srv->id_count == 0U || srv->property_ids == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_gen_server_recv_get_msg_t get = { + .client_properties_get.id = net_buf_simple_pull_le16(buf), + }; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + return; + } + + /* The sequence shall be in an ascending order of Property ID values and shall + * start with a smallest Property ID that is greater than or equal to the value + * of the Generic Client Property field of the Generic Client Properities Get + * message that it is responding to. + */ + + property_id = net_buf_simple_pull_le16(buf); + index = search_prop_id_index(srv->property_ids, srv->id_count - 1, property_id); + if (index < 0) { + NET_BUF_SIMPLE_DEFINE(msg, 1 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS); + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); + return; + } + + sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); + if (sdu == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + bt_mesh_model_msg_init(sdu, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS); + for (i = index; i < srv->id_count; i++) { + total_len += sizeof(u16_t); + if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) { + /* Add this in case the message is too long */ + break; + } + net_buf_simple_add_le16(sdu, srv->property_ids[i]); + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, sdu, NULL, NULL)); + bt_mesh_free_buf(sdu); + return; +} + +/* message handlers (End) */ + +/* Mapping of message handlers for Generic OnOff Server (0x1000) */ +const struct bt_mesh_model_op gen_onoff_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0, gen_onoff_get }, + { BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2, gen_onoff_set }, + { BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Level Server (0x1002) */ +const struct bt_mesh_model_op gen_level_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_LEVEL_GET, 0, gen_level_get }, + { BLE_MESH_MODEL_OP_GEN_LEVEL_SET, 3, gen_level_set }, + { BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK, 3, gen_level_set }, + { BLE_MESH_MODEL_OP_GEN_DELTA_SET, 5, gen_delta_set }, + { BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK, 5, gen_delta_set }, + { BLE_MESH_MODEL_OP_GEN_MOVE_SET, 3, gen_move_set }, + { BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK, 3, gen_move_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Default TT Server (0x1004) */ +const struct bt_mesh_model_op gen_def_trans_time_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET, 0, gen_def_trans_time_get }, + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET, 1, gen_def_trans_time_set }, + { BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK, 1, gen_def_trans_time_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Power OnOff Server (0x1006) */ +const struct bt_mesh_model_op gen_power_onoff_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET, 0, gen_onpowerup_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Power OnOff Setup Server (0x1007) */ +const struct bt_mesh_model_op gen_power_onoff_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET, 1, gen_onpowerup_set }, + { BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK, 1, gen_onpowerup_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Power Level Server (0x1009) */ +const struct bt_mesh_model_op gen_power_level_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET, 0, gen_power_level_get }, + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET, 3, gen_power_level_set }, + { BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK, 3, gen_power_level_set }, + { BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET, 0, gen_power_level_get }, + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET, 0, gen_power_level_get }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET, 0, gen_power_level_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Power Level Setup Server (0x100A) */ +const struct bt_mesh_model_op gen_power_level_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET, 2, gen_power_default_set }, + { BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK, 2, gen_power_default_set }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET, 4, gen_power_range_set }, + { BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK, 4, gen_power_range_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Battery Server (0x100C) */ +const struct bt_mesh_model_op gen_battery_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_BATTERY_GET, 0, gen_battery_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Location Server (0x100E) */ +const struct bt_mesh_model_op gen_location_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET, 0, gen_location_get }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET, 0, gen_location_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Location Setup Server (0x100F) */ +const struct bt_mesh_model_op gen_location_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET, 10, gen_location_set }, + { BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK, 10, gen_location_set }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET, 9, gen_location_set }, + { BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK, 9, gen_location_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic User Property Server (0x1013) */ +const struct bt_mesh_model_op gen_user_prop_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET, 0, gen_user_prop_get }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET, 2, gen_user_prop_get }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET, 3, gen_user_prop_set }, + { BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK, 3, gen_user_prop_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Admin Property Server (0x1011) */ +const struct bt_mesh_model_op gen_admin_prop_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET, 0, gen_admin_prop_get }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET, 2, gen_admin_prop_get }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET, 4, gen_admin_prop_set }, + { BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK, 4, gen_admin_prop_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Manufacturer Property Server (0x1012) */ +const struct bt_mesh_model_op gen_manu_prop_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET, 0, gen_manu_prop_get }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET, 2, gen_manu_prop_get }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET, 3, gen_manu_prop_set }, + { BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK, 3, gen_manu_prop_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Generic Client Property Server (0x1014) */ +const struct bt_mesh_model_op gen_client_prop_srv_op[] = { + { BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, 2, gen_client_prop_get }, + BLE_MESH_MODEL_OP_END, +}; + +static inline int property_id_compare(const void *p1, const void *p2) +{ + if (*(u16_t *)p1 < * (u16_t *)p2) { + return -1; + } + if (*(u16_t *)p1 > *(u16_t *)p2) { + return 1; + } + return 0; +} + +static int generic_server_init(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Generic Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: { + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, generic_onoff_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: { + struct bt_mesh_gen_level_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, generic_level_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV: { + struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data; + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: { + struct bt_mesh_gen_power_onoff_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic OnPowerUp State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: { + struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic OnPowerUp State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Power Level State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, generic_power_level_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: { + struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Power Level State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV: { + struct bt_mesh_gen_battery_srv *srv = model->user_data; + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV: { + struct bt_mesh_gen_location_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Location State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV: { + struct bt_mesh_gen_location_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Location State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV: { + struct bt_mesh_gen_user_prop_srv *srv = model->user_data; + if (srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, NULL Generic User Property State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV: { + struct bt_mesh_gen_admin_prop_srv *srv = model->user_data; + if (srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, NULL Generic Admin Property State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV: { + struct bt_mesh_gen_manu_prop_srv *srv = model->user_data; + if (srv->property_count == 0U || srv->properties == NULL) { + BT_ERR("%s, NULL Generic Manufacturer Property State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV: { + struct bt_mesh_gen_client_prop_srv *srv = model->user_data; + if (srv->id_count == 0U || srv->property_ids == NULL) { + BT_ERR("%s, NULL Generic Client Property State", __func__); + return -EINVAL; + } + /* Quick sort the Client Property IDs in ascending order */ + qsort(srv->property_ids, srv->id_count, sizeof(u16_t), property_id_compare); + srv->model = model; + break; + } + default: + BT_WARN("%s, Unknown Generic Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_generic_server_mutex_new(); + + return 0; +} + +int bt_mesh_gen_onoff_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic OnOff Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_level_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Level Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_def_trans_time_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Default Trans Time Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_power_onoff_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power OnOff Server has no publication support", __func__); + return -EINVAL; + } + + /* When this model is present on an element, the corresponding Generic + * Power OnOff Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV) == NULL) { + BT_WARN("%s, Generic Power OnOff Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return generic_server_init(model); +} + +int bt_mesh_gen_power_onoff_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return generic_server_init(model); +} + +int bt_mesh_gen_power_level_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power Level Server has no publication support", __func__); + return -EINVAL; + } + + /* When this model is present on an Element, the corresponding Generic + * Power Level Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) == NULL) { + BT_WARN("%s, Generic Power Level Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return generic_server_init(model); +} + +int bt_mesh_gen_power_level_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return generic_server_init(model); +} + +int bt_mesh_gen_battery_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Battery Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_location_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Location Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_location_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + /* When this model is present on an Element, the corresponding Generic + * Location Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) == NULL) { + BT_WARN("%s, Generic Location Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return generic_server_init(model); +} + +int bt_mesh_gen_user_prop_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic User Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_admin_prop_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Admin Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_manu_prop_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Manufacturer Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +int bt_mesh_gen_client_prop_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Client Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_init(model); +} + +static int generic_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Generic Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: { + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: { + struct bt_mesh_gen_level_srv *srv = model->user_data; + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: { + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Generic Power Level State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Generic Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_generic_server_mutex_free(); + + return 0; +} + +int bt_mesh_gen_onoff_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic OnOff Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_level_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Level Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_def_trans_time_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Default Trans Time Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_onoff_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power OnOff Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_level_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Power Level Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_power_level_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_battery_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Battery Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_location_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Location Server has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_location_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return generic_server_deinit(model); +} + +int bt_mesh_gen_user_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic User Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_admin_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Admin Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_manu_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Manufacturer Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} + +int bt_mesh_gen_client_prop_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Generic Client Property has no publication support", __func__); + return -EINVAL; + } + + return generic_server_deinit(model); +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/device_property.h b/components/bt/esp_ble_mesh/mesh_models/server/include/device_property.h new file mode 100644 index 000000000..35bdc34ef --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/device_property.h @@ -0,0 +1,1053 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _DEVICE_PROPERTY_H_ +#define _DEVICE_PROPERTY_H_ + +#include "mesh_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * BLE Mesh Device Properties. + * + * Name Type ID Characteristic Type Size + * Average Ambient Temperature In A Period Of Day org.bluetooth.property.average_ambient_temperature_in_a_period_of_day 0x0001 Temperature 8 In A Period Of Day 3 + * Average Input Current org.bluetooth.property.average_input_current 0x0002 Average Current 3 + * Average Input Voltage org.bluetooth.property.average_input_voltage 0x0003 Average Voltage 3 + * Average Output Current org.bluetooth.property.average_output_current 0x0004 Average Current 3 + * Average Output Voltage org.bluetooth.property.average_output_voltage 0x0005 Average Voltage 3 + * Center Beam Intensity At Full Power org.bluetooth.property.center_beam_intensity_at_full_power 0x0006 Luminous Intensity 2 + * Chromaticity Tolerance org.bluetooth.property.chromaticity_tolerance 0x0007 Chromaticity Tolerance 1 + * Color Rendering Index R9 org.bluetooth.property.color_rendering_index_r9 0x0008 Cie 13.3-1995 Color Rendering Index 1 + * Color Rendering Index Ra org.bluetooth.property.color_rendering_index_ra 0x0009 Cie 13.3-1995 Color Rendering Index 1 + * Device Appearance org.bluetooth.property.device_appearance 0x000A Gap.Appearance 2 + * Device Country Of Origin org.bluetooth.property.device_country_of_origin 0x000B Country Code 2 + * Device Date Of Manufacture org.bluetooth.property.device_date_of_manufacture 0x000C Date Utc 4 + * Device Energy Use Since Turn On org.bluetooth.property.device_energy_use_since_turn_on 0x000D Energy 4 + * Device Firmware Revision org.bluetooth.property.device_firmware_revision 0x000E Fixed String 8 8 + * Device Global Trade Item Number org.bluetooth.property.device_global_trade_item_number 0x000F Global Trade Item Number 8 + * Device Hardware Revision org.bluetooth.property.device_hardware_revision 0x0010 Fixed String 16 16 + * Device Manufacturer Name org.bluetooth.property.device_manufacturer_name 0x0011 Fixed String 36 36 + * Device Model Number org.bluetooth.property.device_model_number 0x0012 Fixed String 24 24 + * Device Operating Temperature Range Specification org.bluetooth.property.device_operating_temperature_range_specification 0x0013 Temperature Range 4 + * Device Operating Temperature Statistical Values org.bluetooth.property.device_operating_temperature_statistical_values 0x0014 Temperature Statistics 9 + * Device Over Temperature Event Statistics org.bluetooth.property.device_over_temperature_event_statistics 0x0015 Event Statistics 6 + * Device Power Range Specification org.bluetooth.property.device_power_range_specification 0x0016 Power Specification 12 + * Device Runtime Since Turn On org.bluetooth.property.device_runtime_since_turn_on 0x0017 Time Hour 24 4 + * Device Runtime Warranty org.bluetooth.property.device_runtime_warranty 0x0018 Time Hour 24 4 + * Device Serial Number org.bluetooth.property.device_serial_number 0x0019 Fixed String 16 16 + * Device Software Revision org.bluetooth.property.device_software_revision 0x001A Fixed String 8 8 + * Device Under Temperature Event Statistics org.bluetooth.property.device_under_temperature_event_statistics 0x001B Event Statistics 6 + * Indoor Ambient Temperature Statistical Values org.bluetooth.property.indoor_ambient_temperature_statistical_values 0x001C Temperature 8 Statistics 5 + * Initial CIE 1931 Chromaticity Coordinates org.bluetooth.property.initial_cie_1931_chromaticity_coordinates 0x001D Chromaticity Coordinates 4 + * Initial Correlated Color Temperature org.bluetooth.property.initial_correlated_color_temperature 0x001E Correlated Color Temperature 2 + * Initial Luminous Flux org.bluetooth.property.initial_luminous_flux 0x001F Luminous Flux 2 + * Initial Planckian Distance org.bluetooth.property.initial_planckian_distance 0x0020 Chromatic Distance From Planckian 2 + * Input Current Range Specification org.bluetooth.property.input_current_range_specification 0x0021 Electric Current Specification 6 + * Input Current Statistics org.bluetooth.property.input_current_statistics 0x0022 Electric Current Statistics 9 + * Input Over Current Event Statistics org.bluetooth.property.input_over_current_event_statistics 0x0023 Event Statistics 6 + * Input Over Ripple Voltage Event Statistics org.bluetooth.property.input_over_ripple_voltage_event_statistics 0x0024 Event Statistics 6 + * Input Over Voltage Event Statistics org.bluetooth.property.input_over_voltage_event_statistics 0x0025 Event Statistics 6 + * Input Under Current Event Statistics org.bluetooth.property.input_under_current_event_statistics 0x0026 Event Statistics 6 + * Input Under Voltage Event Statistics org.bluetooth.property.input_under_voltage_event_statistics 0x0027 Event Statistics 6 + * Input Voltage Range Specification org.bluetooth.property.input_voltage_range_specification 0x0028 Voltage Specification 6 + * Input Voltage Ripple Specification org.bluetooth.property.input_voltage_ripple_specification 0x0029 Percentage 8 1 + * Input Voltage Statistics org.bluetooth.property.input_voltage_statistics 0x002A Voltage Statistics 9 + * Light Control Ambient LuxLevel On org.bluetooth.property.light_control_ambient_luxlevel_on 0x002B Illuminance 4 + * Light Control Ambient LuxLevel Prolong org.bluetooth.property.light_control_ambient_luxlevel_prolong 0x002C Illuminance 4 + * Light Control Ambient LuxLevel Standby org.bluetooth.property.light_control_ambient_luxlevel_standby 0x002D Illuminance 4 + * Light Control Lightness On org.bluetooth.property.light_control_lightness_on 0x002E Perceived Lightness 2 + * Light Control Lightness Prolong org.bluetooth.property.light_control_lightness_prolong 0x002F Perceived Lightness 2 + * Light Control Lightness Standby org.bluetooth.property.light_control_lightness_standby 0x0030 Perceived Lightness 2 + * Light Control Regulator Accuracy org.bluetooth.property.light_control_regulator_accuracy 0x0031 Percentage 8 1 + * Light Control Regulator Kid org.bluetooth.property.light_control_regulator_kid 0x0032 Coefficient 4 + * Light Control Regulator Kiu org.bluetooth.property.light_control_regulator_kiu 0x0033 Coefficient 4 + * Light Control Regulator Kpd org.bluetooth.property.light_control_regulator_kpd 0x0034 Coefficient 4 + * Light Control Regulator Kpu org.bluetooth.property.light_control_regulator_kpu 0x0035 Coefficient 4 + * Light Control Time Fade org.bluetooth.property.light_control_time_fade 0x0036 Time Millisecond 24 4 + * Light Control Time Fade On org.bluetooth.property.light_control_time_fade_on 0x0037 Time Millisecond 24 4 + * Light Control Time Fade Standby Auto org.bluetooth.property.light_control_time_fade_standby_auto 0x0038 Time Millisecond 24 4 + * Light Control Time Fade Standby Manual org.bluetooth.property.light_control_time_fade_standby_manual 0x0039 Time Millisecond 24 4 + * Light Control Time Occupancy Delay org.bluetooth.property.light_control_time_occupancy_delay 0x003A Time Millisecond 24 4 + * Light Control Time Prolong org.bluetooth.property.light_control_time_prolong 0x003B Time Millisecond 24 4 + * Light Control Time Run On org.bluetooth.property.light_control_time_run_on 0x003C Time Millisecond 24 4 + * Lumen Maintenance Factor org.bluetooth.property.lumen_maintenance_factor 0x003D Percentage 8 1 + * Luminous Efficacy org.bluetooth.property.luminous_efficacy 0x003E Luminous Efficacy 2 + * Luminous Energy Since Turn On org.bluetooth.property.luminous_energy_since_turn_on 0x003F Luminous Energy 4 + * Luminous Exposure org.bluetooth.property.luminous_exposure 0x0040 Luminous Exposure 4 + * Luminous Flux Range org.bluetooth.property.luminous_flux_range 0x0041 Luminous Flux Range 4 + * Motion Sensed org.bluetooth.property.motion_sensed 0x0042 Percentage 8 1 + * Motion Threshold org.bluetooth.property.motion_threshold 0x0043 Percentage 8 1 + * Open Circuit Event Statistics org.bluetooth.property.open_circuit_event_statistics 0x0044 Event Statistics 6 + * Outdoor Statistical Values org.bluetooth.property.outdoor_statistical_values 0x0045 Temperature 8 Statistics 5 + * Output Current Range org.bluetooth.property.output_current_range 0x0046 Electric Current Range 4 + * Output Current Statistics org.bluetooth.property.output_current_statistics 0x0047 Electric Current Statistics 9 + * Output Ripple Voltage Specification org.bluetooth.property.output_ripple_voltage_specification 0x0048 Percentage 8 1 + * Output Voltage Range org.bluetooth.property.output_voltage_range 0x0049 Voltage Specification 6 + * Output Voltage Statistics org.bluetooth.property.output_voltage_statistics 0x004A Voltage Statistics 9 + * Over Output Ripple Voltage Event Statistics org.bluetooth.property.over_output_ripple_voltage_event_statistics 0x004B Event Statistics 6 + * People Count org.bluetooth.property.people_count 0x004C Count 16 2 + * Presence Detected org.bluetooth.property.presence_detected 0x004D Boolean 1 + * Present Ambient Light Level org.bluetooth.property.present_ambient_light_level 0x004E Illuminance 4 + * Present Ambient Temperature org.bluetooth.property.present_ambient_temperature 0x004F Temperature 8 1 + * Present CIE 1931 Chromaticity Coordinates org.bluetooth.property.present_cie_1931_chromaticity 0x0050 Chromaticity Coordinates 4 + * Present Correlated Color Temperature org.bluetooth.property.present_correlated_color_temperature 0x0051 Correlated Color Temperature 2 + * Present Device Input Power org.bluetooth.property.present_device_input_power 0x0052 Power 4 + * Present Device Operating Efficiency org.bluetooth.property.present_device_operating_efficiency 0x0053 Percentage 8 1 + * Present Device Operating Temperature org.bluetooth.property.present_device_operating_temperature 0x0054 Temperature 2 + * Present Illuminance org.bluetooth.property.present_illuminance 0x0055 Illuminance 4 + * Present Indoor Ambient Temperature org.bluetooth.property.present_indoor_ambient_temperature 0x0056 Temperature 8 1 + * Present Input Current org.bluetooth.property.present_input_current 0x0057 Electric Current 2 + * Present Input Ripple Voltage org.bluetooth.property.present_input_ripple_voltage 0x0058 Percentage 8 1 + * Present Input Voltage org.bluetooth.property.present_input_voltage 0x0059 Voltage 2 + * Present Luminous Flux org.bluetooth.property.present_luminous_flux 0x005A Luminous Flux 2 + * Present Outdoor Ambient Temperature org.bluetooth.property.present_outdoor_ambient_temperature 0x005B Temperature 8 1 + * Present Output Current org.bluetooth.property.present_output_current 0x005C Electric Current 2 + * Present Output Voltage org.bluetooth.property.present_output_voltage 0x005D Voltage 2 + * Present Planckian Distance org.bluetooth.property.present_planckian_distance 0x005E Chromatic Distance From Planckian 2 + * Present Relative Output Ripple Voltage org.bluetooth.property.present_relative_output_ripple_voltage 0x005F Percentage 8 1 + * Relative Device Energy Use In A Period Of Day org.bluetooth.property.relative_device_energy_use_in_a_period_of_day 0x0060 Energy In A Period Of Day 6 + * Relative Device Runtime In A Generic Level Range org.bluetooth.property.relative_device_runtime_in_a_generic_level_range 0x0061 Relative Runtime In A Generic Level Range 5 + * Relative Exposure Time In An Illuminance Range org.bluetooth.property.relative_exposure_time_in_an_illuminance_range 0x0062 Relative Value In An Illuminance Range 9 + * Relative Runtime In A Correlated Color Temperature Range org.bluetooth.property.relative_runtime_in_a_correlated_color_temperature_range 0x0063 Luminous Energy 4 + * Relative Runtime In A Device Operating Temperature Range org.bluetooth.property.relative_runtime_in_a_device_operating_temperature_range 0x0064 Relative Value In A Temperature Range 5 + * Relative Runtime In An Input Current Range org.bluetooth.property.relative_runtime_in_an_input_current_range 0x0065 Relative Runtime In A Current Range 5 + * Relative Runtime In An Input Voltage Range org.bluetooth.property.relative_runtime_in_an_input_voltage_range 0x0066 Relative Value In A Voltage Range 5 + * Short Circuit Event Statistics org.bluetooth.property.short_circuit_event_statistics 0x0067 Event Statistics 6 + * Time Since Motion Sensed org.bluetooth.property.time_since_motion_sensed 0x0068 Time Second 16 2 + * Time Since Presence Detected org.bluetooth.property.time_since_presence_detected 0x0069 Time Second 16 2 + * Total Device Energy Use org.bluetooth.property.total_device_energy_use 0x006A Energy 4 + * Total Device Off On Cycles org.bluetooth.property.total_device_off_on_cycles 0x006B Count 24 4 + * Total Device Power On Cycles org.bluetooth.property.total_device_power_on_cycles 0x006C Count 24 4 + * Total Device Power On Time org.bluetooth.property.total_device_power_on_time 0x006D Time Hour 24 4 + * Total Device Runtime org.bluetooth.property.total_device_runtime 0x006E Time Hour 24 4 + * Total Light Exposure Time org.bluetooth.property.total_light_exposure_time 0x006F Time Hour 24 4 + * Total Luminous Energy org.bluetooth.property.total_luminous_energy 0x0070 Luminous Energy 4 + */ + +/** + * Characteristics referenced by BLE Mesh Device Properties. + * + * Name Uniform Type Identifier Assigned Number Specification Level + * Average Current org.bluetooth.characteristic.average_current 2AE0 Adopted + * Average Voltage org.bluetooth.characteristic.average_voltage 2AE1 Adopted + * Boolean org.bluetooth.characteristic.boolean 2AE2 Adopted + * Chromatic Distance From Planckian org.bluetooth.characteristic.chromatic_distance_from_planckian 2AE3 Adopted + * Chromaticity Coordinate org.bluetooth.characteristic.chromaticity_coordinate 2B1C Adopted + * Chromaticity Coordinates org.bluetooth.characteristic.chromaticity_coordinates 2AE4 Adopted + * Chromaticity In CCT And Duv Values org.bluetooth.characteristic.chromaticity_in_cct_and_duv_values 2AE5 Adopted + * Chromaticity Tolerance org.bluetooth.characteristic.chromaticity_tolerance 2AE6 Adopted + * CIE 13.3-1995 Color Rendering Index org.bluetooth.characteristic.cie_13.3-1995_color_rendering_index 2AE7 Adopted + * Coefficient org.bluetooth.characteristic.coefficient 2AE8 Adopted + * Correlated Color Temperature org.bluetooth.characteristic.correlated_color_temperature 2AE9 Adopted + * Count 16 org.bluetooth.characteristic.count_16 2AEA Adopted + * Count 24 org.bluetooth.characteristic.count_24 2AEB Adopted + * Country Code org.bluetooth.characteristic.country_code 2AEC Adopted + * Date UTC org.bluetooth.characteristic.date_utc 2AED Adopted + * Electric Current org.bluetooth.characteristic.electric_current 2AEE Adopted + * Electric Current Range org.bluetooth.characteristic.electric_current_range 2AEF Adopted + * Electric Current Specification org.bluetooth.characteristic.electric_current_specification 2AF0 Adopted + * Electric Current Statistics org.bluetooth.characteristic.electric_current_statistics 2AF1 Adopted + * Energy org.bluetooth.characteristic.energy 2AF2 Adopted + * Energy In A Period Of Day org.bluetooth.characteristic.energy_in_a_period_of_day 2AF3 Adopted + * Event Statistics org.bluetooth.characteristic.event_statistics 2AF4 Adopted + * Fixed String 16 org.bluetooth.characteristic.fixed_string_16 2AF5 Adopted + * Fixed String 24 org.bluetooth.characteristic.fixed_string_24 2AF6 Adopted + * Fixed String 36 org.bluetooth.characteristic.fixed_string_36 2AF7 Adopted + * Fixed String 8 org.bluetooth.characteristic.fixed_string_8 2AF8 Adopted + * Generic Level org.bluetooth.characteristic.generic_level 2AF9 Adopted + * Global Trade Item Number org.bluetooth.characteristic.global_trade_item_number 2AFA Adopted + * Illuminance org.bluetooth.characteristic.illuminance 2AFB Adopted + * Luminous Efficacy org.bluetooth.characteristic.luminous_efficacy 2AFC Adopted + * Luminous Energy org.bluetooth.characteristic.luminous_energy 2AFD Adopted + * Luminous Exposure org.bluetooth.characteristic.luminous_exposure 2AFE Adopted + * Luminous Flux org.bluetooth.characteristic.luminous_flux 2AFF Adopted + * Luminous Flux Range org.bluetooth.characteristic.luminous_flux_range 2B00 Adopted + * Luminous Intensity org.bluetooth.characteristic.luminous_intensity 2B01 Adopted + * Mass Flow org.bluetooth.characteristic.mass_flow 2B02 Adopted + * Mesh Provisioning Data In org.bluetooth.characteristic.mesh_provisioning_data_in 2ADB Adopted + * Mesh Provisioning Data Out org.bluetooth.characteristic.mesh_provisioning_data_out 2ADC Adopted + * Mesh Proxy Data In org.bluetooth.characteristic.mesh_proxy_data_in 2ADD Adopted + * Mesh Proxy Data Out org.bluetooth.characteristic.mesh_proxy_data_out 2ADE Adopted + * Perceived Lightness org.bluetooth.characteristic.perceived_lightness 2B03 Adopted + * Percentage 8 org.bluetooth.characteristic.percentage_8 2B04 Adopted + * Power org.bluetooth.characteristic.power 2B05 Adopted + * Power Specification org.bluetooth.characteristic.power_specification 2B06 Adopted + * Relative Runtime In A Current Range org.bluetooth.characteristic.relative_runtime_in_a_current_range 2B07 Adopted + * Relative Runtime In A Generic Level Range org.bluetooth.characteristic.relative_runtime_in_a_generic_level_range 2B08 Adopted + * Relative Value In A Period of Day org.bluetooth.characteristic.relative_value_in_a_period_of_day 2B0B Adopted + * Relative Value In A Temperature Range org.bluetooth.characteristic.relative_value_in_a_temperature_range 2B0C Adopted + * Relative Value In A Voltage Range org.bluetooth.characteristic.relative_value_in_a_voltage_range 2B09 Adopted + * Relative Value In An Illuminance Range org.bluetooth.characteristic.relative_value_in_an_illuminance_range 2B0A Adopted + * Temperature 8 org.bluetooth.characteristic.temperature_8 2B0D Adopted + * Temperature 8 In A Period Of Day org.bluetooth.characteristic.temperature_8_in_a_period_of_day 2B0E Adopted + * Temperature 8 Statistics org.bluetooth.characteristic.temperature_8_statistics 2B0F Adopted + * Temperature Range org.bluetooth.characteristic.temperature_range 2B10 Adopted + * Temperature Statistics org.bluetooth.characteristic.temperature_statistics 2B11 Adopted + * Time Decihour 8 org.bluetooth.characteristic.time_decihour_8 2B12 Adopted + * Time Exponential 8 org.bluetooth.characteristic.time_exponential_8 2B13 Adopted + * Time Hour 24 org.bluetooth.characteristic.time_hour_24 2B14 Adopted + * Time Millisecond 24 org.bluetooth.characteristic.time_millisecond_24 2B15 Adopted + * Time Second 16 org.bluetooth.characteristic.time_second_16 2B16 Adopted + * Time Second 8 org.bluetooth.characteristic.time_second_8 2B17 Adopted + * Voltage org.bluetooth.characteristic.voltage 2B18 Adopted + * Voltage Specification org.bluetooth.characteristic.voltage_specification 2B19 Adopted + * Voltage Statistics org.bluetooth.characteristic.voltage_statistics 2B1A Adopted + * Volume Flow org.bluetooth.characteristic.volume_flow 2B1B Adopted + */ + +/** + * @brief BLE Mesh Device Property IDs + */ +#define BLE_MESH_AVERAGE_AMBIENT_TEMPERATURE_IN_A_PERIOD_OF_DAY 0x0001 +#define BLE_MESH_AVERAGE_INPUT_CURRENT 0x0002 +#define BLE_MESH_AVERAGE_INPUT_VOLTAGE 0x0003 +#define BLE_MESH_AVERAGE_OUTPUT_CURRENT 0x0004 +#define BLE_MESH_AVERAGE_OUTPUT_VOLTAGE 0x0005 +#define BLE_MESH_CENTER_BEAM_INTENSITY_AT_FULL_POWER 0x0006 +#define BLE_MESH_CHROMATICITY_TOLERANCE 0x0007 +#define BLE_MESH_COLOR_RENDERING_INDEX_R9 0x0008 +#define BLE_MESH_COLOR_RENDERING_INDEX_RA 0x0009 +#define BLE_MESH_DEVICE_APPEARANCE 0x000A +#define BLE_MESH_DEVICE_COUNTRY_OF_ORIGIN 0x000B +#define BLE_MESH_DEVICE_DATE_OF_MANUFACTURE 0x000C +#define BLE_MESH_DEVICE_ENERGY_USE_SINCE_TURN_ON 0x000D +#define BLE_MESH_DEVICE_FIRMWARE_REVISION 0x000E +#define BLE_MESH_DEVICE_GLOBAL_TRADE_ITEM_NUMBER 0x000F +#define BLE_MESH_DEVICE_HARDWARE_REVISION 0x0010 +#define BLE_MESH_DEVICE_MANUFACTURER_NAME 0x0011 +#define BLE_MESH_DEVICE_MODEL_NUMBER 0x0012 +#define BLE_MESH_DEVICE_OPERATING_TEMPERATURE_RANGE_SPECIFICATION 0x0013 +#define BLE_MESH_DEVICE_OPERATING_TEMPERATURE_STATISTICAL_VALUES 0x0014 +#define BLE_MESH_DEVICE_OVER_TEMPERATURE_EVENT_STATISTICS 0x0015 +#define BLE_MESH_DEVICE_POWER_RANGE_SPECIFICATION 0x0016 +#define BLE_MESH_DEVICE_RUNTIME_SINCE_TURN_ON 0x0017 +#define BLE_MESH_DEVICE_RUNTIME_WARRANTY 0x0018 +#define BLE_MESH_DEVICE_SERIAL_NUMBER 0x0019 +#define BLE_MESH_DEVICE_SOFTWARE_REVISION 0x001A +#define BLE_MESH_DEVICE_UNDER_TEMPERATURE_EVENT_STATISTICS 0x001B +#define BLE_MESH_INDOOR_AMBIENT_TEMPERATURE_STATISTICAL_VALUES 0x001C +#define BLE_MESH_INITIAL_CIE_1931_CHROMATICITY_COORDINATES 0x001D +#define BLE_MESH_INITIAL_CORRELATED_COLOR_TEMPERATURE 0x001E +#define BLE_MESH_INITIAL_LUMINOUS_FLUX 0x001F +#define BLE_MESH_INITIAL_PLANCKIAN_DISTANCE 0x0020 +#define BLE_MESH_INPUT_CURRENT_RANGE_SPECIFICATION 0x0021 +#define BLE_MESH_INPUT_CURRENT_STATISTICS 0x0022 +#define BLE_MESH_INPUT_OVER_CURRENT_EVENT_STATISTICS 0x0023 +#define BLE_MESH_INPUT_OVER_RIPPLE_VOLTAGE_EVENT_STATISTICS 0x0024 +#define BLE_MESH_INPUT_OVER_VOLTAGE_EVENT_STATISTICS 0x0025 +#define BLE_MESH_INPUT_UNDER_CURRENT_EVENT_STATISTICS 0x0026 +#define BLE_MESH_INPUT_UNDER_VOLTAGE_EVENT_STATISTICS 0x0027 +#define BLE_MESH_INPUT_VOLTAGE_RANGE_SPECIFICATION 0x0028 +#define BLE_MESH_INPUT_VOLTAGE_RIPPLE_SPECIFICATION 0x0029 +#define BLE_MESH_INPUT_VOLTAGE_STATISTICS 0x002A +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON 0x002B +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG 0x002C +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY 0x002D +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON 0x002E +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG 0x002F +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY 0x0030 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY 0x0031 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KID 0x0032 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU 0x0033 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD 0x0034 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU 0x0035 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE 0x0036 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON 0x0037 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO 0x0038 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL 0x0039 +#define BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY 0x003A +#define BLE_MESH_LIGHT_CONTROL_TIME_PROLONG 0x003B +#define BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON 0x003C +#define BLE_MESH_LUMEN_MAINTENANCE_FACTOR 0x003D +#define BLE_MESH_LUMINOUS_EFFICACY 0x003E +#define BLE_MESH_LUMINOUS_ENERGY_SINCE_TURN_ON 0x003F +#define BLE_MESH_LUMINOUS_EXPOSURE 0x0040 +#define BLE_MESH_LUMINOUS_FLUX_RANGE 0x0041 +#define BLE_MESH_MOTION_SENSED 0x0042 +#define BLE_MESH_MOTION_THRESHOLD 0x0043 +#define BLE_MESH_OPEN_CIRCUIT_EVENT_STATISTICS 0x0044 +#define BLE_MESH_OUTDOOR_STATISTICAL_VALUES 0x0045 +#define BLE_MESH_OUTPUT_CURRENT_RANGE 0x0046 +#define BLE_MESH_OUTPUT_CURRENT_STATISTICS 0x0047 +#define BLE_MESH_OUTPUT_RIPPLE_VOLTAGE_SPECIFICATION 0x0048 +#define BLE_MESH_OUTPUT_VOLTAGE_RANGE 0x0049 +#define BLE_MESH_OUTPUT_VOLTAGE_STATISTICS 0x004A +#define BLE_MESH_OVER_OUTPUT_RIPPLE_VOLTAGE_EVENT_STATISTICS 0x004B +#define BLE_MESH_PEOPLE_COUNT 0x004C +#define BLE_MESH_PRESENCE_DETECTED 0x004D +#define BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL 0x004E +#define BLE_MESH_PRESENT_AMBIENT_TEMPERATURE 0x004F +#define BLE_MESH_PRESENT_CIE_1931_CHROMATICITY 0x0050 +#define BLE_MESH_PRESENT_CORRELATED_COLOR_TEMPERATURE 0x0051 +#define BLE_MESH_PRESENT_DEVICE_INPUT_POWER 0x0052 +#define BLE_MESH_PRESENT_DEVICE_OPERATING_EFFICIENCY 0x0053 +#define BLE_MESH_PRESENT_DEVICE_OPERATING_TEMPERATURE 0x0054 +#define BLE_MESH_PRESENT_ILLUMINANCE 0x0055 +#define BLE_MESH_PRESENT_INDOOR_AMBIENT_TEMPERATURE 0x0056 +#define BLE_MESH_PRESENT_INPUT_CURRENT 0x0057 +#define BLE_MESH_PRESENT_INPUT_RIPPLE_VOLTAGE 0x0058 +#define BLE_MESH_PRESENT_INPUT_VOLTAGE 0x0059 +#define BLE_MESH_PRESENT_LUMINOUS_FLUX 0x005A +#define BLE_MESH_PRESENT_OUTDOOR_AMBIENT_TEMPERATURE 0x005B +#define BLE_MESH_PRESENT_OUTPUT_CURRENT 0x005C +#define BLE_MESH_PRESENT_OUTPUT_VOLTAGE 0x005D +#define BLE_MESH_PRESENT_PLANCKIAN_DISTANCE 0x005E +#define BLE_MESH_PRESENT_RELATIVE_OUTPUT_RIPPLE_VOLTAGE 0x005F +#define BLE_MESH_RELATIVE_DEVICE_ENERGY_USE_IN_A_PERIOD_OF_DAY 0x0060 +#define BLE_MESH_RELATIVE_DEVICE_RUNTIME_IN_A_GENERIC_LEVEL_RANGE 0x0061 +#define BLE_MESH_RELATIVE_EXPOSURE_TIME_IN_AN_ILLUMINANCE_RANGE 0x0062 +#define BLE_MESH_RELATIVE_RUNTIME_IN_A_CORRELATED_COLOR_TEMPERATURE_RANGE 0x0063 +#define BLE_MESH_RELATIVE_RUNTIME_IN_A_DEVICE_OPERATING_TEMPERATURE_RANGE 0x0064 +#define BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_CURRENT_RANGE 0x0065 +#define BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_VOLTAGE_RANGE 0x0066 +#define BLE_MESH_SHORT_CIRCUIT_EVENT_STATISTICS 0x0067 +#define BLE_MESH_TIME_SINCE_MOTION_SENSED 0x0068 +#define BLE_MESH_TIME_SINCE_PRESENCE_DETECTED 0x0069 +#define BLE_MESH_TOTAL_DEVICE_ENERGY_USE 0x006A +#define BLE_MESH_TOTAL_DEVICE_OFF_ON_CYCLES 0x006B +#define BLE_MESH_TOTAL_DEVICE_POWER_ON_CYCLES 0x006C +#define BLE_MESH_TOTAL_DEVICE_POWER_ON_TIME 0x006D +#define BLE_MESH_TOTAL_DEVICE_RUNTIME 0x006E +#define BLE_MESH_TOTAL_LIGHT_EXPOSURE_TIME 0x006F +#define BLE_MESH_TOTAL_LUMINOUS_ENERGY 0x0070 + +/** + * @brief BLE Mesh Device Property value length + */ +#define BLE_MESH_AVERAGE_AMBIENT_TEMPERATURE_IN_A_PERIOD_OF_DAY_LEN 0x03 +#define BLE_MESH_AVERAGE_INPUT_CURRENT_LEN 0x03 +#define BLE_MESH_AVERAGE_INPUT_VOLTAGE_LEN 0x03 +#define BLE_MESH_AVERAGE_OUTPUT_CURRENT_LEN 0x03 +#define BLE_MESH_AVERAGE_OUTPUT_VOLTAGE_LEN 0x03 +#define BLE_MESH_CENTER_BEAM_INTENSITY_AT_FULL_POWER_LEN 0x02 +#define BLE_MESH_CHROMATICITY_TOLERANCE_LEN 0x01 +#define BLE_MESH_COLOR_RENDERING_INDEX_R9_LEN 0x01 +#define BLE_MESH_COLOR_RENDERING_INDEX_RA_LEN 0x01 +#define BLE_MESH_DEVICE_APPEARANCE_LEN 0x02 +#define BLE_MESH_DEVICE_COUNTRY_OF_ORIGIN_LEN 0x02 +#define BLE_MESH_DEVICE_DATE_OF_MANUFACTURE_LEN 0x04 +#define BLE_MESH_DEVICE_ENERGY_USE_SINCE_TURN_ON_LEN 0x04 +#define BLE_MESH_DEVICE_FIRMWARE_REVISION_LEN 0x08 +#define BLE_MESH_DEVICE_GLOBAL_TRADE_ITEM_NUMBER_LEN 0x08 +#define BLE_MESH_DEVICE_HARDWARE_REVISION_LEN 0x16 +#define BLE_MESH_DEVICE_MANUFACTURER_NAME_LEN 0x36 +#define BLE_MESH_DEVICE_MODEL_NUMBER_LEN 0x24 +#define BLE_MESH_DEVICE_OPERATING_TEMPERATURE_RANGE_SPECIFICATION_LEN 0x04 +#define BLE_MESH_DEVICE_OPERATING_TEMPERATURE_STATISTICAL_VALUES_LEN 0x09 +#define BLE_MESH_DEVICE_OVER_TEMPERATURE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_DEVICE_POWER_RANGE_SPECIFICATION_LEN 0x12 +#define BLE_MESH_DEVICE_RUNTIME_SINCE_TURN_ON_LEN 0x04 +#define BLE_MESH_DEVICE_RUNTIME_WARRANTY_LEN 0x04 +#define BLE_MESH_DEVICE_SERIAL_NUMBER_LEN 0x16 +#define BLE_MESH_DEVICE_SOFTWARE_REVISION_LEN 0x08 +#define BLE_MESH_DEVICE_UNDER_TEMPERATURE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INDOOR_AMBIENT_TEMPERATURE_STATISTICAL_VALUES_LEN 0x05 +#define BLE_MESH_INITIAL_CIE_1931_CHROMATICITY_COORDINATES_LEN 0x04 +#define BLE_MESH_INITIAL_CORRELATED_COLOR_TEMPERATURE_LEN 0x02 +#define BLE_MESH_INITIAL_LUMINOUS_FLUX_LEN 0x02 +#define BLE_MESH_INITIAL_PLANCKIAN_DISTANCE_LEN 0x02 +#define BLE_MESH_INPUT_CURRENT_RANGE_SPECIFICATION_LEN 0x06 +#define BLE_MESH_INPUT_CURRENT_STATISTICS_LEN 0x09 +#define BLE_MESH_INPUT_OVER_CURRENT_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INPUT_OVER_RIPPLE_VOLTAGE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INPUT_OVER_VOLTAGE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INPUT_UNDER_CURRENT_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INPUT_UNDER_VOLTAGE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_INPUT_VOLTAGE_RANGE_SPECIFICATION_LEN 0x06 +#define BLE_MESH_INPUT_VOLTAGE_RIPPLE_SPECIFICATION_LEN 0x01 +#define BLE_MESH_INPUT_VOLTAGE_STATISTICS_LEN 0x09 +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON_LEN 0x02 +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG_LEN 0x02 +#define BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY_LEN 0x02 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY_LEN 0x01 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KID_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU_LEN 0x04 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_PROLONG_LEN 0x03 +#define BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON_LEN 0x03 +#define BLE_MESH_LUMEN_MAINTENANCE_FACTOR_LEN 0x01 +#define BLE_MESH_LUMINOUS_EFFICACY_LEN 0x02 +#define BLE_MESH_LUMINOUS_ENERGY_SINCE_TURN_ON_LEN 0x04 +#define BLE_MESH_LUMINOUS_EXPOSURE_LEN 0x04 +#define BLE_MESH_LUMINOUS_FLUX_RANGE_LEN 0x04 +#define BLE_MESH_MOTION_SENSED_LEN 0x01 +#define BLE_MESH_MOTION_THRESHOLD_LEN 0x01 +#define BLE_MESH_OPEN_CIRCUIT_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_OUTDOOR_STATISTICAL_VALUES_LEN 0x05 +#define BLE_MESH_OUTPUT_CURRENT_RANGE_LEN 0x04 +#define BLE_MESH_OUTPUT_CURRENT_STATISTICS_LEN 0x09 +#define BLE_MESH_OUTPUT_RIPPLE_VOLTAGE_SPECIFICATION_LEN 0x01 +#define BLE_MESH_OUTPUT_VOLTAGE_RANGE_LEN 0x06 +#define BLE_MESH_OUTPUT_VOLTAGE_STATISTICS_LEN 0x09 +#define BLE_MESH_OVER_OUTPUT_RIPPLE_VOLTAGE_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_PEOPLE_COUNT_LEN 0x02 +#define BLE_MESH_PRESENCE_DETECTED_LEN 0x01 +#define BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL_LEN 0x04 +#define BLE_MESH_PRESENT_AMBIENT_TEMPERATURE_LEN 0x01 +#define BLE_MESH_PRESENT_CIE_1931_CHROMATICITY_LEN 0x04 +#define BLE_MESH_PRESENT_CORRELATED_COLOR_TEMPERATURE_LEN 0x02 +#define BLE_MESH_PRESENT_DEVICE_INPUT_POWER_LEN 0x04 +#define BLE_MESH_PRESENT_DEVICE_OPERATING_EFFICIENCY_LEN 0x01 +#define BLE_MESH_PRESENT_DEVICE_OPERATING_TEMPERATURE_LEN 0x02 +#define BLE_MESH_PRESENT_ILLUMINANCE_LEN 0x04 +#define BLE_MESH_PRESENT_INDOOR_AMBIENT_TEMPERATURE_LEN 0x01 +#define BLE_MESH_PRESENT_INPUT_CURRENT_LEN 0x02 +#define BLE_MESH_PRESENT_INPUT_RIPPLE_VOLTAGE_LEN 0x01 +#define BLE_MESH_PRESENT_INPUT_VOLTAGE_LEN 0x02 +#define BLE_MESH_PRESENT_LUMINOUS_FLUX_LEN 0x02 +#define BLE_MESH_PRESENT_OUTDOOR_AMBIENT_TEMPERATURE_LEN 0x01 +#define BLE_MESH_PRESENT_OUTPUT_CURRENT_LEN 0x02 +#define BLE_MESH_PRESENT_OUTPUT_VOLTAGE_LEN 0x02 +#define BLE_MESH_PRESENT_PLANCKIAN_DISTANCE_LEN 0x02 +#define BLE_MESH_PRESENT_RELATIVE_OUTPUT_RIPPLE_VOLTAGE_LEN 0x01 +#define BLE_MESH_RELATIVE_DEVICE_ENERGY_USE_IN_A_PERIOD_OF_DAY_LEN 0x06 +#define BLE_MESH_RELATIVE_DEVICE_RUNTIME_IN_A_GENERIC_LEVEL_RANGE_LEN 0x05 +#define BLE_MESH_RELATIVE_EXPOSURE_TIME_IN_AN_ILLUMINANCE_RANGE_LEN 0x09 +#define BLE_MESH_RELATIVE_RUNTIME_IN_A_CORRELATED_COLOR_TEMPERATURE_RANGE_LEN 0x04 +#define BLE_MESH_RELATIVE_RUNTIME_IN_A_DEVICE_OPERATING_TEMPERATURE_RANGE_LEN 0x05 +#define BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_CURRENT_RANGE_LEN 0x05 +#define BLE_MESH_RELATIVE_RUNTIME_IN_AN_INPUT_VOLTAGE_RANGE_LEN 0x05 +#define BLE_MESH_SHORT_CIRCUIT_EVENT_STATISTICS_LEN 0x06 +#define BLE_MESH_TIME_SINCE_MOTION_SENSED_LEN 0x02 +#define BLE_MESH_TIME_SINCE_PRESENCE_DETECTED_LEN 0x02 +#define BLE_MESH_TOTAL_DEVICE_ENERGY_USE_LEN 0x04 +#define BLE_MESH_TOTAL_DEVICE_OFF_ON_CYCLES_LEN 0x04 +#define BLE_MESH_TOTAL_DEVICE_POWER_ON_CYCLES_LEN 0x04 +#define BLE_MESH_TOTAL_DEVICE_POWER_ON_TIME_LEN 0x04 +#define BLE_MESH_TOTAL_DEVICE_RUNTIME_LEN 0x04 +#define BLE_MESH_TOTAL_LIGHT_EXPOSURE_TIME_LEN 0x04 +#define BLE_MESH_TOTAL_LUMINOUS_ENERGY_LEN 0x04 + +/** + * @brief BLE Mesh Device Property referenced Characteristic UUIDs + */ +#define BLE_MESH_UUID_AVERAGE_CURRENT_VAL 0x2AE0 +#define BLE_MESH_UUID_AVERAGE_VOLTAGE_VAL 0x2AE1 +#define BLE_MESH_UUID_BOOLEAN_VAL 0x2AE2 +#define BLE_MESH_UUID_CHROMATIC_DISTANCE_FROM_PLANCKIAN_VAL 0x2AE3 +#define BLE_MESH_UUID_CHROMATICITY_COORDINATE_VAL 0x2B1C +#define BLE_MESH_UUID_CHROMATICITY_COORDINATES_VAL 0x2AE4 +#define BLE_MESH_UUID_CHROMATICITY_IN_CCT_AND_DUV_VALUES_VAL 0x2AE5 +#define BLE_MESH_UUID_CHROMATICITY_TOLERANCE_VAL 0x2AE6 +#define BLE_MESH_UUID_CIE_13_3_1995_COLOR_RENDERING_INDEX_VAL 0x2AE7 +#define BLE_MESH_UUID_COEFFICIENT_VAL 0x2AE8 +#define BLE_MESH_UUID_CORRELATED_COLOR_TEMPERATURE_VAL 0x2AE9 +#define BLE_MESH_UUID_COUNT_16_VAL 0x2AEA +#define BLE_MESH_UUID_COUNT_24_VAL 0x2AEB +#define BLE_MESH_UUID_COUNTRY_CODE_VAL 0x2AEC +#define BLE_MESH_UUID_DATE_UTC_VAL 0x2AED +#define BLE_MESH_UUID_ELECTRIC_CURRENT_VAL 0x2AEE +#define BLE_MESH_UUID_ELECTRIC_CURRENT_RANGE_VAL 0x2AEF +#define BLE_MESH_UUID_ELECTRIC_CURRENT_SPECIFICATION_VAL 0x2AF0 +#define BLE_MESH_UUID_ELECTRIC_CURRENT_STATISTICS_VAL 0x2AF1 +#define BLE_MESH_UUID_ENERGY_VAL 0x2AF2 +#define BLE_MESH_UUID_ENERGY_IN_A_PERIOD_OF_DAY_VAL 0x2AF3 +#define BLE_MESH_UUID_EVENT_STATISTICS_VAL 0x2AF4 +#define BLE_MESH_UUID_FIXED_STRING_16_VAL 0x2AF5 +#define BLE_MESH_UUID_FIXED_STRING_24_VAL 0x2AF6 +#define BLE_MESH_UUID_FIXED_STRING_36_VAL 0x2AF7 +#define BLE_MESH_UUID_FIXED_STRING_8_VAL 0x2AF8 +#define BLE_MESH_UUID_GENERIC_LEVEL_VAL 0x2AF9 +#define BLE_MESH_UUID_GLOBAL_TRADE_ITEM_NUMBER_VAL 0x2AFA +#define BLE_MESH_UUID_ILLUMINANCE_VAL 0x2AFB +#define BLE_MESH_UUID_LUMINOUS_EFFICACY_VAL 0x2AFC +#define BLE_MESH_UUID_LUMINOUS_ENERGY_VAL 0x2AFD +#define BLE_MESH_UUID_LUMINOUS_EXPOSURE_VAL 0x2AFE +#define BLE_MESH_UUID_LUMINOUS_FLUX_VAL 0x2AFF +#define BLE_MESH_UUID_LUMINOUS_FLUX_RANGE_VAL 0x2B00 +#define BLE_MESH_UUID_LUMINOUS_INTENSITY_VAL 0x2B01 +#define BLE_MESH_UUID_MASS_FLOW_VAL 0x2B02 +/** + * The following four have been defined in mesh_uuid.h + * #define BLE_MESH_UUID_MESH_PROV_DATA_IN_VAL 0x2ADB + * #define BLE_MESH_UUID_MESH_PROV_DATA_OUT_VAL 0x2ADC + * #define BLE_MESH_UUID_MESH_PROXY_DATA_IN_VAL 0x2ADD + * #define BLE_MESH_UUID_MESH_PROXY_DATA_OUT_VAL 0x2ADE + */ +#define BLE_MESH_UUID_PERCEIVED_LIGHTNESS_VAL 0x2B03 +#define BLE_MESH_UUID_PERCENTAGE_8_VAL 0x2B04 +#define BLE_MESH_UUID_POWER_VAL 0x2B05 +#define BLE_MESH_UUID_POWER_SPECIFICATION_VAL 0x2B06 +#define BLE_MESH_UUID_RELATIVE_RUNTIME_IN_A_CURRENT_RANGE_VAL 0x2B07 +#define BLE_MESH_UUID_RELATIVE_RUNTIME_IN_A_GENERIC_LEVEL_RANGE_VAL 0x2B08 +#define BLE_MESH_UUID_RELATIVE_VALUE_IN_A_PERIOD_OF_DAY_VAL 0x2B0B +#define BLE_MESH_UUID_RELATIVE_VALUE_IN_A_TEMPERATURE_RANGE_VAL 0x2B0C +#define BLE_MESH_UUID_RELATIVE_VALUE_IN_A_VOLTAGE_RANGE_VAL 0x2B09 +#define BLE_MESH_UUID_RELATIVE_VALUE_IN_AN_ILLUMINANCE_RANGE_VAL 0x2B0A +#define BLE_MESH_UUID_TEMPERATURE_8_VAL 0x2B0D +#define BLE_MESH_UUID_TEMPERATURE_8_IN_A_PERIOD_OF_DAY_VAL 0x2B0E +#define BLE_MESH_UUID_TEMPERATURE_8_STATISTICS_VAL 0x2B0F +#define BLE_MESH_UUID_TEMPERATURE_RANGE_VAL 0x2B10 +#define BLE_MESH_UUID_TEMPERATURE_STATISTICS_VAL 0x2B11 +#define BLE_MESH_UUID_TIME_DECIHOUR_8_VAL 0x2B12 +#define BLE_MESH_UUID_TIME_EXPONENTIAL_8_VAL 0x2B13 +#define BLE_MESH_UUID_TIME_HOUR_24_VAL 0x2B14 +#define BLE_MESH_UUID_TIME_MILLISECOND_24_VAL 0x2B15 +#define BLE_MESH_UUID_TIME_SECOND_16_VAL 0x2B16 +#define BLE_MESH_UUID_TIME_SECOND_8_VAL 0x2B17 +#define BLE_MESH_UUID_VOLTAGE_VAL 0x2B18 +#define BLE_MESH_UUID_VOLTAGE_SPECIFICATION_VAL 0x2B19 +#define BLE_MESH_UUID_VOLTAGE_STATISTICS_VAL 0x2B1A +#define BLE_MESH_UUID_VOLUME_FLOW_VAL 0x2B1B + +/** + * @brief BLE Mesh Device Property referenced Characteristic Type Definitions + */ + +/* Unit is in degrees Celsius with a resolution of 0.01 degrees Celsius. */ +typedef s16_t bt_mesh_temperature_t; + +typedef u16_t bt_mesh_gap_appearance_t; + +/* Mesh Characteristics Type Definitions */ + +/* This characteristic represents an electric current. + * Note: Unit is ampere with a resolution of 0.01. + * Minimum value: 0, maximum value: 655.34; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_electric_current_t; + +/* The Time Exponential 8 characteristic is used to represent a measure of period of + * time in seconds. + * Note: The time duration is given by the value 1.1^(N-64) in seconds, with N being + * the raw 8-bit value; + * Minimum value: 0.0, maximum value: 73216705; + * A raw value of 0x00 represents 0 seconds, and a raw value of 0xFF represents + * the total life of the device. + */ +typedef u8_t bt_mesh_time_exponential_8_t; + +/* The Voltage characteristic is used to represent a measure of positive electric + * potential difference in units of volts. + * Note: Unit is volt with a resolution of 1/64V; + * Minimum value: 0.0, maximum value: 1022.0; + * A value of 0xFFFF represents 'value is not known'. The minimum representable + * value represents the minimum value or lower, the maximum representable value + * represents the maximum value or higher. + */ +typedef u16_t bt_mesh_voltage_t; + +/* This characteristic aggregates the Electric Current characteristic and instance of + * the Time Exponential 8 characteristic. + */ +typedef struct __packed average_current { + bt_mesh_electric_current_t electric_current; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_average_current_t; + +/* This characteristic aggregates the Voltage characteristic and instance of the Time + * Exponential 8 characteristic. + */ +typedef struct __packed average_voltage { + bt_mesh_voltage_t voltage; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_average_voltage_t; + +/* The Boolean characteristic defines the predefined Boolean values as an enumeration. + * Key | Value + * 0 | False + * 1 | True + * 2 to 255 | Prohibited + */ +typedef u8_t bt_mesh_boolean_t; + +/* The Chromatic Distance From Planckian characteristic represents a distance of a + * chromaticity coordinate from the Planckian locus in the (u',2/3 v') diagram as + * defined by ANSI standard C78.377-2008. The distance is positive if the chromaticity + * coordinate is located above the Planckian locus (i.e. has as higher y value than the + * Planckian), and negative if it is located below. The distance is only valid within + * the range from -0.05 to 0.05. + * Note: Unit is unitless with a resolution of 0.00001; + * Minimum value: -0.05, maximum value: 0.05; + * A value of 0xFFFF represents 'value is not known'; + * A value of 0xFFFE represents 'value is not valid'. + */ +typedef s16_t bt_mesh_chromatic_distance_from_planckian_t; + +/* This characteristic represents a chromaticity coordinate in a color diagram such as + * the CIE1931 diagram. It can represent an x or y coordinate. + * Note: Unit is unitless with a resolution of 1/65535; + * Minimum value: 0, maximum value: 1.0. + */ +typedef u16_t bt_mesh_chromaticity_coordinate_t; + +/* This characteristic represents a chromaticity coordinate as a tuple with an x and + * y coordinate. + */ +typedef struct __packed chromaticity_coordinates { + bt_mesh_chromaticity_coordinate_t chromaticity_x_coordinate; + bt_mesh_chromaticity_coordinate_t chromaticity_y_coordinate; +} bt_mesh_chromaticity_coordinates_t; + +/* The Correlated Color Temperature characteristic is used to represent correlated color + * temperature in a range from 800 to 65534 Kelvin with a resolution of 1 Kelvin. + * Note: Unit is Kelvin with a resolution of 1; + * Minimum value: 800, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_correlated_color_temperature_t; + +/* The Chromaticity In CCT And Duv Values characteristic is a composite characteristic + * consisting of the Correlated Color Temperature characteristic and the Chromatic + * Distance From Planckian characteristic. + */ +typedef struct __packed chromaticity_in_cct_and_duv_values { + bt_mesh_correlated_color_temperature_t correlated_color_temperature; + bt_mesh_chromatic_distance_from_planckian_t chromaticity_distance_from_planckian; +} bt_mesh_chromaticity_in_cct_and_duv_values_t; + +/* The Chromaticity Tolerance characteristic is a tolerance of a tuple of chromaticity + * values represented as a value of a radius of a circle in the CIE 1976 (u',v') diagram; + * value corresponding to the 3-sigma values of the expected chromaticity deviations. + * Note: Unit is unitless with a resolution of 0.0001; + * Minimum value: 0, maximum value: 0.0255. + */ +typedef u8_t bt_mesh_chromaticity_tolerance_t; + +/* The CIE 13.3-1995 Color Rendering Index characteristic is a color rendition index value + * for a color patch as calculated in accordance with the CIE 13.3-1995 standard. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: -128, maximum value: 100. + */ +typedef s8_t bt_mesh_cie_13_3_1995_color_rendering_index_t; + +/* The Coefficient characteristic is used to represent a general coefficient value. */ +typedef float bt_mesh_coefficient_t; + +/* The Count 16 characteristic is used to represent a general count value. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: 0, maximum value 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_count_16_t; + +/* The Count 24 characteristic is used to represent a general count value. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: 0, maximum value 16777214; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_count_24_t[3]; + +/* This characteristic represents a country or dependent areas in accordance with + * the ISO 3166-1 Numeric standard. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: 0, maximum value: 4095; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_country_code_t; + +/* Date as days elapsed since the Epoch (Jan 1, 1970) in the Coordinated Universal + * Time (UTC) time zone. + * Note: Unit is a day with a resolution of 1; + * Minimum value: 1, maximum value: 16777214; + * A value of 0x000000 represents 'value is not known'. + */ +typedef u8_t bt_mesh_date_utc_t[3]; + +/* This characteristic aggregates two instances of the Electric Current characteristic + * to represent a range of Electric Current values. + */ +typedef struct __packed electric_current_range { + bt_mesh_electric_current_t minimum_electric_current_value; + bt_mesh_electric_current_t maximum_electric_current_value; +} bt_mesh_electric_current_range_t; + +/* This characteristic aggregates three instances of the Electric Current characteristic + * to represent a specification of electric current values. + */ +typedef struct __packed electric_current_specification { + bt_mesh_electric_current_t minimum_electric_current_value; + bt_mesh_electric_current_t typical_electric_current_value; + bt_mesh_electric_current_t maximum_electric_current_value; +} bt_mesh_electric_current_specification_t; + +/* This characteristic aggregates four instances of the Electric Current characteristic + * with a Sensing Duration to represent a set of statistical electric current values. + */ +typedef struct __packed electric_current_statistics { + bt_mesh_electric_current_t average_electric_current_value; + bt_mesh_electric_current_t standard_electric_current_value; + bt_mesh_electric_current_t minimum_electric_current_value; + bt_mesh_electric_current_t maximum_electric_current_value; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_electric_current_statistics_t; + +/* The Energy characteristic is used to represent a measure of energy in units of + * kilowatt hours. + * Note: Unit is kilowatt-hour with a resolution of 1; + * Minimum value: 0, maximum value: 16777214; + * A value of 0xFFFFFF represents ‘value is not known’. + */ +typedef u8_t bt_mesh_energy_t[3]; + +/* The Time Decihour 8 characteristic is used to represent a period of time in + * tenths of an hour. + * Note: Unit is hour with a resolution of 0.1; + * Minimum value: 0.0, maximum value: 24.0; + * A value of 0xFF represents 'value is not known'. All other values are Prohibited. + */ +typedef u8_t bt_mesh_time_decihour_8_t; + +/* This characteristic aggregates the Energy characteristic, and two instances of + * the Time Decihour 8 characteristic, to represent energy use in a period of day. + */ +typedef struct __packed energy_in_a_period_of_day { + bt_mesh_energy_t energy_value; + bt_mesh_time_decihour_8_t start_time; + bt_mesh_time_decihour_8_t end_time; +} bt_mesh_energy_in_a_period_of_day_t; + +/* The Time Second 16 characteristic is used to represent a period of time with a + * unit of 1 second. + * Note: Unit is second with a resolution of 1; + * Minimum value: 0, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_time_second_16_t; + +/* This characteristic aggregates the Count 16 characteristic, two instances of the + * Time Decihour 8 characteristic and an instance of the Sensing Duration characteristic, + * to represent statistical values of events. + */ +typedef struct __packed event_statistics { + bt_mesh_count_16_t number_of_events; + bt_mesh_time_second_16_t average_event_duration; + bt_mesh_time_exponential_8_t time_elapsed_since_last_event; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_event_statistics_t; + +/* The Fixed String 16 characteristic represents a 16-octet UTF-8 string. */ +typedef char bt_mesh_fixed_string_16_t[16]; + +/* The Fixed String 24 characteristic represents a 24-octet UTF-8 string. */ +typedef char bt_mesh_fixed_string_24_t[24]; + +/* The Fixed String 36 characteristic represents a 36-octet UTF-8 string. */ +typedef char bt_mesh_fixed_string_36_t[36]; + +/* The Fixed String 8 characteristic represents an 8-octet UTF-8 string. */ +typedef char bt_mesh_fixed_string_8_t[8]; + +/* The Generic Level characteristic represents a general level value of a + * setting of a device. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: 0, maximum value: 65535. + */ +typedef u16_t bt_mesh_generic_level_t; + +/* The Global Trade Item Number characteristic represents an identifier as + * issued by GS1 General Specifications, which may consist up to 14 digits, + * and is here represented as a 48-bit unsigned integer. + */ +typedef u8_t bt_mesh_global_trade_item_number_t[6]; + +/* The Illuminance characteristic is used to represent a measure of illuminance + * in units of lux. + * Note: Unit is lux with a resolution of 0.01; + * Minimum value: 0, maximum value: 167772.14; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_illuminance_t[3]; + +/* The Luminous Efficacy characteristic is used to represent a measure of luminous + * efficacy in units of lumen per watt. + * Note: Unit is lumen per watt with a resolution of 0.1; + * Minimum value: 0, maximum value: 1800; + * A value of 0xFFFF represents 'value is not known'. All other values are Prohibited. + */ +typedef u16_t bt_mesh_luminous_efficacy_t; + +/* The Luminous Energy characteristic is used to represent a measure of luminous + * energy in units of lumen hour. + * Note: Unit is lumen hour with a resolution of 1000; + * Minimum value: 0, maximum value: 16777214000; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_luminous_energy_t[3]; + +/* The Luminous Exposure characteristic is used to represent a measure of luminous + * exposure in units of lux-hour. + * Note: Unit is lux hour with a resolution of 1000; + * Minimum value: 0, maximum value: 16777214000; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_luminous_exposure_t[3]; + +/* The Luminous Flux characteristic is used to represent a measure of luminous flux + * in units of lumen. + * Note: Unit is lumen with a resolution of 1; + * Minimum value: 0, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_luminous_flux_t; + +/* This characteristic aggregates two instances of the Luminous Flux characteristic + * to represent a luminous flux range. + */ +typedef struct __packed luminous_flux_range { + bt_mesh_luminous_flux_t minimum_luminous_flux; + bt_mesh_luminous_flux_t maximum_luminous_flux; +} bt_mesh_luminous_flux_range_t; + +/* The Luminous Intensity characteristic is used to represent a luminous intensity of + * a beam of light in units of candela. + * Note: Unit is candela with a resolution of 1; + * Minimum value: 0, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_luminous_intensity_t; + +/* The Mass Flow characteristic is used to represent a flow of mass. + * Note: Unit is gram/second with a resolution of 1; + * Minimum value: 0, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_mass_flow_t; + +/* The Mesh Provisioning Data In characteristic can be written to send a Proxy PDU + * message containing Provisioning PDU to the Provisioning Server. + */ +struct mesh_provisioning_data_in { + +}; + +/* The Mesh Provisioning Data Out characteristic can be notified to send a Proxy PDU + * message containing Provisioning PDU from a Provisioning Server to a Provisioning Client. + */ +struct mesh_provisioning_data_out { + +}; + +/* The Mesh Proxy Data In characteristic is used by the client to send Proxy PDUs to + * the server. + */ +struct mesh_proxy_data_in { + +}; + +/* The Mesh Proxy Data Out characteristic is used by the server to send Proxy PDUs to + * the client. + */ +struct mesh_proxy_data_out { + +}; + +/* The Perceived Lightness characteristic is used to represent the perceived lightness + * of a light. + * Note: Unit is unitless with a resolution of 1; + * Minimum value: 0, maximum value: 65535. + */ +typedef u16_t bt_mesh_perceived_lightness_t; + +/* The Percentage 8 characteristic is used to represent a measure of percentage. + * Note: Unit is a percentage with a resolution of 0.5; + * Minimum value: 0, maximum value: 100; + * A value of 0xFF represents 'value is not known'. All other values are Prohibited. + */ +typedef u8_t bt_mesh_percentage_8_t; + +/* The Power characteristic is used to represent a measure of power in units of watts. + * Note: Unit is watt with a resolution of 0.1; + * Minimum value: 0, maximum value: 1677721.4; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_power_t[3]; + +/* This characteristic aggregates three instances of the Power characteristic to + * represent a specification of Power values. + */ +typedef struct __packed power_specification { + bt_mesh_power_t minimum_power_value; + bt_mesh_power_t typical_power_value; + bt_mesh_power_t maximum_power_value; +} bt_mesh_power_specification_t; + +/* This characteristic aggregates the Percentage 8 characteristic and two instances of + * the Electric Current characteristic to represent a relative value in an electric + * current range. + */ +typedef struct __packed relative_runtime_in_a_current_range { + bt_mesh_percentage_8_t relative_runtime_value; + bt_mesh_electric_current_t minimum_current; + bt_mesh_electric_current_t maximum_current; +} bt_mesh_relative_runtime_in_a_current_range_t; + +/* This characteristic aggregates the Percentage 8 characteristic and two instances of + * the Generic Level characteristic to represent a runtime in a generic level range. + */ +typedef struct __packed relative_runtime_in_a_generic_level_range { + bt_mesh_percentage_8_t relative_value; + bt_mesh_generic_level_t minimum_generic_level; + bt_mesh_generic_level_t maximum_generic_level; +} bt_mesh_relative_runtime_in_a_generic_level_range_t; + +/* This characteristic aggregates the Percentage 8 characteristic, and two instances of + * the Time Decihour 8 characteristic. + */ +typedef struct __packed relative_value_in_a_period_of_day { + bt_mesh_percentage_8_t relative_value; + bt_mesh_time_decihour_8_t start_time; + bt_mesh_time_decihour_8_t end_time; +} bt_mesh_relative_value_in_a_period_of_day_t; + +/* This characteristic aggregates the Percentage 8 characteristic, and two instances of + * the Temperature characteristic. + */ +typedef struct __packed relative_value_in_a_temperature_range { + bt_mesh_percentage_8_t relative_value; + bt_mesh_temperature_t minimum_temperature_value; + bt_mesh_temperature_t maximum_temperature_value; +} bt_mesh_relative_value_in_a_temperature_range_t; + +/* This characteristic aggregates the Percentage 8 characteristic and two instances of + * the Voltage characteristic to represent a relative value in a voltage range. + */ +typedef struct __packed relative_value_in_a_voltage_range { + bt_mesh_percentage_8_t relative_value; + bt_mesh_voltage_t minimum_voltage; + bt_mesh_voltage_t maximum_voltage; +} bt_mesh_relative_value_in_a_voltage_range_t; + +/* This characteristic aggregates the Percentage 8 characteristic and two instances of + * the Illuminance characteristic to represent a relative value in a illuminance range. + */ +typedef struct __packed relative_value_in_an_illuminance_range { + bt_mesh_percentage_8_t relative_value; + bt_mesh_illuminance_t minimum_illuminance; + bt_mesh_illuminance_t maximum_illuminance; +} bt_mesh_relative_value_in_an_illuminance_range_t; + +/* The Temperature 8 characteristic is used to represent a measure of temperature with + * a unit of 0.5 degree Celsius. + * Note: Unit is degree Celsius with a resolution of 0.5; + * Minimum value: -64.0, maximum value: 63.5; + * A value of 0xFF represents 'value is not known'. + */ +typedef s8_t bt_mesh_temperature_8_t; + +/* This characteristic aggregates the Temperature 8 characteristic, and two instances + * of the Time Decihour 8 characteristic, to represent a temperature value in a period + * of day. + */ +typedef struct __packed temperature_8_in_a_period_of_day { + bt_mesh_temperature_8_t temperature; + bt_mesh_time_decihour_8_t start_time; + bt_mesh_time_decihour_8_t end_time; +} bt_mesh_temperature_8_in_a_period_of_day_t; + +/* This characteristic aggregates four instances of the Temperature 8 characteristic, + * and one instance of the Time Exponential 8 characteristic. + */ +typedef struct __packed temperature_8_statistics { + bt_mesh_temperature_8_t average; + bt_mesh_temperature_8_t standard_deviation_value; + bt_mesh_temperature_8_t minimum_value; + bt_mesh_temperature_8_t maximum_value; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_temperature_8_statistics_t; + +/* This characteristic aggregates two instances of the Temperature characteristic to + * represent a temperature range. + */ +typedef struct __packed temperature_range { + bt_mesh_temperature_t minimum_temperature; + bt_mesh_temperature_t maximum_temperature; +} bt_mesh_temperature_range_t; + +/* This characteristic aggregates four instances of the Temperature characteristic, + * and one instance of the Time Exponential 8 characteristic. + */ +typedef struct __packed temperature_statistics { + bt_mesh_temperature_t average_temperature; + bt_mesh_temperature_t standard_deviation_temperature; + bt_mesh_temperature_t minimum_temperature; + bt_mesh_temperature_t maximum_temperature; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_temperature_statistics_t; + +/* The Time Hour 24 characteristic is used to represent a period of time in hours. + * Note: Unit is hour with a resolution of 1; + * Minimum value: 0, maximum value: 16777214; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_time_hour_24_t[3]; + +/* The Time Millisecond 24 characteristic is used to represent a period of time with + * a resolution of 1 millisecond. + * Note: Unit is second with a resolution of 0.001; + * Minimum value: 0, maximum value: 16777.214; + * A value of 0xFFFFFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_time_millisecond_24_t[3]; + +/* The Time Second 8 characteristic is used to represent a period of time with a unit + * of 1 second. + * Note: Unit is second with a resolution of 1; + * Minimum value: 0, maximum value: 254; + * A value of 0xFF represents 'value is not known'. + */ +typedef u8_t bt_mesh_time_second_8_t; + +/* This characteristic aggregates three instances of the Voltage characteristic to + * represent a specification of voltage values. + */ +typedef struct __packed voltage_specification { + bt_mesh_voltage_t minimum_voltage_value; + bt_mesh_voltage_t typical_voltage_value; + bt_mesh_voltage_t maximum_voltage_value; +} bt_mesh_voltage_specification_t; + +/* This characteristic aggregates four instances of the Voltage characteristic and an + * instance of the Time Exponential 8 characteristic to represent a set of statistical + * voltage values over a period of time. + */ +typedef struct __packed voltage_statistics { + bt_mesh_voltage_t average_voltage_value; + bt_mesh_voltage_t standard_deviation_voltage_value; + bt_mesh_voltage_t minimum_voltage_value; + bt_mesh_voltage_t maximum_voltage_value; + bt_mesh_time_exponential_8_t sensing_duration; +} bt_mesh_voltage_statistics_t; + +/* The Volume Flow characteristic is used to represent a flow of a general volume such + * as a volume of material or gas. + * Note: Unit is liter/second with a resolution of 0.001 (1 milliliter); + * Minimum value: 0, maximum value: 65534; + * A value of 0xFFFF represents 'value is not known'. + */ +typedef u16_t bt_mesh_volume_flow_t; + +/* Mesh Device Property related function */ + +u8_t bt_mesh_get_dev_prop_len(u16_t prop_id); + +#ifdef __cplusplus +} +#endif + +#endif /* _DEVICE_PROPERTY_H_ */ \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h new file mode 100644 index 000000000..241f1112a --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/generic_server.h @@ -0,0 +1,397 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _GENERIC_SERVER_H_ +#define _GENERIC_SERVER_H_ + +#include "server_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_mesh_gen_onoff_state { + u8_t onoff; + u8_t target_onoff; +}; + +struct bt_mesh_gen_onoff_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_onoff_state state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; +}; + +struct bt_mesh_gen_level_state { + s16_t level; + s16_t target_level; + + s16_t last_level; + s32_t last_delta; + + bool move_start; + bool positive; +}; + +struct bt_mesh_gen_level_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_level_state state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_level; +}; + +struct bt_mesh_gen_def_trans_time_state { + u8_t trans_time; +}; + +struct bt_mesh_gen_def_trans_time_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_def_trans_time_state state; +}; + +struct bt_mesh_gen_onpowerup_state { + u8_t onpowerup; +}; + +struct bt_mesh_gen_power_onoff_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_onpowerup_state *state; +}; + +struct bt_mesh_gen_power_onoff_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_onpowerup_state *state; +}; + +struct bt_mesh_gen_power_level_state { + u16_t power_actual; + u16_t target_power_actual; + + u16_t power_last; + u16_t power_default; + + u8_t status_code; + u16_t power_range_min; + u16_t power_range_max; +}; + +struct bt_mesh_gen_power_level_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_power_level_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_level; +}; + +struct bt_mesh_gen_power_level_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_power_level_state *state; +}; + +struct bt_mesh_gen_battery_state { + u32_t battery_level : 8, + time_to_discharge : 24; + u32_t time_to_charge : 24, + battery_flags : 8; +}; + +struct bt_mesh_gen_battery_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_battery_state state; +}; + +struct bt_mesh_gen_location_state { + s32_t global_latitude; + s32_t global_longitude; + s16_t global_altitude; + s16_t local_north; + s16_t local_east; + s16_t local_altitude; + u8_t floor_number; + u16_t uncertainty; +}; + +struct bt_mesh_gen_location_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_location_state *state; +}; + +struct bt_mesh_gen_location_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_gen_location_state *state; +}; + +/** + * According to the hierarchy of Generic Property states (Model Spec section 3.1.8), + * the Manufacturer Properties and Admin Properties may contain multiple Property + * states. User Properties just a collection of which can be accessed. + * + * property_count: Number of the properties contained in the table + * properties: Table of the properties + * + * These variables need to be initialized in the application layer, the precise + * number of the properties should be set and memories used to store the property + * values should be allocated. + */ + +enum bt_mesh_gen_user_prop_access { + USER_ACCESS_PROHIBIT, + USER_ACCESS_READ, + USER_ACCESS_WRITE, + USER_ACCESS_READ_WRITE, +}; + +enum bt_mesh_gen_admin_prop_access { + ADMIN_NOT_USER_PROP, + ADMIN_ACCESS_READ, + ADMIN_ACCESS_WRITE, + ADMIN_ACCESS_READ_WRITE, +}; + +enum bt_mesh_gen_manu_prop_access { + MANU_NOT_USER_PROP, + MANU_ACCESS_READ, +}; + +struct bt_mesh_generic_property { + u16_t id; + u8_t user_access; + u8_t admin_access; + u8_t manu_access; + struct net_buf_simple *val; +}; + +struct bt_mesh_gen_user_prop_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + u8_t property_count; + struct bt_mesh_generic_property *properties; +}; + +struct bt_mesh_gen_admin_prop_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + u8_t property_count; + struct bt_mesh_generic_property *properties; +}; + +struct bt_mesh_gen_manu_prop_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + u8_t property_count; + struct bt_mesh_generic_property *properties; +}; + +struct bt_mesh_gen_client_prop_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + u8_t id_count; + u16_t *property_ids; +}; + +typedef union { + struct { + u8_t onoff; + } gen_onoff_set; + struct { + s16_t level; + } gen_level_set; + struct { + s16_t level; + } gen_delta_set; + struct { + s16_t level; + } gen_move_set; + struct { + u8_t trans_time; + } gen_def_trans_time_set; + struct { + u8_t onpowerup; + } gen_onpowerup_set; + struct { + u16_t power; + } gen_power_level_set; + struct { + u16_t power; + } gen_power_default_set; + struct { + u16_t range_min; + u16_t range_max; + } gen_power_range_set; + struct { + s32_t latitude; + s32_t longitude; + s16_t altitude; + } gen_loc_global_set; + struct { + s16_t north; + s16_t east; + s16_t altitude; + u8_t floor_number; + u16_t uncertainty; + } gen_loc_local_set; + struct { + u16_t id; + struct net_buf_simple *value; + } gen_user_prop_set; + struct { + u16_t id; + u8_t access; + struct net_buf_simple *value; + } gen_admin_prop_set; + struct { + u16_t id; + u8_t access; + } gen_manu_prop_set; +} bt_mesh_gen_server_state_change_t; + +typedef union { + struct { + u16_t id; + } user_property_get; + struct { + u16_t id; + } admin_property_get; + struct { + u16_t id; + } manu_property_get; + struct { + u16_t id; + } client_properties_get; +} bt_mesh_gen_server_recv_get_msg_t; + +typedef union { + struct { + bool op_en; + u8_t onoff; + u8_t tid; + u8_t trans_time; + u8_t delay; + } onoff_set; + struct { + bool op_en; + s16_t level; + u8_t tid; + u8_t trans_time; + u8_t delay; + } level_set; + struct { + bool op_en; + s32_t delta_level; + u8_t tid; + u8_t trans_time; + u8_t delay; + } delta_set; + struct { + bool op_en; + s16_t delta_level; + u8_t tid; + u8_t trans_time; + u8_t delay; + } move_set; + struct { + u8_t trans_time; + } def_trans_time_set; + struct { + u8_t onpowerup; + } onpowerup_set; + struct { + bool op_en; + u16_t power; + u8_t tid; + u8_t trans_time; + u8_t delay; + } power_level_set; + struct { + u16_t power; + } power_default_set; + struct { + u16_t range_min; + u16_t range_max; + } power_range_set; + struct { + s32_t latitude; + s32_t longitude; + s16_t altitude; + } loc_global_set; + struct { + s16_t north; + s16_t east; + s16_t altitude; + u8_t floor_number; + u16_t uncertainty; + } loc_local_set; + struct { + u16_t id; + struct net_buf_simple *value; + } user_property_set; + struct { + u16_t id; + u8_t access; + struct net_buf_simple *value; + } admin_property_set; + struct { + u16_t id; + u8_t access; + } manu_property_set; +} bt_mesh_gen_server_recv_set_msg_t; + +void bt_mesh_generic_server_lock(void); +void bt_mesh_generic_server_unlock(void); + +void gen_onoff_publish(struct bt_mesh_model *model); +void gen_level_publish(struct bt_mesh_model *model); +void gen_onpowerup_publish(struct bt_mesh_model *model); +void gen_power_level_publish(struct bt_mesh_model *model, u16_t opcode); + +int bt_mesh_gen_onoff_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_level_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_def_trans_time_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_battery_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_user_prop_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_admin_prop_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_manu_prop_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_client_prop_srv_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_gen_onoff_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_level_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_def_trans_time_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_power_level_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_battery_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_location_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_user_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_admin_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_manu_prop_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_gen_client_prop_srv_deinit(struct bt_mesh_model *model, bool primary); + +#ifdef __cplusplus +} +#endif + +#endif /* _GENERIC_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h new file mode 100644 index 000000000..34195dd0d --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/lighting_server.h @@ -0,0 +1,538 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _LIGHTING_SERVER_H_ +#define _LIGHTING_SERVER_H_ + +#include "server_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct bt_mesh_light_lightness_state { + u16_t lightness_linear; + u16_t target_lightness_linear; + + u16_t lightness_actual; + u16_t target_lightness_actual; + + u16_t lightness_last; + u16_t lightness_default; + + u8_t status_code; + u16_t lightness_range_min; + u16_t lightness_range_max; +}; + +struct bt_mesh_light_lightness_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_lightness_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition actual_transition; + struct bt_mesh_state_transition linear_transition; + s32_t tt_delta_lightness_actual; + s32_t tt_delta_lightness_linear; +}; + +struct bt_mesh_light_lightness_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_lightness_state *state; +}; + +struct bt_mesh_light_ctl_state { + u16_t lightness; + u16_t target_lightness; + + u16_t temperature; + u16_t target_temperature; + + s16_t delta_uv; + s16_t target_delta_uv; + + u8_t status_code; + u16_t temperature_range_min; + u16_t temperature_range_max; + + u16_t lightness_default; + u16_t temperature_default; + s16_t delta_uv_default; +}; + +struct bt_mesh_light_ctl_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_ctl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_lightness; + s32_t tt_delta_temperature; + s32_t tt_delta_delta_uv; +}; + +struct bt_mesh_light_ctl_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_ctl_state *state; +}; + +struct bt_mesh_light_ctl_temp_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_ctl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_temperature; + s32_t tt_delta_delta_uv; +}; + +struct bt_mesh_light_hsl_state { + u16_t lightness; + u16_t target_lightness; + + u16_t hue; + u16_t target_hue; + + u16_t saturation; + u16_t target_saturation; + + u16_t lightness_default; + u16_t hue_default; + u16_t saturation_default; + + u8_t status_code; + u16_t hue_range_min; + u16_t hue_range_max; + u16_t saturation_range_min; + u16_t saturation_range_max; +}; + +struct bt_mesh_light_hsl_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_hsl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_lightness; + s32_t tt_delta_hue; + s32_t tt_delta_saturation; +}; + +struct bt_mesh_light_hsl_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_hsl_state *state; +}; + +struct bt_mesh_light_hsl_hue_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_hsl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_hue; +}; + +struct bt_mesh_light_hsl_sat_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_hsl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_saturation; +}; + +struct bt_mesh_light_xyl_state { + u16_t lightness; + u16_t target_lightness; + + u16_t x; + u16_t target_x; + + u16_t y; + u16_t target_y; + + u16_t lightness_default; + u16_t x_default; + u16_t y_default; + + u8_t status_code; + u16_t x_range_min; + u16_t x_range_max; + u16_t y_range_min; + u16_t y_range_max; +}; + +struct bt_mesh_light_xyl_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_xyl_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; + s32_t tt_delta_lightness; + s32_t tt_delta_x; + s32_t tt_delta_y; +}; + +struct bt_mesh_light_xyl_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_xyl_state *state; +}; + +struct bt_mesh_light_lc_state { + u32_t mode : 1, /* default 0 */ + occupancy_mode : 1, /* default 1 */ + light_onoff : 1, + target_light_onoff : 1, + occupancy : 1, + ambient_luxlevel : 24; /* 0x000000 ~ 0xFFFFFF */ + + u16_t linear_output; /* 0x0000 ~ 0xFFFF */ +}; + +struct bt_mesh_light_lc_property_state { + u32_t time_occupancy_delay; /* 0x003A */ + u32_t time_fade_on; /* 0x0037 */ + u32_t time_run_on; /* 0x003C */ + u32_t time_fade; /* 0x0036 */ + u32_t time_prolong; /* 0x003B */ + u32_t time_fade_standby_auto; /* 0x0038 */ + u32_t time_fade_standby_manual; /* 0x0039 */ + + u16_t lightness_on; /* 0x002E */ + u16_t lightness_prolong; /* 0x002F */ + u16_t lightness_standby; /* 0x0030 */ + + u16_t ambient_luxlevel_on; /* 0x002B, 0x0000 ~ 0xFFFF */ + u16_t ambient_luxlevel_prolong; /* 0x002C, 0x0000 ~ 0xFFFF */ + u16_t ambient_luxlevel_standby; /* 0x002D, 0x0000 ~ 0xFFFF */ + + float regulator_kiu; /* 0x0033, 0.0 ~ 1000.0, default 250.0 */ + float regulator_kid; /* 0x0032, 0.0 ~ 1000.0, default 25.0 */ + float regulator_kpu; /* 0x0035, 0.0 ~ 1000.0, default 80.0 */ + float regulator_kpd; /* 0x0034, 0.0 ~ 1000.0, default 80.0 */ + s8_t regulator_accuracy; /* 0x0031, 0.0 ~ 100.0, default 2.0 */ + + u32_t set_occupancy_to_1_delay; +}; + +typedef enum { + LC_OFF, + LC_STANDBY, + LC_FADE_ON, + LC_RUN, + LC_FADE, + LC_PROLONG, + LC_FADE_STANDBY_AUTO, + LC_FADE_STANDBY_MANUAL, +} bt_mesh_lc_state; + +struct bt_mesh_light_lc_state_machine { + struct { + u8_t fade_on; + u8_t fade; + u8_t fade_standby_auto; + u8_t fade_standby_manual; + } trans_time; + bt_mesh_lc_state state; + struct k_delayed_work timer; +}; + +struct bt_mesh_light_control { + struct bt_mesh_light_lc_state state; + struct bt_mesh_light_lc_property_state prop_state; + struct bt_mesh_light_lc_state_machine state_machine; +}; + +struct bt_mesh_light_lc_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_control *lc; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; +}; + +struct bt_mesh_light_lc_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_light_control *lc; +}; + +typedef union { + struct { + u16_t lightness; + } lightness_set; + struct { + u16_t lightness; + } lightness_linear_set; + struct { + u16_t lightness; + } lightness_default_set; + struct { + u16_t range_min; + u16_t range_max; + } lightness_range_set; + struct { + u16_t lightness; + u16_t temperature; + s16_t delta_uv; + } ctl_set; + struct { + u16_t temperature; + s16_t delta_uv; + } ctl_temp_set; + struct { + u16_t range_min; + u16_t range_max; + } ctl_temp_range_set; + struct { + u16_t lightness; + u16_t temperature; + s16_t delta_uv; + } ctl_default_set; + struct { + u16_t lightness; + u16_t hue; + u16_t saturation; + } hsl_set; + struct { + u16_t hue; + } hsl_hue_set; + struct { + u16_t saturation; + } hsl_saturation_set; + struct { + u16_t lightness; + u16_t hue; + u16_t saturation; + } hsl_default_set; + struct { + u16_t hue_range_min; + u16_t hue_range_max; + u16_t sat_range_min; + u16_t sat_range_max; + } hsl_range_set; + struct { + u16_t lightness; + u16_t x; + u16_t y; + } xyl_set; + struct { + u16_t lightness; + u16_t x; + u16_t y; + } xyl_default_set; + struct { + u16_t x_range_min; + u16_t x_range_max; + u16_t y_range_min; + u16_t y_range_max; + } xyl_range_set; + struct { + u8_t mode; + } lc_mode_set; + struct { + u8_t mode; + } lc_om_set; + struct { + u8_t onoff; + } lc_light_onoff_set; + struct { + u16_t id; + struct net_buf_simple *value; + } lc_property_set; + struct { + u16_t property_id; + union { + u8_t occupancy; + u32_t set_occupancy_to_1_delay; + u32_t ambient_luxlevel; + } state; + } sensor_status; +} bt_mesh_light_server_state_change_t; + +typedef union { + struct { + u16_t id; + } lc_property_get; +} bt_mesh_light_server_recv_get_msg_t; + +typedef union { + struct { + bool op_en; + u16_t lightness; + u8_t tid; + u8_t trans_time; + u8_t delay; + } lightness_set; + struct { + bool op_en; + u16_t lightness; + u8_t tid; + u8_t trans_time; + u8_t delay; + } lightness_linear_set; + struct { + u16_t lightness; + } lightness_default_set; + struct { + u16_t range_min; + u16_t range_max; + } lightness_range_set; + struct { + bool op_en; + u16_t lightness; + u16_t temperature; + s16_t delta_uv; + u8_t tid; + u8_t trans_time; + u8_t delay; + } ctl_set; + struct { + bool op_en; + u16_t temperature; + s16_t delta_uv; + u8_t tid; + u8_t trans_time; + u8_t delay; + } ctl_temp_set; + struct { + u16_t range_min; + u16_t range_max; + } ctl_temp_range_set; + struct { + u16_t lightness; + u16_t temperature; + s16_t delta_uv; + } ctl_default_set; + struct { + bool op_en; + u16_t lightness; + u16_t hue; + u16_t saturation; + u8_t tid; + u8_t trans_time; + u8_t delay; + } hsl_set; + struct { + bool op_en; + u16_t hue; + u8_t tid; + u8_t trans_time; + u8_t delay; + } hsl_hue_set; + struct { + bool op_en; + u16_t saturation; + u8_t tid; + u8_t trans_time; + u8_t delay; + } hsl_saturation_set; + struct { + u16_t lightness; + u16_t hue; + u16_t saturation; + } hsl_default_set; + struct { + u16_t hue_range_min; + u16_t hue_range_max; + u16_t sat_range_min; + u16_t sat_range_max; + } hsl_range_set; + struct { + bool op_en; + u16_t lightness; + u16_t x; + u16_t y; + u8_t tid; + u8_t trans_time; + u8_t delay; + } xyl_set; + struct { + u16_t lightness; + u16_t x; + u16_t y; + } xyl_default_set; + struct { + u16_t x_range_min; + u16_t x_range_max; + u16_t y_range_min; + u16_t y_range_max; + } xyl_range_set; + struct { + u8_t mode; + } lc_mode_set; + struct { + u8_t mode; + } lc_om_set; + struct { + bool op_en; + u8_t light_onoff; + u8_t tid; + u8_t trans_time; + u8_t delay; + } lc_light_onoff_set; + struct { + u16_t id; + struct net_buf_simple *value; + } lc_property_set; +} bt_mesh_light_server_recv_set_msg_t; + +typedef union { + struct { + struct net_buf_simple *data; + } sensor_status; +} bt_mesh_light_server_recv_status_msg_t; + +void bt_mesh_light_server_lock(void); +void bt_mesh_light_server_unlock(void); + +u8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, u16_t prop_id); + +void light_lightness_publish(struct bt_mesh_model *model, u16_t opcode); +void light_ctl_publish(struct bt_mesh_model *model, u16_t opcode); +void light_hsl_publish(struct bt_mesh_model *model, u16_t opcode); +void light_xyl_publish(struct bt_mesh_model *model, u16_t opcode); +void light_lc_publish(struct bt_mesh_model *model, u16_t opcode); + +int bt_mesh_light_lightness_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lightness_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_temp_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_hue_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_sat_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_setup_srv_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_light_lightness_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lightness_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_ctl_temp_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_hue_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_hsl_sat_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_xyl_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_light_lc_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + +#ifdef __cplusplus +} +#endif + +#endif /* _LIGHTING_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h new file mode 100644 index 000000000..8d7d6fe06 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/sensor_server.h @@ -0,0 +1,260 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _SENSOR_SERVER_H_ +#define _SENSOR_SERVER_H_ + +#include "server_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Sensor Property ID related */ +#define INVALID_SENSOR_PROPERTY_ID 0x0000 + +#define SENSOR_PROPERTY_ID_LEN 0x02 + +/* Sensor Descriptor state related */ +#define SENSOR_DESCRIPTOR_LEN 0x08 + +#define SENSOR_UNSPECIFIED_POS_TOLERANCE 0x000 +#define SENSOR_UNSPECIFIED_NEG_TOLERANCE 0x000 + +#define SENSOR_NOT_APPL_MEASURE_PERIOD 0x00 + +#define SENSOR_NOT_APPL_UPDATE_INTERVAL 0x00 + +/* Sensor Setting state related */ +#define INVALID_SENSOR_SETTING_PROPERTY_ID 0x0000 + +#define SENSOR_SETTING_PROPERTY_ID_LEN 0x02 +#define SENSOR_SETTING_ACCESS_LEN 0x01 + +#define SENSOR_SETTING_ACCESS_READ 0x01 +#define SENSOR_SETTING_ACCESS_READ_WRITE 0x03 + +/* Sensor Cadence state related */ +#define SENSOR_DIVISOR_TRIGGER_TYPE_LEN 0x01 +#define SENSOR_STATUS_MIN_INTERVAL_LEN 0x01 + +#define SENSOR_PERIOD_DIVISOR_MAX_VALUE 15 + +#define SENSOR_STATUS_MIN_INTERVAL_MAX 26 + +#define SENSOR_STATUS_TRIGGER_TYPE_CHAR 0 +#define SENSOR_STATUS_TRIGGER_TYPE_UINT16 1 + +#define SENSOR_STATUS_TRIGGER_UINT16_LEN 0x02 + +/* Sensor Data state related */ +#define SENSOR_DATA_FORMAT_A 0x00 +#define SENSOR_DATA_FORMAT_B 0x01 + +#define SENSOR_DATA_FORMAT_A_MPID_LEN 0x02 +#define SENSOR_DATA_FORMAT_B_MPID_LEN 0x03 + +#define SENSOR_DATA_ZERO_LEN 0x7F + +enum bt_mesh_sensor_sample_func { + UNSPECIFIED, + INSTANTANEOUS, + ARITHMETIC_MEAN, + RMS, + MAXIMUM, + MINIMUM, + ACCUMULATED, + COUNT, +}; + +struct sensor_descriptor { + u32_t positive_tolerance : 12, + negative_tolerance : 12, + sample_function : 8; + u8_t measure_period; + u8_t update_interval; +}; + +struct sensor_setting { + u16_t property_id; + u8_t access; + /* Or use union to include all possible types */ + struct net_buf_simple *raw; +}; + +struct sensor_cadence { + u8_t period_divisor : 7, + trigger_type : 1; + struct net_buf_simple *trigger_delta_down; + struct net_buf_simple *trigger_delta_up; + u8_t min_interval; + struct net_buf_simple *fast_cadence_low; + struct net_buf_simple *fast_cadence_high; +}; + +struct sensor_data { + /** + * Format A: The Length field is a 1-based uint4 value (valid range 0x0–0xF, + * representing range of 1 – 16). + * Format B: The Length field is a 1-based uint7 value (valid range 0x0–0x7F, + * representing range of 1 – 127). The value 0x7F represents a + * length of zero. + */ + u8_t format : 1, + length : 7; + struct net_buf_simple *raw_value; +}; + +struct sensor_series_column { + struct net_buf_simple *raw_value_x; + struct net_buf_simple *column_width; + struct net_buf_simple *raw_value_y; +}; + +struct bt_mesh_sensor_state { + u16_t sensor_property_id; + + /* Constant throughout the lifetime of an element */ + struct sensor_descriptor descriptor; + + /* Multiple Sensor Setting states may be present for each sensor. + * The Sensor Setting Property ID values shall be unique for each + * Sensor Property ID that identifies a sensor within an element. + */ + const u8_t setting_count; + struct sensor_setting *settings; + + /* The Sensor Cadence state may be not supported by sensors based + * on device properties referencing "non-scalar characteristics" + * such as "histograms" or "composite characteristics". + */ + struct sensor_cadence *cadence; + + struct sensor_data sensor_data; + + /* Values measured by sensors may be organized as arrays (and + * represented as series of columns, such as histograms). + * 1. The Sensor Raw Value X field has a size and representation + * defined by the Sensor Property ID and represents the left + * corner of the column on the X axis. + * 2. The Sensor Column Width field has a size and representation + * defined by the Sensor Property ID and represents the width + * of the column on the X axis. + * 3. The Sensor Raw Value Y field has a size and representation + * defined by the Sensor Property ID and represents the height + * of the column on the Y axis. + * Note: Values outside the bins defined by a Sensor Property are + * not included. For example, if the histogram is defined as 3 bins + * representing “lamp operating hours in a given temperature range” + * and the bins are [40,60), [60, 80), and [80,100], then any hours + * outside that [40, 100] range would not be included. + */ + struct sensor_series_column series_column; +}; + +/* 1. Multiple instances of the Sensor states may be present within the + * same model, provided that each instance has a unique value of the + * Sensor Property ID to allow the instances to be differentiated. + * 2. Note: The number of sensors within a multisensor is limited by the + * size of the message payload for the Sensor Descriptor Status message. + * A single Sensor Descriptor may be sent using a single Unsegmented + * Access message. Using Segmentation and Reassembly (SAR), up to 38 + * Sensor Descriptor states may be sent. + */ + +struct bt_mesh_sensor_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + const u8_t state_count; + struct bt_mesh_sensor_state *states; +}; + +struct bt_mesh_sensor_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + const u8_t state_count; + struct bt_mesh_sensor_state *states; +}; + +typedef union { + struct { + u16_t id; + u8_t period_divisor : 7, + trigger_type : 1; + struct net_buf_simple *trigger_delta_down; + struct net_buf_simple *trigger_delta_up; + u8_t min_interval; + struct net_buf_simple *fast_cadence_low; + struct net_buf_simple *fast_cadence_high; + } sensor_cadence_set; + struct { + u16_t id; + u16_t setting_id; + struct net_buf_simple *value; + } sensor_setting_set; +} bt_mesh_sensor_server_state_change_t; + +typedef union { + struct { + bool op_en; + u16_t id; + } sensor_descriptor_get; + struct { + u16_t id; + } sensor_cadence_get; + struct { + u16_t id; + } sensor_settings_get; + struct { + u16_t id; + u16_t setting_id; + } sensor_setting_get; + struct { + bool op_en; + u16_t id; + } sensor_get; + struct { + u16_t id; + struct net_buf_simple *raw_x; + } sensor_column_get; + struct { + bool op_en; + u16_t id; + struct net_buf_simple *raw; + } sensor_series_get; +} bt_mesh_sensor_server_recv_get_msg_t; + +typedef union { + struct { + u16_t id; + struct net_buf_simple *cadence; + } sensor_cadence_set; + struct { + u16_t id; + u16_t setting_id; + struct net_buf_simple *raw; + } sensor_setting_set; +} bt_mesh_sensor_server_recv_set_msg_t; + +int bt_mesh_sensor_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_sensor_setup_srv_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_sensor_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_sensor_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + +#ifdef __cplusplus +} +#endif + +#endif /* _SENSOR_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h b/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h new file mode 100644 index 000000000..08d6437b8 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/server_common.h @@ -0,0 +1,134 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _SERVER_COMMON_H_ +#define _SERVER_COMMON_H_ + +#include +#include +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_MESH_SERVER_RSP_MAX_LEN 384 + +#define BLE_MESH_SERVER_TRANS_MIC_SIZE 4 + +#define BLE_MESH_CHECK_SEND_STATUS(_func) do { \ + int __status = (_func); \ + if (__status) { \ + BT_ERR("%s, Send failed, err %d", __func__, __status); \ + } \ + } while(0); + +#define BLE_MESH_STATE_OFF 0x00 +#define BLE_MESH_STATE_ON 0x01 +#define BLE_MESH_STATE_RESTORE 0x02 + +/* Following 4 values are as per Mesh Model specification */ +#define BLE_MESH_LIGHTNESS_MIN 0x0001 +#define BLE_MESH_LIGHTNESS_MAX 0xFFFF +#define BLE_MESH_TEMPERATURE_MIN 0x0320 +#define BLE_MESH_TEMPERATURE_MAX 0x4E20 +#define BLE_MESH_TEMPERATURE_UNKNOWN 0xFFFF + +/* Refer 7.2 of Mesh Model Specification */ +#define BLE_MESH_RANGE_UPDATE_SUCCESS 0x00 +#define BLE_MESH_CANNOT_SET_RANGE_MIN 0x01 +#define BLE_MESH_CANNOT_SET_RANGE_MAX 0x02 + +#define BLE_MESH_UNKNOWN_REMAIN_TIME 0x3F +#define BLE_MESH_DEVICE_SPECIFIC_RESOLUTION 10 + +#define BLE_MESH_INVALID_DEVICE_PROPERTY_ID 0x0000 + +enum { + BLE_MESH_TRANS_TIMER_START, /* Proper transition timer has been started */ + BLE_MESH_TRANS_FLAG_MAX, +}; + +struct bt_mesh_state_transition { + bool just_started; + + u8_t trans_time; + u8_t remain_time; + u8_t delay; + u32_t quo_tt; + u32_t counter; + u32_t total_duration; + s64_t start_timestamp; + + BLE_MESH_ATOMIC_DEFINE(flag, BLE_MESH_TRANS_FLAG_MAX); + struct k_delayed_work timer; +}; + +struct bt_mesh_last_msg_info { + u8_t tid; + u16_t src; + u16_t dst; + s64_t timestamp; +}; + +#define BLE_MESH_SERVER_RSP_BY_APP 0 +#define BLE_MESH_SERVER_AUTO_RSP 1 + +struct bt_mesh_server_rsp_ctrl { + /** + * @brief BLE Mesh Server Response Option + * 1. If get_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response + * of Client Get messages need to be replied by the application; + * 2. If get_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response + * of Client Get messages will be replied by the server models; + * 3. If set_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response + * of Client Set messages need to be replied by the application; + * 4. If set_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response + * of Client Set messages will be replied by the server models; + * 5. If status_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response + * of Server Status messages need to be replied by the application; + * 6. If status_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response + * of Server status messages will be replied by the server models; + */ + u8_t get_auto_rsp : 1, /* Response for Client Get messages */ + set_auto_rsp : 1, /* Response for Client Set messages */ + status_auto_rsp : 1; /* Response for Server Status messages */ +}; + +u8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model); + +int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, u8_t *trans_time); + +int bt_mesh_server_get_optional(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, + u8_t *trans_time, u8_t *delay, + bool *optional); + +void bt_mesh_server_alloc_ctx(struct k_work *work); +void bt_mesh_server_free_ctx(struct k_work *work); + +bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, + u8_t tid, u16_t src, u16_t dst, s64_t *now); + +void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last, + u8_t tid, u16_t src, u16_t dst, s64_t *now); + +struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, u16_t msg_len); + +#ifdef __cplusplus +} +#endif + +#endif /* _SERVER_COMMON_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h b/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h new file mode 100644 index 000000000..c418192c2 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/state_binding.h @@ -0,0 +1,102 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _STATE_BINDING_H_ +#define _STATE_BINDING_H_ + +#include "mesh_access.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + GENERIC_ONOFF_STATE, + GENERIC_LEVEL_STATE, + GENERIC_ONPOWERUP_STATE, + GENERIC_POWER_ACTUAL_STATE, + LIGHT_LIGHTNESS_ACTUAL_STATE, + LIGHT_LIGHTNESS_LINEAR_STATE, + LIGHT_CTL_LIGHTNESS_STATE, + LIGHT_CTL_TEMP_DELTA_UV_STATE, + LIGHT_HSL_LIGHTNESS_STATE, + LIGHT_HSL_HUE_STATE, + LIGHT_HSL_SATURATION_STATE, + LIGHT_XYL_LIGHTNESS_STATE, + LIGHT_LC_LIGHT_ONOFF_STATE, + BIND_STATE_MAX, +} bt_mesh_server_state_type_t; + +typedef union { + struct { + u8_t onoff; + } gen_onoff; + struct { + s16_t level; + } gen_level; + struct { + u8_t onpowerup; + } gen_onpowerup; + struct { + u16_t power; + } gen_power_actual; + struct { + u16_t lightness; + } light_lightness_actual; + struct { + u16_t lightness; + } light_lightness_linear; + struct { + u16_t lightness; + } light_ctl_lightness; + struct { + u16_t temperature; + s16_t delta_uv; + } light_ctl_temp_delta_uv; + struct { + u16_t lightness; + } light_hsl_lightness; + struct { + u16_t hue; + } light_hsl_hue; + struct { + u16_t saturation; + } light_hsl_saturation; + struct { + u16_t lightness; + } light_xyl_lightness; + struct { + u8_t onoff; + } light_lc_light_onoff; +} bt_mesh_server_state_value_t; + +u16_t bt_mesh_convert_lightness_actual_to_linear(u16_t actual); + +u16_t bt_mesh_convert_lightness_linear_to_actual(u16_t linear); + +s16_t bt_mesh_convert_temperature_to_gen_level(u16_t temp, u16_t min, u16_t max); + +u16_t bt_mesh_covert_gen_level_to_temperature(s16_t level, u16_t min, u16_t max); + +s16_t bt_mesh_convert_hue_to_level(u16_t hue); + +u16_t bt_mesh_convert_level_to_hue(s16_t level); + +s16_t bt_mesh_convert_saturation_to_level(u16_t saturation); + +u16_t bt_mesh_convert_level_to_saturation(s16_t level); + +int bt_mesh_update_binding_state(struct bt_mesh_model *model, + bt_mesh_server_state_type_t type, + bt_mesh_server_state_value_t *value); + +#ifdef __cplusplus +} +#endif + +#endif /* _STATE_BINDING_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h b/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h new file mode 100644 index 000000000..5cb6da9b3 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/state_transition.h @@ -0,0 +1,100 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _STATE_TRANSITION_H_ +#define _STATE_TRANSITION_H_ + +#include "server_common.h" +#include "generic_server.h" +#include "sensor_server.h" +#include "lighting_server.h" +#include "time_scene_server.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void bt_mesh_server_calc_remain_time(struct bt_mesh_state_transition *transition); + +/* APIs used to get server model transition time values */ + +void generic_onoff_tt_values(struct bt_mesh_gen_onoff_srv *srv, + u8_t trans_time, u8_t delay); + +void generic_level_tt_values(struct bt_mesh_gen_level_srv *srv, + u8_t trans_time, u8_t delay); + +void generic_power_level_tt_values(struct bt_mesh_gen_power_level_srv *srv, + u8_t trans_time, u8_t delay); + +void light_lightness_actual_tt_values(struct bt_mesh_light_lightness_srv *srv, + u8_t trans_time, u8_t delay); + +void light_lightness_linear_tt_values(struct bt_mesh_light_lightness_srv *srv, + u8_t trans_time, u8_t delay); + +void light_ctl_tt_values(struct bt_mesh_light_ctl_srv *srv, + u8_t trans_time, u8_t delay); + +void light_ctl_temp_tt_values(struct bt_mesh_light_ctl_temp_srv *srv, + u8_t trans_time, u8_t delay); + +void light_hsl_tt_values(struct bt_mesh_light_hsl_srv *srv, + u8_t trans_time, u8_t delay); + +void light_hsl_hue_tt_values(struct bt_mesh_light_hsl_hue_srv *srv, + u8_t trans_time, u8_t delay); + +void light_hsl_sat_tt_values(struct bt_mesh_light_hsl_sat_srv *srv, + u8_t trans_time, u8_t delay); + +void light_xyl_tt_values(struct bt_mesh_light_xyl_srv *srv, + u8_t trans_time, u8_t delay); + +void light_lc_tt_values(struct bt_mesh_light_lc_srv *srv, + u8_t trans_time, u8_t delay); + +void scene_tt_values(struct bt_mesh_scene_srv *srv, u8_t trans_time, u8_t delay); + +/* Server model transition timer handlers */ + +void generic_onoff_work_handler(struct k_work *work); + +void generic_level_work_handler(struct k_work *work); + +void generic_power_level_work_handler(struct k_work *work); + +void light_lightness_actual_work_handler(struct k_work *work); + +void light_lightness_linear_work_handler(struct k_work *work); + +void light_ctl_work_handler(struct k_work *work); + +void light_ctl_temp_work_handler(struct k_work *work); + +void light_hsl_work_handler(struct k_work *work); + +void light_hsl_hue_work_handler(struct k_work *work); + +void light_hsl_sat_work_handler(struct k_work *work); + +void light_xyl_work_handler(struct k_work *work); + +void light_lc_work_handler(struct k_work *work); + +void scene_recall_work_handler(struct k_work *work); + +void bt_mesh_server_stop_transition(struct bt_mesh_state_transition *transition); + +void bt_mesh_server_start_transition(struct bt_mesh_state_transition *transition); + +#ifdef __cplusplus +} +#endif + +#endif /* _STATE_TRANSITION_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h b/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h new file mode 100644 index 000000000..dcd3efd71 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/include/time_scene_server.h @@ -0,0 +1,406 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _TIME_SCENE_SERVER_H_ +#define _TIME_SCENE_SERVER_H_ + +#include "server_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * 1. Mesh defines times based on International Atomic Time (TAI). The base + * representation of times is the number of seconds after 00:00:00 TAI + * on 2000-01-01 (that is, 1999-12-31 T23:59:28 UTC). + * 2. UTC: Coordinated Universal Time. For more information, please refer + * to https://time.is/zh/UTC + * 3. For the algorithm used for the transfer between TAI and UTC, please + * refer to Mesh Model Spec Section 5.1.1 + */ + +#define UNKNOWN_TAI_SECONDS 0x0000000000 +#define UNKNOWN_TAI_ZONE_CHANGE 0x0000000000 +#define UNKNOWN_TAI_DELTA_CHANGE 0x0000000000 +#define TAI_UTC_DELTA_MAX_VALUE 0x7FFF +#define TAI_SECONDS_LEN 0x05 +#define TAI_OF_ZONE_CHANGE_LEN 0x05 +#define TAI_OF_DELTA_CHANGE_LEN 0x05 + +#define INVALID_SCENE_NUMBER 0x0000 +#define SCENE_NUMBER_LEN 0x02 + +#define SCHEDULE_YEAR_ANY_YEAR 0x64 + +#define SCHEDULE_DAY_ANY_DAY 0x00 + +#define SCHEDULE_HOUR_ANY_HOUR 0x18 +#define SCHEDULE_HOUR_ONCE_A_DAY 0x19 + +#define SCHEDULE_SEC_ANY_OF_HOUR 0x3C +#define SCHEDULE_SEC_EVERY_15_MIN 0x3D +#define SCHEDULE_SEC_EVERY_20_MIN 0x3E +#define SCHEDULE_SEC_ONCE_AN_HOUR 0x3F + +#define SCHEDULE_SEC_ANY_OF_MIN 0x3C +#define SCHEDULE_SEC_EVERY_15_SEC 0x3D +#define SCHEDULE_SEC_EVERY_20_SEC 0x3E +#define SCHEDULE_SEC_ONCE_AN_MIN 0x3F + +#define SCHEDULE_ACT_TURN_OFF 0x00 +#define SCHEDULE_ACT_TURN_ON 0x01 +#define SCHEDULE_ACT_SCENE_RECALL 0x02 +#define SCHEDULE_ACT_NO_ACTION 0x0F + +#define SCHEDULE_SCENE_NO_SCENE 0x0000 + +#define SCHEDULE_ENTRY_MAX_INDEX 0x0F + +#define TIME_NONE 0x00 +#define TIME_AUTHORITY 0x01 +#define TIME_RELAY 0x02 +#define TIME_CLINET 0x03 + +#define SCENE_SUCCESS 0x00 +#define SCENE_REG_FULL 0x01 +#define SCENE_NOT_FOUND 0x02 + +/** + * The Time state represents the present TAI time, the current TAI-UTC Delta + * and local time zone offset, and the next change to each of the latter + * (e.g., because of a switch from winter to summer time or an announced leap + * second). It consists of 10 fields with a total size of 183 bits. + */ +struct bt_mesh_time_state { + struct { + u8_t tai_seconds[5]; + u8_t subsecond; + u8_t uncertainty; + u8_t time_zone_offset_curr; + u8_t time_zone_offset_new; + u8_t tai_zone_change[5]; + u16_t time_authority : 1, + tai_utc_delta_curr : 15; + u16_t tai_utc_delta_new : 15; + u8_t tai_delta_change[5]; + } time; + u8_t time_role; +}; + +struct bt_mesh_time_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_time_state *state; +}; + +struct bt_mesh_time_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_time_state *state; +}; + +struct scene_register { + u16_t scene_number; + u8_t scene_type; /* Indicate the type of scene value */ + /** + * Scene value may use a union to represent later, the union contains + * structures of all the model states which can be stored in a scene. + */ + struct net_buf_simple *scene_value; +}; + +/** + * Scenes serve as memory banks for storage of states (e.g., a power level + * or a light level/color). Values of states of an element can be stored + * as a scene and can be recalled later from the scene memory. + * + * A scene is represented by a Scene Number, which is a 16-bit non-zero, + * mesh-wide value. (There can be a maximum of 65535 scenes in a mesh + * network.) The meaning of a scene, as well as the state storage container + * associated with it, are determined by a model. + * + * The Scenes state change may start numerous parallel model transitions. + * In that case, each individual model handles the transition internally. + * + * The scene transition is defined as a group of individual model transitions + * started by a Scene Recall operation. The scene transition is in progress + * when at least one transition from the group of individual model transitions + * is in progress. + */ +struct bt_mesh_scenes_state { + const u16_t scene_count; + struct scene_register *scenes; + + /** + * The Current Scene state is a 16-bit value that contains either the Scene + * Number of the currently active scene or a value of 0x0000 when no scene + * is active. + * + * When a Scene Store operation or a Scene Recall operation completes with + * success, the Current Scene state value shall be to the Scene Number used + * during that operation. + * + * When the Current Scene Number is deleted from a Scene Register state as a + * result of Scene Delete operation, the Current Scene state shall be set to + * 0x0000. + * + * When any of the element's state that is marked as “Stored with Scene” has + * changed not as a result of a Scene Recall operation, the value of the + * Current Scene state shall be set to 0x0000. + * + * When a scene transition is in progress, the value of the Current Scene + * state shall be set to 0x0000. + */ + u16_t current_scene; + + /** + * The Target Scene state is a 16-bit value that contains the target Scene + * Number when a scene transition is in progress. + * + * When the scene transition is in progress and the target Scene Number is + * deleted from a Scene Register state as a result of Scene Delete operation, + * the Target Scene state shall be set to 0x0000. + * + * When the scene transition is in progress and a new Scene Number is stored + * in the Scene Register as a result of Scene Store operation, the Target + * Scene state shall be set to the new Scene Number. + * + * When the scene transition is not in progress, the value of the Target Scene + * state shall be set to 0x0000. + */ + u16_t target_scene; + + /* Indicate the status code for the last operation */ + u8_t status_code; + + /* Indicate if scene transition is in progress */ + bool in_progress; +}; + +struct bt_mesh_scene_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_scenes_state *state; + struct bt_mesh_last_msg_info last; + struct bt_mesh_state_transition transition; +}; + +struct bt_mesh_scene_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_scenes_state *state; +}; + +struct schedule_register { + bool in_use; + u64_t year : 7, + month : 12, + day : 5, + hour : 5, + minute : 6, + second : 6, + day_of_week : 7, + action : 4, + trans_time : 8; + u16_t scene_number; +}; + +struct bt_mesh_scheduler_state { + const u8_t schedule_count; + struct schedule_register *schedules; /* Up to 16 scheduled entries */ + + /** + * A recommended implementation of the Scheduler should calculate the + * value of the TAI Seconds of the next scheduled event and put it in + * a queue of scheduled events sorted by time. + * + * Every second, the first event in the queue is compared with the value + * of the Time state. The first event is executed if it is less than or + * equal to the Time state and then removed from the queue. After + * execution, the Repeat Flag shall be checked, and the next occurrence + * of the scheduled event is calculated and put in the queue. + * + * One second timeout value, and compare the first event in queue with the + * Time state. If it is satisfied, then execute the first event. Also the + * Repeat Flag need to be checked, if it is set then the event needs to + * be put into the end of queue. + * + * sys_slist_t event_queue; + * + * For each event_queue item, it can use the following struct: + * struct schedule_event { + * sys_snode_t node; + * u8_t event_index; + * }; + * + * Also we need a "struct k_delayed_work track_timer" which can be used to + * track the schedule timer and handle proper scheduled events. + */ +}; + +struct bt_mesh_scheduler_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_scheduler_state *state; +}; + +struct bt_mesh_scheduler_setup_srv { + struct bt_mesh_model *model; + struct bt_mesh_server_rsp_ctrl rsp_ctrl; + struct bt_mesh_scheduler_state *state; +}; + +typedef union { + struct { + u8_t tai_seconds[5]; + u8_t subsecond; + u8_t uncertainty; + u16_t time_authority : 1; + u16_t tai_utc_delta_curr : 15; + u8_t time_zone_offset_curr; + } time_set; + struct { + u8_t tai_seconds[5]; + u8_t subsecond; + u8_t uncertainty; + u16_t time_authority : 1; + u16_t tai_utc_delta_curr : 15; + u8_t time_zone_offset_curr; + } time_status; + struct { + u8_t time_zone_offset_new; + u8_t tai_zone_change[5]; + } time_zone_set; + struct { + u16_t tai_utc_delta_new : 15; + u8_t tai_delta_change[5]; + } tai_utc_delta_set; + struct { + u8_t role; + } time_role_set; + struct { + u16_t scene_number; + } scene_store; + struct { + u16_t scene_number; + } scene_recall; + struct { + u16_t scene_number; + } scene_delete; + struct { + u64_t index : 4, + year : 7, + month : 12, + day : 5, + hour : 5, + minute : 6, + second : 6, + day_of_week : 7, + action : 4, + trans_time : 8; + u16_t scene_number; + } scheduler_act_set; +} bt_mesh_time_scene_server_state_change_t; + +typedef union { + struct { + u8_t index; + } scheduler_act_get; +} bt_mesh_time_scene_server_recv_get_msg_t; + +typedef union { + struct { + u8_t tai_seconds[5]; + u8_t subsecond; + u8_t uncertainty; + u16_t time_authority : 1; + u16_t tai_utc_delta : 15; + u8_t time_zone_offset; + } time_set; + struct { + u8_t time_zone_offset_new; + u8_t tai_zone_change[5]; + } time_zone_set; + struct { + u16_t tai_utc_delta_new : 15; + u16_t padding : 1; + u8_t tai_delta_change[5]; + } tai_utc_delta_set; + struct { + u8_t time_role; + } time_role_set; + struct { + u16_t scene_number; + } scene_store; + struct { + bool op_en; + u16_t scene_number; + u8_t tid; + u8_t trans_time; + u8_t delay; + } scene_recall; + struct { + u16_t scene_number; + } scene_delete; + struct { + u64_t index : 4, + year : 7, + month : 12, + day : 5, + hour : 5, + minute : 6, + second : 6, + day_of_week : 7, + action : 4, + trans_time : 8; + u16_t scene_number; + } scheduler_act_set; +} bt_mesh_time_scene_server_recv_set_msg_t; + +typedef union { + struct { + u8_t tai_seconds[5]; + u8_t subsecond; + u8_t uncertainty; + u16_t time_authority : 1; + u16_t tai_utc_delta : 15; + u8_t time_zone_offset; + } time_status; +} bt_mesh_time_scene_server_recv_status_msg_t; + +void bt_mesh_time_scene_server_lock(void); +void bt_mesh_time_scene_server_unlock(void); + +void scene_publish(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u16_t opcode); + +int bt_mesh_time_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_time_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_setup_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_srv_init(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_setup_srv_init(struct bt_mesh_model *model, bool primary); + +int bt_mesh_time_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_time_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scene_setup_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_srv_deinit(struct bt_mesh_model *model, bool primary); +int bt_mesh_scheduler_setup_srv_deinit(struct bt_mesh_model *model, bool primary); + +#ifdef __cplusplus +} +#endif + +#endif /* _TIME_SCENE_SERVER_H_ */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c new file mode 100644 index 000000000..e3d6e51e9 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/lighting_server.c @@ -0,0 +1,3553 @@ +/* Bluetooth: Mesh Lighting Server Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "btc_ble_mesh_lighting_model.h" + +#include "access.h" +#include "transport.h" +#include "model_opcode.h" +#include "state_transition.h" +#include "device_property.h" + +static bt_mesh_mutex_t light_server_lock; + +static void bt_mesh_light_server_mutex_new(void) +{ + if (!light_server_lock.mutex) { + bt_mesh_mutex_create(&light_server_lock); + } +} + +static void bt_mesh_light_server_mutex_free(void) +{ + bt_mesh_mutex_free(&light_server_lock); +} + +void bt_mesh_light_server_lock(void) +{ + bt_mesh_mutex_lock(&light_server_lock); +} + +void bt_mesh_light_server_unlock(void) +{ + bt_mesh_mutex_unlock(&light_server_lock); +} + +/* message handlers (Start) */ + +/* Light Lightness Server/Setup Server message handlers */ + +static void send_light_lightness_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 5; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_actual); + if (srv->actual_transition.counter) { + bt_mesh_server_calc_remain_time(&srv->actual_transition); + net_buf_simple_add_le16(msg, srv->state->target_lightness_actual); + net_buf_simple_add_u8(msg, srv->actual_transition.remain_time); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_linear); + if (srv->linear_transition.counter) { + bt_mesh_server_calc_remain_time(&srv->linear_transition); + net_buf_simple_add_le16(msg, srv->state->target_lightness_linear); + net_buf_simple_add_u8(msg, srv->linear_transition.remain_time); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_last); + break; + } + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) { + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + } + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->lightness_range_min); + net_buf_simple_add_le16(msg, srv->state->lightness_range_max); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) { + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->lightness_range_min); + net_buf_simple_add_le16(msg, srv->state->lightness_range_max); + } + break; + default: + BT_WARN("%s, Unknown Light Lightness status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_lightness_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lightness_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS; + break; + default: + BT_WARN("%s, Unknown Light Lightness Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_light_lightness_status(model, ctx, false, opcode); + return; +} + +void light_lightness_publish(struct bt_mesh_model *model, u16_t opcode) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light Lightness Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: { + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light Lightness Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Light Lightness Server Model 0x%04x", __func__, model->id); + return; + } + + send_light_lightness_status(model, NULL, true, opcode); + return; +} + +static void light_lightness_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lightness_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t actual = 0U; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + actual = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lightness_set.op_en = optional, + .lightness_set.lightness = actual, + .lightness_set.tid = tid, + .lightness_set.trans_time = trans_time, + .lightness_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->actual_transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + if (actual) { + if (srv->state->lightness_range_min && actual < srv->state->lightness_range_min) { + actual = srv->state->lightness_range_min; + } else if (srv->state->lightness_range_max && actual > srv->state->lightness_range_max) { + actual = srv->state->lightness_range_max; + } + } + srv->state->target_lightness_actual = actual; + + /** + * If the target state is equal to the current state, the transition shall not be + * started and is considered complete. + */ + if (srv->state->target_lightness_actual != srv->state->lightness_actual) { + light_lightness_actual_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .lightness_set.lightness = srv->state->lightness_actual, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->actual_transition.timer.work._reserved) { + memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->actual_transition.counter == 0U) { + srv->state->lightness_actual = srv->state->target_lightness_actual; + /** + * Whenever the Light Lightness Actual state is changed with a non-transactional + * message or a completed sequence of transactional messages to a non-zero value, + * the value of the Light Lightness Last shall be set to the value of the Light + * Lightness Actual. + */ + if (srv->state->lightness_actual) { + srv->state->lightness_last = srv->state->lightness_actual; + } + } + + srv->actual_transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->actual_transition); + return; +} + +static void light_lightness_linear_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lightness_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t linear = 0U; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + linear = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lightness_linear_set.op_en = optional, + .lightness_linear_set.lightness = linear, + .lightness_linear_set.tid = tid, + .lightness_linear_set.trans_time = trans_time, + .lightness_linear_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->linear_transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state->target_lightness_linear = linear; + + /** + * If the target state is equal to the current state, the transition shall not + * be started and is considered complete. + */ + if (srv->state->target_lightness_linear != srv->state->lightness_linear) { + light_lightness_linear_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .lightness_linear_set.lightness = srv->state->lightness_actual, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->linear_transition.timer.work._reserved) { + memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->linear_transition.counter == 0U) { + srv->state->lightness_linear = srv->state->target_lightness_linear; + } + + srv->linear_transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->linear_transition); + return; +} + +static void light_lightness_default_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + u16_t lightness = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lightness_default_set.lightness = lightness, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state->lightness_default != lightness) { + srv->state->lightness_default = lightness; + + bt_mesh_light_server_state_change_t change = { + .lightness_default_set.lightness = lightness, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS); + + return; +} + +static void light_lightness_range_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + u16_t range_min = 0U, range_max = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + range_min = net_buf_simple_pull_le16(buf); + range_max = net_buf_simple_pull_le16(buf); + + if (range_min > range_max) { + BT_ERR("%s, Range Min 0x%04x is greater than Range Max 0x%04x", + __func__, range_min, range_max); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lightness_range_set.range_min = range_min, + .lightness_range_set.range_max = range_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + /** + * When a Light Lightness Setup Server receives a Light Lightness Range Set + * message or a Light Lightness Range Set Unacknowledged message with values + * that cannot be accepted, it shall set the status of the operation to a + * value representing the reason why the values cannot be accepted. + * + * TODO: 0x0000 for Light Range Min/Max is prohibited, but BQB test case + * MMDL/SR/LLNS/BI-01-C requires 'SUCCESS' when it sends a set message with + * Light Range Min set to 0x0000. + */ +#if 0 + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; +#else + if (range_min == 0x0000) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN; + } else if (range_max == 0x0000) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX; + } else { + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; + } +#endif + + if (range_min && srv->state->lightness_range_min != range_min) { + srv->state->lightness_range_min = range_min; + } + + if (range_max && srv->state->lightness_range_max != range_max) { + srv->state->lightness_range_max = range_max; + } + + bt_mesh_light_server_state_change_t change = { + .lightness_range_set.range_min = srv->state->lightness_range_min, + .lightness_range_set.range_max = srv->state->lightness_range_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET) { + send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS); + } + send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS); + + return; +} + +/* Light CTL Server/Temperature Server/Setup Server message handlers */ + +static void send_light_ctl_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 9; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness); + net_buf_simple_add_le16(msg, srv->state->temperature); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_lightness); + net_buf_simple_add_le16(msg, srv->state->target_temperature); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->temperature_range_min); + net_buf_simple_add_le16(msg, srv->state->temperature_range_max); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) { + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->temperature_range_min); + net_buf_simple_add_le16(msg, srv->state->temperature_range_max); + } + break; + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS: { + if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->temperature_default); + net_buf_simple_add_le16(msg, srv->state->delta_uv_default); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) { + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->temperature_default); + net_buf_simple_add_le16(msg, srv->state->delta_uv_default); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->temperature); + net_buf_simple_add_le16(msg, srv->state->delta_uv); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_temperature); + net_buf_simple_add_le16(msg, srv->state->target_delta_uv); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + break; + } + default: + BT_WARN("%s, Unknown Light CTL status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_ctl_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; + u16_t opcode = 0U; + + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Temperature Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + default: + BT_ERR("%s, Invalid Light CTL Server Model 0x%04x", __func__, model->id); + return; + } + + /* Callback the received message to the application layer */ + if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_CTL_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS; + break; + default: + BT_WARN("%s, Unknown Light CTL Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_light_ctl_status(model, ctx, false, opcode); + return; +} + +void light_ctl_publish(struct bt_mesh_model *model, u16_t opcode) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Temperature Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: { + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Light CTL Server Model 0x%04x", __func__, model->id); + return; + } + + send_light_ctl_status(model, NULL, true, opcode); + return; +} + +static void light_ctl_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_ctl_srv *srv = model->user_data; + u16_t lightness = 0U, temperature = 0U; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + s16_t delta_uv = 0; + bool optional = false; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + temperature = net_buf_simple_pull_le16(buf); + delta_uv = (s16_t) net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) { + BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature); + return; + } + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .ctl_set.op_en = optional, + .ctl_set.lightness = lightness, + .ctl_set.temperature = temperature, + .ctl_set.delta_uv = delta_uv, + .ctl_set.tid = tid, + .ctl_set.trans_time = trans_time, + .ctl_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state->target_lightness = lightness; + if (srv->state->temperature_range_min && + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { + temperature = srv->state->temperature_range_min; + } else if (srv->state->temperature_range_max && + srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature > srv->state->temperature_range_max) { + temperature = srv->state->temperature_range_max; + } + srv->state->target_temperature = temperature; + srv->state->target_delta_uv = delta_uv; + + if (srv->state->target_lightness != srv->state->lightness || + srv->state->target_temperature != srv->state->temperature || + srv->state->target_delta_uv != srv->state->delta_uv) { + light_ctl_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .ctl_set.lightness = srv->state->lightness, + .ctl_set.temperature = srv->state->temperature, + .ctl_set.delta_uv = srv->state->delta_uv, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->lightness = srv->state->target_lightness; + srv->state->temperature = srv->state->target_temperature; + srv->state->delta_uv = srv->state->target_delta_uv; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void light_ctl_default_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + u16_t lightness = 0U, temperature = 0U; + s16_t delta_uv = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + temperature = net_buf_simple_pull_le16(buf); + delta_uv = (s16_t) net_buf_simple_pull_le16(buf); + + if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) { + BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .ctl_default_set.lightness = lightness, + .ctl_default_set.temperature = temperature, + .ctl_default_set.delta_uv = delta_uv, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state->temperature_range_min && + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { + temperature = srv->state->temperature_range_min; + } else if (srv->state->temperature_range_max && + srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature > srv->state->temperature_range_max) { + temperature = srv->state->temperature_range_max; + } + + srv->state->lightness_default = lightness; + srv->state->temperature_default = temperature; + srv->state->delta_uv_default = delta_uv; + + bt_mesh_light_server_state_change_t change = { + .ctl_default_set.lightness = srv->state->lightness_default, + .ctl_default_set.temperature = srv->state->temperature_default, + .ctl_default_set.delta_uv = srv->state->delta_uv_default, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS); + + return; +} + +static void light_ctl_temp_range_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + u16_t min = 0U, max = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + min = net_buf_simple_pull_le16(buf); + max = net_buf_simple_pull_le16(buf); + + /* This is as per 6.1.3.1 in Mesh Model Specification */ + if (min > max || + min < BLE_MESH_TEMPERATURE_MIN || (min != BLE_MESH_TEMPERATURE_UNKNOWN && min > BLE_MESH_TEMPERATURE_MAX) || + max < BLE_MESH_TEMPERATURE_MIN || (max != BLE_MESH_TEMPERATURE_UNKNOWN && max > BLE_MESH_TEMPERATURE_MAX)) { + BT_ERR("%s, Invalid parameter, range Min 0x%04x, range max 0x%04x", + __func__, min, max); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .ctl_temp_range_set.range_min = min, + .ctl_temp_range_set.range_max = max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (min == BLE_MESH_TEMPERATURE_UNKNOWN) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN; + } else if (max == BLE_MESH_TEMPERATURE_UNKNOWN ) { + srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX; + } else { + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; + } + + if (min != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_min != min) { + srv->state->temperature_range_min = min; + } + + if (max != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_max != max) { + srv->state->temperature_range_max = max; + } + + bt_mesh_light_server_state_change_t change = { + .ctl_temp_range_set.range_min = srv->state->temperature_range_min, + .ctl_temp_range_set.range_max = srv->state->temperature_range_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS); + + return; +} + +static void light_ctl_temp_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t temperature = 0U; + s16_t delta_uv = 0; + bool optional = false; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + temperature = net_buf_simple_pull_le16(buf); + delta_uv = (s16_t) net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) { + BT_ERR("%s, Invalid temperature 0x%04x", __func__, temperature); + return; + } + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .ctl_temp_set.op_en = optional, + .ctl_temp_set.temperature = temperature, + .ctl_temp_set.delta_uv = delta_uv, + .ctl_temp_set.tid = tid, + .ctl_temp_set.trans_time = trans_time, + .ctl_temp_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + if (srv->state->temperature_range_min && + srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature < srv->state->temperature_range_min) { + temperature = srv->state->temperature_range_min; + } else if (srv->state->temperature_range_max && + srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN && + temperature > srv->state->temperature_range_max) { + temperature = srv->state->temperature_range_max; + } + srv->state->target_temperature = temperature; + srv->state->target_delta_uv = delta_uv; + + if (srv->state->target_temperature != srv->state->temperature || + srv->state->target_delta_uv != srv->state->delta_uv) { + light_ctl_temp_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .ctl_temp_set.temperature = srv->state->temperature, + .ctl_temp_set.delta_uv = srv->state->delta_uv, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->temperature = srv->state->target_temperature; + srv->state->delta_uv = srv->state->target_delta_uv; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET) { + send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + } + send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +/* Light HSL Server/Hue Server/Saturation Server/Setup Server message handlers */ + +static void send_light_hsl_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 9; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS: + case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS) { + net_buf_simple_add_le16(msg, srv->state->lightness); + net_buf_simple_add_le16(msg, srv->state->hue); + net_buf_simple_add_le16(msg, srv->state->saturation); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS) { + net_buf_simple_add_le16(msg, srv->state->target_lightness); + net_buf_simple_add_le16(msg, srv->state->target_hue); + net_buf_simple_add_le16(msg, srv->state->target_saturation); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->hue_default); + net_buf_simple_add_le16(msg, srv->state->saturation_default); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) { + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->hue_default); + net_buf_simple_add_le16(msg, srv->state->saturation_default); + } + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->hue_range_min); + net_buf_simple_add_le16(msg, srv->state->hue_range_max); + net_buf_simple_add_le16(msg, srv->state->saturation_range_min); + net_buf_simple_add_le16(msg, srv->state->saturation_range_max); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) { + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->hue_range_min); + net_buf_simple_add_le16(msg, srv->state->hue_range_max); + net_buf_simple_add_le16(msg, srv->state->saturation_range_min); + net_buf_simple_add_le16(msg, srv->state->saturation_range_max); + } + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->hue); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_hue); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->saturation); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_saturation); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + break; + } + default: + BT_WARN("%s, Unknown Light HSL status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_hsl_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; + u16_t opcode = 0U; + + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Hue Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Saturation Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + default: + BT_ERR("%s, Invalid Light HSL Server Model 0x%04x", __func__, model->id); + return; + } + + /* Callback the received message to the application layer */ + if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_HSL_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS; + break; + default: + BT_WARN("%s, Unknown Light HSL Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_light_hsl_status(model, ctx, false, opcode); + return; +} + +void light_hsl_publish(struct bt_mesh_model *model, u16_t opcode) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Hue Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Saturation Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: { + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Light HSL Server Model 0x%04x", __func__, model->id); + return; + } + + send_light_hsl_status(model, NULL, true, opcode); + return; +} + +static void light_hsl_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_hsl_srv *srv = model->user_data; + u16_t lightness = 0U, hue = 0U, saturation = 0U; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + hue = net_buf_simple_pull_le16(buf); + saturation = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .hsl_set.op_en = optional, + .hsl_set.lightness = lightness, + .hsl_set.hue = hue, + .hsl_set.saturation = saturation, + .hsl_set.tid = tid, + .hsl_set.trans_time = trans_time, + .hsl_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state->target_lightness = lightness; + if (srv->state->hue_range_min && hue < srv->state->hue_range_min) { + hue = srv->state->hue_range_min; + } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) { + hue = srv->state->hue_range_max; + } + srv->state->target_hue = hue; + if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) { + saturation = srv->state->saturation_range_min; + } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) { + saturation = srv->state->saturation_range_max; + } + srv->state->target_saturation = saturation; + + /** + * If the target state is equal to the current state, the transition shall not + * be started and is considered complete. + */ + if (srv->state->target_lightness != srv->state->lightness || + srv->state->target_hue != srv->state->hue || + srv->state->target_saturation != srv->state->saturation) { + light_hsl_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .hsl_set.lightness = srv->state->lightness, + .hsl_set.hue = srv->state->hue, + .hsl_set.saturation = srv->state->saturation, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->lightness = srv->state->target_lightness; + srv->state->hue = srv->state->target_hue; + srv->state->saturation = srv->state->target_saturation; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void light_hsl_default_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + u16_t lightness = 0U, hue = 0U, saturation = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + hue = net_buf_simple_pull_le16(buf); + saturation = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .hsl_default_set.lightness = lightness, + .hsl_default_set.hue = hue, + .hsl_default_set.saturation = saturation, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state->hue_range_min && hue < srv->state->hue_range_min) { + hue = srv->state->hue_range_min; + } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) { + hue = srv->state->hue_range_max; + } + + if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) { + saturation = srv->state->saturation_range_min; + } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) { + saturation = srv->state->saturation_range_max; + } + + srv->state->lightness_default = lightness; + srv->state->hue_default = hue; + srv->state->saturation_default = saturation; + + bt_mesh_light_server_state_change_t change = { + .hsl_default_set.lightness = srv->state->lightness_default, + .hsl_default_set.hue = srv->state->hue_default, + .hsl_default_set.saturation = srv->state->saturation_default, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_STATUS); + + return; +} + +static void light_hsl_range_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + u16_t hue_min = 0U, hue_max = 0U, saturation_min = 0U, saturation_max = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + hue_min = net_buf_simple_pull_le16(buf); + hue_max = net_buf_simple_pull_le16(buf); + saturation_min = net_buf_simple_pull_le16(buf); + saturation_max = net_buf_simple_pull_le16(buf); + + if (hue_min > hue_max) { + BT_ERR("%s, Invalid parameter, Hue min 0x%04x, Hue max 0x%04x", + __func__, hue_min, hue_max); + return; + } + + if (saturation_min > saturation_max) { + BT_ERR("%s, Invalid parameter, Saturation min 0x%04x, Saturation max 0x%04x", + __func__, hue_min, hue_max); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .hsl_range_set.hue_range_min = hue_min, + .hsl_range_set.hue_range_max = hue_max, + .hsl_range_set.sat_range_min = saturation_min, + .hsl_range_set.sat_range_max = saturation_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; + srv->state->hue_range_min = hue_min; + srv->state->hue_range_max = hue_max; + srv->state->saturation_range_min = saturation_min; + srv->state->saturation_range_max = saturation_max; + + bt_mesh_light_server_state_change_t change = { + .hsl_range_set.hue_range_min = srv->state->hue_range_min, + .hsl_range_set.hue_range_max = srv->state->hue_range_max, + .hsl_range_set.sat_range_min = srv->state->saturation_range_min, + .hsl_range_set.sat_range_max = srv->state->saturation_range_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_STATUS); + + return; +} + +static void light_hsl_hue_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u16_t hue = 0U; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + hue = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .hsl_hue_set.op_en = optional, + .hsl_hue_set.hue = hue, + .hsl_hue_set.tid = tid, + .hsl_hue_set.trans_time = trans_time, + .hsl_hue_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + if (srv->state->hue_range_min && hue < srv->state->hue_range_min) { + hue = srv->state->hue_range_min; + } else if (srv->state->hue_range_max && hue > srv->state->hue_range_max) { + hue = srv->state->hue_range_max; + } + srv->state->target_hue = hue; + + /** + * If the target state is equal to the current state, the transition shall not + * be started and is considered complete. + */ + if (srv->state->target_hue != srv->state->hue) { + light_hsl_hue_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .hsl_hue_set.hue = srv->state->hue, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->hue = srv->state->target_hue; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void light_hsl_sat_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t saturation = 0U; + bool optional = false; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + saturation = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .hsl_saturation_set.op_en = optional, + .hsl_saturation_set.saturation = saturation, + .hsl_saturation_set.tid = tid, + .hsl_saturation_set.trans_time = trans_time, + .hsl_saturation_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + if (srv->state->saturation_range_min && saturation < srv->state->saturation_range_min) { + saturation = srv->state->saturation_range_min; + } else if (srv->state->saturation_range_max && saturation > srv->state->saturation_range_max) { + saturation = srv->state->saturation_range_max; + } + srv->state->target_saturation = saturation; + + /** + * If the target state is equal to the current state, the transition shall not + * be started and is considered complete. + */ + if (srv->state->target_saturation != srv->state->saturation) { + light_hsl_sat_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .hsl_saturation_set.saturation = srv->state->saturation, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->saturation = srv->state->target_saturation; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET) { + send_light_hsl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + } + send_light_hsl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +/* Light xyL Server/Setup Server message handlers */ + +static void send_light_xyl_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 9; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS: + case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS: { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS) { + net_buf_simple_add_le16(msg, srv->state->lightness); + net_buf_simple_add_le16(msg, srv->state->x); + net_buf_simple_add_le16(msg, srv->state->y); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } else if (opcode == BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS) { + net_buf_simple_add_le16(msg, srv->state->target_lightness); + net_buf_simple_add_le16(msg, srv->state->target_x); + net_buf_simple_add_le16(msg, srv->state->target_y); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + } + break; + } + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->x_default); + net_buf_simple_add_le16(msg, srv->state->y_default); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) { + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->lightness_default); + net_buf_simple_add_le16(msg, srv->state->x_default); + net_buf_simple_add_le16(msg, srv->state->y_default); + } + break; + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->x_range_min); + net_buf_simple_add_le16(msg, srv->state->x_range_max); + net_buf_simple_add_le16(msg, srv->state->y_range_min); + net_buf_simple_add_le16(msg, srv->state->y_range_max); + } else if (model->id == BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) { + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->status_code); + net_buf_simple_add_le16(msg, srv->state->x_range_min); + net_buf_simple_add_le16(msg, srv->state->x_range_max); + net_buf_simple_add_le16(msg, srv->state->y_range_min); + net_buf_simple_add_le16(msg, srv->state->y_range_max); + } + break; + default: + BT_WARN("%s, Unknown Light xyL status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_xyl_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_xyl_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_XYL_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS; + break; + default: + BT_WARN("%s, Unknown Light xyL Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_light_xyl_status(model, ctx, false, opcode); + return; +} + +void light_xyl_publish(struct bt_mesh_model *model, u16_t opcode) +{ + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light xyL Server state", __func__); + return; + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: { + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light xyL Setup Server state", __func__); + return; + } + break; + } + default: + BT_ERR("%s, Invalid Light xyL Server Model 0x%04x", __func__, model->id); + return; + } + + send_light_xyl_status(model, NULL, true, opcode); + return; +} + +static void light_xyl_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_xyl_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t lightness = 0U, x = 0U, y = 0U; + bool optional = false; + s64_t now = 0; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + x = net_buf_simple_pull_le16(buf); + y = net_buf_simple_pull_le16(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .xyl_set.op_en = optional, + .xyl_set.lightness = lightness, + .xyl_set.x = x, + .xyl_set.y = y, + .xyl_set.tid = tid, + .xyl_set.trans_time = trans_time, + .xyl_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) { + send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + } + send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state->target_lightness = lightness; + if (srv->state->x_range_min && x < srv->state->x_range_min) { + x = srv->state->x_range_min; + } else if (srv->state->x_range_max && x > srv->state->x_range_max) { + x = srv->state->x_range_max; + } + srv->state->target_x = x; + if (srv->state->y_range_min && y < srv->state->y_range_min) { + y = srv->state->y_range_min; + } else if (srv->state->y_range_max && y > srv->state->y_range_max) { + y = srv->state->y_range_max; + } + srv->state->target_y = y; + + /** + * If the target state is equal to the current state, the transition shall not + * be started and is considered complete. + */ + if (srv->state->target_lightness != srv->state->lightness || + srv->state->target_x != srv->state->x || + srv->state->target_y != srv->state->y) { + light_xyl_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .xyl_set.lightness = srv->state->lightness, + .xyl_set.x = srv->state->x, + .xyl_set.y = srv->state->y, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) { + send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + } + send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->lightness = srv->state->target_lightness; + srv->state->x = srv->state->target_x; + srv->state->y = srv->state->target_y; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_SET) { + send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + } + send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void light_xyl_default_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + u16_t lightness = 0U, x = 0U, y = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + lightness = net_buf_simple_pull_le16(buf); + x = net_buf_simple_pull_le16(buf); + y = net_buf_simple_pull_le16(buf); + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .xyl_default_set.lightness = lightness, + .xyl_default_set.x = x, + .xyl_default_set.y = y, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (srv->state->x_range_min && x < srv->state->x_range_min) { + x = srv->state->x_range_min; + } else if (srv->state->x_range_max && x > srv->state->x_range_max) { + x = srv->state->x_range_max; + } + + if (srv->state->y_range_min && y < srv->state->y_range_min) { + y = srv->state->y_range_min; + } else if (srv->state->y_range_max && y > srv->state->y_range_max) { + y = srv->state->y_range_max; + } + + srv->state->lightness_default = lightness; + srv->state->x_default = x; + srv->state->y_default = y; + + bt_mesh_light_server_state_change_t change = { + .xyl_default_set.lightness = srv->state->lightness_default, + .xyl_default_set.x = srv->state->x_default, + .xyl_default_set.y = srv->state->y_default, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET) { + send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS); + } + send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_STATUS); + + return; +} + +static void light_xyl_range_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + u16_t x_min = 0U, x_max = 0U, y_min = 0U, y_max = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + x_min = net_buf_simple_pull_le16(buf); + x_max = net_buf_simple_pull_le16(buf); + y_min = net_buf_simple_pull_le16(buf); + y_max = net_buf_simple_pull_le16(buf); + + if (x_min > x_max) { + BT_ERR("%s, Invalid parameter, xyL x min 0x%04x, xyL x max 0x%04x", + __func__, x_min, x_max); + return; + } + + if (y_min > y_max) { + BT_ERR("%s, Invalid parameter, xyL y min 0x%04x, xyL y max 0x%04x", + __func__, y_min, y_max); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .xyl_range_set.x_range_min = x_min, + .xyl_range_set.x_range_max = x_max, + .xyl_range_set.y_range_min = y_min, + .xyl_range_set.y_range_max = y_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS; + srv->state->x_range_min = x_min; + srv->state->x_range_max = x_max; + srv->state->y_range_min = y_min; + srv->state->y_range_max = y_max; + + bt_mesh_light_server_state_change_t change = { + .xyl_range_set.x_range_min = srv->state->x_range_min, + .xyl_range_set.x_range_max = srv->state->x_range_max, + .xyl_range_set.y_range_min = srv->state->y_range_min, + .xyl_range_set.y_range_max = srv->state->y_range_max, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET) { + send_light_xyl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS); + } + send_light_xyl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_STATUS); + + return; +} + +/* Light LC Server/Setup Server message handlers */ +static void send_light_lc_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + u8_t length = 2 + 3; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS: + net_buf_simple_add_u8(msg, srv->lc->state.mode); + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS: + net_buf_simple_add_u8(msg, srv->lc->state.occupancy_mode); + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS: + net_buf_simple_add_u8(msg, srv->lc->state.light_onoff); + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_u8(msg, srv->lc->state.target_light_onoff); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + break; + default: + BT_WARN("%s, Unknown Light LC status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_lc_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + u16_t opcode = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS; + break; + case BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET: + opcode = BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS; + break; + default: + BT_WARN("%s, Unknown Light LC Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_light_lc_status(model, ctx, false, opcode); + return; +} + +void light_lc_publish(struct bt_mesh_model *model, u16_t opcode) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + send_light_lc_status(model, NULL, true, opcode); + return; +} + +static void light_lc_mode_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + u8_t mode = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + mode = net_buf_simple_pull_u8(buf); + if (mode > BLE_MESH_STATE_ON) { + BT_ERR("%s, Invalid LC Mode 0x%02x", __func__, mode); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lc_mode_set.mode = mode, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + srv->lc->state.mode = mode; + + bt_mesh_light_server_state_change_t change = { + .lc_mode_set.mode = srv->lc->state.mode, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET) { + send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS); + } + send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_MODE_STATUS); + + return; +} + +static void light_lc_om_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + u8_t om = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + om = net_buf_simple_pull_u8(buf); + if (om > BLE_MESH_STATE_ON) { + BT_ERR("%s, Invalid LC Occupancy Mode 0x%02x", __func__, om); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lc_om_set.mode = om, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + srv->lc->state.occupancy_mode = om; + + bt_mesh_light_server_state_change_t change = { + .lc_om_set.mode = srv->lc->state.occupancy_mode, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET) { + send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS); + } + send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_OM_STATUS); + + return; +} + +static void light_lc_light_onoff_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_srv *srv = model->user_data; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + bool optional = false; + u8_t onoff = 0U; + s64_t now = 0; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + onoff = net_buf_simple_pull_u8(buf); + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lc_light_onoff_set.op_en = optional, + .lc_light_onoff_set.light_onoff = onoff, + .lc_light_onoff_set.tid = tid, + .lc_light_onoff_set.trans_time = trans_time, + .lc_light_onoff_set.delay = delay, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) { + send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + } + send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_light_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->lc->state.target_light_onoff = onoff; + + if (srv->lc->state.target_light_onoff != srv->lc->state.light_onoff) { + light_lc_tt_values(srv, trans_time, delay); + } else { + bt_mesh_light_server_state_change_t change = { + .lc_light_onoff_set.onoff = srv->lc->state.light_onoff, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) { + send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + } + send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + + bt_mesh_light_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->lc->state.light_onoff = srv->lc->state.target_light_onoff; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET) { + send_light_lc_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + } + send_light_lc_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + + bt_mesh_light_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void light_lc_sensor_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + /** + * When a Light LC Server receives a Sensor Status message, and if the message + * Raw field contains a Raw Value for the Motion Sensed Property, and the value + * is greater than 0, or a Raw Value for the People Count Property, and the + * value is greater than 0, or a Raw Value for the Presence Detected Property, + * and the value is greater than 0, then it shall set the Light LC Occupancy + * state to 0b1. + * If the message Raw field contains a Raw Value for the Time Since Motion Sensed + * device property, which represents a value less than or equal to the value of + * the Light LC Occupancy Delay state, it shall delay setting the Light LC Occupancy + * state to 0b1 by the difference between the value of the Light LC Occupancy Delay + * state and the received Time Since Motion value. + * When a Light LC Server receives a Sensor Status message, and if the message Raw + * field contains a Raw Value for the Present Ambient Light Level device property, + * it shall set the Light LC Ambient LuxLevel state to the Represented Value of the + * received Present Ambient Light Level. + * + * Motion Sensed: 1 octet, 0x0042 + * People Count: 2 octets, 0x004C + * Presence Detected: 1 octet, 0x004D + * + * Time Since Motion Sensed: 2 octets, 0x0068 + * + * Present Ambient Light Level: 4 octets, 0x004E + */ + struct bt_mesh_light_lc_srv *srv = model->user_data; + bt_mesh_light_server_state_change_t change = {0}; + u16_t mpid = 0U, prop_id = 0U; + u8_t length = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + if (srv->rsp_ctrl.status_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_status_msg_t status = { + .sensor_status.data = buf, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_STATUS_MSG, model, ctx, (const u8_t *)&status, sizeof(status)); + return; + } + + mpid = net_buf_simple_pull_le16(buf); + if (mpid & BIT(0)) { + length = (u8_t)((mpid & 0xff) >> 1); + u8_t msb = net_buf_simple_pull_u8(buf); + prop_id = (u16_t)(msb << 8) | (u16_t)(mpid >> 8); + } else { + length = (u8_t)((mpid & 0x1f) >> 1); + prop_id = (u16_t)(mpid >> 5); + } + + change.sensor_status.property_id = prop_id; + + switch (prop_id) { + case BLE_MESH_MOTION_SENSED: { + if (length != BLE_MESH_MOTION_SENSED_LEN || length != buf->len) { + BT_WARN("%s, Invalid Motion Sensed Property length", __func__); + return; + } + u8_t val = net_buf_simple_pull_u8(buf); + if (val > 0) { + srv->lc->state.occupancy = BLE_MESH_STATE_ON; + + change.sensor_status.state.occupancy = srv->lc->state.occupancy; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + break; + } + case BLE_MESH_PEOPLE_COUNT: { + if (length != BLE_MESH_PEOPLE_COUNT_LEN || length != buf->len) { + BT_WARN("%s, Invalid Motion Sensed Property length", __func__); + return; + } + u16_t val = net_buf_simple_pull_le16(buf); + if (val > 0) { + srv->lc->state.occupancy = BLE_MESH_STATE_ON; + + change.sensor_status.state.occupancy = srv->lc->state.occupancy; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + break; + } + case BLE_MESH_PRESENCE_DETECTED: { + if (length != BLE_MESH_PRESENCE_DETECTED_LEN || length != buf->len) { + BT_WARN("%s, Invalid Motion Sensed Property length", __func__); + return; + } + u8_t val = net_buf_simple_pull_u8(buf); + if (val > 0) { + srv->lc->state.occupancy = BLE_MESH_STATE_ON; + + change.sensor_status.state.occupancy = srv->lc->state.occupancy; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + break; + } + case BLE_MESH_TIME_SINCE_MOTION_SENSED: { + if (length != BLE_MESH_TIME_SINCE_MOTION_SENSED_LEN || length != buf->len) { + BT_WARN("%s, Invalid Motion Sensed Property length", __func__); + return; + } + u16_t val = net_buf_simple_pull_le16(buf); + if (val <= srv->lc->prop_state.time_occupancy_delay) { + srv->lc->prop_state.set_occupancy_to_1_delay = + srv->lc->prop_state.time_occupancy_delay - val; + + change.sensor_status.state.set_occupancy_to_1_delay = srv->lc->prop_state.set_occupancy_to_1_delay; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + break; + } + case BLE_MESH_PRESENT_AMBIENT_LIGHT_LEVEL: { + /** + * Present Ambient Light Level device property is 4 octets, but ambient + * luxlevel length is 3 octets, and other devices may send Sensor Status + * which only contains 3 octets just for Light LC Server. + * Here we just check if the length is larger than 3. + */ + if (buf->len < 3) { + BT_WARN("%s, Invalid Motion Sensed Property length", __func__); + return; + } + u16_t lsb = net_buf_simple_pull_le16(buf); + u8_t msb = net_buf_simple_pull_u8(buf); + srv->lc->state.ambient_luxlevel = (msb << 16) | lsb; + + change.sensor_status.state.ambient_luxlevel = srv->lc->state.ambient_luxlevel; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + break; + } + default: + break; + } +} + +static u8_t *get_light_lc_prop_val(struct bt_mesh_model *model, u16_t prop_id) +{ + struct bt_mesh_light_lc_setup_srv *srv = model->user_data; + u8_t *val = NULL; + + switch (prop_id) { + case BLE_MESH_LIGHT_CONTROL_TIME_OCCUPANCY_DELAY: + val = (u8_t *)&srv->lc->prop_state.time_occupancy_delay; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_FADE_ON: + val = (u8_t *)&srv->lc->prop_state.time_fade_on; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_RUN_ON: + val = (u8_t *)&srv->lc->prop_state.time_run_on; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_FADE: + val = (u8_t *)&srv->lc->prop_state.time_fade; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_PROLONG: + val = (u8_t *)&srv->lc->prop_state.time_prolong; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_AUTO: + val = (u8_t *)&srv->lc->prop_state.time_fade_standby_auto; + break; + case BLE_MESH_LIGHT_CONTROL_TIME_FADE_STANDBY_MANUAL: + val = (u8_t *)&srv->lc->prop_state.time_fade_standby_manual; + break; + case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_ON: + val = (u8_t *)&srv->lc->prop_state.lightness_on; + break; + case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_PROLONG: + val = (u8_t *)&srv->lc->prop_state.lightness_prolong; + break; + case BLE_MESH_LIGHT_CONTROL_LIGHTNESS_STANDBY: + val = (u8_t *)&srv->lc->prop_state.lightness_standby; + break; + case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_ON: + val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_on; + break; + case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_PROLONG: + val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_prolong; + break; + case BLE_MESH_LIGHT_CONTROL_AMBIENT_LUXLEVEL_STANDBY: + val = (u8_t *)&srv->lc->prop_state.ambient_luxlevel_standby; + break; + case BLE_MESH_LIGHT_CONTROL_REGULATOR_KIU: + val = (u8_t *)&srv->lc->prop_state.regulator_kiu; + break; + case BLE_MESH_LIGHT_CONTROL_REGULATOR_KID: + val = (u8_t *)&srv->lc->prop_state.regulator_kid; + break; + case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPU: + val = (u8_t *)&srv->lc->prop_state.regulator_kpu; + break; + case BLE_MESH_LIGHT_CONTROL_REGULATOR_KPD: + val = (u8_t *)&srv->lc->prop_state.regulator_kpd; + break; + case BLE_MESH_LIGHT_CONTROL_REGULATOR_ACCURACY: + val = (u8_t *)&srv->lc->prop_state.regulator_accuracy; + break; + } + + return val; +} + +u8_t *bt_mesh_get_lc_prop_value(struct bt_mesh_model *model, u16_t prop_id) +{ + if (model == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return NULL; + } + + return get_light_lc_prop_val(model, prop_id); +} + +static void send_light_lc_prop_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t prop_id, bool publish) +{ + struct net_buf_simple *msg = NULL; + u8_t length = 1 + 2 + 4; + u8_t *prop_val = NULL; + + prop_val = get_light_lc_prop_val(model, prop_id); + if (prop_val == NULL) { + BT_ERR("%s, Failed to get Light LC Property value", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_STATUS); + net_buf_simple_add_le16(msg, prop_id); + net_buf_simple_add_mem(msg, prop_val, bt_mesh_get_dev_prop_len(prop_id)); + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void light_lc_prop_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_setup_srv *srv = model->user_data; + u16_t prop_id = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id < 0x002B || prop_id > 0x003C) { + BT_ERR("%s, Invalid Light LC Property ID 0x%04x", __func__, prop_id); + return; + } + + /* Callback the received message to the application layer */ + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_get_msg_t get = { + .lc_property_get.id = net_buf_simple_pull_le16(buf), + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + return; + } + + send_light_lc_prop_status(model, ctx, prop_id, false); + return; +} + +static void light_lc_prop_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_light_lc_setup_srv *srv = model->user_data; + u8_t *prop_val = NULL, expect_len = 0U; + u16_t prop_id = 0U; + + if (srv == NULL || srv->lc == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id < 0x002B || prop_id > 0x003C) { + BT_ERR("%s, Invalid Light LC Property ID 0x%04x", __func__, prop_id); + return; + } + + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_light_server_recv_set_msg_t set = { + .lc_property_set.id = net_buf_simple_pull_le16(buf), + .lc_property_set.value = buf, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + expect_len = bt_mesh_get_dev_prop_len(prop_id); + if (buf->len != expect_len) { + BT_ERR("%s, Invalid Light LC Property length, ID 0x%04x, expect %d, actual %d", + __func__, prop_id, expect_len, buf->len); + return; + } + + prop_val = get_light_lc_prop_val(model, prop_id); + if (prop_val == NULL) { + BT_ERR("%s, Failed to get Light LC Property value", __func__); + return; + } + + memcpy(prop_val, buf->data, buf->len); + + bt_mesh_light_server_state_change_t change = { + .lc_property_set.id = prop_id, + .lc_property_set.value = buf, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET) { + send_light_lc_prop_status(model, ctx, prop_id, false); + } + send_light_lc_prop_status(model, ctx, prop_id, true); + + return; +} + +/* message handlers (End) */ + +/* Mapping of message handlers for Light Lightness Server (0x1300) */ +const struct bt_mesh_model_op light_lightness_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET, 0, light_lightness_get }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET, 3, light_lightness_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET_UNACK, 3, light_lightness_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET, 0, light_lightness_get }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET, 3, light_lightness_linear_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET_UNACK, 3, light_lightness_linear_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET, 0, light_lightness_get }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET, 0, light_lightness_get }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET, 0, light_lightness_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light Lightness Setup Server (0x1301) */ +const struct bt_mesh_model_op light_lightness_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET, 2, light_lightness_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET_UNACK, 2, light_lightness_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET, 4, light_lightness_range_set }, + { BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET_UNACK, 4, light_lightness_range_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light CTL Server (0x1303) */ +const struct bt_mesh_model_op light_ctl_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_CTL_GET, 0, light_ctl_get }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_SET, 7, light_ctl_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_SET_UNACK, 7, light_ctl_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET, 0, light_ctl_get }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET, 0, light_ctl_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light CTL Setup Server (0x1304) */ +const struct bt_mesh_model_op light_ctl_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET, 6, light_ctl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET_UNACK, 6, light_ctl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET, 4, light_ctl_temp_range_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET_UNACK, 4, light_ctl_temp_range_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light CTL Temperature Server (0x1306) */ +const struct bt_mesh_model_op light_ctl_temp_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET, 0, light_ctl_get }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET, 5, light_ctl_temp_set }, + { BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_SET_UNACK, 5, light_ctl_temp_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light HSL Server (0x1307) */ +const struct bt_mesh_model_op light_hsl_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_HSL_GET, 0, light_hsl_get }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SET, 7, light_hsl_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SET_UNACK, 7, light_hsl_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_TARGET_GET, 0, light_hsl_get }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_GET, 0, light_hsl_get }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_GET, 0, light_hsl_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light HSL Setup Server (0x1308) */ +const struct bt_mesh_model_op light_hsl_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET, 6, light_hsl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_DEFAULT_SET_UNACK, 6, light_hsl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET, 8, light_hsl_range_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_RANGE_SET_UNACK, 8, light_hsl_range_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light HSL Hue Server (0x130A) */ +const struct bt_mesh_model_op light_hsl_hue_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_GET, 0, light_hsl_get }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET, 3, light_hsl_hue_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_SET_UNACK, 3, light_hsl_hue_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light HSL Saturation Server (0x130B) */ +const struct bt_mesh_model_op light_hsl_sat_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_GET, 0, light_hsl_get }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET, 3, light_hsl_sat_set }, + { BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_SET_UNACK, 3, light_hsl_sat_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light xyL Server (0x130C) */ +const struct bt_mesh_model_op light_xyl_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_XYL_GET, 0, light_xyl_get }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_SET, 7, light_xyl_set }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_SET_UNACK, 7, light_xyl_set }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_TARGET_GET, 0, light_xyl_get }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_GET, 0, light_xyl_get }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_GET, 0, light_xyl_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light xyL Setup Server (0x130D) */ +const struct bt_mesh_model_op light_xyl_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET, 6, light_xyl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_DEFAULT_SET_UNACK, 6, light_xyl_default_set }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET, 8, light_xyl_range_set }, + { BLE_MESH_MODEL_OP_LIGHT_XYL_RANGE_SET_UNACK, 8, light_xyl_range_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light LC Server (0x130F) */ +const struct bt_mesh_model_op light_lc_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_GET, 0, light_lc_get }, + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET, 1, light_lc_mode_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_MODE_SET_UNACK, 1, light_lc_mode_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_GET, 0, light_lc_get }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET, 1, light_lc_om_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_OM_SET_UNACK, 1, light_lc_om_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_GET, 0, light_lc_get }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET, 2, light_lc_light_onoff_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_SET_UNACK, 2, light_lc_light_onoff_set }, + { BLE_MESH_MODEL_OP_SENSOR_STATUS, 3, light_lc_sensor_status }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Light LC Setup Server (0x1310) */ +const struct bt_mesh_model_op light_lc_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_GET, 2, light_lc_prop_get }, + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET, 3, light_lc_prop_set }, + { BLE_MESH_MODEL_OP_LIGHT_LC_PROPERTY_SET_UNACK, 3, light_lc_prop_set }, + BLE_MESH_MODEL_OP_END, +}; + +static int light_server_init(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Light Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light Lightness State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->actual_transition.timer.work); + bt_mesh_server_alloc_ctx(&srv->linear_transition.timer.work); + k_delayed_work_init(&srv->actual_transition.timer, light_lightness_actual_work_handler); + k_delayed_work_init(&srv->linear_transition.timer, light_lightness_linear_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: { + struct bt_mesh_light_lightness_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light Lightness State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_ctl_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: { + struct bt_mesh_light_ctl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_ctl_temp_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_hsl_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV: { + struct bt_mesh_light_hsl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_hsl_hue_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_hsl_sat_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light xyL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_xyl_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV: { + struct bt_mesh_light_xyl_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light xyL State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: { + struct bt_mesh_light_lc_srv *srv = model->user_data; + if (srv->lc == NULL) { + BT_ERR("%s, NULL Light LC State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, light_lc_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV: { + struct bt_mesh_light_lc_setup_srv *srv = model->user_data; + if (srv->lc == NULL) { + BT_ERR("%s, NULL Light LC State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + default: + BT_WARN("%s, Unknown Light Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_light_server_mutex_new(); + + return 0; +} + +int bt_mesh_light_lightness_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light Lightness Server has no publication support", __func__); + return -EINVAL; + } + + /* When this model is present on an Element, the corresponding Light Lightness + * Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) == NULL) { + BT_WARN("%s, Light Lightness Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return light_server_init(model); +} + +int bt_mesh_light_lightness_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return light_server_init(model); +} + +int bt_mesh_light_ctl_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Server has no publication support", __func__); + return -EINVAL; + } + + /** + * When this model is present on an Element, the corresponding Light CTL + * Temperature Server model and the corresponding Light CTL Setup Server + * model shall also be present. + * The model requires two elements: the main element and the Temperature + * element. The Temperature element contains the corresponding Light CTL + * Temperature Server model. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) == NULL) { + BT_WARN("%s, Light CTL Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + if (bt_mesh_elem_count() < 2) { + BT_WARN("%s, Light CTL Server requires two elements", __func__); + /* Just give a warning here, continue with the initialization */ + } + return light_server_init(model); +} + +int bt_mesh_light_ctl_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return light_server_init(model); +} + +int bt_mesh_light_ctl_temp_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Temperature Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_init(model); +} + +int bt_mesh_light_hsl_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Server has no publication support", __func__); + return -EINVAL; + } + + /** + * When this model is present on an Element, the corresponding Light HSL Hue + * Server model and the corresponding Light HSL Saturation Server model and + * the corresponding Light HSL Setup Server model shall also be present. + * The model requires three elements: the main element and the Hue element + * and the Saturation element. The Hue element contains the corresponding + * Light HSL Hue Server model, and the Saturation element contains the corr- + * esponding Light HSL Saturation Server model. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV) == NULL) { + BT_WARN("%s, Light HSL Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + if (bt_mesh_elem_count() < 3) { + BT_WARN("%s, Light HSL Server requires three elements", __func__); + /* Just give a warning here, continue with the initialization */ + } + return light_server_init(model); +} + +int bt_mesh_light_hsl_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return light_server_init(model); +} + +int bt_mesh_light_hsl_hue_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Hue Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_init(model); +} + +int bt_mesh_light_hsl_sat_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Saturation Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_init(model); +} + +int bt_mesh_light_xyl_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light xyL Server has no publication support", __func__); + return -EINVAL; + } + + /** + * When this model is present on an Element, the corresponding Light xyL + * Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV) == NULL) { + BT_WARN("%s, Light xyL Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return light_server_init(model); +} + +int bt_mesh_light_xyl_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + return light_server_init(model); +} + +int bt_mesh_light_lc_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_init(model); +} + +int bt_mesh_light_lc_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Setup Server has no publication support", __func__); + return -EINVAL; + } + + /** + * When this model is present on an Element, the corresponding Light LC + * Setup Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV) == NULL) { + BT_WARN("%s, Light LC Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return light_server_init(model); +} + +static int light_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Light Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: { + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light Lightness State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->actual_transition.timer.work); + bt_mesh_server_free_ctx(&srv->linear_transition.timer.work); + k_delayed_work_free(&srv->actual_transition.timer); + k_delayed_work_free(&srv->linear_transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: { + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: { + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light CTL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV: { + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV: { + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV: { + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light HSL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV: { + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Light xyL State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + case BLE_MESH_MODEL_ID_LIGHT_LC_SRV: { + struct bt_mesh_light_lc_srv *srv = model->user_data; + if (srv->lc == NULL) { + BT_ERR("%s, NULL Light LC State", __func__); + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Light Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_light_server_mutex_free(); + + return 0; +} + +int bt_mesh_light_lightness_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light Lightness Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_lightness_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_ctl_temp_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light CTL Temperature Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_hue_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Hue Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_hsl_sat_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light HSL Saturation Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_xyl_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light xyL Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_xyl_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return light_server_deinit(model); +} + +int bt_mesh_light_lc_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} + +int bt_mesh_light_lc_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Light LC Setup Server has no publication support", __func__); + return -EINVAL; + } + + return light_server_deinit(model); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c b/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c new file mode 100644 index 000000000..5cbcb1bdc --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/sensor_server.c @@ -0,0 +1,1140 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc_ble_mesh_sensor_model.h" + +#include "access.h" +#include "transport.h" +#include "model_opcode.h" +#include "state_transition.h" +#include "device_property.h" + +static void update_sensor_periodic_pub(struct bt_mesh_model *model, u16_t prop_id); + +/* message handlers (Start) */ + +/* Sensor Server & Sensor Setup Server message handlers */ +static void send_sensor_descriptor_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t prop_id, bool get_all) +{ + struct bt_mesh_sensor_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct net_buf_simple *msg = NULL; + u16_t total_len = 5U; + int i; + + msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS); + + if (get_all == true) { + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) { + total_len += SENSOR_DESCRIPTOR_LEN; + if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) { + /* Add this in case the message is too long */ + break; + } + net_buf_simple_add_le16(msg, state->sensor_property_id); + net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) | + (state->descriptor.negative_tolerance << 12) | + (state->descriptor.positive_tolerance)); + net_buf_simple_add_u8(msg, state->descriptor.measure_period); + net_buf_simple_add_u8(msg, state->descriptor.update_interval); + } + } + } else { + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + net_buf_simple_add_le16(msg, state->sensor_property_id); + net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) | + (state->descriptor.negative_tolerance << 12) | + (state->descriptor.positive_tolerance)); + net_buf_simple_add_u8(msg, state->descriptor.measure_period); + net_buf_simple_add_u8(msg, state->descriptor.update_interval); + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + net_buf_simple_add_le16(msg, prop_id); + } + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; +} + +static void send_sensor_data_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t prop_id, bool get_all) +{ + struct bt_mesh_sensor_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct net_buf_simple *msg = NULL; + u16_t total_len = 5U; + int i; + + msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_STATUS); + + if (get_all == true) { + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) { + u8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ? + SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN; + total_len += (mpid_len + (state->sensor_data.raw_value ? + state->sensor_data.raw_value->len : 0)); + if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) { + /* Add this in case the message is too long */ + break; + } + if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) { + u16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) | + ((state->sensor_data.length & BIT_MASK(4)) << 1) | state->sensor_data.format; + net_buf_simple_add_le16(msg, mpid); + } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) { + u8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format; + net_buf_simple_add_u8(msg, mpid); + net_buf_simple_add_le16(msg, state->sensor_property_id); + } + if (state->sensor_data.raw_value) { + net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data, state->sensor_data.raw_value->len); + } + } + } + } else { + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) { + u16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) | + ((state->sensor_data.length & BIT_MASK(4)) << 1) | + state->sensor_data.format; + net_buf_simple_add_le16(msg, mpid); + } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) { + u8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format; + net_buf_simple_add_u8(msg, mpid); + net_buf_simple_add_le16(msg, state->sensor_property_id); + } + if (state->sensor_data.raw_value) { + net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data, + state->sensor_data.raw_value->len); + } + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + u8_t mpid = (SENSOR_DATA_ZERO_LEN << 1) | SENSOR_DATA_FORMAT_B; + net_buf_simple_add_u8(msg, mpid); + net_buf_simple_add_le16(msg, prop_id); + } + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; +} + +static void send_sensor_cadence_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t prop_id, bool publish) +{ + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct net_buf_simple *msg = NULL; + u16_t length = 0U; + int i; + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id && state->cadence) { + length = SENSOR_PROPERTY_ID_LEN + 1 + 1; + if (state->cadence->trigger_delta_down) { + if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { + length += state->cadence->trigger_delta_down->len; + } else { + length += SENSOR_STATUS_TRIGGER_UINT16_LEN; + } + } + if (state->cadence->trigger_delta_up) { + if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { + length += state->cadence->trigger_delta_up->len; + } else { + length += SENSOR_STATUS_TRIGGER_UINT16_LEN; + } + } + if (state->cadence->fast_cadence_low) { + length += state->cadence->fast_cadence_low->len; + } + if (state->cadence->fast_cadence_high) { + length += state->cadence->fast_cadence_high->len; + } + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + length = SENSOR_PROPERTY_ID_LEN; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 1 + length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS); + net_buf_simple_add_le16(msg, prop_id); + if (i != srv->state_count) { + if (state->cadence) { + net_buf_simple_add_u8(msg, (state->cadence->trigger_type << 7) | + state->cadence->period_divisor); + if (state->cadence->trigger_delta_down) { + if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { + net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data, + state->cadence->trigger_delta_down->len); + } else { + net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data, + SENSOR_STATUS_TRIGGER_UINT16_LEN); + } + } + if (state->cadence->trigger_delta_up) { + if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { + net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data, + state->cadence->trigger_delta_up->len); + } else { + net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data, + SENSOR_STATUS_TRIGGER_UINT16_LEN); + } + } + net_buf_simple_add_u8(msg, state->cadence->min_interval); + if (state->cadence->fast_cadence_low) { + net_buf_simple_add_mem(msg, state->cadence->fast_cadence_low->data, + state->cadence->fast_cadence_low->len); + } + if (state->cadence->fast_cadence_high) { + net_buf_simple_add_mem(msg, state->cadence->fast_cadence_high->data, + state->cadence->fast_cadence_high->len); + } + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void send_sensor_settings_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u16_t prop_id) +{ + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct sensor_setting *item = NULL; + struct net_buf_simple *msg = NULL; + u16_t total_len = 7U; + int i, j; + + msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS); + net_buf_simple_add_le16(msg, prop_id); + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id && + state->setting_count && state->settings) { + for (j = 0; j < state->setting_count; j++) { + item = &state->settings[j]; + if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID) { + total_len += SENSOR_SETTING_PROPERTY_ID_LEN; + if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) { + /* Add this in case the message is too long */ + break; + } + net_buf_simple_add_le16(msg, item->property_id); + } + } + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; +} + +static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model, + u16_t prop_id, u16_t set_prop_id) +{ + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct sensor_setting *item = NULL; + int i, j; + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id && + state->setting_count && state->settings) { + for (j = 0; j < state->setting_count; j++) { + item = &state->settings[j]; + if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID && + item->property_id == set_prop_id) { + return item; + } + } + } + } + + return NULL; +} + +static void send_sensor_setting_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, u16_t prop_id, + u16_t set_prop_id, bool publish) +{ + struct sensor_setting *item = NULL; + struct net_buf_simple *msg = NULL; + u16_t length = 0U; + + item = find_sensor_setting(model, prop_id, set_prop_id); + if (item) { + length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN + + SENSOR_SETTING_ACCESS_LEN + (item->raw ? item->raw->len : 0); + } else { + /* If the message is sent as a response to the Sensor Setting Get message or + * a Sensor Setting Set message with an unknown Sensor Property ID field or + * an unknown Sensor Setting Property ID field, the Sensor Setting Access + * field and the Sensor Setting Raw field shall be omitted. + */ + BT_WARN("%s, Sensor Setting not found, 0x%04x, 0x%04x", __func__, prop_id, set_prop_id); + length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 1 + length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS); + net_buf_simple_add_le16(msg, prop_id); + net_buf_simple_add_le16(msg, set_prop_id); + if (item) { + /** + * If the message is sent as a response to the Sensor Setting Set message with + * a Sensor Setting Property ID field that identifies an existing Sensor Setting, + * and the value of the Sensor Setting Access state is 0x01 (can be read), the + * Sensor Setting Property ID field shall be set to the value of the Sensor + * Setting Property ID field of the incoming message, the Sensor Setting Access + * field shall be set to the value of the Sensor Setting Access state field, and + * the Sensor Setting Raw field shall be omitted. + * + * TODO: What if the Sensor Setting Access is Prohibited? + */ + net_buf_simple_add_u8(msg, item->access); + if (ctx->recv_op != BLE_MESH_MODEL_OP_SENSOR_SETTING_SET || + item->access == SENSOR_SETTING_ACCESS_READ_WRITE) { + if (item->raw) { + net_buf_simple_add_mem(msg, item->raw->data, item->raw->len); + } + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void send_sensor_column_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, u16_t prop_id) +{ + struct bt_mesh_sensor_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct net_buf_simple *msg = NULL; + bool optional = false; + u16_t length = 0U; + int i; + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + length = SENSOR_PROPERTY_ID_LEN; + if (state->series_column.raw_value_x) { + length += state->series_column.raw_value_x->len; + } + /** + * TODO: column width & raw value y in Sensor Column Status are optional, + * here we need to add some conditions to decide whether put these two + * in the status message. + */ + if (optional) { + if (state->series_column.column_width) { + length += state->series_column.column_width->len; + } + if (state->series_column.raw_value_y) { + length += state->series_column.raw_value_y->len; + } + } + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + length = SENSOR_PROPERTY_ID_LEN; + } + + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + /** + * TODO: Sensor Column Get contains Raw Value X which identifies a column, + * we need to use this value to decide the column. + */ + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS); + net_buf_simple_add_le16(msg, prop_id); + if (i != srv->state_count) { + if (state->series_column.raw_value_x) { + net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data, + state->series_column.raw_value_x->len); + } + if (optional) { + if (state->series_column.column_width) { + net_buf_simple_add_mem(msg, state->series_column.column_width->data, + state->series_column.column_width->len); + } + if (state->series_column.raw_value_y) { + net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data, + state->series_column.raw_value_y->len); + } + } + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; +} + +static void send_sensor_series_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, u16_t prop_id) +{ + struct bt_mesh_sensor_srv *srv = model->user_data; + struct bt_mesh_sensor_state *state = NULL; + struct net_buf_simple *msg = NULL; + bool optional = false; + u16_t length = 0U; + int i; + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + length = SENSOR_PROPERTY_ID_LEN; + /* TODO: raw value x, column width & raw value y in Sensor Series + * Status are optional, here we need to add some conditions to + * decide whether put these three in the status message. + */ + if (optional) { + if (state->series_column.raw_value_x) { + length += state->series_column.raw_value_x->len; + } + if (state->series_column.column_width) { + length += state->series_column.column_width->len; + } + if (state->series_column.raw_value_y) { + length += state->series_column.raw_value_y->len; + } + } + break; + } + } + if (i == srv->state_count) { + BT_WARN("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + length = SENSOR_PROPERTY_ID_LEN; + } + + msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + + /** + * TODO: Sensor Series Get may contain Raw Value X1 and Raw Value X2 which + * identifies a starting column and a ending column, we need to use these + * values to decide the columns. + */ + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS); + net_buf_simple_add_le16(msg, prop_id); + if (i != srv->state_count) { + if (optional) { + if (state->series_column.raw_value_x) { + net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data, + state->series_column.raw_value_x->len); + } + if (state->series_column.column_width) { + net_buf_simple_add_mem(msg, state->series_column.column_width->data, + state->series_column.column_width->len); + } + if (state->series_column.raw_value_y) { + net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data, + state->series_column.raw_value_y->len); + } + } + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + return; +} + +static void sensor_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + u16_t set_prop_id = INVALID_SENSOR_PROPERTY_ID; + u16_t prop_id = INVALID_SENSOR_PROPERTY_ID; + + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: + case BLE_MESH_MODEL_OP_SENSOR_GET: + case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: + case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: { + struct bt_mesh_sensor_srv *srv = model->user_data; + if (srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid Sensor Server state", __func__); + return; + } + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET || + ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_GET) { + bool get_all = buf->len ? false : true; + if (buf->len) { + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + } + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET) { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_descriptor_get.op_en = !get_all, + .sensor_descriptor_get.id = prop_id, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_descriptor_status(model, ctx, prop_id, get_all); + } + } else { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_get.op_en = !get_all, + .sensor_get.id = prop_id, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_data_status(model, ctx, prop_id, get_all); + } + } + } else { + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET) { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_column_get.id = prop_id, + .sensor_column_get.raw_x = buf, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_column_status(model, ctx, buf, prop_id); + } + } else { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_series_get.id = prop_id, + .sensor_series_get.raw = buf, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_series_status(model, ctx, buf, prop_id); + } + } + } + return; + } + case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: + case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: + case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: { + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + if (srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid Sensor Setup Server state", __func__); + return; + } + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET || + ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET) { + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET) { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_cadence_get.id = prop_id, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_cadence_status(model, ctx, prop_id, false); + } + } else { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_settings_get.id = prop_id, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_settings_status(model, ctx, prop_id); + } + } + } else { + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + set_prop_id = net_buf_simple_pull_le16(buf); + if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Setting Property ID 0x0000", __func__); + return; + } + + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_get_msg_t get = { + .sensor_setting_get.id = prop_id, + .sensor_setting_get.setting_id = set_prop_id, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + } else { + send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false); + } + } + return; + } + default: + BT_WARN("%s, Unknown Sensor Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +static void sensor_cadence_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + bt_mesh_sensor_server_state_change_t change = {0}; + struct bt_mesh_sensor_state *state = NULL; + struct bt_mesh_model *sensor_model = NULL; + struct bt_mesh_elem *element = NULL; + u16_t prop_id = 0U, trigger_len = 0U; + u8_t val = 0U, divisor = 0U; + int i; + + if (srv == NULL || srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_set_msg_t set = { + .sensor_cadence_set.id = prop_id, + .sensor_cadence_set.cadence = buf, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + break; + } + } + if (i == srv->state_count || state->cadence == NULL) { + /* When the message is sent as a response to the Sensor Cadence Get message or + * a Sensor Cadence Set message with an unknown Property ID field or the Sensor + * Server does not support the Sensor Cadence state for the sensor referred by + * the Property ID, the following fields shall be omitted: + * • Fast Cadence Period Divisor + * • Status Trigger Type + * • Status Trigger Delta Down + * • Status Trigger Delta Up + * • Status Min Interval + * • Fast Cadence Low + * • Fast Cadence High + */ + send_sensor_cadence_status(model, ctx, prop_id, false); + return; + } + + val = net_buf_simple_pull_u8(buf); + divisor = val & BIT_MASK(7); + if (divisor > SENSOR_PERIOD_DIVISOR_MAX_VALUE) { + BT_ERR("%s, Prohibited Fast Cadence Period Divisor 0x%02x", __func__, divisor); + return; + } + state->cadence->period_divisor = divisor; + state->cadence->trigger_type = (val >> 7) & BIT_MASK(1); + + if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) { + trigger_len = bt_mesh_get_dev_prop_len(prop_id); + } else { + trigger_len = SENSOR_STATUS_TRIGGER_UINT16_LEN; + } + if (buf->len < (trigger_len << 1) + SENSOR_STATUS_MIN_INTERVAL_LEN) { + BT_ERR("%s, Invalid Sensor Cadence Set length %d, trigger type %d", + __func__, buf->len + 3, state->cadence->trigger_type); + return; + } + + if (state->cadence->trigger_delta_down) { + net_buf_simple_reset(state->cadence->trigger_delta_down); + net_buf_simple_add_mem(state->cadence->trigger_delta_down, buf->data, trigger_len); + net_buf_simple_pull_mem(buf, trigger_len); + } + if (state->cadence->trigger_delta_up) { + net_buf_simple_reset(state->cadence->trigger_delta_up); + net_buf_simple_add_mem(state->cadence->trigger_delta_up, buf->data, trigger_len); + net_buf_simple_pull_mem(buf, trigger_len); + } + + /* The valid range for the Status Min Interval is 0–26 and other values are Prohibited. */ + val = net_buf_simple_pull_u8(buf); + if (val > SENSOR_STATUS_MIN_INTERVAL_MAX) { + BT_ERR("%s, Invalid Status Min Interval %d", __func__, val); + return; + } + state->cadence->min_interval = val; + + if (buf->len % 2) { + BT_ERR("%s, Different length of Fast Cadence Low & High, length %d", __func__, buf->len); + return; + } + if (buf->len) { + u8_t range_len = buf->len / 2; + if (state->cadence->fast_cadence_low) { + net_buf_simple_reset(state->cadence->fast_cadence_low); + net_buf_simple_add_mem(state->cadence->fast_cadence_low, buf->data, range_len); + net_buf_simple_pull_mem(buf, range_len); + } + if (state->cadence->fast_cadence_high) { + net_buf_simple_reset(state->cadence->fast_cadence_high); + net_buf_simple_add_mem(state->cadence->fast_cadence_high, buf->data, range_len); + net_buf_simple_pull_mem(buf, range_len); + } + } + + change.sensor_cadence_set.id = prop_id; + change.sensor_cadence_set.period_divisor = state->cadence->period_divisor; + change.sensor_cadence_set.trigger_type = state->cadence->trigger_type; + change.sensor_cadence_set.trigger_delta_down = state->cadence->trigger_delta_down; + change.sensor_cadence_set.trigger_delta_up = state->cadence->trigger_delta_up; + change.sensor_cadence_set.min_interval = state->cadence->min_interval; + change.sensor_cadence_set.fast_cadence_low = state->cadence->fast_cadence_low; + change.sensor_cadence_set.fast_cadence_high = state->cadence->fast_cadence_high; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET) { + send_sensor_cadence_status(model, ctx, prop_id, false); + } + send_sensor_cadence_status(model, ctx, prop_id, true); + + /* Try to find the corresponding Sensor Server Model */ + element = bt_mesh_model_elem(model); + sensor_model = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SRV); + if (sensor_model == NULL) { + BT_WARN("%s, Sensor Server Model does not exist in the element", __func__); + return; + } + + /** + * Based on the configured Sensor Cadence state, change Periodic Sensor + * status publication mechanism. + */ + update_sensor_periodic_pub(sensor_model, prop_id); + return; +} + +static void update_sensor_periodic_pub(struct bt_mesh_model *model, u16_t prop_id) +{ + struct bt_mesh_sensor_state *state = NULL; + struct bt_mesh_sensor_srv *srv = NULL; + int i; + + if (model->id != BLE_MESH_MODEL_ID_SENSOR_SRV) { + BT_ERR("%s, Not a Sensor Server Model", __func__); + return; + } + + srv = (struct bt_mesh_sensor_srv *)model->user_data; + if (srv == NULL || srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + for (i = 0; i < srv->state_count; i++) { + state = &srv->states[i]; + if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID && + state->sensor_property_id == prop_id) { + break; + } + } + if (i == srv->state_count) { + BT_ERR("%s, Sensor Property ID 0x%04x does not exist", __func__, prop_id); + return; + } + + if (state->cadence == NULL) { + BT_WARN("%s, Sensor Cadence state does not exist", __func__); + return; + } + + /** + * Currently when the device receives a Sensor Cadence Set message, + * a event will be callback to the application layer, and users can + * change the Sensor Data publication period in the event. And this + * is exactly what we do for the BQB test. + */ +} + +static void sensor_setting_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + bt_mesh_sensor_server_state_change_t change = {0}; + struct sensor_setting *item = NULL; + u16_t prop_id = 0U, set_prop_id = 0U; + + if (srv == NULL || srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + prop_id = net_buf_simple_pull_le16(buf); + if (prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Property ID 0x0000", __func__); + return; + } + + set_prop_id = net_buf_simple_pull_le16(buf); + if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Prohibited Sensor Setting Property ID 0x0000", __func__); + return; + } + + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_sensor_server_recv_set_msg_t set = { + .sensor_setting_set.id = prop_id, + .sensor_setting_set.setting_id = set_prop_id, + .sensor_setting_set.raw = buf, + }; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + item = find_sensor_setting(model, prop_id, set_prop_id); + if (item) { + if (item->access == SENSOR_SETTING_ACCESS_READ_WRITE && item->raw) { + net_buf_simple_reset(item->raw); + net_buf_simple_add_mem(item->raw, buf->data, + MIN(buf->len, item->raw->size)); + + change.sensor_setting_set.id = prop_id; + change.sensor_setting_set.setting_id = set_prop_id; + change.sensor_setting_set.value = item->raw; + bt_mesh_sensor_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + } + + if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTING_SET) { + send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false); + } + if (item) { + send_sensor_setting_status(model, ctx, prop_id, set_prop_id, true); + } + + return; +} + +/* message handlers (End) */ + +/* Mapping of message handlers for Sensor Server (0x1100) */ +const struct bt_mesh_model_op sensor_srv_op[] = { + { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, 0, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_GET, 0, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET, 2, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, 2, sensor_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Sensor Setup Server (0x1101) */ +const struct bt_mesh_model_op sensor_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET, 2, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET, 4, sensor_cadence_set }, + { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK, 4, sensor_cadence_set }, + { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET, 2, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_GET, 4, sensor_get }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET, 4, sensor_setting_set }, + { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK, 4, sensor_setting_set }, + BLE_MESH_MODEL_OP_END, +}; + +static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start, + const u8_t state_count) +{ + struct bt_mesh_sensor_state *state = NULL; + struct sensor_setting *setting = NULL; + int i, j; + + for (i = 0; i < state_count; i++) { + state = &state_start[i]; + if (state->sensor_property_id == INVALID_SENSOR_PROPERTY_ID) { + BT_ERR("%s, Invalid Sensor Property ID 0x%04x", __func__, state->sensor_property_id); + return -EINVAL; + } + /* Check if the same Sensor Property ID exists */ + for (int k = i + 1; k < state_count; k++) { + if (state->sensor_property_id == state_start[k].sensor_property_id) { + BT_ERR("%s, Same Sensor Property ID 0x%04x exists", __func__, state->sensor_property_id); + return -EINVAL; + } + } + if (state->setting_count && state->settings) { + for (j = 0; j < state->setting_count; j++) { + setting = &state->settings[j]; + if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) { + BT_ERR("%s, Invalid Sensor Setting state", __func__); + return -EINVAL; + } + /* Check if the same Sensor Setting Property ID exists */ + for (int k = j + 1; k < state->setting_count; k++) { + if (setting->property_id == state->settings[k].property_id) { + BT_ERR("%s, Same Sensor Setting Property ID 0x%04x exists", __func__, setting->property_id); + return -EINVAL; + } + } + } + } + if (state->cadence) { + if (state->cadence->trigger_delta_down == NULL || + state->cadence->trigger_delta_up == NULL || + state->cadence->fast_cadence_low == NULL || + state->cadence->fast_cadence_high == NULL) { + BT_ERR("%s, Invalid Sensor Cadence state", __func__); + return -EINVAL; + } + } + if (state->sensor_data.raw_value == NULL) { + BT_ERR("%s, Invalid Sensor Data state", __func__); + return -EINVAL; + } + } + + return 0; +} + +static int sensor_server_init(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Sensor Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_SENSOR_SRV: { + struct bt_mesh_sensor_srv *srv = model->user_data; + if (srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid Sensor state, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + if (check_sensor_server_init(srv->states, srv->state_count)) { + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: { + struct bt_mesh_sensor_setup_srv *srv = model->user_data; + if (srv->state_count == 0U || srv->states == NULL) { + BT_ERR("%s, Invalid Sensor state, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + if (check_sensor_server_init(srv->states, srv->state_count)) { + return -EINVAL; + } + srv->model = model; + break; + } + default: + BT_WARN("%s, Unknown Sensor Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + return 0; +} + +int bt_mesh_sensor_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Server has no publication support", __func__); + return -EINVAL; + } + + /* When this model is present on an element, the corresponding Sensor Setup + * Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV) == NULL) { + BT_WARN("%s, Sensor Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return sensor_server_init(model); +} + +int bt_mesh_sensor_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Setup Server has no publication support", __func__); + return -EINVAL; + } + + return sensor_server_init(model); +} + +static int sensor_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Sensor Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + return 0; +} + +int bt_mesh_sensor_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Server has no publication support", __func__); + return -EINVAL; + } + + return sensor_server_deinit(model); +} + +int bt_mesh_sensor_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Sensor Setup Server has no publication support", __func__); + return -EINVAL; + } + + return sensor_server_deinit(model); +} diff --git a/components/bt/esp_ble_mesh/mesh_models/server/server_common.c b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c new file mode 100644 index 000000000..ee698e2d7 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/server_common.c @@ -0,0 +1,258 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "mesh.h" +#include "access.h" +#include "mesh_common.h" +#include "generic_server.h" +#include "lighting_server.h" + +/** + * According to Mesh Model Spec: + * If the Transition Time field is not present and the Generic Default Transition + * Time state is supported, the Generic Default Transition Time state shall be + * used. Otherwise the transition shall be instantaneous. + */ +#define INSTANTANEOUS_TRANS_TIME 0 + +u8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model) +{ + /** + * 1. If a Generic Default Transition Time Server model is present on the + * main element of the model, that model instance shall be used. + * 2. If a Generic Default Transition Time Server model is not present on + * the main element of the model, then the Generic Default Transition + * Time Server model instance that is present on the element with the + * largest address that is smaller than the address of the main element + * of the node shall be used; if no model instance is present on any + * element with an address smaller than the address of the main element, + * then the Generic Default Transition Time Server is not supported. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + struct bt_mesh_gen_def_trans_time_srv *state = NULL; + u16_t primary_addr = bt_mesh_primary_addr(); + struct bt_mesh_model *srv = NULL; + + for (u16_t addr = element->addr; addr >= primary_addr; addr--) { + element = bt_mesh_elem_find(addr); + if (element) { + srv = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV); + if (srv) { + state = (struct bt_mesh_gen_def_trans_time_srv *)srv->user_data; + if (state) { + return state->state.trans_time; + } + } + } + } + + return INSTANTANEOUS_TRANS_TIME; +} + +int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, u8_t *trans_time) +{ + struct bt_mesh_light_lc_srv *srv = NULL; + u32_t value = 0U; + + if (model == NULL || trans_time == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (model->id != BLE_MESH_MODEL_ID_LIGHT_LC_SRV) { + BT_ERR("%s, Not a Light LC Server", __func__); + return -EINVAL; + } + + srv = (struct bt_mesh_light_lc_srv *)model->user_data; + if (srv == NULL) { + BT_ERR("%s, Invalid Light LC Server user_data", __func__); + return -EINVAL; + } + + /** + * 1. Set transition time to 0x54 for BQB test case MESH/SR/LLC/BV-04-C. + * Light LC Property Set: 0x3C, 0x004E20 -> Light LC Time Run On + * Light LC Property Set: 0x37, 0x004E20 -> Light LC Time Fade On + * Light LC Property Set: 0x39, 0x004E20 -> Light LC Time Fade Standby Manual + * + * 2. Set transition time to 0x0 for BQB test case MESH/SR/LLC/BV-08-C. + * + * TODO: Based on Light LC state and choose property property value as the + * transition time. Currently directly use Light LC Time Run On property value. + * Unit: Millisecond, range: [0, 16777214(0xFFFFFE)] + */ + value = srv->lc->prop_state.time_run_on & 0xFFFFFF; + + /** + * Convert value into Default Transition Time state format. + * 0b00: 0 ~ 6.2s, 100 millisecond step resolution + * 0b01: 0 ~ 62s, 1 second step resolution + * 0b10: 0 ~ 620s, 10 seconds step resolution + * 0b11: 0 ~ 620m, 10 minutes step resolution + */ + if (value <= 6200) { + *trans_time = (0 << 6) | (value / 100); + } else if (value <= 62000) { + *trans_time = (1 << 6) | (value / 1000); + } else if (value <= 620000) { + *trans_time = (2 << 6) | (value / 10000); + } else { + *trans_time = (3 << 6) | (value / 600000); + } + + return 0; +} + +int bt_mesh_server_get_optional(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf, + u8_t *trans_time, u8_t *delay, + bool *optional) +{ + if (model == NULL || buf == NULL || trans_time == NULL || + delay == NULL || optional == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + if (buf->len != 0x00 && buf->len != 0x02) { + BT_ERR("%s, Invalid optional message length %d", __func__, buf->len); + return -EINVAL; + } + + /* Currently we only get optional msg info which dst is set to a unicast address */ + if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) { + *trans_time = 0U; + *delay = 0U; + *optional = false; + return 0; + } + + /* No optional fields are available */ + if (buf->len == 0x00) { + if (model->id == BLE_MESH_MODEL_ID_LIGHT_LC_SRV) { + /** + * Both messages(i.e. Light LC OnOff Set/Set Unack) may optionally include + * a Transition Time field indicating the transition time to the target state. + * If the Transition Time is not included, the Light LC Server shall use + * its appropriate transition times defined by the Light LC Property states. + */ + if (bt_mesh_get_light_lc_trans_time(model, trans_time)) { + BT_ERR("%s, Failed to get Light LC transition time", __func__); + return -EIO; + } + } else { + *trans_time = bt_mesh_get_default_trans_time(model); + } + *delay = 0U; + *optional = false; + return 0; + } + + /* Optional fields are available */ + *trans_time = net_buf_simple_pull_u8(buf); + if ((*trans_time & 0x3F) == 0x3F) { + BT_ERR("%s, Invalid Transaction Number of Steps 0x3F", __func__); + return -EINVAL; + } + + *delay = net_buf_simple_pull_u8(buf); + *optional = true; + return 0; +} + +void bt_mesh_server_alloc_ctx(struct k_work *work) +{ + /** + * This function is used to allocate memory for storing "struct bt_mesh_msg_ctx" + * of the received messages, because some server models will callback the "struct + * bt_mesh_msg_ctx" info to the application layer after a certain delay. + * Here we use the allocated heap memory to store the "struct bt_mesh_msg_ctx". + */ + __ASSERT(work, "%s, Invalid parameter", __func__); + if (!work->_reserved) { + work->_reserved = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx)); + __ASSERT(work->_reserved, "%s, Failed to allocate memory", __func__); + } +} + +void bt_mesh_server_free_ctx(struct k_work *work) +{ + __ASSERT(work, "%s, Invalid parameter", __func__); + if (work->_reserved) { + bt_mesh_free(work->_reserved); + work->_reserved = NULL; + } +} + +bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last, + u8_t tid, u16_t src, u16_t dst, s64_t *now) +{ + *now = k_uptime_get(); + + /* Currently we only compare msg info which dst is set to a unicast address */ + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + return false; + } + + if (last->tid == tid && last->src == src && last->dst == dst && + (*now - last->timestamp <= K_SECONDS(6))) { + return true; + } + + return false; +} + +void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last, + u8_t tid, u16_t src, u16_t dst, s64_t *now) +{ + /* Currently we only update msg info which dst is set to a unicast address */ + if (!BLE_MESH_ADDR_IS_UNICAST(dst)) { + return; + } + + last->tid = tid; + last->src = src; + last->dst = dst; + last->timestamp = *now; + return; +} + +struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, u16_t msg_len) +{ + struct net_buf_simple *buf = NULL; + + if (model == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return NULL; + } + + if (model->pub == NULL || model->pub->msg == NULL || + model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + BT_DBG("%s, Model 0x%04x has no publication support", __func__, model->id); + return NULL; + } + + buf = model->pub->msg; + if (buf->size < msg_len) { + BT_ERR("%s, Too small publication msg size %d, model 0x%04x", + __func__, buf->size, model->id); + return NULL; + } + + return buf; +} \ No newline at end of file diff --git a/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c b/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c new file mode 100644 index 000000000..9aef2081c --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/state_binding.c @@ -0,0 +1,341 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "mesh_common.h" +#include "model_opcode.h" +#include "state_binding.h" +#include "state_transition.h" + +#define MINDIFF (2.25e-308) + +static float bt_mesh_sqrt(float square) +{ + float root = 0.0, last = 0.0, diff = 0.0; + + root = square / 3.0; + diff = 1; + + if (square <= 0) { + return 0; + } + + do { + last = root; + root = (root + square / root) / 2.0; + diff = root - last; + } while (diff > MINDIFF || diff < -MINDIFF); + + return root; +} + +static s32_t bt_mesh_ceiling(float num) +{ + s32_t inum = (s32_t)num; + + if (num == (float)inum) { + return inum; + } + + return inum + 1; +} + +u16_t bt_mesh_convert_lightness_actual_to_linear(u16_t actual) +{ + float tmp = ((float) actual / UINT16_MAX); + + return bt_mesh_ceiling(UINT16_MAX * tmp * tmp); +} + +u16_t bt_mesh_convert_lightness_linear_to_actual(u16_t linear) +{ + return (u16_t) (UINT16_MAX * bt_mesh_sqrt(((float) linear / UINT16_MAX))); +} + +s16_t bt_mesh_convert_temperature_to_gen_level(u16_t temp, u16_t min, u16_t max) +{ + float tmp = (temp - min) * UINT16_MAX / (max - min); + return (s16_t) (tmp + INT16_MIN); +} + +u16_t bt_mesh_covert_gen_level_to_temperature(s16_t level, u16_t min, u16_t max) +{ + float diff = (float) (max - min) / UINT16_MAX; + u16_t tmp = (u16_t) ((level - INT16_MIN) * diff); + return (u16_t) (min + tmp); +} + +s16_t bt_mesh_convert_hue_to_level(u16_t hue) +{ + return (s16_t) (hue + INT16_MIN); +} + +u16_t bt_mesh_convert_level_to_hue(s16_t level) +{ + return (u16_t) (level - INT16_MIN); +} + +s16_t bt_mesh_convert_saturation_to_level(u16_t saturation) +{ + return (s16_t) (saturation + INT16_MIN); +} + +u16_t bt_mesh_convert_level_to_saturation(s16_t level) +{ + return (u16_t) (level - INT16_MIN); +} + +int bt_mesh_update_binding_state(struct bt_mesh_model *model, + bt_mesh_server_state_type_t type, + bt_mesh_server_state_value_t *value) +{ + if (model == NULL || model->user_data == NULL || + value == NULL || type > BIND_STATE_MAX) { + BT_ERR("%s, Invalid parameter", __func__); + return -EINVAL; + } + + switch (type) { + case GENERIC_ONOFF_STATE: { + if (model->id != BLE_MESH_MODEL_ID_GEN_ONOFF_SRV) { + BT_ERR("%s, Not a Generic OnOff Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_gen_onoff_srv *srv = model->user_data; + bt_mesh_server_stop_transition(&srv->transition); + srv->state.onoff = value->gen_onoff.onoff; + gen_onoff_publish(model); + break; + } + case GENERIC_LEVEL_STATE: { + if (model->id != BLE_MESH_MODEL_ID_GEN_LEVEL_SRV) { + BT_ERR("%s, Not a Generic Level Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_gen_level_srv *srv = model->user_data; + bt_mesh_server_stop_transition(&srv->transition); + srv->state.level = value->gen_level.level; + gen_level_publish(model); + break; + } + case GENERIC_ONPOWERUP_STATE: { + if (model->id != BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV) { + BT_ERR("%s, Not a Generic Power OnOff Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_gen_power_onoff_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power OnOff Server state", __func__); + return -EINVAL; + } + + srv->state->onpowerup = value->gen_onpowerup.onpowerup; + gen_onpowerup_publish(model); + break; + } + case GENERIC_POWER_ACTUAL_STATE: { + if (model->id != BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) { + BT_ERR("%s, Not a Generic Power Level Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_gen_power_level_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Generic Power Level Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->power_actual = value->gen_power_actual.power; + /** + * Whenever the Generic Power Actual state is changed to a non-zero value + * as a result of a non-transactional message or a completed sequence of + * transactional messages, the value of the Generic Power Last state shall + * be set to the value of the Generic Power Actual state. + */ + if (srv->state->power_actual) { + srv->state->power_last = srv->state->power_actual; + } + gen_power_level_publish(model, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + break; + } + case LIGHT_LIGHTNESS_ACTUAL_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) { + BT_ERR("%s, Not a Light Lightness Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light Lightness Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->actual_transition); + srv->state->lightness_actual = value->light_lightness_actual.lightness; + /** + * Whenever the Light Lightness Actual state is changed with a non-transactional + * message or a completed sequence of transactional messages to a non-zero value, + * the value of the Light Lightness Last shall be set to the value of the Light + * Lightness Actual. + */ + if (srv->state->lightness_actual) { + srv->state->lightness_last = srv->state->lightness_actual; + } + light_lightness_publish(model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + break; + } + case LIGHT_LIGHTNESS_LINEAR_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) { + BT_ERR("%s, Not a Light Lightness Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_lightness_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light Lightness Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->linear_transition); + srv->state->lightness_linear = value->light_lightness_linear.lightness; + light_lightness_publish(model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + break; + } + case LIGHT_CTL_LIGHTNESS_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) { + BT_ERR("%s, Not a Light CTL Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_ctl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->lightness = value->light_ctl_lightness.lightness; + light_ctl_publish(model, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + break; + } + case LIGHT_CTL_TEMP_DELTA_UV_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV) { + BT_ERR("%s, Not a Light CTL Temperature Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_ctl_temp_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light CTL Temperature Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->temperature = value->light_ctl_temp_delta_uv.temperature; + srv->state->delta_uv = value->light_ctl_temp_delta_uv.delta_uv; + light_ctl_publish(model, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + break; + } + case LIGHT_HSL_LIGHTNESS_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_HSL_SRV) { + BT_ERR("%s, Not a Light HSL Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_hsl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->lightness = value->light_hsl_lightness.lightness; + light_hsl_publish(model, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + break; + } + case LIGHT_HSL_HUE_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV) { + BT_ERR("%s, Not a Light HSL Hue Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_hsl_hue_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Hue Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->hue = value->light_hsl_hue.hue; + light_hsl_publish(model, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + break; + } + case LIGHT_HSL_SATURATION_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV) { + BT_ERR("%s, Not a Light HSL Saturation Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_hsl_sat_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light HSL Saturation Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->saturation = value->light_hsl_saturation.saturation; + light_hsl_publish(model, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + break; + } + case LIGHT_XYL_LIGHTNESS_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_XYL_SRV) { + BT_ERR("%s, Not a Light xyL Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_xyl_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Light xyL Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->state->lightness = value->light_xyl_lightness.lightness; + light_xyl_publish(model, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + break; + } + case LIGHT_LC_LIGHT_ONOFF_STATE: { + if (model->id != BLE_MESH_MODEL_ID_LIGHT_LC_SRV) { + BT_ERR("%s, Not a Light LC Server Model, id 0x%04x", __func__, model->id); + return -EINVAL; + } + + struct bt_mesh_light_lc_srv *srv = model->user_data; + if (srv->lc == NULL) { + BT_ERR("%s, Invalid Light LC Server state", __func__); + return -EINVAL; + } + + bt_mesh_server_stop_transition(&srv->transition); + srv->lc->state.light_onoff = value->light_lc_light_onoff.onoff; + light_lc_publish(model, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + break; + } + default: + BT_WARN("%s, Unknown binding state type 0x%02x", __func__, type); + return -EINVAL; + } + + return 0; +} + diff --git a/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c b/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c new file mode 100644 index 000000000..56871faa8 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/state_transition.c @@ -0,0 +1,1020 @@ +/* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models + * + * Copyright (c) 2018 Vikrant More + * Additional Copyright (c) 2018 Espressif Systems (Shanghai) PTE LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "btc_ble_mesh_generic_model.h" +#include "btc_ble_mesh_lighting_model.h" +#include "btc_ble_mesh_time_scene_model.h" +#include "btc_ble_mesh_sensor_model.h" + +#include "model_opcode.h" +#include "state_transition.h" + +/* Function to calculate Remaining Time (Start) */ + +void bt_mesh_server_calc_remain_time(struct bt_mesh_state_transition *transition) +{ + u8_t steps = 0U, resolution = 0U; + s32_t duration_remainder = 0; + s64_t now = 0; + + if (transition->just_started) { + transition->remain_time = transition->trans_time; + } else { + now = k_uptime_get(); + duration_remainder = transition->total_duration - + (now - transition->start_timestamp); + if (duration_remainder > 620000) { + /* > 620 seconds -> resolution = 0b11 [10 minutes] */ + resolution = 0x03; + steps = duration_remainder / 600000; + } else if (duration_remainder > 62000) { + /* > 62 seconds -> resolution = 0b10 [10 seconds] */ + resolution = 0x02; + steps = duration_remainder / 10000; + } else if (duration_remainder > 6200) { + /* > 6.2 seconds -> resolution = 0b01 [1 seconds] */ + resolution = 0x01; + steps = duration_remainder / 1000; + } else if (duration_remainder > 0) { + /* <= 6.2 seconds -> resolution = 0b00 [100 ms] */ + resolution = 0x00; + steps = duration_remainder / 100; + } else { + resolution = 0x00; + steps = 0x00; + } + + transition->remain_time = (resolution << 6) | steps; + } +} + +/* Function to calculate Remaining Time (End) */ + +static void tt_values_calculator(struct bt_mesh_state_transition *transition) +{ + u8_t steps_multiplier = 0U, resolution = 0U; + + resolution = (transition->trans_time >> 6); + steps_multiplier = (transition->trans_time & 0x3F); + + switch (resolution) { + case 0: /* 100ms */ + transition->total_duration = steps_multiplier * 100; + break; + case 1: /* 1 second */ + transition->total_duration = steps_multiplier * 1000; + break; + case 2: /* 10 seconds */ + transition->total_duration = steps_multiplier * 10000; + break; + case 3: /* 10 minutes */ + transition->total_duration = steps_multiplier * 600000; + break; + } + + transition->counter = ((float) transition->total_duration / 100); + + if (transition->counter > BLE_MESH_DEVICE_SPECIFIC_RESOLUTION) { + transition->counter = BLE_MESH_DEVICE_SPECIFIC_RESOLUTION; + } +} + +static void transition_time_values(struct bt_mesh_state_transition *transition, + u8_t trans_time, u8_t delay) +{ + transition->trans_time = trans_time; + transition->delay = delay; + + if (trans_time == 0U) { + return; + } + + tt_values_calculator(transition); + transition->quo_tt = transition->total_duration / transition->counter; +} + +void generic_onoff_tt_values(struct bt_mesh_gen_onoff_srv *srv, + u8_t trans_time, u8_t delay) +{ + return transition_time_values(&srv->transition, trans_time, delay); +} + +void generic_level_tt_values(struct bt_mesh_gen_level_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_level = + ((float) (srv->state.level - srv->state.target_level) / srv->transition.counter); +} + +void generic_power_level_tt_values(struct bt_mesh_gen_power_level_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_level = + ((float) (srv->state->power_actual - srv->state->target_power_actual) / srv->transition.counter); +} + +void light_lightness_actual_tt_values(struct bt_mesh_light_lightness_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->actual_transition, trans_time, delay); + srv->tt_delta_lightness_actual = + ((float) (srv->state->lightness_actual - srv->state->target_lightness_actual) / srv->actual_transition.counter); +} + +void light_lightness_linear_tt_values(struct bt_mesh_light_lightness_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->linear_transition, trans_time, delay); + srv->tt_delta_lightness_linear = + ((float) (srv->state->lightness_linear - srv->state->target_lightness_linear) / srv->linear_transition.counter); +} + +void light_ctl_tt_values(struct bt_mesh_light_ctl_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_lightness = + ((float) (srv->state->lightness - srv->state->target_lightness) / srv->transition.counter); + srv->tt_delta_temperature = + ((float) (srv->state->temperature - srv->state->target_temperature) / srv->transition.counter); + srv->tt_delta_delta_uv = + ((float) (srv->state->delta_uv - srv->state->target_delta_uv) / srv->transition.counter); +} + +void light_ctl_temp_tt_values(struct bt_mesh_light_ctl_temp_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_temperature = + ((float) (srv->state->temperature - srv->state->target_temperature) / srv->transition.counter); + srv->tt_delta_delta_uv = + ((float) (srv->state->delta_uv - srv->state->target_delta_uv) / srv->transition.counter); +} + +void light_hsl_tt_values(struct bt_mesh_light_hsl_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_lightness = + ((float) (srv->state->lightness - srv->state->target_lightness) / srv->transition.counter); + srv->tt_delta_hue = + ((float) (srv->state->hue - srv->state->target_hue) / srv->transition.counter); + srv->tt_delta_saturation = + ((float) (srv->state->saturation - srv->state->target_saturation) / srv->transition.counter); +} + +void light_hsl_hue_tt_values(struct bt_mesh_light_hsl_hue_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_hue = + ((float) (srv->state->hue - srv->state->target_hue) / srv->transition.counter); +} + +void light_hsl_sat_tt_values(struct bt_mesh_light_hsl_sat_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_saturation = + ((float) (srv->state->saturation - srv->state->target_saturation) / srv->transition.counter); +} + +void light_xyl_tt_values(struct bt_mesh_light_xyl_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); + srv->tt_delta_lightness = + ((float) (srv->state->lightness - srv->state->target_lightness) / srv->transition.counter); + srv->tt_delta_x = + ((float) (srv->state->x - srv->state->target_x) / srv->transition.counter); + srv->tt_delta_y = + ((float) (srv->state->y - srv->state->target_y) / srv->transition.counter); +} + +void light_lc_tt_values(struct bt_mesh_light_lc_srv *srv, + u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); +} + +void scene_tt_values(struct bt_mesh_scene_srv *srv, u8_t trans_time, u8_t delay) +{ + transition_time_values(&srv->transition, trans_time, delay); +} + +static void transition_timer_start(struct bt_mesh_state_transition *transition) +{ + transition->start_timestamp = k_uptime_get(); + k_delayed_work_submit_periodic(&transition->timer, K_MSEC(transition->quo_tt)); + bt_mesh_atomic_set_bit(transition->flag, BLE_MESH_TRANS_TIMER_START); +} + +static void transition_timer_stop(struct bt_mesh_state_transition *transition) +{ + k_delayed_work_cancel(&transition->timer); + bt_mesh_atomic_clear_bit(transition->flag, BLE_MESH_TRANS_TIMER_START); +} + +/* Timers related handlers & threads (Start) */ +void generic_onoff_work_handler(struct k_work *work) +{ + struct bt_mesh_gen_onoff_srv *srv = + CONTAINER_OF(work, struct bt_mesh_gen_onoff_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_gen_server_state_change_t change = {0}; + + if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_generic_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.gen_onoff_set.onoff = srv->state.onoff; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + /** + * Because binary states cannot support transitions, when changing to + * 0x01 (On), the Generic OnOff state shall change immediately when + * the transition starts, and when changing to 0x00, the state shall + * change when the transition finishes. + */ + if (srv->state.target_onoff == BLE_MESH_STATE_ON) { + srv->state.onoff = BLE_MESH_STATE_ON; + change.gen_onoff_set.onoff = srv->state.onoff; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + } + transition_timer_start(&srv->transition); + } + + bt_mesh_generic_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state.onoff = srv->state.target_onoff; + if (srv->state.target_onoff != BLE_MESH_STATE_ON) { + change.gen_onoff_set.onoff = srv->state.onoff; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + } + } + + gen_onoff_publish(srv->model); + + bt_mesh_generic_server_unlock(); + return; +} + +void generic_level_work_handler(struct k_work *work) +{ + struct bt_mesh_gen_level_srv *srv = + CONTAINER_OF(work, struct bt_mesh_gen_level_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_gen_server_state_change_t change = {0}; + + if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_generic_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: + change.gen_level_set.level = srv->state.level; + break; + case BLE_MESH_MODEL_OP_GEN_DELTA_SET: + case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: + change.gen_delta_set.level = srv->state.level; + break; + case BLE_MESH_MODEL_OP_GEN_MOVE_SET: + case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: + change.gen_move_set.level = srv->state.level; + break; + } + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_generic_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state.level -= srv->tt_delta_level; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state.level = srv->state.target_level; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET: + case BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK: + change.gen_level_set.level = srv->state.level; + break; + case BLE_MESH_MODEL_OP_GEN_DELTA_SET: + case BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK: + change.gen_delta_set.level = srv->state.level; + break; + case BLE_MESH_MODEL_OP_GEN_MOVE_SET: + case BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK: + change.gen_move_set.level = srv->state.level; + break; + } + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + gen_level_publish(srv->model); + + bt_mesh_generic_server_unlock(); + return; +} + +void generic_power_level_work_handler(struct k_work *work) +{ + struct bt_mesh_gen_power_level_srv *srv = + CONTAINER_OF(work, struct bt_mesh_gen_power_level_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_gen_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_generic_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.gen_power_level_set.power = srv->state->power_actual; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_generic_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->power_actual -= srv->tt_delta_level; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + + srv->state->power_actual = srv->state->target_power_actual; + /** + * Whenever the Generic Power Actual state is changed to a non-zero value + * as a result of a non-transactional message or a completed sequence of + * transactional messages, the value of the Generic Power Last state shall + * be set to the value of the Generic Power Actual state. + */ + if (srv->state->power_actual) { + srv->state->power_last = srv->state->power_actual; + } + } + + change.gen_power_level_set.power = srv->state->power_actual; + bt_mesh_generic_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + gen_power_level_publish(srv->model, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS); + + bt_mesh_generic_server_unlock(); + return; +} + +void light_lightness_actual_work_handler(struct k_work *work) +{ + struct bt_mesh_light_lightness_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_lightness_srv, actual_transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->actual_transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->actual_transition.timer.work._reserved; + + if (srv->actual_transition.just_started) { + srv->actual_transition.just_started = false; + if (srv->actual_transition.counter == 0U) { + change.lightness_set.lightness = srv->state->lightness_actual; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->actual_transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->actual_transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->actual_transition.counter != 0U) { + srv->actual_transition.counter--; + srv->state->lightness_actual -= srv->tt_delta_lightness_actual; + } + + if (srv->actual_transition.counter == 0U) { + transition_timer_stop(&srv->actual_transition); + + srv->state->lightness_actual = srv->state->target_lightness_actual; + /** + * Whenever the Light Lightness Actual state is changed with a non- + * transactional message or a completed sequence of transactional + * messages to a non-zero value, the value of the Light Lightness + * Last shall be set to the value of the Light Lightness Actual. + */ + if (srv->state->lightness_actual) { + srv->state->lightness_last = srv->state->lightness_actual; + } + } + + change.lightness_set.lightness = srv->state->lightness_actual; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_lightness_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_lightness_linear_work_handler(struct k_work *work) +{ + struct bt_mesh_light_lightness_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_lightness_srv, linear_transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->linear_transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->linear_transition.timer.work._reserved; + + if (srv->linear_transition.just_started) { + srv->linear_transition.just_started = false; + if (srv->linear_transition.counter == 0U) { + change.lightness_linear_set.lightness = srv->state->lightness_linear; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->linear_transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->linear_transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->linear_transition.counter != 0U) { + srv->linear_transition.counter--; + srv->state->lightness_linear -= srv->tt_delta_lightness_linear; + } + + if (srv->linear_transition.counter == 0U) { + transition_timer_stop(&srv->linear_transition); + srv->state->lightness_linear = srv->state->target_lightness_linear; + } + + change.lightness_linear_set.lightness = srv->state->lightness_linear; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_lightness_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_ctl_work_handler(struct k_work *work) +{ + struct bt_mesh_light_ctl_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_ctl_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.ctl_set.lightness = srv->state->lightness; + change.ctl_set.temperature = srv->state->temperature; + change.ctl_set.delta_uv = srv->state->delta_uv; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->lightness -= srv->tt_delta_lightness; + srv->state->temperature -= srv->tt_delta_temperature; + srv->state->delta_uv -= srv->tt_delta_delta_uv; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->lightness = srv->state->target_lightness; + srv->state->temperature = srv->state->target_temperature; + srv->state->delta_uv = srv->state->target_delta_uv; + } + + change.ctl_set.lightness = srv->state->lightness; + change.ctl_set.temperature = srv->state->temperature; + change.ctl_set.delta_uv = srv->state->delta_uv; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_ctl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_ctl_temp_work_handler(struct k_work *work) +{ + struct bt_mesh_light_ctl_temp_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_ctl_temp_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.ctl_temp_set.temperature = srv->state->temperature; + change.ctl_temp_set.delta_uv = srv->state->delta_uv; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->temperature -= srv->tt_delta_temperature; + srv->state->delta_uv -= srv->tt_delta_delta_uv; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->temperature = srv->state->target_temperature; + srv->state->delta_uv = srv->state->target_delta_uv; + } + + change.ctl_temp_set.temperature = srv->state->temperature; + change.ctl_temp_set.delta_uv = srv->state->delta_uv; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_ctl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_hsl_work_handler(struct k_work *work) +{ + struct bt_mesh_light_hsl_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_hsl_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.hsl_set.lightness = srv->state->lightness; + change.hsl_set.hue = srv->state->hue; + change.hsl_set.saturation = srv->state->saturation; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->lightness -= srv->tt_delta_lightness; + srv->state->hue -= srv->tt_delta_hue; + srv->state->saturation -= srv->tt_delta_saturation; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->lightness = srv->state->target_lightness; + srv->state->hue = srv->state->target_hue; + srv->state->saturation = srv->state->target_saturation; + } + + change.hsl_set.lightness = srv->state->lightness; + change.hsl_set.hue = srv->state->hue; + change.hsl_set.saturation = srv->state->saturation; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_hsl_hue_work_handler(struct k_work *work) +{ + struct bt_mesh_light_hsl_hue_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_hsl_hue_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.hsl_hue_set.hue = srv->state->hue; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->hue -= srv->tt_delta_hue; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->hue = srv->state->target_hue; + } + + change.hsl_hue_set.hue = srv->state->hue; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_HUE_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_hsl_sat_work_handler(struct k_work *work) +{ + struct bt_mesh_light_hsl_sat_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_hsl_sat_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.hsl_saturation_set.saturation = srv->state->saturation; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->saturation -= srv->tt_delta_saturation; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->saturation = srv->state->target_saturation; + } + + change.hsl_saturation_set.saturation = srv->state->saturation; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_hsl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_HSL_SATURATION_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_xyl_work_handler(struct k_work *work) +{ + struct bt_mesh_light_xyl_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_xyl_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.xyl_set.lightness = srv->state->lightness; + change.xyl_set.x = srv->state->x; + change.xyl_set.y = srv->state->y; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + srv->state->lightness -= srv->tt_delta_lightness; + srv->state->x -= srv->tt_delta_x; + srv->state->y -= srv->tt_delta_y; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->lightness = srv->state->target_lightness; + srv->state->x = srv->state->target_x; + srv->state->y = srv->state->target_y; + } + + change.xyl_set.lightness = srv->state->lightness; + change.xyl_set.x = srv->state->x; + change.xyl_set.y = srv->state->y; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + light_xyl_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_XYL_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void light_lc_work_handler(struct k_work *work) +{ + struct bt_mesh_light_lc_srv *srv = + CONTAINER_OF(work, struct bt_mesh_light_lc_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_light_server_state_change_t change = {0}; + + if (srv == NULL || srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_light_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.lc_light_onoff_set.onoff = srv->lc->state.light_onoff; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + /** + * Because binary states cannot support transitions, when changing to + * 0x01 (On), the Generic OnOff state shall change immediately when + * the transition starts, and when changing to 0x00, the state shall + * change when the transition finishes. + */ + if (srv->lc->state.target_light_onoff == BLE_MESH_STATE_ON) { + srv->lc->state.light_onoff = BLE_MESH_STATE_ON; + bt_mesh_light_server_state_change_t change = { + .lc_light_onoff_set.onoff = srv->lc->state.light_onoff, + }; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + } + transition_timer_start(&srv->transition); + } + + bt_mesh_light_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->lc->state.light_onoff = srv->lc->state.target_light_onoff; + if (srv->lc->state.light_onoff != BLE_MESH_STATE_ON) { + change.lc_light_onoff_set.onoff = srv->lc->state.light_onoff; + bt_mesh_lighting_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + } + } + + light_lc_publish(srv->model, BLE_MESH_MODEL_OP_LIGHT_LC_LIGHT_ONOFF_STATUS); + + bt_mesh_light_server_unlock(); + return; +} + +void scene_recall_work_handler(struct k_work *work) +{ + struct bt_mesh_scene_srv *srv = + CONTAINER_OF(work, struct bt_mesh_scene_srv, transition.timer.work); + struct bt_mesh_msg_ctx *ctx = NULL; + bt_mesh_time_scene_server_state_change_t change = {0}; + + if (srv == NULL || srv->state == NULL || + srv->transition.timer.work._reserved == NULL) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + bt_mesh_time_scene_server_lock(); + + ctx = (struct bt_mesh_msg_ctx *)srv->transition.timer.work._reserved; + + if (srv->transition.just_started) { + srv->transition.just_started = false; + if (srv->transition.counter == 0U) { + change.scene_recall.scene_number = srv->state->current_scene; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + bt_mesh_atomic_clear_bit(srv->transition.flag, BLE_MESH_TRANS_TIMER_START); + } else { + transition_timer_start(&srv->transition); + } + + bt_mesh_time_scene_server_unlock(); + return; + } + + if (srv->transition.counter != 0U) { + srv->transition.counter--; + } + + if (srv->transition.counter == 0U) { + transition_timer_stop(&srv->transition); + srv->state->current_scene = srv->state->target_scene; + srv->state->in_progress = false; + srv->state->target_scene = INVALID_SCENE_NUMBER; + } + + change.scene_recall.scene_number = srv->state->current_scene; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, srv->model, ctx, (const u8_t *)&change, sizeof(change)); + + scene_publish(srv->model, ctx, BLE_MESH_MODEL_OP_SCENE_STATUS); + + bt_mesh_time_scene_server_unlock(); + return; +} + +/* Timers related handlers & threads (End) */ + +void bt_mesh_server_stop_transition(struct bt_mesh_state_transition *transition) +{ + memset(transition, 0x0, offsetof(struct bt_mesh_state_transition, flag)); + if (bt_mesh_atomic_test_and_clear_bit(transition->flag, BLE_MESH_TRANS_TIMER_START)) { + k_delayed_work_cancel(&transition->timer); + } +} + +void bt_mesh_server_start_transition(struct bt_mesh_state_transition *transition) +{ + if (transition->delay) { + k_delayed_work_submit(&transition->timer, K_MSEC(5 * transition->delay)); + bt_mesh_atomic_set_bit(transition->flag, BLE_MESH_TRANS_TIMER_START); + } else { + k_work_submit(&transition->timer.work); + } +} + +/* Messages handlers (End) */ diff --git a/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c new file mode 100644 index 000000000..890dcc019 --- /dev/null +++ b/components/bt/esp_ble_mesh/mesh_models/server/time_scene_server.c @@ -0,0 +1,1500 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "btc_ble_mesh_time_scene_model.h" + +#include "access.h" +#include "transport.h" +#include "model_opcode.h" +#include "state_transition.h" + +static bt_mesh_mutex_t time_scene_server_lock; + +static void bt_mesh_time_scene_server_mutex_new(void) +{ + if (!time_scene_server_lock.mutex) { + bt_mesh_mutex_create(&time_scene_server_lock); + } +} + +static void bt_mesh_time_scene_server_mutex_free(void) +{ + bt_mesh_mutex_free(&time_scene_server_lock); +} + +void bt_mesh_time_scene_server_lock(void) +{ + bt_mesh_mutex_lock(&time_scene_server_lock); +} + +void bt_mesh_time_scene_server_unlock(void) +{ + bt_mesh_mutex_unlock(&time_scene_server_lock); +} + +/* message handlers (Start) */ + +/* Time Server & Time Setup Server message handlers */ +static void send_time_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish, u16_t opcode) +{ + struct net_buf_simple *msg = NULL; + u8_t zero[5] = {0}; + u8_t length = 1 + 10; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, opcode); + switch (opcode) { + case BLE_MESH_MODEL_OP_TIME_STATUS: + if (model->id == BLE_MESH_MODEL_ID_TIME_SRV) { + struct bt_mesh_time_srv *srv = model->user_data; + net_buf_simple_add_mem(msg, srv->state->time.tai_seconds, TAI_SECONDS_LEN); + if (memcmp(srv->state->time.tai_seconds, zero, TAI_SECONDS_LEN)) { + net_buf_simple_add_u8(msg, srv->state->time.subsecond); + /** + * Set the Uncertainty field to a value that is a sum of the value of + * the Uncertainty state and an estimated time it will take the message + * to be processed before being sent on the radio interface. + * + * TODO: how to estimate the processing time? + */ + net_buf_simple_add_u8(msg, srv->state->time.uncertainty); + net_buf_simple_add_le16(msg, + (srv->state->time.tai_utc_delta_curr << 1) | srv->state->time.time_authority); + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_curr); + } + } else if (model->id == BLE_MESH_MODEL_ID_TIME_SETUP_SRV) { + struct bt_mesh_time_setup_srv *srv = model->user_data; + net_buf_simple_add_mem(msg, srv->state->time.tai_seconds, TAI_SECONDS_LEN); + if (memcmp(srv->state->time.tai_seconds, zero, TAI_SECONDS_LEN)) { + net_buf_simple_add_u8(msg, srv->state->time.subsecond); + net_buf_simple_add_u8(msg, srv->state->time.uncertainty); + net_buf_simple_add_le16(msg, + (srv->state->time.tai_utc_delta_curr << 1) | srv->state->time.time_authority); + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_curr); + } + } + break; + case BLE_MESH_MODEL_OP_TIME_ZONE_STATUS: + if (model->id == BLE_MESH_MODEL_ID_TIME_SRV) { + struct bt_mesh_time_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_curr); + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_new); + net_buf_simple_add_mem(msg, srv->state->time.tai_zone_change, TAI_OF_ZONE_CHANGE_LEN); + } else if (model->id == BLE_MESH_MODEL_ID_TIME_SETUP_SRV) { + struct bt_mesh_time_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_curr); + net_buf_simple_add_u8(msg, srv->state->time.time_zone_offset_new); + net_buf_simple_add_mem(msg, srv->state->time.tai_zone_change, TAI_OF_ZONE_CHANGE_LEN); + } + break; + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS: + if (model->id == BLE_MESH_MODEL_ID_TIME_SRV) { + struct bt_mesh_time_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->time.tai_utc_delta_curr); + net_buf_simple_add_le16(msg, srv->state->time.tai_utc_delta_new); + net_buf_simple_add_mem(msg, srv->state->time.tai_delta_change, TAI_OF_DELTA_CHANGE_LEN); + } else if (model->id == BLE_MESH_MODEL_ID_TIME_SETUP_SRV) { + struct bt_mesh_time_setup_srv *srv = model->user_data; + net_buf_simple_add_le16(msg, srv->state->time.tai_utc_delta_curr); + net_buf_simple_add_le16(msg, srv->state->time.tai_utc_delta_new); + net_buf_simple_add_mem(msg, srv->state->time.tai_delta_change, TAI_OF_DELTA_CHANGE_LEN); + } + break; + case BLE_MESH_MODEL_OP_TIME_ROLE_STATUS: { + struct bt_mesh_time_setup_srv *srv = model->user_data; + net_buf_simple_add_u8(msg, srv->state->time_role); + break; + } + default: + BT_WARN("%s, Unknown Time status opcode 0x%04x", __func__, opcode); + if (publish == false) { + bt_mesh_free_buf(msg); + } + return; + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void time_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL; + u8_t zero[5] = {0}; + u16_t opcode = 0U, val = 0U; + u8_t prev_ttl = 0U; + + if (model->user_data == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_TIME_SRV: { + struct bt_mesh_time_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Time Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + case BLE_MESH_MODEL_ID_TIME_SETUP_SRV: { + struct bt_mesh_time_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Time Setup Server state", __func__); + return; + } + rsp_ctrl = &srv->rsp_ctrl; + break; + } + default: + BT_ERR("%s, Invalid Time Server 0x%04x", __func__, model->id); + return; + } + + if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + if (ctx->recv_op != BLE_MESH_MODEL_OP_TIME_STATUS) { + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_TIME_GET: + opcode = BLE_MESH_MODEL_OP_TIME_STATUS; + break; + case BLE_MESH_MODEL_OP_TIME_STATUS: { + struct bt_mesh_time_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, Invalid Time Server state", __func__); + return; + } + if (srv->state->time_role != TIME_RELAY && + srv->state->time_role != TIME_CLINET) { + /** + * If the value of the Time Role state of the element is 0x00 (None) or + * 0x01 (Time Authority), the message shall be ignored. + */ + return; + } + if (rsp_ctrl->status_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_status_msg_t status = {0}; + memcpy(status.time_status.tai_seconds, buf->data, TAI_SECONDS_LEN); + net_buf_simple_pull(buf, TAI_SECONDS_LEN); + if (memcmp(status.time_status.tai_seconds, zero, TAI_SECONDS_LEN)) { + if (buf->len != TAI_SECONDS_LEN) { + BT_ERR("%s, Invalid Time Status length %d", __func__, buf->len + TAI_SECONDS_LEN); + return; + } + status.time_status.subsecond = net_buf_simple_pull_u8(buf); + status.time_status.uncertainty = net_buf_simple_pull_u8(buf); + val = net_buf_simple_pull_le16(buf); + status.time_status.time_authority = val & BIT(0); + status.time_status.tai_utc_delta = (val >> 1) & BIT_MASK(15); + status.time_status.time_zone_offset = net_buf_simple_pull_u8(buf); + } + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_STATUS_MSG, model, ctx, (const u8_t *)&status, sizeof(status)); + return; + } + memcpy(srv->state->time.tai_seconds, buf->data, TAI_SECONDS_LEN); + net_buf_simple_pull(buf, TAI_SECONDS_LEN); + /** + * If the TAI Seconds field is 0x0000000000 the Subsecond, Uncertainty, + * Time Authority, TAI-UTC Delta and Time Zone Offset fields shall be + * omitted; otherwise these fields shall be present. + */ + if (memcmp(srv->state->time.tai_seconds, zero, TAI_SECONDS_LEN)) { + if (buf->len != TAI_SECONDS_LEN) { + BT_ERR("%s, Invalid Time Status length %d", __func__, buf->len + TAI_SECONDS_LEN); + return; + } + srv->state->time.subsecond = net_buf_simple_pull_u8(buf); + srv->state->time.uncertainty = net_buf_simple_pull_u8(buf); + val = net_buf_simple_pull_le16(buf); + srv->state->time.tai_utc_delta_curr = (val >> 1) & BIT_MASK(15); + srv->state->time.time_zone_offset_curr = net_buf_simple_pull_u8(buf); + } + + bt_mesh_time_scene_server_state_change_t change = {0}; + memcpy(change.time_status.tai_seconds, srv->state->time.tai_seconds, TAI_SECONDS_LEN); + change.time_status.subsecond = srv->state->time.subsecond; + change.time_status.uncertainty = srv->state->time.uncertainty; + change.time_status.time_authority = srv->state->time.time_authority; + change.time_status.tai_utc_delta_curr = srv->state->time.subsecond; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (model->pub == NULL || model->pub->msg == NULL || + model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) { + return; + } + prev_ttl = model->pub->ttl; + if (srv->state->time_role == TIME_RELAY) { + /** + * Shall publish a Time Status message using TTL = 0 if the value of the + * Time Role state is 0x02 (Time Relay) and the Publish Address for the + * Time Server model is not set to unassigned address. + */ + model->pub->ttl = 0U; + } + send_time_status(model, NULL, true, BLE_MESH_MODEL_OP_TIME_STATUS); + /* Restore model publication ttl value */ + model->pub->ttl = prev_ttl; + return; + } + case BLE_MESH_MODEL_OP_TIME_ZONE_GET: + opcode = BLE_MESH_MODEL_OP_TIME_ZONE_STATUS; + break; + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET: + opcode = BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS; + break; + case BLE_MESH_MODEL_OP_TIME_ROLE_GET: + opcode = BLE_MESH_MODEL_OP_TIME_ROLE_STATUS; + break; + default: + BT_WARN("%s, Unknown Time Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + send_time_status(model, ctx, false, opcode); + return; +} + +static void time_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_time_setup_srv *srv = model->user_data; + bt_mesh_time_scene_server_state_change_t change = {0}; + u16_t opcode = 0U, val = 0U; + u8_t role = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_TIME_SET: + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = {0}; + memcpy(set.time_set.tai_seconds, buf->data, TAI_SECONDS_LEN); + net_buf_simple_pull(buf, TAI_SECONDS_LEN); + set.time_set.subsecond = net_buf_simple_pull_u8(buf); + set.time_set.uncertainty = net_buf_simple_pull_u8(buf); + val = net_buf_simple_pull_le16(buf); + set.time_set.time_authority = val & BIT(0); + set.time_set.tai_utc_delta = (val >> 1) & BIT_MASK(15); + set.time_set.time_zone_offset = net_buf_simple_pull_u8(buf); + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + memcpy(srv->state->time.tai_seconds, buf->data, TAI_SECONDS_LEN); + net_buf_simple_pull(buf, TAI_SECONDS_LEN); + srv->state->time.subsecond = net_buf_simple_pull_u8(buf); + srv->state->time.uncertainty = net_buf_simple_pull_u8(buf); + val = net_buf_simple_pull_le16(buf); + srv->state->time.time_authority = val & BIT(0); + srv->state->time.tai_utc_delta_curr = (val >> 1) & BIT_MASK(15); + srv->state->time.time_zone_offset_curr = net_buf_simple_pull_u8(buf); + opcode = BLE_MESH_MODEL_OP_TIME_STATUS; + break; + case BLE_MESH_MODEL_OP_TIME_ZONE_SET: + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = {0}; + set.time_zone_set.time_zone_offset_new = net_buf_simple_pull_u8(buf); + memcpy(set.time_zone_set.tai_zone_change, buf->data, TAI_OF_ZONE_CHANGE_LEN); + net_buf_simple_pull(buf, TAI_OF_ZONE_CHANGE_LEN); + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + srv->state->time.time_zone_offset_new = net_buf_simple_pull_u8(buf); + memcpy(srv->state->time.tai_zone_change, buf->data, TAI_OF_ZONE_CHANGE_LEN); + net_buf_simple_pull(buf, TAI_OF_ZONE_CHANGE_LEN); + opcode = BLE_MESH_MODEL_OP_TIME_ZONE_STATUS; + break; + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET: + val = net_buf_simple_pull_le16(buf); + if ((val >> 15) & BIT(0)) { + BT_ERR("%s, Invalid Padding value 1", __func__); + return; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = {0}; + set.tai_utc_delta_set.tai_utc_delta_new = val & BIT_MASK(15); + memcpy(set.tai_utc_delta_set.tai_delta_change, buf->data, TAI_OF_DELTA_CHANGE_LEN); + net_buf_simple_pull(buf, TAI_OF_DELTA_CHANGE_LEN); + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + srv->state->time.tai_utc_delta_new = val & BIT_MASK(15); + memcpy(srv->state->time.tai_delta_change, buf->data, TAI_OF_DELTA_CHANGE_LEN); + net_buf_simple_pull(buf, TAI_OF_DELTA_CHANGE_LEN); + opcode = BLE_MESH_MODEL_OP_TAI_UTC_DELTA_STATUS; + break; + case BLE_MESH_MODEL_OP_TIME_ROLE_SET: + role = net_buf_simple_pull_u8(buf); + if (role > TIME_CLINET) { + BT_ERR("%s, Invalid Time Role 0x%02x", __func__, role); + return; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = { + .time_role_set.time_role = role, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + srv->state->time_role = role; + opcode = BLE_MESH_MODEL_OP_TIME_ROLE_STATUS; + break; + default: + BT_ERR("%s, Unknown Time Set opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_TIME_SET: + memcpy(change.time_set.tai_seconds, srv->state->time.tai_seconds, TAI_SECONDS_LEN); + change.time_set.subsecond = srv->state->time.subsecond; + change.time_set.uncertainty = srv->state->time.uncertainty; + change.time_set.time_authority = srv->state->time.time_authority; + change.time_set.tai_utc_delta_curr = srv->state->time.subsecond; + break; + case BLE_MESH_MODEL_OP_TIME_ZONE_SET: + change.time_zone_set.time_zone_offset_new = srv->state->time.time_zone_offset_new; + memcpy(change.time_zone_set.tai_zone_change, srv->state->time.tai_zone_change, TAI_OF_ZONE_CHANGE_LEN); + break; + case BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET: + change.tai_utc_delta_set.tai_utc_delta_new = srv->state->time.tai_utc_delta_new; + memcpy(change.tai_utc_delta_set.tai_delta_change, srv->state->time.tai_delta_change, TAI_OF_DELTA_CHANGE_LEN); + break; + case BLE_MESH_MODEL_OP_TIME_ROLE_SET: + change.time_role_set.role = srv->state->time_role; + break; + default: + return; + } + + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + /* Send corresponding time status message */ + send_time_status(model, ctx, false, opcode); + return; +} + +/* Scene Server & Scene Setup Server message handlers */ +static void send_scene_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + bool publish) +{ + struct bt_mesh_scene_srv *srv = model->user_data; + struct net_buf_simple *msg = NULL; + u8_t length = 1 + 6; + + if (publish == false) { + msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, length); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SCENE_STATUS); + /** + * If the message is sent as a reply to the Scene Recall message, the + * Status Code field identifies the result of the related operation; + * otherwise, the Status Code field shall be set to Success. + */ + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_GET) { + net_buf_simple_add_u8(msg, SCENE_SUCCESS); + } else { + net_buf_simple_add_u8(msg, srv->state->status_code); + } + net_buf_simple_add_le16(msg, srv->state->current_scene); + /** + * When an element is in the process of changing the Scene state, the + * Target Scene field identifies the target Scene Number of the target + * Scene state the element is to reach. + * When an element is not in the process of changing the Scene state, + * the Target Scene field shall be omitted. + */ + if (srv->transition.counter) { + bt_mesh_server_calc_remain_time(&srv->transition); + net_buf_simple_add_le16(msg, srv->state->target_scene); + net_buf_simple_add_u8(msg, srv->transition.remain_time); + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void send_scene_register_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u8_t status_code, bool publish) +{ + struct bt_mesh_scene_setup_srv *srv = model->user_data; + struct scene_register *scene = NULL; + struct net_buf_simple *msg = NULL; + u16_t total_len = 9U; + int i; + + if (ctx == NULL && publish == false) { + BT_ERR("%s, Invalid parameter", __func__); + return; + } + + if (publish == false) { + msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)); + if (msg == NULL) { + BT_ERR("%s, Failed to allocate memory", __func__); + return; + } + } else { + msg = bt_mesh_server_get_pub_msg(model, 5); + if (msg == NULL) { + return; + } + } + + bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SCENE_REGISTER_STATUS); + net_buf_simple_add_u8(msg, status_code); + net_buf_simple_add_le16(msg, srv->state->current_scene); + + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number != INVALID_SCENE_NUMBER) { + total_len += SCENE_NUMBER_LEN; + if ((publish == false && total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) || + (publish == true && total_len > msg->size + BLE_MESH_SERVER_TRANS_MIC_SIZE)) { + /* Add this in case the message is too long */ + break; + } + net_buf_simple_add_le16(msg, scene->scene_number); + } + } + + if (publish == false) { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL)); + bt_mesh_free_buf(msg); + } else { + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model)); + } + return; +} + +static void scene_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_scene_srv *srv = model->user_data; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SCENE_GET: + send_scene_status(model, ctx, false); + return; + case BLE_MESH_MODEL_OP_SCENE_REGISTER_GET: + /** + * When a Scene Server receives a Scene Register Get message, it shall + * respond with a Scene Register Status message, setting the Status + * Code field to Success. + */ + send_scene_register_status(model, ctx, SCENE_SUCCESS, false); + return; + default: + BT_WARN("%s, Unknown Scene Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +void scene_publish(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, u16_t opcode) +{ + struct bt_mesh_scene_srv *srv = model->user_data; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + send_scene_status(model, ctx, true); + return; +} + +static void scene_recall(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_scene_srv *srv = model->user_data; + struct scene_register *scene = NULL; + u8_t tid = 0U, trans_time = 0U, delay = 0U; + u16_t scene_number = 0U; + bool optional = false; + s64_t now = 0; + int i; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + scene_number = net_buf_simple_pull_le16(buf); + if (scene_number == INVALID_SCENE_NUMBER) { + BT_ERR("%s, Invalid Scene Number 0x0000", __func__); + return; + } + tid = net_buf_simple_pull_u8(buf); + + if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) { + return; + } + + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = { + .scene_recall.op_en = optional, + .scene_recall.scene_number = scene_number, + .scene_recall.tid = tid, + .scene_recall.trans_time = trans_time, + .scene_recall.delay = delay, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number == scene_number) { + break; + } + } + if (i == srv->state->scene_count) { + BT_WARN("%s, Scene Number 0x%04x not exist", __func__, scene_number); + srv->state->status_code = SCENE_NOT_FOUND; + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_RECALL) { + send_scene_status(model, ctx, false); + } + send_scene_status(model, ctx, true); + return; + } + srv->state->status_code = SCENE_SUCCESS; + + /* Mesh Model Spec doesn't mention about this operation. */ + if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) { + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_RECALL) { + send_scene_status(model, ctx, false); + } + send_scene_status(model, ctx, true); + /* In this condition, no event will be callback to application layer */ + return; + } + + bt_mesh_time_scene_server_lock(); + + bt_mesh_server_stop_transition(&srv->transition); + bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now); + + srv->state->in_progress = false; + /** + * When the scene transition is not in progress, the value of the Target + * Scene state shall be set to 0x0000. + */ + srv->state->target_scene = INVALID_SCENE_NUMBER; + + /** + * If the target state is equal to the current state, the transition + * shall not be started and is considered complete. + */ + if (srv->state->current_scene != scene_number) { + scene_tt_values(srv, trans_time, delay); + } else { + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_RECALL) { + send_scene_status(model, ctx, false); + } + send_scene_status(model, ctx, true); + + bt_mesh_time_scene_server_state_change_t change = { + .scene_recall.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + bt_mesh_time_scene_server_unlock(); + return; + } + + /* Copy the ctx of the received message */ + if (srv->transition.timer.work._reserved) { + memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx)); + } + + /* For Instantaneous Transition */ + if (srv->transition.counter == 0U) { + srv->state->current_scene = scene_number; + } else { + /** + * When a scene transition is in progress, the value of the Current + * Scene state shall be set to 0x0000. + */ + srv->state->in_progress = true; + srv->state->current_scene = INVALID_SCENE_NUMBER; + srv->state->target_scene = scene_number; + } + + srv->transition.just_started = true; + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_RECALL) { + send_scene_status(model, ctx, false); + } + send_scene_status(model, ctx, true); + + bt_mesh_time_scene_server_unlock(); + + bt_mesh_server_start_transition(&srv->transition); + return; +} + +static void scene_action(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_scene_setup_srv *srv = model->user_data; + struct scene_register *scene = NULL; + u16_t scene_number = 0U; + int i; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + scene_number = net_buf_simple_pull_le16(buf); + if (scene_number == INVALID_SCENE_NUMBER) { + BT_ERR("%s, Invalid Scene number 0x0000", __func__); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SCENE_STORE: + case BLE_MESH_MODEL_OP_SCENE_STORE_UNACK: { + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = { + .scene_store.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + /* Try to find a matching Scene Number */ + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number == scene_number) { + srv->state->status_code = SCENE_SUCCESS; + srv->state->current_scene = scene_number; + break; + } + } + /* Try to find a unset entry if no matching Scene Number is found */ + if (i == srv->state->scene_count) { + BT_DBG("%s, No matching Scene Number 0x%04x found", __func__, scene_number); + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number == INVALID_SCENE_NUMBER) { + scene->scene_number = scene_number; + srv->state->status_code = SCENE_SUCCESS; + srv->state->current_scene = scene_number; + break; + } + } + if (i == srv->state->scene_count) { + BT_WARN("%s, Scene Register full", __func__); + srv->state->status_code = SCENE_REG_FULL; + /* Get the Scene Number of the currently active scene */ + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number != INVALID_SCENE_NUMBER) { + srv->state->current_scene = scene->scene_number; + break; + } + } + if (i == srv->state->scene_count) { + /* A value of 0x0000 when no scene is active */ + srv->state->current_scene = INVALID_SCENE_NUMBER; + } + } + } + + if (srv->state->in_progress == true) { + /** + * When the scene transition is in progress and a new Scene Number is + * stored in the Scene Register as a result of Scene Store operation, + * the Target Scene state shall be set to the new Scene Number. + */ + srv->state->target_scene = scene_number; + } + if (srv->state->status_code == SCENE_SUCCESS) { + bt_mesh_time_scene_server_state_change_t change = { + .scene_store.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + } + break; + } + case BLE_MESH_MODEL_OP_SCENE_DELETE: + case BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK: { + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = { + .scene_delete.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + for (i = 0; i < srv->state->scene_count; i++) { + scene = &srv->state->scenes[i]; + if (scene->scene_number == scene_number) { + scene->scene_number = INVALID_SCENE_NUMBER; + break; + } + } + if (i == srv->state->scene_count) { + BT_WARN("%s, Scene Number 0x%04x not exist", __func__, scene_number); + /** + * When a Scene Server receives a Scene Delete message with the Scene + * Number value that does not match a Scene Number stored within the + * Scene Register state, it shall respond with the Scene Register + * Status message, setting the Status Code field to Success. + */ + } + srv->state->status_code = SCENE_SUCCESS; + if (srv->state->current_scene == scene_number) { + /** + * When the Current Scene Number is deleted from a Scene Register state + * as a result of Scene Delete operation, the Current Scene state shall + * be set to 0x0000. + */ + srv->state->current_scene = INVALID_SCENE_NUMBER; + } else { + /** + * MMDL/SR/SCES/BV-02-C requires response with Current Scene set to the + * latest Scene Number, but this is not mentioned in the spec. + * + * TODO: Do we need a timestamp for each newly added scene? + */ + for (i = srv->state->scene_count; i > 0; i--) { + scene = &srv->state->scenes[i - 1]; + if (scene->scene_number != INVALID_SCENE_NUMBER) { + srv->state->current_scene = scene->scene_number; + break; + } + } + if (i == 0) { + /* A value of 0x0000 when no scene is active */ + srv->state->current_scene = INVALID_SCENE_NUMBER; + } + } + + if (srv->state->target_scene == scene_number && + srv->state->in_progress == true) { + /** + * When the scene transition is in progress and the target Scene Number + * is deleted from a Scene Register state as a result of Scene Delete + * operation, the Target Scene state shall be set to 0x0000. + */ + srv->state->target_scene = INVALID_SCENE_NUMBER; + + /** + * When a scene is deleted when a scene transition to the deleted Scene + * Number is in progress, the scene transition shall be terminated, but + * individual model transitions shall not be terminated. + */ + struct bt_mesh_scene_srv *scene_srv = NULL; + struct bt_mesh_model *scene_model = NULL; + + scene_model = bt_mesh_model_find(bt_mesh_model_elem(model), BLE_MESH_MODEL_ID_SCENE_SRV); + if (scene_model == NULL) { + BT_ERR("%s, Scene Server is not present in the element", __func__); + break; + } + + scene_srv = scene_model->user_data; + if (scene_srv == NULL || scene_srv->state == NULL) { + BT_ERR("%s, Invalid Scene Server parameter", __func__); + break; + } + + if (srv->state != scene_srv->state) { + /** + * Add this in case the Scene Setup Server is extending the Scene + * Server in another element. + */ + BT_WARN("%s, Different Scene state in Scene Server & Scene Setup Server", __func__); + break; + } + + scene_srv->state->in_progress = false; + bt_mesh_server_stop_transition(&scene_srv->transition); + } + + bt_mesh_time_scene_server_state_change_t change = { + .scene_delete.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + break; + } + default: + BT_ERR("%s, Unknown Scene setup action opcode 0x%04x", __func__, ctx->recv_op); + return; + } + + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_STORE || + ctx->recv_op == BLE_MESH_MODEL_OP_SCENE_DELETE) { + send_scene_register_status(model, ctx, srv->state->status_code, false); + } + send_scene_register_status(model, NULL, srv->state->status_code, true); + + return; +} + +static u16_t get_schedule_reg_bit(struct bt_mesh_scheduler_state *state) +{ + u16_t val = 0U; + int i; + + for (i = 0; i < state->schedule_count; i++) { + if (state->schedules[i].in_use) { + val |= (1 << i); + } + } + + return val; +} + +static u64_t get_schedule_reg_state(struct bt_mesh_scheduler_state *state, u8_t index) +{ + struct schedule_register *reg = &state->schedules[index]; + u64_t val = 0U; + + val = ((u64_t)(reg->year) << 4) | index; + val |= ((u64_t)(reg->day) << 23) | ((u64_t)(reg->month) << 11); + val |= ((u64_t)(reg->minute) << 33) | ((u64_t)(reg->hour) << 28); + val |= ((u64_t)(reg->day_of_week) << 45) | ((u64_t)(reg->second) << 39); + val |= ((u64_t)(reg->trans_time) << 56) | ((u64_t)(reg->action) << 52); + + return val; +} + +static void send_scheduler_act_status(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + u8_t index) +{ + NET_BUF_SIMPLE_DEFINE(msg, 1 + 10 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + u64_t value = 0U; + + bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_SCHEDULER_ACT_STATUS); + switch (model->id) { + case BLE_MESH_MODEL_ID_SCHEDULER_SRV: { + struct bt_mesh_scheduler_srv *srv = model->user_data; + value = get_schedule_reg_state(srv->state, index); + net_buf_simple_add_le32(&msg, (u32_t)value); + net_buf_simple_add_le32(&msg, (u32_t)(value >> 32)); + net_buf_simple_add_le16(&msg, srv->state->schedules[index].scene_number); + break; + } + case BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV: { + struct bt_mesh_scheduler_setup_srv *srv = model->user_data; + value = get_schedule_reg_state(srv->state, index); + net_buf_simple_add_le32(&msg, (u32_t)value); + net_buf_simple_add_le32(&msg, (u32_t)(value >> 32)); + net_buf_simple_add_le16(&msg, srv->state->schedules[index].scene_number); + break; + } + default: + BT_ERR("%s, Invalid Scheduler Server 0x%04x", __func__, model->id); + return; + } + + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); + return; +} + +static void scheduler_get(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + struct bt_mesh_scheduler_srv *srv = model->user_data; + NET_BUF_SIMPLE_DEFINE(msg, 2 + 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE); + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + switch (ctx->recv_op) { + case BLE_MESH_MODEL_OP_SCHEDULER_GET: { + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, NULL, 0); + return; + } + + bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_SCHEDULER_STATUS); + net_buf_simple_add_le16(&msg, get_schedule_reg_bit(srv->state)); + BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL)); + return; + } + case BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET: { + u8_t index = net_buf_simple_pull_u8(buf); + if (index > SCHEDULE_ENTRY_MAX_INDEX) { + BT_ERR("%s, Invalid Scheduler Register Entry index 0x%02x", __func__, index); + return; + } + + if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_get_msg_t get = { + .scheduler_act_get.index = index, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_GET_MSG, model, ctx, (const u8_t *)&get, sizeof(get)); + return; + } + + send_scheduler_act_status(model, ctx, index); + return; + } + default: + BT_WARN("%s, Unknown Scheduler Get opcode 0x%04x", __func__, ctx->recv_op); + return; + } +} + +static void scheduler_act_set(struct bt_mesh_model *model, + struct bt_mesh_msg_ctx *ctx, + struct net_buf_simple *buf) +{ + /** + * A recommended implementation of the Scheduler should calculate the value + * of the TAI Seconds of the next scheduled event and put it in a queue of + * scheduled events sorted by time. Every second, the first event in the + * queue is compared with the value of the Time state. The first event is + * executed if it is less than or equal to the Time state and then removed + * from the queue. After execution, the Repeat Flag shall be checked, and + * the next occurrence of the scheduled event is calculated and put in the + * queue. + */ + struct bt_mesh_scheduler_setup_srv *srv = model->user_data; + u8_t index = 0U, year = 0U, day = 0U, hour = 0U, minute = 0U, + second = 0U, day_of_week = 0U, action = 0U, trans_time = 0U; + u16_t month = 0U, scene_number = 0U; + u64_t value = 0U; + + if (srv == NULL || srv->state == NULL) { + BT_ERR("%s, Invalid model user_data", __func__); + return; + } + + value = net_buf_simple_pull_le32(buf); + value |= ((u64_t)net_buf_simple_pull_le32(buf) << 32); + + index = value & BIT_MASK(4); + year = (value >> 4) & BIT_MASK(7); + month = (value >> 11) & BIT_MASK(12); + day = (value >> 23) & BIT_MASK(5); + hour = (value >> 28) & BIT_MASK(5); + minute = (value >> 33) & BIT_MASK(6); + second = (value >> 39) & BIT_MASK(6); + day_of_week = (value >> 45) & BIT_MASK(7); + action = (value >> 52) & BIT_MASK(4); + trans_time = (value >> 56) & BIT_MASK(8); + + if (index > SCHEDULE_ENTRY_MAX_INDEX) { + BT_ERR("%s, Invalid Scheduler Register Entry index 0x%02x", __func__, index); + return; + } + + if (year > SCHEDULE_YEAR_ANY_YEAR) { + BT_ERR("%s, Invalid Scheduler Register year 0x%02x", __func__, year); + return; + } + + if (hour > SCHEDULE_HOUR_ONCE_A_DAY) { + BT_ERR("%s, Invalid Scheduler Register hour 0x%02x", __func__, hour); + return; + } + + if (action > SCHEDULE_ACT_SCENE_RECALL && action != SCHEDULE_ACT_NO_ACTION) { + BT_ERR("%s, Invalid Scheduler Register action 0x%02x", __func__, action); + return; + } + + scene_number = net_buf_simple_pull_le16(buf); + + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) { + bt_mesh_time_scene_server_recv_set_msg_t set = { + .scheduler_act_set.index = index, + .scheduler_act_set.year = year, + .scheduler_act_set.month = month, + .scheduler_act_set.day = day, + .scheduler_act_set.hour = hour, + .scheduler_act_set.minute = minute, + .scheduler_act_set.second = second, + .scheduler_act_set.day_of_week = day_of_week, + .scheduler_act_set.action = action, + .scheduler_act_set.trans_time = trans_time, + .scheduler_act_set.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_RECV_SET_MSG, model, ctx, (const u8_t *)&set, sizeof(set)); + return; + } + + srv->state->schedules[index].in_use = true; + srv->state->schedules[index].year = year; + srv->state->schedules[index].month = month; + srv->state->schedules[index].day = day; + srv->state->schedules[index].hour = hour; + srv->state->schedules[index].minute = minute; + srv->state->schedules[index].second = second; + srv->state->schedules[index].day_of_week = day_of_week; + srv->state->schedules[index].action = action; + srv->state->schedules[index].trans_time = trans_time; + srv->state->schedules[index].scene_number = scene_number; + + bt_mesh_time_scene_server_state_change_t change = { + .scheduler_act_set.index = index, + .scheduler_act_set.year = year, + .scheduler_act_set.month = month, + .scheduler_act_set.day = day, + .scheduler_act_set.hour = hour, + .scheduler_act_set.minute = minute, + .scheduler_act_set.second = second, + .scheduler_act_set.day_of_week = day_of_week, + .scheduler_act_set.action = action, + .scheduler_act_set.trans_time = trans_time, + .scheduler_act_set.scene_number = scene_number, + }; + bt_mesh_time_scene_server_cb_evt_to_btc( + BTC_BLE_MESH_EVT_TIME_SCENE_SERVER_STATE_CHANGE, model, ctx, (const u8_t *)&change, sizeof(change)); + + if (ctx->recv_op == BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET) { + send_scheduler_act_status(model, ctx, index); + } + + return; +} + +/* message handlers (End) */ + +/* Mapping of message handlers for Time Server (0x1200) */ +const struct bt_mesh_model_op time_srv_op[] = { + { BLE_MESH_MODEL_OP_TIME_GET, 0, time_get }, + { BLE_MESH_MODEL_OP_TIME_STATUS, 5, time_get }, + { BLE_MESH_MODEL_OP_TIME_ZONE_GET, 0, time_get }, + { BLE_MESH_MODEL_OP_TAI_UTC_DELTA_GET, 0, time_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Time Setup Server (0x1201) */ +const struct bt_mesh_model_op time_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_TIME_SET, 10, time_set }, + { BLE_MESH_MODEL_OP_TIME_ZONE_SET, 6, time_set }, + { BLE_MESH_MODEL_OP_TAI_UTC_DELTA_SET, 7, time_set }, + { BLE_MESH_MODEL_OP_TIME_ROLE_GET, 0, time_get }, + { BLE_MESH_MODEL_OP_TIME_ROLE_SET, 1, time_set }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Scene Server (0x1203) */ +const struct bt_mesh_model_op scene_srv_op[] = { + { BLE_MESH_MODEL_OP_SCENE_GET, 0, scene_get }, + { BLE_MESH_MODEL_OP_SCENE_RECALL, 3, scene_recall }, + { BLE_MESH_MODEL_OP_SCENE_RECALL_UNACK, 3, scene_recall }, + { BLE_MESH_MODEL_OP_SCENE_REGISTER_GET, 0, scene_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Scene Setup Server (0x1204) */ +const struct bt_mesh_model_op scene_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_SCENE_STORE, 2, scene_action }, + { BLE_MESH_MODEL_OP_SCENE_STORE_UNACK, 2, scene_action }, + { BLE_MESH_MODEL_OP_SCENE_DELETE, 2, scene_action }, + { BLE_MESH_MODEL_OP_SCENE_DELETE_UNACK, 2, scene_action }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Scheduler Server (0x1206) */ +const struct bt_mesh_model_op scheduler_srv_op[] = { + { BLE_MESH_MODEL_OP_SCHEDULER_GET, 0, scheduler_get }, + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_GET, 1, scheduler_get }, + BLE_MESH_MODEL_OP_END, +}; + +/* Mapping of message handlers for Scheduler Setup Server (0x1207) */ +const struct bt_mesh_model_op scheduler_setup_srv_op[] = { + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET, 10, scheduler_act_set }, + { BLE_MESH_MODEL_OP_SCHEDULER_ACT_SET_UNACK, 10, scheduler_act_set }, + BLE_MESH_MODEL_OP_END, +}; + +static int check_scene_server_init(struct bt_mesh_scenes_state *state) +{ + int i; + + if (state->scene_count == 0U || state->scenes == NULL) { + BT_ERR("%s, Invalid Scene state", __func__); + return -EINVAL; + } + + for (i = 0; i < state->scene_count; i++) { + if (state->scenes[i].scene_value == NULL) { + BT_ERR("%s, Invalid Scene value, index %d", __func__, i); + return -EINVAL; + } + } + + return 0; +} + +static int time_scene_server_init(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Time Scene Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_TIME_SRV: { + struct bt_mesh_time_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Time State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_TIME_SETUP_SRV: { + struct bt_mesh_time_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Time State", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_SCENE_SRV: { + struct bt_mesh_scene_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scene State", __func__); + return -EINVAL; + } + if (check_scene_server_init(srv->state)) { + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_alloc_ctx(&srv->transition.timer.work); + k_delayed_work_init(&srv->transition.timer, scene_recall_work_handler); + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_SCENE_SETUP_SRV: { + struct bt_mesh_scene_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scene State", __func__); + return -EINVAL; + } + if (check_scene_server_init(srv->state)) { + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_SCHEDULER_SRV: { + struct bt_mesh_scheduler_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scheduler State", __func__); + return -EINVAL; + } + if (srv->state->schedule_count == 0U || srv->state->schedules == NULL) { + BT_ERR("%s, NULL Register Schedule", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + case BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV: { + struct bt_mesh_scheduler_setup_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scheduler State", __func__); + return -EINVAL; + } + if (srv->state->schedule_count == 0U || srv->state->schedules == NULL) { + BT_ERR("%s, NULL Register Schedule", __func__); + return -EINVAL; + } + srv->model = model; + break; + } + default: + BT_WARN("%s, Unknown Time Scene Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_time_scene_server_mutex_new(); + + return 0; +} + +int bt_mesh_time_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Time Server has no publication support", __func__); + return -EINVAL; + } + + /** + * When this model is present on an Element, the corresponding Time Setup + * Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_TIME_SETUP_SRV) == NULL) { + BT_WARN("%s, Time Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return time_scene_server_init(model); +} + +int bt_mesh_time_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + /* This model does not support subscribing nor publishing */ + if (model->pub) { + BT_ERR("%s, Time Setup Server shall not support publication", __func__); + return -EINVAL; + } + + return time_scene_server_init(model); +} + +int bt_mesh_scene_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scene Server has no publication support", __func__); + return -EINVAL; + } + + /* The model may be present only on the Primary element of a node. */ + if (primary == false) { + BT_WARN("%s, Scene Server is not on the Primary element", __func__); + /* Just give a warning here, continue with the initialization */ + } + /** + * When this model is present on an Element, the corresponding Scene Setup + * Server model shall also be present. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SCENE_SETUP_SRV) == NULL) { + BT_WARN("%s, Scene Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return time_scene_server_init(model); +} + +int bt_mesh_scene_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + /* The model may be present only on the Primary element of a node. */ + if (primary == false) { + BT_WARN("%s, Scene Setup Server is not on the Primary element", __func__); + /* Just give a warning here, continue with the initialization */ + } + return time_scene_server_init(model); +} + +int bt_mesh_scheduler_srv_init(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scheduler Server has no publication support", __func__); + return -EINVAL; + } + + /* The model may be present only on the Primary element of a node. */ + if (primary == false) { + BT_WARN("%s, Scheduler Server is not on the Primary element", __func__); + /* Just give a warning here, continue with the initialization */ + } + /** + * When this model is present on an Element, the corresponding Scheduler + * Setup Server model shall also be present. The model requires the Time + * Server model shall be present on the element. + */ + struct bt_mesh_elem *element = bt_mesh_model_elem(model); + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV) == NULL) { + BT_WARN("%s, Scheduler Setup Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_TIME_SRV) == NULL) { + BT_WARN("%s, Time Server is not present", __func__); + /* Just give a warning here, continue with the initialization */ + } + return time_scene_server_init(model); +} + +int bt_mesh_scheduler_setup_srv_init(struct bt_mesh_model *model, bool primary) +{ + /* The model may be present only on the Primary element of a node. */ + if (primary == false) { + BT_WARN("%s, Scheduler Setup Server is not on the Primary element", __func__); + /* Just give a warning here, continue with the initialization */ + } + return time_scene_server_init(model); +} + +static int time_scene_server_deinit(struct bt_mesh_model *model) +{ + if (model->user_data == NULL) { + BT_ERR("%s, No Time Scene Server context provided, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + switch (model->id) { + case BLE_MESH_MODEL_ID_SCENE_SRV: { + struct bt_mesh_scene_srv *srv = model->user_data; + if (srv->state == NULL) { + BT_ERR("%s, NULL Scene State", __func__); + return -EINVAL; + } + if (check_scene_server_init(srv->state)) { + return -EINVAL; + } + if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) { + bt_mesh_server_free_ctx(&srv->transition.timer.work); + k_delayed_work_free(&srv->transition.timer); + } + break; + } + default: + BT_WARN("%s, Unknown Time Scene Server Model, model_id 0x%04x", __func__, model->id); + return -EINVAL; + } + + bt_mesh_time_scene_server_mutex_free(); + + return 0; +} + +int bt_mesh_time_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Time Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_time_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub) { + BT_ERR("%s, Time Setup Server shall not support publication", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scene_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scene Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scene_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_server_deinit(model); +} + +int bt_mesh_scheduler_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + if (model->pub == NULL) { + BT_ERR("%s, Scheduler Server has no publication support", __func__); + return -EINVAL; + } + + return time_scene_server_deinit(model); +} + +int bt_mesh_scheduler_setup_srv_deinit(struct bt_mesh_model *model, bool primary) +{ + return time_scene_server_deinit(model); +} diff --git a/components/bt/include/esp_bt.h b/components/bt/include/esp_bt.h index a0648645c..acf346aab 100644 --- a/components/bt/include/esp_bt.h +++ b/components/bt/include/esp_bt.h @@ -25,7 +25,7 @@ extern "C" { #endif -#define ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL 0x20190506 +#define ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL 0x20200106 /** * @brief Bluetooth mode for controller enable/disable @@ -96,6 +96,12 @@ the adv packet will be discarded until the memory is restored. */ #define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_BTDM #endif +#ifdef CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF +#define BTDM_CTRL_AUTO_LATENCY_EFF CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF +#else +#define BTDM_CTRL_AUTO_LATENCY_EFF false +#endif + #define BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT 9 //Maximum BLE connection limitation #define BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT 7 //Maximum ACL connection limitation #define BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT 3 //Maximum SCO/eSCO connection limitation @@ -117,7 +123,8 @@ the adv packet will be discarded until the memory is restored. */ .mode = BTDM_CONTROLLER_MODE_EFF, \ .ble_max_conn = CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF, \ .bt_max_acl_conn = CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_EFF, \ - .bt_sco_datapath = CONFIG_BTDM_CONTROLLER_BR_EDR_SCO_DATA_PATH_EFF, \ + .bt_sco_datapath = CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF, \ + .auto_latency = BTDM_CTRL_AUTO_LATENCY_EFF, \ .bt_max_sync_conn = CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_EFF, \ .magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL, \ }; @@ -149,6 +156,7 @@ typedef struct { uint8_t ble_max_conn; /*!< BLE maximum connection numbers */ uint8_t bt_max_acl_conn; /*!< BR/EDR maximum ACL connection numbers */ uint8_t bt_sco_datapath; /*!< SCO data path, i.e. HCI or PCM module */ + bool auto_latency; /*!< BLE auto latency, used to enhance classic BT performance */ /* * Following parameters can not be configured runtime when call esp_bt_controller_init() * It will be overwrite with a constant value which in menuconfig or from a macro. diff --git a/components/bt/lib b/components/bt/lib index f099e78fc..63e7a37c6 160000 --- a/components/bt/lib +++ b/components/bt/lib @@ -1 +1 @@ -Subproject commit f099e78fc9c19cc5b527b785215abcce52080e39 +Subproject commit 63e7a37c6c6c5647ed09ff5196c0b76ebd98de16 diff --git a/components/driver/can.c b/components/driver/can.c index 25b91ebaf..ddfd1e5d6 100644 --- a/components/driver/can.c +++ b/components/driver/can.c @@ -44,12 +44,26 @@ #define CAN_RESET_FLAG(var, mask) ((var) &= ~(mask)) #define CAN_TAG "CAN" +/* + * Baud Rate Prescaler Divider config/values. The BRP_DIV bit is located in the + * CAN interrupt enable register, and is only available in ESP32 Revision 2 or + * later. Setting this bit will cause the APB clock to be prescaled (divided) by + * a factor 2, before having the BRP applied. This will allow for lower bit rates + * to be achieved. + */ +#define BRP_DIV_EN_THRESH 128 //A BRP config value large this this will need to enable brp_div +#define BRP_DIV_EN_BIT 0x10 //Bit mask for brp_div in the interrupt register +//When brp_div is enabled, the BRP config value must be any multiple of 4 between 132 and 256 +#define BRP_CHECK_WITH_DIV(brp) ((brp) >= 132 && (brp) <= 256 && ((brp) & 0x3) == 0) +//When brp_div is disabled, the BRP config value must be any even number between 2 to 128 +#define BRP_CHECK_NO_DIV(brp) ((brp) >= 2 && (brp) <= 128 && ((brp) & 0x1) == 0) + //Driver default config/values #define DRIVER_DEFAULT_EWL 96 //Default Error Warning Limit value #define DRIVER_DEFAULT_TEC 0 //TX Error Counter starting value #define DRIVER_DEFAULT_REC 0 //RX Error Counter starting value #define DRIVER_DEFAULT_CLKOUT_DIV 14 //APB CLK divided by two -#define DRIVER_DEFAULT_INTERRUPTS 0xE7 //Exclude data overrun +#define DRIVER_DEFAULT_INTERRUPTS 0xE7 //Exclude data overrun (bit[3]) and brp_div (bit[4]) #define DRIVER_DEFAULT_ERR_PASS_CNT 128 //Error counter threshold for error passive //Command Bit Masks @@ -137,8 +151,10 @@ typedef struct { static can_obj_t *p_can_obj = NULL; static portMUX_TYPE can_spinlock = portMUX_INITIALIZER_UNLOCKED; -#define CAN_ENTER_CRITICAL() portENTER_CRITICAL(&can_spinlock) -#define CAN_EXIT_CRITICAL() portEXIT_CRITICAL(&can_spinlock) +#define CAN_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&can_spinlock) +#define CAN_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&can_spinlock) +#define CAN_ENTER_CRITICAL() portENTER_CRITICAL(&can_spinlock) +#define CAN_EXIT_CRITICAL() portEXIT_CRITICAL(&can_spinlock) /* ------------------- Configuration Register Functions---------------------- */ @@ -199,7 +215,7 @@ static inline void can_config_bus_timing(uint32_t brp, uint32_t sjw, uint32_t ts - SJW (1 to 4) is number of T_scl to shorten/lengthen for bit synchronization - TSEG_1 (1 to 16) is number of T_scl in a bit time before sample point - TSEG_2 (1 to 8) is number of T_scl in a bit time after sample point - - triple_sampling will cause each bit time to be sampled 3 times*/ + - triple_sampling will cause each bit time to be sampled 3 times */ can_bus_tim_0_reg_t timing_reg_0; can_bus_tim_1_reg_t timing_reg_1; timing_reg_0.baud_rate_prescaler = (brp / 2) - 1; @@ -376,7 +392,8 @@ static void can_intr_handler_err_warn(can_status_reg_t *status, BaseType_t *task can_alert_handler(CAN_ALERT_ABOVE_ERR_WARN, alert_req); } else if (p_can_obj->control_flags & CTRL_FLAG_RECOVERING) { //Bus recovery complete. - can_enter_reset_mode(); + esp_err_t err = can_enter_reset_mode(); + assert(err == ESP_OK); //Reset and set flags to the equivalent of the stopped state CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING | CTRL_FLAG_ERR_WARN | CTRL_FLAG_ERR_PASSIVE | CTRL_FLAG_BUS_OFF | @@ -454,13 +471,17 @@ static void can_intr_handler_tx(can_status_reg_t *status, int *alert_req) //Update TX message count p_can_obj->tx_msg_count--; - configASSERT(p_can_obj->tx_msg_count >= 0); //Sanity check + assert(p_can_obj->tx_msg_count >= 0); //Sanity check //Check if there are more frames to transmit if (p_can_obj->tx_msg_count > 0 && p_can_obj->tx_queue != NULL) { can_frame_t frame; - configASSERT(xQueueReceiveFromISR(p_can_obj->tx_queue, &frame, NULL) == pdTRUE); - can_set_tx_buffer_and_transmit(&frame); + int res = xQueueReceiveFromISR(p_can_obj->tx_queue, &frame, NULL); + if (res == pdTRUE) { + can_set_tx_buffer_and_transmit(&frame); + } else { + assert(false && "failed to get a frame from TX queue"); + } } else { //No more frames to transmit CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED); @@ -475,7 +496,7 @@ static void can_intr_handler_main(void *arg) can_status_reg_t status; can_intr_reg_t intr_reason; - CAN_ENTER_CRITICAL(); + CAN_ENTER_CRITICAL_ISR(); status.val = can_get_status(); intr_reason.val = (p_can_obj != NULL) ? can_get_interrupt_reason() : 0; //Incase intr occurs whilst driver is being uninstalled @@ -510,7 +531,7 @@ static void can_intr_handler_main(void *arg) } /* Todo: Check possible bug where transmitting self reception request then clearing rx buffer will cancel the transmission. */ - CAN_EXIT_CRITICAL(); + CAN_EXIT_CRITICAL_ISR(); if (p_can_obj->alert_semphr != NULL && alert_req) { //Give semaphore if alerts were triggered @@ -625,6 +646,12 @@ esp_err_t can_driver_install(const can_general_config_t *g_config, const can_tim CAN_CHECK(g_config->rx_queue_len > 0, ESP_ERR_INVALID_ARG); CAN_CHECK(g_config->tx_io >= 0 && g_config->tx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG); CAN_CHECK(g_config->rx_io >= 0 && g_config->rx_io < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG); +#if (CONFIG_ESP32_REV_MIN >= 2) + //ESP32 revision 2 or later chips have a brp_div bit. Check that the BRP config value is valid when brp_div is enabled or disabled + CAN_CHECK(BRP_CHECK_WITH_DIV(t_config->brp) || BRP_CHECK_NO_DIV(t_config->brp), ESP_ERR_INVALID_ARG); +#else + CAN_CHECK(BRP_CHECK_NO_DIV(t_config->brp), ESP_ERR_INVALID_ARG); +#endif esp_err_t ret; can_obj_t *p_can_obj_dummy; @@ -673,14 +700,22 @@ esp_err_t can_driver_install(const can_general_config_t *g_config, const can_tim ret = ESP_ERR_INVALID_STATE; goto err; } + periph_module_reset(PERIPH_CAN_MODULE); periph_module_enable(PERIPH_CAN_MODULE); //Enable APB CLK to CAN peripheral - configASSERT(can_enter_reset_mode() == ESP_OK); //Must enter reset mode to write to config registers + esp_err_t err = can_enter_reset_mode(); //Must enter reset mode to write to config registers + assert(err == ESP_OK); can_config_pelican(); //Use PeliCAN addresses /* Note: REC is allowed to increase even in reset mode. Listen only mode will freeze REC. The desired mode will be set when can_start() is called. */ can_config_mode(CAN_MODE_LISTEN_ONLY); +#if (CONFIG_ESP32_REV_MIN >= 2) + //If the BRP config value is large enough, the brp_div bit must be enabled to achieve the same effective baud rate prescaler + can_config_interrupts((t_config->brp > BRP_DIV_EN_THRESH) ? DRIVER_DEFAULT_INTERRUPTS | BRP_DIV_EN_BIT : DRIVER_DEFAULT_INTERRUPTS); + can_config_bus_timing((t_config->brp > BRP_DIV_EN_THRESH) ? t_config->brp/2 : t_config->brp, t_config->sjw, t_config->tseg_1, t_config->tseg_2, t_config->triple_sampling); +#else can_config_interrupts(DRIVER_DEFAULT_INTERRUPTS); can_config_bus_timing(t_config->brp, t_config->sjw, t_config->tseg_1, t_config->tseg_2, t_config->triple_sampling); +#endif can_config_error(DRIVER_DEFAULT_EWL, DRIVER_DEFAULT_REC, DRIVER_DEFAULT_TEC); can_config_acceptance_filter(f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter); can_config_clk_out(g_config->clkout_divider); @@ -729,7 +764,8 @@ esp_err_t can_driver_uninstall() //Check state CAN_CHECK_FROM_CRIT(p_can_obj != NULL, ESP_ERR_INVALID_STATE); CAN_CHECK_FROM_CRIT(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF), ESP_ERR_INVALID_STATE); - configASSERT(can_enter_reset_mode() == ESP_OK); //Enter reset mode to stop any CAN bus activity + esp_err_t err = can_enter_reset_mode(); //Enter reset mode to stop any CAN bus activity + assert(err == ESP_OK); //Clear registers by reading (void) can_get_interrupt_reason(); (void) can_get_arbitration_lost_capture(); @@ -767,7 +803,8 @@ esp_err_t can_start() //Reset RX queue, and RX message count xQueueReset(p_can_obj->rx_queue); p_can_obj->rx_msg_count = 0; - configASSERT(can_enter_reset_mode() == ESP_OK); //Should already be in bus-off mode, set again to make sure + esp_err_t err = can_enter_reset_mode(); //Should already be in bus-off mode, set again to make sure + assert(err == ESP_OK); //Currently in listen only mode, need to set to mode specified by configuration can_mode_t mode; @@ -780,7 +817,8 @@ esp_err_t can_start() } can_config_mode(mode); //Set mode (void) can_get_interrupt_reason(); //Clear interrupt register - configASSERT(can_exit_reset_mode() == ESP_OK); + err = can_exit_reset_mode(); + assert(err == ESP_OK); CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_STOPPED); CAN_EXIT_CRITICAL(); @@ -795,7 +833,8 @@ esp_err_t can_stop() CAN_CHECK_FROM_CRIT(!(p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)), ESP_ERR_INVALID_STATE); //Clear interrupts and reset flags - configASSERT(can_enter_reset_mode() == ESP_OK); + esp_err_t err = can_enter_reset_mode(); + assert(err == ESP_OK); (void) can_get_interrupt_reason(); //Read interrupt register to clear interrupts can_config_mode(CAN_MODE_LISTEN_ONLY); //Set to listen only mode to freeze REC CAN_RESET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED); @@ -846,11 +885,13 @@ esp_err_t can_transmit(const can_message_t *message, TickType_t ticks_to_wait) CAN_ENTER_CRITICAL(); if (p_can_obj->control_flags & (CTRL_FLAG_STOPPED | CTRL_FLAG_BUS_OFF)) { //TX queue was reset (due to stop/bus_off), remove copied frame from queue to prevent transmission - configASSERT(xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0) == pdTRUE); + int res = xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0); + assert(res == pdTRUE); ret = ESP_ERR_INVALID_STATE; } else if ((p_can_obj->tx_msg_count == 0) && !(p_can_obj->control_flags & CTRL_FLAG_TX_BUFF_OCCUPIED)) { //TX buffer was freed during copy, manually trigger transmission - configASSERT(xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0) == pdTRUE); + int res = xQueueReceive(p_can_obj->tx_queue, &tx_frame, 0); + assert(res == pdTRUE); can_set_tx_buffer_and_transmit(&tx_frame); p_can_obj->tx_msg_count++; CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_TX_BUFF_OCCUPIED); @@ -941,7 +982,8 @@ esp_err_t can_initiate_recovery() CAN_SET_FLAG(p_can_obj->control_flags, CTRL_FLAG_RECOVERING); //Trigger start of recovery process - configASSERT(can_exit_reset_mode() == ESP_OK); + esp_err_t err = can_exit_reset_mode(); + assert(err == ESP_OK); CAN_EXIT_CRITICAL(); return ESP_OK; diff --git a/components/driver/gpio.c b/components/driver/gpio.c index 302aa850f..d4651e0c6 100644 --- a/components/driver/gpio.c +++ b/components/driver/gpio.c @@ -22,21 +22,36 @@ #include "soc/soc.h" #include "esp_log.h" #include "soc/gpio_periph.h" +#if !CONFIG_FREERTOS_UNICORE +#include "esp_ipc.h" +#endif -static const char* GPIO_TAG = "gpio"; #define GPIO_CHECK(a, str, ret_val) \ if (!(a)) { \ ESP_LOGE(GPIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \ return (ret_val); \ } +#define GPIO_ISR_CORE_ID_UNINIT (3) typedef struct { gpio_isr_t fn; /*!< isr function */ void* args; /*!< isr function args */ } gpio_isr_func_t; +// Used by the IPC call to register the interrupt service routine. +typedef struct { + int source; /*!< ISR source */ + int intr_alloc_flags; /*!< ISR alloc flag */ + void (*fn)(void*); /*!< ISR function */ + void *arg; /*!< ISR function args*/ + void *handle; /*!< ISR handle */ + esp_err_t ret; +} gpio_isr_alloc_t; + +static const char* GPIO_TAG = "gpio"; static gpio_isr_func_t* gpio_isr_func = NULL; static gpio_isr_handle_t gpio_isr_handle; +static uint32_t isr_core_id = GPIO_ISR_CORE_ID_UNINIT; static portMUX_TYPE gpio_spinlock = portMUX_INITIALIZER_UNLOCKED; esp_err_t gpio_pullup_en(gpio_num_t gpio_num) @@ -102,7 +117,6 @@ static void gpio_intr_status_clr(gpio_num_t gpio_num) static esp_err_t gpio_intr_enable_on_core (gpio_num_t gpio_num, uint32_t core_id) { - GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG); gpio_intr_status_clr(gpio_num); if (core_id == 0) { GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr @@ -114,7 +128,13 @@ static esp_err_t gpio_intr_enable_on_core (gpio_num_t gpio_num, uint32_t core_id esp_err_t gpio_intr_enable(gpio_num_t gpio_num) { - return gpio_intr_enable_on_core (gpio_num, xPortGetCoreID()); + GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG); + portENTER_CRITICAL(&gpio_spinlock); + if(isr_core_id == GPIO_ISR_CORE_ID_UNINIT) { + isr_core_id = xPortGetCoreID(); + } + portEXIT_CRITICAL(&gpio_spinlock); + return gpio_intr_enable_on_core (gpio_num, isr_core_id); } esp_err_t gpio_intr_disable(gpio_num_t gpio_num) @@ -332,11 +352,9 @@ void IRAM_ATTR gpio_intr_service(void* arg) //GPIO intr process uint32_t gpio_num = 0; //read status to get interrupt status for GPIO0-31 - uint32_t gpio_intr_status; - gpio_intr_status = GPIO.status; + const uint32_t gpio_intr_status = (isr_core_id == 0) ? GPIO.pcpu_int : GPIO.acpu_int; //read status1 to get interrupt status for GPIO32-39 - uint32_t gpio_intr_status_h; - gpio_intr_status_h = GPIO.status1.intr_st; + const uint32_t gpio_intr_status_h = (isr_core_id == 0) ? GPIO.pcpu_int1.intr : GPIO.acpu_int1.intr; if (gpio_isr_func == NULL) { return; @@ -395,12 +413,12 @@ esp_err_t gpio_install_isr_service(int intr_alloc_flags) esp_err_t ret; portENTER_CRITICAL(&gpio_spinlock); gpio_isr_func = (gpio_isr_func_t*) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t)); + portEXIT_CRITICAL(&gpio_spinlock); if (gpio_isr_func == NULL) { ret = ESP_ERR_NO_MEM; } else { ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_isr_handle); } - portEXIT_CRITICAL(&gpio_spinlock); return ret; } @@ -413,14 +431,43 @@ void gpio_uninstall_isr_service() esp_intr_free(gpio_isr_handle); free(gpio_isr_func); gpio_isr_func = NULL; + isr_core_id = GPIO_ISR_CORE_ID_UNINIT; portEXIT_CRITICAL(&gpio_spinlock); return; } +static void gpio_isr_register_on_core_static(void *param) +{ + gpio_isr_alloc_t *p = (gpio_isr_alloc_t *)param; + //We need to check the return value. + p->ret = esp_intr_alloc(p->source, p->intr_alloc_flags, p->fn, p->arg, p->handle); +} + esp_err_t gpio_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, gpio_isr_handle_t *handle) { GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG); - return esp_intr_alloc(ETS_GPIO_INTR_SOURCE, intr_alloc_flags, fn, arg, handle); + gpio_isr_alloc_t p; + p.source = ETS_GPIO_INTR_SOURCE; + p.intr_alloc_flags = intr_alloc_flags; + p.fn = fn; + p.arg = arg; + p.handle = handle; + portENTER_CRITICAL(&gpio_spinlock); + if(isr_core_id == GPIO_ISR_CORE_ID_UNINIT) { + isr_core_id = xPortGetCoreID(); + } + portEXIT_CRITICAL(&gpio_spinlock); + esp_err_t ret; +#if CONFIG_FREERTOS_UNICORE + gpio_isr_register_on_core_static(&p); + ret = ESP_OK; +#else /* CONFIG_FREERTOS_UNICORE */ + ret = esp_ipc_call_blocking(isr_core_id, gpio_isr_register_on_core_static, (void *)&p); +#endif /* !CONFIG_FREERTOS_UNICORE */ + if(ret != ESP_OK || p.ret != ESP_OK) { + return ESP_ERR_NOT_FOUND; + } + return ESP_OK; } esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type) @@ -444,8 +491,13 @@ esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type) esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num) { GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG); - GPIO.pin[gpio_num].wakeup_enable = 0; - return ESP_OK; + esp_err_t ret = ESP_OK; + if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) { + ret = rtc_gpio_wakeup_disable(gpio_num); + } else { + GPIO.pin[gpio_num].wakeup_enable = 0; + } + return ret; } esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength) diff --git a/components/driver/i2c.c b/components/driver/i2c.c index 7c693ef88..3c8cb544b 100644 --- a/components/driver/i2c.c +++ b/components/driver/i2c.c @@ -29,6 +29,7 @@ #include "driver/i2c.h" #include "driver/gpio.h" #include "driver/periph_ctrl.h" +#include "esp_pm.h" static const char* I2C_TAG = "i2c"; #define I2C_CHECK(a, str, ret) if(!(a)) { \ @@ -67,6 +68,7 @@ static DRAM_ATTR i2c_dev_t* const I2C[I2C_NUM_MAX] = { &I2C0, &I2C1 }; #define I2C_ACK_TYPE_ERR_STR "i2c ack type error" #define I2C_DATA_LEN_ERR_STR "i2c data read length error" #define I2C_PSRAM_BUFFER_WARN_STR "Using buffer allocated from psram" +#define I2C_LOCK_ERR_STR "Power lock creation error" #define I2C_FIFO_FULL_THRESH_VAL (28) #define I2C_FIFO_EMPTY_THRESH_VAL (5) #define I2C_IO_INIT_LEVEL (1) @@ -134,6 +136,9 @@ typedef struct { StaticQueue_t evt_queue_buffer; /*!< The buffer that will hold the queue structure*/ #endif xSemaphoreHandle cmd_mux; /*!< semaphore to lock command process */ +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_handle_t pm_lock; +#endif size_t tx_fifo_remain; /*!< tx fifo remain length, for master mode */ size_t rx_fifo_remain; /*!< rx fifo remain length, for master mode */ @@ -226,6 +231,12 @@ esp_err_t i2c_driver_install(i2c_port_t i2c_num, i2c_mode_t mode, size_t slv_rx_ } else { //semaphore to sync sending process, because we only have 32 bytes for hardware fifo. p_i2c->cmd_mux = xSemaphoreCreateMutex(); +#ifdef CONFIG_PM_ENABLE + if (esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2c_driver", &p_i2c->pm_lock) != ESP_OK) { + ESP_LOGE(I2C_TAG, I2C_LOCK_ERR_STR); + goto err; + } +#endif #if !CONFIG_SPIRAM_USE_MALLOC p_i2c->cmd_evt_queue = xQueueCreate(I2C_EVT_QUEUE_LEN, sizeof(i2c_cmd_evt_t)); #else @@ -297,6 +308,12 @@ esp_err_t i2c_driver_install(i2c_port_t i2c_num, i2c_mode_t mode, size_t slv_rx_ if (p_i2c_obj[i2c_num]->slv_tx_mux) { vSemaphoreDelete(p_i2c_obj[i2c_num]->slv_tx_mux); } +#ifdef CONFIG_PM_ENABLE + if (p_i2c_obj[i2c_num]->pm_lock) { + esp_pm_lock_delete(p_i2c_obj[i2c_num]->pm_lock); + p_i2c_obj[i2c_num]->pm_lock = NULL; + } +#endif #if CONFIG_SPIRAM_USE_MALLOC if (p_i2c_obj[i2c_num]->evt_queue_storage) { free(p_i2c_obj[i2c_num]->evt_queue_storage); @@ -365,6 +382,12 @@ esp_err_t i2c_driver_delete(i2c_port_t i2c_num) p_i2c->tx_ring_buf = NULL; p_i2c->tx_buf_length = 0; } +#ifdef CONFIG_PM_ENABLE + if (p_i2c->pm_lock) { + esp_pm_lock_delete(p_i2c->pm_lock); + p_i2c->pm_lock = NULL; + } +#endif #if CONFIG_SPIRAM_USE_MALLOC if (p_i2c_obj[i2c_num]->evt_queue_storage) { free(p_i2c_obj[i2c_num]->evt_queue_storage); @@ -1261,6 +1284,9 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle, i2c_obj_t* p_i2c = p_i2c_obj[i2c_num]; portTickType ticks_start = xTaskGetTickCount(); portBASE_TYPE res = xSemaphoreTake(p_i2c->cmd_mux, ticks_to_wait); +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_acquire(p_i2c->pm_lock); +#endif if (res == pdFALSE) { return ESP_ERR_TIMEOUT; } @@ -1338,6 +1364,9 @@ esp_err_t i2c_master_cmd_begin(i2c_port_t i2c_num, i2c_cmd_handle_t cmd_handle, } } p_i2c->status = I2C_STATUS_DONE; +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_release(p_i2c->pm_lock); +#endif xSemaphoreGive(p_i2c->cmd_mux); return ret; } @@ -1351,13 +1380,18 @@ int i2c_slave_write_buffer(i2c_port_t i2c_num, uint8_t* data, int size, TickType portBASE_TYPE res; int cnt = 0; - portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait; + portTickType ticks_start = xTaskGetTickCount(); res = xSemaphoreTake(p_i2c->slv_tx_mux, ticks_to_wait); if (res == pdFALSE) { return 0; } - ticks_to_wait = ticks_end - xTaskGetTickCount(); + TickType_t ticks_end = xTaskGetTickCount(); + if (ticks_end - ticks_start > ticks_to_wait) { + ticks_to_wait = 0; + } else { + ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start); + } res = xRingbufferSend(p_i2c->tx_ring_buf, data, size, ticks_to_wait); if (res == pdFALSE) { cnt = 0; @@ -1392,18 +1426,28 @@ int i2c_slave_read_buffer(i2c_port_t i2c_num, uint8_t* data, size_t max_size, Ti i2c_obj_t* p_i2c = p_i2c_obj[i2c_num]; portBASE_TYPE res; - portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait; + portTickType ticks_start = xTaskGetTickCount(); res = xSemaphoreTake(p_i2c->slv_rx_mux, ticks_to_wait); if (res == pdFALSE) { return 0; } - ticks_to_wait = ticks_end - xTaskGetTickCount(); + TickType_t ticks_end = xTaskGetTickCount(); + if (ticks_end - ticks_start > ticks_to_wait) { + ticks_to_wait = 0; + } else { + ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start); + } int cnt = i2c_slave_read(i2c_num, data, max_size, ticks_to_wait); if (cnt > 0) { I2C_ENTER_CRITICAL(&i2c_spinlock[i2c_num]); I2C[i2c_num]->int_ena.rx_fifo_full = 1; I2C_EXIT_CRITICAL(&i2c_spinlock[i2c_num]); - ticks_to_wait = ticks_end - xTaskGetTickCount(); + ticks_end = xTaskGetTickCount(); + if (ticks_end - ticks_start > ticks_to_wait) { + ticks_to_wait = 0; + } else { + ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start); + } if (cnt < max_size && ticks_to_wait > 0) { cnt += i2c_slave_read(i2c_num, data + cnt, max_size - cnt, ticks_to_wait); } diff --git a/components/driver/i2s.c b/components/driver/i2s.c index 7559a6c56..843539593 100644 --- a/components/driver/i2s.c +++ b/components/driver/i2s.c @@ -36,8 +36,10 @@ #include "esp_intr.h" #include "esp_err.h" #include "esp_log.h" +#include "esp_pm.h" static const char* I2S_TAG = "I2S"; + #define I2S_CHECK(a, str, ret) if (!(a)) { \ ESP_LOGE(I2S_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ return (ret); \ @@ -91,10 +93,14 @@ typedef struct { bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */ int fixed_mclk; /*!< I2S fixed MLCK clock */ double real_rate; +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_handle_t pm_lock; +#endif } i2s_obj_t; static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0}; -static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1}; +/* DRAM_ATTR is required to avoid I2S array placed in flash, due to accessed from ISR */ +static DRAM_ATTR i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1}; static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED}; static int _i2s_adc_unit = -1; static int _i2s_adc_channel = -1; @@ -543,7 +549,6 @@ static void IRAM_ATTR i2s_intr_handler_default(void *arg) } xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken); } - } if (i2s_reg->int_st.in_suc_eof && p_i2s->rx) { @@ -885,12 +890,6 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co I2S_CHECK(!((i2s_config->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG); I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG); - if (i2s_num == I2S_NUM_1) { - periph_module_enable(PERIPH_I2S1_MODULE); - } else { - periph_module_enable(PERIPH_I2S0_MODULE); - } - if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) { //in ADC built-in mode, we need to call i2s_set_adc_mode to //initialize the specific ADC channel. @@ -943,7 +942,7 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co I2S[i2s_num]->conf.rx_start = 0; if (i2s_config->mode & I2S_MODE_TX) { - I2S[i2s_num]->conf.tx_msb_right = 0; + I2S[i2s_num]->conf.tx_msb_right = 1; I2S[i2s_num]->conf.tx_right_first = 0; I2S[i2s_num]->conf.tx_slave_mod = 0; // Master @@ -955,7 +954,7 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co } if (i2s_config->mode & I2S_MODE_RX) { - I2S[i2s_num]->conf.rx_msb_right = 0; + I2S[i2s_num]->conf.rx_msb_right = 1; I2S[i2s_num]->conf.rx_right_first = 0; I2S[i2s_num]->conf.rx_slave_mod = 0; // Master I2S[i2s_num]->fifo_conf.rx_fifo_mod_force_en = 1; @@ -1082,16 +1081,37 @@ esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1; +#ifdef CONFIG_PM_ENABLE + if (i2s_config->use_apll) { + err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock); + } else { + err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock); + } + if (err != ESP_OK) { + free(p_i2s_obj[i2s_num]); + p_i2s_obj[i2s_num] = NULL; + ESP_LOGE(I2S_TAG, "I2S pm lock error"); + return err; + } +#endif //CONFIG_PM_ENABLE + //To make sure hardware is enabled before any hardware register operations. if (i2s_num == I2S_NUM_1) { + periph_module_reset(PERIPH_I2S1_MODULE); periph_module_enable(PERIPH_I2S1_MODULE); } else { + periph_module_reset(PERIPH_I2S0_MODULE); periph_module_enable(PERIPH_I2S0_MODULE); } //initial interrupt err = i2s_isr_register(i2s_num, i2s_config->intr_alloc_flags, i2s_intr_handler_default, p_i2s_obj[i2s_num], &p_i2s_obj[i2s_num]->i2s_isr_handle); if (err != ESP_OK) { +#ifdef CONFIG_PM_ENABLE + if (p_i2s_obj[i2s_num]->pm_lock) { + esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock); + } +#endif free(p_i2s_obj[i2s_num]); p_i2s_obj[i2s_num] = NULL; ESP_LOGE(I2S_TAG, "Register I2S Interrupt error"); @@ -1147,6 +1167,11 @@ esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num) if(p_i2s_obj[i2s_num]->use_apll) { rtc_clk_apll_enable(0, 0, 0, 0, 0); } +#ifdef CONFIG_PM_ENABLE + if (p_i2s_obj[i2s_num]->pm_lock) { + esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock); + } +#endif free(p_i2s_obj[i2s_num]); p_i2s_obj[i2s_num] = NULL; @@ -1180,6 +1205,9 @@ esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *by I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG); I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG); xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY); +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock); +#endif src_byte = (char *)src; while (size > 0) { if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) { @@ -1201,6 +1229,10 @@ esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *by p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write; (*bytes_written) += bytes_can_write; } +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock); +#endif + xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux); return ESP_OK; } @@ -1311,6 +1343,9 @@ esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_re I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG); I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG); xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY); +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock); +#endif while (size > 0) { if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) { if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) { @@ -1330,6 +1365,9 @@ esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_re p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read; (*bytes_read) += bytes_can_read; } +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock); +#endif xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux); return ESP_OK; } @@ -1359,5 +1397,3 @@ int i2s_pop_sample(i2s_port_t i2s_num, void *sample, TickType_t ticks_to_wait) return bytes_pop; } } - - diff --git a/components/driver/include/driver/can.h b/components/driver/include/driver/can.h index da0613aac..28d42077f 100644 --- a/components/driver/include/driver/can.h +++ b/components/driver/include/driver/can.h @@ -45,7 +45,13 @@ extern "C" { * The following initializer macros offer commonly found bit rates. * * @note These timing values are based on the assumption APB clock is at 80MHz + * @note The 20K, 16K and 12.5K bit rates are only available from ESP32 Revision 2 onwards */ +#if (CONFIG_ESP32_REV_MIN >= 2) +#define CAN_TIMING_CONFIG_12_5KBITS() {.brp = 256, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} +#define CAN_TIMING_CONFIG_16KBITS() {.brp = 200, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} +#define CAN_TIMING_CONFIG_20KBITS() {.brp = 200, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} +#endif #define CAN_TIMING_CONFIG_25KBITS() {.brp = 128, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false} #define CAN_TIMING_CONFIG_50KBITS() {.brp = 80, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} #define CAN_TIMING_CONFIG_100KBITS() {.brp = 40, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false} @@ -153,7 +159,8 @@ typedef struct { * @note Macro initializers are available for this structure */ typedef struct { - uint8_t brp; /**< Baudrate prescaler (APB clock divider, even number from 2 to 128) */ + uint32_t brp; /**< Baudrate prescaler (i.e., APB clock divider) can be any even number from 2 to 128. + For ESP32 Rev 2 or later, multiples of 4 from 132 to 256 are also supported */ uint8_t tseg_1; /**< Timing segment 1 (Number of time quanta, between 1 to 16) */ uint8_t tseg_2; /**< Timing segment 2 (Number of time quanta, 1 to 8) */ uint8_t sjw; /**< Synchronization Jump Width (Max time quanta jump for synchronize from 1 to 4) */ diff --git a/components/driver/include/driver/ledc.h b/components/driver/include/driver/ledc.h index 6a82c19a2..f54f5acd1 100644 --- a/components/driver/include/driver/ledc.h +++ b/components/driver/include/driver/ledc.h @@ -171,6 +171,19 @@ esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf); */ esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel); +/** + * @brief Set LEDC output gpio. + * + * @param gpio_num The LEDC output gpio + * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode + * @param ledc_channel LEDC channel (0-7), select from ledc_channel_t + * + * @return + * - ESP_OK Success + * - ESP_ERR_INVALID_ARG Parameter error + */ +esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel); + /** * @brief LEDC stop. * Disable LEDC output, and set idle level diff --git a/components/driver/include/driver/sigmadelta.h b/components/driver/include/driver/sigmadelta.h index 76237c193..b62d937a5 100644 --- a/components/driver/include/driver/sigmadelta.h +++ b/components/driver/include/driver/sigmadelta.h @@ -19,7 +19,7 @@ #include "soc/gpio_sd_reg.h" #include "driver/gpio.h" -#ifdef _cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -102,7 +102,7 @@ esp_err_t sigmadelta_set_prescale(sigmadelta_channel_t channel, uint8_t prescale */ esp_err_t sigmadelta_set_pin(sigmadelta_channel_t channel, gpio_num_t gpio_num); -#ifdef _cplusplus +#ifdef __cplusplus } #endif diff --git a/components/driver/include/driver/spi_common.h b/components/driver/include/driver/spi_common.h index 02a757bda..c2d645566 100644 --- a/components/driver/include/driver/spi_common.h +++ b/components/driver/include/driver/spi_common.h @@ -42,11 +42,11 @@ extern "C" * * Then points tx_buffer to ``&data``. * - * @param data Data to be sent, can be uint8_t, uint16_t or uint32_t. @param - * len Length of data to be sent, since the SPI peripheral sends from the MSB, - * this helps to shift the data to the MSB. + * @param DATA Data to be sent, can be uint8_t, uint16_t or uint32_t. + * @param LEN Length of data to be sent, since the SPI peripheral sends from + * the MSB, this helps to shift the data to the MSB. */ -#define SPI_SWAP_DATA_TX(data, len) __builtin_bswap32((uint32_t)data<<(32-len)) +#define SPI_SWAP_DATA_TX(DATA, LEN) __builtin_bswap32((uint32_t)(DATA)<<(32-(LEN))) /** * Transform received data of length <= 32 bits to the format of an unsigned integer. @@ -55,11 +55,11 @@ extern "C" * * uint16_t data = SPI_SWAP_DATA_RX(*(uint32_t*)t->rx_data, 15); * - * @param data Data to be rearranged, can be uint8_t, uint16_t or uint32_t. - * @param len Length of data received, since the SPI peripheral writes from + * @param DATA Data to be rearranged, can be uint8_t, uint16_t or uint32_t. + * @param LEN Length of data received, since the SPI peripheral writes from * the MSB, this helps to shift the data to the LSB. */ -#define SPI_SWAP_DATA_RX(data, len) (__builtin_bswap32(data)>>(32-len)) +#define SPI_SWAP_DATA_RX(DATA, LEN) (__builtin_bswap32(DATA)>>(32-(LEN))) /** * @brief Enum with the three SPI peripherals that are software-accessible in it diff --git a/components/driver/mcpwm.c b/components/driver/mcpwm.c index 6eeb935f3..1ba554391 100644 --- a/components/driver/mcpwm.c +++ b/components/driver/mcpwm.c @@ -79,7 +79,7 @@ esp_err_t mcpwm_gpio_init(mcpwm_unit_t mcpwm_num, mcpwm_io_signals_t io_signal, MCPWM_CHECK((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num)), MCPWM_GPIO_ERROR, ESP_ERR_INVALID_ARG); gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT); gpio_matrix_out(gpio_num, PWM1_OUT0A_IDX + io_signal, 0, 0); - } else if (io_signal >= MCPWM_SYNC_0 && io_signal < MCPWM_FAULT_2) { + } else if (io_signal >= MCPWM_SYNC_0 && io_signal <= MCPWM_FAULT_2) { gpio_set_direction(gpio_num, GPIO_MODE_INPUT); gpio_matrix_in(gpio_num, PWM1_SYNC0_IN_IDX + io_signal - OFFSET_FOR_GPIO_IDX_1, 0); } else { @@ -625,6 +625,9 @@ esp_err_t mcpwm_fault_set_oneshot_mode(mcpwm_unit_t mcpwm_num, mcpwm_timer_t tim MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG); MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG); portENTER_CRITICAL(&mcpwm_spinlock); + //clear the ost triggered status + MCPWM[mcpwm_num]->channel[timer_num].tz_cfg1.clr_ost = 1; + MCPWM[mcpwm_num]->channel[timer_num].tz_cfg1.clr_ost = 0; if (fault_sig == MCPWM_SELECT_F0) { MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_ost = 1; MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_cbc = 0; @@ -656,6 +659,9 @@ esp_err_t mcpwm_capture_enable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t ca { MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG); portENTER_CRITICAL(&mcpwm_spinlock); + //We have to do this here, since there is no standalone init function + //without enabling any PWM channels. + MCPWM[mcpwm_num]->clk_cfg.prescale = MCPWM_CLK_PRESCL; MCPWM[mcpwm_num]->cap_timer_cfg.timer_en = 1; MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].en = 1; MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].mode = (1 << cap_edge); diff --git a/components/driver/pcnt.c b/components/driver/pcnt.c index 6fcc2b552..74846953b 100644 --- a/components/driver/pcnt.c +++ b/components/driver/pcnt.c @@ -59,6 +59,11 @@ esp_err_t pcnt_unit_config(const pcnt_config_t *pcnt_config) PCNT_CHECK((pcnt_config->pos_mode < PCNT_COUNT_MAX) && (pcnt_config->neg_mode < PCNT_COUNT_MAX), PCNT_COUNT_MODE_ERR_STR, ESP_ERR_INVALID_ARG); PCNT_CHECK((pcnt_config->hctrl_mode < PCNT_MODE_MAX) && (pcnt_config->lctrl_mode < PCNT_MODE_MAX), PCNT_CTRL_MODE_ERR_STR, ESP_ERR_INVALID_ARG); /*Enalbe hardware module*/ + static bool pcnt_enable = false; + if (pcnt_enable == false) { + periph_module_reset(PERIPH_PCNT_MODULE); + pcnt_enable = true; + } periph_module_enable(PERIPH_PCNT_MODULE); /*Set counter range*/ pcnt_set_event_value(unit, PCNT_EVT_H_LIM, pcnt_config->counter_h_lim); diff --git a/components/driver/rmt.c b/components/driver/rmt.c index 2d1a2bc24..2596358a4 100644 --- a/components/driver/rmt.c +++ b/components/driver/rmt.c @@ -425,6 +425,11 @@ esp_err_t rmt_config(const rmt_config_t* rmt_param) RMT_CHECK((!carrier_en || carrier_freq_hz > 0), "RMT carrier frequency can't be zero", ESP_ERR_INVALID_ARG); } + static bool rmt_enable = false; + if (rmt_enable == false) { + periph_module_reset(PERIPH_RMT_MODULE); + rmt_enable = true; + } periph_module_enable(PERIPH_RMT_MODULE); RMT.conf_ch[channel].conf0.div_cnt = clk_div; diff --git a/components/driver/rtc_module.c b/components/driver/rtc_module.c index 6627a66a8..be428fc14 100644 --- a/components/driver/rtc_module.c +++ b/components/driver/rtc_module.c @@ -785,9 +785,9 @@ uint32_t IRAM_ATTR touch_pad_get_status() esp_err_t IRAM_ATTR touch_pad_clear_status() { - portENTER_CRITICAL(&rtc_spinlock); + portENTER_CRITICAL_SAFE(&rtc_spinlock); SENS.sar_touch_ctrl2.touch_meas_en_clr = 1; - portEXIT_CRITICAL(&rtc_spinlock); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); return ESP_OK; } @@ -995,7 +995,7 @@ esp_err_t touch_pad_filter_start(uint32_t filter_period_ms) } if (s_touch_pad_filter->timer == NULL) { s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE, - NULL, touch_pad_filter_cb); + NULL, (void(*)(TimerHandle_t))touch_pad_filter_cb); if (s_touch_pad_filter->timer == NULL) { ret = ESP_ERR_NO_MEM; } @@ -1217,10 +1217,12 @@ esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en) if (adc_unit & ADC_UNIT_1) { // Enable ADC data invert SENS.sar_read_ctrl.sar1_data_inv = inv_en; + SYSCON.saradc_ctrl2.sar1_inv = inv_en; } if (adc_unit & ADC_UNIT_2) { // Enable ADC data invert SENS.sar_read_ctrl2.sar2_data_inv = inv_en; + SYSCON.saradc_ctrl2.sar2_inv = inv_en; } portEXIT_CRITICAL(&rtc_spinlock); return ESP_OK; diff --git a/components/driver/sdio_slave.c b/components/driver/sdio_slave.c index 9036131c2..d04fb1efc 100644 --- a/components/driver/sdio_slave.c +++ b/components/driver/sdio_slave.c @@ -360,19 +360,39 @@ static inline bool sdio_ringbuf_empty(sdio_ringbuf_t* buf) } /**************** End of Ring buffer for SDIO *****************/ -static inline void show_ll(buf_desc_t *item) +static inline void show_queue_item(buf_desc_t *item) { - ESP_EARLY_LOGD(TAG, "=> %p: size: %d(%d), eof: %d, owner: %d", item, item->size, item->length, item->eof, item->owner); - ESP_EARLY_LOGD(TAG, " buf: %p, stqe_next: %p, tqe-prev: %p", item->buf, item->qe.stqe_next, item->te.tqe_prev); + ESP_EARLY_LOGI(TAG, "=> %p: size: %d(%d), eof: %d, owner: %d", item, item->size, item->length, item->eof, item->owner); + ESP_EARLY_LOGI(TAG, " buf: %p, stqe_next: %p, tqe-prev: %p", item->buf, item->qe.stqe_next, item->te.tqe_prev); } -static void __attribute((unused)) dump_ll(buf_stailq_t *queue) +static void __attribute((unused)) dump_queue(buf_stailq_t *queue) { + int cnt = 0; buf_desc_t *item = NULL; - ESP_EARLY_LOGD(TAG, ">>>>> first: %p, last: %p <<<<<", queue->stqh_first, queue->stqh_last); + ESP_EARLY_LOGI(TAG, ">>>>> first: %p, last: %p <<<<<", queue->stqh_first, queue->stqh_last); STAILQ_FOREACH(item, queue, qe) { - show_ll(item); + cnt++; + show_queue_item(item); } + ESP_EARLY_LOGI(TAG, "total: %d", cnt); +} + +static inline void show_ll(lldesc_t *item) +{ + ESP_EARLY_LOGI(TAG, "=> %p: size: %d(%d), eof: %d, owner: %d", item, item->size, item->length, item->eof, item->owner); + ESP_EARLY_LOGI(TAG, " buf: %p, stqe_next: %p", item->buf, item->qe.stqe_next); +} +static void __attribute((unused)) dump_ll(lldesc_t *queue) +{ + int cnt = 0; + lldesc_t *item = queue; + while (item != NULL) { + cnt++; + show_ll(item); + item = STAILQ_NEXT(item, qe); + } + ESP_EARLY_LOGI(TAG, "total: %d", cnt); } static inline void deinit_context() @@ -964,7 +984,7 @@ static esp_err_t send_flush_data() buf_desc_t *last = NULL; if (context.in_flight) { buf_desc_t *desc = context.in_flight; - while(desc != NULL) { + while (desc != NULL) { xQueueSend(context.ret_queue, &desc->arg, portMAX_DELAY); last = desc; desc = STAILQ_NEXT(desc, qe); @@ -975,13 +995,14 @@ static esp_err_t send_flush_data() context.in_flight_end = NULL; } - buf_desc_t *head; - esp_err_t ret = sdio_ringbuf_recv(&context.sendbuf, (uint8_t**)&head, NULL, RINGBUF_GET_ALL, 0); + buf_desc_t *head, *tail; + esp_err_t ret = sdio_ringbuf_recv(&context.sendbuf, (uint8_t**)&head, (uint8_t**)&tail, RINGBUF_GET_ALL, 0); if (ret == ESP_OK) { buf_desc_t *desc = head; - while(desc != NULL) { + while (1) { xQueueSend(context.ret_queue, &desc->arg, portMAX_DELAY); last = desc; + if (desc == tail) break; desc = STAILQ_NEXT(desc, qe); } sdio_ringbuf_return(&context.sendbuf, (uint8_t*)head); @@ -1162,6 +1183,7 @@ esp_err_t sdio_slave_recv_load_buf(sdio_slave_buf_handle_t handle) buf_stailq_t *const queue = &context.recv_link_list; critical_enter_recv(); + assert(desc->not_receiving); TAILQ_REMOVE(&context.recv_reg_list, desc, te); desc->owner = 1; desc->not_receiving = 0; //manually remove the prev link (by set not_receiving=0), to indicate this is in the queue @@ -1253,3 +1275,9 @@ uint8_t* sdio_slave_recv_get_buf(sdio_slave_buf_handle_t handle, size_t *len_o) if (len_o!= NULL) *len_o= desc->length; return desc->buf; } + +static void __attribute((unused)) sdio_slave_recv_get_loaded_buffer_num() +{ + buf_stailq_t *const queue = &context.recv_link_list; + dump_queue(queue); +} diff --git a/components/driver/sdmmc_host.c b/components/driver/sdmmc_host.c index 2d276b409..6dd8ebbda 100644 --- a/components/driver/sdmmc_host.c +++ b/components/driver/sdmmc_host.c @@ -222,6 +222,7 @@ esp_err_t sdmmc_host_init() return ESP_ERR_INVALID_STATE; } + periph_module_reset(PERIPH_SDMMC_MODULE); periph_module_enable(PERIPH_SDMMC_MODULE); // Enable clock to peripheral. Use smallest divider first. @@ -304,10 +305,6 @@ static void configure_pin(int pin) esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config) { - bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP; - if (pullup) { - sdmmc_host_pullup_en(slot, slot_config->width); - } if (!s_intr_handle) { return ESP_ERR_INVALID_STATE; } @@ -317,6 +314,10 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config) if (slot_config == NULL) { return ESP_ERR_INVALID_ARG; } + bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP; + if (pullup) { + sdmmc_host_pullup_en(slot, slot_config->width); + } int gpio_cd = slot_config->gpio_cd; int gpio_wp = slot_config->gpio_wp; uint8_t slot_width = slot_config->width; diff --git a/components/driver/spi_master.c b/components/driver/spi_master.c index 5976b008f..61376c9b3 100644 --- a/components/driver/spi_master.c +++ b/components/driver/spi_master.c @@ -1174,7 +1174,7 @@ static SPI_MASTER_ISR_ATTR esp_err_t setup_priv_desc(spi_transaction_t *trans_de } if (send_ptr && isdma && !esp_ptr_dma_capable( send_ptr )) { //if txbuf in the desc not DMA-capable, malloc a new one - ESP_LOGI( SPI_TAG, "Allocate TX buffer for DMA" ); + ESP_LOGD( SPI_TAG, "Allocate TX buffer for DMA" ); uint32_t *temp = heap_caps_malloc((trans_desc->length + 7) / 8, MALLOC_CAP_DMA); if (temp == NULL) goto clean_up; diff --git a/components/driver/test/test_gpio.c b/components/driver/test/test_gpio.c index e2550c7ee..4b164da44 100644 --- a/components/driver/test/test_gpio.c +++ b/components/driver/test/test_gpio.c @@ -616,3 +616,128 @@ TEST_CASE("GPIO drive capability test", "[gpio][ignore]") drive_capability_set_get(GPIO_OUTPUT_IO, GPIO_DRIVE_CAP_3); prompt_to_continue("If this test finishes"); } + +#if !CONFIG_FREERTOS_UNICORE +void gpio_enable_task(void *param) +{ + int gpio_num = (int)param; + TEST_ESP_OK(gpio_intr_enable(gpio_num)); + vTaskDelete(NULL); +} + +/** Test the GPIO Interrupt Enable API with dual core enabled. The GPIO ISR service routine is registered on one core. + * When the GPIO interrupt on another core is enabled, the GPIO interrupt will be lost. + * First on the core 0, Do the following steps: + * 1. Configure the GPIO18 input_output mode, and enable the rising edge interrupt mode. + * 2. Trigger the GPIO18 interrupt and check if the interrupt responds correctly. + * 3. Disable the GPIO18 interrupt + * Then on the core 1, Do the following steps: + * 1. Enable the GPIO18 interrupt again. + * 2. Trigger the GPIO18 interrupt and check if the interrupt responds correctly. + * + */ +TEST_CASE("GPIO Enable/Disable interrupt on multiple cores", "[gpio][ignore]") +{ + const int test_io18 = GPIO_NUM_18; + gpio_config_t io_conf; + io_conf.intr_type = GPIO_INTR_NEGEDGE; + io_conf.mode = GPIO_MODE_INPUT_OUTPUT; + io_conf.pin_bit_mask = (1ULL << test_io18); + io_conf.pull_down_en = 0; + io_conf.pull_up_en = 1; + TEST_ESP_OK(gpio_config(&io_conf)); + TEST_ESP_OK(gpio_set_level(test_io18, 0)); + TEST_ESP_OK(gpio_install_isr_service(0)); + TEST_ESP_OK(gpio_isr_handler_add(test_io18, gpio_isr_edge_handler, (void*) test_io18)); + vTaskDelay(1000 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_set_level(test_io18, 1)); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_set_level(test_io18, 0)); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_intr_disable(test_io18)); + TEST_ASSERT(edge_intr_times == 1); + xTaskCreatePinnedToCore(gpio_enable_task, "gpio_enable_task", 1024*4, (void*)test_io18, 8, NULL, (xPortGetCoreID() == 0)); + vTaskDelay(1000 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_set_level(test_io18, 1)); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_set_level(test_io18, 0)); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_intr_disable(test_io18)); + TEST_ESP_OK(gpio_isr_handler_remove(test_io18)); + gpio_uninstall_isr_service(); + TEST_ASSERT(edge_intr_times == 2); +} +#endif + +typedef struct { + int gpio_num; + int isr_cnt; +} gpio_isr_param_t; + +static void gpio_isr_handler(void* arg) +{ + gpio_isr_param_t *param = (gpio_isr_param_t *)arg; + ets_printf("GPIO[%d] intr, val: %d\n", param->gpio_num, gpio_get_level(param->gpio_num)); + param->isr_cnt++; +} + +/** The previous GPIO interrupt service routine polls the interrupt raw status register to find the GPIO that triggered the interrupt. + * But this will incorrectly handle the interrupt disabled GPIOs, because the raw interrupt status register can still be set when + * the trigger signal arrives, even if the interrupt is disabled. + * First on the core 0: + * 1. Configure the GPIO18 and GPIO19 input_output mode. + * 2. Enable GPIO18 dual edge triggered interrupt, enable GPIO19 falling edge triggered interrupt. + * 3. Trigger GPIO18 interrupt, then disable the GPIO8 interrupt, and then trigger GPIO18 again(This time will not respond to the interrupt). + * 4. Trigger GPIO19 interrupt. + * If the bug is not fixed, you will see, in the step 4, the interrupt of GPIO18 will also respond. + */ +TEST_CASE("GPIO ISR service test", "[gpio][ignore]") +{ + const int test_io18 = GPIO_NUM_18; + const int test_io19 = GPIO_NUM_19; + static gpio_isr_param_t io18_param = { + .gpio_num = GPIO_NUM_18, + .isr_cnt = 0, + }; + static gpio_isr_param_t io19_param = { + .gpio_num = GPIO_NUM_19, + .isr_cnt = 0, + }; + gpio_config_t io_conf; + io_conf.intr_type = GPIO_INTR_DISABLE; + io_conf.mode = GPIO_MODE_INPUT_OUTPUT; + io_conf.pin_bit_mask = (1ULL << test_io18) | (1ULL << test_io19); + io_conf.pull_down_en = 0; + io_conf.pull_up_en = 1; + TEST_ESP_OK(gpio_config(&io_conf)); + TEST_ESP_OK(gpio_set_level(test_io18, 0)); + TEST_ESP_OK(gpio_set_level(test_io19, 0)); + TEST_ESP_OK(gpio_install_isr_service(0)); + TEST_ESP_OK(gpio_set_intr_type(test_io18, GPIO_INTR_ANYEDGE)); + TEST_ESP_OK(gpio_set_intr_type(test_io19, GPIO_INTR_NEGEDGE)); + TEST_ESP_OK(gpio_isr_handler_add(test_io18, gpio_isr_handler, (void*)&io18_param)); + TEST_ESP_OK(gpio_isr_handler_add(test_io19, gpio_isr_handler, (void*)&io19_param)); + printf("Triggering the interrupt of GPIO18\n"); + vTaskDelay(1000 / portTICK_RATE_MS); + //Rising edge + TEST_ESP_OK(gpio_set_level(test_io18, 1)); + printf("Disable the interrupt of GPIO18"); + vTaskDelay(100 / portTICK_RATE_MS); + //Disable GPIO18 interrupt, GPIO18 will not respond to the next falling edge interrupt. + TEST_ESP_OK(gpio_intr_disable(test_io18)); + vTaskDelay(100 / portTICK_RATE_MS); + //Falling edge + TEST_ESP_OK(gpio_set_level(test_io18, 0)); + + printf("Triggering the interrupt of GPIO19\n"); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_set_level(test_io19, 1)); + vTaskDelay(100 / portTICK_RATE_MS); + //Falling edge + TEST_ESP_OK(gpio_set_level(test_io19, 0)); + vTaskDelay(100 / portTICK_RATE_MS); + TEST_ESP_OK(gpio_isr_handler_remove(test_io18)); + TEST_ESP_OK(gpio_isr_handler_remove(test_io19)); + gpio_uninstall_isr_service(); + TEST_ASSERT((io18_param.isr_cnt == 1) && (io19_param.isr_cnt == 1)); +} \ No newline at end of file diff --git a/components/driver/test/test_i2c.c b/components/driver/test/test_i2c.c index 48aea2a4b..96c236dd9 100644 --- a/components/driver/test/test_i2c.c +++ b/components/driver/test/test_i2c.c @@ -506,3 +506,67 @@ static void i2c_slave_repeat_read() } TEST_CASE_MULTIPLE_DEVICES("I2C repeat write test", "[i2c][test_env=UT_T2_I2C][timeout=150]", i2c_master_repeat_write, i2c_slave_repeat_read); + +static volatile bool exit_flag; +static bool test_read_func; + +static void test_task(void *pvParameters) +{ + xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + + uint8_t *data = (uint8_t *) malloc(DATA_LENGTH); + i2c_config_t conf_slave = i2c_slave_init(); + TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave)); + TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE, + I2C_SLAVE_RX_BUF_LEN, + I2C_SLAVE_TX_BUF_LEN, 0)); + while (exit_flag == false) { + if (test_read_func) { + i2c_slave_read_buffer(I2C_SLAVE_NUM, data, DATA_LENGTH, 0); + } else { + i2c_slave_write_buffer(I2C_SLAVE_NUM, data, DATA_LENGTH, 0); + } + } + + free(data); + xSemaphoreGive(*sema); + vTaskDelete(NULL); +} + +TEST_CASE("test i2c_slave_read_buffer is not blocked when ticks_to_wait=0", "[i2c]") +{ + xSemaphoreHandle exit_sema = xSemaphoreCreateBinary(); + exit_flag = false; + + test_read_func = true; + xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL); + + printf("Waiting for 5 sec\n"); + vTaskDelay(5000 / portTICK_PERIOD_MS); + exit_flag = true; + if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) { + vSemaphoreDelete(exit_sema); + } else { + TEST_FAIL_MESSAGE("i2c_slave_read_buffer is blocked"); + } + TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM)); +} + +TEST_CASE("test i2c_slave_write_buffer is not blocked when ticks_to_wait=0", "[i2c]") +{ + xSemaphoreHandle exit_sema = xSemaphoreCreateBinary(); + exit_flag = false; + + test_read_func = false; + xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL); + + printf("Waiting for 5 sec\n"); + vTaskDelay(5000 / portTICK_PERIOD_MS); + exit_flag = true; + if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) { + vSemaphoreDelete(exit_sema); + } else { + TEST_FAIL_MESSAGE("i2c_slave_write_buffer is blocked"); + } + TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM)); +} diff --git a/components/driver/test/test_pwm.c b/components/driver/test/test_pwm.c index c202af68c..d3e293a9a 100644 --- a/components/driver/test/test_pwm.c +++ b/components/driver/test/test_pwm.c @@ -314,10 +314,10 @@ static void oneshot_fault_test(mcpwm_unit_t unit, mcpwm_io_signals_t mcpwm_a, mc // one shot mode, it just can be triggered once TEST_ESP_OK(mcpwm_fault_init(unit, input_sig, fault_sig)); TEST_ESP_OK(mcpwm_fault_set_oneshot_mode(unit, timer, fault_sig, action_a, action_b)); - vTaskDelay(1000 / portTICK_RATE_MS); + vTaskDelay(10/ portTICK_RATE_MS); // trigger it gpio_set_level(FAULT_SIG_NUM, input_sig); - vTaskDelay(1000 / portTICK_RATE_MS); + vTaskDelay(10/ portTICK_RATE_MS); get_action_level(input_sig, action_a, action_b, 1000, 5); TEST_ESP_OK(mcpwm_fault_deinit(unit, fault_sig)); } @@ -666,7 +666,7 @@ TEST_CASE("MCPWM timer1 cycle fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeou } } -TEST_CASE("MCPWM timer2 cycle fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=180][ignore]") +TEST_CASE("MCPWM timer2 cycle fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=180]") { // API just supports the high level trigger now, so comment it // mcpwm_fault_input_level_t fault_input[2] = {MCPWM_LOW_LEVEL_TGR, MCPWM_HIGH_LEVEL_TGR}; @@ -685,24 +685,7 @@ TEST_CASE("MCPWM timer2 cycle fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeou } } -// to debug the "mcpwm_fault_deinit" case. The "MCPWM_NO_CHANGE_IN_MCPWMXA, MCPWM_FORCE_MCPWMXB_HIGH" scenario can work right -// however, the mcpwm_fault_deinit can not release the status after "MCPWM_NO_CHANGE_IN_MCPWMXA, MCPWM_FORCE_MCPWMXB_LOW" scenario -TEST_CASE("MCPWM timer0 one shot fault test single", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60]") -{ - // API just supports the high level trigger now, so comment it -// mcpwm_fault_input_level_t fault_input[2] = {MCPWM_LOW_LEVEL_TGR, MCPWM_HIGH_LEVEL_TGR}; - mcpwm_action_on_pwmxa_t action_a[4] = {MCPWM_NO_CHANGE_IN_MCPWMXA, MCPWM_FORCE_MCPWMXA_LOW, MCPWM_FORCE_MCPWMXA_HIGH, MCPWM_TOG_MCPWMXA}; - mcpwm_action_on_pwmxb_t action_b[4] = {MCPWM_NO_CHANGE_IN_MCPWMXB, MCPWM_FORCE_MCPWMXB_LOW, MCPWM_FORCE_MCPWMXB_HIGH, MCPWM_TOG_MCPWMXB}; - - oneshot_fault_test(MCPWM_UNIT_0, MCPWM0A, MCPWM0B, MCPWM_TIMER_0, - MCPWM_SELECT_F0, MCPWM_HIGH_LEVEL_TGR, MCPWM_FAULT_0, - action_a[0], action_b[2]); -} - -// the mcpwm_fault_deinit can not release the status after "MCPWM_NO_CHANGE_IN_MCPWMXA, MCPWM_FORCE_MCPWMXB_LOW" scenario -// set it ignore -// same as the case "MCPWM timer1 one shot fault test" and case "MCPWM timer2 one shot fault test" -TEST_CASE("MCPWM timer0 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60][ignore]") +TEST_CASE("MCPWM timer0 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60]") { // API just supports the high level trigger now, so comment it // mcpwm_fault_input_level_t fault_input[2] = {MCPWM_LOW_LEVEL_TGR, MCPWM_HIGH_LEVEL_TGR}; @@ -722,7 +705,7 @@ TEST_CASE("MCPWM timer0 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][tim } } -TEST_CASE("MCPWM timer1 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60][ignore]") +TEST_CASE("MCPWM timer1 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60]") { // API just supports the high level trigger now, so comment it // mcpwm_fault_input_level_t fault_input[2] = {MCPWM_LOW_LEVEL_TGR, MCPWM_HIGH_LEVEL_TGR}; @@ -741,7 +724,7 @@ TEST_CASE("MCPWM timer1 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][tim } } -TEST_CASE("MCPWM timer2 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60][ignore]") +TEST_CASE("MCPWM timer2 one shot fault test", "[mcpwm][test_env=UT_T1_MCPWM][timeout=60]") { // API just supports the high level trigger now, so comment it // mcpwm_fault_input_level_t fault_input[2] = {MCPWM_LOW_LEVEL_TGR, MCPWM_HIGH_LEVEL_TGR}; diff --git a/components/driver/test/test_uart.c b/components/driver/test/test_uart.c index 370f00ffe..db2146efd 100644 --- a/components/driver/test/test_uart.c +++ b/components/driver/test/test_uart.c @@ -118,6 +118,56 @@ static void uart_config(uint32_t baud_rate, bool use_ref_tick) uart_driver_install(UART_NUM1, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0); } +static volatile bool exit_flag; + +static void test_task(void *pvParameters) +{ + xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + char* data = (char *) malloc(256); + + while (exit_flag == false) { + uart_tx_chars(UART_NUM1, data, 256); + // The uart_wait_tx_done() function does not block anything if ticks_to_wait = 0. + uart_wait_tx_done(UART_NUM1, 0); + } + + free(data); + xSemaphoreGive(*sema); + vTaskDelete(NULL); +} + +static void test_task2(void *pvParameters) +{ + while (exit_flag == false) { + // This task obstruct a setting tx_done_sem semaphore in the UART interrupt. + // It leads to waiting the ticks_to_wait time in uart_wait_tx_done() function. + uart_disable_intr_mask(UART_NUM1, UART_TX_DONE_INT_ENA_M); + } + vTaskDelete(NULL); +} + +TEST_CASE("test uart_wait_tx_done is not blocked when ticks_to_wait=0", "[uart]") +{ + uart_config(UART_BAUD_11520, false); + + xSemaphoreHandle exit_sema = xSemaphoreCreateBinary(); + exit_flag = false; + + xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL); + xTaskCreate(test_task2, "tsk2", 2048, NULL, 5, NULL); + + printf("Waiting for 5 sec\n"); + vTaskDelay(5000 / portTICK_PERIOD_MS); + exit_flag = true; + + if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) { + vSemaphoreDelete(exit_sema); + } else { + TEST_FAIL_MESSAGE("uart_wait_tx_done is blocked"); + } + TEST_ESP_OK(uart_driver_delete(UART_NUM1)); +} + TEST_CASE("test uart get baud-rate","[uart]") { uint32_t baud_rate1 = 0; diff --git a/components/driver/timer.c b/components/driver/timer.c index 5881a58ac..202027829 100644 --- a/components/driver/timer.c +++ b/components/driver/timer.c @@ -36,7 +36,8 @@ static const char* TIMER_TAG = "timer_group"; #define TIMER_SCALE_ERROR "HW TIMER SCALE ERROR" #define TIMER_ALARM_ERROR "HW TIMER ALARM ERROR" #define DIVIDER_RANGE_ERROR "HW TIMER divider outside of [2, 65536] range error" -static timg_dev_t *TG[2] = {&TIMERG0, &TIMERG1}; +/* DRAM_ATTR is required to avoid TG array placed in flash, due to accessed from ISR */ +static DRAM_ATTR timg_dev_t *TG[2] = {&TIMERG0, &TIMERG1}; static portMUX_TYPE timer_spinlock[TIMER_GROUP_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED}; #define TIMER_ENTER_CRITICAL(mux) portENTER_CRITICAL_SAFE(mux); @@ -258,7 +259,12 @@ esp_err_t timer_group_intr_enable(timer_group_t group_num, uint32_t en_mask) { TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); portENTER_CRITICAL(&timer_spinlock[group_num]); - TG[group_num]->int_ena.val |= en_mask; + for (int i = 0; i < 2; i++) { + if (en_mask & (1 << i)) { + TG[group_num]->hw_timer[i].config.level_int_en = 1; + TG[group_num]->int_ena.val |= (1 << i); + } + } portEXIT_CRITICAL(&timer_spinlock[group_num]); return ESP_OK; } @@ -267,7 +273,12 @@ esp_err_t timer_group_intr_disable(timer_group_t group_num, uint32_t disable_mas { TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); portENTER_CRITICAL(&timer_spinlock[group_num]); - TG[group_num]->int_ena.val &= (~disable_mask); + for (int i = 0; i < 2; i++) { + if (disable_mask & (1 << i)) { + TG[group_num]->hw_timer[i].config.level_int_en = 0; + TG[group_num]->int_ena.val &= ~(1 << i); + } + } portEXIT_CRITICAL(&timer_spinlock[group_num]); return ESP_OK; } @@ -276,14 +287,22 @@ esp_err_t timer_enable_intr(timer_group_t group_num, timer_idx_t timer_num) { TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); - return timer_group_intr_enable(group_num, BIT(timer_num)); + portENTER_CRITICAL(&timer_spinlock[group_num]); + TG[group_num]->hw_timer[timer_num].config.level_int_en = 1; + TG[group_num]->int_ena.val |= (1 << timer_num); + portEXIT_CRITICAL(&timer_spinlock[group_num]); + return ESP_OK; } esp_err_t timer_disable_intr(timer_group_t group_num, timer_idx_t timer_num) { TIMER_CHECK(group_num < TIMER_GROUP_MAX, TIMER_GROUP_NUM_ERROR, ESP_ERR_INVALID_ARG); TIMER_CHECK(timer_num < TIMER_MAX, TIMER_NUM_ERROR, ESP_ERR_INVALID_ARG); - return timer_group_intr_disable(group_num, BIT(timer_num)); + portENTER_CRITICAL(&timer_spinlock[group_num]); + TG[group_num]->hw_timer[timer_num].config.level_int_en = 0; + TG[group_num]->int_ena.val &= ~(1 << timer_num); + portEXIT_CRITICAL(&timer_spinlock[group_num]); + return ESP_OK; } diff --git a/components/driver/uart.c b/components/driver/uart.c index 188be1418..27e13bca0 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -649,18 +649,31 @@ esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num) return ESP_OK; } +static periph_module_t get_periph_module(uart_port_t uart_num) +{ + periph_module_t periph_module = PERIPH_UART0_MODULE; + if (uart_num == UART_NUM_0) { + periph_module = PERIPH_UART0_MODULE; + } else if (uart_num == UART_NUM_1) { + periph_module = PERIPH_UART1_MODULE; + } else if (uart_num == UART_NUM_2) { + periph_module = PERIPH_UART2_MODULE; + } else { + assert(0 && "uart_num error"); + } + return periph_module; +} + esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config) { esp_err_t r; UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_config), "param null", ESP_FAIL); - if(uart_num == UART_NUM_0) { - periph_module_enable(PERIPH_UART0_MODULE); - } else if(uart_num == UART_NUM_1) { - periph_module_enable(PERIPH_UART1_MODULE); - } else if(uart_num == UART_NUM_2) { - periph_module_enable(PERIPH_UART2_MODULE); + periph_module_t periph_module = get_periph_module(uart_num); + if (uart_num != CONFIG_CONSOLE_UART_NUM) { + periph_module_reset(periph_module); } + periph_module_enable(periph_module); r = uart_set_hw_flow_ctrl(uart_num, uart_config->flow_ctrl, uart_config->rx_flow_ctrl_thresh); if (r != ESP_OK) return r; @@ -852,6 +865,17 @@ static void uart_rx_intr_handler_default(void *param) || (uart_intr_status & UART_AT_CMD_CHAR_DET_INT_ST_M) ) { rx_fifo_len = uart_reg->status.rxfifo_cnt; + typeof(uart_reg->mem_rx_status) rx_status = uart_reg->mem_rx_status; + + // When using DPort to read fifo, fifo_cnt is not credible, we need to calculate the real cnt based on the fifo read and write pointer. + // When using AHB to read FIFO, we can use fifo_cnt to indicate the data length in fifo. + if (rx_status.wr_addr > rx_status.rd_addr) { + rx_fifo_len = rx_status.wr_addr - rx_status.rd_addr; + } else if (rx_status.wr_addr < rx_status.rd_addr) { + rx_fifo_len = (rx_status.wr_addr + 128) - rx_status.rd_addr; + } else { + rx_fifo_len = rx_fifo_len > 0 ? 128 : 0; + } if(pat_flg == 1) { uart_intr_status |= UART_AT_CMD_CHAR_DET_INT_ST_M; pat_flg = 0; @@ -1445,14 +1469,9 @@ esp_err_t uart_driver_delete(uart_port_t uart_num) free(p_uart_obj[uart_num]); p_uart_obj[uart_num] = NULL; - if (uart_num != CONFIG_CONSOLE_UART_NUM ) { - if(uart_num == UART_NUM_0) { - periph_module_disable(PERIPH_UART0_MODULE); - } else if(uart_num == UART_NUM_1) { - periph_module_disable(PERIPH_UART1_MODULE); - } else if(uart_num == UART_NUM_2) { - periph_module_disable(PERIPH_UART2_MODULE); - } + if (uart_num != CONFIG_CONSOLE_UART_NUM) { + periph_module_t periph_module = get_periph_module(uart_num); + periph_module_disable(periph_module); } return ESP_OK; } diff --git a/components/efuse/esp32/esp_efuse_table.c b/components/efuse/esp32/esp_efuse_table.c index 36b54b50b..e0d4e2d10 100644 --- a/components/efuse/esp32/esp_efuse_table.c +++ b/components/efuse/esp32/esp_efuse_table.c @@ -17,7 +17,7 @@ #include #include "esp_efuse_table.h" -// md5_digest_table 544d434da010ce22f7db1b14d38e1d66 +// md5_digest_table 2e23344575b3d07f01ecb695294e9770 // This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY. // If you want to change some fields, you need to change esp_efuse_table.csv file // then run `efuse_common_table` or `efuse_custom_table` command it will generate this file. @@ -151,6 +151,10 @@ static const esp_efuse_desc_t CHIP_VER_REV1[] = { {EFUSE_BLK0, 111, 1}, // EFUSE_RD_CHIP_VER_REV1, }; +static const esp_efuse_desc_t CHIP_VER_REV2[] = { + {EFUSE_BLK0, 180, 1}, // EFUSE_RD_CHIP_VER_REV2, +}; + static const esp_efuse_desc_t XPD_SDIO_REG[] = { {EFUSE_BLK0, 142, 1}, // EFUSE_RD_XPD_SDIO_REG, }; @@ -336,6 +340,11 @@ const esp_efuse_desc_t* ESP_EFUSE_CHIP_VER_REV1[] = { NULL }; +const esp_efuse_desc_t* ESP_EFUSE_CHIP_VER_REV2[] = { + &CHIP_VER_REV2[0], // EFUSE_RD_CHIP_VER_REV2 + NULL +}; + const esp_efuse_desc_t* ESP_EFUSE_XPD_SDIO_REG[] = { &XPD_SDIO_REG[0], // EFUSE_RD_XPD_SDIO_REG NULL diff --git a/components/efuse/esp32/esp_efuse_table.csv b/components/efuse/esp32/esp_efuse_table.csv index 23b28ec47..1445ac8ae 100644 --- a/components/efuse/esp32/esp_efuse_table.csv +++ b/components/efuse/esp32/esp_efuse_table.csv @@ -6,7 +6,7 @@ ########################################################################## # *) The value MAX_BLK_LEN depends on CONFIG_EFUSE_MAX_BLK_LEN, will be replaced with "None" - 256. "3/4" - 192. "REPEAT" - 128. # !!!!!!!!!!! # -# After editing this file, run the command manually "make efuse_common_table" or "idf.py efuse_common_table" +# After editing this file, run the command manually "make efuse_common_table" or "idf.py efuse_common_table" # this will generate new source files, next rebuild all the sources. # !!!!!!!!!!! # @@ -36,11 +36,11 @@ ABS_DONE_0, EFUSE_BLK0, 196, 1, Secure boot is enabled for ENCRYPT_FLASH_KEY, EFUSE_BLK1, 0, MAX_BLK_LEN, Flash encrypt. Key. (length = "None" - 256. "3/4" - 192. "REPEAT" - 128) ENCRYPT_CONFIG, EFUSE_BLK0, 188, 4, Flash encrypt. EFUSE_FLASH_CRYPT_CONFIG_M -DISABLE_DL_ENCRYPT, EFUSE_BLK0, 199, 1, Flash encrypt. Disable UART bootloader encryption. EFUSE_DISABLE_DL_ENCRYPT. -DISABLE_DL_DECRYPT, EFUSE_BLK0, 200, 1, Flash encrypt. Disable UART bootloader decryption. EFUSE_DISABLE_DL_DECRYPT. -DISABLE_DL_CACHE, EFUSE_BLK0, 201, 1, Flash encrypt. Disable UART bootloader MMU cache. EFUSE_DISABLE_DL_CACHE. -DISABLE_JTAG, EFUSE_BLK0, 198, 1, Flash encrypt. Disable JTAG. EFUSE_RD_DISABLE_JTAG. -CONSOLE_DEBUG_DISABLE, EFUSE_BLK0, 194, 1, Flash encrypt. Disable ROM BASIC interpreter fallback. EFUSE_RD_CONSOLE_DEBUG_DISABLE. +DISABLE_DL_ENCRYPT, EFUSE_BLK0, 199, 1, Flash encrypt. Disable UART bootloader encryption. EFUSE_DISABLE_DL_ENCRYPT. +DISABLE_DL_DECRYPT, EFUSE_BLK0, 200, 1, Flash encrypt. Disable UART bootloader decryption. EFUSE_DISABLE_DL_DECRYPT. +DISABLE_DL_CACHE, EFUSE_BLK0, 201, 1, Flash encrypt. Disable UART bootloader MMU cache. EFUSE_DISABLE_DL_CACHE. +DISABLE_JTAG, EFUSE_BLK0, 198, 1, Flash encrypt. Disable JTAG. EFUSE_RD_DISABLE_JTAG. +CONSOLE_DEBUG_DISABLE, EFUSE_BLK0, 194, 1, Flash encrypt. Disable ROM BASIC interpreter fallback. EFUSE_RD_CONSOLE_DEBUG_DISABLE. FLASH_CRYPT_CNT, EFUSE_BLK0, 20, 7, Flash encrypt. Flash encryption is enabled if this field has an odd number of bits set. EFUSE_FLASH_CRYPT_CNT. # Write protection # @@ -53,7 +53,7 @@ WR_DIS_BLK3, EFUSE_BLK0, 9, 1, Write protection for EFUSE_B # Read protection # ################### RD_DIS_BLK1, EFUSE_BLK0, 16, 1, Flash encrypt. efuse_key_read_protected. EFUSE_RD_DIS_BLK1 -RD_DIS_BLK2, EFUSE_BLK0, 17, 1, Security boot. efuse_key_read_protected. EFUSE_RD_DIS_BLK2 +RD_DIS_BLK2, EFUSE_BLK0, 17, 1, Security boot. efuse_key_read_protected. EFUSE_RD_DIS_BLK2 RD_DIS_BLK3, EFUSE_BLK0, 18, 1, Read protection for EFUSE_BLK3. EFUSE_RD_DIS_BLK3 # Chip info # @@ -64,6 +64,7 @@ CHIP_VER_PKG, EFUSE_BLK0, 105, 3, EFUSE_RD_CHIP_VER_PKG CHIP_CPU_FREQ_LOW, EFUSE_BLK0, 108, 1, EFUSE_RD_CHIP_CPU_FREQ_LOW CHIP_CPU_FREQ_RATED, EFUSE_BLK0, 109, 1, EFUSE_RD_CHIP_CPU_FREQ_RATED CHIP_VER_REV1, EFUSE_BLK0, 111, 1, EFUSE_RD_CHIP_VER_REV1 +CHIP_VER_REV2, EFUSE_BLK0, 180, 1, EFUSE_RD_CHIP_VER_REV2 XPD_SDIO_REG, EFUSE_BLK0, 142, 1, EFUSE_RD_XPD_SDIO_REG SDIO_TIEH, EFUSE_BLK0, 143, 1, EFUSE_RD_SDIO_TIEH SDIO_FORCE, EFUSE_BLK0, 144, 1, EFUSE_RD_SDIO_FORCE diff --git a/components/efuse/esp32/include/esp_efuse_table.h b/components/efuse/esp32/include/esp_efuse_table.h index 807fb0888..a0137c012 100644 --- a/components/efuse/esp32/include/esp_efuse_table.h +++ b/components/efuse/esp32/include/esp_efuse_table.h @@ -17,7 +17,7 @@ extern "C" { #endif -// md5_digest_table 544d434da010ce22f7db1b14d38e1d66 +// md5_digest_table 2e23344575b3d07f01ecb695294e9770 // This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY. // If you want to change some fields, you need to change esp_efuse_table.csv file // then run `efuse_common_table` or `efuse_custom_table` command it will generate this file. @@ -52,6 +52,7 @@ extern const esp_efuse_desc_t* ESP_EFUSE_CHIP_VER_PKG[]; extern const esp_efuse_desc_t* ESP_EFUSE_CHIP_CPU_FREQ_LOW[]; extern const esp_efuse_desc_t* ESP_EFUSE_CHIP_CPU_FREQ_RATED[]; extern const esp_efuse_desc_t* ESP_EFUSE_CHIP_VER_REV1[]; +extern const esp_efuse_desc_t* ESP_EFUSE_CHIP_VER_REV2[]; extern const esp_efuse_desc_t* ESP_EFUSE_XPD_SDIO_REG[]; extern const esp_efuse_desc_t* ESP_EFUSE_SDIO_TIEH[]; extern const esp_efuse_desc_t* ESP_EFUSE_SDIO_FORCE[]; diff --git a/components/efuse/src/esp_efuse_api.c b/components/efuse/src/esp_efuse_api.c index d697a0d4c..20ad5bb5c 100644 --- a/components/efuse/src/esp_efuse_api.c +++ b/components/efuse/src/esp_efuse_api.c @@ -197,7 +197,7 @@ esp_efuse_coding_scheme_t esp_efuse_get_coding_scheme(esp_efuse_block_t blk) scheme = EFUSE_CODING_SCHEME_REPEAT; } } - ESP_LOGD(TAG, "coding scheme %d", scheme); + ESP_EARLY_LOGD(TAG, "coding scheme %d", scheme); return scheme; } diff --git a/components/efuse/src/esp_efuse_fields.c b/components/efuse/src/esp_efuse_fields.c index f4f30714c..a344d83c6 100644 --- a/components/efuse/src/esp_efuse_fields.c +++ b/components/efuse/src/esp_efuse_fields.c @@ -23,6 +23,7 @@ #include "esp_log.h" #include "soc/efuse_reg.h" #include "bootloader_random.h" +#include "soc/apb_ctrl_reg.h" const static char *TAG = "efuse"; @@ -31,8 +32,29 @@ const static char *TAG = "efuse"; // Returns chip version from efuse uint8_t esp_efuse_get_chip_ver(void) { - uint8_t chip_ver; - esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_REV1, &chip_ver, 1); + uint8_t eco_bit0, eco_bit1, eco_bit2; + esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_REV1, &eco_bit0, 1); + esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_REV2, &eco_bit1, 1); + eco_bit2 = (REG_READ(APB_CTRL_DATE_REG) & 0x80000000) >> 31; + uint32_t combine_value = (eco_bit2 << 2) | (eco_bit1 << 1) | eco_bit0; + uint8_t chip_ver = 0; + switch (combine_value) { + case 0: + chip_ver = 0; + break; + case 1: + chip_ver = 1; + break; + case 3: + chip_ver = 2; + break; + case 7: + chip_ver = 3; + break; + default: + chip_ver = 0; + break; + } return chip_ver; } @@ -111,7 +133,7 @@ void esp_efuse_write_random_key(uint32_t blk_wdata0_reg) ESP_LOGV(TAG, "Writing random values to address 0x%08x", blk_wdata0_reg); for (int i = 0; i < 8; i++) { ESP_LOGV(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, buf[i]); - REG_WRITE(blk_wdata0_reg + 4*i, buf[i]); + REG_WRITE(blk_wdata0_reg + 4 * i, buf[i]); } bzero(buf, sizeof(buf)); bzero(raw, sizeof(raw)); diff --git a/components/efuse/src/esp_efuse_utility.c b/components/efuse/src/esp_efuse_utility.c index a9401f163..ff83921cc 100644 --- a/components/efuse/src/esp_efuse_utility.c +++ b/components/efuse/src/esp_efuse_utility.c @@ -15,6 +15,7 @@ #include "esp_efuse_utility.h" #include "soc/efuse_reg.h" +#include "esp_clk.h" #include "esp_log.h" #include "assert.h" #include "sdkconfig.h" @@ -209,6 +210,25 @@ void esp_efuse_utility_burn_efuses(void) } } #else + // Update Efuse timing configuration + uint32_t apb_freq_mhz = esp_clk_apb_freq() / 1000000; + uint32_t clk_sel0, clk_sel1, dac_clk_div; + if (apb_freq_mhz <= 26) { + clk_sel0 = 250; + clk_sel1 = 255; + dac_clk_div = 52; + } else if (apb_freq_mhz <= 40) { + clk_sel0 = 160; + clk_sel1 = 255; + dac_clk_div = 80; + } else { + clk_sel0 = 80; + clk_sel1 = 128; + dac_clk_div = 100; + } + REG_SET_FIELD(EFUSE_DAC_CONF_REG, EFUSE_DAC_CLK_DIV, dac_clk_div); + REG_SET_FIELD(EFUSE_CLK_REG, EFUSE_CLK_SEL0, clk_sel0); + REG_SET_FIELD(EFUSE_CLK_REG, EFUSE_CLK_SEL1, clk_sel1); // Permanently update values written to the efuse write registers REG_WRITE(EFUSE_CONF_REG, EFUSE_CONF_WRITE); REG_WRITE(EFUSE_CMD_REG, EFUSE_CMD_PGM); diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 861861d5d..07b53b777 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -117,6 +117,7 @@ static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, struct timeval tv; ms_to_timeval(cfg->timeout_ms, &tv); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); } if (cfg->non_block) { int flags = fcntl(fd, F_GETFL, 0); @@ -242,18 +243,26 @@ static int create_ssl_handle(esp_tls_t *tls, const char *hostname, size_t hostle goto exit; } - /* Hostname set here should match CN in server certificate */ - char *use_host = strndup(hostname, hostlen); - if (!use_host) { - goto exit; - } + if (!cfg->skip_common_name) { + char *use_host = NULL; + if (cfg->common_name != NULL) { + use_host = strndup(cfg->common_name, strlen(cfg->common_name)); + } else { + use_host = strndup(hostname, hostlen); + } - if ((ret = mbedtls_ssl_set_hostname(&tls->ssl, use_host)) != 0) { - ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret); + if (use_host == NULL) { + goto exit; + } + + /* Hostname set here should match CN in server certificate */ + if ((ret = mbedtls_ssl_set_hostname(&tls->ssl, use_host)) != 0) { + ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret); + free(use_host); + goto exit; + } free(use_host); - goto exit; } - free(use_host); if ((ret = mbedtls_ssl_config_defaults(&tls->conf, MBEDTLS_SSL_IS_CLIENT, @@ -473,6 +482,7 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e } /* esp_tls_conn_new() API establishes connection in a blocking manner thus this loop ensures that esp_tls_conn_new() API returns only after connection is established unless there is an error*/ + size_t start = xTaskGetTickCount(); while (1) { int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); if (ret == 1) { @@ -481,6 +491,14 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e esp_tls_conn_delete(tls); ESP_LOGE(TAG, "Failed to open new connection"); return NULL; + } else if (ret == 0 && cfg->timeout_ms >= 0) { + size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms); + uint32_t expired = xTaskGetTickCount() - start; + if (expired >= timeout_ticks) { + esp_tls_conn_delete(tls); + ESP_LOGE(TAG, "Failed to open new connection in specified timeout"); + return NULL; + } } } return NULL; diff --git a/components/esp-tls/esp_tls.h b/components/esp-tls/esp_tls.h index 38538ed0a..a1cceabe8 100644 --- a/components/esp-tls/esp_tls.h +++ b/components/esp-tls/esp_tls.h @@ -17,6 +17,7 @@ #include #include #include +#include "esp_err.h" #include "mbedtls/platform.h" #include "mbedtls/net_sockets.h" @@ -56,17 +57,20 @@ typedef struct esp_tls_cfg { - where the first '2' is the length of the protocol and - the subsequent 'h2' is the protocol name */ - const unsigned char *cacert_pem_buf; /*!< Certificate Authority's certificate in a buffer */ + const unsigned char *cacert_pem_buf; /*!< Certificate Authority's certificate in a buffer. + This buffer should be NULL terminated */ unsigned int cacert_pem_bytes; /*!< Size of Certificate Authority certificate pointed to by cacert_pem_buf */ - const unsigned char *clientcert_pem_buf;/*!< Client certificate in a buffer */ + const unsigned char *clientcert_pem_buf;/*!< Client certificate in a buffer + This buffer should be NULL terminated */ unsigned int clientcert_pem_bytes; /*!< Size of client certificate pointed to by clientcert_pem_buf */ - const unsigned char *clientkey_pem_buf; /*!< Client key in a buffer */ + const unsigned char *clientkey_pem_buf; /*!< Client key in a buffer + This buffer should be NULL terminated */ unsigned int clientkey_pem_bytes; /*!< Size of client key pointed to by clientkey_pem_buf */ @@ -84,6 +88,11 @@ typedef struct esp_tls_cfg { bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */ + + const char *common_name; /*!< If non-NULL, server certificate CN must match this name. + If NULL, server certificate CN must match hostname. */ + + bool skip_common_name; /*!< Skip any validation of server certificate CN field */ } esp_tls_cfg_t; /** diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index cf36f9b8e..7a6f92d3d 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -33,7 +33,6 @@ else() "hw_random.c" "int_wdt.c" "intr_alloc.c" - "ipc.c" "lib_printf.c" "panic.c" "phy_init.c" @@ -53,6 +52,10 @@ else() "hwcrypto/sha.c") set(COMPONENT_ADD_INCLUDEDIRS "include") + if(NOT CONFIG_FREERTOS_UNICORE) + list(APPEND COMPONENT_SRCS "ipc.c") + endif() + set(COMPONENT_REQUIRES driver tcpip_adapter esp_event efuse) # driver is a public requirement because esp_sleep.h uses gpio_num_t & touch_pad_t # tcpip_adapter is a public requirement because esp_event.h uses tcpip_adapter types @@ -151,7 +154,7 @@ else() DEPENDS ${CMAKE_CURRENT_LIST_DIR}/phy_init_data.h COMMAND ${CMAKE_C_COMPILER} -x c -c -I ${CMAKE_CURRENT_LIST_DIR} -I ${CMAKE_CURRENT_LIST_DIR}/include -I ${IDF_BUILD_ARTIFACTS_DIR} - -o phy_init_data.obj + -I ${CONFIG_DIR} -o phy_init_data.obj ${CMAKE_CURRENT_LIST_DIR}/phy_init_data.h COMMAND ${CMAKE_OBJCOPY} -O binary phy_init_data.obj ${PHY_INIT_DATA_BIN} ) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 8b1cf70b6..0dcb91890 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -6,6 +6,34 @@ menu "ESP32-specific" default "y" if IDF_TARGET="esp32" default "n" + choice ESP32_REV_MIN + prompt "Minimum Supported ESP32 Revision" + default ESP32_REV_MIN_0 + help + Minimum revision that ESP-IDF would support. + ESP-IDF performs different strategy on different esp32 revision. + + config ESP32_REV_MIN_0 + bool "Rev 0" + config ESP32_REV_MIN_1 + bool "Rev 1" + config ESP32_REV_MIN_2 + bool "Rev 2" + config ESP32_REV_MIN_3 + bool "Rev 3" + endchoice + + config ESP32_REV_MIN + int + default 0 if ESP32_REV_MIN_0 + default 1 if ESP32_REV_MIN_1 + default 2 if ESP32_REV_MIN_2 + default 3 if ESP32_REV_MIN_3 + + config ESP32_DPORT_WORKAROUND + bool + default "y" if !FREERTOS_UNICORE && ESP32_REV_MIN < 2 + choice ESP32_DEFAULT_CPU_FREQ_MHZ prompt "CPU frequency" default ESP32_DEFAULT_CPU_FREQ_160 @@ -127,7 +155,7 @@ menu "ESP32-specific" config SPIRAM_CACHE_WORKAROUND bool "Enable workaround for bug in SPI RAM cache for Rev1 ESP32s" - depends on SPIRAM_USE_MEMMAP || SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC + depends on (SPIRAM_USE_MEMMAP || SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC) && (ESP32_REV_MIN < 3) default "y" help Revision 1 of the ESP32 has a bug that can cause a write to PSRAM not to take place in some situations @@ -138,6 +166,8 @@ menu "ESP32-specific" This will also not use any bits of newlib that are located in ROM, opting for a version that is compiled with the workaround and located in flash instead. + The workaround is not required for ESP32 revision 3 and above. + config SPIRAM_BANKSWITCH_ENABLE bool "Enable bank switching for >4MiB external RAM" default y @@ -150,6 +180,9 @@ menu "ESP32-specific" #Note that this is limited to 62 banks, as esp_spiram_writeback_cache needs some kind of mapping of #some banks below that mark to work. We cannot at this moment guarantee this to exist when himem is #enabled. + + If spiram 2T mode is enabled, the size of 64Mbit psram will be changed as 32Mbit, so himem will be + unusable. config SPIRAM_BANKSWITCH_RESERVE int "Amount of 32K pages to reserve for bank switching" depends on SPIRAM_BANKSWITCH_ENABLE @@ -237,6 +270,9 @@ menu "ESP32-specific" bool "HSPI host (SPI2)" config SPIRAM_OCCUPY_VSPI_HOST bool "VSPI host (SPI3)" + config SPIRAM_OCCUPY_NO_HOST + bool "Will not try to use any host, will abort if not able to use the PSRAM" + endchoice menu "PSRAM clock and cs IO for ESP32-DOWD" @@ -316,6 +352,20 @@ menu "ESP32-specific" For ESP32-PICO chip, the default value of this config should be 7. + config SPIRAM_2T_MODE + bool "Enable SPI PSRAM 2T mode" + depends on SPIRAM_SUPPORT + default "n" + help + Enable this option to fix single bit errors inside 64Mbit PSRAM. + + Some 64Mbit PSRAM chips have a hardware issue in the RAM which causes bit errors at multiple + fixed bit positions. + + Note: If this option is enabled, the 64Mbit PSRAM chip will appear to be 32Mbit in size. + Applications will not be affected unless the use the esp_himem APIs, which are not supported + in 2T mode. + endmenu # "SPI RAM config" config MEMMAP_TRACEMEM @@ -886,6 +936,16 @@ menu "ESP32-specific" In case more value will help improve the definition of the launch of the crystal. If the crystal could not start, it will be switched to internal RC. + config ESP32_RTC_XTAL_CAL_RETRY + int "Number of attempts to repeat 32k XTAL calibration" + default 1 + depends on ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL + help + Number of attempts to repeat 32k XTAL calibration + before giving up and switching to the internal RC. + Increase this option if the 32k crystal oscillator + does not start and switches to internal RC. + config ESP32_RTC_XTAL_BOOTSTRAP_CYCLES int "Bootstrap cycles for external 32kHz crystal" depends on ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL @@ -1018,6 +1078,13 @@ menu "ESP32-specific" This option depends on the CONFIG_FREERTOS_UNICORE option because RTC fast memory can be accessed only by PRO_CPU core. + config ESP32_DPORT_DIS_INTERRUPT_LVL + int "Disable the interrupt level for the DPORT workarounds" + default 5 + help + To prevent interrupting DPORT workarounds, + need to disable interrupt with a maximum used level in the system. + endmenu # ESP32-Specific menu Wi-Fi @@ -1033,37 +1100,6 @@ menu Wi-Fi If only Bluetooth is used, it is recommended to disable this option to reduce binary file size. - choice SW_COEXIST_PREFERENCE - prompt "WiFi/Bluetooth coexistence performance preference" - depends on SW_COEXIST_ENABLE - default SW_COEXIST_PREFERENCE_BALANCE - help - Choose Bluetooth/WiFi/Balance for different preference. - If choose WiFi, it will make WiFi performance better. Such, keep WiFi Audio more fluent. - If choose Bluetooth, it will make Bluetooth performance better. Such, keep Bluetooth(A2DP) Audio more - fluent. - If choose Balance, the performance of WiFi and bluetooth will be balance. It's default. Normally, just - choose balance, the A2DP audio can play fluently, too. - Except config preference in menuconfig, you can also call esp_coex_preference_set() dynamically. - - config SW_COEXIST_PREFERENCE_WIFI - bool "WiFi" - - config SW_COEXIST_PREFERENCE_BT - bool "Bluetooth(include BR/EDR and BLE)" - - config SW_COEXIST_PREFERENCE_BALANCE - bool "Balance" - - endchoice - - config SW_COEXIST_PREFERENCE_VALUE - int - depends on SW_COEXIST_ENABLE - default 0 if SW_COEXIST_PREFERENCE_WIFI - default 1 if SW_COEXIST_PREFERENCE_BT - default 2 if SW_COEXIST_PREFERENCE_BALANCE - config ESP32_WIFI_STATIC_RX_BUFFER_NUM int "Max number of WiFi static RX buffers" range 2 25 if !WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST @@ -1331,12 +1367,22 @@ menu Wi-Fi config ESP32_WIFI_IRAM_OPT bool "WiFi IRAM speed optimization" + default n if (BT_ENABLED && SPIRAM_SUPPORT) default y help Select this option to place frequently called Wi-Fi library functions in IRAM. When this option is disabled, more than 10Kbytes of IRAM memory will be saved but Wi-Fi throughput will be reduced. + config ESP32_WIFI_RX_IRAM_OPT + bool "WiFi RX IRAM speed optimization" + default n if (BT_ENABLED && SPIRAM_SUPPORT) + default y + help + Select this option to place frequently called Wi-Fi library RX functions in IRAM. + When this option is disabled, more than 17Kbytes of IRAM memory will be saved + but Wi-Fi performance will be reduced. + endmenu # Wi-Fi menu PHY @@ -1386,7 +1432,6 @@ menu PHY endmenu # PHY - menu "Power Management" config PM_ENABLE diff --git a/components/esp32/cache_err_int.c b/components/esp32/cache_err_int.c index a6de81ce6..c2a43929e 100644 --- a/components/esp32/cache_err_int.c +++ b/components/esp32/cache_err_int.c @@ -73,7 +73,6 @@ void esp_cache_err_int_init() int IRAM_ATTR esp_cache_err_get_cpuid() { - esp_dport_access_int_pause(); const uint32_t pro_mask = DPORT_PRO_CPU_DISABLED_CACHE_IA_DRAM1 | DPORT_PRO_CPU_DISABLED_CACHE_IA_DROM0 | diff --git a/components/esp32/clk.c b/components/esp32/clk.c index c965511cb..096933bbc 100644 --- a/components/esp32/clk.c +++ b/components/esp32/clk.c @@ -40,8 +40,19 @@ */ #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32_RTC_CLK_CAL_CYCLES +#ifdef CONFIG_ESP32_RTC_XTAL_CAL_RETRY +#define RTC_XTAL_CAL_RETRY CONFIG_ESP32_RTC_XTAL_CAL_RETRY +#else +#define RTC_XTAL_CAL_RETRY 1 +#endif + #define MHZ (1000000) +/* Lower threshold for a reasonably-looking calibration value for a 32k XTAL. + * The ideal value (assuming 32768 Hz frequency) is 1000000/32768*(2**19) = 16*10^6. + */ +#define MIN_32K_XTAL_CAL_VAL 15000000L + /* Indicates that this 32k oscillator gets input from external oscillator, rather * than a crystal. */ @@ -167,6 +178,11 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) { rtc_slow_freq_t rtc_slow_freq = slow_clk & RTC_CNTL_ANA_CLK_RTC_SEL_V; uint32_t cal_val = 0; + /* number of times to repeat 32k XTAL calibration + * before giving up and switching to the internal RC + */ + int retry_32k_xtal = RTC_XTAL_CAL_RETRY; + do { if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) { /* 32k XTAL oscillator needs to be enabled and running before it can @@ -185,7 +201,10 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) // When SLOW_CLK_CAL_CYCLES is set to 0, clock calibration will not be performed at startup. if (SLOW_CLK_CAL_CYCLES > 0) { cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, SLOW_CLK_CAL_CYCLES); - if (cal_val == 0 || cal_val < 15000000L) { + if (cal_val == 0 || cal_val < MIN_32K_XTAL_CAL_VAL) { + if (retry_32k_xtal-- > 0) { + continue; + } ESP_EARLY_LOGW(TAG, "32 kHz XTAL not found, switching to internal 150 kHz oscillator"); rtc_slow_freq = RTC_SLOW_FREQ_RTC; } diff --git a/components/esp32/component.mk b/components/esp32/component.mk index 3199ec127..699d74d32 100644 --- a/components/esp32/component.mk +++ b/components/esp32/component.mk @@ -8,6 +8,10 @@ ifndef CONFIG_NO_BLOBS LIBS += core rtc net80211 pp wpa smartconfig coexist wps wpa2 espnow phy mesh endif +ifdef CONFIG_FREERTOS_UNICORE + COMPONENT_OBJEXCLUDE := ipc.o +endif + ifdef CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY # This linker script must come before esp32.project.ld LINKER_SCRIPTS += esp32.extram.bss.ld diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 4ffccc035..887402030 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -47,7 +47,6 @@ #include "nvs_flash.h" #include "esp_event.h" #include "esp_spi_flash.h" -#include "esp_ipc.h" #include "esp_crosscore_int.h" #include "esp_dport_access.h" #include "esp_log.h" @@ -69,9 +68,11 @@ #include "esp_clk_internal.h" #include "esp_timer.h" #include "esp_pm.h" +#include "esp_flash_encrypt.h" #include "pm_impl.h" #include "trax.h" #include "esp_ota_ops.h" +#include "bootloader_flash_config.h" #define STRINGIFY(s) STRINGIFY2(s) #define STRINGIFY2(s) #s @@ -347,6 +348,11 @@ void start_cpu0_default(void) #endif #if CONFIG_DISABLE_BASIC_ROM_CONSOLE esp_efuse_disable_basic_rom_console(); +#endif +#ifdef CONFIG_FLASH_ENCRYPTION_DISABLE_PLAINTEXT + if (esp_flash_encryption_enabled()) { + esp_flash_write_protect_crypt_cnt(); + } #endif rtc_gpio_force_hold_dis_all(); esp_vfs_dev_uart_register(); @@ -390,6 +396,7 @@ void start_cpu0_default(void) spi_flash_init(); /* init default OS-aware flash access critical section */ spi_flash_guard_set(&g_flash_guard_default_ops); + #ifdef CONFIG_PM_ENABLE esp_pm_impl_init(); #ifdef CONFIG_PM_DFS_INIT_AUTO @@ -413,6 +420,21 @@ void start_cpu0_default(void) #if CONFIG_SW_COEXIST_ENABLE esp_coex_adapter_register(&g_coex_adapter_funcs); + coex_pre_init(); +#endif + + bootloader_flash_update_id(); +#if !CONFIG_SPIRAM_BOOT_INIT + // Read the application binary image header. This will also decrypt the header if the image is encrypted. + esp_image_header_t fhdr = {0}; + // This assumes that DROM is the first segment in the application binary, i.e. that we can read + // the binary header through cache by accessing SOC_DROM_LOW address. + memcpy(&fhdr, (void*) SOC_DROM_LOW, sizeof(fhdr)); + // If psram is uninitialized, we need to improve some flash configuration. + bootloader_flash_clock_config(&fhdr); + bootloader_flash_gpio_config(&fhdr); + bootloader_flash_dummy_config(&fhdr); + bootloader_flash_cs_timing_config(); #endif #if CONFIG_SW_COEXIST_ENABLE diff --git a/components/esp32/dport_access.c b/components/esp32/dport_access.c index 731bdb7ec..f7435a164 100644 --- a/components/esp32/dport_access.c +++ b/components/esp32/dport_access.c @@ -16,7 +16,7 @@ * DPORT access is used for do protection when dual core access DPORT internal register and APB register via DPORT simultaneously * This function will be initialize after FreeRTOS startup. * When cpu0 want to access DPORT register, it should notify cpu1 enter in high-priority interrupt for be mute. When cpu1 already in high-priority interrupt, - * cpu0 can access DPORT register. Currently, cpu1 will wait for cpu0 finish access and exit high-priority interrupt. + * cpu0 can access DPORT register. Currently, cpu1 will wait for cpu0 finish access and exit high-priority interrupt. */ #include @@ -116,7 +116,7 @@ void IRAM_ATTR esp_dport_access_stall_other_cpu_end(void) { #ifndef CONFIG_FREERTOS_UNICORE int cpu_id = xPortGetCoreID(); - + if (dport_core_state[0] == DPORT_CORE_STATE_IDLE || dport_core_state[1] == DPORT_CORE_STATE_IDLE) { return; @@ -249,14 +249,14 @@ void IRAM_ATTR esp_dport_access_read_buffer(uint32_t *buff_out, uint32_t address */ uint32_t IRAM_ATTR esp_dport_access_reg_read(uint32_t reg) { -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) return _DPORT_REG_READ(reg); #else uint32_t apb; unsigned int intLvl; __asm__ __volatile__ (\ + "rsil %[LVL], "XTSTR(CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL)"\n"\ "movi %[APB], "XTSTR(0x3ff40078)"\n"\ - "rsil %[LVL], "XTSTR(3)"\n"\ "l32i %[APB], %[APB], 0\n"\ "l32i %[REG], %[REG], 0\n"\ "wsr %[LVL], "XTSTR(PS)"\n"\ @@ -295,7 +295,7 @@ uint32_t IRAM_ATTR esp_dport_access_reg_read(uint32_t reg) */ uint32_t IRAM_ATTR esp_dport_access_sequence_reg_read(uint32_t reg) { -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) return _DPORT_REG_READ(reg); #else uint32_t apb; diff --git a/components/esp32/dport_panic_highint_hdl.S b/components/esp32/dport_panic_highint_hdl.S index bddde3cdf..35412deba 100644 --- a/components/esp32/dport_panic_highint_hdl.S +++ b/components/esp32/dport_panic_highint_hdl.S @@ -31,9 +31,10 @@ Interrupt , a high-priority interrupt, is used for several things: */ -#define L4_INTR_STACK_SIZE 8 +#define L4_INTR_STACK_SIZE 12 #define L4_INTR_A2_OFFSET 0 #define L4_INTR_A3_OFFSET 4 +#define L4_INTR_A4_OFFSET 8 .data _l4_intr_stack: .space L4_INTR_STACK_SIZE @@ -145,10 +146,11 @@ xt_highint4: movi a0, (1<callback == NULL) { + if (args == NULL || args->callback == NULL || out_handle == NULL) { return ESP_ERR_INVALID_ARG; } esp_timer_handle_t result = (esp_timer_handle_t) calloc(1, sizeof(*result)); @@ -122,33 +122,48 @@ esp_err_t esp_timer_create(const esp_timer_create_args_t* args, esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } + timer_list_lock(); timer->alarm = esp_timer_get_time() + timeout_us; timer->period = 0; #if WITH_PROFILING timer->times_armed++; #endif - return timer_insert(timer); + esp_err_t err = timer_insert(timer); + timer_list_unlock(); + return err; } esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } + timer_list_lock(); period_us = MAX(period_us, esp_timer_impl_get_min_period_us()); timer->alarm = esp_timer_get_time() + period_us; timer->period = period_us; #if WITH_PROFILING timer->times_armed++; #endif - return timer_insert(timer); + esp_err_t err = timer_insert(timer); + timer_list_unlock(); + return err; } esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer) { + if (timer == NULL) { + return ESP_ERR_INVALID_ARG; + } if (!is_initialized() || !timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } @@ -163,21 +178,17 @@ esp_err_t esp_timer_delete(esp_timer_handle_t timer) if (timer_armed(timer)) { return ESP_ERR_INVALID_STATE; } - xSemaphoreTakeRecursive(s_timer_delete_mutex, portMAX_DELAY); -#if WITH_PROFILING - if (timer == s_timer_in_callback) { - s_timer_in_callback = NULL; - } - timer_remove_inactive(timer); -#endif - free(timer); - xSemaphoreGiveRecursive(s_timer_delete_mutex); + timer_list_lock(); + timer->event_id = EVENT_ID_DELETE_TIMER; + timer->alarm = esp_timer_get_time(); + timer->period = 0; + timer_insert(timer); + timer_list_unlock(); return ESP_OK; } static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer) { - timer_list_lock(); #if WITH_PROFILING timer_remove_inactive(timer); #endif @@ -200,7 +211,6 @@ static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer) if (timer == LIST_FIRST(&s_timers)) { esp_timer_impl_set_alarm(timer->alarm); } - timer_list_unlock(); return ESP_OK; } @@ -251,12 +261,12 @@ static IRAM_ATTR bool timer_armed(esp_timer_handle_t timer) return timer->alarm > 0; } -static IRAM_ATTR void timer_list_lock() +static IRAM_ATTR void timer_list_lock(void) { portENTER_CRITICAL(&s_timer_lock); } -static IRAM_ATTR void timer_list_unlock() +static IRAM_ATTR void timer_list_unlock(void) { portEXIT_CRITICAL(&s_timer_lock); } @@ -266,13 +276,17 @@ static void timer_process_alarm(esp_timer_dispatch_t dispatch_method) /* unused, provision to allow running callbacks from ISR */ (void) dispatch_method; - xSemaphoreTakeRecursive(s_timer_delete_mutex, portMAX_DELAY); timer_list_lock(); uint64_t now = esp_timer_impl_get_time(); esp_timer_handle_t it = LIST_FIRST(&s_timers); while (it != NULL && it->alarm < now) { LIST_REMOVE(it, list_entry); + if (it->event_id == EVENT_ID_DELETE_TIMER) { + free(it); + it = LIST_FIRST(&s_timers); + continue; + } if (it->period > 0) { it->alarm += it->period; timer_insert(it); @@ -284,21 +298,16 @@ static void timer_process_alarm(esp_timer_dispatch_t dispatch_method) } #if WITH_PROFILING uint64_t callback_start = now; - s_timer_in_callback = it; #endif + esp_timer_cb_t callback = it->callback; + void* arg = it->arg; timer_list_unlock(); - (*it->callback)(it->arg); + (*callback)(arg); timer_list_lock(); now = esp_timer_impl_get_time(); #if WITH_PROFILING - /* The callback might have deleted the timer. - * If this happens, esp_timer_delete will set s_timer_in_callback - * to NULL. - */ - if (s_timer_in_callback) { - s_timer_in_callback->times_triggered++; - s_timer_in_callback->total_callback_run_time += now - callback_start; - } + it->times_triggered++; + it->total_callback_run_time += now - callback_start; #endif it = LIST_FIRST(&s_timers); } @@ -307,7 +316,6 @@ static void timer_process_alarm(esp_timer_dispatch_t dispatch_method) esp_timer_impl_set_alarm(first->alarm); } timer_list_unlock(); - xSemaphoreGiveRecursive(s_timer_delete_mutex); } static void timer_task(void* arg) @@ -331,7 +339,7 @@ static void IRAM_ATTR timer_alarm_handler(void* arg) } } -static IRAM_ATTR bool is_initialized() +static IRAM_ATTR bool is_initialized(void) { return s_timer_task != NULL; } @@ -355,18 +363,6 @@ esp_err_t esp_timer_init(void) goto out; } -#if CONFIG_SPIRAM_USE_MALLOC - memset(&s_timer_delete_mutex_memory, 0, sizeof(StaticQueue_t)); - s_timer_delete_mutex = xSemaphoreCreateRecursiveMutexStatic(&s_timer_delete_mutex_memory); -#else - s_timer_delete_mutex = xSemaphoreCreateRecursiveMutex(); -#endif - if (!s_timer_delete_mutex) { - err = ESP_ERR_NO_MEM; - goto out; - } - - int ret = xTaskCreatePinnedToCore(&timer_task, "esp_timer", ESP_TASK_TIMER_STACK, NULL, ESP_TASK_TIMER_PRIO, &s_timer_task, PRO_CPU_NUM); if (ret != pdPASS) { @@ -390,10 +386,6 @@ out: vSemaphoreDelete(s_timer_semaphore); s_timer_semaphore = NULL; } - if (s_timer_delete_mutex) { - vSemaphoreDelete(s_timer_delete_mutex); - s_timer_delete_mutex = NULL; - } return ESP_ERR_NO_MEM; } @@ -498,7 +490,7 @@ esp_err_t esp_timer_dump(FILE* stream) return ESP_OK; } -int64_t IRAM_ATTR esp_timer_get_next_alarm() +int64_t IRAM_ATTR esp_timer_get_next_alarm(void) { int64_t next_alarm = INT64_MAX; timer_list_lock(); @@ -510,7 +502,7 @@ int64_t IRAM_ATTR esp_timer_get_next_alarm() return next_alarm; } -int64_t IRAM_ATTR esp_timer_get_time() +int64_t IRAM_ATTR esp_timer_get_time(void) { return (int64_t) esp_timer_impl_get_time(); } diff --git a/components/esp32/esp_timer_esp32.c b/components/esp32/esp_timer_esp32.c index 75e3a58fa..8e048f2d0 100644 --- a/components/esp32/esp_timer_esp32.c +++ b/components/esp32/esp_timer_esp32.c @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "sys/param.h" #include "esp_err.h" #include "esp_timer.h" #include "esp_system.h" @@ -127,13 +128,6 @@ static uint32_t s_timer_us_per_overflow; // will not increment s_time_base_us if this flag is set. static bool s_mask_overflow; -//The timer_overflow_happened read alarm register to tell if overflow happened. -//However, there is a monent that overflow happens, and before ISR function called -//alarm register is set to another value, then you call timer_overflow_happened, -//it will return false. -//So we store the overflow value when new alarm is to be set. -static bool s_overflow_happened; - #ifdef CONFIG_PM_DFS_USE_RTC_TIMER_REF // If DFS is enabled, upon the first frequency change this value is set to the // difference between esp_timer value and RTC timer value. On every subsequent @@ -152,10 +146,6 @@ portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED; // Check if timer overflow has happened (but was not handled by ISR yet) static inline bool IRAM_ATTR timer_overflow_happened() { - if (s_overflow_happened) { - return true; - } - return ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) != 0 && ((REG_READ(FRC_TIMER_ALARM_REG(1)) == ALARM_OVERFLOW_VAL && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1))) && !s_mask_overflow) || (!TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_ALARM_REG(1))) && TIMER_IS_AFTER_OVERFLOW(REG_READ(FRC_TIMER_COUNT_REG(1)))))); @@ -222,35 +212,47 @@ uint64_t IRAM_ATTR esp_timer_impl_get_time() void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) { portENTER_CRITICAL(&s_time_update_lock); - // Alarm time relative to the moment when counter was 0 - uint64_t time_after_timebase_us = timestamp - s_time_base_us; - // Adjust current time if overflow has happened - bool overflow = timer_overflow_happened(); - uint64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1)); - - if (overflow) { - assert(time_after_timebase_us > s_timer_us_per_overflow); - time_after_timebase_us -= s_timer_us_per_overflow; - s_overflow_happened = true; - } - // Calculate desired timer compare value (may exceed 2^32-1) - uint64_t compare_val = time_after_timebase_us * s_timer_ticks_per_us; - uint32_t alarm_reg_val = ALARM_OVERFLOW_VAL; // Use calculated alarm value if it is less than ALARM_OVERFLOW_VAL. // Note that if by the time we update ALARM_REG, COUNT_REG value is higher, // interrupt will not happen for another ALARM_OVERFLOW_VAL timer ticks, // so need to check if alarm value is too close in the future (e.g. <2 us away). - const uint32_t offset = s_timer_ticks_per_us * 2; - if (compare_val < ALARM_OVERFLOW_VAL) { - if (compare_val < cur_count + offset) { - compare_val = cur_count + offset; - if (compare_val > ALARM_OVERFLOW_VAL) { - compare_val = ALARM_OVERFLOW_VAL; - } + int32_t offset = s_timer_ticks_per_us * 2; + do { + // Adjust current time if overflow has happened + if (timer_overflow_happened() || + ((REG_READ(FRC_TIMER_COUNT_REG(1)) > ALARM_OVERFLOW_VAL) && + ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) == 0))) { + // 1. timer_overflow_happened() checks overflow with the interrupt flag. + // 2. During several loops, the counter can be higher than the alarm and even step over ALARM_OVERFLOW_VAL boundary (the interrupt flag is not set). + timer_count_reload(); + s_time_base_us += s_timer_us_per_overflow; } - alarm_reg_val = (uint32_t) compare_val; - } - REG_WRITE(FRC_TIMER_ALARM_REG(1), alarm_reg_val); + s_mask_overflow = false; + int64_t cur_count = REG_READ(FRC_TIMER_COUNT_REG(1)); + // Alarm time relative to the moment when counter was 0 + int64_t time_after_timebase_us = (int64_t)timestamp - s_time_base_us; + // Calculate desired timer compare value (may exceed 2^32-1) + int64_t compare_val = time_after_timebase_us * s_timer_ticks_per_us; + + compare_val = MAX(compare_val, cur_count + offset); + uint32_t alarm_reg_val = ALARM_OVERFLOW_VAL; + if (compare_val < ALARM_OVERFLOW_VAL) { + alarm_reg_val = (uint32_t) compare_val; + } + REG_WRITE(FRC_TIMER_ALARM_REG(1), alarm_reg_val); + int64_t delta = (int64_t)alarm_reg_val - (int64_t)REG_READ(FRC_TIMER_COUNT_REG(1)); + if (delta <= 0) { + /* + When the timestamp is a bit less than the current counter then the alarm = current_counter + offset. + But due to CPU_freq in some case can be equal APB_freq the offset time can not exceed the overhead + (the alarm will be less than the counter) and it leads to the infinity loop. + To exclude this behavior to the offset was added the delta to have the opportunity to go through it. + */ + offset += abs((int)delta) + s_timer_ticks_per_us * 2; + } else { + break; + } + } while (1); portEXIT_CRITICAL(&s_time_update_lock); } @@ -261,7 +263,6 @@ static void IRAM_ATTR timer_alarm_isr(void *arg) if (timer_overflow_happened()) { timer_count_reload(); s_time_base_us += s_timer_us_per_overflow; - s_overflow_happened = false; } s_mask_overflow = false; // Clear interrupt status @@ -269,6 +270,17 @@ static void IRAM_ATTR timer_alarm_isr(void *arg) // Set alarm to the next overflow moment. Later, upper layer function may // call esp_timer_impl_set_alarm to change this to an earlier value. REG_WRITE(FRC_TIMER_ALARM_REG(1), ALARM_OVERFLOW_VAL); + if ((REG_READ(FRC_TIMER_COUNT_REG(1)) > ALARM_OVERFLOW_VAL) && + ((REG_READ(FRC_TIMER_CTRL_REG(1)) & FRC_TIMER_INT_STATUS) == 0)) { + /* + This check excludes the case when the alarm can be less than the counter. + Without this check, it is possible because DPORT uses 4-lvl, and users can use the 5 Hi-interrupt, + they can interrupt this function between FRC_TIMER_INT_CLR and setting the alarm = ALARM_OVERFLOW_VAL + that lead to the counter will go ahead leaving the alarm behind. + */ + timer_count_reload(); + s_time_base_us += s_timer_us_per_overflow; + } portEXIT_CRITICAL_ISR(&s_time_update_lock); // Call the upper layer handler (*s_alarm_handler)(arg); @@ -336,7 +348,6 @@ void esp_timer_impl_advance(int64_t time_us) REG_WRITE(FRC_TIMER_ALARM_REG(1), 0); REG_WRITE(FRC_TIMER_LOAD_REG(1), 0); s_time_base_us += count / s_timer_ticks_per_us + time_us; - s_overflow_happened = false; portEXIT_CRITICAL(&s_time_update_lock); } diff --git a/components/esp32/hwcrypto/aes.c b/components/esp32/hwcrypto/aes.c index e51e1aefc..3bf90a9bc 100644 --- a/components/esp32/hwcrypto/aes.c +++ b/components/esp32/hwcrypto/aes.c @@ -28,6 +28,7 @@ #include #include "mbedtls/aes.h" #include "hwcrypto/aes.h" +#include "mbedtls/platform_util.h" #include "soc/dport_reg.h" #include "soc/hwcrypto_reg.h" #include @@ -49,6 +50,11 @@ */ static portMUX_TYPE aes_spinlock = portMUX_INITIALIZER_UNLOCKED; +static inline bool valid_key_length(const esp_aes_context *ctx) +{ + return ctx->key_bytes == 128/8 || ctx->key_bytes == 192/8 || ctx->key_bytes == 256/8; +} + void esp_aes_acquire_hardware( void ) { portENTER_CRITICAL(&aes_spinlock); @@ -93,6 +99,7 @@ int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key, } ctx->key_bytes = keybits / 8; memcpy(ctx->key, key, ctx->key_bytes); + ctx->key_in_hardware = 0; return 0; } @@ -102,35 +109,83 @@ int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key, * * Call only while holding esp_aes_acquire_hardware(). */ -static inline void esp_aes_setkey_hardware( esp_aes_context *ctx, int mode) +static void esp_aes_setkey_hardware(esp_aes_context *ctx, int mode) { const uint32_t MODE_DECRYPT_BIT = 4; unsigned mode_reg_base = (mode == ESP_AES_ENCRYPT) ? 0 : MODE_DECRYPT_BIT; + ctx->key_in_hardware = 0; + for (int i = 0; i < ctx->key_bytes/4; ++i) { DPORT_REG_WRITE(AES_KEY_BASE + i * 4, *(((uint32_t *)ctx->key) + i)); + ctx->key_in_hardware += 4; } DPORT_REG_WRITE(AES_MODE_REG, mode_reg_base + ((ctx->key_bytes / 8) - 2)); + + /* Fault injection check: all words of key data should have been written to hardware */ + if (ctx->key_in_hardware < 16 + || ctx->key_in_hardware != ctx->key_bytes) { + abort(); + } } /* Run a single 16 byte block of AES, using the hardware engine. * * Call only while holding esp_aes_acquire_hardware(). */ -static inline void esp_aes_block(const void *input, void *output) +static int esp_aes_block(esp_aes_context *ctx, const void *input, void *output) { const uint32_t *input_words = (const uint32_t *)input; + uint32_t i0, i1, i2, i3; uint32_t *output_words = (uint32_t *)output; - uint32_t *mem_block = (uint32_t *)AES_TEXT_BASE; - for(int i = 0; i < 4; i++) { - mem_block[i] = input_words[i]; + /* If no key is written to hardware yet, either the user hasn't called + mbedtls_aes_setkey_enc/mbedtls_aes_setkey_dec - meaning we also don't + know which mode to use - or a fault skipped the + key write to hardware. Treat this as a fatal error and zero the output block. + */ + if (ctx->key_in_hardware != ctx->key_bytes) { + bzero(output, 16); + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; } + /* Storing i0,i1,i2,i3 in registers not an array + helps a lot with optimisations at -Os level */ + i0 = input_words[0]; + DPORT_REG_WRITE(AES_TEXT_BASE, i0); + + i1 = input_words[1]; + DPORT_REG_WRITE(AES_TEXT_BASE + 4, i1); + + i2 = input_words[2]; + DPORT_REG_WRITE(AES_TEXT_BASE + 8, i2); + + i3 = input_words[3]; + DPORT_REG_WRITE(AES_TEXT_BASE + 12, i3); + DPORT_REG_WRITE(AES_START_REG, 1); + while (DPORT_REG_READ(AES_IDLE_REG) != 1) { } - esp_dport_access_read_buffer(output_words, (uint32_t)&mem_block[0], 4); + + esp_dport_access_read_buffer(output_words, AES_TEXT_BASE, 4); + + /* Physical security check: Verify the AES accelerator actually ran, and wasn't + skipped due to external fault injection while starting the peripheral. + + Note that i0,i1,i2,i3 are copied from input buffer in case input==output. + + Bypassing this check requires at least one additional fault. + */ + if(i0 == output_words[0] && i1 == output_words[1] && i2 == output_words[2] && i3 == output_words[3]) { + // calling zeroing functions to narrow the + // window for a double-fault of the abort step, here + memset(output, 0, 16); + mbedtls_platform_zeroize(output, 16); + abort(); + } + + return 0; } /* @@ -140,11 +195,18 @@ int esp_internal_aes_encrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) { + int r; + + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT); - esp_aes_block(input, output); + r = esp_aes_block(ctx, input, output); esp_aes_release_hardware(); - return 0; + return r; } void esp_aes_encrypt( esp_aes_context *ctx, @@ -162,11 +224,18 @@ int esp_internal_aes_decrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) { + int r; + + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, ESP_AES_DECRYPT); - esp_aes_block(input, output); + r = esp_aes_block(ctx, input, output); esp_aes_release_hardware(); - return 0; + return r; } void esp_aes_decrypt( esp_aes_context *ctx, @@ -176,7 +245,6 @@ void esp_aes_decrypt( esp_aes_context *ctx, esp_internal_aes_decrypt(ctx, input, output); } - /* * AES-ECB block encryption/decryption */ @@ -185,12 +253,19 @@ int esp_aes_crypt_ecb( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) { + int r; + + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, mode); - esp_aes_block(input, output); + r = esp_aes_block(ctx, input, output); esp_aes_release_hardware(); - return 0; + return r; } @@ -214,14 +289,19 @@ int esp_aes_crypt_cbc( esp_aes_context *ctx, return ( ERR_ESP_AES_INVALID_INPUT_LENGTH ); } + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, mode); if ( mode == ESP_AES_DECRYPT ) { while ( length > 0 ) { memcpy(temp, input_words, 16); - esp_aes_block(input_words, output_words); + esp_aes_block(ctx, input_words, output_words); for ( i = 0; i < 4; i++ ) { output_words[i] = output_words[i] ^ iv_words[i]; @@ -240,7 +320,7 @@ int esp_aes_crypt_cbc( esp_aes_context *ctx, output_words[i] = input_words[i] ^ iv_words[i]; } - esp_aes_block(output_words, output_words); + esp_aes_block(ctx, output_words, output_words); memcpy( iv_words, output_words, 16 ); input_words += 4; @@ -268,14 +348,19 @@ int esp_aes_crypt_cfb128( esp_aes_context *ctx, int c; size_t n = *iv_off; + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT); if ( mode == ESP_AES_DECRYPT ) { while ( length-- ) { if ( n == 0 ) { - esp_aes_block(iv, iv ); + esp_aes_block(ctx, iv, iv ); } c = *input++; @@ -287,7 +372,7 @@ int esp_aes_crypt_cfb128( esp_aes_context *ctx, } else { while ( length-- ) { if ( n == 0 ) { - esp_aes_block(iv, iv ); + esp_aes_block(ctx, iv, iv ); } iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); @@ -316,13 +401,18 @@ int esp_aes_crypt_cfb8( esp_aes_context *ctx, unsigned char c; unsigned char ov[17]; + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT); while ( length-- ) { memcpy( ov, iv, 16 ); - esp_aes_block(iv, iv); + esp_aes_block(ctx, iv, iv); if ( mode == ESP_AES_DECRYPT ) { ov[16] = *input; @@ -356,13 +446,18 @@ int esp_aes_crypt_ctr( esp_aes_context *ctx, int c, i; size_t n = *nc_off; + if (!valid_key_length(ctx)) { + return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; + } + esp_aes_acquire_hardware(); + ctx->key_in_hardware = 0; esp_aes_setkey_hardware(ctx, ESP_AES_ENCRYPT); while ( length-- ) { if ( n == 0 ) { - esp_aes_block(nonce_counter, stream_block); + esp_aes_block(ctx, nonce_counter, stream_block); for ( i = 16; i > 0; i-- ) if ( ++nonce_counter[i - 1] != 0 ) { diff --git a/components/esp32/hwcrypto/sha.c b/components/esp32/hwcrypto/sha.c index 7a064340a..a8b5de18a 100644 --- a/components/esp32/hwcrypto/sha.c +++ b/components/esp32/hwcrypto/sha.c @@ -195,7 +195,7 @@ static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_t void esp_sha_unlock_engine(esp_sha_type sha_type) { - SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type); + SemaphoreHandle_t engine_state = sha_get_engine_state(sha_type); portENTER_CRITICAL(&engines_in_use_lock); @@ -226,9 +226,12 @@ void esp_sha_wait_idle(void) void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state) { + uint32_t *digest_state_words = NULL; + uint32_t *reg_addr_buf = NULL; + uint32_t word_len = sha_length(sha_type)/4; #ifndef NDEBUG { - SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type); + SemaphoreHandle_t engine_state = sha_get_engine_state(sha_type); assert(uxSemaphoreGetCount(engine_state) == 0 && "SHA engine should be locked" ); } @@ -243,27 +246,37 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state) DPORT_REG_WRITE(SHA_LOAD_REG(sha_type), 1); while(DPORT_REG_READ(SHA_BUSY_REG(sha_type)) == 1) { } - uint32_t *digest_state_words = (uint32_t *)digest_state; - uint32_t *reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE); + digest_state_words = (uint32_t *)digest_state; + reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE); if(sha_type == SHA2_384 || sha_type == SHA2_512) { /* for these ciphers using 64-bit states, swap each pair of words */ DPORT_INTERRUPT_DISABLE(); // Disable interrupt only on current CPU. - for(int i = 0; i < sha_length(sha_type)/4; i += 2) { + for(int i = 0; i < word_len; i += 2) { digest_state_words[i+1] = DPORT_SEQUENCE_REG_READ((uint32_t)®_addr_buf[i]); digest_state_words[i] = DPORT_SEQUENCE_REG_READ((uint32_t)®_addr_buf[i+1]); } DPORT_INTERRUPT_RESTORE(); // restore the previous interrupt level } else { - esp_dport_access_read_buffer(digest_state_words, (uint32_t)®_addr_buf[0], sha_length(sha_type)/4); + esp_dport_access_read_buffer(digest_state_words, (uint32_t)®_addr_buf[0], word_len); } esp_sha_unlock_memory_block(); + + /* Fault injection check: verify SHA engine actually ran, + state is not all zeroes. + */ + for (int i = 0; i < word_len; i++) { + if (digest_state_words[i] != 0) { + return; + } + } + abort(); // SHA peripheral returned all zero state, probably due to fault injection } void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block) { #ifndef NDEBUG { - SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type); + SemaphoreHandle_t engine_state = sha_get_engine_state(sha_type); assert(uxSemaphoreGetCount(engine_state) == 0 && "SHA engine should be locked" ); } @@ -310,6 +323,8 @@ void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, uns esp_sha_lock_engine(sha_type); + bzero(output, sha_length(sha_type)); + SHA_CTX ctx; ets_sha_init(&ctx); @@ -353,4 +368,14 @@ void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, uns } esp_sha_unlock_engine(sha_type); + + /* Fault injection check: verify SHA engine actually ran, + state is not all zeroes. + */ + for (int i = 0; i < sha_length(sha_type); i++) { + if (output[i] != 0) { + return; + } + } + abort(); // SHA peripheral returned all zero state, probably due to fault injection } diff --git a/components/esp32/include/esp_coexist.h b/components/esp32/include/esp_coexist.h index c9b241d3d..7d31e8602 100644 --- a/components/esp32/include/esp_coexist.h +++ b/components/esp32/include/esp_coexist.h @@ -32,6 +32,22 @@ typedef enum { ESP_COEX_PREFER_NUM, /*!< Prefer value numbers */ } esp_coex_prefer_t; + /** + * @brief coex status type + */ +typedef enum { + ESP_COEX_ST_TYPE_WIFI = 0, + ESP_COEX_ST_TYPE_BLE, + ESP_COEX_ST_TYPE_BT, +} esp_coex_status_type_t; + +#define ESP_COEX_BLE_ST_MESH_CONFIG 0x08 +#define ESP_COEX_BLE_ST_MESH_TRAFFIC 0x10 +#define ESP_COEX_BLE_ST_MESH_STANDBY 0x20 + +#define ESP_COEX_BT_ST_A2DP_STREAMING 0x10 +#define ESP_COEX_BT_ST_A2DP_PAUSED 0x20 + /** * @brief Get software coexist version string * @@ -40,7 +56,8 @@ typedef enum { const char *esp_coex_version_get(void); /** - * @brief Set coexist preference of performance + * @deprecated Use esp_coex_status_bit_set() and esp_coex_status_bit_clear() instead. + * Set coexist preference of performance * For example, if prefer to bluetooth, then it will make A2DP(play audio via classic bt) * more smooth while wifi is runnning something. * If prefer to wifi, it will do similar things as prefer to bluetooth. @@ -51,6 +68,23 @@ const char *esp_coex_version_get(void); */ esp_err_t esp_coex_preference_set(esp_coex_prefer_t prefer); +/** + * @brief Set coex schm status + * @param type : WIFI/BLE/BT + * @param status : WIFI/BLE/BT STATUS + * @return : ESP_OK - success, other - failed + */ +esp_err_t esp_coex_status_bit_set(esp_coex_status_type_t type, uint32_t status); + +/** + * @brief Clear coex schm status + * @param type : WIFI/BLE/BT + * @param status : WIFI/BLE/BT STATUS + * @return : ESP_OK - success, other - failed + */ +esp_err_t esp_coex_status_bit_clear(esp_coex_status_type_t type, uint32_t status); + + #ifdef __cplusplus } #endif diff --git a/components/esp32/include/esp_coexist_internal.h b/components/esp32/include/esp_coexist_internal.h index 1d94d6db7..bdd023a93 100644 --- a/components/esp32/include/esp_coexist_internal.h +++ b/components/esp32/include/esp_coexist_internal.h @@ -31,6 +31,14 @@ typedef enum { typedef void (* coex_func_cb_t)(uint32_t event, int sched_cnt); +/** + * @brief Pre-Init software coexist + * extern function for internal use. + * + * @return Init ok or failed. + */ +esp_err_t coex_pre_init(void); + /** * @brief Init software coexist * extern function for internal use. @@ -79,6 +87,12 @@ esp_err_t coex_preference_set(coex_prefer_t prefer); */ uint32_t coex_status_get(void); +/** + * @brief Set software coexist condition. + * @return : software coexist condition + */ +void coex_condition_set(uint32_t type, bool dissatisfy); + /** * @brief WiFi requests coexistence. * diff --git a/components/esp32/include/esp_dport_access.h b/components/esp32/include/esp_dport_access.h index adbb82820..8ea60c4cd 100644 --- a/components/esp32/include/esp_dport_access.h +++ b/components/esp32/include/esp_dport_access.h @@ -33,7 +33,7 @@ uint32_t esp_dport_access_sequence_reg_read(uint32_t reg); //only call in case of panic(). void esp_dport_access_int_abort(void); -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) #define DPORT_STALL_OTHER_CPU_START() #define DPORT_STALL_OTHER_CPU_END() #define DPORT_INTERRUPT_DISABLE() @@ -41,7 +41,7 @@ void esp_dport_access_int_abort(void); #else #define DPORT_STALL_OTHER_CPU_START() esp_dport_access_stall_other_cpu_start() #define DPORT_STALL_OTHER_CPU_END() esp_dport_access_stall_other_cpu_end() -#define DPORT_INTERRUPT_DISABLE() unsigned int intLvl = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL) +#define DPORT_INTERRUPT_DISABLE() unsigned int intLvl = XTOS_SET_INTLEVEL(CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL) #define DPORT_INTERRUPT_RESTORE() XTOS_RESTORE_JUST_INTLEVEL(intLvl) #endif diff --git a/components/esp32/include/esp_idf_version.h b/components/esp32/include/esp_idf_version.h new file mode 100644 index 000000000..735abc78b --- /dev/null +++ b/components/esp32/include/esp_idf_version.h @@ -0,0 +1,58 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/** Major version number (X.x.x) */ +#define ESP_IDF_VERSION_MAJOR 3 +/** Minor version number (x.X.x) */ +#define ESP_IDF_VERSION_MINOR 3 +/** Patch version number (x.x.X) */ +#define ESP_IDF_VERSION_PATCH 2 + +/** + * Macro to convert IDF version number into an integer + * + * To be used in comparisons, such as ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0) + */ +#define ESP_IDF_VERSION_VAL(major, minor, patch) ((major << 16) | (minor << 8) | (patch)) + +/** + * Current IDF version, as an integer + * + * To be used in comparisons, such as ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0) + */ +#define ESP_IDF_VERSION ESP_IDF_VERSION_VAL(ESP_IDF_VERSION_MAJOR, \ + ESP_IDF_VERSION_MINOR, \ + ESP_IDF_VERSION_PATCH) + +/** + * Return full IDF version string, same as 'git describe' output. + * + * @note If you are printing the ESP-IDF version in a log file or other information, + * this function provides more information than using the numerical version macros. + * For example, numerical version macros don't differentiate between development, + * pre-release and release versions, but the output of this function does. + * + * @return constant string from IDF_VER + */ +const char* esp_get_idf_version(void); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp32/include/esp_phy_init.h b/components/esp32/include/esp_phy_init.h index 6783ff54b..3402f197b 100644 --- a/components/esp32/include/esp_phy_init.h +++ b/components/esp32/include/esp_phy_init.h @@ -202,6 +202,18 @@ esp_err_t esp_phy_rf_deinit(phy_rf_module_t module); */ void esp_phy_load_cal_and_init(phy_rf_module_t module); +/** +* @brief Enable WiFi/BT common clock +* +*/ +void esp_phy_common_clock_enable(void); + +/** +* @brief Disable WiFi/BT common clock +* +*/ +void esp_phy_common_clock_disable(void); + /** * @brief Module requires to enter modem sleep */ diff --git a/components/esp32/include/esp_private/esp_wifi_private.h b/components/esp32/include/esp_private/esp_wifi_private.h new file mode 100644 index 000000000..fcdebb6ba --- /dev/null +++ b/components/esp32/include/esp_private/esp_wifi_private.h @@ -0,0 +1,24 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_WIFI_PRIVATE_H +#define _ESP_WIFI_PRIVATE_H + +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" +#include "rom/queue.h" +#include "sdkconfig.h" +#include "esp_wifi_crypto_types.h" +#include "esp_wifi_os_adapter.h" + +#endif /* _ESP_WIFI_PRIVATE_H */ diff --git a/components/esp32/include/esp_private/esp_wifi_types_private.h b/components/esp32/include/esp_private/esp_wifi_types_private.h new file mode 100644 index 000000000..dd56e0fde --- /dev/null +++ b/components/esp32/include/esp_private/esp_wifi_types_private.h @@ -0,0 +1,21 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_WIFI_TYPES_PRIVATE_H +#define _ESP_WIFI_TYPES_PRIVATE_H + +#include "rom/queue.h" +#include "esp_interface.h" + +#endif diff --git a/components/esp32/include/esp_system.h b/components/esp32/include/esp_system.h index 05214c8f5..1a51999d3 100644 --- a/components/esp32/include/esp_system.h +++ b/components/esp32/include/esp_system.h @@ -19,6 +19,7 @@ #include #include "esp_err.h" #include "esp_sleep.h" +#include "esp_idf_version.h" #ifdef __cplusplus extern "C" { @@ -292,14 +293,6 @@ esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac) const char* system_get_sdk_version(void) __attribute__ ((deprecated)); /** @endcond */ -/** - * Get IDF version - * - * @return constant string from IDF_VER - */ -const char* esp_get_idf_version(void); - - /** * @brief Chip models */ diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index e67f4606b..e82c05911 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -59,15 +59,10 @@ #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/queue.h" -#include "rom/queue.h" -#include "sdkconfig.h" #include "esp_err.h" #include "esp_wifi_types.h" -#include "esp_wifi_crypto_types.h" #include "esp_event.h" -#include "esp_wifi_os_adapter.h" +#include "esp_private/esp_wifi_private.h" #ifdef __cplusplus extern "C" { @@ -89,6 +84,10 @@ extern "C" { #define ESP_ERR_WIFI_WOULD_BLOCK (ESP_ERR_WIFI_BASE + 14) /*!< The caller would block */ #define ESP_ERR_WIFI_NOT_CONNECT (ESP_ERR_WIFI_BASE + 15) /*!< Station still in disconnect status */ +#define ESP_ERR_WIFI_POST (ESP_ERR_WIFI_BASE + 18) /*!< Failed to post the event to WiFi task */ +#define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalod WiFi state when init/deinit is called */ +#define ESP_ERR_WIFI_STOP_STATE (ESP_ERR_WIFI_BASE + 20) /*!< Returned when WiFi is stopping */ + /** * @brief WiFi stack configuration parameters passed to esp_wifi_init call. */ @@ -543,8 +542,10 @@ esp_err_t esp_wifi_get_bandwidth(wifi_interface_t ifx, wifi_bandwidth_t *bw); /** * @brief Set primary/secondary channel of ESP32 * - * @attention 1. This is a special API for sniffer - * @attention 2. This API should be called after esp_wifi_start() or esp_wifi_set_promiscuous() + * @attention 1. This API should be called after esp_wifi_start() + * @attention 2. When ESP32 is in STA mode, this API should not be called when STA is scanning or connecting to an external AP + * @attention 3. When ESP32 is in softAP mode, this API should not be called when softAP has connected to external STAs + * @attention 4. When ESP32 is in STA+softAP mode, this API should not be called when in the scenarios described above * * @param primary for HT20, primary is the channel number, for HT40, primary is the primary channel * @param second for HT20, second is ignored, for HT40, second is the second channel @@ -581,7 +582,7 @@ esp_err_t esp_wifi_get_channel(uint8_t *primary, wifi_second_chan_t *second); * and the country info of the AP to which the station is connected is {.cc="JP", .schan=1, .nchan=14} * then the country info that will be used is {.cc="JP", .schan=1, .nchan=14}. If the station disconnected * from the AP the country info is set back back to the country info of the station automatically, - * {.cc="USA", .schan=1, .nchan=11} in the example. + * {.cc="US", .schan=1, .nchan=11} in the example. * @attention 3. When the country policy is WIFI_COUNTRY_POLICY_MANUAL, always use the configured country info. * @attention 4. When the country info is changed because of configuration or because the station connects to a different * external AP, the country IE in probe response/beacon of the soft-AP is changed also. @@ -799,6 +800,21 @@ esp_err_t esp_wifi_get_config(wifi_interface_t interface, wifi_config_t *conf); */ esp_err_t esp_wifi_ap_get_sta_list(wifi_sta_list_t *sta); +/** + * @brief Get AID of STA connected with soft-AP + * + * @param mac STA's mac address + * @param[out] aid Store the AID corresponding to STA mac + * + * @return + * - ESP_OK: succeed + * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init + * - ESP_ERR_INVALID_ARG: invalid argument + * - ESP_ERR_NOT_FOUND: Requested resource not found + * - ESP_ERR_WIFI_MODE: WiFi mode is wrong + * - ESP_ERR_WIFI_CONN: WiFi internal error, the station/soft-AP control block is invalid + */ +esp_err_t esp_wifi_ap_get_sta_aid(const uint8_t mac[6], uint16_t *aid); /** * @brief Set the WiFi API configuration storage type @@ -840,6 +856,16 @@ esp_err_t esp_wifi_set_auto_connect(bool en) __attribute__ ((deprecated)); */ esp_err_t esp_wifi_get_auto_connect(bool *en) __attribute__ ((deprecated)); +/** + * @brief Function signature for received Vendor-Specific Information Element callback. + * @param ctx Context argument, as passed to esp_wifi_set_vendor_ie_cb() when registering callback. + * @param type Information element type, based on frame type received. + * @param sa Source 802.11 address. + * @param vnd_ie Pointer to the vendor specific element data received. + * @param rssi Received signal strength indication. + */ +typedef void (*esp_vendor_ie_cb_t) (void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const vendor_ie_data_t *vnd_ie, int rssi); + /** * @brief Set 802.11 Vendor-Specific Information Element * @@ -858,16 +884,6 @@ esp_err_t esp_wifi_get_auto_connect(bool *en) __attribute__ ((deprecated)); */ esp_err_t esp_wifi_set_vendor_ie(bool enable, wifi_vendor_ie_type_t type, wifi_vendor_ie_id_t idx, const void *vnd_ie); -/** - * @brief Function signature for received Vendor-Specific Information Element callback. - * @param ctx Context argument, as passed to esp_wifi_set_vendor_ie_cb() when registering callback. - * @param type Information element type, based on frame type received. - * @param sa Source 802.11 address. - * @param vnd_ie Pointer to the vendor specific element data received. - * @param rssi Received signal strength indication. - */ -typedef void (*esp_vendor_ie_cb_t) (void *ctx, wifi_vendor_ie_type_t type, const uint8_t sa[6], const vendor_ie_data_t *vnd_ie, int rssi); - /** * @brief Register Vendor-Specific Information Element monitoring callback. * @@ -881,66 +897,59 @@ typedef void (*esp_vendor_ie_cb_t) (void *ctx, wifi_vendor_ie_type_t type, const esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx); /** - * @brief Set maximum WiFi transmiting power + * @brief Set maximum transmitting power after WiFi start. * - * @attention WiFi transmiting power is divided to six levels in phy init data. - * Level0 represents highest transmiting power and level5 represents lowest - * transmiting power. Packets of different rates are transmitted in - * different powers according to the configuration in phy init data. - * This API only sets maximum WiFi transmiting power. If this API is called, - * the transmiting power of every packet will be less than or equal to the - * value set by this API. If this API is not called, the value of maximum - * transmitting power set in phy_init_data.bin or menuconfig (depend on - * whether to use phy init data in partition or not) will be used. Default - * value is level0. Values passed in power are mapped to transmit power - * levels as follows: - * - [78, 127]: level0 - * - [76, 77]: level1 - * - [74, 75]: level2 - * - [68, 73]: level3 - * - [60, 67]: level4 - * - [52, 59]: level5 - * - [44, 51]: level5 - 2dBm - * - [34, 43]: level5 - 4.5dBm - * - [28, 33]: level5 - 6dBm - * - [20, 27]: level5 - 8dBm - * - [8, 19]: level5 - 11dBm - * - [-128, 7]: level5 - 14dBm - * - * @param power Maximum WiFi transmiting power. + * @attention 1. Maximum power before wifi startup is limited by PHY init data bin. + * @attention 2. The value set by this API will be mapped to the max_tx_power of the structure wifi_country_t variable. + * @attention 3. Mapping Table {Power, max_tx_power} = {{8, 2}, {20, 5}, {28, 7}, {34, 8}, {44, 11}, + * {52, 13}, {56, 14}, {60, 15}, {66, 16}, {72, 18}, {78, 20}}. + * @attention 4. Param power unit is 0.25dBm, range is [8, 78] corresponding to 2dBm - 20dBm. + * @attention 5. Relationship between set value and actual value. As follows: + * +------------+--------------+ + * | set value | actual value | + * +============+==============+ + * | [8, 19] | 8 | + * +------------+--------------+ + * | [20, 27] | 20 | + * +------------+--------------+ + * | [28, 33] | 28 | + * +------------+--------------+ + * | [34, 43] | 34 | + * +------------+--------------+ + * | [44, 51] | 44 | + * +------------+--------------+ + * | [52, 55] | 52 | + * +------------+--------------+ + * | [56, 59] | 56 | + * +------------+--------------+ + * | [60, 65] | 60 | + * +------------+--------------+ + * | [66, 71] | 66 | + * +------------+--------------+ + * | [72, 77] | 72 | + * +------------+--------------+ + * | 78 | 78 | + * +------------+--------------+ + * @param power Maximum WiFi transmitting power. * * @return * - ESP_OK: succeed * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init * - ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start + * - ESP_ERR_WIFI_ARG: invalid argument, e.g. parameter is out of range */ esp_err_t esp_wifi_set_max_tx_power(int8_t power); /** - * @brief Get maximum WiFi transmiting power + * @brief Get maximum transmiting power after WiFi start * - * @attention This API gets maximum WiFi transmiting power. Values got - * from power are mapped to transmit power levels as follows: - * - 78: 19.5dBm - * - 76: 19dBm - * - 74: 18.5dBm - * - 68: 17dBm - * - 60: 15dBm - * - 52: 13dBm - * - 44: 11dBm - * - 34: 8.5dBm - * - 28: 7dBm - * - 20: 5dBm - * - 8: 2dBm - * - -4: -1dBm - * - * @param power Maximum WiFi transmiting power. + * @param power Maximum WiFi transmitting power, unit is 0.25dBm. * * @return * - ESP_OK: succeed * - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init * - ESP_ERR_WIFI_NOT_START: WiFi is not started by esp_wifi_start - * - ESP_ERR_INVALID_ARG: invalid argument + * - ESP_ERR_WIFI_ARG: invalid argument */ esp_err_t esp_wifi_get_max_tx_power(int8_t *power); @@ -1102,19 +1111,6 @@ esp_err_t esp_wifi_set_ant(const wifi_ant_config_t *config); */ esp_err_t esp_wifi_get_ant(wifi_ant_config_t *config); -/** - * @brief A general API to set/get WiFi internal configuration, it's for debug only - * - * @param cmd : ioctl command type - * @param cfg : configuration for the command - * - * @return - * - ESP_OK: succeed - * - others: failed - */ -esp_err_t esp_wifi_internal_ioctl(int cmd, wifi_ioctl_config_t *cfg); - - #ifdef __cplusplus } #endif diff --git a/components/esp32/include/esp_wifi_internal.h b/components/esp32/include/esp_wifi_internal.h index 979d0134e..71d0a9c24 100644 --- a/components/esp32/include/esp_wifi_internal.h +++ b/components/esp32/include/esp_wifi_internal.h @@ -204,15 +204,26 @@ esp_err_t esp_wifi_internal_osi_funcs_md5_check(const char *md5); esp_err_t esp_wifi_internal_crypto_funcs_md5_check(const char *md5); /** - * @brief Check the git commit id of WiFi library + * @brief Check the MD5 values of the esp_wifi_types.h in IDF and WiFi library * - * @attention 1. It is used for internal CI WiFi library check + * @attention 1. It is used for internal CI version check * * @return * - ESP_OK : succeed - * - ESP_FAIL : fail + * - ESP_WIFI_INVALID_ARG : MD5 check fail */ -esp_err_t esp_wifi_internal_git_commit_id_check(void); +esp_err_t esp_wifi_internal_wifi_type_md5_check(const char *md5); + +/** + * @brief Check the MD5 values of the esp_wifi.h in IDF and WiFi library + * + * @attention 1. It is used for internal CI version check + * + * @return + * - ESP_OK : succeed + * - ESP_WIFI_INVALID_ARG : MD5 check fail + */ +esp_err_t esp_wifi_internal_esp_wifi_md5_check(const char *md5); /** * @brief Allocate a chunk of memory for WiFi driver @@ -307,6 +318,43 @@ esp_err_t esp_wifi_internal_set_log_mod(wifi_log_module_t module, uint32_t submo */ esp_err_t esp_wifi_internal_get_log(wifi_log_level_t *log_level, uint32_t *log_mod); +/** + * @brief Get the user-configured channel info + * + * @param ifx : WiFi interface + * @param primary : store the configured primary channel + * @param second : store the configured second channel + * + * @return + * - ESP_OK: succeed + */ +esp_err_t esp_wifi_internal_get_config_channel(wifi_interface_t ifx, uint8_t *primary, uint8_t *second); + +/** + * @brief Get the negotiated channel info after WiFi connection established + * + * @param ifx : WiFi interface + * @param aid : the connection number when a STA connects to the softAP + * @param primary : store the negotiated primary channel + * @param second : store the negotiated second channel + * @attention the aid param is only works when the ESP32 in softAP/softAP+STA mode + * + * @return + * - ESP_OK: succeed + */ +esp_err_t esp_wifi_internal_get_negotiated_channel(wifi_interface_t ifx, uint8_t aid, uint8_t *primary, uint8_t *second); + +/** + * @brief Get the negotiated bandwidth info after WiFi connection established + * + * @param ifx : WiFi interface + * @param bw : store the negotiated bandwidth + * + * @return + * - ESP_OK: succeed + */ +esp_err_t esp_wifi_internal_get_negotiated_bandwidth(wifi_interface_t ifx, uint8_t aid, uint8_t *bw); + #ifdef __cplusplus } #endif diff --git a/components/esp32/include/esp_wifi_os_adapter.h b/components/esp32/include/esp_wifi_os_adapter.h index 165caa6ed..666c44d92 100644 --- a/components/esp32/include/esp_wifi_os_adapter.h +++ b/components/esp32/include/esp_wifi_os_adapter.h @@ -21,7 +21,7 @@ extern "C" { #endif -#define ESP_WIFI_OS_ADAPTER_VERSION 0x00000002 +#define ESP_WIFI_OS_ADAPTER_VERSION 0x00000004 #define ESP_WIFI_OS_ADAPTER_MAGIC 0xDEADBEAF #define OSI_FUNCS_TIME_BLOCKING 0xffffffff @@ -78,6 +78,8 @@ typedef struct { void (* _dport_access_stall_other_cpu_end_wrap)(void); int32_t (* _phy_rf_deinit)(uint32_t module); void (* _phy_load_cal_and_init)(uint32_t module); + void (* _phy_common_clock_enable)(void); + void (* _phy_common_clock_disable)(void); int32_t (* _read_mac)(uint8_t* mac, uint32_t type); void (* _timer_arm)(void *timer, uint32_t tmout, bool repeat); void (* _timer_disarm)(void *timer); @@ -103,6 +105,7 @@ typedef struct { int32_t (* _get_time)(void *t); unsigned long (* _random)(void); void (* _log_write)(uint32_t level, const char* tag, const char* format, ...); + void (* _log_writev)(uint32_t level, const char* tag, const char* format, va_list args); uint32_t (* _log_timestamp)(void); void * (* _malloc_internal)(size_t size); void * (* _realloc_internal)(void *ptr, size_t size); @@ -121,6 +124,7 @@ typedef struct { void (* _sc_ack_send)(void *param); void (* _sc_ack_send_stop)(void); uint32_t (* _coex_status_get)(void); + void (* _coex_condition_set)(uint32_t type, bool dissatisfy); int32_t (* _coex_wifi_request)(uint32_t event, uint32_t latency, uint32_t duration); int32_t (* _coex_wifi_release)(uint32_t event); int32_t _magic; diff --git a/components/esp32/include/esp_wifi_types.h b/components/esp32/include/esp_wifi_types.h index cfe45ac6c..0ca206565 100644 --- a/components/esp32/include/esp_wifi_types.h +++ b/components/esp32/include/esp_wifi_types.h @@ -18,9 +18,8 @@ #include #include -#include "rom/queue.h" #include "esp_err.h" -#include "esp_interface.h" +#include "esp_private/esp_wifi_types_private.h" #ifdef __cplusplus extern "C" { @@ -49,7 +48,7 @@ typedef struct { char cc[3]; /**< country code string */ uint8_t schan; /**< start channel */ uint8_t nchan; /**< total channel number */ - int8_t max_tx_power; /**< maximum tx power */ + int8_t max_tx_power; /**< This field is used for getting WiFi maximum transmitting power, call esp_wifi_set_max_tx_power to set the maximum transmitting power. */ wifi_country_policy_t policy; /**< country policy */ } wifi_country_t; @@ -94,6 +93,7 @@ typedef enum { WIFI_REASON_ASSOC_FAIL = 203, WIFI_REASON_HANDSHAKE_TIMEOUT = 204, WIFI_REASON_CONNECTION_FAIL = 205, + WIFI_REASON_AUTH_CHANGED = 206, } wifi_err_reason_t; typedef enum { @@ -115,7 +115,7 @@ typedef struct { } wifi_active_scan_time_t; /** @brief Aggregate of active & passive scan time per channel */ -typedef union { +typedef struct { wifi_active_scan_time_t active; /**< active scan time per channel, units: millisecond. */ uint32_t passive; /**< passive scan time per channel, units: millisecond, values above 1500ms may cause station to disconnect from AP and are not recommended. */ @@ -215,7 +215,7 @@ typedef struct { uint8_t channel; /**< Channel of ESP32 soft-AP */ wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */ uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */ - uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 4 */ + uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */ uint16_t beacon_interval; /**< Beacon interval, 100 ~ 60000 ms, default 100 ms */ } wifi_ap_config_t; diff --git a/components/esp32/include/esp_wps.h b/components/esp32/include/esp_wps.h index 9bd61cc3a..cb5e93279 100644 --- a/components/esp32/include/esp_wps.h +++ b/components/esp32/include/esp_wps.h @@ -74,14 +74,21 @@ typedef struct { wps_factory_information_t factory_info; } esp_wps_config_t; +/* C & C++ compilers have different rules about C99-style named initializers */ +#ifdef __cplusplus +#define WPS_AGG(X) { X } +#else +#define WPS_AGG(X) X +#endif + #define WPS_CONFIG_INIT_DEFAULT(type) { \ .wps_type = type, \ .crypto_funcs = &g_wifi_default_wps_crypto_funcs, \ .factory_info = { \ - .manufacturer = "ESPRESSIF", \ - .model_number = "ESP32", \ - .model_name = "ESPRESSIF IOT", \ - .device_name = "ESP STATION", \ + WPS_AGG( .manufacturer = "ESPRESSIF" ), \ + WPS_AGG( .model_number = "ESP32" ), \ + WPS_AGG( .model_name = "ESPRESSIF IOT" ), \ + WPS_AGG( .device_name = "ESP STATION" ), \ } \ } diff --git a/components/esp32/include/hwcrypto/aes.h b/components/esp32/include/hwcrypto/aes.h index 1dd5291f0..5a3fd24fe 100644 --- a/components/esp32/include/hwcrypto/aes.h +++ b/components/esp32/include/hwcrypto/aes.h @@ -41,17 +41,13 @@ extern "C" { /** * \brief AES context structure * - * \note buf is able to hold 32 extra bytes, which can be used: - * - for alignment purposes if VIA padlock is used, and/or - * - to simplify key expansion in the 256-bit case by - * generating an extra round key */ typedef struct { uint8_t key_bytes; + volatile uint8_t key_in_hardware; /* This variable is used for fault injection checks, so marked volatile to avoid optimisation */ uint8_t key[32]; } esp_aes_context; - /** * \brief The AES XTS context-type definition. */ diff --git a/components/esp32/include/rom/spi_flash.h b/components/esp32/include/rom/spi_flash.h index 165aaefe6..a6ed793d6 100644 --- a/components/esp32/include/rom/spi_flash.h +++ b/components/esp32/include/rom/spi_flash.h @@ -118,6 +118,11 @@ extern "C" { #define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) #define ESP_ROM_SPIFLASH_QE BIT9 +//Extra dummy for flash read +#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M 0 +#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M 1 +#define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M 2 + #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { diff --git a/components/esp32/intr_alloc.c b/components/esp32/intr_alloc.c index d4726499f..c4bc73fa0 100644 --- a/components/esp32/intr_alloc.c +++ b/components/esp32/intr_alloc.c @@ -29,9 +29,11 @@ #include "esp_intr.h" #include "esp_attr.h" #include "esp_intr_alloc.h" -#include "esp_ipc.h" #include #include +#if !CONFIG_FREERTOS_UNICORE +#include "esp_ipc.h" +#endif static const char* TAG = "intr_alloc"; @@ -706,20 +708,26 @@ esp_err_t IRAM_ATTR esp_intr_set_in_iram(intr_handle_t handle, bool is_in_iram) return ESP_OK; } +#if !CONFIG_FREERTOS_UNICORE static void esp_intr_free_cb(void *arg) { (void)esp_intr_free((intr_handle_t)arg); } +#endif /* !CONFIG_FREERTOS_UNICORE */ esp_err_t esp_intr_free(intr_handle_t handle) { bool free_shared_vector=false; if (!handle) return ESP_ERR_INVALID_ARG; + +#if !CONFIG_FREERTOS_UNICORE //Assign this routine to the core where this interrupt is allocated on. if (handle->vector_desc->cpu!=xPortGetCoreID()) { esp_err_t ret = esp_ipc_call_blocking(handle->vector_desc->cpu, &esp_intr_free_cb, (void *)handle); return ret == ESP_OK ? ESP_OK : ESP_FAIL; } +#endif /* !CONFIG_FREERTOS_UNICORE */ + portENTER_CRITICAL(&spinlock); esp_intr_disable(handle); if (handle->vector_desc->flags&VECDESC_FL_SHARED) { @@ -743,11 +751,11 @@ esp_err_t esp_intr_free(intr_handle_t handle) } //If nothing left, disable interrupt. if (handle->vector_desc->shared_vec_info==NULL) free_shared_vector=true; - ESP_LOGV(TAG, "esp_intr_free: Deleting shared int: %s. Shared int is %s", svd?"not found or last one":"deleted", free_shared_vector?"empty now.":"still in use"); + ESP_EARLY_LOGV(TAG, "esp_intr_free: Deleting shared int: %s. Shared int is %s", svd?"not found or last one":"deleted", free_shared_vector?"empty now.":"still in use"); } if ((handle->vector_desc->flags&VECDESC_FL_NONSHARED) || free_shared_vector) { - ESP_LOGV(TAG, "esp_intr_free: Disabling int, killing handler"); + ESP_EARLY_LOGV(TAG, "esp_intr_free: Disabling int, killing handler"); #if CONFIG_SYSVIEW_ENABLE if (!free_shared_vector) { void *isr_arg = xt_get_interrupt_handler_arg(handle->vector_desc->intno); diff --git a/components/esp32/ld/esp32.rom.ld b/components/esp32/ld/esp32.rom.ld index c1e3e94bc..d493417f3 100644 --- a/components/esp32/ld/esp32.rom.ld +++ b/components/esp32/ld/esp32.rom.ld @@ -665,6 +665,7 @@ PROVIDE ( lmp_dhkey_chk_handler = 0x4002ab48 ); PROVIDE ( lmp_pause_enc_aes_req_handler = 0x400279a4 ); PROVIDE ( lmp_io_cap_res_handler = 0x4002c670 ); PROVIDE ( lmp_io_cap_req_handler = 0x4002c7a4 ); +PROVIDE ( lc_cmd_cmp_bd_addr_send = 0x4002cec4 ); PROVIDE ( ld_acl_tx_packet_type_select = 0x4002fb40 ); PROVIDE ( ld_acl_sched = 0x40033268 ); PROVIDE ( ld_acl_sniff_sched = 0x4003340c ); @@ -674,6 +675,7 @@ PROVIDE ( ld_acl_rx_sync = 0x4002fbec ); PROVIDE ( ld_acl_rx_sync2 = 0x4002fd8c ); PROVIDE ( ld_acl_rx_no_sync = 0x4002fe78 ); PROVIDE ( ld_acl_clk_isr = 0x40030cf8 ); +PROVIDE ( ld_acl_rsw_frm_cbk = 0x40033bb0 ); PROVIDE ( ld_sco_modify = 0x40031778 ); PROVIDE ( lm_cmd_cmp_send = 0x40051838 ); PROVIDE ( ld_sco_frm_cbk = 0x400349dc ); @@ -725,6 +727,11 @@ PROVIDE ( r_ld_acl_timing_accuracy_set = 0x4003673c ); PROVIDE ( r_ld_acl_t_poll_get = 0x40036024 ); PROVIDE ( r_ld_acl_t_poll_set = 0x40036068 ); PROVIDE ( r_ld_acl_tx_enc = 0x400362f8 ); +PROVIDE ( ld_acl_frm_cbk = 0x40034414 ); +PROVIDE ( ld_acl_rsw_end = 0x40032bc0 ); +PROVIDE ( ld_acl_end = 0x40033140 ); +PROVIDE ( ld_acl_resched = 0x40033814 ); +PROVIDE ( ld_acl_test_mode_update = 0x40032050 ); PROVIDE ( r_ld_acl_unsniff = 0x400361e0 ); PROVIDE ( r_ld_active_check = 0x4003cac4 ); PROVIDE ( r_ld_afh_ch_assess_data_get = 0x4003caec ); @@ -1382,6 +1389,10 @@ PROVIDE ( esp_rom_spiflash_attach = 0x40062a6c ); PROVIDE ( esp_rom_spiflash_config_clk = 0x40062bc8 ); PROVIDE ( g_rom_spiflash_chip = 0x3ffae270 ); +PROVIDE ( hci_le_rd_rem_used_feats_cmd_handler = 0x400417b4 ); +PROVIDE ( llcp_length_req_handler = 0x40043808 ); +PROVIDE ( llcp_unknown_rsp_handler = 0x40043ba8 ); + /* These functions are xtos-related (or call xtos-related functions) and do not play well with multicore FreeRTOS. Where needed, we provide alternatives that are multicore diff --git a/components/esp32/ld/esp32_fragments.lf b/components/esp32/ld/esp32_fragments.lf index 932197caf..c60b2e19d 100644 --- a/components/esp32/ld/esp32_fragments.lf +++ b/components/esp32/ld/esp32_fragments.lf @@ -45,6 +45,10 @@ entries: .dram1+ [sections:wifi_iram] +entries: + .wifirxiram+ + +[sections:wifi_rx_iram] entries: .wifi0iram+ @@ -62,6 +66,7 @@ entries: rtc_rodata -> rtc_data rtc_bss -> rtc_bss wifi_iram -> flash_text + wifi_rx_iram -> flash_text [scheme:rtc] entries: @@ -87,3 +92,7 @@ entries: [scheme:wifi_iram] entries: wifi_iram -> iram0_text + +[scheme:wifi_rx_iram] +entries: + wifi_rx_iram -> iram0_text diff --git a/components/esp32/lib b/components/esp32/lib index de6c4ab21..9f4e045a1 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit de6c4ab21119f0ba309808778142c1deb17be79e +Subproject commit 9f4e045a1d4ce4f4d7ffc708c5ea8f2405ecf6ed diff --git a/components/esp32/linker.lf b/components/esp32/linker.lf index 040e29336..16c3e6193 100644 --- a/components/esp32/linker.lf +++ b/components/esp32/linker.lf @@ -40,3 +40,14 @@ entries: : ESP32_WIFI_IRAM_OPT = y * (wifi_iram) +[mapping] +archive: libpp.a +entries: + : ESP32_WIFI_RX_IRAM_OPT = y + * (wifi_rx_iram) + +[mapping] +archive: libnet80211.a +entries: + : ESP32_WIFI_RX_IRAM_OPT = y + * (wifi_rx_iram) diff --git a/components/esp32/panic.c b/components/esp32/panic.c index 30dcb3888..7d3960cfb 100644 --- a/components/esp32/panic.c +++ b/components/esp32/panic.c @@ -644,7 +644,7 @@ static __attribute__((noreturn)) void commonErrorHandler(XtExcFrame *frame) rtc_wdt_disable(); #if CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT panicPutStr("Rebooting...\r\n"); - if (frame->exccause != PANIC_RSN_CACHEERR) { + if (esp_cache_err_get_cpuid() == -1) { esp_restart_noos(); } else { // The only way to clear invalid cache access interrupt is to reset the digital part diff --git a/components/esp32/phy_init.c b/components/esp32/phy_init.c index 18b3fa8b4..5968fef50 100644 --- a/components/esp32/phy_init.c +++ b/components/esp32/phy_init.c @@ -52,6 +52,12 @@ static uint32_t s_module_phy_rf_init = 0; /* Whether modem sleep is turned on */ static volatile bool s_is_phy_rf_en = false; +/* Whether WiFi/BT common clock enabled reference */ +static volatile int32_t s_common_clock_enable_ref = 0; + +/* PHY spinlock mux */ +static portMUX_TYPE s_phy_spin_lock = portMUX_INITIALIZER_UNLOCKED; + /* Bit mask of modules needing to enter modem sleep mode */ static uint32_t s_modem_sleep_module_enter = 0; @@ -68,14 +74,27 @@ static _lock_t s_modem_sleep_lock; /* time stamp updated when the PHY/RF is turned on */ static int64_t s_phy_rf_en_ts = 0; +static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED; + uint32_t IRAM_ATTR phy_enter_critical(void) { - return portENTER_CRITICAL_NESTED(); + if (xPortInIsrContext()) { + portENTER_CRITICAL_ISR(&s_phy_int_mux); + } else { + portENTER_CRITICAL(&s_phy_int_mux); + } + // Interrupt level will be stored in current tcb, so always return zero. + return 0; } void IRAM_ATTR phy_exit_critical(uint32_t level) { - portEXIT_CRITICAL_NESTED(level); + // Param level don't need any more, ignore it. + if (xPortInIsrContext()) { + portEXIT_CRITICAL_ISR(&s_phy_int_mux); + } else { + portEXIT_CRITICAL(&s_phy_int_mux); + } } int64_t esp_phy_rf_get_on_ts(void) @@ -102,6 +121,56 @@ static inline void phy_update_wifi_mac_time(bool en_clock_stopped, int64_t now) } } +IRAM_ATTR static inline void phy_spin_lock(void) +{ + if (xPortInIsrContext()) { + portENTER_CRITICAL_ISR(&s_phy_spin_lock); + } else { + portENTER_CRITICAL(&s_phy_spin_lock); + } +} + +IRAM_ATTR static inline void phy_spin_unlock(void) +{ + if (xPortInIsrContext()) { + portEXIT_CRITICAL_ISR(&s_phy_spin_lock); + } else { + portEXIT_CRITICAL(&s_phy_spin_lock); + } +} + +IRAM_ATTR void esp_phy_common_clock_enable(void) +{ + phy_spin_lock(); + + if (s_common_clock_enable_ref == 0) { + // Enable WiFi/BT common clock + periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE); + } + + s_common_clock_enable_ref++; + phy_spin_unlock(); +} + +IRAM_ATTR void esp_phy_common_clock_disable(void) +{ + phy_spin_lock(); + + if (s_common_clock_enable_ref > 0) { + s_common_clock_enable_ref --; + + if (s_common_clock_enable_ref == 0) { + // Disable WiFi/BT common clock + periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE); + } + } else { + abort(); + } + + phy_spin_unlock(); +} + + esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibration_mode_t mode, esp_phy_calibration_data_t* calibration_data, phy_rf_module_t module) { @@ -150,7 +219,8 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibrat // Update WiFi MAC time before WiFi/BT common clock is enabled phy_update_wifi_mac_time(false, s_phy_rf_en_ts); // Enable WiFi/BT common peripheral clock - periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE); + //periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE); + esp_phy_common_clock_enable(); phy_set_wifi_mode_only(0); if (ESP_CAL_DATA_CHECK_FAIL == register_chipv7_phy(init_data, calibration_data, mode)) { @@ -171,7 +241,6 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibrat uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE); if ((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) { //both wifi & bt enabled coex_init(); - coex_preference_set(CONFIG_SW_COEXIST_PREFERENCE_VALUE); coex_resume(); } } @@ -235,7 +304,8 @@ esp_err_t esp_phy_rf_deinit(phy_rf_module_t module) // Update WiFi MAC time before disalbe WiFi/BT common peripheral clock phy_update_wifi_mac_time(true, esp_timer_get_time()); // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG - periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE); + //periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE); + esp_phy_common_clock_disable(); } } diff --git a/components/esp32/spiram_psram.c b/components/esp32/spiram_psram.c index a618f948c..abeb95a63 100644 --- a/components/esp32/spiram_psram.c +++ b/components/esp32/spiram_psram.c @@ -96,8 +96,6 @@ typedef enum { // WARNING: PSRAM shares all but the CS and CLK pins with the flash, so these defines // hardcode the flash pins as well, making this code incompatible with either a setup // that has the flash on non-standard pins or ESP32s with built-in flash. -#define FLASH_CLK_IO 6 -#define FLASH_CS_IO 11 #define PSRAM_SPIQ_SD0_IO 7 #define PSRAM_SPID_SD1_IO 8 #define PSRAM_SPIWP_SD3_IO 10 @@ -136,8 +134,8 @@ typedef struct { #define PSRAM_INTERNAL_IO_28 28 #define PSRAM_INTERNAL_IO_29 29 -#define PSRAM_IO_MATRIX_DUMMY_40M 1 -#define PSRAM_IO_MATRIX_DUMMY_80M 2 +#define PSRAM_IO_MATRIX_DUMMY_40M ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M +#define PSRAM_IO_MATRIX_DUMMY_80M ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M #define _SPI_CACHE_PORT 0 #define _SPI_FLASH_PORT 1 @@ -175,7 +173,8 @@ typedef enum { static psram_cache_mode_t s_psram_mode = PSRAM_CACHE_MAX; static psram_clk_mode_t s_clk_mode = PSRAM_CLK_MODE_DCLK; -static uint32_t s_psram_id = 0; +static uint64_t s_psram_id = 0; +static bool s_2t_mode_enabled = false; /* dummy_len_plus values defined in ROM for SPI flash configuration */ extern uint8_t g_rom_spiflash_dummy_len_plus[]; @@ -400,11 +399,12 @@ static void psram_disable_qio_mode(psram_spi_num_t spi_num) } //read psram id -static void psram_read_id(uint32_t* dev_id) +static void psram_read_id(uint64_t* dev_id) { psram_spi_num_t spi_num = PSRAM_SPI_1; psram_disable_qio_mode(spi_num); uint32_t dummy_bits = 0 + extra_dummy; + uint32_t psram_id[2] = {0}; psram_cmd_t ps_cmd; uint32_t addr = 0; @@ -428,14 +428,15 @@ static void psram_read_id(uint32_t* dev_id) ps_cmd.addr = &addr; ps_cmd.txDataBitLen = 0; ps_cmd.txData = NULL; - ps_cmd.rxDataBitLen = 4 * 8; - ps_cmd.rxData = dev_id; + ps_cmd.rxDataBitLen = 8 * 8; + ps_cmd.rxData = psram_id; ps_cmd.dummyBitLen = dummy_bits; psram_cmd_config(spi_num, &ps_cmd); psram_clear_spi_fifo(spi_num); psram_cmd_recv_start(spi_num, ps_cmd.rxData, ps_cmd.rxDataBitLen / 8, PSRAM_CMD_SPI); psram_cmd_end(spi_num); + *dev_id = (uint64_t)(((uint64_t)psram_id[1] << 32) | psram_id[0]); } //enter QPI mode @@ -470,6 +471,182 @@ static esp_err_t IRAM_ATTR psram_enable_qio_mode(psram_spi_num_t spi_num) return ESP_OK; } +#if CONFIG_SPIRAM_2T_MODE +// use SPI user mode to write psram +static void spi_user_psram_write(psram_spi_num_t spi_num, uint32_t address, uint32_t *data_buffer, uint32_t data_len) +{ + uint32_t addr = (PSRAM_QUAD_WRITE << 24) | (address & 0x7fffff); + psram_cmd_t ps_cmd; + ps_cmd.cmdBitLen = 0; + ps_cmd.cmd = 0; + ps_cmd.addr = &addr; + ps_cmd.addrBitLen = 4 * 8; + ps_cmd.txDataBitLen = 32 * 8; + ps_cmd.txData = NULL; + ps_cmd.rxDataBitLen = 0; + ps_cmd.rxData = NULL; + ps_cmd.dummyBitLen = 0; + + for(uint32_t i=0; ipsram_spihd_sd2_io, SPIHD_IN_IDX, 0); //select pin function gpio - if ((psram_io->flash_clk_io == FLASH_CLK_IO) && (psram_io->flash_clk_io != psram_io->psram_clk_io)) { + if ((psram_io->flash_clk_io == SPI_IOMUX_PIN_NUM_CLK) && (psram_io->flash_clk_io != psram_io->psram_clk_io)) { //flash clock signal should come from IO MUX. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[psram_io->flash_clk_io], FUNC_SD_CLK_SPICLK); } else { @@ -602,7 +780,7 @@ static void IRAM_ATTR psram_gpio_config(psram_io_t *psram_io, psram_cache_mode_t psram_size_t psram_get_size() { if ((PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_64MBITS) || PSRAM_IS_64MBIT_TRIAL(s_psram_id)) { - return PSRAM_SIZE_64MBITS; + return s_2t_mode_enabled ? PSRAM_SIZE_32MBITS : PSRAM_SIZE_64MBITS; } else if (PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_32MBITS) { return PSRAM_SIZE_32MBITS; } else if (PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_16MBITS) { @@ -612,6 +790,12 @@ psram_size_t psram_get_size() } } +//used in UT only +bool psram_is_32mbit_ver0(void) +{ + return PSRAM_IS_32MBIT_VER0(s_psram_id); +} + /* * Psram mode init will overwrite original flash speed mode, so that it is possible to change psram and flash speed after OTA. * Flash read mode(QIO/QOUT/DIO/DOUT) will not be changed in app bin. It is decided by bootloader, OTA can not change this mode. @@ -651,8 +835,8 @@ esp_err_t IRAM_ATTR psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vad const uint32_t spiconfig = ets_efuse_get_spiconfig(); if (spiconfig == EFUSE_SPICONFIG_SPI_DEFAULTS) { - psram_io.flash_clk_io = FLASH_CLK_IO; - psram_io.flash_cs_io = FLASH_CS_IO; + psram_io.flash_clk_io = SPI_IOMUX_PIN_NUM_CLK; + psram_io.flash_cs_io = SPI_IOMUX_PIN_NUM_CS; psram_io.psram_spiq_sd0_io = PSRAM_SPIQ_SD0_IO; psram_io.psram_spid_sd1_io = PSRAM_SPID_SD1_IO; psram_io.psram_spiwp_sd3_io = PSRAM_SPIWP_SD3_IO; @@ -722,9 +906,13 @@ esp_err_t IRAM_ATTR psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vad return ESP_FAIL; } - if (PSRAM_IS_32MBIT_VER0(s_psram_id)) { + if (psram_is_32mbit_ver0()) { s_clk_mode = PSRAM_CLK_MODE_DCLK; if (mode == PSRAM_CACHE_F80M_S80M) { +#ifdef CONFIG_SPIRAM_OCCUPY_NO_HOST + ESP_EARLY_LOGE(TAG, "This version of PSRAM needs to claim an extra SPI peripheral at 80MHz. Please either: choose lower frequency by SPIRAM_SPEED_, or select one SPI peripheral it by SPIRAM_OCCUPY_*SPI_HOST in the menuconfig."); + abort(); +#else /* note: If the third mode(80Mhz+80Mhz) is enabled for 32MBit 1V8 psram, one of HSPI/VSPI port will be occupied by the system (according to kconfig). Application code should never touch HSPI/VSPI hardware in this case. We try to stop applications @@ -748,6 +936,7 @@ esp_err_t IRAM_ATTR psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vad break; } } +#endif } } else { // For other psram, we don't need any extra clock cycles after cs get back to high level @@ -761,6 +950,27 @@ esp_err_t IRAM_ATTR psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vad psram_set_cs_timing(PSRAM_SPI_1, s_clk_mode); psram_set_cs_timing(_SPI_CACHE_PORT, s_clk_mode); psram_enable_qio_mode(PSRAM_SPI_1); + + if(((PSRAM_SIZE_ID(s_psram_id) == PSRAM_EID_SIZE_64MBITS) || PSRAM_IS_64MBIT_TRIAL(s_psram_id))) { +#if CONFIG_SPIRAM_2T_MODE +#if CONFIG_SPIRAM_BANKSWITCH_ENABLE + ESP_EARLY_LOGE(TAG, "PSRAM 2T mode and SPIRAM bank switching can not enabled meanwhile. Please read the help text for SPIRAM_2T_MODE in the project configuration menu."); + abort(); +#endif + /* Note: 2T mode command should not be sent twice, + otherwise psram would get back to normal mode. */ + if (psram_2t_mode_check(PSRAM_SPI_1) != ESP_OK) { + psram_2t_mode_enable(PSRAM_SPI_1); + if (psram_2t_mode_check(PSRAM_SPI_1) != ESP_OK) { + ESP_EARLY_LOGE(TAG, "PSRAM 2T mode enable fail!"); + return ESP_FAIL; + } + } + s_2t_mode_enabled = true; + ESP_EARLY_LOGI(TAG, "PSRAM is in 2T mode"); +#endif + } + psram_cache_init(mode, vaddrmode); return ESP_OK; } diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index acd2b3eb0..a673e4515 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -206,7 +206,7 @@ esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type) ESP_LOGW(TAG, "incorrect mac type"); break; } - + return ESP_OK; } @@ -297,10 +297,10 @@ void IRAM_ATTR esp_restart_noos() WRITE_PERI_REG(GPIO_FUNC5_IN_SEL_CFG_REG, 0x30); // Reset wifi/bluetooth/ethernet/sdio (bb/mac) - DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, + DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST | DPORT_BT_RST | DPORT_BTMAC_RST | DPORT_SDIO_RST | - DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST | + DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST | DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST); DPORT_REG_WRITE(DPORT_CORE_RST_EN_REG, 0); @@ -356,35 +356,27 @@ const char* esp_get_idf_version(void) return IDF_VER; } -static void get_chip_info_esp32(esp_chip_info_t* out_info) +void esp_chip_info(esp_chip_info_t* out_info) { - uint32_t reg = REG_READ(EFUSE_BLK0_RDATA3_REG); + uint32_t efuse_rd3 = REG_READ(EFUSE_BLK0_RDATA3_REG); memset(out_info, 0, sizeof(*out_info)); - + out_info->model = CHIP_ESP32; - if ((reg & EFUSE_RD_CHIP_VER_REV1_M) != 0) { - out_info->revision = 1; - } - if ((reg & EFUSE_RD_CHIP_VER_DIS_APP_CPU_M) == 0) { + out_info->revision = esp_efuse_get_chip_ver(); + + if ((efuse_rd3 & EFUSE_RD_CHIP_VER_DIS_APP_CPU_M) == 0) { out_info->cores = 2; } else { out_info->cores = 1; } out_info->features = CHIP_FEATURE_WIFI_BGN; - if ((reg & EFUSE_RD_CHIP_VER_DIS_BT_M) == 0) { + if ((efuse_rd3 & EFUSE_RD_CHIP_VER_DIS_BT_M) == 0) { out_info->features |= CHIP_FEATURE_BT | CHIP_FEATURE_BLE; } - int package = (reg & EFUSE_RD_CHIP_VER_PKG_M) >> EFUSE_RD_CHIP_VER_PKG_S; + int package = (efuse_rd3 & EFUSE_RD_CHIP_VER_PKG_M) >> EFUSE_RD_CHIP_VER_PKG_S; if (package == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 || package == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2 || package == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) { out_info->features |= CHIP_FEATURE_EMB_FLASH; } } - -void esp_chip_info(esp_chip_info_t* out_info) -{ - // Only ESP32 is supported now, in the future call one of the - // chip-specific functions based on sdkconfig choice - return get_chip_info_esp32(out_info); -} diff --git a/components/esp32/test/CMakeLists.txt b/components/esp32/test/CMakeLists.txt index b99e2888b..7c2c0df64 100644 --- a/components/esp32/test/CMakeLists.txt +++ b/components/esp32/test/CMakeLists.txt @@ -28,10 +28,25 @@ execute_process(COMMAND md5sum ${IDF_PATH}/components/esp32/include/esp_coexist_ OUTPUT_VARIABLE COEX_ADAPTER_MD5 OUTPUT_STRIP_TRAILING_WHITESPACE) +# Calculate MD5 value of header file esp_wifi_types.h +execute_process(COMMAND md5sum ${IDF_PATH}/components/esp32/include/esp_wifi_types.h + COMMAND cut -c 1-7 + OUTPUT_VARIABLE WIFI_TYPE_MD5 + OUTPUT_STRIP_TRAILING_WHITESPACE) + +# Calculate MD5 value of header file esp_wifi.h +execute_process(COMMAND md5sum ${IDF_PATH}/components/esp32/include/esp_wifi.h + COMMAND cut -c 1-7 + OUTPUT_VARIABLE WIFI_ESP_WIFI_MD5 + OUTPUT_STRIP_TRAILING_WHITESPACE) + add_definitions(-DWIFI_OS_ADAPTER_MD5=\"${WIFI_OS_ADAPTER_MD5}\") add_definitions(-DWIFI_CRYPTO_MD5=\"${WIFI_CRYPTO_MD5}\") add_definitions(-DCOEX_ADAPTER_MD5=\"${COEX_ADAPTER_MD5}\") +add_definitions(-DWIFI_TYPE_MD5=\"${WIFI_TYPE_MD5}\") +add_definitions(-DWIFI_ESP_WIFI_MD5=\"${WIFI_ESP_WIFI_MD5}\") add_custom_target(esp32_test_logo DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/test_tjpgd_logo.h") -add_dependencies(${COMPONENT_TARGET} esp32_test_logo) \ No newline at end of file +add_dependencies(${COMPONENT_TARGET} esp32_test_logo) +target_link_libraries(${COMPONENT_TARGET} "-u ld_include_test_dport_xt_highint5") diff --git a/components/esp32/test/component.mk b/components/esp32/test/component.mk index 6fb41f30a..411ac5710 100644 --- a/components/esp32/test/component.mk +++ b/components/esp32/test/component.mk @@ -4,7 +4,8 @@ COMPONENT_EXTRA_CLEAN := test_tjpgd_logo.h -COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive \ + -u ld_include_test_dport_xt_highint5 \ COMPONENT_SRCDIRS := . @@ -20,6 +21,16 @@ CFLAGS+=-DWIFI_CRYPTO_MD5=$(WIFI_CRYPTO_MD5_VAL) COEX_ADAPTER_MD5_VAL=\"$(shell md5sum $(IDF_PATH)/components/esp32/include/esp_coexist_adapter.h | cut -c 1-7)\" CFLAGS+=-DCOEX_ADAPTER_MD5=$(COEX_ADAPTER_MD5_VAL) +# Calculate MD5 value of header file esp_wifi_types.h +WIFI_TYPE_MD5_VAL=\"$(shell md5sum $(IDF_PATH)/components/esp32/include/esp_wifi_types.h | cut -c 1-7)\" +CFLAGS+=-DWIFI_TYPE_MD5=$(WIFI_TYPE_MD5_VAL) + + +# Calculate MD5 value of header file esp_wifi.h +WIFI_ESP_WIFI_MD5_VAL=\"$(shell md5sum $(IDF_PATH)/components/esp32/include/esp_wifi.h | cut -c 1-7)\" +CFLAGS+=-DWIFI_ESP_WIFI_MD5=$(WIFI_ESP_WIFI_MD5_VAL) + + test_tjpgd.o: test_tjpgd_logo.h test_tjpgd_logo.h: $(COMPONENT_PATH)/logo.jpg diff --git a/components/esp32/test/test_4mpsram.c b/components/esp32/test/test_4mpsram.c index a1cd36023..05401ee5b 100644 --- a/components/esp32/test/test_4mpsram.c +++ b/components/esp32/test/test_4mpsram.c @@ -36,37 +36,60 @@ static void test_psram_content() } #endif -// NOTE: this unit test rely on the config that PSRAM of 8MB is used only when CONFIG_SPIRAM_BNKSWITCH_ENABLE is set -TEST_CASE("can use spi when not being used by psram", "[psram_4m]") -{ - spi_host_device_t host; -#if !CONFIG_SPIRAM_SUPPORT || !CONFIG_SPIRAM_SPEED_80M || CONFIG_SPIRAM_BANKSWITCH_ENABLE - //currently all 8M psram don't need more SPI peripherals - host = -1; -#elif CONFIG_SPIRAM_OCCUPY_HSPI_HOST - host = HSPI_HOST; -#elif CONFIG_SPIRAM_OCCUPY_VSPI_HOST - host = VSPI_HOST; -#endif +bool psram_is_32mbit_ver0(void); +static void test_spi_bus_occupy(spi_host_device_t expected_occupied_host) +{ bool claim_hspi = spicommon_periph_claim(HSPI_HOST, "ut-hspi"); if (claim_hspi) ESP_LOGI(TAG, "HSPI claimed."); bool claim_vspi = spicommon_periph_claim(VSPI_HOST, "ut-vspi"); if (claim_vspi) ESP_LOGI(TAG, "VSPI claimed."); - if (host == HSPI_HOST) { - TEST_ASSERT(claim_hspi==false); - TEST_ASSERT(claim_vspi==true); - } else if (host == VSPI_HOST) { - TEST_ASSERT(claim_vspi==false); - TEST_ASSERT(claim_hspi==true); + if (expected_occupied_host == HSPI_HOST) { + TEST_ASSERT_FALSE(claim_hspi); + TEST_ASSERT(claim_vspi); + } else if (expected_occupied_host == VSPI_HOST) { + TEST_ASSERT_FALSE(claim_vspi); + TEST_ASSERT(claim_hspi); } else { - TEST_ASSERT(claim_hspi==true); - TEST_ASSERT(claim_vspi==true); + TEST_ASSERT(claim_hspi); + TEST_ASSERT(claim_vspi); } #ifdef CONFIG_SPIRAM_SUPPORT test_psram_content(); #endif } + +#if CONFIG_SPIRAM_OCCUPY_HSPI_HOST || CONFIG_SPIRAM_OCCUPY_VSPI_HOST +TEST_CASE("some spi bus occpied by psram", "[psram_4m][test_env=UT_T1_PSRAMV0]") +{ +// NOTE: this unit test rely on the config that PSRAM of 8MB is used only when CONFIG_SPIRAM_BNKSWITCH_ENABLE is set +//currently all 8M psram don't need more SPI peripherals +#if !CONFIG_SPIRAM_SUPPORT || !CONFIG_SPIRAM_SPEED_80M || CONFIG_SPIRAM_BANKSWITCH_ENABLE +#error unexpected test config, only psram 32MBit ver 0 at 80MHz will trigger the workaround +#endif + + spi_host_device_t host; + if (!psram_is_32mbit_ver0()) { + TEST_FAIL_MESSAGE("unexpected psram version"); + } + +#if CONFIG_SPIRAM_OCCUPY_HSPI_HOST + host = HSPI_HOST; +#elif CONFIG_SPIRAM_OCCUPY_VSPI_HOST + host = VSPI_HOST; +#endif + test_spi_bus_occupy(host); +} +#else +TEST_CASE("can use spi when not being used by psram", "[psram_4m]") +{ +#if CONFIG_SPIRAM_SUPPORT && CONFIG_SPIRAM_SPEED_80M +#error unexpected test config, some runners using the UT_T1_PSRAMV0 but still perform this test.\ +they will not pass this test at 80MHz. +#endif + test_spi_bus_occupy(-1); +} +#endif diff --git a/components/esp32/test/test_dport.c b/components/esp32/test/test_dport.c index 4520a70aa..165e64a0b 100644 --- a/components/esp32/test/test_dport.c +++ b/components/esp32/test/test_dport.c @@ -1,5 +1,7 @@ #include #include +#include "xtensa/core-macros.h" +#include "xtensa/hal.h" #include "esp_types.h" #include "esp_clk.h" @@ -14,11 +16,14 @@ #include "soc/uart_reg.h" #include "soc/dport_reg.h" #include "soc/rtc.h" +#include "esp_intr_alloc.h" +#include "driver/timer.h" #define MHZ (1000000) static volatile bool exit_flag; static bool dport_test_result; static bool apb_test_result; +uint32_t volatile apb_intr_test_result; static void accessDPORT(void *pvParameters) { @@ -60,6 +65,7 @@ static void accessAPB(void *pvParameters) void run_tasks(const char *task1_description, void (* task1_func)(void *), const char *task2_description, void (* task2_func)(void *), uint32_t delay_ms) { + apb_intr_test_result = 1; int i; TaskHandle_t th[2]; xSemaphoreHandle exit_sema[2]; @@ -95,7 +101,7 @@ void run_tasks(const char *task1_description, void (* task1_func)(void *), const vSemaphoreDelete(exit_sema[i]); } } - TEST_ASSERT(dport_test_result == true && apb_test_result == true); + TEST_ASSERT(dport_test_result == true && apb_test_result == true && apb_intr_test_result == 1); } TEST_CASE("access DPORT and APB at same time", "[esp32]") @@ -325,3 +331,167 @@ TEST_CASE("BENCHMARK for DPORT access performance", "[freertos]") } BENCHMARK_END("_DPORT_REG_READ"); } + +uint32_t xt_highint5_read_apb; + +#ifndef CONFIG_FREERTOS_UNICORE +timer_isr_handle_t inth; +xSemaphoreHandle sync_sema; + +static void init_hi_interrupt(void *arg) +{ + printf("init hi_interrupt on CPU%d \n", xPortGetCoreID()); + TEST_ESP_OK(esp_intr_alloc(ETS_INTERNAL_TIMER2_INTR_SOURCE, ESP_INTR_FLAG_LEVEL5 | ESP_INTR_FLAG_IRAM, NULL, NULL, &inth)); + while (exit_flag == false); + esp_intr_free(inth); + printf("disable hi_interrupt on CPU%d \n", xPortGetCoreID()); + vTaskDelete(NULL); +} + +static void accessDPORT2_stall_other_cpu(void *pvParameters) +{ + xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + dport_test_result = true; + while (exit_flag == false) { + DPORT_STALL_OTHER_CPU_START(); + XTHAL_SET_CCOMPARE(2, XTHAL_GET_CCOUNT()); + xt_highint5_read_apb = 1; + for (int i = 0; i < 200; ++i) { + if (_DPORT_REG_READ(DPORT_DATE_REG) != _DPORT_REG_READ(DPORT_DATE_REG)) { + apb_test_result = false; + break; + } + } + xt_highint5_read_apb = 0; + DPORT_STALL_OTHER_CPU_END(); + } + printf("accessDPORT2_stall_other_cpu finish\n"); + + xSemaphoreGive(*sema); + vTaskDelete(NULL); +} + +TEST_CASE("Check stall workaround DPORT and Hi-interrupt", "[esp32]") +{ + xt_highint5_read_apb = 0; + dport_test_result = false; + apb_test_result = true; + TEST_ASSERT(xTaskCreatePinnedToCore(&init_hi_interrupt, "init_hi_intr", 2048, NULL, 6, NULL, 1) == pdTRUE); + // Access DPORT(stall other cpu method) - CPU0 + // STALL - CPU1 + // Hi-interrupt - CPU1 + run_tasks("accessDPORT2_stall_other_cpu", accessDPORT2_stall_other_cpu, " - ", NULL, 10000); +} + +static void accessDPORT2(void *pvParameters) +{ + xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + dport_test_result = true; + + TEST_ESP_OK(esp_intr_alloc(ETS_INTERNAL_TIMER2_INTR_SOURCE, ESP_INTR_FLAG_LEVEL5 | ESP_INTR_FLAG_IRAM, NULL, NULL, &inth)); + + XTHAL_SET_CCOMPARE(2, XTHAL_GET_CCOUNT() + 21); + int sync = 0; + while (exit_flag == false) { + ets_delay_us(++sync % 10); + for (int i = 0; i < 200; ++i) { + if (DPORT_REG_READ(DPORT_DATE_REG) != DPORT_REG_READ(DPORT_DATE_REG)) { + dport_test_result = false; + break; + } + } + } + esp_intr_free(inth); + printf("accessDPORT2 finish\n"); + + xSemaphoreGive(*sema); + vTaskDelete(NULL); +} + +TEST_CASE("Check pre-read workaround DPORT and Hi-interrupt", "[esp32]") +{ + xt_highint5_read_apb = 0; + dport_test_result = false; + apb_test_result = true; + // Access DPORT(pre-read method) - CPU1 + // Hi-interrupt - CPU1 + run_tasks("accessAPB", accessAPB, "accessDPORT2", accessDPORT2, 10000); +} + +static uint32_t s_shift_counter; + +/* +The test_dport_access_reg_read() is similar DPORT_REG_READ() but has differents: +- generate an interrupt by SET_CCOMPARE +- additional branch command helps get good reproducing an issue with breaking the DPORT pre-read workaround +- uncomment (1) and comment (2) it allows seeing the broken pre-read workaround +For pre-reading the workaround, it is important that the two reading commands APB and DPORT +are executed without interruption. For this reason, it disables interrupts and to do reading inside the safe area. +But despite a disabling interrupt it was still possible that these two readings can be interrupted. +The reason is linked with work parallel execution commands in the pipeline (it is not a bug). +To resolve this issue (1) was moved to (2) position into the disabled interrupt part. +When the read command is interrupted after stage E(execute), the result of its execution will be saved in the internal buffer, +and after returning from the interrupt, this command takes this value from the buffer without repeating the reading, +which is critical for the DPORT pre-read workaround. To fix it we added additional command under safe area ((1)->(2)). +*/ +static uint32_t IRAM_ATTR test_dport_access_reg_read(uint32_t reg) +{ +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) + return _DPORT_REG_READ(reg); +#else + uint32_t apb; + unsigned int intLvl; + XTHAL_SET_CCOMPARE(2, XTHAL_GET_CCOUNT() + s_shift_counter); + __asm__ __volatile__ (\ + /* "movi %[APB], "XTSTR(0x3ff40078)"\n" */ /* (1) uncomment for reproduce issue */ \ + "bnez %[APB], kl1\n" /* this branch command helps get good reproducing */ \ + "kl1:\n"\ + "rsil %[LVL], "XTSTR(CONFIG_ESP32_DPORT_DIS_INTERRUPT_LVL)"\n"\ + "movi %[APB], "XTSTR(0x3ff40078)"\n" /* (2) comment for reproduce issue */ \ + "l32i %[APB], %[APB], 0\n"\ + "l32i %[REG], %[REG], 0\n"\ + "wsr %[LVL], "XTSTR(PS)"\n"\ + "rsync\n"\ + : [APB]"=a"(apb), [REG]"+a"(reg), [LVL]"=a"(intLvl)\ + : \ + : "memory" \ + ); + return reg; +#endif +} + +// The accessDPORT3 task is similar accessDPORT2 but uses test_dport_access_reg_read() instead of usual DPORT_REG_READ(). +static void accessDPORT3(void *pvParameters) +{ + xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + dport_test_result = true; + + TEST_ESP_OK(esp_intr_alloc(ETS_INTERNAL_TIMER2_INTR_SOURCE, ESP_INTR_FLAG_LEVEL5 | ESP_INTR_FLAG_IRAM, NULL, NULL, &inth)); + int i = 0; + while (exit_flag == false) { + if (test_dport_access_reg_read(DPORT_DATE_REG) != test_dport_access_reg_read(DPORT_DATE_REG)) { + dport_test_result = false; + break; + } + if ((++i % 100) == 0) { + s_shift_counter = (s_shift_counter + 1) % 30; + } + } + esp_intr_free(inth); + printf("accessDPORT3 finish\n"); + + xSemaphoreGive(*sema); + vTaskDelete(NULL); +} + +TEST_CASE("Check pre-read workaround DPORT and Hi-interrupt (2)", "[esp32]") +{ + s_shift_counter = 1; + xt_highint5_read_apb = 0; + dport_test_result = false; + apb_test_result = true; + // Access DPORT(pre-read method) - CPU1 + // Hi-interrupt - CPU1 + run_tasks("accessAPB", accessAPB, "accessDPORT3", accessDPORT3, 10000); +} +#endif // CONFIG_FREERTOS_UNICORE diff --git a/components/esp32/test/test_dport_xt_highint5.S b/components/esp32/test/test_dport_xt_highint5.S new file mode 100644 index 000000000..19828bf91 --- /dev/null +++ b/components/esp32/test/test_dport_xt_highint5.S @@ -0,0 +1,78 @@ +#include +#include +#include +#include "freertos/xtensa_context.h" + +#include "sdkconfig.h" +#include "soc/soc.h" +#include "soc/dport_reg.h" + +#ifndef CONFIG_FREERTOS_UNICORE + +#define L5_INTR_STACK_SIZE 12 +#define L5_INTR_A2_OFFSET 0 +#define L5_INTR_A3_OFFSET 4 +#define L5_INTR_A4_OFFSET 8 + .data +_l5_intr_stack: + .space L5_INTR_STACK_SIZE + + .section .iram1,"ax" + .global xt_highint5 + .type xt_highint5,@function + .align 4 +xt_highint5: + + movi a0, xt_highint5_read_apb + l32i a0, a0, 0 + bnez a0, .read_apb_reg + +// Short interrupt + movi a0, 0 + wsr a0, CCOMPARE2 + esync + + rsr a0, EXCSAVE_5 // restore a0 + rfi 5 + + + +// read APB reg 10 time. +.read_apb_reg: + movi a0, _l5_intr_stack + s32i a2, a0, L5_INTR_A2_OFFSET + s32i a3, a0, L5_INTR_A3_OFFSET + s32i a4, a0, L5_INTR_A4_OFFSET + + movi a4, 10 // count of reading + movi a0, 0x3ff40078 // read APB reg + l32i a2, a0, 0 +.loop_read_apb_reg: + l32i a3, a0, 0 + bne a3, a2, .need_set_apb_test_result + addi a4, a4, -1 + l32i a2, a0, 0 + bnez a4, .loop_read_apb_reg + j 1f +.need_set_apb_test_result: + movi a0, apb_intr_test_result // set fail + movi a2, 0 + s32i a2, a0, 0 + memw +1: + movi a0, _l5_intr_stack + l32i a2, a0, L5_INTR_A2_OFFSET + l32i a3, a0, L5_INTR_A3_OFFSET + l32i a4, a0, L5_INTR_A4_OFFSET + rsync +.L_xt_highint5_exit: + rsr a0, EXCSAVE_5 // restore a0 + rfi 5 + +/* The linker has no reason to link in this file; all symbols it exports are already defined + (weakly!) in the default int handler. Define a symbol here so we can use it to have the + linker inspect this anyway. */ + + .global ld_include_test_dport_xt_highint5 +ld_include_test_dport_xt_highint5: +#endif // CONFIG_FREERTOS_UNICORE diff --git a/components/esp32/test/test_esp_timer.c b/components/esp32/test/test_esp_timer.c index 510bed461..6d386a442 100644 --- a/components/esp32/test/test_esp_timer.c +++ b/components/esp32/test/test_esp_timer.c @@ -4,6 +4,7 @@ #include #include #include "unity.h" +#include "soc/frc_timer_reg.h" #include "esp_timer.h" #include "esp_heap_caps.h" #include "freertos/FreeRTOS.h" @@ -536,6 +537,61 @@ TEST_CASE("Can delete timer from callback", "[esp_timer]") vSemaphoreDelete(args.notify_from_timer_cb); } + +typedef struct { + SemaphoreHandle_t delete_start; + SemaphoreHandle_t delete_done; + SemaphoreHandle_t test_done; + esp_timer_handle_t timer; +} timer_delete_test_args_t; + +static void timer_delete_task(void* arg) +{ + timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg; + xSemaphoreTake(args->delete_start, portMAX_DELAY); + printf("Deleting the timer\n"); + esp_timer_delete(args->timer); + printf("Timer deleted\n"); + xSemaphoreGive(args->delete_done); + vTaskDelete(NULL); +} + +static void timer_delete_test_callback(void* arg) +{ + timer_delete_test_args_t* args = (timer_delete_test_args_t*) arg; + printf("Timer callback called\n"); + xSemaphoreGive(args->delete_start); + xSemaphoreTake(args->delete_done, portMAX_DELAY); + printf("Callback complete\n"); + xSemaphoreGive(args->test_done); +} + +TEST_CASE("Can delete timer from a separate task, triggered from callback", "[esp_timer]") +{ + timer_delete_test_args_t args = { + .delete_start = xSemaphoreCreateBinary(), + .delete_done = xSemaphoreCreateBinary(), + .test_done = xSemaphoreCreateBinary(), + }; + + esp_timer_create_args_t timer_args = { + .callback = &timer_delete_test_callback, + .arg = &args + }; + esp_timer_handle_t timer; + TEST_ESP_OK(esp_timer_create(&timer_args, &timer)); + args.timer = timer; + + xTaskCreate(timer_delete_task, "deleter", 4096, &args, 5, NULL); + + esp_timer_start_once(timer, 100); + TEST_ASSERT(xSemaphoreTake(args.test_done, pdMS_TO_TICKS(1000))); + + vSemaphoreDelete(args.delete_done); + vSemaphoreDelete(args.delete_start); + vSemaphoreDelete(args.test_done); +} + TEST_CASE("esp_timer_impl_advance moves time base correctly", "[esp_timer]") { ref_clock_init(); @@ -594,3 +650,161 @@ TEST_CASE("after esp_timer_impl_advance, timers run when expected", "[esp_timer] ref_clock_deinit(); } + +#if !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND) + +#include "soc/dport_reg.h" +#include "soc/frc_timer_reg.h" +#include "esp_ipc.h" +static bool task_stop; +static bool time_jumped; + +static void task_check_time(void *p) +{ + int64_t t1 = 0, t2 = 0; + while (task_stop == false) { + + t1 = t2; + t2 = esp_timer_get_time(); + if (t1 > t2) { + int64_t shift_us = t2 - t1; + time_jumped = true; + printf("System clock jumps back: %lli us\n", shift_us); + } + + vTaskDelay(1); + } + vTaskDelete(NULL); +} + +static void timer_callback(void* arg) +{ + +} + +static void dport_task(void *pvParameters) +{ + while (task_stop == false) { + DPORT_STALL_OTHER_CPU_START(); + ets_delay_us(3); + DPORT_STALL_OTHER_CPU_END(); + } + vTaskDelete(NULL); +} + +TEST_CASE("esp_timer_impl_set_alarm does not set an alarm below the current time", "[esp_timer][timeout=62]") +{ + const int max_timers = 2; + time_jumped = false; + task_stop = false; + + xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0); + // dport_task is used here to interrupt the esp_timer_impl_set_alarm function. + // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0. + // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0. + xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1); + + const esp_timer_create_args_t periodic_timer_args = { + .callback = &timer_callback, + }; + + esp_timer_handle_t periodic_timer[max_timers]; + printf("timers created\n"); + + esp_timer_create(&periodic_timer_args, &periodic_timer[0]); + esp_timer_start_periodic(periodic_timer[0], 9000); + + esp_timer_create(&periodic_timer_args, &periodic_timer[1]); + esp_timer_start_periodic(periodic_timer[1], 9000); + + vTaskDelay(60 * 1000 / portTICK_PERIOD_MS); + task_stop = true; + + esp_timer_stop(periodic_timer[0]); + esp_timer_delete(periodic_timer[0]); + esp_timer_stop(periodic_timer[1]); + esp_timer_delete(periodic_timer[1]); + printf("timers deleted\n"); + + vTaskDelay(1000 / portTICK_PERIOD_MS); + + TEST_ASSERT(time_jumped == false); +} + + +static esp_timer_handle_t oneshot_timer; + +static void oneshot_timer_callback(void* arg) +{ + esp_timer_start_once(oneshot_timer, 5000); +} + +static const esp_timer_create_args_t oneshot_timer_args = { + .callback = &oneshot_timer_callback, +}; + +TEST_CASE("esp_timer_impl_set_alarm and using start_once do not lead that the System time jumps back", "[esp_timer][timeout=62]") +{ + time_jumped = false; + task_stop = false; + + xTaskCreatePinnedToCore(task_check_time, "task_check_time", 4096, NULL, 5, NULL, 0); + // dport_task is used here to interrupt the esp_timer_impl_set_alarm function. + // To interrupt it we can use an interrupt with 4 or 5 levels which will run on CPU0. + // Instead, an interrupt we use the dport workaround which has 4 interrupt level for stall CPU0. + xTaskCreatePinnedToCore(dport_task, "dport_task", 4096, NULL, 5, NULL, 1); + + const esp_timer_create_args_t periodic_timer_args = { + .callback = &timer_callback, + }; + + esp_timer_handle_t periodic_timer; + + esp_timer_create(&periodic_timer_args, &periodic_timer); + esp_timer_start_periodic(periodic_timer, 5000); + + esp_timer_create(&oneshot_timer_args, &oneshot_timer); + esp_timer_start_once(oneshot_timer, 9990); + printf("timers created\n"); + + vTaskDelay(60 * 1000 / portTICK_PERIOD_MS); + task_stop = true; + + esp_timer_stop(oneshot_timer); + esp_timer_delete(oneshot_timer); + printf("timers deleted\n"); + + vTaskDelay(1000 / portTICK_PERIOD_MS); + + TEST_ASSERT(time_jumped == false); +} + +#endif // !defined(CONFIG_FREERTOS_UNICORE) && defined(CONFIG_ESP32_DPORT_WORKAROUND) + +TEST_CASE("Test case when esp_timer_impl_set_alarm needs set timer < now_time", "[esp_timer]") +{ + REG_WRITE(FRC_TIMER_LOAD_REG(1), 0); + esp_timer_impl_advance(50331648); // 0xefffffff/80 = 50331647 + + ets_delay_us(2); + + portDISABLE_INTERRUPTS(); + esp_timer_impl_set_alarm(50331647); + uint32_t alarm_reg = REG_READ(FRC_TIMER_ALARM_REG(1)); + uint32_t count_reg = REG_READ(FRC_TIMER_COUNT_REG(1)); + portENABLE_INTERRUPTS(); + + const uint32_t offset = 80 * 2; // s_timer_ticks_per_us + printf("alarm_reg = 0x%x, count_reg 0x%x\n", alarm_reg, count_reg); + TEST_ASSERT(alarm_reg <= (count_reg + offset)); +} + +TEST_CASE("Test esp_timer_impl_set_alarm when the counter is near an overflow value", "[esp_timer]") +{ + for (int i = 0; i < 1024; ++i) { + uint32_t count_reg = 0xeffffe00 + i; + REG_WRITE(FRC_TIMER_LOAD_REG(1), count_reg); + printf("%d) count_reg = 0x%x\n", i, count_reg); + esp_timer_impl_set_alarm(1); // timestamp is expired + } +} diff --git a/components/esp32/test/test_header_files_md5.c b/components/esp32/test/test_header_files_md5.c index c2d80350a..a958e049c 100644 --- a/components/esp32/test/test_header_files_md5.c +++ b/components/esp32/test/test_header_files_md5.c @@ -12,7 +12,7 @@ TEST_CASE("wifi os adapter MD5","[wifi]") { const char *test_wifi_os_funcs_md5 = WIFI_OS_ADAPTER_MD5; - ESP_LOGI(TAG, "test wifi os adapter MD5..."); + ESP_LOGI(TAG, "test eps_wifi_os_adapter.h MD5..."); TEST_ESP_OK(esp_wifi_internal_osi_funcs_md5_check(test_wifi_os_funcs_md5)); ESP_LOGI(TAG, "test passed..."); @@ -22,13 +22,13 @@ TEST_CASE("wifi crypto types MD5","[wifi]") { const char *test_wifi_crypto_funcs_md5 = WIFI_CRYPTO_MD5; - ESP_LOGI(TAG, "test wifi crypto adapter MD5..."); + ESP_LOGI(TAG, "test esp_wifi_crypto_adapter.h MD5..."); TEST_ESP_OK(esp_wifi_internal_crypto_funcs_md5_check(test_wifi_crypto_funcs_md5)); ESP_LOGI(TAG, "test passed..."); } -TEST_CASE("coexist adapter MD5","[coex]") +TEST_CASE("coexist esp_coexist_adapter.h MD5","[coex]") { const char *test_coex_adapter_funcs_md5 = COEX_ADAPTER_MD5; @@ -37,3 +37,23 @@ TEST_CASE("coexist adapter MD5","[coex]") ESP_LOGI(TAG, "test passed..."); } + +TEST_CASE("wifi type MD5","[wifi]") +{ + const char *test_wifi_type_md5 = WIFI_TYPE_MD5; + + ESP_LOGI(TAG, "test esp_wifi_types.h MD5..."); + TEST_ESP_OK(esp_wifi_internal_wifi_type_md5_check(test_wifi_type_md5)); + + ESP_LOGI(TAG, "test passed..."); +} + +TEST_CASE("esp wifi MD5","[wifi]") +{ + const char *test_esp_wifi_md5 = WIFI_ESP_WIFI_MD5; + + ESP_LOGI(TAG, "test esp_wifi.h MD5..."); + TEST_ESP_OK(esp_wifi_internal_esp_wifi_md5_check(test_esp_wifi_md5)); + + ESP_LOGI(TAG, "test passed..."); +} diff --git a/components/esp32/test/test_ipc.c b/components/esp32/test/test_ipc.c index c58e8f79a..f6dd53103 100644 --- a/components/esp32/test/test_ipc.c +++ b/components/esp32/test/test_ipc.c @@ -3,7 +3,9 @@ #include "freertos/task.h" #include "unity.h" #include "esp_ipc.h" +#include "sdkconfig.h" +#if !CONFIG_FREERTOS_UNICORE static void test_func_ipc_cb(void *arg) { vTaskDelay(50); @@ -14,10 +16,7 @@ static void test_func_ipc_cb(void *arg) TEST_CASE("Test blocking IPC function call", "[ipc]") { int val = 0x5a5a; -#ifdef CONFIG_FREERTOS_UNICORE - esp_ipc_call_blocking(xPortGetCoreID(), test_func_ipc_cb, &val); -#else esp_ipc_call_blocking(!xPortGetCoreID(), test_func_ipc_cb, &val); -#endif TEST_ASSERT_EQUAL_HEX(val, 0xa5a5); } +#endif /* !CONFIG_FREERTOS_UNICORE */ diff --git a/components/esp32/test/test_wifi_lib_git_commit.c b/components/esp32/test/test_wifi_lib_git_commit.c deleted file mode 100644 index fbf1dea0e..000000000 --- a/components/esp32/test/test_wifi_lib_git_commit.c +++ /dev/null @@ -1,12 +0,0 @@ -/* - Tests for the Wi-Fi -*/ -#include "unity.h" -#include "esp_log.h" -#include "esp_wifi_internal.h" - -TEST_CASE("wifi lib git commit id","[wifi]") -{ - TEST_ESP_OK( esp_wifi_internal_git_commit_id_check() ); -} - diff --git a/components/esp32/wifi_init.c b/components/esp32/wifi_init.c index cf5d79b7b..7b700dd4e 100644 --- a/components/esp32/wifi_init.c +++ b/components/esp32/wifi_init.c @@ -20,6 +20,14 @@ #include "soc/rtc.h" #include "esp_mesh.h" +#if (CONFIG_ESP32_WIFI_RX_BA_WIN > CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM) +#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!" +#endif + +#if (CONFIG_ESP32_WIFI_RX_BA_WIN > (CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM << 1)) +#error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than double of the WIFI_STATIC_RX_BUFFER_NUM!" +#endif + /* mesh event callback handler */ mesh_event_cb_t g_mesh_event_cb = NULL; diff --git a/components/esp_event/default_event_loop.c b/components/esp_event/default_event_loop.c index 91fb78927..6b1f71ac4 100644 --- a/components/esp_event/default_event_loop.c +++ b/components/esp_event/default_event_loop.c @@ -14,6 +14,7 @@ #include "esp_event.h" #include "esp_event_internal.h" +#include "esp_task.h" /* ------------------------- Static Variables ------------------------------- */ diff --git a/components/esp_event/esp_event.c b/components/esp_event/esp_event.c index d9f65c4a2..22774a880 100644 --- a/components/esp_event/esp_event.c +++ b/components/esp_event/esp_event.c @@ -164,6 +164,7 @@ static esp_err_t handler_instances_add(esp_event_handler_instances_t* handlers, if (handler == it->handler) { it->arg = handler_arg; ESP_LOGW(TAG, "handler already registered, overwriting"); + free(handler_instance); return ESP_OK; } last = it; @@ -195,7 +196,7 @@ static esp_err_t base_node_add_handler(esp_event_base_node_t* base_node, int32_t id_node = (esp_event_id_node_t*) calloc(1, sizeof(*id_node)); if (!id_node) { - ESP_LOGI(TAG, "alloc for new id node failed"); + ESP_LOGE(TAG, "alloc for new id node failed"); return ESP_ERR_NO_MEM; } @@ -212,6 +213,8 @@ static esp_err_t base_node_add_handler(esp_event_base_node_t* base_node, int32_t else { SLIST_INSERT_AFTER(last_id_node, id_node, next); } + } else { + free(id_node); } return err; @@ -263,6 +266,8 @@ static esp_err_t loop_node_add_handler(esp_event_loop_node_t* loop_node, esp_eve else { SLIST_INSERT_AFTER(last_base_node, base_node, next); } + } else { + free(base_node); } return err; @@ -413,7 +418,7 @@ esp_err_t esp_event_loop_create(const esp_event_loop_args_t* event_loop_args, es loop = calloc(1, sizeof(*loop)); if (loop == NULL) { ESP_LOGE(TAG, "alloc for event loop failed"); - goto on_err; + return err; } loop->queue = xQueueCreate(event_loop_args->queue_size , sizeof(esp_event_post_instance_t)); @@ -521,30 +526,30 @@ esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t tick bool exec = false; - esp_event_handler_instance_t *handler; - esp_event_loop_node_t *loop_node; - esp_event_base_node_t *base_node; - esp_event_id_node_t *id_node; + esp_event_handler_instance_t *handler, *temp_handler; + esp_event_loop_node_t *loop_node, *temp_node; + esp_event_base_node_t *base_node, *temp_base; + esp_event_id_node_t *id_node, *temp_id_node; - SLIST_FOREACH(loop_node, &(loop->loop_nodes), next) { + SLIST_FOREACH_SAFE(loop_node, &(loop->loop_nodes), next, temp_node) { // Execute loop level handlers - SLIST_FOREACH(handler, &(loop_node->handlers), next) { + SLIST_FOREACH_SAFE(handler, &(loop_node->handlers), next, temp_handler) { handler_execute(loop, handler, post); exec |= true; } - SLIST_FOREACH(base_node, &(loop_node->base_nodes), next) { + SLIST_FOREACH_SAFE(base_node, &(loop_node->base_nodes), next, temp_base) { if (base_node->base == post.base) { // Execute base level handlers - SLIST_FOREACH(handler, &(base_node->handlers), next) { + SLIST_FOREACH_SAFE(handler, &(base_node->handlers), next, temp_handler) { handler_execute(loop, handler, post); exec |= true; } - SLIST_FOREACH(id_node, &(base_node->id_nodes), next) { - if(id_node->id == post.id) { + SLIST_FOREACH_SAFE(id_node, &(base_node->id_nodes), next, temp_id_node) { + if (id_node->id == post.id) { // Execute id level handlers - SLIST_FOREACH(handler, &(id_node->handlers), next) { + SLIST_FOREACH_SAFE(handler, &(id_node->handlers), next, temp_handler) { handler_execute(loop, handler, post); exec |= true; } @@ -688,6 +693,8 @@ esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop, es else { SLIST_INSERT_AFTER(last_loop_node, loop_node, next); } + } else { + free(loop_node); } } else { diff --git a/components/esp_event/include/esp_event.h b/components/esp_event/include/esp_event.h index f97deaf8b..48918398c 100644 --- a/components/esp_event/include/esp_event.h +++ b/components/esp_event/include/esp_event.h @@ -296,7 +296,7 @@ esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop, address - memory address of the event loop name - name of the event loop, 'none' if no dedicated task total_recieved - number of successfully posted events - total_dropped - number of events unsucessfully posted due to queue being full + total_dropped - number of events unsuccessfully posted due to queue being full handler format: address ev:base,id inv:total_invoked run:total_runtime diff --git a/components/esp_event/test/test_event.c b/components/esp_event/test/test_event.c index 9805bd2e0..d0a06c737 100644 --- a/components/esp_event/test/test_event.c +++ b/components/esp_event/test/test_event.c @@ -126,6 +126,9 @@ static esp_event_loop_args_t test_event_get_default_loop_args() static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + if (!event_handler_arg) { + return; + } simple_arg_t* arg = (simple_arg_t*) event_handler_arg; xSemaphoreTake(arg->mutex, portMAX_DELAY); @@ -248,6 +251,17 @@ static void test_handler_post_wo_task(void* event_handler_arg, esp_event_base_t } } +static void test_handler_unregister_itself(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data) +{ + esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data; + int* unregistered = (int*) event_handler_arg; + + (*unregistered) += (event_base == s_test_base1 ? 0 : 10) + event_id + 1; + + // Unregister this handler for this event + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, event_base, event_id, test_handler_unregister_itself)); +} + static void test_post_from_handler_loop_task(void* args) { esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args; @@ -345,7 +359,13 @@ TEST_CASE("can register/unregister handlers for all events/all events for a spec loop_args.task_name = NULL; TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop)); + /* Register the handler twice to the same base and id but with a different argument (expects to return ESP_OK and log a warning) + * This aims to verify: 1) Handler's argument to be updated + * 2) Registration not to leak memory + */ + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, NULL)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg)); TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg)); @@ -418,6 +438,61 @@ TEST_CASE("can unregister handler", "[event]") TEST_TEARDOWN(); } +TEST_CASE("handler can unregister itself", "[event]") +{ + /* this test aims to verify that handlers can unregister themselves */ + + TEST_SETUP(); + + esp_event_loop_handle_t loop; + esp_event_loop_args_t loop_args = test_event_get_default_loop_args(); + + loop_args.task_name = NULL; + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop)); + + int unregistered = 0; + + /* + * s_test_base1, ev1 = 1 + * s_test_base1, ev2 = 2 + * s_test_base2, ev1 = 11 + * s_test_base2, ev2 = 12 + */ + int expected_unregistered = 0; + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_unregister_itself, &unregistered)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_unregister_itself, &unregistered)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_handler_unregister_itself, &unregistered)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV2, test_handler_unregister_itself, &unregistered)); + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10))); + expected_unregistered = 2; // base1, ev2 + TEST_ASSERT_EQUAL(expected_unregistered, unregistered); + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10))); + expected_unregistered += 1 + 11; // base1, ev1 + base2, ev1 + TEST_ASSERT_EQUAL(expected_unregistered, unregistered); + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10))); + expected_unregistered += 12; // base2, ev2 + TEST_ASSERT_EQUAL(expected_unregistered, unregistered); + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY)); + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10))); + TEST_ASSERT_EQUAL(expected_unregistered, unregistered); // all handlers unregistered + + TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop)); + + TEST_TEARDOWN(); +} + TEST_CASE("can exit running loop at approximately the set amount of time", "[event]") { /* this test aims to verify that running loop does not block indefinitely in cases where diff --git a/components/esp_http_client/Kconfig b/components/esp_http_client/Kconfig index c362d1e89..9cc8a6805 100644 --- a/components/esp_http_client/Kconfig +++ b/components/esp_http_client/Kconfig @@ -7,4 +7,11 @@ menu "ESP HTTP client" help This option will enable https protocol by linking mbedtls library and initializing SSL transport + config ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH + bool "Enable HTTP Basic Authentication" + default n + help + This option will enable HTTP Basic Authentication. It is disabled by default as Basic + auth uses unencrypted encoding, so it introduces a vulnerability when not using TLS + endmenu diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 09734ffbf..25c0d5d87 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -152,20 +152,6 @@ static const char *HTTP_METHOD_MAPPING[] = { "OPTIONS" }; -/** - * Enum for the HTTP status codes. - */ -enum HttpStatus_Code -{ - /* 3xx - Redirection */ - HttpStatus_MovedPermanently = 301, - HttpStatus_Found = 302, - - /* 4xx - Client Error */ - HttpStatus_Unauthorized = 401 -}; - - static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len); static esp_err_t esp_http_client_connect(esp_http_client_handle_t client); static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client); @@ -297,6 +283,57 @@ esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const c return http_header_delete(client->request->headers, key); } +esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value) +{ + if (client == NULL || value == NULL) { + ESP_LOGE(TAG, "client or value must not be NULL"); + return ESP_ERR_INVALID_ARG; + } + *value = client->connection_info.username; + return ESP_OK; +} + +esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username) +{ + if (client == NULL) { + ESP_LOGE(TAG, "client must not be NULL"); + return ESP_ERR_INVALID_ARG; + } + if (username == NULL && client->connection_info.username != NULL) { + free(client->connection_info.username); + client->connection_info.username = NULL; + } else if (username != NULL) { + client->connection_info.username = strdup(username); + } + return ESP_OK; +} + +esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value) +{ + if (client == NULL || value == NULL) { + ESP_LOGE(TAG, "client or value must not be NULL"); + return ESP_ERR_INVALID_ARG; + } + *value = client->connection_info.password; + return ESP_OK; +} + +esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, char *password) +{ + if (client == NULL) { + ESP_LOGE(TAG, "client must not be NULL"); + return ESP_ERR_INVALID_ARG; + } + if (password == NULL && client->connection_info.password != NULL) { + memset(client->connection_info.password, 0, strlen(client->connection_info.password)); + free(client->connection_info.password); + client->connection_info.password = NULL; + } else if (password != NULL) { + client->connection_info.password = strdup(password); + } + return ESP_OK; +} + static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config) { client->connection_info.method = config->method; @@ -500,6 +537,10 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co if (config->client_key_pem) { esp_transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem)); } + + if (config->skip_cert_common_name_check) { + esp_transport_ssl_skip_common_name_check(ssl); + } #endif if (_set_config(client, config) != ESP_OK) { @@ -597,13 +638,12 @@ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client) if (client->location == NULL) { return ESP_ERR_INVALID_ARG; } + ESP_LOGD(TAG, "Redirect to %s", client->location); return esp_http_client_set_url(client, client->location); } static esp_err_t esp_http_check_response(esp_http_client_handle_t client) { - char *auth_header = NULL; - if (client->redirect_counter >= client->max_redirection_count || client->disable_auto_redirect) { ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter); return ESP_ERR_HTTP_MAX_REDIRECT; @@ -611,44 +651,12 @@ static esp_err_t esp_http_check_response(esp_http_client_handle_t client) switch (client->response->status_code) { case HttpStatus_MovedPermanently: case HttpStatus_Found: - ESP_LOGI(TAG, "Redirect to %s", client->location); - esp_http_client_set_url(client, client->location); + esp_http_client_set_redirection(client); client->redirect_counter ++; client->process_again = 1; break; case HttpStatus_Unauthorized: - auth_header = client->auth_header; - if (auth_header) { - http_utils_trim_whitespace(&auth_header); - ESP_LOGD(TAG, "UNAUTHORIZED: %s", auth_header); - client->redirect_counter ++; - if (http_utils_str_starts_with(auth_header, "Digest") == 0) { - ESP_LOGD(TAG, "type = Digest"); - client->connection_info.auth_type = HTTP_AUTH_TYPE_DIGEST; - } else if (http_utils_str_starts_with(auth_header, "Basic") == 0) { - ESP_LOGD(TAG, "type = Basic"); - client->connection_info.auth_type = HTTP_AUTH_TYPE_BASIC; - } else { - client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE; - ESP_LOGE(TAG, "This authentication method is not supported: %s", auth_header); - break; - } - - _clear_auth_data(client); - - client->auth_data->method = strdup(HTTP_METHOD_MAPPING[client->connection_info.method]); - - client->auth_data->nc = 1; - client->auth_data->realm = http_utils_get_string_between(auth_header, "realm=\"", "\""); - client->auth_data->algorithm = http_utils_get_string_between(auth_header, "algorithm=", ","); - client->auth_data->qop = http_utils_get_string_between(auth_header, "qop=\"", "\""); - client->auth_data->nonce = http_utils_get_string_between(auth_header, "nonce=\"", "\""); - client->auth_data->opaque = http_utils_get_string_between(auth_header, "opaque=\"", "\""); - client->process_again = 1; - } else { - client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE; - ESP_LOGW(TAG, "This request requires authentication, but does not provide header information for that"); - } + esp_http_client_add_auth(client); } return ESP_OK; } @@ -734,13 +742,7 @@ esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *u } else { return ESP_ERR_NO_MEM; } - } else { - free(client->connection_info.username); - free(client->connection_info.password); - client->connection_info.username = NULL; - client->connection_info.password = NULL; - } - + } //Reset path and query if there are no information if (purl.field_data[UF_PATH].len) { @@ -788,6 +790,22 @@ static int esp_http_client_get_data(esp_http_client_handle_t client) return rlen; } +bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client) +{ + if (client->response->is_chunked) { + if (!client->is_chunk_complete) { + ESP_LOGD(TAG, "Chunks were not completely read"); + return false; + } + } else { + if (client->response->data_process != client->response->content_length) { + ESP_LOGD(TAG, "Data processed %d != Data specified in content length %d", client->response->data_process, client->response->content_length); + return false; + } + } + return true; +} + int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) { esp_http_buffer_t *res_buffer = client->response->buffer; @@ -811,7 +829,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) } else { is_data_remain = client->response->data_process < client->response->content_length; } - ESP_LOGD(TAG, "is_data_remain=%d, is_chunked=%d", is_data_remain, client->response->is_chunked); + ESP_LOGD(TAG, "is_data_remain=%d, is_chunked=%d, content_length=%d", is_data_remain, client->response->is_chunked, client->response->content_length); if (!is_data_remain) { break; } @@ -819,10 +837,23 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) if (byte_to_read > client->buffer_size) { byte_to_read = client->buffer_size; } + errno = 0; rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms); ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx); if (rlen <= 0) { + if (errno != 0) { + esp_log_level_t sev = ESP_LOG_WARN; + /* On connection close from server, recv should ideally return 0 but we have error conversion + * in `tcp_transport` SSL layer which translates it `-1` and hence below additional checks */ + if (rlen == -1 && errno == ENOTCONN && client->response->is_chunked) { + /* Explicit call to parser for invoking `message_complete` callback */ + http_parser_execute(client->parser, client->parser_settings, res_buffer->data, 0); + /* ...and lowering the message severity, as closed connection from server side is expected in chunked transport */ + sev = ESP_LOG_DEBUG; + } + ESP_LOG_LEVEL(sev, TAG, "esp_transport_read returned:%d and errno:%d ", rlen, errno); + } return ridx; } res_buffer->output_ptr = buffer + ridx; @@ -1095,6 +1126,7 @@ static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, i client->data_written_index = 0; client->data_write_left = client->post_len; + http_dispatch_event(client, HTTP_EVENT_HEADER_SENT, NULL, 0); client->state = HTTP_STATE_REQ_COMPLETE_HEADER; return ESP_OK; } @@ -1135,7 +1167,7 @@ esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len) return err; } if ((err = esp_http_client_request_send(client, write_len)) != ESP_OK) { - return err; + return err; } return ESP_OK; } @@ -1225,3 +1257,48 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h return HTTP_TRANSPORT_UNKNOWN; } } + +void esp_http_client_add_auth(esp_http_client_handle_t client) +{ + if (client == NULL) { + return; + } + if (client->state != HTTP_STATE_RES_COMPLETE_HEADER) { + return; + } + + char *auth_header = client->auth_header; + if (auth_header) { + http_utils_trim_whitespace(&auth_header); + ESP_LOGD(TAG, "UNAUTHORIZED: %s", auth_header); + client->redirect_counter++; + if (http_utils_str_starts_with(auth_header, "Digest") == 0) { + ESP_LOGD(TAG, "type = Digest"); + client->connection_info.auth_type = HTTP_AUTH_TYPE_DIGEST; +#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH + } else if (http_utils_str_starts_with(auth_header, "Basic") == 0) { + ESP_LOGD(TAG, "type = Basic"); + client->connection_info.auth_type = HTTP_AUTH_TYPE_BASIC; +#endif + } else { + client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE; + ESP_LOGE(TAG, "This authentication method is not supported: %s", auth_header); + return; + } + + _clear_auth_data(client); + + client->auth_data->method = strdup(HTTP_METHOD_MAPPING[client->connection_info.method]); + + client->auth_data->nc = 1; + client->auth_data->realm = http_utils_get_string_between(auth_header, "realm=\"", "\""); + client->auth_data->algorithm = http_utils_get_string_between(auth_header, "algorithm=", ","); + client->auth_data->qop = http_utils_get_string_between(auth_header, "qop=\"", "\""); + client->auth_data->nonce = http_utils_get_string_between(auth_header, "nonce=\"", "\""); + client->auth_data->opaque = http_utils_get_string_between(auth_header, "opaque=\"", "\""); + client->process_again = 1; + } else { + client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE; + ESP_LOGW(TAG, "This request requires authentication, but does not provide header information for that"); + } +} \ No newline at end of file diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 18b68b883..4ee0ff359 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -118,8 +118,20 @@ typedef struct { void *user_data; /*!< HTTP user_data context */ bool is_async; /*!< Set asynchronous mode, only supported with HTTPS for now */ bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */ + bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field */ } esp_http_client_config_t; +/** + * Enum for the HTTP status codes. + */ +typedef enum { + /* 3xx - Redirection */ + HttpStatus_MovedPermanently = 301, + HttpStatus_Found = 302, + + /* 4xx - Client Error */ + HttpStatus_Unauthorized = 401 +} HttpStatus_Code; #define ESP_ERR_HTTP_BASE (0x7000) /*!< Starting number of HTTP error codes */ #define ESP_ERR_HTTP_MAX_REDIRECT (ESP_ERR_HTTP_BASE + 1) /*!< The error exceeds the number of HTTP redirects */ @@ -235,13 +247,71 @@ esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char */ esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value); +/** + * @brief Get http request username. + * The address of username buffer will be assigned to value parameter. + * This function must be called after `esp_http_client_init`. + * + * @param[in] client The esp_http_client handle + * @param[out] value The username value + * + * @return + * - ESP_OK + * - ESP_ERR_INVALID_ARG + */ +esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value); + +/** + * @brief Set http request username. + * The value of username parameter will be assigned to username buffer. + * If the username parameter is NULL then username buffer will be freed. + * + * @param[in] client The esp_http_client handle + * @param[in] username The username value + * + * @return + * - ESP_OK + * - ESP_ERR_INVALID_ARG + */ +esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username); + +/** + * @brief Get http request password. + * The address of password buffer will be assigned to value parameter. + * This function must be called after `esp_http_client_init`. + * + * @param[in] client The esp_http_client handle + * @param[out] value The password value + * + * @return + * - ESP_OK + * - ESP_ERR_INVALID_ARG + */ +esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value); + +/** + * @brief Set http request password. + * The value of password parameter will be assigned to password buffer. + * If the password parameter is NULL then password buffer will be freed. + * + * @param[in] client The esp_http_client handle + * @param[in] password The password value + * + * @return + * - ESP_OK + * - ESP_ERR_INVALID_ARG + */ +esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, char *password); + /** * @brief Set http request method * * @param[in] client The esp_http_client handle * @param[in] method The method * - * @return ESP_OK + * @return + * - ESP_OK + * - ESP_ERR_INVALID_ARG */ esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method); @@ -383,12 +453,34 @@ esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_h * * @param[in] client The esp_http_client handle * - * @return + * @return * - ESP_OK * - ESP_FAIL */ esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client); +/** + * @brief On receiving HTTP Status code 401, this API can be invoked to add authorization + * information. + * + * @note There is a possibility of receiving body message with redirection status codes, thus make sure + * to flush off body data after calling this API. + * + * @param[in] client The esp_http_client handle + */ +void esp_http_client_add_auth(esp_http_client_handle_t client); + +/** + * @brief Checks if entire data in the response has been read without any error. + * + * @param[in] client The esp_http_client handle + * + * @return + * - true + * - false + */ +bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client); + #ifdef __cplusplus } #endif diff --git a/components/esp_http_client/lib/http_header.c b/components/esp_http_client/lib/http_header.c index b771f6f6e..0e41786ec 100644 --- a/components/esp_http_client/lib/http_header.c +++ b/components/esp_http_client/lib/http_header.c @@ -178,6 +178,8 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu int idx = 0; int ret_idx = -1; bool is_end = false; + + // iterate over the header entries to calculate buffer size and determine last item STAILQ_FOREACH(item, header, next) { if (item->value && idx >= index) { siz += strlen(item->key); @@ -187,7 +189,9 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu idx ++; if (siz + 1 > *buffer_len - 2) { + // if this item would not fit to the buffer, return the index of the last fitting one ret_idx = idx - 1; + break; } } @@ -195,10 +199,12 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu return 0; } if (ret_idx < 0) { + // all items would fit, mark this as the end of http header string ret_idx = idx; is_end = true; } + // iterate again over the header entries to write only the fitting indeces int str_len = 0; idx = 0; STAILQ_FOREACH(item, header, next) { @@ -208,6 +214,7 @@ int http_header_generate_string(http_header_handle_t header, int index, char *bu idx ++; } if (is_end) { + // write the http header terminator if all header entries have been written in this function call str_len += snprintf(buffer + str_len, *buffer_len - str_len, "\r\n"); } *buffer_len = str_len; diff --git a/components/esp_http_client/test/test_http_client.c b/components/esp_http_client/test/test_http_client.c index 64c6ad3d0..4a2f725d8 100644 --- a/components/esp_http_client/test/test_http_client.c +++ b/components/esp_http_client/test/test_http_client.c @@ -20,7 +20,11 @@ #include "unity.h" #include "test_utils.h" -TEST_CASE("Input Param Tests", "[ESP HTTP CLIENT]") +#define HOST "httpbin.org" +#define USERNAME "user" +#define PASSWORD "challenge" + +TEST_CASE("Test in common case: Only URL and hostname are specified.", "[ESP HTTP CLIENT]") { esp_http_client_config_t config_incorrect = {0}; @@ -38,10 +42,98 @@ TEST_CASE("Input Param Tests", "[ESP HTTP CLIENT]") esp_http_client_config_t config_with_hostname_path = { - .host = "httpbin.org", + .host = HOST, .path = "/get", }; client = esp_http_client_init(&config_with_hostname_path); TEST_ASSERT(client != NULL); TEST_ASSERT(esp_http_client_cleanup(client) == ESP_OK); } + +TEST_CASE("Get username and password after initialization.", "[ESP HTTP CLIENT]") +{ + esp_http_client_config_t config_with_auth = { + .host = HOST, + .path = "/", + .username = USERNAME, + .password = PASSWORD + }; + char *value = NULL; + esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); + TEST_ASSERT_NOT_NULL(client); + // Test with username + esp_err_t r = esp_http_client_get_username(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + TEST_ASSERT_NOT_NULL(value); + TEST_ASSERT_EQUAL_STRING(USERNAME, value); + // Test with password + value = NULL; + r = esp_http_client_get_password(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + TEST_ASSERT_NOT_NULL(value); + TEST_ASSERT_EQUAL_STRING(PASSWORD, value); + esp_http_client_cleanup(client); +} + +/** + * Test case to test that, the esp_http_client_set_url won't drop username and password + * when pass a path "/abc" for url. + **/ +TEST_CASE("Username is unmodified when we change to new path", "[ESP HTTP CLIENT]") +{ + esp_http_client_config_t config_with_auth = { + .host = HOST, + .path = "/", + .username = USERNAME, + .password = PASSWORD + }; + char *value = NULL; + esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); + TEST_ASSERT_NOT_NULL(client); + esp_err_t r = esp_http_client_get_username(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + TEST_ASSERT_NOT_NULL(value); + TEST_ASSERT_EQUAL_STRING(USERNAME, value); + esp_http_client_set_url(client, "/something-else/"); + r = esp_http_client_get_username(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + TEST_ASSERT_NOT_NULL(value); + TEST_ASSERT_EQUAL_STRING(USERNAME, value); + esp_http_client_cleanup(client); +} + +/** + * Test case to test that, the esp_http_client_set_url do not reset the auth credentials + * Explicit APIs esp_http_client_set_username and esp_http_client_set_password are used to change + * the auth credentials + **/ +TEST_CASE("Username and password will not reset if new absolute URL doesnot specify auth credentials.", "[ESP HTTP CLIENT]") +{ + esp_http_client_config_t config_with_auth = { + .host = HOST, + .path = "/", + .username = USERNAME, + .password = PASSWORD + }; + char *value = NULL; + esp_http_client_handle_t client = esp_http_client_init(&config_with_auth); + TEST_ASSERT_NOT_NULL(client); + esp_err_t r = esp_http_client_get_username(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + TEST_ASSERT_NOT_NULL(value); + TEST_ASSERT_EQUAL_STRING(USERNAME, value); + esp_http_client_set_url(client, "http://" HOST "/get"); + esp_http_client_set_username(client, value); + esp_http_client_set_password(client, value); + //checks if username is set or not + r = esp_http_client_get_username(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + //If username is set then value should not be NULL + TEST_ASSERT_NOT_NULL(value); + //checks if password is set or not + r = esp_http_client_get_password(client, &value); + TEST_ASSERT_EQUAL(ESP_OK, r); + //If password is set then value should not be NULL + TEST_ASSERT_NOT_NULL(value); + esp_http_client_cleanup(client); +} diff --git a/components/esp_http_server/src/httpd_parse.c b/components/esp_http_server/src/httpd_parse.c index 765510f0c..ec73bbb5f 100644 --- a/components/esp_http_server/src/httpd_parse.c +++ b/components/esp_http_server/src/httpd_parse.c @@ -267,6 +267,23 @@ static esp_err_t cb_header_value(http_parser *parser, const char *at, size_t len parser_data->last.at = at; parser_data->last.length = 0; parser_data->status = PARSING_HDR_VALUE; + + if (length == 0) { + /* As per behavior of http_parser, when length > 0, + * `at` points to the start of CRLF. But, in the + * case when header value is empty (zero length), + * then `at` points to the position right after + * the CRLF. Since for our purpose we need `last.at` + * to point to exactly where the CRLF starts, it + * needs to be adjusted by the right offset */ + char *at_adj = (char *)parser_data->last.at; + /* Find the end of header field string */ + while (*(--at_adj) != ':'); + /* Now skip leading spaces' */ + while (*(++at_adj) == ' '); + /* Now we are at the right position */ + parser_data->last.at = at_adj; + } } else if (parser_data->status != PARSING_HDR_VALUE) { ESP_LOGE(TAG, LOG_FMT("unexpected state transition")); parser_data->error = HTTPD_500_INTERNAL_SERVER_ERROR; diff --git a/components/esp_http_server/src/httpd_sess.c b/components/esp_http_server/src/httpd_sess.c index c56c22efa..fb6bba106 100644 --- a/components/esp_http_server/src/httpd_sess.c +++ b/components/esp_http_server/src/httpd_sess.c @@ -202,7 +202,7 @@ static int fd_is_valid(int fd) static inline uint64_t httpd_sess_get_lru_counter() { static uint64_t lru_counter = 0; - return lru_counter++; + return ++lru_counter; } void httpd_sess_delete_invalid(struct httpd_data *hd) @@ -378,6 +378,10 @@ static void httpd_sess_close(void *arg) { struct sock_db *sock_db = (struct sock_db *)arg; if (sock_db) { + if (sock_db->lru_counter == 0) { + ESP_LOGD(TAG, "Skipping session close for %d as it seems to be a race condition", sock_db->fd); + return; + } int fd = sock_db->fd; struct httpd_data *hd = (struct httpd_data *) sock_db->handle; httpd_sess_delete(hd, fd); diff --git a/components/esp_https_ota/CMakeLists.txt b/components/esp_https_ota/CMakeLists.txt index 6ef0a5c9e..d0a4ee838 100644 --- a/components/esp_https_ota/CMakeLists.txt +++ b/components/esp_https_ota/CMakeLists.txt @@ -1,7 +1,7 @@ set(COMPONENT_ADD_INCLUDEDIRS include) set(COMPONENT_SRCS "src/esp_https_ota.c") -set(COMPONENT_REQUIRES esp_http_client) +set(COMPONENT_REQUIRES esp_http_client bootloader_support) set(COMPONENT_PRIV_REQUIRES log app_update) register_component() diff --git a/components/esp_https_ota/include/esp_https_ota.h b/components/esp_https_ota/include/esp_https_ota.h index c87ec3bdf..fd69f27f8 100644 --- a/components/esp_https_ota/include/esp_https_ota.h +++ b/components/esp_https_ota/include/esp_https_ota.h @@ -15,20 +15,40 @@ #pragma once #include +#include #ifdef __cplusplus extern "C" { #endif +typedef void *esp_https_ota_handle_t; + +/** + * @brief ESP HTTPS OTA configuration + */ +typedef struct { + const esp_http_client_config_t *http_config; /*!< ESP HTTP client configuration */ +} esp_https_ota_config_t; + +#define ESP_ERR_HTTPS_OTA_BASE (0x9000) +#define ESP_ERR_HTTPS_OTA_IN_PROGRESS (ESP_ERR_HTTPS_OTA_BASE + 1) /* OTA operation in progress */ + /** * @brief HTTPS OTA Firmware upgrade. * - * This function performs HTTPS OTA Firmware upgrade - * + * This function allocates HTTPS OTA Firmware upgrade context, establishes HTTPS connection, + * reads image data from HTTP stream and writes it to OTA partition and + * finishes HTTPS OTA Firmware upgrade operation. + * This API supports URL redirection, but if CA cert of URLs differ then it + * should be appended to `cert_pem` member of `config`. + * * @param[in] config pointer to esp_http_client_config_t structure. * - * @note For secure HTTPS updates, the `cert_pem` member of `config` - * structure must be set to the server certificate. + * @note This API handles the entire OTA operation, so if this API is being used + * then no other APIs from `esp_https_ota` component should be called. + * If more information and control is needed during the HTTPS OTA process, + * then one can use `esp_https_ota_begin` and subsequent APIs. If this API returns + * successfully, esp_restart() must be called to boot from the new firmware image. * * @return * - ESP_OK: OTA data updated, next reboot will use specified partition. @@ -41,6 +61,121 @@ extern "C" { */ esp_err_t esp_https_ota(const esp_http_client_config_t *config); +/** + * @brief Start HTTPS OTA Firmware upgrade + * + * This function initializes ESP HTTPS OTA context and establishes HTTPS connection. + * This function must be invoked first. If this function returns successfully, then `esp_https_ota_perform` should be + * called to continue with the OTA process and there should be a call to `esp_https_ota_finish` on + * completion of OTA operation or on failure in subsequent operations. + * This API supports URL redirection, but if CA cert of URLs differ then it + * should be appended to `cert_pem` member of `http_config`, which is a part of `ota_config`. + * In case of error, this API explicitly sets `handle` to NULL. + * + * @param[in] ota_config pointer to esp_https_ota_config_t structure + * @param[out] handle pointer to an allocated data of type `esp_https_ota_handle_t` + * which will be initialised in this function + * + * @note This API is blocking, so setting `is_async` member of `http_config` structure will + * result in an error. + * + * @return + * - ESP_OK: HTTPS OTA Firmware upgrade context initialised and HTTPS connection established + * - ESP_FAIL: For generic failure. + * - ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, certificate, etc.) + * - For other return codes, refer documentation in app_update component and esp_http_client + * component in esp-idf. + */ +esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle); + +/** + * @brief Read image data from HTTP stream and write it to OTA partition + * + * This function reads image data from HTTP stream and writes it to OTA partition. This function + * must be called only if esp_https_ota_begin() returns successfully. + * This function must be called in a loop since it returns after every HTTP read operation thus + * giving you the flexibility to stop OTA operation midway. + * + * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure + * + * @return + * - ESP_ERR_HTTPS_OTA_IN_PROGRESS: OTA update is in progress, call this API again to continue. + * - ESP_OK: OTA update was successful + * - ESP_FAIL: OTA update failed + * - ESP_ERR_INVALID_ARG: Invalid argument + * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image + * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation. + * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed. + * - For other return codes, refer OTA documentation in esp-idf's app_update component. + */ +esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle); + +/** + * @brief Checks if complete data was received or not + * + * @note This API can be called just before esp_https_ota_finish() to validate if the complete image was indeed received. + * + * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure + * + * @return + * - false + * - true + */ +bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle); + +/** + * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection + * + * This function closes the HTTP connection and frees the ESP HTTPS OTA context. + * This function switches the boot partition to the OTA partition containing the + * new firmware image. + * + * @note If this API returns successfully, esp_restart() must be called to + * boot from the new firmware image + * + * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure + * + * @return + * - ESP_OK: Clean-up successful + * - ESP_ERR_INVALID_STATE + * - ESP_ERR_INVALID_ARG: Invalid argument + * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image + */ +esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle); + + +/** + * @brief Reads app description from image header. The app description provides information + * like the "Firmware version" of the image. + * + * @note This API can be called only after esp_https_ota_begin() and before esp_https_ota_perform(). + * Calling this API is not mandatory. + * + * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure + * @param[out] new_app_info pointer to an allocated esp_app_desc_t structure + * + * @return + * - ESP_ERR_INVALID_ARG: Invalid arguments + * - ESP_FAIL: Failed to read image descriptor + * - ESP_OK: Successfully read image descriptor + */ +esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info); + + +/* +* @brief This function returns OTA image data read so far. +* +* @note This API should be called only if `esp_https_ota_perform()` has been called atleast once or +* if `esp_https_ota_get_img_desc` has been called before. +* +* @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure +* +* @return +* - -1 On failure +* - total bytes read so far +*/ +int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle); + #ifdef __cplusplus } #endif diff --git a/components/esp_https_ota/src/esp_https_ota.c b/components/esp_https_ota/src/esp_https_ota.c index 010be37e2..4bab1dcdb 100644 --- a/components/esp_https_ota/src/esp_https_ota.c +++ b/components/esp_https_ota/src/esp_https_ota.c @@ -16,121 +16,399 @@ #include #include #include -#include #include +#include +#include -#define DEFAULT_OTA_BUF_SIZE 256 +#define IMAGE_HEADER_SIZE sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1 +#define DEFAULT_OTA_BUF_SIZE IMAGE_HEADER_SIZE static const char *TAG = "esp_https_ota"; -static void http_cleanup(esp_http_client_handle_t client) +typedef enum { + ESP_HTTPS_OTA_INIT, + ESP_HTTPS_OTA_BEGIN, + ESP_HTTPS_OTA_IN_PROGRESS, + ESP_HTTPS_OTA_SUCCESS, +} esp_https_ota_state; + +struct esp_https_ota_handle { + esp_ota_handle_t update_handle; + const esp_partition_t *update_partition; + esp_http_client_handle_t http_client; + char *ota_upgrade_buf; + size_t ota_upgrade_buf_size; + int binary_file_len; + esp_https_ota_state state; +}; + +typedef struct esp_https_ota_handle esp_https_ota_t; + +static bool process_again(int status_code) +{ + switch (status_code) { + case HttpStatus_MovedPermanently: + case HttpStatus_Found: + case HttpStatus_Unauthorized: + return true; + default: + return false; + } + return false; +} + +static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code) +{ + esp_err_t err; + if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found) { + err = esp_http_client_set_redirection(http_client); + if (err != ESP_OK) { + ESP_LOGE(TAG, "URL redirection Failed"); + return err; + } + } else if (status_code == HttpStatus_Unauthorized) { + esp_http_client_add_auth(http_client); + } + + char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE]; + // process_again() returns true only in case of redirection. + if (process_again(status_code)) { + while (1) { + /* + * In case of redirection, esp_http_client_read() is called + * to clear the response buffer of http_client. + */ + int data_read = esp_http_client_read(http_client, upgrade_data_buf, DEFAULT_OTA_BUF_SIZE); + if (data_read < 0) { + ESP_LOGE(TAG, "Error: SSL data read error"); + return ESP_FAIL; + } else if (data_read == 0) { + return ESP_OK; + } + } + } + return ESP_OK; +} + +static esp_err_t _http_connect(esp_http_client_handle_t http_client) +{ + esp_err_t err = ESP_FAIL; + int status_code, header_ret; + do { + err = esp_http_client_open(http_client, 0); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); + return err; + } + header_ret = esp_http_client_fetch_headers(http_client); + if (header_ret < 0) { + return header_ret; + } + status_code = esp_http_client_get_status_code(http_client); + err = _http_handle_response_code(http_client, status_code); + if (err != ESP_OK) { + return err; + } + } while (process_again(status_code)); + return err; +} + +static void _http_cleanup(esp_http_client_handle_t client) { esp_http_client_close(client); esp_http_client_cleanup(client); } +static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len) +{ + if (buffer == NULL || https_ota_handle == NULL) { + return ESP_FAIL; + } + esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err); + } else { + https_ota_handle->binary_file_len += buf_len; + ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len); + err = ESP_ERR_HTTPS_OTA_IN_PROGRESS; + } + return err; +} + +esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle) +{ + esp_err_t err; + + if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) { + ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument"); + if (handle) { + *handle = NULL; + } + return ESP_ERR_INVALID_ARG; + } + +#if !CONFIG_OTA_ALLOW_HTTP + if (!ota_config->http_config->cert_pem) { + ESP_LOGE(TAG, "Server certificate not found in esp_http_client config"); + *handle = NULL; + return ESP_ERR_INVALID_ARG; + } +#endif + + esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t)); + if (!https_ota_handle) { + ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer"); + *handle = NULL; + return ESP_ERR_NO_MEM; + } + + /* Initiate HTTP Connection */ + https_ota_handle->http_client = esp_http_client_init(ota_config->http_config); + if (https_ota_handle->http_client == NULL) { + ESP_LOGE(TAG, "Failed to initialise HTTP connection"); + err = ESP_FAIL; + goto failure; + } + + err = _http_connect(https_ota_handle->http_client); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to establish HTTP connection"); + goto http_cleanup; + } + + https_ota_handle->update_partition = NULL; + ESP_LOGI(TAG, "Starting OTA..."); + https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL); + if (https_ota_handle->update_partition == NULL) { + ESP_LOGE(TAG, "Passive OTA partition not found"); + err = ESP_FAIL; + goto http_cleanup; + } + ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", + https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address); + + const int alloc_size = (ota_config->http_config->buffer_size > DEFAULT_OTA_BUF_SIZE) ? + ota_config->http_config->buffer_size : DEFAULT_OTA_BUF_SIZE; + https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size); + if (!https_ota_handle->ota_upgrade_buf) { + ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer"); + err = ESP_ERR_NO_MEM; + goto http_cleanup; + } + https_ota_handle->ota_upgrade_buf_size = alloc_size; + + https_ota_handle->binary_file_len = 0; + *handle = (esp_https_ota_handle_t)https_ota_handle; + https_ota_handle->state = ESP_HTTPS_OTA_BEGIN; + return ESP_OK; + +http_cleanup: + _http_cleanup(https_ota_handle->http_client); +failure: + free(https_ota_handle); + *handle = NULL; + return err; +} + +esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info) +{ + esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle; + if (handle == NULL || new_app_info == NULL) { + ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument"); + return ESP_ERR_INVALID_ARG; + } + if (handle->state < ESP_HTTPS_OTA_BEGIN) { + ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state"); + return ESP_FAIL; + } + /* + * `data_read_size` holds number of bytes needed to read complete header. + * `bytes_read` holds number of bytes read. + */ + int data_read_size = IMAGE_HEADER_SIZE; + int data_read = 0, bytes_read = 0; + /* + * while loop is added to download complete image headers, even if the headers + * are not sent in a single packet. + */ + while (data_read_size > 0 && !esp_https_ota_is_complete_data_received(https_ota_handle)) { + data_read = esp_http_client_read(handle->http_client, + (handle->ota_upgrade_buf + bytes_read), + data_read_size); + /* + * As esp_http_client_read never returns negative error code, we rely on + * `errno` to check for underlying transport connectivity closure if any + */ + if (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) { + ESP_LOGE(TAG, "Connection closed, errno = %d", errno); + break; + } + data_read_size -= data_read; + bytes_read += data_read; + } + if (data_read_size > 0) { + ESP_LOGE(TAG, "Complete headers were not received"); + return ESP_FAIL; + } + handle->binary_file_len = bytes_read; + memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t)); + return ESP_OK; +} + +esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle) +{ + esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle; + if (handle == NULL) { + ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument"); + return ESP_ERR_INVALID_ARG; + } + if (handle->state < ESP_HTTPS_OTA_BEGIN) { + ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state"); + return ESP_FAIL; + } + + esp_err_t err; + int data_read; + switch (handle->state) { + case ESP_HTTPS_OTA_BEGIN: + err = esp_ota_begin(handle->update_partition, OTA_SIZE_UNKNOWN, &handle->update_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); + return err; + } + handle->state = ESP_HTTPS_OTA_IN_PROGRESS; + /* In case `esp_https_ota_read_img_desc` was invoked first, + then the image data read there should be written to OTA partition + */ + if (handle->binary_file_len) { + return _ota_write(handle, (const void *)handle->ota_upgrade_buf, handle->binary_file_len); + } + /* falls through */ + case ESP_HTTPS_OTA_IN_PROGRESS: + data_read = esp_http_client_read(handle->http_client, + handle->ota_upgrade_buf, + handle->ota_upgrade_buf_size); + if (data_read == 0) { + /* + * esp_https_ota_is_complete_data_received is added to check whether + * complete image is received. + */ + bool is_recv_complete = esp_https_ota_is_complete_data_received(https_ota_handle); + /* + * As esp_http_client_read never returns negative error code, we rely on + * `errno` to check for underlying transport connectivity closure if any. + * Incase the complete data has not been received but the server has sent + * an ENOTCONN or ECONNRESET, failure is returned. We close with success + * if complete data has been received. + */ + if ((errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) && !is_recv_complete) { + ESP_LOGE(TAG, "Connection closed, errno = %d", errno); + return ESP_FAIL; + } else if (!is_recv_complete) { + return ESP_ERR_HTTPS_OTA_IN_PROGRESS; + } + ESP_LOGI(TAG, "Connection closed"); + } else if (data_read > 0) { + return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read); + } + handle->state = ESP_HTTPS_OTA_SUCCESS; + break; + default: + ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State"); + return ESP_FAIL; + break; + } + return ESP_OK; +} + +bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle) +{ + esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle; + return esp_http_client_is_complete_data_received(handle->http_client); +} + +esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle) +{ + esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle; + if (handle == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (handle->state < ESP_HTTPS_OTA_BEGIN) { + return ESP_FAIL; + } + + esp_err_t err = ESP_OK; + switch (handle->state) { + case ESP_HTTPS_OTA_SUCCESS: + case ESP_HTTPS_OTA_IN_PROGRESS: + err = esp_ota_end(handle->update_handle); + /* falls through */ + case ESP_HTTPS_OTA_BEGIN: + if (handle->ota_upgrade_buf) { + free(handle->ota_upgrade_buf); + } + if (handle->http_client) { + _http_cleanup(handle->http_client); + } + break; + default: + ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State"); + break; + } + + if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) { + esp_err_t err = esp_ota_set_boot_partition(handle->update_partition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err); + } + } + free(handle); + return err; +} + +int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle) +{ + esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle; + if (handle == NULL) { + return -1; + } + if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) { + return -1; + } + return handle->binary_file_len; +} + esp_err_t esp_https_ota(const esp_http_client_config_t *config) { if (!config) { ESP_LOGE(TAG, "esp_http_client config not found"); return ESP_ERR_INVALID_ARG; - } + } -#if !CONFIG_OTA_ALLOW_HTTP - if (!config->cert_pem && !config->use_global_ca_store) { - ESP_LOGE(TAG, "Server certificate not found, either through configuration or global CA store"); - return ESP_ERR_INVALID_ARG; - } -#endif + esp_https_ota_config_t ota_config = { + .http_config = config, + }; - esp_http_client_handle_t client = esp_http_client_init(config); - if (client == NULL) { - ESP_LOGE(TAG, "Failed to initialise HTTP connection"); + esp_https_ota_handle_t https_ota_handle = NULL; + esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle); + if (https_ota_handle == NULL) { return ESP_FAIL; } -#if !CONFIG_OTA_ALLOW_HTTP - if (esp_http_client_get_transport_type(client) != HTTP_TRANSPORT_OVER_SSL) { - ESP_LOGE(TAG, "Transport is not over HTTPS"); - return ESP_FAIL; - } -#endif - - esp_err_t err = esp_http_client_open(client, 0); - if (err != ESP_OK) { - esp_http_client_cleanup(client); - ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); - return err; - } - esp_http_client_fetch_headers(client); - - esp_ota_handle_t update_handle = 0; - const esp_partition_t *update_partition = NULL; - ESP_LOGI(TAG, "Starting OTA..."); - update_partition = esp_ota_get_next_update_partition(NULL); - if (update_partition == NULL) { - ESP_LOGE(TAG, "Passive OTA partition not found"); - http_cleanup(client); - return ESP_FAIL; - } - ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x", - update_partition->subtype, update_partition->address); - - err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); - if (err != ESP_OK) { - ESP_LOGE(TAG, "esp_ota_begin failed, error=%d", err); - http_cleanup(client); - return err; - } - ESP_LOGI(TAG, "esp_ota_begin succeeded"); - ESP_LOGI(TAG, "Please Wait. This may take time"); - - esp_err_t ota_write_err = ESP_OK; - const int alloc_size = (config->buffer_size > 0) ? config->buffer_size : DEFAULT_OTA_BUF_SIZE; - char *upgrade_data_buf = (char *)malloc(alloc_size); - if (!upgrade_data_buf) { - ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer"); - return ESP_ERR_NO_MEM; - } - - int binary_file_len = 0; while (1) { - int data_read = esp_http_client_read(client, upgrade_data_buf, alloc_size); - if (data_read == 0) { - ESP_LOGI(TAG, "Connection closed, all data received"); + err = esp_https_ota_perform(https_ota_handle); + if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) { break; } - if (data_read < 0) { - ESP_LOGE(TAG, "Error: SSL data read error"); - break; - } - if (data_read > 0) { - ota_write_err = esp_ota_write(update_handle, (const void *) upgrade_data_buf, data_read); - if (ota_write_err != ESP_OK) { - break; - } - binary_file_len += data_read; - ESP_LOGD(TAG, "Written image length %d", binary_file_len); - } - } - free(upgrade_data_buf); - http_cleanup(client); - ESP_LOGD(TAG, "Total binary data length writen: %d", binary_file_len); - - esp_err_t ota_end_err = esp_ota_end(update_handle); - if (ota_write_err != ESP_OK) { - ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err); - return ota_write_err; - } else if (ota_end_err != ESP_OK) { - ESP_LOGE(TAG, "Error: esp_ota_end failed! err=0x%d. Image is invalid", ota_end_err); - return ota_end_err; } - err = esp_ota_set_boot_partition(update_partition); + esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle); if (err != ESP_OK) { - ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err); + /* If there was an error in esp_https_ota_perform(), + then it is given more precedence than error in esp_https_ota_finish() + */ return err; + } else if (ota_finish_err != ESP_OK) { + return ota_finish_err; } - ESP_LOGI(TAG, "esp_ota_set_boot_partition succeeded"); - return ESP_OK; } diff --git a/components/esp_websocket_client/CMakeLists.txt b/components/esp_websocket_client/CMakeLists.txt new file mode 100644 index 000000000..b346e7c58 --- /dev/null +++ b/components/esp_websocket_client/CMakeLists.txt @@ -0,0 +1,6 @@ +set(COMPONENT_SRCS "esp_websocket_client.c") +set(COMPONENT_ADD_INCLUDEDIRS "include") + +set(COMPONENT_REQUIRES lwip esp-tls tcp_transport nghttp) + +register_component() diff --git a/components/esp_websocket_client/component.mk b/components/esp_websocket_client/component.mk new file mode 100644 index 000000000..7fb6cd504 --- /dev/null +++ b/components/esp_websocket_client/component.mk @@ -0,0 +1,3 @@ +COMPONENT_SRCDIRS := . + +COMPONENT_ADD_INCLUDEDIRS := include \ No newline at end of file diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c new file mode 100644 index 000000000..6b2833f42 --- /dev/null +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -0,0 +1,717 @@ +// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "esp_websocket_client.h" +#include "esp_transport.h" +#include "esp_transport_tcp.h" +#include "esp_transport_ssl.h" +#include "esp_transport_ws.h" +/* using uri parser */ +#include "http_parser.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" +#include "esp_log.h" +#include "esp_timer.h" + +static const char *TAG = "WEBSOCKET_CLIENT"; + +#define WEBSOCKET_TCP_DEFAULT_PORT (80) +#define WEBSOCKET_SSL_DEFAULT_PORT (443) +#define WEBSOCKET_BUFFER_SIZE_BYTE (1024) +#define WEBSOCKET_RECONNECT_TIMEOUT_MS (10*1000) +#define WEBSOCKET_TASK_PRIORITY (5) +#define WEBSOCKET_TASK_STACK (4*1024) +#define WEBSOCKET_NETWORK_TIMEOUT_MS (10*1000) +#define WEBSOCKET_PING_TIMEOUT_MS (10*1000) +#define WEBSOCKET_EVENT_QUEUE_SIZE (1) + +#define ESP_WS_CLIENT_MEM_CHECK(TAG, a, action) if (!(a)) { \ + ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \ + action; \ + } + +const static int STOPPED_BIT = BIT0; + +ESP_EVENT_DEFINE_BASE(WEBSOCKET_EVENTS); + +typedef struct { + int task_stack; + int task_prio; + char *uri; + char *host; + char *path; + char *scheme; + char *username; + char *password; + int port; + bool auto_reconnect; + void *user_context; + int network_timeout_ms; + char *subprotocol; + char *user_agent; + char *headers; + int pingpong_timeout_sec; +} websocket_config_storage_t; + +typedef enum { + WEBSOCKET_STATE_ERROR = -1, + WEBSOCKET_STATE_UNKNOW = 0, + WEBSOCKET_STATE_INIT, + WEBSOCKET_STATE_CONNECTED, + WEBSOCKET_STATE_WAIT_TIMEOUT, +} websocket_client_state_t; + +struct esp_websocket_client { + esp_event_loop_handle_t event_handle; + esp_transport_list_handle_t transport_list; + esp_transport_handle_t transport; + websocket_config_storage_t *config; + websocket_client_state_t state; + uint64_t keepalive_tick_ms; + uint64_t reconnect_tick_ms; + uint64_t ping_tick_ms; + uint64_t pingpong_tick_ms; + int wait_timeout_ms; + int auto_reconnect; + bool run; + bool wait_for_pong_resp; + EventGroupHandle_t status_bits; + xSemaphoreHandle lock; + char *rx_buffer; + char *tx_buffer; + int buffer_size; + ws_transport_opcodes_t last_opcode; + int payload_len; + int payload_offset; +}; + +static uint64_t _tick_get_ms(void) +{ + return esp_timer_get_time()/1000; +} + +static esp_err_t esp_websocket_client_dispatch_event(esp_websocket_client_handle_t client, + esp_websocket_event_id_t event, + const char *data, + int data_len) +{ + esp_err_t err; + esp_websocket_event_data_t event_data; + + event_data.client = client; + event_data.user_context = client->config->user_context; + event_data.data_ptr = data; + event_data.data_len = data_len; + event_data.op_code = client->last_opcode; + event_data.payload_len = client->payload_len; + event_data.payload_offset = client->payload_offset; + + if ((err = esp_event_post_to(client->event_handle, + WEBSOCKET_EVENTS, event, + &event_data, + sizeof(esp_websocket_event_data_t), + portMAX_DELAY)) != ESP_OK) { + return err; + } + return esp_event_loop_run(client->event_handle, 0); +} + +static esp_err_t esp_websocket_client_abort_connection(esp_websocket_client_handle_t client) +{ + esp_transport_close(client->transport); + client->wait_timeout_ms = WEBSOCKET_RECONNECT_TIMEOUT_MS; + client->reconnect_tick_ms = _tick_get_ms(); + client->state = WEBSOCKET_STATE_WAIT_TIMEOUT; + ESP_LOGI(TAG, "Reconnect after %d ms", client->wait_timeout_ms); + esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DISCONNECTED, NULL, 0); + return ESP_OK; +} + +static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t client, const esp_websocket_client_config_t *config) +{ + websocket_config_storage_t *cfg = client->config; + cfg->task_prio = config->task_prio; + if (cfg->task_prio <= 0) { + cfg->task_prio = WEBSOCKET_TASK_PRIORITY; + } + + cfg->task_stack = config->task_stack; + if (cfg->task_stack == 0) { + cfg->task_stack = WEBSOCKET_TASK_STACK; + } + + if (config->host) { + cfg->host = strdup(config->host); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->host, return ESP_ERR_NO_MEM); + } + + if (config->port) { + cfg->port = config->port; + } + + if (config->username) { + free(cfg->username); + cfg->username = strdup(config->username); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->username, return ESP_ERR_NO_MEM); + } + + if (config->password) { + free(cfg->password); + cfg->password = strdup(config->password); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->password, return ESP_ERR_NO_MEM); + } + + if (config->uri) { + free(cfg->uri); + cfg->uri = strdup(config->uri); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->uri, return ESP_ERR_NO_MEM); + } + if (config->path) { + free(cfg->path); + cfg->path = strdup(config->path); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->path, return ESP_ERR_NO_MEM); + } + if (config->subprotocol) { + free(cfg->subprotocol); + cfg->subprotocol = strdup(config->subprotocol); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->subprotocol, return ESP_ERR_NO_MEM); + } + if (config->user_agent) { + free(cfg->user_agent); + cfg->user_agent = strdup(config->user_agent); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->user_agent, return ESP_ERR_NO_MEM); + } + if (config->headers) { + free(cfg->headers); + cfg->headers = strdup(config->headers); + ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->headers, return ESP_ERR_NO_MEM); + } + + cfg->network_timeout_ms = WEBSOCKET_NETWORK_TIMEOUT_MS; + cfg->user_context = config->user_context; + cfg->auto_reconnect = true; + if (config->disable_auto_reconnect) { + cfg->auto_reconnect = false; + } + + cfg->pingpong_timeout_sec = config->pingpong_timeout_sec; + + return ESP_OK; +} + +static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + websocket_config_storage_t *cfg = client->config; + if (client->config == NULL) { + return ESP_ERR_INVALID_ARG; + } + free(cfg->host); + free(cfg->uri); + free(cfg->path); + free(cfg->scheme); + free(cfg->username); + free(cfg->password); + free(cfg->subprotocol); + free(cfg->user_agent); + free(cfg->headers); + memset(cfg, 0, sizeof(websocket_config_storage_t)); + free(client->config); + client->config = NULL; + return ESP_OK; +} + +static void set_websocket_transport_optional_settings(esp_websocket_client_handle_t client, esp_transport_handle_t trans) +{ + if (trans && client->config->path) { + esp_transport_ws_set_path(trans, client->config->path); + } + if (trans && client->config->subprotocol) { + esp_transport_ws_set_subprotocol(trans, client->config->subprotocol); + } + if (trans && client->config->user_agent) { + esp_transport_ws_set_user_agent(trans, client->config->user_agent); + } + if (trans && client->config->headers) { + esp_transport_ws_set_headers(trans, client->config->headers); + } +} + +esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config) +{ + esp_websocket_client_handle_t client = calloc(1, sizeof(struct esp_websocket_client)); + ESP_WS_CLIENT_MEM_CHECK(TAG, client, return NULL); + + esp_event_loop_args_t event_args = { + .queue_size = WEBSOCKET_EVENT_QUEUE_SIZE, + .task_name = NULL // no task will be created + }; + + if (esp_event_loop_create(&event_args, &client->event_handle) != ESP_OK) { + ESP_LOGE(TAG, "Error create event handler for websocket client"); + free(client); + return NULL; + } + + client->lock = xSemaphoreCreateRecursiveMutex(); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail); + + client->config = calloc(1, sizeof(websocket_config_storage_t)); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config, goto _websocket_init_fail); + + client->transport_list = esp_transport_list_init(); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->transport_list, goto _websocket_init_fail); + + esp_transport_handle_t tcp = esp_transport_tcp_init(); + ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail); + + esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT); + esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup + + + esp_transport_handle_t ws = esp_transport_ws_init(tcp); + ESP_WS_CLIENT_MEM_CHECK(TAG, ws, goto _websocket_init_fail); + + esp_transport_set_default_port(ws, WEBSOCKET_TCP_DEFAULT_PORT); + esp_transport_list_add(client->transport_list, ws, "ws"); + if (config->transport == WEBSOCKET_TRANSPORT_OVER_TCP) { + asprintf(&client->config->scheme, "ws"); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail); + } + + esp_transport_handle_t ssl = esp_transport_ssl_init(); + ESP_WS_CLIENT_MEM_CHECK(TAG, ssl, goto _websocket_init_fail); + + esp_transport_set_default_port(ssl, WEBSOCKET_SSL_DEFAULT_PORT); + if (config->cert_pem) { + esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem)); + } + esp_transport_list_add(client->transport_list, ssl, "_ssl"); // need to save to transport list, for cleanup + + esp_transport_handle_t wss = esp_transport_ws_init(ssl); + ESP_WS_CLIENT_MEM_CHECK(TAG, wss, goto _websocket_init_fail); + + esp_transport_set_default_port(wss, WEBSOCKET_SSL_DEFAULT_PORT); + + esp_transport_list_add(client->transport_list, wss, "wss"); + if (config->transport == WEBSOCKET_TRANSPORT_OVER_SSL) { + asprintf(&client->config->scheme, "wss"); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail); + } + + if (config->uri) { + if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) { + ESP_LOGE(TAG, "Invalid uri"); + goto _websocket_init_fail; + } + } + + if (esp_websocket_client_set_config(client, config) != ESP_OK) { + ESP_LOGE(TAG, "Failed to set the configuration"); + goto _websocket_init_fail; + } + + if (client->config->scheme == NULL) { + asprintf(&client->config->scheme, "ws"); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail); + } + + set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "ws")); + set_websocket_transport_optional_settings(client, esp_transport_list_get_transport(client->transport_list, "wss")); + + client->keepalive_tick_ms = _tick_get_ms(); + client->reconnect_tick_ms = _tick_get_ms(); + client->ping_tick_ms = _tick_get_ms(); + + int buffer_size = config->buffer_size; + if (buffer_size <= 0) { + buffer_size = WEBSOCKET_BUFFER_SIZE_BYTE; + } + client->rx_buffer = malloc(buffer_size); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->rx_buffer, { + goto _websocket_init_fail; + }); + client->tx_buffer = malloc(buffer_size); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->tx_buffer, { + goto _websocket_init_fail; + }); + client->status_bits = xEventGroupCreate(); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->status_bits, { + goto _websocket_init_fail; + }); + + client->buffer_size = buffer_size; + return client; + +_websocket_init_fail: + esp_websocket_client_destroy(client); + return NULL; +} + +esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (client->run) { + esp_websocket_client_stop(client); + } + if (client->event_handle) { + esp_event_loop_delete(client->event_handle); + } + esp_websocket_client_destroy_config(client); + esp_transport_list_destroy(client->transport_list); + vQueueDelete(client->lock); + free(client->tx_buffer); + free(client->rx_buffer); + if (client->status_bits) { + vEventGroupDelete(client->status_bits); + } + free(client); + client = NULL; + return ESP_OK; +} + +esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, const char *uri) +{ + if (client == NULL || uri == NULL) { + return ESP_ERR_INVALID_ARG; + } + struct http_parser_url puri; + http_parser_url_init(&puri); + int parser_status = http_parser_parse_url(uri, strlen(uri), 0, &puri); + if (parser_status != 0) { + ESP_LOGE(TAG, "Error parse uri = %s", uri); + return ESP_FAIL; + } + if (puri.field_data[UF_SCHEMA].len) { + free(client->config->scheme); + asprintf(&client->config->scheme, "%.*s", puri.field_data[UF_SCHEMA].len, uri + puri.field_data[UF_SCHEMA].off); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, return ESP_ERR_NO_MEM); + } + + if (puri.field_data[UF_HOST].len) { + free(client->config->host); + asprintf(&client->config->host, "%.*s", puri.field_data[UF_HOST].len, uri + puri.field_data[UF_HOST].off); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->host, return ESP_ERR_NO_MEM); + } + + + if (puri.field_data[UF_PATH].len || puri.field_data[UF_QUERY].len) { + free(client->config->path); + if (puri.field_data[UF_QUERY].len == 0) { + asprintf(&client->config->path, "%.*s", puri.field_data[UF_PATH].len, uri + puri.field_data[UF_PATH].off); + } else if (puri.field_data[UF_PATH].len == 0) { + asprintf(&client->config->path, "/?%.*s", puri.field_data[UF_QUERY].len, uri + puri.field_data[UF_QUERY].off); + } else { + asprintf(&client->config->path, "%.*s?%.*s", puri.field_data[UF_PATH].len, uri + puri.field_data[UF_PATH].off, + puri.field_data[UF_QUERY].len, uri + puri.field_data[UF_QUERY].off); + } + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM); + } + if (puri.field_data[UF_PORT].off) { + client->config->port = strtol((const char*)(uri + puri.field_data[UF_PORT].off), NULL, 10); + } + + if (puri.field_data[UF_USERINFO].len) { + char *user_info = NULL; + asprintf(&user_info, "%.*s", puri.field_data[UF_USERINFO].len, uri + puri.field_data[UF_USERINFO].off); + if (user_info) { + char *pass = strchr(user_info, ':'); + if (pass) { + pass[0] = 0; //terminal username + pass ++; + free(client->config->password); + client->config->password = strdup(pass); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->password, return ESP_ERR_NO_MEM); + } + free(client->config->username); + client->config->username = strdup(user_info); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->username, return ESP_ERR_NO_MEM); + free(user_info); + } else { + return ESP_ERR_NO_MEM; + } + } + return ESP_OK; +} + +static esp_err_t esp_websocket_client_recv(esp_websocket_client_handle_t client) +{ + int rlen; + client->payload_offset = 0; + do { + rlen = esp_transport_read(client->transport, client->rx_buffer, client->buffer_size, client->config->network_timeout_ms); + if (rlen < 0) { + ESP_LOGE(TAG, "Error read data"); + esp_websocket_client_abort_connection(client); + return ESP_FAIL; + } + client->payload_len = esp_transport_ws_get_read_payload_len(client->transport); + client->last_opcode = esp_transport_ws_get_read_opcode(client->transport); + + esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_DATA, client->rx_buffer, rlen); + + client->payload_offset += rlen; + } while (client->payload_offset < client->payload_len); + + // if a PING message received -> send out the PONG, this will not work for PING messages with payload longer than buffer len + if (client->last_opcode == WS_TRANSPORT_OPCODES_PING) { + const char *data = (client->payload_len == 0) ? NULL : client->rx_buffer; + esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PONG | WS_TRANSPORT_OPCODES_FIN, data, client->payload_len, + client->config->network_timeout_ms); + } + else if (client->last_opcode == WS_TRANSPORT_OPCODES_PONG) { + client->wait_for_pong_resp = false; + } + + return ESP_OK; +} + +static void esp_websocket_client_task(void *pv) +{ + const int lock_timeout = portMAX_DELAY; + esp_websocket_client_handle_t client = (esp_websocket_client_handle_t) pv; + client->run = true; + + //get transport by scheme + client->transport = esp_transport_list_get_transport(client->transport_list, client->config->scheme); + + if (client->transport == NULL) { + ESP_LOGE(TAG, "There are no transports valid, stop websocket client"); + client->run = false; + } + //default port + if (client->config->port == 0) { + client->config->port = esp_transport_get_default_port(client->transport); + } + + client->state = WEBSOCKET_STATE_INIT; + xEventGroupClearBits(client->status_bits, STOPPED_BIT); + int read_select = 0; + while (client->run) { + if (xSemaphoreTakeRecursive(client->lock, lock_timeout) != pdPASS) { + ESP_LOGE(TAG, "Failed to lock ws-client tasks, exitting the task..."); + break; + } + switch ((int)client->state) { + case WEBSOCKET_STATE_INIT: + if (client->transport == NULL) { + ESP_LOGE(TAG, "There are no transport"); + client->run = false; + break; + } + if (esp_transport_connect(client->transport, + client->config->host, + client->config->port, + client->config->network_timeout_ms) < 0) { + ESP_LOGE(TAG, "Error transport connect"); + esp_websocket_client_abort_connection(client); + break; + } + ESP_LOGD(TAG, "Transport connected to %s://%s:%d", client->config->scheme, client->config->host, client->config->port); + + client->state = WEBSOCKET_STATE_CONNECTED; + client->wait_for_pong_resp = false; + esp_websocket_client_dispatch_event(client, WEBSOCKET_EVENT_CONNECTED, NULL, 0); + + break; + case WEBSOCKET_STATE_CONNECTED: + if (_tick_get_ms() - client->ping_tick_ms > WEBSOCKET_PING_TIMEOUT_MS) { + client->ping_tick_ms = _tick_get_ms(); + ESP_LOGD(TAG, "Sending PING..."); + esp_transport_ws_send_raw(client->transport, WS_TRANSPORT_OPCODES_PING | WS_TRANSPORT_OPCODES_FIN, NULL, 0, client->config->network_timeout_ms); + if (!client->wait_for_pong_resp && client->config->pingpong_timeout_sec) { + client->pingpong_tick_ms = _tick_get_ms(); + client->wait_for_pong_resp = true; + } + } + if ( _tick_get_ms() - client->pingpong_tick_ms > client->config->pingpong_timeout_sec*1000 ) { + if (client->wait_for_pong_resp) { + ESP_LOGE(TAG, "Error, no PONG received for more than %d seconds after PING", client->config->pingpong_timeout_sec); + esp_websocket_client_abort_connection(client); + break; + } + } + + if (read_select == 0) { + ESP_LOGV(TAG, "Read poll timeout: skipping esp_transport_read()..."); + break; + } + client->ping_tick_ms = _tick_get_ms(); + + if (esp_websocket_client_recv(client) == ESP_FAIL) { + ESP_LOGE(TAG, "Error receive data"); + esp_websocket_client_abort_connection(client); + break; + } + break; + case WEBSOCKET_STATE_WAIT_TIMEOUT: + + if (!client->config->auto_reconnect) { + client->run = false; + break; + } + if (_tick_get_ms() - client->reconnect_tick_ms > client->wait_timeout_ms) { + client->state = WEBSOCKET_STATE_INIT; + client->reconnect_tick_ms = _tick_get_ms(); + ESP_LOGD(TAG, "Reconnecting..."); + } + break; + } + xSemaphoreGiveRecursive(client->lock); + if (WEBSOCKET_STATE_CONNECTED == client->state) { + read_select = esp_transport_poll_read(client->transport, 1000); //Poll every 1000ms + if (read_select < 0) { + ESP_LOGE(TAG, "Network error: esp_transport_poll_read() returned %d, errno=%d", read_select, errno); + esp_websocket_client_abort_connection(client); + } + } else if (WEBSOCKET_STATE_WAIT_TIMEOUT == client->state) { + // waiting for reconnecting... + vTaskDelay(client->wait_timeout_ms / 2 / portTICK_RATE_MS); + } + } + + esp_transport_close(client->transport); + xEventGroupSetBits(client->status_bits, STOPPED_BIT); + client->state = WEBSOCKET_STATE_UNKNOW; + vTaskDelete(NULL); +} + +esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (client->state >= WEBSOCKET_STATE_INIT) { + ESP_LOGE(TAG, "The client has started"); + return ESP_FAIL; + } + if (xTaskCreate(esp_websocket_client_task, "websocket_task", client->config->task_stack, client, client->config->task_prio, NULL) != pdTRUE) { + ESP_LOGE(TAG, "Error create websocket task"); + return ESP_FAIL; + } + xEventGroupClearBits(client->status_bits, STOPPED_BIT); + return ESP_OK; +} + +esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (!client->run) { + ESP_LOGW(TAG, "Client was not started"); + return ESP_FAIL; + } + client->run = false; + xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY); + client->state = WEBSOCKET_STATE_UNKNOW; + return ESP_OK; +} + +static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const char *data, int len, TickType_t timeout); + +int esp_websocket_client_send_text(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout) +{ + return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_TEXT, data, len, timeout); +} + +int esp_websocket_client_send(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout) +{ + return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, data, len, timeout); +} + +int esp_websocket_client_send_bin(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout) +{ + return esp_websocket_client_send_with_opcode(client, WS_TRANSPORT_OPCODES_BINARY, data, len, timeout); +} + +static int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client, ws_transport_opcodes_t opcode, const char *data, int len, TickType_t timeout) +{ + int need_write = len; + int wlen = 0, widx = 0; + int ret = ESP_FAIL; + + if (client == NULL || data == NULL || len <= 0) { + ESP_LOGE(TAG, "Invalid arguments"); + return ESP_FAIL; + } + + if (xSemaphoreTakeRecursive(client->lock, timeout) != pdPASS) { + ESP_LOGE(TAG, "Could not lock ws-client within %d timeout", timeout); + return ESP_FAIL; + } + + if (!esp_websocket_client_is_connected(client)) { + ESP_LOGE(TAG, "Websocket client is not connected"); + goto unlock_and_return; + } + + if (client->transport == NULL) { + ESP_LOGE(TAG, "Invalid transport"); + goto unlock_and_return; + } + uint32_t current_opcode = opcode; + while (widx < len) { + if (need_write > client->buffer_size) { + need_write = client->buffer_size; + } else { + current_opcode |= WS_TRANSPORT_OPCODES_FIN; + } + memcpy(client->tx_buffer, data + widx, need_write); + // send with ws specific way and specific opcode + wlen = esp_transport_ws_send_raw(client->transport, current_opcode, (char *)client->tx_buffer, need_write, + (timeout==portMAX_DELAY)? -1 : timeout * portTICK_PERIOD_MS); + if (wlen <= 0) { + ret = wlen; + ESP_LOGE(TAG, "Network error: esp_transport_write() returned %d, errno=%d", ret, errno); + esp_websocket_client_abort_connection(client); + goto unlock_and_return; + } + current_opcode = 0; + widx += wlen; + need_write = len - widx; + + } + ret = widx; +unlock_and_return: + xSemaphoreGiveRecursive(client->lock); + return ret; +} + +bool esp_websocket_client_is_connected(esp_websocket_client_handle_t client) +{ + if (client == NULL) { + return false; + } + return client->state == WEBSOCKET_STATE_CONNECTED; +} + +esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client, + esp_websocket_event_id_t event, + esp_event_handler_t event_handler, + void *event_handler_arg) +{ + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } + return esp_event_handler_register_with(client->event_handle, WEBSOCKET_EVENTS, event, event_handler, event_handler_arg); +} diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h new file mode 100644 index 000000000..db521c2d0 --- /dev/null +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -0,0 +1,217 @@ +// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _ESP_WEBSOCKET_CLIENT_H_ +#define _ESP_WEBSOCKET_CLIENT_H_ + + +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "esp_err.h" +#include "esp_event.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct esp_websocket_client *esp_websocket_client_handle_t; + +ESP_EVENT_DECLARE_BASE(WEBSOCKET_EVENTS); // declaration of the task events family + +/** + * @brief Websocket Client events id + */ +typedef enum { + WEBSOCKET_EVENT_ANY = -1, + WEBSOCKET_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */ + WEBSOCKET_EVENT_CONNECTED, /*!< Once the Websocket has been connected to the server, no data exchange has been performed */ + WEBSOCKET_EVENT_DISCONNECTED, /*!< The connection has been disconnected */ + WEBSOCKET_EVENT_DATA, /*!< When receiving data from the server, possibly multiple portions of the packet */ + WEBSOCKET_EVENT_MAX +} esp_websocket_event_id_t; + +/** + * @brief Websocket event data + */ +typedef struct { + const char *data_ptr; /*!< Data pointer */ + int data_len; /*!< Data length */ + uint8_t op_code; /*!< Received opcode */ + esp_websocket_client_handle_t client; /*!< esp_websocket_client_handle_t context */ + void *user_context; /*!< user_data context, from esp_websocket_client_config_t user_data */ + int payload_len; /*!< Total payload length, payloads exceeding buffer will be posted through multiple events */ + int payload_offset; /*!< Actual offset for the data associated with this event */ +} esp_websocket_event_data_t; + +/** + * @brief Websocket Client transport + */ +typedef enum { + WEBSOCKET_TRANSPORT_UNKNOWN = 0x0, /*!< Transport unknown */ + WEBSOCKET_TRANSPORT_OVER_TCP, /*!< Transport over tcp */ + WEBSOCKET_TRANSPORT_OVER_SSL, /*!< Transport over ssl */ +} esp_websocket_transport_t; + +/** + * @brief Websocket client setup configuration + */ +typedef struct { + const char *uri; /*!< Websocket URI, the information on the URI can be overrides the other fields below, if any */ + const char *host; /*!< Domain or IP as string */ + int port; /*!< Port to connect, default depend on esp_websocket_transport_t (80 or 443) */ + const char *username; /*!< Using for Http authentication - Not supported for now */ + const char *password; /*!< Using for Http authentication - Not supported for now */ + const char *path; /*!< HTTP Path, if not set, default is `/` */ + bool disable_auto_reconnect; /*!< Disable the automatic reconnect function when disconnected */ + void *user_context; /*!< HTTP user data context */ + int task_prio; /*!< Websocket task priority */ + int task_stack; /*!< Websocket task stack */ + int buffer_size; /*!< Websocket buffer size */ + const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */ + esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */ + char *subprotocol; /*!< Websocket subprotocol */ + char *user_agent; /*!< Websocket user-agent */ + char *headers; /*!< Websocket additional headers */ + int pingpong_timeout_sec; /*!< Period before connection is aborted due to no PONGs received, disabled if value is 0 */ +} esp_websocket_client_config_t; + +/** + * @brief Start a Websocket session + * This function must be the first function to call, + * and it returns a esp_websocket_client_handle_t that you must use as input to other functions in the interface. + * This call MUST have a corresponding call to esp_websocket_client_destroy when the operation is complete. + * + * @param[in] config The configuration + * + * @return + * - `esp_websocket_client_handle_t` + * - NULL if any errors + */ +esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config); + +/** + * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones + * Must stop the WebSocket client before set URI if the client has been connected + * + * @param[in] client The client + * @param[in] uri The uri + * + * @return esp_err_t + */ +esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, const char *uri); + +/** + * @brief Open the WebSocket connection + * + * @param[in] client The client + * + * @return esp_err_t + */ +esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client); + +/** + * @brief Close the WebSocket connection + * + * @param[in] client The client + * + * @return esp_err_t + */ +esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client); + +/** + * @brief Destroy the WebSocket connection and free all resources. + * This function must be the last function to call for an session. + * It is the opposite of the esp_websocket_client_init function and must be called with the same handle as input that a esp_websocket_client_init call returned. + * This might close all connections this handle has used. + * + * @param[in] client The client + * + * @return esp_err_t + */ +esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client); + +/** + * @brief Generic write data to the WebSocket connection; defaults to binary send + * + * @param[in] client The client + * @param[in] data The data + * @param[in] len The length + * @param[in] timeout Write data timeout in RTOS ticks + * + * @return + * - Number of data was sent + * - (-1) if any errors + */ +int esp_websocket_client_send(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout); + +/** + * @brief Write binary data to the WebSocket connection (data send with WS OPCODE=02, i.e. binary) + * + * @param[in] client The client + * @param[in] data The data + * @param[in] len The length + * @param[in] timeout Write data timeout in RTOS ticks + * + * @return + * - Number of data was sent + * - (-1) if any errors + */ +int esp_websocket_client_send_bin(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout); + +/** + * @brief Write textual data to the WebSocket connection (data send with WS OPCODE=01, i.e. text) + * + * @param[in] client The client + * @param[in] data The data + * @param[in] len The length + * @param[in] timeout Write data timeout in RTOS ticks + * + * @return + * - Number of data was sent + * - (-1) if any errors + */ +int esp_websocket_client_send_text(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout); + +/** + * @brief Check the WebSocket client connection state + * + * @param[in] client The client handle + * + * @return + * - true + * - false + */ +bool esp_websocket_client_is_connected(esp_websocket_client_handle_t client); + +/** + * @brief Register the Websocket Events + * + * @param client The client handle + * @param event The event id + * @param event_handler The callback function + * @param event_handler_arg User context + * @return esp_err_t + */ +esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client, + esp_websocket_event_id_t event, + esp_event_handler_t event_handler, + void *event_handler_arg); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/esptool_py/Makefile.projbuild b/components/esptool_py/Makefile.projbuild index ef892bb09..ceb0d80ac 100644 --- a/components/esptool_py/Makefile.projbuild +++ b/components/esptool_py/Makefile.projbuild @@ -34,6 +34,10 @@ endif ESPTOOL_ELF2IMAGE_OPTIONS := +ifdef CONFIG_ESP32_REV_MIN +ESPTOOL_ELF2IMAGE_OPTIONS += --min-rev $(CONFIG_ESP32_REV_MIN) +endif + ifdef CONFIG_SECURE_BOOT_ENABLED ifndef CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION ifndef IS_BOOTLOADER_BUILD diff --git a/components/esptool_py/esptool b/components/esptool_py/esptool index 9ad444a6e..de30f21a2 160000 --- a/components/esptool_py/esptool +++ b/components/esptool_py/esptool @@ -1 +1 @@ -Subproject commit 9ad444a6e06e58833d5e6044c1d5f3eb3dd56023 +Subproject commit de30f21a222ec62f5a023dd955439b4f57702768 diff --git a/components/esptool_py/project_include.cmake b/components/esptool_py/project_include.cmake index d03552723..eaa95ceaa 100644 --- a/components/esptool_py/project_include.cmake +++ b/components/esptool_py/project_include.cmake @@ -56,18 +56,16 @@ if(NOT BOOTLOADER_BUILD) set(ESPTOOLPY_ELF2IMAGE_OPTIONS --elf-sha256-offset 0xb0) endif() +if(CONFIG_ESP32_REV_MIN) + set(ESPTOOLPY_ELF2IMAGE_OPTIONS ${ESPTOOLPY_ELF2IMAGE_OPTIONS} --min-rev ${CONFIG_ESP32_REV_MIN}) +endif() + if(CONFIG_ESPTOOLPY_FLASHSIZE_DETECT) # Set ESPFLASHSIZE to 'detect' *after* elf2image options are generated, # as elf2image can't have 'detect' as an option... set(ESPFLASHSIZE detect) endif() -# Set variables if the PHY data partition is in the flash -if(CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION) - set(PHY_PARTITION_OFFSET ${CONFIG_PHY_DATA_OFFSET}) - set(PHY_PARTITION_BIN_FILE "esp32/phy_init_data.bin") -endif() - get_filename_component(IDF_PROJECT_NAME ${IDF_PROJECT_EXECUTABLE} NAME_WE) if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT BOOTLOADER_BUILD) set(unsigned_project_binary "${IDF_PROJECT_NAME}-unsigned.bin") @@ -129,7 +127,7 @@ function(esptool_py_custom_target target_name flasher_filename dependencies) -D ESPTOOLPY="${ESPTOOLPY}" -D ESPTOOL_ARGS="write_flash;@flash_${flasher_filename}_args" -D ESPTOOL_WORKING_DIR="${IDF_BUILD_ARTIFACTS_DIR}" - -P run_esptool.cmake + -P ${IDF_PATH}/components/esptool_py/run_esptool.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} USES_TERMINAL ) diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index a74d594d0..c32d946dd 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -825,9 +825,9 @@ static void emac_start(void *param) emac_mac_init(); /* check if enable promiscuous mode */ - if(emac_config.promiscuous_enable){ + if (emac_config.promiscuous_enable) { emac_enable_promiscuous(); - }else{ + } else { emac_disable_promiscuous(); } @@ -1116,12 +1116,15 @@ esp_err_t esp_eth_init_internal(eth_config_t *config) if (emac_config.clock_mode != ETH_CLOCK_GPIO0_IN) { #if CONFIG_SPIRAM_SUPPORT - if (esp_spiram_is_initialized()) { - ESP_LOGE(TAG, "GPIO16 and GPIO17 has been occupied by PSRAM, Only ETH_CLOCK_GPIO_IN is supported!"); - ret = ESP_FAIL; - goto _verify_err; - } else { - ESP_LOGW(TAG, "GPIO16/17 is used for clock of EMAC, Please Make Sure you're not using PSRAM."); + // make sure Ethernet won't have conflict with PSRAM + if (emac_config.clock_mode >= ETH_CLOCK_GPIO16_OUT) { + if (esp_spiram_is_initialized()) { + ESP_LOGE(TAG, "GPIO16 and GPIO17 are occupied by PSRAM, please switch to ETH_CLOCK_GPIO_IN or ETH_CLOCK_GPIO_OUT mode"); + ret = ESP_FAIL; + goto _verify_err; + } else { + ESP_LOGW(TAG, "Using GPIO16/17 to output Ethernet RMII clock, make sure you don't have PSRAM on board"); + } } #endif // 50 MHz = 40MHz * (6 + 4) / (2 * (2 + 2) = 400MHz / 8 diff --git a/components/ethernet/eth_phy/phy_lan8720.c b/components/ethernet/eth_phy/phy_lan8720.c index 666b82868..79f2836da 100644 --- a/components/ethernet/eth_phy/phy_lan8720.c +++ b/components/ethernet/eth_phy/phy_lan8720.c @@ -128,11 +128,11 @@ void phy_lan8720_dump_registers() ESP_LOGD(TAG, "ANAR 0x%04x", esp_eth_smi_read(0x4)); ESP_LOGD(TAG, "ANLPAR 0x%04x", esp_eth_smi_read(0x5)); ESP_LOGD(TAG, "ANER 0x%04x", esp_eth_smi_read(0x6)); - ESP_LOGD(TAG, "MCSR 0x%04x", esp_eth_smi_read(0x17)); - ESP_LOGD(TAG, "SM 0x%04x", esp_eth_smi_read(0x18)); - ESP_LOGD(TAG, "SECR 0x%04x", esp_eth_smi_read(0x26)); - ESP_LOGD(TAG, "CSIR 0x%04x", esp_eth_smi_read(0x27)); - ESP_LOGD(TAG, "ISR 0x%04x", esp_eth_smi_read(0x29)); - ESP_LOGD(TAG, "IMR 0x%04x", esp_eth_smi_read(0x30)); - ESP_LOGD(TAG, "PSCSR 0x%04x", esp_eth_smi_read(0x31)); + ESP_LOGD(TAG, "MCSR 0x%04x", esp_eth_smi_read(0x11)); + ESP_LOGD(TAG, "SM 0x%04x", esp_eth_smi_read(0x12)); + ESP_LOGD(TAG, "SECR 0x%04x", esp_eth_smi_read(0x1A)); + ESP_LOGD(TAG, "CSIR 0x%04x", esp_eth_smi_read(0x1B)); + ESP_LOGD(TAG, "ISR 0x%04x", esp_eth_smi_read(0x1D)); + ESP_LOGD(TAG, "IMR 0x%04x", esp_eth_smi_read(0x1E)); + ESP_LOGD(TAG, "PSCSR 0x%04x", esp_eth_smi_read(0x1F)); } diff --git a/components/expat/CMakeLists.txt b/components/expat/CMakeLists.txt index 821705658..2fc1db422 100644 --- a/components/expat/CMakeLists.txt +++ b/components/expat/CMakeLists.txt @@ -1,6 +1,5 @@ set(COMPONENT_ADD_INCLUDEDIRS expat/expat/lib port/include) -set(COMPONENT_SRCS "expat/expat/lib/loadlibrary.c" - "expat/expat/lib/xmlparse.c" +set(COMPONENT_SRCS "expat/expat/lib/xmlparse.c" "expat/expat/lib/xmlrole.c" "expat/expat/lib/xmltok.c" "expat/expat/lib/xmltok_impl.c" @@ -16,4 +15,4 @@ component_compile_definitions(HAVE_GETRANDOM) # Temporary suppress "fallthrough" warnings until they are fixed in expat repo if(GCC_NOT_5_2_0) component_compile_options(-Wno-implicit-fallthrough) -endif() \ No newline at end of file +endif() diff --git a/components/expat/expat b/components/expat/expat index 968b8cc46..a7bc26b69 160000 --- a/components/expat/expat +++ b/components/expat/expat @@ -1 +1 @@ -Subproject commit 968b8cc46dbee47b83318d5f31a8e7907199614b +Subproject commit a7bc26b69768f7fb24f0c7976fae24b157b85b13 diff --git a/components/fatfs/src/vfs_fat_sdmmc.c b/components/fatfs/src/vfs_fat_sdmmc.c index b667aa19f..131a0493a 100644 --- a/components/fatfs/src/vfs_fat_sdmmc.c +++ b/components/fatfs/src/vfs_fat_sdmmc.c @@ -158,6 +158,8 @@ fail: ff_diskio_unregister(pdrv); free(s_card); s_card = NULL; + free(s_base_path); + s_base_path = NULL; return err; } diff --git a/components/freemodbus/modbus_controller/mbcontroller.c b/components/freemodbus/modbus_controller/mbcontroller.c index 2ca06bef9..b8447b44b 100644 --- a/components/freemodbus/modbus_controller/mbcontroller.c +++ b/components/freemodbus/modbus_controller/mbcontroller.c @@ -422,8 +422,8 @@ eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress, usCoils--; } // Send an event to notify application task about event - (void)send_param_access_notification(MB_EVENT_COILS_WR); - (void)send_param_info(MB_EVENT_COILS_WR, (uint16_t)usAddress, + (void)send_param_access_notification(MB_EVENT_COILS_RD); + (void)send_param_info(MB_EVENT_COILS_RD, (uint16_t)usAddress, (uint8_t*)(pucCoilsDataBuf), (uint16_t)usNCoils); break; case MB_REG_WRITE: diff --git a/components/freertos/include/freertos/portable.h b/components/freertos/include/freertos/portable.h index ce189f31c..61cee7644 100644 --- a/components/freertos/include/freertos/portable.h +++ b/components/freertos/include/freertos/portable.h @@ -86,6 +86,8 @@ specific constants has been moved into the deprecated_definitions.h header file. */ #include "deprecated_definitions.h" +#include "soc/cpu.h" + /* If portENTER_CRITICAL is not defined then including deprecated_definitions.h did not result in a portmacro.h header file being included - and it should be included here. In this case the path to the correct portmacro.h header file @@ -215,6 +217,24 @@ static inline uint32_t IRAM_ATTR xPortGetCoreID() { /* Get tick rate per second */ uint32_t xPortGetTickRateHz(void); + +static inline bool IRAM_ATTR xPortCanYield(void) +{ + uint32_t ps_reg = 0; + + //Get the current value of PS (processor status) register + RSR(PS, ps_reg); + + /* + * intlevel = (ps_reg & 0xf); + * excm = (ps_reg >> 4) & 0x1; + * CINTLEVEL is max(excm * EXCMLEVEL, INTLEVEL), where EXCMLEVEL is 3. + * However, just return true, only intlevel is zero. + */ + + return ((ps_reg & PS_INTLEVEL_MASK) == 0); +} + #ifdef __cplusplus } #endif diff --git a/components/freertos/include/freertos/task.h b/components/freertos/include/freertos/task.h index 3fc06d9e3..adefb3a85 100644 --- a/components/freertos/include/freertos/task.h +++ b/components/freertos/include/freertos/task.h @@ -1962,7 +1962,7 @@ BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClea * * \ingroup TaskNotifications */ -#define xTaskNotifyGive( xTaskToNotify ) xTaskNotify( ( xTaskToNotify ), 0, eIncrement ); +#define xTaskNotifyGive( xTaskToNotify ) xTaskNotify( ( xTaskToNotify ), 0, eIncrement ) /** * Simplified macro for sending task notification from ISR. diff --git a/components/freertos/tasks.c b/components/freertos/tasks.c index 6c077ff2f..13da17cb8 100644 --- a/components/freertos/tasks.c +++ b/components/freertos/tasks.c @@ -1302,7 +1302,7 @@ static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode //No mux; no harm done if this misfires. The deleted task won't get scheduled anyway. if( pxTCB == pxCurrentTCB[ core ] ) //If task was currently running on this core { - configASSERT( uxSchedulerSuspended[ core ] == 0 ); + configASSERT( xTaskGetSchedulerState() != taskSCHEDULER_SUSPENDED ) /* The pre-delete hook is primarily for the Windows simulator, in which Windows specific clean up operations are performed, @@ -1337,7 +1337,7 @@ static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode configASSERT( pxPreviousWakeTime ); configASSERT( ( xTimeIncrement > 0U ) ); - configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 ); + configASSERT( xTaskGetSchedulerState() != taskSCHEDULER_SUSPENDED ); taskENTER_CRITICAL(&xTaskQueueMutex); // vTaskSuspendAll(); @@ -1435,7 +1435,7 @@ static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode /* A delay time of zero just forces a reschedule. */ if( xTicksToDelay > ( TickType_t ) 0U ) { - configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 ); + configASSERT( xTaskGetSchedulerState() != taskSCHEDULER_SUSPENDED ); taskENTER_CRITICAL(&xTaskQueueMutex); // vTaskSuspendAll(); { @@ -1818,7 +1818,7 @@ static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode if( xSchedulerRunning != pdFALSE ) { /* The current task has just been suspended. */ - configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 ); + configASSERT( xTaskGetSchedulerState() != taskSCHEDULER_SUSPENDED ); portYIELD_WITHIN_API(); } else @@ -2212,9 +2212,9 @@ BaseType_t xTaskResumeAll( void ) TCB_t *pxTCB; BaseType_t xAlreadyYielded = pdFALSE; - /* If uxSchedulerSuspended[ xPortGetCoreID() ] is zero then this function does not match a + /* If scheduler state is `taskSCHEDULER_RUNNING` then this function does not match a previous call to vTaskSuspendAll(). */ - configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] ); + configASSERT( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING ); /* It is possible that an ISR caused a task to be removed from an event list while the scheduler was suspended. If this was the case then the removed task will have been added to the xPendingReadyList. Once the diff --git a/components/freertos/test/test_suspend_scheduler.c b/components/freertos/test/test_suspend_scheduler.c index 9aef2ce7e..37cbb2eeb 100644 --- a/components/freertos/test/test_suspend_scheduler.c +++ b/components/freertos/test/test_suspend_scheduler.c @@ -108,6 +108,7 @@ TEST_CASE("Scheduler disabled can handle a pending context switch on resume", "[ // When we resume scheduler, we expect the counter task // will preempt and count at least one more item esp_intr_noniram_enable(); + timer_enable_intr(TIMER_GROUP_0, TIMER_0); xTaskResumeAll(); TEST_ASSERT_NOT_EQUAL(count_config.counter, no_sched_task); diff --git a/components/heap/multi_heap_poisoning.c b/components/heap/multi_heap_poisoning.c index dabf6cc24..2287d3db1 100644 --- a/components/heap/multi_heap_poisoning.c +++ b/components/heap/multi_heap_poisoning.c @@ -110,7 +110,7 @@ static poison_head_t *verify_allocated_region(void *data, bool print_errors) } if (canary != TAIL_CANARY_PATTERN) { if (print_errors) { - printf("CORRUPT HEAP: Bad tail at %p. Expected 0x%08x got 0x%08x\n", &tail->tail_canary, + MULTI_HEAP_STDERR_PRINTF("CORRUPT HEAP: Bad tail at %p. Expected 0x%08x got 0x%08x\n", &tail->tail_canary, TAIL_CANARY_PATTERN, canary); } return NULL; @@ -184,6 +184,10 @@ static bool verify_fill_pattern(void *data, size_t size, bool print_errors, bool void *multi_heap_malloc(multi_heap_handle_t heap, size_t size) { + if (!size) { + return NULL; + } + if(size > SIZE_MAX - POISON_OVERHEAD) { return NULL; } @@ -302,9 +306,11 @@ void *multi_heap_get_block_owner(multi_heap_block_handle_t block) multi_heap_handle_t multi_heap_register(void *start, size_t size) { +#ifdef SLOW if (start != NULL) { memset(start, FREE_FILL_PATTERN, size); } +#endif return multi_heap_register_impl(start, size); } diff --git a/components/heap/test/test_malloc.c b/components/heap/test/test_malloc.c index 703270c9a..e147d9f29 100644 --- a/components/heap/test/test_malloc.c +++ b/components/heap/test/test_malloc.c @@ -132,3 +132,9 @@ TEST_CASE("unreasonable allocs should all fail", "[heap]") TEST_ASSERT_NULL(test_malloc_wrapper(xPortGetFreeHeapSize() - 1)); } +TEST_CASE("malloc(0) should return a NULL pointer", "[heap]") +{ + void *p; + p = malloc(0); + TEST_ASSERT(p == NULL); +} \ No newline at end of file diff --git a/components/idf_test/integration_test/CIConfigs/nvs_compatible_test.yml b/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml similarity index 52% rename from components/idf_test/integration_test/CIConfigs/nvs_compatible_test.yml rename to components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml index 6318d4e42..917efd748 100644 --- a/components/idf_test/integration_test/CIConfigs/nvs_compatible_test.yml +++ b/components/idf_test/integration_test/CIConfigs/nvs_compatible_test_.yml @@ -1,11 +1,11 @@ BinPath: - path: SSC/ssc_bin/ESP32_IDF/SSC_BLE - test app: SSC_BLE + path: SSC/ssc_bin/ESP32_IDF/SSC_BLE_WIFI + test app: SSC_BLE_WIFI DUT: [SSC1] Filter: - Add: SDK: ESP32_IDF - Test App: SSC_BLE + Test App: SSC_BLE_WIFI summary: 'use old NVS data WIFI function test' diff --git a/components/idf_test/integration_test/TC_IT_MESH_EST.yml b/components/idf_test/integration_test/TC_IT_MESH_EST.yml index 0c2f011c5..12c9979e9 100644 --- a/components/idf_test/integration_test/TC_IT_MESH_EST.yml +++ b/components/idf_test/integration_test/TC_IT_MESH_EST.yml @@ -4216,10 +4216,10 @@ test cases: - - SSC SSC1 reboot - - P SSC1 C !!!ready!!! - P SSC2 C MESH_EVENT_DISCONNECTE - - - SSC SSC1 meshset -O -o 0 -n 1 -t 1 - - - P SSC1 C +MESHSET:SELF_ORG,OK - - - SSC SSC1 meshset -T -o 1 - - - P SSC1 C MESHGET:TYPE,OK,0 + - - SSC SSC2 meshset -O -o 0 -n 1 -t 1 + - - P SSC2 C +MESHSET:SELF_ORG,OK + - - SSC SSC2 meshset -T -o 1 + - - P SSC2 C MESHGET:TYPE,OK,0 expected result: |- 1. succeed 2. succeed @@ -4227,7 +4227,7 @@ test cases: steps: |- 1. dut1 set AP, dut2 start mesh and connected with dut1 2. reboot dut1, dut2 disconnected with dut1 - 3. set self_organized(1,1) and check dut2 IDLE + 3. set dut2 self_organized(1,1) and check it IDLE initial condition: MESH_DEINIT_STA test environment: SSC_T2_MESH1 summary: set self_organized (1,1) to give up root state diff --git a/components/idf_test/integration_test/TC_IT_WIFI_CONN.yml b/components/idf_test/integration_test/TC_IT_WIFI_CONN.yml index 75984760b..8a9c23683 100644 --- a/components/idf_test/integration_test/TC_IT_WIFI_CONN.yml +++ b/components/idf_test/integration_test/TC_IT_WIFI_CONN.yml @@ -28,13 +28,13 @@ test cases: - - SSC SSC2 sta -C -s -p - - R SSC2 RE "\+JAP:CONNECTED,%%s"%%() - - SSC SSC1 ap -S -s -p -t 1 - - - R SSC1 C +SAP:OK + - - R SSC1 C +SAP:ERROR - - SSC SSC2 sta -D - - R SSC2 C +QAP:OK - - SSC SSC2 sta -S - - R SSC2 RE "\+SCAN:%%s,.+,0,\d+"%%() C +SCANDONE - - SSC SSC1 ap -S -s -p -t 5 - - - R SSC1 C +SAP:OK + - - R SSC1 C +SAP:ERROR - - SSC SSC2 sta -S - - R SSC2 RE "\+SCAN:%%s,.+,0,\d+"%%() C +SCANDONE execution time: 0.0 @@ -78,7 +78,7 @@ test cases: - - SSC SSC2 sta -C -s - - R SSC2 RE "\+JAP:CONNECTED,%%s"%%() - - SSC SSC1 ap -S -s -n 15 - - - R SSC1 C +SAP:OK + - - R SSC1 C +SAP:ERROR - - SSC SSC2 sta -C -s - - R SSC2 RE "\+JAP:CONNECTED,%%s"%%() - - SSC SSC2 sta -D diff --git a/components/idf_test/unit_test/InitialConditionAll.yml b/components/idf_test/unit_test/InitialConditionAll.yml deleted file mode 100644 index 6e5d898e7..000000000 --- a/components/idf_test/unit_test/InitialConditionAll.yml +++ /dev/null @@ -1,2955 +0,0 @@ -initial condition: -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - initial condition detail: AP mode, DHCP on, will autogen a TC with initial condition - APSTA1 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 31.0 - tag: APM1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 ap -L - - ['R SSC1 RE "\+LSTA:.+,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - initial condition detail: AP mode, PC join AP, DHCP on, will autogen a TC with initial - condition APSTA2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 38.0 - tag: APM2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - initial condition detail: AP mode, will NOT autogen a TC with initial condition - APSTA1 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 31.0 - tag: APO1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 ap -L - - ['R SSC1 RE "\+LSTA:.+,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - initial condition detail: AP mode, will NOT autogen a TC with initial condition - APSTA2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 38.0 - tag: APO2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 1 - - ['R SSC1 C BIN_ID,0'] - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - force restore cmd set: - - '' - - - SSC SSC1 upgrade -R -r 1 -s - - [R SSC1 NC ERROR C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SOC SOC1 ULISTEN - - [R SOC_COM L OK] - - - SOC SOC1 SETOPT REPLY BIN - - [R SOC_COM C OK] - - - SSC SSC1 upgrade -I -b 0 -f 0 - - ['P SSC1 C +UPGRADE:OK'] - - - SSC SSC1 upgrade -U -i -p -u - - ['P SSC1 C +UPGRADE:SUCCEED'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - initial condition detail: AP only mode, running BIN0 (located on flash id 0) - restore cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - restore post cmd set: - - '' - - - SSC SSC1 upgrade -D - - ['R SSC1 C +UPGRADE:OK'] - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 31.0 - tag: APOBIN0 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:3'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - initial condition detail: testing ap on sta + ap mode (autogen by APM1) - restore cmd set: - - '' - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 59.0 - tag: APSTA1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:3'] - - - SSC SSC1 ap -Q - - ['R SSC1 RE "\+APCONFIG:%%s,%%s,\d+,\d+,\d+,4,"%%(,)'] - - - SSC SSC1 ap -L - - ['R SSC1 RE "\+LSTA:.+,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - initial condition detail: testing ap on sta + ap mode, PC join AP (autogen by APM2) - restore cmd set: - - '' - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC1 ap -S -s -p -t - - ['R SSC1 C +SAP:OK'] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 66.0 - tag: APSTA2 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - DELAY 5 - - [''] - - - ATC AT1 CWSAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - initial condition detail: StationSoftAP mode - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 24.0 - tag: ATAP1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:3 L OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - DELAY 5 - - [''] - - - ATC AT1 CWSAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - initial condition detail: StationSoftAP mode, PC join Target AP, multi link, use - dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 R *] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 31.0 - tag: ATAP3 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:3 L OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - DELAY 10 - - [''] - - - ATC AT1 CWSAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - initial condition detail: StationSoftAP mode, PC join Target AP, single link, use - dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 45.0 - tag: ATAP4 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+RST - - [R AT1 L OK] - initial condition detail: StationSoftAP mode, both PC join Target AP, single link, - use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 3.0 - tag: ATAP5 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+RST - - [R AT1 L OK] - initial condition detail: StationSoftAP mode, both PC join Target AP, multi link, - use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 3.0 - tag: ATAP6 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:2'] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - initial condition detail: SoftAP mode, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 59.0 - tag: ATAPO1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:2 L OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATC AT1 CWSAP_DEF - - [R AT1 L OK] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - initial condition detail: SoftAP mode, PC join Target AP, multi link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 R *] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 66.0 - tag: ATAPO3 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:2 L OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATC AT1 CWSAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - - - WIFI CONN - - - ['R PC_COM NC ERROR C +WIFICONN:OK'] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - initial condition detail: SoftAP mode, PC join Target AP, single link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CWLIF - - [R AT1 P ] - - - ATS AT1 AT+CWDHCP_DEF=0,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 73.0 - tag: ATAPO4 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+RST - - [R AT1 L OK] - initial condition detail: SoftAP mode, both PC join Target AP, single link, use - dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 3.0 - tag: ATAPO5 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+RST - - [R AT1 L OK] - initial condition detail: SoftAP mode, both PC join Target AP, multi link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 3.0 - tag: ATAPO6 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - initial condition detail: StationSoftAP mode - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 87.0 - tag: ATAPSTA1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - initial condition detail: StationSoftAP mode, DHCP client on - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 87.0 - tag: ATAPSTA2 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CWDHCP_CUR? - - ['R AT1 C DHCP:3'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - initial condition detail: StationSoftAP mode, connected to AP, multi link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 94.0 - tag: ATAPSTA3 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CWDHCP_CUR? - - ['R AT1 C DHCP:3'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - initial condition detail: StationSoftAP mode, connected to AP, single link, use - dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CWDHCP_DEF=2,1 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 101.0 - tag: ATAPSTA4 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - initial condition detail: StationSoftAP mode, connected to AP, multi link, use static - ip - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 129.0 - tag: ATAPSTA5 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:3'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - initial condition detail: StationSoftAP mode, connected to AP, single link, use - static ip - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 136.0 - tag: ATAPSTA6 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT+RESTORE - - [R AT1 L OK, R AT1 C ready] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT+RESTORE - - [R AT1 L OK, R AT1 C ready] - initial condition detail: 'first time usage. Use restore function. ' - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+RESTORE - - [R AT1 L OK, R AT1 C ready] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 143.0 - tag: ATFTU - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT - - [R AT1 L OK] - - - ATS AT1 AT - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+RST - - [R AT1 L OK] - initial condition detail: none - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 3.0 - tag: ATNone - test script: InitCondBase -- check cmd set: - - '' - - - DELAY 0.1 - - [dummy] - force restore cmd set: - - '' - - - DELAY 0.1 - - [dummy] - initial condition detail: none 2 - restore cmd set: - - '' - - - DELAY 0.1 - - [dummy] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 108.0 - tag: ATNone2 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - initial condition detail: same as STA1, but will not autogen STA+AP STA test case - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 10.0 - tag: ATOSTA1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CWDHCP_CUR? - - ['R AT1 C DHCP:3'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - initial condition detail: same as STA4, but will not autogen STA+AP STA test case - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 17.0 - tag: ATOSTA4 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:3 C OK'] - - - ATS AT2 AT+CWMODE_CUR? - - ['R AT2 C +CWMODE_CUR:1 C OK'] - - - ATS AT1 AT+CWJAP_CUR? - - [R AT1 NC OK L ERROR] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=1 - - [R AT2 L OK] - - - ATS AT1 AT+CWQAP - - [R AT1 L OK] - initial condition detail: same as OT2_1, but will not autogen STA+AP STA test case - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=1 - - [R AT2 L OK] - - - ATS AT1 AT+CWQAP - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 52.0 - tag: ATOT2_1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - initial condition detail: station mode, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 10.0 - tag: ATSTA1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - initial condition detail: station mode, DHCP client on, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 10.0 - tag: ATSTA2 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CWDHCP_CUR? - - ['R AT1 C DHCP:3'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - initial condition detail: station mode, connected to AP, multi link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 38.0 - tag: ATSTA3 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CWDHCP_CUR? - - ['R AT1 C DHCP:3'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - initial condition detail: station mode, connected to AP, single link, use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATS AT1 AT+CWDHCP_DEF=1,1 - - [R AT1 R *] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 17.0 - tag: ATSTA4 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:1'] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - initial condition detail: station mode, connected to AP, multi link, use static - ip - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=1 - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 115.0 - tag: ATSTA5 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 L +CWMODE_CUR:1'] - - - ATS AT1 AT+CWJAP_CUR? - - ['R AT1 C +CWJAP_CUR:', R AT1 P ] - - - ATS AT1 AT+CIPMUX? - - ['R AT1 L +CIPMUX:0'] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - initial condition detail: station mode, connected to AP, single link, use static - ip - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=1 - - [R AT1 L OK] - - - ATC AT1 CWJAP_DEF - - [R AT1 L OK] - - - ATS AT1 AT+CIPSERVER=0 - - [R AT1 R *] - - - ATC AT1 CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMUX=0 - - [R AT1 L OK] - - - ATS AT1 AT+CIPCLOSE - - [R AT1 R *] - - - ATS AT1 AT+CIPMODE=0 - - [R AT1 R *] - - - ATC AT1 CIPSTA_DEF - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 122.0 - tag: ATSTA6 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:3 C OK'] - - - ATS AT2 AT+CWMODE_CUR? - - ['R AT2 C +CWMODE_CUR:1 C OK'] - - - ATS AT1 AT+CWJAP_CUR? - - [R AT1 NC OK L ERROR] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=1 - - [R AT2 L OK] - - - ATS AT1 AT+CWQAP - - [R AT1 L OK] - initial condition detail: Target 1 in StationSoftAP mode, Target 2 in station mode, - use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=3 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=1 - - [R AT2 L OK] - - - ATS AT1 AT+CWQAP - - [R AT1 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 52.0 - tag: ATT2_1 - test script: InitCondBase -- check cmd set: - - '' - - - ATS AT1 AT+CWMODE_CUR? - - ['R AT1 C +CWMODE_CUR:2 C OK'] - - - ATS AT2 AT+CWMODE_CUR? - - ['R AT2 C +CWMODE_CUR:3 C OK'] - - - ATS AT1 AT+CWJAP_CUR? - - [R AT1 NC OK L ERROR] - force restore cmd set: - - '' - - - ATS AT1 AT+RST - - [R AT1 C ready] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=3 - - [R AT2 L OK] - initial condition detail: Target 1 in SoftAP mode, Target 2 in StationSoftAP mode, - use dhcp - restore cmd set: - - '' - - - ATSO AT1 +++ - - [''] - - - ATS AT1 AT - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - ATS AT1 AT+CWMODE_DEF=2 - - [R AT1 L OK] - - - ATS AT2 AT+CWMODE_DEF=3 - - [R AT2 L OK] - restore post cmd set: - - '' - - - ATS AT1 AT+CWSTOPSMART - - [R AT1 R *] - - - ATS AT1 AT+SAVETRANSLINK=0 - - [R AT1 R *] - - - AT+SYSRAM - - ['R AT1 A :(\d+)'] - script path: InitCondBase.py - start: 80.0 - tag: ATT2_2 - test script: InitCondBase -- check cmd set: - - '' - - - ASSERT - - [dummy] - force restore cmd set: - - '' - - - SSC SSC[1-] reboot - - ['P SSC[1-] C !!!ready!!!'] - - - SSC SSC[1-] mesh -E -o 0 - - ['P SSC[1-] C +MESH:DISABLED'] - - - SSC SSC[1-] op -S -o 1 - - ['P SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] sta -D - - ['P SSC[1-] C +QAP:OK'] - initial condition detail: all mesh node disabled - restore cmd set: - - '' - - - SSC SSC[1-] mesh -E -o 0 - - ['P SSC[1-] C +MESH:DISABLED'] - - - SSC SSC[1-] op -S -o 1 - - ['P SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] sta -D - - ['P SSC[1-] C +QAP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 31.0 - tag: DISABLED - test script: InitCondBase -- check cmd set: - - '' - - - ASSERT - - [dummy] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - force restore cmd set: - - '' - - - SOC SOC1 LISTEN - - [R SOC_COM L OK] - - - SSC SSC[1-] mesh -E -o 0 - - ['P SSC[1-] C +MESH:DISABLED'] - - - SSC SSC[1-] mesh -I -g -a 4 -k -i - -p -h 5 - - ['P SSC[1-] C ENCRYPTION,OK C GROUP,OK C SERVER,OK C HOP,OK'] - - - SSC SSC[1-] mesh -A -s -k - - ['P SSC[1-] C +MESHINIT:AP,OK'] - - - SSC SSC1 mesh -E -o 1 -t 2 - - ['P SSC1 C +MESH:ENABLED'] - - - SOC SOC1 MACCEPT GSOC1 - - [R SOC_COM L OK] - - - SSC SSC[2-] mesh -E -o 1 -t 2 - - ['P SSC[2-] C +MESH:ENABLED'] - - - DELAY 60 - - [''] - - - SSC SSC[1-] mesh -C - - ['P SSC[1-] C +MESH:CONNECTED'] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - - - SSC SSC[1-] mesh -O -t 1 -o 1 - - ['P SSC[1-] C +MESH:OK'] - initial condition detail: all mesh node enabled as ONLINE, mesh network established - restore cmd set: - - '' - - - SSC SSC[1-] reboot - - ['P SSC[1-] C !!!ready!!!'] - - - SOC SOC1 LISTEN - - [R SOC_COM L OK] - - - SSC SSC[1-] mesh -E -o 0 - - ['P SSC[1-] C +MESH:DISABLED'] - - - SSC SSC[1-] mesh -I -g -a 4 -k -i - -p -h 5 - - ['P SSC[1-] C ENCRYPTION,OK C GROUP,OK C SERVER,OK C HOP,OK'] - - - SSC SSC[1-] mesh -A -s -k - - ['P SSC[1-] C +MESHINIT:AP,OK'] - - - SSC SSC1 mesh -E -o 1 -t 2 - - ['P SSC1 C +MESH:ENABLED'] - - - SOC SOC1 MACCEPT GSOC1 - - [R SOC_COM L OK] - - - SSC SSC[2-] mesh -E -o 1 -t 2 - - ['P SSC[2-] C +MESH:ENABLED'] - - - DELAY 60 - - [''] - - - SSC SSC[1-] mesh -C - - ['P SSC[1-] C +MESH:CONNECTED'] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - - - SSC SSC[1-] mesh -O -t 1 -o 1 - - ['P SSC[1-] C +MESH:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 17.0 - tag: ENABLED_1 - test script: InitCondBase -- check cmd set: - - '' - - - ASSERT - - [dummy] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - force restore cmd set: - - '' - - - SSC SSC[1-] reboot - - ['P SSC[1-] C !!!ready!!!'] - - - SSC SSC[1-] mesh -I -g -a 4 -k -i - -p -h 5 - - ['P SSC[1-] C ENCRYPTION,OK C GROUP,OK C SERVER,OK C HOP,OK'] - - - SSC SSC1 mesh -A -s -k - - ['P SSC1 C +MESHINIT:AP,OK'] - - - SSC SSC1 mesh -E -o 1 -t 1 - - ['P SSC1 C +MESH:ENABLED'] - - - SSC SSC[2-] mesh -E -o 1 -t 2 - - [''] - - - DELAY 60 - - ['P SSC[2-] C +MESH:ENABLED'] - - - SSC SSC[1-] mesh -C - - ['P SSC[1-] C +MESH:CONNECTED'] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - initial condition detail: root as LOCAL, rest node as ONLINE, mesh network established - restore cmd set: - - '' - - - SSC SSC[1-] mesh -E -o 0 - - ['P SSC[1-] C +MESH:DISABLED'] - - - SSC SSC[1-] mesh -I -g -a 4 -k -i - -p -h 5 - - ['P SSC[1-] C ENCRYPTION,OK C GROUP,OK C SERVER,OK C HOP,OK'] - - - SSC SSC1 mesh -A -s -k - - ['P SSC1 C +MESHINIT:AP,OK'] - - - SSC SSC1 mesh -E -o 1 -t 1 - - ['P SSC1 C +MESH:ENABLED'] - - - SSC SSC[2-] mesh -E -o 1 -t 2 - - [''] - - - DELAY 60 - - ['P SSC[2-] C +MESH:ENABLED'] - - - SSC SSC[1-] mesh -C - - ['P SSC[1-] C +MESH:CONNECTED'] - - - SSC SSC[1-] mesh -Q -t 4 - - ['R SSC[1-] T '] - - - MESHTREE - - ['R PC_COM RE "MESHTREE:%%s%20nodes"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 24.0 - tag: ENABLED_2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - - - SSC SSC1 espnow -D - - ['R SSC1 C +ESPNOW:'] - force restore cmd set: - - '' - - - SSC SSC[1-] reboot - - ['R SSC[1-] C !!!ready!!!'] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -m -o 2 - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 espnow -D - - ['R SSC1 C +ESPNOW:'] - initial condition detail: one target in AP mode and espnow is de-initialized - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 mac -S -m -o 2 - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC1 espnow -D - - ['R SSC1 C +ESPNOW:'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 17.0 - tag: NOW1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC[1-] op -Q - - ['R SSC[1-] C +CURMODE:2'] - - - SSC SSC[1-] mac -Q -o 3 - - ['R SSC[1-] P ]_ap_mac> P ]_mac>'] - - - SSC SSC[1-] espnow -D - - ['R SSC[1-] C +ESPNOW:'] - - - SSC SSC[1-] espnow -I - - ['R SSC[1-] C +ESPNOW:OK'] - - - SSC SSC[1-] espnow -R -t Set -r 2 - - ['R SSC[1-] C +ESPNOW:OK'] - force restore cmd set: - - '' - - - SSC SSC[1-] reboot - - ['R SSC[1-] C !!!ready!!!'] - - - SSC SSC[1-] op -S -o 3 - - ['R SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] mac -S -m ]_ap_mac> -o 2 - - ['R SSC[1-] C +MAC:AP,OK'] - - - SSC SSC[1-] mac -S -m ]_mac> -o 1 - - ['R SSC[1-] C +MAC:STA,OK'] - - - SSC SSC[1-] op -S -o 2 - - ['R SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] espnow -D - - ['R SSC[1-] C +ESPNOW:'] - - - SSC SSC[1-] espnow -I - - ['R SSC[1-] C +ESPNOW:OK'] - - - SSC SSC[1-] espnow -R -t Set -r 2 - - ['R SSC[1-] C +ESPNOW:OK'] - initial condition detail: multiple () targets in AP mode, espnow is initialized - with self role slave - restore cmd set: - - '' - - - SSC SSC[1-] op -S -o 3 - - ['R SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] mac -S -m ]_ap_mac> -o 2 - - ['R SSC[1-] C +MAC:AP,OK'] - - - SSC SSC[1-] mac -S -m ]_mac> -o 1 - - ['R SSC[1-] C +MAC:STA,OK'] - - - SSC SSC[1-] op -S -o 2 - - ['R SSC[1-] C +MODE:OK'] - - - SSC SSC[1-] espnow -D - - ['R SSC[1-] C +ESPNOW:'] - - - SSC SSC[1-] espnow -I - - ['R SSC[1-] C +ESPNOW:OK'] - - - SSC SSC[1-] espnow -R -t Set -r 2 - - ['R SSC[1-] C +ESPNOW:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 24.0 - tag: NOW2 - test script: InitCondBase -- check cmd set: - - '' - - - DELAY 0.1 - - [dummy] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - initial condition detail: none - restore cmd set: - - '' - - - DELAY 0.1 - - [dummy] - restore post cmd set: - - '' - - - DELAY 0.1 - - [dummy] - script path: InitCondBase.py - start: 10.0 - tag: None - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 sp -D - - ['R SSC1 C +SP:OK'] - force restore cmd set: - - '' - - - SSC SSC1 sp -D - - ['R SSC1 C +SP:OK'] - initial condition detail: one target and simple is de-inited - restore cmd set: - - '' - - - SSC SSC1 sp -D - - ['R SSC1 C +SP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 31.0 - tag: PAIR1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC[1,2] op -Q - - ['R SSC[1,2] C +MODE:[2,1]'] - - - SSC SSC[1,2] mac -Q -o 3 - - ['R SSC[1,2] P P '] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - force restore cmd set: - - '' - - - SSC SSC[1,2] reboot - - ['R SSC[1,2] C !!!ready!!!'] - - - SSC SSC[1,2] op -S -o 3 - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] mac -S -m -o 2 - - ['R SSC[1,2] C +MAC:AP,OK'] - - - SSC SSC[1,2] mac -S -m -o 1 - - ['R SSC[1,2] C +MAC:STA,OK'] - - - SSC SSC[1,2] op -S -o [2,1] - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - initial condition detail: target1 in AP mode, target2 in STA mode, two targets de-init - and init simple pair - restore cmd set: - - '' - - - SSC SSC[1,2] op -S -o 3 - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] mac -S -m -o 2 - - ['R SSC[1,2] C +MAC:AP,OK'] - - - SSC SSC[1,2] mac -S -m -o 1 - - ['R SSC[1,2] C +MAC:STA,OK'] - - - SSC SSC[1,2] op -S -o [2,1] - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 38.0 - tag: PAIR2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC[1,2] op -Q - - ['R SSC[1,2] C +MODE:[3,3]'] - - - SSC SSC[1,2] mac -Q -o 3 - - ['R SSC[1,2] P P '] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - force restore cmd set: - - '' - - - SSC SSC[1,2] reboot - - ['R SSC[1,2] C !!!ready!!!'] - - - SSC SSC[1,2] op -S -o [3,3] - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] mac -S -m -o 2 - - ['R SSC[1,2] C +MAC:AP,OK'] - - - SSC SSC[1,2] mac -S -m -o 1 - - ['R SSC[1,2] C +MAC:STA,OK'] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - initial condition detail: target1 and target2 in STA+AP mode, two targets de-init - and init simple pair - restore cmd set: - - '' - - - SSC SSC[1,2] op -S -o [3,3] - - ['R SSC[1,2] C +MODE:OK'] - - - SSC SSC[1,2] mac -S -m -o 2 - - ['R SSC[1,2] C +MAC:AP,OK'] - - - SSC SSC[1,2] mac -S -m -o 1 - - ['R SSC[1,2] C +MAC:STA,OK'] - - - SSC SSC[1,2] sp -D - - ['R SSC[1,2] C +SP:OK'] - - - SSC SSC[1,2] sp -I - - ['R SSC[1,2] C +SP:OK'] - restore post cmd set: - - '' - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 45.0 - tag: PAIR3 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:3'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - initial condition detail: testing sta on sta + ap mode, quit AP (autogen by STAM1) - restore cmd set: - - '' - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 45.0 - tag: STAAP1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:3'] - - - SSC SSC1 sta -Q - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - initial condition detail: testing sta on sta + ap mode, join AP, DHCP on (autogen - by STAM2) - restore cmd set: - - '' - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 52.0 - tag: STAAP2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 1 - - ['R SSC1 C BIN_ID,0'] - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - force restore cmd set: - - '' - - - SSC SSC1 upgrade -R -r 1 -s - - [R SSC1 NC ERROR C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SOC SOC1 ULISTEN - - [R SOC_COM L OK] - - - SOC SOC1 SETOPT REPLY BIN - - [R SOC_COM C OK] - - - SSC SSC1 upgrade -I -b 0 -f 0 - - ['P SSC1 C +UPGRADE:OK'] - - - SSC SSC1 upgrade -U -i -p -u - - ['P SSC1 C +UPGRADE:SUCCEED'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - initial condition detail: APSTA mode, connected to AP, running BIN0 (located on - flash id 0) - restore cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 upgrade -D - - ['R SSC1 C +UPGRADE:OK'] - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 24.0 - tag: STAAPBIN0 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:1'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - initial condition detail: sta mode, quit AP, DHCP on, will autogen a TC with initial - condition STAAP1 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 17.0 - tag: STAM1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:1'] - - - SSC SSC1 sta -Q - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - initial condition detail: sta mode, join AP, DHCP on, will autogen a TC with initial - condition STAAP2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 24.0 - tag: STAM2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 1 - - ['R SSC1 C BIN_ID,0'] - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - force restore cmd set: - - '' - - - SSC SSC1 upgrade -R -r 1 -s - - [R SSC1 NC ERROR C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SOC SOC1 ULISTEN - - [R SOC_COM L OK] - - - SOC SOC1 SETOPT REPLY BIN - - [R SOC_COM C OK] - - - SSC SSC1 upgrade -I -b 0 -f 0 - - ['P SSC1 C +UPGRADE:OK'] - - - SSC SSC1 upgrade -U -i -p -u - - ['P SSC1 C +UPGRADE:SUCCEED'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - initial condition detail: STA mode, connected to AP, running BIN0 (located on flash - id 0) - restore cmd set: - - '' - - - SSC SSC1 upgrade -Q -t 2 -b 0 - - ['R SSC1 C BIN_INFO,0'] - - - SSC SSC1 upgrade -R -b 0 - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 upgrade -D - - ['R SSC1 C +UPGRADE:OK'] - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 17.0 - tag: STAMBIN0 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:1'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - initial condition detail: sta mode, quit AP, will NOT autogen a TC with initial - condition STAAP1 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 sta -D - - ['R SSC1 C +QAP:'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 17.0 - tag: STAO1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:1'] - - - SSC SSC1 sta -Q - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - - - SSC SSC1 dhcp -Q -o 1 - - ['R SSC1 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 1 - - [R SSC1 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - initial condition detail: sta mode, join AP, DHCP on, will NOT autogen a TC with - initial condition STAAP2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 1 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC1 dhcp -S -o 1 - - [R SSC1 C +DHCP] - - - SSC SSC1 mac -S -o 1 -m - - ['R SSC1 C +MAC:STA,OK'] - - - SSC SSC1 sta -C -s -p - - ['R SSC1 RE "\+JAP:CONNECTED,%%s"%%()'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 24.0 - tag: STAO2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC2 op -Q - - ['R SSC2 C +CURMODE:1'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC2 dhcp -Q -o 1 - - ['R SSC2 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - - - SSC SSC2 mac -Q -o 1 - - [R SSC2 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC2 reboot - - [R SSC2 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 1 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - initial condition detail: same as T2_1 but will NOT autogen a TC with initial condition - T2_2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 1 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 73.0 - tag: T2O_1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:2'] - - - SSC SSC2 op -Q - - ['R SSC2 C +CURMODE:1'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC2 dhcp -Q -o 1 - - ['R SSC2 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - - - SSC SSC2 mac -Q -o 1 - - [R SSC2 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC2 reboot - - [R SSC2 C !!!ready!!!] - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 1 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - initial condition detail: target 1 as SoftAP, target 2 as STA, will autogen a TC - with initial condition T2_2 - restore cmd set: - - '' - - - SSC SSC1 op -S -o 2 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 1 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [''] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 73.0 - tag: T2_1 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC1 op -Q - - ['R SSC1 C +CURMODE:3'] - - - SSC SSC2 op -Q - - ['R SSC2 C +CURMODE:3'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [R SSC2 C +CLOSEALL] - - - SSC SSC1 dhcp -Q -o 2 - - ['R SSC1 C +DHCP:AP,STARTED'] - - - SSC SSC2 dhcp -Q -o 1 - - ['R SSC2 C +DHCP:STA,STARTED'] - - - SSC SSC1 mac -Q -o 2 - - [R SSC1 P ] - - - SSC SSC2 mac -Q -o 1 - - [R SSC2 P ] - force restore cmd set: - - '' - - - SSC SSC1 reboot - - [R SSC1 C !!!ready!!!] - - - SSC SSC2 reboot - - [R SSC2 C !!!ready!!!] - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 3 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [R SSC2 C +CLOSEALL] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - initial condition detail: target 1 as AP+STA, target 2 as AP+STA (autogen) - restore cmd set: - - '' - - - SSC SSC1 op -S -o 3 - - ['R SSC1 C +MODE:OK'] - - - SSC SSC2 op -S -o 3 - - ['R SSC2 C +MODE:OK'] - - - SSC SSC2 sta -D - - ['R SSC2 C +QAP:'] - - - SSC SSC2 soc -T - - [R SSC2 C +CLOSEALL] - - - SSC SSC1 dhcp -S -o 2 - - [R SSC1 C +DHCP] - - - SSC SSC2 dhcp -S -o 1 - - [R SSC2 C +DHCP] - - - SSC SSC1 mac -S -o 2 -m - - ['R SSC1 C +MAC:AP,OK'] - - - SSC SSC2 mac -S -o 1 -m - - ['R SSC2 C +MAC:STA,OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 ram - - ['R SSC1 C +FREEHEAP:'] - script path: InitCondBase.py - start: 80.0 - tag: T2_2 - test script: InitCondBase -- check cmd set: - - '' - - - SSC SSC[1-3] op -Q - - ['R SSC[1-3] C +CURMODE:3'] - - - SSC SSC[1-3] phy -Q -o 3 - - ['R SSC[1-3] C STA,n,40 C AP,n,40'] - force restore cmd set: - - '' - - - SSC SSC[1-3] reboot - - ['R SSC[1-3] C !!!ready!!!'] - - - SSC SSC[1-3] op -S -o 3 - - ['R SSC[1-3] C +MODE:OK'] - - - SSC SSC[1-3] phy -S -o 3 -m n -b 40 - - ['R SSC[1-3] C +PHY:OK'] - initial condition detail: '1. target 1 and target 2 set to AP+STA mode, target 3 - set to STA mode - - 2. all interface of target 2,3 set to 11n ht40 - - 3. config softAP of target 1 and target 2' - restore cmd set: - - '' - - - SSC SSC[1-3] op -S -o 3 - - ['R SSC[1-3] C +MODE:OK'] - - - SSC SSC[1-3] phy -S -o 3 -m n -b 40 - - ['R SSC[1-3] C +PHY:OK'] - restore post cmd set: - - '' - - - SSC SSC1 soc -T - - [R SSC1 C +CLOSEALL] - - - SSC SSC1 sta -R -r 1 - - [R SSC1 C OK] - - - SSC SSC1 ram - - ['R SSC1 A :(\d+)'] - script path: InitCondBase.py - start: 87.0 - tag: T3_PHY1 - test script: InitCondBase -- check cmd set: - - '' - - - FREBOOT UT1 - - ['R UT1 C Press%20ENTER%20to%20see%20the%20list%20of%20tests'] - force restore cmd set: - - '' - - - FREBOOT UT1 - - ['R UT1 C Press%20ENTER%20to%20see%20the%20list%20of%20tests'] - initial condition detail: At UT menu page - restore cmd set: - - '' - - - FREBOOT UT1 - - ['R UT1 C Press%20ENTER%20to%20see%20the%20list%20of%20tests'] - restore post cmd set: - - '' - - - DELAY 0.1 - - [''] - script path: InitCondBase.py - tag: UTINIT1 - test script: InitCondBase diff --git a/components/idf_test/unit_test/TestCaseScript/IDFUnitTest/__init__.py b/components/idf_test/unit_test/TestCaseScript/IDFUnitTest/__init__.py deleted file mode 100755 index 876a5d402..000000000 --- a/components/idf_test/unit_test/TestCaseScript/IDFUnitTest/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = ["UnitTest"] diff --git a/components/idf_test/unit_test/TestEnvAll.yml b/components/idf_test/unit_test/TestEnvAll.yml deleted file mode 100644 index a6054cf51..000000000 --- a/components/idf_test/unit_test/TestEnvAll.yml +++ /dev/null @@ -1,292 +0,0 @@ -test environment: -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_1, - test environment detail: 'PC has 2 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 1 AT target connect with PC by UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_2, - test environment detail: 'PC has 1 WiFi NIC. - - 1 AT target connect with PC by UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_3, - test environment detail: 'Able to access WAN after connect to AP. - - 1 AT target connect with PC by UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_ADC, - test environment detail: 'PC has 1 wired NIC connected to AP. - - Analog input connect to AT1 TOUT. - - Multimeter connect to input, able to measure input voltage. - - 1 AT target connect with PC by UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_APC1, - test environment detail: "PC has 1 wired NIC connected to AP.\nPC has 1 wired NIC\ - \ connected to APC (static IP within the same subnet with APC). \nAPC control\ - \ AP power supply. \nPC has 1 WiFi NIC. \n1 AT target connect with PC by UART\ - \ (AT and LOG port).", test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_APC2, - test environment detail: "Able to access WAN after connect to AP.\nPC has 1 wired\ - \ NIC connected to APC (static IP within the same subnet with APC). \nAPC control\ - \ AP power supply.\nPC has 1 WiFi NIC.\n1 AT target connect with PC by UART (AT\ - \ and LOG port).", test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_HighSpeedUART, - test environment detail: 'PC has 2 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 1 AT target connect with PC by high speed UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: AT_T1_SmartConfigIOT, - test environment detail: '1 AT target connect with PC by UART (AT and LOG port). - - PC has 1 wired NIC connect to Common AP. - - Several AP are placed near AT target. - - Several smart phone installed test APK are placed near AT target.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: AT_T2_1, - test environment detail: 'PC has 1 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 2 AT target connect with PC by UART (AT and LOG port).', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, script path: EnvBase.py, tag: AT_T2_JAP, - test environment detail: "Several AP are placed near AT target.\nPC has 1 wired\ - \ NIC connected to APC (static IP within the same subnet with APC).\nAPC control\ - \ power supply for all APs. \n2 AT target connect with PC by UART (AT and LOG\ - \ port).", test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, script path: EnvBase.py, tag: AT_T2_Sleep, - test environment detail: 'AP support DTIM placed with AT target. - - 2 AT target connect with PC by UART (AT and LOG port). - - Multimeter connect with PC via GPIB. - - Series multimeter between GND and VCC of AT1. - - AT1''s light sleep wakeup pin and wakeup indication connect with AT2''s GPIO.', - test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: AT_T2_SmartConfig, - test environment detail: '2 AT target connect with PC by UART (AT and LOG port). - - PC has 1 WiFi NIC. - - One HT20 AP and One HT40 AP are placed near target.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, UART ports: 'SSC1 - - SSC2', additional param list: '', basic param list: '', script path: EnvBase.py, - tag: IR_T2_1, test environment detail: '[TBD] 本测试为非自动测试, 红外能够做到数据收发吻合即可通过', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, script path: EnvBase.py, tag: NVS_T1_1, - test environment detail: '1 NVS target connect with PC by UART. - - 1 SSC target connect with PC by UART. - - SSC2 GPIO connect to NVS1 power control pin.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, UART ports: SSC_1, additional param list: '', - basic param list: '', script path: EnvBase.py, tag: PWM_T1_1, test environment detail: "[TBD]\ - \ 1. PWM OS SDK 以及 Non-OS SDK的测试建议分开进行, 放在不同的文件夹, 防止文件命名混淆\n2. 分析CSV文件的Python脚本只能分析单个channel\ - \ \n3. 如果Init脚本打印\"Network Error\" 检查TCP Server是不是正常发送data", test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_1, - test environment detail: 'PC has 2 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 1 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_2, - test environment detail: 'Able to access WAN after connect to AP. - - 1 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_8089, - test environment detail: 'PC has 1 wired NIC connected to AP. - - 1 8089 tablet able to run iperf test placed near SSC1. - - 1 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_ADC, - test environment detail: 'PC has 1 wired NIC connected to AP. - - Analog input connect to SSC1 TOUT. - - Multimeter connect to input, able to measure input voltage. - - 1 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_APC, - test environment detail: "PC has 1 wired NIC connected to AP.\nPC has 1 wired NIC\ - \ connected to APC (static IP within the same subnet with APC). \nAPC control\ - \ AP power supply. \nPC has 1 WiFi NIC. \n1 SSC target connect with PC by UART.", - test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_Enterprise, - test environment detail: "AP use WPA2-Etherprise is placed near SSC1. \n1 SSC target\ - \ connect with PC by UART.", test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_IOT1, - test environment detail: 'PC has 1 WiFi NIC. - - 1 SSC target connect with PC by UART. - - AP todo IOT test are placed near SSC1.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T1_InitData, - test environment detail: '2 SSC target connect with PC by UART. - - SSC1 use 40M crystal oscillator. - - SSC2 use normal 26M crystal oscillator. - - SSC2 GPIO connect to SSC1 power control pin.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_ShieldBox, - test environment detail: 'refer to figure. - - All APs and APC should be set to the same IP subnet. - - PC wired NIC should set static IP address within the same subnet with AP. - - Must use onboard wired NIC.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_Sleep1, - test environment detail: 'AP support DTIM placed with AT target. - - SSC target connect with Raspberry Pi by UART. - - Multimeter connect with Raspberry Pi via GPIB. - - Series multimeter between GND and VCC of SSC1. - - SSC1''s light sleep wakeup pin and wakeup indication connect with Raspberry Pi''s - GPIO. - - SSC1''s XPD connect with RSTB.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_Sleep2, - test environment detail: 'AP support DTIM placed with AT target. - - SSC target connect with Raspberry Pi by UART. - - Multimeter connect with Raspberry Pi via GPIB. - - Series multimeter between GND and VCC of SSC1. - - SSC1''s RSTB pin connect with Raspberry Pi''s GPIO.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_TempBox, - test environment detail: '1 SSC target connect with PC by UART. - - Put SSC target to temperature box.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, UART ports: SSC_1, additional param list: '', - basic param list: '', script path: EnvBase.py, tag: SSC_T1_Timer, test environment detail: '[TBD] - 通过串口工具调节Timer, 将GPIO_13端口连接到逻辑分析仪', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_VDD33, - test environment detail: '1 SSC target connect with PC by UART. - - Multimeter connect to VDD33, able to measure voltage.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T1_WEP, - test environment detail: '1 SSC target connect with PC by UART. - - One WEP share key AP placed near SSC1.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T2_1, - test environment detail: 'PC has 1 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 2 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, UART ports: 'SSC1 - - SSC2', additional param list: '', basic param list: '', script path: EnvBase.py, - tag: SSC_T2_GPIO1, test environment detail: '[TBD] 2个ESP_8266通过UART连到PC, ESP_8266的 - GPIO_6相连', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, UART ports: 'SSC1 - - SSC2', additional param list: '', basic param list: '', script path: EnvBase.py, - tag: SSC_T2_GPIO2, test environment detail: '[TBD] 1. 2个ESP_8266通过UART连到PC, ESP_8266的 - GPIO_15通过面包板相连 - - 2. 可借助面包板, 将GPIO_15, 以及中断函数被打开的8266板的GPIO_2 相连', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, UART ports: 'SSC1 - - SSC2', additional param list: '', basic param list: '', script path: EnvBase.py, - tag: SSC_T2_GPIO3, test environment detail: '[TBD] 2个ESP_8266通过UART连到PC, ESP_8266之间需要测试的Target_GPIO相连', - test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T2_JAP, - test environment detail: 'PC has 1 wired NIC connected to APC. - - APC control the power supply of multiple APs. - - 2 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T2_PhyMode, - test environment detail: '2 SSC target connect with PC by UART. - - PC has one WiFi NIC support capture wlan packet using libpcap. - - Set 4 AP with phy mode 11b, 11g, 11n HT20, 11n HT40. - - Put 4 APs near SSC targets.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T2_ShieldBox, - test environment detail: '2 SSC target connect with PC by UART. - - Put them to Shield box.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 2.0, script path: EnvBase.py, tag: SSC_T2_SmartConfig, - test environment detail: '2 SSC target connect with PC by UART. - - PC has 1 WiFi NIC. - - One HT20 AP and One HT40 AP are placed near target.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 3.0, script path: EnvBase.py, tag: SSC_T3_PhyMode, - test environment detail: '3 SSC target connect with PC by UART. - - PC has one WiFi NIC support capture wlan packet using libpcap. - - Set 4 AP with (HT20, channel1), (HT20, channel2), (HT40, channel1), (HT40, channel2). - - Put 4 APs near SSC targets.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 5.0, script path: EnvBase.py, tag: SSC_T5_1, - test environment detail: 5 SSC target connect with PC by UART., test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 5.0, script path: EnvBase.py, tag: SSC_T5_IOT1, - test environment detail: '5 SSC targets connect with PC by UART. - - some Android smart phone are placed near SSC targets.', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: SSC_T6_1, - test environment detail: 'PC has 1 wired NIC connected to AP. - - PC has 1 WiFi NIC. - - 6 SSC target connect with PC by UART.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: TempSensor_T1_1, - test environment detail: 'Tempeture sensor target connect with PC by UART. - - AP support DTIM placed with AT target. - - Multimeter connect with PC via GPIB. - - Series multimeter between GND and VCC of TempSensor1. - - PC has 1 wired NIC connected to switch. - - APC, AP also connect with swtich. - - All devices connected with switch use the same IP subnet. - - APC control AP power supply.', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, UART ports: SSC_1, additional param list: '', - basic param list: '', script path: EnvBase.py, tag: UART_T1_1, test environment detail: '[TBD] - 将ESP_8266通过UART连到PC', test script: EnvBase} -- {PC OS: '', Special: Y, Target Count: 1.0, UART ports: 'SSC1 - - SSC2', additional param list: '', basic param list: '', script path: EnvBase.py, - tag: UART_T1_2, test environment detail: '[TBD] ESP_8266通过UART_0通过USB, UART_1 TXD - 通过 TTLcable 连到PC', test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: UT_T1_1, - test environment detail: Environment for running ESP32 unit tests, test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: UT_T1_SDMODE, - test environment detail: Environment for running sd card sd mode unit tests, test script: EnvBase} -- {PC OS: '', Special: N, Target Count: 1.0, script path: EnvBase.py, tag: UT_T1_SPIMODE, - test environment detail: Environment for running sd card spi mode unit tests, test script: EnvBase} -- {PC OS: linux, Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: WebServer_T1_1, - test environment detail: 'Web Server target connect with PC by UART. - - PC has 1 wired NIC connected to switch. - - APC, AP also connect with swtich. - - All devices connected with switch use same IP subnet. - - APC control AP power supply.', test script: EnvBase} -- {PC OS: linux, Special: Y, Target Count: 1.0, script path: EnvBase.py, tag: WebServer_T1_2, - test environment detail: 'Web Server target connect with PC by UART. - - 4 PC with WiFi NIC placed near WebServer1.', test script: EnvBase} diff --git a/components/json/cJSON b/components/json/cJSON index 93688cbe7..3c8935676 160000 --- a/components/json/cJSON +++ b/components/json/cJSON @@ -1 +1 @@ -Subproject commit 93688cbe72b190300d1be6b98e86b772df9b9ead +Subproject commit 3c8935676a97c7c97bf006db8312875b4f292f6c diff --git a/components/libsodium/test/test_sodium.c b/components/libsodium/test/test_sodium.c index 7a9f096da..5ab4b0edc 100644 --- a/components/libsodium/test/test_sodium.c +++ b/components/libsodium/test/test_sodium.c @@ -39,7 +39,7 @@ TEST_CASE("box tests", "[libsodium]") extern int ed25519_convert_xmain(); -TEST_CASE("ed25519_convert tests", "[libsodium]") +TEST_CASE("ed25519_convert tests", "[libsodium][timeout=60]") { printf("Running ed25519_convert\n"); TEST_ASSERT_EQUAL(0, ed25519_convert_xmain() ); diff --git a/components/log/include/esp_log.h b/components/log/include/esp_log.h index e57aabbdd..8616c5610 100644 --- a/components/log/include/esp_log.h +++ b/components/log/include/esp_log.h @@ -108,6 +108,15 @@ void esp_log_write(esp_log_level_t level, const char* tag, const char* format, . /** @cond */ +/** + * @brief Write message into the log, va_list variant + * @see esp_log_write() + * + * This function is provided to ease integration toward other logging framework, + * so that esp_log can be used as a log sink. + */ +void esp_log_writev(esp_log_level_t level, const char* tag, const char* format, va_list args); + #include "esp_log_internal.h" #ifndef LOG_LOCAL_LEVEL diff --git a/components/log/log.c b/components/log/log.c index ea9eaf955..9e9e97e14 100644 --- a/components/log/log.c +++ b/components/log/log.c @@ -185,9 +185,10 @@ void clear_log_level_list() #endif } -void IRAM_ATTR esp_log_write(esp_log_level_t level, +void IRAM_ATTR esp_log_writev(esp_log_level_t level, const char* tag, - const char* format, ...) + const char* format, + va_list args) { if (!s_log_mutex) { s_log_mutex = xSemaphoreCreateMutex(); @@ -223,9 +224,16 @@ void IRAM_ATTR esp_log_write(esp_log_level_t level, return; } + (*s_log_print_func)(format, args); +} + +void esp_log_write(esp_log_level_t level, + const char *tag, + const char *format, ...) +{ va_list list; va_start(list, format); - (*s_log_print_func)(format, list); + esp_log_writev(level, tag, format, list); va_end(list); } @@ -352,7 +360,8 @@ uint32_t IRAM_ATTR esp_log_timestamp() if (base == 0 && xPortGetCoreID() == 0) { base = esp_log_early_timestamp(); } - return base + xTaskGetTickCount() * (1000 / configTICK_RATE_HZ); + TickType_t tick_count = xPortInIsrContext() ? xTaskGetTickCountFromISR() : xTaskGetTickCount(); + return base + tick_count * (1000 / configTICK_RATE_HZ); } #else diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index aa53a6fec..632d866ed 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -1,5 +1,6 @@ set(COMPONENT_ADD_INCLUDEDIRS include/apps + include/apps/sntp lwip/src/include port/esp32/include port/esp32/include/arch @@ -9,6 +10,7 @@ set(COMPONENT_ADD_INCLUDEDIRS set(COMPONENT_SRCS "apps/dhcpserver/dhcpserver.c" "apps/ping/esp_ping.c" "apps/ping/ping.c" + "apps/sntp/sntp.c" "lwip/src/api/api_lib.c" "lwip/src/api/api_msg.c" "lwip/src/api/err.c" diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 025305d95..b352985e5 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -16,6 +16,16 @@ menu "LWIP" Please make sure you fully understand the impact of this feature before enabling it. + config ETHARP_SUPPORT_VLAN + bool "Support receiving and sending ethernet packets with VLAN header" + default n + help + ETHARP_SUPPORT_VLAN==1: support receiving and sending ethernet packets with + VLAN header. See the description of LWIP_HOOK_VLAN_CHECK and LWIP_HOOK_VLAN_SET + hooks to check/set VLAN headers. + If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted. + If ETHARP_VLAN_CHECK is not defined, all traffic is accepted. + config LWIP_IRAM_OPTIMIZATION bool "Enable LWIP IRAM optimization" default n @@ -37,6 +47,14 @@ menu "LWIP" the maximum amount of sockets here. The valid value is from 1 to 16. + config LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS + bool "Randomize the local port for the first" + default y + help + LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS==1: randomize the local port for the first + local TCP/UDP pcb (default==0). This can prevent creating predictable port + numbers after booting a device. + config USE_ONLY_LWIP_SELECT bool "Support LWIP socket select() only" default n @@ -47,6 +65,20 @@ menu "LWIP" will be redirected to lwip_select(), therefore, select can be used for sockets only. + config LWIP_SO_LINGER + bool "Enable SO_LINGER processing" + default n + help + Enabling this option allows SO_LINGER processing. + l_onoff = 1,l_linger can set the timeout. + + If l_linger=0, When a connection is closed, TCP will terminate the connection. + This means that TCP will discard any data packets stored in the socket send buffer + and send an RST to the peer. + + If l_linger!=0,Then closesocket() calls to block the process until + the remaining data packets has been sent or timed out. + config LWIP_SO_REUSE bool "Enable SO_REUSEADDR option" default y @@ -74,18 +106,9 @@ menu "LWIP" help Enabling this option allows checking for available data on a netconn. - config LWIP_DHCP_MAX_NTP_SERVERS - int "Maximum number of NTP servers" - default 1 - range 1 16 - help - Set maximum number of NTP servers used by LwIP SNTP module. - First argument of sntp_setserver/sntp_setservername functions - is limited to this value. - config LWIP_IP_FRAG bool "Enable fragment outgoing IP packets" - default n + default y help Enabling this option allows fragmenting outgoing IP packets if their size exceeds MTU. @@ -232,6 +255,14 @@ menu "LWIP" If rate limiting self-assignment requests, wait this long between each request. + config LWIP_IPV6_AUTOCONFIG + bool "Enable IPV6 stateless address autoconfiguration" + default n + help + Enabling this option allows the devices to IPV6 stateless address autoconfiguration. + + See RFC 4862. + menuconfig LWIP_NETIF_LOOPBACK bool "Support per-interface loopback" default y @@ -295,12 +326,14 @@ menu "LWIP" config TCP_MSS int "Maximum Segment Size (MSS)" - default 1436 - range 1220 1436 + default 1440 + range 536 1436 help Set maximum segment size for TCP transmission. - Can be set lower to save RAM, the default value 1436 will give best throughput. + Can be set lower to save RAM, the default value 1460(ipv4)/1440(ipv6) will give best throughput. + IPv4 TCP_MSS Range: 576 <= TCP_MSS <= 1460 + IPv6 TCP_MSS Range: 1220<= TCP_MSS <= 1440 config TCP_MSL int "Maximum segment lifetime (MSL)" @@ -311,7 +344,8 @@ menu "LWIP" config TCP_SND_BUF_DEFAULT int "Default send buffer size" default 5744 # 4 * default MSS - range 2440 65535 + range 2440 65535 if !LWIP_WND_SCALE + range 2440 1024000 if LWIP_WND_SCALE help Set default send buffer size for new TCP sockets. @@ -327,7 +361,8 @@ menu "LWIP" config TCP_WND_DEFAULT int "Default receive window size" default 5744 # 4 * default MSS - range 2440 65535 + range 2440 65535 if !LWIP_WND_SCALE + range 2440 1024000 if LWIP_WND_SCALE help Set default TCP receive window size for new TCP sockets. @@ -560,4 +595,33 @@ menu "LWIP" endmenu # LWIP RAW API + menu "SNTP" + + config LWIP_DHCP_MAX_NTP_SERVERS + int "Maximum number of NTP servers" + default 1 + range 1 16 + help + Set maximum number of NTP servers used by LwIP SNTP module. + First argument of sntp_setserver/sntp_setservername functions + is limited to this value. + + config LWIP_SNTP_UPDATE_DELAY + int "Request interval to update time (ms)" + range 15000 4294967295 + default 3600000 + help + This option allows you to set the time update period via SNTP. + Default is 1 hour. Must not be below 15 seconds by specification. + (SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds). + + endmenu # SNTP + + config LWIP_ESP_LWIP_ASSERT + bool "Enable LWIP ASSERT checks" + default y + help + Enable this option allows lwip to check assert. + It is recommended to keep it open, do not close it. + endmenu diff --git a/components/lwip/apps/sntp/sntp.c b/components/lwip/apps/sntp/sntp.c new file mode 100644 index 000000000..58b84f5a7 --- /dev/null +++ b/components/lwip/apps/sntp/sntp.c @@ -0,0 +1,93 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include "esp_log.h" +#include "sntp.h" + +static const char *TAG = "sntp"; + +static volatile sntp_sync_mode_t sntp_sync_mode = SNTP_SYNC_MODE_IMMED; +static volatile sntp_sync_status_t sntp_sync_status = SNTP_SYNC_STATUS_RESET; +static sntp_sync_time_cb_t time_sync_notification_cb = NULL; + +inline void sntp_set_sync_status(sntp_sync_status_t sync_status) +{ + sntp_sync_status = sync_status; +} + +void __attribute__((weak)) sntp_sync_time(struct timeval *tv) +{ + if (sntp_sync_mode == SNTP_SYNC_MODE_IMMED) { + settimeofday(tv, NULL); + sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED); + } else if (sntp_sync_mode == SNTP_SYNC_MODE_SMOOTH) { + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + int64_t cpu_time = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec; + int64_t sntp_time = (int64_t)tv->tv_sec * 1000000L + (int64_t)tv->tv_usec; + int64_t delta = sntp_time - cpu_time; + struct timeval tv_delta = { .tv_sec = delta / 1000000L, .tv_usec = delta % 1000000L }; + if (adjtime(&tv_delta, NULL) == -1) { + ESP_LOGD(TAG, "Function adjtime don't update time because the error is very big"); + settimeofday(tv, NULL); + ESP_LOGD(TAG, "Time was synchronized through settimeofday"); + sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED); + } else { + sntp_set_sync_status(SNTP_SYNC_STATUS_IN_PROGRESS); + } + } + if (time_sync_notification_cb) { + time_sync_notification_cb(tv); + } +} + +void sntp_set_sync_mode(sntp_sync_mode_t sync_mode) +{ + sntp_sync_mode = sync_mode; +} + +sntp_sync_mode_t sntp_get_sync_mode(void) +{ + return sntp_sync_mode; +} + +// set a callback function for time synchronization notification +void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback) +{ + time_sync_notification_cb = callback; +} + +sntp_sync_status_t sntp_get_sync_status(void) +{ + sntp_sync_status_t ret_sync_status = SNTP_SYNC_STATUS_RESET; + sntp_sync_status_t sync_status = sntp_sync_status; + if (sync_status == SNTP_SYNC_STATUS_COMPLETED) { + sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); + ret_sync_status = SNTP_SYNC_STATUS_COMPLETED; + } else if (sync_status == SNTP_SYNC_STATUS_IN_PROGRESS) { + struct timeval outdelta; + adjtime(NULL, &outdelta); + if (outdelta.tv_sec == 0 && outdelta.tv_usec == 0) { + sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); + ret_sync_status = SNTP_SYNC_STATUS_COMPLETED; + } else { + ret_sync_status = SNTP_SYNC_STATUS_IN_PROGRESS; + } + } + return ret_sync_status; +} diff --git a/components/lwip/component.mk b/components/lwip/component.mk index 45a083d50..f43250baa 100644 --- a/components/lwip/component.mk +++ b/components/lwip/component.mk @@ -5,6 +5,7 @@ COMPONENT_SUBMODULES += lwip COMPONENT_ADD_INCLUDEDIRS := \ include/apps \ + include/apps/sntp \ lwip/src/include \ port/esp32/include \ port/esp32/include/arch \ @@ -13,6 +14,7 @@ COMPONENT_ADD_INCLUDEDIRS := \ COMPONENT_SRCDIRS := \ apps/dhcpserver \ apps/ping \ + apps/sntp \ lwip/src/api \ lwip/src/apps/sntp \ lwip/src/core \ diff --git a/components/lwip/include/apps/esp_sntp.h b/components/lwip/include/apps/esp_sntp.h new file mode 100644 index 000000000..020f0cf36 --- /dev/null +++ b/components/lwip/include/apps/esp_sntp.h @@ -0,0 +1,22 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef __ESP_SNTP_H__ +#define __ESP_SNTP_H__ + +#include "lwip/err.h" +#include "lwip/apps/sntp.h" +#include "sntp.h" + +#endif // __ESP_SNTP_H__ diff --git a/components/lwip/include/apps/sntp/sntp.h b/components/lwip/include/apps/sntp/sntp.h new file mode 100644 index 000000000..a94981b5d --- /dev/null +++ b/components/lwip/include/apps/sntp/sntp.h @@ -0,0 +1,127 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef __SNTP_H__ +#define __SNTP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * The time update takes place in the sntp_sync_time() function. + * The user has the ability to redefine this function in order + * to re-define its functionality. This function has two time update modes, + * which can be set via the sntp_set_sync_mode() function. + * Two modes are available: + * - the first is an immediate update when receiving time from the sntp server, + * - the second is a smooth time update (if the time error is no more than 35 minutes, + * and an immediate update if the error is more than 35 minutes). + * + * To receive notification of time synchronization, + * you can use the callback function or get the synchronization status + * via the sntp_get_sync_status() function. + * + * To determine the time synchronization time on the device, you can use: + * 1) sntp_set_time_sync_notification_cb() function to set the callback function, + * which is convenient to use to receive notification of the update time. + * 2) sntp_get_sync_status() function for getting time synchronization status. + * After the time synchronization is completed, the status will be + * SNTP_SYNC_STATUS_COMPLETED, after, it will be reseted to SNTP_SYNC_STATUS_RESET + * to wait for the next sync cycle. + */ + +/// SNTP time update mode +typedef enum { + SNTP_SYNC_MODE_IMMED, /*!< Update system time immediately when receiving a response from the SNTP server. */ + SNTP_SYNC_MODE_SMOOTH, /*!< Smooth time updating. Time error is gradually reduced using adjtime function. If the difference between SNTP response time and system time is large (more than 35 minutes) then update immediately. */ +} sntp_sync_mode_t; + +/// SNTP sync status +typedef enum { + SNTP_SYNC_STATUS_RESET, // Reset status. + SNTP_SYNC_STATUS_COMPLETED, // Time is synchronized. + SNTP_SYNC_STATUS_IN_PROGRESS, // Smooth time sync in progress. +} sntp_sync_status_t; + +/** + * @brief SNTP callback function for notifying about time sync event + * + * @param tv Time received from SNTP server. + */ +typedef void (*sntp_sync_time_cb_t) (struct timeval *tv); + +/** + * @brief This function updates the system time. + * + * This is a weak-linked function. It is possible to replace all SNTP update functionality + * by placing a sntp_sync_time() function in the app firmware source. + * If the default implementation is used, calling sntp_set_sync_mode() allows + * the time synchronization mode to be changed to instant or smooth. + * If a callback function is registered via sntp_set_time_sync_notification_cb(), + * it will be called following time synchronization. + * + * @param tv Time received from SNTP server. + */ +void sntp_sync_time(struct timeval *tv); + +/** + * @brief Set the sync mode + * + * Allowable two mode: SNTP_SYNC_MODE_IMMED and SNTP_SYNC_MODE_SMOOTH. + * @param sync_mode Sync mode. + */ +void sntp_set_sync_mode(sntp_sync_mode_t sync_mode); + +/** + * @brief Get set sync mode + * + * @return SNTP_SYNC_MODE_IMMED: Update time immediately. + * SNTP_SYNC_MODE_SMOOTH: Smooth time updating. + */ +sntp_sync_mode_t sntp_get_sync_mode(void); + +/** + * @brief Get status of time sync + * + * After the update is completed, the status will be returned as SNTP_SYNC_STATUS_COMPLETED. + * After that, the status will be reset to SNTP_SYNC_STATUS_RESET. + * If the update operation is not completed yet, the status will be SNTP_SYNC_STATUS_RESET. + * If a smooth mode was chosen and the synchronization is still continuing (adjtime works), then it will be SNTP_SYNC_STATUS_IN_PROGRESS. + * + * @return SNTP_SYNC_STATUS_RESET: Reset status. + * SNTP_SYNC_STATUS_COMPLETED: Time is synchronized. + * SNTP_SYNC_STATUS_IN_PROGRESS: Smooth time sync in progress. + */ +sntp_sync_status_t sntp_get_sync_status(void); + +/** + * @brief Set status of time sync + * + * @param sync_status status of time sync (see sntp_sync_status_t) + */ +void sntp_set_sync_status(sntp_sync_status_t sync_status); + +/** + * @brief Set a callback function for time synchronization notification + * + * @param callback a callback function + */ +void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback); + +#ifdef __cplusplus +} +#endif + +#endif // __SNTP_H__ diff --git a/components/lwip/include_compat/apps/sntp/sntp.h b/components/lwip/include_compat/apps/sntp/sntp.h index 3db0fba1e..7758cfa7d 100644 --- a/components/lwip/include_compat/apps/sntp/sntp.h +++ b/components/lwip/include_compat/apps/sntp/sntp.h @@ -1,3 +1,3 @@ #pragma once -#warning "This header file is deprecated, please include lwip/apps/sntp.h instead." -#include "lwip/apps/sntp.h" +#warning "This header file is deprecated, please include esp_sntp.h instead." +#include "esp_sntp.h" diff --git a/components/lwip/lwip b/components/lwip/lwip index 3ed39f279..453b291c4 160000 --- a/components/lwip/lwip +++ b/components/lwip/lwip @@ -1 +1 @@ -Subproject commit 3ed39f27981e7738c0a454f9e83b8e5164b7078b +Subproject commit 453b291c4c0752e05b585a62d32d8bb23e1538d7 diff --git a/components/lwip/port/esp32/freertos/sys_arch.c b/components/lwip/port/esp32/freertos/sys_arch.c index 1168b9e89..1f98eee71 100644 --- a/components/lwip/port/esp32/freertos/sys_arch.c +++ b/components/lwip/port/esp32/freertos/sys_arch.c @@ -426,9 +426,11 @@ sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize void sys_init(void) { + if (!g_lwip_protect_mutex) { if (ERR_OK != sys_mutex_new(&g_lwip_protect_mutex)) { - ESP_LOGE(TAG, "sys_init: failed to init lwip protect mutex\n"); + ESP_LOGE(TAG, "sys_init: failed to init lwip protect mutex\n"); } + } // Create the pthreads key for the per-thread semaphore storage pthread_key_create(&sys_thread_sem_key, sys_thread_sem_free); @@ -466,6 +468,9 @@ sys_now(void) sys_prot_t sys_arch_protect(void) { + if (!g_lwip_protect_mutex) { + sys_mutex_new(&g_lwip_protect_mutex); + } sys_mutex_lock(&g_lwip_protect_mutex); return (sys_prot_t) 1; } diff --git a/components/lwip/port/esp32/include/arch/cc.h b/components/lwip/port/esp32/include/arch/cc.h index cba0b365e..b3ff59bc4 100644 --- a/components/lwip/port/esp32/include/arch/cc.h +++ b/components/lwip/port/esp32/include/arch/cc.h @@ -43,6 +43,10 @@ #define BYTE_ORDER LITTLE_ENDIAN +#ifndef CONFIG_LWIP_ESP_LWIP_ASSERT +#define LWIP_NOASSERT 1 +#endif + typedef uint8_t u8_t; typedef int8_t s8_t; typedef uint16_t u16_t; diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 883abe557..a458fc487 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -43,7 +43,7 @@ #include "esp_task.h" #include "esp_system.h" #include "sdkconfig.h" - +#include "sntp.h" #include "netif/dhcp_state.h" /* Enable all Espressif-only options */ @@ -525,6 +525,13 @@ */ #define LWIP_SO_SNDTIMEO 1 +/** + * LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS==1: randomize the local port for the first + * local TCP/UDP pcb (default==0). This can prevent creating predictable port + * numbers after booting a device. + */ +#define LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS CONFIG_LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS + /** * LWIP_SO_RCVTIMEO==1: Enable receive timeout for sockets/netconns and * SO_RCVTIMEO processing. @@ -538,6 +545,11 @@ */ #define LWIP_TCP_KEEPALIVE 1 +/** + * LWIP_SO_LINGER==1: Enable SO_LINGER processing. + */ +#define LWIP_SO_LINGER CONFIG_LWIP_SO_LINGER + /** * LWIP_SO_RCVBUF==1: Enable SO_RCVBUF processing. */ @@ -653,6 +665,13 @@ */ #define LWIP_IPV6 1 +/** + * LWIP_ND6_RDNSS_MAX_DNS_SERVERS: Allow IPv6 DNS servers to be retrieved from + * NDP, up to the maximum number of allowed DNS servers (minus fallback slot) + */ +#define LWIP_ND6_RDNSS_MAX_DNS_SERVERS 0 + + /* --------------------------------------- ---------- Hook options --------------- @@ -731,6 +750,17 @@ */ #define ETHARP_TRUST_IP_MAC CONFIG_LWIP_ETHARP_TRUST_IP_MAC +/** + * ETHARP_SUPPORT_VLAN==1: support receiving and sending ethernet packets with + * VLAN header. See the description of LWIP_HOOK_VLAN_CHECK and + * LWIP_HOOK_VLAN_SET hooks to check/set VLAN headers. + * Additionally, you can define ETHARP_VLAN_CHECK to an u16_t VLAN ID to check. + * If ETHARP_VLAN_CHECK is defined, only VLAN-traffic for this VLAN is accepted. + * If ETHARP_VLAN_CHECK is not defined, all traffic is accepted. + * Alternatively, define a function/define ETHARP_VLAN_CHECK_FN(eth_hdr, vlan) + * that returns 1 to accept a packet or 0 to drop a packet. + */ +#define ETHARP_SUPPORT_VLAN CONFIG_ETHARP_SUPPORT_VLAN /** * POSIX I/O functions are mapped to LWIP via the VFS layer @@ -752,14 +782,13 @@ #define ESP_LWIP 1 #define ESP_LWIP_ARP 1 +#define ESP_IPV6 1 #define ESP_PER_SOC_TCP_WND 0 #define ESP_THREAD_SAFE 1 #define ESP_THREAD_SAFE_DEBUG LWIP_DBG_OFF #define ESP_DHCP 1 #define ESP_DNS 1 -#define ESP_IPV6_AUTOCONFIG 1 #define ESP_PERF 0 -#define ESP_RANDOM_TCP_PORT 1 #define ESP_IP4_ATON 1 #define ESP_LIGHT_SLEEP 1 #define ESP_L2_TO_L3_COPY CONFIG_L2_TO_L3_COPY @@ -774,6 +803,10 @@ #define ESP_AUTO_RECV 1 #define ESP_GRATUITOUS_ARP CONFIG_ESP_GRATUITOUS_ARP +#ifdef CONFIG_LWIP_IPV6_AUTOCONFIG +#define ESP_IPV6_AUTOCONFIG CONFIG_LWIP_IPV6_AUTOCONFIG +#endif + #ifdef ESP_IRAM_ATTR #undef ESP_IRAM_ATTR #endif @@ -833,10 +866,20 @@ enum { #define LWIP_DHCP_MAX_NTP_SERVERS CONFIG_LWIP_DHCP_MAX_NTP_SERVERS #define LWIP_TIMEVAL_PRIVATE 0 +/* + -------------------------------------- + ------------ SNTP options ------------ + -------------------------------------- +*/ +/* + * SNTP update delay - in milliseconds + */ +#define SNTP_UPDATE_DELAY CONFIG_LWIP_SNTP_UPDATE_DELAY + #define SNTP_SET_SYSTEM_TIME_US(sec, us) \ do { \ struct timeval tv = { .tv_sec = sec, .tv_usec = us }; \ - settimeofday(&tv, NULL); \ + sntp_sync_time(&tv); \ } while (0); #define SNTP_GET_SYSTEM_TIME(sec, us) \ @@ -845,6 +888,7 @@ enum { gettimeofday(&tv, NULL); \ (sec) = tv.tv_sec; \ (us) = tv.tv_usec; \ + sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); \ } while (0); #define SOC_SEND_LOG //printf diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index 7c60b6934..ed5e13ed1 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -81,6 +81,12 @@ low_level_init(struct netif *netif) #endif #endif +#if ESP_IPV6 +#if LWIP_IPV6 && LWIP_IPV6_MLD + netif->flags |= NETIF_FLAG_MLD6; +#endif +#endif + #if !ESP_L2_TO_L3_COPY netif->l2_buffer_free_notify = esp_wifi_internal_free_rx_buffer; #endif diff --git a/components/mbedtls/Kconfig b/components/mbedtls/Kconfig index 4ba018022..681e3a719 100644 --- a/components/mbedtls/Kconfig +++ b/components/mbedtls/Kconfig @@ -92,6 +92,19 @@ menu "mbedTLS" at runtime in order to enable mbedTLS debug output via the ESP log mechanism. + config MBEDTLS_ECP_RESTARTABLE + bool "Enable mbedTLS ecp restartable" + default n + help + Enable "non-blocking" ECC operations that can return early and be resumed. + + config MBEDTLS_CMAC_C + bool "Enable CMAC mode for block ciphers" + default n + help + Enable the CMAC (Cipher-based Message Authentication Code) mode for + block ciphers. + config MBEDTLS_HARDWARE_AES bool "Enable hardware AES acceleration" default y diff --git a/components/mbedtls/mbedtls b/components/mbedtls/mbedtls index 97959e779..9ef92c551 160000 --- a/components/mbedtls/mbedtls +++ b/components/mbedtls/mbedtls @@ -1 +1 @@ -Subproject commit 97959e77912524bd8db7cbb2e00fc9f6189f7a82 +Subproject commit 9ef92c551eb8d92677034c3ec8078a8076febf41 diff --git a/components/mbedtls/port/esp_bignum.c b/components/mbedtls/port/esp_bignum.c index 275adad6d..3e2a114c8 100644 --- a/components/mbedtls/port/esp_bignum.c +++ b/components/mbedtls/port/esp_bignum.c @@ -509,6 +509,9 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi return ret; } + /* Grow Z to result size early, avoid interim allocations */ + MBEDTLS_MPI_CHK( mbedtls_mpi_grow(Z, z_words) ); + /* If either factor is over 2048 bits, we can't use the standard hardware multiplier (it assumes result is double longest factor, and result is max 4096 bits.) @@ -553,8 +556,6 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi start_op(RSA_MULT_START_REG); - MBEDTLS_MPI_CHK( mbedtls_mpi_grow(Z, z_words) ); - wait_op_complete(RSA_MULT_START_REG); /* Read back the result */ @@ -661,9 +662,6 @@ static int mpi_mult_mpi_overlong(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbe }; mbedtls_mpi_init(&Ztemp); - /* Grow Z to result size early, avoid interim allocations */ - mbedtls_mpi_grow(Z, z_words); - /* Get result Ztemp = Yp * X (need temporary variable Ztemp) */ MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi(&Ztemp, X, &Yp) ); diff --git a/components/mbedtls/port/include/mbedtls/esp_config.h b/components/mbedtls/port/include/mbedtls/esp_config.h index 89cdef892..85f2abe37 100644 --- a/components/mbedtls/port/include/mbedtls/esp_config.h +++ b/components/mbedtls/port/include/mbedtls/esp_config.h @@ -448,6 +448,47 @@ #define MBEDTLS_REMOVE_ARC4_CIPHERSUITES #endif +/** + * \def MBEDTLS_ECP_RESTARTABLE + * + * Enable "non-blocking" ECC operations that can return early and be resumed. + * + * This allows various functions to pause by returning + * #MBEDTLS_ERR_ECP_IN_PROGRESS (or, for functions in the SSL module, + * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) and then be called later again in + * order to further progress and eventually complete their operation. This is + * controlled through mbedtls_ecp_set_max_ops() which limits the maximum + * number of ECC operations a function may perform before pausing; see + * mbedtls_ecp_set_max_ops() for more information. + * + * This is useful in non-threaded environments if you want to avoid blocking + * for too long on ECC (and, hence, X.509 or SSL/TLS) operations. + * + * Uncomment this macro to enable restartable ECC computations. + * + * \note This option only works with the default software implementation of + * elliptic curve functionality. It is incompatible with + * MBEDTLS_ECP_ALT, MBEDTLS_ECDH_XXX_ALT and MBEDTLS_ECDSA_XXX_ALT. + */ +#ifdef CONFIG_MBEDTLS_ECP_RESTARTABLE +#define MBEDTLS_ECP_RESTARTABLE +#endif + +/** + * \def MBEDTLS_CMAC_C + * + * Enable the CMAC (Cipher-based Message Authentication Code) mode for block + * ciphers. + * + * Module: library/cmac.c + * + * Requires: MBEDTLS_AES_C or MBEDTLS_DES_C + * + */ +#ifdef CONFIG_MBEDTLS_CMAC_C +#define MBEDTLS_CMAC_C +#endif + /** * \def MBEDTLS_ECP_DP_SECP192R1_ENABLED * diff --git a/components/mbedtls/port/net_sockets.c b/components/mbedtls/port/net_sockets.c index 6d8a1cc55..94a1569e1 100644 --- a/components/mbedtls/port/net_sockets.c +++ b/components/mbedtls/port/net_sockets.c @@ -212,13 +212,6 @@ static int net_would_block( const mbedtls_net_context *ctx, int *errout ) *errout = error; } - /* - * Never return 'WOULD BLOCK' on a non-blocking socket - */ - if ( ( fcntl( ctx->fd, F_GETFL, 0) & O_NONBLOCK ) != O_NONBLOCK ) { - return ( 0 ); - } - switch ( error ) { #if defined EAGAIN case EAGAIN: diff --git a/components/mbedtls/test/test_mbedtls_mpi.c b/components/mbedtls/test/test_mbedtls_mpi.c index 084fe3484..ed394e6bf 100644 --- a/components/mbedtls/test/test_mbedtls_mpi.c +++ b/components/mbedtls/test/test_mbedtls_mpi.c @@ -18,7 +18,7 @@ */ void mbedtls_mpi_printf(const char *name, const mbedtls_mpi *X) { - static char buf[1024]; + static char buf[2048]; size_t n; memset(buf, 0, sizeof(buf)); mbedtls_mpi_write_string(X, 16, buf, sizeof(buf)-1, &n); @@ -29,11 +29,15 @@ void mbedtls_mpi_printf(const char *name, const mbedtls_mpi *X) } } -/* Assert E = A * B */ -static void test_bignum_mult(const char *a_str, const char *b_str, const char *e_str, size_t mod_bits) +/* + Assert E == X, X=A*B if res_operands_overlap==0 + Assert E == A, A=A*B if res_operands_overlap==1 + Assert E == B, B=A*B if res_operands_overlap==2 +*/ +static void test_bignum_mult_variant(const char *a_str, const char *b_str, const char *e_str, size_t mod_bits, int res_operands_overlap) { mbedtls_mpi A, B, X, E, M; - char x_buf[1024] = { 0 }; + char x_buf[2048] = {0}; size_t x_buf_len = 0; mbedtls_mpi_init(&A); @@ -44,9 +48,17 @@ static void test_bignum_mult(const char *a_str, const char *b_str, const char *e TEST_ASSERT_FALSE(mbedtls_mpi_read_string(&A, 16, a_str)); TEST_ASSERT_FALSE(mbedtls_mpi_read_string(&B, 16, b_str)); - /* E = A * B */ + /* calulate X = A * B variant */ TEST_ASSERT_FALSE(mbedtls_mpi_read_string(&E, 16, e_str)); - TEST_ASSERT_FALSE(mbedtls_mpi_mul_mpi(&X, &A, &B)); + if (res_operands_overlap == 0) { + TEST_ASSERT_FALSE(mbedtls_mpi_mul_mpi(&X, &A, &B)); + } else if (res_operands_overlap == 1) { + mbedtls_mpi_copy( &X, &A ); + TEST_ASSERT_FALSE(mbedtls_mpi_mul_mpi(&X, &X, &B)); + } else if (res_operands_overlap == 2) { + mbedtls_mpi_copy( &X, &B ); + TEST_ASSERT_FALSE(mbedtls_mpi_mul_mpi(&X, &A, &X)); + } mbedtls_mpi_write_string(&X, 16, x_buf, sizeof(x_buf)-1, &x_buf_len); TEST_ASSERT_EQUAL_STRING_MESSAGE(e_str, x_buf, "mbedtls_mpi_mul_mpi result wrong"); @@ -73,6 +85,15 @@ static void test_bignum_mult(const char *a_str, const char *b_str, const char *e mbedtls_mpi_free(&E); } +/* Assert E = A * B, including 3 variants: X=A*B A*=B, B*=A */ +static void test_bignum_mult(const char *a_str, const char *b_str, const char *e_str, size_t mod_bits) +{ + for (int overlap_operands=0; overlap_operands < 3; ++overlap_operands) { + test_bignum_mult_variant(a_str, b_str, e_str, mod_bits, overlap_operands); + } +} + + TEST_CASE("test MPI multiplication", "[bignum]") { /* Run some trivial numbers tests w/ various high modulo bit counts, @@ -115,6 +136,13 @@ TEST_CASE("test MPI multiplication", "[bignum]") "390587293875124938ABBECD0EEEEE3333333333333333333333333333333399999888000AAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBB00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000EDFABC0204048975876873487387478327482374871327482347328742837483247283748234723874238478327400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003012111111111111111100000000000000000000000111111111111111111111111", "02603AF70D0421C1AD82CE623F28F70B128118D06D00C27D433EC25BA86E6105C3890A0B1973B8BE068CA68E159A21078785DDB37F94216FBF4AEC939958AF4B8CEA2A48895CECA87562FC846EAAE0C866AF9D41EEABFB1D579F5828E9666A15E2AF946F16A189B5C645872FDCA247D309AB0BCAFB0D112881186FCFFEDC87061B4AE4A375E9BBCF579A7BC87A8EAC8C6F66E107986FC603F920F5E1A0FD8C619D88D90066FFFC8F4DB77437EBD7E3BD7E398C4C01F93426E347E039DCA7B0A73C0C90A9C4271BB761ADFF88971D190CE5DA98EFC5D7390D33BC034908AF81D784A4D7F32D0902E0C5DABC706635D5A28FC0E3A364EDEB21E8E117041D0E4B51CA6F9684F434057E7FCF2AF6BD050334B1D11E043B0967154E57354B681161D3C618974D5A7E0385755B80B931AE9B59DD4402BAEC206F04B8440741B3C4CA6D9F7DAF0AE6B3BF1B24B76C2F12B9E9A7C50D32E2093608FC9A30CBD852329E64A9AE0BC3F513899EBFA28629C1DF38081FB8C6630408F70D7B9A37701ABA4176C8B7DCB8CC78BD7783B861A7FC50862E75191DB8", 4096); + + /* multiply two very large numbers (4080 bits x 4088 bits) with and without overlapping multipliers/multiplicant */ + test_bignum_mult("B96BF707C8CD3CA5AE8247C5CA2AF98140EFAE60179BE3F5BEAD7DA3C6D17404C529239DD1EFE6CADAE1AAFB4FE936B0107839C28A7861E4364EB093CB4698E4BBF6BD8BEF85D9B35781D14AEE1BE86E57B49DF98896CF037CCBD8C622603D84891FD6AC48BE4728E564E64FB715C149C243BAA289569D0FF2E0C9E183D38C8A669CEFF542737E35F3E484D39FF7A3727EF8DB733DAB3E359E1456C0AE33C358EFEC8079EDDD5D58E09B37744EE1DBDF567742CFC0CE98BCC9AD90242ECCF7F6FA696C8C1B32A4D7285C56AB3658DB1AD89A7331F69DEFE212DE8EEEE5B377EC7A4112A27A0FD02EFABB9D3025F6563B65DC214A38A6E7BF8C78B6A3D2A8BA12D75BFBF26ACA655EF13A145AC18D2A6C9B535AAF8290314A2512451B3BD6DA19C42F1FD1B958E1F49303EDEC0392A8CD8450FBC177B26FD2D6CC23F051655565B42FEDE9685A9E708CFC8EA766B94D7B9B627BFA98945BB8EF88E9E7FB696BC4729240F1C25F7085E8C8A9DE2241BBC388FFC65E0058B4327D554FD2D8AA872614052C38BE177F9EC0E705DFDD5F82DD5ED49DAF3582CA64E7F14CE97FD6F25B53FD888D1593450EDC5E79A947F18D0917E01F66ACE99FF4A249C14957A9860B839CEE5096F78FE02C7610E558FC0FCA803A6EF0FBA64AB94893E61080BC5D2AC5DA548E9E0D8E2B63BAB6B82247DF22007D925711E0FE45EB14B92665B6", + "C15B96BF707C8CD3CA5AE8247C5CA2AF98140EFAE60179BE3F5BEAD7DA3C6D17404C529239DD1EFE6CADAE1AAFB4FE936B0107839C28A7861E4364EB093CB4698E4BBF6BD8BEF85D9B35781D14AEE1BE86E57B49DF98896CF037CCBD8C622603D84891FD6AC48BE4728E564E64FB715C149C243BAA289569D0FF2E0C9E183D38C8A669CEFF542737E35F3E484D39FF7A3727EF8DB733DAB3E359E1456C0AE33C358EFEC8079EDDD5D58E09B37744EE1DBDF567742CFC0CE98BCC9AD90242ECCF7F6FA696C8C1B32A4D7285C56AB3658DB1AD89A7331F69DEFE212DE8EEEE5B377EC7A4112A27A0FD02EFABB9D3025F6563B65DC214A38A6E7BF8C78B6A3D2A8BA12D75BFBF26ACA655EF13A145AC18D2A6C9B535AAF8290314A2512451B3BD6DA19C42F1FD1B958E1F49303EDEC0392A8CD8450FBC177B26FD2D6CC23F051655565B42FEDE9685A9E708CFC8EA766B94D7B9B627BFA98945BB8EF88E9E7FB696BC4729240F1C25F7085E8C8A9DE2241BBC388FFC65E0058B4327D554FD2D8AA872614052C38BE177F9EC0E705DFDD5F82DD5ED49DAF3582CA64E7F14CE97FD6F25B53FD888D1593450EDC5E79A947F18D0917E01F66ACE99FF4A249C14957A9860B839CEE5096F78FE02C7610E558FC0FCA803A6EF0FBA64AB94893E61080BC5D2AC5DA548E9E0D8E2B63BAB6B82247DF22007D925711E0FE45EB14B92665B6", + "08C0CBBCD99EC6C840B63887B07378C45DF268C118061B2AFFD9D0C854EE0F7DE5F548FF5BA7F155CCA81EF086F9760FCD86722167AC88E504723CB19A772D9EFF4EC75DD29F6187D34535B2A801C82DF9B1D0F08B64373A5AE5BD31346EE06EDB032B0A306A871E4FDB576F3E8D8F32ED4E054D9292719E77A1C5500FBEC23C59F5CC49A4E0B49D15F92D426FEF36376CB674AACDABF68A731746CA74440C71534D30CBF0252DE4EC38EA53E5821C9868F636239923C460CB813C4F05DD5EC36987A390FDBB7EE345F9D2687D1EF9A26A1FF84BF38049E995E1A5F2D5318A7BEFC9AA15528F7A2253299D30718459CF7958694AA58D91CA28F22718D07105C3FBFA4FEA970A810DD52BFC6AB4D44E3253347A3C5F42C1E723588343B210581F3B8A97C616A26C9C99C1376E169B8C8ED5DE6FA2272D24FB05BA6351B687A27C5CA1CF3FC1BA2BF06DD7598DED3F89080A2AB6DD694000698A7488274396E55EA78104584A5A0523C4744D018DCBFD3E411DA8CA679199FE818C59E08B126356028E3B6AAEA61ACE679D6EF2587CCAE513AB99EB161A405A8AA6FB36BB308BB7CBC21E2117B7CE4B4D1A1FE7EE0386DD229220BFD892A293FD7B8F6617743612736CC2F6200C736FD49C6A4AC9565E4B4EFD841C5FC3F6883621FD4319A11319EF462B549A12DA699612F2E8CE08525559C3487AAD57DFDBF1CAB847174C2FF8BA8D5232E33E5D8F60E5BE4CD8BE5857A860B615EFCFFF38DC0EE6A40F725D5275892419D3915EDB534166AC402B5849EB16AF1E8ADA68EF8C2E6AC8BE254DAEDAC135DA0ECA0E5A3CCF5332CFAB4C2249A92EF96A7DD6F69B4B0F9379CDA164FB1CB1934F27F42C0CEF9AFBA6A0B1716A4A1092E6CC7081AC74503A22F3C2071BB4D3A6842772C02CE78BD1FB4E0D39EB19E2425D3DC77777A8E8254F86A69950C75C1D8CF98C512ECEAF2FF15DC8A6D373A20045F63166BFDB8899C6095D91FC282D49DB9154E74DC64C41A98EFABDB207FF31A88718FF5410EA5A15E76F9591E5BE3B26035FD8567117588D9B94708B98FD764529B43B09EC6A2CDF79E2C05D3A799484516369795E103C7FCB78F1B1CADA47C3092695220FF2DB05136A9401897DE182EDB89022E4E7419B43172808C0A9F3ED80A8DF0A9E5F8E59E57994053050E709E63A4809AAECC3DEABDF5E2B9ED3F8FDA6297811A666E81BB0914B1F9D5D558EE40DBD89BE8B9D7F58575CE66C5A5EC2939463D1CECDD760B2C0584535FEEB2125CB675A1AB09CEDC81F27FA6830423B1F8D426E361EB1B9AD203C33176ACAB28C618714E068DA294C9338EF92FF4ED9F67F438E33E53797C1C31F8FF8D5466887E5610EA41C0CABC07ED90894BA73ECF84F5F3C5443EFAC61F9826C54D176D482CB5174D08A7EA3C3933FEE4986DB38A1EEC08D3366711D64", + 0); + } static bool test_bignum_modexp(const char *z_str, const char *x_str, const char *y_str, const char *m_str, int ret_error) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 0f52ecf8e..d0530cf78 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -441,6 +441,10 @@ static uint16_t _mdns_append_ptr_record(uint8_t * packet, uint16_t * index, cons uint16_t record_length = 0; uint8_t part_length; + if (service == NULL) { + return 0; + } + str[0] = instance; str[1] = service; str[2] = proto; @@ -485,6 +489,10 @@ static uint16_t _mdns_append_sdptr_record(uint8_t * packet, uint16_t * index, md uint16_t record_length = 0; uint8_t part_length; + if (service == NULL) { + return 0; + } + sd_str[0] = (char*)"_services"; sd_str[1] = (char*)"_dns-sd"; sd_str[2] = (char*)"_udp"; @@ -530,6 +538,10 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns uint16_t record_length = 0; uint8_t part_length; + if (service == NULL) { + return 0; + } + str[0] = _mdns_get_service_instance_name(service); str[1] = service->service; str[2] = service->proto; @@ -598,6 +610,10 @@ static uint16_t _mdns_append_srv_record(uint8_t * packet, uint16_t * index, mdns uint16_t record_length = 0; uint8_t part_length; + if (service == NULL) { + return 0; + } + str[0] = _mdns_get_service_instance_name(service); str[1] = service->service; str[2] = service->proto; @@ -1147,6 +1163,7 @@ static bool _mdns_alloc_answer(mdns_out_answer_t ** destnation, uint16_t type, m } a->type = type; a->service = service; + a->custom_service = NULL; a->bye = bye; a->flush = flush; a->next = NULL; @@ -1234,7 +1251,7 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t * parsed } } else if (q->type == MDNS_TYPE_SDPTR) { shared = true; - if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_PTR, service->service, false, false)) { + if (!_mdns_alloc_answer(&packet->answers, MDNS_TYPE_SDPTR, service->service, false, false)) { _mdns_free_tx_packet(packet); return; } diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index de79975aa..e0d5f318b 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -78,6 +78,21 @@ menu "ESP-MQTT Configurations" help MQTT task stack size + config MQTT_DISABLE_API_LOCKS + bool "Disable API locks" + default n + depends on MQTT_USE_CUSTOM_CONFIG + help + Default config employs API locks to protect internal structures. It is possible to disable + these locks if the user code doesn't access MQTT API from multiple concurrent tasks + + config MQTT_TASK_PRIORITY + int "MQTT task priority" + default 5 + depends on MQTT_USE_CUSTOM_CONFIG + help + MQTT task priority. Higher number denotes higher priority. + config MQTT_TASK_CORE_SELECTION_ENABLED bool "Enable MQTT task core selection" default false diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index f08f3b678..6bc94add8 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit f08f3b678717865234637164a29ed3a63e756ca7 +Subproject commit 6bc94add892437d0fd50f26bfabe78c646648c13 diff --git a/components/newlib/include/stdio.h b/components/newlib/include/stdio.h index e336ee6eb..0836ab563 100644 --- a/components/newlib/include/stdio.h +++ b/components/newlib/include/stdio.h @@ -696,8 +696,10 @@ _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr, int _c, FILE *_p) { #ifndef __CYGWIN__ #ifndef lint +#ifdef __SINGLE_THREAD__ #define getc(fp) __sgetc_r(_REENT, fp) #define putc(x, fp) __sputc_r(_REENT, x, fp) +#endif /* __SINGLE_THREAD__ */ #endif /* lint */ #endif /* __CYGWIN__ */ @@ -714,8 +716,10 @@ _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr, int _c, FILE *_p) { #endif /* !__CUSTOM_FILE_IO__ */ +#ifdef __SINGLE_THREAD__ #define getchar() getc(stdin) #define putchar(x) putc(x, stdout) +#endif /* __SINGLE_THREAD__ */ #ifndef __STRICT_ANSI__ #define getchar_unlocked() getc_unlocked(stdin) diff --git a/components/newlib/locks.c b/components/newlib/locks.c index 708ddab3f..0cc63cf47 100644 --- a/components/newlib/locks.c +++ b/components/newlib/locks.c @@ -137,7 +137,7 @@ static int IRAM_ATTR lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t } BaseType_t success; - if (xPortInIsrContext()) { + if (!xPortCanYield()) { /* In ISR Context */ if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) { abort(); /* recursive mutexes make no sense in ISR context */ @@ -191,7 +191,7 @@ static void IRAM_ATTR lock_release_generic(_lock_t *lock, uint8_t mutex_type) { return; } - if (xPortInIsrContext()) { + if (!xPortCanYield()) { if (mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) { abort(); /* indicates logic bug, it shouldn't be possible to lock recursively in ISR */ } diff --git a/components/newlib/test/test_time.c b/components/newlib/test/test_time.c index 871860d97..f5175aab3 100644 --- a/components/newlib/test/test_time.c +++ b/components/newlib/test/test_time.c @@ -224,19 +224,16 @@ static void get_time_task(void *pvParameters) static void start_measure(int64_t* sys_time, int64_t* real_time) { struct timeval tv_time; - *real_time = esp_timer_get_time(); - gettimeofday(&tv_time, NULL); + int64_t t1, t2; + do { + t1 = esp_timer_get_time(); + gettimeofday(&tv_time, NULL); + t2 = esp_timer_get_time(); + } while (t2 - t1 > 40); + *real_time = t2; *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec; } -static void end_measure(int64_t* sys_time, int64_t* real_time) -{ - struct timeval tv_time; - gettimeofday(&tv_time, NULL); - *real_time = esp_timer_get_time(); - *sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec; -} - static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time) { int64_t dt_real_time_us = real_time[1] - real_time[0]; @@ -254,51 +251,54 @@ static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real static void measure_time_task(void *pvParameters) { - struct timeval tv_time; - int64_t real_time_us[2]; - int64_t sys_time_us[2]; - int64_t delay_us = 2 * 1000000; // 2 sec xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters; + int64_t main_real_time_us[2]; + int64_t main_sys_time_us[2]; + struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0}; + TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL)); + struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000}; + adjtime(&delta, NULL); gettimeofday(&tv_time, NULL); - start_measure(&sys_time_us[0], &real_time_us[0]); - // although exit flag is set in another task, checking (exit_flag == false) is safe - while (exit_flag == false) { - ets_delay_us(delay_us); + start_measure(&main_sys_time_us[0], &main_real_time_us[0]); - end_measure(&sys_time_us[1], &real_time_us[1]); - result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us); + { + int64_t real_time_us[2] = { main_real_time_us[0], 0}; + int64_t sys_time_us[2] = { main_sys_time_us[0], 0}; + // although exit flag is set in another task, checking (exit_flag == false) is safe + while (exit_flag == false) { + ets_delay_us(2 * 1000000); // 2 sec - sys_time_us[0] = sys_time_us[1]; - real_time_us[0] = real_time_us[1]; + start_measure(&sys_time_us[1], &real_time_us[1]); + result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us); + + sys_time_us[0] = sys_time_us[1]; + real_time_us[0] = real_time_us[1]; + } + main_sys_time_us[1] = sys_time_us[1]; + main_real_time_us[1] = real_time_us[1]; } + + result_adjtime_correction_us[0] = calc_correction("main", main_sys_time_us, main_real_time_us); + int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1]; + printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us); + TEST_ASSERT_INT_WITHIN(100, 0, delta_us); + xSemaphoreGive(*sema); vTaskDelete(NULL); } TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]") { - int64_t real_time_us[2]; - int64_t sys_time_us[2]; - exit_flag = false; - struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0}; - TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL)); - - struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000}; - adjtime(&delta, NULL); - gettimeofday(&tv_time, NULL); - xSemaphoreHandle exit_sema[2]; for (int i = 0; i < 2; ++i) { exit_sema[i] = xSemaphoreCreateBinary(); result_adjtime_correction_us[i] = 0; } - start_measure(&sys_time_us[0], &real_time_us[0]); - - xTaskCreatePinnedToCore(get_time_task, "get_time_task", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0); - xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1); + xTaskCreatePinnedToCore(get_time_task, "get_time_task", 4096, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0); + xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 4096, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1); printf("start waiting for 30 seconds\n"); vTaskDelay(30000 / portTICK_PERIOD_MS); @@ -312,13 +312,6 @@ TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]") } } - end_measure(&sys_time_us[1], &real_time_us[1]); - result_adjtime_correction_us[0] = calc_correction("main", sys_time_us, real_time_us); - - int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1]; - printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us); - TEST_ASSERT_INT_WITHIN(100, 0, delta_us); - for (int i = 0; i < 2; ++i) { vSemaphoreDelete(exit_sema[i]); } diff --git a/components/newlib/time.c b/components/newlib/time.c index 948c80a31..4fec2dd6e 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -415,10 +415,10 @@ int clock_gettime (clockid_t clock_id, struct timespec *tp) return -1; } struct timeval tv; - _gettimeofday_r(NULL, &tv, NULL); uint64_t monotonic_time_us = 0; switch (clock_id) { case CLOCK_REALTIME: + _gettimeofday_r(NULL, &tv, NULL); tp->tv_sec = tv.tv_sec; tp->tv_nsec = tv.tv_usec * 1000L; break; diff --git a/components/nghttp/port/http_parser.c b/components/nghttp/port/http_parser.c index 8c1f4dd07..ef1ecd48c 100644 --- a/components/nghttp/port/http_parser.c +++ b/components/nghttp/port/http_parser.c @@ -280,8 +280,11 @@ enum state { s_dead = 1 /* important that this is > 0 */ , s_start_req_or_res + , s_res_or_resp_I /* for ICY URIs */ , s_res_or_resp_H , s_start_res + , s_res_I /* for ICY URIs */ + , s_res_IC /* for ICY URIs */ , s_res_H , s_res_HT , s_res_HTT @@ -728,6 +731,10 @@ reexecute: if (ch == 'H') { UPDATE_STATE(s_res_or_resp_H); + CALLBACK_NOTIFY(message_begin); + } else if (ch == 'I') { + UPDATE_STATE(s_res_or_resp_I); + CALLBACK_NOTIFY(message_begin); } else { parser->type = HTTP_REQUEST; @@ -738,6 +745,13 @@ reexecute: break; } + case s_res_or_resp_I: /* ICY URI case */ + if (ch == 'C') { + parser->type = HTTP_RESPONSE; + UPDATE_STATE(s_res_IC); + } + break; + case s_res_or_resp_H: if (ch == 'T') { parser->type = HTTP_RESPONSE; @@ -764,7 +778,9 @@ reexecute: case 'H': UPDATE_STATE(s_res_H); break; - + case 'I': /* ICY URI */ + UPDATE_STATE(s_res_I); + break; case CR: case LF: break; @@ -777,6 +793,15 @@ reexecute: CALLBACK_NOTIFY(message_begin); break; } + case s_res_I: + STRICT_CHECK(ch != 'C'); + UPDATE_STATE(s_res_IC); + break; + + case s_res_IC: + STRICT_CHECK(ch != 'Y'); + UPDATE_STATE(s_res_http_minor); + break; case s_res_H: STRICT_CHECK(ch != 'T'); diff --git a/components/nimble/CMakeLists.txt b/components/nimble/CMakeLists.txt new file mode 100644 index 000000000..ce92e7f81 --- /dev/null +++ b/components/nimble/CMakeLists.txt @@ -0,0 +1,150 @@ +if(CONFIG_NIMBLE_ENABLED) + + set(COMPONENT_ADD_INCLUDEDIRS + nimble/porting/nimble/include + port/include + nimble/nimble/include + nimble/nimble/host/include + nimble/nimble/host/services/ans/include + nimble/nimble/host/services/bas/include + nimble/nimble/host/services/gap/include + nimble/nimble/host/services/gatt/include + nimble/nimble/host/services/ias/include + nimble/nimble/host/services/lls/include + nimble/nimble/host/services/tps/include + nimble/nimble/host/util/include + nimble/nimble/host/store/ram/include + nimble/nimble/host/store/config/include + nimble/porting/npl/freertos/include + esp-hci/include) + + + set(COMPONENT_SRCS "./nimble/nimble/host/util/src/addr.c" + "./nimble/nimble/host/services/gatt/src/ble_svc_gatt.c" + "./nimble/nimble/host/services/tps/src/ble_svc_tps.c" + "./nimble/nimble/host/services/ias/src/ble_svc_ias.c" + "./nimble/nimble/host/services/ans/src/ble_svc_ans.c" + "./nimble/nimble/host/services/gap/src/ble_svc_gap.c" + "./nimble/nimble/host/services/bas/src/ble_svc_bas.c" + "./nimble/nimble/host/services/lls/src/ble_svc_lls.c" + "./nimble/nimble/host/src/ble_hs_conn.c" + "./nimble/nimble/host/src/ble_store_util.c" + "./nimble/nimble/host/src/ble_sm.c" + "./nimble/nimble/host/src/ble_hs_shutdown.c" + "./nimble/nimble/host/src/ble_l2cap_sig_cmd.c" + "./nimble/nimble/host/src/ble_hs_hci_cmd.c" + "./nimble/nimble/host/src/ble_hs_id.c" + "./nimble/nimble/host/src/ble_att_svr.c" + "./nimble/nimble/host/src/ble_gatts_lcl.c" + "./nimble/nimble/host/src/ble_ibeacon.c" + "./nimble/nimble/host/src/ble_hs_atomic.c" + "./nimble/nimble/host/src/ble_sm_alg.c" + "./nimble/nimble/host/src/ble_hs_stop.c" + "./nimble/nimble/host/src/ble_hs.c" + "./nimble/nimble/host/src/ble_hs_hci_evt.c" + "./nimble/nimble/host/src/ble_hs_dbg.c" + "./nimble/nimble/host/src/ble_hs_mqueue.c" + "./nimble/nimble/host/src/ble_att.c" + "./nimble/nimble/host/src/ble_gattc.c" + "./nimble/nimble/host/src/ble_store.c" + "./nimble/nimble/host/src/ble_sm_lgcy.c" + "./nimble/nimble/host/src/ble_hs_cfg.c" + "./nimble/nimble/host/src/ble_monitor.c" + "./nimble/nimble/host/src/ble_att_clt.c" + "./nimble/nimble/host/src/ble_l2cap_coc.c" + "./nimble/nimble/host/src/ble_hs_mbuf.c" + "./nimble/nimble/host/src/ble_att_cmd.c" + "./nimble/nimble/host/src/ble_hs_log.c" + "./nimble/nimble/host/src/ble_eddystone.c" + "./nimble/nimble/host/src/ble_hs_startup.c" + "./nimble/nimble/host/src/ble_l2cap_sig.c" + "./nimble/nimble/host/src/ble_gap.c" + "./nimble/nimble/host/src/ble_sm_cmd.c" + "./nimble/nimble/host/src/ble_uuid.c" + "./nimble/nimble/host/src/ble_hs_pvcy.c" + "./nimble/nimble/host/src/ble_hs_flow.c" + "./nimble/nimble/host/src/ble_l2cap.c" + "./nimble/nimble/host/src/ble_sm_sc.c" + "./nimble/nimble/host/src/ble_hs_misc.c" + "./nimble/nimble/host/src/ble_gatts.c" + "./nimble/nimble/host/src/ble_hs_adv.c" + "./nimble/nimble/host/src/ble_hs_hci.c" + "./nimble/nimble/host/src/ble_hs_hci_util.c" + "./nimble/nimble/host/src/ble_hs_resolv.c" + "./nimble/nimble/host/store/ram/src/ble_store_ram.c" + "./nimble/nimble/host/store/config/src/ble_store_config.c" + "./nimble/nimble/host/store/config/src/ble_store_nvs.c" + "./nimble/nimble/src/ble_util.c" + "./nimble/porting/npl/freertos/src/nimble_port_freertos.c" + "./nimble/porting/npl/freertos/src/npl_os_freertos.c" + "./nimble/porting/nimble/src/endian.c" + "./nimble/porting/nimble/src/os_cputime_pwr2.c" + "./nimble/porting/nimble/src/hal_timer.c" + "./nimble/porting/nimble/src/os_mempool.c" + "./nimble/porting/nimble/src/os_msys_init.c" + "./nimble/porting/nimble/src/nimble_port.c" + "./nimble/porting/nimble/src/mem.c" + "./nimble/porting/nimble/src/os_mbuf.c" + "./nimble/porting/nimble/src/os_cputime.c" + "./esp-hci/src/esp_nimble_hci.c" + "./port/src/esp_nimble_mem.c") + + if(NOT CONFIG_NIMBLE_CRYPTO_STACK_MBEDTLS) + + list(APPEND COMPONENT_ADD_INCLUDEDIRS + nimble/ext/tinycrypt/include) + + list(APPEND COMPONENT_SRCS "./nimble/ext/tinycrypt/src/utils.c" + "./nimble/ext/tinycrypt/src/sha256.c" + "./nimble/ext/tinycrypt/src/ecc.c" + "./nimble/ext/tinycrypt/src/ctr_prng.c" + "./nimble/ext/tinycrypt/src/ctr_mode.c" + "./nimble/ext/tinycrypt/src/aes_decrypt.c" + "./nimble/ext/tinycrypt/src/aes_encrypt.c" + "./nimble/ext/tinycrypt/src/ccm_mode.c" + "./nimble/ext/tinycrypt/src/ecc_dsa.c" + "./nimble/ext/tinycrypt/src/cmac_mode.c" + "./nimble/ext/tinycrypt/src/ecc_dh.c" + "./nimble/ext/tinycrypt/src/hmac_prng.c" + "./nimble/ext/tinycrypt/src/ecc_platform_specific.c" + "./nimble/ext/tinycrypt/src/hmac.c" + "./nimble/ext/tinycrypt/src/cbc_mode.c") + endif() + + + if(CONFIG_NIMBLE_MESH) + + list(APPEND COMPONENT_ADD_INCLUDEDIRS + nimble/nimble/host/mesh/include) + + list(APPEND COMPONENT_SRCS + "./nimble/nimble/host/mesh/src/shell.c" + "./nimble/nimble/host/mesh/src/friend.c" + "./nimble/nimble/host/mesh/src/crypto.c" + "./nimble/nimble/host/mesh/src/settings.c" + "./nimble/nimble/host/mesh/src/adv.c" + "./nimble/nimble/host/mesh/src/model_srv.c" + "./nimble/nimble/host/mesh/src/beacon.c" + "./nimble/nimble/host/mesh/src/glue.c" + "./nimble/nimble/host/mesh/src/model_cli.c" + "./nimble/nimble/host/mesh/src/transport.c" + "./nimble/nimble/host/mesh/src/prov.c" + "./nimble/nimble/host/mesh/src/mesh.c" + "./nimble/nimble/host/mesh/src/access.c" + "./nimble/nimble/host/mesh/src/cfg_srv.c" + "./nimble/nimble/host/mesh/src/cfg_cli.c" + "./nimble/nimble/host/mesh/src/light_model.c" + "./nimble/nimble/host/mesh/src/health_cli.c" + "./nimble/nimble/host/mesh/src/lpn.c" + "./nimble/nimble/host/mesh/src/proxy.c" + "./nimble/nimble/host/mesh/src/health_srv.c" + "./nimble/nimble/host/mesh/src/testing.c" + "./nimble/nimble/host/mesh/src/net.c") + + endif() +endif() + +# requirements can't depend on config +set(COMPONENT_PRIV_REQUIRES bt nvs_flash) + +register_component() diff --git a/components/nimble/component.mk b/components/nimble/component.mk new file mode 100644 index 000000000..c3f7ed46d --- /dev/null +++ b/components/nimble/component.mk @@ -0,0 +1,54 @@ +# +# Component Makefile +# + +ifeq ($(CONFIG_NIMBLE_ENABLED),y) +COMPONENT_ADD_INCLUDEDIRS += nimble/nimble/include \ + nimble/nimble/host/include \ + nimble/porting/nimble/include \ + nimble/porting/npl/freertos/include \ + nimble/nimble/host/services/ans/include \ + nimble/nimble/host/services/bas/include \ + nimble/nimble/host/services/gap/include \ + nimble/nimble/host/services/gatt/include \ + nimble/nimble/host/services/ias/include \ + nimble/nimble/host/services/lls/include \ + nimble/nimble/host/services/tps/include \ + nimble/nimble/host/util/include \ + nimble/nimble/host/store/ram/include \ + nimble/nimble/host/store/config/include \ + esp-hci/include \ + port/include + +ifndef CONFIG_NIMBLE_CRYPTO_STACK_MBEDTLS +COMPONENT_ADD_INCLUDEDIRS += nimble/ext/tinycrypt/include +endif + +COMPONENT_SRCDIRS += nimble/nimble/host/src \ + nimble/porting/nimble/src \ + nimble/porting/npl/freertos/src \ + nimble/nimble/host/services/ans/src \ + nimble/nimble/host/services/bas/src \ + nimble/nimble/host/services/gap/src \ + nimble/nimble/host/services/gatt/src \ + nimble/nimble/host/services/ias/src \ + nimble/nimble/host/services/lls/src \ + nimble/nimble/host/services/tps/src \ + nimble/nimble/host/util/src \ + nimble/nimble/host/store/ram/src \ + nimble/nimble/host/store/config/src \ + esp-hci/src \ + port/src + +ifndef CONFIG_NIMBLE_CRYPTO_STACK_MBEDTLS +COMPONENT_SRCDIRS += nimble/ext/tinycrypt/src +endif + +COMPONENT_OBJEXCLUDE += nimble/nimble/host/store/config/src/ble_store_config_conf.o + +ifeq ($(CONFIG_NIMBLE_MESH),y) +COMPONENT_ADD_INCLUDEDIRS += nimble/nimble/host/mesh/include +COMPONENT_SRCDIRS += nimble/nimble/host/mesh/src + +endif +endif diff --git a/components/nimble/esp-hci/include/esp_nimble_hci.h b/components/nimble/esp-hci/include/esp_nimble_hci.h new file mode 100644 index 000000000..e10436f3c --- /dev/null +++ b/components/nimble/esp-hci/include/esp_nimble_hci.h @@ -0,0 +1,138 @@ +/* + * Copyright 2019 Espressif Systems (Shanghai) PTE LTD + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __ESP_NIMBLE_HCI_H__ +#define __ESP_NIMBLE_HCI_H__ + +#include "nimble/ble_hci_trans.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_HCI_UART_H4_NONE 0x00 +#define BLE_HCI_UART_H4_CMD 0x01 +#define BLE_HCI_UART_H4_ACL 0x02 +#define BLE_HCI_UART_H4_SCO 0x03 +#define BLE_HCI_UART_H4_EVT 0x04 + +/** + * @brief Initialize VHCI transport layer between NimBLE Host and + * ESP Bluetooth controller + * + * This function initializes the transport buffers to be exchanged + * between NimBLE host and ESP controller. It also registers required + * host callbacks with the controller. + * + * @return + * - ESP_OK if the initialization is successful + * - Appropriate error code from esp_err_t in case of an error + */ +esp_err_t esp_nimble_hci_init(void); + +/** + * @brief Initialize ESP Bluetooth controller(link layer) and VHCI transport + * layer between NimBLE Host and ESP Bluetooth controller + * + * This function initializes ESP controller in BLE only mode and the + * transport buffers to be exchanged between NimBLE host and ESP controller. + * It also registers required host callbacks with the controller. + * + * Below is the sequence of APIs to be called to init/enable NimBLE host and ESP controller: + * + * @code{c} + * void ble_host_task(void *param) + * { + * nimble_port_run(); //This function will return only when nimble_port_stop() is executed. + * nimble_port_freertos_deinit(); + * } + * + * int ret = esp_nimble_hci_and_controller_init(); + * if (ret != ESP_OK) { + ESP_LOGE(TAG, "esp_nimble_hci_and_controller_init() failed with error: %d", ret); + * return; + * } + * + * nimble_port_init(); + * + * //Initialize the NimBLE Host configuration + * + * nimble_port_freertos_init(ble_host_task); + * @endcode + * + * nimble_port_freertos_init() is an optional call that creates a new task in which the NimBLE + * host will run. The task function should have a call to nimble_port_run(). If a separate task + * is not required, calling nimble_port_run() will run the NimBLE host in the current task. + * + * @return + * - ESP_OK if the initialization is successful + * - Appropriate error code from esp_err_t in case of an error + */ +esp_err_t esp_nimble_hci_and_controller_init(void); + +/** + * @brief Deinitialize VHCI transport layer between NimBLE Host and + * ESP Bluetooth controller + * + * @note This function should be called after the NimBLE host is deinitialized. + * + * @return + * - ESP_OK if the deinitialization is successful + * - Appropriate error codes from esp_err_t in case of an error + */ +esp_err_t esp_nimble_hci_deinit(void); + +/** + * @brief Deinitialize VHCI transport layer between NimBLE Host and + * ESP Bluetooth controller and disable and deinitialize the controller + * + * @note This function should not be executed in the context of Bluetooth host task. + * + * @note This function should be called after the NimBLE host is deinitialized. + * + * Below is the sequence of APIs to be called to disable/deinit NimBLE host and ESP controller: + * + * @code{c} + * int ret = nimble_port_stop(); + * if (ret == 0) { + * nimble_port_deinit(); + * + * ret = esp_nimble_hci_and_controller_deinit(); + * if (ret != ESP_OK) { + ESP_LOGE(TAG, "esp_nimble_hci_and_controller_deinit() failed with error: %d", ret); + * } + * } + * @endcode + * + * If nimble_port_freertos_init() is used during initialization, then + * nimble_port_freertos_deinit() should be called in the host task after nimble_port_run(). + * + * @return + * - ESP_OK if the deinitialization is successful + * - Appropriate error codes from esp_err_t in case of an error + */ +esp_err_t esp_nimble_hci_and_controller_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __ESP_NIMBLE_HCI_H__ */ diff --git a/components/nimble/esp-hci/src/esp_nimble_hci.c b/components/nimble/esp-hci/src/esp_nimble_hci.c new file mode 100644 index 000000000..92acaf6a0 --- /dev/null +++ b/components/nimble/esp-hci/src/esp_nimble_hci.c @@ -0,0 +1,521 @@ +/* + * Copyright 2019 Espressif Systems (Shanghai) PTE LTD + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include "sysinit/sysinit.h" +#include "nimble/hci_common.h" +#include "host/ble_hs.h" +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "esp_nimble_hci.h" +#include "esp_nimble_mem.h" +#include "esp_bt.h" +#include "freertos/semphr.h" + +#define NIMBLE_VHCI_TIMEOUT_MS 2000 + +static ble_hci_trans_rx_cmd_fn *ble_hci_rx_cmd_hs_cb; +static void *ble_hci_rx_cmd_hs_arg; + +static ble_hci_trans_rx_acl_fn *ble_hci_rx_acl_hs_cb; +static void *ble_hci_rx_acl_hs_arg; + +static struct os_mbuf_pool ble_hci_acl_mbuf_pool; +static struct os_mempool_ext ble_hci_acl_pool; +/* + * The MBUF payload size must accommodate the HCI data header size plus the + * maximum ACL data packet length. The ACL block size is the size of the + * mbufs we will allocate. + */ +#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \ + + BLE_MBUF_MEMBLOCK_OVERHEAD \ + + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT) + +static os_membuf_t *ble_hci_acl_buf; + +static struct os_mempool ble_hci_cmd_pool; +static os_membuf_t *ble_hci_cmd_buf; + +static struct os_mempool ble_hci_evt_hi_pool; +static os_membuf_t *ble_hci_evt_hi_buf; + +static struct os_mempool ble_hci_evt_lo_pool; +static os_membuf_t *ble_hci_evt_lo_buf; + +static SemaphoreHandle_t vhci_send_sem; +const static char *TAG = "NimBLE"; + +int os_msys_buf_alloc(void); +void os_msys_buf_free(void); + +void ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb, + void *cmd_arg, + ble_hci_trans_rx_acl_fn *acl_cb, + void *acl_arg) +{ + ble_hci_rx_cmd_hs_cb = cmd_cb; + ble_hci_rx_cmd_hs_arg = cmd_arg; + ble_hci_rx_acl_hs_cb = acl_cb; + ble_hci_rx_acl_hs_arg = acl_arg; +} + + +int ble_hci_trans_hs_cmd_tx(uint8_t *cmd) +{ + uint16_t len; + uint8_t rc = 0; + + assert(cmd != NULL); + *cmd = BLE_HCI_UART_H4_CMD; + len = BLE_HCI_CMD_HDR_LEN + cmd[3] + 1; + if (!esp_vhci_host_check_send_available()) { + ESP_LOGD(TAG, "Controller not ready to receive packets"); + } + + if (xSemaphoreTake(vhci_send_sem, NIMBLE_VHCI_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) { + esp_vhci_host_send_packet(cmd, len); + } else { + rc = BLE_HS_ETIMEOUT_HCI; + } + + ble_hci_trans_buf_free(cmd); + return rc; +} + +int ble_hci_trans_ll_evt_tx(uint8_t *hci_ev) +{ + int rc = ESP_FAIL; + + if (ble_hci_rx_cmd_hs_cb) { + rc = ble_hci_rx_cmd_hs_cb(hci_ev, ble_hci_rx_cmd_hs_arg); + } + return rc; +} + +int ble_hci_trans_hs_acl_tx(struct os_mbuf *om) +{ + uint16_t len = 0; + uint8_t data[MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 1], rc = 0; + /* If this packet is zero length, just free it */ + if (OS_MBUF_PKTLEN(om) == 0) { + os_mbuf_free_chain(om); + return 0; + } + data[0] = BLE_HCI_UART_H4_ACL; + len++; + + if (!esp_vhci_host_check_send_available()) { + ESP_LOGD(TAG, "Controller not ready to receive packets"); + } + + os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), &data[1]); + len += OS_MBUF_PKTLEN(om); + + if (xSemaphoreTake(vhci_send_sem, NIMBLE_VHCI_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) { + esp_vhci_host_send_packet(data, len); + } else { + rc = BLE_HS_ETIMEOUT_HCI; + } + + os_mbuf_free_chain(om); + + return rc; +} + +int ble_hci_trans_ll_acl_tx(struct os_mbuf *om) +{ + int rc = ESP_FAIL; + + if (ble_hci_rx_acl_hs_cb) { + rc = ble_hci_rx_acl_hs_cb(om, ble_hci_rx_acl_hs_arg); + } + return rc; +} + +uint8_t *ble_hci_trans_buf_alloc(int type) +{ + uint8_t *buf; + + switch (type) { + case BLE_HCI_TRANS_BUF_CMD: + buf = os_memblock_get(&ble_hci_cmd_pool); + break; + + case BLE_HCI_TRANS_BUF_EVT_HI: + buf = os_memblock_get(&ble_hci_evt_hi_pool); + if (buf == NULL) { + /* If no high-priority event buffers remain, try to grab a + * low-priority one. + */ + buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO); + } + break; + + case BLE_HCI_TRANS_BUF_EVT_LO: + buf = os_memblock_get(&ble_hci_evt_lo_pool); + break; + + default: + assert(0); + buf = NULL; + } + + return buf; +} + +void ble_hci_trans_buf_free(uint8_t *buf) +{ + int rc; + /* XXX: this may look a bit odd, but the controller uses the command + * buffer to send back the command complete/status as an immediate + * response to the command. This was done to insure that the controller + * could always send back one of these events when a command was received. + * Thus, we check to see which pool the buffer came from so we can free + * it to the appropriate pool + */ + if (os_memblock_from(&ble_hci_evt_hi_pool, buf)) { + rc = os_memblock_put(&ble_hci_evt_hi_pool, buf); + assert(rc == 0); + } else if (os_memblock_from(&ble_hci_evt_lo_pool, buf)) { + rc = os_memblock_put(&ble_hci_evt_lo_pool, buf); + assert(rc == 0); + } else { + assert(os_memblock_from(&ble_hci_cmd_pool, buf)); + rc = os_memblock_put(&ble_hci_cmd_pool, buf); + assert(rc == 0); + } +} + +/** + * Unsupported; the RAM transport does not have a dedicated ACL data packet + * pool. + */ +int ble_hci_trans_set_acl_free_cb(os_mempool_put_fn *cb, void *arg) +{ + return BLE_ERR_UNSUPPORTED; +} + +int ble_hci_trans_reset(void) +{ + /* No work to do. All allocated buffers are owned by the host or + * controller, and they will get freed by their owners. + */ + return 0; +} + +/** + * Allocates a buffer (mbuf) for ACL operation. + * + * @return The allocated buffer on success; + * NULL on buffer exhaustion. + */ +static struct os_mbuf *ble_hci_trans_acl_buf_alloc(void) +{ + struct os_mbuf *m; + uint8_t usrhdr_len; + +#if MYNEWT_VAL(BLE_DEVICE) + usrhdr_len = sizeof(struct ble_mbuf_hdr); +#elif MYNEWT_VAL(BLE_HS_FLOW_CTRL) + usrhdr_len = BLE_MBUF_HS_HDR_LEN; +#else + usrhdr_len = 0; +#endif + + m = os_mbuf_get_pkthdr(&ble_hci_acl_mbuf_pool, usrhdr_len); + return m; +} + +static void ble_hci_rx_acl(uint8_t *data, uint16_t len) +{ + struct os_mbuf *m; + int sr; + if (len < BLE_HCI_DATA_HDR_SZ || len > MYNEWT_VAL(BLE_ACL_BUF_SIZE)) { + return; + } + + m = ble_hci_trans_acl_buf_alloc(); + + if (!m) { + return; + } + if (os_mbuf_append(m, data, len)) { + os_mbuf_free_chain(m); + return; + } + OS_ENTER_CRITICAL(sr); + if (ble_hci_rx_acl_hs_cb) { + ble_hci_rx_acl_hs_cb(m, NULL); + } + OS_EXIT_CRITICAL(sr); +} + +static void ble_hci_transport_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = os_mempool_ext_init(&ble_hci_acl_pool, + MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE, + ble_hci_acl_buf, + "ble_hci_acl_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = os_mbuf_pool_init(&ble_hci_acl_mbuf_pool, + &ble_hci_acl_pool.mpe_mp, + ACL_BLOCK_SIZE, + MYNEWT_VAL(BLE_ACL_BUF_COUNT)); + SYSINIT_PANIC_ASSERT(rc == 0); + + /* + * Create memory pool of HCI command buffers. NOTE: we currently dont + * allow this to be configured. The controller will only allow one + * outstanding command. We decided to keep this a pool in case we allow + * allow the controller to handle more than one outstanding command. + */ + rc = os_mempool_init(&ble_hci_cmd_pool, + 1, + BLE_HCI_TRANS_CMD_SZ, + ble_hci_cmd_buf, + "ble_hci_cmd_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = os_mempool_init(&ble_hci_evt_hi_pool, + MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_evt_hi_buf, + "ble_hci_evt_hi_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = os_mempool_init(&ble_hci_evt_lo_pool, + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_evt_lo_buf, + "ble_hci_evt_lo_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); +} + +/* + * @brief: BT controller callback function, used to notify the upper layer that + * controller is ready to receive command + */ +static void controller_rcv_pkt_ready(void) +{ + if (vhci_send_sem) { + xSemaphoreGive(vhci_send_sem); + } +} + +/* + * @brief: BT controller callback function, to transfer data packet to the host + */ +static int host_rcv_pkt(uint8_t *data, uint16_t len) +{ + + if (data[0] == BLE_HCI_UART_H4_EVT) { + uint8_t *evbuf; + int totlen; + int rc; + + totlen = BLE_HCI_EVENT_HDR_LEN + data[2]; + assert(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN); + + if (data[1] == BLE_HCI_EVCODE_HW_ERROR) { + assert(0); + } + + /* Allocate LE Advertising Report Event from lo pool only */ + if ((data[1] == BLE_HCI_EVCODE_LE_META) && (data[3] == BLE_HCI_LE_SUBEV_ADV_RPT)) { + evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO); + /* Skip advertising report if we're out of memory */ + if (!evbuf) { + return 0; + } + } else { + evbuf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI); + assert(evbuf != NULL); + } + + memcpy(evbuf, &data[1], totlen); + + rc = ble_hci_trans_ll_evt_tx(evbuf); + assert(rc == 0); + } else if (data[0] == BLE_HCI_UART_H4_ACL) { + ble_hci_rx_acl(data + 1, len - 1); + } + return 0; +} + +static const esp_vhci_host_callback_t vhci_host_cb = { + .notify_host_send_available = controller_rcv_pkt_ready, + .notify_host_recv = host_rcv_pkt, +}; + +static void ble_buf_free(void) +{ + os_msys_buf_free(); + + nimble_platform_mem_free(ble_hci_evt_hi_buf); + ble_hci_evt_hi_buf = NULL; + nimble_platform_mem_free(ble_hci_evt_lo_buf); + ble_hci_evt_lo_buf = NULL; + nimble_platform_mem_free(ble_hci_cmd_buf); + ble_hci_cmd_buf = NULL; + nimble_platform_mem_free(ble_hci_acl_buf); + ble_hci_acl_buf = NULL; +} + +static esp_err_t ble_buf_alloc(void) +{ + if (os_msys_buf_alloc()) { + return ESP_ERR_NO_MEM; + } + + ble_hci_evt_hi_buf = (os_membuf_t *) nimble_platform_mem_calloc(1, + (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)))); + + ble_hci_evt_lo_buf = (os_membuf_t *) nimble_platform_mem_calloc(1, + (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)))); + + ble_hci_cmd_buf = (os_membuf_t *) nimble_platform_mem_calloc(1, + (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ))); + + ble_hci_acl_buf = (os_membuf_t *) nimble_platform_mem_calloc(1, + (sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE))); + + if (!ble_hci_evt_hi_buf || !ble_hci_evt_lo_buf || !ble_hci_cmd_buf || !ble_hci_acl_buf) { + ble_buf_free(); + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +esp_err_t esp_nimble_hci_init(void) +{ + esp_err_t ret; + + ret = ble_buf_alloc(); + if (ret != ESP_OK) { + goto err; + } + if ((ret = esp_vhci_host_register_callback(&vhci_host_cb)) != ESP_OK) { + goto err; + } + + ble_hci_transport_init(); + + vhci_send_sem = xSemaphoreCreateBinary(); + if (vhci_send_sem == NULL) { + ret = ESP_ERR_NO_MEM; + goto err; + } + + xSemaphoreGive(vhci_send_sem); + + return ret; +err: + ble_buf_free(); + return ret; + +} + +esp_err_t esp_nimble_hci_and_controller_init(void) +{ + esp_err_t ret; + + esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); + + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + + if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { + return ret; + } + + if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) { + return ret; + } + return esp_nimble_hci_init(); +} + +static esp_err_t ble_hci_transport_deinit(void) +{ + int ret = 0; + + ret += os_mempool_clear(&ble_hci_evt_lo_pool); + + ret += os_mempool_clear(&ble_hci_evt_hi_pool); + + ret += os_mempool_clear(&ble_hci_cmd_pool); + + ret += os_mempool_ext_clear(&ble_hci_acl_pool); + + if (ret) { + return ESP_FAIL; + } else { + return ESP_OK; + } +} + +esp_err_t esp_nimble_hci_deinit(void) +{ + if (vhci_send_sem) { + /* Dummy take & give semaphore before deleting */ + xSemaphoreTake(vhci_send_sem, portMAX_DELAY); + xSemaphoreGive(vhci_send_sem); + vSemaphoreDelete(vhci_send_sem); + vhci_send_sem = NULL; + } + esp_err_t ret = ble_hci_transport_deinit(); + if (ret != ESP_OK) { + return ret; + } + + ble_buf_free(); + + return ESP_OK; +} + +esp_err_t esp_nimble_hci_and_controller_deinit(void) +{ + int ret; + ret = esp_nimble_hci_deinit(); + if (ret != ESP_OK) { + return ret; + } + + ret = esp_bt_controller_disable(); + if (ret != ESP_OK) { + return ret; + } + + ret = esp_bt_controller_deinit(); + if (ret != ESP_OK) { + return ret; + } + + return ESP_OK; +} diff --git a/components/nimble/nimble b/components/nimble/nimble new file mode 160000 index 000000000..a9239c6ca --- /dev/null +++ b/components/nimble/nimble @@ -0,0 +1 @@ +Subproject commit a9239c6ca5e8f6dc72792ac716a8fe3ffad08b1e diff --git a/components/nimble/port/include/console/console.h b/components/nimble/port/include/console/console.h new file mode 100644 index 000000000..96f965159 --- /dev/null +++ b/components/nimble/port/include/console/console.h @@ -0,0 +1,21 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +//Licensed under the Apache License, Version 2.0 (the "License"); +//you may not use this file except in compliance with the License. +//You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +//Unless required by applicable law or agreed to in writing, software +//distributed under the License is distributed on an "AS IS" BASIS, +//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +//See the License for the specific language governing permissions and +//limitations under the License. +#ifndef _CONSOLE_H +#define _CONSOLE_H + +#include + +#define console_printf printf + +#endif diff --git a/components/nimble/port/include/esp_nimble_cfg.h b/components/nimble/port/include/esp_nimble_cfg.h new file mode 100644 index 000000000..3c9957518 --- /dev/null +++ b/components/nimble/port/include/esp_nimble_cfg.h @@ -0,0 +1,1093 @@ + +#ifndef __ESP_NIMBLE_CFG__ +#define __ESP_NIMBLE_CFG__ +#include "sdkconfig.h" + +/** + * This macro exists to ensure code includes this header when needed. If code + * checks the existence of a setting directly via ifdef without including this + * header, the setting macro will silently evaluate to 0. In contrast, an + * attempt to use these macros without including this header will result in a + * compiler error. + */ +#define MYNEWT_VAL(x) MYNEWT_VAL_ ## x + +/*** kernel/os */ +#ifndef MYNEWT_VAL_MSYS_1_BLOCK_COUNT +#ifdef CONFIG_NIMBLE_MESH +#define MYNEWT_VAL_MSYS_1_BLOCK_COUNT (CONFIG_NIMBLE_MSYS1_BLOCK_COUNT + 8) +#else +#define MYNEWT_VAL_MSYS_1_BLOCK_COUNT CONFIG_NIMBLE_MSYS1_BLOCK_COUNT +#endif +#endif + +#ifndef MYNEWT_VAL_MSYS_1_BLOCK_SIZE +#define MYNEWT_VAL_MSYS_1_BLOCK_SIZE (292) +#endif + +#ifndef MYNEWT_VAL_MSYS_2_BLOCK_COUNT +#define MYNEWT_VAL_MSYS_2_BLOCK_COUNT (0) +#endif + +#ifndef MYNEWT_VAL_MSYS_2_BLOCK_SIZE +#define MYNEWT_VAL_MSYS_2_BLOCK_SIZE (0) +#endif + +#ifndef MYNEWT_VAL_OS_CPUTIME_FREQ +#define MYNEWT_VAL_OS_CPUTIME_FREQ (1000000) +#endif + +#ifndef MYNEWT_VAL_OS_CPUTIME_TIMER_NUM +#define MYNEWT_VAL_OS_CPUTIME_TIMER_NUM (0) +#endif + +/*** nimble */ +#ifndef MYNEWT_VAL_BLE_EXT_ADV +#define MYNEWT_VAL_BLE_EXT_ADV (0) +#endif + +#ifndef MYNEWT_VAL_BLE_EXT_ADV_MAX_SIZE +#define MYNEWT_VAL_BLE_EXT_ADV_MAX_SIZE (31) +#endif + +#ifndef MYNEWT_VAL_BLE_MAX_CONNECTIONS +#define MYNEWT_VAL_BLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS +#endif + +#ifndef MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES +#define MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES (0) +#endif + +#ifndef MYNEWT_VAL_BLE_ROLE_BROADCASTER +#ifdef CONFIG_NIMBLE_ROLE_BROADCASTER +#define MYNEWT_VAL_BLE_ROLE_BROADCASTER (1) +#else +#define MYNEWT_VAL_BLE_ROLE_BROADCASTER (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_ROLE_CENTRAL +#ifdef CONFIG_NIMBLE_ROLE_CENTRAL +#define MYNEWT_VAL_BLE_ROLE_CENTRAL (1) +#else +#define MYNEWT_VAL_BLE_ROLE_CENTRAL (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_ROLE_OBSERVER +#ifdef CONFIG_NIMBLE_ROLE_OBSERVER +#define MYNEWT_VAL_BLE_ROLE_OBSERVER (1) +#else +#define MYNEWT_VAL_BLE_ROLE_OBSERVER (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_ROLE_PERIPHERAL +#ifdef CONFIG_NIMBLE_ROLE_PERIPHERAL +#define MYNEWT_VAL_BLE_ROLE_PERIPHERAL (1) +#else +#define MYNEWT_VAL_BLE_ROLE_PERIPHERAL (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_WHITELIST +#define MYNEWT_VAL_BLE_WHITELIST (1) +#endif + +/*** @apache-mynewt-nimble/nimble/controller */ +#ifndef MYNEWT_VAL_BLE_DEVICE +#define MYNEWT_VAL_BLE_DEVICE (0) +#endif + +/* Overridden by @apache-mynewt-nimble/nimble/controller (defined by @apache-mynewt-nimble/nimble/controller) */ +#ifndef MYNEWT_VAL_BLE_HW_WHITELIST_ENABLE +#define MYNEWT_VAL_BLE_HW_WHITELIST_ENABLE (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_ADD_STRICT_SCHED_PERIODS +#define MYNEWT_VAL_BLE_LL_ADD_STRICT_SCHED_PERIODS (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_CONN_PARAM_REQ +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_CONN_PARAM_REQ (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_DATA_LEN_EXT +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_DATA_LEN_EXT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_EXT_SCAN_FILT +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_EXT_SCAN_FILT (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_2M_PHY +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_2M_PHY (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CODED_PHY +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CODED_PHY (0) +#endif + +/* Overridden by @apache-mynewt-nimble/nimble/controller (defined by @apache-mynewt-nimble/nimble/controller) */ +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CSA2 +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_CSA2 (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_ENCRYPTION +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_ENCRYPTION (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_PING +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_PING (MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_ENCRYPTION) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LL_EXT_ADV +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LL_EXT_ADV (MYNEWT_VAL_BLE_EXT_ADV) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_LL_PRIVACY +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_LL_PRIVACY (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG +#define MYNEWT_VAL_BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG (1) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CONN_INIT_MAX_TX_BYTES +#define MYNEWT_VAL_BLE_LL_CONN_INIT_MAX_TX_BYTES (27) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CONN_INIT_MIN_WIN_OFFSET +#define MYNEWT_VAL_BLE_LL_CONN_INIT_MIN_WIN_OFFSET (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_CONN_INIT_SLOTS +#define MYNEWT_VAL_BLE_LL_CONN_INIT_SLOTS (4) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_DIRECT_TEST_MODE +#define MYNEWT_VAL_BLE_LL_DIRECT_TEST_MODE (0) +#endif + +/* Overridden by @apache-mynewt-nimble/nimble/controller (defined by @apache-mynewt-nimble/nimble/controller) */ +#ifndef MYNEWT_VAL_BLE_LL_EXT_ADV_AUX_PTR_CNT +#define MYNEWT_VAL_BLE_LL_EXT_ADV_AUX_PTR_CNT (5) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_MASTER_SCA +#define MYNEWT_VAL_BLE_LL_MASTER_SCA (4) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE +#define MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE (251) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_MFRG_ID +#define MYNEWT_VAL_BLE_LL_MFRG_ID (0xFFFF) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_NUM_SCAN_DUP_ADVS +#define MYNEWT_VAL_BLE_LL_NUM_SCAN_DUP_ADVS (8) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_NUM_SCAN_RSP_ADVS +#define MYNEWT_VAL_BLE_LL_NUM_SCAN_RSP_ADVS (8) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_OUR_SCA +#define MYNEWT_VAL_BLE_LL_OUR_SCA (60) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_PRIO +#define MYNEWT_VAL_BLE_LL_PRIO (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_RESOLV_LIST_SIZE +#define MYNEWT_VAL_BLE_LL_RESOLV_LIST_SIZE (4) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_RNG_BUFSIZE +#define MYNEWT_VAL_BLE_LL_RNG_BUFSIZE (32) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_STRICT_CONN_SCHEDULING +#define MYNEWT_VAL_BLE_LL_STRICT_CONN_SCHEDULING (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_SUPP_MAX_RX_BYTES +#define MYNEWT_VAL_BLE_LL_SUPP_MAX_RX_BYTES (MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_SUPP_MAX_TX_BYTES +#define MYNEWT_VAL_BLE_LL_SUPP_MAX_TX_BYTES (MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_SYSVIEW +#define MYNEWT_VAL_BLE_LL_SYSVIEW (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_TX_PWR_DBM +#define MYNEWT_VAL_BLE_LL_TX_PWR_DBM (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_USECS_PER_PERIOD +#define MYNEWT_VAL_BLE_LL_USECS_PER_PERIOD (3250) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_VND_EVENT_ON_ASSERT +#define MYNEWT_VAL_BLE_LL_VND_EVENT_ON_ASSERT (0) +#endif + +#ifndef MYNEWT_VAL_BLE_LL_WHITELIST_SIZE +#define MYNEWT_VAL_BLE_LL_WHITELIST_SIZE (8) +#endif + +#ifndef MYNEWT_VAL_BLE_LP_CLOCK +#define MYNEWT_VAL_BLE_LP_CLOCK (1) +#endif + +#ifndef MYNEWT_VAL_BLE_NUM_COMP_PKT_RATE +#define MYNEWT_VAL_BLE_NUM_COMP_PKT_RATE ((2 * OS_TICKS_PER_SEC)) +#endif + +#ifndef MYNEWT_VAL_BLE_PUBLIC_DEV_ADDR +#define MYNEWT_VAL_BLE_PUBLIC_DEV_ADDR ((uint8_t[6]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) +#endif + +#ifndef MYNEWT_VAL_BLE_XTAL_SETTLE_TIME +#define MYNEWT_VAL_BLE_XTAL_SETTLE_TIME (0) +#endif + +/*** @apache-mynewt-nimble/nimble/host */ +#ifndef MYNEWT_VAL_BLE_ATT_PREFERRED_MTU +#define MYNEWT_VAL_BLE_ATT_PREFERRED_MTU CONFIG_NIMBLE_ATT_PREFERRED_MTU +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_FIND_INFO +#define MYNEWT_VAL_BLE_ATT_SVR_FIND_INFO (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_FIND_TYPE +#define MYNEWT_VAL_BLE_ATT_SVR_FIND_TYPE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_INDICATE +#define MYNEWT_VAL_BLE_ATT_SVR_INDICATE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_MAX_PREP_ENTRIES +#define MYNEWT_VAL_BLE_ATT_SVR_MAX_PREP_ENTRIES (64) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_NOTIFY +#define MYNEWT_VAL_BLE_ATT_SVR_NOTIFY (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_QUEUED_WRITE +#define MYNEWT_VAL_BLE_ATT_SVR_QUEUED_WRITE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_QUEUED_WRITE_TMO +#define MYNEWT_VAL_BLE_ATT_SVR_QUEUED_WRITE_TMO (30000) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_READ +#define MYNEWT_VAL_BLE_ATT_SVR_READ (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_READ_BLOB +#define MYNEWT_VAL_BLE_ATT_SVR_READ_BLOB (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_READ_GROUP_TYPE +#define MYNEWT_VAL_BLE_ATT_SVR_READ_GROUP_TYPE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_READ_MULT +#define MYNEWT_VAL_BLE_ATT_SVR_READ_MULT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_READ_TYPE +#define MYNEWT_VAL_BLE_ATT_SVR_READ_TYPE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_SIGNED_WRITE +#define MYNEWT_VAL_BLE_ATT_SVR_SIGNED_WRITE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_WRITE +#define MYNEWT_VAL_BLE_ATT_SVR_WRITE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_ATT_SVR_WRITE_NO_RSP +#define MYNEWT_VAL_BLE_ATT_SVR_WRITE_NO_RSP (1) +#endif + +#ifndef MYNEWT_VAL_BLE_GAP_MAX_PENDING_CONN_PARAM_UPDATE +#define MYNEWT_VAL_BLE_GAP_MAX_PENDING_CONN_PARAM_UPDATE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_DISC_ALL_CHRS +#define MYNEWT_VAL_BLE_GATT_DISC_ALL_CHRS (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_DISC_ALL_DSCS +#define MYNEWT_VAL_BLE_GATT_DISC_ALL_DSCS (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_DISC_ALL_SVCS +#define MYNEWT_VAL_BLE_GATT_DISC_ALL_SVCS (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_DISC_CHR_UUID +#define MYNEWT_VAL_BLE_GATT_DISC_CHR_UUID (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_DISC_SVC_UUID +#define MYNEWT_VAL_BLE_GATT_DISC_SVC_UUID (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_FIND_INC_SVCS +#define MYNEWT_VAL_BLE_GATT_FIND_INC_SVCS (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_INDICATE +#define MYNEWT_VAL_BLE_GATT_INDICATE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_MAX_PROCS +#define MYNEWT_VAL_BLE_GATT_MAX_PROCS (4) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_NOTIFY +#define MYNEWT_VAL_BLE_GATT_NOTIFY (1) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_READ +#define MYNEWT_VAL_BLE_GATT_READ (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_READ_LONG +#define MYNEWT_VAL_BLE_GATT_READ_LONG (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_READ_MAX_ATTRS +#define MYNEWT_VAL_BLE_GATT_READ_MAX_ATTRS (8) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_READ_MULT +#define MYNEWT_VAL_BLE_GATT_READ_MULT (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_READ_UUID +#define MYNEWT_VAL_BLE_GATT_READ_UUID (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_RESUME_RATE +#define MYNEWT_VAL_BLE_GATT_RESUME_RATE (1000) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_SIGNED_WRITE +#define MYNEWT_VAL_BLE_GATT_SIGNED_WRITE (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_WRITE +#define MYNEWT_VAL_BLE_GATT_WRITE (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_WRITE_LONG +#define MYNEWT_VAL_BLE_GATT_WRITE_LONG (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_WRITE_MAX_ATTRS +#define MYNEWT_VAL_BLE_GATT_WRITE_MAX_ATTRS (4) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_WRITE_NO_RSP +#define MYNEWT_VAL_BLE_GATT_WRITE_NO_RSP (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_GATT_WRITE_RELIABLE +#define MYNEWT_VAL_BLE_GATT_WRITE_RELIABLE (MYNEWT_VAL_BLE_ROLE_CENTRAL) +#endif + +#ifndef MYNEWT_VAL_BLE_HOST +#define MYNEWT_VAL_BLE_HOST (1) +#endif + +#ifndef MYNEWT_VAL_ESP_BLE_MESH +#ifdef CONFIG_BLE_MESH_HCI_5_0 +#define MYNEWT_VAL_ESP_BLE_MESH (1) +#else +#define MYNEWT_VAL_ESP_BLE_MESH (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_HS_DEBUG +#ifdef CONFIG_NIMBLE_DEBUG +#define MYNEWT_VAL_BLE_HS_DEBUG (1) +#else +#define MYNEWT_VAL_BLE_HS_DEBUG (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SM_SC_DEBUG_KEYS +#ifdef CONFIG_NIMBLE_SM_SC_DEBUG_KEYS +#define MYNEWT_VAL_BLE_SM_SC_DEBUG_KEYS (1) +#else +#define MYNEWT_VAL_BLE_SM_SC_DEBUG_KEYS (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_HS_AUTO_START +#define MYNEWT_VAL_BLE_HS_AUTO_START (1) +#endif + +#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL +#ifdef CONFIG_NIMBLE_HS_FLOW_CTRL +#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (1) +#else +#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL_ITVL +#define MYNEWT_VAL_BLE_HS_FLOW_CTRL_ITVL CONFIG_NIMBLE_HS_FLOW_CTRL_ITVL +#endif + +#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL_THRESH +#define MYNEWT_VAL_BLE_HS_FLOW_CTRL_THRESH CONFIG_NIMBLE_HS_FLOW_CTRL_THRESH +#endif + +#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL_TX_ON_DISCONNECT +#define MYNEWT_VAL_BLE_HS_FLOW_CTRL_TX_ON_DISCONNECT CONFIG_NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT +#endif + +#ifndef MYNEWT_VAL_BLE_HS_PHONY_HCI_ACKS +#define MYNEWT_VAL_BLE_HS_PHONY_HCI_ACKS (0) +#endif + +#ifndef MYNEWT_VAL_BLE_HS_REQUIRE_OS +#define MYNEWT_VAL_BLE_HS_REQUIRE_OS (1) +#endif + +#ifndef MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM +#define MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM CONFIG_NIMBLE_L2CAP_COC_MAX_NUM +#endif + +#ifndef MYNEWT_VAL_BLE_L2CAP_JOIN_RX_FRAGS +#define MYNEWT_VAL_BLE_L2CAP_JOIN_RX_FRAGS (1) +#endif + +#ifndef MYNEWT_VAL_BLE_L2CAP_MAX_CHANS +#define MYNEWT_VAL_BLE_L2CAP_MAX_CHANS (3*MYNEWT_VAL_BLE_MAX_CONNECTIONS) +#endif + +#ifndef MYNEWT_VAL_BLE_L2CAP_RX_FRAG_TIMEOUT +#define MYNEWT_VAL_BLE_L2CAP_RX_FRAG_TIMEOUT (30000) +#endif + +#ifndef MYNEWT_VAL_BLE_L2CAP_SIG_MAX_PROCS +#define MYNEWT_VAL_BLE_L2CAP_SIG_MAX_PROCS (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH +#ifdef CONFIG_NIMBLE_MESH +#define MYNEWT_VAL_BLE_MESH (1) +#else +#define MYNEWT_VAL_BLE_MESH (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_CONSOLE_BUFFER_SIZE +#define MYNEWT_VAL_BLE_MONITOR_CONSOLE_BUFFER_SIZE (128) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_RTT +#define MYNEWT_VAL_BLE_MONITOR_RTT (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_RTT_BUFFERED +#define MYNEWT_VAL_BLE_MONITOR_RTT_BUFFERED (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_RTT_BUFFER_NAME +#define MYNEWT_VAL_BLE_MONITOR_RTT_BUFFER_NAME ("monitor") +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_RTT_BUFFER_SIZE +#define MYNEWT_VAL_BLE_MONITOR_RTT_BUFFER_SIZE (256) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_UART +#define MYNEWT_VAL_BLE_MONITOR_UART (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_UART_BAUDRATE +#define MYNEWT_VAL_BLE_MONITOR_UART_BAUDRATE (1000000) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_UART_BUFFER_SIZE +#define MYNEWT_VAL_BLE_MONITOR_UART_BUFFER_SIZE (64) +#endif + +#ifndef MYNEWT_VAL_BLE_MONITOR_UART_DEV +#define MYNEWT_VAL_BLE_MONITOR_UART_DEV ("uart0") +#endif + +#ifndef MYNEWT_VAL_BLE_HOST_BASED_PRIVACY +#define MYNEWT_VAL_BLE_HOST_BASED_PRIVACY (1) +#endif + +#ifndef MYNEWT_VAL_BLE_RPA_TIMEOUT +#define MYNEWT_VAL_BLE_RPA_TIMEOUT (CONFIG_NIMBLE_RPA_TIMEOUT) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_BONDING +#define MYNEWT_VAL_BLE_SM_BONDING (1) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_IO_CAP +#define MYNEWT_VAL_BLE_SM_IO_CAP (BLE_HS_IO_NO_INPUT_OUTPUT) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_KEYPRESS +#define MYNEWT_VAL_BLE_SM_KEYPRESS (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_LEGACY +#ifdef CONFIG_NIMBLE_SM_LEGACY +#define MYNEWT_VAL_BLE_SM_LEGACY (1) +#else +#define MYNEWT_VAL_BLE_SM_LEGACY (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SM_MAX_PROCS +#define MYNEWT_VAL_BLE_SM_MAX_PROCS (1) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_MITM +#define MYNEWT_VAL_BLE_SM_MITM (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_OOB_DATA_FLAG +#define MYNEWT_VAL_BLE_SM_OOB_DATA_FLAG (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_OUR_KEY_DIST +#define MYNEWT_VAL_BLE_SM_OUR_KEY_DIST (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SM_SC +#ifdef CONFIG_NIMBLE_SM_SC +#define MYNEWT_VAL_BLE_SM_SC (1) +#else +#define MYNEWT_VAL_BLE_SM_SC (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SM_THEIR_KEY_DIST +#define MYNEWT_VAL_BLE_SM_THEIR_KEY_DIST (0) +#endif + +#ifndef MYNEWT_VAL_BLE_CRYPTO_STACK_MBEDTLS +#define MYNEWT_VAL_BLE_CRYPTO_STACK_MBEDTLS (CONFIG_NIMBLE_CRYPTO_STACK_MBEDTLS) +#endif + +#ifndef MYNEWT_VAL_BLE_STORE_MAX_BONDS +#define MYNEWT_VAL_BLE_STORE_MAX_BONDS CONFIG_NIMBLE_MAX_BONDS +#endif + +#ifndef MYNEWT_VAL_BLE_STORE_MAX_CCCDS +#define MYNEWT_VAL_BLE_STORE_MAX_CCCDS CONFIG_NIMBLE_MAX_CCCDS +#endif + +#ifndef MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST +#ifdef CONFIG_NIMBLE_NVS_PERSIST +#define MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST (1) +#else +#define MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST (0) +#endif +#endif + +/*** nimble/host/services/ans */ +#ifndef MYNEWT_VAL_BLE_SVC_ANS_NEW_ALERT_CAT +#define MYNEWT_VAL_BLE_SVC_ANS_NEW_ALERT_CAT (0) +#endif + + +#ifndef MYNEWT_VAL_BLE_SVC_ANS_UNR_ALERT_CAT +#define MYNEWT_VAL_BLE_SVC_ANS_UNR_ALERT_CAT (0) +#endif + +/*** nimble/host/services/bas */ +#ifndef MYNEWT_VAL_BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE +#define MYNEWT_VAL_BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_BAS_BATTERY_LEVEL_READ_PERM +#define MYNEWT_VAL_BLE_SVC_BAS_BATTERY_LEVEL_READ_PERM (0) +#endif +#ifndef MYNEWT_VAL_BLE_MESH_ADV_TASK_PRIO +#define MYNEWT_VAL_BLE_MESH_ADV_TASK_PRIO (9) +#endif + + +/*** @apache-mynewt-nimble/nimble/host/mesh */ +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_ADV_BUF_COUNT +#define MYNEWT_VAL_BLE_MESH_ADV_BUF_COUNT (20) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_APP_KEY_COUNT +#define MYNEWT_VAL_BLE_MESH_APP_KEY_COUNT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_CFG_CLI +#define MYNEWT_VAL_BLE_MESH_CFG_CLI (0) +#endif +#ifndef MYNEWT_VAL_BLE_MESH_CRPL +#define MYNEWT_VAL_BLE_MESH_CRPL (10) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG +#define MYNEWT_VAL_BLE_MESH_DEBUG (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_ACCESS +#define MYNEWT_VAL_BLE_MESH_DEBUG_ACCESS (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_ADV +#define MYNEWT_VAL_BLE_MESH_DEBUG_ADV (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG +#define MYNEWT_VAL_BLE_MESH_DEBUG (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_ACCESS +#define MYNEWT_VAL_BLE_MESH_DEBUG_ACCESS (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_ADV +#define MYNEWT_VAL_BLE_MESH_DEBUG_ADV (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_BEACON +#define MYNEWT_VAL_BLE_MESH_DEBUG_BEACON (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_CRYPTO +#define MYNEWT_VAL_BLE_MESH_DEBUG_CRYPTO (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_FRIEND +#define MYNEWT_VAL_BLE_MESH_DEBUG_FRIEND (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_LOW_POWER +#define MYNEWT_VAL_BLE_MESH_DEBUG_LOW_POWER (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_MODEL +#define MYNEWT_VAL_BLE_MESH_DEBUG_MODEL (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_NET +#define MYNEWT_VAL_BLE_MESH_DEBUG_NET (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_PROV +#define MYNEWT_VAL_BLE_MESH_DEBUG_PROV (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_PROXY +#define MYNEWT_VAL_BLE_MESH_DEBUG_PROXY (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_SETTINGS +#define MYNEWT_VAL_BLE_MESH_DEBUG_SETTINGS (1) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_DEBUG_TRANS +#define MYNEWT_VAL_BLE_MESH_DEBUG_TRANS (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_DEVICE_NAME +#define MYNEWT_VAL_BLE_MESH_DEVICE_NAME CONFIG_NIMBLE_MESH_DEVICE_NAME +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_DEV_UUID +#define MYNEWT_VAL_BLE_MESH_DEV_UUID (((uint8_t[16]){0x11, 0x22, 0})) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND +#ifdef CONFIG_NIMBLE_MESH_FRIEND +#define MYNEWT_VAL_BLE_MESH_FRIEND (1) +#else +#define MYNEWT_VAL_BLE_MESH_FRIEND (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND_LPN_COUNT +#define MYNEWT_VAL_BLE_MESH_FRIEND_LPN_COUNT (2) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND_QUEUE_SIZE +#define MYNEWT_VAL_BLE_MESH_FRIEND_QUEUE_SIZE (16) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND_RECV_WIN +#define MYNEWT_VAL_BLE_MESH_FRIEND_RECV_WIN (255) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND_SEG_RX +#define MYNEWT_VAL_BLE_MESH_FRIEND_SEG_RX (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_FRIEND_SUB_LIST_SIZE +#define MYNEWT_VAL_BLE_MESH_FRIEND_SUB_LIST_SIZE (3) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_GATT_PROXY +#ifdef CONFIG_NIMBLE_MESH_GATT_PROXY +#define MYNEWT_VAL_BLE_MESH_GATT_PROXY (1) +#else +#define MYNEWT_VAL_BLE_MESH_GATT_PROXY (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_HEALTH_CLI +#define MYNEWT_VAL_BLE_MESH_HEALTH_CLI (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_IVU_DIVIDER +#define MYNEWT_VAL_BLE_MESH_IVU_DIVIDER (4) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_IV_UPDATE_TEST +#define MYNEWT_VAL_BLE_MESH_IV_UPDATE_TEST (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LABEL_COUNT +#define MYNEWT_VAL_BLE_MESH_LABEL_COUNT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LOW_POWER +#ifdef CONFIG_NIMBLE_MESH_LOW_POWER +#define MYNEWT_VAL_BLE_MESH_LOW_POWER (1) +#else +#define MYNEWT_VAL_BLE_MESH_LOW_POWER (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_AUTO +#define MYNEWT_VAL_BLE_MESH_LPN_AUTO (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_AUTO_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_LPN_AUTO_TIMEOUT (15) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_ESTABLISHMENT +#define MYNEWT_VAL_BLE_MESH_LPN_ESTABLISHMENT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_GROUPS +#define MYNEWT_VAL_BLE_MESH_LPN_GROUPS (10) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_INIT_POLL_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_LPN_INIT_POLL_TIMEOUT (MYNEWT_VAL_BLE_MESH_LPN_POLL_TIMEOUT) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_MIN_QUEUE_SIZE +#define MYNEWT_VAL_BLE_MESH_LPN_MIN_QUEUE_SIZE (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_POLL_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_LPN_POLL_TIMEOUT (300) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_RECV_DELAY +#define MYNEWT_VAL_BLE_MESH_LPN_RECV_DELAY (100) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_RECV_WIN_FACTOR +#define MYNEWT_VAL_BLE_MESH_LPN_RECV_WIN_FACTOR (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_RETRY_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_LPN_RETRY_TIMEOUT (8) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_RSSI_FACTOR +#define MYNEWT_VAL_BLE_MESH_LPN_RSSI_FACTOR (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_LPN_SCAN_LATENCY +#define MYNEWT_VAL_BLE_MESH_LPN_SCAN_LATENCY (10) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_MODEL_GROUP_COUNT +#define MYNEWT_VAL_BLE_MESH_MODEL_GROUP_COUNT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_MODEL_KEY_COUNT +#define MYNEWT_VAL_BLE_MESH_MODEL_KEY_COUNT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_MSG_CACHE_SIZE +#define MYNEWT_VAL_BLE_MESH_MSG_CACHE_SIZE (10) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_NODE_ID_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_NODE_ID_TIMEOUT (60) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_OOB_INPUT_ACTIONS +#define MYNEWT_VAL_BLE_MESH_OOB_INPUT_ACTIONS (((BT_MESH_NO_INPUT))) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_OOB_INPUT_SIZE +#define MYNEWT_VAL_BLE_MESH_OOB_INPUT_SIZE (4) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_OOB_OUTPUT_ACTIONS +#define MYNEWT_VAL_BLE_MESH_OOB_OUTPUT_ACTIONS (((BT_MESH_DISPLAY_NUMBER))) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_OOB_OUTPUT_SIZE +#define MYNEWT_VAL_BLE_MESH_OOB_OUTPUT_SIZE (4) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_PB_ADV +#ifdef CONFIG_NIMBLE_MESH_PB_ADV +#define MYNEWT_VAL_BLE_MESH_PB_ADV (1) +#else +#define MYNEWT_VAL_BLE_MESH_PB_ADV (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_PB_GATT +#ifdef CONFIG_NIMBLE_MESH_PB_GATT +#define MYNEWT_VAL_BLE_MESH_PB_GATT (1) +#else +#define MYNEWT_VAL_BLE_MESH_PB_GATT (0) +#endif +#endif + +/* Overridden by @apache-mynewt-nimble/nimble/host/mesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_PROV +#ifdef CONFIG_NIMBLE_MESH_PROV +#define MYNEWT_VAL_BLE_MESH_PROV (1) +#else +#define MYNEWT_VAL_BLE_MESH_PROV (0) +#endif +#endif + +/* Overridden by @apache-mynewt-nimble/nimble/host/mesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_PROXY +#ifdef CONFIG_NIMBLE_MESH_PROXY +#define MYNEWT_VAL_BLE_MESH_PROXY (1) +#else +#define MYNEWT_VAL_BLE_MESH_PROXY (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_PROXY_FILTER_SIZE +#define MYNEWT_VAL_BLE_MESH_PROXY_FILTER_SIZE (1) +#endif + + +#ifndef MYNEWT_VAL_BLE_MESH_RELAY +#ifdef CONFIG_NIMBLE_MESH_RELAY +#define MYNEWT_VAL_BLE_MESH_RELAY (1) +#else +#define MYNEWT_VAL_BLE_MESH_RELAY (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_RPL_STORE_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_RPL_STORE_TIMEOUT (5) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_RX_SDU_MAX +#define MYNEWT_VAL_BLE_MESH_RX_SDU_MAX (72) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_RX_SEG_MSG_COUNT +#define MYNEWT_VAL_BLE_MESH_RX_SEG_MSG_COUNT (2) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_SEQ_STORE_RATE +#define MYNEWT_VAL_BLE_MESH_SEQ_STORE_RATE (128) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_SETTINGS +#define MYNEWT_VAL_BLE_MESH_SETTINGS (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_SHELL +#define MYNEWT_VAL_BLE_MESH_SHELL (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_SHELL_MODELS +#define MYNEWT_VAL_BLE_MESH_SHELL_MODELS (0) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_STORE_TIMEOUT +#define MYNEWT_VAL_BLE_MESH_STORE_TIMEOUT (2) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_SUBNET_COUNT +#define MYNEWT_VAL_BLE_MESH_SUBNET_COUNT (1) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_TESTING +#define MYNEWT_VAL_BLE_MESH_TESTING (0) +#endif + +/* Overridden by apps/blemesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */ +#ifndef MYNEWT_VAL_BLE_MESH_TX_SEG_MAX +#define MYNEWT_VAL_BLE_MESH_TX_SEG_MAX (6) +#endif + +#ifndef MYNEWT_VAL_BLE_MESH_TX_SEG_MSG_COUNT +#define MYNEWT_VAL_BLE_MESH_TX_SEG_MSG_COUNT (4) +#endif + +/*** @apache-mynewt-nimble/nimble/host/services/gap */ +#ifndef MYNEWT_VAL_BLE_SVC_GAP_APPEARANCE +#define MYNEWT_VAL_BLE_SVC_GAP_APPEARANCE CONFIG_NIMBLE_SVC_GAP_APPEARANCE +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_APPEARANCE_WRITE_PERM +#define MYNEWT_VAL_BLE_SVC_GAP_APPEARANCE_WRITE_PERM (-1) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION +#define MYNEWT_VAL_BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION (-1) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME +#define MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME CONFIG_NIMBLE_SVC_GAP_DEVICE_NAME +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH +#define MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH CONFIG_NIMBLE_GAP_DEVICE_NAME_MAX_LEN +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM +#define MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM (-1) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_PPCP_MAX_CONN_INTERVAL +#define MYNEWT_VAL_BLE_SVC_GAP_PPCP_MAX_CONN_INTERVAL (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_PPCP_MIN_CONN_INTERVAL +#define MYNEWT_VAL_BLE_SVC_GAP_PPCP_MIN_CONN_INTERVAL (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_PPCP_SLAVE_LATENCY +#define MYNEWT_VAL_BLE_SVC_GAP_PPCP_SLAVE_LATENCY (0) +#endif + +#ifndef MYNEWT_VAL_BLE_SVC_GAP_PPCP_SUPERVISION_TMO +#define MYNEWT_VAL_BLE_SVC_GAP_PPCP_SUPERVISION_TMO (0) +#endif + +/*** nimble/transport */ +#ifndef MYNEWT_VAL_BLE_HCI_TRANSPORT_EMSPI +#define MYNEWT_VAL_BLE_HCI_TRANSPORT_EMSPI (0) +#endif + +/* Overridden by targets/porting-nimble (defined by nimble/transport) */ +#ifndef MYNEWT_VAL_BLE_HCI_TRANSPORT_NIMBLE_BUILTIN +#define MYNEWT_VAL_BLE_HCI_TRANSPORT_NIMBLE_BUILTIN (0) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_TRANSPORT_RAM +#define MYNEWT_VAL_BLE_HCI_TRANSPORT_RAM (0) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_TRANSPORT_SOCKET +#define MYNEWT_VAL_BLE_HCI_TRANSPORT_SOCKET (0) +#endif + +/* Overridden by targets/porting-nimble (defined by nimble/transport) */ +#ifndef MYNEWT_VAL_BLE_HCI_TRANSPORT_UART +#define MYNEWT_VAL_BLE_HCI_TRANSPORT_UART (1) +#endif + +/*** nimble/transport/uart */ +#ifndef MYNEWT_VAL_BLE_ACL_BUF_COUNT +#define MYNEWT_VAL_BLE_ACL_BUF_COUNT CONFIG_NIMBLE_ACL_BUF_COUNT +#endif + +#ifndef MYNEWT_VAL_BLE_ACL_BUF_SIZE +#define MYNEWT_VAL_BLE_ACL_BUF_SIZE CONFIG_NIMBLE_ACL_BUF_SIZE +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_ACL_OUT_COUNT +#define MYNEWT_VAL_BLE_HCI_ACL_OUT_COUNT (12) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_EVT_BUF_SIZE +#define MYNEWT_VAL_BLE_HCI_EVT_BUF_SIZE CONFIG_NIMBLE_HCI_EVT_BUF_SIZE +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_EVT_HI_BUF_COUNT +#define MYNEWT_VAL_BLE_HCI_EVT_HI_BUF_COUNT CONFIG_NIMBLE_HCI_EVT_HI_BUF_COUNT +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_EVT_LO_BUF_COUNT +#define MYNEWT_VAL_BLE_HCI_EVT_LO_BUF_COUNT CONFIG_NIMBLE_HCI_EVT_LO_BUF_COUNT +#endif + +/* Overridden by targets/porting-nimble (defined by nimble/transport/uart) */ +#ifndef MYNEWT_VAL_BLE_HCI_UART_BAUD +#define MYNEWT_VAL_BLE_HCI_UART_BAUD (115200) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_UART_DATA_BITS +#define MYNEWT_VAL_BLE_HCI_UART_DATA_BITS (8) +#endif + +/* Overridden by targets/porting-nimble (defined by nimble/transport/uart) */ +#ifndef MYNEWT_VAL_BLE_HCI_UART_FLOW_CTRL +#define MYNEWT_VAL_BLE_HCI_UART_FLOW_CTRL (0) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_UART_PARITY +#define MYNEWT_VAL_BLE_HCI_UART_PARITY (HAL_UART_PARITY_NONE) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_UART_PORT +#define MYNEWT_VAL_BLE_HCI_UART_PORT (0) +#endif + +#ifndef MYNEWT_VAL_BLE_HCI_UART_STOP_BITS +#define MYNEWT_VAL_BLE_HCI_UART_STOP_BITS (1) +#endif + +#endif diff --git a/components/nimble/port/include/esp_nimble_mem.h b/components/nimble/port/include/esp_nimble_mem.h new file mode 100644 index 000000000..90e52a20d --- /dev/null +++ b/components/nimble/port/include/esp_nimble_mem.h @@ -0,0 +1,39 @@ +/* + * Copyright 2020 Espressif Systems (Shanghai) PTE LTD + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __ESP_NIMBLE_MEM_H__ +#define __ESP_NIMBLE_MEM_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void *nimble_platform_mem_malloc(size_t size); +void *nimble_platform_mem_calloc(size_t n, size_t size); +void nimble_platform_mem_free(void *ptr); + +#ifdef __cplusplus +} +#endif + +#endif /* __ESP_NIMBLE_MEM_H__ */ diff --git a/components/nimble/port/src/esp_nimble_mem.c b/components/nimble/port/src/esp_nimble_mem.c new file mode 100644 index 000000000..ee2013034 --- /dev/null +++ b/components/nimble/port/src/esp_nimble_mem.c @@ -0,0 +1,52 @@ +/* + * Copyright 2020 Espressif Systems (Shanghai) PTE LTD + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "esp_attr.h" +#include "esp_heap_caps.h" +#include "sdkconfig.h" +#include "esp_nimble_mem.h" + +IRAM_ATTR void *nimble_platform_mem_malloc(size_t size) +{ +#ifdef CONFIG_NIMBLE_MEM_ALLOC_MODE_INTERNAL + return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_NIMBLE_MEM_ALLOC_MODE_EXTERNAL + return heap_caps_malloc(size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); +#else + return malloc(size); +#endif +} + +IRAM_ATTR void *nimble_platform_mem_calloc(size_t n, size_t size) +{ +#ifdef CONFIG_NIMBLE_MEM_ALLOC_MODE_INTERNAL + return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_NIMBLE_MEM_ALLOC_MODE_EXTERNAL + return heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT); +#else + return calloc(n, size); +#endif +} + +IRAM_ATTR void nimble_platform_mem_free(void *ptr) +{ + heap_caps_free(ptr); +} diff --git a/components/partition_table/gen_esp32part.py b/components/partition_table/gen_esp32part.py index 170750740..ac8f3af6a 100755 --- a/components/partition_table/gen_esp32part.py +++ b/components/partition_table/gen_esp32part.py @@ -160,7 +160,7 @@ class PartitionTable(list): subtype = SUBTYPES[int(ptype)][subtype] except KeyError: try: - ptype = int(ptype, 0) + subtype = int(subtype, 0) except TypeError: pass diff --git a/components/partition_table/parttool.py b/components/partition_table/parttool.py index 14fcdf4fe..3a1a6bf25 100755 --- a/components/partition_table/parttool.py +++ b/components/partition_table/parttool.py @@ -253,7 +253,8 @@ def main(): print_partition_info_subparser = subparsers.add_parser("get_partition_info", help="get partition information") print_partition_info_subparser_info_type = print_partition_info_subparser.add_mutually_exclusive_group() - print_partition_info_subparser_info_type.add_argument("--info", help="type of partition information to get", nargs="+") + print_partition_info_subparser_info_type.add_argument("--info", help="type of partition information to get", + choices=["offset", "size"], default=["offset", "size"], nargs="+") print_partition_info_subparser_info_type.add_argument("--table", help="dump the partition table to a file") generate_blank_subparser = subparsers.add_parser("generate_blank_partition_file", help="generate a blank (all 0xFF) partition file of \ diff --git a/components/partition_table/project_include.cmake b/components/partition_table/project_include.cmake index 03ee63b3f..9d31a462b 100644 --- a/components/partition_table/project_include.cmake +++ b/components/partition_table/project_include.cmake @@ -45,6 +45,7 @@ function(get_partition_info variable get_part_info_args part_info) set(${variable} ${result} PARENT_SCOPE) endfunction() +# Set variables if the PHY data partition is in the flash if(CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION) get_partition_info(PHY_PARTITION_OFFSET "--partition-type data --partition-subtype phy" "offset") diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index c04059812..3b5161fdb 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -112,7 +112,7 @@ static void transport_simple_ble_read(esp_gatts_cb_event_t event, esp_gatt_if_t ESP_LOGD(TAG, "Inside read w/ session - %d on param %d %d", param->read.conn_id, param->read.handle, read_len); - if (!read_len) { + if (!read_len && !param->read.offset) { ESP_LOGD(TAG, "Reading attr value first time"); status = esp_ble_gatts_get_attr_value(param->read.handle, &read_len, &read_buf); } else { diff --git a/components/smartconfig_ack/include/smartconfig_ack.h b/components/smartconfig_ack/include/smartconfig_ack.h index be49fd3bd..39f7636eb 100644 --- a/components/smartconfig_ack/include/smartconfig_ack.h +++ b/components/smartconfig_ack/include/smartconfig_ack.h @@ -24,6 +24,8 @@ extern "C" { #define SC_ACK_TOUCH_SERVER_PORT 18266 /*!< ESP touch UDP port of server on cellphone */ #define SC_ACK_AIRKISS_SERVER_PORT 10000 /*!< Airkiss UDP port of server on cellphone */ +#define SC_ACK_AIRKISS_DEVICE_PORT 10001 /*!< Airkiss UDP port of server on device */ +#define SC_ACK_AIRKISS_TIMEOUT 1500 /*!< Airkiss read data timeout millisecond */ #define SC_ACK_TOUCH_LEN 11 /*!< Length of ESP touch ACK context */ #define SC_ACK_AIRKISS_LEN 7 /*!< Length of Airkiss ACK context */ diff --git a/components/smartconfig_ack/smartconfig_ack.c b/components/smartconfig_ack/smartconfig_ack.c index f84aad367..91edc7b2a 100644 --- a/components/smartconfig_ack/smartconfig_ack.c +++ b/components/smartconfig_ack/smartconfig_ack.c @@ -61,7 +61,7 @@ static void sc_ack_send_task(void *pvParameters) bzero(&server_addr, sizeof(struct sockaddr_in)); server_addr.sin_family = AF_INET; - server_addr.sin_addr.s_addr = inet_addr((const char*)remote_ip); + memcpy(&server_addr.sin_addr.s_addr, remote_ip, sizeof(remote_ip)); server_addr.sin_port = htons(remote_port); esp_wifi_get_mac(WIFI_IF_STA, ack->ctx.mac); @@ -86,6 +86,33 @@ static void sc_ack_send_task(void *pvParameters) setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &optval, sizeof(int)); + if (ack->type == SC_ACK_TYPE_AIRKISS) { + char data = 0; + struct sockaddr_in local_addr, from; + socklen_t sockadd_len = sizeof(struct sockaddr); + struct timeval timeout = { + SC_ACK_AIRKISS_TIMEOUT / 1000, + SC_ACK_AIRKISS_TIMEOUT % 1000 * 1000 + }; + + bzero(&local_addr, sizeof(struct sockaddr_in)); + bzero(&from, sizeof(struct sockaddr_in)); + local_addr.sin_family = AF_INET; + local_addr.sin_addr.s_addr = INADDR_ANY; + local_addr.sin_port = htons(SC_ACK_AIRKISS_DEVICE_PORT); + + bind(send_sock, (struct sockaddr *)&local_addr, sockadd_len); + setsockopt(send_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + + recvfrom(send_sock, &data, 1, 0, (struct sockaddr *)&from, &sockadd_len); + if (from.sin_addr.s_addr != INADDR_ANY) { + memcpy(remote_ip, &from.sin_addr, 4); + server_addr.sin_addr.s_addr = from.sin_addr.s_addr; + } else { + goto _end; + } + } + while (s_sc_ack_send) { /* Send smartconfig ACK every 100ms. */ vTaskDelay(100 / portTICK_RATE_MS); diff --git a/components/soc/esp32/include/soc/can_struct.h b/components/soc/esp32/include/soc/can_struct.h index fd9609344..81ab02ca0 100644 --- a/components/soc/esp32/include/soc/can_struct.h +++ b/components/soc/esp32/include/soc/can_struct.h @@ -93,7 +93,7 @@ typedef union { uint32_t tx: 1; /* IER.1 Transmit Interrupt Enable */ uint32_t err_warn: 1; /* IER.2 Error Interrupt Enable */ uint32_t data_overrun: 1; /* IER.3 Data Overrun Interrupt Enable */ - uint32_t reserved1: 1; /* Internal Reserved (Wake-up not supported) */ + uint32_t brp_div: 1; /* THIS IS NOT AN INTERRUPT. brp_div will prescale BRP by 2. Only available on ESP32 Revision 2 or later. Reserved otherwise */ uint32_t err_passive: 1; /* IER.5 Error Passive Interrupt Enable */ uint32_t arb_lost: 1; /* IER.6 Arbitration Lost Interrupt Enable */ uint32_t bus_err: 1; /* IER.7 Bus Error Interrupt Enable */ diff --git a/components/soc/esp32/include/soc/dport_access.h b/components/soc/esp32/include/soc/dport_access.h index 00e7c8381..2639e46ef 100644 --- a/components/soc/esp32/include/soc/dport_access.h +++ b/components/soc/esp32/include/soc/dport_access.h @@ -48,7 +48,7 @@ extern "C" { // After completing read operations, use DPORT_STALL_OTHER_CPU_END(). // This method uses stall other CPU while reading DPORT registers. // Useful for compatibility, as well as for large consecutive readings. -// This method is slower, but must be used if ROM functions or +// This method is slower, but must be used if ROM functions or // other code is called which accesses DPORT without any other workaround. // *) The pre-readable APB register before reading the DPORT register // helps synchronize the operation of the two CPUs, @@ -73,7 +73,7 @@ extern "C" { */ static inline uint32_t IRAM_ATTR DPORT_REG_READ(uint32_t reg) { -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) return _DPORT_REG_READ(reg); #else return esp_dport_access_reg_read(reg); @@ -106,7 +106,7 @@ static inline uint32_t IRAM_ATTR DPORT_REG_READ(uint32_t reg) */ static inline uint32_t IRAM_ATTR DPORT_SEQUENCE_REG_READ(uint32_t reg) { -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) return _DPORT_REG_READ(reg); #else return esp_dport_access_sequence_reg_read(reg); @@ -166,7 +166,7 @@ static inline uint32_t IRAM_ATTR DPORT_SEQUENCE_REG_READ(uint32_t reg) */ static inline uint32_t IRAM_ATTR DPORT_READ_PERI_REG(uint32_t reg) { -#if defined(BOOTLOADER_BUILD) || defined(CONFIG_FREERTOS_UNICORE) || !defined(ESP_PLATFORM) +#if defined(BOOTLOADER_BUILD) || !defined(CONFIG_ESP32_DPORT_WORKAROUND) || !defined(ESP_PLATFORM) return _DPORT_REG_READ(reg); #else return esp_dport_access_reg_read(reg); diff --git a/components/soc/esp32/include/soc/soc.h b/components/soc/esp32/include/soc/soc.h index 96dea8023..d1d468303 100644 --- a/components/soc/esp32/include/soc/soc.h +++ b/components/soc/esp32/include/soc/soc.h @@ -147,7 +147,7 @@ #define IS_DPORT_REG(_r) (((_r) >= DR_REG_DPORT_BASE) && (_r) <= DR_REG_DPORT_END) -#if !defined( BOOTLOADER_BUILD ) && !defined( CONFIG_FREERTOS_UNICORE ) && defined( ESP_PLATFORM ) +#if !defined( BOOTLOADER_BUILD ) && defined( CONFIG_ESP32_DPORT_WORKAROUND ) && defined( ESP_PLATFORM ) #define ASSERT_IF_DPORT_REG(_r, OP) TRY_STATIC_ASSERT(!IS_DPORT_REG(_r), (Cannot use OP for DPORT registers use DPORT_##OP)); #else #define ASSERT_IF_DPORT_REG(_r, OP) diff --git a/components/soc/esp32/rtc_init.c b/components/soc/esp32/rtc_init.c index 7236225d4..ab5eb0ecb 100644 --- a/components/soc/esp32/rtc_init.c +++ b/components/soc/esp32/rtc_init.c @@ -24,7 +24,8 @@ void rtc_init(rtc_config_t cfg) { - CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PVTMON_PU); + CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PVTMON_PU | RTC_CNTL_TXRF_I2C_PU | + RTC_CNTL_RFRX_PBUS_PU | RTC_CNTL_CKGEN_I2C_PU | RTC_CNTL_PLL_I2C_PU); REG_SET_FIELD(RTC_CNTL_TIMER1_REG, RTC_CNTL_PLL_BUF_WAIT, cfg.pll_wait); REG_SET_FIELD(RTC_CNTL_TIMER1_REG, RTC_CNTL_XTL_BUF_WAIT, cfg.xtal_wait); diff --git a/components/soc/esp32/test/test_rtc_clk.c b/components/soc/esp32/test/test_rtc_clk.c index ee05a302b..55cee7569 100644 --- a/components/soc/esp32/test/test_rtc_clk.c +++ b/components/soc/esp32/test/test_rtc_clk.c @@ -219,10 +219,7 @@ static void start_freq(rtc_slow_freq_t required_src_freq, uint32_t start_delay_m printf("Test passed successfully\n"); } -#ifdef CONFIG_SPIRAM_SUPPORT -// PSRAM tests run on ESP-WROVER-KIT boards, which have the 32k XTAL installed. -// Other tests may run on DevKitC boards, which don't have a 32k XTAL. -TEST_CASE("Test starting external RTC quartz", "[rtc_clk]") +TEST_CASE("Test starting external RTC quartz", "[rtc_clk][test_env=UT_T1_32kXTAL]") { int i = 0, fail = 0; uint32_t start_time; @@ -263,15 +260,13 @@ TEST_CASE("Test starting external RTC quartz", "[rtc_clk]") printf("Test passed successfully\n"); } -TEST_CASE("Test starting 'External 32kHz XTAL' on the board with it.", "[rtc_clk]") +TEST_CASE("Test starting 'External 32kHz XTAL' on the board with it.", "[rtc_clk][test_env=UT_T1_32kXTAL]") { start_freq(RTC_SLOW_FREQ_32K_XTAL, 200); start_freq(RTC_SLOW_FREQ_32K_XTAL, 0); } -#else - -TEST_CASE("Test starting 'External 32kHz XTAL' on the board without it.", "[rtc_clk][ignore]") +TEST_CASE("Test starting 'External 32kHz XTAL' on the board without it.", "[rtc_clk][test_env=UT_T1_no32kXTAL]") { printf("Tries to start the 'External 32kHz XTAL' on the board without it. " "Clock switching to 'Internal 150 kHz RC oscillator'.\n"); @@ -284,5 +279,3 @@ TEST_CASE("Test starting 'External 32kHz XTAL' on the board without it.", "[rtc_ start_freq(RTC_SLOW_FREQ_RTC, 200); start_freq(RTC_SLOW_FREQ_RTC, 0); } - -#endif // CONFIG_SPIRAM_SUPPORT diff --git a/components/soc/include/soc/soc_memory_layout.h b/components/soc/include/soc/soc_memory_layout.h index 5587abe8d..9bca1120a 100644 --- a/components/soc/include/soc/soc_memory_layout.h +++ b/components/soc/include/soc/soc_memory_layout.h @@ -154,6 +154,10 @@ inline static bool IRAM_ATTR esp_ptr_executable(const void *p) intptr_t ip = (intptr_t) p; return (ip >= SOC_IROM_LOW && ip < SOC_IROM_HIGH) || (ip >= SOC_IRAM_LOW && ip < SOC_IRAM_HIGH) + || (ip >= SOC_IROM_MASK_LOW && ip < SOC_IROM_MASK_HIGH) +#if defined(SOC_CACHE_APP_LOW) && defined(CONFIG_FREERTOS_UNICORE) + || (ip >= SOC_CACHE_APP_LOW && ip < SOC_CACHE_APP_HIGH) +#endif || (ip >= SOC_RTC_IRAM_LOW && ip < SOC_RTC_IRAM_HIGH); } diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index 2debccd84..b535e8008 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -522,7 +522,17 @@ esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size) goto out; } COUNTER_ADD_BYTES(read, read_size); +#ifdef ESP_PLATFORM + if (esp_ptr_external_ram(dstv)) { + spi_flash_guard_end(); + memcpy(dstv, ((uint8_t *) t) + left_off, size); + spi_flash_guard_start(); + } else { + memcpy(dstv, ((uint8_t *) t) + left_off, size); + } +#else memcpy(dstv, ((uint8_t *) t) + left_off, size); +#endif goto out; } uint8_t *dstc = (uint8_t *) dstv; diff --git a/components/spi_flash/test/test_read_write.c b/components/spi_flash/test/test_read_write.c index 8b1061b96..9d71ad315 100644 --- a/components/spi_flash/test/test_read_write.c +++ b/components/spi_flash/test/test_read_write.c @@ -29,6 +29,7 @@ #include "soc/timer_group_reg.h" #include "esp_heap_caps.h" +#define MIN_BLOCK_SIZE 12 /* Base offset in flash for tests. */ static size_t start; @@ -265,4 +266,36 @@ TEST_CASE("spi_flash_write can write from external RAM buffer", "[spi_flash]") free(buf_int); } -#endif // CONFIG_SPIRAM_SUPPORT +TEST_CASE("spi_flash_read less than 16 bytes into buffer in external RAM", "[spi_flash]") +{ + uint8_t *buf_ext_8 = (uint8_t *) heap_caps_malloc(MIN_BLOCK_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + TEST_ASSERT_NOT_NULL(buf_ext_8); + + uint8_t *buf_int_8 = (uint8_t *) heap_caps_malloc(MIN_BLOCK_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + TEST_ASSERT_NOT_NULL(buf_int_8); + + uint8_t data_8[MIN_BLOCK_SIZE]; + for (int i = 0; i < MIN_BLOCK_SIZE; i++) { + data_8[i] = i; + } + + const esp_partition_t *part = get_test_data_partition(); + TEST_ESP_OK(spi_flash_erase_range(part->address, SPI_FLASH_SEC_SIZE)); + TEST_ESP_OK(spi_flash_write(part->address, data_8, MIN_BLOCK_SIZE)); + TEST_ESP_OK(spi_flash_read(part->address, buf_ext_8, MIN_BLOCK_SIZE)); + TEST_ESP_OK(spi_flash_read(part->address, buf_int_8, MIN_BLOCK_SIZE)); + + TEST_ASSERT_EQUAL(0, memcmp(buf_ext_8, data_8, MIN_BLOCK_SIZE)); + TEST_ASSERT_EQUAL(0, memcmp(buf_int_8, data_8, MIN_BLOCK_SIZE)); + + if (buf_ext_8) { + free(buf_ext_8); + buf_ext_8 = NULL; + } + if (buf_int_8) { + free(buf_int_8); + buf_int_8 = NULL; + } +} + +#endif // CONFIG_ESP32_SPIRAM_SUPPORT diff --git a/components/tcp_transport/CMakeLists.txt b/components/tcp_transport/CMakeLists.txt index e8125a726..9c2ef846b 100644 --- a/components/tcp_transport/CMakeLists.txt +++ b/components/tcp_transport/CMakeLists.txt @@ -2,7 +2,8 @@ set(COMPONENT_SRCS "transport.c" "transport_ssl.c" "transport_tcp.c" "transport_ws.c" - "transport_utils.c") + "transport_utils.c" + "transport_strcasestr.c") set(COMPONENT_ADD_INCLUDEDIRS "include") diff --git a/components/tcp_transport/include/esp_transport_ssl.h b/components/tcp_transport/include/esp_transport_ssl.h index c42fd0935..0f83c1d6e 100644 --- a/components/tcp_transport/include/esp_transport_ssl.h +++ b/components/tcp_transport/include/esp_transport_ssl.h @@ -69,6 +69,15 @@ void esp_transport_ssl_set_client_cert_data(esp_transport_handle_t t, const char */ void esp_transport_ssl_set_client_key_data(esp_transport_handle_t t, const char *data, int len); +/** + * @brief Skip validation of certificate's common name field + * + * @note Skipping CN validation is not recommended + * + * @param t ssl transport + */ +void esp_transport_ssl_skip_common_name_check(esp_transport_handle_t t); + #ifdef __cplusplus } #endif diff --git a/components/tcp_transport/include/esp_transport_ws.h b/components/tcp_transport/include/esp_transport_ws.h index 582c5c7da..08ed2ef27 100644 --- a/components/tcp_transport/include/esp_transport_ws.h +++ b/components/tcp_transport/include/esp_transport_ws.h @@ -13,6 +13,15 @@ extern "C" { #endif +typedef enum ws_transport_opcodes { + WS_TRANSPORT_OPCODES_CONT = 0x00, + WS_TRANSPORT_OPCODES_TEXT = 0x01, + WS_TRANSPORT_OPCODES_BINARY = 0x02, + WS_TRANSPORT_OPCODES_CLOSE = 0x08, + WS_TRANSPORT_OPCODES_PING = 0x09, + WS_TRANSPORT_OPCODES_PONG = 0x0a, + WS_TRANSPORT_OPCODES_FIN = 0x80, +} ws_transport_opcodes_t; /** * @brief Create web socket transport @@ -23,8 +32,90 @@ extern "C" { */ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle); +/** + * @brief Set HTTP path to update protocol to websocket + * + * @param t websocket transport handle + * @param path The HTTP Path + */ void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path); +/** + * @brief Set websocket sub protocol header + * + * @param t websocket transport handle + * @param sub_protocol Sub protocol string + * + * @return + * - ESP_OK on success + * - One of the error codes + */ +esp_err_t esp_transport_ws_set_subprotocol(esp_transport_handle_t t, const char *sub_protocol); + +/** + * @brief Set websocket user-agent header + * + * @param t websocket transport handle + * @param sub_protocol user-agent string + * + * @return + * - ESP_OK on success + * - One of the error codes + */ +esp_err_t esp_transport_ws_set_user_agent(esp_transport_handle_t t, const char *user_agent); + +/** + * @brief Set websocket additional headers + * + * @param t websocket transport handle + * @param sub_protocol additional header strings each terminated with \r\n + * + * @return + * - ESP_OK on success + * - One of the error codes + */ +esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *headers); + +/** + * @brief Sends websocket raw message with custom opcode and payload + * + * Note that generic esp_transport_write for ws handle sends + * binary massages by default if size is > 0 and + * ping message if message size is set to 0. + * This API is provided to support explicit messages with arbitrary opcode, + * should it be PING, PONG or TEXT message with arbitrary data. + * + * @param[in] t Websocket transport handle + * @param[in] opcode ws operation code + * @param[in] buffer The buffer + * @param[in] len The length + * @param[in] timeout_ms The timeout milliseconds (-1 indicates block forever) + * + * @return + * - Number of bytes was written + * - (-1) if there are any errors, should check errno + */ +int esp_transport_ws_send_raw(esp_transport_handle_t t, ws_transport_opcodes_t opcode, const char *b, int len, int timeout_ms); + +/** + * @brief Returns websocket op-code for last received data + * + * @param t websocket transport handle + * + * @return + * - Received op-code as enum + */ +ws_transport_opcodes_t esp_transport_ws_get_read_opcode(esp_transport_handle_t t); + +/** + * @brief Returns payload length of the last received data + * + * @param t websocket transport handle + * + * @return + * - Number of bytes in the payload + */ +int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t); #ifdef __cplusplus diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index 1ea404987..28f0c1e12 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -74,30 +74,55 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int ESP_LOGE(TAG, "Failed to open a new connection"); return -1; } + return 0; } static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &readset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "ssl_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_ssl_t *ssl = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(ssl->tls->sockfd, &writeset); + FD_SET(ssl->tls->sockfd, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout); + ret = select(ssl->tls->sockfd + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(ssl->tls->sockfd, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "ssl_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd); + ret = -1; + } + return ret; } static int ssl_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) @@ -190,6 +215,14 @@ void esp_transport_ssl_set_client_key_data(esp_transport_handle_t t, const char } } +void esp_transport_ssl_skip_common_name_check(esp_transport_handle_t t) +{ + transport_ssl_t *ssl = esp_transport_get_context_data(t); + if (t && ssl) { + ssl->cfg.skip_common_name = true; + } +} + esp_transport_handle_t esp_transport_ssl_init() { esp_transport_handle_t t = esp_transport_init(); diff --git a/components/tcp_transport/transport_strcasestr.c b/components/tcp_transport/transport_strcasestr.c new file mode 100644 index 000000000..80551a4b3 --- /dev/null +++ b/components/tcp_transport/transport_strcasestr.c @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * The quadratic code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* Linear algorithm Copyright (C) 2008 Eric Blake + * Permission to use, copy, modify, and distribute the linear portion of + * software is freely granted, provided that this notice is preserved. + */ +#include "transport_strcasestr.h" +#include +#include + +char *transport_strcasestr(const char *buffer, const char *key) +{ + char c, sc; + size_t len; + + if ((c = *key++) != 0) { + c = tolower((unsigned char)c); + len = strlen(key); + do { + do { + if ((sc = *buffer++) == 0) + return (NULL); + } while ((char)tolower((unsigned char)sc) != c); + } while (strncasecmp(buffer, key, len) != 0); + buffer--; + } + return ((char *)buffer); +} diff --git a/components/tcp_transport/transport_strcasestr.h b/components/tcp_transport/transport_strcasestr.h new file mode 100644 index 000000000..e337d90d5 --- /dev/null +++ b/components/tcp_transport/transport_strcasestr.h @@ -0,0 +1,38 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * The quadratic code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* Linear algorithm Copyright (C) 2008 Eric Blake + * Permission to use, copy, modify, and distribute the linear portion of + * software is freely granted, provided that this notice is preserved. + */ + +char *transport_strcasestr(const char *buffer, const char *key); + diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index ff6925b2d..8bd429eab 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -34,18 +34,20 @@ typedef struct { static int resolve_dns(const char *host, struct sockaddr_in *ip) { - struct hostent *he; - struct in_addr **addr_list; - he = gethostbyname(host); - if (he == NULL) { - return ESP_FAIL; - } - addr_list = (struct in_addr **)he->h_addr_list; - if (addr_list[0] == NULL) { + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_STREAM, + }; + struct addrinfo *res; + + int err = getaddrinfo(host, NULL, &hints, &res); + if(err != 0 || res == NULL) { + ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res); return ESP_FAIL; } ip->sin_family = AF_INET; - memcpy(&ip->sin_addr, addr_list[0], sizeof(ip->sin_addr)); + memcpy(&ip->sin_addr, &((struct sockaddr_in *)(res->ai_addr))->sin_addr, sizeof(ip->sin_addr)); + freeaddrinfo(res); return ESP_OK; } @@ -77,6 +79,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int esp_transport_utils_ms_to_timeval(timeout_ms, &tv); setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(tcp->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); ESP_LOGD(TAG, "[sock=%d],connecting to server IP:%s,Port:%d...", tcp->sock, ipaddr_ntoa((const ip_addr_t*)&remote_ip.sin_addr.s_addr), port); @@ -115,23 +118,47 @@ static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set readset; + fd_set errset; FD_ZERO(&readset); + FD_ZERO(&errset); FD_SET(tcp->sock, &readset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, &readset, NULL, NULL, &timeout); + ret = select(tcp->sock + 1, &readset, NULL, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "tcp_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms) { transport_tcp_t *tcp = esp_transport_get_context_data(t); + int ret = -1; fd_set writeset; + fd_set errset; FD_ZERO(&writeset); + FD_ZERO(&errset); FD_SET(tcp->sock, &writeset); + FD_SET(tcp->sock, &errset); struct timeval timeout; esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); - return select(tcp->sock + 1, NULL, &writeset, NULL, &timeout); + ret = select(tcp->sock + 1, NULL, &writeset, &errset, &timeout); + if (ret > 0 && FD_ISSET(tcp->sock, &errset)) { + int sock_errno = 0; + uint32_t optlen = sizeof(sock_errno); + getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen); + ESP_LOGE(TAG, "tcp_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock); + ret = -1; + } + return ret; } static int tcp_close(esp_transport_handle_t t) diff --git a/components/tcp_transport/transport_ws.c b/components/tcp_transport/transport_ws.c index 55ea6d115..65f4a3e64 100644 --- a/components/tcp_transport/transport_ws.c +++ b/components/tcp_transport/transport_ws.c @@ -8,6 +8,7 @@ #include "esp_transport_tcp.h" #include "esp_transport_ws.h" #include "esp_transport_utils.h" +#include "transport_strcasestr.h" #include "mbedtls/base64.h" #include "mbedtls/sha1.h" @@ -24,18 +25,40 @@ static const char *TAG = "TRANSPORT_WS"; #define WS_MASK 0x80 #define WS_SIZE16 126 #define WS_SIZE64 127 -#define MAX_WEBSOCKET_HEADER_SIZE 10 +#define MAX_WEBSOCKET_HEADER_SIZE 16 #define WS_RESPONSE_OK 101 + +typedef struct { + uint8_t opcode; + char mask_key[4]; /*!< Mask key for this payload */ + int payload_len; /*!< Total length of the payload */ + int bytes_remaining; /*!< Bytes left to read of the payload */ +} ws_transport_frame_state_t; + typedef struct { char *path; char *buffer; + char *sub_protocol; + char *user_agent; + char *headers; + ws_transport_frame_state_t frame_state; esp_transport_handle_t parent; } transport_ws_t; +static inline uint8_t ws_get_bin_opcode(ws_transport_opcodes_t opcode) +{ + return (uint8_t)opcode; +} + static esp_transport_handle_t ws_get_payload_transport_handle(esp_transport_handle_t t) { transport_ws_t *ws = esp_transport_get_context_data(t); + + /* Reading parts of a frame directly will disrupt the WS internal frame state, + reset bytes_remaining to prepare for reading a new frame */ + ws->frame_state.bytes_remaining = 0; + return ws->parent; } @@ -60,10 +83,9 @@ static char *trimwhitespace(const char *str) return (char *)str; } - static char *get_http_header(const char *buffer, const char *key) { - char *found = strstr(buffer, key); + char *found = transport_strcasestr(buffer, key); if (found) { found += strlen(key); char *found_end = strstr(found, "\r\n"); @@ -80,7 +102,8 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int { transport_ws_t *ws = esp_transport_get_context_data(t); if (esp_transport_connect(ws->parent, host, port, timeout_ms) < 0) { - ESP_LOGE(TAG, "Error connect to ther server"); + ESP_LOGE(TAG, "Error connecting to host %s:%d", host, port); + return -1; } unsigned char random_key[16]; @@ -89,6 +112,10 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int // Size of base64 coded string is equal '((input_size * 4) / 3) + (input_size / 96) + 6' including Z-term unsigned char client_key[28] = {0}; + // Default values for backwards compatibility + const char *user_agent_ptr = (ws->user_agent)?(ws->user_agent):"ESP32 Websocket Client"; + const char *sub_protocol_ptr = (ws->sub_protocol)?(ws->sub_protocol):"mqtt"; + size_t outlen = 0; mbedtls_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key)); int len = snprintf(ws->buffer, DEFAULT_WS_BUFFER, @@ -97,25 +124,49 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int "Host: %s:%d\r\n" "Upgrade: websocket\r\n" "Sec-WebSocket-Version: 13\r\n" - "Sec-WebSocket-Protocol: mqtt\r\n" + "Sec-WebSocket-Protocol: %s\r\n" "Sec-WebSocket-Key: %s\r\n" - "User-Agent: ESP32 Websocket Client\r\n\r\n", + "User-Agent: %s\r\n", ws->path, - host, port, - client_key); + host, port, sub_protocol_ptr, + client_key, user_agent_ptr); if (len <= 0 || len >= DEFAULT_WS_BUFFER) { ESP_LOGE(TAG, "Error in request generation, %d", len); return -1; } + if (ws->headers) { + ESP_LOGD(TAG, "headers: %s", ws->headers); + int r = snprintf(ws->buffer + len, DEFAULT_WS_BUFFER - len, "%s", ws->headers); + len += r; + if (r <= 0 || len >= DEFAULT_WS_BUFFER) { + ESP_LOGE(TAG, "Error in request generation" + "(strncpy of headers returned %d, desired request len: %d, buffer size: %d", r, len, DEFAULT_WS_BUFFER); + return -1; + } + } + int r = snprintf(ws->buffer + len, DEFAULT_WS_BUFFER - len, "\r\n"); + len += r; + if (r <= 0 || len >= DEFAULT_WS_BUFFER) { + ESP_LOGE(TAG, "Error in request generation" + "(snprintf of header terminal returned %d, desired request len: %d, buffer size: %d", r, len, DEFAULT_WS_BUFFER); + return -1; + } ESP_LOGD(TAG, "Write upgrate request\r\n%s", ws->buffer); if (esp_transport_write(ws->parent, ws->buffer, len, timeout_ms) <= 0) { ESP_LOGE(TAG, "Error write Upgrade header %s", ws->buffer); return -1; } - if ((len = esp_transport_read(ws->parent, ws->buffer, DEFAULT_WS_BUFFER, timeout_ms)) <= 0) { - ESP_LOGE(TAG, "Error read response for Upgrade header %s", ws->buffer); - return -1; - } + int header_len = 0; + do { + if ((len = esp_transport_read(ws->parent, ws->buffer + header_len, DEFAULT_WS_BUFFER - header_len, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read response for Upgrade header %s", ws->buffer); + return -1; + } + header_len += len; + ws->buffer[header_len] = '\0'; + ESP_LOGD(TAG, "Read header chunk %d, current header size: %d", len, header_len); + } while (NULL == strstr(ws->buffer, "\r\n\r\n") && header_len < DEFAULT_WS_BUFFER); + char *server_key = get_http_header(ws->buffer, "Sec-WebSocket-Accept:"); if (server_key == NULL) { ESP_LOGE(TAG, "Sec-WebSocket-Accept not found"); @@ -144,48 +195,127 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int return 0; } -static int ws_write(esp_transport_handle_t t, const char *buff, int len, int timeout_ms) +static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const char *b, int len, int timeout_ms) { transport_ws_t *ws = esp_transport_get_context_data(t); + char *buffer = (char *)b; char ws_header[MAX_WEBSOCKET_HEADER_SIZE]; char *mask; int header_len = 0, i; - char *buffer = (char *)buff; + int poll_write; if ((poll_write = esp_transport_poll_write(ws->parent, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error transport_poll_write"); return poll_write; } + ws_header[header_len++] = opcode; - ws_header[header_len++] = WS_OPCODE_BINARY | WS_FIN; - - // NOTE: no support for > 16-bit sized messages - if (len > 125) { - ws_header[header_len++] = WS_SIZE16 | WS_MASK; + if (len <= 125) { + ws_header[header_len++] = (uint8_t)(len | mask_flag); + } else if (len < 65536) { + ws_header[header_len++] = WS_SIZE16 | mask_flag; ws_header[header_len++] = (uint8_t)(len >> 8); ws_header[header_len++] = (uint8_t)(len & 0xFF); } else { - ws_header[header_len++] = (uint8_t)(len | WS_MASK); + ws_header[header_len++] = WS_SIZE64 | mask_flag; + /* Support maximum 4 bytes length */ + ws_header[header_len++] = 0; //(uint8_t)((len >> 56) & 0xFF); + ws_header[header_len++] = 0; //(uint8_t)((len >> 48) & 0xFF); + ws_header[header_len++] = 0; //(uint8_t)((len >> 40) & 0xFF); + ws_header[header_len++] = 0; //(uint8_t)((len >> 32) & 0xFF); + ws_header[header_len++] = (uint8_t)((len >> 24) & 0xFF); + ws_header[header_len++] = (uint8_t)((len >> 16) & 0xFF); + ws_header[header_len++] = (uint8_t)((len >> 8) & 0xFF); + ws_header[header_len++] = (uint8_t)((len >> 0) & 0xFF); } - mask = &ws_header[header_len]; - getrandom(ws_header + header_len, 4, 0); - header_len += 4; - for (i = 0; i < len; ++i) { - buffer[i] = (buffer[i] ^ mask[i % 4]); + if (mask_flag) { + mask = &ws_header[header_len]; + getrandom(ws_header + header_len, 4, 0); + header_len += 4; + + for (i = 0; i < len; ++i) { + buffer[i] = (buffer[i] ^ mask[i % 4]); + } } + if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) { ESP_LOGE(TAG, "Error write header"); return -1; } - return esp_transport_write(ws->parent, buffer, len, timeout_ms); + + if (len == 0) { + return 0; + } + + int ret = esp_transport_write(ws->parent, buffer, len, timeout_ms); + // in case of masked transport we have to revert back to the original data, as ws layer + // does not create its own copy of data to be sent + if (mask_flag) { + mask = &ws_header[header_len-4]; + for (i = 0; i < len; ++i) { + buffer[i] = (buffer[i] ^ mask[i % 4]); + } + } + return ret; } -static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) +int esp_transport_ws_send_raw(esp_transport_handle_t t, ws_transport_opcodes_t opcode, const char *b, int len, int timeout_ms) +{ + uint8_t op_code = ws_get_bin_opcode(opcode); + if (t == NULL) { + ESP_LOGE(TAG, "Transport must be a valid ws handle"); + return ESP_ERR_INVALID_ARG; + } + ESP_LOGD(TAG, "Sending raw ws message with opcode %d", op_code); + return _ws_write(t, op_code, WS_MASK, b, len, timeout_ms); +} + +static int ws_write(esp_transport_handle_t t, const char *b, int len, int timeout_ms) +{ + return _ws_write(t, WS_OPCODE_BINARY | WS_FIN, WS_MASK, b, len, timeout_ms); +} + + +static int ws_read_payload(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) +{ + transport_ws_t *ws = esp_transport_get_context_data(t); + + int bytes_to_read; + int rlen = 0; + + if (ws->frame_state.bytes_remaining > len) { + ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", ws->frame_state.bytes_remaining, len); + bytes_to_read = len; + + } else { + bytes_to_read = ws->frame_state.bytes_remaining; + } + + // Receive and process payload + if (bytes_to_read != 0 && (rlen = esp_transport_read(ws->parent, buffer, bytes_to_read, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read data"); + return rlen; + } + ws->frame_state.bytes_remaining -= rlen; + + if (ws->frame_state.mask_key) { + for (int i = 0; i < bytes_to_read; i++) { + buffer[i] = (buffer[i] ^ ws->frame_state.mask_key[i % 4]); + } + } + return rlen; +} + + +/* Read and parse the WS header, determine length of payload */ +static int ws_read_header(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) { transport_ws_t *ws = esp_transport_get_context_data(t); int payload_len; - int payload_len_buff = len; - char *data_ptr = buffer, opcode, mask, *mask_key = NULL; + + char ws_header[MAX_WEBSOCKET_HEADER_SIZE]; + char *data_ptr = ws_header, mask; int rlen; int poll_read; if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) { @@ -194,16 +324,17 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ // Receive and process header first (based on header size) int header = 2; - if ((rlen = esp_transport_read(ws->parent, buffer, header, timeout_ms)) <= 0) { + int mask_len = 4; + if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) { ESP_LOGE(TAG, "Error read data"); return rlen; } - opcode = (*data_ptr & 0x0F); + ws->frame_state.opcode = (*data_ptr & 0x0F); data_ptr ++; mask = ((*data_ptr >> 7) & 0x01); payload_len = (*data_ptr & 0x7F); data_ptr++; - ESP_LOGD(TAG, "Opcode: %d, mask: %d, len: %d\r\n", opcode, mask, payload_len); + ESP_LOGD(TAG, "Opcode: %d, mask: %d, len: %d\r\n", ws->frame_state.opcode, mask, payload_len); if (payload_len == 126) { // headerLen += 2; if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) { @@ -211,8 +342,6 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ return rlen; } payload_len = data_ptr[0] << 8 | data_ptr[1]; - payload_len_buff = len - 4; - data_ptr += 2; } else if (payload_len == 127) { // headerLen += 8; header = 8; @@ -227,31 +356,50 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ } else { payload_len = data_ptr[4] << 24 | data_ptr[5] << 16 | data_ptr[6] << 8 | data_ptr[7]; } - data_ptr += 8; - payload_len_buff = len - 10; - } - // Then receive and process payload - if ((rlen = esp_transport_read(ws->parent, data_ptr, payload_len, timeout_ms)) <= 0) { - ESP_LOGE(TAG, "Error read data"); - return rlen; - } - if (payload_len > payload_len_buff) { - ESP_LOGD(TAG, "Actual data received (%d) are longer than mqtt buffer (%d)", payload_len, payload_len_buff); - payload_len = payload_len_buff; } if (mask) { - mask_key = data_ptr; - data_ptr += 4; - for (int i = 0; i < payload_len; i++) { - buffer[i] = (data_ptr[i] ^ mask_key[i % 4]); + // Read and store mask + if (payload_len != 0 && (rlen = esp_transport_read(ws->parent, buffer, mask_len, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read data"); + return rlen; } + memcpy(ws->frame_state.mask_key, buffer, mask_len); } else { - memmove(buffer, data_ptr, payload_len); + memset(ws->frame_state.mask_key, 0, mask_len); } + + ws->frame_state.payload_len = payload_len; + ws->frame_state.bytes_remaining = payload_len; + return payload_len; } +static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) +{ + int rlen = 0; + transport_ws_t *ws = esp_transport_get_context_data(t); + + // If message exceeds buffer len then subsequent reads will skip reading header and read whatever is left of the payload + if (ws->frame_state.bytes_remaining <= 0) { + if ( (rlen = ws_read_header(t, buffer, len, timeout_ms)) <= 0) { + // If something when wrong then we prepare for reading a new header + ws->frame_state.bytes_remaining = 0; + return rlen; + } + } + if (ws->frame_state.payload_len) { + if ( (rlen = ws_read_payload(t, buffer, len, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error reading payload data"); + ws->frame_state.bytes_remaining = 0; + return rlen; + } + } + + return rlen; +} + + static int ws_poll_read(esp_transport_handle_t t, int timeout_ms) { transport_ws_t *ws = esp_transport_get_context_data(t); @@ -275,6 +423,9 @@ static esp_err_t ws_destroy(esp_transport_handle_t t) transport_ws_t *ws = esp_transport_get_context_data(t); free(ws->buffer); free(ws->path); + free(ws->sub_protocol); + free(ws->user_agent); + free(ws->headers); free(ws); return 0; } @@ -284,6 +435,7 @@ void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path) ws->path = realloc(ws->path, strlen(path) + 1); strcpy(ws->path, path); } + esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle) { esp_transport_handle_t t = esp_transport_init(); @@ -292,7 +444,10 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl ws->parent = parent_handle; ws->path = strdup("/"); - ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, return NULL); + ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, { + free(ws); + return NULL; + }); ws->buffer = malloc(DEFAULT_WS_BUFFER); ESP_TRANSPORT_MEM_CHECK(TAG, ws->buffer, { free(ws->path); @@ -308,3 +463,74 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl return t; } +esp_err_t esp_transport_ws_set_subprotocol(esp_transport_handle_t t, const char *sub_protocol) +{ + if (t == NULL) { + return ESP_ERR_INVALID_ARG; + } + transport_ws_t *ws = esp_transport_get_context_data(t); + if (ws->sub_protocol) { + free(ws->sub_protocol); + } + if (sub_protocol == NULL) { + ws->sub_protocol = NULL; + return ESP_OK; + } + ws->sub_protocol = strdup(sub_protocol); + if (ws->sub_protocol == NULL) { + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +esp_err_t esp_transport_ws_set_user_agent(esp_transport_handle_t t, const char *user_agent) +{ + if (t == NULL) { + return ESP_ERR_INVALID_ARG; + } + transport_ws_t *ws = esp_transport_get_context_data(t); + if (ws->user_agent) { + free(ws->user_agent); + } + if (user_agent == NULL) { + ws->user_agent = NULL; + return ESP_OK; + } + ws->user_agent = strdup(user_agent); + if (ws->user_agent == NULL) { + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *headers) +{ + if (t == NULL) { + return ESP_ERR_INVALID_ARG; + } + transport_ws_t *ws = esp_transport_get_context_data(t); + if (ws->headers) { + free(ws->headers); + } + if (headers == NULL) { + ws->headers = NULL; + return ESP_OK; + } + ws->headers = strdup(headers); + if (ws->headers == NULL) { + return ESP_ERR_NO_MEM; + } + return ESP_OK; +} + +ws_transport_opcodes_t esp_transport_ws_get_read_opcode(esp_transport_handle_t t) +{ + transport_ws_t *ws = esp_transport_get_context_data(t); + return ws->frame_state.opcode; +} + +int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t) +{ + transport_ws_t *ws = esp_transport_get_context_data(t); + return ws->frame_state.payload_len; +} \ No newline at end of file diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 6850f23a4..848bf16ad 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -406,6 +406,21 @@ esp_err_t tcpip_adapter_create_ip6_linklocal(tcpip_adapter_if_t tcpip_if); */ esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6); +/** + * @brief Get interface global IPv6 address + * + * If the specified interface is up and a preferred global IPv6 address + * has been created for the interface, return a copy of it. + * + * @param[in] tcpip_if Interface to get global IPv6 address + * @param[out] if_ip6 IPv6 information will be returned in this argument if successful. + * + * @return + * - ESP_OK + * - ESP_FAIL If interface is down, does not have a global IPv6 address. + */ +esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6); + #if 0 esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac); diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 443fa54bb..1fa912fc9 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -325,6 +325,9 @@ esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if) tcpip_adapter_reset_ip_info(tcpip_if); } + for(int8_t i = 0 ;i < LWIP_IPV6_NUM_ADDRESSES ;i++) { + netif_ip6_addr_set(esp_netif[tcpip_if] ,i ,IP6_ADDR_ANY6); + } netif_set_addr(esp_netif[tcpip_if], IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4); netif_set_down(esp_netif[tcpip_if]); tcpip_adapter_start_ip_lost_timer(tcpip_if); @@ -536,6 +539,29 @@ esp_err_t tcpip_adapter_get_ip6_linklocal(tcpip_adapter_if_t tcpip_if, ip6_addr_ return ESP_OK; } +esp_err_t tcpip_adapter_get_ip6_global(tcpip_adapter_if_t tcpip_if, ip6_addr_t *if_ip6) +{ + ESP_LOGD(TAG, "%s esp-netif:%p", __func__, esp_netif); + + if (tcpip_if >=TCPIP_ADAPTER_IF_MAX || if_ip6 == NULL) { + return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS; + } + + int i; + struct netif *p_netif = esp_netif[tcpip_if]; + + if (p_netif != NULL && netif_is_up(p_netif)) { + for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) { + if (ip6_addr_ispreferred(netif_ip6_addr_state(p_netif, i))) { + memcpy(if_ip6, &p_netif->ip6_addr[i], sizeof(ip6_addr_t)); + return ESP_OK; + } + } + } + + return ESP_FAIL; +} + #if 0 esp_err_t tcpip_adapter_get_mac(tcpip_adapter_if_t tcpip_if, uint8_t mac[6]) { @@ -752,6 +778,7 @@ esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ dns_param.dns_type = type; dns_param.dns_info = dns; + const ip_addr_t* dns_ip = NULL; TCPIP_ADAPTER_IPC_CALL(tcpip_if, type, 0, &dns_param, tcpip_adapter_get_dns_info_api); if (!dns) { @@ -770,7 +797,10 @@ esp_err_t tcpip_adapter_get_dns_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ } if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { - dns->ip = dns_getserver(type); + dns_ip = dns_getserver(type); + if (dns_ip != NULL) { + dns->ip = *dns_ip; + } } else { dns->ip.u_addr.ip4 = dhcps_dns_getserver(); } @@ -808,6 +838,7 @@ esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if) if (p_netif != NULL && netif_is_up(p_netif)) { tcpip_adapter_ip_info_t default_ip; tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &default_ip); + dhcps_set_new_lease_cb(tcpip_adapter_dhcps_cb); dhcps_start(p_netif, default_ip.ip); dhcps_status = TCPIP_ADAPTER_DHCP_STARTED; ESP_LOGD(TAG, "dhcp server start successfully"); diff --git a/components/vfs/README.rst b/components/vfs/README.rst index e7e50fd67..bb398a171 100644 --- a/components/vfs/README.rst +++ b/components/vfs/README.rst @@ -82,8 +82,16 @@ then you need to register the VFS with :cpp:func:`start_select` and :cpp:func:`start_select` is called for setting up the environment for detection of read/write/error conditions on file descriptors belonging to the -given VFS. :cpp:func:`end_select` is called to stop/deinitialize/free the -environment which was setup by :cpp:func:`start_select`. Please refer to the +given VFS driver. + +:cpp:func:`end_select` is called to stop/deinitialize/free the +environment which was setup by :cpp:func:`start_select`. + +.. note:: + :cpp:func:`end_select` might be called without a previous :cpp:func:`start_select` call in some rare + circumstances. :cpp:func:`end_select` should fail gracefully if this is the case. + +Please refer to the reference implementation for the UART peripheral in :component_file:`vfs/vfs_uart.c` and most particularly to functions :cpp:func:`esp_vfs_dev_uart_register`, :cpp:func:`uart_start_select` and @@ -97,6 +105,10 @@ If :cpp:func:`select` is used for socket file descriptors only then one can enable the :envvar:`CONFIG_USE_ONLY_LWIP_SELECT` option which can reduce the code size and improve performance. +.. note:: + Don't change the socket driver during an active :cpp:func:`select` call or you might experience some undefined + behavior. + Paths ----- diff --git a/components/vfs/test/test_vfs_select.c b/components/vfs/test/test_vfs_select.c index 341dec1c5..4c627fb74 100644 --- a/components/vfs/test/test_vfs_select.c +++ b/components/vfs/test/test_vfs_select.c @@ -17,11 +17,11 @@ #include #include #include "unity.h" -#include "soc/uart_struct.h" #include "freertos/FreeRTOS.h" #include "driver/uart.h" #include "esp_vfs.h" #include "esp_vfs_dev.h" +#include "esp_vfs_fat.h" #include "lwip/sockets.h" #include "lwip/netdb.h" #include "test_utils.h" @@ -32,9 +32,19 @@ typedef struct { xSemaphoreHandle sem; } test_task_param_t; +typedef struct { + fd_set *rdfds; + fd_set *wrfds; + fd_set *errfds; + int maxfds; + struct timeval *tv; + int select_ret; + xSemaphoreHandle sem; +} test_select_task_param_t; + static const char message[] = "Hello world!"; -static int open_dummy_socket() +static int open_dummy_socket(void) { const struct addrinfo hints = { .ai_family = AF_INET, @@ -52,7 +62,7 @@ static int open_dummy_socket() return dummy_socket_fd; } -static int socket_init() +static int socket_init(void) { const struct addrinfo hints = { .ai_family = AF_INET, @@ -84,7 +94,7 @@ static int socket_init() return socket_fd; } -static void uart1_init() +static void uart1_init(void) { uart_config_t uart_config = { .baud_rate = 115200, @@ -492,3 +502,83 @@ TEST_CASE("concurent selects work", "[vfs]") deinit(uart_fd, socket_fd); close(dummy_socket_fd); } + +static void select_task2(void *task_param) +{ + const test_select_task_param_t *param = task_param; + + int s = select(param->maxfds, param->rdfds, param->wrfds, param->errfds, param->tv); + TEST_ASSERT_EQUAL(param->select_ret, s); + + if (param->sem) { + xSemaphoreGive(param->sem); + } + vTaskDelete(NULL); +} + +static void inline start_select_task2(test_select_task_param_t *param) +{ + xTaskCreate(select_task2, "select_task2", 4*1024, (void *) param, 5, NULL); +} + +TEST_CASE("select() works with concurrent mount", "[vfs][fatfs]") +{ + wl_handle_t test_wl_handle; + int uart_fd, socket_fd; + + init(&uart_fd, &socket_fd); + const int dummy_socket_fd = open_dummy_socket(); + + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = true, + .max_files = 2 + }; + + // select() will be waiting for a socket & UART and FATFS mount will occur in parallel + + struct timeval tv = { + .tv_sec = 1, + .tv_usec = 0, + }; + + fd_set rdfds; + FD_ZERO(&rdfds); + FD_SET(uart_fd, &rdfds); + FD_SET(dummy_socket_fd, &rdfds); + + test_select_task_param_t param = { + .rdfds = &rdfds, + .wrfds = NULL, + .errfds = NULL, + .maxfds = MAX(uart_fd, dummy_socket_fd) + 1, + .tv = &tv, + .select_ret = 0, // expected timeout + .sem = xSemaphoreCreateBinary(), + }; + TEST_ASSERT_NOT_NULL(param.sem); + + start_select_task2(¶m); + vTaskDelay(10 / portTICK_PERIOD_MS); //make sure the task has started and waits in select() + + TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &test_wl_handle)); + + TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(param.sem, 1500 / portTICK_PERIOD_MS)); + + // select() will be waiting for a socket & UART and FATFS unmount will occur in parallel + + FD_ZERO(&rdfds); + FD_SET(uart_fd, &rdfds); + FD_SET(dummy_socket_fd, &rdfds); + + start_select_task2(¶m); + vTaskDelay(10 / portTICK_PERIOD_MS); //make sure the task has started and waits in select() + + TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", test_wl_handle)); + + TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(param.sem, 1500 / portTICK_PERIOD_MS)); + + vSemaphoreDelete(param.sem); + + deinit(uart_fd, socket_fd); + close(dummy_socket_fd); +} diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 19d9dadee..3851ee11b 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -811,8 +811,12 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds return -1; } + // Capture s_vfs_count to a local variable in case a new driver is registered or removed during this actual select() + // call. s_vfs_count cannot be protected with a mutex during a select() call (which can be one without a timeout) + // because that could block the registration of new driver. + const size_t vfs_count = s_vfs_count; fds_triple_t *vfs_fds_triple; - if ((vfs_fds_triple = calloc(s_vfs_count, sizeof(fds_triple_t))) == NULL) { + if ((vfs_fds_triple = calloc(vfs_count, sizeof(fds_triple_t))) == NULL) { __errno_r(r) = ENOMEM; ESP_LOGD(TAG, "calloc is unsuccessful"); return -1; @@ -895,7 +899,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds } } - for (int i = 0; i < s_vfs_count; ++i) { + for (int i = 0; i < vfs_count; ++i) { const vfs_entry_t *vfs = get_vfs_for_index(i); fds_triple_t *item = &vfs_fds_triple[i]; @@ -910,7 +914,7 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds if (err != ESP_OK) { call_end_selects(i, vfs_fds_triple); - (void) set_global_fd_sets(vfs_fds_triple, s_vfs_count, readfds, writefds, errorfds); + (void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); if (select_sem) { vSemaphoreDelete(select_sem); select_sem = NULL; @@ -954,9 +958,9 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds xSemaphoreTake(select_sem, ticks_to_wait); } - call_end_selects(s_vfs_count, vfs_fds_triple); // for VFSs for start_select was called before + call_end_selects(vfs_count, vfs_fds_triple); // for VFSs for start_select was called before if (ret >= 0) { - ret += set_global_fd_sets(vfs_fds_triple, s_vfs_count, readfds, writefds, errorfds); + ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); } if (select_sem) { vSemaphoreDelete(select_sem); @@ -980,6 +984,8 @@ void esp_vfs_select_triggered(SemaphoreHandle_t *signal_sem) // which has a permanent FD. But in order to avoid to lock // s_fd_table_lock we go through the VFS table. for (int i = 0; i < s_vfs_count; ++i) { + // Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't + // matter here stop_socket_select() will be called for only valid VFS drivers. const vfs_entry_t *vfs = s_vfs[i]; if (vfs != NULL && vfs->vfs.stop_socket_select != NULL) { vfs->vfs.stop_socket_select(); @@ -998,6 +1004,8 @@ void esp_vfs_select_triggered_isr(SemaphoreHandle_t *signal_sem, BaseType_t *wok // which has a permanent FD. But in order to avoid to lock // s_fd_table_lock we go through the VFS table. for (int i = 0; i < s_vfs_count; ++i) { + // Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't + // matter here stop_socket_select() will be called for only valid VFS drivers. const vfs_entry_t *vfs = s_vfs[i]; if (vfs != NULL && vfs->vfs.stop_socket_select_isr != NULL) { vfs->vfs.stop_socket_select_isr(woken); diff --git a/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h b/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h index 043b14e20..eda48be0f 100644 --- a/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h +++ b/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h @@ -29,6 +29,19 @@ extern "C" { */ extern const wifi_prov_scheme_t wifi_prov_scheme_softap; +/** + * + * @brief Provide HTTPD Server handle externally. + * + * Useful in cases wherein applications need the webserver for some + * different operations, and do not want the wifi provisioning component + * to start/stop a new instance. + * + * @note This API should be called before wifi_prov_mgr_start_provisioning() + * + * @param[in] handle Handle to HTTPD server instance + */ +void wifi_prov_scheme_softap_set_httpd_handle(void *handle); #ifdef __cplusplus } #endif diff --git a/components/wifi_provisioning/src/manager.c b/components/wifi_provisioning/src/manager.c index 41b998463..46c83bb53 100644 --- a/components/wifi_provisioning/src/manager.c +++ b/components/wifi_provisioning/src/manager.c @@ -766,7 +766,7 @@ esp_err_t wifi_prov_mgr_event_handler(void *ctx, system_event_t *event) /* Only handle events when credential is received and * Wi-Fi STA is yet to complete trying the connection */ - if (prov_ctx->prov_state != WIFI_PROV_STATE_CRED_RECV) { + if (prov_ctx->prov_state < WIFI_PROV_STATE_CRED_RECV) { RELEASE_LOCK(prov_ctx_lock); return ESP_OK; } diff --git a/components/wifi_provisioning/src/scheme_ble.c b/components/wifi_provisioning/src/scheme_ble.c index add908401..6faf49bd2 100644 --- a/components/wifi_provisioning/src/scheme_ble.c +++ b/components/wifi_provisioning/src/scheme_ble.c @@ -72,8 +72,8 @@ static void *new_config(void) const uint8_t service_uuid[16] = { /* LSB <--------------------------------------- * ---------------------------------------> MSB */ - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, - 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, + 0x07, 0xed, 0x9b, 0x2d, 0x0f, 0x06, 0x7c, 0x87, + 0x9b, 0x43, 0x43, 0x6b, 0x4d, 0x24, 0x75, 0x17, }; memcpy(ble_config->service_uuid, service_uuid, sizeof(ble_config->service_uuid)); diff --git a/components/wifi_provisioning/src/scheme_softap.c b/components/wifi_provisioning/src/scheme_softap.c index 84262e937..2b0c3c242 100644 --- a/components/wifi_provisioning/src/scheme_softap.c +++ b/components/wifi_provisioning/src/scheme_softap.c @@ -33,7 +33,7 @@ typedef struct softap_config { static const char *TAG = "wifi_prov_scheme_softap"; extern const wifi_prov_scheme_t wifi_prov_scheme_softap; - +static void *scheme_softap_prov_httpd_handle; static esp_err_t start_wifi_ap(const char *ssid, const char *pass) { /* Build Wi-Fi configuration for AP mode */ @@ -85,6 +85,11 @@ static esp_err_t prov_start(protocomm_t *pc, void *config) protocomm_httpd_config_t *httpd_config = &softap_config->httpd_config; + if (scheme_softap_prov_httpd_handle) { + httpd_config->ext_handle_provided = true; + httpd_config->data.handle = scheme_softap_prov_httpd_handle; + } + /* Start protocomm server on top of HTTP */ esp_err_t err = protocomm_httpd_start(pc, httpd_config); if (err != ESP_OK) { @@ -186,6 +191,11 @@ static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, ui return ESP_OK; } +void wifi_prov_scheme_softap_set_httpd_handle(void *handle) +{ + scheme_softap_prov_httpd_handle = handle; +} + const wifi_prov_scheme_t wifi_prov_scheme_softap = { .prov_start = prov_start, .prov_stop = prov_stop, diff --git a/components/wpa_supplicant/Kconfig b/components/wpa_supplicant/Kconfig new file mode 100644 index 000000000..21175c963 --- /dev/null +++ b/components/wpa_supplicant/Kconfig @@ -0,0 +1,21 @@ +menu "Supplicant" + + config WPA_WPS_WARS + bool "Enable WPS Inter operatability Fixes" + default n + help + Select this option to enable WPS related IOT fixes with different + APs. This option fixes IOT related issues with APs which do not + follow some of the standard of WPS-2.0 specification. However + these do not include any of the security related bypassing, just + simple configuration corrections. Current fixes under this flag: + 1. Allow NULL-padded WPS attributes: Some APs keep NULL-padding + at the end of some variable length WPS Attributes. This is not + as par the WPS2.0 specs, but to avoid interop issues, ignore the + padding by reducing the attribute length by 1. + 2. Bypass WPS-Config method validation: Some APs set display/pbc + button bit without setting virtual/phycial display/button bit + which will cause M2 validation fail, bypassing WPS-Config method + validation. + +endmenu diff --git a/components/wpa_supplicant/src/crypto/crypto_mbedtls.c b/components/wpa_supplicant/src/crypto/crypto_mbedtls.c index 835af1d37..4d4a09507 100644 --- a/components/wpa_supplicant/src/crypto/crypto_mbedtls.c +++ b/components/wpa_supplicant/src/crypto/crypto_mbedtls.c @@ -269,7 +269,7 @@ struct crypto_ec *crypto_ec_init(int group) return NULL; } - mbedtls_ecp_group_init( &e->group ); + mbedtls_ecp_group_init(&e->group); if (mbedtls_ecp_group_load(&e->group, grp_id)) { crypto_ec_deinit(e); @@ -286,7 +286,7 @@ void crypto_ec_deinit(struct crypto_ec *e) return; } - mbedtls_ecp_group_free( &e->group ); + mbedtls_ecp_group_free(&e->group); os_free(e); } @@ -420,6 +420,7 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, mbedtls_ctr_drbg_context ctr_drbg; mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); MBEDTLS_MPI_CHK(mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)); @@ -431,8 +432,8 @@ int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, mbedtls_ctr_drbg_random, &ctr_drbg)); cleanup: - mbedtls_ctr_drbg_free( &ctr_drbg ); - mbedtls_entropy_free( &entropy ); + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); return ret ? -1 : 0; } diff --git a/components/wpa_supplicant/src/wps/wps.c b/components/wpa_supplicant/src/wps/wps.c index 2ed83bbf6..621119d1b 100644 --- a/components/wpa_supplicant/src/wps/wps.c +++ b/components/wpa_supplicant/src/wps/wps.c @@ -273,42 +273,40 @@ _out: * provisioning, -1 if wps_a is considered more like, or 0 if no preference */ int wps_ap_priority_compar(const struct wpabuf *wps_a, - const struct wpabuf *wps_b) + const struct wpabuf *wps_b) { - struct wps_parse_attr *attr_a, *attr_b; + struct wps_parse_attr *attr = NULL; int sel_a, sel_b; - int ret = 0; + int ret = 0; /* No preference */ - attr_a = (struct wps_parse_attr *)os_zalloc(sizeof(struct wps_parse_attr)); - attr_b = (struct wps_parse_attr *)os_zalloc(sizeof(struct wps_parse_attr)); + attr = os_zalloc(sizeof(*attr)); - if (attr_a == NULL || attr_b == NULL) { - ret = 0; - goto _out; + if (!attr) + return ret; + + if (wps_a == NULL || wps_parse_msg(wps_a, attr) < 0) { + ret = 1; + goto exit; } + sel_a = attr->selected_registrar && *(attr->selected_registrar) != 0; - if (wps_a == NULL || wps_parse_msg(wps_a, attr_a) < 0) - return 1; - if (wps_b == NULL || wps_parse_msg(wps_b, attr_b) < 0) - return -1; - - sel_a = attr_a->selected_registrar && *attr_a->selected_registrar != 0; - sel_b = attr_b->selected_registrar && *attr_b->selected_registrar != 0; + if (wps_b == NULL || wps_parse_msg(wps_b, attr) < 0) { + ret = -1; + goto exit; + } + sel_b = attr->selected_registrar && *(attr->selected_registrar) != 0; if (sel_a && !sel_b) { ret = -1; - goto _out; + goto exit; } if (!sel_a && sel_b) { ret = 1; - goto _out; + goto exit; } -_out: - if (attr_a) - os_free(attr_a); - if (attr_b) - os_free(attr_b); +exit: + os_free(attr); return ret; } diff --git a/components/wpa_supplicant/src/wps/wps_attr_parse.c b/components/wpa_supplicant/src/wps/wps_attr_parse.c index a8cf76683..95ec4234d 100644 --- a/components/wpa_supplicant/src/wps/wps_attr_parse.c +++ b/components/wpa_supplicant/src/wps/wps_attr_parse.c @@ -128,10 +128,44 @@ static int wps_parse_vendor_ext(struct wps_parse_attr *attr, const u8 *pos, return 0; } +static u16 wps_ignore_null_padding_in_attr(const u8 *pos, u16 type, u16 attr_data_len) +{ + u16 len = attr_data_len; + +#ifdef CONFIG_WPA_WPS_WARS + if (len == 0) + return 0; + /* + * Some AP's keep NULL-padding at the end of some variable length WPS Attributes. + * This is not as par the WPS2.0 specs, but to avoid interop issues, ignore the + * padding by reducing the attribute length by 1. + */ + switch (type) { + case ATTR_MANUFACTURER: + case ATTR_MODEL_NAME: + case ATTR_MODEL_NUMBER: + case ATTR_SERIAL_NUMBER: + case ATTR_DEV_NAME: + case ATTR_SSID: + case ATTR_NETWORK_KEY: + if (pos[len - 1] == 0) + len--; + break; + default: + break; + } +#endif + + return len; +} static int wps_set_attr(struct wps_parse_attr *attr, u16 type, - const u8 *pos, u16 len) + const u8 *pos, u16 attr_data_len) { + u16 len; + + len = wps_ignore_null_padding_in_attr(pos, type, attr_data_len); + switch (type) { case ATTR_VERSION: if (len != 1) { @@ -637,4 +671,4 @@ int wps_parse_msg(const struct wpabuf *msg, struct wps_parse_attr *attr) } return 0; -} \ No newline at end of file +} diff --git a/components/wpa_supplicant/src/wps/wps_registrar.c b/components/wpa_supplicant/src/wps/wps_registrar.c index a38a75d1f..1efb3c88b 100644 --- a/components/wpa_supplicant/src/wps/wps_registrar.c +++ b/components/wpa_supplicant/src/wps/wps_registrar.c @@ -1689,6 +1689,7 @@ int wps_build_cred(struct wps_data *wps, struct wpabuf *msg) if (random_get_bytes(r, sizeof(r)) < 0) return -1; os_free(wps->new_psk); + wps->new_psk = NULL; //wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len); if (wps->new_psk == NULL) return -1; diff --git a/components/wpa_supplicant/src/wps/wps_validate.c b/components/wpa_supplicant/src/wps/wps_validate.c index 7f2ad5eeb..f02e2fa18 100644 --- a/components/wpa_supplicant/src/wps/wps_validate.c +++ b/components/wpa_supplicant/src/wps/wps_validate.c @@ -95,24 +95,13 @@ static int wps_validate_response_type(const u8 *response_type, int mandatory) static int valid_config_methods(u16 val, int wps2) { +#ifndef CONFIG_WPA_WPS_WARS if (wps2) { - if ((val & 0x6000) && !(val & WPS_CONFIG_DISPLAY)) { - wpa_printf(MSG_INFO, "WPS-STRICT: Physical/Virtual " - "Display flag without old Display flag " - "set"); - return 0; - } if (!(val & 0x6000) && (val & WPS_CONFIG_DISPLAY)) { wpa_printf(MSG_INFO, "WPS-STRICT: Display flag " "without Physical/Virtual Display flag"); return 0; } - if ((val & 0x0600) && !(val & WPS_CONFIG_PUSHBUTTON)) { - wpa_printf(MSG_INFO, "WPS-STRICT: Physical/Virtual " - "PushButton flag without old PushButton " - "flag set"); - return 0; - } if (!(val & 0x0600) && (val & WPS_CONFIG_PUSHBUTTON)) { wpa_printf(MSG_INFO, "WPS-STRICT: PushButton flag " "without Physical/Virtual PushButton flag"); @@ -120,6 +109,7 @@ static int valid_config_methods(u16 val, int wps2) } } +#endif return 1; } diff --git a/docs/Doxyfile b/docs/Doxyfile index 027b283bc..9bd831634 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -53,6 +53,22 @@ INPUT = \ ../../components/bt/bluedroid/api/include/api/esp_spp_api.h \ ../../components/bt/bluedroid/api/include/api/esp_hf_defs.h \ ../../components/bt/bluedroid/api/include/api/esp_hf_client_api.h \ + ## NimBLE related Bluetooth APIs + ../../components/nimble/esp-hci/include/esp_nimble_hci.h \ + ## ESP BLE Mesh APIs + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_common_api.h \ + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_local_data_operation_api.h \ + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_low_power_api.h \ + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_networking_api.h \ + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_provisioning_api.h \ + ../../components/bt/esp_ble_mesh/api/core/include/esp_ble_mesh_proxy_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_config_model_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_generic_model_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_health_model_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_lighting_model_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_sensor_model_api.h \ + ../../components/bt/esp_ble_mesh/api/models/include/esp_ble_mesh_time_scene_model_api.h \ + ../../components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h \ ## ## Ethernet - API Reference ## @@ -102,6 +118,8 @@ INPUT = \ ../../components/esp_http_client/include/esp_http_client.h \ ../../components/esp_http_server/include/esp_http_server.h \ ../../components/esp_https_server/include/esp_https_server.h \ + ## Websocket Client + ../../components/esp_websocket_client/include/esp_websocket_client.h \ ## ## Provisioning - API Reference ## @@ -179,6 +197,8 @@ INPUT = \ ## Base MAC address ## NOTE: for line below header_file.inc is not used ../../components/esp32/include/esp_system.h \ + ## IDF version + ../../components/esp32/include/esp_idf_version.h \ ## ## ULP Coprocessor - API Guides ## @@ -236,7 +256,8 @@ PREDEFINED = \ configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS=1 \ configNUM_THREAD_LOCAL_STORAGE_POINTERS=1 \ configUSE_APPLICATION_TASK_TAG=1 \ - configTASKLIST_INCLUDE_COREID=1 + configTASKLIST_INCLUDE_COREID=1 \ + "ESP_EVENT_DECLARE_BASE(x)=extern esp_event_base_t x" ## Do not complain about not having dot ## diff --git a/docs/README.md b/docs/README.md index 89a0db0a9..b0a4e5c2d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,7 +25,7 @@ The above URLs are all for the master branch latest version. Click the drop-down If using Windows and the MSYS2 MINGW32 terminal, run this command before running "make html" the first time: ``` -pacman -S doxygen mingw-w64-i686-python2-pillow +pacman -S doxygen mingw-w64-i686-python-pillow ``` Note: Currently it is not possible to build docs on Windows without using a Unix-on-Windows layer such as MSYS2 MINGW32. diff --git a/docs/_static/api-guides.gif b/docs/_static/api-guides.gif deleted file mode 100644 index bc9dd36db..000000000 Binary files a/docs/_static/api-guides.gif and /dev/null differ diff --git a/docs/_static/api-guides.png b/docs/_static/api-guides.png new file mode 100644 index 000000000..885e36bba Binary files /dev/null and b/docs/_static/api-guides.png differ diff --git a/docs/_static/api-reference.gif b/docs/_static/api-reference.gif deleted file mode 100644 index 7fd81755b..000000000 Binary files a/docs/_static/api-reference.gif and /dev/null differ diff --git a/docs/_static/api-reference.png b/docs/_static/api-reference.png new file mode 100644 index 000000000..91b09ad6e Binary files /dev/null and b/docs/_static/api-reference.png differ diff --git a/docs/_static/ble-mesh-config-complete.png b/docs/_static/ble-mesh-config-complete.png new file mode 100644 index 000000000..9dfc317ab Binary files /dev/null and b/docs/_static/ble-mesh-config-complete.png differ diff --git a/docs/_static/ble-mesh-device-power-on.png b/docs/_static/ble-mesh-device-power-on.png new file mode 100644 index 000000000..c18e1176a Binary files /dev/null and b/docs/_static/ble-mesh-device-power-on.png differ diff --git a/docs/_static/ble-mesh-generic-onoff.png b/docs/_static/ble-mesh-generic-onoff.png new file mode 100644 index 000000000..69008f430 Binary files /dev/null and b/docs/_static/ble-mesh-generic-onoff.png differ diff --git a/docs/_static/ble-mesh-identify-provision.png b/docs/_static/ble-mesh-identify-provision.png new file mode 100644 index 000000000..709bc06fe Binary files /dev/null and b/docs/_static/ble-mesh-identify-provision.png differ diff --git a/docs/_static/ble-mesh-initial-config-fail.png b/docs/_static/ble-mesh-initial-config-fail.png new file mode 100644 index 000000000..feb6c7f10 Binary files /dev/null and b/docs/_static/ble-mesh-initial-config-fail.png differ diff --git a/docs/_static/ble-mesh-model-bind-appkey.png b/docs/_static/ble-mesh-model-bind-appkey.png new file mode 100644 index 000000000..0a1bf994f Binary files /dev/null and b/docs/_static/ble-mesh-model-bind-appkey.png differ diff --git a/docs/_static/ble-mesh-provision.png b/docs/_static/ble-mesh-provision.png new file mode 100644 index 000000000..dc2274841 Binary files /dev/null and b/docs/_static/ble-mesh-provision.png differ diff --git a/docs/_static/ble-mesh-reconnect-initial-config.png b/docs/_static/ble-mesh-reconnect-initial-config.png new file mode 100644 index 000000000..67927b655 Binary files /dev/null and b/docs/_static/ble-mesh-reconnect-initial-config.png differ diff --git a/docs/_static/ble-mesh-reconnect-three.png b/docs/_static/ble-mesh-reconnect-three.png new file mode 100644 index 000000000..4da6f7d00 Binary files /dev/null and b/docs/_static/ble-mesh-reconnect-three.png differ diff --git a/docs/_static/ble-mesh-scanner.png b/docs/_static/ble-mesh-scanner.png new file mode 100644 index 000000000..c7c45e396 Binary files /dev/null and b/docs/_static/ble-mesh-scanner.png differ diff --git a/docs/_static/ble-mesh-three-nodes-on.png b/docs/_static/ble-mesh-three-nodes-on.png new file mode 100644 index 000000000..fad4e8d02 Binary files /dev/null and b/docs/_static/ble-mesh-three-nodes-on.png differ diff --git a/docs/_static/contribute.gif b/docs/_static/contribute.gif deleted file mode 100644 index 48a450890..000000000 Binary files a/docs/_static/contribute.gif and /dev/null differ diff --git a/docs/_static/contribute.png b/docs/_static/contribute.png new file mode 100644 index 000000000..198473654 Binary files /dev/null and b/docs/_static/contribute.png differ diff --git a/docs/_static/esp-ble-mesh-architecture.png b/docs/_static/esp-ble-mesh-architecture.png new file mode 100644 index 000000000..a046e1e10 Binary files /dev/null and b/docs/_static/esp-ble-mesh-architecture.png differ diff --git a/docs/_static/esp-ble-mesh-architecture.xml b/docs/_static/esp-ble-mesh-architecture.xml new file mode 100644 index 000000000..df6935b95 --- /dev/null +++ b/docs/_static/esp-ble-mesh-architecture.xml @@ -0,0 +1 @@ +5VtbV6M6FP41ffQsSCDAYy9WPWpPl1Zdc15cFELLSAknTS/Orz+BBtuSdGTWkOrUupaFnXDJ9+3s7Evagt3Z+oL62fSWhDhpASNct2CvBYBpWw7/yiWvQmJa1kYyoXEoZFvBffwDC6EhpIs4xPO9joyQhMXZvjAgaYoDtifzKSWr/W4RSfafmvkTLAnuAz+RpU9xyKYbqWsbW/kljifT8smWIVpmftlZCOZTPySrHRE8b8EuJYRtjmbrLk5y9EpcNtf1D7S+vRjFKatzwWAQuXdzSDrtdpSsv/9z2R0kZ1Dws/SThRixeFv2WkJAySINcX4XswU7q2nM8H3mB3nrirPOZVM2S0RzRFImWOTvAzt+Ek9SfhLwt8Q07xAnSZckhB/3UpLivA8NxCWQnxF+75jl+mLlN5CHKUa+xJTh9Y5IDPsCkxlm9JV3KVtLToQSgvJ8tWXUQUI23WHTdYXQF1o0ebv3Fmh+ILBW474wY/e893dyi25Q8m108/jv9PnMlGC/xfMplxRTaN4sBzlSMVfptuCCkawhYME+sBDKwAJXAezbpPgdYJUKLQPbzrKEj57FJG0Y111VbgEY+tiNAi6fM0pe8E4LClw8jvRg/gblrjIDBeaWLsiBDPnwqpW/Qp//P1/mY9WJe2Tnf0rci4+4w45889HEh2P9ZX8wI9Yh6zLAbEXoS5xO/lALY6OaFgbaniZwbQncfg5dYWBKA86/b/xXvt7p1HtshjZ2VHrvIQf6qCEGrH0GLAfKDFgKBpCliQAka/dnwlzfXKgwAR1DYWmUXLi6JoPsPraDAM/nn4UMDbAjoyboyNYEuiuB/pBlOdbGiPrpPCOUnTL+rllX7ZEutfckBm7I6ssw4KC6S4CuGVBmB3YIEK7NCcPuWrXtPXJ1AS/HV33sswXFf0rMagHJnEiYmipMAdQGqhxB9WmM01CrEkdRBAJluBqiMbIbUmILVmxH6Zfvwq0KjoAu221ClfHmguHGhDcEuel+GshNVA9yCHRBLgekdzjxX09Rvz33o/VbDlCHlKzzd77HdHmSKu6Weex3VVxb3lGOhLYeya2f+hM8azwRJiXXj7ZqQqAw46pVE1qmLsTlMGiLeJdikZBpUtd/WswolB+jA8rveGOjoXJGVfltR/YKlcpvaXPH5XjoGudvfIcjWmQevwALsCYL2lZZIAdFV4/8/CELfYa/AgeWV3cZ0GWUgBwfFRxccajXxYQICB9fc57PZ2bDUlVCjmuZgBxaDUiICyZmhEu/AhHQ9WoToSvGBXLUJWpS3DldxnO+VjdelTqee2S777tHQLXDwPa0VWUPFgE7mA+VRwE6EmXHg7xaGfSgHOUqq66OrsALyIFXOyzGPy9U+w14rYBXNyXY2A0tlcVxwRiiplKVFYvjOXJSDdrHjMeAXCW8aI9GJ82C5dVgQRmkaWNBjoo7/IgRwnJLtMm7naeYTvJhdAnNV+b7DAdxJLbsHJenft/jHxVPhmE7xfpc2UpiFB89Js00wEfvJAFykL2/XhvD3kPDqf/3SBqbYRgZKpJMw4EebmhJr0wm6CiWdNVk8nT5slCOLVoAJfyxnTBe8sNJfniB+XSKg7KFP2mnUdF/n866F/UeSumYVvtVrz593bA8RTaszMNO97YH69oUU2YVfq4cIhH86yyfPqdOyVeZzzFU1dZjsypHTcPOWbv3eIoMVGcVQopSigp/T1c2B77j0Z8+B45bkwOzLKs2T4LsyTe1jOX1sJjz84WXsqrZOy7hBrkazWByiYfw20NwEfUuM6bYT95NYt2byA9v6enYHO1eQx6+XXEcDEXC2tDk4CvBlrNEZbG4YdB/rVjcQx19xWKn7n4IXaArKvSds0164vQtzFGX9eV4+bIO+5fOcnZ99t0Y34Sj/xS7NweP93nigRGa/xywUZX/nCUBBJy6JYEm6pXD9nD2NJyQ1+jZ7LgDcP28eFLY+Yb3p/yWnZewVTBwEG7gHM/O89Ptj0iLtp3f4sLz/wE= \ No newline at end of file diff --git a/docs/_static/esp-ble-mesh-interface.png b/docs/_static/esp-ble-mesh-interface.png new file mode 100644 index 000000000..c83c42222 Binary files /dev/null and b/docs/_static/esp-ble-mesh-interface.png differ diff --git a/docs/_static/esp-ble-mesh-interface.xml b/docs/_static/esp-ble-mesh-interface.xml new file mode 100644 index 000000000..3a708e713 --- /dev/null +++ b/docs/_static/esp-ble-mesh-interface.xml @@ -0,0 +1 @@ +5V1Zd9o6EP41PJJjyfL2yJImadMebrO0yUuPgxVw6yDXmK2//spggW2pNngnzUssIQtpZr7RzGgkOvLgbX3lme70M7Gw04GSte7Iww6EQFIV+i+o2exqNN3YVUw82wobHSru7D+YvRnWLmwLz2MNfUIc33bjlWMym+GxH6szPY+s4s1eiRP/VtecYK7ibmw6fO032/Knu1odaof6a2xPpuybgRrO781kjcOZzKemRVaRKvmyIw88Qvzd09t6gJ2AeIwuu/c+/OXT/cA8PPOPecH89ji6sqeoD7Vln3gzMH/0u3DXy9J0FuGEw8H6G0YBjyxmFg46AR25v5raPr5zzXHw6YrynNZN/Tcn/PjVdpwBcYhHyzMyo436hDa2/YD9SKJF0xuHXJZpiZ9EOK8l9ny8jlSFk7rC5A373oY2YZ+ikMChhClhcXVgF9LDummEVToTMTMUkcm+6wMV6UNISDFRn/9D4x+uv9xcrdZfVeKB9Z9BV84mKraolIVF4vlTMiEz07k81PYPtbeEuCF5f2Lf34TkMxc+iRP/wCkpjbRzsvDGOGX8DGemN8F+SrtwWsFcUhnlYcf07WUcUSKqb1/teZ65iTRwiT3z55GeR0HFgf9Qj/MfAD2Og9Pa04fdCA78308lv0gATiR6ruvYY0oXMqMf3Job7BWEHpn5oWjAVP6fAC01TipV56GFBNBCJSDrCSs3t78kfwAfwca3Fqun0U0X5QBSHBU5YIXXtv89eP1CU8LiU9hb8DxcRwsbVpjR+UbfCspPrMegcHhvW2Iv5octPBK2oGzYFlKW/ArUv72kFZ/xfEr/9e8HZwMPgGrEx/Of62ul9+yPPj4PH34vDWx8WnRzrTSlAQRE0XEAy4kAkTIAEmEkCF78uXhz2QSpcVEIQkgAIRGdS4dQrpVPkeWY/CFDy1j59LT2hVe+yWLjgsX1qouk6ydp+fTxavXQ1ZpV2QYyIlIJLiQJHimWMZm8gMoJYsm+YoQ9m9IPe7XpdtSUbjduxtLzmzQD6PfNd8MjL0D3uzIozPwxebPH4TOP9G3Njuh6QUnJqbt0pMUFxTCyVvhtKSkae++wmJyIFFgtpnshGwBxNkAHqg4df9+ylzH5UX8vAn95i7bufMvUHm0AFHd9+JA+TYL/I48s7Tk1sAPbQeqyPukQt93uGpX7TfZs8revKWK8WLaHx1tXQR5SGQiMC24dLMGgUUDcoJGBxhk0hsCeAXIJBo1w9QDKORo0sgLjSkGCKI9SSK4pR+kFoeMkcunba9jsY2bMUNHKddHTSFRaKCwDs1U5ISLMQpETUgpoxV5I8WW/AdAWckLyI1Nk2bUXmYiZdAyZeobLgZS09tW4HI2uGXkNSRWgqABSTwVmyGDFS8axLkc7osBJwaQV1Qsa878bi0cCLSZthvFenNs0iWxa0rS4SoNKuZKWNvWyPaQvxMJR1+jFy99Xze5VFUM9AxcNSfEYXq0umlA0VU40e6MbjpZ0en6cYB6mbDJftg0C/RHijrZW+h1lSGsC1TcPyRcUHXsSUNfBr0FXAc3ssen0wmo/UJ/9ebDbPJvcb3VpF1XDBKjHA68AqBwTZAETYFU80EpRD1tgJKByuQzIdBxe3hWPkaFk8lipiMdCB0Bv1tbItfV50s5nSRs7QoAYAmNWROTS4+e5TAw5kaKgqOleVjJul2hfPKVB6JA26uUzo5Vt7BwrkbAhN18ggMJ2slq2BBZSOkzpNbZ9J+lH650ks6IBWd04RRMJPevcPk+aPJSsaHjNoBgxzaDKKN7FTiDDt9JUTLKjMCS772g3Y66jshwgg7NwYmkiPct0/e02z3mkiqgsTbSxVCrQInviaO0NDDmK4650IQEtD5jLU+xsGczU7KUjvhj7+e2OBKACv+F+6mHTKg1NZdnmSTQpBuLQBLRa0WScozGkxuGUZQ1VjSRB8kK7kCQ0kRpOcKojJ7VaHtfjiBWDN5+hMvLIOhgA/e+TMXFapyQRPEJJig5GVJadCuA5KsmmPEbmCWa7jFqr9CH4B5L0G+Byu8xHmY/69+mT5RH6GpSuydwvTSFW5YNpMq8Q603XB03vH58XVLRjFWJjMbTUcUewcnk3ohU7h2tACeQRxzmDqIVm1Bi1ECKm4XCk1oAFkfd8S0oSWSaCQDv2QZIHVhQtfR8EqUpa+8L7IOJDDsWt2toOOeSLup0kvjkOMhSS1OZOM6YM+29RtdHwIYiqfS9N0281xMQzLZtSOHHwnlUPI5kps0B6OvFz+h0ov27/aL1lzqf7YVSwgux3KuuI1ImTkxu2uWAOmwvEAHhaul7ZS8ixQbqW5CsnVgSUsYLIKHnyoIaddNiwx/yPSCRshUQmczVQRm4H0tS09oUlUhk6v66d3n9DRXq5//Hyg+jjS0aqVpjZ+SQySyBLFbd2HcVNG3W6YfK1ZMOkbGuCC2mLrIl6Q9oKR9e7Qe8LR8Z3lRop6/JFfFlVZT4woNaZHAn4GEtv+Pi+2QCRnsmEWjNUWUpJhAlXvfv7980FpGZzoV4o8KlS758LSjK/qkYuiA+uc0z4gv0V8X51yk5RSz/md4qrHs/wQ4KTJaCqUw1CGkKOhrdktU3yu/fM2dylNubZUFPWm6Ymfyvhg+ueKzWVpqnJ29O98RjP5+dDQtg0CVk6SISGN9Rkkx5cy/Rx6wgoJy9HQZAjoMj3qI5+PKI/4WDAX/Grt3Xr2k5BjRfBeiko8IqxOQ5u5Gw56RBoWPjkhndi84TRpQtJjSe8shPztdyyIBZB0cm0Vt3YJWZ/w1mvQI+dC6J8NI6Vgfy3TdZy80GqlLTkXj9Y/IaVare582kHeFJi01FcF5JPsLd9FlwvfsahWq7nys6JrwelbBscrehbvHHK7s853HKS+HWBjFvVE+2ruX9HEOr6EMgbu1V9+3MU89YZc8mkCMiSWKKuGBBYc2Xc4aE4X7TpoDeaf0M/HLP3uvj0CJjMnZcxJ6OYMRfc7avnMebyY5pZ1+013oTcbjjdAOnRK/r2bCvfdmvGVmNC0ZZVu/g537NftaP5lafnJx4N/XqW82IhdYlbMXcZAB8e8cwiXnsXTc7AEC2aLLmv7EVTjKziPnCNyDpbL6h8fVqOQaymG8Qyk09x+4py0aWWi6QkybG4jaYeHbc5SSzLvaxFIKppIt2SlZ/fgmY/z/fZdN3tVYpFdHzVqeKQpYVneUVQP5mAtHj44b0d+A4/Xyhf/g8= \ No newline at end of file diff --git a/docs/_static/esp32_nimble_stack.png b/docs/_static/esp32_nimble_stack.png new file mode 100644 index 000000000..4aba0ae88 Binary files /dev/null and b/docs/_static/esp32_nimble_stack.png differ diff --git a/docs/_static/get-started.gif b/docs/_static/get-started.gif deleted file mode 100644 index 7a7a8023b..000000000 Binary files a/docs/_static/get-started.gif and /dev/null differ diff --git a/docs/_static/get-started.png b/docs/_static/get-started.png new file mode 100644 index 000000000..344463511 Binary files /dev/null and b/docs/_static/get-started.png differ diff --git a/docs/_static/hw-reference.gif b/docs/_static/hw-reference.gif deleted file mode 100644 index 0b969c209..000000000 Binary files a/docs/_static/hw-reference.gif and /dev/null differ diff --git a/docs/_static/hw-reference.png b/docs/_static/hw-reference.png new file mode 100644 index 000000000..ec31ab52b Binary files /dev/null and b/docs/_static/hw-reference.png differ diff --git a/docs/_static/resources.gif b/docs/_static/resources.gif deleted file mode 100644 index cde4bd809..000000000 Binary files a/docs/_static/resources.gif and /dev/null differ diff --git a/docs/_static/resources.png b/docs/_static/resources.png new file mode 100644 index 000000000..8989b6298 Binary files /dev/null and b/docs/_static/resources.png differ diff --git a/docs/docs_common.mk b/docs/docs_common.mk index 7a77b5729..e1298bd66 100644 --- a/docs/docs_common.mk +++ b/docs/docs_common.mk @@ -177,9 +177,10 @@ linkcheck: | check_python_packages "or in $(BUILDDIR)/linkcheck/output.txt." gh-linkcheck: | check_python_packages - @echo "Checking for hardcoded GitHub links" + @echo "Checking for hardcoded GitHub links" # note: exception for links to support policy doc as we *want* this to be a link to master's policy @if (find ../ -name '*.rst' | xargs grep \ - 'https://github.com/espressif/esp-idf/tree\|https://github.com/espressif/esp-idf/blob\|https://github.com/espressif/esp-idf/raw'\ + 'https://github.com/espressif/esp-idf/tree\|https://github.com/espressif/esp-idf/blob\|https://github.com/espressif/esp-idf/raw' \ + | grep -v 'SUPPORT_POLICY\.md' \ ); \ then \ echo "WARNINIG: Some .rst files contain hardcoded Github links."; \ diff --git a/docs/en/COPYRIGHT.rst b/docs/en/COPYRIGHT.rst index e9aa5a4ed..3589702cc 100644 --- a/docs/en/COPYRIGHT.rst +++ b/docs/en/COPYRIGHT.rst @@ -57,6 +57,10 @@ These third party libraries can be included into the application (firmware) prod * :component:`ESP-MQTT ` MQTT Package (contiki-mqtt) - Copyright (c) 2014, Stephen Robinson, MQTT-ESP - Tuan PM is licensed under Apache License 2.0. +* `mynewt-nimble`_ Apache Mynewt NimBLE, Copyright 2015-2018, The Apache Software Foundation, is licensed under Apache License 2.0. + +* :component:`BLE Mesh ` is adapted from Zephyr Project, Copyright (c) 2017-2018 Intel Corporation and licensed under Apache License 2.0 + Build Tools ----------- @@ -158,3 +162,4 @@ Copyright (C) 2011, ChaN, all right reserved. .. _spiffs: https://github.com/pellepl/spiffs .. _asio: https://github.com/chriskohlhoff/asio .. _mqtt: https://github.com/espressif/esp-mqtt +.. _mynewt-nimble: https://github.com/apache/mynewt-nimble diff --git a/docs/en/api-guides/bootloader.rst b/docs/en/api-guides/bootloader.rst index 6e5efbcc0..aea98d6c9 100644 --- a/docs/en/api-guides/bootloader.rst +++ b/docs/en/api-guides/bootloader.rst @@ -42,13 +42,15 @@ Partition table.:: ota_0, 0, ota_0, , 512K ota_1, 0, ota_1, , 512K +.. _bootloader_boot_from_test_firmware: + Boot from TEST firmware ------------------------ The user can write a special firmware for testing in production, and run it as needed. The partition table also needs a dedicated partition for this testing firmware (See `partition table`). To trigger a test app you need to set :ref:`CONFIG_BOOTLOADER_APP_TEST`. -:ref:`CONFIG_BOOTLOADER_NUM_PIN_APP_TEST` - number of the GPIO input to boot TEST partition. The selected GPIO will be configured as an input with internal pull-up enabled. To trigger a test app, this GPIO must be pulled low on reset. -After the GPIO input is deactivated and the device reboots, the old application will boot (factory or any OTA slot). +:ref:`CONFIG_BOOTLOADER_NUM_PIN_APP_TEST` - GPIO number to boot TEST partition. The selected GPIO will be configured as an input with internal pull-up enabled. To trigger a test app, this GPIO must be pulled low on reset. +After the GPIO input is deactivated and the device reboots, the normally configured application will boot (factory or any OTA slot). :ref:`CONFIG_BOOTLOADER_HOLD_TIME_GPIO` - this is hold time of GPIO for reset/test mode (by default 5 seconds). The GPIO must be held low continuously for this period of time after reset before a factory reset or test partition boot (as applicable) is performed. diff --git a/docs/en/api-guides/build-system-cmake.rst b/docs/en/api-guides/build-system-cmake.rst index 813857b61..b5054c40a 100644 --- a/docs/en/api-guides/build-system-cmake.rst +++ b/docs/en/api-guides/build-system-cmake.rst @@ -341,6 +341,7 @@ The following variables are set at the project level, but available for use in c - ``COMPONENTS``: Names of all components that are included in this build, formatted as a semicolon-delimited CMake list. - ``CONFIG_*``: Each value in the project configuration has a corresponding variable available in cmake. All names begin with ``CONFIG_``. :doc:`More information here `. - ``IDF_VER``: Git version of ESP-IDF (produced by ``git describe``) +- ``IDF_VERSION_MAJOR``, ``IDF_VERSION_MINOR``, ``IDF_VERSION_PATCH``: Components of ESP-IDF version, to be used in conditional expressions. Note that this information is less precise than that provided by ``IDF_VER`` variable. ``v4.0-dev-*``, ``v4.0-beta1``, ``v4.0-rc1`` and ``v4.0`` will all have the same values of ``IDF_VERSION_*`` variables, but different ``IDF_VER`` values. - ``IDF_TARGET``: Name of the target for which the project is being built. - ``PROJECT_VER``: Project version. diff --git a/docs/en/api-guides/build-system.rst b/docs/en/api-guides/build-system.rst index 8c85c65d3..5f936c05f 100644 --- a/docs/en/api-guides/build-system.rst +++ b/docs/en/api-guides/build-system.rst @@ -188,6 +188,7 @@ The following variables are set at the project level, but exported for use in th - ``CC``, ``LD``, ``AR``, ``OBJCOPY``: Full paths to each tool from the gcc xtensa cross-toolchain. - ``HOSTCC``, ``HOSTLD``, ``HOSTAR``: Full names of each tool from the host native toolchain. - ``IDF_VER``: ESP-IDF version, retrieved from either ``$(IDF_PATH)/version.txt`` file (if present) else using git command ``git describe``. Recommended format here is single liner that specifies major IDF release version, e.g. ``v2.0`` for a tagged release or ``v2.0-275-g0efaa4f`` for an arbitrary commit. Application can make use of this by calling :cpp:func:`esp_get_idf_version`. +- ``IDF_VERSION_MAJOR``, ``IDF_VERSION_MINOR``, ``IDF_VERSION_PATCH``: Components of ESP-IDF version, to be used in conditional expressions. Note that this information is less precise than that provided by ``IDF_VER`` variable. ``v4.0-dev-*``, ``v4.0-beta1``, ``v4.0-rc1`` and ``v4.0`` will all have the same values of ``ESP_IDF_VERSION_*`` variables, but different ``IDF_VER`` values. - ``PROJECT_VER``: Project version. * If ``PROJECT_VER`` variable is set in project Makefile file, its value will be used. diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst new file mode 100644 index 000000000..4f06d46b1 --- /dev/null +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-architecture.rst @@ -0,0 +1,395 @@ +ESP-BLE-MESH Architecture +========================= + +:link_to_translation:`zh_CN:[中文]` + +This document introduces ESP-BLE-MESH architecture overview, ESP-BLE-MESH architecture implementation as well as ESP-BLE-MESH auxiliary routines. + +- ESP-BLE-MESH Architecture Overview + + - Describes the five major parts of ESP-BLE-MESH architecture and the functionality of each part. + +- ESP-BLE-MESH Architecture Implementation + + - Describes the basic functions of ESP-BLE-MESH files, the correspondence between files and ESP-BLE-MESH architecture, and the interface for calling among files. + +- ESP-BLE-MESH Auxiliary Routines + + - Describe the auxiliary routines of ESP-BLE-MESH, such as Mesh network management, Mesh features, etc. + +1. ESP-BLE-MESH Architecture Overview +------------------------------------- + +Currently ESP-BLE-MESH has implemented most functions of Mesh Profile and all the Client Models defined in Mesh Model specification. Those missing functions/models are under development and will be provided soon. ESP-BLE-MESH architecture has been granted the official Bluetooth `certification `__. + +.. figure:: ../../../_static/esp-ble-mesh-architecture.png + :align: center + + Figure 1.1 ESP-BLE-MESH Architecture Diagram + +ESP-BLE-MESH architecture includes five key parts: + +- ``Mesh Protocol Stack`` + + - ``Mesh Networking`` is responsible for processing of messages of ESP-BLE-MESH nodes. + - ``Mesh Provisioning`` is responsible for provisioning flow of ESP-BLE-MESH devices. + - ``Mesh Models`` is responsible for the implementation of SIG-defined models. + +- ``Network Management`` + + - Implements several network management procedures, including node removal procedure, IV Index recovery procedure, etc. + +- ``Features`` + + - Include several ESP-BLE-MESH features, e.g. Low Power feature, Friend feature, Relay feature, etc. + +- ``Mesh Bearer Layer`` + + - Includes ``Advertising Bearer`` and ``GATT Bearer``. The bearer layer is crucial to ESP-BLE-MESH protocol stack which is built on Bluetooth Low-Energy technology, because the protocol stack must make use of the bearer layer to transmit data via the BLE advertising channel and connection channel. + +- ``Applications`` + + - Based on ESP-BLE-MESH protocol stack and ``Mesh Models``. + - By calling API and handling Event, ``Applications`` interact with ``Mesh Networking`` and ``Mesh Provisioning`` in ESP-BLE-MESH protocol stack, as well as a series of Models provided by ``Mesh Models``. + +1.1 Mesh Protocol Stack +----------------------- + +1.1.1 Mesh Networking +^^^^^^^^^^^^^^^^^^^^^ + +``Mesh Networking`` in the protocol stack architecture implements the following functions: + +- The communication between nodes in the Mesh network. +- Encryption and decryption of messages in the Mesh network. +- Management of Mesh network resources (Network Key, IV Index, etc.). +- Segmentation and reassembly of Mesh network messages. +- Model mapping of messages between different models. +- For more features, please see :doc:`ble-mesh-feature-list`. + +The implementation of ``Mesh Networking`` functions is based on hierarchy structure. Functions of each layer are shown in Table 1.1: + +.. list-table:: Table 1.1 Mesh Networking Architecture Description + :widths: 40 150 + :header-rows: 1 + + * - Layer + - Function + * - Access Layer + - Access Layer not only defines the format of application data, but also defines and controls the encryption and decryption of the data packets conducted by Upper Transport Layer. + * - Upper Transport Layer + - Upper Transport Layer encrypts, decrypts, and authenticates application data to and from the access layer; it also handles special messages called "transport control messages", including messages related to "friendship" and heartbeat messages. + * - Lower Transport Layer + - Lower Transport Layer handles segmentation and reassembly of PDU. + * - Network Layer + - Network Layer defines the address type and format of the network messages, and implements the relay function of the device. + +1.1.2 Mesh Provisioning +^^^^^^^^^^^^^^^^^^^^^^^ + +``Mesh Provisioning`` in the protocol stack architecture implements the following functions: + +- Provisioning of unprovisioned devices. +- Allocation of Mesh network resources (unicast address, IV Index, NetKey, etc.). +- Four authentication methods support during provisioning. +- For more features, please see :doc:`ble-mesh-feature-list`. + +The implementation of ``Mesh Provisioning`` functions is based on hierarchy structure. Functions of each layer are shown in Table 1.2: + +.. list-table:: Table 1.2 Mesh Provisioning Architecture Description + :widths: 40 150 + :header-rows: 1 + + * - Layer + - Function + * - Provisioning PDUs + - Provisioning PDUs from different layers are handled using provisioning protocol. + * - Generic Provisioning PDU/Proxy PDU + - The Provisioning PDUs are transmitted to an unprovisioned device using a Generic Provisioning layer or Proxy protocol layer. + * - PB-ADV/PB-GATT + - These layers define how the Provisioning PDUs are transmitted as transactions that can be segmented and reassembled. + * - Advertising/Provisioning Service + - The provisioning bearers define how sessions are established such that the transactions from the generic provisioning layer can be delivered to a single device. + +1.1.3 Mesh Models +^^^^^^^^^^^^^^^^^ + +``Mesh Models`` in the protocol stack architecture implements the following functions: + +- Configuration Client/Server Models +- Health Client/Server Models +- Generic Client/Server Models +- Sensor Client/Server Models +- Time and Scenes Client/Server Models +- Lighting Client/Server Models + +Functions of each layer are shown in Table 1.3: + +.. list-table:: Table 1.3 Mesh Models Architecture Description + :widths: 40 150 + :header-rows: 1 + + * - Layer + - Function + * - Model Layer + - Model Layer implements models used to standardize the operation of typical user scenarios, including Generic Client/Server Models, Sensor Client/Server Models, Time and Scenes Client/Server Models, Lighting Client/Server Models and several vendor models. + * - Foundation Model Layer + - Foundation Model Layer implements models related to ESP-BLE-MESH configuration, management, self diagnosis, etc. + +1.2 Mesh Network Management +--------------------------- + +``Network Management`` implements the following functions: + +- Node removal procedure is used to remove a node from the network. +- IV Index recovery procedure is used to recover a node's IV Index. +- IV update procedure is used to update the nodes' IV Index. +- Key refresh procedure is used to update the nodes' NetKey, AppKey, etc. +- Network creation procedure is used to create a mesh network. +- NVS storage is used to store node's networking information. + +1.3 Mesh Features +----------------- + +``Features`` includes the following options: + +- Low Power feature is used to reduce node's power consumption. +- Friend feature is used to store messages for Low Power nodes. +- Relay feature is used to relay/forward Network PDUs received by a node over the advertising bearer. +- Proxy Server/Client are two node roles in proxy protocol, which enable nodes to send and receive Network PDUs, mesh beacons, proxy configuration messages and Provisioning PDUs over a connection-oriented bearer. + +1.4 Mesh Bearer Layer +--------------------- + +``Bearers`` in the protocol stack architecture are responsible for passing of data between ESP-BLE-MESH protocol stack and Bluetooth Low Energy Core. + +``Bearers`` can be taken as a carrier layer based on Bluetooth Low Energy Core, which implements the function of receiving and transmitting data for the ESP-BLE-MESH protocol stack. + +.. list-table:: Table 1.3 Mesh Bearers Description + :widths: 40 150 + :header-rows: 1 + + * - Layer + - Function + * - GATT Bearer + - The GATT Bearer uses the Proxy protocol to transmit and receive ``Proxy PDUs`` between two devices over a GATT connection. + * - Advertising Bearer + - When using the Advertising Bearer, a mesh packet shall be sent in the Advertising Data of a ``Bluetooth Low Energy advertising PDU`` using the Mesh Message AD Type. + +1.5 Mesh Applications +--------------------- + +The ``Applications`` in the protocol stack architecture implement the corresponding functions by calling the API provided by the ESP-BLE-MESH protocol stack and processing the Event reported by the protocol stack. There are some common applications, such as gateway, lighting and etc. + +Interaction between application layer(``Applications``)and ``API / Event`` + +- Application layer calls API + + - Call the provisioning-related API for provisioning. + - Call the model-related API to send messages. + - Call the device-attributes-related API to get local information about the device. + +- Application layer processes Event + + The application layer is designed based on events, which take parameters to the application layer. Events are mainly divided into two categories. + + - The events completed by calling API. + - Such as nodes sending messages. + - The events that the protocol stack actively reports to the application layer. + - The Event that the protocol stack actively reports. + - The Event that Model actively reports. + +- The event is reported by the callback function registered by the application layer, and the callback function also contains the corresponding processing of the event. + +Interaction between ``API / Event`` and ESP-BLE-MESH protocol stack + +- API used by user mainly calls functions provided by ``Mesh Networking``, ``Mesh Provisioning`` and ``Mesh Models``. + +- The interaction between ``API / Event`` and the protocol stack does not operate across the hierarchy of the protocol stack. For example, API does not call functions related to ``Network Layer``. + +2. ESP-BLE-MESH Architecture Implementation +------------------------------------------- + +The design and implementation of ESP-BLE-MESH architecture is based on layers and modules. In details, Section 2.1 (Mesh Networking Implementation), Section 2.2 (Mesh Provisioning Implementation) and Section 2.3 (Mesh Bearers Implementation) are based on layers, and Section 2.4 (Mesh Models Implementation) is on modules. + +- **Layer-based Approach**: With Layer-based approach, the architecture is designed according to the layers specified in the Mesh Profile Specification. Each layer has its unique files which include APIs of this layer and etc. The specific design is shown in Figure 2.1. + +- **Module-based Approach**: Every file implements an independent function that can be called by other programs. + +.. figure:: ../../../_static/esp-ble-mesh-interface.png + :align: center + + Figure 2.1 ESP-BLE-MESH Architecture Implementation Diagram + +The design of ESP-BLE-MESH architecture uses layer-based approach. The sequence of layers which data packets are processed through is fixed, i.e., the processing of packets will form a ``message flow``. Thus, we could see flows of messages from the Protocol Stack Interface Diagram in Figure 2.1. + +2.1 Mesh Protocol Stack Implementation +-------------------------------------- + +2.1.1 Mesh Networking Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The list of files and the functions implemented in each file in ``Mesh Networking`` are shown in Table 2.1: + +.. list-table:: Table 2.1 Mesh Networking File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`access.c ` + - ESP-BLE-MESH Access Layer + * - :component_file:`transport.c ` + - ESP-BLE-MESH Lower/Upper Transport Layer + * - :component_file:`net.c ` + - ESP-BLE-MESH Network Layer + * - :component_file:`adv.c ` + - A task used to send ESP-BLE-MESH advertising packets, a callback used to handle received advertising packets and APIs used to allocate adv buffers + +2.1.2 Mesh Provisioning Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The implementation of Mesh Provisioning is divided into two chunks due to the Node/Provisioner coexistence. + +Specific files that provide implementation of provisioning of Node are shown in Table 2.2: + +.. list-table:: Table 2.2 Mesh Provisioning (Node) File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`prov.c ` + - ESP-BLE-MESH Node provisioning (PB-ADV & PB-GATT) + * - :component_file:`proxy_server.c ` + - ESP-BLE-MESH Proxy Server related functionalities + * - :component_file:`beacon.c ` + - APIs used to handle ESP-BLE-MESH Beacons + +Specific files that implement functions of Provisioner are shown in Table 2.3: + +.. list-table:: Table 2.3 Mesh Provisioning (Provisioner) File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`provisioner_prov.c ` + - ESP-BLE-MESH Provisioner provisioning (PB-ADV & PB-GATT) + * - :component_file:`proxy_client.c ` + - ESP-BLE-MESH Proxy Client related functionalities + * - :component_file:`provisioner_beacon.c ` + - ESP-BLE-MESH Provisioner receives Unprovisioned Device Beacon + +2.1.3 Mesh Models Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Mesh Models are used to implement the specific functions of model in nodes. Server model is used to maintain node status. Client model is used to obtain and modify node state. + +.. list-table:: Table 2.4 Mesh Models File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`cfg_cli.c ` + - Send Configuration Client messages and receive corresponding response messages + * - :component_file:`cfg_srv.c ` + - Receive Configuration Client messages and send proper response messages + * - :component_file:`health_cli.c ` + - Send Health Client messages and receive corresponding response messages + * - :component_file:`health_srv.c ` + - Receive Health Client messages and send proper response messages + * - :component_file:`client_common.c ` + - ESP-BLE-MESH model related operations + * - :component_file:`generic_client.c ` + - Send ESP-BLE-MESH Generic Client messages and receive corresponding response messages + * - :component_file:`lighting_client.c ` + - Send ESP-BLE-MESH Lighting Client messages and receive corresponding response messages + * - :component_file:`sensor_client.c ` + - Send ESP-BLE-MESH Sensor Client messages and receive corresponding response messages + * - :component_file:`time_scene_client.c ` + - Send ESP-BLE-MESH Time Scene Client messages and receive corresponding response messages + * - :component_file:`generic_server.c ` + - Receive ESP-BLE-MESH Generic Client messages and send corresponding response messages + * - :component_file:`lighting_server.c ` + - Receive ESP-BLE-MESH Lighting Client messages and send corresponding response messages + * - :component_file:`sensor_server.c ` + - Receive ESP-BLE-MESH Sensor Client messages and send corresponding response messages + * - :component_file:`time_scene_server.c ` + - Receive ESP-BLE-MESH Time Scene Client messages and send corresponding response messages + +2.2 Mesh Bearers Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Portability is fully considered in the implementation of Mesh Bearers. When the ESP-BLE-MESH protocol stack is being ported to other platforms, users only need to modify :component_file:`mesh_bearer_adapt.c `. + +.. list-table:: Table 2.5 Mesh Bearers File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`mesh_bearer_adapt.c ` + - ESP-BLE-MESH Bearer Layer adapter,This file provides the interfaces used to receive and send ESP-BLE-MESH ADV & GATT related packets. + +.. note:: + + :component_file:`mesh_bearer_adapt.c ` is the implementation of ``Advertising Bearer`` and ``GATT Bearer`` in Mesh Networking framework. + +2.3 Mesh Applications Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +We have provided a series of application examples for customer development, and users can develop products based on :ref:`esp-ble-mesh-examples`. + +3. Auxiliary Routine +--------------------- + +Auxiliary routine refers to optional functions in the ESP-BLE-MESH protocol stack. The design of the auxiliary routine generally implement the truncation of code through :ref:`CONFIG_BLE_MESH`. + +3.1 Features +^^^^^^^^^^^^ + +- Low Power +- Friend +- Relay +- Proxy Client/Server + +3.2 Network Management +^^^^^^^^^^^^^^^^^^^^^^ + +- Node Removal procedure +- IV Index Recovery procedure +- IV Update procedure +- Key Refresh procedure +- Network Creation procedure +- NVS Storage + +3.3 Auxiliary Routine Implementation +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When adopting the design of independent module, the two main factors should be considered: + +- The module can not be implemented hierarchically, and it can be completely independent, which means it does not rely on the implementation of other modules. +- The functions in the module will be used repeatedly, so it is reasonable to design it into a module. Independent module is shown in Table 3.1: + +.. list-table:: Table 3.1 Module File Description + :widths: 40 150 + :header-rows: 1 + + * - File + - Functionality + * - :component_file:`lpn.c ` + - ESP-BLE-MESH Low Power functionality + * - :component_file:`friend.c ` + - ESP-BLE-MESH Friend functionality + * - :component_file:`net.c ` + - ESP-BLE-MESH Relay feature, network creation, IV Update procedure, IV Index recovery procedure, Key Refresh procedure related functionalities + * - :component_file:`proxy_server.c ` + - ESP-BLE-MESH Proxy Server related functionalities + * - :component_file:`proxy_client.c ` + - ESP-BLE-MESH Proxy Client related functionalities + * - :component_file:`settings.c ` + - ESP-BLE-MESH NVS storage functionality + * - :component_file:`main.c ` + - ESP-BLE-MESH stack initialize, stack enable, node removal related functionalities diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst new file mode 100644 index 000000000..d15e4603a --- /dev/null +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-faq.rst @@ -0,0 +1,663 @@ +ESP-BLE-MESH FAQ +================ + +This document provides a summary of frequently asked questions about developing with ESP-BLE-MESH, and is divided into seven sections: + +* :ref:`ble-mesh-faq-provisioner-development` +* :ref:`ble-mesh-faq-node-development` +* :ref:`ble-mesh-faq-ble-mesh-and-wi-fi-coexistence` +* :ref:`ble-mesh-faq-fast-provisioning` +* :ref:`ble-mesh-faq-log-help` +* :ref:`ble-mesh-faq-example-help` +* :ref:`ble-mesh-faq-others` + +Users could refer to the sections for quick answer to their questions. This document will be updated based on the feedback collected via various channels. + + +.. _ble-mesh-faq-provisioner-development: + +1. Provisioner Development +-------------------------- + +Generally, a Provisioner is used to provision unprovisioned devices and form a mesh network. And after provisioning, roles of the unprovisioned devices will be changed to those of a node. + +1.1 What is the flow for an unprovisioned device to join ESP-BLE-MESH network? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + There are two phases for a device to join ESP-BLE-MESH network via a Provisioner, namely, provisioning and configuration. + + - The phase of provisioning is to assign unicast address, add NetKey and etc. to a device. By provisioning, the device joins the ESP-BLE-MESH network and its role is changed from an unprovisioned device to a node. + + - The phase of configuration is to add AppKeys to the node and bind AppKeys to corresponding models. And some items are optional during configuration, including adding subscription addresses to the node, set publication information, etc. By configuration, the node can actually transmit messages to a Provisioner and receive messages from it. + +1.2 If a Provisioner wants to change states of a node, what requirements should be met for a Provisioner? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Client model that corresponds to server model of the node is required. + - NetKey and AppKey used to encrypt messages shall be owned by both the node and the Provisioner. + - The address owned by the node shall be known, which could be its unicast address or subscription address. + +1.3 How can NetKey and AppKey be used? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - NetKey is used for encryption of messages in Network Layer. Nodes with the same NetKey are assumed to be in the same subnet while those with different NetKeys cannot communicate with each other. + - AppKey is used for encryption of messages in Upper Transport Layer. If client model and server model are bound to different AppKeys, the communication cannot be achieved. + +1.4 How to generate a NetKey or AppKey for Provisioner? Can we use a fixed NetKey or AppKey? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - The API :cpp:func:`esp_ble_mesh_provisioner_add_local_net_key` can be used to add a NetKey with a fixed or random value. + - The API :cpp:func:`esp_ble_mesh_provisioner_add_local_app_key` can be used to add an AppKey with a fixed or random value. + +1.5 Is the unicast address of Provisioner fixed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The value of :code:`prov_unicast_addr` in :cpp:type:`esp_ble_mesh_prov_t` is used to set the unicast address of Provisioner, it can be set only once during initialization and can't be changed afterwards. + +1.6 Can the address of Provisioner serve as destination address of the node-reporting-status message? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The unicast address of Provisioner can be set only once during initialization and can't be changed afterwards. In theory, it can serve as the destination address of the node-reporting-status message, provided that the unicast address of the Provisioner is known by nodes. Nodes can know the unicast address of Provisioner during configuration since Provisioner sends messages to them with its unicast address used as the source address. + Subscription address can also be used. Provisioner subscribes to a group address or virtual address, and nodes send messages to the subscription address. + +1.7 Is the unicast address of the node that is firstly provisioned by Provisioner to ESP-BLE-MESH network fixed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The value of :code:`prov_start_address` in :cpp:type:`esp_ble_mesh_prov_t` is used to set the starting address when the Provisioner provisions unprovisioned devices, i.e. the unicast address of the node it firstly provisioned. It can be set only once during initialization and can't be changed afterwards. + +1.8 Is the unicast address of the node that mobile App firstly provisioned fixed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The App will decide the unicast address, and currently most of them are fixed. + +1.9 How to know which unprovisioned device is the Provisioner that is provisioning currently? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The value of :code:`prov_attention` in :cpp:type:`esp_ble_mesh_prov_t` is used by Provisioner set to unprovisioned device during provisioning. It can be set only once during initialization and can't be changed afterwards. When the unprovisioned device is joining the mesh network, it can display in a specific way like flashing light to notify Provisioner that it is being provisioned. + +1.10 How many ways to authenticate the devices during provisioning? Which way was used in the :example:`provided examples `? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + There are four authentication methods, i.e. No OOB, Static OOB, Output OOB and Input OOB. In the provided examples, No OOB is used. + +1.11 What information can be carried by the advertising packets of the unprovisioned device before provisioning into the network? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Device UUID + - OOB Info + - URL Hash (optional) + +1.12 Can such information be used for device identification? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + For example, each unprovisioned device contains a unique Device UUID, which can be used for device identification. + +1.13 How is the unicast address assigned when the node provisioned by Provisioner contains multiple elements? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Provisioner will assign an unicast address for the primary element of the node, and unicast address of the remaining elements are incremented one by one. + - For example: If an unprovisioned device has three elements, i.e. the primary element, the second element and the third element. After provisioning, the primary element address of the node is 0x0002 while the second element address is 0x0003, and the third element address is 0x0004. + +1.14 How can Provisioner get and parse the :ref:`Composition Data ` of nodes through Configuration Client Model? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Provisioner can get the Composition Data of nodes using the :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state` with :code:`comp_data_get` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_get_state_t` set properly. + - Users can refer to the following code to parse the Composition Data: + + .. code:: c + + #include + #include + #include + + //test date: 0C001A0001000800030000010501000000800100001003103F002A00 + //0C00 1A00 0100 0800 0300 0001 05 01 0000 0080 0100 0010 0310 3F002A00 + + // CID is 0x000C + // PID is 0x001A + // VID is 0x0001 + // CRPL is 0x0008 + // Features is 0x0003 – Relay and Friend features. + // Loc is “front” – 0x0100 + // NumS is 5 + // NumV is 1 + // The Bluetooth SIG Models supported are: 0x0000, 0x8000, 0x0001, 0x1000, 0x1003 + // The Vendor Models supported are: Company Identifier 0x003F and Model Identifier 0x002A + + typedef struct { + int16_t cid; + int16_t pid; + int16_t vid; + int16_t crpl; + int16_t features; + int16_t all_models; + uint8_t sig_models; + uint8_t vnd_models; + } esp_ble_mesh_composition_head; + + typedef struct { + uint16_t model_id; + uint16_t vendor_id; + } tsModel; + + typedef struct { + // reserve space for up to 20 SIG models + uint16_t SIG_models[20]; + uint8_t numSIGModels; + + // reserve space for up to 4 vendor models + tsModel Vendor_models[4]; + uint8_t numVendorModels; + } esp_ble_mesh_composition_decode; + + int decode_comp_data(esp_ble_mesh_composition_head *head, esp_ble_mesh_composition_decode *data, uint8_t *mystr, int size) + { + int pos_sig_base; + int pos_vnd_base; + int i; + + memcpy(head, mystr, sizeof(*head)); + + if(size < sizeof(*head) + head->sig_models * 2 + head->vnd_models * 4) { + return -1; + } + + pos_sig_base = sizeof(*head) - 1; + + for(i = 1; i < head->sig_models * 2; i = i + 2) { + data->SIG_models[i/2] = mystr[i + pos_sig_base] | (mystr[i + pos_sig_base + 1] << 8); + printf("%d: %4.4x\n", i/2, data->SIG_models[i/2]); + } + + pos_vnd_base = head->sig_models * 2 + pos_sig_base; + + for(i = 1; i < head->vnd_models * 2; i = i + 2) { + data->Vendor_models[i/2].model_id = mystr[i + pos_vnd_base] | (mystr[i + pos_vnd_base + 1] << 8); + printf("%d: %4.4x\n", i/2, data->Vendor_models[i/2].model_id); + + data->Vendor_models[i/2].vendor_id = mystr[i + pos_vnd_base + 2] | (mystr[i + pos_vnd_base + 3] << 8); + printf("%d: %4.4x\n", i/2, data->Vendor_models[i/2].vendor_id); + } + + return 0; + } + + void app_main(void) + { + esp_ble_mesh_composition_head head = {0}; + esp_ble_mesh_composition_decode data = {0}; + uint8_t mystr[] = { 0x0C, 0x00, 0x1A, 0x00, + 0x01, 0x00, 0x08, 0x00, + 0x03, 0x00, 0x00, 0x01, + 0x05, 0x01, 0x00, 0x00, + 0x00, 0x80, 0x01, 0x00, + 0x00, 0x10, 0x03, 0x10, + 0x3F, 0x00, 0x2A, 0x00}; + int ret; + + ret = decode_comp_data(&head, &data, mystr, sizeof(mystr)); + if (ret == -1) { + printf("decode_comp_data error"); + } + } + +1.15 How can Provisioner further configure nodes through obtained Composition Data? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner do the following configuration by calling the :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state`. + + - Add AppKey to nodes with :code:`app_key_add` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` set properly. + - Add subscription address to the models of nodes with :code:`model_sub_add` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` set properly. + - Set publication information to the models of nodes with :code:`model_pub_set` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` set properly. + +1.16 Can nodes add corresponding configurations for themselves? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + This method can be used in special cases like testing period. + + - Here is an example to show nodes add new group addresses for their models. + + .. code:: c + + esp_err_t example_add_fast_prov_group_address(uint16_t model_id, uint16_t group_addr) + { + const esp_ble_mesh_comp_t *comp = NULL; + esp_ble_mesh_elem_t *element = NULL; + esp_ble_mesh_model_t *model = NULL; + int i, j; + + if (!ESP_BLE_MESH_ADDR_IS_GROUP(group_addr)) { + return ESP_ERR_INVALID_ARG; + } + + comp = esp_ble_mesh_get_composition_data(); + if (!comp) { + return ESP_FAIL; + } + + for (i = 0; i < comp->element_count; i++) { + element = &comp->elements[i]; + model = esp_ble_mesh_find_sig_model(element, model_id); + if (!model) { + continue; + } + for (j = 0; j < ARRAY_SIZE(model->groups); j++) { + if (model->groups[j] == group_addr) { + break; + } + } + if (j != ARRAY_SIZE(model->groups)) { + ESP_LOGW(TAG, "%s: Group address already exists, element index: %d", __func__, i); + continue; + } + for (j = 0; j < ARRAY_SIZE(model->groups); j++) { + if (model->groups[j] == ESP_BLE_MESH_ADDR_UNASSIGNED) { + model->groups[j] = group_addr; + break; + } + } + if (j == ARRAY_SIZE(model->groups)) { + ESP_LOGE(TAG, "%s: Model is full of group addresses, element index: %d", __func__, i); + } + } + + return ESP_OK; + } + +.. note:: + + When the NVS storage of the node is enabled, group address added and AppKey bound by this method will not be saved in the NVS when the device is powered off currently. These configuration information can only be saved if they are configured by Configuration Client Model. + +1.17 How does Provisioner control nodes by grouping? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Generally there are two approaches to implement group control in ESP-BLE-MESH network, group address approach and virtual address approach. And supposing there are 10 devices, i.e., five devices with blue lights and five devices with red lights. + + - Method 1: 5 blue lights can subscribe to a group address, 5 red lights subscribe to another one. By sending messages to different group addresses, Provisioner can realize group control. + + - Method 2: 5 blue lights can subscribe to a virtual address, 5 red lights subscribe to another one. By sending messages to different virtual addresses, Provisioner can realize group control. + +1.18 How does Provisioner add nodes to multiple subnets? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner can add multiple NetKeys to nodes during configuration, and nodes sharing the same NetKey belong to the same subnet. Provisioner can communicate with nodes on different subnets by using different NetKeys. + +1.19 How does Provisioner know if a node in the mesh network is offline? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Node offline is usually defined as: the condition that the node cannot be properly communicated with other nodes in the mesh network due to power failure or some other reasons. + + There is no connection between nodes and nodes in the ESP-BLE-MESH network. They communicate with each other through advertising channels. + + An example is given here to show how to detect a node is offline by Provisioner. + + - The node can periodically send heartbeat messages to Provisioner. And if Provisioner failed to receive heartbeat messages in a certain period, the node is considered to be offline. + +.. note:: + + The heartbeat message should be designed into a single package (less than 11 bytes), so the transmission and reception of it can be more efficient. + +1.20 What operations should be performed when Provisioner removes nodes from the network? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Usually when Provisioner tries to remove node from the mesh network, the procedure includes three main steps: + + - Firstly, Provisioner adds the node that need to be removed to the "blacklist". + + - Secondly, Provisioner performs the :ref:`Key Refresh procedure `. + + - Lastly, the node performs node reset procedure, and switches itself to an unprovisioned device. + +1.21 In the Key Refresh procedure, how does Provisioner update the Netkey owned by nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Provisioner updates the NetKey of nodes using the :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state` with :code:`net_key_update` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` set properly. + + - Provisioner updates the AppKey of nodes using the :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state` with :code:`app_key_update` in the parameter :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` set properly. + +1.22 How does Provisioner manage nodes in the mesh network? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + ESP-BLE-MESH implements several functions related to basic node management in the example, such as :cpp:func:`esp_ble_mesh_store_node_info`. And ESP-BLE-MESH also provides the API :cpp:func:`esp_ble_mesh_provisioner_set_node_name` which can be used to set the node's local name and the API :cpp:func:`esp_ble_mesh_provisioner_get_node_name` which can be used to get the node's local name. + +1.23 What does Provisioner need when trying to control the server model of nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner must include corresponding client model before controlling the server model of nodes. + + Provisioner shall add its local NetKey and AppKey. + + - Provisioner add NetKey by calling the API :cpp:func:`esp_ble_mesh_provisioner_add_local_net_key`. + + - Provisioner add AppKey by calling the API :cpp:func:`esp_ble_mesh_provisioner_add_local_app_key`. + + Provisioner shall configure its own client model. + + - Provisioner bind AppKey to its own client model by calling the API :cpp:func:`esp_ble_mesh_provisioner_bind_app_key_to_local_model`. + +1.24 How does Provisoner control the server model of nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + ESP-BLE-MESH supports all SIG-defined client models. Provisioner can use these client models to control the server models of nodes. And the client models are divided into 6 categories with each category has the corresponding functions. + + - Configuration Client Model + + - The API :cpp:func:`esp_ble_mesh_config_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_cfg_client_get_state_t` values of Configuration Server Model. + - The API :cpp:func:`esp_ble_mesh_config_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` values of Configuration Server Model. + + - Health Client Model + + - The API :cpp:func:`esp_ble_mesh_health_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_health_client_get_state_t` values of Health Server Model. + - The API :cpp:func:`esp_ble_mesh_health_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_health_client_set_state_t` values of Health Server Model. + + - Generic Client Models + + - The API :cpp:func:`esp_ble_mesh_generic_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_generic_client_get_state_t` values of Generic Server Models. + - The API :cpp:func:`esp_ble_mesh_generic_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_generic_client_set_state_t` values of Generic Server Models. + + - Lighting Client Models + + - The API :cpp:func:`esp_ble_mesh_light_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_light_client_get_state_t` values of Lighting Server Models. + - The API :cpp:func:`esp_ble_mesh_light_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_light_client_set_state_t` values of Lighting Server Models. + + - Sensor Client Models + + - The API :cpp:func:`esp_ble_mesh_sensor_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_sensor_client_get_state_t` values of Sensor Server Model. + - The API :cpp:func:`esp_ble_mesh_sensor_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_sensor_client_set_state_t` values of Sensor Server Model. + + - Time and Scenes Client Models + - The API :cpp:func:`esp_ble_mesh_time_scene_client_get_state` can be used to get the :cpp:type:`esp_ble_mesh_time_scene_client_get_state_t` values of Time and Scenes Server Models. + - The API :cpp:func:`esp_ble_mesh_time_scene_client_set_state` can be used to set the :cpp:type:`esp_ble_mesh_time_scene_client_set_state_t` values of Time and Scenes Server Models. + + +.. _ble-mesh-faq-node-development: + +2. Node Development +------------------- + +2.1 What kind of models are included by nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - In ESP-BLE-MESH, nodes are all composed of a series of models with each model implements some functions of the node. + + - Model has two types, client model and server model. Client model can get and set the states of server model. + + - Model can also be divided into SIG model and vendor model. All behaviors of SIG models are officially defined while behaviors of vendor models are defined by users. + +2.2 Is the format of messages corresponding to each model fixed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Messages, which consist of opcode and payload, are divided by opcode. + + - The type and the format of the messages corresponding to models are both fixed, which means the messages transmitted between models are fixed. + +2.3 Which functions can be used to send messages with the models of nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - For client models, users can use the API :cpp:func:`esp_ble_mesh_client_model_send_msg` to send messages. + + - For server models, users can use the API :cpp:func:`esp_ble_mesh_server_model_send_msg` to send messages. + + - For publication, users call the API :cpp:func:`esp_ble_mesh_model_publish` to publish messages. + +2.4 How to achieve the transmission of messages without packet loss? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Acknowledegd message is needed if users want to transmit messages without packet loss. The default time to wait for corresponding response is set in :ref:`CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT`. If the sender waits for the response until the timer expires, the corresponding timeout event would be triggered. + +.. note:: + + Response timeout can be set in the API :cpp:func:`esp_ble_mesh_client_model_send_msg`. The default value (4 seconds) would be applied if the parameter :code:`msg_timeout` is set to **0**. + +2.5 How to send unacknowledged messages? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + For client models, users can use the API :cpp:func:`esp_ble_mesh_client_model_send_msg` with the parameter :code:`need_rsp` set to :code:`false` to send unacknowledged messages. + + For server models, the messages sent by using the API :cpp:func:`esp_ble_mesh_server_model_send_msg` are always unacknowledged messages. + +2.6 How to add subscription address to models? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Subscription address can be added through Configuration Client Model. + +2.7 What is the difference between messages sent and published by models? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Messages sent by calling the API :cpp:func:`esp_ble_mesh_client_model_send_msg` or :cpp:func:`esp_ble_mesh_server_model_send_msg` will be sent in the duration determined by the Network Transmit state. + + Messages published by calling the API :cpp:func:`esp_ble_mesh_model_publish` will be published determined by the Model Publication state. And the publication of messages is generally periodic or with a fixed number of counts. The publication period and publication count are controlled by the Model Publication state, and can be configured through Configuration Client Model. + +2.8 How many bytes can be carried when sending unsegmented messages? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The total payload length (which can be set by users) of unsegmented message is 11 octets, so if the opcode of the message is 2 octets, then the message can carry 9-octets of valid information. For vendor messages, due to the 3-octets opcode, the remaining payload length is 8 octets. + +2.9 When should the :ref:`Relay ` feature of nodes be enabled? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Users can enable the Relay feature of all nodes when nodes detected in the mesh network are sparse. + + For dense mesh network, users can choose to just enable the Relay feature of several nodes. + + And users can enable the Relay feature by default if the mesh network size is unknown. + +2.10 When should the :ref:`Proxy ` feature of node be enabled? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + If the unprovisioned device is expected to be provisioned by a phone, then it should enable the Proxy feature since almost all the phones do not support sending ESP-BLE-MESH packets through advertising bearer currently. And after the unprovisioned device is provisioned successfully and becoming a Proxy node, it will communicate with the phone using GATT bearer and using advertising bearer to communicate with other nodes in the mesh network. + +2.11 How to use the Proxy filter? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The Proxy filter is used to reduce the number of Network PDUs exchanged between a Proxy Client (e.g. the phone) and a Proxy Server (e.g. the node). And with the Proxy filter, Proxy Client can explicitly request to receive only mesh messages with certain destination addresses from Proxy Server. + +2.12 When a message can be relayed by a Relay node? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + If a message need to be relayed, the following conditions should be met. + + - The message is in the mesh network. + + - The message is not sent to the unicast address of the node. + + - The value of TTL in the message is greater than 1. + +2.13 If a message is segmented into several segments, should the other Relay nodes just relay when one of these segments is received or wait until the message is received completely? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Relay nodes will forward segments when one of them are received rather than keeping waiting until all the segments are received. + +2.14 What is the principle of reducing power consumption using :ref:`Low Power ` feature? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - When the radio is turned on for listening, the device is consuming energy. When low power feature of the node is enabled, it will turn off its radio in the most of the time. + + - And cooperation is needed between low power node and friend node, thus low power node can receive messages at an appropriate or lower frequency without the need to keep listening. + + - When there are some new messages for low power node, its friend node will store the messages for it. And low power node can poll friend nodes to see if there are new messages at a fixed interval. + +2.15 How to continue the communication on the network after powering-down and powering-up again? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Enable the configuration :code:`Store ESP-BLE-MESH Node configuration persistently` in `menuconfig`. + +2.16 How to send out the self-test results of nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + It is recommended that nodes can publish its self-test results periodically through Health Server Model. + +2.17 How to transmit information between nodes? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + One possible application scenario for transmitting information between nodes is that spray nodes would be triggered once smoke alarm detected high smoke concentration. There are two approaches in implementation. + + - Approach 1 is that spray node subscribes to a group address. When smoke alarm detects high smoke concentration, it will publish a message whose destination address is the group address which has been subscribed by spray node. + + - Approach 2 is that Provisioner can configure the unicast address of spray node to the smoke alarm. When high smoke concentration is detected, smoke alarm can use send messages to the spray node with the spray node's unicast address as the destination address. + +2.18 Is gateway a must for nodes communication? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Situation 1: nodes only communicate within the mesh network. In this situation, no gateway is need. ESP-BLE-MESH network is a flooded network, messages in the network have no fixed paths, and nodes can communicate with each other freely. + + - Situation 2: if users want to control the nodes remotely, for example turn on some nodes before getting home, then a gateway is needed. + +2.19 When will the IV Update procedure be performed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + IV Update procedure would be performed once sequence number of messages sent detected by the bottom layer of node reached a critical value. + +2.20 How to perform IV Update procedure? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Nodes can perform IV Update procedure with Secure Network Beacon. + + +.. _ble-mesh-faq-ble-mesh-and-wi-fi-coexistence: + +3. ESP-BLE-MESH and Wi-Fi Coexistence +------------------------------------- + +3.1 Which modes does Wi-Fi support when it coexists with ESP-BLE-MESH? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Currently only Wi-Fi station mode supports the coexistence. + +3.2 Why is the Wi-Fi throughput so low when Wi-Fi and ESP-BLE-MESH coexist? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The `ESP32-DevKitC <../../hw-reference/get-started-devkitc>`_ board without PSRAM can run properly but the throughput of it is low since it has no PSRAM. When Bluetooth and Wi-Fi coexist, the throughput of ESP32-DevKitC with PSRAM can be stabilized to more than 1Mbps. + + And some configurations in menuconfig shall be enabled to support PSRAM. + + - :code:`ESP32-specific --> Support for external,SPI-connected RAM --> Try to allocate memories of Wi-Fi and LWIP...` + - :code:`Bluetooth --> Bluedroid Enable --> BT/BLE will first malloc the memory from the PSRAM` + - :code:`Bluetooth --> Bluedroid Enable --> Use dynamic memory allocation in BT/BLE stack.` + - :code:`Bluetooth --> Bluetooth controller --> BLE full scan feature supported.` + - :code:`Wi-Fi --> Software controls Wi-Fi/Bluetooth coexistence --> Wi-Fi` + + +.. _ble-mesh-faq-fast-provisioning: + +4. Fast Provisioning +-------------------- + +4.1 Why is fast provisioning needed? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Normally when they are several unprovisioned devices, users can provision them one by one. But when it comes to a large number of unprovisioned devices (e.g. 100), provisioning them one by one will take huge amount of time. With fast provisioning, users can provision 100 unprovisioned devices in about 50 seconds. + +4.2 Why EspBleMesh App would wait for a long time during fast provisioning? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + After the App provisioned one Proxy node, it will disconnect from the App during fast provisioning, and reconnect with the App when all the nodes are provisioned. + +4.3 Why is the number of node addresses displayed in the App is more than that of existing node addresses? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Each time after a fast provisioning process, and before starting a new one, the node addresses in the App should be cleared, otherwise the number of the node address will be incorrect. + +4.4 What is the usage of the **count** value which was input in EspBleMesh App? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The **count** value is provided to the Proxy node which is provisioned by the App so as to determine when to start Proxy advertising in advance. + +4.5 When will Configuration Client Model of the node running :example:`fast_prov_server ` example start to work? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Configuration Client Model will start to work after the Temporary Provisioner functionality is enabled. + +4.6 Will the Temporary Provisioner functionality be enabled all the time? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + After the nodes receive messages used to turn on/off lights, all the nodes will disable its Temporary Provisioner functionality and become nodes. + + +.. _ble-mesh-faq-log-help: + +5. Log Help +----------- + +You can find meaning of errors or warnings when they appear at the bottom of ESP-BLE-MESH stack. + +5.1 What is the meaning of warning :code:`ran out of retransmit attempts`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node transmits a segmented message, and due to some reasons, the receiver doesn't receive the complete message. Then the node will retransmit the message. When the retransmission count reaches the maximum number, which is 4 currently, then this warning will appear. + +5.2 What is the meaning of warning :code:`Duplicate found in Network Message Cache`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node receives a message, it will compare the message with the ones stored in the network cache. If the same has been found in the cache, which means it has been received before, then the message will be dropped. + +5.3 What is the meaning of warning :code:`Incomplete timer expired`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node doesn't receive all the segments of a segmented message during a certain period (e.g. 10 seconds), then the Incomplete timer will expire and this warning will appear. + +5.4 What is the meaning of warning :code:`No matching TX context for ack`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node receives a segment ack and it doesn't find any self-send segmented message related with this ack, then this warning will appear. + +5.5 What is the meaning of warning :code:`No free slots for new incoming segmented messages`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node has no space for receiving new segmented message, this warning will appear. Users can make the space larger through the configuration :ref:`CONFIG_BLE_MESH_RX_SEG_MSG_COUNT`. + +5.6 What is the meaning of error :code:`Model not bound to Appkey 0x0000`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + When the node sends messages with a model and the model has not been bound to the AppKey with AppKey Index 0x000, then this error will appear. + +5.7 What is the meaning of error :code:`Busy sending message to DST xxxx`? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + This error means client model of the node has transmitted a message to the target node and now is waiting for a response, users can not send messages to the same node with the same unicast address. After the corresponding response is received or timer is expired, then another message can be sent. + + +.. _ble-mesh-faq-example-help: + +6. Example Help +--------------- + +6.1 How are the ESP-BLE-MESH callback functions classified? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - The API :cpp:func:`esp_ble_mesh_register_prov_callback` is used to register callback function used to handle provisioning and networking related events. + - The API :cpp:func:`esp_ble_mesh_register_config_client_callback` is used to register callback function used to handle Configuration Client Model related events. + - The API :cpp:func:`esp_ble_mesh_register_config_server_callback` is used to register callback function used to handle Configuration Server Model related events. + - The API :cpp:func:`esp_ble_mesh_register_health_client_callback` is used to register callback function used to handle Health Client Model related events. + - The API :cpp:func:`esp_ble_mesh_register_health_server_callback` is used to register callback function used to handle Health Server Model related events. + - The API :cpp:func:`esp_ble_mesh_register_generic_client_callback` is used to register callback function used to handle Generic Client Models related events. + - The API :cpp:func:`esp_ble_mesh_register_light_client_callback` is used to register callback function used to handle Lighting Client Models related events. + - The API :cpp:func:`esp_ble_mesh_register_sensor_client_callback` is used to register callback function used to handle Sensor Client Model related events. + - The API :cpp:func:`esp_ble_mesh_register_time_scene_client_callback` is used to register callback function used to handle Time and Scenes Client Models related events. + - The API :cpp:func:`esp_ble_mesh_register_custom_model_callback` is used to register callback function used to handle vendor model and unrealized server models related events. + + +.. _ble-mesh-faq-others: + +7. Others +--------- + +7.1 How to print the message context? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The examples use :cpp:func:`ESP_LOG_BUFFER_HEX` to print the message context while the ESP-BLE-MESH protocol stack uses :cpp:func:`bt_hex`. + +7.2 Which API can be used to restart ESP32? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The API :cpp:func:`esp_restart`. + +7.3 How to monitor the remaining space of the stack of a task? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The API :cpp:func:`vTaskList` can be used to print the remaining space of the task stack periodically. + +7.4 How to change the level of log without changing the menuconfig output level? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + The API :cpp:func:`esp_log_level_set` can be used to change the log output level rather than using menuconfig to change it. diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst new file mode 100644 index 000000000..c12beb2d3 --- /dev/null +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst @@ -0,0 +1,147 @@ +ESP-BLE-MESH Feature List +========================= + +Supported Features +------------------ + +Mesh Core +""""""""" + +* Provisioning: Node Role + * PB-ADV and PB-GATT + * OOB Authentication + +* Provisioning: Provisioner Role + * PB-ADV and PB-GATT + * OOB Authentication + +* Networking + * Relay + * Segmentation and Reassembly + * Key Refresh Procedure + * IV Update Procedure + * Friend + * Low Power + * Proxy Server + * Proxy Client + +* Multiple Client Models Run Simultaneously + * Support multiple client models send packets to different nodes simultaneously + * No blocking between client model and server model + +* NVS Storing + * Store provisioning and configuration information of ESP-BLE-MESH Node + +Mesh Models +""""""""""" + +* Foundation models + * Configuration Server model + * Configuration Client model + * Health Server model + * Health Client model + +* Generic client models + * Generic OnOff Client + * Generic Level Client + * Generic Default Transition Time Client + * Generic Power OnOff Client + * Generic Power Level Client + * Generic Battery Client + * Generic Location Client + * Generic Property Client + +* Sensor client models + * Sensor Client + +* Time and Scenes client models + * Time Client + * Scene Client + * Scheduler Client + +* Lighting client models + * Light Lightness Client + * Light CTL Client + * Light HSL Client + * Light xyL Client + * Light LC Client + +* Generic server models + * Generic OnOff Server + * Generic Level Server + * Generic Default Transition Time Server + * Generic Power OnOff Server + * Generic Power OnOff Setup Server + * Generic Power Level Server + * Generic Power Level Setup Server + * Generic Battery Server + * Generic Location Server + * Generic Location Setup Server + * Generic User Property Server + * Generic Admin Property Server + * Generic Manufacturer Property Server + * Generic Client Property Server + +* Sensor server models + * Sensor Server + * Sensor Setup Server + +* Time and Scenes server models + * Time Server + * Time Setup Server + * Scene Server + * Scene Setup Server + * Scheduler Server + * Scheduler Setup Server + +* Lighting server models + * Light Lightness Server + * Light Lightness Setup Server + * Light CTL Server + * Light CTL Temperature Server + * Light CTL Setup Server + * Light HSL Server + * Light HSL Hue Server + * Light HSL Saturation Server + * Light HSL Setup Server + * Light xyL Server + * Light xyL Setup Server + * Light LC Server + * Light LC Setup Server + +Mesh Applications +""""""""""""""""" + +* ESP-BLE-MESH Node + * :example:`Tutorial ` + * :example:`Tutorial ` + * :example:`Example ` +* ESP-BLE-MESH Provisioner + * :example:`Tutorial ` + * :example:`Example ` +* ESP-BLE-MESH Fast Provisioning + * :example:`Fast Provisioning Client Model Tutorial ` + * :example:`Fast Provisioning Server Model Tutorial ` + * :example:`Example ` + * `Demo Video `__ +* ESP-BLE-MESH and Wi-Fi Coexistence + * :example:`Tutorial ` + * :example:`Example ` + * `Demo Video `__ +* ESP-BLE-MESH Console Commands + * :example:`Example ` + + +Future Release Features +----------------------- + +Mesh Core +""""""""" + +* Provisioner NVS Storage + +Mesh Applications +""""""""""""""""" + +* Fast OTA +* Friendship \ No newline at end of file diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst new file mode 100644 index 000000000..b74cb24d5 --- /dev/null +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-index.rst @@ -0,0 +1,274 @@ +************ +ESP-BLE-MESH +************ + +Bluetooth® mesh networking enables many-to-many (m:m) device communications and is optimized for creating large-scale device networks. + +Devices may relay data to other devices not in direct radio range of the originating device. In this way, mesh networks can span very large physical areas and contain large numbers of devices. It is ideally suited for building automation, sensor networks, and other IoT solutions where tens, hundreds, or thousands of devices need to reliably and securely communicate with one another. + +Bluetooth mesh is not a wireless communications technology, but a networking technology. This technology is dependent upon Bluetooth Low Energy (BLE) - a wireless communications protocol stack. + +Built on top of Zephyr Bluetooth Mesh stack, the ESP-BLE-MESH implementation supports device provisioning and node control. It also supports such node features as Proxy, Relay, Low power and Friend. + +Please see the :doc:`ble-mesh-architecture` for information about the implementation of ESP-BLE-MESH architecture and :doc:`ESP-BLE-MESH API Reference <../../api-reference/bluetooth/esp-ble-mesh>` for information about respective API. + +ESP-BLE-MESH is implemented and certified based on the latest Mesh Profile v1.0.1, users can refer `here `_ for the certification details of ESP-BLE-MESH. + +.. note:: + + If you are looking for Wi-Fi based implementation of mesh for ESP32, please check another product by Espressif called ESP-MESH. For more information and documentation see :doc:`ESP-MESH <../../api-reference/network/esp_mesh>`. + + +.. _getting-started-with-ble-mesh: + +Getting Started with ESP-BLE-MESH +================================= + +This section is intended to help you get started with ESP-BLE-MESH for the hardware based on the ESP32 chip by Espressif. + +We are going to demonstrate process of setting and operation of a small ESP-BLE-MESH network of three nodes. This process will cover device provisioning and node configuration, and then sending on/off commands to Generic OnOff Server Models on specific nodes. + +If you are new to ESP-IDF, please first set up development environment, compile , flash and run example application following top level ESP-IDF :doc:`../../get-started/index` documentation. + + +What You Need +------------- + +Hardware: + +* Three ESP32 boards, see :ref:`options `. +* USB cables to connect the boards. +* Computer configured with ESP-IDF. +* Mobile phone or tablet running Android or iOS. + +Software: + +* Example application :example:`bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server` code to load to the ESP32 boards. +* Mobile App: **nRF Mesh** for Android or iOS. Optionally you can use some other Apps: + + - `EspBleMesh `_ Android App + - Silicon Labs Android or iOS App + +Installation Step by Step +------------------------- + +This is a detailed roadmap to walk you through the installation process. + + +.. _get-started-ble-mesh-check-hardware: + +Step 1. Check Hardware +"""""""""""""""""""""" + +Both `ESP32-DevKitC`_ and `ESP-WROVER-KIT`_ development boards are supported for ESP-BLE-MESH implementation. You can choose particular board through menuconfig: :code:`idf.py menuconfig` > ``Example Configuration`` > ``Board selection for ESP-BLE-MESH`` + +.. note:: + + If you plan to use `ESP32-DevKitC`_, connect a RGB LED to GPIO pins 25, 26 and 27. + + +Step 2. Configure Software +"""""""""""""""""""""""""" + +Enter the :example:`bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server` example directory, run :code:`idf.py menuconfig` to select your board and then run :code:`idf.py build` to compile the example. + +Step 3. Upload Application to Nodes +""""""""""""""""""""""""""""""""""" + +After the :example:`bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server` example is compiled successfully, users can run :code:`idf.py flash` to upload the same generated binary files into each of the three development boards. + +Once boards are powered on, the RGB LED on each board should turn **GREEN**. + +.. figure:: ../../../_static/ble-mesh-device-power-on.png + :align: center + + ESP-BLE-MESH Devices Power On + +Step 4. Provision Nodes +""""""""""""""""""""""" + +In this section, we will use the **nRF Mesh Android** App to demonstrate how to provision an unprovisioned device. Users can also get its iOS version from the App Store. + +4.1 Scanner +^^^^^^^^^^^ + +The Scanner is App's functionality to search for unprovisioned devices in range. Open the App, press **Scanner** at the bottom and the search will start. After a short while we should see three unprovisioned devices displayed. + +.. figure:: ../../../_static/ble-mesh-scanner.png + :align: center + :height: 370 + + nRF Mesh - Scanner + +4.2 Identify +^^^^^^^^^^^^ + +Users can select any unprovisioned device, then the App will try to set up a connection with the selected device. After the BLE connection is established successfully (sometimes users need to try multiple times to get connected), and proper ESP-BLE-MESH GATT Service is discovered, users can see the **IDENTIFY** interface button on the screen. The IDENTIFY operation can be used to tell users which device is going to be provisioned. + +.. note:: + The IDENTIFY operation also needs some cooperation on the device side, then users can see which device is in the provisioning process. Currently when pressing the **IDENTIFY** interface button, no signs can been seen from the device except from the log on the serial monitor. + +After the **IDENTIFY** interface button is pressed, users can see the **PROVISION** interface button. + +.. figure:: ../../../_static/ble-mesh-identify-provision.png + :align: center + :height: 370 + + nRF Mesh - IDENTIFY - PROVISION + +4.3 Provision +^^^^^^^^^^^^^ + +Then, the App will try to provision the unprovisioned device. When the device is provisioned successfully, the RGB LED on the board will turn off, and the App will implement the following procedures: + +#. Disconnect with the node +#. Try to reconnect with the node +#. Connect successfully and discover ESP-BLE-MESH GATT Service +#. Get Composition Data of the node and add AppKey to it + +When all the procedures are finished, the node is configured properly. And after pressing **OK**, users can see that unicast address is assigned, and Composition Data of the node is decoded successfully. + +.. figure:: ../../../_static/ble-mesh-config-complete.png + :align: center + :height: 370 + + nRF Mesh - Configuration Complete + +Sometimes in procedure 2, the App may fail to reconnect with the node. In this case, after pressing **OK**, users can see that only unicast address of the node has been assigned, but no Composition Data has been got. Then users need to press **CONNECT** on the top right, and the previously provisioned node will be displayed on the screen, and users need to choose it and try to connect with the node. + +.. figure:: ../../../_static/ble-mesh-initial-config-fail.png + :align: center + :height: 370 + + nRF Mesh - Initial Configuration Failed + +After connecting successfully, the App will show the interface buttons which can be used to get Composition Data and add AppKey. + +.. figure:: ../../../_static/ble-mesh-reconnect-initial-config.png + :align: center + :height: 370 + + nRF Mesh - Reconnect - Initial Configuration + +If the device is the second or the third one which has been provisioned by the App, and after pressing **CONNECT**, users can see two or three nodes on the screen. In this situation, users can choose any device to connect with, once succeed then go back to the main screen to choose the node which needs to be configured. + +Here an example of three devices listed. + +* The left picture shows that the third device is provisioned successfully, but the App failed to connect with it. When it tries to reconnect with the third node, three nodes are displayed on the App. +* The right picture shows that after connecting with any node successfully, the App displays the information of the three nodes. Users can see that the App has got the Composition Data of the first and the second nodes, but for the third one, only the unicast address has been assigned to it while the Composition Data is unknown. + +.. figure:: ../../../_static/ble-mesh-reconnect-three.png + :align: center + :height: 370 + + nRF Mesh - Reconnect - Three Nodes + +4.4 Configuration +^^^^^^^^^^^^^^^^^ + +When provisioning and initial configuration are finished, users can start to configure the node, such as binding AppKey with each model with the elements, setting publication information to it, etc. + +Example below shows how to bind AppKey with Generic OnOff Server Model within the Primary Element. + +.. figure:: ../../../_static/ble-mesh-model-bind-appkey.png + :align: center + :height: 370 + + nRF Mesh - Model Bind AppKey + +.. note:: + + No need to bind AppKey with the Configuration Server Model, since it only uses the DevKey to encrypt messages in the Upper Transport Layer. + +Step 5. Operate Network +""""""""""""""""""""""" + +After all the Generic OnOff Server Models within the three elements are bound with proper AppKey, users can use the App to turn on/off the RGB LED. + +In the :example:`bluetooth/esp_ble_mesh/ble_mesh_node/onoff_server` example, the first Generic OnOff Server Model is used to control the **RED** color, the second one is used to control the **GREEN** color and the third one is used to control the **BLUE** color. + +.. figure:: ../../../_static/ble-mesh-generic-onoff.png + :align: center + :height: 370 + + nRF Mesh - Generic OnOff Control + +The following screenshot shows different board with different color on. + +.. figure:: ../../../_static/ble-mesh-three-nodes-on.png + :align: center + + Three ESP-BLE-MESH Nodes On + +.. note:: + For **nRF Mesh** iOS App [version 1.0.4], when the node contains more than one element, the App is not behaving correctly. If users try to turn on/off the second or the third Generic OnOff Server Model, the message sent by the App is destinated to the first Generic OnOff Server Model within the Primary Element. + + +.. _esp-ble-mesh-examples: + +ESP-BLE-MESH Examples +===================== + +* :example:`ESP-BLE-MESH Node OnOff Server ` - shows the use of ESP-BLE-MESH as a node having a Configuration Server model and a Generic OnOff Server model. A ESP-BLE-MESH Provisioner can then provision the unprovisioned device and control a RGB LED representing on/off state, see :example:`example code `. + +* :example:`ESP-BLE-MESH Node OnOff Client ` - shows how a Generic OnOff Client model works within a node. The node has a Configuration Server model and a Generic OnOff Client model, see :example:`example code `. + +* :example:`ESP-BLE-MESH Provisioner ` - shows how a device can act as an ESP-BLE-MESH Provisioner to provision devices. The Provisioner has a Configuration Server model, a Configuration Client model and a Generic OnOff Client model, see :example:`example code `. + +* ESP-BLE-MESH Fast Provisioning - :example:`Client ` and :example_file:`Server ` - this example is used for showing how fast provisioning can be used in order to create a mesh network. It takes no more than 60 seconds to provision 100 devices, see :example:`example client code ` and :example:`example server code `. + +* :example:`ESP-BLE-MESH and Wi-Fi Coexistence ` - an example that demonstrates the Wi-Fi and Bluetooth (BLE/BR/EDR) coexistence feature of ESP32. Simply put, users can use the Wi-Fi while operating Bluetooth, see :example:`example code `. + +* ESP-BLE-MESH Node Console - an example that implements BLE Mesh node basic features. Within this example a node can be scanned and provisioned by Provisioner and reply to get/set message from Provisioner, see :example:`example node code ` and :example:`example Provisioner code `. + + +.. _esp-ble-mesh-demo-videos: + +ESP-BLE-MESH Demo Videos +======================== + +* `Provisioning of ESP-BLE-MESH nodes using Smartphone App `_ +* `Espressif Fast Provisioning using ESP-BLE-MESH App `_ +* `Espressif ESP-BLE-MESH and Wi-Fi Coexistence `_ + + +ESP-BLE-MESH FAQ +================ + +* :ref:`ble-mesh-faq-provisioner-development` +* :ref:`ble-mesh-faq-node-development` +* :ref:`ble-mesh-faq-ble-mesh-and-wi-fi-coexistence` +* :ref:`ble-mesh-faq-fast-provisioning` +* :ref:`ble-mesh-faq-log-help` +* :ref:`ble-mesh-faq-example-help` +* :ref:`ble-mesh-faq-others` + + +Related Documents +================= + +.. toctree:: + :maxdepth: 1 + + ble-mesh-feature-list + ble-mesh-architecture + ble-mesh-faq + ble-mesh-terminology + + + +Bluetooth SIG Documentation +--------------------------- + +- `BLE Mesh Core Specification `_ +- `BLE Mesh Model Specification `_ +- `An Intro to Bluetooth Mesh Part 1 `_ / `Part 2 `__ +- `The Fundamental Concepts of Bluetooth Mesh Networking, Part 1 `_ / `Part 2 `__ +- `Bluetooth Mesh Networking: Friendship `_ +- `Management of Devices in a Bluetooth Mesh Network `_ +- `Bluetooth Mesh Security Overview `_ +- `Provisioning a Bluetooth Mesh Network Part 1 `_ / `Part 2 `__ + + +.. _ESP32-DevKitC: https://www.espressif.com/en/products/hardware/esp32-devkitc/overview +.. _ESP-WROVER-KIT: https://www.espressif.com/en/products/hardware/esp-wrover-kit/overview diff --git a/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst b/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst new file mode 100644 index 000000000..ee6ce6150 --- /dev/null +++ b/docs/en/api-guides/esp-ble-mesh/ble-mesh-terminology.rst @@ -0,0 +1,221 @@ +ESP-BLE-MESH Terminology +======================== + +:link_to_translation:`zh_CN:[中文]` + +.. _ble-mesh-terminology-role: + +.. list-table:: Table 1 ESP-BLE-MESH Terminology - Role + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Unprovisioned Device + - A device that is not a member of a mesh network is known as an unprovisioned device. + - Examples: lighting devices, temperature control devices, manufacturing equipments and electric doors, etc. + * - Node + - A node is a provisioned device. + - The role of unprovisioned device will change to node after being provisioned to ESP-BLE-MESH network. Nodes (such as lighting devices, temperature control devices, manufacturing equipments, and electric doors) are devices that can send, receive, or relay messages in ESP-BLE-MESH network, and they can optionally support one or more subnets. + * - Relay Node + - A node that supports the Relay feature and has the Relay feature enabled is known as a Relay node. + - Relay nodes can receive and resend ESP-BLE-MESH messages, so the messages can be transferred further. Users can decide whether or not to enable forwarding function of nodes according to nodes' status. Messages can be relayed for multiple times, and each relay is considered as a "hop". Messages can hop up to 126 times, which is enough for message transmission in a wide area. + * - Proxy Node + - A node that supports the Proxy feature and has the Proxy feature enabled is known as a Proxy node. + - Proxy nodes receive messages from one bearer (it generally includes advertising bearer and GATT bearer) and resend it from another one. The purpose is to connect communication equipments that only support GATT bearer to ESP-BLE-MESH network. Generally, mobile apps need a Proxy node to access Mesh network. Without Proxy nodes, mobile apps cannot communicate with members in Mesh network. + * - Friend Node + - A node that supports the Friend feature, has the Friend feature enabled, and has a friendship with a node that supports the Low Power feature is known as a Friend node. + - Friend node, like the backup of Low Power node (LPN), can store messages that are sent to Low Power node and security updates; the stored information will be transferred to Low Power node when Low Power node needs it. Low Power node must establish "friendship" with another node that supports the Friend Feature to reduce duty cycle of its receiver, thus power consumption of Low Power node can be reduced. Low Power node needs to find a Friend node to establish a friendship with it. The process involved is called "friendship establishment". Cooperation between Low Power node and Friend nodes enables Low Power node to schedule the use of the radio, thus Low Power node can receive messages at an appropriate or lower frequency without the need of keeping listening. Low Power node will poll Friend node to see if there is new message. + * - Low Power Node + - A node that supports the Low Power feature and has a friendship with a node that supports the Friend feature is known as a Low Power node. + - By polling, Low Power node gets information from Friend node, such as messages, security updates, and etc. + * - Provisioner + - A node that is capable of adding a device to a mesh network. + - The device that can provision unprovisioned devices is called a Provisioner. This process usually needs to be implemented through an app that is typically provided by the product manufacturer and can be used on a gateway, a smartphone, tablet or other carriers. + + +.. _ble-mesh-terminology-composition: + +.. list-table:: Table 2 ESP-BLE-MESH Terminology - Composition + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - State + - A value representing a condition of an element that is exposed by an element of a node. + - Each node in a ESP-BLE-MESH network has an independent set of state values that indicate certain states of the device, like brightness, and color of lighting device. Change of state value will lead to change of the physical state of devices. For example, changing the on/off state of a device is actually turning on/off the device. + * - Model + - A model defines the basic functionality of a node. + - A node may contain multiple models, and each model defines basic functionalities of nodes, like the states needed by the nodes, the messages controlling the states, and actions resulted from messages handling. The function implementation of the nodes is based on models, which can be divided into SIG Model and Vendor Model, with the former defined by SIG and latter defined by users. + * - Element + - An addressable entity within a device. + - A node can contain one or more elements, with each having a unicast address and one or more models, and the models contained by the same element must not be the same. + * - Composition Data State + - The Composition Data state contains information about a node, the elements it includes, and the supported models. + - By reading the value of the Composition Data state, users can know basic information of the node, such as the number of elements, and the models in each element. Provisioner gets this message to further provision the device, such as configuring subscription address and publishing address of nodes. + + +.. _ble-mesh-terminology-features: + +.. list-table:: Table 3 ESP-BLE-MESH Terminology - Features + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Low Power Feature + - The ability to operate within a mesh network at significantly reduced receiver duty cycles only in conjunction with a node supporting the Friend feature. + - Low Power feature reduces power consumption of nodes. When a Low Power node is searching for a Friend node, and there are multiple Friend nodes nearby, it selects the most suitable Friend node through algorithm. + * - Friend Feature + - The ability to help a node supporting the Low Power feature to operate by storing messages destined for those nodes. + - By enabling friend feature, the node can help to store information for Low Power node. The nodes enabled with friend feature may cause more power and memory consumption. + * - Relay Feature + - The ability to receive and retransmit mesh messages over the advertising bearer to enable larger networks. + - The relay feature enables ESP-BLE-MESH messages to hop among nodes for multiple times, and the transmission distance can exceed the range of direct radio transmission between two nodes, thereby covering the entire network. When a node is enabled with the relay feature to relay messages, it only relays the messages of its own subnet, and does not relay the messages of other subnets. The data integrity will not be considered when the node enabled with relay feature relays segmented messages. The node would relay every segmented message once it receives one rather than waiting for the complete message. + * - Proxy Feature + - The ability to receive and retransmit mesh messages between GATT and advertising bearers. + - The purpose of the proxy feature is to allow nodes without an advertising bearer to access the ESP-BLE-MESH network. The proxy feature is typically used in nodes that need to connect to mobile apps. + + +.. _ble-mesh-terminology-provisioning: + +.. list-table:: Table 4 ESP-BLE-MESH Terminology - Provisioning + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - PB-ADV + - PB-ADV is a provisioning bearer used to provision a device using Generic Provisioning PDUs over the advertising channels. + - PB-ADV transfers packets generated during the provisioning process over the advertising channels. This way can only be used for provisioning when provisioner and unprovisioned device both support PB-ADV. + * - PB-GATT + - PB-GATT is a provisioning bearer used to provision a device using Proxy PDUs to encapsulate Provisioning PDUs within the Mesh Provisioning Service. + - PB-GATT uses connection channels to transfer packets generated during the provisioning process. If an unprovisioned device wants to be provisioned through this method, it needs to implement the related Mesh Provisioning Service. Unprovisioned devices which don't implement such service cannot be provisioned into mesh network through PB-GATT bearer. + * - Provisioning + - Provisioning is a process of adding an unprovisioned device to a mesh network, managed by a Provisioner. + - The process of provisioning turns the "unprovisioned device" into a "node", making it a member of the ESP-BLE-MESH network. + * - Authentication Method + - Authentication is a step during the provisioning of nodes. + - There are four authentication methods for unprovisioned devices: Output OOB, Input OOB, Static OOB, and No OOB. + * - Input OOB + - Input Out-of-Band + - For example, a Provisioner generates and displays a random number, and then prompts users to take appropriate actions to input the random number into the unprovisioned device. Taking lighting switch as an example, users can press the button for several times in a certain period of time to input the random number displayed on the Provisioner. Authentication method of the Input OOB is similar to that of Output OOB, but the role of the device is reversed. + * - Output OOB + - Output Out-of-Band + - For example, an unprovisioned device will choose a random number and output the number in a way that is compatible with its functionality. If the unprovisioned device is a bulb, it can flash a specified number of times. If the unprovisioned device has an LCD screen, the random number can display as a multi-digit value. Users who start provisioning should input the observed number to authenticate the unprovisioned device. + * - Static OOB + - Static Out-of-Band + - Authentication method of Static OOB: use Static OOB information. Use 0 as Static OOB information if No OOB information is needed. Use Static OOB information to authenticate devices which are going through provisioning if OOB information is needed. + * - No OOB + - No Out-of-Band + - Authentication method of No OOB: Set the value of the Static OOB field to 0. Using this way is like not authenticating the unprovisioned devices. + + +.. _ble-mesh-terminology-address: + +.. list-table:: Table 5 ESP-BLE-MESH Terminology - Address + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Unassigned Address + - This is a special address type, with a value of 0x0000. Its use indicates that an Element has not yet been configured or had a Unicast Address assigned to it. + - The addresses owned by elements which has not been configured yet or no address has been allocated are unassigned addresses. These elements will not be used for messages transfer because they have no fixed address. Unassigned address is recommended to set as the value of the address before setting the address of user code. + * - Unicast Address + - A unicast address is a unique address allocated to each element. + - During provisioning, the Provisioner will assign a unicast address to each element of node within the life cycle of the nodes in the network. A unicast address may appear in the source/destination address field of a message. Messages sent to a unicast address can only be processed by the element that owns the unicast address. + * - Virtual Address + - A virtual address represents a set of destination addresses. Each virtual address logically represents a Label UUID, which is a 128-bit value that does not have to be managed centrally. + - Associated with specific UUID labels, a virtual address may serve as the publishing or subscription address of the model. A UUID label is a 128-bit value associated with elements of one or more nodes. For virtual addresses, the 15th and 14th bits are set to 1 and 0 respectively; bits from 13th to 0 are set to hash values (providing 16384 hash values). The hash is a derivation of the Label UUID. To use subscribing elements to check the full 128-bit UUID is very inefficient while hash values provide a more efficient way to determine which elements that which messages are finally sent to. + * - Group Address + - A group address is an address that is programmed into zero or more elements + - Group address is another kind of multicast address in the ESP-BLE-MESH network, which is usually used to group nodes. A message sent to the all-proxies address shall be processed by the primary element of all nodes that have the proxy functionality enabled. A message sent to the all-friends address shall be processed by the primary element of all nodes that have the friend functionality enabled. A message sent to the all-relays address shall be processed by the primary element of all nodes that have the relay functionality enabled. A message sent to the all-nodes address shall be processed by the primary element of all nodes. + + +.. _ble-mesh-terminology-security: + +.. list-table:: Table 6 ESP-BLE-MESH Terminology - Security + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Device Key (DevKey) + - There is also a device key, which is a special application key that is unique to each node, is known only to the node and a Configuration Client, and is used to secure communications between the node and a Configuration Client. + - The device key enables you to provision the devices, configure the nodes. The device key is used to encrypt Configuration Messages, i.e. the message transferred between the Provisioner and the node when the device is configured. + * - Application Key (AppKey) + - Application keys are used to secure communications at the upper transport layer. + - Application key is used for decryption of application data before delivering application data to application layer and encryption of them during the delivery of application layer. Some nodes in the network have a specific purpose and can restrict access to potentially sensitive data based on the needs of the application. With specific application keys, these nodes are associated with specific applications. Generally speaking, the fields using different application keys include security (access control of buildings, machine rooms and CEO offices), lighting (plant, exterior building and sidewalks) and HVAC systems. Application keys are bound to Network keys. This means application keys are only used in a context of a Network key they are bound to. An application key shall only be bound to a single Network key. + * - Master Security Material + - The master security material is derived from the network key (NetKey) and can be used by other nodes in the same network. Messages encrypted with master security material can be decoded by any node in the same network. + - The corresponding friendship messages encrypted with the friendship security material: 1. Friend Poll, 2. Friend Update, 3. Friend Subscription List, add/delete/confirm, 4. The Stored Messages" sent by friend nodes to Low Power node. The corresponding friendship messages encrypted with the master security material: 1. Friend Clear, 2. Friend Clear Confirm. Based on the setup of the applications, the messages sent from the Low Power node to the friend nodes will be encrypted with the friendship security material or master security material, with the former being used by the messages transmitted between Low Power node and friend nodes and the latter being used by other network messages. + + +.. _ble-mesh-terminology-message: + +.. list-table:: Table 7 ESP-BLE-MESH Terminology - Message + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Reassembly / Segmentation + - Segmentation and reassembly (SAR) is a method of communication network, which is divided into small units before transmitting packets and reassembled in a proper order at the communication receiving end. + - The lower transport layer will automatically segment the message whose size is too big. The receiving end will return a response message, and the transmitting end will send the data packet again that the receiving end does not receive according to the response message. This is automatically completed by the lower transport layer. Unsegmented messages have at most 15 bytes, of which 4 bytes are transMIC, so the remaining is 11 bytes; in the case of segmentation, there are 12 valid bytes in the first several packets, and 8 in the last one. Special case: A shorter packet requires mandatory segmentation from lower transport layer, in which case the valid byte is 8 bytes. + * - Unacknowledged / Acknowledged + - There are two types of messages: Unacknowledged or Acknowledged + - Based on the whether or not the receiving end needs to send the response message, the messages sent are divided into two kinds. The sending end should set the maximum number of retransmission. + + +.. _ble-mesh-terminology-foundation-models: + +.. list-table:: Table 8 ESP-BLE-MESH Terminology - Foundation Models + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Configuration Server Model + - This model is used to represent a mesh network configuration of a device. + - The node must contain the Configuration Server Model, which is responsible for maintaining configuration-related states. The states that Configuration Server Model maintains include: NetKey List, AppKey List, Model to AppKey List, Node Identity, Key Refresh Phase, Heartbeat Publish, Heartbeat Subscription, Network Transmit, Relay Retransmit etc. + * - Configuration Client Model + - The model is used to represent an element that can control and monitor the configuration of a node. + - The Configuration Client Model uses messages to control the state maintained by the Configuration Server Model. The Provisioner must contain the Configuration Client Model, with which the configuration messages, like Configuration Composition Data Get can be sent. + * - Health Server Model + - This model is used to represent a mesh network diagnostics of a device. + - The Health Server Model is primarily used by devices to check their states and see if there is an error. The states maintained by Health Server model include: Current Fault, Registered Fault, Health Period, and Attention Timer. + * - Health Client Model + - The model is used to represent an element that can control and monitor the health of a node. + - The Health Client Model uses messages to control the state maintained by the Health Server Model. The model can get the self-test information of other nodes through the message "Health Fault Get". + + +.. _ble-mesh-terminology-network-management: + +.. list-table:: Table 9 ESP-BLE-MESH Terminology - Network Management + :widths: 10 40 60 + :header-rows: 1 + + * - Term + - Official Definition + - Detailed Explanation + * - Key Refresh procedure + - This procedure is used when the security of one or more network keys and/or one or more of the application keys has been compromised or could be compromised. + - Key Refresh Procedure is used to update network key and application key of ESP-BLE-MESH network. Key Refresh Procedure is used when the security of one or more network keys and/or one or more application keys is threatened or potentially threatened. Keys are usually updated after some nodes in the network are removed. + * - IV (Initialisation Vector) Update Procedure + - A node can also use an IV Update procedure to signal to peer nodes that it is updating the IV Index. + - The IV Update procedure is used to update the value of ESP-BLE-MESH network's IV Index. This value is related to the random number required for message encryption. To ensure that the value of the random number is not repeated, this value is periodically incremented. IV Index is a 32-bit value and a shared network resource. For example, all nodes in a mesh network share the same IV Index value. Starting from 0x00000000, the IV Index increments during the IV Update procedure and maintained by a specific process, ensuring the IV Index shared in the mesh network is the same. This can be done when the node believes that it has the risk of exhausting its sequence number, or when it determines that another node is nearly exhausting its sequence number. Note: The update time must not be less than 96 hours. It can be triggered when a secure network beacon is received, or when the node determines that its sequence number is greater than a certain value. + +For more terms, please see: `ESP-BLE-MESH Glossary of Terms `_. + + + diff --git a/docs/en/api-guides/index.rst b/docs/en/api-guides/index.rst index 17be4f178..0ef7e2ec3 100644 --- a/docs/en/api-guides/index.rst +++ b/docs/en/api-guides/index.rst @@ -30,6 +30,7 @@ API Guides RF Calibration WiFi Driver ESP-MESH + ESP-BLE-MESH BluFi External SPI-connected RAM Linker Script Generation diff --git a/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst b/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst index 3911191d7..606d758db 100644 --- a/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst +++ b/docs/en/api-guides/jtag-debugging/tips-and-quirks.rst @@ -234,6 +234,34 @@ Below is an excerpt from series of errors reported by GDB after the application cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an exception! cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an overrun! +.. _jtag-debugging-security-features: + +JTAG with Flash Encryption or Secure Boot +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, enabling Flash Encryption and/or Secure Boot will disable JTAG debugging. On first boot, the bootloader will burn an eFuse bit to permanently disable JTAG at the same time it enables the other features. + +The project configuration option :ref:`CONFIG_SECURE_BOOT_ALLOW_JTAG` will keep JTAG enabled at this time, removing all physical security but allowing debugging. (Although the name suggests Secure Boot, this option can be applied even when only Flash Encryption is enabled). + +However, OpenOCD may attempt to automatically read and write the flash in order to set :ref:`software breakpoints `. This has two problems: + +- Software breakpoints are incompatible with Flash Encryption, OpenOCD currently has no support for encrypting or decrypting flash contents. +- If Secure Boot is enabled, setting a software breakpoint will change the digest of a signed app and make the signature invalid. This means if a software breakpoint is set and then a reset occurs, the signature verification will fail on boot. + +To disable software breakpoints while using JTAG, add an extra argument ``-c 'set ESP_FLASH_SIZE 0'`` to the start of the OpenOCD command line. For example:: + + openocd -c 'set ESP_FLASH_SIZE 0' -f board/esp32-wrover-kit-3.3v.cfg + +.. note:: + + For the same reason, the ESP-IDF app may fail bootloader verification of app signatures, when this option is enabled and a software breakpoint is set. + +JTAG and ESP32-WROOM-32 AT firmware Compatibility Issue +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The ESP32-WROOM series of modules come pre-flashed with AT firmware. This firmware configures the pins GPIO12 to GPIO15 as SPI slave interface, which makes using JTAG impossible. + +To make JTAG available, build new firmware that is not using pins GPIO12 to GPIO15 dedicated to JTAG communication. After that, flash the firmware onto your module. See also :ref:`jtag-debugging-tip-jtag-pins-reconfigured`. .. _jtag-debugging-tip-reporting-issues: diff --git a/docs/en/api-guides/partition-tables.rst b/docs/en/api-guides/partition-tables.rst index 502c7118c..f538a507b 100644 --- a/docs/en/api-guides/partition-tables.rst +++ b/docs/en/api-guides/partition-tables.rst @@ -92,7 +92,7 @@ The 8-bit subtype field is specific to a given partition type. esp-idf currently - OTA never updates the factory partition. - If you want to conserve flash usage in an OTA project, you can remove the factory partition and use ota_0 instead. - ota_0 (0x10) ... ota_15 (0x1F) are the OTA app slots. Refer to the :doc:`OTA documentation <../api-reference/system/ota>` for more details, which then use the OTA data partition to configure which app slot the bootloader should boot. If using OTA, an application should have at least two OTA application slots (ota_0 & ota_1). Refer to the :doc:`OTA documentation <../api-reference/system/ota>` for more details. - - test (0x2) is a reserved subtype for factory test procedures. It is not currently supported by the esp-idf bootloader. + - test (0x20) is a reserved subtype for factory test procedures. It will be used as the fallback boot partition if no other valid app partition is found. It is also possible to configure the bootloader to read a GPIO input during each boot, and boot this partition if the GPIO is held low, see :ref:`bootloader_boot_from_test_firmware`. * When type is "data", the subtype field can be specified as ota (0), phy (1), nvs (2), or nvs_keys (4). diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index d25ba605b..5819ac913 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -191,10 +191,6 @@ Another thing deserves our attention is that the default behavior of LwIP is to In above scenario, ideally, the application sockets and the network layer should not be affected, since the Wi-Fi connection only fails temporarily and recovers very quickly. The application can enable "Keep TCP connections when IP changed" via LwIP menuconfig. -SYSTEM_EVENT_STA_AUTHMODE_CHANGE -++++++++++++++++++++++++++++++++++++ -This event arises when the AP to which the station is connected changes its authentication mode, e.g., from no auth to WPA. Upon receiving this event, the event task will do nothing. Generally, the application event callback does not need to handle this either. - SYSTEM_EVENT_STA_GOT_IP ++++++++++++++++++++++++++++++++++++ This event arises when the DHCP client successfully gets the IPV4 address from the DHCP server, or when the IPV4 address is changed. The event means that everything is ready and the application can begin its tasks (e.g., creating sockets). @@ -1019,6 +1015,17 @@ The table below shows the reason-code defined in ESP32. The first column is the | | | | WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT. | | | | | | +---------------------------+-------+---------+-------------------------------------------------------------+ +| CONNECTION_FAIL | 205 |reserved | Espressif-specific Wi-Fi reason-code: the | +| | | | connection to the AP has failed. | +| | | | | ++---------------------------+-------+---------+-------------------------------------------------------------+ +| AUTH_CHANGED | 206 |reserved | Espressif-specific Wi-Fi reason-code: the | +| | | | disconnection has happened since AP has changed the | +| | | | authmode. | +| | | | | ++---------------------------+-------+---------+-------------------------------------------------------------+ + + ESP32 Wi-Fi Station Connecting When Multiple APs Are Found --------------------------------------------------------------- @@ -1148,6 +1155,8 @@ API esp_wifi_set_config() can be used to configure the station. The table below | | threshold is open. | +------------------+--------------------------------------------------------------+ +.. attention:: + WEP/WPA security modes are deprecated in IEEE802.11-2016 specifications and are recommended not to be used. These modes c an be rejected using authmode threshold by setting threshold as WPA2 by threshold.authmode as WIFI_AUTH_WPA2_PSK. AP Basic Configuration +++++++++++++++++++++++++++++++++++++ diff --git a/docs/en/api-reference/bluetooth/esp-ble-mesh.rst b/docs/en/api-reference/bluetooth/esp-ble-mesh.rst new file mode 100644 index 000000000..a10c79fe2 --- /dev/null +++ b/docs/en/api-reference/bluetooth/esp-ble-mesh.rst @@ -0,0 +1,154 @@ +ESP-BLE-MESH +============ + +With various features of ESP-BLE-MESH, users can create a managed flooding mesh network for several +scenarios, such as lighting, sensor and etc. + +For an ESP32 to join and work on a ESP-BLE-MESH network, it must be provisioned firstly. By provisioning, +the ESP32, as an unprovisioned device, will join the ESP-BLE-MESH network and become a ESP-BLE-MESH node, +communicating with other nodes within or beyond the radio range. + +Apart from ESP-BLE-MESH nodes, inside ESP-BLE-MESH network, there is also ESP32 that works as ESP-BLE-MESH +Provisioner, which could provision unprovisioned devices into ESP-BLE-MESH nodes and configure the nodes +with various features. + +For information how to start using ESP32 and ESP-BLE-MESH, please see the Section :ref:`getting-started-with-ble-mesh`. If you are interested in information on ESP-BLE-MESH architecture, including some details of software implementation, please see Section :doc:`../../api-guides/esp-ble-mesh/ble-mesh-architecture`. + + +Application Examples and Demos +------------------------------ + +Please refer to Sections :ref:`esp-ble-mesh-examples` and :ref:`esp-ble-mesh-demo-videos`. + + +API Reference +------------- + +ESP-BLE-MESH APIs are divided into the following parts: + +* `ESP-BLE-MESH Definitions`_ +* `ESP-BLE-MESH Core API Reference`_ +* `ESP-BLE-MESH Models API Reference`_ + + +ESP-BLE-MESH Definitions +------------------------ + +This section contains only one header file, which lists the following items of ESP-BLE-MESH. + +* ID of all the models and related message opcodes +* Structs of model, element and Composition Data +* Structs of used by ESP-BLE-MESH Node/Provisioner for provisioning +* Structs used to transmit/receive messages +* Event types and related event parameters + +.. include:: /_build/inc/esp_ble_mesh_defs.inc + + +ESP-BLE-MESH Core API Reference +------------------------------- + +This section contains ESP-BLE-MESH Core related APIs, which can be used to initialize ESP-BLE-MESH +stack, provision, send/publish messages, etc. + +This API reference covers six components: + +* `ESP-BLE-MESH Stack Initialization`_ +* `Reading of Local Data Information`_ +* `Low Power Operation (Updating)`_ +* `Send/Publish Messages, add Local AppKey, etc.`_ +* `ESP-BLE-MESH Node/Provisioner Provisioning`_ +* `ESP-BLE-MESH GATT Proxy Server`_ + + +ESP-BLE-MESH Stack Initialization +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_common_api.inc + + +Reading of Local Data Information +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_local_data_operation_api.inc + + +Low Power Operation (Updating) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_low_power_api.inc + + +Send/Publish Messages, add Local AppKey, etc. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_networking_api.inc + + +ESP-BLE-MESH Node/Provisioner Provisioning +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_provisioning_api.inc + + +ESP-BLE-MESH GATT Proxy Server +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_proxy_api.inc + + +ESP-BLE-MESH Models API Reference +--------------------------------- + +This section contains ESP-BLE-MESH Model related APIs, event types, event parameters, etc. + +There are six categories of models: + +* `Configuration Client/Server Models`_ +* `Health Client/Server Models`_ +* `Generic Client/Server Models`_ +* `Sensor Client/Server Models`_ +* `Time and Scenes Client/Server Models`_ +* `Lighting Client/Server Models`_ + + +.. note:: + + Definitions related to Server Models are being updated, and will be released soon. + + +Configuration Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_config_model_api.inc + + +Health Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_generic_model_api.inc + + +Generic Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_health_model_api.inc + + +Sensor Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_lighting_model_api.inc + + +Time and Scenes Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_sensor_model_api.inc + + +Lighting Client/Server Models +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. include:: /_build/inc/esp_ble_mesh_time_scene_model_api.inc + diff --git a/docs/en/api-reference/bluetooth/index.rst b/docs/en/api-reference/bluetooth/index.rst index af7d3cfca..7056d5810 100644 --- a/docs/en/api-reference/bluetooth/index.rst +++ b/docs/en/api-reference/bluetooth/index.rst @@ -8,7 +8,12 @@ Bluetooth API Bluetooth Common Bluetooth LE Bluetooth Classic + NimBLE + ESP-BLE-MESH +ESP-IDF currently supports two host stacks. The Bluedroid based stack (default) supports classic Bluetooth as well as BLE. On the other hand, Apache NimBLE based stack is BLE only. For users to make a +* For usecases involving classic Bluetooth as well as BLE, Bluedroid should be used. +* For BLE-only usecases, using NimBLE is recommended. It is less demanding in terms of code footprint and runtime memory, making it suitable for such scenarios. To see the overview of the ESP32 Bluetooth stack architecture, follow links below: diff --git a/docs/en/api-reference/bluetooth/nimble/index.rst b/docs/en/api-reference/bluetooth/nimble/index.rst new file mode 100644 index 000000000..9452d9e58 --- /dev/null +++ b/docs/en/api-reference/bluetooth/nimble/index.rst @@ -0,0 +1,44 @@ +NimBLE-based host APIs +********************** +Overview +======== + +Apache MyNewt NimBLE is a highly configurable and BT SIG qualifiable BLE stack providing both host and controller functionalities. ESP-IDF supports NimBLE host stack which is specifically ported for ESP32 platform and FreeRTOS. The underlying controller is still the same (as in case of Bluedroid) providing VHCI interface. Refer to `NimBLE user guide `_ for a complete list of features and additional information on NimBLE stack. Most features of NimBLE including BLE Mesh are supported by ESP-IDF. The porting layer is kept cleaner by maintaining all the existing APIs of NimBLE along with a single ESP-NimBLE API for initialization, making it simpler for the application developers. + +Architecture +============ + +Currently, NimBLE host and controller support different transports such UART and RAM between them. However, RAM transport cannot be used as is in case of ESP as ESP controller supports VHCI interface and buffering schemes used by NimBLE host is incompatible with that used by ESP controller. Therefore, a new transport between NimBLE host and ESP controller has been added. This is depicted in the figure below. This layer is responsible for maintaining pool of transport buffers and formatting buffers exchanges between host and controller as per the requirements. + +.. figure:: ../../../../_static/esp32_nimble_stack.png + :align: center + :alt: ESP NimBLE Stack. + :scale: 50 + + ESP NimBLE Stack + + +Threading Model +=============== + +The NimBLE host can run inside the application thread or can have its own independent thread. This flexibility is inherently provided by NimBLE design. By default, a thread is spawned by the porting function ``nimble_port_freertos_init``. This behavior can be changed by overriding the same function. For BLE Mesh, additional thread (advertising thread) is used which keeps on feeding advertisement events to the main thread. + +Programming Sequence +==================== + +To begin with, make sure that the NimBLE stack is enabled from menuconfig :ref:`Enable NimBLE host stack`, for this option to be visible please disable :ref:`Bluedroid Enable`. + +Typical programming sequence with NimBLE stack consists of the following steps: + * Initialize NVS flash using :cpp:func:`nvs_flash_init` API. This is because ESP controller uses NVS during initialization. + * Call :cpp:func:`esp_nimble_hci_and_controller_init` to initialize ESP controller as well as transport layer. This will also link the host and controller modules together. Alternatively, if ESP controller is already initialized, then ``esp_nimble_hci_init`` can be called for the remaining initialization. + * Initialize the host stack using ``nimble_port_init``. + * Initialize the required NimBLE host configuration parameters and callbacks + * Perform application specific tasks/initialization + * Run the thread for host stack using ``nimble_port_freertos_init`` + +This documentation does not cover NimBLE APIs. Refer to `NimBLE tutorial `_ for more details on the programming sequence/NimBLE APIs for different scenarios. + +API Reference +============= + +.. include:: /_build/inc/esp_nimble_hci.inc diff --git a/docs/en/api-reference/peripherals/can.rst b/docs/en/api-reference/peripherals/can.rst index d24cbd7f6..ce190de6a 100644 --- a/docs/en/api-reference/peripherals/can.rst +++ b/docs/en/api-reference/peripherals/can.rst @@ -168,7 +168,7 @@ The operating bit rate of the CAN controller is configured using the :cpp:type:` 2. **Timing Segment 1** consists of 1 to 16 time quanta before sample point 3. **Timing Segment 2** consists of 1 to 8 time quanta after sample point -The **Baudrate Prescaler** is used to determine the period of each time quanta by dividing the CAN controller's source clock (80 MHz APB clock). The ``brp`` can be **any even number from 2 to 128**. +The **Baudrate Prescaler** is used to determine the period of each time quanta by dividing the CAN controller's source clock (80 MHz APB clock). The ``brp`` can be **any even number from 2 to 128**. If the ESP32 is a revision 2 or later chip, the ``brp`` will also support **any multiple of 4 from 132 to 256**, and can be enabled by setting the :ref:`CONFIG_ESP32_REV_MIN` to revision 2 or higher. .. packetdiag:: ../../../_static/diagrams/can/can_bit_timing.diag :caption: Bit timing configuration for 500kbit/s given BRP = 8 @@ -183,6 +183,9 @@ The **Synchronization Jump Width** is used to determined the maximum number of t Bit timing **macro initializers** are also available for commonly used CAN bus bit rates. The following macro initializers are provided by the CAN driver. + - ``CAN_TIMING_CONFIG_12_5KBITS()`` + - ``CAN_TIMING_CONFIG_16KBITS()`` + - ``CAN_TIMING_CONFIG_20KBITS()`` - ``CAN_TIMING_CONFIG_25KBITS()`` - ``CAN_TIMING_CONFIG_50KBITS()`` - ``CAN_TIMING_CONFIG_100KBITS()`` @@ -192,6 +195,10 @@ Bit timing **macro initializers** are also available for commonly used CAN bus b - ``CAN_TIMING_CONFIG_800KBITS()`` - ``CAN_TIMING_CONFIG_1MBITS()`` +.. note:: + The macro initializers for 12.5K, 16K, and 20K bit rates are only available + for ESP32 revision 2 or later. + Acceptance Filter ^^^^^^^^^^^^^^^^^ diff --git a/docs/en/api-reference/peripherals/sdio_slave.rst b/docs/en/api-reference/peripherals/sdio_slave.rst index 163cf1168..3c3031ed7 100644 --- a/docs/en/api-reference/peripherals/sdio_slave.rst +++ b/docs/en/api-reference/peripherals/sdio_slave.rst @@ -181,7 +181,7 @@ set in the ``send_queue_size``. All the buffers are restricted to be no larger t mode several buffers can be sent in one transfer, each buffer is still counted as one in the queue. The application can call ``sdio_slave_transmit`` to send packets. In this case the function returns when the transfer -is sucessfully done, so the queue is not fully used. When higher effeciency is required, the application can use the +is successfully done, so the queue is not fully used. When higher effeciency is required, the application can use the following functions instead: 1. Pass buffer information (address, length, as well as an ``arg`` indicating the buffer) to ``sdio_slave_send_queue``. diff --git a/docs/en/api-reference/protocols/esp_websocket_client.rst b/docs/en/api-reference/protocols/esp_websocket_client.rst new file mode 100644 index 000000000..cd4db2413 --- /dev/null +++ b/docs/en/api-reference/protocols/esp_websocket_client.rst @@ -0,0 +1,70 @@ +ESP WebSocket Client +==================== + +Overview +-------- +The ESP WebSocket client is an implementation of `WebSocket protocol client `_ for ESP32 + +Features +-------- + * supports WebSocket over TCP, SSL with mbedtls + * Easy to setup with URI + * Multiple instances (Multiple clients in one application) + +Configuration +------------- +URI +^^^ + +- Supports ``ws``, ``wss`` schemes +- WebSocket samples: + + - ``ws://websocket.org``: WebSocket over TCP, default port 80 + - ``wss://websocket.org``: WebSocket over SSL, default port 443 + +- Minimal configurations: + +.. code:: c + + const esp_websocket_client_config_t ws_cfg = { + .uri = "ws://websocket.org", + }; + +- If there are any options related to the URI in + ``esp_websocket_client_config_t``, the option defined by the URI will be + overridden. Sample: + +.. code:: c + + const esp_websocket_client_config_t ws_cfg = { + .uri = "ws://websocket.org:123", + .port = 4567, + }; + //WebSocket client will connect to websocket.org using port 4567 + +SSL +^^^ + +- Get certificate from server, example: ``websocket.org`` + ``openssl s_client -showcerts -connect websocket.org:443 /dev/null|openssl x509 -outform PEM >websocket_org.pem`` +- Configuration: + +.. code:: cpp + + const esp_websocket_client_config_t ws_cfg = { + .uri = "wss://websocket.org", + .cert_pem = (const char *)websocket_org_pem_start, + }; + +For more options on ``esp_websocket_client_config_t``, please refer to API reference below + +Application Example +------------------- +Simple WebSocket example that uses esp_websocket_client to establish a websocket connection and send/receive data with the `websocket.org `_ Server: :example:`protocols/websocket`. + + +API Reference +------------- + +.. include:: /_build/inc/esp_websocket_client.inc + diff --git a/docs/en/api-reference/protocols/index.rst b/docs/en/api-reference/protocols/index.rst index 8c3637d9b..79a28f85d 100644 --- a/docs/en/api-reference/protocols/index.rst +++ b/docs/en/api-reference/protocols/index.rst @@ -12,6 +12,7 @@ Application Protocols ASIO ESP-MQTT Modbus slave + Websocket Client Example code for this API section is provided in :example:`protocols` directory of ESP-IDF examples. diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 3fa366b0c..7579d7046 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -32,30 +32,30 @@ URI - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: - - ``mqtt://iot.eclipse.org``: MQTT over TCP, default port 1883: - - ``mqtt://iot.eclipse.org:1884`` MQTT over TCP, port 1884: - - ``mqtt://username:password@iot.eclipse.org:1884`` MQTT over TCP, + - ``mqtt://mqtt.eclipse.org``: MQTT over TCP, default port 1883: + - ``mqtt://mqtt.eclipse.org:1884`` MQTT over TCP, port 1884: + - ``mqtt://username:password@mqtt.eclipse.org:1884`` MQTT over TCP, port 1884, with username and password - MQTT over SSL samples: - - ``mqtts://iot.eclipse.org``: MQTT over SSL, port 8883 - - ``mqtts://iot.eclipse.org:8884``: MQTT over SSL, port 8884 + - ``mqtts://mqtt.eclipse.org``: MQTT over SSL, port 8883 + - ``mqtts://mqtt.eclipse.org:8884``: MQTT over SSL, port 8884 - MQTT over Websocket samples: - - ``ws://iot.eclipse.org:80/ws`` + - ``ws://mqtt.eclipse.org:80/mqtt`` - MQTT over Websocket Secure samples: - - ``wss://iot.eclipse.org:443/ws`` + - ``wss://mqtt.eclipse.org:443/mqtt`` - Minimal configurations: .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://iot.eclipse.org", + .uri = "mqtt://mqtt.eclipse.org", .event_handle = mqtt_event_handler, // .user_context = (void *)your_context }; @@ -67,26 +67,26 @@ URI .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://iot.eclipse.org:1234", + .uri = "mqtt://mqtt.eclipse.org:1234", .event_handle = mqtt_event_handler, .port = 4567, }; - //MQTT client will connect to iot.eclipse.org using port 4567 + //MQTT client will connect to mqtt.eclipse.org using port 4567 SSL ^^^ -- Get certificate from server, example: ``iot.eclipse.org`` - ``openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem`` +- Get certificate from server, example: ``mqtt.eclipse.org`` + ``openssl s_client -showcerts -connect mqtt.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` - Check the sample application: ``examples/mqtt_ssl`` - Configuration: .. code:: cpp const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://iot.eclipse.org:8883", + .uri = "mqtts://mqtt.eclipse.org:8883", .event_handle = mqtt_event_handler, - .cert_pem = (const char *)iot_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; For more options on ``esp_mqtt_client_config_t``, please refer to API reference below diff --git a/docs/en/api-reference/provisioning/wifi_provisioning.rst b/docs/en/api-reference/provisioning/wifi_provisioning.rst index 5cec30165..2ae3331ce 100644 --- a/docs/en/api-reference/provisioning/wifi_provisioning.rst +++ b/docs/en/api-reference/provisioning/wifi_provisioning.rst @@ -302,7 +302,7 @@ On the other hand, if device was not able to connect using the provided Wi-Fi cr If this default behavior is not desired, it can be disabled by calling :cpp:func:`wifi_prov_mgr_disable_auto_stop()`. Now the provisioning service will only be stopped after an explicit call to :cpp:func:`wifi_prov_mgr_stop_provisioning()`, which returns immediately after scheduling a task for stopping the service. The service stops after a certain delay and WIFI_PROV_END event gets emitted. This delay is specified by the argument to :cpp:func:`wifi_prov_mgr_disable_auto_stop()`. -The customized behavior is useful for applications which want the provisioning service to be stopped some time after the Wi-Fi connection is successfully established. For example, if the application requires the device to connect to some cloud service and obtain another set of credentials, and exchange this credentials over a custom protocomm endpoint, then after sucessfully doing so stop the provisioning service by calling :cpp:func:`wifi_prov_mgr_stop_provisioning()` inside the protocomm handler itself. The right amount of delay ensures that the transport resources are freed only after the response from the protocomm handler reaches the client side application. +The customized behavior is useful for applications which want the provisioning service to be stopped some time after the Wi-Fi connection is successfully established. For example, if the application requires the device to connect to some cloud service and obtain another set of credentials, and exchange this credentials over a custom protocomm endpoint, then after successfully doing so stop the provisioning service by calling :cpp:func:`wifi_prov_mgr_stop_provisioning()` inside the protocomm handler itself. The right amount of delay ensures that the transport resources are freed only after the response from the protocomm handler reaches the client side application. Application Examples -------------------- diff --git a/docs/en/api-reference/system/esp_event.rst b/docs/en/api-reference/system/esp_event.rst index dd969be70..958b0fe7a 100644 --- a/docs/en/api-reference/system/esp_event.rst +++ b/docs/en/api-reference/system/esp_event.rst @@ -54,7 +54,7 @@ In code, the flow above may look like as follows: esp_event_loop_handle_t loop_handle; - esp_event_loop_create(&loop_args, &loop_handle) + esp_event_loop_create(&loop_args, &loop_handle); // 3. Register event handler defined in (1). MY_EVENT_BASE and MY_EVENT_ID specifies a hypothetical // event that handler run_on_event should execute on when it gets posted to the loop. diff --git a/docs/en/api-reference/system/power_management.rst b/docs/en/api-reference/system/power_management.rst index 9380a8d88..6c7446a1d 100644 --- a/docs/en/api-reference/system/power_management.rst +++ b/docs/en/api-reference/system/power_management.rst @@ -108,6 +108,10 @@ Currently, the following peripheral drivers are aware of DFS and will use ``ESP_ - SPI master +- I2C + +- I2S (If APLL clock is used then it will use ``ESP_PM_NO_LIGHT_SLEEP`` lock) + - SDMMC The following drivers will hold ``ESP_PM_APB_FREQ_MAX`` lock while the driver is enabled: @@ -124,10 +128,6 @@ The following drivers will hold ``ESP_PM_APB_FREQ_MAX`` lock while the driver is The following peripheral drivers are not aware of DFS yet. Applications need to acquire/release locks when necessary: -- I2C - -- I2S - - MCPWM - PCNT diff --git a/docs/en/api-reference/system/system.rst b/docs/en/api-reference/system/system.rst index a15860e8a..e4a8c7af1 100644 --- a/docs/en/api-reference/system/system.rst +++ b/docs/en/api-reference/system/system.rst @@ -104,11 +104,28 @@ Chip version :cpp:func:`esp_chip_info` function fills :cpp:class:`esp_chip_info_t` structure with information about the chip. This includes the chip revision, number of CPU cores, and a bit mask of features enabled in the chip. +.. _idf-version-h: + SDK version ----------- :cpp:func:`esp_get_idf_version` returns a string describing the IDF version which was used to compile the application. This is the same value as the one available through ``IDF_VER`` variable of the build system. The version string generally has the format of ``git describe`` output. +To get the version at build time, additional version macros are provided. They can be used to enable or disable parts of the program depending on IDF version. + +* :c:macro:`ESP_IDF_VERSION_MAJOR`, :c:macro:`ESP_IDF_VERSION_MINOR`, :c:macro:`ESP_IDF_VERSION_PATCH` are defined to integers representing major, minor, and patch version. + +* :c:macro:`ESP_IDF_VERSION_VAL` and :c:macro:`ESP_IDF_VERSION` can be used when implementing version checks: + + .. code-block:: c + + #include "esp_idf_version.h" + + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0) + // enable functionality present in IDF v4.0 + #endif + + App version ----------- Application version is stored in :cpp:class:`esp_app_desc_t` structure. It is located in DROM sector and has a fixed offset from the beginning of the binary file. @@ -125,5 +142,6 @@ API Reference ------------- .. include:: /_build/inc/esp_system.inc +.. include:: /_build/inc/esp_idf_version.inc diff --git a/docs/en/conf.py b/docs/en/conf.py index 918bf8b0f..cb77736dd 100644 --- a/docs/en/conf.py +++ b/docs/en/conf.py @@ -6,14 +6,17 @@ # Importing conf_common adds all the non-language-specific # parts to this conf module -import sys -import os -sys.path.insert(0, os.path.abspath('..')) -from conf_common import * # noqa: F401, F403 - need to make available everything from common +try: + from conf_common import * # noqa: F403,F401 +except ImportError: + import sys + import os + sys.path.insert(0, os.path.abspath('..')) + from conf_common import * # noqa: F403,F401 # General information about the project. project = u'ESP-IDF Programming Guide' -copyright = u'2016 - 2019, Espressif Systems (Shanghai) CO., LTD' +copyright = u'2016 - 2020, Espressif Systems (Shanghai) CO., LTD' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/en/contribute/documenting-code.rst b/docs/en/contribute/documenting-code.rst index 990665dd6..f76f2201c 100644 --- a/docs/en/contribute/documenting-code.rst +++ b/docs/en/contribute/documenting-code.rst @@ -291,9 +291,9 @@ Installation of Doxygen is OS dependent: :: - $ pacman -S mingw32/mingw-w64-i686-python2-pillow + $ pacman -S mingw32/mingw-w64-i686-python-pillow - Check the log on the screen that ``mingw-w64-i686-python2-pillow-4.3.0-1`` is installed. Previous versions of *pillow* will not work. + Check the log on the screen that ``mingw-w64-i686-python-pillow-4.3.0-1`` is installed. Previous versions of *pillow* will not work. A downside of Windows installation is that fonts of the `blockdiag pictures ` do not render correctly, you will see some random characters instead. Until this issue is fixed, you can use the `interactive shell`_ to see how the complete picture looks like. diff --git a/docs/en/get-started-cmake/linux-setup-scratch.rst b/docs/en/get-started-cmake/linux-setup-scratch.rst index e36f9c983..53c327388 100644 --- a/docs/en/get-started-cmake/linux-setup-scratch.rst +++ b/docs/en/get-started-cmake/linux-setup-scratch.rst @@ -19,7 +19,7 @@ To compile with ESP-IDF you need to get the following packages: - Ubuntu and Debian:: - sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache + sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache libffi-dev libssl-dev - Arch:: diff --git a/docs/en/get-started-cmake/linux-setup.rst b/docs/en/get-started-cmake/linux-setup.rst index 8418378b2..e73aa4627 100644 --- a/docs/en/get-started-cmake/linux-setup.rst +++ b/docs/en/get-started-cmake/linux-setup.rst @@ -17,7 +17,7 @@ To compile with ESP-IDF you need to get the following packages: - Ubuntu and Debian:: - sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache + sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache libffi-dev libssl-dev - Arch:: diff --git a/docs/en/get-started/linux-setup-scratch.rst b/docs/en/get-started/linux-setup-scratch.rst index 71028e64d..1b6d5f9ef 100644 --- a/docs/en/get-started/linux-setup-scratch.rst +++ b/docs/en/get-started/linux-setup-scratch.rst @@ -14,7 +14,7 @@ To compile with ESP-IDF you need to get the following packages: - Ubuntu and Debian:: - sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing + sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing libffi-dev libssl-dev - Arch:: diff --git a/docs/en/get-started/linux-setup.rst b/docs/en/get-started/linux-setup.rst index 564fa63fa..25d5c1f03 100644 --- a/docs/en/get-started/linux-setup.rst +++ b/docs/en/get-started/linux-setup.rst @@ -14,7 +14,7 @@ To compile with ESP-IDF you need to get the following packages: - Ubuntu and Debian:: - sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing + sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing libffi-dev libssl-dev - Arch:: diff --git a/docs/en/get-started/windows-setup.rst b/docs/en/get-started/windows-setup.rst index ad7d82101..ee2dcea11 100644 --- a/docs/en/get-started/windows-setup.rst +++ b/docs/en/get-started/windows-setup.rst @@ -14,10 +14,14 @@ Toolchain Setup The quick setup is to download the Windows all-in-one toolchain & MSYS2 zip file from dl.espressif.com: -https://dl.espressif.com/dl/esp32_win32_msys2_environment_and_toolchain-20181001.zip +https://dl.espressif.com/dl/esp32_win32_msys2_environment_and_toolchain_idf3-20200601.zip Unzip the zip file to ``C:\`` (or some other location, but this guide assumes ``C:\``) and it will create an ``msys32`` directory with a pre-prepared environment. +.. important:: + + If another toolchain location is used (different than the default ``C:\msys32``), please ensure that the path where the all-in-one toolchain gets unzipped is a plain ASCII, contains no spaces, symlinks or accents. + Check it Out ============ diff --git a/docs/en/index.rst b/docs/en/index.rst index b6a852468..b9f7cb042 100644 --- a/docs/en/index.rst +++ b/docs/en/index.rst @@ -14,22 +14,22 @@ This is the documentation for Espressif IoT Development Framework (`esp-idf ` feature, and you can use flash encryption without enabling secure boot. However, for a secure environment both should be used simultaneously. In absence of secure boot, additional configuration needs to be performed to ensure effectiveness of flash encryption. See :ref:`flash-encryption-without-secure-boot` for more details. +Flash Encryption is separate from the :doc:`Secure Boot ` feature, and you can use flash encryption without enabling secure boot. However, **for a secure environment both should be used simultaneously**. + +When using any non-default configuration in production, additional steps may also be needed to ensure effectiveness of flash encryption. See :ref:`securing-flash-encryption` for more details. .. important:: - Enabling flash encryption limits your options for further updates of your ESP32. Make sure to read this document (including :ref:`flash-encryption-limitations`) and understand the implications of enabling flash encryption. + Enabling flash encryption limits your options for further updates of your ESP32. Make sure to read this document (including :ref:`flash-encryption-limitations`) and understand the implications of enabling flash encryption. + +.. note:: + Flash encryption is only supported when using the default GNU Make build system. The CMake build system preview in ESP-IDF v3.x does not support flash encryption. Background ---------- @@ -37,6 +42,23 @@ Background - If flash encryption may be enabled, the programmer must take certain precautions when writing code that :ref:`uses encrypted flash `. +.. _storing-encrypted-data: + +Storing Encrypted Data +---------------------- + +Aside from encrypting the firmware binary, the app may need to store some sensitive data in an encrypted form. For example, in a filesystem or NVS data partition. + +The recommended way to do this is to use :ref:`nvs_encryption` . + +Alternatively, it is possible to use the :doc:`Wear Levelling feature ` with an encrypted partition, if the "encrypted" flag is set on the partition. This allows, for example, a VFAT partition to be stored encrypted in flash. + +The following are **not suitable** and will store data where an attacker with physical access can read it out: + +- Custom efuse fields (these can be write protected against modification but not read protected if the app needs to read them) +- SPIFFS (SPIFFS is optimized for the read and write behavior of NOR flash, so it's not possible to encrypt this filesystem) + + .. _flash-encryption-initialisation: Flash Encryption Initialisation @@ -172,15 +194,7 @@ Serial Re-Flashing Procedure - Reset the device and it will re-encrypt plaintext partitions, then burn the :ref:`FLASH_CRYPT_CNT` again to re-enable encryption. - -Disabling Serial Updates -~~~~~~~~~~~~~~~~~~~~~~~~ - -To prevent further plaintext updates via serial, use espefuse.py to write protect the :ref:`FLASH_CRYPT_CNT` after flash encryption has been enabled (ie after first boot is complete):: - - espefuse.py --port PORT write_protect_efuse FLASH_CRYPT_CNT - -This prevents any further modifications to disable or re-enable flash encryption. +To prevent any further serial updates, see :ref:`securing-flash-encryption`. .. _pregenerated-flash-encryption-key: @@ -269,7 +283,7 @@ Limitations of Flash Encryption Flash Encryption prevents plaintext readout of the encrypted flash, to protect firmware against unauthorised readout and modification. It is important to understand the limitations of the flash encryption system: -- Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behaviour). If generating keys off-device (see :ref:`pregenerated-flash-encryption-key`), ensure proper procedure is followed. +- Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behavior). If generating keys off-device (see :ref:`pregenerated-flash-encryption-key`), ensure proper procedure is followed. - Not all data is stored encrypted. If storing data on flash, check if the method you are using (library, API, etc.) supports flash encryption. @@ -291,20 +305,29 @@ It is recommended to use flash encryption and secure boot together. However, if - :ref:`pregenerated-flash-encryption-key` is still possible, provided the bootloader is not reflashed. Reflashing the bootloader requires the same :ref:`Reflashable ` option to be enabled in the Secure Boot config. .. _flash-encryption-without-secure-boot: +.. _securing-flash-encryption: -Using Flash Encryption without Secure Boot ------------------------------------------- +Securing Flash Encryption +------------------------- -If flash encryption is used without secure boot, it is possible to load unauthorised code using serial re-flashing. See :ref:`updating-encrypted-flash-serial` for details. This unauthorised code can then read all encrypted partitions (in decrypted form) making flash-encryption ineffective. This can be avoided by write-protecting :ref:`FLASH_CRYPT_CNT` and thereby disallowing serial re-flashing. :ref:`FLASH_CRYPT_CNT` can be write-protected using command:: +In a production setting it's important to ensure that flash encryption cannot be temporarily disabled. + +This is because if the :doc:`secure-boot` feature is not enabled, or if Secure Boot is somehow bypassed by an attacker, then unauthorised code can be written to flash in plaintext. This code can then re-enable encryption and access encrypted data, making flash encryption ineffective. + +This problem must be avoided by write-protecting :ref:`FLASH_CRYPT_CNT` and thereby keeping flash encryption permanently enabled. + +The simplest way to do this is to enable the configuration option :ref:`CONFIG_FLASH_ENCRYPTION_DISABLE_PLAINTEXT` (enabled by default if Secure Boot is enabled). This option causes :ref:`FLASH_CRYPT_CNT` to be write protected during initial app startup, or during first boot when the bootloader enables flash encryption. This includes if an app with this option is OTA updated. + +Alternatively, :ref:`FLASH_CRYPT_CNT` can be write-protected using the serial bootloader:: espefuse.py --port PORT write_protect_efuse FLASH_CRYPT_CNT -Alternatively, the app can call :func:`esp_flash_write_protect_crypt_cnt` during its startup process. +A third option with more flexibility: the app can call :func:`esp_flash_write_protect_crypt_cnt` at a convenient time during its startup or provisioning process. .. _flash-encryption-advanced-features: -Flash Encryption Advanced Features ----------------------------------- +Advanced Features +----------------- The following information is useful for advanced use of flash encryption: @@ -370,6 +393,12 @@ It is possible to write these efuse manually, and write protect it before first It is strongly recommended to never write protect ``FLASH_CRYPT_CONFIG`` when it the value is zero. If this efuse is set to zero, no bits in the flash encryption key are tweaked and the flash encryption algorithm is equivalent to AES ECB mode. +JTAG Debugging +^^^^^^^^^^^^^^ + +By default, when Flash Encryption is enabled then JTAG debugging is disabled via eFuse. The bootloader does this on first boot, at the same time it enables flash encryption. + +See :ref:`jtag-debugging-security-features` for more information about using JTAG Debugging with Flash Encryption. Technical Details ----------------- diff --git a/docs/en/security/secure-boot.rst b/docs/en/security/secure-boot.rst index 79baa8725..223425c75 100644 --- a/docs/en/security/secure-boot.rst +++ b/docs/en/security/secure-boot.rst @@ -9,6 +9,10 @@ Secure Boot is separate from the :doc:`Flash Encryption ` feat Enabling secure boot limits your options for further updates of your ESP32. Make sure to read this document throughly and understand the implications of enabling secure boot. +.. note:: + + Secure boot is only supported when using the default GNU Make build system. The CMake build system preview in ESP-IDF v3.x does not support secure boot. + Background ---------- @@ -300,3 +304,12 @@ How To Enable Signed App Verification 4. If you disable "Sign binaries during build" option then you'll have to enter path of a public key file used to verify signed images in "Secure boot public signature verification key". In this case, private signing key should be generated by following instructions in :ref:`secure-boot-generate-key`; public verification key and signed image should be generated by following instructions in :ref:`remote-sign-image`. +Advanced Features +----------------- + +JTAG Debugging +~~~~~~~~~~~~~~ + +By default, when Secure Boot is enabled then JTAG debugging is disabled via eFuse. The bootloader does this on first boot, at the same time it enables Secure Boot. + +See :ref:`jtag-debugging-security-features` for more information about using JTAG Debugging with either Secure Boot or signed app verification enabled. diff --git a/docs/en/versions.rst b/docs/en/versions.rst index 0c36d7302..69c955600 100644 --- a/docs/en/versions.rst +++ b/docs/en/versions.rst @@ -25,6 +25,8 @@ Which Version Should I Start With? - For production purposes, use the `current stable version`_. Stable versions have been manually tested, and are updated with "bugfix releases" which fix bugs without changing other functionality (see `Versioning Scheme`_ for more details). + In order to maximize the time between updates to new ESP-IDF versions, use the latest stable Long Term Support release version. This version can be found on the `Releases page`_. + - For prototyping, experimentation or for developing new ESP-IDF features, use the `latest version (master branch in Git) `_. The latest version in the master branch has all the latest features and has passed automated testing, but has not been completely manually tested ("bleeding edge"). - If a required feature is not yet available in a stable release, but you don't want to use the master branch, it is possible to check out a pre-release version or a release branch. It is recommended to start from a stable version and then follow the instructions for :ref:`updating-pre-release` or :ref:`updating-release-branch`. @@ -46,6 +48,22 @@ ESP-IDF uses `Semantic Versioning `_. This means: If updating to a new bugfix release (for example, from ``v3.0`` to ``v3.0.1``), you should not need to change any code in your project and should only need to re-test functionality relating directly to bugs listed in the release notes on the `Releases page`_. +Support Periods +--------------- + +Each ESP-IDF major and minor release version has an associated support period. After this period, the release is End of Life and no longer supported. Some releases are designated Long Term Support, which means the support period is longer than for other releases. + +The `ESP-IDF Support Period Policy`_ explains this in detail, and describes how the support periods for each release are determined. + +Each release on the `Releases page`_ includes information about the support period for that particular release. + +As a general guideline: + +- Using Long Term Support releases will maximize the amount of time between required ESP-IDF major or minor upgrades. +- Using standard stable releases will require more frequent upgrades to new ESP-IDF versions. However, this means that new features and major improvements will be available more frequently. + +It is also possible to upgrade from a Long Term Support release to a standard release, and vice versa. + Checking The Current Version ---------------------------- @@ -56,6 +74,8 @@ The local ESP-IDF version can be checked using git:: The version is also compiled into the firmware and can be accessed (as a string) via the macro ``IDF_VER``. The default ESP-IDF bootloader will print the version on boot (these versions in code will not always update, it only changes if that particular source file is recompiled). +If writing code that needs to support multiple ESP-IDF versions, the version can be checked at compile time using :ref:`compile-time macros`. + Examples of ESP-IDF versions: ============================ ================================================== @@ -176,3 +196,4 @@ Each time you ``git pull`` this branch, ESP-IDF will be updated with fixes for t .. _`list of branches`: https://github.com/espressif/esp-idf/branches .. _`list of tags`: https://github.com/espressif/esp-idf/tags .. _`current stable version`: https://docs.espressif.com/projects/esp-idf/en/stable/ +.. _`ESP-IDF Support Period Policy`: https://github.com/espressif/esp-idf/blob/master/SUPPORT_POLICY.md diff --git a/docs/requirements.txt b/docs/requirements.txt index f2f2fa26f..ef4051e44 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -4,9 +4,13 @@ sphinx>=1.8.4 breathe==4.11.1 sphinx-rtd-theme -sphinxcontrib-blockdiag>=1.5.5 -sphinxcontrib-seqdiag>=0.8.5 -sphinxcontrib-actdiag>=0.8.5 -sphinxcontrib-nwdiag>=0.9.5 +sphinxcontrib-blockdiag>=1.5.5, <2.0.0 +sphinxcontrib-seqdiag>=0.8.5, <2.0.0 +sphinxcontrib-actdiag>=0.8.5, <2.0.0 +sphinxcontrib-nwdiag>=0.9.5, <2.0.0 +blockdiag>=1.5.4, <2.0.0 +seqdiag>=0.9.6, <2.0.0 +actdiag>=0.5.4, <2.0.0 +nwdiag>=1.0.4, <2.0.0 recommonmark future>=0.16.0 # for ../tools/gen_esp_err_to_name.py \ No newline at end of file diff --git a/docs/zh_CN/api-guides/build-system-cmake.rst b/docs/zh_CN/api-guides/build-system-cmake.rst index 1764a7034..27849606f 100644 --- a/docs/zh_CN/api-guides/build-system-cmake.rst +++ b/docs/zh_CN/api-guides/build-system-cmake.rst @@ -315,6 +315,7 @@ ESP-IDF 在搜索所有待构建的组件时,会按照 ``COMPONENT_DIRS`` 指 - ``COMPONENTS``:此次构建中包含的所有组件的名称,具体格式为用分号隔开的 CMake 列表。 - ``CONFIG_*``:项目配置中的每个值在 cmake 中都对应一个以 ``CONFIG_`` 开头的变量。更多详细信息请参阅 :doc:`Kconfig `。 - ``IDF_VER``:ESP-IDF 的 git 版本号,由 ``git describe`` 命令生成。 +- ``IDF_VERSION_MAJOR``, ``IDF_VERSION_MINOR``, ``IDF_VERSION_PATCH``: ESP-IDF 的组件版本,可用于条件表达式。请注意这些信息的精确度不如 ``IDF_VER`` 变量,版本号 ``v4.0-dev-*``, ``v4.0-beta1``, ``v4.0-rc1`` 和 ``v4.0`` 对应的 ``IDF_VERSION_*`` 变量值是相同的,但是 ``IDF_VER`` 的值是不同的。 - ``IDF_TARGET``:项目的硬件目标名称。 - ``PROJECT_VER``:项目版本号。 diff --git a/docs/zh_CN/api-guides/build-system.rst b/docs/zh_CN/api-guides/build-system.rst index 366f7d79f..60bba24ef 100644 --- a/docs/zh_CN/api-guides/build-system.rst +++ b/docs/zh_CN/api-guides/build-system.rst @@ -176,6 +176,7 @@ ESP-IDF 搜索组件时,会按照 ``COMPONENT_DIRS`` 指定的顺序依次进 - ``CC``,``LD``,``AR``,``OBJCOPY``: gcc xtensa 交叉编译工具链中每个工具的完整路径。 - ``HOSTCC``,``HOSTLD``,``HOSTAR``: 主机本地工具链中每个工具的全名。 - ``IDF_VER``: ESP-IDF 的版本号,可以通过检索 ``$(IDF_PATH)/version.txt`` 文件(假如存在的话)或者使用 git 命令 ``git describe`` 来获取。这里推荐的格式是在一行中指定主 IDF 的发布版本号,例如标记为 ``v2.0`` 的发布版本或者是标记任意一次提交记录的 ``v2.0-275-g0efaa4f``。应用程序可以通过调用 :cpp:func:`esp_get_idf_version` 函数来使用该变量。 +- ``IDF_VERSION_MAJOR``, ``IDF_VERSION_MINOR``, ``IDF_VERSION_PATCH``: ESP-IDF 的组件版本,可用于条件表达式。请注意这些信息的精确度不如 ``IDF_VER`` 变量,版本号 ``v4.0-dev-*``, ``v4.0-beta1``, ``v4.0-rc1`` 和 ``v4.0`` 对应的 ``IDF_VERSION_*`` 变量值是相同的,但是 ``IDF_VER`` 的值是不同的。 如果您在 ``component.mk`` 文件中修改这些变量,这并不会影响其它组件的构建,但可能会使您的组件变得难以构建或调试。 diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst new file mode 100644 index 000000000..d9768116b --- /dev/null +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-architecture.rst @@ -0,0 +1,388 @@ +ESP-BLE-MESH 架构 +================= + +:link_to_translation:`en:[English]` + +本文档将介绍 ESP-BLE-MESH 的架构概览、架构实现和辅助程序。 + +- ESP-BLE-MESH 架构概览 + + - 描述了 ESP-BLE-MESH 架构的 5 大部分及每个部分的功能。 + +- ESP-BLE-MESH 架构实现 + + - 描述了 ESP-BLE-MESH 文件的基本功能、文件与 ESP-BLE-MESH 架构的对应关系及文件间调用的接口。 + +- ESP-BLE-MESH 辅助程序 + + - 描述了 ESP-BLE-MESH 的辅助程序,比如 Mesh 网络管理,Mesh 特性等。 + +1. ESP-BLE-MESH 架构概览 +------------------------ + +目前,ESP-BLE-MESH 已经实现了 Mesh Profile 的大多数功能及 Mesh Model 规范中定义的所有 Client Model。未支持的功能/模型尚在开发中,会尽快提供。 ESP-BLE-MESH 已通过 Bluetooth SIG 蓝牙技术联盟的 `认证 `__。 + +.. figure:: ../../../_static/esp-ble-mesh-architecture.png + :align: center + + 图 1.1 ESP-BLE-MESH 架构图 + +ESP-BLE-MESH 架构主要由以下 5 大部分组成: + +- ``Mesh 协议栈`` + + - ``Mesh Networking`` 负责 BLE Mesh 设备的网络消息处理等。 + - ``Mesh Provisioning`` 负责 BLE Mesh 设备的启动配置流程。 + - ``Mesh Models`` 负责实现 SIG 定义的模型。 + +- ``网络管理`` + + - 负责实现网络管理程序,包括节点删除程序、网络索引 (IV Index) 恢复程序等。 + +- ``特性`` + + - 包括 BLE Mesh 特性,如低功耗特性、好友特性、中继特性等。 + +- ``Mesh 承载层`` + + - 包括 ``广播承载层`` 和 ``GATT 承载层``。承载层对于 ESP-BLE-MESH 协议栈至关重要,因为协议栈基于蓝牙低功耗技术构建而成,其必须利用承载层通过 BLE 广播通道和连接通道进行数据传输。 + +- ``应用程序`` + + - 基于 ESP-BLE-MESH 协议栈和 ``Mesh Models``。 + - 通过调用 API 和处理事件,``Applications`` 实现了与 ESP-BLE-MESH 协议栈中的 ``Mesh Networking`` 和 ``Mesh Provisioning`` 的交互,也实现了与 ``Mesh Models`` 中一系列模型的交互。 + + +1.1 Mesh 协议栈 +--------------- + +1.1.1 Mesh Networking +^^^^^^^^^^^^^^^^^^^^^ + +协议栈架构中的 ``Mesh Networking`` 实现了如下功能: + +- Mesh 网络中节点间的通讯。 +- Mesh 网络中消息的加解密。 +- Mesh 网络资源的管理,如网络秘钥 (NetKey)、网络索引等。 +- Mesh 网络消息的分包与重组。 +- 消息在不同模型间的模型映射。 +- 更多功能,请参见 :doc:`ble-mesh-feature-list`。 + +``Mesh Networking`` 功能的实现是基于层级结构的。每一层的功能如表 1.1 所示: + +.. list-table:: 表 1.1 Mesh Networking 框架描述 + :widths: 40 150 + :header-rows: 1 + + * - 层级 + - 功能 + * - 接入层 + - 接入层定义应用程序数据的格式,还对上层传输层对数据包的加密和解密进行定义和控制。 + * - 上层传输层 + - 上层传输层对接入层进出的应用数据进行加密、解密和认证,同时也处理被称为 “传输控制消息”的特殊消息,这种消息包括了与“友谊”和心跳包相关的消息。 + * - 底层传输层 + - 底层传输层处理 PDU 的分包和重组。 + * - 网络层 + - 网络层定义网络消息的地址类型和格式,实现设备的中继功能。 + +1.1.2 Mesh Provisioning +^^^^^^^^^^^^^^^^^^^^^^^ + +协议栈架构中的 ``Mesh Provisioning`` 实现了如下功能: + +- 对未配网设备的配网。 +- Mesh 网络资源的分配 (单播地址、网络索引和网络秘钥)。 +- 配网期间对 4 种验证方法的支持。 +- 更多功能,请参见 :doc:`ble-mesh-feature-list`。 + +``Mesh Provisioning`` 功能的实现是基于层级结构的。每一层的功能如表 1.2 所示: + +.. list-table:: 表 1.2 Mesh Provisioning 框架描述 + :widths: 40 150 + :header-rows: 1 + + * - 层级 + - 功能 + * - Provisioning PDUs + - 通过配网协议处理不同层级的 Provisioning PDUs。 + * - Generic Provisioning PDU/Proxy PDU + - 使用 Generic Provisioning 层或代理协议层将 Provisioning PDU 传输到未配网的设备。 + * - PB-ADV/PB-GATT + - 这些层级定义了 Provisioning PDUs 作为可分包和重组的消息进行传输的方式。 + * - Advertising/Provisioning Service + - Provisioning bearer 定义了会话建立的方式,该方式用来将 Generic Provisioning 层的传输包传送到设备。 + +1.1.3 Mesh Models +^^^^^^^^^^^^^^^^^ + +协议栈架构中的 ``Mesh Models`` 实现了如下功能: + +- Configuration Client/Server Models +- Health Client/Server Models +- Generic Client/Server Models +- Sensor Client/Server Models +- Time and Scenes Client/Server Models +- Lighting Client/Server Models + +每一层的功能如表 1.3 所示: + +.. list-table:: 表 1.3 Mesh Models 框架描述 + :widths: 40 150 + :header-rows: 1 + + * - 层级 + - 功能 + * - 模型层 + - 模型层实现用于标准化典型用户场景操作的模型,包括 Generic Client/Server Models、Sensor Client/Server Models、Time and Scenes Client/Server Models、Lighting Client/Server Models 和若干自定义模型。 + * - 基础模型层 + - 基础模型层实现与 ESP-BLE-MESH 网络配置、管理和自我诊断等相关的模型。 + +1.2 Mesh 网络管理 +----------------- + +``网络管理`` 实现了如下功能: + +- 节点移除程序:用于将节点从网络中移除。 +- 网络索引恢复程序:用于恢复节点的网络索引。 +- 网络索引更新程序:用于更新节点的网络索引。 +- 秘钥更新程序:用于更新节点的网络秘钥、应用秘钥 (AppKey) 等。 +- 网络创建程序:用于创建 mesh 网络。 +- NVS 存储器:用于存储节点的网络信息。 + +1.3 Mesh 特性 +------------- + +``特性`` 包括以下几项: + +- 低功耗特性:用于降低节点的能耗。 +- 好友特性:用于为低功耗节点存储消息。 +- 中继特性:用于中继/转发节点通过广播承载层收到的网络 PDU. +- Proxy Server/Client 是代理协议中的两个节点角色,其使节点可以通过面向连接的承载层收发 Network PDUs、mesh beacons、代理配置消息和 Provisioning PDU。 + +1.4 Mesh 承载层 +--------------- + +协议栈框架中的 ``承载层`` 负责 ESP-BLE-MESH 协议栈和低功耗蓝牙核心协议间的数据传输。 + +``承载层`` 可视为是基于蓝牙低功耗核心协议的载体层,其实现了 ESP-BLE-MESH 协议栈数据的接收和传输。 + +.. list-table:: 表 1.3 Mesh 承载层描述 + :widths: 40 150 + :header-rows: 1 + + * - 层级 + - 功能 + * - GATT 承载层 + - GATT 承载层使用代理协议通过 GATT 连接在两个设备之间发送和接收 ``Proxy PDUs``。 + * - 广播承载层 + - 使用广播承载层时,必须使用低功耗蓝牙广播通道来发送 mesh 数据包, 数据包中的 AD Type 需要设置为 mesh 数据包的类型。 + +1.5 Mesh ``应用层`` +-------------------- + +协议栈框架图中的 ``应用层`` 通过调用 ESP-BLE-MESH 协议栈提供的 API 并处理协议栈上报的事件来实现相应的功能,有一些常见应用,比如网关、照明等。 + +应用层和 ``API / 事件`` 之间的交互 + +- 应用层调用 API + + - 调用配网相关的 API 进行配网。 + - 调用模型相关的 API 发送消息。 + - 调用设备属性相关的 API 获取设备的本地信息。 + +- 应用层处理事件 + + 应用层的设计基于事件设计,事件将参数传输给应用层。事件主要分为两大类。 + + - 调用 API 完成的事件。 + - 比如接收消息的节点。 + - 协议栈主动上报给应用层的事件。 + - 协议栈主动上报的事件。 + - 模型主动上报的事件。 + +- 事件通过应用层注册的回调函数进行上报,同时回调函数中也会包含对事件的相应处理。 + +``API /事件`` 与 ESP-BLE-MESH 协议栈的交互 + +- 用户使用的 API 主要调用``Mesh Networking``、``Mesh Provisioning`` 和 ``Mesh Models`` 提供的函数。 + +- ``API /事件`` 和协议栈的交互不会跨越协议栈的层级进行操作。比如 API 不会调用 ``Network Layer`` 相关的函数。 + +2. ESP-BLE-MESH 架构实现 +------------------------ + +ESP-BLE-MESH 架构的设计和实现是基于层级和模块的。具体而言,第 2.1 节(Mesh 网络的实现),第 2.2 节(Mesh 配网实现)和第 2.3 节(Mesh 层级实现)基于层级思想,第 2.4 节(网格模型的实现)基于模块思想。 + +- **层级思想**: 基于层级思想,网络架构根据 Mesh Profile Specification 中指定的层级设计而成。每层都有独特的文件,文件包括该层的 API 等。具体设计如图 2.1 所示。 + +- **模块思想**: 每个文件实现一个独立的功能,供其它程序调用。 + +.. figure:: ../../../_static/esp-ble-mesh-interface.png + :align: center + + 图 2.1 ESP-BLE-MESH 架构实现图 + +ESP-BLE-MESH 架构采用分层的方式进行设计,数据包的处理所经过的层级顺序是固定的,也就是数据包的处理过程会形成一个 ``消息流``。因此,我们可以从图 2.1 的协议栈接口图中看到消息流。 + +2.1 Mesh 协议栈的实现 +--------------------- + +2.1.1 Mesh Networking 实现 +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``Mesh Networking`` 中的文件列表和每个文件实现的功能如表 2.1 所示: + +.. list-table:: 表 2.1 Mesh Networking 文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`access.c ` + - BLE Mesh 接入层 + * - :component_file:`transport.c ` + - BLE Mesh 底层/上层传输层 + * - :component_file:`net.c ` + - BLE Mesh 网络层 + * - :component_file:`adv.c ` + - 用于发送 BLE Mesh 广播包的任务,一个用于处理收到的广播包的回调以及用于分配 adv 缓冲区的 API + +2.1.2 Mesh Provisioning 实现 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +由于 Node/Provisioner 共存的限制,Mesh Provisioning 的实现分为两大模块。 + +实现 Node 启动配置的特定文件如表 2.2 所示: + +.. list-table:: 表 2.2 Mesh Provisioning(节点)文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`prov.c ` + - BLE Mesh 节点配网 (PB-ADV & PB-GATT) + * - :component_file:`proxy_server.c ` + - BLE Mesh 节点代理服务器相关功能 + * - :component_file:`beacon.c ` + - 用于处理 BLE Mesh Beacon 的 API + +实现 Provisioner 配置功能的特定文件如表 2.3 所示: + +.. list-table:: 表 2.3 Mesh Provisioning (Provisioner) 文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`provisioner_prov.c ` + - BLE Mesh Provisioner 配置入网 (PB-ADV & PB-GATT) + * - :component_file:`proxy_client.c ` + - BLE Mesh 代理客户端相关功能 + * - :component_file:`provisioner_beacon.c ` + - BLE Mesh Provisioner 接收 Unprovisioned Device Beacon + +2.1.3 Mesh Models 实现 +^^^^^^^^^^^^^^^^^^^^^^ + +Mesh Models 用于实现节点中所包含的模型的具体功能。服务器模型主要用于维护节点状态。客户端模型主要用于获取和修改节点状态。 + +.. list-table:: Table 2.4 Mesh Models 文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`cfg_cli.c ` + - 发送 Configuration Client 消息,接收相应应答消息 + * - :component_file:`cfg_srv.c ` + - 接收 Configuration Client 消息,发送适当应答消息 + * - :component_file:`health_cli.c ` + - 发送 Health Client 消息,接收相应应答消息 + * - :component_file:`health_srv.c ` + - 接收 Health Client 消息,发送适当应答消息 + * - :component_file:`client_common.c ` + - BLE Mesh 模型相关操作 + * - :component_file:`generic_client.c ` + - 发送 BLE Mesh Generic Client 消息,接收相应应答消息 + * - :component_file:`lighting_client.c ` + - 发送 BLE Mesh Lighting Client 消息,接收相应应答消息 + * - :component_file:`sensor_client.c ` + - 发送 BLE Mesh Sensor Client 消息,接收相应应答消息 + * - :component_file:`time_scene_client.c ` + - 发送 BLE Mesh Time Scene Client 消息,接收相应应答消息 + +2.2 Mesh Bearers 实现 +^^^^^^^^^^^^^^^^^^^^^ + +Mesh Bearers 在实现时充分考虑了可移植性。当 ESP-BLE-MESH 协议栈需要移植到其它平台时,用户只需要修改 :component_file:`mesh_bearer_adapt.c ` 就能移植成功。 + +.. list-table:: 表 2.5 Mesh Bearers 文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`mesh_bearer_adapt.c ` + - BLE Mesh 承载层适配文件。此文件提供用于接收和发送 BLE Mesh ADV 和 GATT 相关数据包的接口。 + +.. note:: + + :component_file:`mesh_bearer_adapt.c ` 是对 Mesh 网络框架中 ``Advertising Bearer`` 和 ``GATT Bearer`` 的实现。 + +2.3 Mesh Applications 实现 +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +我们提供了一系列用于客户开发的应用示例,用户可以基于 :ref:`esp-ble-mesh-examples` 开发产品。 + +3. ESP-BLE-MESH 辅助程序 +------------------------- + +辅助程序指的是 ESP-BLE-MESH 协议栈中可选的功能。辅助程序的设计通常通过 :ref:`CONFIG_BLE_MESH` 来实现代码的裁剪。 + +3.1 特性 +^^^^^^^^ + +- 低功耗 +- 好友 +- 中继 +- 代理客户端/代理服务器 + +3.2 网络管理 +^^^^^^^^^^^^ + +- 节点移除程序 +- 网络索引恢复程序 +- 网络索引更新程序 +- 秘钥更新程序 +- 网络创建程序 +- NVS 存储器 + +3.3 辅助程序实现 +^^^^^^^^^^^^^^^^ + +采用独立模块的设计主要考虑到两个因素: + +- 该模块不具备分层实现的条件,其实现可以完全独立,不需要依赖其它模块。 +- 模块中的函数会被反复使用到,因此最好设计成独立模块。独立模块如表 3.1 所示: + +.. list-table:: 表 3.1 模块文件描述 + :widths: 40 150 + :header-rows: 1 + + * - 文件 + - 功能 + * - :component_file:`lpn.c ` + - BLE Mesh 低功耗功能 + * - :component_file:`friend.c ` + - BLE Mesh 好友功能 + * - :component_file:`net.c ` + - BLE Mesh 中继功能、网络创建、网络索引更新程序、网络索引恢复程序、秘钥更新程序相关功能 + * - :component_file:`proxy_server.c ` + - BLE Mesh 代理服务器相关功能 + * - :component_file:`proxy_client.c ` + - BLE Mesh 代理客户端相关功能 + * - :component_file:`settings.c ` + - BLE Mesh NVS 存储器功能 + * - :component_file:`main.c ` + - BLE Mesh 协议栈初始化,协议栈使能,节点移除相关功能 diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst new file mode 100644 index 000000000..34b601a86 --- /dev/null +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-faq.rst @@ -0,0 +1,665 @@ +ESP-BLE-MESH 常见问题手册 +========================= + +:link_to_translation:`en:[English]` + +本文汇总了 ESP-BLE-MESH 协议栈开发的常见问题及解答,全文分为 7 个章节。 + +* :ref:`ble-mesh-faq-provisioner-development` +* :ref:`ble-mesh-faq-node-development` +* :ref:`ble-mesh-faq-ble-mesh-and-wi-fi-coexistence` +* :ref:`ble-mesh-faq-fast-provisioning` +* :ref:`ble-mesh-faq-log-help` +* :ref:`ble-mesh-faq-example-help` +* :ref:`ble-mesh-faq-others` + +用户可以参考这些章节,快速找到问题的答案。该文档会根据各种渠道收集的反馈进行更新。 + +.. _ble-mesh-faq-provisioner-development: + +1. Provisioner 开发 +-------------------- + +通常而言,Provisioner 用于配网未配网设备并形成 mesh 网络。组网后,设备的角色变成节点。 + + +1.1 未配网设备加入 ESP-BLE-MESH 网络的流程是什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 设备通过 Provisioner 加入 ESP-BLE-MESH 网络分为两个阶段,配网阶段和配置阶段。 + + - 配网阶段:为设备分配单播地址、添加网络密钥 (NetKey) 等。通过配网,设备加入 ESP-BLE-MESH 网络,身份从未配网设备变为节点。 + + - 配置阶段:为节点添加应用密钥 (AppKey), 并将应用密钥绑定到相应模型。配置期间,有些选项是可选的,比如为节点添加订阅地址、设置发布地址等。通过配置,该节点实际上可以向 Provisioner 发送消息,也可以接收来自 Provisioner 的消息。 + +1.2 如果 Provisioner 想要改变节点状态,其需满足什么条件? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 需要有和节点的服务器模型相对应的客户端模型。 + - 需要和节点有相同的、可用于加密消息的网络密钥和应用密钥。 + - 需要知道节点的地址,可以是单播地址,也可以是订阅地址。 + +1.3 如何使用网络密钥和应用密钥? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 网络密钥用于加密网络层的消息。具有相同网络密钥的节点视作在同一网络中,具有不同网络密钥的节点相互之间不能进行通信。 + - 应用密钥用于加密上层传输层中的消息。如果服务器模型和客户端模型绑定的应用密钥不同,则无法实现相互通信。 + +1.4 如何生成网络密钥或应用密钥?是否可以采用固定的网络密钥或应用密钥? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - API :cpp:func:`esp_ble_mesh_provisioner_add_local_net_key` 可以用来添加包含固定值或随机值的网络密钥。 + - API :cpp:func:`esp_ble_mesh_provisioner_add_local_app_key` 可以用来添加包含固定值或随机值的应用密钥。 + +1.5 Provisioner 的单播地址是不是固定的? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + :cpp:type:`esp_ble_mesh_prov_t` 中 :code:`prov_unicast_addr` 的值用于设置 Provisioner 的单播地址,只能在初始化期间设置一次,此后不能更改。 + +1.6 Provisioner 的地址是否可以作为节点上报状态消息的目的地址? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner 的单播地址只能在初始化期间设置一次,此后不能更改。理论而言,只要节点知道 Provisioner 的单播地址,此地址便可用作节点上报状态消息的目的地址。节点在网络配置的过程中可以知道 Provisioner 的单播地址,因为 Provisioner 往节点发送消息时,消息的源地址就是 Provisioner 的单播地址。 + + 订阅地址也可使用。Provisioner 订阅组地址或者虚拟地址,节点向该订阅地址发送消息。 + +1.7 被 Provisioner 配网到 ESP-BLE-MESH 网络中的第一个节点的单播地址是不是固定的? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + :cpp:type:`esp_ble_mesh_prov_t` 中 :code:`prov_start_address` 的值用于设置 Provisioner 配网未配网设备的起始地址,即其首先配网的节点的单播地址。单播地址只能在初始化期间设置一次,此后不能修改。 + +1.8 手机 App 首先配置的节点的单播地址是不是固定的? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 该 App 将确定单播地址,目前大多数单播地址是固定的。 + +1.9 如何知道当前 Provisioner 正在配网哪个未配网设备? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + :cpp:type:`esp_ble_mesh_prov_t` 中 :code:`prov_attention` 的值由 Provisioner 在配网过程中设置给未配网设备。该值只能在初始化期间设置一次,此后不能修改。未配网设备加入 mesh 网络后可以用特定的方式来显示自己正在配网,比如灯光闪烁,以告知 Provisioner 其正在配网。 + +1.10 配网过程中,认证设备共有多少种方法?提供的范例中 :example:`provided examples ` 使用了什么方法? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 共有四种设备认证方法,即 No OOB、Static OOB、Output OOB 和 Input OOB。提供的范例使用了 No OOB 的方式。 + +1.11 配置入网前,未配网设备的广播包可以携带哪些信息? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Device UUID + - OOB Info + - URL Hash (可选的) + +1.12 这些信息可以用于设备识别吗? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 是的。每个设备都有独一无二的 Device UUID, 用户可以通过 Device UUID 识别设备。 + +1.13 当 Provisioner 配网的节点包含多个元素时,单播地址是如何分配的? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Provisioner 会给设备的主元素分配一个单播地址,其余元素的单播地址在此基础上递增。 + - 比如:如果一个未配网设备有三个元素,即主要元素、第二元素和第三元素。配网完成后,节点主元素的单播地址为 0x0002,节点第二元素的单播地址为 0x0003,节点第三元素的单播地址为 0x0004。 + +1.14 Provisioner 如何通过 Configuration Client Model 获取并且解析节点的 :ref:`构成数据 ` ? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - Provisioner 可以调用 :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state` 设置参数,调用 :cpp:type:`esp_ble_mesh_cfg_client_get_state_t` 中的 :code:`comp_data_get` 获取节点的构成数据。 + - 用户可以参考以下代码解析 Composition Data: + + .. code:: c + + #include + #include + #include + + //test date: 0C001A0001000800030000010501000000800100001003103F002A00 + //0C00 1A00 0100 0800 0300 0001 05 01 0000 0080 0100 0010 0310 3F002A00 + + // CID is 0x000C + // PID is 0x001A + // VID is 0x0001 + // CRPL is 0x0008 + // Features is 0x0003 – Relay and Friend features. + // Loc is “front” – 0x0100 + // NumS is 5 + // NumV is 1 + // The Bluetooth SIG Models supported are: 0x0000, 0x8000, 0x0001, 0x1000, 0x1003 + // The Vendor Models supported are: Company Identifier 0x003F and Model Identifier 0x002A + + typedef struct { + int16_t cid; + int16_t pid; + int16_t vid; + int16_t crpl; + int16_t features; + int16_t all_models; + uint8_t sig_models; + uint8_t vnd_models; + } esp_ble_mesh_composition_head; + + typedef struct { + uint16_t model_id; + uint16_t vendor_id; + } tsModel; + + typedef struct { + // reserve space for up to 20 SIG models + uint16_t SIG_models[20]; + uint8_t numSIGModels; + + // reserve space for up to 4 vendor models + tsModel Vendor_models[4]; + uint8_t numVendorModels; + } esp_ble_mesh_composition_decode; + + int decode_comp_data(esp_ble_mesh_composition_head *head, esp_ble_mesh_composition_decode *data, uint8_t *mystr, int size) + { + int pos_sig_base; + int pos_vnd_base; + int i; + + memcpy(head, mystr, sizeof(*head)); + + if(size < sizeof(*head) + head->sig_models * 2 + head->vnd_models * 4) { + return -1; + } + + pos_sig_base = sizeof(*head) - 1; + + for(i = 1; i < head->sig_models * 2; i = i + 2) { + data->SIG_models[i/2] = mystr[i + pos_sig_base] | (mystr[i + pos_sig_base + 1] << 8); + printf("%d: %4.4x\n", i/2, data->SIG_models[i/2]); + } + + pos_vnd_base = head->sig_models * 2 + pos_sig_base; + + for(i = 1; i < head->vnd_models * 2; i = i + 2) { + data->Vendor_models[i/2].model_id = mystr[i + pos_vnd_base] | (mystr[i + pos_vnd_base + 1] << 8); + printf("%d: %4.4x\n", i/2, data->Vendor_models[i/2].model_id); + + data->Vendor_models[i/2].vendor_id = mystr[i + pos_vnd_base + 2] | (mystr[i + pos_vnd_base + 3] << 8); + printf("%d: %4.4x\n", i/2, data->Vendor_models[i/2].vendor_id); + } + + return 0; + } + + void app_main(void) + { + esp_ble_mesh_composition_head head = {0}; + esp_ble_mesh_composition_decode data = {0}; + uint8_t mystr[] = { 0x0C, 0x00, 0x1A, 0x00, + 0x01, 0x00, 0x08, 0x00, + 0x03, 0x00, 0x00, 0x01, + 0x05, 0x01, 0x00, 0x00, + 0x00, 0x80, 0x01, 0x00, + 0x00, 0x10, 0x03, 0x10, + 0x3F, 0x00, 0x2A, 0x00}; + int ret; + + ret = decode_comp_data(&head, &data, mystr, sizeof(mystr)); + if (ret == -1) { + printf("decode_comp_data error"); + } + } + +1.15 Provisioner 如何通过获取的 Composition Data 进一步配置节点? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner 通过调用 :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state` 来进行如下配置。 + + - 正确设置参数 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 中的 :code:`app_key_add`,将应用密钥添加到节点中。 + - 正确设置参数 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 中的 :code:`model_sub_add`,将订阅地址添加到节点的模型中。 + - 正确设置参数 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 中的 :code:`model_pub_set`,将发布地址添加到节点的模型中。 + +1.16 节点可以自己添加相应的配置吗? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 本法可用于特殊情况,如测试阶段。 + + - 此示例展示了节点如何为自己的模型添加新的组地址。 + + .. code:: c + + esp_err_t example_add_fast_prov_group_address(uint16_t model_id, uint16_t group_addr) + { + const esp_ble_mesh_comp_t *comp = NULL; + esp_ble_mesh_elem_t *element = NULL; + esp_ble_mesh_model_t *model = NULL; + int i, j; + + if (!ESP_BLE_MESH_ADDR_IS_GROUP(group_addr)) { + return ESP_ERR_INVALID_ARG; + } + + comp = esp_ble_mesh_get_composition_data(); + if (!comp) { + return ESP_FAIL; + } + + for (i = 0; i < comp->element_count; i++) { + element = &comp->elements[i]; + model = esp_ble_mesh_find_sig_model(element, model_id); + if (!model) { + continue; + } + for (j = 0; j < ARRAY_SIZE(model->groups); j++) { + if (model->groups[j] == group_addr) { + break; + } + } + if (j != ARRAY_SIZE(model->groups)) { + ESP_LOGW(TAG, "%s: Group address already exists, element index: %d", __func__, i); + continue; + } + for (j = 0; j < ARRAY_SIZE(model->groups); j++) { + if (model->groups[j] == ESP_BLE_MESH_ADDR_UNASSIGNED) { + model->groups[j] = group_addr; + break; + } + } + if (j == ARRAY_SIZE(model->groups)) { + ESP_LOGE(TAG, "%s: Model is full of group addresses, element index: %d", __func__, i); + } + } + + return ESP_OK; + } + +.. note:: + + 使能了节点的 NVS 存储器后,通过该方式添加的组地址以及绑定的应用密钥在设备掉电的情况下不能保存。这些配置信息只有通过 Configuration Client Model 配置时才会保存。 + +1.17 Provisioner 如何通过分组的方式控制节点? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 通常而言,在 ESP-BLE-MESH 网络中实现组控制有两种方法,即组地址方法和虚拟地址方法。假设有 10 个设备,即 5 个带蓝灯的设备和 5 个带红灯的设备。 + + - 方案一:5 个蓝灯设备订阅一个组地址,5 个红灯设备订阅另一个组地址。Provisioner 往不同的组地址发送消息,即可实现分组控制设备。 + + - 方案二:5 个蓝灯设备订阅一个虚拟地址,5 个红灯设备订阅另一个虚拟地址,Provisioner 往不同的虚拟地址发送消息,即可实现分组控制设备。 + +1.18 Provisioner 如何将节点添加至多个子网? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点配置期间,Provisioner 可以为节点添加多个网络密钥,拥有相同网络密钥的节点属于同一子网。Provisioner 可以通过不同的网络密钥与不同子网内的节点进行通信。 + +1.19 Provisioner 如何知道网络中的某个设备是否离线? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点离线通常定义为:电源故障或其他原因导致的节点无法与 mesh 网络中的其他节点正常通信的情况。 + + ESP-BLE-MESH 网络中的节点间彼此不连接,它们通过广播通道进行通信。 + + 此示例展示了如何通过 Provisioner 检测节点是否离线。 + + - 节点定期给 Provisioner 发送心跳包。如果 Provisioner 超过一定的时间未接收到心跳包,则视该节点离线。 + +.. note:: + + 心跳包的设计应该采用单包(字节数小于 11 个字节)的方式,这样收发效率会更高。 + +1.20 Provisioner 删除网络中的节点时,需要进行哪些操作? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 通常而言,Provisioner 从网络中移除节点主要涉及三个步骤: + + - 首先,Provisioner 将需要移除的节点添加至“黑名单”。 + + - 其次,Provisioner 启动 :ref:`密钥更新程序 `。 + + - 最后,节点执行节点重置程序,切换自身身份为未配网设备。 + +1.21 在密钥更新的过程中,Provisioner 如何更新节点的网络密钥? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 通过正确设置参数 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 中的 :code:`net_key_update`,使用 :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state`,Provisioner 更新节点的网络密钥。 + + - 通过正确设置参数 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 中的 :code:`app_key_update`,使用 :ref:`Configuration Client Model ` API :cpp:func:`esp_ble_mesh_config_client_set_state`,Provisioner 更新节点的应用密钥。 + +1.22 Provisioner 如何管理 mesh 网络中的节点? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + ESP-BLE-MESH 在示例中实现了一些基本的节点管理功能,比如 :cpp:func:`esp_ble_mesh_store_node_info`。 + ESP-BLE-MESH 还提供可用于设置节点本地名称的 API :cpp:func:`esp_ble_mesh_provisioner_set_node_name` 和可用于获取节点本地名称的 API :cpp:func:`esp_ble_mesh_provisioner_get_node_name`。 + +1.23 Provisioner 想要控制节点的服务器模型时需要什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Provisioner 在控制节点的服务器模型前,必须包括相应的客户端模型。 + + Provisioner 应当添加本地的网络密钥和应用密钥。 + + - Provisioner 调用 API :cpp:func:`esp_ble_mesh_provisioner_add_local_net_key` 以添加网络密钥。 + + - Provisioner 调用 API :cpp:func:`esp_ble_mesh_provisioner_add_local_app_key` 以添加应用密钥。 + + Provisioner 应当配置自己的客户端模型。 + + - Provisioner 调用 API :cpp:func:`esp_ble_mesh_provisioner_bind_app_key_to_local_model` 以绑定应用密钥至自己的客户端模型。 + +1.24 Provisoner 如何控制节点的服务器模型? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + ESP-BLE-MESH 支持所有 SIG 定义的客户端模型。Provisioner 可以使用这些客户端模型控制节点的服务器模型。客户端模型分为 6 类,每类有相应的功能。 + + - Configuration Client Model + + - API :cpp:func:`esp_ble_mesh_config_client_get_state` 可用于获取 Configuration Server Model 的 :cpp:type:`esp_ble_mesh_cfg_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_config_client_set_state` 可用于获取 Configuration Server Model 的 :cpp:type:`esp_ble_mesh_cfg_client_set_state_t` 值。 + + - Health Client Model + + - API :cpp:func:`esp_ble_mesh_health_client_get_state` 可用于获取 Health Server Model 的 :cpp:type:`esp_ble_mesh_health_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_health_client_set_state` 可用于获取 Health Server Model 的 :cpp:type:`esp_ble_mesh_health_client_set_state_t` 值。 + + - Generic Client Models + + - API :cpp:func:`esp_ble_mesh_generic_client_get_state` 可用于获取 Generic Server Model 的 :cpp:type:`esp_ble_mesh_generic_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_generic_client_set_state` 可用于获取 Generic Server Model 的 :cpp:type:`esp_ble_mesh_generic_client_set_state_t` 值。 + + - Lighting Client Models + + - API :cpp:func:`esp_ble_mesh_light_client_get_state` 可用于获取 Lighting Server Model 的 :cpp:type:`esp_ble_mesh_light_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_light_client_set_state` 可用于获取 Lighting Server Model 的 :cpp:type:`esp_ble_mesh_light_client_set_state_t` 值。 + + - Sensor Client Models + + - API :cpp:func:`esp_ble_mesh_sensor_client_get_state` 可用于获取 Sensor Server Model 的 :cpp:type:`esp_ble_mesh_sensor_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_sensor_client_set_state` 可用于获取 Sensor Server Model 的 :cpp:type:`esp_ble_mesh_sensor_client_set_state_t` 值。 + + - Time and Scenes Client Models + - API :cpp:func:`esp_ble_mesh_time_scene_client_get_state` 可用于获取 Time and Scenes Server Model 的 :cpp:type:`esp_ble_mesh_time_scene_client_get_state_t` 值。 + - API :cpp:func:`esp_ble_mesh_time_scene_client_set_state` 可用于获取 Time and Scenes Server Model 的 :cpp:type:`esp_ble_mesh_time_scene_client_set_state_t` 值。 + + +.. _ble-mesh-faq-node-development: + +2. 节点开发 +------------ + +2.1 节点包含什么样的模型? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - ESP-BLE-MESH 中,节点由一系列的模型组成,每个模型实现节点的某些功能。 + + - 模型分为两种,客户端模型和服务器模型。客户端模型可以获取并设置服务器模型的状态。 + + - 模型也可以分为 SIG 模型和自定义模型。 SIG 模型的所有行为都由官方定义,而自定义模型的行为均由用户定义。 + +2.2 每个模型对应的消息格式是不是固定的? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 消息由 opcode 和 payload 组成,通过 opcode 进行区分。 + + - 与模型对应的消息的类型和格式都是固定的,这意味着模型之间传输的消息是固定的。 + +2.3 节点的模型可以使用哪些函数发送消息? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 对于客户端模型,用户可以调用 API :cpp:func:`esp_ble_mesh_client_model_send_msg` 发送消息。 + + - 对于服务器模型,用户可以调用 API :cpp:func:`esp_ble_mesh_server_model_send_msg` 发送消息。 + + - 对于发布,用户可以调用 API :cpp:func:`esp_ble_mesh_model_publish` 发布消息。 + +2.4 如何实现消息传输不丢包? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 如果用户要实现消息传输不丢包,则需有应答的消息。等待应答的默认时间在 :ref:`CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT` 中设置。如果发送端等待应答超时,就会触发对应的超时事件。 + +.. note:: + + API :cpp:func:`esp_ble_mesh_client_model_send_msg` 中可以设置应答的超时时间。如果参数 :code:`msg_timeout` 设为 **0**, 那么超时时间便会采用默认值(4 秒)。 + +2.5 如何发送无应答的消息? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 对于客户端模型,用户可以调用 API :cpp:func:`esp_ble_mesh_client_model_send_msg` with the parameter :code:`need_rsp` set to :code:`false` 发送无应答消息。 + + 对于服务器模型,调用 API :cpp:func:`esp_ble_mesh_server_model_send_msg` 发送的消息总是无应答的消息。 + +2.6 如何为模型添加订阅地址? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 通过 Configuration Client Model 添加订阅地址。 + +2.7 模型发送的消息和发布的消息有何不同? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 调用 API :cpp:func:`esp_ble_mesh_client_model_send_msg` 或 :cpp:func:`esp_ble_mesh_server_model_send_msg` 发送的消息会在 Network Transmit 状态规定的期限内发送。 + + 调用 API :cpp:func:`esp_ble_mesh_model_publish` 发布的消息将由模型发布状态决定是否发布。消息的发布一般是周期性的,或者有固定次数。发布周期和发布次数由模型发布状态控制,并且可以通过 Configuration Client Model 进行配置。 + +2.8 发送不分包消息时,最多可携带多少有效字节? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 不分包消息的总有效载荷长度(可由用户设置)为 11 个八位位组,因此,如果消息的 opcode 为 2 个八位位组,则该消息可以携带 9 个八位位组的有效信息。 对于 vendor 消息,由于 opcode 是 3 个八位位组,剩余的有效负载长度为 8 个八位位组。 + +2.9 什么时候应该使能节点的 :ref:`Relay ` 功能? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 如果 mesh 网络中检测到的节点很稀疏,用户可以使能节点的 Relay 功能。 + + 如果 mesh 网络中检测到的节点很密集,用户可以选择仅使能一些节点的 Relay 功能。 + + 如果 mesh 网络大小未知,用户可以默认使能 Relay 功能。 + +2.10 什么时候应该使能节点的 :ref:`Proxy ` 功能? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 如果未配网设备将由电话配网,则未配网设备应该使能 Proxy 功能,因为当前几乎所有电话都不支持通过广播承载层发送 ESP-BLE-MESH 数据包。并且,未配网设备成功配网成为 Proxy 节点后,其会通过 GATT 承载层和广播承载层与 mesh 网络中的其他节点通信。 + +2.11 如何使用代理过滤器? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 代理过滤器用于减少 Proxy Client(如手机)和 Proxy Server(如节点)之间交换的 Network PDU 的数量。另外,通过代理过滤器,Proxy Client 可以明确请求仅接收来自 Proxy Server 的某些目标地址的 mesh 消息。 + +2.12 Relay 节点什么时候可以中继消息? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 如果要中继消息,消息需满足以下要求。 + + - 消息存在于 mesh 网络中。 + + - 消息的目的地址不是节点的单播地址。 + + - 消息的 TTL 值需大于 1。 + +2.13 如果一条消息分成几段,那么其他 Relay 节点是接收到一段消息就中继还是等接收到完整的数据包才中继? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Relay 节点收到其中一段消息时就中继,而非一直等到接收所有的消息。 + +2.14 使用 :ref:`Low Power ` 功能降低功耗的原理是什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 开启无线电进行收听时,设备消耗能量。使能节点的低功耗功能后,它将在大多数时间内关闭无线电功能。 + + - 低功耗节点和好友节点需要合作,因此低功耗节点可以以适当或较低的频率接收消息,而无需一直收听。 + + - 当低功耗节点有一些新消息时,好友节点将为其存储消息。低功耗节点可以间隔固定时间轮询好友节点,以查看是否有新的消息。 + +2.15 设备断电后上电,如何能继续在网络中进行通讯? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 在 `menuconfig` 中启用配置 :code:`Store BLE Mesh Node configuration persistently` 。 + +2.16 如何实现将节点自检的信息发送出来? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 推荐节点通过 Health Server Model 定期发布其自检结果。 + +2.17 节点间如何传输消息? +^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点间传输信息的可能应用场景是,一旦烟雾警报检测到高浓度的烟雾,就会触发喷淋设备。 有两种实现方法。 + + - 方法 1:喷淋设备订阅组地址。当烟雾警报器检测到高浓度的烟雾时,它会发布一条消息,该消息的目标地址是喷淋设备已订阅的组地址。 + + - 方法 2:Provisioner 可以配置喷淋设备的单播地址为烟雾报警器的地址。当检测到高浓度的烟雾时,烟雾警报器以喷淋设备的单播地址为目标地址,将消息发送到喷淋设备。 + +2.18 设备通信必须要网关吗? +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - 情况 1:节点仅在 mesh 网络内通信。这种情况下,不需要网关。ESP-BLE-MESH 网络是一个泛洪的网络,网络中的消息没有固定的路径,节点与节点之间可以随意通信. + + - 情况 2:如果用户想要远程控制网络,比如在到家之前打开某些节点,则需要网关。 + +2.19 何时使用 IV Update 更新程序? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 一旦节点的底层检测到发送的消息的序列号达到临界值,IV Update 更新程序便会启用。 + +2.20 如何启用 IV Update 更新程序? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点可以使用带有 Secure Network Beacon 的 IV Update 更新程序。 + + +.. _ble-mesh-faq-ble-mesh-and-wi-fi-coexistence: + +3. ESP-BLE-MESH 和 Wi-Fi 共存 +------------------------------- + +3.1 Wi-Fi 和 ESP-BLE-MESH 共存时,支持哪些模式? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 目前,只有 Wi-Fi STA 模式支持共存。 + +3.2 Wi-Fi 和 ESP-BLE-MESH 共存时,为什么 Wi-Fi 吞吐量很低? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 未搭载 PSRAM 的 `ESP32-DevKitC <../../hw-reference/get-started-devkitc>`_ 开发板,Wi-Fi 和 ESP-BLE-MESH 共存可以正常运行,但是吞吐率较低。当 Wi-Fi 和 ESP-BLE-MESH 共存时,搭载 PSRAM 的 ESP32-DevKitC 速率可以稳定在 1 Mbps 以上。 + + 应使能 menuconfig 中的一些配置来支持 PSRAM。 + + - :code:`ESP32-specific --> Support for external,SPI-connected RAM --> Try to allocate memories of Wi-Fi and LWIP...` + - :code:`Bluetooth --> Bluedriod Enable --> BT/BLE will first malloc the memory from the PSRAM` + - :code:`Bluetooth --> Bluedriod Enable --> Use dynamic memory allocation in BT/BLE stack.` + - :code:`Bluetooth --> Blutooth controller --> BLE full scan feature supported.` + - :code:`Wi-Fi --> Software controls Wi-Fi/Bluetooth coexistence --> Wi-Fi` + +.. _ble-mesh-faq-fast-provisioning: + +4. 快速配网 +----------- + +4.1 为什么需要快速配网? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 通常而言,存在少量未配网设备时,用户可以逐个配置。但是如果有大量未配网设备(比如 100 个)时,逐个配置会耗费大量时间。通过快速配网,用户可以在约 50 秒内配网 100 个未配网设备。 + +4.2 为什么会出现 EspBleMesh App 在快速配网期间长时间等待的情况? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 快速配网期间,代理节点在配置完一个节点后会断开与 APP 的连接,待所有节点配网完成后再与 APP 重新建立连接。 + +4.3 为什么 APP 中显示的节点地址的数量比现有的节点地址更多? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 每完成一次快速配网后、开始新一次快速配网前,APP 会存有上次配网的数据,因此 APP 中显示的节点地址的数量比现有的节点地址更多。 + +4.4 在 EspBleMesh App 中输入的 ** count ** 值有什么用途? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 此 **count** 值提供给 App 配置的代理节点,以决定何时提前开始 Proxy 广播信息。 + +4.5 运行以下示例 :example:`fast_prov_server ` 的节点的 Configuration Client Model 何时开始工作? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 使能了 Temporary Provisioner 功能后,Configuration Client Model 会开始工作。 + +4.6 Temporary Provisioner 功能会一直处于使能的状态吗? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点收到打开/关闭电灯的消息后,所有节点会禁用其 Temporary Provisioner 功能并且转化为一般节点。 + + +.. _ble-mesh-faq-log-help: + +5. Log 帮助 +----------- + +当 ESP-BLE-MESH 协议栈底层出现错误或者警告时,您可以在这儿找到这些错误和警告的含义。 + +5.1 :code:`ran out of retransmit attempts` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 节点发送分段消息时,由于某些原因,接收端未收到完整的消息。节点会重传消息。当重传次数达到最大重传数时,会出现该警告,当前最大重传数为 4。 + +5.2 :code:`Duplicate found in Network Message Cache` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 当节点收到一条消息时,它会把该消息与网络缓存中存储的消息进行比较。如果在缓存中找到相同的消息,这意味着之前已接受过该消息,则该消息会被丢弃。 + +5.3 :code:`Incomplete timer expired` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 当节点在一定时间段(比如 10 秒)内未收到分段消息的所有段时,则 Incomplete 计时器到时,并且出现该警告。 + +5.4 :code:`No matching TX context for ack` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 当节点收到一个分段 ack 且不能找到任何自己发送的与该 ack 相关的消息时,会出现该警告。 + +5.5 :code:`No free slots for new incoming segmented messages` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 当节点没有空间来接收新的分段消息时,会出现该警告。用户可以通过配置 :ref:`CONFIG_BLE_MESH_RX_SEG_MSG_COUNT` 扩大空间。 + +5.6 :code:`Model not bound to AppKey 0x0000` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 当节点发送带有模型的消息且该模型尚未绑定到索引为 0x000 的应用密钥时,会出现该报错。 + +5.7 :code:`Busy sending message to DST xxxx` 代表什么? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 该错误表示节点的客户端模型已将消息发送给目标节点,并且正在等待响应,用户无法将消息发送到单播地址相同的同一节点。接收到相应的响应或计时器到时后,可以发送另一条消息。 + + +.. _ble-mesh-faq-example-help: + +6. 示例帮助 +----------- + +6.1 ESP-BLE-MESH 回调函数如何分类? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + - API :cpp:func:`esp_ble_mesh_register_prov_callback` 用于注册处理配网和入网相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_config_client_callback` 用于注册处理 Configuration Client Model 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_config_server_callback` 用于注册处理 Configuration Server Model 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_health_client_callback` 用于注册处理 Health Client Model 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_health_server_callback` 用于注册处理 Health Server Model 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_generic_client_callback` 用于注册处理 Generic Client Models 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_light_client_callback` 用于注册处理 Lighting Client Models 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_sensor_client_callback` 用于注册处理 Sensor Client Model 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_time_scene_client_callback` 用于注册处理 Time and Scenes Client Models 相关事件的回调函数。 + - API :cpp:func:`esp_ble_mesh_register_custom_model_callback` 用于注册处理自定义模型和未实现服务器模型的相关事件的回调函数。 + + +.. _ble-mesh-faq-others: + +7. 其他 +--------- + +7.1 如何打印数据包? +^^^^^^^^^^^^^^^^^^^^^^ + + 示例使用如下函数 :cpp:func:`ESP_LOG_BUFFER_HEX` 打印信息语境,而 ESP-BLE-MESH 协议栈使用 :cpp:func:`bt_hex` 打印。 + +7.2 重启 ESP32 应使用哪个 API? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + API :cpp:func:`esp_restart`. + +7.3 如何监测任务栈的剩余空间? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + API :cpp:func:`vTaskList` 可以用于定期打印任务栈的剩余空间。 + +7.4 如何在不更改 menuconfig 输出级别的情况下改变 log 级别? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + 无需使用 menuconfig,可以通过 API :cpp:func:`esp_log_level_set` 修改 log 的输出级别。 diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst new file mode 100644 index 000000000..e208eeddd --- /dev/null +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst @@ -0,0 +1 @@ +.. include:: ../../../en/api-guides/esp-ble-mesh/ble-mesh-feature-list.rst \ No newline at end of file diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst new file mode 100644 index 000000000..d41ad1883 --- /dev/null +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-index.rst @@ -0,0 +1,276 @@ +************ +ESP-BLE-MESH +************ + +:link_to_translation:`en:[English]` + +概述 +===== + +蓝牙 mesh 网络实现了无线设备的“多对多”通讯,其可用于建立包含大量设备的网络。 + +设备能将数据中继至不在初始设备无线电覆盖范围内的其他设备。这样,mesh 网络就能够覆盖非常大的物理区域,并且囊括大量设备。Mesh 网络非常适用于楼宇自动化、传感器网络和其他物联网解决方案,这些情景下数以十计、百计、千计的设备需要与其他设备进行安全可靠的通信。 + +蓝牙 mesh 并非无线通信技术,而是一种网络技术。该技术基于一种无线通讯协议栈,即低功耗蓝牙。 + +ESP-BLE-MESH 基于 Zephyr 蓝牙 Mesh 协议栈的顶端,其实现支持设备配网和节点控制,同时也实现了代理、中继、低功耗和朋友等节点功能。 + +有关 ESP-BLE-MESH 架构实现的信息,请参见 :doc:`ble-mesh-architecture`;有关各自 API 的信息,请参见 :doc:`ESP-BLE-MESH API Reference <../../api-reference/bluetooth/esp-ble-mesh>`。 + +ESP-BLE-MESH 的实现和认证基于最新的 `Mesh Profile v1.0.1 `_ 。有关 ESP-BLE-MESH 认证的细节,请参考 `此处 `_ 。 + +.. note:: + + 如果您在寻找 ESP32 基于 Wi-Fi 的 mesh 方案,请查阅乐鑫的另一款产品 ESP-MESH。更多相关信息及文档,请参见:doc:`ESP-MESH <../../api-reference/network/esp_mesh>`。 + +.. _getting-started-with-ble-mesh: + +ESP-BLE-MESH 快速入门 +===================== + +该章节旨在帮助您基于乐鑫的 ESP32 开发板搭建 ESP-BLE-MESH 网络。 + +我们将会展示如何搭建并运行一个包含 3 个节点的小型 ESP-BLE-MESH 网络,其中包含设备配网、节点配置,以及向特定节点上的 Generic OnOff Server Model 发送开关灯命令。 + +如果您是第一次接触 ESP-IDF,请参见 esp-idf :doc:../../get-started/index 来设置开发环境,编译、烧写和运行示例应用程序。 + +硬件及软件准备 +-------------- + +硬件: + +* 3 块 ESP32 开发板,请参见 :ref:`options `。 +* 连接开发板的 USB 线。 +* ESP-IDF 开发环境。 +* 运行 Android 或 iOS 的手机或平板。 + +软件: + +* 下载至 ESP32 开发板的示例应用 :example:`bluetooth/esp_ble_mesh/ble_mesh_node`。 +* 手机 App: **nRF Mesh** Android 或 iOS 版本。除 nRF Mesh 的 App,以下 App 也支持 ESP-BLE-MESH: + + - `EspBleMesh `_ Android App + - Silicon Labs Android 或 iOS App + +安装 +---- + +以下详细步骤可指导您完成安装过程。 + + +.. _get-started-ble-mesh-check-hardware: + +步骤 1. 检查硬件 +""""""""""""""""" + +`ESP32-DevKitC`_ 和 `ESP-WROVER-KIT`_ 开发板均支持 ESP-BLE-MESH。您可以通过 menuconfig: :code:`idf.py menuconfig` > ``Example Configuration`` > ``Board selection for ESP-BLE-MESH`` 选择特定的开发板。 + +.. note:: + + 如果您打算使用 `ESP32-DevKitC`_ 开发板,请将 RGB 灯焊接至 GPIO 管脚 25、26 和 27。 + + +步骤 2. 配置软件 +"""""""""""""""" + +进入 :example:`bluetooth/esp_ble_mesh/ble_mesh_node` 示例文件夹,运行 :code:`idf.py menuconfig` 选择所使用的开发板,然后运行 :code:`idf.py build` 编译示例。 + +步骤 3. 下载应用 +""""""""""""""""" + + :example:`bluetooth/esp_ble_mesh/ble_mesh_node` 示例编译成功后,用户可以运行 :code:`idf.py flash` 将编译生成的二进制文件下载至 3 块开发板中。 + +当开发板上电后,RGB 灯会变为 **绿色**。 + +.. figure:: ../../../_static/ble-mesh-device-power-on.png + :align: center + + ESP-BLE-MESH 设备上电 + +步骤 4. 设备配网 +""""""""""""""""" + +在该章节中,我们将使用 **nRF Mesh Android** App 演示如何配网设备。用户也可以从 App Store 下载其 iOS 版本。 + +4.1 扫描 (scanner) +^^^^^^^^^^^^^^^^^^ + +扫描 (Scanner) 是 nRF Mesh App 搜索蓝牙通信范围内未配网设备的功能。打开 App,点击底部的扫描按钮 **Scanner**。App 就会开始扫描设备,很快,我们便可在屏幕上看到 3 个未配网设备。 + +.. figure:: ../../../_static/ble-mesh-scanner.png + :align: center + :height: 370 + + nRF Mesh - 扫描 + +4.2 识别 +^^^^^^^^^^^^ + +用户可以选择任何一个未配网设备,此时 App 会尝试和该设备建立连接。连接成功(有时可能需要尝试多次),且发现相应的 ESP-BLE-MESH GATT 服务后,用户可以在屏幕中看到识别按钮 **IDENTIFY**。IDENTIFY 操作告诉用户哪个设备将被配网。 + +.. note:: + IDENTIFY 需要设备侧的支持,然后才能用来识别当前正在配网的设备。当前如果点击识别按钮 **IDENTIFY**,除了串口输出的 log,在当前的 example 中设备侧不会有其他现象。 + +点击识别按钮 **IDENTIFY** 后,用户可以看到配网按钮 **PROVISION**。 + +.. figure:: ../../../_static/ble-mesh-identify-provision.png + :align: center + :height: 370 + + nRF Mesh - 识别 - 配网 + +4.3 配网 +^^^^^^^^^ + +点击配网按钮 **PROVISION**,App 会开始配网设备。当设备配网成功后,开发板上的 RGB 灯会熄灭,此时 App 会执行以下几个步骤: + +1. 和该节点(设备配网后成为节点)断开连接 +2. 尝试和该节点重新建立连接 +3. 连接成功并且发现了相应的 ESP-BLE-MESH GATT 服务 +4. 获取节点的 Composition Data 并且给该节点添加 AppKey + +当以上所有的步骤完成后,节点初始配置完成. 此时点击 **OK**,用户可以看见节点的单播地址分配成功,并且其 Composition Data 也被成功解析. + +.. figure:: ../../../_static/ble-mesh-config-complete.png + :align: center + :height: 370 + + nRF Mesh - 配置完成 + +有时在上述步骤 2 中,App 可能与节点连接失败。这种情况下,用户点击 **OK** 后可以看到,节点只有单播地址被成功分配,Composition data 并没有被成功获取。此时用户需要点击右上角的连接按钮 **CONNECT**,屏幕上会显示原先配网的节点,用户需要选择该节点并与其建立连接。 + +.. figure:: ../../../_static/ble-mesh-initial-config-fail.png + :align: center + :height: 370 + + nRF Mesh - 初始配置失败 + +连接成功后,App 会显示获取 Composition Data 以及添加 AppKey 的按钮。 + +.. figure:: ../../../_static/ble-mesh-reconnect-initial-config.png + :align: center + :height: 370 + + nRF Mesh - 重连 - 初始配置 + +如果该设备是 App 配网的第二个或第三个节点,此时点击连接按钮 **CONNECT** 后,用户可以在屏幕中看到 2 个或 3 个节点。这种情况下,用户可以选择其中的任何一个节点建立连接,连接成功后可以返回至主界面选择需要配置的节点。 + +这里给出了一个 3 个节点的示例。 + +* 左侧图片表示第三个设备成功配网,但是 App 没有和其成功建立连接。当 App 尝试去重新连接第三个节点时,界面上会出现 3 个节点。 +* 右侧图片表示和节点成功建立连接后,App 显示这 3 个节点的信息。用户可以看到 App 已经获取了第一个和第二个节点的 Composition Data,但是对于第三个节点,只有单播地址被成功分配而节点的 Composition Data 未知。 + +.. figure:: ../../../_static/ble-mesh-reconnect-three.png + :align: center + :height: 370 + + nRF Mesh - 重连 - 3 个节点 + +4.4 配置 +^^^^^^^^^ + +当成功配网和初始配置完成后,用户可以配置节点的其余信息,例如将 AppKey 绑定至每个元素 (element) 的每个模型 (model) 中、设置模型的发布信息等。 + +下图展示了如何将 AppKey 绑定至 Primary Element 中的 Generic OnOff Server Model 上。 + +.. figure:: ../../../_static/ble-mesh-model-bind-appkey.png + :align: center + :height: 370 + + nRF Mesh - Model Bind AppKey + +.. note:: + + 用户不需要将 AppKey 绑定至 Configuration Server Model,因为该模型使用 DevKey 在 Upper Transport Layer 中对消息进行加密。 + +Step 5. 运行网络 +""""""""""""""""" + +当 3 个元素中的 Generic OnOff Server Models 均成功绑定 AppKey 后,用户可以使用 App 开关 RBG 灯。 + +在 :example:`bluetooth/esp_ble_mesh/ble_mesh_node` 示例中,第一个 Generic OnOff Server Model 用来控制 **红色**,第二个用来控制 **绿色**,同时第三个用来控制 **蓝色**. + +.. figure:: ../../../_static/ble-mesh-generic-onoff.png + :align: center + :height: 370 + + nRF Mesh - 通用开关控制 + +下图展示了打开了不同色灯的开发板。 + +.. figure:: ../../../_static/ble-mesh-three-nodes-on.png + :align: center + + 3 个上电的 ESP-BLE-MESH 节点 + +.. note:: + 对于 **nRF Mesh** iOS App [version 1.0.4],当节点包含超过一个元素时,App 表现不正确。如果用户尝试打开或者关闭第 2 个或第 3 个 Generic OnOff Server Model,App 会将相应的消息发送至第 1 个 Generic OnOff Server Model。 + + +.. _esp-ble-mesh-examples: + +ESP-BLE-MESH 示例 +=================== + +* :example:`ESP-BLE-MESH 节点 ` - 展示了将 ESP-BLE-MESH 作为拥有 Configuration Server model 和 Generic OnOff Server model 的节点设备的用法。然后,ESP-BLE-MESH Provisioner 可以配网设备,控制表示开/关状态的 RGB 灯,示例请见 :example:`example code `。 + +* :example:`ESP-BLE-MESH 客户端模型 ` - 展示了 Generic OnOff Client model 如何在节点内工作。节点拥有 Configuration Server model、Generic OnOff Server model 和 Generic OnOff Client model,示例请见::example:`example code `。 + +* :example:`ESP-BLE-MESH Provisioner ` - 展示了设备如何充当 ESP-BLE-MESH Provisioner 以配网设备。Provisioner 拥有 Configuration Server model、Configuration Client model 和 Generic OnOff Client model,示例请见 :example:`example code `。 + +* ESP-BLE-MESH 快速配网 - :example:`Client ` 和 :example_file`Server ` - 该示例用于演示快速配网。配网 100 个设备费时不超过 60 秒,示例请见::example:`example client code ` 和 :example:`example server code `。 + +* :example:`Wi-Fi 和 ESP-BLE-MESH 共存 ` - 该示例用于演示 Wi-Fi 和 ESP-BLE-MESH 共存的功能。简而言之,用户可在运行 ESP-BLE-MESH 时使用 Wi-Fi,示例请见 :example:`example code `。 + +* ESP-BLE-MESH 节点控制台 - 该演示实现 ESP-BLE-MESH 节点的基本功能。在演示中,Provisioner 可以扫描、验证节点,节点可以回复 Provisioner 的获取/设置消息,示例请见::example:`example node code ` 和 :example:`example provisioner code `。 + +.. _esp-ble-mesh-demo-videos: + +ESP-BLE-MESH 演示视频 +====================== + +* `Provisioning of ESP-BLE-MESH nodes using Smartphone App `_ +* `Espressif Fast Provisioning using ESP-BLE-MESH App `_ +* `Espressif ESP-BLE-MESH and Wi-Fi Coexistence `_ + + +ESP-BLE-MESH 常见问题手册 +========================= + +* :ref:`ble-mesh-faq-provisioner-development` +* :ref:`ble-mesh-faq-node-development` +* :ref:`ble-mesh-faq-ble-mesh-and-wi-fi-coexistence` +* :ref:`ble-mesh-faq-fast-provisioning` +* :ref:`ble-mesh-faq-log-help` +* :ref:`ble-mesh-faq-example-help` +* :ref:`ble-mesh-faq-others` + + +相关文档 +======== + +.. toctree:: + :maxdepth: 1 + + ble-mesh-feature-list + ble-mesh-architecture + ble-mesh-faq + ble-mesh-terminology + + + +蓝牙 SIG 文档 +------------- + +- `BLE Mesh Profile Specification `_ +- `BLE Mesh Model Specification `_ +- `An Intro to Bluetooth Mesh Part 1 `_ / `Part 2 `__ +- `The Fundamental Concepts of Bluetooth Mesh Networking, Part 1 `_ / `Part 2 `__ +- `Bluetooth Mesh Networking: Friendship `_ +- `Management of Devices in a Bluetooth Mesh Network `_ +- `Bluetooth Mesh Security Overview `_ +- `Provisioning a Bluetooth Mesh Network Part 1 `_ / `Part 2 `__ + + +.. _ESP32-DevKitC: https://www.espressif.com/en/products/hardware/esp32-devkitc/overview +.. _ESP-WROVER-KIT: https://www.espressif.com/en/products/hardware/esp-wrover-kit/overview diff --git a/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst new file mode 100644 index 000000000..8f89cbb84 --- /dev/null +++ b/docs/zh_CN/api-guides/esp-ble-mesh/ble-mesh-terminology.rst @@ -0,0 +1,221 @@ +ESP-BLE-MESH Terminology +======================== + +.. _ble-mesh-terminology-role: + +:link_to_translation:`en:[English]` + +.. list-table:: 表 1 ESP-BLE-MESH 术语 - 身份 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 未配网设备 + - "A device that is not a member of a mesh network is known as an unprovisioned device." + - 示例:照明装置、温控设备、制造设备和电动门等。 + * - 节点 + - "A node is a provisioned device." + - 经配网加入 ESP-BLE-MESH 网络后,未配网设备的身份转变成节点。节点(如照明装置、温控设备、制造设备和电动门)是指能在蓝牙 ESP-BLE-MESH 网络中发送、接收或中继消息的设备,且节点可以选择性地支持一个或多个子网。 + * - 中继节点 + - "A node that supports the Relay feature and has the Relay feature enabled is known as a Relay node." + - 中继节点接收并中继 ESP-BLE-MESH 消息,因此消息可以传输得更远。用户可以根据节点的状态来决定是否使能节点的中继功能。消息可以中继多次,每次中继为“一跳”,消息最多可有 126 跳,足以让消息在广阔的区域内传输。 + * - 代理节点 + - "A node that supports the Proxy feature and has the Proxy feature enabled is known as a Proxy node." + - 代理节点从一个承载层(通常包括广播承载层和 GATT 承载层)接收消息,并通过另一个承载层重新发送消息。其目的是将只支持 GATT 承载层的通讯设备接入到 ESP-BLE-MESH 网络中。通常而言,手机 App 需要一个代理节点才能接入 Mesh 网络。没有代理节点,手机 App 无法与 Mesh 网络中成员通信。 + * - 好友节点 + - "A node that supports the Friend feature, has the Friend feature enabled, and has a friendship with a node that supports the Low Power feature is known as a Friend node." + - 好友节点相当于低功耗节点(LPN)的备份,可存储发往低功耗节点的消息及安全更新信息;当低功耗节点需要这些存储的信息时,这些信息便会被传输至低功耗节点。低功耗节点必须与支持好友特性的另一节点建立“友谊”,以减少其接收器的占空比,从而降低低功耗节点的功耗。低功耗节点需要找到好友节点,与其建立友谊关系,其中涉及的过程称为“友谊建立”。低功耗节点与好友节点的搭配可让低功耗节点规划对无线电的使用,从而以适当或较低的频率接收消息,无需保持收听状态。低功耗节点会轮询好友节点以查看是否有新的消息。 + * - 低功耗节点 + - "A node that supports the Low Power feature and has a friendship with a node that supports the Friend feature is known as a Low Power node." + - 低功耗节点通过轮询从好友节点获取信息,比如消息、安全更新等。 + * - 启动配置设备(以下称为 Provisioner) + - "A node that is capable of adding a device to a mesh network." + - 能够配网未配网设备的设备称为启动配置设备。这一流程通常需要通过产品制造商的提供的 App 来实现,并可在网关、智能手机、平板电脑和其他载体上使用。 + + +.. _ble-mesh-terminology-composition: + +.. list-table:: 表 2 ESP-BLE-MESH 术语 - 节点构成 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 状态 + - "A value representing a condition of an element that is exposed by an element of a node." + - ESP-BLE-MESH 网络中的每台设备都具有一组独立的状态值,表示设备的某些状态,比如照明设备的亮度、颜色等状态。更改状态值会修改设备本身的物理状态,比如更改设备的开关状态值实际是在打开或关闭设备。 + * - 模型 + - "A model defines the basic functionality of a node." + - 一个节点可能包含多个模型,而每个模型定义了节点的基本功能,比如节点所需要的状态、控制状态的消息以及处理消息所产生的动作等。节点功能的实现是基于模型的,模型可分为 SIG 模型和自定义模型,前者由 SIG 定义,而后者由用户定义。 + * - 元素 + - "An addressable entity within a device." + - 一个节点可以包含一个或多个元素,每个元素都有一个单播地址和一个或多个模型,并且同一元素所包含的模型不可以出现重复。 + * - 节点构成状态 + - "The Composition Data state contains information about a node, the elements it includes, and the supported models." + - 通过读取节点构成状态的值,用户可以了解节点的基本信息,比如元素的数量及每个元素中的模型。Provisioner 通过获取这个消息对设备进一步配置,比如配置节点的订阅地址与发布地址。 + + +.. _ble-mesh-terminology-features: + +.. list-table:: 表 3 ESP-BLE-MESH 术语 - 特性 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 低功耗特性 + - "The ability to operate within a mesh network at significantly reduced receiver duty cycles only in conjunction with a node supporting the Friend feature." + - 低功耗功能可降低节点的功耗。当低功耗节点寻找好友节点、且附近有多个好友节点时,它会通过算法选择最适合的好友节点。 + * - 好友特性 + - "The ability to help a node supporting the Low Power feature to operate by storing messages destined for those nodes." + - 通过使能好友特性,节点可以帮助存储低功耗节点的信息。使能了好友特性的节点可能会产生更大的功耗和内存消耗。 + * - 中继特性 + - "The ability to receive and retransmit mesh messages over the advertising bearer to enable larger networks." + - 中继特性能让 ESP-BLE-MESH 的消息在节点之间实现多次跳跃,传输距离可超过两个节点之间直接进行无线电传输的范围,从而覆盖整个网络。使能了中继特性的节点中继消息时,只中继其所在子网的消息,不中继其它子网的消息。使能了中继特性的节点中继分段消息时不考虑数据的完整性。节点每收到一条分段消息便直接中继,不会等待收到完整的消息。 + * - 代理特性 + - "The ability to receive and retransmit mesh messages between GATT and advertising bearers." + - 代理特性的目的是允许不具备广播承载层的节点访问 ESP-BLE-MESH 网络。代理特性通常为需要和手机 App 连接的节点所用。 + + +.. _ble-mesh-terminology-provisioning: + +.. list-table:: 表 4 ESP-BLE-MESH 术语 - 配置入网 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - PB-ADV + - "PB-ADV is a provisioning bearer used to provision a device using Generic Provisioning PDUs over the advertising channels." + - PB-ADV 通过广播通道传输配网过程中产生的数据包。只有 Provisioner 和未配网设备都支持 PB-ADV 时才可使用这种方法进行配网。 + * - PB-GATT + - "PB-GATT is a provisioning bearer used to provision a device using Proxy PDUs to encapsulate Provisioning PDUs within the Mesh Provisioning Service." + - PB-GATT 通过连接通道传输配网过程中产生的数据包。如果未配网设备想使用此方式进行配网,其需要实现相关的 Mesh Provisioning Service。未实现此服务的未配网设备不能通过 PB-GATT 承载层配网接入 mesh 网络。 + * - 配置入网 + - "Provisioning is a process of adding an unprovisioned device to a mesh network, managed by a Provisioner." + - 经过配网,“未配网设备”的身份转变为“节点”,成为 ESP-BLE-MESH 网络中的一员。 + * - 认证方式 + - "Authentication is a step during the provisioning of nodes." + - 未配网设备有四种认证方法:输入带外 (Input OOB)、输出带外 (Output OOB)、静态带外 (Static OOB) 和无带外 (No OOB)。 + * - 输入带外 (Input OOB) + - Input Out-of-Band + - 比如,Provisioner 生成并显示随机数,然后提示用户采取适当操作将随机数输入未配网的设备中。以照明开关为例,用户可以在一定时间内数次按下按钮,以这种形式输入 Provisioner 端显示的随机数。输入带外认证方法与输出带外的认证方法类似,但设备的角色相反。 + * - 输出带外 (Output OOB) + - Output Out-of-Band + - 比如,未配网设备会选择一个随机数,并通过与其功能兼容的方式输出该数字。如果未配网设备是一个灯泡,则其能够闪烁指定的次数。如果未配网设备有 LCD 屏幕,则可以将随机数显示为多位数值。启动 Provisioner 的用户需要输入观察到的数字,来认证未配网的设备。 + * - 静态带外 (Static OOB) + - Static Out-of-Band + - 静态 OOB 的认证方法:使用静态 OOB 信息。如果需要使用无 OOB 信息,请将静态 OOB 字段赋值为 0。如果需要使用 OOB 信息,请使用静态 OOB 信息认证正在配网的设备。 + * - 无带外 (No OOB) + - No Out-of-Band + - 无 OOB 的认证方法:将“静态 OOB”字段赋值为 0。采用这种方式相当于不认证未配网的设备。 + + +.. _ble-mesh-terminology-address: + +.. list-table:: 表 5 ESP-BLE-MESH 术语 - 地址 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 未分配地址 + - "This is a special address type, with a value of 0x0000. Its use indicates that an Element has not yet been configured or had a Unicast Address assigned to it." + - 未配置的元素地址或未分配的元素地址都称为未分配地址。鉴于这些元素没有固定的地址,它们不会用于消息的传输。建议在设置用户代码的地址之前,将该地址的值设为未分配地址。 + * - 单播地址 + - "A unicast address is a unique address allocated to each element." + - 在配网期间,Provisioner 会给网络中处于生命周期内节点的每个元素分配一个单播地址。单播地址可能会出现在消息的源/目标地址字段中。发送到单播地址的消息只能由拥有该单播地址的元素进行处理。 + * - 虚拟地址 + - "A virtual address represents a set of destination addresses. Each virtual address logically represents a Label UUID, which is a 128-bit value that does not have to be managed centrally." + - 虚拟地址与特定的 UUID 标签相关联,可以用作模型的发布地址或订阅地址。UUID 标签是与一个或多个节点的元素相关联的 128 位值。虚拟地址的第 15 位和第 14 位分别设置为 1 和 0。从第 13 位到第 0 位设置为散列值(提供 16384 个散列值)。散列是 UUID 标签的派生。使用订阅元素检查完整的 128 位 UUID 十分低效,而散列值提供了一种更有效的方法来确定最终将哪些消息发送到哪些元素。 + * - 群组地址 + - "A group address is an address that is programmed into zero or more elements." + - 群组地址是 ESP-BLE-MESH 网络中的另一种多播地址,通常用于将节点进行分组。发送到 all-proxies 地址的信息应由启用了代理功能的所有节点的主要元素处理。 发送到 all-friends 地址的消息应由启用了好友功能的所有节点的主要元素处理。 发送到 all-relays 地址的消息应由启用了中继功能的所有节点的主要元素处理。 发送到 all-nodes 地址的消息应由所有节点的主要元素处理。 + + +.. _ble-mesh-terminology-security: + +.. list-table:: 表 6 ESP-BLE-MESH 术语 - 安全 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 设备密钥 (DevKey) + - "There is also a device key, which is a special application key that is unique to each node, is known only to the node and a Configuration Client, and is used to secure communications between the node and a Configuration Client." + - 设备密钥让您能够配网未配网设备、配置节点。设备密钥用来加密配置信息,即配置设备时 Provisioner 和节点之间传输的消息。 + * - 应用密钥 (AppKey) + - "Application keys are used to secure communications at the upper transport layer." + - 应用密钥用于应用数据传递至应用层过程中对应用数据的解密,和应用层下发过程中对数据的加密。网络中的一些节点有特定的用途,并且可以根据应用程序的需求对一些潜在敏感数据的访问进行限制。通过特定的应用密钥,这些节点与特定应用程序相关联。通常而言,使用不同应用密钥的领域有安全(楼宇门禁、机房门禁和 CEO 办公室门禁)、照明(工厂、外部楼宇和人行道)和 HVAC 系统。应用密钥绑定在网络密钥上,这意味着应用密钥仅在绑定网络密钥的情况下使用。每一个应用密钥仅可绑定到一个网络密钥。 + * - 主安全资料 + - "The master security material is derived from the network key (NetKey) and can be used by other nodes in the same network. Messages encrypted with master security material can be decoded by any node in the same network. " + - 使用好友安全材料加密的相应友谊消息有:1. 好友轮询 (Friend Poll),2. 好友更新 (Friend Update),3. 好友订阅列表 (Friend Subscription List),添加/删除/确认,4. 好友节点发送到低功耗节点的“已存储消息”,使用主安全材料加密的相应友谊消息有:1. 好友清除 (Friend Clear),2. 好友清除确认 (Friend Clear Confirm)。根据应用程序的设置,从低功耗节点发送到好友节点的消息会使用友谊安全材料或主安全材料进行加密,前者用于低功耗节点与好友节点之间的消息传输,而后者用于其他网络消息。 + + +.. _ble-mesh-terminology-message: + +.. list-table:: 表 7 ESP-BLE-MESH 术语 - 消息 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 重组 / 分包 + - "Segmentation and reassembly (SAR) is a method of communication network, which is divided into small units before transmitting packets and reassembled in a proper order at the communication receiving end." + - 底层传输层会自动分包过大的消息。接收端会回复一条应答消息,根据应答消息,发送端会重新向接收端发送其未接收到的数据包。这些都是底层传输层自动完成的。未分包的消息最多携带 15 个字节,其中 4 个字节是 transMIC,所以剩余 11 个字节;在分包的情况下,前面的包中每包有 12 个有效字节,最后一个包中有 8 个有效字节。特殊情况:一个较短的包需要底层传输端强制分包,这种情况下有 8 个有效字节。 + * - 无应答 / 有应答 + - "There are two types of messages: Unacknowledged or Acknowledged." + - 根据接收端是否需要发送应答消息,发送的消息可分为两种。发送端需要设置最大重传次数。 + +.. _ble-mesh-terminology-foundation-models: + +.. list-table:: 表 8 ESP-BLE-MESH 术语 - 基础模型 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - Configuration Server Model + - "This model is used to represent a mesh network configuration of a device." + - 节点必须包含 Configuration Server Model,其负责维护配置相关的状态。Configuration Server Model 维护的状态包含:网络密钥名单 (NetKey List)、应用密钥名单 (AppKey List)、模型绑定的应用密钥名单 (Model to AppKey List)、节点身份 (Node Identity)、密钥更新阶段 (Key Refresh Phase)、心跳消息发布 (Heartbeat Publish)、心跳消息订阅 (Heartbeat Subscription)、网络传输 (Network Transmit) 和中继重传 (Relay Retransmit) 等。 + * - Configuration Client Model + - "The model is used to represent an element that can control and monitor the configuration of a node." + - Configuration Client Model 通过消息控制 Configuration Server Model 维护的状态。Provisioner 必须包含 Configuration Client Model,有了该模型才可发送 "Configuration Composition Data Get" 等配置消息。 + * - Health Server Model + - "This model is used to represent a mesh network diagnostics of a device." + - Health Server Model 主要用于设备检查自身状态,查看自身是否发生错误。Health Server model 维护的状态包含:当前故障 (Current Fault)、已登记故障 (Registered Fault)、健康周期 (Health Period) 和关注计时器 (Attention Timer)。 + * - Health Client Model + - "The model is used to represent an element that can control and monitor the health of a node." + - Health Client Model 通过消息控制 Health Server Model 维护的状态。该模型可通过消息 “Health Fault Get” 获取其他节点的自检信息。 + + +.. _ble-mesh-terminology-network-management: + +.. list-table:: 表 9 ESP-BLE-MESH 术语 - 网络管理 + :widths: 10 40 60 + :header-rows: 1 + + * - 术语 + - 官方定义 + - 详细说明 + * - 密钥更新程序 + - "This procedure is used when the security of one or more network keys and/or one or more of the application keys has been compromised or could be compromised." + - 密钥更新程序用于更新 ESP-BLE-MESH 网络的网络密钥和应用密钥。当一个或多个网络密钥和/或一个或多个应用密钥的安全受到威胁或可能受到威胁时,会启动密钥更新程序。通常而言,在网络中某些节点移除后可以进行密钥更新。 + * - IV 更新程序 + - "A node can also use an IV Update procedure to signal to peer nodes that it is updating the IV Index." + - IV 更新程序用于更新 ESP-BLE-MESH 网络的 IV Index 的值,这个值和消息加密时所需的随机数相关。为了保证随机数的值不重复,所以将这个值定期增加。IV Index 是一个 32 位的值,是一种共享网络资源,比如一个 mesh 网中的所有节点共享一个 IV Index 值。IV Index 从 0x00000000 开始,在 IV 更新过程中递增,并由特定的进程维护,以保证整个 Mesh 网内共享一个 IV Index。当节点认为它有耗尽其序列号的风险,或它确定另一个节点即将耗尽其序列号时,可以启动该程序。注意:每次的更新时间不得低于 96 小时。节点接收到 secure network beacon 或者确定自己的序列号大于特定值时,会触发 IV 更新程序。 + +官方定义摘自 `ESP-BLE-MESH Glossary of Terms `_. +查看更多术语,也请参照上述网址。 + + + diff --git a/docs/zh_CN/api-guides/index.rst b/docs/zh_CN/api-guides/index.rst index bc205d50e..5e24c44dd 100644 --- a/docs/zh_CN/api-guides/index.rst +++ b/docs/zh_CN/api-guides/index.rst @@ -30,6 +30,7 @@ API 指南 RF Calibration WiFi Driver ESP-MESH + ESP-BLE-MESH BluFi External SPI-connected RAM 链接脚本生成机制 diff --git a/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst b/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst index fe04714c8..ddb20a596 100644 --- a/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst +++ b/docs/zh_CN/api-guides/jtag-debugging/tips-and-quirks.rst @@ -233,6 +233,27 @@ ESP32 的目标配置文件 cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an exception! cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an overrun! +.. _jtag-debugging-security-features: + +JTAG with Flash Encryption or Secure Boot +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, enabling Flash Encryption and/or Secure Boot will disable JTAG debugging. On first boot, the bootloader will burn an eFuse bit to permanently disable JTAG at the same time it enables the other features. + +The project configuration option :ref:`CONFIG_SECURE_BOOT_ALLOW_JTAG` will keep JTAG enabled at this time, removing all physical security but allowing debugging. (Although the name suggests Secure Boot, this option can be applied even when only Flash Encryption is enabled). + +However, OpenOCD may attempt to automatically read and write the flash in order to set :ref:`software breakpoints `. This has two problems: + +- Software breakpoints are incompatible with Flash Encryption, OpenOCD currently has no support for encrypting or decrypting flash contents. +- If Secure Boot is enabled, setting a software breakpoint will change the digest of a signed app and make the signature invalid. This means if a software breakpoint is set and then a reset occurs, the signature verification will fail on boot. + +To disable software breakpoints while using JTAG, add an extra argument ``-c 'set ESP_FLASH_SIZE 0'`` to the start of the OpenOCD command line. For example:: + + openocd -c 'set ESP_FLASH_SIZE 0' -f board/esp32-wrover-kit-3.3v.cfg + +.. note:: + + For the same reason, the ESP-IDF app may fail bootloader verification of app signatures, when this option is enabled and a software breakpoint is set. .. _jtag-debugging-tip-reporting-issues: diff --git a/docs/zh_CN/api-reference/bluetooth/esp-ble-mesh.rst b/docs/zh_CN/api-reference/bluetooth/esp-ble-mesh.rst new file mode 100644 index 000000000..d27fa24f9 --- /dev/null +++ b/docs/zh_CN/api-reference/bluetooth/esp-ble-mesh.rst @@ -0,0 +1 @@ +.. include:: ../../../en/api-reference/bluetooth/esp-ble-mesh.rst \ No newline at end of file diff --git a/docs/zh_CN/api-reference/bluetooth/nimble/index.rst b/docs/zh_CN/api-reference/bluetooth/nimble/index.rst new file mode 100644 index 000000000..48efe410c --- /dev/null +++ b/docs/zh_CN/api-reference/bluetooth/nimble/index.rst @@ -0,0 +1 @@ +.. include:: ../../../../en/api-reference/bluetooth/nimble/index.rst diff --git a/docs/zh_CN/api-reference/protocols/esp_websocket_client.rst b/docs/zh_CN/api-reference/protocols/esp_websocket_client.rst new file mode 100644 index 000000000..c31856240 --- /dev/null +++ b/docs/zh_CN/api-reference/protocols/esp_websocket_client.rst @@ -0,0 +1 @@ +.. include:: ../../../en/api-reference/protocols/esp_websocket_client.rst diff --git a/docs/zh_CN/conf.py b/docs/zh_CN/conf.py index 6e3c230d8..567585ffb 100644 --- a/docs/zh_CN/conf.py +++ b/docs/zh_CN/conf.py @@ -6,14 +6,17 @@ # Importing conf_common adds all the non-language-specific # parts to this conf module -import sys -import os -sys.path.insert(0, os.path.abspath('..')) -from conf_common import * # noqa: F401, F403 - need to make available everything from common +try: + from conf_common import * # noqa: F403,F401 +except ImportError: + import sys + import os + sys.path.insert(0, os.path.abspath('..')) + from conf_common import * # noqa: F403,F401 # General information about the project. project = u'ESP-IDF 编程指南' -copyright = u'2016 - 2019 乐鑫信息科技(上海)股份有限公司' +copyright = u'2016 - 2020 乐鑫信息科技(上海)股份有限公司' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/zh_CN/get-started-cmake/linux-setup-scratch.rst b/docs/zh_CN/get-started-cmake/linux-setup-scratch.rst index 05bcc281d..9ba3ff170 100644 --- a/docs/zh_CN/get-started-cmake/linux-setup-scratch.rst +++ b/docs/zh_CN/get-started-cmake/linux-setup-scratch.rst @@ -19,7 +19,7 @@ - Ubuntu 和 Debian:: - sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache + sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache libffi-dev libssl-dev - Arch:: diff --git a/docs/zh_CN/get-started-cmake/linux-setup.rst b/docs/zh_CN/get-started-cmake/linux-setup.rst index acc1cd3bf..7595b94d2 100644 --- a/docs/zh_CN/get-started-cmake/linux-setup.rst +++ b/docs/zh_CN/get-started-cmake/linux-setup.rst @@ -17,7 +17,7 @@ Linux 平台工具链的标准设置 (CMake) - Ubuntu 和 Debian:: - sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache + sudo apt-get install git wget libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing cmake ninja-build ccache libffi-dev libssl-dev - Arch:: diff --git a/docs/zh_CN/get-started-cmake/windows-setup-scratch.rst b/docs/zh_CN/get-started-cmake/windows-setup-scratch.rst index a2669f234..154bdf6b8 100644 --- a/docs/zh_CN/get-started-cmake/windows-setup-scratch.rst +++ b/docs/zh_CN/get-started-cmake/windows-setup-scratch.rst @@ -58,7 +58,7 @@ Python 安装完成后,打开 Windows 开始菜单下的 Command Prompt,并 从 dl.espressif.com 下载预编译的 Windows 平台工具链: -https://dl.espressif.com/dl/xtensa-esp32-elf-win32-1.22.0-80-g6c4433a-5.2.0.zip +https://dl.espressif.com/dl/xtensa-esp32-elf-win32-1.22.0-96-g2852398-5.2.0.zip 解压压缩包文件到 ``C:\Program Files`` (或其他地址)。压缩包文件包含 ``xtensa-esp32-elf`` 目录。 diff --git a/docs/zh_CN/get-started/linux-setup-scratch.rst b/docs/zh_CN/get-started/linux-setup-scratch.rst index f79816892..8c7dcdadb 100644 --- a/docs/zh_CN/get-started/linux-setup-scratch.rst +++ b/docs/zh_CN/get-started/linux-setup-scratch.rst @@ -15,7 +15,7 @@ - Ubuntu 和 Debian:: - sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing + sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing libffi-dev libssl-dev - Arch:: diff --git a/docs/zh_CN/get-started/linux-setup.rst b/docs/zh_CN/get-started/linux-setup.rst index 62bf8ed73..8cd23e928 100644 --- a/docs/zh_CN/get-started/linux-setup.rst +++ b/docs/zh_CN/get-started/linux-setup.rst @@ -16,7 +16,7 @@ Linux 平台工具链的标准设置 - Ubuntu and Debian:: - sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing + sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing libffi-dev libssl-dev - Arch:: diff --git a/docs/zh_CN/get-started/windows-setup.rst b/docs/zh_CN/get-started/windows-setup.rst index 348f5f6e7..95e3e560b 100644 --- a/docs/zh_CN/get-started/windows-setup.rst +++ b/docs/zh_CN/get-started/windows-setup.rst @@ -15,7 +15,7 @@ Windows 没有内置的 "make" 环境,因此如果要安装工具链,你需 快速设置的方法是从 dl.espressif.com 下载集成在一起的工具链和 MSYS2 压缩文件: -https://dl.espressif.com/dl/esp32_win32_msys2_environment_and_toolchain-20181001.zip +https://dl.espressif.com/dl/esp32_win32_msys2_environment_and_toolchain_idf3-20200601.zip 将 zip 压缩文件解压到 ``C:\`` (或其它路径,这里假设是 ``C:\``),它会使用预先准备的环境创建一个 ``msys32`` 目录。 diff --git a/docs/zh_CN/index.rst b/docs/zh_CN/index.rst index 232b5a88f..577ac478f 100644 --- a/docs/zh_CN/index.rst +++ b/docs/zh_CN/index.rst @@ -15,22 +15,22 @@ ESP-IDF 编程指南 ================== ================== ================== -.. |快速入门| image:: ../_static/get-started.gif +.. |快速入门| image:: ../_static/get-started.png .. _快速入门: get-started/index.html -.. |API 参考| image:: ../_static/api-reference.gif +.. |API 参考| image:: ../_static/api-reference.png .. _API 参考: api-reference/index.html -.. |H/W 参考| image:: ../_static/hw-reference.gif +.. |H/W 参考| image:: ../_static/hw-reference.png .. _H/W 参考: hw-reference/index.html -.. |API 指南| image:: ../_static/api-guides.gif +.. |API 指南| image:: ../_static/api-guides.png .. _API 指南: api-guides/index.html -.. |贡献代码| image:: ../_static/contribute.gif +.. |贡献代码| image:: ../_static/contribute.png .. _贡献代码: contribute/index.html -.. |相关资源| image:: ../_static/resources.gif +.. |相关资源| image:: ../_static/resources.png .. _相关资源: resources.html diff --git a/examples/README.md b/examples/README.md index 62e0b73bd..ba1539fd1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,7 +6,8 @@ This directory contains a range of example ESP-IDF projects. These are intended The examples are grouped into subdirectories by category. Each category directory contains one or more example projects: -* `bluetooth` contains Bluetooth (BLE & BT Classic) examples. +* `bluetooth` contains Bluetooth (BLE & BT Classic) examples using default Bluedroid host stack. +* `bluetooth/nimble` contains BLE examples using NimBLE host stack * `ethernet` contains Ethernet examples. * `get-started` contains some very simple examples with minimal functionality. * `mesh` contains Wi-Fi Mesh examples. diff --git a/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h b/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h index 32c8c6c0c..513bcbef3 100644 --- a/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h +++ b/examples/bluetooth/ble_hid_device_demo/main/esp_hidd_prf_api.h @@ -50,14 +50,15 @@ typedef enum { ESP_HIDD_DEINIT_FAILED = 0, } esp_hidd_deinit_state_t; -#define LEFT_CONTROL_KEY_MASK (1 >> 0) -#define LEFT_SHIFT_KEY_MASK (1 >> 1) -#define LEFT_ALT_KEY_MASK (1 >> 2) -#define LEFT_GUI_KEY_MASK (1 >> 3) -#define RIGHT_CONTROL_KEY_MASK (1 >> 4) -#define RIGHT_SHIFT_KEY_MASK (1 >> 5) -#define RIGHT_ALT_KEY_MASK (1 >> 6) -#define RIGHT_GUI_KEY_MASK (1 >> 7) +#define LEFT_CONTROL_KEY_MASK (1 << 0) +#define LEFT_SHIFT_KEY_MASK (1 << 1) +#define LEFT_ALT_KEY_MASK (1 << 2) +#define LEFT_GUI_KEY_MASK (1 << 3) +#define RIGHT_CONTROL_KEY_MASK (1 << 4) +#define RIGHT_SHIFT_KEY_MASK (1 << 5) +#define RIGHT_ALT_KEY_MASK (1 << 6) +#define RIGHT_GUI_KEY_MASK (1 << 7) + typedef uint8_t key_mask_t; /** * @brief HIDD callback parameters union diff --git a/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c b/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c index b0ca2daa9..d438b7fd9 100644 --- a/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c +++ b/examples/bluetooth/ble_throughput/throughput_client/main/example_ble_client_throughput.c @@ -30,6 +30,15 @@ #include "freertos/semphr.h" #include "freertos/task.h" +/********************************************************** + * Thread/Task reference + **********************************************************/ +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLUETOOTH_TASK_PINNED_TO_CORE (0) +#endif + #define GATTC_TAG "GATTC_DEMO" #define REMOTE_SERVICE_UUID 0x00FF #define REMOTE_NOTIFY_CHAR_UUID 0xFF01 @@ -58,7 +67,7 @@ static SemaphoreHandle_t gattc_semaphore; uint8_t write_data[GATTC_WRITE_LEN] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x0f}; #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ -static bool is_connecet = false; +static bool is_connect = false; /* Declare static functions */ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); @@ -141,7 +150,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } break; case ESP_GATTC_CONNECT_EVT: { - is_connecet = true; + is_connect = true; ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if); gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->connect.conn_id; memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t)); @@ -331,7 +340,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGI(GATTC_TAG, "write char success "); break; case ESP_GATTC_DISCONNECT_EVT: - is_connecet = false; + is_connect = false; get_server = false; #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) start = false; @@ -477,15 +486,15 @@ static void throughput_client_task(void *param) while(1) { #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) vTaskDelay(2000 / portTICK_PERIOD_MS); - if(is_connecet){ + if(is_connect){ uint32_t bit_rate = 0; if (start_time) { current_time = esp_timer_get_time(); bit_rate = notify_len * SECOND_TO_USECOND / (current_time - start_time); - ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Btye/s, = %d bit/s, time = %ds", + ESP_LOGI(GATTC_TAG, "Notify Bit rate = %d Byte/s, = %d bit/s, time = %ds", bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND)); } else { - ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Btye/s, = 0 bit/s"); + ESP_LOGI(GATTC_TAG, "Notify Bit rate = 0 Byte/s, = 0 bit/s"); } } #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ @@ -494,15 +503,22 @@ static void throughput_client_task(void *param) int res = xSemaphoreTake(gattc_semaphore, portMAX_DELAY); assert(res == pdTRUE); } else { - if (is_connecet) { - // the app data set to 490 just for divided into two packages to send in the low layer - // when the packet length set to 251. - esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, - gl_profile_tab[PROFILE_A_APP_ID].conn_id, - gl_profile_tab[PROFILE_A_APP_ID].char_handle, - sizeof(write_data), write_data, - ESP_GATT_WRITE_TYPE_NO_RSP, - ESP_GATT_AUTH_REQ_NONE); + if (is_connect) { + int free_buff_num = esp_ble_get_sendable_packets_num(); + if(free_buff_num > 0) { + for( ; free_buff_num > 0; free_buff_num--) { + // the app data set to 490 just for divided into two packages to send in the low layer + // when the packet length set to 251. + esp_ble_gattc_write_char(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, + gl_profile_tab[PROFILE_A_APP_ID].conn_id, + gl_profile_tab[PROFILE_A_APP_ID].char_handle, + sizeof(write_data), write_data, + ESP_GATT_WRITE_TYPE_NO_RSP, + ESP_GATT_AUTH_REQ_NONE); + } + } else { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all. + vTaskDelay( 10 / portTICK_PERIOD_MS ); + } } } #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ @@ -518,7 +534,7 @@ void app_main() ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } - ESP_ERROR_CHECK( ret ); + ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); @@ -570,10 +586,12 @@ void app_main() if (local_mtu_ret){ ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret); } + // The task is only created on the CPU core that Bluetooth is working on, + // preventing the sending task from using the un-updated Bluetooth state on another CPU. + xTaskCreatePinnedToCore(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL, BLUETOOTH_TASK_PINNED_TO_CORE); - xTaskCreate(&throughput_client_task, "throughput_client_task", 4096, NULL, 10, NULL); #if (CONFIG_GATTC_WRITE_THROUGHPUT) - gattc_semaphore = xSemaphoreCreateMutex(); + gattc_semaphore = xSemaphoreCreateBinary(); if (!gattc_semaphore) { ESP_LOGE(GATTC_TAG, "%s, init fail, the gattc semaphore create fail.", __func__); return; diff --git a/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c b/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c index 59a36a01a..9087de10b 100644 --- a/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c +++ b/examples/bluetooth/ble_throughput/throughput_server/main/example_ble_server_throughput.c @@ -24,16 +24,23 @@ #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" - #include "esp_gap_ble_api.h" #include "esp_gatts_api.h" #include "esp_bt_defs.h" #include "esp_bt_main.h" -#include "esp_bt_main.h" #include "esp_gatt_common_api.h" #include "sdkconfig.h" +/********************************************************** + * Thread/Task reference + **********************************************************/ +#ifdef CONFIG_BLUEDROID_PINNED_TO_CORE +#define BLUETOOTH_TASK_PINNED_TO_CORE (CONFIG_BLUEDROID_PINNED_TO_CORE < portNUM_PROCESSORS ? CONFIG_BLUEDROID_PINNED_TO_CORE : tskNO_AFFINITY) +#else +#define BLUETOOTH_TASK_PINNED_TO_CORE (0) +#endif + #define SECOND_TO_USECOND 1000000 #define GATTS_TAG "GATTS_DEMO" @@ -53,7 +60,7 @@ static uint64_t start_time = 0; static uint64_t current_time = 0; #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ -static bool is_connecet = false; +static bool is_connect = false; ///Declare the static function static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); @@ -522,7 +529,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i case ESP_GATTS_STOP_EVT: break; case ESP_GATTS_CONNECT_EVT: { - is_connecet = true; + is_connect = true; esp_ble_conn_update_params_t conn_params = {0}; memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t)); /* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */ @@ -540,7 +547,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i break; } case ESP_GATTS_DISCONNECT_EVT: - is_connecet = false; + is_connect = false; ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT"); esp_ble_gap_start_advertising(&adv_params); break; @@ -611,15 +618,22 @@ void throughput_server_task(void *param) #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ while(1) { -#if (CONFIG_GATTS_NOTIFY_THROUGHPUT) +#if (CONFIG_GATTS_NOTIFY_THROUGHPUT) if (!can_send_notify) { int res = xSemaphoreTake(gatts_semaphore, portMAX_DELAY); assert(res == pdTRUE); } else { - if (is_connecet) { - esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, - gl_profile_tab[PROFILE_A_APP_ID].char_handle, - sizeof(indicate_data), indicate_data, false); + if (is_connect) { + int free_buff_num = esp_ble_get_sendable_packets_num(); + if(free_buff_num > 0) { + for( ; free_buff_num > 0; free_buff_num--) { + esp_ble_gatts_send_indicate(gl_profile_tab[PROFILE_A_APP_ID].gatts_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, + gl_profile_tab[PROFILE_A_APP_ID].char_handle, + sizeof(indicate_data), indicate_data, false); + } + } else { //Add the vTaskDelay to prevent this task from consuming the CPU all the time, causing low-priority tasks to not be executed at all. + vTaskDelay( 10 / portTICK_PERIOD_MS ); + } } } #endif /* #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) */ @@ -630,10 +644,10 @@ void throughput_server_task(void *param) if (start_time) { current_time = esp_timer_get_time(); bit_rate = write_len * SECOND_TO_USECOND / (current_time - start_time); - ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Btye/s, = %d bit/s, time = %ds", + ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = %d Byte/s, = %d bit/s, time = %ds", bit_rate, bit_rate<<3, (int)((current_time - start_time) / SECOND_TO_USECOND)); } else { - ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Btye/s, = 0 bit/s"); + ESP_LOGI(GATTS_TAG, "GATTC write Bit rate = 0 Byte/s, = 0 bit/s"); } #endif /* #if (CONFIG_GATTC_WRITE_THROUGHPUT) */ @@ -695,12 +709,13 @@ void app_main() esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(517); if (local_mtu_ret){ - ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret); + ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret); } - - xTaskCreate(&throughput_server_task, "throughput_server_task", 4048, NULL, 15, NULL); + // The task is only created on the CPU core that Bluetooth is working on, + // preventing the sending task from using the un-updated Bluetooth state on another CPU. + xTaskCreatePinnedToCore(&throughput_server_task, "throughput_server_task", 4096, NULL, 15, NULL, BLUETOOTH_TASK_PINNED_TO_CORE); #if (CONFIG_GATTS_NOTIFY_THROUGHPUT) - gatts_semaphore = xSemaphoreCreateMutex(); + gatts_semaphore = xSemaphoreCreateBinary(); if (!gatts_semaphore) { ESP_LOGE(GATTS_TAG, "%s, init fail, the gatts semaphore create fail.", __func__); return; diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 4106dd24e..04415327c 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -86,6 +86,7 @@ const int CONNECTED_BIT = BIT0; /* store the station info for send back to phone */ static bool gl_sta_connected = false; +static bool ble_is_connected = false; static uint8_t gl_sta_bssid[6]; static uint8_t gl_sta_ssid[32]; static int gl_sta_ssid_len; @@ -112,7 +113,11 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) info.sta_bssid_set = true; info.sta_ssid = gl_sta_ssid; info.sta_ssid_len = gl_sta_ssid_len; - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + if (ble_is_connected == true) { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); + } else { + BLUFI_INFO("BLUFI BLE is not connected yet\n"); + } break; } case SYSTEM_EVENT_STA_CONNECTED: @@ -135,10 +140,14 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) esp_wifi_get_mode(&mode); /* TODO: get config or information of softap, then set to report extra_info */ - if (gl_sta_connected) { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); + if (ble_is_connected == true) { + if (gl_sta_connected) { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); + } else { + esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + } } else { - esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); + BLUFI_INFO("BLUFI BLE is not connected yet\n"); } break; case SYSTEM_EVENT_SCAN_DONE: { @@ -167,7 +176,13 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event) blufi_ap_list[i].rssi = ap_list[i].rssi; memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid)); } - esp_blufi_send_wifi_list(apCount, blufi_ap_list); + + if (ble_is_connected == true) { + esp_blufi_send_wifi_list(apCount, blufi_ap_list); + } else { + BLUFI_INFO("BLUFI BLE is not connected yet\n"); + } + esp_wifi_scan_stop(); free(ap_list); free(blufi_ap_list); @@ -186,7 +201,6 @@ static void initialise_wifi(void) ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); - ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_start() ); } @@ -215,6 +229,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; case ESP_BLUFI_EVENT_BLE_CONNECT: BLUFI_INFO("BLUFI ble connect\n"); + ble_is_connected = true; server_if = param->connect.server_if; conn_id = param->connect.conn_id; esp_ble_gap_stop_advertising(); @@ -222,6 +237,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para break; case ESP_BLUFI_EVENT_BLE_DISCONNECT: BLUFI_INFO("BLUFI ble disconnect\n"); + ble_is_connected = false; blufi_security_deinit(); esp_ble_gap_start_advertising(&example_adv_params); break; @@ -251,7 +267,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para esp_wifi_get_mode(&mode); - if (gl_sta_connected ) { + if (gl_sta_connected) { memset(&info, 0, sizeof(esp_blufi_extra_info_t)); memcpy(info.sta_bssid, gl_sta_bssid, 6); info.sta_bssid_set = true; diff --git a/examples/bluetooth/esp_ble_mesh/README.md b/examples/bluetooth/esp_ble_mesh/README.md new file mode 100644 index 000000000..50659c91e --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/README.md @@ -0,0 +1,94 @@ +# ESP-BLE-MESH Examples + +[ESP-BLE-MESH]($IDF_PATH/components/bt/esp_ble_mesh/) is the official Bluetooth® Mesh stack of Espressif Systems. We will provide long-term support for new features, performance optimization, etc. + +Please help note that breaking changes may be introduced into ESP-BLE-MESH on [minor IDF versions](https://docs.espressif.com/projects/esp-idf/en/latest/versions.html). + +Note: To use examples in this directory, you need to have Bluetooth enabled in configuration, and either Bluedroid or NimBLE can be selected as the host stack. + +# Example Layout + +This directory includes examples to demonstrate ESP-BLE-MESH functionality based on [Zephyr Bluetooth Mesh stack](https://github.com/zephyrproject-rtos/zephyr/tree/master/subsys/bluetooth/mesh). + +## ble_mesh_console + +This example demonstrates how ESP-BLE-MESH uses Console for message transmitting/receiving tests. + +#### ble_mesh_node + +This example shows how ESP32 acts as a BLE Mesh Node and sends vendor messages for testing. + +See [ble_mesh_node](ble_mesh_console/ble_mesh_node) folder for more details. + +#### ble_mesh_provisioner + +This example shows how ESP32 acts as a BLE Mesh Provisioner and sends vendor messages for testing. + +See [ble_mesh_provisioner](ble_mesh_console/ble_mesh_provisioner) folder for more details. + +## ble_mesh_fast_provision + +This example illustrates the solution of ESP-BLE-MESH Fast Provisioning. + +#### fast_prov_client + +This example shows how ESP32, acting as a BLE Mesh Fast Provisioning Client, provisions other unprovisioned devices and then controls the nodes. + +See [fast_prov_client](ble_mesh_fast_provision/fast_prov_client) folder for more details. + +#### fast_prov_server + +This example illustrates the process that: +1. ESP32 as a BLE Mesh Fast Provisioning Server is provisioned into a node; +2. ESP32 as a Temporary Provisioner provisions other unprovisioned devices. + +See [fast_prov_server](ble_mesh_fast_provision/fast_prov_server) folder for more details. + +## ble_mesh_node + +This example demonstrates how ESP32 acts as a BLE Mesh node with Generic OnOff Server model or Generic OnOff Client model on board. + +#### onoff_client + +This example shows how ESP32 acts as a BLE Mesh Node with Generic OnOff Client model in the Primary Element. + +See [onoff_client](ble_mesh_node/onoff_client) folder for more details. + +#### onoff_server + +This example shows how ESP32 acts as a BLE Mesh Node with only Generic OnOff Server model in the Primary Element. + +See [onoff_server](ble_mesh_node/onoff_server) folder for more details. + +## ble_mesh_provisioner + +This example shows how ESP32 acts as a BLE Mesh Provisioner and provisions other unprovisioned devices. + +See [ble_mesh_provisioner](ble_mesh_provisioner) folder for more details. + +## ble_mesh_vendor_model + +This example demonstrates how ESP32 acts as a BLE Mesh Provisioner with vendor client model or as a BLE Mesh node with vendor server model. + +#### vendor_client + +This example shows how ESP32 acts as a BLE Mesh Provisioner with a vendor client model in the Primary Element. + +See [vendor_client](ble_mesh_vendor_model/vendor_client) folder for more details. + +#### vendor_server + +This example shows how ESP32 acts as a BLE Mesh Node with a vendor server model in the Primary Element. + +See [vendor_server](ble_mesh_vendor_model/vendor_server) folder for more details. + +## ble_mesh_wifi_coexist + +This example shows how ESP32 acts as a BLE Mesh Fast Provisioning Server and coexists with Wi-Fi iperf functionality. + +See [ble_mesh_wifi_coexist](ble_mesh_wifi_coexist) folder for more details. + +# More + +See the [README.md](../../README.md) file in the upper level [examples](../../) directory for more information about examples. + diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/CMakeLists.txt new file mode 100644 index 000000000..74361e558 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(ble_mesh_console_node) diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/Makefile b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/Makefile new file mode 100644 index 000000000..209aa2e0c --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/Makefile @@ -0,0 +1,10 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := ble_mesh_console_node + +COMPONENT_ADD_INCLUDEDIRS := components/include + +include $(IDF_PATH)/make/project.mk diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/README.md b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/README.md new file mode 100644 index 000000000..0bb260c81 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/README.md @@ -0,0 +1,9 @@ +# ble mesh node console demo +## Introduction +This demo implements ble mesh node basic features.Based on this demo, node can be scaned and proved by provisioner, reply get/set message to provisioner. + +Demo steps: +1. Build the ble mesh node console demo with sdkconfig.default +2. register node and set oob info, load model to init ble mesh node +3. enable bearer, so that it can be scaned and provisioned by provisioner + diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt new file mode 100644 index 000000000..40921b35e --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/CMakeLists.txt @@ -0,0 +1,13 @@ +set(COMPONENT_SRCS "ble_mesh_adapter.c" + "ble_mesh_cfg_srv_model.c" + "ble_mesh_console_lib.c" + "ble_mesh_console_main.c" + "ble_mesh_console_system.c" + "ble_mesh_register_node_cmd.c" + "ble_mesh_register_server_cmd.c" + "ble_mesh_reg_gen_onoff_client_cmd.c" + "register_bluetooth.c") + +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c new file mode 100644 index 000000000..41e1fc611 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.c @@ -0,0 +1,165 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "ble_mesh_adapter.h" + +esp_ble_mesh_model_t *ble_mesh_get_model(uint16_t model_id) +{ + esp_ble_mesh_model_t *model = NULL; + + switch (model_id) { + case ESP_BLE_MESH_MODEL_ID_CONFIG_SRV: + model = &config_server_models[0]; + break; + case ESP_BLE_MESH_MODEL_ID_CONFIG_CLI: + model = &config_client_models[0]; + break; + case ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: + model = &gen_onoff_srv_models[1]; + break; +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + // as server + case ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: + model = &gen_onoff_cli_models[2]; + break; +#endif + case ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI: + model = &test_perf_cli_models[0]; + break; + case ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_SRV: + model = &test_perf_srv_models[0]; + break; + } + return model; +} + +esp_ble_mesh_comp_t *ble_mesh_get_component(uint16_t model_id) +{ + esp_ble_mesh_comp_t *comp = NULL; + + switch (model_id) { + case ESP_BLE_MESH_MODEL_ID_CONFIG_SRV: + comp = &config_server_comp; + break; + case ESP_BLE_MESH_MODEL_ID_CONFIG_CLI: + comp = &config_client_comp; + break; + case ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: + comp = &gen_onoff_srv_comp; + break; +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + case ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: + comp = &gen_onoff_cli_comp; + break; +#endif + case ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI: + comp = &test_perf_cli_comp; + break; + case ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_SRV: + comp = &test_perf_srv_comp; + break; + } + return comp; +} + +void ble_mesh_node_init(void) +{ + uint16_t i; + + for (i = 0; i < NODE_MAX_GROUP_CONFIG; i++) { + ble_mesh_node_prestore_params[i].net_idx = 0xFFFF; + ble_mesh_node_prestore_params[i].unicast_addr = 0xFFFF; + } + + ble_mesh_node_sema = xSemaphoreCreateMutex(); + if (!ble_mesh_node_sema) { + ESP_LOGE(TAG, "%s init fail, mesh node semaphore create fail", __func__); + } +} + +void ble_mesh_set_node_prestore_params(uint16_t netkey_index, uint16_t unicast_addr) +{ + uint16_t i; + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); + for (i = 0; i < NODE_MAX_GROUP_CONFIG; i++) { + if (ble_mesh_node_prestore_params[i].net_idx != 0xFFFF && ble_mesh_node_prestore_params[i].unicast_addr != 0xFFFF) { + ble_mesh_node_prestore_params[i].net_idx = netkey_index; + ble_mesh_node_prestore_params[i].unicast_addr = unicast_addr; + } + } + xSemaphoreGive(ble_mesh_node_sema); +} + +void ble_mesh_node_statistics_get(void) +{ + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); + ESP_LOGI(TAG, "statistics:%d,%d\n", ble_mesh_node_statistics.statistics, ble_mesh_node_statistics.package_num); + xSemaphoreGive(ble_mesh_node_sema); +} + +int ble_mesh_node_statistics_accumultate(uint8_t *data, uint32_t value, uint16_t type) +{ + uint16_t i; + uint16_t sequence_num = (data[0] << 8) | data[1]; + + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); + for (i = 0; i < ble_mesh_node_statistics.total_package_num; i++) { + if (ble_mesh_node_statistics.package_index[i] == sequence_num) { + xSemaphoreGive(ble_mesh_node_sema); + return 1; + } + } + + // package type wrong + if (data[2] != type) { + xSemaphoreGive(ble_mesh_node_sema); + return 1; + } + + for (i = 0; i < ble_mesh_node_statistics.total_package_num; i++) { + if (ble_mesh_node_statistics.package_index[i] == 0) { + ble_mesh_node_statistics.package_index[i] = sequence_num; + ble_mesh_node_statistics.package_num += 1; + ble_mesh_node_statistics.statistics += value; + break; + } + } + xSemaphoreGive(ble_mesh_node_sema); + return 0; +} + +int ble_mesh_node_statistics_init(uint16_t package_num) +{ + uint16_t i; + + ble_mesh_node_statistics.package_index = malloc(sizeof(uint16_t) * package_num); + ble_mesh_node_statistics.total_package_num = package_num; + if (ble_mesh_node_statistics.package_index == NULL) { + ESP_LOGE(TAG, " %s, %d malloc fail\n", __func__, __LINE__); + return 1; + } + + ble_mesh_node_statistics.package_num = 0; + for (i = 0; i < package_num; i++) { + ble_mesh_node_statistics.package_index[i] = 0; + } + return 0; +} + +void ble_mesh_node_statistics_destroy(void) +{ + if (ble_mesh_node_statistics.package_index != NULL) { + free(ble_mesh_node_statistics.package_index); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.h new file mode 100644 index 000000000..d7feee5fe --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_adapter.h @@ -0,0 +1,97 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_ADAPTER_H_ +#define _BLE_MESH_ADAPTER_H_ + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +#include "ble_mesh_console_lib.h" +#include "ble_mesh_cfg_srv_model.h" + +#define TAG "ble_mesh_node_console" + +typedef enum { + VENDOR_MODEL_PERF_OPERATION_TYPE_GET = 1, + VENDOR_MODEL_PERF_OPERATION_TYPE_SET, + VENDOR_MODEL_PERF_OPERATION_TYPE_SET_UNACK +} ble_mesh_perf_operation_type; + +typedef struct { + uint8_t current; + uint8_t previous; + char *name; +} ble_mesh_node_status; + +typedef struct { + uint32_t statistics; + uint32_t package_num; + uint16_t *package_index; + uint32_t total_package_num; +} ble_mesh_node_statistics_t; +ble_mesh_node_statistics_t ble_mesh_node_statistics; + +extern SemaphoreHandle_t ble_mesh_node_sema; + +#define arg_int_to_value(src_msg, dst_msg, message) do { \ + if (src_msg->count != 0) {\ + ESP_LOGD(TAG, "\n%s, %s\n", __func__, message);\ + dst_msg = src_msg->ival[0];\ + } \ +} while(0) \ + +#define ble_mesh_node_get_value(index, key, value) do { \ + uint16_t _index = 0; \ + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); \ + for (_index = 0; _index < NODE_MAX_GROUP_CONFIG; _index) { \ + if (node_set_prestore_params[_index].key == value) { \ + break; \ + } \ + } \ + index = _index; \ + xSemaphoreGive(ble_mesh_node_sema); \ +} while(0) \ + +#define ble_mesh_node_set_state(status) do { \ + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); \ + node_status.previous = node_status.current; \ + node_status.current = status; \ + xSemaphoreGive(ble_mesh_node_sema); \ +}while(0) \ + +#define ble_mesh_node_get_state(status) do { \ + xSemaphoreTake(ble_mesh_node_sema, portMAX_DELAY); \ + status = node_status.current; \ + xSemaphoreGive(ble_mesh_node_sema); \ +}while(0) \ + +#define ble_mesh_callback_check_err_code(err_code, message) do { \ + if (err_code == ESP_OK) { \ + ESP_LOGI(TAG, "%s,OK\n", message); \ + } else { \ + ESP_LOGE(TAG, "%s,Fail,%d\n", message, err_code); \ + } \ +}while(0) \ + +void ble_mesh_node_init(void); +void ble_mesh_set_node_prestore_params(uint16_t netkey_index, uint16_t unicast_addr); +esp_ble_mesh_model_t *ble_mesh_get_model(uint16_t model_id); +esp_ble_mesh_comp_t *ble_mesh_get_component(uint16_t model_id); +void ble_mesh_node_statistics_get(void); +int ble_mesh_node_statistics_accumultate(uint8_t *data, uint32_t value, uint16_t type); +int ble_mesh_node_statistics_init(uint16_t package_num); +void ble_mesh_node_statistics_destroy(void); + +#endif //_BLE_MESH_ADAOTER_H_ diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c new file mode 100644 index 000000000..69f37a3bd --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.c @@ -0,0 +1,207 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "ble_mesh_cfg_srv_model.h" +#include "esp_ble_mesh_generic_model_api.h" +uint8_t dev_uuid[16] = {0xdd, 0xdd}; + +#if CONFIG_BLE_MESH_NODE +esp_ble_mesh_prov_t prov = { + .uuid = dev_uuid, +}; +#endif //CONFIG_BLE_MESH_NODE + +#if CONFIG_BLE_MESH_PROVISIONER +esp_ble_mesh_prov_t prov = { + .prov_uuid = dev_uuid, + .prov_unicast_addr = 0x0001, + .prov_start_address = 0x0005, + .prov_attention = 0x00, + .prov_algorithm = 0x00, + .prov_pub_key_oob = 0x00, + .prov_pub_key_oob_cb = NULL, + .prov_static_oob_val = NULL, + .prov_static_oob_len = 0x00, + .prov_input_num = NULL, + .prov_output_num = NULL, + .flags = 0x00, + .iv_index = 0x00, +}; +#endif //CONFIG_BLE_MESH_PROVISIONER + +esp_ble_mesh_model_pub_t vendor_model_pub_config; +ESP_BLE_MESH_MODEL_PUB_DEFINE(model_pub_config, 2 + 1, ROLE_NODE); + +// configure server module +esp_ble_mesh_cfg_srv_t cfg_srv = { + .relay = ESP_BLE_MESH_RELAY_ENABLED, + .beacon = ESP_BLE_MESH_BEACON_ENABLED, +#if defined(CONFIG_BLE_MESH_FRIEND) + .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, +#else + .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, +#endif +#if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, +#else + .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, +#endif + .default_ttl = 7, + + /* 3 transmissions with 20ms interval */ + .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), + .relay_retransmit = ESP_BLE_MESH_TRANSMIT(0, 20), +}; + +esp_ble_mesh_model_t config_server_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), +}; + +esp_ble_mesh_elem_t config_server_elements[] = { + ESP_BLE_MESH_ELEMENT(0, config_server_models, ESP_BLE_MESH_MODEL_NONE), +}; + +esp_ble_mesh_comp_t config_server_comp = { + .cid = CID_ESP, + .elements = config_server_elements, + .element_count = ARRAY_SIZE(config_server_elements), +}; + +// config client model +esp_ble_mesh_model_t config_client_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), + ESP_BLE_MESH_MODEL_CFG_CLI(&cfg_cli), +}; + +esp_ble_mesh_elem_t config_client_elements[] = { + ESP_BLE_MESH_ELEMENT(0, config_client_models, ESP_BLE_MESH_MODEL_NONE), +}; + +esp_ble_mesh_comp_t config_client_comp = { + .cid = CID_ESP, + .elements = config_client_elements, + .element_count = ARRAY_SIZE(config_client_elements), +}; + +// configure special module +ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_pub_0, 2 + 3, ROLE_NODE); +static esp_ble_mesh_gen_onoff_srv_t onoff_server = { + .rsp_ctrl.get_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, + .rsp_ctrl.set_auto_rsp = ESP_BLE_MESH_SERVER_RSP_BY_APP, +}; + +esp_ble_mesh_model_t gen_onoff_srv_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server), +}; + +esp_ble_mesh_elem_t gen_onoff_srv_elements[] = { + ESP_BLE_MESH_ELEMENT(0, gen_onoff_srv_models, ESP_BLE_MESH_MODEL_NONE), +}; + +esp_ble_mesh_comp_t gen_onoff_srv_comp = { + .cid = CID_ESP, + .elements = gen_onoff_srv_elements, + .element_count = ARRAY_SIZE(gen_onoff_srv_elements), +}; + +// config generic onoff client +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + +esp_ble_mesh_client_t gen_onoff_cli; + +esp_ble_mesh_model_t gen_onoff_cli_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), + ESP_BLE_MESH_MODEL_CFG_CLI(&cfg_cli), + ESP_BLE_MESH_MODEL_GEN_ONOFF_SRV(&onoff_pub_0, &onoff_server), + ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(&model_pub_config, &gen_onoff_cli), +}; + +esp_ble_mesh_elem_t gen_onoff_cli_elements[] = { + ESP_BLE_MESH_ELEMENT(0, gen_onoff_cli_models, ESP_BLE_MESH_MODEL_NONE), +}; + +esp_ble_mesh_comp_t gen_onoff_cli_comp = { + .cid = CID_ESP, + .elements = gen_onoff_cli_elements, + .element_count = ARRAY_SIZE(gen_onoff_cli_elements), +}; +#endif //CONFIG_BLE_MESH_GENERIC_ONOFF_CLI + +//CONFIG VENDOR MODEL TEST PERFORMANCE +#define ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_SRV 0x2000 +#define ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI 0x2001 + +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_GET ESP_BLE_MESH_MODEL_OP_3(0x01, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET ESP_BLE_MESH_MODEL_OP_3(0x02, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET_UNACK ESP_BLE_MESH_MODEL_OP_3(0x03, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS ESP_BLE_MESH_MODEL_OP_3(0x04, CID_ESP) + +esp_ble_mesh_client_op_pair_t test_perf_cli_op_pair[] = { + {ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_GET, ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS}, + {ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET, ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS}, + {ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET_UNACK, ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS}, +}; + +esp_ble_mesh_client_t test_perf_cli = { + .op_pair_size = ARRAY_SIZE(test_perf_cli_op_pair), + .op_pair = test_perf_cli_op_pair, +}; + +esp_ble_mesh_model_op_t test_perf_srv_op[] = { + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_GET, 1), + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET, 1), + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET_UNACK, 1), + ESP_BLE_MESH_MODEL_OP_END, +}; + +esp_ble_mesh_model_op_t test_perf_cli_op[] = { + ESP_BLE_MESH_MODEL_OP(ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS, 1), + ESP_BLE_MESH_MODEL_OP_END, +}; + +esp_ble_mesh_model_t config_models[] = { + ESP_BLE_MESH_MODEL_CFG_SRV(&cfg_srv), + ESP_BLE_MESH_MODEL_CFG_CLI(&cfg_cli), +}; + +esp_ble_mesh_model_t test_perf_cli_models[] = { + ESP_BLE_MESH_VENDOR_MODEL(CID_ESP, ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI, + test_perf_cli_op, &vendor_model_pub_config, &test_perf_cli), +}; + +esp_ble_mesh_elem_t test_perf_cli_elements[] = { + ESP_BLE_MESH_ELEMENT(0, config_models, test_perf_cli_models), +}; + +esp_ble_mesh_comp_t test_perf_cli_comp = { + .cid = CID_ESP, + .elements = test_perf_cli_elements, + .element_count = ARRAY_SIZE(test_perf_cli_elements), +}; + +esp_ble_mesh_model_t test_perf_srv_models[] = { + ESP_BLE_MESH_VENDOR_MODEL(CID_ESP, ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_SRV, + test_perf_srv_op, NULL, NULL), +}; + +esp_ble_mesh_elem_t test_perf_srv_elements[] = { + ESP_BLE_MESH_ELEMENT(0, config_models, test_perf_srv_models), +}; + +esp_ble_mesh_comp_t test_perf_srv_comp = { + .cid = CID_ESP, + .elements = test_perf_srv_elements, + .element_count = ARRAY_SIZE(test_perf_srv_elements), +}; diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.h new file mode 100644 index 000000000..eba953c4e --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_cfg_srv_model.h @@ -0,0 +1,107 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_CFG_SRV_MODEL_H_ +#define _BLE_MESH_CFG_SRV_MODEL_H_ + +#include "esp_ble_mesh_defs.h" +#include "esp_ble_mesh_config_model_api.h" + +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) +#include "esp_ble_mesh_generic_model_api.h" +#endif //CONFIG_BLE_MESH_GENERIC_ONOFF_CLI + +#define NODE_MAX_GROUP_CONFIG 3 +#define CID_ESP 0x02C4 + +extern uint8_t dev_uuid[16]; + +typedef struct { + uint16_t net_idx; + uint16_t unicast_addr; +} ble_mesh_node_config_params; +ble_mesh_node_config_params ble_mesh_node_prestore_params[NODE_MAX_GROUP_CONFIG]; + +extern esp_ble_mesh_prov_t prov; + +extern esp_ble_mesh_model_pub_t vendor_model_pub_config; + +// configure server module +extern esp_ble_mesh_cfg_srv_t cfg_srv; + +extern esp_ble_mesh_model_t config_server_models[]; + +extern esp_ble_mesh_elem_t config_server_elements[]; + +extern esp_ble_mesh_comp_t config_server_comp; + +// config client model +esp_ble_mesh_client_t cfg_cli; +extern esp_ble_mesh_model_t config_client_models[]; + +extern esp_ble_mesh_elem_t config_client_elements[]; + +extern esp_ble_mesh_comp_t config_client_comp; + +// configure special module +extern esp_ble_mesh_model_op_t gen_onoff_srv_model_op_config[]; + +extern esp_ble_mesh_model_t gen_onoff_srv_models[]; + +extern esp_ble_mesh_elem_t gen_onoff_srv_elements[]; + +extern esp_ble_mesh_comp_t gen_onoff_srv_comp; + +// config generic onoff client +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + +extern esp_ble_mesh_client_t gen_onoff_cli; + +extern esp_ble_mesh_model_t gen_onoff_cli_models[]; + +extern esp_ble_mesh_elem_t gen_onoff_cli_elements[]; + +extern esp_ble_mesh_comp_t gen_onoff_cli_comp; +#endif //CONFIG_BLE_MESH_GENERIC_ONOFF_CLI + +//CONFIG VENDOR MODEL TEST PERFORMANCE +#define ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_SRV 0x2000 +#define ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI 0x2001 + +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_GET ESP_BLE_MESH_MODEL_OP_3(0x01, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET ESP_BLE_MESH_MODEL_OP_3(0x02, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET_UNACK ESP_BLE_MESH_MODEL_OP_3(0x03, CID_ESP) +#define ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_STATUS ESP_BLE_MESH_MODEL_OP_3(0x04, CID_ESP) + +extern esp_ble_mesh_client_t test_perf_cli; + +extern esp_ble_mesh_model_op_t test_perf_srv_op[]; + +extern esp_ble_mesh_model_op_t test_perf_cli_op[]; + +extern esp_ble_mesh_model_t config_models[]; + +extern esp_ble_mesh_model_t test_perf_cli_models[]; + +extern esp_ble_mesh_elem_t test_perf_cli_elements[]; + +extern esp_ble_mesh_comp_t test_perf_cli_comp; + +extern esp_ble_mesh_model_t test_perf_srv_models[]; + +extern esp_ble_mesh_elem_t test_perf_srv_elements[]; + +extern esp_ble_mesh_comp_t test_perf_srv_comp; + +#endif //_BLE_MESH_CFG_SRV_MODEL_H_ diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h new file mode 100644 index 000000000..a18a4e7e4 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_decl.h @@ -0,0 +1,33 @@ +/* Console example — declarations of command registration functions. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#pragma once + +#include "esp_ble_mesh_defs.h" + +// Register system functions +void register_system(void); + +// Register blutooth +void register_bluetooth(void); + +// Register mesh node cmd +void ble_mesh_register_mesh_node(void); + +// Register mesh config server and generic server operation cmd +void ble_mesh_register_server(void); + +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) +// Register mesh client operation cmd +void ble_mesh_register_gen_onoff_client(void); +#endif + +#if (CONFIG_BLE_MESH_CFG_CLI) +// Register mesh config client operation cmd +void ble_mesh_register_configuration_client_model(void); +#endif diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c new file mode 100644 index 000000000..7efe28c41 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.c @@ -0,0 +1,132 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "ble_mesh_console_lib.h" + +static int hex2num(char c); +static int hex2byte(const char *hex); + +static int hex2num(char c) +{ + if (c >= '0' && c <= '9') { + return c - '0'; + } + if (c >= 'a' && c <= 'f') { + return c - 'a' + 10; + } + if (c >= 'A' && c <= 'F') { + return c - 'A' + 10; + } + return -1; +} + +static int hex2byte(const char *hex) +{ + int a, b; + a = hex2num(*hex++); + if (a < 0) { + return -1; + } + b = hex2num(*hex++); + if (b < 0) { + return -1; + } + return (a << 4) | b; +} + +int hexstr_2_bin(const char *hex, uint8_t *buf, uint32_t len) +{ + uint32_t i; + int a; + const char *ipos = hex; + uint8_t *opos = buf; + + for (i = 0; i < len; i++) { + a = hex2byte(ipos); + if (a < 0) { + return -1; + } + *opos ++ = a; + ipos += 2; + } + return 0; +} + +int get_value_string(char *value_in, char *buf) +{ + int result = -1; + uint8_t loop = 0; + uint16_t length = strlen(value_in); + + // address string, need sepcial test + for (loop = 0; loop < 17 ; loop++) { + if (loop % 3 == 2) { + if (value_in[loop] == ':') { + return result; + } + } + } + + if (length > 2) { + if (value_in[0] == '0' && value_in[1] == 'x') { + buf[(length - 2) / 2] = 0; + result = hexstr_2_bin(&value_in[2], (uint8_t *)buf, (length - 2) / 2); + length = (length - 2) / 2; + } else { + strcpy(buf, value_in); + result = 0; + } + } else { + strcpy(buf, value_in); + result = 0; + } + return result; +} + +bool str_2_mac(uint8_t *str, uint8_t *dest) +{ + uint8_t loop = 0; + uint8_t tmp = 0; + uint8_t *src_p = str; + + if (strlen((char *)src_p) != 17) { // must be like 12:34:56:78:90:AB + return false; + } + + for (loop = 0; loop < 17 ; loop++) { + if (loop % 3 == 2) { + if (src_p[loop] != ':') { + return false; + } + continue; + } + + if ((src_p[loop] >= '0') && (src_p[loop] <= '9')) { + tmp = tmp * 16 + src_p[loop] - '0'; + } else if ((src_p[loop] >= 'A') && (src_p[loop] <= 'F')) { + tmp = tmp * 16 + src_p[loop] - 'A' + 10; + } else if ((src_p[loop] >= 'a') && (src_p[loop] <= 'f')) { + tmp = tmp * 16 + src_p[loop] - 'a' + 10; + } else { + return false; + } + + if (loop % 3 == 1) { + *dest++ = tmp; + tmp = 0; + } + } + + return true; +} \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.h b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.h new file mode 100644 index 000000000..f0e2e77c9 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_lib.h @@ -0,0 +1,31 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _BLE_MESH_CONSOLE_LIB_H_ +#define _BLE_MESH_CONSOLE_LIB_H_ + +#include +#include + +#include "esp_system.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" + +bool str_2_mac(uint8_t *str, uint8_t *dest); +int hexstr_2_bin(const char *hex, uint8_t *buf, uint32_t len); +int get_value_string(char *value_in, char *buf); + +#endif //_BLE_MESH_CONSOLE_LIB_H_ \ No newline at end of file diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c new file mode 100644 index 000000000..3e961a4f2 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_main.c @@ -0,0 +1,220 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "esp_system.h" +#include "esp_log.h" +#include "esp_vfs_dev.h" +#include "driver/uart.h" +#include "nvs.h" +#include "nvs_flash.h" + +#include "esp_bt.h" +#include "esp_bt_main.h" + +#include "esp_console.h" +#include "linenoise/linenoise.h" +#include "argtable3/argtable3.h" + +#include "ble_mesh_console_decl.h" + +#if CONFIG_STORE_HISTORY + +#define MOUNT_PATH "/data" +#define HISTORY_PATH MOUNT_PATH "/history.txt" + +static void initialize_filesystem(void) +{ + static wl_handle_t wl_handle; + const esp_vfs_fat_mount_config_t mount_config = { + .max_files = 4, + .format_if_mount_failed = true + }; + esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle); + if (err != ESP_OK) { + printf("Failed to mount FATFS (0x%x)", err); + return; + } +} +#endif // CONFIG_STORE_HISTORY + +static void initialize_console(void) +{ + /* Disable buffering on stdin and stdout */ + setvbuf(stdin, NULL, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); + + /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */ + esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); + /* Move the caret to the beginning of the next line on '\n' */ + esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); + + /* Install UART driver for interrupt-driven reads and writes */ + ESP_ERROR_CHECK( uart_driver_install(CONFIG_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0) ); + + /* Tell VFS to use UART driver */ + esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); + + /* Initialize the console */ + esp_console_config_t console_config = { + .max_cmdline_args = 20, + .max_cmdline_length = 256, +#if CONFIG_LOG_COLORS + .hint_color = atoi(LOG_COLOR_CYAN) +#endif + }; + ESP_ERROR_CHECK( esp_console_init(&console_config) ); + + /* Configure linenoise line completion library */ + /* Enable multiline editing. If not set, long commands will scroll within + * single line. + */ + linenoiseSetMultiLine(1); + + /* Tell linenoise where to get command completions and hints */ + linenoiseSetCompletionCallback(&esp_console_get_completion); + linenoiseSetHintsCallback((linenoiseHintsCallback *) &esp_console_get_hint); + + /* Set command history size */ + linenoiseHistorySetMaxLen(100); + +#if CONFIG_STORE_HISTORY + /* Load command history from filesystem */ + linenoiseHistoryLoad(HISTORY_PATH); +#endif +} + + +esp_err_t bluetooth_init(void) +{ + esp_err_t ret; + + esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + ret = esp_bt_controller_init(&bt_cfg); + if (ret) { + printf("%s initialize controller failed\n", __func__); + return ret; + } + + ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); + if (ret) { + printf("%s enable controller failed\n", __func__); + return ret; + } + ret = esp_bluedroid_init(); + if (ret) { + printf("%s init bluetooth failed\n", __func__); + return ret; + } + ret = esp_bluedroid_enable(); + if (ret) { + printf("%s enable bluetooth failed\n", __func__); + return ret; + } + + esp_log_level_set("*", ESP_LOG_ERROR); + esp_log_level_set("ble_mesh_node_console", ESP_LOG_INFO); + return ret; +} + +void app_main(void) +{ + esp_err_t res; + + nvs_flash_init(); + + // init and enable bluetooth + res = bluetooth_init(); + if (res) { + printf("esp32_bluetooth_init failed (ret %d)", res); + } + +#if CONFIG_STORE_HISTORY + initialize_filesystem(); +#endif + + initialize_console(); + + /* Register commands */ + esp_console_register_help_command(); + register_system(); + register_bluetooth(); + ble_mesh_register_mesh_node(); + ble_mesh_register_server(); +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) + ble_mesh_register_gen_onoff_client(); +#endif + + /* Prompt to be printed before each line. + * This can be customized, made dynamic, etc. + */ + const char *prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR; + + printf("\n" + "This is an example of ESP-IDF console component.\n" + "Type 'help' to get the list of commands.\n" + "Use UP/DOWN arrows to navigate through command history.\n" + "Press TAB when typing command name to auto-complete.\n"); + + /* Figure out if the terminal supports escape sequences */ + int probe_status = linenoiseProbe(); + if (probe_status) { /* zero indicates success */ + printf("\n" + "Your terminal application does not support escape sequences.\n" + "Line editing and history features are disabled.\n" + "On Windows, try using Putty instead.\n"); + linenoiseSetDumbMode(1); +#if CONFIG_LOG_COLORS + /* Since the terminal doesn't support escape sequences, + * don't use color codes in the prompt. + */ + prompt = "esp32> "; +#endif //CONFIG_LOG_COLORS + } + + /* Main loop */ + while (true) { + /* Get a line using linenoise. + * The line is returned when ENTER is pressed. + */ + char *line = linenoise(prompt); + if (line == NULL) { /* Ignore empty lines */ + continue; + } + /* Add the command to the history */ + linenoiseHistoryAdd(line); +#if CONFIG_STORE_HISTORY + /* Save command history to filesystem */ + linenoiseHistorySave(HISTORY_PATH); +#endif + + /* Try to run the command */ + int ret; + esp_err_t err = esp_console_run(line, &ret); + if (err == ESP_ERR_NOT_FOUND) { + printf("Unrecognized command\n"); + } else if (err == ESP_ERR_INVALID_ARG) { + // command was empty + } else if (err == ESP_OK && ret != ESP_OK) { + printf("\nCommand returned non-zero error code: 0x%x\n", ret); + } else if (err != ESP_OK) { + printf("Internal error: 0x%x\n", err); + } + /* linenoise allocates line buffer on the heap, so need to free it */ + linenoiseFree(line); + } +} diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_system.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_system.c new file mode 100644 index 000000000..88c0a6a0a --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_console_system.c @@ -0,0 +1,183 @@ +/* Console example — various system commands + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include +#include +#include + +#include "esp_log.h" +#include "esp_console.h" +#include "esp_system.h" +#include "esp_sleep.h" +#include "driver/rtc_io.h" +#include "soc/rtc_cntl_reg.h" +#include "argtable3/argtable3.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "ble_mesh_console_decl.h" + +#if CONFIG_IDF_CMAKE +#define CONFIG_ESPTOOLPY_PORT "Which is choosen by Users for CMake" +#endif + +static void register_free(void); +static void register_restart(void); +static void register_make(void); + +void register_system(void) +{ + register_free(); + register_restart(); + register_make(); +} + +/** 'restart' command restarts the program */ + +static int restart(int argc, char **argv) +{ + printf("%s, %s", __func__, "Restarting"); + esp_restart(); +} + +static void register_restart(void) +{ + const esp_console_cmd_t cmd = { + .command = "restart", + .help = "Restart the program", + .hint = NULL, + .func = &restart, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); +} + +/** 'free' command prints available heap memory */ + +static int free_mem(int argc, char **argv) +{ + printf("%d\n", esp_get_free_heap_size()); + return 0; +} + +static void register_free(void) +{ + const esp_console_cmd_t cmd = { + .command = "free", + .help = "Get the total size of heap memory available", + .hint = NULL, + .func = &free_mem, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); +} + +static int make(int argc, char **argv) +{ + int count = REG_READ(RTC_CNTL_STORE0_REG); + if (++count >= 3) { + printf("This is not the console you are looking for.\n"); + return 0; + } + REG_WRITE(RTC_CNTL_STORE0_REG, count); + + const char *make_output = + R"(LD build/console.elf +esptool.py v2.1-beta1 +)"; + + const char* flash_output[] = { +R"(Flashing binaries to serial port )" CONFIG_ESPTOOLPY_PORT R"( (app at offset 0x10000)... +esptool.py v2.1-beta1 +Connecting.... +)", +R"(Chip is ESP32D0WDQ6 (revision 0) +Uploading stub... +Running stub... +Stub running... +Changing baud rate to 921600 +Changed. +Configuring flash size... +Auto-detected Flash size: 4MB +Flash params set to 0x0220 +Compressed 15712 bytes to 9345... +)", +R"(Wrote 15712 bytes (9345 compressed) at 0x00001000 in 0.1 seconds (effective 1126.9 kbit/s)... +Hash of data verified. +Compressed 333776 bytes to 197830... +)", +R"(Wrote 333776 bytes (197830 compressed) at 0x00010000 in 3.3 seconds (effective 810.3 kbit/s)... +Hash of data verified. +Compressed 3072 bytes to 82... +)", +R"(Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 1588.4 kbit/s)... +Hash of data verified. +Leaving... +Hard resetting... +)" + }; + + const char* monitor_output = +R"(MONITOR +)" LOG_COLOR_W R"(--- idf_monitor on )" CONFIG_ESPTOOLPY_PORT R"( 115200 --- +--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H -- +)" LOG_RESET_COLOR; + + bool need_make = false; + bool need_flash = false; + bool need_monitor = false; + for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "all") == 0) { + need_make = true; + } else if (strcmp(argv[i], "flash") == 0) { + need_make = true; + need_flash = true; + } else if (strcmp(argv[i], "monitor") == 0) { + need_monitor = true; + } else if (argv[i][0] == '-') { + /* probably -j option */ + } else if (isdigit((int) argv[i][0])) { + /* might be an argument to -j */ + } else { + printf("make: *** No rule to make target `%s'. Stop.\n", argv[i]); + /* Technically this is an error, but let's not spoil the output */ + return 0; + } + } + if (argc == 1) { + need_make = true; + } + if (need_make) { + printf("%s", make_output); + } + if (need_flash) { + size_t n_items = sizeof(flash_output) / sizeof(flash_output[0]); + for (int i = 0; i < n_items; ++i) { + printf("%s", flash_output[i]); + vTaskDelay(200/portTICK_PERIOD_MS); + } + } + if (need_monitor) { + printf("%s", monitor_output); + esp_restart(); + } + return 0; +} + +static void register_make(void) +{ + const esp_console_cmd_t cmd = { + .command = "make", + .help = NULL, /* Do not include in 'help' output */ + .hint = "all | flash | monitor", + .func = &make, + }; + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); +} + + diff --git a/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c new file mode 100644 index 000000000..f9caa8931 --- /dev/null +++ b/examples/bluetooth/esp_ble_mesh/ble_mesh_console/ble_mesh_node/main/ble_mesh_reg_gen_onoff_client_cmd.c @@ -0,0 +1,180 @@ +// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp_timer.h" +#include "ble_mesh_adapter.h" + +#if (CONFIG_BLE_MESH_GENERIC_ONOFF_CLI) +typedef struct { + struct arg_str *action_type; + struct arg_int *op_en; + struct arg_int *unicast_address; + struct arg_int *onoff_state; + struct arg_int *trans_id; + struct arg_int *trans_time; + struct arg_int *delay; + struct arg_int *opcode; + struct arg_int *appkey_idx; + struct arg_int *role; + struct arg_int *net_idx; + struct arg_end *end; +} ble_mesh_gen_onoff_state_t; +ble_mesh_gen_onoff_state_t gen_onoff_state; + +void ble_mesh_register_gen_onoff_client_command(void); +void ble_mesh_generic_onoff_client_model_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param); + +void ble_mesh_register_gen_onoff_client(void) +{ + ble_mesh_register_gen_onoff_client_command(); +} + +void ble_mesh_generic_onoff_client_model_cb(esp_ble_mesh_generic_client_cb_event_t event, + esp_ble_mesh_generic_client_cb_param_t *param) +{ + uint32_t opcode = param->params->opcode; + + ESP_LOGD(TAG, "enter %s: event is %d, error code is %d, opcode is 0x%x\n", + __func__, event, param->error_code, opcode); + + switch (event) { + case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: { + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:GetStatus,OK,%d", param->status_cb.onoff_status.present_onoff); + } else { + ESP_LOGE(TAG, "GenOnOffClient:GetStatus,Fail,%d", param->error_code); + } + break; + default: + break; + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: { + switch (opcode) { + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:SetStatus,OK,%d", param->status_cb.onoff_status.present_onoff); + } else { + ESP_LOGE(TAG, "GenOnOffClient:SetStatus,Fail,%d", param->error_code); + } + break; + case ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK: + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:SetUNACK,OK"); + } else { + ESP_LOGE(TAG, "GenOnOffClient:SetUNACK,Fail,%d", param->error_code); + } + break; + default: + break; + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: { + if (param->error_code == ESP_OK) { + ESP_LOGI(TAG, "GenOnOffClient:Publish,OK"); + } else { + ESP_LOGE(TAG, "GenOnOffClient:Publish,Fail,%d", param->error_code); + } + break; + } + case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: + ESP_LOGE(TAG, "GenOnOffClient:TimeOut,%d", param->error_code); + break; + case ESP_BLE_MESH_GENERIC_CLIENT_EVT_MAX: + ESP_LOGE(TAG, "GenONOFFClient:InvalidEvt,%d", param->error_code); + break; + default: + break; + } + ESP_LOGD(TAG, "exit %s \n", __func__); +} + +int ble_mesh_generic_onoff_client_model(int argc, char **argv) +{ + int err = ESP_OK; + esp_ble_mesh_generic_client_set_state_t gen_client_set; + esp_ble_mesh_generic_client_get_state_t gen_client_get; + esp_ble_mesh_client_common_param_t onoff_common = { + .msg_timeout = 0, + .ctx.send_ttl = 7, + }; + + ESP_LOGD(TAG, "enter %s\n", __func__); + + int nerrors = arg_parse(argc, argv, (void **) &gen_onoff_state); + if (nerrors != 0) { + arg_print_errors(stderr, gen_onoff_state.end, argv[0]); + return 1; + } + + onoff_common.model = ble_mesh_get_model(ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI); + + arg_int_to_value(gen_onoff_state.appkey_idx, onoff_common.ctx.app_idx, "appkey_index"); + arg_int_to_value(gen_onoff_state.opcode, onoff_common.opcode, "opcode"); + arg_int_to_value(gen_onoff_state.role, onoff_common.msg_role, "role"); + arg_int_to_value(gen_onoff_state.unicast_address, onoff_common.ctx.addr, "address"); + arg_int_to_value(gen_onoff_state.net_idx, onoff_common.ctx.net_idx, "network key index"); + arg_int_to_value(gen_onoff_state.op_en, gen_client_set.onoff_set.op_en, "op_en"); + arg_int_to_value(gen_onoff_state.onoff_state, gen_client_set.onoff_set.onoff, "onoff"); + arg_int_to_value(gen_onoff_state.trans_id, gen_client_set.onoff_set.tid, "tid"); + arg_int_to_value(gen_onoff_state.trans_time, gen_client_set.onoff_set.trans_time, "trans_time"); + arg_int_to_value(gen_onoff_state.delay, gen_client_set.onoff_set.delay, "delay"); + + if (gen_onoff_state.action_type->count != 0) { + if (strcmp(gen_onoff_state.action_type->sval[0], "get") == 0) { + err = esp_ble_mesh_generic_client_get_state(&onoff_common, &gen_client_get); + } else if (strcmp(gen_onoff_state.action_type->sval[0], "set") == 0) { + err = esp_ble_mesh_generic_client_set_state(&onoff_common, &gen_client_set); + } else if (strcmp(gen_onoff_state.action_type->sval[0], "reg") == 0) { + err = esp_ble_mesh_register_generic_client_callback(ble_mesh_generic_onoff_client_model_cb); + if (err == ESP_OK) { + ESP_LOGI(TAG, "GenONOFFClient:Reg,OK"); + } + } + } + ESP_LOGD(TAG, "exit %s\n", __func__); + return err; +} + +void ble_mesh_register_gen_onoff_client_command(void) +{ + gen_onoff_state.action_type = arg_str1("z", NULL, "", "action type"); + gen_onoff_state.opcode = arg_int0("o", NULL, "", "message opcode"); + gen_onoff_state.appkey_idx = arg_int0("a", NULL, "", "appkey index"); + gen_onoff_state.role = arg_int0("r", NULL, "", "role"); + gen_onoff_state.unicast_address = arg_int0("u", NULL, "
", "unicast address"); + gen_onoff_state.net_idx = arg_int0("n", NULL, "", "network key index"); + gen_onoff_state.op_en = arg_int0("e", NULL, "", "whether optional parameters included"); + gen_onoff_state.onoff_state = arg_int0("s", NULL, "", "present onoff state"); + gen_onoff_state.trans_id = arg_int0("i", NULL, "", "transaction identifier"); + gen_onoff_state.trans_time = arg_int0("t", NULL, "