diff --git a/vendor/hellokun/demo/sparklink_car_net/img/obu_protocol.png b/vendor/hellokun/demo/sparklink_car_net/img/obu_protocol.png new file mode 100644 index 0000000000000000000000000000000000000000..958d85e26502a82e3cb2a16bd98787ecebc80850 Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/img/obu_protocol.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_client_ble_server.png b/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_client_ble_server.png new file mode 100644 index 0000000000000000000000000000000000000000..c191d71ef643666d2f106811bb32aed5b692e61f Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_client_ble_server.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_server_ble_server.png b/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_server_ble_server.png new file mode 100644 index 0000000000000000000000000000000000000000..439f64059f9ebe62bb131752a728b2b65c415ccb Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/img/obu_sle_server_ble_server.png differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\247.png" "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\247.png" new file mode 100644 index 0000000000000000000000000000000000000000..d5825c3e89ff0b59f0cb6822a496a75adcbd3f60 Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\247.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863.png" "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863.png" new file mode 100644 index 0000000000000000000000000000000000000000..9ae28b9f305f30ebdfed6d14f0babf202f57d6c8 Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863\347\274\226\350\257\221\351\200\211\351\241\271.png" "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863\347\274\226\350\257\221\351\200\211\351\241\271.png" new file mode 100644 index 0000000000000000000000000000000000000000..31f7f5d2c9e2b00760117d0c2b209aa444a9b99f Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/sle\344\270\255\346\216\2473863\347\274\226\350\257\221\351\200\211\351\241\271.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/sle\350\275\246\350\275\275\350\256\276\345\244\2073863\347\274\226\350\257\221\351\200\211\351\241\271.png" "b/vendor/hellokun/demo/sparklink_car_net/img/sle\350\275\246\350\275\275\350\256\276\345\244\2073863\347\274\226\350\257\221\351\200\211\351\241\271.png" new file mode 100644 index 0000000000000000000000000000000000000000..e944961d279c7442d75f365a73868418fa14537e Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/sle\350\275\246\350\275\275\350\256\276\345\244\2073863\347\274\226\350\257\221\351\200\211\351\241\271.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/\346\241\206\346\236\266\345\233\276.png" "b/vendor/hellokun/demo/sparklink_car_net/img/\346\241\206\346\236\266\345\233\276.png" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d6ec9537d17f68e6eb6729b361d76b4682dc1 Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/\346\241\206\346\236\266\345\233\276.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\267\257\345\215\217\345\220\214.png" "b/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\267\257\345\215\217\345\220\214.png" new file mode 100644 index 0000000000000000000000000000000000000000..5692835469b407b9ecb670e0af14adcd78ae1779 Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\267\257\345\215\217\345\220\214.png" differ diff --git "a/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\275\275\350\256\276\345\244\207.png" "b/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\275\275\350\256\276\345\244\207.png" new file mode 100644 index 0000000000000000000000000000000000000000..c0186e1364bdbd3dc9a651024ae82dd4dfb89635 Binary files /dev/null and "b/vendor/hellokun/demo/sparklink_car_net/img/\350\275\246\350\275\275\350\256\276\345\244\207.png" differ diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/CMakeLists.txt b/vendor/hellokun/demo/sparklink_car_net/obu/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9760244b2b3d3348481469de678be2109c44e987 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/CMakeLists.txt @@ -0,0 +1,33 @@ +#=============================================================================== +# @brief cmake file +# Copyright (c) CompanyNameMagicTag 2023-2023. All rights reserved. +#=============================================================================== +if(DEFINED CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) +set(SOURCES_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart_server/sle_uart_server_adv.c + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart_server/sle_uart_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server/ble_uart_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server/ble_uart_server_adv.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart.c +) +set(HEADER_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart_server + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server +) +elseif(DEFINED CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) +set(SOURCES_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart_client/sle_uart_client.c + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server/ble_uart_server.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server/ble_uart_server_adv.c + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart.c +) +set(HEADER_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/sle_uart_client + ${CMAKE_CURRENT_SOURCE_DIR}/ble_uart_server +) +endif() + +set(SOURCES "${SOURCES}" ${SOURCES_LIST} PARENT_SCOPE) +set(PUBLIC_HEADER "${PUBLIC_HEADER}" ${HEADER_LIST} PARENT_SCOPE) \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/Kconfig b/vendor/hellokun/demo/sparklink_car_net/obu/Kconfig new file mode 100644 index 0000000000000000000000000000000000000000..698507ffcb25366f6ebedf21d84719855a66d9e4 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/Kconfig @@ -0,0 +1,62 @@ +#=============================================================================== +# @brief Kconfig file. +# Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. +#=============================================================================== + +config SLE_UART_BUS + int + prompt "Set the UART BUS of the currrent sample." + default 0 + depends on SAMPLE_SUPPORT_OBU + help + This option means the UART BUS of the currrent sample. + +config UART_TXD_PIN + int + prompt "Choose UART TXD pin." + depends on SAMPLE_SUPPORT_OBU + default 19 + +config UART_RXD_PIN + int + prompt "Choose UART RXD pin." + depends on SAMPLE_SUPPORT_OBU + default 20 + +choice + prompt "Select sle uart type" + default SAMPLE_SUPPORT_SLE_UART_SERVER + config SAMPLE_SUPPORT_SLE_UART_SERVER + bool "Enable SLE UART Server sample." + config SAMPLE_SUPPORT_SLE_UART_CLIENT + bool "Enable SLE UART Client sample." +endchoice + +menu "Select SLE UART sample mode" +choice + prompt "Select sle uart sample type" + default SAMPLE_SUPPORT_NORMAL_TYPE + config SAMPLE_SUPPORT_NORMAL_TYPE + bool "Enable SLE UART normal sample." + config SAMPLE_SUPPORT_LOW_LATENCY_TYPE + bool "Enable SLE UART low latency sample." +endchoice + +config SAMPLE_SUPPORT_PERFORMANCE_TYPE + bool + prompt "Set the currrent sample in PERFORMANCE mode." + default n + depends on SAMPLE_SUPPORT_LOW_LATENCY_TYPE + help + This option means the currrent sample is in performance mode. +endmenu + + +config SUPPORT_SLE_PERIPHERAL + bool + default y if (SAMPLE_SUPPORT_SLE_UART_SERVER) + + +config SUPPORT_SLE_CENTRAL + bool + default y if (SAMPLE_SUPPORT_SLE_UART_CLIENT) diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/LICENSE b/vendor/hellokun/demo/sparklink_car_net/obu/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..5f90739ab310acdeab0e1d788247ea6d23780ba8 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 ckunkun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/README.md b/vendor/hellokun/demo/sparklink_car_net/obu/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7b2a6a7246ff9644eb00336d6374d211aefa4320 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/README.md @@ -0,0 +1,138 @@ +# obu +#### 介绍 +存放OBU demo案例代码 + +#### 使用说明 + +1. 拉取代码到application\samples\products\obu + + ```sh + bearpi-h2821_pico/application/samples/products/obu + . + |-- CMakeLists.txt + |-- Kconfig + |-- ble_uart.c + |-- ble_uart_server + | |-- ble_uart_server.c + | |-- ble_uart_server.h + | |-- ble_uart_server_adv.c + | `-- ble_uart_server_adv.h + |-- board.json + |-- sle_uart.c + |-- sle_uart_client + | |-- sle_uart_client.c + | `-- sle_uart_client.h + `-- sle_uart_server + |-- sle_uart_server.c + |-- sle_uart_server.h + |-- sle_uart_server_adv.c + `-- sle_uart_server_adv.h + ``` + +2. 修改编译application\samples\products\CMakeLists.txt文件 + + ```cmake + #=============================================================================== + # @brief cmake file + # Copyright (c) CompanyNameMagicTag 2023-2023. All rights reserved. + #=============================================================================== + + + if(DEFINED CONFIG_SAMPLE_SUPPORT_SLE_UART) + add_subdirectory_if_exist(sle_uart) + endif() + + if(DEFINED CONFIG_SAMPLE_SUPPORT_BLE_UART) + add_subdirectory_if_exist(ble_uart) + endif() + + if(DEFINED CONFIG_SAMPLE_SUPPORT_OBU) + add_subdirectory_if_exist(obu) + endif() + + set(SOURCES "${SOURCES}" PARENT_SCOPE) + set(PUBLIC_HEADER "${PUBLIC_HEADER}" PARENT_SCOPE) + set(LIBS "${LIBS}" PARENT_SCOPE) + ``` + +3. 修改application\samples\products\Kconfig文件 + + ```kotlin + #=============================================================================== + # @brief Kconfig file. + # Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. + #=============================================================================== + config ENABLE_ALL_PRODUCTS_SAMPLE + bool + prompt "Enable all the sample of product, it's just for build." + default n + depends on ENABLE_PRODUCTS_SAMPLE + select SAMPLE_SUPPORT_BLE_UART + select SAMPLE_SUPPORT_SLE_UART + select SAMPLE_SUPPORT_SLE_BLE + help + This option means enable all the sample of product, it is just for build. + + + + config SAMPLE_SUPPORT_BLE_UART + bool + prompt "Support BLE UART sample." + default n + depends on ENABLE_PRODUCTS_SAMPLE + help + This option means support BLE UART Sample. + + if SAMPLE_SUPPORT_BLE_UART + menu "BLE UART Sample Configuration" + osource "application/samples/products/ble_uart/Kconfig" + endmenu + endif + + + + config SAMPLE_SUPPORT_SLE_UART + bool + prompt "Support SLE UART sample." + default n + depends on ENABLE_PRODUCTS_SAMPLE + help + This option means support SLE UART Sample. + + if SAMPLE_SUPPORT_SLE_UART + menu "SLE UART Sample Configuration" + osource "application/samples/products/sle_uart/Kconfig" + endmenu + endif + + config SAMPLE_SUPPORT_OBU + bool + prompt "Support OBU sample." + default n + depends on ENABLE_PRODUCTS_SAMPLE + help + This option means support OBU Sample. + + if SAMPLE_SUPPORT_OBU + menu "OBU Sample Configuration" + osource "application/samples/products/obu/Kconfig" + endmenu + endif + + ``` + +4. 编译 + + #### 1)-sle cilent, ble server [OBU 使用该模式]![image-20240805201350912](./images/sle_client_ble_server.png) + + **协议选 sle ble central default** + + ![image-20240805201548944](./images/protocol.png) + + #### 2)-sle server, ble server + + ![image-20240805201651083](./images/sle_server_ble_server.png) + +**协议选 sle ble central default** **与模式1)一致** + +![image-20240805201548944](./images/protocol.png) diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_client_server.md b/vendor/hellokun/demo/sparklink_car_net/obu/ble_client_server.md new file mode 100644 index 0000000000000000000000000000000000000000..c2135aab7e7625c03d91594b641d5fca9334aea7 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_client_server.md @@ -0,0 +1,166 @@ +## 1 修改BLE客户端要连接的服务 +修改方法: +client端修改要连接的server_addr的全局定义: +`static uint8_t g_ble_server_addr_connect[] = { 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };` +在扫描结果回调中,匹配到该server_add,会获取并使用该remote地址,执行连接。 + +这个地址怎么知道的? +在server代码ble_uart_server.c中,定义ble广播地址: +`static uint8_t g_ble_uart_server_addr[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };` +这个server地址和client端连接地址是逆序的,原因是比对时直接比的网络字节序。真实地址以server端定义的地址为准。 + +## 2 BLE服务端连接过程 +#### Step1 ble广播开启 + +server上电,触发上电回调,执行`enable_ble();`,触发ble_enable_cbk。 +在ble_enable_cbk中,设置server的local_name和local_addr;注册和添加ble服务。 + +注册ble服务: +`gatts_register_server(&app_uuid, &g_server_id);` //生成app_uuid和g_server_id + +添加ble服务: +`gatts_add_service(BLE_UART_SERVER_ID, &uart_service_uuid, true);` //这里直接使用`BLE_UART_SERVER_ID` = 1 来 add_service + +添加ble服务触发ble_uart_server_service_add_cbk,回调中会添加tx/rx配置并start_service。 + +添加ble uart发送服务的所有特征和描述符: +`ble_uart_add_tx_characters_and_descriptors(server_id, handle);` + +添加ble uart接收服务的所有特征和描述符: +`ble_uart_add_rx_characters_and_descriptors(server_id, handle);` + +启动ble service: +`gatts_start_service(server_id, handle);` + +添加tx/rx的characters配置:触发特征添加回调,回调函数中会设置`g_notify_indicate_handle`(type: uint16_t)的值。 +添加tx/rx的descriptors配置:触发描述符添加回调,会进行一些log打印操作。 +启动ble service:触发开始服务回调`ble_uart_server_service_start_cbk`。 + +在开始服务回调`ble_uart_server_service_start_cbk`中,设置并启动ble设备: +设置设备参数: +`ble_uart_set_adv_data();` +设置广播参数并开始ble广播: +`ble_uart_start_adv();` + +`start_adv`触发adv_enable回调,回调中仅打印log。 + +#### Step2 ble rx/tx开启 + +注册rx回调: +``` +uapi_uart_register_rx_callback(CONFIG_BLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 1, ble_uart_read_int_handler); +``` +//`ble_uart_read_int_handler(const void *buffer, uint16_t length, bool error)`用于从串口输入读数据,通过mouse_msg_queue将数据发往remote client。 + +循环接收: +``` +while(1) { + msg_data_t msg_data = { 0 }; + osal_msg_queue_read_copy(mouse_msg_queue, &msg_data, &msg_rev_size, + OSAL_WAIT_FOREVER); //从mouse_msg_queue获取rx数据 + if (msg_data.value != NULL) { + ble_uart_server_send_input_report(msg_data.value, msg_data.value_len); + //通过g_ble_uart_conn_id和g_notify_indicate_handle(uint16_t,在特征添加回调生成),将数据从设备发给remote client + } +} +``` + +#### Step3 client scan/seek + +client上电,上电回调执行`enable_ble();`,触发client_enable回调。 +在ble_uart_client_enable_cbk中,注册client(生成g_client_app_uuid和g_uart_client_id),并设置扫描参数。 + +触发设置扫描参数回调,remove所有配对,并开始扫描ble server。 + +注册rx回调://所有参数和ble server的相同,`ble_uart_read_int_handler`内部处理也相同。 +``` +uapi_uart_register_rx_callback(CONFIG_BLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 1, ble_uart_read_int_handler); +``` +//`ble_uart_read_int_handler(const void *buffer, uint16_t length, bool error)`用于从串口输入读数据,通过mouse_msg_queue将数据发往remote server + +循环接收: +``` +while(1) { + msg_data_t msg_data = { 0 }; + osal_msg_queue_read_copy(mouse_msg_queue, &msg_data, &msg_rev_size, + OSAL_WAIT_FOREVER); //从mouse_msg_queue获取rx数据 + if (msg_data.value != NULL) { + //将数据从设备发到remote server + uint16_t write_handle = ble_uart_get_write_vlaue_handle(); + ble_uart_client_write_cmd(msg_data.value, msg_data.value_len, write_handle); + } +} +``` +这时,client还在scan中。 + +#### Step4 扫描结果回调 +扫描到有ble server,触发扫描结果回调,直接比对result里的server_addr数据,如果和自己要连接的server_addr(在client写死)相同,先stop扫描,再执行connect连接到remote server。 + +#### Step5 connect连接状态变更回调 +client连接server成功 + +client端:触发connect连接状态变更回调 +`conn_state == GAP_BLE_STATE_CONNECTED`,发送exchange_mtu_req: +`gattc_exchange_mtu_req(g_uart_client_id, g_uart_conn_id, g_uart_mtu);` + +server端:也触发connect连接状态变更回调 +`conn_state == GAP_BLE_STATE_CONNECTED`,不做任何处理,直接return。 + +#### Step6 mtu变更回调 +client端产生了mtu设置/变更 + +client端:触发mtu变更回调,去发现所有server设备: +`ble_uart_client_discover_all_service(conn_id);` + +#### Step7 discovery和等待配对完成 +client去发现所有server设备 +client端:触发`client_discover_service_cbk`,获取设备发现结果,执行特征发现: +`gattc_discovery_character(g_uart_client_id, conn_id, ¶m);` + +触发`client_discover_character_cbk`,获取特征发现结果,执行描述符发现: +`gattc_discovery_descriptor(g_uart_client_id, conn_id, character->declare_handle);` + +触发`client_discover_descriptor_cbk`,获取描述符发现结果,执行read_req请求: +`gattc_read_req_by_uuid(client_id, conn_id, ¶msss);` + +在这些discover完成后,会依次触发: +``` +client_discover_service_compl_cbk, +client_discover_character_compl_cbk, +client_discover_descriptor_compl_cbk, +``` +这几个回调仅打印log。 + +此时,ble才配对完成,触发配对完成回调,会再次尝试设置mtu: +`gattc_exchange_mtu_req(g_uart_client_id, g_uart_conn_id, g_uart_mtu);` +但因为mtu实际没变,不会触发后续mtu变更回调。 + +执行read_req请求 +client端:触发`client_read_cfm_cbk`,只做log打印。 +server端:触发`receive_read_req_cbk`,也只做log打印。 + +在read_req执行完成后 +client端:触发`client_read_compl_cbk`,只做log打印。 + + +至此,连接建立并配对成功,并在client和server都完成一些必要的后续处理,可以相互发送数据了。 + + +## 3 数据收发接口 +ble clien接收: +`static void *ble_uart_client_task(const char *arg);` + +ble client发送: +`errcode_t ble_uart_client_write_cmd(uint8_t *data, uint16_t len, uint16_t hand);` + +ble server接收: +`static void *ble_uart_server_task(const char *arg);` + +ble server发送: +`errcode_t ble_uart_server_send_input_report(uint8_t *data, uint16_t len);` + + diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart.c b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart.c new file mode 100644 index 0000000000000000000000000000000000000000..4d4335d2906ca0d81bb566b420a0d634c6917f77 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart.c @@ -0,0 +1,145 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: BLE UART Sample Source. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-20, Create file. \n + */ +#define CONFIG_SAMPLE_SUPPORT_BLE_UART_SERVER 1 +#include "securec.h" +#include "common_def.h" +#include "soc_osal.h" +#include "app_init.h" +#include "uart.h" +#if defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_SERVER) +#include "bts_gatt_server.h" +#include "ble_uart_server.h" +#elif defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT) +#include "bts_gatt_client.h" +#include "ble_uart_client.h" +#endif /* CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT */ + +#define BLE_UART_BT_STACK_POWER_MS 10000 + +typedef struct { + uint8_t *value; + uint16_t value_len; +} msg_data_t; +unsigned long mouse_msg_queue = 0; +unsigned int msg_rev_size = sizeof(msg_data_t); + +#if defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_SERVER) +// 串口收到数据时回调该函数,注意:sle client一起使用时串口被sle占用了 +static void ble_uart_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + osal_printk("ble_uart_read_int_handler server.\r\n"); + unused(error); + if (ble_uart_get_connection_state() != 0) { + msg_data_t msg_data = { 0 }; + void* buffer_cpy = osal_vmalloc(length); + if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) { + osal_vfree(buffer_cpy); + return; + } + msg_data.value = (uint8_t *)buffer_cpy; + msg_data.value_len = length; + // 串口数据写入到消息队列中,idmouse_msg_queue + osal_msg_queue_write_copy(mouse_msg_queue, (void *)&msg_data, msg_rev_size, 0); + } +} + +static void *ble_uart_server_task(const char *arg) +{ + unused(arg); + ble_uart_server_init(); + // 注册接收回调函数,这个回调函数会根据触发条件和Size触发。 + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 1, ble_uart_read_int_handler); + if (ret != ERRCODE_SUCC) { + osal_printk("Register uart callback fail."); + return NULL; + } + while (1) { + msg_data_t msg_data = { 0 }; + // 读取串口写入到mouse_msg_queue消息队列中的数据 + int msg_ret = osal_msg_queue_read_copy(mouse_msg_queue, &msg_data, &msg_rev_size, OSAL_WAIT_FOREVER); + if (msg_ret != OSAL_SUCCESS) { + osal_printk("msg queue read copy fail."); + if (msg_data.value != NULL) { + osal_vfree(msg_data.value); + } + continue; + } + if (msg_data.value != NULL) { //将有效数据通过ble接口发送到host/device + ble_uart_server_send_input_report(msg_data.value, msg_data.value_len); + osal_vfree(msg_data.value); + } + } + return NULL; +} +#elif defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT) +static void ble_uart_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + osal_printk("ble_uart_read_int_handler client.\r\n"); + unused(error); + msg_data_t msg_data = { 0 }; + void* buffer_cpy = osal_vmalloc(length); + if (memcpy_s(buffer_cpy, length, buffer, length) != EOK) { + osal_vfree(buffer_cpy); + return; + } + msg_data.value = (uint8_t *)buffer_cpy; + msg_data.value_len = length; + osal_msg_queue_write_copy(mouse_msg_queue, (void *)&msg_data, msg_rev_size, 0); +} + +static void *ble_uart_client_task(const char *arg) +{ + unused(arg); + ble_uart_client_init(); + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_BLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 1, ble_uart_read_int_handler); + if (ret != ERRCODE_SUCC) { + osal_printk("Register uart callback fail."); + return NULL; + } + while (1) { + msg_data_t msg_data = { 0 }; + int msg_ret = osal_msg_queue_read_copy(mouse_msg_queue, &msg_data, &msg_rev_size, OSAL_WAIT_FOREVER); + if (msg_ret != OSAL_SUCCESS) { + osal_printk("msg queue read copy fail."); + if (msg_data.value != NULL) { + osal_vfree(msg_data.value); + } + continue; + } + if (msg_data.value != NULL) { + uint16_t write_handle = ble_uart_get_write_vlaue_handle(); + ble_uart_client_write_cmd(msg_data.value, msg_data.value_len, write_handle); + osal_vfree(msg_data.value); + } + } + return NULL; +} +#endif /* CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT */ + +static void ble_uart_entry(void) +{ + char *arg = NULL; + int msg_ret = osal_msg_queue_create("task_msg", msg_rev_size, &mouse_msg_queue, 0, msg_rev_size); + if (msg_ret != OSAL_SUCCESS) { + osal_printk("msg queue create fail."); + return; + } +#if defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_SERVER) + ble_uart_server_task(arg); +#elif defined(CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT) + ble_uart_client_task(arg); +#endif /* CONFIG_SAMPLE_SUPPORT_BLE_UART_CLIENT */ +} + +/* Run the ble_uart_entry. */ +app_run(ble_uart_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.c b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.c new file mode 100644 index 0000000000000000000000000000000000000000..7ab30346351a37d4b52d2ea0d7d0773469f63213 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.c @@ -0,0 +1,364 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: BLE UART Server Source. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-26, Create file. \n + */ +#include "osal_addr.h" +#include "osal_debug.h" +#include "product.h" +#include "securec.h" +#include "errcode.h" +#include "uart.h" +#include "bts_def.h" +#include "bts_device_manager.h" +#include "bts_gatt_stru.h" +#include "bts_gatt_server.h" +#include "bts_le_gap.h" +#include "ble_uart_server_adv.h" +#include "ble_uart_server.h" + +/* uart gatt server id */ +#define BLE_UART_SERVER_ID 1 +/* uart ble connect id */ +#define BLE_SINGLE_LINK_CONNECT_ID 1 +/* octets of 16 bits uart */ +#define UART16_LEN 2 +/* invalid attribute handle */ +#define INVALID_ATT_HDL 0 +/* invalid server ID */ +#define INVALID_SERVER_ID 0 + +#define BLE_UART_SERVER_LOG "[ble uart server]" +#define BLE_UART_SERVER_ERROR "[ble uart server error]" +#define BLE_UART_SERVICE_NUM 3 + +static uint16_t g_ble_uart_conn_id; +static uint8_t g_ble_uart_name_value[] = { 'o', 'b', 'u', '_', 'u', 'a', 'r', 't', '\0' }; +static uint8_t g_uart_server_app_uuid[] = { 0x00, 0x00 }; +#if defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) +static uint8_t g_ble_uart_server_addr[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; +#elif defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) +static uint8_t g_ble_uart_server_addr[] = { 0x12, 0x23, 0x34, 0x45, 0x56, 0x67 }; +#endif /* CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT */ + +static uint8_t g_server_id = INVALID_SERVER_ID; +static uint8_t g_connection_state = 0; +static uint16_t g_notify_indicate_handle = 0; +static uint8_t g_service_num = 0; + +/* 将uint16的uuid数字转化为bt_uuid_t */ +static void bts_data_to_uuid_len2(uint16_t uuid_data, bt_uuid_t *out_uuid) +{ + out_uuid->uuid_len = UART16_LEN; + out_uuid->uuid[0] = (uint8_t)(uuid_data >> 8); /* 8: octet bit num */ + out_uuid->uuid[1] = (uint8_t)(uuid_data); +} + +/* 设置注册服务时的name */ +void ble_uart_set_device_name_value(const uint8_t *name, const uint8_t len) +{ + size_t len_name = sizeof(g_ble_uart_name_value); + if (memcpy_s(g_ble_uart_name_value, len_name, name, len) != EOK) { + osal_printk("%s memcpy name fail\n", BLE_UART_SERVER_ERROR); + } +} + +/* 创建服务 */ +static void ble_uart_add_service(void) +{ + bt_uuid_t uart_service_uuid = { 0 }; + bts_data_to_uuid_len2(BLE_UART_UUID_SERVER_SERVICE, &uart_service_uuid); + gatts_add_service(BLE_UART_SERVER_ID, &uart_service_uuid, true); +} + +/* 添加uart发送服务的所有特征和描述符 */ +static void ble_uart_add_tx_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle) +{ + osal_printk("%s TX characters:%d srv_handle:%d \n", BLE_UART_SERVER_LOG, server_id, srvc_handle); + bt_uuid_t characters_uuid = { 0 }; + uint8_t characters_value[] = { 0x12, 0x34 }; + bts_data_to_uuid_len2(BLE_UART_CHARACTERISTIC_UUID_TX, &characters_uuid); + gatts_add_chara_info_t character; + character.chara_uuid = characters_uuid; + character.properties = GATT_CHARACTER_PROPERTY_BIT_NOTIFY | GATT_CHARACTER_PROPERTY_BIT_READ; + character.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE; + character.value_len = sizeof(characters_value); + character.value = characters_value; + gatts_add_characteristic(server_id, srvc_handle, &character); + osal_printk("%s characters_uuid:%2x %2x\n", BLE_UART_SERVER_LOG, characters_uuid.uuid[0], characters_uuid.uuid[1]); + + static uint8_t ccc_val[] = { 0x01, 0x00 }; // notify + bt_uuid_t ccc_uuid = { 0 }; + bts_data_to_uuid_len2(BLE_UART_CLIENT_CHARACTERISTIC_CONFIGURATION, &ccc_uuid); + gatts_add_desc_info_t descriptor; + descriptor.desc_uuid = ccc_uuid; + descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_CHARACTER_PROPERTY_BIT_WRITE | + GATT_ATTRIBUTE_PERMISSION_WRITE; + descriptor.value_len = sizeof(ccc_val); + descriptor.value = ccc_val; + gatts_add_descriptor(server_id, srvc_handle, &descriptor); + osal_printk("%s ccc_uuid:%2x %2x\n", BLE_UART_SERVER_LOG, characters_uuid.uuid[0], characters_uuid.uuid[1]); +} + +/* 添加uart接收服务的所有特征和描述符 */ +static void ble_uart_add_rx_characters_and_descriptors(uint8_t server_id, uint16_t srvc_handle) +{ + osal_printk("%s RX characters:%d srv_handle: %d \n", BLE_UART_SERVER_LOG, server_id, srvc_handle); + bt_uuid_t characters_uuid = { 0 }; + uint8_t characters_value[] = { 0x12, 0x34 }; + bts_data_to_uuid_len2(BLE_UART_CHARACTERISTIC_UUID_RX, &characters_uuid); + gatts_add_chara_info_t character; + character.chara_uuid = characters_uuid; + character.properties = GATT_CHARACTER_PROPERTY_BIT_READ | GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP; + character.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE; + character.value_len = sizeof(characters_value); + character.value = characters_value; + gatts_add_characteristic(server_id, srvc_handle, &character); + osal_printk("%s characters_uuid:%2x %2x\n", BLE_UART_SERVER_LOG, characters_uuid.uuid[0], characters_uuid.uuid[1]); + + bt_uuid_t ccc_uuid = { 0 }; + /* uart client characteristic configuration value for test */ + static uint8_t ccc_val[] = { 0x00, 0x00 }; + bts_data_to_uuid_len2(BLE_UART_CLIENT_CHARACTERISTIC_CONFIGURATION, &ccc_uuid); + gatts_add_desc_info_t descriptor; + descriptor.desc_uuid = ccc_uuid; + descriptor.permissions = GATT_ATTRIBUTE_PERMISSION_READ | GATT_ATTRIBUTE_PERMISSION_WRITE; + descriptor.value_len = sizeof(ccc_val); + descriptor.value = ccc_val; + gatts_add_descriptor(server_id, srvc_handle, &descriptor); + osal_printk("%s ccc_uuid:%2x %2x\n", BLE_UART_SERVER_LOG, characters_uuid.uuid[0], characters_uuid.uuid[1]); +} + +bool bts_uart_compare_uuid(bt_uuid_t *uuid1, bt_uuid_t *uuid2) +{ + if (uuid1->uuid_len != uuid2->uuid_len) { + return false; + } + if (memcmp(uuid1->uuid, uuid2->uuid, uuid1->uuid_len) != 0) { + return false; + } + return true; +} + +/* 服务添加回调 */ +static void ble_uart_server_service_add_cbk(uint8_t server_id, bt_uuid_t *uuid, uint16_t handle, errcode_t status) +{ + osal_printk("%s add characters_and_descriptors cbk service:%d, srv_handle:%d, uuid_len:%d, status:%d, uuid:", + BLE_UART_SERVER_LOG, server_id, handle, uuid->uuid_len, status); + for (int8_t i = 0; i < uuid->uuid_len; i++) { + osal_printk("%02x ", uuid->uuid[i]); + } + osal_printk("\n"); + ble_uart_add_tx_characters_and_descriptors(server_id, handle); + ble_uart_add_rx_characters_and_descriptors(server_id, handle); + gatts_start_service(server_id, handle); +} + +/* 特征添加回调 */ +static void ble_uart_server_characteristic_add_cbk(uint8_t server_id, bt_uuid_t *uuid, uint16_t service_handle, + gatts_add_character_result_t *result, errcode_t status) +{ + osal_printk("%s add character cbk service:%d service_hdl: %d char_hdl: %d char_val_hdl: %d uuid_len: %d \n", + BLE_UART_SERVER_LOG, server_id, service_handle, result->handle, result->value_handle, uuid->uuid_len); + osal_printk("uuid:"); + for (int8_t i = 0; i < uuid->uuid_len; i++) { + osal_printk("%02x ", uuid->uuid[i]); + } + bt_uuid_t characters_cbk_uuid = { 0 }; + bts_data_to_uuid_len2(BLE_UART_CHARACTERISTIC_UUID_TX, &characters_cbk_uuid); + characters_cbk_uuid.uuid_len = uuid->uuid_len; + if (bts_uart_compare_uuid(uuid, &characters_cbk_uuid)) { + g_notify_indicate_handle = result->value_handle; + } + osal_printk("%s status:%d indicate_handle:%d\n", BLE_UART_SERVER_LOG, status, g_notify_indicate_handle); +} + +/* 描述符添加回调 */ +static void ble_uart_server_descriptor_add_cbk(uint8_t server_id, bt_uuid_t *uuid, uint16_t service_handle, + uint16_t handle, errcode_t status) +{ + osal_printk("%s service:%d service_hdl: %d desc_hdl: %d uuid_len: %d \n", + BLE_UART_SERVER_LOG, server_id, service_handle, handle, uuid->uuid_len); + osal_printk("uuid:"); + for (int8_t i = 0; i < uuid->uuid_len; i++) { + osal_printk("%02x ", (uint8_t)uuid->uuid[i]); + } + osal_printk("%s status:%d\n", BLE_UART_SERVER_LOG, status); +} + +/* 开始服务回调 */ +static void ble_uart_server_service_start_cbk(uint8_t server_id, uint16_t handle, errcode_t status) +{ + g_service_num++; + if ((g_service_num == BLE_UART_SERVICE_NUM) && (status == 0)) { + osal_printk("%s start service cbk , start adv\n", BLE_UART_SERVER_LOG); + ble_uart_set_adv_data(); + ble_uart_start_adv(); + } + osal_printk("%s start service:%2d service_hdl: %d status: %d\n", + BLE_UART_SERVER_LOG, server_id, handle, status); +} +// 收到APP写入的数据,触发该回调函数,在串口输出该数据 +// 新增sle,将APP发来的数据发到sle server/client +#include "sle_uart_client.h" +static void ble_uart_receive_write_req_cbk(uint8_t server_id, uint16_t conn_id, gatts_req_write_cb_t *write_cb_para, + errcode_t status) +{ + osal_printk("%s ble uart write cbk server_id:%d, conn_id:%d, status%d\n", + BLE_UART_SERVER_LOG, server_id, conn_id, status); + osal_printk("%s ble uart write cbk len:%d, data:%s\n", + BLE_UART_SERVER_LOG, write_cb_para->length, write_cb_para->value); + if ((write_cb_para->length > 0) && write_cb_para->value) { + // 发送到sle server + ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param(); + uint16_t g_sle_uart_conn_id = get_g_sle_uart_conn_id(); + sle_uart_send_param->data_len = write_cb_para->length; + sle_uart_send_param->data = (uint8_t *)write_cb_para->value; + ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param); + // 串口输出 + uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(write_cb_para->value), write_cb_para->length, 0); + } +} +//收到串口的数据,触发该回调,输出读取到的串口数据 +static void ble_uart_receive_read_req_cbk(uint8_t server_id, uint16_t conn_id, gatts_req_read_cb_t *read_cb_para, + errcode_t status) +{ + osal_printk("%s ReceiveReadReq--server_id:%d conn_id:%d\n", BLE_UART_SERVER_LOG, server_id, conn_id); + osal_printk("%s request_id:%d, att_handle:%d offset:%d, need_rsp:%d, is_long:%d\n", + BLE_UART_SERVER_LOG, read_cb_para->request_id, read_cb_para->handle, read_cb_para->offset, + read_cb_para->need_rsp, read_cb_para->is_long); + osal_printk("%s status:%d\n", BLE_UART_SERVER_LOG, status); +} + +static void ble_uart_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id, uint16_t mtu_size, errcode_t status) +{ + osal_printk("%s MtuChanged--server_id:%d conn_id:%d\n", BLE_UART_SERVER_LOG, server_id, conn_id); + osal_printk("%s mtusize:%d, status:%d\n", BLE_UART_SERVER_LOG, mtu_size, status); +} + +static void ble_uart_server_adv_enable_cbk(uint8_t adv_id, adv_status_t status) +{ + osal_printk("%s adv enable cbk adv_id:%d status:%d\n", BLE_UART_SERVER_LOG, adv_id, status); +} + +static void ble_uart_server_adv_disable_cbk(uint8_t adv_id, adv_status_t status) +{ + osal_printk("%s adv disable adv_id: %d, status:%d\n", BLE_UART_SERVER_LOG, adv_id, status); +} + +void ble_uart_server_connect_change_cbk(uint16_t conn_id, bd_addr_t *addr, gap_ble_conn_state_t conn_state, + gap_ble_pair_state_t pair_state, gap_ble_disc_reason_t disc_reason) +{ + g_ble_uart_conn_id = conn_id; + g_connection_state = (uint8_t)conn_state; + osal_printk("%s connect state change conn_id: %d, status: %d, pair_status:%d, addr %x disc_reason %x\n", + BLE_UART_SERVER_LOG, conn_id, conn_state, pair_state, addr[0], disc_reason); + if (conn_state == GAP_BLE_STATE_CONNECTED) { + return; + } else if (conn_state == GAP_BLE_STATE_DISCONNECTED) { + ble_uart_set_adv_data(); + ble_uart_start_adv(); + } +} +void ble_uart_server_pair_result_cb(uint16_t conn_id, const bd_addr_t *addr, errcode_t status) +{ + osal_printk("%s pair result conn_id: %d, status: %d, addr %x \n", + BLE_UART_SERVER_LOG, conn_id, status, addr[0]); +} + +void ble_uart_server_power_on_cbk(uint8_t status) +{ + osal_printk("ble power on: %d\n", status); + enable_ble(); +} + +void ble_uart_server_enable_cbk(uint8_t status) +{ + osal_printk("ble enable: %d\n", status); + errcode_t ret = 0; + bt_uuid_t app_uuid = { 0 }; + bd_addr_t ble_addr = { 0 }; + app_uuid.uuid_len = sizeof(g_uart_server_app_uuid); + if (memcpy_s(app_uuid.uuid, app_uuid.uuid_len, g_uart_server_app_uuid, sizeof(g_uart_server_app_uuid)) != EOK) { + osal_printk("%s add server app uuid memcpy failed\n", BLE_UART_SERVER_ERROR); + return; + } + ble_addr.type = BLE_PUBLIC_DEVICE_ADDRESS; + if (memcpy_s(ble_addr.addr, BD_ADDR_LEN, g_ble_uart_server_addr, sizeof(g_ble_uart_server_addr)) != EOK) { + osal_printk("%s add server app addr memcpy failed\n", BLE_UART_SERVER_ERROR); + return; + } + gap_ble_set_local_name(g_ble_uart_name_value, sizeof(g_ble_uart_name_value)); + gap_ble_set_local_addr(&ble_addr); + ret = gatts_register_server(&app_uuid, &g_server_id); + if ((ret != ERRCODE_BT_SUCCESS) || (g_server_id == INVALID_SERVER_ID)) { + osal_printk("%s add server failed\r\n", BLE_UART_SERVER_ERROR); + return; + } + ble_uart_add_service(); /* 添加uart服务 */ + osal_printk("%s beginning add service\r\n", BLE_UART_SERVER_LOG); + bth_ota_init(); +} + +static errcode_t ble_uart_server_register_callbacks(void) +{ + bts_dev_manager_callbacks_t dev_mgr_cb = { 0 }; + gap_ble_callbacks_t gap_cb = { 0 }; + gatts_callbacks_t service_cb = { 0 }; + + dev_mgr_cb.power_on_cb = ble_uart_server_power_on_cbk; + dev_mgr_cb.ble_enable_cb = ble_uart_server_enable_cbk; + gap_cb.start_adv_cb = ble_uart_server_adv_enable_cbk; + gap_cb.conn_state_change_cb = ble_uart_server_connect_change_cbk; + gap_cb.stop_adv_cb = ble_uart_server_adv_disable_cbk; + gap_cb.pair_result_cb = ble_uart_server_pair_result_cb; + errcode_t ret = gap_ble_register_callbacks(&gap_cb); + ret |= bts_dev_manager_register_callbacks(&dev_mgr_cb); + if (ret != ERRCODE_BT_SUCCESS) { + osal_printk("%s reg gap cbk failed ret = %d\n", BLE_UART_SERVER_ERROR, ret); + return ret; + } + + service_cb.add_service_cb = ble_uart_server_service_add_cbk; + service_cb.add_characteristic_cb = ble_uart_server_characteristic_add_cbk; + service_cb.add_descriptor_cb = ble_uart_server_descriptor_add_cbk; + service_cb.start_service_cb = ble_uart_server_service_start_cbk; + service_cb.read_request_cb = ble_uart_receive_read_req_cbk; + service_cb.write_request_cb = ble_uart_receive_write_req_cbk; + service_cb.mtu_changed_cb = ble_uart_mtu_changed_cbk; + ret = gatts_register_callbacks(&service_cb); + if (ret != ERRCODE_BT_SUCCESS) { + osal_printk("%s reg service cbk failed ret = %d\n", BLE_UART_SERVER_ERROR, ret); + return ret; + } +#if (CORE_NUMS < 2) + enable_ble(); +#endif + return ret; +} + +void ble_uart_server_init(void) +{ + ble_uart_server_register_callbacks(); +} + +/* device向host发送数据:input report */ +errcode_t ble_uart_server_send_input_report(uint8_t *data, uint16_t len) +{ + gatts_ntf_ind_t param = { 0 }; + uint16_t conn_id = g_ble_uart_conn_id; + param.attr_handle = g_notify_indicate_handle; + param.value_len = len; + param.value = data; + osal_printk("%s send input report indicate_handle:%d\n", BLE_UART_SERVER_LOG, g_notify_indicate_handle); + gatts_notify_indicate(BLE_UART_SERVER_ID, conn_id, ¶m); + return ERRCODE_BT_SUCCESS; +} + +uint8_t ble_uart_get_connection_state(void) +{ + return g_connection_state; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.h b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.h new file mode 100644 index 0000000000000000000000000000000000000000..5cdc4646abef60101576520b1e75d10fd721a117 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server.h @@ -0,0 +1,41 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: BLE uart server Config. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-26, Create file. \n + */ +#ifndef BLE_UART_SERVER_H +#define BLE_UART_SERVER_H + +#include +#include "errcode.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* Service UUID */ +#define BLE_UART_UUID_SERVER_SERVICE 0x9011 +/* Characteristic UUID */ +#define BLE_UART_CHARACTERISTIC_UUID_TX 0x9012 +/* Characteristic UUID */ +#define BLE_UART_CHARACTERISTIC_UUID_RX 0xEFEF +/* Client Characteristic Configuration UUID */ +#define BLE_UART_CLIENT_CHARACTERISTIC_CONFIGURATION 0x2902 + +void ble_uart_set_device_name_value(const uint8_t *name, const uint8_t len); +void ble_uart_set_device_appearance_value(uint16_t appearance); +void ble_uart_server_init(void); +errcode_t ble_uart_server_send_input_report(uint8_t *data, uint16_t len); +uint8_t ble_uart_get_connection_state(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.c b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.c new file mode 100644 index 0000000000000000000000000000000000000000..54a4f1f8ac56ad38f52977e129b0a0342b085e6c --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.c @@ -0,0 +1,274 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: BLE UART Server adv Source. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-26, Create file. \n + */ +#include "osal_addr.h" +#include "securec.h" +#include "errcode.h" +#include "osal_debug.h" +#include "bts_def.h" +#include "bts_le_gap.h" +#include "ble_uart_server_adv.h" + + +/* Ble Adv type Flag length */ +#define BLE_ADV_FLAG_LEN 0x03 +/* Ble Adv data length */ +#define BLE_GENERAL_BYTE_1 1 +/* Ble Adv appearance length */ +#define BLE_ADV_APPEARANCE_LENGTH 4 +/* Ble Adv appearance type */ +#define BLE_ADV_APPEARANCE_DATA_TYPE 0x19 +/* Ble uart appearance type */ +#define BLE_ADV_CATEGORY_UART_VALUE 0x0080 +/* Ble uart categorylength */ +#define BLE_ADV_CATEGORY_LEN 2 +/* Ble name adv param type length */ +#define BLE_ADV_PARAM_DATATYPE_LENGTH 1 +/* Ble name adv name type */ +#define BLE_ADV_LOCAL_NAME_DATA_TYPE 0x09 +/* Ble name adv tx power type */ +#define BLE_ADV_TX_POWER_LEVEL 0x0A +/* Ble name adv tx power response type */ +#define BLE_SCAN_RSP_TX_POWER_LEVEL_LEN 0x03 +/* Ble adv flag data */ +#define BLE_ADV_FLAG_DATA 0x05 + +/* Ble adv min interval */ +#define BLE_ADV_MIN_INTERVAL 0x30 +/* Ble adv max interval */ +#define BLE_ADV_MAX_INTERVAL 0x60 +/* Ble adv handle */ +#define BTH_GAP_BLE_ADV_HANDLE_DEFAULT 0x01 +/* Ble adv duration */ +#define BTH_GAP_BLE_ADV_FOREVER_DURATION 0 + +#define MAX_NAME_LENGTH 15 +#define EXT_ADV_OR_SCAN_RSP_DATA_LEN 251 +#define ADV_APPEA_CATOGORY_HIGH 8 + +#define BLE_UART_ADV_LOG "[ble uart adv]" + +typedef enum ble_adv_filter_policy { + BLE_ADV_FILTER_POLICY_SCAN_ANY_CONNECT_ANY = 0x00, + BLE_ADV_FILTER_POLICY_SCAN_WHITE_LIST_CONNECT_ANY = 0x01, + BLE_ADV_FILTER_POLICY_SCAN_ANY_CONNECT_WHITE_LIST = 0x02, + BLE_ADV_FILTER_POLICY_SCAN_WHITE_LIST_CONNECT_WHITE_LIST = 0x03 +} ble_adv_filter_policy_t; + +typedef enum ble_adverting_type { + BLE_ADV_TYPE_CONNECTABLE_UNDIRECTED = 0x00, + BLE_ADV_TYPE_CONNECTABLE_HIGH_DUTY_CYCLE_DIRECTED = 0x01, + BLE_ADV_TYPE_SCANNABLE_UNDIRECTED = 0x02, + BLE_ADV_TYPE_NON_CONNECTABLE_UNDIRECTED = 0x03, + BLE_ADV_TYPE_CONNECTABLE_LOW_DUTY_CYCLE_DIRECTED = 0x04 +} ble_adverting_type_t; + +typedef enum ble_adv_channel_map { + BLE_ADV_CHANNEL_MAP_CH_37 = 0x01, + BLE_ADV_CHANNEL_MAP_CH_38 = 0x02, + BLE_ADV_CHANNEL_MAP_CH_39 = 0x04, + BLE_ADV_CHANNEL_MAP_CH_37_CH_38 = 0x03, + BLE_ADV_CHANNEL_MAP_CH_37_CH_39 = 0x05, + BLE_ADV_CHANNEL_MAP_CH_38_CH_39 = 0x06, + BLE_ADV_CHANNEL_MAP_CH_DEFAULT = 0x07 +} ble_adv_channel_map_t; + +typedef struct { + uint8_t length; /* 广播数据类型长度 */ + uint8_t adv_data_type; /* 广播数据类型 */ + uint8_t flags; /* 广播数据标志 */ +} ble_adv_flag; + +typedef struct { + uint8_t length; /* 设备外观数据类型长度 */ + uint8_t adv_data_type; /* 设备外观数据类型 */ + uint8_t catogory_id[BLE_ADV_CATEGORY_LEN]; /* 设备外观数据 */ +} ble_appearance_t; + +typedef struct { + uint8_t length; /* 广播设备名称类型长度 */ + uint8_t adv_data_type; /* 设备名称类型 */ + int8_t *name; /* 设备名称数据指针 */ +} ble_local_name_t; + +typedef struct { + uint8_t length; /* 广播发送功率长度 */ + uint8_t adv_data_type; /* 广播发送数据类型 */ + uint8_t tx_power_value; /* 广播发送数据 */ +} ble_tx_power_level_t; + +#if defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) +uint8_t g_uart_local_name[ MAX_NAME_LENGTH] = { 'b', 'l', 'e', '_', 's', 'l', 'e', 's', '_', 's', + 'e', 'r', 'v', 'e', 'r' }; +#elif defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) +uint8_t g_uart_local_name[ MAX_NAME_LENGTH] = { 'b', 'l', 'e', '_', 's', 'l', 'e', 'c', '_', 's', + 'e', 'r', 'v', 'e', 'r' }; +#endif /* CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT */ + + + +static inline uint8_t u16_low_u8(uint16_t val) +{ + return (uint8_t)((uint16_t)(val) & 0xff); +} + +static inline uint8_t u16_high_u8(uint16_t val) +{ + return (uint8_t)(((uint16_t)(val) >> ADV_APPEA_CATOGORY_HIGH) & 0xff); +} + +static uint8_t ble_set_adv_flag_data(uint8_t *set_adv_data_position, uint8_t max_len) +{ + ble_adv_flag adv_flags = { + .length = BLE_ADV_FLAG_LEN - BLE_GENERAL_BYTE_1, + .adv_data_type = 1, + .flags = BLE_ADV_FLAG_DATA + }; + if (memcpy_s(set_adv_data_position, max_len, &adv_flags, BLE_ADV_FLAG_LEN) != EOK) { + return 0; + } + return BLE_ADV_FLAG_LEN; +} + +static uint8_t ble_set_adv_appearance(uint8_t *set_adv_data_position, uint8_t max_len) +{ + ble_appearance_t adv_appearance_data = { + .length = BLE_ADV_APPEARANCE_LENGTH - BLE_GENERAL_BYTE_1, + .adv_data_type = BLE_ADV_APPEARANCE_DATA_TYPE, + .catogory_id = { u16_low_u8(BLE_ADV_CATEGORY_UART_VALUE), u16_high_u8(BLE_ADV_CATEGORY_UART_VALUE) } + }; + if (memcpy_s(set_adv_data_position, max_len, &adv_appearance_data, BLE_ADV_APPEARANCE_LENGTH) != EOK) { + return 0; + } + return BLE_ADV_APPEARANCE_LENGTH; +} + +static uint8_t ble_set_adv_name(uint8_t *set_adv_data_position, uint8_t max_len) +{ + uint8_t len = BLE_ADV_PARAM_DATATYPE_LENGTH + BLE_ADV_PARAM_DATATYPE_LENGTH; + ble_local_name_t adv_local_name_data = { + .length = (uint8_t)(BLE_ADV_PARAM_DATATYPE_LENGTH + sizeof(g_uart_local_name)), + .adv_data_type = BLE_ADV_LOCAL_NAME_DATA_TYPE + }; + + if (memcpy_s(set_adv_data_position, max_len, &adv_local_name_data, len) != EOK) { + return 0; + } + if (memcpy_s((set_adv_data_position+len), (size_t)(max_len-len), + g_uart_local_name, sizeof(g_uart_local_name)) != EOK) { + return 0; + } + len = (uint8_t)(len + sizeof(g_uart_local_name)); + return len; +} + +static uint8_t ble_set_adv_appearance_data(uint8_t *set_adv_data_position, uint8_t max_len) +{ + uint8_t idx = 0; + + idx += ble_set_adv_appearance(set_adv_data_position, max_len); + idx += ble_set_adv_name(set_adv_data_position + idx, (max_len - idx)); + return idx; +} + +static uint16_t ble_uart_server_set_adv_data(uint8_t *set_adv_data, uint8_t adv_data_max_len) +{ + uint8_t idx = 0; + + if ((set_adv_data == NULL) || (adv_data_max_len == 0)) { + return 0; + } + + idx += ble_set_adv_flag_data(set_adv_data, adv_data_max_len); + idx += ble_set_adv_appearance_data(&set_adv_data[idx], adv_data_max_len - idx); + return idx; +} + +static uint16_t ble_set_scan_response_data(uint8_t *scan_rsp_data, uint8_t scan_rsp_data_max_len) +{ + uint8_t idx = 0; + + if (scan_rsp_data == NULL || scan_rsp_data_max_len == 0) { + return 0; + } + + /* tx power level */ + ble_tx_power_level_t tx_power_level = { + .length = BLE_SCAN_RSP_TX_POWER_LEVEL_LEN - BLE_GENERAL_BYTE_1, + .adv_data_type = BLE_ADV_TX_POWER_LEVEL, + .tx_power_value = 0 + }; + + if (memcpy_s(scan_rsp_data, scan_rsp_data_max_len, &tx_power_level, sizeof(ble_tx_power_level_t)) != EOK) { + return 0; + } + + idx += BLE_SCAN_RSP_TX_POWER_LEVEL_LEN; + + /* set local name */ + scan_rsp_data[idx++] = sizeof(g_uart_local_name) + BLE_GENERAL_BYTE_1; + scan_rsp_data[idx++] = BLE_ADV_LOCAL_NAME_DATA_TYPE; + if ((idx + sizeof(g_uart_local_name)) > scan_rsp_data_max_len) { + return 0; + } + if (memcpy_s(&scan_rsp_data[idx], scan_rsp_data_max_len - idx, g_uart_local_name, + sizeof(g_uart_local_name)) != EOK) { + return 0; + } + idx += sizeof(g_uart_local_name); + return idx; +} + +uint8_t ble_uart_set_adv_data(void) +{ + uint8_t set_adv_data[EXT_ADV_OR_SCAN_RSP_DATA_LEN] = { 0 }; + uint8_t set_scan_rsp_data[EXT_ADV_OR_SCAN_RSP_DATA_LEN] = { 0 }; + gap_ble_config_adv_data_t cfg_adv_data; + + /* set adv data */ + uint16_t adv_data_len = ble_uart_server_set_adv_data(set_adv_data, EXT_ADV_OR_SCAN_RSP_DATA_LEN); + if ((adv_data_len > EXT_ADV_OR_SCAN_RSP_DATA_LEN) || (adv_data_len == 0)) { + return 0; + } + /* set scan response data */ + uint16_t scan_rsp_data_len = ble_set_scan_response_data(set_scan_rsp_data, EXT_ADV_OR_SCAN_RSP_DATA_LEN); + if ((scan_rsp_data_len > EXT_ADV_OR_SCAN_RSP_DATA_LEN) || (scan_rsp_data_len == 0)) { + return 0; + } + cfg_adv_data.adv_data = set_adv_data; + cfg_adv_data.adv_length = adv_data_len; + + cfg_adv_data.scan_rsp_data = set_scan_rsp_data; + cfg_adv_data.scan_rsp_length = scan_rsp_data_len; + osal_printk("%s ble_uart_set_adv_data adv_handle %d, len:%d, data:%s\n", + BLE_UART_ADV_LOG, BTH_GAP_BLE_ADV_HANDLE_DEFAULT, adv_data_len, set_adv_data); + return gap_ble_set_adv_data(BTH_GAP_BLE_ADV_HANDLE_DEFAULT, &cfg_adv_data); +} + +uint8_t ble_uart_start_adv(void) +{ + errcode_t ret = ERRCODE_BT_SUCCESS; + int adv_id = BTH_GAP_BLE_ADV_HANDLE_DEFAULT; + + gap_ble_adv_params_t adv_para = { + .min_interval = BLE_ADV_MIN_INTERVAL, + .max_interval = BLE_ADV_MAX_INTERVAL, + .duration = BTH_GAP_BLE_ADV_FOREVER_DURATION, + .peer_addr.type = BLE_PUBLIC_DEVICE_ADDRESS, + /* 广播通道选择bitMap, 可参考BleAdvChannelMap */ + .channel_map = BLE_ADV_CHANNEL_MAP_CH_DEFAULT, + .adv_type = BLE_ADV_TYPE_CONNECTABLE_UNDIRECTED, + .adv_filter_policy = BLE_ADV_FILTER_POLICY_SCAN_ANY_CONNECT_ANY + }; + + (void)memset_s(&adv_para.peer_addr.addr, BD_ADDR_LEN, 0, BD_ADDR_LEN); + osal_printk("%s ble_uart_start_adv adv_id %d\n", BLE_UART_ADV_LOG, adv_id); + ret |= gap_ble_set_adv_param(adv_id, &adv_para); + ret |= gap_ble_start_adv(adv_id); + return ret; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.h b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.h new file mode 100644 index 0000000000000000000000000000000000000000..6b656984d0e7d514db8bcb9ae77bf5f8af736b99 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/ble_uart_server/ble_uart_server_adv.h @@ -0,0 +1,36 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: BLE UART Server adv config. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-26, Create file. \n + */ + +#ifndef BLE_UART_SERVER_ADV_H +#define BLE_UART_SERVER_ADV_H + +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +typedef enum { + BLE_PUBLIC_DEVICE_ADDRESS, + BLE_RANDOM_DEVICE_ADDRESS, + BLE_PUBLIC_IDENTITY_ADDRESS, + BLE_RANDOM_STATIC_IDENTITY_ADDRESS +} ble_address_t; + +uint8_t ble_uart_set_adv_data(void); +uint8_t ble_uart_start_adv(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/board.json b/vendor/hellokun/demo/sparklink_car_net/obu/board.json new file mode 100644 index 0000000000000000000000000000000000000000..f791466fe3efe11b2679622e0459d02ff3d28e6d --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/board.json @@ -0,0 +1 @@ +{"all":{"CLOCKSettings":{},"SAMPLE Settings":{},"PLIC Settings":{},"MACRO Settings":{},"gpio":{"GPIO":{},"UART2":{"UART2_TXD":{"Pin Name":"S_AGPIO8"},"UART2_RXD":{"Pin Name":"S_AGPIO9"}},"UART3":{"UART3_TXD":{"Pin Name":"S_AGPIO12"},"UART3_RXD":{"Pin Name":"S_AGPIO13"}}},"pinName":["S_AGPIO8","S_AGPIO9","S_AGPIO12","S_AGPIO13"],"GPIO Settings":{},"DMA Settings":{}},"enabled":["UART2","UART3"],"info":{"board":"bs25","chipName":"bs25","clockName":"clock3","seriesName":"bs25","package":"QFN48","package path":"","package version":"1.0","part":"","needSDK":true,"pins":{}},"part":{"UART2":{"UART2 Configuration":{"Send Mode":"POLLING","Receive Mode":"INTERRUPT","TX Interrrupt FIFO Level":"1_8","RX Interrrupt FIFO Level":"1_8","Baud Rate":115200,"Word Length(Bit)":8,"Parity":"None","Stop Bits(Bit)":1,"Hardware Flow Control":"Disable","UART2_TXD":"S_AGPIO8","UART2_RXD":"S_AGPIO9","UART2_CTS":"PIN_NONE","UART2_RTS":"PIN_NONE"},"advancedSetting":{"PLIC Settings":{"IRQ_UART2":{"Name":"IRQ_UART2","dependence":"","Module":"UART2","Priority":"1","Enable":"disable","key":"0"}},"DMA Settings":{"UART2_RX":{"SrcPeriph":"UART2_RX","dependence":"","DestPeriph":"None","Direction":"None","key":"0","Channel":"None","Priority":"None","SrcAddrInc":"UNALTERED","DestAddrInc":"UNALTERED","SrcBurst":"1","DestBurst":"1","SrcWidth":"BYTE","DestWidth":"BYTE","Enable":"disable","Module":"UART2"},"UART2_TX":{"SrcPeriph":"UART2_TX","dependence":"","DestPeriph":"None","Direction":"None","key":"0","Channel":"None","Priority":"None","SrcAddrInc":"UNALTERED","DestAddrInc":"UNALTERED","SrcBurst":"1","DestBurst":"1","SrcWidth":"BYTE","DestWidth":"BYTE","Enable":"disable","Module":"UART2"}}},"pins":{"UART2_TXD":{"Pin Name":"S_AGPIO8","dependence":true,"pin_mode":1},"UART2_RXD":{"Pin Name":"S_AGPIO9","dependence":true,"pin_mode":1}}},"UART3":{"UART3 Configuration":{"Send Mode":"POLLING","Receive Mode":"INTERRUPT","TX Interrrupt FIFO Level":"1_8","RX Interrrupt FIFO Level":"1_8","Baud Rate":115200,"Word Length(Bit)":8,"Parity":"None","Stop Bits(Bit)":1,"Hardware Flow Control":"Disable","UART3_TXD":"S_AGPIO12","UART3_RXD":"S_AGPIO13","UART3_CTS":"PIN_NONE","UART3_RTS":"PIN_NONE"},"advancedSetting":{"PLIC Settings":{"IRQ_UART3":{"Name":"IRQ_UART3","dependence":"","Module":"UART3","Priority":"1","Enable":"disable","key":"0"}},"DMA Settings":{"UART3_RX":{"SrcPeriph":"UART3_RX","dependence":"","DestPeriph":"None","Direction":"None","key":"0","Channel":"None","Priority":"None","SrcAddrInc":"UNALTERED","DestAddrInc":"UNALTERED","SrcBurst":"1","DestBurst":"1","SrcWidth":"BYTE","DestWidth":"BYTE","Enable":"disable","Module":"UART3"},"UART3_TX":{"SrcPeriph":"UART3_TX","dependence":"","DestPeriph":"None","Direction":"None","key":"0","Channel":"None","Priority":"None","SrcAddrInc":"UNALTERED","DestAddrInc":"UNALTERED","SrcBurst":"1","DestBurst":"1","SrcWidth":"BYTE","DestWidth":"BYTE","Enable":"disable","Module":"UART3"}}},"pins":{"UART3_TXD":{"Pin Name":"S_AGPIO12","dependence":true,"pin_mode":1},"UART3_RXD":{"Pin Name":"S_AGPIO13","dependence":true,"pin_mode":1}}}},"base":{"GPIO":{"GPIO":""},"DMA":{"SrcByteOrder":"Big endian","DestByteOrder":"Big endian"}},"selectPinName":{},"configType":"menu","deleteType":"moduleDelete","globalCheckResult":{"UART2":{"status":"STATUS_OK","tips":""},"UART3":{"status":"STATUS_OK","tips":""}},"globalCheckItem":{"UART2":[{"check":"all:gpio:UART2:UART2_TXD","tips":[]},{"check":"all:gpio:UART2:UART2_RXD","tips":[]}],"UART3":[{"check":"all:gpio:UART3:UART3_TXD","tips":[]},{"check":"all:gpio:UART3:UART3_RXD","tips":[]}]}} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/images/protocol.png b/vendor/hellokun/demo/sparklink_car_net/obu/images/protocol.png new file mode 100644 index 0000000000000000000000000000000000000000..958d85e26502a82e3cb2a16bd98787ecebc80850 Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/obu/images/protocol.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_client_ble_server.png b/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_client_ble_server.png new file mode 100644 index 0000000000000000000000000000000000000000..c191d71ef643666d2f106811bb32aed5b692e61f Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_client_ble_server.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_server_ble_server.png b/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_server_ble_server.png new file mode 100644 index 0000000000000000000000000000000000000000..439f64059f9ebe62bb131752a728b2b65c415ccb Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/obu/images/sle_server_ble_server.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_client_server.md b/vendor/hellokun/demo/sparklink_car_net/obu/sle_client_server.md new file mode 100644 index 0000000000000000000000000000000000000000..5b4eb453dd3b6b9ae12c4f8fb8c0025e42e0dbc1 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_client_server.md @@ -0,0 +1,236 @@ +## 1 修改SLE客户端要连接的服务 +修改方法: +client端修改要连接的server name的宏定义: +`#define SLE_UART_SERVER_NAME "sle_uart_server"` +在扫描结果回调中,匹配到该server才会去获取remote地址,执行连接。 + +这个name怎么知道的? +在server代码`sle_uart_server_adv.c`中,定义sle广播名称: +`static uint8_t sle_local_name[NAME_MAX_LENGTH] = “sle_uart_server”;` + + +## 2 SLE服务端连接过程 +#### Step1 sle广播开启 + +server上电,触发上电回调,执行`enable_sle();`,触发sle_enable_cbk。 +在sle_enable_cbk中,执行`sle_uart_server_add()`, +``` +sle_uart_server_add() +{ + ssaps_register_server(&app_uuid, &g_server_id); //生成app_uuid, g_server_id + + sle_uuid_server_service_add(); //获取g_service_handle (type: uint16_t) + + /* + * 设置property.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + * add property + * 获取g_property_handle (type: uint16_t) + * add descriptor + * 设置descriptor.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + */ + sle_uuid_server_property_add(); + + //start service: + ssaps_start_service(g_server_id, g_service_handle) +} +``` +在sle_enable_cbk中,执行`sle_uart_server_adv_init()`, +``` +sle_uart_server_adv_init() +{ + sle_set_default_announce_parm() + { + 设置sle server地址: + unsigned char local_addr[SLE_ADDR_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + //6个0 + param.announce_mode = SLE_ANNOUNCE_MODE_CONNECTABLE_SCANABLE; + //可扫描连接 + param.announce_handle = SLE_ADV_HANDLE_DEFAULT; //设置广播ID为1 + //#define SLE_ADV_HANDLE_DEFAULT 1 //广播ID + param.announce_channel_map = SLE_ADV_CHANNEL_MAP_DEFAULT; + param.announce_interval_min = SLE_ADV_INTERVAL_MIN_DEFAULT; + //广播间隔25ms,单位125us + //#define SLE_ADV_INTERVAL_MIN_DEFAULT 0xC8 + param.announce_interval_max = SLE_ADV_INTERVAL_MAX_DEFAULT; + //广播间隔25ms,单位125us + //#define SLE_ADV_INTERVAL_MAX_DEFAULT 0xC8 + param.conn_interval_min = SLE_CONN_INTV_MIN_DEFAULT; + //连接调度间隔12.5ms,单位125us + //#define SLE_CONN_INTV_MIN_DEFAULT 0x64 + param.conn_interval_max = SLE_CONN_INTV_MAX_DEFAULT; + //连接调度间隔12.5ms,单位125us + //#define SLE_CONN_INTV_MAX_DEFAULT 0x64 + param.conn_supervision_timeout = SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT; + //超时时间5000ms,单位10ms + //#define SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT 0x1F4 + memcpy_s(param.own_addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + //param.own_addr = local_addr + sle_set_announce_param(param.announce_handle, ¶m); + } + + sle_set_default_announce_data() + { + sle_set_adv_data(&announce_data); |adv_discovery_level|adv_access_mode| + data.announce_data = announce_data; + + sle_set_scan_response_data(&seek_rsp_data); |tx_power_level|local_name| + data.seek_rsp_data = seek_rsp_data; + + //关联data到adv_handle: + sle_set_announce_data(adv_handle, &data); //input: adv_handle = 1 + } + + //开启sle广播: + sle_start_announce(SLE_ADV_HANDLE_DEFAULT); //是否会触发回调 + //#define SLE_ADV_HANDLE_DEFAULT 1 //广播ID +} +``` + +#### Step2 sle rx/tx开启 +创建`sle_uart_server_task`, + +task执行: + +创建msgqueue: +`sle_uart_server_create_msgqueue();` //生成g_sle_uart_server_msgqueue_id + +注册sle msg回调: +`sle_uart_server_register_msg(Sle_uart_server_write_msgqueue);` + +注册sle server回调: +`sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk);` +注册rx回调: +``` +uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_server_read_int_handler); +``` +//`sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error)`用于从串口输入读数据,根据send_param和g_server_id + handle(g_sle_conn_hdl)将数据发往remote client。 + +循环接收: +```` +while(1) { + sle_uart_server_rx_buf_init(rx_buf, &rx_length); //清0,rx_length = 32(宏定义) + sle_uart_server_receive_msgqueue(rx_buf, &rx_length); //从msgqueue获取rx_data + { + //每次从msgqueue copy 32字节到rx_buf + osal_msg_queue_write_copy(g_sle_uart_server_msgqueue_id, //create生成 + (void*)rx_buf, (uint32_t)rx_length, 0); + } + + uint8_t sle_connect_state[] = "sle_dis_connect"; + if (strncmp((const char *)rx_buf, (const char *)sle_connect_state, sizeof(sle_connect_state)) == 0) //比对连接状态,sle_connect_state在buf最前面部分 + //如果连接状态为 sle_dis_connect,开启sle广播。 + { + sle_start_announce(SLE_ADV_HANDLE_DEFAULT); //开启sle广播 + //#define SLE_ADV_HANDLE_DEFAULT 1 //广播ID + } +} +```` + +至此,server开启完成。 + +#### Step3 client scan/seek +client上电,上电回调执行`enable_sle();`,触发sle_enable回调。 + +在`sle_uart_client_sample_sle_enable_cbk()`中,注册seek回调,conn回调,是ssapc回调;并执行sle_uart_start_scan(),开始扫描是否有sle server开启。 + +接下来创建`sle_uart_client_task`, + +task执行: +初始化uart_buffer:`g_app_uart_buffer_config`,设置buf_size = 256。 + +注册rx回调://除了read_int_handler,其他参数和sle server的相同。 +``` +uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_client_read_int_handler); +``` +//`sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error)`用于从串口输入读数据根据send_param和g_sle_uart_conn_id将数据发往remote server。 + +这时,client还在seek中。 + +#### Step4 seek_result_info回调 +client seek拿到数据,触发seek_result_info回调, +比对发现是要连接的server,保留remote server地址,并停止seek。 + +#### Step5 seek_disable回调 +client停止seek,触发seek_disable回调, +先将自己已配对的remote设备数据清除;再通过remote server地址,去连接新发现的remote servre: +`sle_connect_remote_device(&g_sle_uart_remote_addr);` + +#### Step6 connect_state连接状态变更回调 +client连接server成功 + +client端:触发connect_state连接状态变更回调 +`conn_state == SLE_ACB_STATE_CONNECTED`,进行sle配对: +`sle_pair_remote_device(&g_sle_uart_remote_addr);` + +server端:也触发connect_state连接状态变更回调 +`conn_state == SLE_ACB_STATE_CONNECTED`,正常情况下,仅更新sle_conn_handle(uint16),可以用于串口工具独到的数据发送到client等: +`g_sle_conn_hdl = conn_id;` + +#### step7 配对完成回调 +配对完成 + +Client端:触发pair_complele配对完成回调 +设置`ssap_exchange_info_t info`的`mtu_size = SLE_MTU_SIZE_DEFAULT`(520); +并发送exchange_info请求: +`ssapc_exchange_info_req(0, g_sle_uart_conn_id, &info);` + +server端:触发pair_complele配对完成回调 +先根据conn_id配置sle_pair_handle(uint16) +`g_sle_pair_hdl = conn_id + 1;` //前面g_sle_conn_hdl = conn_id; +然后也设置`ssap_exchange_info_t info`的`mtu_size = 520;`, +并发送exchange_info请求: +`ssaps_set_info(g_server_id, &info);` + +#### Step8 mtu变更回调 +产生了mtu设置/变更 + +client端:触发exchange_info回调,配置find_param并执行: +``` +ssapc_find_structure_param_t find_param = { 0 }; +find_param.type = SSAP_FIND_TYPE_PROPERTY; +find_param.start_hdl = 1; +find_param.end_hdl = 0xFFFF; +ssapc_find_structure(0, conn_id, &find_param); +``` + +server端:触发mtu_changed回调,只打log并确保`g_sle_pair_hdl = conn_id + 1;`。 + +#### Step9 find_structure回调 +find_structure只在client端执行 + +在client端,触发find_structure回调,在client端保留一些server信息: +``` +g_sle_uart_find_service_result.start_hdl = service->start_hdl; +g_sle_uart_find_service_result.end_hdl = service->end_hdl; +memcpy_s(&g_sle_uart_find_service_result.uuid, sizeof(sle_uuid_t), &service->uuid, sizeof(sle_uuid_t)); +``` + +至此,连接建立并配对成功,并在client和server都完成一些必要的后续处理,可以相互发送数据了。 + + +## 3 数据收发接口 +sle client接收: + +`void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status);` + +sle client发送: + +`errcode_t ssapc_write_req(uint8_t client_id, uint16_t conn_id, ssapc_write_param_t *param);` + +sle server接收: + +`static void *sle_uart_server_task(const char *arg);` + +`static int32_t sle_uart_server_receive_msgqueue(uint8_t *buffer_addr, uint32_t *buffer_size);` + +sle server发送: + +`errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint16_t len);` + +`errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len);` + + diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart.c b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart.c new file mode 100644 index 0000000000000000000000000000000000000000..a7dd9070b2ff8728019255b95c911dd609beeb6c --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart.c @@ -0,0 +1,325 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: SLE UART Sample Source. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-17, Create file. \n + */ +#include "common_def.h" +#include "soc_osal.h" +#include "app_init.h" +#include "pinctrl.h" +#include "uart.h" +#include "pm_clock.h" +#include "sle_low_latency.h" +#if defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) +#include "securec.h" +#include "sle_uart_server.h" +#include "sle_uart_server_adv.h" +#include "sle_device_discovery.h" +#include "sle_errcode.h" +#elif defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) +#define SLE_UART_TASK_STACK_SIZE 0x600 +#include "sle_connection_manager.h" +#include "sle_ssap_client.h" +#include "sle_uart_client.h" +#endif /* CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT */ + +#define SLE_UART_TASK_PRIO 28 +#define SLE_UART_TASK_DURATION_MS 2000 +#define SLE_UART_BAUDRATE 115200 +#define SLE_UART_TRANSFER_SIZE 256 + +static uint8_t g_app_uart_rx_buff[SLE_UART_TRANSFER_SIZE] = { 0 }; + +static uart_buffer_config_t g_app_uart_buffer_config = { + .rx_buffer = g_app_uart_rx_buff, + .rx_buffer_size = SLE_UART_TRANSFER_SIZE +}; + +static void uart_init_pin(void) +{ + if (CONFIG_SLE_UART_BUS == 0) { + uapi_pin_set_mode(CONFIG_UART_TXD_PIN, HAL_PIO_UART_L0_TXD); + uapi_pin_set_mode(CONFIG_UART_RXD_PIN, HAL_PIO_UART_L0_RXD); + }else if (CONFIG_SLE_UART_BUS == 1) { + uapi_pin_set_mode(CONFIG_UART_TXD_PIN, HAL_PIO_UART_H0_TXD); + uapi_pin_set_mode(CONFIG_UART_RXD_PIN, HAL_PIO_UART_H0_RXD); + }else if (CONFIG_SLE_UART_BUS == 2) { + uapi_pin_set_mode(CONFIG_UART_TXD_PIN, HAL_PIO_UART_L1_TXD); + uapi_pin_set_mode(CONFIG_UART_RXD_PIN, HAL_PIO_UART_L1_RXD); + } +} + +static void uart_init_config(void) +{ + uart_attr_t attr = { + .baud_rate = SLE_UART_BAUDRATE, + .data_bits = UART_DATA_BIT_8, + .stop_bits = UART_STOP_BIT_1, + .parity = UART_PARITY_NONE + }; + + uart_pin_config_t pin_config = { + .tx_pin = CONFIG_UART_TXD_PIN, + .rx_pin = CONFIG_UART_RXD_PIN, + .cts_pin = PIN_NONE, + .rts_pin = PIN_NONE + }; + uapi_uart_deinit(CONFIG_SLE_UART_BUS); + uapi_uart_init(CONFIG_SLE_UART_BUS, &pin_config, &attr, NULL, &g_app_uart_buffer_config); + +} + +#if defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) +#define SLE_UART_SERVER_DELAY_COUNT 5 + +#define SLE_UART_TASK_STACK_SIZE 0x1200 +#define SLE_ADV_HANDLE_DEFAULT 1 +#define SLE_UART_SERVER_MSG_QUEUE_LEN 5 +#define SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE 32 +#define SLE_UART_SERVER_QUEUE_DELAY 0xFFFFFFFF +#define SLE_UART_SERVER_BUFF_MAX_SIZE 800 +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE +#define SLE_UART_SERVER_SEND_BUFF_MAX_LEN 250 +#else +#define SLE_UART_SERVER_SEND_BUFF_MAX_LEN 40 +#endif +unsigned long g_sle_uart_server_msgqueue_id; +#define SLE_UART_SERVER_LOG "[sle uart server]" +static void ssaps_server_read_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_read_cb_t *read_cb_para, + errcode_t status) +{ + osal_printk("%s ssaps read request cbk callback server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, read_cb_para->handle, status); +} +static void ssaps_server_write_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para, + errcode_t status) +{ + osal_printk("%s ssaps write request callback cbk server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, write_cb_para->handle, status); + if ((write_cb_para->length > 0) && write_cb_para->value) { + uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)write_cb_para->value, write_cb_para->length, 0); + } +} + +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +uint8_t g_buff[SLE_UART_SERVER_BUFF_MAX_SIZE] = {0}; +uint16_t g_uart_buff_len = 0; +uint8_t g_buff_data_valid = 0; +uint8_t g_mcs_flag = 0; +#endif +static void sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + unused(error); + if (sle_uart_client_is_connected()) { +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE + g_buff_data_valid = 1; + g_uart_buff_len = 0; + (void)memcpy_s(g_buff, SLE_UART_SERVER_SEND_BUFF_MAX_LEN, buffer, length); + g_uart_buff_len = length; +#else + sle_uart_server_send_report_by_handle(buffer, length); +#endif + } else { + osal_printk("%s sle client is not connected! \r\n", SLE_UART_SERVER_LOG); + } +} + +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +uint8_t *sle_uart_low_latency_tx_cbk(uint16_t *len) +{ +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE + if (g_mcs_flag == 0) { + sle_uart_server_sample_set_mcs(get_connect_id()); + } + g_uart_buff_len = SLE_UART_SERVER_SEND_BUFF_MAX_LEN; + g_buff_data_valid = 1; + g_mcs_flag = 1; +#endif + if (g_buff_data_valid == 0) { + return NULL; + } + if (g_uart_buff_len == 0) { + return NULL; + } + *len = g_uart_buff_len; + g_buff_data_valid = 0; + return g_buff; +} + +void sle_uart_low_latency_tx_cbk_register(void) +{ + sle_low_latency_tx_callbacks_t cbk_func = {0}; + cbk_func.low_latency_tx_cb = sle_uart_low_latency_tx_cbk; + sle_low_latency_tx_register_callbacks(&cbk_func); +} +#endif + +static void sle_uart_server_create_msgqueue(void) +{ + if (osal_msg_queue_create("sle_uart_server_msgqueue", SLE_UART_SERVER_MSG_QUEUE_LEN, \ + (unsigned long *)&g_sle_uart_server_msgqueue_id, 0, SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE) != OSAL_SUCCESS) { + osal_printk("^%s sle_uart_server_create_msgqueue message queue create failed!\n", SLE_UART_SERVER_LOG); + } +} + +static void sle_uart_server_delete_msgqueue(void) +{ + osal_msg_queue_delete(g_sle_uart_server_msgqueue_id); +} + +static void sle_uart_server_write_msgqueue(uint8_t *buffer_addr, uint16_t buffer_size) +{ + osal_msg_queue_write_copy(g_sle_uart_server_msgqueue_id, (void *)buffer_addr, \ + (uint32_t)buffer_size, 0); +} + +static int32_t sle_uart_server_receive_msgqueue(uint8_t *buffer_addr, uint32_t *buffer_size) +{ + return osal_msg_queue_read_copy(g_sle_uart_server_msgqueue_id, (void *)buffer_addr, \ + buffer_size, SLE_UART_SERVER_QUEUE_DELAY); +} +static void sle_uart_server_rx_buf_init(uint8_t *buffer_addr, uint32_t *buffer_size) +{ + *buffer_size = SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE; + (void)memset_s(buffer_addr, *buffer_size, 0, *buffer_size); +} + +static void *sle_uart_server_task(const char *arg) +{ + unused(arg); + uint8_t rx_buf[SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE] = {0}; + uint32_t rx_length = SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE; + uint8_t sle_connect_state[] = "sle_dis_connect"; + + sle_uart_server_create_msgqueue(); + sle_uart_server_register_msg(sle_uart_server_write_msgqueue); + sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk); + + +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE + sle_uart_low_latency_tx_cbk_register(); +#endif + /* UART pinmux. */ + uart_init_pin(); + + /* UART init config. */ + uart_init_config(); + + uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS); + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_server_read_int_handler); + if (ret != ERRCODE_SUCC) { + osal_printk("%s Register uart callback fail.[%x]\r\n", SLE_UART_SERVER_LOG, ret); + return NULL; + } + while (1) { + sle_uart_server_rx_buf_init(rx_buf, &rx_length); + sle_uart_server_receive_msgqueue(rx_buf, &rx_length); + if (strncmp((const char *)rx_buf, (const char *)sle_connect_state, sizeof(sle_connect_state)) == 0) { + ret = sle_start_announce(SLE_ADV_HANDLE_DEFAULT); + if (ret != ERRCODE_SLE_SUCCESS) { + osal_printk("%s sle_connect_state_changed_cbk,sle_start_announce fail :%02x\r\n", + SLE_UART_SERVER_LOG, ret); + } + } + osal_msleep(SLE_UART_TASK_DURATION_MS); + } + sle_uart_server_delete_msgqueue(); + return NULL; +} +#elif defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) +// sle clinet收到sle server数据通知,触发该回调,并使用串口输出该数据 +// 新增bel,将sle client收到的数据通过ble server发送出去 +#include "ble_uart_server.h" +void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, + errcode_t status) +{ + unused(client_id); + unused(conn_id); + unused(status); + osal_printk("\n sle uart recived data : %s\r\n", data->data); + // 通过ble发送到APP PAI see:"ble_uart_server.h" + // errcode_t ble_uart_server_send_input_report(uint8_t *data, uint16_t len); + // uint8_t ble_uart_get_connection_state(void); + if(ble_uart_get_connection_state()==1) // 连接-1 断线-0 + { + ble_uart_server_send_input_report(data->data, data->data_len); + }else{ + osal_printk("\n ble disconnected\r\n"); + } + // 通过串口发出去 + uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); +} + +void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, + errcode_t status) +{ + unused(client_id); + unused(conn_id); + unused(status); + osal_printk("\n sle uart recived data : %s\r\n", data->data); + uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); +} +// sle client收到串口数据,回调该函数,使用sle发送到sle server +static void sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + unused(error); + ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param(); + uint16_t g_sle_uart_conn_id = get_g_sle_uart_conn_id(); + sle_uart_send_param->data_len = length; + sle_uart_send_param->data = (uint8_t *)buffer; + ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param); +} + +static void *sle_uart_client_task(const char *arg) +{ + unused(arg); + /* UART pinmux. */ + uart_init_pin(); + + /* UART init config. */ + uart_init_config(); + + uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS); + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_client_read_int_handler); + if (ret != ERRCODE_SUCC) { + osal_printk("Register uart callback fail."); + return NULL; + } + + return NULL; +} +#endif /* CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT */ + +static void sle_uart_entry(void) +{ + osal_task *task_handle = NULL; + if (uapi_clock_control(CLOCK_CONTROL_FREQ_LEVEL_CONFIG, CLOCK_FREQ_LEVEL_HIGH) == ERRCODE_SUCC) { + osal_printk("Clock config succ.\r\n"); + } else { + osal_printk("Clock config fail.\r\n"); + } + osal_kthread_lock(); +#if defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_SERVER) + sle_dev_register_cbks(); + task_handle = osal_kthread_create((osal_kthread_handler)sle_uart_server_task, 0, "SLEUartServerTask", + SLE_UART_TASK_STACK_SIZE); +#elif defined(CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT) + sle_uart_client_sample_dev_cbk_register(); + task_handle = osal_kthread_create((osal_kthread_handler)sle_uart_client_task, 0, "SLEUartDongleTask", + SLE_UART_TASK_STACK_SIZE); +#endif /* CONFIG_SAMPLE_SUPPORT_SLE_UART_CLIENT */ + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, SLE_UART_TASK_PRIO); + } + osal_kthread_unlock(); +} + +/* Run the sle_uart_entry. */ +app_run(sle_uart_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.c b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.c new file mode 100644 index 0000000000000000000000000000000000000000..bb40841c9f5074f9ddabdb80e27f2fed2fb103ed --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.c @@ -0,0 +1,325 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: SLE uart sample of client. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-04-03, Create file. \n + */ +#include "common_def.h" +#include "soc_osal.h" +#include "securec.h" +#include "product.h" +#include "bts_le_gap.h" +#include "bts_device_manager.h" +#include "sle_device_manager.h" +#include "sle_device_discovery.h" +#include "sle_connection_manager.h" +#include "sle_uart_client.h" +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +#include "sle_low_latency.h" +#endif +#define SLE_MTU_SIZE_DEFAULT 520 +#define SLE_SEEK_INTERVAL_DEFAULT 100 +#define SLE_SEEK_WINDOW_DEFAULT 100 +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +#define SLE_UART_TASK_DELAY_MS 1000 +#define SLE_UART_WAIT_SLE_CORE_READY_MS 5000 +#define SLE_UART_RECV_CNT 1000 +#define SLE_UART_LOW_LATENCY_2K 2000 +#ifndef SLE_UART_SERVER_NAME +#define SLE_UART_SERVER_NAME "rsu_sle_server" +#endif +#define SLE_UART_CLIENT_LOG "[sle uart client]" + +static ssapc_find_service_result_t g_sle_uart_find_service_result = { 0 }; +static sle_dev_manager_callbacks_t g_sle_dev_mgr_cbk = { 0 }; +static sle_announce_seek_callbacks_t g_sle_uart_seek_cbk = { 0 }; +static sle_connection_callbacks_t g_sle_uart_connect_cbk = { 0 }; +static ssapc_callbacks_t g_sle_uart_ssapc_cbk = { 0 }; +static sle_addr_t g_sle_uart_remote_addr = { 0 }; +ssapc_write_param_t g_sle_uart_send_param = { 0 }; +uint16_t g_sle_uart_conn_id = 0; + +uint16_t get_g_sle_uart_conn_id(void) +{ + return g_sle_uart_conn_id; +} + +ssapc_write_param_t *get_g_sle_uart_send_param(void) +{ + return &g_sle_uart_send_param; +} + +void sle_uart_start_scan(void) +{ + sle_seek_param_t param = { 0 }; + param.own_addr_type = 0; + param.filter_duplicates = 0; + param.seek_filter_policy = 0; + param.seek_phys = 1; + param.seek_type[0] = 1; + param.seek_interval[0] = SLE_SEEK_INTERVAL_DEFAULT; + param.seek_window[0] = SLE_SEEK_WINDOW_DEFAULT; + sle_set_seek_param(¶m); + sle_start_seek(); +} + +static void sle_uart_client_sample_sle_power_on_cbk(uint8_t status) +{ + osal_printk("sle power on: %d.\r\n", status); + enable_sle(); +} + +static void sle_uart_client_sample_sle_enable_cbk(uint8_t status) +{ + osal_printk("sle enable: %d.\r\n", status); + sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb); + sle_uart_start_scan(); +} + +static void sle_uart_client_sample_seek_enable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_enable_cbk,status error\r\n", SLE_UART_CLIENT_LOG); + } +} + +static void sle_uart_client_sample_seek_result_info_cbk(sle_seek_result_info_t *seek_result_data) +{ + osal_printk("%s sle uart scan data :%s\r\n", SLE_UART_CLIENT_LOG, seek_result_data->data); + if (seek_result_data == NULL) { + osal_printk("status error\r\n"); + } else if (strstr((const char *)seek_result_data->data, SLE_UART_SERVER_NAME) != NULL) { + memcpy_s(&g_sle_uart_remote_addr, sizeof(sle_addr_t), &seek_result_data->addr, sizeof(sle_addr_t)); + sle_stop_seek(); + } +} + +static void sle_uart_client_sample_seek_disable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_disable_cbk,status error = %x\r\n", SLE_UART_CLIENT_LOG, status); + } else { + sle_remove_paired_remote_device(&g_sle_uart_remote_addr); + sle_connect_remote_device(&g_sle_uart_remote_addr); + } +} + +void sle_uart_client_sample_dev_cbk_register(void) +{ + g_sle_dev_mgr_cbk.sle_power_on_cb = sle_uart_client_sample_sle_power_on_cbk; + g_sle_dev_mgr_cbk.sle_enable_cb = sle_uart_client_sample_sle_enable_cbk; + sle_dev_manager_register_callbacks(&g_sle_dev_mgr_cbk); +#if (CORE_NUMS < 2) + enable_sle(); +#endif +} + +static void sle_uart_client_sample_seek_cbk_register(void) +{ + g_sle_uart_seek_cbk.seek_enable_cb = sle_uart_client_sample_seek_enable_cbk; + g_sle_uart_seek_cbk.seek_result_cb = sle_uart_client_sample_seek_result_info_cbk; + g_sle_uart_seek_cbk.seek_disable_cb = sle_uart_client_sample_seek_disable_cbk; + sle_announce_seek_register_callbacks(&g_sle_uart_seek_cbk); +} + +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +static void sle_uart_client_sample_set_phy_param(void) +{ +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE + sle_set_phy_t param = {0}; + param.tx_format = 1; // 0 :无线帧类型1(GFSK); 1:无线帧类型2(QPSK) + param.rx_format = 1; // + param.tx_phy = 2; // 0:1M; 1:2M; 2:4M; + param.rx_phy = 2; // + param.tx_pilot_density = 0x2; // 导频密度16:1 + param.rx_pilot_density = 0x2; // 导频密度16:1 + param.g_feedback = 0; + param.t_feedback = 0; + if (sle_set_phy_param(get_g_sle_uart_conn_id(), ¶m) != 0) { + osal_printk("%s sle_set_phy_param fail\r\n", SLE_UART_CLIENT_LOG); + return; + } + osal_printk("%s sle_set_phy_param success\r\n", SLE_UART_CLIENT_LOG); +#else + // 非跑流sample使用原phy参数 +#endif + return; +} +#endif + +static void sle_uart_client_sample_connect_state_changed_cbk(uint16_t conn_id, const sle_addr_t *addr, + sle_acb_state_t conn_state, sle_pair_state_t pair_state, + sle_disc_reason_t disc_reason) +{ + unused(addr); + unused(pair_state); + osal_printk("%s conn state changed disc_reason:0x%x\r\n", SLE_UART_CLIENT_LOG, disc_reason); + g_sle_uart_conn_id = conn_id; + if (conn_state == SLE_ACB_STATE_CONNECTED) { + osal_printk("%s SLE_ACB_STATE_CONNECTED\r\n", SLE_UART_CLIENT_LOG); + if (pair_state == SLE_PAIR_NONE) { + sle_pair_remote_device(&g_sle_uart_remote_addr); + } +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE + sle_uart_client_sample_set_phy_param(); + osal_msleep(SLE_UART_TASK_DELAY_MS); + sle_low_latency_rx_enable(); + sle_low_latency_set(get_g_sle_uart_conn_id(), true, SLE_UART_LOW_LATENCY_2K); +#endif + osal_printk("%s sle_low_latency_rx_enable \r\n", SLE_UART_CLIENT_LOG); + } else if (conn_state == SLE_ACB_STATE_NONE) { + osal_printk("%s SLE_ACB_STATE_NONE\r\n", SLE_UART_CLIENT_LOG); + } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) { + osal_printk("%s SLE_ACB_STATE_DISCONNECTED\r\n", SLE_UART_CLIENT_LOG); + sle_remove_paired_remote_device(&g_sle_uart_remote_addr); + sle_uart_start_scan(); + } else { + osal_printk("%s status error\r\n", SLE_UART_CLIENT_LOG); + } +} + +void sle_uart_client_sample_pair_complete_cbk(uint16_t conn_id, const sle_addr_t *addr, errcode_t status) +{ + osal_printk("%s pair complete conn_id:%d, addr:%02x***%02x%02x\n", SLE_UART_CLIENT_LOG, conn_id, + addr->addr[0], addr->addr[4], addr->addr[5]); + if (status == 0) { + ssap_exchange_info_t info = {0}; + info.mtu_size = SLE_MTU_SIZE_DEFAULT; + info.version = 1; + ssapc_exchange_info_req(0, g_sle_uart_conn_id, &info); + } +} + +static void sle_uart_client_sample_connect_cbk_register(void) +{ + g_sle_uart_connect_cbk.connect_state_changed_cb = sle_uart_client_sample_connect_state_changed_cbk; + g_sle_uart_connect_cbk.pair_complete_cb = sle_uart_client_sample_pair_complete_cbk; + sle_connection_register_callbacks(&g_sle_uart_connect_cbk); +} + +static void sle_uart_client_sample_exchange_info_cbk(uint8_t client_id, uint16_t conn_id, ssap_exchange_info_t *param, + errcode_t status) +{ + osal_printk("%s exchange_info_cbk,pair complete client id:%d status:%d\r\n", + SLE_UART_CLIENT_LOG, client_id, status); + osal_printk("%s exchange mtu, mtu size: %d, version: %d.\r\n", SLE_UART_CLIENT_LOG, + param->mtu_size, param->version); + ssapc_find_structure_param_t find_param = { 0 }; + find_param.type = SSAP_FIND_TYPE_PROPERTY; + find_param.start_hdl = 1; + find_param.end_hdl = 0xFFFF; + ssapc_find_structure(0, conn_id, &find_param); +} + +static void sle_uart_client_sample_find_structure_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_service_result_t *service, + errcode_t status) +{ + osal_printk("%s find structure cbk client: %d conn_id:%d status: %d \r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, status); + osal_printk("%s find structure start_hdl:[0x%02x], end_hdl:[0x%02x], uuid len:%d\r\n", SLE_UART_CLIENT_LOG, + service->start_hdl, service->end_hdl, service->uuid.len); + g_sle_uart_find_service_result.start_hdl = service->start_hdl; + g_sle_uart_find_service_result.end_hdl = service->end_hdl; + memcpy_s(&g_sle_uart_find_service_result.uuid, sizeof(sle_uuid_t), &service->uuid, sizeof(sle_uuid_t)); +} + +static void sle_uart_client_sample_find_property_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_property_result_t *property, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_find_property_cbk, client id: %d, conn id: %d, operate ind: %d, " + "descriptors count: %d status:%d property->handle %d\r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, property->operate_indication, + property->descriptors_count, status, property->handle); + g_sle_uart_send_param.handle = property->handle; + g_sle_uart_send_param.type = SSAP_PROPERTY_TYPE_VALUE; +} + +static void sle_uart_client_sample_find_structure_cmp_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_structure_result_t *structure_result, + errcode_t status) +{ + unused(conn_id); + osal_printk("%s sle_uart_client_sample_find_structure_cmp_cbk,client id:%d status:%d type:%d uuid len:%d \r\n", + SLE_UART_CLIENT_LOG, client_id, status, structure_result->type, structure_result->uuid.len); +} + +static void sle_uart_client_sample_write_cfm_cb(uint8_t client_id, uint16_t conn_id, + ssapc_write_result_t *write_result, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_write_cfm_cb, conn_id:%d client id:%d status:%d handle:%02x type:%02x\r\n", + SLE_UART_CLIENT_LOG, conn_id, client_id, status, write_result->handle, write_result->type); +} + +static void sle_uart_client_sample_ssapc_cbk_register(ssapc_notification_callback notification_cb, + ssapc_notification_callback indication_cb) +{ + g_sle_uart_ssapc_cbk.exchange_info_cb = sle_uart_client_sample_exchange_info_cbk; + g_sle_uart_ssapc_cbk.find_structure_cb = sle_uart_client_sample_find_structure_cbk; + g_sle_uart_ssapc_cbk.ssapc_find_property_cbk = sle_uart_client_sample_find_property_cbk; + g_sle_uart_ssapc_cbk.find_structure_cmp_cb = sle_uart_client_sample_find_structure_cmp_cbk; + g_sle_uart_ssapc_cbk.write_cfm_cb = sle_uart_client_sample_write_cfm_cb; + g_sle_uart_ssapc_cbk.notification_cb = notification_cb; + g_sle_uart_ssapc_cbk.indication_cb = indication_cb; + ssapc_register_callbacks(&g_sle_uart_ssapc_cbk); +} + +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +#include "uart.h" +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE +#include "tcxo.h" +static uint32_t g_sle_recv_count = 0; +static uint64_t g_sle_recv_start_time = 0; +static uint64_t g_sle_recv_end_time = 0; +static uint64_t g_sle_recv_param[2] = { 0 }; +#endif +void sle_uart_client_low_latency_recv_data_cbk(uint16_t len, uint8_t *value) +{ +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE + static uint64_t sle_throughput = 0; + if (value == NULL || len == 0) { + return; + } + g_sle_recv_count++; + if (g_sle_recv_count == 1) { + g_sle_recv_start_time = uapi_tcxo_get_us(); + } else if (g_sle_recv_count == SLE_UART_RECV_CNT) { + g_sle_recv_end_time = uapi_tcxo_get_us(); + g_sle_recv_param[0] = g_sle_recv_count; + g_sle_recv_param[1] = g_sle_recv_end_time - g_sle_recv_start_time; + g_sle_recv_count = 0; + g_sle_recv_end_time = 0; + g_sle_recv_start_time = 0; + uint64_t tmp; + tmp = g_sle_recv_param[1] / 1000; // 1000 代表us转化成ms + sle_throughput = len * SLE_UART_RECV_CNT * 8 / tmp; // 8 代表1byte = 8bit + osal_printk("recv_len = %d, recv_count = %llu\r\n", len, g_sle_recv_param[0]); + osal_printk("diff time:%lluus, throughput:%llukbps\r\n", g_sle_recv_param[1], sle_throughput); + } +#else + osal_printk("uart recv low latency data:\r\n"); + uapi_uart_write(CONFIG_SLE_UART_BUS, value, len, 0); +#endif +} + +void sle_uart_client_low_latency_recv_data_cbk_register(void) +{ + osal_printk("uart recv low latency data register success\r\n"); + sle_low_latency_rx_callbacks_t cbk_func = { NULL }; + cbk_func.low_latency_rx_cb = (low_latency_general_rx_callback)sle_uart_client_low_latency_recv_data_cbk; + sle_low_latency_rx_register_callbacks(&cbk_func); +} +#endif + +void sle_uart_client_init(ssapc_notification_callback notification_cb, ssapc_indication_callback indication_cb) +{ + sle_uart_client_sample_seek_cbk_register(); + sle_uart_client_sample_connect_cbk_register(); + sle_uart_client_sample_ssapc_cbk_register(notification_cb, indication_cb); +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE + sle_uart_client_low_latency_recv_data_cbk_register(); +#endif +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.h b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.h new file mode 100644 index 0000000000000000000000000000000000000000..8a39e2264492cacac4af441ff9831c1aa895e1a7 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_client/sle_uart_client.h @@ -0,0 +1,26 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: SLE UART sample of client. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-04-03, Create file. \n + */ +#ifndef SLE_UART_CLIENT_H +#define SLE_UART_CLIENT_H + +#include "sle_ssap_client.h" + +void sle_uart_client_init(ssapc_notification_callback notification_cb, ssapc_indication_callback indication_cb); + +void sle_uart_start_scan(void); + +uint16_t get_g_sle_uart_conn_id(void); +void sle_uart_client_sample_dev_cbk_register(void); + +ssapc_write_param_t *get_g_sle_uart_send_param(void); +// sle收到数据通知,触发该回调,并使用串口输出该数据 +void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status); +void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status); + +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.c b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.c new file mode 100644 index 0000000000000000000000000000000000000000..2bc6278abe27e92da96575ca2fa45742edf50b7e --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.c @@ -0,0 +1,422 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: SLE UART Server Source. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-17, Create file. \n + */ +#include "common_def.h" +#include "securec.h" +#include "soc_osal.h" +#include "sle_errcode.h" +#include "sle_device_manager.h" +#include "sle_connection_manager.h" +#include "sle_device_discovery.h" +#include "sle_uart_server_adv.h" +#include "sle_uart_server.h" +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE +#include "sle_low_latency.h" +#endif +#define OCTET_BIT_LEN 8 +#define UUID_LEN_2 2 +#define UUID_INDEX 14 +#define BT_INDEX_4 4 +#define BT_INDEX_0 0 +#define UART_BUFF_LENGTH 0x100 + +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* sle server app uuid for test */ +static char g_sle_uuid_app_uuid[UUID_LEN_2] = { 0x12, 0x34 }; +/* server notify property uuid for test */ +static char g_sle_property_value[OCTET_BIT_LEN] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; +/* sle connect acb handle */ +static uint16_t g_sle_conn_hdl = 0; +/* sle server handle */ +static uint8_t g_server_id = 0; +/* sle service handle */ +static uint16_t g_service_handle = 0; +/* sle ntf property handle */ +static uint16_t g_property_handle = 0; +/* sle pair acb handle */ +uint16_t g_sle_pair_hdl; + +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" +#define SLE_SERVER_INIT_DELAY_MS 1000 +static sle_uart_server_msg_queue g_sle_uart_server_msg_queue = NULL; +static uint8_t g_sle_uart_base[] = { 0x37, 0xBE, 0xA8, 0x80, 0xFC, 0x70, 0x11, 0xEA, \ + 0xB7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +uint16_t get_connect_id(void) +{ + return g_sle_conn_hdl; +} + +static void encode2byte_little(uint8_t *_ptr, uint16_t data) +{ + *(uint8_t *)((_ptr) + 1) = (uint8_t)((data) >> 0x8); + *(uint8_t *)(_ptr) = (uint8_t)(data); +} + +static void sle_uuid_set_base(sle_uuid_t *out) +{ + errcode_t ret; + ret = memcpy_s(out->uuid, SLE_UUID_LEN, g_sle_uart_base, SLE_UUID_LEN); + if (ret != EOK) { + sample_at_log_print("%s sle_uuid_set_base memcpy fail\n", SLE_UART_SERVER_LOG); + out->len = 0; + return ; + } + out->len = UUID_LEN_2; +} + +static void sle_uuid_setu2(uint16_t u2, sle_uuid_t *out) +{ + sle_uuid_set_base(out); + out->len = UUID_LEN_2; + encode2byte_little(&out->uuid[UUID_INDEX], u2); +} +static void sle_uart_uuid_print(sle_uuid_t *uuid) +{ + if (uuid == NULL) { + sample_at_log_print("%s uuid_print,uuid is null\r\n", SLE_UART_SERVER_LOG); + return; + } + if (uuid->len == UUID_16BIT_LEN) { + sample_at_log_print("%s uuid: %02x %02x.\n", SLE_UART_SERVER_LOG, + uuid->uuid[14], uuid->uuid[15]); /* 14 15: uuid index */ + } else if (uuid->len == UUID_128BIT_LEN) { + sample_at_log_print("%s uuid: \n", SLE_UART_SERVER_LOG); /* 14 15: uuid index */ + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[0], uuid->uuid[1], + uuid->uuid[2], uuid->uuid[3]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[4], uuid->uuid[5], + uuid->uuid[6], uuid->uuid[7]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[8], uuid->uuid[9], + uuid->uuid[10], uuid->uuid[11]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[12], uuid->uuid[13], + uuid->uuid[14], uuid->uuid[15]); + } +} + +static void ssaps_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id, ssap_exchange_info_t *mtu_size, + errcode_t status) +{ + sample_at_log_print("%s ssaps ssaps_mtu_changed_cbk callback server_id:%x, conn_id:%x, mtu_size:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, mtu_size->mtu_size, status); + if (g_sle_pair_hdl == 0) { + g_sle_pair_hdl = conn_id + 1; + } +} + +static void ssaps_start_service_cbk(uint8_t server_id, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s start service cbk callback server_id:%d, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); +} +static void ssaps_add_service_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add service cbk callback server_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_property_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t service_handle, + uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add property cbk callback server_id:%x, service_handle:%x,handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, service_handle, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_descriptor_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t service_handle, + uint16_t property_handle, errcode_t status) +{ + sample_at_log_print("%s add descriptor cbk callback server_id:%x, service_handle:%x, property_handle:%x, \ + status:%x\r\n", SLE_UART_SERVER_LOG, server_id, service_handle, property_handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_delete_all_service_cbk(uint8_t server_id, errcode_t status) +{ + sample_at_log_print("%s delete all service callback server_id:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, status); +} +static errcode_t sle_ssaps_register_cbks(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback) +{ + errcode_t ret; + ssaps_callbacks_t ssaps_cbk = {0}; + ssaps_cbk.add_service_cb = ssaps_add_service_cbk; + ssaps_cbk.add_property_cb = ssaps_add_property_cbk; + ssaps_cbk.add_descriptor_cb = ssaps_add_descriptor_cbk; + ssaps_cbk.start_service_cb = ssaps_start_service_cbk; + ssaps_cbk.delete_all_service_cb = ssaps_delete_all_service_cbk; + ssaps_cbk.mtu_changed_cb = ssaps_mtu_changed_cbk; + ssaps_cbk.read_request_cb = ssaps_read_callback; + ssaps_cbk.write_request_cb = ssaps_write_callback; + ret = ssaps_register_callbacks(&ssaps_cbk); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_ssaps_register_cbks,ssaps_register_callbacks fail :%x\r\n", SLE_UART_SERVER_LOG, + ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_service_add(void) +{ + errcode_t ret; + sle_uuid_t service_uuid = {0}; + sle_uuid_setu2(SLE_UUID_SERVER_SERVICE, &service_uuid); + ret = ssaps_add_service_sync(g_server_id, &service_uuid, 1, &g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uuid add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_property_add(void) +{ + errcode_t ret; + ssaps_property_info_t property = {0}; + ssaps_desc_info_t descriptor = {0}; + uint8_t ntf_value[] = { 0x01, 0x02 }; + + property.permissions = SLE_UUID_TEST_PROPERTIES; + property.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &property.uuid); + property.value = (uint8_t *)osal_vmalloc(sizeof(g_sle_property_value)); + if (property.value == NULL) { + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(property.value, sizeof(g_sle_property_value), g_sle_property_value, + sizeof(g_sle_property_value)) != EOK) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + ret = ssaps_add_property_sync(g_server_id, g_service_handle, &property, &g_property_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add property fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + descriptor.permissions = SLE_UUID_TEST_DESCRIPTOR; + descriptor.type = SSAP_DESCRIPTOR_CLIENT_CONFIGURATION; + descriptor.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + descriptor.value = (uint8_t *)osal_vmalloc(sizeof(ntf_value)); + if (descriptor.value == NULL) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(descriptor.value, sizeof(ntf_value), ntf_value, sizeof(ntf_value)) != EOK) { + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + ret = ssaps_add_descriptor_sync(g_server_id, g_service_handle, g_property_handle, &descriptor); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add descriptor fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uart_server_add(void) +{ + errcode_t ret; + sle_uuid_t app_uuid = {0}; + + sample_at_log_print("%s sle uart add service in\r\n", SLE_UART_SERVER_LOG); + app_uuid.len = sizeof(g_sle_uuid_app_uuid); + if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) { + return ERRCODE_SLE_FAIL; + } + ssaps_register_server(&app_uuid, &g_server_id); + + if (sle_uuid_server_service_add() != ERRCODE_SLE_SUCCESS) { + ssaps_unregister_server(g_server_id); + return ERRCODE_SLE_FAIL; + } + if (sle_uuid_server_property_add() != ERRCODE_SLE_SUCCESS) { + ssaps_unregister_server(g_server_id); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service, server_id:%x, service_handle:%x, property_handle:%x\r\n", + SLE_UART_SERVER_LOG, g_server_id, g_service_handle, g_property_handle); + ret = ssaps_start_service(g_server_id, g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service out\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + +/* device通过uuid向host发送数据:report */ +errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len) +{ + errcode_t ret; + ssaps_ntf_ind_by_uuid_t param = {0}; + param.type = SSAP_PROPERTY_TYPE_VALUE; + param.start_handle = g_service_handle; + param.end_handle = g_property_handle; + param.value_len = len; + param.value = (uint8_t *)osal_vmalloc(len); + if (param.value == NULL) { + sample_at_log_print("%s send report new fail\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(param.value, param.value_len, data, len) != EOK) { + sample_at_log_print("%s send input report memcpy fail\r\n", SLE_UART_SERVER_LOG); + osal_vfree(param.value); + return ERRCODE_SLE_FAIL; + } + sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, ¶m.uuid); + ret = ssaps_notify_indicate_by_uuid(g_server_id, g_sle_conn_hdl, ¶m); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_send_report_by_uuid,ssaps_notify_indicate_by_uuid fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + osal_vfree(param.value); + return ret; + } + osal_vfree(param.value); + return ERRCODE_SLE_SUCCESS; +} + +/* device通过handle向host发送数据:report */ +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint16_t len) +{ + ssaps_ntf_ind_t param = {0}; + uint8_t receive_buf[UART_BUFF_LENGTH] = { 0 }; /* max receive length. */ + param.handle = g_property_handle; + param.type = SSAP_PROPERTY_TYPE_VALUE; + param.value = receive_buf; + param.value_len = len; + if (memcpy_s(param.value, param.value_len, data, len) != EOK) { + return ERRCODE_SLE_FAIL; + } + return ssaps_notify_indicate(g_server_id, g_sle_conn_hdl, ¶m); +} + +void sle_uart_server_sample_set_mcs(uint16_t conn_id) +{ +#ifdef CONFIG_SAMPLE_SUPPORT_PERFORMANCE_TYPE + if (sle_set_mcs(conn_id, 10) != 0) { // mcs10 + osal_printk("%s sle_set_mcs fail\r\n", SLE_UART_SERVER_LOG); + return; + } + osal_printk("%s sle_set_mcs success\r\n", SLE_UART_SERVER_LOG); +#else + unused(conn_id); + // 非跑流sample使用原mcs参数 +#endif + return; +} + +static void sle_connect_state_changed_cbk(uint16_t conn_id, const sle_addr_t *addr, + sle_acb_state_t conn_state, sle_pair_state_t pair_state, sle_disc_reason_t disc_reason) +{ + uint8_t sle_connect_state[] = "sle_dis_connect"; + sample_at_log_print("%s connect state changed callback conn_id:0x%02x, conn_state:0x%x, pair_state:0x%x, \ + disc_reason:0x%x\r\n", SLE_UART_SERVER_LOG,conn_id, conn_state, pair_state, disc_reason); + sample_at_log_print("%s connect state changed callback addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + if (conn_state == SLE_ACB_STATE_CONNECTED) { + g_sle_conn_hdl = conn_id; +#ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE + sle_low_latency_tx_enable(); + osal_printk("%s sle_low_latency_tx_enable \r\n", SLE_UART_SERVER_LOG); +#endif + } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) { + g_sle_conn_hdl = 0; + g_sle_pair_hdl = 0; + if (g_sle_uart_server_msg_queue != NULL) { + g_sle_uart_server_msg_queue(sle_connect_state, sizeof(sle_connect_state)); + } + } +} + +static void sle_pair_complete_cbk(uint16_t conn_id, const sle_addr_t *addr, errcode_t status) +{ + sample_at_log_print("%s pair complete conn_id:%02x, status:%x\r\n", SLE_UART_SERVER_LOG, + conn_id, status); + sample_at_log_print("%s pair complete addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + g_sle_pair_hdl = conn_id + 1; + ssap_exchange_info_t parameter = { 0 }; + parameter.mtu_size = 520; + parameter.version = 1; + ssaps_set_info(g_server_id, ¶meter); +} + +static errcode_t sle_conn_register_cbks(void) +{ + errcode_t ret; + sle_connection_callbacks_t conn_cbks = {0}; + conn_cbks.connect_state_changed_cb = sle_connect_state_changed_cbk; + conn_cbks.pair_complete_cb = sle_pair_complete_cbk; + ret = sle_connection_register_callbacks(&conn_cbks); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_conn_register_cbks,sle_connection_register_callbacks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +uint16_t sle_uart_client_is_connected(void) +{ + return g_sle_pair_hdl; +} + +/* 初始化uuid server */ +errcode_t sle_uart_server_init(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback) +{ + errcode_t ret; + ret = sle_uart_announce_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_announce_register_cbks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_conn_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_conn_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_ssaps_register_cbks(ssaps_read_callback, ssaps_write_callback); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_ssaps_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + sample_at_log_print("%s init ok\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + +errcode_t sle_enable_server_cbk(void) +{ + errcode_t ret; + ret = sle_uart_server_add(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_add fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_uart_server_adv_init(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_adv_init fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg) +{ + g_sle_uart_server_msg_queue = sle_uart_server_msg; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.h b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.h new file mode 100644 index 0000000000000000000000000000000000000000..b1100dcf55ab97bf49ca5e86ff52a111694a2aab --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server.h @@ -0,0 +1,63 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: SLE uart server Config. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-17, Create file. \n + */ + +#ifndef SLE_UART_SERVER_H +#define SLE_UART_SERVER_H + +#include +#include "sle_ssap_server.h" +#include "errcode.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* Service UUID */ +#define SLE_UUID_SERVER_SERVICE 0x2222 + +/* Property UUID */ +#define SLE_UUID_SERVER_NTF_REPORT 0x2323 + +/* Property Property */ +#define SLE_UUID_TEST_PROPERTIES (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +/* Operation indication */ +#define SLE_UUID_TEST_OPERATION_INDICATION (SSAP_OPERATE_INDICATION_BIT_READ | SSAP_OPERATE_INDICATION_BIT_WRITE) + +/* Descriptor Property */ +#define SLE_UUID_TEST_DESCRIPTOR (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +errcode_t sle_uart_server_init(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback); + +errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len); + +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint16_t len); + +uint16_t sle_uart_client_is_connected(void); + +typedef void (*sle_uart_server_msg_queue)(uint8_t *buffer_addr, uint16_t buffer_size); + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg); + +errcode_t sle_enable_server_cbk(void); + +void sle_uart_server_sample_set_mcs(uint16_t conn_id); + +uint16_t get_connect_id(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server_adv.c b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server_adv.c new file mode 100644 index 0000000000000000000000000000000000000000..6a7c589cfb81a982811cf03d8d5202a889e7cad2 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/obu/sle_uart_server/sle_uart_server_adv.c @@ -0,0 +1,277 @@ +/** + * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n + * + * Description: sle adv config for sle uart server. \n + * Author: @CompanyNameTag \n + * History: \n + * 2023-07-17, Create file. \n + */ +#include "securec.h" +#include "errcode.h" +#include "osal_addr.h" +#include "product.h" +#include "sle_common.h" +#include "sle_uart_server.h" +#include "sle_device_manager.h" +#include "sle_device_discovery.h" +#include "sle_errcode.h" +#include "osal_debug.h" +#include "osal_task.h" +#include "string.h" +#include "sle_uart_server_adv.h" + +/* sle device name */ +#define NAME_MAX_LENGTH 16 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MIN_DEFAULT 0x64 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MAX_DEFAULT 0x64 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MIN_DEFAULT 0xC8 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MAX_DEFAULT 0xC8 +/* 超时时间5000ms,单位10ms */ +#define SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT 0x1F4 +/* 超时时间4990ms,单位10ms */ +#define SLE_CONN_MAX_LATENCY 0x1F3 +/* 广播发送功率 */ +#define SLE_ADV_TX_POWER 10 +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* 最大广播数据长度 */ +#define SLE_ADV_DATA_LEN_MAX 251 +/* 广播名称 */ +static uint8_t sle_local_name[NAME_MAX_LENGTH] = "sle_uart_server"; +#define SLE_SERVER_INIT_DELAY_MS 1000 +#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" + +static uint16_t sle_set_adv_local_name(uint8_t *adv_data, uint16_t max_len) +{ + errno_t ret; + uint8_t index = 0; + + uint8_t *local_name = sle_local_name; + uint8_t local_name_len = sizeof(sle_local_name) - 1; + sample_at_log_print("%s local_name_len = %d\r\n", SLE_UART_SERVER_LOG, local_name_len); + sample_at_log_print("%s local_name: ", SLE_UART_SERVER_LOG); + for (uint8_t i = 0; i < local_name_len; i++) { + sample_at_log_print("0x%02x ", local_name[i]); + } + sample_at_log_print("\r\n"); + adv_data[index++] = local_name_len + 1; + adv_data[index++] = SLE_ADV_DATA_TYPE_COMPLETE_LOCAL_NAME; + ret = memcpy_s(&adv_data[index], max_len - index, local_name, local_name_len); + if (ret != EOK) { + sample_at_log_print("%s memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + return (uint16_t)index + local_name_len; +} + +static uint16_t sle_set_adv_data(uint8_t *adv_data) +{ + size_t len = 0; + uint16_t idx = 0; + errno_t ret = 0; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_disc_level = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_DISCOVERY_LEVEL, + .value = SLE_ANNOUNCE_LEVEL_NORMAL, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_disc_level, len); + if (ret != EOK) { + sample_at_log_print("%s adv_disc_level memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_access_mode = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_ACCESS_MODE, + .value = 0, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_access_mode, len); + if (ret != EOK) { + sample_at_log_print("%s adv_access_mode memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + return idx; +} + +static uint16_t sle_set_scan_response_data(uint8_t *scan_rsp_data) +{ + uint16_t idx = 0; + errno_t ret; + size_t scan_rsp_data_len = sizeof(struct sle_adv_common_value); + + struct sle_adv_common_value tx_power_level = { + .length = scan_rsp_data_len - 1, + .type = SLE_ADV_DATA_TYPE_TX_POWER_LEVEL, + .value = SLE_ADV_TX_POWER, + }; + ret = memcpy_s(scan_rsp_data, SLE_ADV_DATA_LEN_MAX, &tx_power_level, scan_rsp_data_len); + if (ret != EOK) { + sample_at_log_print("%s sle scan response data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += scan_rsp_data_len; + + /* set local name */ + idx += sle_set_adv_local_name(&scan_rsp_data[idx], SLE_ADV_DATA_LEN_MAX - idx); + return idx; +} + +static int sle_set_default_announce_param(void) +{ + errno_t ret; + sle_announce_param_t param = {0}; + uint8_t index; + unsigned char local_addr[SLE_ADDR_LEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + param.announce_mode = SLE_ANNOUNCE_MODE_CONNECTABLE_SCANABLE; + param.announce_handle = SLE_ADV_HANDLE_DEFAULT; + param.announce_gt_role = SLE_ANNOUNCE_ROLE_T_CAN_NEGO; + param.announce_level = SLE_ANNOUNCE_LEVEL_NORMAL; + param.announce_channel_map = SLE_ADV_CHANNEL_MAP_DEFAULT; + param.announce_interval_min = SLE_ADV_INTERVAL_MIN_DEFAULT; + param.announce_interval_max = SLE_ADV_INTERVAL_MAX_DEFAULT; + param.conn_interval_min = SLE_CONN_INTV_MIN_DEFAULT; + param.conn_interval_max = SLE_CONN_INTV_MAX_DEFAULT; + param.conn_max_latency = SLE_CONN_MAX_LATENCY; + param.conn_supervision_timeout = SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT; + param.own_addr.type = 0; + ret = memcpy_s(param.own_addr.addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + if (ret != EOK) { + sample_at_log_print("%s sle_set_default_announce_param data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + sample_at_log_print("%s sle_uart_local addr: ", SLE_UART_SERVER_LOG); + for (index = 0; index < SLE_ADDR_LEN; index++) { + sample_at_log_print("0x%02x ", param.own_addr.addr[index]); + } + sample_at_log_print("\r\n"); + return sle_set_announce_param(param.announce_handle, ¶m); +} + +static int sle_set_default_announce_data(void) +{ + errcode_t ret; + uint8_t announce_data_len = 0; + uint8_t seek_data_len = 0; + sle_announce_data_t data = {0}; + uint8_t adv_handle = SLE_ADV_HANDLE_DEFAULT; + uint8_t announce_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t seek_rsp_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t data_index = 0; + + announce_data_len = sle_set_adv_data(announce_data); + data.announce_data = announce_data; + data.announce_data_len = announce_data_len; + + sample_at_log_print("%s data.announce_data_len = %d\r\n", SLE_UART_SERVER_LOG, data.announce_data_len); + sample_at_log_print("%s data.announce_data: ", SLE_UART_SERVER_LOG); + for (data_index = 0; data_index + + +## OpenharmonySTA模式原始API接口 + +使用Openharmony原始WiFI API接口进行编程,STA模式需要使用原始STA接口以及一些DHCP客户端接口。 + +### STA模式 + +OpenharmonyWiFi STA模式的API接口有: + +| API | 功能说明 | +| ------------------------------------------------------------ | --------------------------------------- | +| `WifiErrorCode EnableWifi(void);` | 开启STA | +| `WifiErrorCode DisableWifi(void);` | 关闭STA | +| `int IsWifiActive(void);` | 查询STA是否已开启 | +| `WifiErrorCode Scan(void);` | 触发扫描 | +| `WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size);` | 获取扫描结果 | +| `WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result);` | 添加热点配置,成功会通过result传出netId | +| `WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size);` | 获取本机所有热点配置 | +| `WifiErrorCode RemoveDevice(int networkId);` | 删除热点配置 | +| `WifiErrorCode ConnectTo(int networkId);` | 连接到热点 | +| `WifiErrorCode Disconnect(void);` | 断开热点连接 | +| `WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result);` | 获取当前连接热点信息 | +| `WifiErrorCode RegisterWifiEvent(WifiEvent* event);` | 注册事件监听 | +| `WifiErrorCode UnRegisterWifiEvent(const WifiEvent* event);` | 解除事件监听 | +| `WifiErrorCode GetDeviceMacAddress(unsigned char* result);` | 获取Mac地址 | +| `WifiErrorCode AdvanceScan(WifiScanParams *params);` | 高级搜索 | + + + +#### DHCP 客户端接口 + +以及DHCP客户端接口: + +| API | 描述 | +| ------------------- | ------------------ | +| netifapi_netif_find | 按名称查找网络接口 | +| netifapi_dhcp_start | 启动DHCP客户端 | +| netifapi_dhcp_stop | 停止DHCP客户端 | + + + +### AP模式 + +使用Openharmony原始WiFI API接口进行编程,AP模式需要使用原始AP模式接口以及一些DHCP服务端接口。 + +OpenharmonyWiFi STA模式的API接口有: + +| API | 说明 | +| ------------------------------------------------------------ | -------------------- | +| `WifiErrorCode EnableHotspot(void);` | 打开AP模式 | +| `WifiErrorCode DisableHotspot(void);` | 关闭AP模式 | +| `WifiErrorCode SetHotspotConfig(const HotspotConfig* config);` | 设置当前热点配置参数 | +| `WifiErrorCode GetHotspotConfig(HotspotConfig* result);` | 获取当前热点配置参数 | +| `int IsHotspotActive(void);` | 查询AP是否已开启 | +| `WifiErrorCode GetStationList(StationInfo* result, unsigned int* size);` | 获取接入的设备列表 | +| `int GetSignalLevel(int rssi, int band);` | 获取信号强度等级 | +| `WifiErrorCode SetBand(int band);` | 设置当前频段 | +| `WifiErrorCode GetBand(int* result);` | 获取当前频段 | + + + +#### DHCP服务端接口 + +以及一些DHCP服务端接口: + +| API | 描述 | +| ----------------------- | -------------------------------- | +| netifapi_netif_set_addr | 设置当前接口的IP、网关、子网掩码 | +| netifapi_dhcps_start | 启动DHCP服务端 | +| netifapi_dhcps_stop | 停止DHCP服务端 | + + +## 如何编译 + +本项目下有两个示例代码,源码位于`demo`目录下; + +1. 将easy_wifi_demo文件夹克隆到本地openharmony源码的applications\sample\wifi-iot\app目录下; +2. 修改openharmony的`applications\sample\wifi-iot\app\BUILD.gn`文件: + * 将其中的 features 改为: + features = [ + "easy_wifi_demo/demo:wifi_demo", + "easy_wifi_demo/src:easy_wifi", + ] +3. 执行编译命令:`python build.py wifiiot` +4. 如需编译AP模式的demo,请修改`demo`目录下的BUILD.gn文件; + * 注释掉`"wifi_connect_demo.c"`行,放开`"wifi_hotspot_demo.c"`行; \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/BUILD.gn b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a41ee408678c229740c8fafd3f761d8eeaf2c493 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (C) 2022 HiHope Open Source Organization . +# 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. + +static_library("wifi_demo") { + sources = [ + "app_demo_iot.c" + ] + + include_dirs = [ + "../src", + "//commonlibrary/utils_lite/include", + "//foundation/communication/wifi_lite/interfaces/wifiservice", + "//applications/sample/wifi-iot/app/rsu/sle_uart_server/", + "//base/startup/init/interfaces/innerkits/include", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//device/soc/hisilicon/ws63v100/sdkv100/include/middleware/services/bts/sle", + "//foundation/communication/sle", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/debug", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/memory", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/schedule", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//base/iothardware/peripheral/interfaces/inner_api", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/drivers/hal/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/porting/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/middleware/utils/common_headers/native", + "//third_party/cJSON", + ] +} diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.c new file mode 100644 index 0000000000000000000000000000000000000000..95d117bac4c39d77ea50c43c7aab44a3fc4dccbf --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 "iot_config.h" +#include "iot_main.h" +#include "iot_profile.h" +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_connecter.h" +#include "cjson_init.h" +#include "app_demo_iot.h" + +#define TASK_SLEEP_1000MS (1000) +#define CN_COMMAND_INDEX "commands/request_id=" + +#if CHIP_WS63 +#define STACK_SIZE 10240 +#define MAX_CONNECT_RETRY_TIMES 16 +#define CONNECT_ATTEMPT_INTERVAL_MS 300 +#else +#define ATTR.STACK_SIZE 10240 +#endif +#include "sle_uart_server.h" +#include +char MqttMsg[20] = {0}; +char *const_water_in = "water_in"; // 浸水示例 +static void DemoMsgRcvCallBack(int qos, char *topic, char *payload) +{ + const char *requesID; + char *tmp; + IoTCmdResp resp; + printf("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", qos, topic, payload); + strncpy(MqttMsg, const_water_in, 8); + // strncpy(MqttMsg, payload, 8); // 全部数据 + sle_uart_server_send_report_by_handle(MqttMsg,strlen(MqttMsg)); // sle 发出去 +} + +/* traffic light:1.control module */ +void IotPublishSample(void) +{ + char topic[] = "RSU"; + char msg[] = "{\"RSU_SLE\":\"RSU_SLE\"}"; + IotSendMsg(0, topic, msg); +} + +static void WifiConnectToHost(int *arg) +{ + (void)arg; + + osDelay(10); + + // setup your AP params + // 设置AP参数,包括SSID、预共享密钥、安全类型(PSK)、netID + WifiDeviceConfig apConfig = {0}; + if (strcpy_s(apConfig.ssid, sizeof(apConfig.ssid), CONFIG_AP_SSID)) { + printf("CONFIG_AP_SSID OK"); + } + + if (strcpy_s(apConfig.preSharedKey, sizeof(apConfig.preSharedKey), CONFIG_AP_PWD)) { + printf("CONFIG_AP_PWD OK"); + } + + apConfig.securityType = WIFI_SEC_TYPE_PSK; + +#if CHIP_WS63 + int netId = -1; + + // demo启动时,系统的WiFi功能可能没有初始化完毕,需要等待不确定的时候。待优化。 + for (uint8_t i = 0; i < MAX_CONNECT_RETRY_TIMES; i++) { + osDelay(CONNECT_ATTEMPT_INTERVAL_MS); + netId = ConnectToHotspot(&apConfig); + if (netId >= 0) { + break; + } + } +#else + int netId = ConnectToHotspot(&apConfig); +#endif + +} + +// /< this is the demo main task entry,here we will set the wifi/cjson/mqtt ready ,and +// /< wait if any work to do in the while +static void DemoEntry(int *arg) +{ + WifiConnectToHost(arg); + CJsonInit(); + IoTMain(); + IoTSetMsgCallback(DemoMsgRcvCallBack); + TimeDelaySeconds(3);//TaskMsleep(30000); // 30000 = 3s连接华为云 +/* 主动上报 */ + while (1) { + TimeDelaySeconds(1);//TaskMsleep(TASK_SLEEP_1000MS); + // IotPublishSample(); + } +} + +// This is the demo entry, we create a task here, and all the works has been done in the demo_entry +#define CN_IOT_TASK_STACKSIZE 0x1000 +#define CN_IOT_TASK_PRIOR 28 +#define CN_IOT_TASK_NAME "AppDemoIot" +static void AppDemoIot(void) +{ + osThreadAttr_t attr; + //IoTWatchDogDisable(); + attr.name = CN_IOT_TASK_NAME; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = CN_IOT_TASK_STACKSIZE; + attr.priority = CN_IOT_TASK_PRIOR; + + if (osThreadNew((osThreadFunc_t)DemoEntry, NULL, &attr) == NULL) { + printf("Failed to create IOTDEMO!\n"); + } else { + printf("IOTDEMO created successfully!\n"); + } +} + +SYS_RUN(AppDemoIot); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.h new file mode 100644 index 0000000000000000000000000000000000000000..58fb6ade0bcb80932b2decea6667315cd3e2b956 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/app_demo_iot.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 APP_DEMO_IOT_H +#define APP_DEMO_IOT_H +extern char MqttMsg[20]; +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_connect_demo.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_connect_demo.c new file mode 100644 index 0000000000000000000000000000000000000000..ec75cc2acc9454ff9933984222fc6d323c9c603a --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_connect_demo.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 "ohos_init.h" +#include "cmsis_os2.h" + +#include "wifi_connecter.h" +#define TEN 10 +#define ONE_HUNDRED 100 + +#if CHIP_WS63 +#define STACK_SIZE 10240 +#define MAX_CONNECT_RETRY_TIMES 16 +#define CONNECT_ATTEMPT_INTERVAL_MS 300 +#else +#define ATTR.STACK_SIZE 10240 +#endif + +static void WifiConnectTask(int *arg) +{ + (void)arg; + + osDelay(TEN); + + // setup your AP params + // 设置AP参数,包括SSID、预共享密钥、安全类型(PSK)、netID + WifiDeviceConfig apConfig = {0}; + if (strcpy_s(apConfig.ssid, sizeof(apConfig.ssid), "r1")) { + printf("OK"); + } + + if (strcpy_s(apConfig.preSharedKey, sizeof(apConfig.preSharedKey), "88888889")) { + printf("OK"); + } + + apConfig.securityType = WIFI_SEC_TYPE_PSK; + +#if CHIP_WS63 + int netId = -1; + + // demo启动时,系统的WiFi功能可能没有初始化完毕,需要等待不确定的时候。待优化。 + for (uint8_t i = 0; i < MAX_CONNECT_RETRY_TIMES; i++) { + osDelay(CONNECT_ATTEMPT_INTERVAL_MS); + netId = ConnectToHotspot(&apConfig); + if (netId >= 0) { + break; + } + } +#else + int netId = ConnectToHotspot(&apConfig); +#endif +} + +static void WifiConnectDemo(void) +{ + osThreadAttr_t attr; + + attr.name = "WifiConnectTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; +#if CHIP_WS63 + attr.stack_size = STACK_SIZE; +#else + attr.stack_size = ATTR.STACK_SIZE; +#endif + attr.priority = osPriorityNormal; + + if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL) { + printf("[WifiConnectDemo] Failed to create WifiConnectTask!\n"); + } +} + +SYS_RUN(WifiConnectDemo); diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_hotspot_demo.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_hotspot_demo.c new file mode 100644 index 0000000000000000000000000000000000000000..8c8eb4b90d2eaea28309937b5fef285406aba074 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/demo/wifi_hotspot_demo.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 "ohos_init.h" +#include "cmsis_os2.h" + +#include "wifi_starter.h" +#define SEVEN 7 +#define TEN 10 +#define ONE_HUNDRED 100 + +#if CHIP_WS63 +#define STACK_SIZE 10240 +#define MAX_CONNECT_RETRY_TIMES 16 +#define CONNECT_ATTEMPT_INTERVAL_MS 300 +#else +#define ATTR.STACK_SIZE 10240 +#endif + +static void WifiHotspotTask(int *arg) +{ + (void)arg; + WifiErrorCode errCode; + HotspotConfig config = {0}; + + // 准备AP的配置参数 + if (strcpy_s(config.ssid, sizeof(config.ssid), "HiSpark-AP")) { + printf("OK"); + } + if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678")) { + printf("OK"); + } + config.securityType = WIFI_SEC_TYPE_PSK; + config.band = HOTSPOT_BAND_TYPE_2G; + config.channelNum = SEVEN; + + osDelay(TEN); + + printf("starting AP ...\r\n"); +#if CHIP_WS63 + // demo启动时,系统的WiFi功能可能没有初始化完毕,需要等待不确定的时候。待优化。 + for (uint8_t i = 0; i < MAX_CONNECT_RETRY_TIMES; i++) { + osDelay(CONNECT_ATTEMPT_INTERVAL_MS); + errCode = StartHotspot(&config); + + if (WIFI_SUCCESS == errCode) { + break; + } + } +#else + errCode = StartHotspot(&config); +#endif + printf("StartHotspot: %d\r\n", errCode); + + int timeout = 60; + while (timeout--) { + printf("After %d seconds Ap will turn off ghjfghjdghdfhd!\r\n", timeout); + osDelay(ONE_HUNDRED); + } + + printf("stop AP ...\r\n"); + StopHotspot(); + printf("stop AP ...\r\n"); + osDelay(TEN); +} + +static void WifiHotspotDemo(void) +{ + osThreadAttr_t attr; + + attr.name = "WifiHotspotTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; +#if CHIP_WS63 + attr.stack_size = STACK_SIZE; +#else + attr.stack_size = ATTR.STACK_SIZE; +#endif + attr.priority = osPriorityNormal; + + if (osThreadNew(WifiHotspotTask, NULL, &attr) == NULL) { + printf("[WifiHotspotDemo] Failed to create WifiHotspotTask!\n"); + } +} + +SYS_RUN(WifiHotspotDemo); + diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/BUILD.gn b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..bb75196c1ee5fec0a01bde26c9e52650f514be1c --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (C) 2022 HiHope Open Source Organization . +# 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. + +static_library("easy_wifi") { + sources = [ + "wifi_connecter.c", + "wifi_starter.c", + "cjson_init.c", + "iot_main.c", + "iot_profile.c", + ] + + include_dirs = [ + "//commonlibrary/utils_lite/include", +# "//kernel/liteos_m/components/cmsis/2.0", +# "//base/iot_hardware/interfaces/kits/wifiiot_lite", +# "//foundation/communication/interfaces/kits/wifi_lite/wifiservice", + "//foundation/communication/wifi_lite/interfaces/wifiservice", +# "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/", + "//device/soc/hisilicon/ws63v100/sdkv100/open_source/lwip/lwip_v2.1.3/src/include", + "//device/soc/hisilicon/ws63v100/sdkv100/open_source/lwip/lwip_adapter/liteos_207/src/include", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/liteos/liteos_v208.5.0/Huawei_LiteOS/compat/linux/include", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/lock", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/schedule", + "//third_party/cJSON", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/memory/", + "//device/soc/hisilicon/ws63v100/sdkv100/open_source/mqtt/paho.mqtt.c/src/", + "//device/soc/hisilicon/ws63v100/sdkv100/open_source/wpa_supplicant/src/", + ] + + defines = [ "LWIP_CONFIG_FILE=\"lwip/lwipopts_default.h\"" ] +} diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.c new file mode 100644 index 0000000000000000000000000000000000000000..575aaa1d3a2e46467e2994243293b8733bb55268 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 Huawei Device Co., 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 "osal_addr.h" + +static void *CJsonMalloc(size_t sz) +{ + return osal_vmalloc(sz); +} + +static void CJsonFree(void *p) +{ + osal_vfree(p); +} + +void CJsonInit(void) +{ + cJSON_Hooks hooks; + hooks.malloc_fn = CJsonMalloc; + hooks.free_fn = CJsonFree; + cJSON_InitHooks(&hooks); + + return; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.h new file mode 100644 index 0000000000000000000000000000000000000000..7c5b4521e990d01981a007b6235c23515f822264 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/cjson_init.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 CJSON_INIT_H +#define CJSON_INIT_H + +void CJsonInit(void); +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_errno.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_errno.h new file mode 100644 index 0000000000000000000000000000000000000000..64d1a1cec9b0fb89786fb0a518dbe372493f77ef --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_errno.h @@ -0,0 +1,532 @@ +/* + * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 __HI_ERRNO_H__ +#define __HI_ERRNO_H__ + +/***************************************************************************** +* 1、Common error code. Note: 0x8000 0000 is not recommended. If a truncation error occurs, it is considered successful. +* CNcomment:通用错误码。注意0x8000 0000最好别用,产生截断误判为成功 +*****************************************************************************/ +#define HI_ERR_SUCCESS 0 +#define HI_ERR_FAILURE (hi_u32)(-1) +#define HI_ERR_S_FAILURE (-1) +#define HI_ERR_MALLOC_FAILUE 0x80000001 +#define HI_ERR_TIMEOUT 0x80000002 +#define HI_ERR_RECVING 0x80000003 +#define HI_ERR_MEMCPY_S 0x80000004 +#define HI_ERR_MEMSET_S 0x80000005 +#define HI_ERR_SPRINTF_S 0x80000006 +#define HI_ERR_STRCPY_S 0x80000007 + +/***************************************************************************** +* 2、Error codes of the system adaptation layer.CNcomment:系统适配层错误码 +*****************************************************************************/ +/* Task */ +#define HI_ERR_TASK_INVALID_PARAM 0x80000080 +#define HI_ERR_TASK_CREATE_FAIL 0x80000081 +#define HI_ERR_TASK_DELETE_FAIL 0x80000082 +#define HI_ERR_TASK_SUPPEND_FAIL 0x80000083 +#define HI_ERR_TASK_RESUME_FAIL 0x80000084 +#define HI_ERR_TASK_GET_PRI_FAIL 0x80000085 +#define HI_ERR_TASK_SET_PRI_FAIL 0x80000086 +#define HI_ERR_TASK_LOCK_FAIL 0x80000087 +#define HI_ERR_TASK_UNLOCK_FAIL 0x80000088 +#define HI_ERR_TASK_DELAY_FAIL 0x80000089 +#define HI_ERR_TASK_GET_INFO_FAIL 0x8000008A +#define HI_ERR_TASK_REGISTER_SCHEDULE_FAIL 0x8000008B +#define HI_ERR_TASK_NOT_CREATED 0x8000008C + +/* ISR */ +#define HI_ERR_ISR_INVALID_PARAM 0x800000C0 +#define HI_ERR_ISR_REQ_IRQ_FAIL 0x800000C1 +#define HI_ERR_ISR_ADD_JOB_MALLOC_FAIL 0x800000C2 +#define HI_ERR_ISR_ADD_JOB_SYS_FAIL 0x800000C3 +#define HI_ERR_ISR_DEL_IRQ_FAIL 0x800000C4 +#define HI_ERR_ISR_ALREADY_CREATED 0x800000C5 +#define HI_ERR_ISR_NOT_CREATED 0x800000C6 +#define HI_ERR_ISR_ENABLE_IRQ_FAIL 0x800000C7 +#define HI_ERR_ISR_IRQ_ADDR_NOK 0x800000C8 + +/* Memory */ +#define HI_ERR_MEM_INVALID_PARAM 0x80000100 +#define HI_ERR_MEM_CREAT_POOL_FAIL 0x80000101 +#define HI_ERR_MEM_CREATE_POOL_NOT_ENOUGH_HANDLE 0x80000102 +#define HI_ERR_MEM_FREE_FAIL 0x80000103 +#define HI_ERR_MEM_RE_INIT 0x80000104 +#define HI_ERR_MEM_NOT_INIT 0x80000105 +#define HI_ERR_MEM_CREAT_POOL_MALLOC_FAIL 0x80000106 +#define HI_ERR_MEM_GET_INFO_FAIL 0x80000107 +#define HI_ERR_MEM_GET_OS_INFO_NOK 0x80000108 + +/* OSTIMER */ +#define HI_ERR_TIMER_FAILURE 0x80000140 +#define HI_ERR_TIMER_INVALID_PARAM 0x80000141 +#define HI_ERR_TIMER_CREATE_HANDLE_FAIL 0x80000142 +#define HI_ERR_TIMER_START_FAIL 0x80000143 +#define HI_ERR_TIMER_HANDLE_NOT_CREATE 0x80000144 +#define HI_ERR_TIMER_HANDLE_INVALID 0x80000145 +#define HI_ERR_TIMER_STATUS_INVALID 0x80000146 +#define HI_ERR_TIMER_STATUS_START 0x80000147 +#define HI_ERR_TIMER_INVALID_MODE 0x80000148 +#define HI_ERR_TIMER_EXPIRE_INVALID 0x80000149 +#define HI_ERR_TIMER_FUNCTION_NULL 0x8000014A +#define HI_ERR_TIMER_HANDLE_MAXSIZE 0x8000014B +#define HI_ERR_TIMER_MALLOC_FAIL 0x8000014C +#define HI_ERR_TIMER_NOT_INIT 0x8000014D + +/* semaphore */ +#define HI_ERR_SEM_INVALID_PARAM 0x80000180 +#define HI_ERR_SEM_CREATE_FAIL 0x80000181 +#define HI_ERR_SEM_DELETE_FAIL 0x80000182 +#define HI_ERR_SEM_WAIT_FAIL 0x80000183 +#define HI_ERR_SEM_SIG_FAIL 0x80000184 +#define HI_ERR_SEM_WAIT_TIME_OUT 0x80000185 + +/* Mutex */ +#define HI_ERR_MUX_INVALID_PARAM 0x800001C0 +#define HI_ERR_MUX_CREATE_FAIL 0x800001C1 +#define HI_ERR_MUX_DELETE_FAIL 0x800001C2 +#define HI_ERR_MUX_PEND_FAIL 0x800001C3 +#define HI_ERR_MUX_POST_FAIL 0x800001C4 + +/* message */ +#define HI_ERR_MSG_INVALID_PARAM 0x80000200 +#define HI_ERR_MSG_CREATE_Q_FAIL 0x80000201 +#define HI_ERR_MSG_DELETE_Q_FAIL 0x80000202 +#define HI_ERR_MSG_WAIT_FAIL 0x80000203 +#define HI_ERR_MSG_SEND_FAIL 0x80000204 +#define HI_ERR_MSG_GET_Q_INFO_FAIL 0x80000205 +#define HI_ERR_MSG_Q_DELETE_FAIL 0x80000206 +#define HI_ERR_MSG_WAIT_TIME_OUT 0x80000207 + +/* events */ +#define HI_ERR_EVENT_INVALID_PARAM 0x80000240 +#define HI_ERR_EVENT_CREATE_NO_HADNLE 0x80000241 +#define HI_ERR_EVENT_CREATE_SYS_FAIL 0x80000242 +#define HI_ERR_EVENT_SEND_FAIL 0x80000243 +#define HI_ERR_EVENT_WAIT_FAIL 0x80000244 +#define HI_ERR_EVENT_CLEAR_FAIL 0x80000245 +#define HI_ERR_EVENT_RE_INIT 0x80000246 +#define HI_ERR_EVENT_NOT_ENOUGH_MEMORY 0x80000247 +#define HI_ERR_EVENT_NOT_INIT 0x80000248 +#define HI_ERR_EVENT_DELETE_FAIL 0x80000249 +#define HI_ERR_EVENT_WAIT_TIME_OUT 0x8000024A + +/* os Maintenance and test */ +#define HI_ERR_OSSTAT_INVALID_PARAM 0x80000280 +#define HI_ERR_OSSTAT_SYSTEM_CALL_ERROR 0x80000281 + +/* liteos fpb */ +#define HI_ERR_FPB_COMP_REPEAT 0x800002C0 +#define HI_ERR_FPB_NO_COMP 0x800002C1 +#define HI_ERR_FPB_TYPE 0x800002C2 +#define HI_ERR_FPB_NO_FREE_COMP 0x800002C3 +#define HI_ERR_FPB_ADDR_NOT_ALIGN 0x800002C4 +#define HI_ERR_FPB_TARGET_ADDR 0x800002C5 +#define HI_ERR_FPB_BUSY 0x800002C6 /* ????????? */ +#define HI_ERR_FPB_ERROR_INPUT 0x800002C7 + +/* CPU */ +#define HI_ERR_CPUP_NOT_INIT 0x80000300 +#define HI_ERR_CPUP_INVALID_PARAM 0x80000301 +#define HI_ERR_CPU_CLK_INVALID_PARAM 0x80000302 + +/* file system */ +#define HI_ERR_FS_INVALID_PARAM 0x80000400 +#define HI_ERR_FS_NO_DEVICE 0x80000401 +#define HI_ERR_FS_NO_SPACE 0x80000402 /* No space left on device */ +#define HI_ERR_FS_BAD_DESCRIPTOR 0x80000403 +#define HI_ERR_FS_FILE_EXISTS 0x80000404 +#define HI_ERR_FS_NOT_FOUND 0x80000405 +#define HI_ERR_FS_NAME_TOO_LONG 0x80000406 +#define HI_ERR_FS_READ_ONLY_FS 0x80000407 /* Read-only file system */ +#define HI_ERR_FS_IO_ERROR 0x80000408 +#define HI_ERR_FS_NO_MORE_FILES 0x80000409 + +/***************************************************************************** +* 3、Driver error code +*****************************************************************************/ +/* Serial port */ +#define HI_ERR_UART_INVALID_PARAMETER 0x80001000 +#define HI_ERR_UART_INVALID_SUSPEND 0x80001001 +#define HI_ERR_UART_INVALID_PARITY 0x80001002 +#define HI_ERR_UART_INVALID_DATA_BITS 0x80001003 +#define HI_ERR_UART_INVALID_STOP_BITS 0x80001004 +#define HI_ERR_UART_INVALID_BAUD 0x80001005 +#define HI_ERR_UART_INVALID_COM_PORT 0x80001006 +#define HI_ERR_UART_NOT_SUPPORT_DMA 0x80001007 +#define HI_ERR_UART_NOT_BLOCK_MODE 0x80001008 + +/* gpio */ +#define HI_ERR_GPIO_INVALID_PARAMETER 0x80001040 +#define HI_ERR_GPIO_REPEAT_INIT 0x80001041 +#define HI_ERR_GPIO_NOT_INIT 0x80001042 +#define HI_ERR_GPIO_NOT_SUPPORT 0x80001043 + +/* watchdog */ +#define HI_ERR_WATCHDOG_PARA_ERROR 0x80001080 + +/* Flash */ +#define HI_ERR_FLASH_NOT_INIT 0x800010C0 +#define HI_ERR_FLASH_INVALID_PARAM 0x800010C1 +#define HI_ERR_FLASH_INVALID_PARAM_BEYOND_ADDR 0x800010C2 +#define HI_ERR_FLASH_INVALID_PARAM_SIZE_ZERO 0x800010C3 +#define HI_ERR_FLASH_INVALID_PARAM_ERASE_NOT_ALIGN 0x800010C4 +#define HI_ERR_FLASH_INVALID_PARAM_IOCTRL_DATA_NULL 0x800010C5 +#define HI_ERR_FLASH_INVALID_PARAM_DATA_NULL 0x800010C6 +#define HI_ERR_FLASH_INVALID_PARAM_PKI_MODIFY 0x800010C7 +#define HI_ERR_FLASH_INVALID_PARAM_PAD1 0x800010C8 +#define HI_ERR_FLASH_INVALID_PARAM_PAD2 0x800010C9 +#define HI_ERR_FLASH_INVALID_PARAM_PAD3 0x800010CA +#define HI_ERR_FLASH_TIME_OUT_WAIT_READY 0x800010CB +#define HI_ERR_FLASH_QUAD_MODE_READ_REG1 0x800010CC +#define HI_ERR_FLASH_QUAD_MODE_READ_REG2 0x800010CD +#define HI_ERR_FLASH_QUAD_MODE_COMPARE_REG 0x800010CE +#define HI_ERR_FLASH_NO_MATCH_FLASH 0x800010CF +#define HI_ERR_FLASH_WRITE_ENABLE 0x800010D0 +#define HI_ERR_FLASH_NO_MATCH_ERASE_SIZE 0x800010D1 +#define HI_ERR_FLASH_MAX_SPI_OP 0x800010D2 +#define HI_ERR_FLASH_NOT_SUPPORT_IOCTRL_ID 0x800010D3 +#define HI_ERR_FLASH_INVALID_CHIP_ID 0x800010D4 +#define HI_ERR_FLASH_RE_INIT 0x800010D5 +#define HI_ERR_FLASH_WRITE_NOT_SUPPORT_ERASE 0x800010D6 +#define HI_ERR_FLASH_WRITE_COMPARE_WRONG 0x800010D7 +#define HI_ERR_FLASH_WAIT_CFG_START_TIME_OUT 0x800010D8 +#define HI_ERR_FLASH_PATITION_INIT_FAIL 0x800010D9 +#define HI_ERR_FLASH_INITILIZATION 0x800010DA +#define HI_ERR_FLASH_ERASE_NOT_4K_ALIGN 0x800010DB +#define HI_ERR_FLASH_PROTECT_NOT_SUPPORT 0x800010DC +#define HI_ERR_FLASH_PROTECT_NOT_INIT 0x800010DD +#define HI_ERR_FLASH_PROTECT_RE_INIT 0x800010DE +#define HI_ERR_FLASH_PROTECT_NOT_FIND_CHIP 0x800010DF +#define HI_ERR_FLASH_PROTECT_ADDR_WRONG 0x800010E0 +#define HI_ERR_FLASH_CRYPTO_INVALID_PARAM 0x800010F0 +#define HI_ERR_FLASH_CRYPTO_BEYOND_ADDR_SIZE 0x800010F1 +#define HI_ERR_FLASH_CRYPTO_MALLOC_FAIL 0x800001F2 +#define HI_ERR_FLASH_CRYPTO_DATA_ENCRYPT_ERR 0x800001F3 +#define HI_ERR_FLASH_CRYPTO_DATA_DECRYPT_ERR 0x800001F4 +#define HI_ERR_FLASH_CRYPTO_KEY_EMPTY_ERR 0x800001F5 +#define HI_ERR_FLASH_CRYPTO_MEMCPY_FAIL 0x800001F6 +#define HI_ERR_FLASH_CRYPTO_NOT_SUPPORT 0x800001F7 +#define HI_ERR_FLASH_CRYPTO_PREPARE_ERR 0x800001F8 +#define HI_ERR_FLASH_CRYPTO_KEY_INVALID_ERR 0x800001F9 +#define HI_ERR_FLASH_CRYPTO_KEY_SAVE_ERR 0x800001FA +#define HI_ERR_FLASH_CRYPTO_KERNEL_ADDR_ERR 0x800001FB +#define HI_ERR_FLASH_CRYPTO_ROOT_SALT_EMPTY_ERR 0x800001FC + +/* HRTIMER */ +#define HI_ERR_HRTIMER_ALREADY_INIT 0x80001100 +#define HI_ERR_HRTIMER_NOT_INIT 0x80001101 +#define HI_ERR_HRTIMER_HAVE_NO_AVAILABLE_HANDLE 0x80001102 +#define HI_ERR_HRTIMER_NOT_CREATE_HANDLE 0x80001103 +#define HI_ERR_HRTIMER_IN_START_STATUS 0x80001104 +#define HI_ERR_HRTIMER_NOT_START 0x80001105 +#define HI_ERR_HRTIMER_INVALID_ID 0x80001106 +#define HI_ERR_HRTIMER_INVALID_PARAMETER 0x80001107 +#define HI_ERR_HRTIMER_MALLOC_FAILUE 0x80001108 + +/* hardware timer */ +#define HI_ERR_HWTIMER_INVALID_PARAMETER 0x80001140 +#define HI_ERR_HWTIMER_INITILIZATION_ALREADY 0x80001141 +#define HI_ERR_HWTIMER_NO_INIT 0x80001142 + +/* i2c */ +#define HI_ERR_I2C_NOT_INIT 0x80001180 +#define HI_ERR_I2C_INVALID_PARAMETER 0x80001181 +#define HI_ERR_I2C_TIMEOUT_START 0x80001182 +#define HI_ERR_I2C_TIMEOUT_WAIT 0x80001183 +#define HI_ERR_I2C_TIMEOUT_STOP 0x80001184 +#define HI_ERR_I2C_TIMEOUT_RCV_BYTE 0x80001185 +#define HI_ERR_I2C_TIMEOUT_RCV_BYTE_PROC 0x80001186 +#define HI_ERR_I2C_WAIT_SEM_FAIL 0x80001187 +#define HI_ERR_I2C_START_ACK_ERR 0x80001188 +#define HI_ERR_I2C_WAIT_ACK_ERR 0x80001189 + +/* spi */ +#define HI_ERR_SPI_NOT_INIT 0x800011C0 +#define HI_ERR_SPI_REINIT 0x800011C1 +#define HI_ERR_SPI_PARAMETER_WRONG 0x800011C2 +#define HI_ERR_SPI_BUSY 0x800011C3 +#define HI_ERR_SPI_WRITE_TIMEOUT 0x800011C4 +#define HI_ERR_SPI_READ_TIMEOUT 0x800011C5 +#define HI_ERR_SPI_NOT_SUPPORT_DMA 0x800011C6 + +/* efuse */ +#define HI_ERR_EFUSE_INVALIDATE_ID 0x80001200 +#define HI_ERR_EFUSE_INVALIDATE_PARA 0x80001201 +#define HI_ERR_EFUSE_WRITE_ERR 0x80001202 +#define HI_ERR_EFUSE_INVALIDATE_AUTH 0x80001203 +#define HI_ERR_EFUSE_BUSY 0x80001204 +#define HI_ERR_EFUSE_TIMEOUT 0x80001205 + +/* cipher */ +#define HI_ERR_CIPHER_NOT_INIT 0x80001240 +#define HI_ERR_CIPHER_INVALID_POINT 0x80001241 +#define HI_ERR_CIPHER_INVALID_PARAMETER 0x80001242 +#define HI_ERR_CIPHER_NO_AVAILABLE_RNG 0x80001243 +#define HI_ERR_CIPHER_FAILED_MEM 0x80001244 +#define HI_ERR_CIPHER_OVERFLOW 0x80001245 +#define HI_ERR_CIPHER_TIMEOUT 0x80001246 +#define HI_ERR_CIPHER_UNSUPPORTED 0x80001247 +#define HI_ERR_CIPHER_REGISTER_IRQ 0x80001248 +#define HI_ERR_CIPHER_ILLEGAL_KEY 0x80001249 +#define HI_ERR_CIPHER_INVALID_ADDR 0x8000124A +#define HI_ERR_CIPHER_INVALID_LENGTH 0x8000124B +#define HI_ERR_CIPHER_ILLEGAL_DATA 0x8000124C +#define HI_ERR_CIPHER_RSA_SIGN 0x8000124D +#define HI_ERR_CIPHER_RSA_VERIFY 0x8000124E +#define HI_ERR_CIPHER_RESULT_WARNING 0x8000124F +#define HI_ERR_CIPHER_FLUSH_DCACHE_FAILED 0x80001250 + +/* sdio */ +#define HI_ERR_SDIO_INVALID_PARAMETER 0x80001280 + +/* tsensor */ +#define HI_ERR_TSENSOR_INVALID_PARAMETER 0x800012C0 + +/* adc */ +#define HI_ERR_ADC_PARAMETER_WRONG 0x80001300 +#define HI_ERR_ADC_INVALID_CHANNEL_ID 0x80001301 +#define HI_ERR_ADC_TIMEOUT 0x80001302 +#define HI_ERR_ADC_NOT_INIT 0x80001303 + +/* pmw */ +#define HI_ERR_PWM_NO_INIT 0x80001340 +#define HI_ERR_PWM_INITILIZATION_ALREADY 0x80001341 +#define HI_ERR_PWM_INVALID_PARAMETER 0x80001342 + + +/* dma */ +#define HI_ERR_DMA_INVALID_PARA 0x80001380 +#define HI_ERR_DMA_NOT_INIT 0x80001381 +#define HI_ERR_DMA_BUSY 0x80001382 +#define HI_ERR_DMA_TRANSFER_FAIL 0x80001383 +#define HI_ERR_DMA_TRANSFER_TIMEOUT 0x80001384 +#define HI_ERR_DMA_GET_NOTE_FAIL 0x80001385 +#define HI_ERR_DMA_LLI_NOT_CREATE 0x80001386 +#define HI_ERR_DMA_CH_IRQ_ENABLE_FAIL 0x80001387 +/* audio */ +#define HI_ERR_AUDIO_BUSY 0x800013C0 +#define HI_ERR_AUDIO_INVALID_PARAMETER 0x800013C1 + +/* i2s */ +#define HI_ERR_I2S_INVALID_PARAMETER 0x80001400 +#define HI_ERR_I2S_WRITE_TIMEOUT 0x80001401 +#define HI_ERR_I2S_MALLOC_FAIL 0x80001402 +#define HI_ERR_I2S_MEMCPY_FAIL 0x80001403 + +/* shell */ +#define HI_ERR_SHELL_NOT_INIT 0x80001440 +#define HI_ERR_SHELL_CMDREG_PARA_ERROR 0x80001441 +#define HI_ERR_SHELL_CMDREG_CMD_ERROR 0x80001442 +#define HI_ERR_SHELL_CMDREG_CMD_EXIST 0x80001443 +#define HI_ERR_SHELL_CMDREG_MEMALLOC_ERROR 0x80001444 + +/***************************************************************************** +* 4、Intermediate application error code +*****************************************************************************/ +/* NV */ +#define HI_ERR_NV_FILE_ERR 0x80003000 +#define HI_ERR_NV_MEMCPY_FAIL 0x80003001 +#define HI_ERR_NV_WRITE_FILE_FAIL 0x80003002 +#define HI_ERR_NV_UPDATA_DATA_FAIL 0x80003003 +#define HI_ERR_NV_UPDATA_FILE_FAIL 0x80003004 +#define HI_ERR_NV_NOT_SUPPORT_WRITE 0x80003005 +#define HI_ERR_NV_FSEC_TOTAL_NUM_INVALID 0x80003006 /* 工厂NV项个数非法 */ +#define HI_ERR_NV_FAIL_N_TIMES 0x80003007 +#define HI_ERR_NV_SEM_FAIL 0x80003008 +#define HI_ERR_NV_LEN_ERR 0x80003009 +#define HI_ERR_NV_NOT_FOUND 0x8000300A +#define HI_ERR_NV_FULL 0x8000300B +#define HI_ERR_NV_NOT_ENOUGH_MEMORY 0x8000300C +#define HI_ERR_NV_NOT_SUPPORT 0x8000300D +#define HI_ERR_NV_NOT_SUPPORT_ID 0x8000300E +#define HI_ERR_NV_BAD_DATA 0x8000300F +#define HI_ERR_NV_INVALID_TYPE 0x80003010 +/* NV读取失败 CNcomment:Read NVIM Failure */ +#define HI_ERR_NV_ERROR_READ 0x80003011 +/* NV写失败,长度过长CNcomment:Write Error for Length Overflow */ +#define HI_ERR_NV_NOT_SUPPORT_LENTH 0x80003012 +/* NV写失败,Flash坏块 CNcomment:Write Error for Flash Bad Block */ +#define HI_ERR_NV_BAD_BLOCK 0x80003013 +/* NV写失败,其他错误 CNcomment:Write Error for Unknown Reason */ +#define HI_ERR_NV_ERROR_WRITE 0x80003014 +#define HI_ERR_NV_INITILIZATION 0x80003015 +#define HI_ERR_NV_INVALID_PARAMETER 0x80003016 + +/* Low power consumption */ +#define HI_ERR_LOWPOWER_INVALID_PARAMETER 0x80003040 +#define HI_ERR_LOWPOWER_XTAL_UNSTABLE 0x80003041 +#define HI_ERR_LOWPOWER_XTAL_SAMPLING 0x80003042 + +/* upgrade common error */ +#define HI_ERR_UPG_COMMON 0x80003060 +#define HI_ERR_UPG_NULL_POINTER (HI_ERR_UPG_COMMON + 0x0) +#define HI_ERR_UPG_PARAMETER (HI_ERR_UPG_COMMON + 0x1) +#define HI_ERR_UPG_BACKUP_ADDR (HI_ERR_UPG_COMMON + 0x2) +#define HI_ERR_UPG_BUSY (HI_ERR_UPG_COMMON + 0x3) +#define HI_ERR_UPG_FLASH_BAD (HI_ERR_UPG_COMMON + 0x4) +#define HI_ERR_UPG_START_ADDR (HI_ERR_UPG_COMMON + 0x5) +#define HI_ERR_UPG_INITILIZATION_ALREADY (HI_ERR_UPG_COMMON + 0x6) +#define HI_ERR_UPG_FILE_LEN (HI_ERR_UPG_COMMON + 0x7) +#define HI_ERR_UPG_NOT_START (HI_ERR_UPG_COMMON + 0x8) +#define HI_ERR_UPG_MALLOC_FAIL (HI_ERR_UPG_COMMON + 0x9) +#define HI_ERR_UPG_GET_SECTION_HEAD (HI_ERR_UPG_COMMON + 0xA) +#define HI_ERR_UPG_BUF_LEN (HI_ERR_UPG_COMMON + 0xB) +#define HI_ERR_UPG_FLASH_SIZE (HI_ERR_UPG_COMMON + 0xC) +#define HI_ERR_UPG_NV_SIZE (HI_ERR_UPG_COMMON + 0xD) +#define HI_ERR_UPG_ALREADY_FINISH (HI_ERR_UPG_COMMON + 0xE) +#define HI_ERR_UPG_RSA_KEY_ADDR (HI_ERR_UPG_COMMON + 0xF) +#define HI_ERR_UPG_ECC_KEY_ADDR (HI_ERR_UPG_COMMON + 0x10) +#define HI_ERR_UPG_FILE_LEN_OVER (HI_ERR_UPG_COMMON + 0x11) +#define HI_ERR_UPG_STOP (HI_ERR_UPG_COMMON + 0x12) +#define HI_ERR_UPG_LOW_KERNEL_VER (HI_ERR_UPG_COMMON + 0x13) +#define HI_ERR_UPG_FULL_KERNEL_VER (HI_ERR_UPG_COMMON + 0x14) +#define HI_ERR_UPG_LOW_BOOT_VER (HI_ERR_UPG_COMMON + 0x15) +#define HI_ERR_UPG_FULL_BOOT_VER (HI_ERR_UPG_COMMON + 0x16) +#define HI_ERR_UPG_FIRST_PACKET_OFFSET (HI_ERR_UPG_COMMON + 0x17) +#define HI_ERR_UPG_UPDATE_VER_TIMEOUT (HI_ERR_UPG_COMMON + 0x18) +#define HI_ERR_UPG_UPDATE_VER_FAIL (HI_ERR_UPG_COMMON + 0x19) +#define HI_ERR_UPG_UPDATE_VER_INVALID_PARAM (HI_ERR_UPG_COMMON + 0x1A) +#define HI_ERR_UPG_KERNEL_VER_OVER (HI_ERR_UPG_COMMON + 0x1B) +#define HI_ERR_UPG_BOOT_VER_OVER (HI_ERR_UPG_COMMON + 0x1C) +#define HI_ERR_UPG_KERNEL_LEN (HI_ERR_UPG_COMMON + 0x1D) +#define HI_ERR_UPG_KEY_LEN (HI_ERR_UPG_COMMON + 0x1E) + +/* upgrade file check error */ +#define HI_ERR_UPG_CHECK 0x80003080 +#define HI_ERR_UPG_IMAGE_ID (HI_ERR_UPG_CHECK + 0x0) +#define HI_ERR_UPG_FILE_TYPE (HI_ERR_UPG_CHECK + 0x1) +#define HI_ERR_UPG_HEAD_LEN (HI_ERR_UPG_CHECK + 0x2) +#define HI_ERR_UPG_SIGN_ALG (HI_ERR_UPG_CHECK + 0x3) +#define HI_ERR_UPG_RSA_KEY_LEN (HI_ERR_UPG_CHECK + 0x4) +#define HI_ERR_UPG_RSA_HEAD_SIGN (HI_ERR_UPG_CHECK + 0x5) +#define HI_ERR_UPG_ECC_KEY_LEN (HI_ERR_UPG_CHECK + 0x6) +#define HI_ERR_UPG_ECC_HEAD_SIGN (HI_ERR_UPG_CHECK + 0x7) +#define HI_ERR_UPG_COMMON_SHA256 (HI_ERR_UPG_CHECK + 0x8) +#define HI_ERR_UPG_SECTION_SHA256 (HI_ERR_UPG_CHECK + 0x9) +#define HI_ERR_UPG_KERNEL_VER (HI_ERR_UPG_CHECK + 0xA) +#define HI_ERR_UPG_BOOT_VER (HI_ERR_UPG_CHECK + 0xB) +#define HI_ERR_UPG_BOOT_HEAD (HI_ERR_UPG_CHECK + 0xC) +#define HI_ERR_UPG_BOOT_LEN (HI_ERR_UPG_CHECK + 0xD) +#define HI_ERR_UPG_BOOT_ROOT_KEY (HI_ERR_UPG_CHECK + 0xE) +#define HI_ERR_UPG_BOOT_ROOT_KEY_LEN (HI_ERR_UPG_CHECK + 0xF) +#define HI_ERR_UPG_BOOT_KEY_ID (HI_ERR_UPG_CHECK + 0x10) +#define HI_ERR_UPG_BOOT_SIGN_ALG (HI_ERR_UPG_CHECK + 0x11) +#define HI_ERR_UPG_BOOT_SUB_KEY (HI_ERR_UPG_CHECK + 0x12) +#define HI_ERR_UPG_BOOT_SUB_KEY_CAT (HI_ERR_UPG_CHECK + 0x13) +#define HI_ERR_UPG_BOOT_SUB_KEY_RSIM (HI_ERR_UPG_CHECK + 0x14) +#define HI_ERR_UPG_BOOT_DIE_ID (HI_ERR_UPG_CHECK + 0x15) +#define HI_ERR_UPG_BOOT_HASH (HI_ERR_UPG_CHECK + 0x16) +#define HI_ERR_UPG_BOOT_SUB_KEY_LEN (HI_ERR_UPG_CHECK + 0x17) +#define HI_ERR_UPG_USER_VERIFY (HI_ERR_UPG_CHECK + 0x18) +#define HI_ERR_UPG_BOOT_SUB_KEY_HASH (HI_ERR_UPG_CHECK + 0x19) +#define HI_ERR_UPG_BOOT_SECTION_HASH (HI_ERR_UPG_CHECK + 0x1A) +#define HI_ERR_UPG_COMMON_HASH (HI_ERR_UPG_CHECK + 0x1B) +#define HI_ERR_UPG_SECTION_HASH (HI_ERR_UPG_CHECK + 0x1C) +#define HI_ERR_UPG_ADD_START (HI_ERR_UPG_CHECK + 0x1D) +#define HI_ERR_UPG_ENCRYPT_NOT_MATCH (HI_ERR_UPG_CHECK + 0x1E) +#define HI_ERR_UPG_OTA_NOT_MATCH (HI_ERR_UPG_CHECK + 0x1F) + +/* upgrade file crypto error */ +#define HI_ERR_UPG_CRYPTO 0x800030A0 +#define HI_ERR_UPG_CRYPTO_PREPARE_ERR (HI_ERR_UPG_CRYPTO + 0x0) +#define HI_ERR_UPG_FILE_DECRYPT_ERR (HI_ERR_UPG_CRYPTO + 0x1) +#define HI_ERR_UPG_NOT_WITH_CACHE_MODE_ERR (HI_ERR_UPG_CRYPTO + 0x2) +#define HI_ERR_UPG_NOT_DOWNLOAD_FINISH (HI_ERR_UPG_CRYPTO + 0x3) + +/* DIAG */ +#define HI_ERR_DIAG_NOT_FOUND 0x800030C0 +#define HI_ERR_DIAG_INVALID_ID 0x800030C1 +#define HI_ERR_DIAG_FULL 0x800030C2 +#define HI_ERR_DIAG_CONSUMED 0x800030C3 +#define HI_ERR_DIAG_CONTINUE 0x800030C4 +#define HI_ERR_DIAG_TOO_SMALL_BUFFER 0x800030C5 +#define HI_ERR_DIAG_NO_MORE_DATA 0x800030C6 +#define HI_ERR_DIAG_NOT_ENOUGH_MEMORY 0x800030C7 +#define HI_ERR_DIAG_INVALID_HEAP_ADDR 0x800030C8 +#define HI_ERR_DIAG_NOT_CONNECT 0x800030C9 +#define HI_ERR_DIAG_BUSY 0x800030CA +#define HI_ERR_DIAG_TOO_LARGE_FRAME 0x800030CB +#define HI_ERR_DIAG_RAM_ALIGN 0x800030CC +#define HI_ERR_DIAG_NOT_SUPPORT 0x800030CD +#define HI_ERR_DIAG_UNAVAILABLE 0x800030CE +#define HI_ERR_DIAG_CFG_NOT_ALLOW 0x800030CF +#define HI_ERR_DIAG_INVALID_CODE_ADDR 0x800030D0 +#define HI_ERR_DIAG_OBJ_NOT_FOUND 0x800030D1 +#define HI_ERR_DIAG_QUEUE_FULL 0x800030D2 +#define HI_ERR_DIAG_NO_MORE_MEMORY 0x800030D3 +#define HI_ERR_DIAG_SYSTEM_CALL_ERROR 0x800030D4 +#define HI_ERR_DIAG_NO_INIT 0x800030D5 +#define HI_ERR_DIAG_INVALID_PARAMETER 0x800030D6 +#define HI_ERR_DIAG_STAT_NOT_SUPPORT 0x800030D7 +#define HI_ERR_DIAG_ID_OR_CALLBACK_ALREADY_REGISTERED 0x800030D8 +#define HI_ERR_DIAG_SET_CONN_ACK_INFO 0x800030D9 +#define HI_ERR_DIAG_CMD_NUM_EXCEED_UPPER_LIMIT 0x800030DA + +/* reset */ +#define HI_ERR_RESET_TOO_LARGE_DATA 0x80003100 +#define HI_ERR_RESET_INVALID_PARAMETER 0x80003101 + +/* syserror */ +#define HI_ERR_SYSERROR_NOT_FOUND 0x80003140 +#define HI_ERR_SYSERROR_INVALID_PARAMETER 0x80003141 + + +/* APP */ +#define HI_ERR_APP_INITILIZATION_ALREADY 0x80003180 +#define HI_ERR_APP_INVALID_PARAMETER 0x80003181 + +/* CRC */ +#define HI_ERR_CRC_INVALID_PARAMETER 0x800031C0 + + +/* sigma */ +#define HI_ERR_SIGMA_INVALID_PARAMETER 0x80003200 + +/* data collect */ +#define HI_ERR_DATACOLLECT_INVALID_PARAMETER 0x80003240 +#define HI_ERR_DATACOLLECT_BUSY 0x80003241 + +/* AT */ +#define HI_ERR_AT_NAME_OR_FUNC_REPEAT_REGISTERED 0x80003280 +#define HI_ERR_AT_INVALID_PARAMETER 0x80003281 + +/***************************************************************************** +* 5、Protocol stack error codes +*****************************************************************************/ +/* wifi */ +/* Note: The base value of the Wi-Fi error code hi_err_code_enum is changed to the start value, + * and other error codes are delayed at one time. + * CNcomment:说明:wifi错误码hi_err_code_enum 将base基值修改为起始值,其他错误码一次延顺 + */ +/* dmac */ +#define HI_ERR_WIFI_DMAC_NOT_SUPPORT 0x80004000 + +/* hmac */ +#define HI_ERR_WIFI_HMAC_INVALID_PARAMETER 0x80004040 + +/* wal */ +#define HI_ERR_WIFI_WAL_MALLOC_FAIL 0x80004080 +#define HI_ERR_WIFI_WAL_FAILURE 0x80004081 +#define HI_ERR_WIFI_WAL_BUSY 0x80004082 +#define HI_ERR_WIFI_WAL_INVALID_PARAMETER 0x80004083 + +/***************************************************************************** +* 6、128 units reserved for Hisi.CNcomment:Hisi保留 unit128个 +* 7、64 units reserved for customer use. CNcomment:客户使用预留 unit64个 +*****************************************************************************/ +#endif /* __HI_ERRNO_H__ */ diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_stdlib.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_stdlib.h new file mode 100644 index 0000000000000000000000000000000000000000..1b57e9d9709a3300770f778ce21c687b97f25c39 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_stdlib.h @@ -0,0 +1,311 @@ +/** +* @file hi_stdlib.h +* +* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. +* 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. +* +* Description: A parameter is added to the security C APIs based on the standard C interface, that is, the upper +* limit of the write operation address space to prevent out-of-bounds write. +CNcomment:安全C接口主要是基于标准C接口增加了一个参数,即写操作地址空间上限,从而防止写越界。CNend \n +* Create: 2019-12-18 +*/ + +/** + * @defgroup iot_secure_c Secure C library APIs + * @ingroup iot_secure + */ + +#ifndef __HI_STDLIB_H__ +#define __HI_STDLIB_H__ +#include "hi_types_base.h" + +HI_START_HEADER + +#ifndef EOK +#define EOK 0 +#endif + +#ifndef errno_t +typedef int errno_t; +#endif + +#ifndef size_t +typedef unsigned int size_t; +#endif + +/** +* @ingroup iot_secure_c +* @brief Copies the source string to the destination buffer.CNcomment:复制源字符串到目的缓冲区。CNend +* +* @par 描述: Copies the source string to the destination buffer. +CNcomment:复制源字符串到目的缓冲区。CNend +* +* @attention None +* +* @param dest [OUT] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t strcpy_s(char *dest, size_t dest_max, const char *src); + +/** +* @ingroup iot_secure_c +* @brief Copies the source string of a specified length to the destination buffer. +CNcomment:复制指定长度源字符串到目的缓冲区。CNend +* +* @par 描述:Copies the source string of a specified length to the destination buffer. +CNcomment:复制指定长度源字符串到目的缓冲区。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* @param count [IN] type #size_t, Number of characters copied from the source buffer. +CNcomment:从源缓冲区中复制的字符数。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t strncpy_s(char *dest, size_t dest_max, const char *src, size_t count); + +/** +* @ingroup iot_secure_c +* @brief Concatenates the source string to the end of the destination string. +CNcomment:将源字符串连接到目的字符串后面CNend +* +* @par 描述:Concatenates the source string to the end of the destination string. +CNcomment:将源字符串连接到目的字符串后面。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t strcat_s(char *dest, size_t dest_max, const char *src); + +/** +* @ingroup iot_secure_c +* @brief Concatenates the source string of a specified length to the end of the destination string. +CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend +* +* @par 描述: Concatenates the source string of a specified length to the end of the destination string. +CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* @param count [IN] type #size_t, Number of characters copied from the source buffer. +CNcomment:从源缓冲区连接的字符数。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t strncat_s(char *dest, size_t dest_max, const char *src, size_t count); + +/** +* @ingroup iot_secure_c +* @brief Copies the data from the source buffer to the destination buffer. +CNcomment:复制源缓冲区的数据到目的缓冲区。CNend +* +* @par 描述: Copies the data from the source buffer to the destination buffer. +CNcomment:复制源缓冲区的数据到目的缓冲区。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* @param count [IN] type #size_t, Number of characters copied from the source buffer. +CNcomment:从源缓冲区中复制的字符数。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t memcpy_s(void *dest, size_t dest_max, const void *src, size_t count); + +/** +* @ingroup iot_secure_c +* @brief Sets the size of the destination buffer to a specific value. +CNcomment:设置目的缓冲区为特定值。CNend +* +* @par 描述: Sets the size of the destination buffer to a specific value. +CNcomment:设置目的缓冲区为特定值。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param c [IN] type #const #char *, Source buffer.CNcomment:特定值。CNend +* @param count [IN] type #size_t, Number of characters copied from the source buffer. +CNcomment:设置为特定值的字符数。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t memset_s(void *dest, size_t dest_max, int c, size_t count); + +/** +* @ingroup iot_secure_c +* @brief Moves the data from the source buffer to the destination buffer. +CNcomment:移动源缓冲区的数据到目的缓冲区。CNend +* +* @par 描述:Moves the data from the source buffer to the destination buffer. +CNcomment:移动源缓冲区的数据到目的缓冲区。CNend +* @attention None +* +* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend +* @param count [IN] type #size_t, Number of characters copied from the source buffer. +CNcomment:从源缓冲区中移动的字符数。CNend +* +* @retval #EOK Success +* @retval #Other Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern errno_t memmove_s(void *dest, size_t dest_max, const void *src, size_t count); + +/** +* @ingroup iot_secure_c +* @brief Splits a string into substrings according to the specified separators. +CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend +* +* @par 描述: Splits a string into substrings according to the specified separators. +CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend +* @attention None +* +* @param token [IN] type #char *。 String to be split.CNcomment:要分割的字符串。CNend +* @param delimit [IN] type #const char *。 String separator.CNcomment:字符串分隔符。CNend +* @param context [IN] type #char** 。Position information after a call to HI_strtok_s is saved. +CNcomment:保存调用HI_strtok_s后的位置信息。CNend +* +* @retval #char* Point to the next token. CNcomment:指向在token中的下一个token。CNend +* @retval #HI_NULL A specified substring is not found or an error occurs. +CNcomment:没有找到指定的子字符串或者发生错误。CNend +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern char *strtok_s(char *token, const char *delimit, char **context); + +/** +* @ingroup iot_secure_c +* @brief Formats the data and outputs it to the destination buffer. +CNcomment:将数据格式化输出到目的缓冲区。CNend +* +* @par 描述: Formats the data and outputs it to the destination buffer. +CNcomment:将数据格式化输出到目的缓冲区。CNend +* @attention None +* +* @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend +* @param ... [IN] Optional parameter CNcomment:可选参数。CNend +* +* @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. +CNcomment:返回存储在dest的字节数,不包括结束符CNend +* @retval #-1 Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern int sprintf_s(char *dest, size_t dest_max, const char *format, ...); + +/** +* @ingroup iot_secure_c +* @brief Formats the data according to a specified length and outputs the data to the destination buffer. +CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend +* +* @par 描述: Formats the data according to a specified length and outputs the data to the destination buffer. +CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend +* @attention None +* +* @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend +* @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend +* @param count [IN] type #size_t。 Number of formatted characters to be output to the destination buffer. +CNcomment:要输出到目的缓冲区的格式化字符个数。CNend +* @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend +* @param ... [IN] Optional parameter CNcomment:可选参数。CNend +* +* @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. +CNcomment:返回存储在dest的字节数,不包括结束符CNend +* @retval #-1 Failure +* +* @par 依赖: +* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend +* @see None +*/ +extern int snprintf_s(char *dest, size_t dest_max, size_t count, const char *format, ...); + +extern int memcmp(const void *str1, const void *str2, size_t n); +extern size_t strlen(const char *src); +extern int strcmp(const char *str1, const char *str2); +extern int strncmp(const char *str1, const char *str2, size_t n); + +#if defined(HAVE_PCLINT_CHECK) +#define UT_CONST const +#else +#define UT_CONST +#endif + +extern unsigned long strtoul(const char *nptr, char **endptr, int base); +extern UT_CONST char *strstr(const char *str1, const char *str2); +extern UT_CONST char *strchr(const char *s, int c); +extern char *strcpy(char* dest, const char *src); +extern char* strsep(char** __s_ptr, const char* __delimiter); +extern long strtol(const char *nptr, char **endptr, int base); + +extern int atoi (const char *s); + +extern void *memset(void *s, int ch, size_t n); +extern void* memmove(void* dst, const void* src, size_t n); +extern void* memcpy(void* dst, const void* src, size_t n); + + +HI_END_HEADER +#endif /* __HI_STDLIB_H__ */ + diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_types_base.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_types_base.h new file mode 100644 index 0000000000000000000000000000000000000000..d176e90001aaf3e648bce46ff6ca7338d6ce32aa --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/hi_types_base.h @@ -0,0 +1,559 @@ +/* + * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 __HI_TYPES_BASE_H__ +#define __HI_TYPES_BASE_H__ + +#if defined(__CC_ARM) +#define HI_HAVE_CROSS_COMPILER_ARM_ARMCC +#elif defined(__GNUC__) +#define HI_HAVE_CROSS_COMPILER_ARM_GCC +#elif defined(__DCC__) +#define HI_HAVE_CROSS_COMPILER_DIAB +#if defined(__HI_ASM_LANGUAGE__) || defined(_ASMLANGUAGE) +#define HI_HAVE_CROSS_COMPILER_DIAB_AS +#endif +#elif defined(_MSC_VER) +#define HI_HAVE_NOTIVE_COMPILER_VC +#endif + +#if !defined(HI_HAVE_CROSS_COMPILER_DIAB_AS) +#if defined(HI_HAVE_CROSS_COMPILER_ARM_GCC) || defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) || \ + defined(HI_HAVE_CROSS_COMPILER_DIAB) +#undef HI_HAVE_CROSS_COMPILER +#define HI_HAVE_CROSS_COMPILER +#endif + +#ifdef PRODUCT_CFG_PLATFORM_HI3921 +#define SAL_HAVE_HI3911_VER +#endif + +#ifdef PRODUCT_CFG_OS_WIN +# undef SAL_HAVE_OS_WIN_VER +# undef SAL_HAVE_OS_NU_VER +# undef SAL_HAVE_OS_VX_VER +#define SAL_HAVE_OS_WIN_VER +#endif + +#if defined (PRODUCT_CFG_OS_LOS) +# undef SAL_HAVE_OS_VX_VER +# define SAL_HAVE_OS_VX_VER +#else +# if defined (_WIN32) && defined (_MSC_VER) && (_MSC_VER >= 1200) +# pragma once +# ifndef SAL_HAVE_SIMU_WIN_VER +# define SAL_HAVE_SIMU_WIN_VER +# ifdef _USRDLL +# if !defined(PRODUCT_CFG_MSVC_HIDE_AUTOLINK_OUTPUT_INFO) +# pragma message("Windows VC Simulator DLL Version ") +# endif +# endif +# ifdef _LIB +# if !defined(PRODUCT_CFG_MSVC_HIDE_AUTOLINK_OUTPUT_INFO) +# pragma message("Windows VC Simulator lib Version ") +# endif +# endif +# ifdef _CONSOLE +# if !defined(PRODUCT_CFG_MSVC_HIDE_AUTOLINK_OUTPUT_INFO) +# pragma message("Windows VC Simulator console Version ") +# endif +# endif +# else +# if !defined(PRODUCT_CFG_MSVC_HIDE_AUTOLINK_OUTPUT_INFO) +# pragma message("Windows Version") +# endif +# endif +# endif +#endif + +#undef HI_SYS_DEBUG +#if (defined(PRODUCT_CFG_VERSION_DEBUG) || defined(SAL_HAVE_DEBUG_VERSION)) && !defined(SAL_HAVE_RELEASE_VERSION) +# define HI_SYS_DEBUG +#endif + +#if defined(PRODUCT_CFG_OS_WIN) +#pragma warning(disable:4200) /* disable nonstandard extension used : zero-sized array in struct/union. */ +#pragma warning(disable:4214) /* allows bitfield structure members to be of any integral type. */ +#pragma warning(disable:4201) +#pragma warning(disable:4514) +#pragma warning(disable:4127) +#endif + +/* 基本数据类型定义 */ +typedef unsigned char hi_uchar; +typedef unsigned char hi_u8; +typedef unsigned short hi_u16; +typedef unsigned int hi_u32; +typedef unsigned long long hi_u64; +typedef unsigned long hi_ulong; +typedef char hi_char; +typedef signed char hi_s8; +typedef short hi_s16; +typedef int hi_s32; +typedef long long hi_s64; +typedef long hi_slong; +typedef float hi_float; +typedef double hi_double; +typedef unsigned long hi_size_t; +typedef unsigned long hi_length_t; +typedef hi_u8 hi_bool; +typedef void hi_void; +typedef void* hi_pvoid; +typedef hi_u8 hi_byte; +typedef hi_byte* hi_pbyte; + +/* for 64bits platform, intptr_t/uintptr_t should be defined as 64bits length. */ +typedef int intptr_t; +typedef unsigned int uintptr_t; + +#undef ERROR +#define ERROR (-1) + +/* defines */ +#undef NULL +#ifndef NULL +#define NULL 0 +#endif + +#define HI_CONST const +#define HI_REG register + +#define HI_U32_MAX 0xFFFFFFFF +#define HI_U64_MAX 0xFFFFFFFFFFFFFFFFUL +#define HI_U16_MAX 0xFFFF + +typedef hi_pvoid (*hi_pvoid_callback_f)(hi_void); +typedef hi_void (*hi_void_callback_f)(hi_void); +typedef hi_void (*hi_void_callback)(hi_void); +typedef hi_bool (*hi_bool_callback_f)(hi_void); +typedef hi_void (*hi_void_u32_callback_f)(hi_u32); +typedef hi_u32 (*hi_u32_pvoid_callback_f)(hi_pvoid); +typedef hi_u32 (*hi_u32_void_callback)(hi_void); /* 周边代码待刷新 */ +typedef hi_u32 (*hi_u32_u32_pvoid_callback_f)(hi_pvoid, hi_u32); +typedef hi_s32 (*funcptr)(hi_void); /* ptr to function returning int */ +typedef hi_void (*voidfuncptr)(hi_void); /* ptr to function returning void */ + +typedef HI_CONST hi_char* hi_pcstr; + +#ifdef PRODUCT_CFG_OS_WIN +typedef unsigned __int64 hi_u64; +typedef __int64 hi_s64; +#elif defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) +typedef unsigned __int64 hi_u64; +typedef __int64 hi_s64; +#elif defined(HI_HAVE_CROSS_COMPILER_ARM_GCC) || defined(HAVE_PCLINT_CHECK) +typedef unsigned long long hi_u64; +typedef long long hi_s64; +#elif defined(HI_HAVE_CROSS_COMPILER_DIAB) +typedef unsigned long long hi_u64; +typedef long long hi_s64; +#elif !defined(PRODUCT_CFG_HSO) +/* #error "unknown compiler" */ +typedef unsigned __int64 hi_u64; +typedef __int64 hi_s64; +#endif + +#define HI_S32_BITS 32 +#define HI_S32_MAX (~(~0 << (HI_S32_BITS - 1))) + +#define HI_PUBLIC extern +#if !defined(PRODUCT_CFG_FEATURE_UT) +# define HI_PRV static +#else +# define HI_PRV +#endif + +# define STATIC static + +#endif /* HI_HAVE_CROSS_COMPILER_DIAB_AS */ + +#ifdef PRODUCT_CFG_OS_WIN +# define HI_API _declspec(dllexport) +# define HI_INLINE __inline +#elif defined(HI_HAVE_CROSS_COMPILER_ARM_GCC) +# define HI_INLINE inline +# define HI_API +#elif defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) +# define HI_INLINE inline +# define HI_API +#elif defined(HI_HAVE_CROSS_COMPILER_DIAB) +# define HI_INLINE __inline__ +# define HI_API +# else +# define HI_INLINE __inline +# define HI_API +#endif + +#define HI_PRVL HI_PRV HI_INLINE + +#if defined(__ONEBUILDER__CROSS_COMPILER_PRODUCT_CONFIG__) +#if defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) || defined(HI_HAVE_CROSS_COMPILER_ARM_GCC) +# define hi_section(name_string) __attribute__ ((section(name_string))) +# define HI_PACKED __attribute__((packed)) +# define HI_ALIGNED4 __attribute__ ((aligned (4))) +#elif defined(HI_HAVE_CROSS_COMPILER_DIAB) +# define hi_section(name_string) __attribute__ ((section(name_string))) +# define HI_PACKED __attribute__((packed)) +# define HI_ALIGNED4 __attribute__ ((aligned (4))) +#endif +#elif defined(SAL_HAVE_OS_WIN_VER) || defined(PRODUCT_CFG_HSO) +# define hi_section(name_string) +# define HI_PACKED +# define HI_ALIGNED4 +#else +# define hi_section(name_string) +# define HI_PACKED +# define HI_ALIGNED4 +#endif + +#if defined(SAL_HAVE_OS_WIN_VER) +# if defined(_DEBUG) || defined(PRODUCT_CFG_VERSION_DEBUG) +# define hi_dll_lib_name(x) x ## "_debug.dll" +# else +# define hi_dll_lib_name(x) x ## "_release.dll" +# endif +#else +#if defined(HI_HAVE_CROSS_COMPILER_ARM_GCC) +# define hi_dll_lib_name(x) x +#else +# define hi_dll_lib_name(x) x ## ".lib" +#endif +#endif + +#if defined(SAL_HAVE_NO_EXTERN_DEFINED) +# define HI_EXTERN +# define HI_EXTERN_C +#else +# if defined(PRODUCT_CFG_OS_WIN) +# define HI_EXTERN extern HI_API +# define HI_EXTERN_C HI_EXTERN +# define HI_EAPI extern HI_API +# else +# define HI_EXTERN extern +# define HI_EAPI +# define HI_EXTERN_C +# endif +#endif + +#ifdef __cplusplus +# define HI_CPP_START extern "C" { +# define HI_CPP_END } +#else +# define HI_CPP_START +# define HI_CPP_END +#endif + +#if defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) +#define HI_NOP __asm { nop } +#define hi_dbg_break() __asm { swi 0x14DEAD } +#elif defined(HI_HAVE_CROSS_COMPILER_DIAB) +#define HI_NOP +#define hi_dbg_break() +#else +#define HI_NOP +#ifdef PRODUCT_CFG_OS_WIN +#define hi_dbg_break() _asm { int 3 } +#else +#define hi_dbg_break() +#endif +#endif + +#define HI_START_HEADER HI_CPP_START +#define HI_END_HEADER HI_CPP_END + +#undef HI_OUT +#undef HI_IN +#undef HI_INOUT +#define HI_OUT +#define HI_IN +#define HI_INOUT + +#define HI_FALSE 0 +#define HI_TRUE 1 +#define HI_SWITCH_OFF 0 +#define HI_SWITCH_ON 1 + +#ifdef __cplusplus +#define HI_NULL 0 +#else +#define HI_NULL ((void *)0) +#endif + +#ifndef CACHE_ALIGNED_SIZE +#define CACHE_ALIGNED_SIZE 32 +#endif + +#define hi_array_count(x) (sizeof(x) / sizeof((x)[0])) + +#if !defined(hi_unref_param) && !defined(HI_HAVE_CROSS_COMPILER_DIAB) +#define hi_unref_param(P) ((P) = (P)) +#else +#define hi_unref_param(P) +#endif + +#if defined(PRODUCT_CFG_PLATFORM_HI3921) +#if defined(HI_HAVE_CROSS_COMPILER_ARM_ARMCC) +#define HI_VOLATILE volatile +#define hi_sys_get_lr() __return_address() +#elif defined(HAVE_PCLINT_CHECK) +#define HI_VOLATILE +#define hi_sys_get_lr() 0 +#elif defined(HI_HAVE_CROSS_COMPILER_DIAB) +#define HI_VOLATILE volatile +#define hi_sys_get_lr() 0 +#else +#define HI_VOLATILE __volatile__ +#define hi_sys_get_lr() 0 +#endif +#else +#define HI_VOLATILE +#define hi_sys_get_lr() 0 +#endif + +#define hi_aligin_u32_size(x) (((x) & (~3)) + 4) /* 构造4个字节对齐 */ +#define hi_is_align_u32(x) (!((x) & 3)) /* 判断是否为4字节对齐 */ +#define hi_is_unalign_u32(x) ((x) & 3) /* 判断是否为4字节对齐 */ +#if defined(HAVE_PCLINT_CHECK) +#define hi_fieldoffset(s, m) (0) +#else +#define hi_fieldoffset(s, m) ((hi_u32)&(((s *)0)->m)) /* 结构成员偏移 */ +#endif + +#define HI_CHAR_CR '\r' /* 0x0D */ +#define HI_CHAR_LF '\n' /* 0x0A */ +#define hi_tolower(x) ((x) | 0x20) /* Works only for digits and letters, but small and fast */ + +#define hi_array_size(_array) (sizeof(_array) / sizeof((_array)[0])) +#define hi_makeu16(a, b) ((hi_u16)(((hi_u8)(a)) | ((hi_u16)((hi_u8)(b))) << 8)) +#define hi_makeu32(a, b) ((hi_u32)(((hi_u16)(a)) | ((hi_u32)((hi_u16)(b))) << 16)) +#define hi_makeu64(a, b) ((hi_u64)(((hi_u32)(a)) | ((hi_u64)((hi_u32)(b))) <<32)) +#define hi_joinu32(a, b, c, d) ((a) | ((hi_u32)(b) << 8) | ((hi_u32)(c) << 16) | ((hi_u32)(d) << 24)) + +#define hi_hiu32(l) ((hi_u32)(((hi_u64)(l) >> 32) & 0xFFFFFFFF)) +#define hi_lou32(l) ((hi_u32)(l)) + +#define hi_hiu16(l) ((hi_u16)(((hi_u32)(l) >> 16) & 0xFFFF)) +#define hi_lou16(l) ((hi_u16)(l)) +#define hi_hiu8(l) ((hi_u8)(((hi_u16)(l) >> 8) & 0xFF)) +#define hi_lou8(l) ((hi_u8)(l)) + +#define hi_max(a, b) (((a) > (b)) ? (a) : (b)) +#define hi_min(a, b) (((a) < (b)) ? (a) : (b)) +#define hi_sub(a, b) (((a) > (b)) ? ((a) - (b)) : 0) +#define hi_abs_sub(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a))) +#define hi_byte_align(value, align) (((value) + (align) - 1) & (~((align) -1))) +#define hi_is_byte_align(value, align) (((hi_u32)(value) & ((align) - 1))== 0) +#define hi_inc_wraparound(value, round_size) ++(value); (value) &= ((round_size) - 1) +#define hi_dec_wraparound(value, round_size) --(value); (value) &= ((round_size) - 1) + +#define hi_swap_byteorder_16(value) ((((value) & 0xFF) << 8) + (((value) & 0xFF00) >> 8)) + +#define hi_swap_byteorder_32(value) \ + ((hi_u32)(((value) & 0x000000FF) << 24) + \ + (hi_u32)(((value) & 0x0000FF00) << 8) + \ + (hi_u32)(((value) & 0x00FF0000) >> 8) + \ + (hi_u32)(((value) & 0xFF000000) >> 24)) + +#define hi_swap_byteorder_64(value) \ + ((((value) & 0x00000000000000ffULL) << 56) + \ + (((value) & 0x000000000000ff00ULL) << 40) + \ + (((value) & 0x0000000000ff0000ULL) << 24) + \ + (((value) & 0x00000000ff000000ULL) << 8) + \ + (((value) & 0x000000ff00000000ULL) >> 8) + \ + (((value) & 0x0000ff0000000000ULL) >> 24) + \ + (((value) & 0x00ff000000000000ULL) >> 40) + \ + (((value) & 0xff00000000000000ULL) >> 56)) + +#undef MIN_T +#define MIN_T hi_min + +#define hi_make_identifier(a, b, c, d) hi_makeu32(hi_makeu16(a, b), hi_makeu16(c, d)) +#define hi_make_ver16(spc, b) ((hi_u16)(((hi_u8)((spc)&0x0F)) | ((hi_u16)((hi_u8)(b))) << 12)) + +#define hi_set_bit_i(val, n) ((val) |= (1 << (n))) +#define hi_clr_bit_i(val, n) ((val) &= ~(1 << (n))) +#define hi_is_bit_set_i(val, n) ((val) & (1 << (n))) +#define hi_is_bit_clr_i(val, n) (~((val) & (1 << (n)))) +#define hi_switch_bit_i(val, n) ((val) ^= (1 << (n))) +#define hi_get_bit_i(val, n) (((val) >> (n)) & 1) +#define hi_reg_clr_bit_i(reg, n) ((*(volatile unsigned int *)(reg)) &= ~(1 << (n))) + +#define hi_u8_bit_val(b7, b6, b5, b4, b3, b2, b1, b0) \ + (((b7) << 7) | ((b6) << 6) | ((b5) << 5) | ((b4) << 4) | ((b3) << 3) | ((b2) << 2) | ((b1) << 1) | ((b0) << 0)) +#define hi_u16_bit_val(b12, b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0) \ + (hi_u16)(((b12) << 12) | ((b11) << 11) | ((b10) << 10) | ((b9) << 9) | ((b8) << 8) | ((b7) << 7) | \ + ((b6) << 6) | ((b5) << 5) | ((b4) << 4) | ((b3) << 3) | ((b2) << 2) | ((b1) << 1) | ((b0) << 0)) + +#if defined(__ONEBUILDER__CROSS_COMPILER_PRODUCT_CONFIG__) +#define HSO_ENUM HI_ALIGNED4 enum +#define HI_U8A HI_ALIGNED4 hi_u8 +#define HI_U16A HI_ALIGNED4 hi_u16 +#define HI_CHARTA HI_ALIGNED4 hi_char +#else +#define HSO_ENUM enum +#define HI_U8A hi_u8 +#define HI_U16A hi_u16 +#define HI_CHARTA hi_char +#endif + +#if defined(PRODUCT_CFG_HSO) && defined(HI_HAVE_NOTIVE_COMPILER_VC) +#define __FUNCTION__ "NA" +#endif + +/*****************************************************************************/ +#define hi_set_u32_ptr_val(ptr, offset, val) (*((hi_u32*)(((hi_u8*)(ptr)) + (offset))) = (val)) +#define hi_get_u32_ptr_val(ptr, offset) *((hi_u32*)(((hi_u8*)(ptr)) + (offset))) +/*****************************************************************************/ +/*****************************************************************************/ +#define HI_SIZE_1K 1024 +#define HI_SIZE_1M (1024 * 1024) +/*****************************************************************************/ +#ifndef bit +#define bit(x) (1UL << (x)) +#endif +#define BIT31 ((hi_u32)(1UL << 31)) +#define BIT30 ((hi_u32)(1 << 30)) +#define BIT29 ((hi_u32)(1 << 29)) +#define BIT28 ((hi_u32)(1 << 28)) +#define BIT27 ((hi_u32)(1 << 27)) +#define BIT26 ((hi_u32)(1 << 26)) +#define BIT25 ((hi_u32)(1 << 25)) +#define BIT24 ((hi_u32)(1 << 24)) +#define BIT23 ((hi_u32)(1 << 23)) +#define BIT22 ((hi_u32)(1 << 22)) +#define BIT21 ((hi_u32)(1 << 21)) +#define BIT20 ((hi_u32)(1 << 20)) +#define BIT19 ((hi_u32)(1 << 19)) +#define BIT18 ((hi_u32)(1 << 18)) +#define BIT17 ((hi_u32)(1 << 17)) +#define BIT16 ((hi_u32)(1 << 16)) +#define BIT15 ((hi_u32)(1 << 15)) +#define BIT14 ((hi_u32)(1 << 14)) +#define BIT13 ((hi_u32)(1 << 13)) +#define BIT12 ((hi_u32)(1 << 12)) +#define BIT11 ((hi_u32)(1 << 11)) +#define BIT10 ((hi_u32)(1 << 10)) +#define BIT9 ((hi_u32)(1 << 9)) +#define BIT8 ((hi_u32)(1 << 8)) +#define BIT7 ((hi_u32)(1 << 7)) +#define BIT6 ((hi_u32)(1 << 6)) +#define BIT5 ((hi_u32)(1 << 5)) +#define BIT4 ((hi_u32)(1 << 4)) +#define BIT3 ((hi_u32)(1 << 3)) +#define BIT2 ((hi_u32)(1 << 2)) +#define BIT1 ((hi_u32)(1 << 1)) +#define BIT0 ((hi_u32)(1 << 0)) + +#define HALFWORD_BIT_WIDTH 16 + +/* 寄存器访问接口 */ +#define hi_reg_write(addr, val) (*(volatile unsigned int *)(uintptr_t)(addr) = (val)) +#define hi_reg_read(addr, val) ((val) = *(volatile unsigned int *)(uintptr_t)(addr)) +#define hi_reg_write32(addr, val) (*(volatile unsigned int *)(uintptr_t)(addr) = (val)) +#define hi_reg_read32(addr, val) ((val) = *(volatile unsigned int *)(uintptr_t)(addr)) +#define hi_reg_read_val32(addr) (*(volatile unsigned int*)(uintptr_t)(addr)) +#define hi_reg_setbitmsk(addr, msk) ((hi_reg_read_val32(addr)) |= (msk)) +#define hi_reg_clrbitmsk(addr, msk) ((hi_reg_read_val32(addr)) &= ~(msk)) +#define hi_reg_clrbit(addr, pos) ((hi_reg_read_val32(addr)) &= ~((unsigned int)(1) << (pos))) +#define hi_reg_setbit(addr, pos) ((hi_reg_read_val32(addr)) |= ((unsigned int)(1) << (pos))) +#define hi_reg_clrbits(addr, pos, bits) (hi_reg_read_val32(addr) &= ~((((unsigned int)1 << (bits)) - 1) << (pos))) +#define hi_reg_setbits(addr, pos, bits, val) (hi_reg_read_val32(addr) = \ + (hi_reg_read_val32(addr) & (~((((unsigned int)1 << (bits)) - 1) << (pos)))) | \ + ((unsigned int)((val) & (((unsigned int)1 << (bits)) - 1)) << (pos))) +#define hi_reg_getbits(addr, pos, bits) ((hi_reg_read_val32(addr) >> (pos)) & (((unsigned int)1 << (bits)) - 1)) + +#define hi_reg_write16(addr, val) (*(volatile unsigned short *)(uintptr_t)(addr) = (val)) +#define hi_reg_read16(addr, val) ((val) = *(volatile unsigned short *)(uintptr_t)(addr)) +#define hi_reg_read_val16(addr) (*(volatile unsigned short*)(uintptr_t)(addr)) +#define hi_reg_clrbit16(addr, pos) ((hi_reg_read_val16(addr)) &= ~((unsigned short)(1) << (pos))) +#define hi_reg_setbit16(addr, pos) ((hi_reg_read_val16(addr)) |= ((unsigned short)(1) << (pos))) +#define hi_reg_clrbits16(addr, pos, bits) (hi_reg_read_val16(addr) &= ~((((unsigned short)1 << (bits)) - 1) << (pos))) +#define hi_reg_setbits16(addr, pos, bits, val) (hi_reg_read_val16(addr) = \ + (hi_reg_read_val16(addr) & (~((((unsigned short)1 << (bits)) - 1) << (pos)))) | \ + ((unsigned short)((val) & (((unsigned short)1 << (bits)) - 1)) << (pos))) +#define hi_reg_getbits16(addr, pos, bits) ((hi_reg_read_val16(addr) >> (pos)) & (((unsigned short)1 << (bits)) - 1)) + +#define reg_write32(addr, val) (*(volatile unsigned int *)(uintptr_t)(addr) = (val)) +#define reg_read32(addr, val) ((val) = *(volatile unsigned int *)(uintptr_t)(addr)) +#define reg_read_val(addr) (*(volatile unsigned *)(uintptr_t)(addr)) + +#ifndef BSP_RAM_TEXT_SECTION +#define BSP_RAM_TEXT_SECTION __attribute__ ((section(".bsp.ram.text"))) +#endif + +#ifndef BSP_ROM_RODATA_SECTION +#define BSP_ROM_RODATA_SECTION __attribute__ ((section(".bsp.rom.rodata"))) +#endif + +#ifndef BSP_ROM_DATA0_SECTION +#define BSP_ROM_DATA0_SECTION __attribute__ ((section(".bsp.rom.data0"))) +#endif + +#ifndef ROM_TEXT_PATCH_SECTION +#define ROM_TEXT_PATCH_SECTION __attribute__ ((section(".rom.text.patch"))) +#endif + +#ifndef LP_RAM_BSS_SECTION +#define LP_RAM_BSS_SECTION __attribute__ ((section(".lowpower.ram.bss"))) +#endif + +#ifndef ROM_DATA_PATCH_SECTION +#define ROM_DATA_PATCH_SECTION __attribute__ ((section(".rom.data.patch"))) +#endif + +#ifndef EXTERN_ROM_BSS_SECTION +#define EXTERN_ROM_BSS_SECTION __attribute__ ((section(".extern.rom.bss"))) +#endif + +#ifndef EXTERN_ROM_DATA1_BSS_SECTION +#define EXTERN_ROM_DATA1_BSS_SECTION __attribute__ ((section(".extern.rom.data1.bss"))) +#endif + +#ifdef CONFIG_FLASH_ENCRYPT_SUPPORT +#ifndef CRYPTO_RAM_TEXT_SECTION +#define CRYPTO_RAM_TEXT_SECTION __attribute__ ((section(".crypto.ram.text"))) +#endif +#else +#ifndef CRYPTO_RAM_TEXT_SECTION +#define CRYPTO_RAM_TEXT_SECTION +#endif +#endif + +#ifdef HAVE_PCLINT_CHECK +#define hi_likely(x) (x) +#define hi_unlikely(x) (x) +#else +#define hi_likely(x) __builtin_expect(!!(x), 1) +#define hi_unlikely(x) __builtin_expect(!!(x), 0) +#endif +#define HI_ALWAYS_STAIC_INLINE __attribute__((always_inline)) static inline + +#ifdef HAVE_PCLINT_CHECK +#define hi_offset_of_member(type, member) 0 +#else +#define hi_offset_of_member(type, member) ((hi_u32)(&((type *)0)->member)) +#endif +#define BITS_PER_BYTE 8 +#define HEXADECIMAL 16 +#define DECIMAL 10 +#define SZ_1KB 1024 +#define SZ_1MB (SZ_1KB * SZ_1KB) +#define SZ_4KB 4096 +/*****************************************************************************/ +#include "hi_errno.h" + +#define HI_SYS_WAIT_FOREVER 0xFFFFFFFF + +#endif /* __HI_TYPES__BASE_H__ */ diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_config.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_config.h new file mode 100644 index 0000000000000000000000000000000000000000..1fc67b9a6dc08434c59efd70ddab66b421a54c32 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_config.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 Huawei Device Co., 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 IOT_CONFIG_H +#define IOT_CONFIG_H + +// / +#include +#include "hi_types_base.h" +#include "cmsis_os2.h" +#include "iot_errno.h" +#include "iot_config.h" +#include "MQTTClient.h" +#include "osal_addr.h" +#include "iot_main.h" + +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; +typedef int64_t s64; +typedef int32_t s32; +typedef int16_t s16; +typedef int8_t s8; + +#include "crypto/md5.h" + +#define CONFIG_COMMAND_TIMEOUT 10000L +#define CN_KEEPALIVE_TIME 50 +#define CN_CLEANSESSION 1 +#define CN_HMAC_PWD_LEN 65 // SHA256 IS 32 BYTES AND END APPEND'\0' +#define CN_EVENT_TIME "1970000100" +#define CN_CLIENTID_FMT "%s_0_0_%s" // This is the cient ID format, deviceID_0_0_TIME +#define CN_QUEUE_WAITTIMEOUT 1000 +#define CN_QUEUE_MSGNUM 16 +#define CN_QUEUE_MSGSIZE (sizeof(hi_pvoid)) + +#define CN_TASK_PRIOR 28 +#define CN_TASK_STACKSIZE 0X2000 +#define CN_TASK_NAME "IoTMain" + +typedef enum { + EN_IOT_MSG_PUBLISH = 0, + EN_IOT_MSG_RECV, +}EnIotMsgT; + +typedef struct { + EnIotMsgT type; + int qos; + char *topic; + char *payload; +}IoTMsgT; + +typedef struct { + hi_bool stop; + hi_u32 conLost; + void* queueID; + hi_u32 iotTaskID; + FnMsgCallBack msgCallBack; + MQTTClient_deliveryToken tocken; +}IotAppCbT; +static IotAppCbT g_ioTAppCb; + +// the default subscribe topic +static const char *g_defaultSubscribeTopic[] = {CONFIG_CMD_TOPIC_SUBSCRIBE}; + + +#define CN_TOPIC_SUBSCRIBE_NUM (sizeof(g_defaultSubscribeTopic) / sizeof(const char *)) + +void TimeDelaySeconds (int s) +{ + osDelay(s*100); +} + +static int MsgRcvCallBack(void *context, char *topic, int topicLen, MQTTClient_message *message) +{ + (void) context; + IoTMsgT *msg; + char *buf; + hi_u32 bufSize; + int topicLength = topicLen; + + if (topicLength == 0) { + topicLength = strlen(topic); + } + bufSize = topicLength + 1 + message->payloadlen + 1 + sizeof(IoTMsgT); + buf = osal_vmalloc(bufSize); + if (buf != NULL) { + msg = (IoTMsgT *)buf; + buf += sizeof(IoTMsgT); + bufSize -= sizeof(IoTMsgT); + msg->qos = message->qos; + msg->type = EN_IOT_MSG_RECV; + (void)memcpy_s(buf, bufSize, topic, topicLength); + buf[topicLength] = '\0'; + msg->topic = buf; + buf += topicLength + 1; + bufSize -= (topicLength + 1); + (void)memcpy_s(buf, bufSize, message->payload, message->payloadlen); + buf[message->payloadlen] = '\0'; + msg->payload = buf; + printf("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", msg->qos, msg->topic, msg->payload); + if (IOT_SUCCESS != osMessageQueuePut(g_ioTAppCb.queueID, &msg, 0, CN_QUEUE_WAITTIMEOUT)) { + printf("Write queue failed\r\n"); + osal_vfree(msg); + } + } + + MQTTClient_freeMessage(&message); + MQTTClient_free(topic); + return 1; +} + +// when the connect lost and this callback will be called +static void ConnLostCallBack(void *context, char *cause) +{ + (void) context; + printf("Connection lost:caused by:%s\r\n", cause == NULL ? "Unknown" : cause); + return; +} + +void IoTMsgProcess(IoTMsgT *msg, MQTTClient_message pubmsg, MQTTClient client) +{ + hi_u32 ret; + switch (msg->type) { + case EN_IOT_MSG_PUBLISH: + pubmsg.payload = (void *)msg->payload; + pubmsg.payloadlen = (int)strlen(msg->payload); + pubmsg.qos = msg->qos; + pubmsg.retained = 0; + ret = MQTTClient_publishMessage(client, msg->topic, &pubmsg, &g_ioTAppCb.tocken); + if (ret != MQTTCLIENT_SUCCESS) { + printf("MSGSEND:failed\r\n"); + } + printf("MSGSEND:SUCCESS\r\n"); + g_ioTAppCb.tocken++; + break; + case EN_IOT_MSG_RECV: + if (g_ioTAppCb.msgCallBack != NULL) { + g_ioTAppCb.msgCallBack(msg->qos, msg->topic, msg->payload); + } + break; + default: + break; + } + return; +} + +// use this function to deal all the coming message +static int ProcessQueueMsg(MQTTClient client) +{ + printf("ProcessQueueMsg\r\n"); + hi_u32 ret; + uint8_t msgSize; + IoTMsgT *msg; + hi_u32 timeout; + MQTTClient_message pubmsg = MQTTClient_message_initializer; + + timeout = CN_QUEUE_WAITTIMEOUT; + do { + msg = NULL; + msgSize = sizeof(hi_pvoid); + ret = osMessageQueueGet(g_ioTAppCb.queueID, &msg, &msgSize, timeout); + if (ret != MQTTCLIENT_SUCCESS) { + return HI_ERR_FAILURE; + } + if (msg != NULL) { + IoTMsgProcess(msg, pubmsg, client); + osal_vfree(msg); + } + timeout = 0; // continuos to deal the message without wait here + } while (ret == IOT_SUCCESS); + return IOT_SUCCESS; +} + +void MqttProcess(MQTTClient client, char *clientID, char *userPwd, MQTTClient_connectOptions connOpts, int subQos[]) +{ + int rc = MQTTClient_create(&client, CN_IOT_SERVER, clientID, MQTTCLIENT_PERSISTENCE_NONE, NULL); + if (rc != MQTTCLIENT_SUCCESS) { + printf("Create Client failed,Please check the parameters--%d\r\n", rc); + if (userPwd != NULL) { + osal_vfree(userPwd); + return; + } + } + + rc = MQTTClient_setCallbacks(client, NULL, ConnLostCallBack, MsgRcvCallBack, NULL); + if (rc != MQTTCLIENT_SUCCESS) { + printf("Set the callback failed,Please check the callback paras\r\n"); + MQTTClient_destroy(&client); + return; + } + + rc = MQTTClient_connect(client, &connOpts); + if (rc != MQTTCLIENT_SUCCESS) { + printf("Connect IoT server failed,please check the network and parameters:%d\r\n", rc); + MQTTClient_destroy(&client); + return; + } + printf("Connect success\r\n"); + + rc = MQTTClient_subscribeMany(client, CN_TOPIC_SUBSCRIBE_NUM, (char* const*)g_defaultSubscribeTopic, + (int *)&subQos[0]); + if (rc != MQTTCLIENT_SUCCESS) { + printf("Subscribe the default topic failed,Please check the parameters\r\n"); + MQTTClient_destroy(&client); + return; + } + printf("Subscribe success\r\n"); + while (MQTTClient_isConnected(client)) { + ProcessQueueMsg(client); // do the job here + int ret = ProcessQueueMsg(client); // do the job here + if (ret == HI_ERR_SUCCESS) { + return; + } + MQTTClient_yield(); // make the keepalive done + } + MQTTClient_disconnect(client, CONFIG_COMMAND_TIMEOUT); + return; +} + + +/** + * @brief 字节数组转HEX字符串 + * + * @param bufIn 字节数组 + * @param len 数组长度 + * @param bufOut 存放HEX字符串的数组首地址 + * @return int + */ +int Byte2HexStr(unsigned char *bufIn, int len, char *bufOut) +{ + int i = 0; + unsigned char tmp_l = 0x0; + unsigned char tmp_h = 0; + if ((NULL == bufIn) || (len <= 0) || (NULL == bufOut)) { + return -1; + } + for (i = 0; i < len; i++) { + tmp_h = (bufIn[i] >> 4) & 0X0F; + tmp_l = bufIn[i] & 0x0F; + bufOut[2 * i] = (tmp_h > 9) ? (tmp_h - 10 + 'A') : (tmp_h + '0'); + bufOut[2 * i + 1] = (tmp_l > 9) ? (tmp_l - 10 + 'A') : (tmp_l + '0'); + } + bufOut[2 * len] = '\0'; + return 0; +} + +void ZaiOhHmacGeneratePassword(char *content, int contentlen, char *key, int keylen, char *outPutHex, int outPutHexLen) +{ + if (content == NULL || key == NULL || outPutHex == NULL) { + printf("params error!"); + } + + printf("\r\n key string: %s \n", key); + printf("\r\n content string: %s \n", content); + char src[16] = {0}; + hmac_md5(key, keylen, content, contentlen, src); + Byte2HexStr(src,sizeof(src),outPutHex); + printf("\r\n outPutHex: %s \n",outPutHex); +} + +char g_content[256] = {0}; + +char g_clientID[256] = {0}; +char g_userID[256] = {0}; +char g_userPwd[256] = {0}; +static void MainEntryProcess(void) +{ + int subQos[CN_TOPIC_SUBSCRIBE_NUM] = {1}; + + MQTTClient client = NULL; + MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; + + conn_opts.keepAliveInterval = CN_KEEPALIVE_TIME; + conn_opts.cleansession = CN_CLEANSESSION; + conn_opts.username = "g_userID"; + conn_opts.password = "g_userPwd"; + conn_opts.MQTTVersion = MQTTVERSION_3_1_1; + // wait for the wifi connect ok + printf("\r\n IOTSERVER:%s\r\n", CN_IOT_SERVER); + printf("\r\n g_userID = %s\r\n", "g_userID"); + printf("\r\n g_userPwd = %s\r\n", "g_userPwd"); + MqttProcess(client, "g_clientID", "g_userPwd", conn_opts, subQos); +} + +void MainEntry(void) +{ + while (g_ioTAppCb.stop == HI_FALSE) { + MainEntryProcess(); + printf("The connection lost and we will try another connect\r\n"); + //hi_sleep(1000*5); /* 延时5*1000ms */ + TimeDelaySeconds(5); + } +} + +void IoTMain(void) +{ + g_ioTAppCb.queueID = osMessageQueueNew(CN_QUEUE_MSGNUM, CN_QUEUE_MSGSIZE, NULL); + osThreadAttr_t attr; + attr.name = "MainEntry"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = CN_TASK_STACKSIZE; + attr.priority = CN_TASK_PRIOR; + + if (osThreadNew((osThreadFunc_t)MainEntry, NULL, &attr) == NULL) { + printf("[TrafficLight] Failed to create IOTDEMO!\n"); + } +} + +int IoTSetMsgCallback(FnMsgCallBack msgCallback) +{ + g_ioTAppCb.msgCallBack = msgCallback; + return 0; +} + +int IotSendMsg(int qos, char *topic, char *payload) +{ + int rc = -1; + IoTMsgT *msg; + char *buf; + hi_u32 bufSize; + + bufSize = strlen(topic) + 1 + strlen(payload) + 1 + sizeof(IoTMsgT); + buf = osal_vmalloc(bufSize); + if (buf != NULL) { + msg = (IoTMsgT *)buf; + buf += sizeof(IoTMsgT); + bufSize -= sizeof(IoTMsgT); + msg->qos = qos; + msg->type = EN_IOT_MSG_PUBLISH; + (void)memcpy_s(buf, bufSize, topic, strlen(topic)); + buf[strlen(topic)] = '\0'; + msg->topic = buf; + buf += strlen(topic) + 1; + bufSize -= (strlen(topic) + 1); + (void)memcpy_s(buf, bufSize, payload, strlen(payload)); + buf[strlen(payload)] = '\0'; + msg->payload = buf; + printf("SNDMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", msg->qos, msg->topic, msg->payload); + if (osMessageQueuePut(g_ioTAppCb.queueID, &msg, 0, CN_QUEUE_WAITTIMEOUT) != IOT_SUCCESS) { + printf("Write queue failed\r\n"); + osal_vfree(msg); + return rc; + } else { + rc = 0; + } + } + return rc; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_main.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_main.h new file mode 100644 index 0000000000000000000000000000000000000000..c8d791852e20c4e8b8f8a829ae2e604552d44e7c --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_main.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 IOT_MAIN_H +#define IOT_MAIN_H + +typedef void (*FnMsgCallBack)(int qos, char *topic, char *payload); + +void TimeDelaySeconds(int s); +/* + * This is the iot main function. Please call this function first +*/ +void IoTMain(void); + + +/** + * Use this function to set the message call back function, when some messages comes, + * the callback will be called, if you don't care about the message, set it to NULL + */ +int IoTSetMsgCallback(FnMsgCallBack msgCallback); + +/* + * When you want to send some messages to the iot server(including the response message), + * please call this api + * @param qos: the mqtt qos,:0,1,2 + * @param topic: the iot mqtt topic + * @param payload: the mqtt payload + * @return 0 success while others failed + * @instruction: if success means we write the message to the queue susccess, + * not means communicate with the server success + */ +int IotSendMsg(int qos, char *topic, char *payload); + +#endif /* IOT_MAIN_H_ */ \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.c new file mode 100644 index 0000000000000000000000000000000000000000..7e9799907858d9fe6b2fae42770cd3a345d85c86 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.c @@ -0,0 +1,312 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 "hi_stdlib.h" +#include "stdio.h" +#include "osal_addr.h" +#include "iot_main.h" +#include "iot_profile.h" + +// format the report data to json string mode +static cJSON *FormatProfileValue(IoTProfileKV *kv) +{ + cJSON *ret = NULL; + switch (kv->type) { + case EN_IOT_DATATYPE_INT: + ret = cJSON_CreateNumber(kv->iValue); + break; + case EN_IOT_DATATYPE_LONG: + ret = cJSON_CreateNumber((double)(*(long *)kv->value)); + break; + case EN_IOT_DATATYPE_STRING: + ret = cJSON_CreateString((const char *)kv->value); + break; + default: + break; + } + return ret; +} + +static cJSON *MakeKvs(IoTProfileKV *kvlst) +{ + cJSON *root; + cJSON *kv; + IoTProfileKV *kvInfo; + + // build a root node + root = cJSON_CreateObject(); + if (root == NULL) { + return root; + } + + // add all the property to the properties + kvInfo = kvlst; + while (kvInfo != NULL) { + kv = FormatProfileValue(kvInfo); + if (kv == NULL) { + if (root != NULL) { + cJSON_Delete(root); + root = NULL; + } + return root; + } + + cJSON_AddItemToObject(root, kvInfo->key, kv); + kvInfo = kvInfo->nxt; + } + // OK, now we return it + return root; +} + +#define CN_PROFILE_SERVICE_KEY_SERVICEID "service_id" +#define CN_PROFILE_SERVICE_KEY_PROPERTIIES "properties" +#define CN_PROFILE_SERVICE_KEY_EVENTTIME "event_time" +#define CN_PROFILE_KEY_SERVICES "services" +static cJSON *MakeService(IoTProfileService *serviceInfo) +{ + cJSON *root; + cJSON *serviceID; + cJSON *properties; + cJSON *eventTime; + + // build a root node + root = cJSON_CreateObject(); + if (root == NULL) { + return root; + } + + // add the serviceID node to the root node + serviceID = cJSON_CreateString(serviceInfo->serviceID); + if (serviceID == NULL) { + if (root != NULL) { + cJSON_Delete(root); + root = NULL; + } + return root; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_SERVICE_KEY_SERVICEID, serviceID); + + // add the properties node to the root + properties = MakeKvs(serviceInfo->serviceProperty); + if (properties == NULL) { + if (root != NULL) { + cJSON_Delete(root); + cJSON_Delete(properties); + root = NULL; + } + return root; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_SERVICE_KEY_PROPERTIIES, properties); + // add the event time (optional) to the root + if (serviceInfo->eventTime != NULL) { + eventTime = cJSON_CreateString(serviceInfo->eventTime); + if (eventTime == NULL) { + if (root != NULL) { + cJSON_Delete(root); + root = NULL; + } + return root; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_SERVICE_KEY_EVENTTIME, eventTime); + } + // OK, now we return it + return root; +} + +static cJSON *MakeServices(IoTProfileService *serviceInfo) +{ + cJSON *services = NULL; + cJSON *service; + IoTProfileService *serviceTmp; + + // create the services array node + services = cJSON_CreateArray(); + if (services == NULL) { + return services; + } + + serviceTmp = serviceInfo; + while (serviceTmp != NULL) { + service = MakeService(serviceTmp); + if (service == NULL) { + return services; + } + cJSON_AddItemToArray(services, service); + serviceTmp = serviceTmp->nxt; + } + + // now we return the services + return services; +} + +// use this function to make a topic to publish +// if request_id is needed depends on the fmt +static char *MakeTopic(const char *fmt, const char *deviceId, const char *requestID) +{ + int len; + char *ret = NULL; + + len = strlen(fmt) + strlen(deviceId); + if (requestID != NULL) { + len += strlen(requestID); + } + + ret = osal_vmalloc(len); + if (ret != NULL) { + if (requestID != NULL) { + if (snprintf_s(ret, len + 1, len, fmt, deviceId, requestID) < 0) { + printf("string is null\r\n"); + } + } else { + if (snprintf_s(ret, len + 1, len, fmt, deviceId) < 0) { + printf("string is null\r\n"); + } + } + } + return ret; +} + +#define CN_PROFILE_CMDRESP_KEY_RETCODE "result_code" +#define CN_PROFILE_CMDRESP_KEY_RESPNAME "response_name" +#define CN_PROFILE_CMDRESP_KEY_PARAS "paras" +static char *MakeProfileCmdResp(IoTCmdResp *payload) +{ + char *ret = NULL; + cJSON *root; + cJSON *retCode; + cJSON *respName; + cJSON *paras; + + // create the root node + root = cJSON_CreateObject(); + if (root == NULL) { + return ret; + } + + // create retcode and retdesc and add it to the root + retCode = cJSON_CreateNumber(payload->retCode); + if (retCode == NULL) { + if (root != NULL) { + cJSON_Delete(root); + } + return ret; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_CMDRESP_KEY_RETCODE, retCode); + + if (payload->respName != NULL) { + respName = cJSON_CreateString(payload->respName); + if (respName == NULL) { + if (root != NULL) { + cJSON_Delete(root); + } + return ret; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_CMDRESP_KEY_RESPNAME, respName); + } + + if (payload->paras != NULL) { + paras = MakeKvs(payload->paras); + if (paras == NULL) { + if (root != NULL) { + cJSON_Delete(root); + } + return ret; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_CMDRESP_KEY_PARAS, paras); + } + + // OK, now we make it to a buffer + ret = cJSON_PrintUnformatted(root); + cJSON_Delete(root); + return ret; +} + +#define CN_PROFILE_TOPICFMT_CMDRESP "$oc/devices/%s/sys/commands/response/request_id=%s" +int IoTProfileCmdResp(char *deviceID, IoTCmdResp *payload) +{ + int ret = -1; + char *topic; + char *msg; + + if ((deviceID == NULL) || (payload == NULL) || (payload->requestID == NULL)) { + return ret; + } + + topic = MakeTopic(CN_PROFILE_TOPICFMT_CMDRESP, deviceID, payload->requestID); + if (topic == NULL) { + return ret; + } + msg = MakeProfileCmdResp(payload); + if ((topic != NULL) && (msg != NULL)) { + ret = IotSendMsg(0, topic, msg); + } + + osal_vfree(topic); + cJSON_free(msg); + return ret; +} + +static char *MakeProfilePropertyReport(IoTProfileService *payload) +{ + char *ret = NULL; + cJSON *root; + cJSON *services; + + // create the root node + root = cJSON_CreateObject(); + if (root == NULL) { + return ret; + } + + // create the services array node to the root + services = MakeServices(payload); + if (services == NULL) { + if (root != NULL) { + cJSON_Delete(root); + } + return ret; + } + cJSON_AddItemToObjectCS(root, CN_PROFILE_KEY_SERVICES, services); + + // OK, now we make it to a buffer + ret = cJSON_PrintUnformatted(root); + cJSON_Delete(root); + return ret; +} +#define CN_PROFILE_TOPICFMT_PROPERTYREPORT "$oc/devices/%s/sys/properties/report" +int IoTProfilePropertyReport(char *deviceID, IoTProfileService *payload) +{ + int ret = -1; + char *topic; + char *msg; + + if ((deviceID == NULL) || (payload == NULL) || (payload->serviceID == NULL) || (payload->serviceProperty == NULL)) { + return ret; + } + topic = MakeTopic(CN_PROFILE_TOPICFMT_PROPERTYREPORT, deviceID, NULL); + if (topic == NULL) { + return ret; + } + msg = MakeProfilePropertyReport(payload); + if ((topic != NULL) && (msg != NULL)) { + ret = IotSendMsg(0, topic, msg); + } + + osal_vfree(topic); + cJSON_free(msg); + return ret; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.h new file mode 100644 index 0000000000000000000000000000000000000000..967485b260b137604f3690a3a1baa237011757a1 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/iot_profile.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED. + * 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 IOT_PROFILE_H_ +#define IOT_PROFILE_H_ +#include "iot_profile.h" + +#define OC_BEEP_STATUS_ON ((hi_u8) 0x01) +#define OC_BEEP_STATUS_OFF ((hi_u8) 0x00) + +////< enum all the data type for the oc profile +typedef enum { + EN_IOT_DATATYPE_INT = 0, + EN_IOT_DATATYPE_LONG, + EN_IOT_DATATYPE_FLOAT, + EN_IOT_DATATYPE_DOUBLE, + EN_IOT_DATATYPE_STRING, ///< must be ended with '\0' + EN_IOT_DATATYPE_LAST, +}IoTDataType; + + +typedef struct { + void *nxt; ///< ponit to the next key + const char *key; + const char *value; + int iValue; + float LedValue; + IoTDataType type; +}IoTProfileKV; + +typedef struct { + void *nxt; + char *serviceID; ///< the service id in the profile, which could not be NULL + char *eventTime; ///< eventtime, which could be NULL means use the platform time + IoTProfileKV *serviceProperty; ///< the property in the profile, which could not be NULL +}IoTProfileService; + +typedef struct { + int retCode; ///< response code, 0 success while others failed + const char *respName; ///< response name + const char *requestID;///< specified by the message command + IoTProfileKV *paras; ///< the command paras +}IoTCmdResp; +/** + * Use this function to make the command response here + * and you must supplied the device id, and the payload defines as IoTCmdResp_t + * +*/ +int IoTProfileCmdResp(char *deviceID, IoTCmdResp *payload); +/** + * use this function to report the property to the iot platform + * +*/ +int IoTProfilePropertyReport(char *deviceID, IoTProfileService *payload); +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.c new file mode 100644 index 0000000000000000000000000000000000000000..0fdcd6862718f53e4ffafc6933884edb888ac71b --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.c @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 "wifi_device.h" +#include "cmsis_os2.h" + +#include "lwip/netifapi.h" + +#if CHIP_WS63 +#include "lwip/nettool/misc.h" +#else +#include "lwip/api_shell.h" +#endif + +#define ZERO 0 +#define ONE 1 +#define TWO 2 +#define THREE 3 +#define FOUR 4 +#define FIVE 5 +#define TEN 10 +#define ONE_HUNDRED 100 + +// 打印WiFi连接信息 +static void PrintLinkedInfo(WifiLinkedInfo* info) +{ + if (!info) return; +} + +// 全局变量,记录WiFi连接状态 +static volatile int g_connected = 0; + +// WiFI连接状态改变时的回调函数 +static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info) +{ + if (!info) return; + +//debug +#if 1 + printf("OnWifiConnectionChanged info: %p\r\n", info); +#endif + + printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state); + PrintLinkedInfo(info); + + if (state == WIFI_STATE_AVAILABLE) { + g_connected = 1; + } else { + g_connected = 0; + } +} + +// 定义一个OnWifiScanStateChanged函数,用于处理wifi扫描状态改变的事件 +static void OnWifiScanStateChanged(int state, int size) +{ + // 打印函数名和行号 + printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size); +} + +// 定义一个g_defaultWifiEventListener变量,用于保存默认的wifi事件监听器 +static WifiEvent g_defaultWifiEventListener = { + .OnWifiConnectionChanged = OnWifiConnectionChanged, + .OnWifiScanStateChanged = OnWifiScanStateChanged +}; + +// 定义一个g_iface变量,用于保存网络接口 +static struct netif* g_iface = NULL; + +/*连接到AP*/ +int ConnectToHotspot(WifiDeviceConfig* apConfig) +{ + WifiErrorCode errCode; + int netId = -1; + + //注册WiFi事件监听器 + errCode = RegisterWifiEvent(&g_defaultWifiEventListener); + printf("RegisterWifiEvent: %d\r\n", errCode); + + //启用WiFi + errCode = EnableWifi(); + printf("EnableWifi: %d\r\n", errCode); + + //添加设备配置 + errCode = AddDeviceConfig(apConfig, &netId); + printf("AddDeviceConfig: %d\r\n", errCode); + + //连接到指定的网络ID + g_connected = 0; + errCode = ConnectTo(netId); + printf("ConnectTo(%d): %d\r\n", netId, errCode); + + //等待连接到AP + while (!g_connected) { + osDelay(TEN); + } + printf("g_connected: %d\r\n", g_connected); + + //获取网络接口 + g_iface = netifapi_netif_find("wlan0"); + if (g_iface) { + //启动DHCP + err_t ret = netifapi_dhcp_start(g_iface); + printf("netifapi_dhcp_start: %d\r\n", ret); + + //等待DHCP服务器给我IP + osDelay(ONE_HUNDRED); + //获取DHCP客户端信息 + ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL); + printf("netifapi_netif_common: %d\r\n", ret); + } + return netId; +} + +/*断开与AP的连接*/ +void DisconnectWithHotspot(int netId) +{ + // 断开热点的连接 + if (g_iface) { + err_t ret = netifapi_dhcp_stop(g_iface); + printf("netifapi_dhcp_stop: %d\r\n", ret); + } + + // 断开与AP的连接 + WifiErrorCode errCode = Disconnect(); + printf("Disconnect: %d\r\n", errCode); + + // 取消注册wifi事件 + errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener); + printf("UnRegisterWifiEvent: %d\r\n", errCode); + + // 移除AP配置 + RemoveDevice(netId); + printf("RemoveDevice: %d\r\n", errCode); + + // 禁用wifi + errCode = DisableWifi(); + printf("DisableWifi: %d\r\n", errCode); +} + diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.h new file mode 100644 index 0000000000000000000000000000000000000000..8f5e5d4ea544f97581b8def4935479b5d5d11f31 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_connecter.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 WIFI_CONNECTER_H +#define WIFI_CONNECTER_H + +#include "wifi_device.h" + +int ConnectToHotspot(WifiDeviceConfig* apConfig); + +void DisconnectWithHotspot(int netId); + +#endif // WIFI_CONNECTER_H \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.c b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.c new file mode 100644 index 0000000000000000000000000000000000000000..1aa81589d3353256569ff5a687c0972e6b0ffd32 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.c @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 "wifi_starter.h" +#include "cmsis_os2.h" + +#include "lwip/netifapi.h" +#define ZERO 0 +#define ONE 1 +#define TWO 2 +#define THREE 3 +#define FOUR 4 +#define FIVE 5 +#define TEN 10 + +static volatile int g_hotspotStarted = 0; + +static void OnHotspotStateChanged(int state) +{ + printf("OnHotspotStateChanged: %d.\r\n", state); + if (state == WIFI_HOTSPOT_ACTIVE) { + g_hotspotStarted = 1; + } else { + g_hotspotStarted = 0; + } +} + +static volatile int g_joinedStations = 0; + +static void PrintStationInfo(StationInfo* info) +{ + if (!info) return; + static char macAddress[32] = {0}; + unsigned char* mac = info->macAddress; + if (snprintf_s(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X", + mac[ZERO], mac[ONE], mac[TWO], mac[THREE], mac[FOUR], mac[FIVE]) == TRUE) { + printf("OK"); + } + + printf(" PrintStationInfo: mac=%s, reason=%d.\r\n", macAddress, info->disconnectedReason); +} + +static void OnHotspotStaJoin(StationInfo* info) +{ +//debug +#if 1 + printf("OnWifiConnectionChanged info: %p\r\n", info); +#endif + g_joinedStations++; + PrintStationInfo(info); + printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations); +} + +static void OnHotspotStaLeave(StationInfo* info) +{ + g_joinedStations--; + PrintStationInfo(info); + printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations); +} + +WifiEvent g_defaultWifiEventListener = { + .OnHotspotStaJoin = OnHotspotStaJoin, + .OnHotspotStaLeave = OnHotspotStaLeave, + .OnHotspotStateChanged = OnHotspotStateChanged, +}; + +static struct netif* g_iface = NULL; + +int StartHotspot(const HotspotConfig* config) +{ + WifiErrorCode errCode = WIFI_SUCCESS; + + errCode = RegisterWifiEvent(&g_defaultWifiEventListener); + printf("RegisterWifiEvent: %d\r\n", errCode); + + errCode = SetHotspotConfig(config); + printf("SetHotspotConfig: %d\r\n", errCode); + + g_hotspotStarted = 0; + errCode = EnableHotspot(); + printf("EnableHotspot: %d\r\n", errCode); + + while (!g_hotspotStarted) { + osDelay(TEN); + } + printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted); + +#if CHIP_WS63 +#else + g_iface = netifapi_netif_find("ap0"); + if (g_iface) { + ip4_addr_t ipaddr; + ip4_addr_t gateway; + ip4_addr_t netmask; + + IP4_ADDR(&ipaddr, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */ + IP4_ADDR(&gateway, 192, 168, 1, 1); /* input your gateway for example: 192.168.1.1 */ + IP4_ADDR(&netmask, 255, 255, 255, 0); /* input your netmask for example: 255.255.255.0 */ + err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway); + printf("netifapi_netif_set_addr: %d\r\n", ret); + + ret = netifapi_dhcps_start(g_iface, 0, 0); + printf("netifapi_dhcp_start: %d\r\n", ret); + } +#endif + return errCode; +} + +void StopHotspot(void) +{ + if (g_iface) { + err_t ret = netifapi_dhcps_stop(g_iface); + printf("netifapi_dhcps_stop: %d\r\n", ret); + } + + WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener); + printf("UnRegisterWifiEvent: %d\r\n", errCode); + + errCode = DisableHotspot(); + printf("EnableHotspot: %d\r\n", errCode); +} diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.h b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.h new file mode 100644 index 0000000000000000000000000000000000000000..b13a3f97a9d43003ea21fb60792e1a735ce591de --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/easy_wifi_demo/src/wifi_starter.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2022 HiHope Open Source Organization . + * 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 WIFI_CONNECTER_H +#define WIFI_CONNECTER_H + +#include +#include "wifi_hotspot.h" + +int StartHotspot(const HotspotConfig* config); + +void StopHotspot(void); + +#endif // WIFI_CONNECTER_H \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/images/sle_process.png b/vendor/hellokun/demo/sparklink_car_net/rsu/images/sle_process.png new file mode 100644 index 0000000000000000000000000000000000000000..68e600f5cda06bbf7d6eb92ea2cce07da1fc112a Binary files /dev/null and b/vendor/hellokun/demo/sparklink_car_net/rsu/images/sle_process.png differ diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/rsu_sle.md b/vendor/hellokun/demo/sparklink_car_net/rsu/rsu_sle.md new file mode 100644 index 0000000000000000000000000000000000000000..2883970b88a32767b23aae956a86d0d315089b68 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/rsu_sle.md @@ -0,0 +1,301 @@ +# RSU——SLE解析 + +## 一. 前言 + +本文章分析了SLE的接口及流程,使用。 + +sle的client端和server端的的流程挺相似,都是通过注册相关回调函数后EnableSle开启SLE,后续过程依赖回调函数触发进行,相关代码被编译成了库并没有开源出来。 + +相关的接口头文件可以在下面目录找到: + +`device/soc/hisilicon/ws63v100/sdkv100/include/middleware/services/bts/sle` + +`foundation/communication/sle` + +## 二. server端解析 + +sle的server进程就执行了sle_uart_server_init,主要进行回调函数的注册,开启SLE。 + +```jsx +errcode_t sle_uart_server_init() +{ + errcode_t ret; + ret = sle_uart_announce_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_announce_register_cbks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_conn_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_conn_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_ssaps_register_cbks(ssaps_read_request_callbacks, ssaps_write_request_callbacks); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_ssaps_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = EnableSle(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,enable_sle fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + sample_at_log_print("%s init ok\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} +``` + +### 回调注册 + +1. SleAnnounceSeekRegisterCallbacks注册设备公开和设备发现回调函数。 + +```c +typedef struct{ + SleEnableCallback sleEnableCb; /* 开启sle回调函数 */ + SleDisableCallback sleDisableCb; /* 关闭sle回调函数 */ + SleAnnounceEnableCallback sleAnnounceEnableCb; /* 开启sle广播回调函数 */ + SleAnnounceDisableCallback sleAnnounceDisableCb; /* 关闭sle广播回调函数 */ + SleAnnounceTerminalCallback sleAnnounceTerminalCb; /* sle广播终止的回调函数 */ + SleSeekResultCallback sleSeekResultCb; /* 搜索结果回调函数 */ + +}SleAnnounceSeekCallbacks; +``` + +1. SleConnectionRegisterCallbacks注册连接管理回调函数。 + +```c +typedef struct { + SleConnectStateChangedCallback connectStateChangedCb; /*!< @if Eng Connect state changed callback. + @else 连接状态改变回调函数。 @endif */ + SleConnectParamUpdateReqCallback connectParamUpdateReqCb; /*!< @if Eng Connect param updated callback. + @else 连接参数更新回调函数。 @endif */ + SleConnectParamUpdateCallback connectParamUpdateCb; /*!< @if Eng Connect param updated callback. + @else 连接参数更新回调函数。 @endif */ + SleAuthCompleteCallback authCompleteCb; /*!< @if Eng Authentication complete callback. + @else 认证完成回调函数。 @endif */ + SlePairCompleteCallback pairCompleteCb; /*!< @if Eng Pairing complete callback. + @else 配对完成回调函数。 @endif */ + SleReadRssiCallback readRssiCb; /*!< @if Eng Read rssi callback. + @else 读取rssi回调函数。 @endif */ +} SleConnectionCallbacks; +``` + +1. SsapsRegisterCallbacks注册服务端回调函数 + +```c +typedef struct{ + SsapsAddServiceCallback addServiceCb; /*!< @if Eng Service added callback. + @else 添加服务回调函数。 @endif */ + SsapsAddPropertyCallback addPropertyCb; /*!< @if Eng Characteristc added callback. + @else 添加特征回调函数。 @endif */ + SsapsAddDescriptorCallback addDescriptorCb; /*!< @if Eng Descriptor added callback. + @else 添加描述符回调函数。 @endif */ + SsapsStartServiceCallback startServiceCb; /*!< @if Eng Service started callback. + @else 启动服务回调函数。 @endif */ + SsapsDeleteAllServiceCallback deleteAllServiceCb; /*!< @if Eng Service deleted callback. + @else 删除服务回调函数。 @endif */ + SsapsReadRequestCallback readRequestCb; /*!< @if Eng Read request received callback. + @else 收到远端读请求回调函数。 @endif */ + SsapsWriteRequestCallback writeRequestCb; /*!< @if Eng Write request received callback. + @else 收到远端写请求回调函数。 @endif */ + SsapsMtuChangedCallback mtuChangedCb; /*!< @if Eng Mtu changed callback. + @else mtu 大小更新回调函数。 @*/ + +}SsapsCallbacks; +``` + +### 连接配置 + +配置本地名称,client端通过名称进行匹配,通过一下代码修改广播名称。 + +```jsx +/* 广播名称 */ +static uint8_t sle_local_name[NAME_MAX_LENGTH] = "rsu_sle_server"; +``` + +配置广播参数,通过sle_set_default_announce_param可以进行广播配置,设置连接和扫描模式等参数,还有加密但没有具体用法,具体的配对过程没有相关代码。 + +```c +static int sle_set_default_announce_param(void) +{ + errno_t ret; + SleAnnounceParam param = {0}; + + uint8_t index; + unsigned char local_addr[SLE_ADDR_LEN] = { 0x78, 0x70, 0x60, 0x88, 0x96, 0x45 }; + param.announceMode = SLE_ANNOUNCE_MODE_CONNECTABLE_SCANABLE; + param.announceHandle = SLE_ADV_HANDLE_DEFAULT; + param.announceGtRole = SLE_ANNOUNCE_ROLE_T_CAN_NEGO; + param.announceLevel = SLE_ANNOUNCE_LEVEL_NORMAL; + param.announceChannelMap = SLE_ADV_CHANNEL_MAP_DEFAULT; + param.announceIntervalMin = SLE_ADV_INTERVAL_MIN_DEFAULT; + param.connIntervalMin = SLE_CONN_INTV_MIN_DEFAULT; + param.announceIntervalMax = SLE_ADV_INTERVAL_MAX_DEFAULT; + param.connIntervalMax = SLE_CONN_INTV_MAX_DEFAULT; + param.connMaxLatency = SLE_CONN_MAX_LATENCY; + param.connSupervisionTimeout = SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT; + param.ownAddr.type = 0; + ret = memcpy_s(param.ownAddr.addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + + if (ret != EOK) { + sample_at_log_print("%s sle_set_default_announce_param data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + sample_at_log_print("%s sle_uart_local addr: ", SLE_UART_SERVER_LOG); + for (index = 0; index < SLE_ADDR_LEN; index++) { + sample_at_log_print("0x%02x ", param.ownAddr.addr[index]); + } + sample_at_log_print("\r\n"); + return SleSetAnnounceParam(param.announceHandle, ¶m); +} +``` + +```c +/* sle device name */ +#define NAME_MAX_LENGTH 16 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MIN_DEFAULT 0x64 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MAX_DEFAULT 0x64 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MIN_DEFAULT 0xC8 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MAX_DEFAULT 0xC8 +/* 超时时间5000ms,单位10ms */ +#define SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT 0x1F4 +/* 超时时间4990ms,单位10ms */ +#define SLE_CONN_MAX_LATENCY 0x1F3 +/* 广播发送功率 */ +#define SLE_ADV_TX_POWER 10 +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* 最大广播数据长度 */ +#define SLE_ADV_DATA_LEN_MAX 251 +/* 广播名称 */ +``` + +## 三. Clinet端解析 + +Clinet端进程也是通过sle_uart_client_init进行回调函数的注册和开启SLE。 + +```jsx +void sle_uart_client_init() +{ + uint8_t local_addr[SLE_ADDR_LEN] = { 0x13, 0x67,0x5c, 0x07, 0x00, 0x51 };//0x04-->0x07 + SleAddr local_address; + local_address.type = 0; + (void)memcpy_s(local_address.addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + sle_uuid_client_register(); + sle_uart_client_sample_seek_cbk_register(); + sle_uart_client_sample_connect_cbk_register(); + sle_uart_client_sample_ssapc_cbk_register(ssapc_notification_callbacks, ssapc_indication_callbacks); + EnableSle(); + SleSetLocalAddr(&local_address); + +} +``` + +### 回调注册 + +1. SsapsRegisterCallbacks*注册SLE设备发现回调函数* + +```jsx +typedef struct { + sle_enable_callback sle_enable_cb; /*!< @if Eng SLE stack enable callback. + @else SLE协议栈使能回调函数。 @endif */ + sle_disable_callback sle_disable_cb; /*!< @if Eng SLE stack disable callback. + @else SLE协议栈去使能回调函数。 @endif */ + sle_announce_enable_callback announce_enable_cb; /*!< @if Eng device announce enable callback. + @else 设备公开使能回调函数。 @endif */ + sle_announce_disable_callback announce_disable_cb; /*!< @if Eng device announce disable callback. + @else 设备公开关闭回调函数。 @endif */ + sle_announce_terminal_callback announce_terminal_cb; /*!< @if Eng device announce terminated callback. + @else 设备公开停止回调函数。 @endif */ + sle_announce_remove_callback announce_remove_cb; /*!< @if Eng device announce remove callback. + @else 设备公开停止回调函数。 @endif */ + sle_start_seek_callback seek_enable_cb; /*!< @if Eng scan enable callback. + @else 扫描使能回调函数。 @endif */ + sle_seek_disable_callback seek_disable_cb; /*!< @if Eng scan disable callback. + @else 扫描关闭回调函数。 @endif */ + sle_seek_result_callback seek_result_cb; /*!< @if Eng scan result callback. + @else 扫描结果回调函数。 @endif */ +} sle_announce_seek_callbacks_t; +``` + +1. SleConnectionRegisterCallbacks*注册SLE设备连接回调函数* + +```jsx +typedef struct { + SleConnectStateChangedCallback connectStateChangedCb; /*!< @if Eng Connect state changed callback. + @else 连接状态改变回调函数。 @endif */ + SleConnectParamUpdateReqCallback connectParamUpdateReqCb; /*!< @if Eng Connect param updated callback. + @else 连接参数更新回调函数。 @endif */ + SleConnectParamUpdateCallback connectParamUpdateCb; /*!< @if Eng Connect param updated callback. + @else 连接参数更新回调函数。 @endif */ + SleAuthCompleteCallback authCompleteCb; /*!< @if Eng Authentication complete callback. + @else 认证完成回调函数。 @endif */ + SlePairCompleteCallback pairCompleteCb; /*!< @if Eng Pairing complete callback. + @else 配对完成回调函数。 @endif */ + SleReadRssiCallback readRssiCb; /*!< @if Eng Read rssi callback. + @else 读取rssi回调函数。 @endif */ +} SleConnectionCallbacks; +``` + +1. ssapc_register_callbacks*注册SSAP客户端回调函数。* + +```c +typedef struct { + ssapc_find_structure_callback find_structure_cb; /*!< @if Eng Discovery structure callback. + @else 发现服务回调函数。 @endif */ + ssapc_find_property_callback ssapc_find_property_cbk; /*!< @if Eng Discovery property callback. + @else 发现特征回调函数。 @endif */ + ssapc_find_structure_complete_callback find_structure_cmp_cb; /*!< @if Eng Discovery structure complete callback. + @else 发现特征完成回调函数。 @endif */ + ssapc_read_cfm_callback read_cfm_cb; /*!< @if Eng Receive write response callback. + @else 收到读响应回调函数。 @endif */ + ssapc_read_by_uuid_complete_callback read_by_uuid_cmp_cb; /*!< @if Eng Callback hook for read property complete. + @else 读特征值完成回调钩子。 @endif */ + ssapc_write_cfm_callback write_cfm_cb; /*!< @if Eng Receive write response callback. + @else 收到写响应回调函数。 @endif */ + ssapc_exchange_info_callback exchange_info_cb; /*!< @if Eng Callback hook for configure mtu size + complete. + @else 更新mtu大小回调钩子。 @endif */ + ssapc_notification_callback notification_cb; /*!< @if Eng Callback hook for receive notification. + @else 通知事件上报钩子。 @endif */ + ssapc_indication_callback indication_cb; /*!< @if Eng Callback hook for receive indication. + @else 指示事件上报钩子。 @endif */ +} ssapc_callbacks_t; +``` + +### 连接配置 + +client的连接是通过SleConnectRemoteDevice(const SleAddr **addr*)进行连接,地址通过扫描得到的server设备信息,通过SERVER_NAME进行匹配,拿到对应的地址进行连接,SERVER_NAME通过下面这个宏进行修改。 + +```jsx +#define SLE_UART_SERVER_NAME "rsu_sle_server" +``` + +扫描参数也可进行配置,例如*设备发现类型,过滤类型等,*对应值可以在`device/soc/hisilicon/ws63v100/sdkv100/include/middleware/services/bts/sle/sle_device_discovery.h`找。 + +```jsx +void sle_uart_start_scan(void) +{ + SleSeekParam param = { 0 }; + param.ownaddrtype = 0; + param.filterduplicates = 0; + param.seekfilterpolicy = 0; + param.seekphys = 1; + param.seekType[0] = 1; + param.seekInterval[0] = SLE_SEEK_INTERVAL_DEFAULT; + param.seekWindow[0] = SLE_SEEK_WINDOW_DEFAULT; + SleSetSeekParam(¶m); + SleStartSeek(); +} +``` + +## 四. 通信流程 + +整体的通信流程如下: + +![sle_ process](./images/sle_process.png) \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/BUILD.gn b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e42187c95ae5d55f856d4528ffe95eb6f122db2f --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/BUILD.gn @@ -0,0 +1,39 @@ +# Copyright (c) 2020-2022 Huawei Device Co., 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. + +static_library("sle_uart_client") { + sources = [ + "sle_uart_client.c", + ] + + defines = [ + "CONFIG_UART_SUPPORT_TX", + "CONFIG_UART_SUPPORT_RX", + ] + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//device/soc/hisilicon/ws63v100/sdkv100/include/middleware/services/bts/sle", + "//foundation/communication/sle", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/debug", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/memory", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/schedule", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//base/iothardware/peripheral/interfaces/inner_api", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/drivers/hal/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/porting/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/middleware/utils/common_headers/native", + ] +} diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/README.md b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e1148158f7709e6085ecf65d533e76679f5862e1 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/README.md @@ -0,0 +1,72 @@ +# 客户端和服务端使用星闪功能转发串口数据 + +### Device Discovery OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | --------------------------------------- | +| `EnableSle` | 开启SLE | +| `SleSetLocalAddr` | 设置本地设备地址 | +| `SleSetAnnounceData` | 设置设备公开数据 | +| `SleSetAnnounceParam` | 设置设备公开参数 | +| `SleStartAnnounce` | 开始广播 | +| `SleSetSeekParam` | 设置扫描参数 | +| `SleStartSeek` | 开始扫描 | +| `SleStopSeek` | 停止扫描 | +| `SleAnnounceSeekRegisterCallbacks` | 注册设备公开和设备发现回调函数 | + +### Connection Manager OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -----------------------| +| `SleConnectRemoteDevice` | 向对端设备发起连接请求 | +| `SleConnectionRegisterCallbacks` | 注册连接管理回调函数 | + +### SSAP server OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `ssapsRegisterServer` | 注册 SSAP server | +| `SsapsAddDescriptorSync` | 添加特征描述符同步接口 | +| `SsapsAddPropertySync` | 添加特征同步接口 | +| `SsapsAddServiceSync` | 添加服务同步接口 | +| `SsapsStartService` | 启动服务 | +| `SsapsNotifyIndicate` | 给对端发送通知或指示 | +| `SsapsRegisterCallbacks` | 注册服务端回调函数 | + +### SSAP client OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `SsapcRegisterClient` | 注册 SSAP client | +| `SsapWriteReq` | 发起写请求 | + +### SSAP client 原生SDK接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `ssapc_register_callbacks` | 注册客户端回调函数 | + + +### UART 原生SDK接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `uapi_uart_register_rx_callback` | 注册串口接收回调函数 | + + + + + +## 如何编译 + +本项目下有两个示例代码,需要运行在两块WS63开发板上,一块作为服务端,一块作为客户端,SLE服务端需要注释掉`"sle_uart_client.c"`行,放开`"sle_uart_server_adv.c","sle_uart_server.c"`行,SLE客户端需要注释掉`"sle_uart_server_adv.c","sle_uart_server.c"`行,放开`"sle_uart_client.c"`行,之前生成的libsle_uart_demo.a静态库文件需要删除; + +1. 将sle_uart_demo文件夹克隆到本地openharmony源码的applications\sample\wifi-iot\app目录下; +2. 修改openharmony的`applications\sample\wifi-iot\app\BUILD.gn`文件: + * 将其中的 features 改为: + features = [ + "sle_uart_demo:sle_uart_demo", + ] +3. 在device/soc/hisilicon/ws63v100/sdkv100/build/config/target_config/ws63/config.py文件的`'ram_component': []`中添加`"sle_uart_demo"`,在device/soc/hisilicon/ws63v100/sdkv100/libs_url/ws63cmake/ohos.cmake文件的`set(COMPONENT_LIST)`中添加`"sle_uart_demo"`; +4. 执行编译命令:`hb build -f` +5. 运行结果:两块ws63开发板可以通过星闪功能转发串口数据,波特率默认为115200,服务端在接收到客户端发送的数据`"123"`后会打印`"client_send_data: 123"`; \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.c b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.c new file mode 100644 index 0000000000000000000000000000000000000000..ee57e8bed7862bc4ae972e71b616d36a2eb9813f --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.c @@ -0,0 +1,377 @@ +/** +# Copyright (C) 2024 HiHope Open Source Organization . +# 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 "string.h" +#include "common_def.h" +#include "osal_debug.h" +#include "osal_task.h" +#include "cmsis_os2.h" +#include "securec.h" +#include "sle_device_discovery.h" +#include "sle_connection_manager.h" +#include "sle_ssap_client.h" +#include "sle_uart_client.h" +#include "sle_errcode.h" +#include "ohos_init.h" +#include "ohos_sle_common.h" +#include "ohos_sle_errcode.h" +#include "ohos_sle_ssap_server.h" +#include "ohos_sle_ssap_client.h" +#include "ohos_sle_device_discovery.h" +#include "ohos_sle_connection_manager.h" +#include "iot_uart.h" +#include "pinctrl.h" +#include "uart.h" +#include "errcode.h" + +#define SLE_MTU_SIZE_DEFAULT 512 +#define SLE_SEEK_INTERVAL_DEFAULT 100 +#define SLE_SEEK_WINDOW_DEFAULT 100 +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +#define SLE_UART_TASK_DELAY_MS 1000 +#define SLE_UART_WAIT_SLE_CORE_READY_MS 5000 +#ifndef SLE_UART_SERVER_NAME +#define SLE_UART_SERVER_NAME "rsu_sle_server" +#endif +#define SLE_UART_CLIENT_LOG "[sle uart client]" +#define UUID_LEN_2 2 + +static char g_sle_uuid_app_uuid[] = { 0x39, 0xBE, 0xA8, 0x80, 0xFC, 0x70, 0x11, 0xEA, \ + 0xB7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +static unsigned char uartReadBuff[100]; +static ssapc_find_service_result_t g_sle_uart_find_service_result = { 0 }; +static sle_announce_seek_callbacks_t g_sle_uart_seek_cbk = { 0 }; +static SleConnectionCallbacks g_sle_uart_connect_cbk = { 0 }; +static ssapc_callbacks_t g_sle_uart_ssapc_cbk = { 0 }; +static SleAddr g_sle_uart_remote_addr = { 0 }; +ssapc_write_param_t g_sle_uart_send_param = { 0 }; +uint16_t g_sle_uart_conn_id = 0; +uint8_t g_client_id = 0; +static uint8_t g_at_pre_char = 0; +static uint32_t g_at_uart_recv_cnt = 0; +uint16_t get_g_sle_uart_conn_id(void) +{ + return g_sle_uart_conn_id; +} + +ssapc_write_param_t *get_g_sle_uart_send_param(void) +{ + return &g_sle_uart_send_param; +} + +static void uart_rx_callback(const void *buffer, uint16_t length, bool error) +{ + errcode_t ret; + const uint8_t *data = (const uint8_t *)buffer; + if (error) { + osal_printk("*******uart error*******\r\n"); + } + g_at_uart_recv_cnt += length; + if (((char *)buffer)[0] == 0xD) { + uapi_uart_write(0, (uint8_t *)"\r\n", (uint16_t)strlen("\r\n"), 0); + } else { + uapi_uart_write(0, (const uint8_t *)buffer, (uint32_t)length, 0); + } + ret = uapi_at_channel_data_recv(0, (uint8_t *)buffer, (uint32_t)length); + if (ret != 0) { + /* 前一个字符为'\r'时单独一个'\n'导致的CHANNEL_BUSY不打印 */ + if (g_at_pre_char != '\r' || length != 1 || data[0] != '\n' || ret != ERRCODE_AT_CHANNEL_BUSY) { + osal_printk("\r\nat_uart_rx_callback fail:0x%x\r\n", ret); + } + } + if(length > 0){ + ret = uart_sle_client_send_data((uint8_t *)buffer,(uint8_t)length); + if(ret != 0){ + osal_printk("\r\nsle_client_send_data_fail:%d\r\n", ret); + } +} + +} + +void sle_uart_start_scan(void) +{ + SleSeekParam param = { 0 }; + param.ownaddrtype = 0; + param.filterduplicates = 0; + param.seekfilterpolicy = 0; + param.seekphys = 1; + param.seekType[0] = 1; + param.seekInterval[0] = SLE_SEEK_INTERVAL_DEFAULT; + param.seekWindow[0] = SLE_SEEK_WINDOW_DEFAULT; + SleSetSeekParam(¶m); + SleStartSeek(); +} + +static void sle_uart_client_sample_sle_enable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_sle_enable_cbk,status error\r\n", SLE_UART_CLIENT_LOG); + } else { + osal_msleep(SLE_UART_TASK_DELAY_MS); + sle_uart_start_scan(); + } +} + +static void sle_uart_client_sample_seek_enable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_enable_cbk,status error\r\n", SLE_UART_CLIENT_LOG); + } +} + +static void sle_uart_client_sample_seek_result_info_cbk(SleSeekResultInfo *seek_result_data) +{ + osal_printk("%s sle uart scan data :%s\r\n", SLE_UART_CLIENT_LOG, seek_result_data->data); + if (seek_result_data == NULL) { + osal_printk("status error\r\n"); + } else if (strstr((const char *)seek_result_data->data, SLE_UART_SERVER_NAME) != NULL) { + memcpy_s(&g_sle_uart_remote_addr, sizeof(sle_addr_t), &seek_result_data->addr, sizeof(sle_addr_t)); + SleStopSeek(); + + } +} + +static void sle_uart_client_sample_seek_disable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_disable_cbk,status error = %x\r\n", SLE_UART_CLIENT_LOG, status); + } else { + SleConnectRemoteDevice(&g_sle_uart_remote_addr); + } +} + +static void sle_uart_client_sample_seek_cbk_register(void) +{ + g_sle_uart_seek_cbk.sle_enable_cb = sle_uart_client_sample_sle_enable_cbk; + g_sle_uart_seek_cbk.seek_enable_cb = sle_uart_client_sample_seek_enable_cbk; + g_sle_uart_seek_cbk.seek_result_cb = sle_uart_client_sample_seek_result_info_cbk; + g_sle_uart_seek_cbk.seek_disable_cb = sle_uart_client_sample_seek_disable_cbk; + sle_announce_seek_register_callbacks(&g_sle_uart_seek_cbk); +} + +static void sle_uart_client_sample_connect_state_changed_cbk(uint16_t conn_id, const SleAddr *addr, + SleAcbStateType conn_state, SlePairStateType pair_state, SleDiscReasonType disc_reason) +{ + unused(addr); + unused(pair_state); + osal_printk("%s conn state changed disc_reason:0x%x\r\n", SLE_UART_CLIENT_LOG, disc_reason); + g_sle_uart_conn_id = conn_id; + if (conn_state == SLE_ACB_STATE_CONNECTED) { + osal_printk("%s SLE_ACB_STATE_CONNECTED\r\n", SLE_UART_CLIENT_LOG); + SsapcExchangeInfo info = {0}; + info.mtuSize = SLE_MTU_SIZE_DEFAULT; + info.version = 1; + SsapcExchangeInfoReq(0, conn_id, &info); + (void)uapi_uart_register_rx_callback(0, UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 16, uart_rx_callback); + } else if (conn_state == SLE_ACB_STATE_NONE) { + osal_printk("%s SLE_ACB_STATE_NONE\r\n", SLE_UART_CLIENT_LOG); + } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) { + osal_printk("%s SLE_ACB_STATE_DISCONNECTED\r\n", SLE_UART_CLIENT_LOG); + sle_uart_start_scan(); + + } else { + osal_printk("%s status error\r\n", SLE_UART_CLIENT_LOG); + } + + +} + +static void sle_uart_client_sample_connect_cbk_register(void) +{ + g_sle_uart_connect_cbk.connectStateChangedCb = sle_uart_client_sample_connect_state_changed_cbk; + SleConnectionRegisterCallbacks(&g_sle_uart_connect_cbk); +} + +static void sle_uart_client_sample_exchange_info_cbk(uint8_t client_id, uint16_t conn_id, ssap_exchange_info_t *param, + errcode_t status) +{ + osal_printk("%s exchange_info_cbk,pair complete client id:%d status:%d\r\n", + SLE_UART_CLIENT_LOG, client_id, status); + osal_printk("%s exchange mtu, mtu size: %d, version: %d.\r\n", SLE_UART_CLIENT_LOG, + param->mtu_size, param->version); + ssapc_find_structure_param_t find_param = { 0 }; + find_param.type = 1; + find_param.start_hdl = 1; + find_param.end_hdl = 0xFFFF; + int ret = ssapc_find_structure(client_id, conn_id, &find_param); + + printf("ssapc_find_structure_errcode:%d\r\n",ret); +} + +static void sle_uart_client_sample_find_structure_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_service_result_t *service, + errcode_t status) +{ + osal_printk("%s find structure cbk client: %d conn_id:%d status: %d \r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, status); + osal_printk("%s find structure start_hdl:[0x%02x], end_hdl:[0x%02x], uuid len:%d\r\n", SLE_UART_CLIENT_LOG, + service->start_hdl, service->end_hdl, service->uuid.len); + g_sle_uart_find_service_result.start_hdl = service->start_hdl; + g_sle_uart_find_service_result.end_hdl = service->end_hdl; + memcpy_s(&g_sle_uart_find_service_result.uuid, sizeof(sle_uuid_t), &service->uuid, sizeof(sle_uuid_t)); + + +} + +static void sle_uart_client_sample_find_property_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_property_result_t *property, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_find_property_cbk, client id: %d, conn id: %d, operate ind: %d, " + "descriptors count: %d status:%d property->handle %d\r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, property->operate_indication, + property->descriptors_count, status, property->handle); + g_sle_uart_send_param.handle = property->handle; + g_sle_uart_send_param.type = SSAP_PROPERTY_TYPE_VALUE; +} + +static void sle_uart_client_sample_find_structure_cmp_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_structure_result_t *structure_result, + errcode_t status) +{ + unused(conn_id); + + + osal_printk("%s sle_uart_client_sample_find_structure_cmp_cbk,client id:%d status:%d type:%d uuid len:%d \r\n", + SLE_UART_CLIENT_LOG, client_id, status, structure_result->type, structure_result->uuid.len); +} + +static void sle_uart_client_sample_write_cfm_cb(uint8_t client_id, uint16_t conn_id, + ssapc_write_result_t *write_result, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_write_cfm_cb, conn_id:%d client id:%d status:%d handle:%02x type:%02x\r\n", + SLE_UART_CLIENT_LOG, conn_id, client_id, status, write_result->handle, write_result->type); +} + +static void sle_uart_client_sample_ssapc_cbk_register(ssapc_notification_callback notification_cb, + ssapc_indication_callback indication_cb) +{ + g_sle_uart_ssapc_cbk.exchange_info_cb = sle_uart_client_sample_exchange_info_cbk; + g_sle_uart_ssapc_cbk.find_structure_cb = sle_uart_client_sample_find_structure_cbk; + g_sle_uart_ssapc_cbk.ssapc_find_property_cbk = sle_uart_client_sample_find_property_cbk; + g_sle_uart_ssapc_cbk.find_structure_cmp_cb = sle_uart_client_sample_find_structure_cmp_cbk; + g_sle_uart_ssapc_cbk.write_cfm_cb = sle_uart_client_sample_write_cfm_cb; + g_sle_uart_ssapc_cbk.notification_cb = notification_cb; + g_sle_uart_ssapc_cbk.indication_cb = indication_cb; + ssapc_register_callbacks(&g_sle_uart_ssapc_cbk); +} + + +static errcode_t sle_uuid_client_register(void) +{ + errcode_t ret; + SleUuid app_uuid = {0}; + + printf("[uuid client] ssapc_register_client \r\n"); + app_uuid.len = sizeof(g_sle_uuid_app_uuid); + if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) { + return ERRCODE_SLE_FAIL; + } + ret = SsapcRegisterClient(&app_uuid, &g_client_id); + + return ret; +} + + + +/* device通过handle向host发送数据:report */ +errcode_t sle_uart_client_send_report_by_handle(const uint8_t *data, uint8_t len) +{ + ssapc_write_param_t param = {0}; + uint8_t receive_buf[0x100] = { 0 }; /* max receive length. */ + param.handle = g_sle_uart_find_service_result.start_hdl; + param.type = 0; + param.data = receive_buf; + param.data_len = len+1; + if (memcpy_s(param.data, param.data_len, data, len) != EOK) { + return ERRCODE_SLE_FAIL; + } + + int ret =SsapWriteReq(g_client_id,g_sle_uart_conn_id, ¶m); + + + return ret; +} + + +int uart_sle_client_send_data(uint8_t *data,uint8_t length){ + int ret; + ret = sle_uart_client_send_report_by_handle(data,length); + return ret ; +} + +void ssapc_notification_callbacks(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, + errcode_t status){ + (void)client_id; + (void)conn_id; + (void)status; + + data->data[data->data_len -1] = '\0'; + printf("server_send_data: %s\r\n",data->data); + } + +void ssapc_indication_callbacks(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, + errcode_t status){ +(void)client_id; + (void)conn_id; + (void)data; + (void)status; + } + + +void sle_uart_client_init() +{ + uint8_t local_addr[SLE_ADDR_LEN] = { 0x13, 0x67,0x5c, 0x07, 0x00, 0x51 };//0x04-->0x07 + SleAddr local_address; + local_address.type = 0; + (void)memcpy_s(local_address.addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + sle_uuid_client_register(); + sle_uart_client_sample_seek_cbk_register(); + sle_uart_client_sample_connect_cbk_register(); + sle_uart_client_sample_ssapc_cbk_register(ssapc_notification_callbacks, ssapc_indication_callbacks); + EnableSle(); + SleSetLocalAddr(&local_address); + +} + + + + +static void SleTask( char* arg) +{ + (void)arg; + + sle_uart_client_init(); + + return NULL; +} + +static void SleClientExample(void) +{ + osThreadAttr_t attr; + attr.name = "SleTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 2048; + attr.priority = 25; + + if (osThreadNew(SleTask, NULL, &attr) == NULL) { + printf("[SleExample] Falied to create SleTask!\n"); + }else printf("[SleExample] create SleTask successfully !\n"); +} + +SYS_RUN(SleClientExample); // if test add it \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.h b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.h new file mode 100644 index 0000000000000000000000000000000000000000..1f829c6ea907de9f6033c642c00bb139ad5ccc78 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_client/sle_uart_client.h @@ -0,0 +1,30 @@ +/** +# Copyright (C) 2024 HiHope Open Source Organization . +# 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 SLE_UART_CLIENT_H +#define SLE_UART_CLIENT_H + +#include "sle_ssap_client.h" + +void sle_uart_client_init(void); + +void sle_uart_start_scan(void); + +uint16_t get_g_sle_uart_conn_id(void); + +ssapc_write_param_t *get_g_sle_uart_send_param(void); + + +int uart_sle_client_send_data(uint8_t *data,uint8_t length); +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/BUILD.gn b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d61d6c9c37e0fba8397c14127d492c9558cb955b --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2020-2022 Huawei Device Co., 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. + +static_library("sle_uart_server") { + sources = [ + "sle_uart_server_adv.c", + "sle_uart_server.c", + ] + + defines = [ + "CONFIG_UART_SUPPORT_TX", + "CONFIG_UART_SUPPORT_RX", + ] + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//device/soc/hisilicon/ws63v100/sdkv100/include/middleware/services/bts/sle", + "//foundation/communication/sle", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/debug", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/memory", + "//device/soc/hisilicon/ws63v100/sdkv100/kernel/osal/include/schedule", + "//device/soc/hisilicon/ws63v100/sdkv100/include/driver", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/rom/drivers/chips/ws63/porting/pinctrl", + "//base/iothardware/peripheral/interfaces/inner_api", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/drivers/hal/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/drivers/chips/ws63/porting/uart", + "//device/soc/hisilicon/ws63v100/sdkv100/middleware/utils/common_headers/native", + ] +} diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/README.md b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e1148158f7709e6085ecf65d533e76679f5862e1 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/README.md @@ -0,0 +1,72 @@ +# 客户端和服务端使用星闪功能转发串口数据 + +### Device Discovery OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | --------------------------------------- | +| `EnableSle` | 开启SLE | +| `SleSetLocalAddr` | 设置本地设备地址 | +| `SleSetAnnounceData` | 设置设备公开数据 | +| `SleSetAnnounceParam` | 设置设备公开参数 | +| `SleStartAnnounce` | 开始广播 | +| `SleSetSeekParam` | 设置扫描参数 | +| `SleStartSeek` | 开始扫描 | +| `SleStopSeek` | 停止扫描 | +| `SleAnnounceSeekRegisterCallbacks` | 注册设备公开和设备发现回调函数 | + +### Connection Manager OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -----------------------| +| `SleConnectRemoteDevice` | 向对端设备发起连接请求 | +| `SleConnectionRegisterCallbacks` | 注册连接管理回调函数 | + +### SSAP server OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `ssapsRegisterServer` | 注册 SSAP server | +| `SsapsAddDescriptorSync` | 添加特征描述符同步接口 | +| `SsapsAddPropertySync` | 添加特征同步接口 | +| `SsapsAddServiceSync` | 添加服务同步接口 | +| `SsapsStartService` | 启动服务 | +| `SsapsNotifyIndicate` | 给对端发送通知或指示 | +| `SsapsRegisterCallbacks` | 注册服务端回调函数 | + +### SSAP client OH接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `SsapcRegisterClient` | 注册 SSAP client | +| `SsapWriteReq` | 发起写请求 | + +### SSAP client 原生SDK接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `ssapc_register_callbacks` | 注册客户端回调函数 | + + +### UART 原生SDK接口 + +| API | 功能说明 | +| ------------------------------------------------------------ | -------------------- | +| `uapi_uart_register_rx_callback` | 注册串口接收回调函数 | + + + + + +## 如何编译 + +本项目下有两个示例代码,需要运行在两块WS63开发板上,一块作为服务端,一块作为客户端,SLE服务端需要注释掉`"sle_uart_client.c"`行,放开`"sle_uart_server_adv.c","sle_uart_server.c"`行,SLE客户端需要注释掉`"sle_uart_server_adv.c","sle_uart_server.c"`行,放开`"sle_uart_client.c"`行,之前生成的libsle_uart_demo.a静态库文件需要删除; + +1. 将sle_uart_demo文件夹克隆到本地openharmony源码的applications\sample\wifi-iot\app目录下; +2. 修改openharmony的`applications\sample\wifi-iot\app\BUILD.gn`文件: + * 将其中的 features 改为: + features = [ + "sle_uart_demo:sle_uart_demo", + ] +3. 在device/soc/hisilicon/ws63v100/sdkv100/build/config/target_config/ws63/config.py文件的`'ram_component': []`中添加`"sle_uart_demo"`,在device/soc/hisilicon/ws63v100/sdkv100/libs_url/ws63cmake/ohos.cmake文件的`set(COMPONENT_LIST)`中添加`"sle_uart_demo"`; +4. 执行编译命令:`hb build -f` +5. 运行结果:两块ws63开发板可以通过星闪功能转发串口数据,波特率默认为115200,服务端在接收到客户端发送的数据`"123"`后会打印`"client_send_data: 123"`; \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.c b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.c new file mode 100644 index 0000000000000000000000000000000000000000..2fa9e1bc28da40df02183456d55943de1e9c6e50 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.c @@ -0,0 +1,491 @@ +/** +# Copyright (C) 2024 HiHope Open Source Organization . +# 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 "securec.h" +#include "sle_common.h" +#include "osal_debug.h" +#include "sle_errcode.h" +#include "osal_addr.h" +#include "osal_task.h" +#include "sle_connection_manager.h" +#include "sle_device_discovery.h" +#include "sle_uart_server_adv.h" +#include "sle_uart_server.h" +#include "cmsis_os2.h" +#include "ohos_init.h" +#include "ohos_sle_common.h" +#include "ohos_sle_errcode.h" +#include "ohos_sle_ssap_server.h" +#include "ohos_sle_ssap_client.h" +#include "ohos_sle_device_discovery.h" +#include "ohos_sle_connection_manager.h" +#include "iot_uart.h" +#include "pinctrl.h" +#include "uart.h" +#include "errcode.h" + +#define OCTET_BIT_LEN 8 +#define UUID_LEN_2 2 +#define UUID_INDEX 14 +#define BT_INDEX_4 4 +#define BT_INDEX_0 0 +#define UART_BUFF_LENGTH 0x100 + +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* sle server app uuid for test */ +static char g_sle_uuid_app_uuid[UUID_LEN_2] = { 0x12, 0x34 }; +/* server notify property uuid for test */ +static char g_sle_property_value[OCTET_BIT_LEN] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; +/* sle connect acb handle */ +static uint16_t g_sle_conn_hdl = 0; +/* sle server handle */ +static uint8_t g_server_id = 0; +/* sle service handle */ +static uint16_t g_service_handle = 0; +/* sle ntf property handle */ +static uint16_t g_property_handle = 0; +/* sle pair acb handle */ +uint16_t g_sle_pair_hdl; + +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +//#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define sample_at_log_print(fmt, args...) printf("[%d]"fmt,__LINE__,##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" +#define SLE_SERVER_INIT_DELAY_MS 1000 +static sle_uart_server_msg_queue g_sle_uart_server_msg_queue = NULL; +static uint8_t g_sle_uart_base[] = { 0x37, 0xBE, 0xA8, 0x80, 0xFC, 0x70, 0x11, 0xEA, \ + 0xB7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +static uint8_t g_at_pre_char = 0; +static uint32_t g_at_uart_recv_cnt = 0; + +static void server_uart_rx_callback(const void *buffer, uint16_t length, bool error) +{ + errcode_t ret; + const uint8_t *data = (const uint8_t *)buffer; + if (error) { + osal_printk("*******uart error*******\r\n"); + } + + g_at_uart_recv_cnt += length; + if (((char *)buffer)[0] == 0xD) { + uapi_uart_write(0, (uint8_t *)"\r\n", (uint16_t)strlen("\r\n"), 0); + } else { + uapi_uart_write(0, (const uint8_t *)buffer, (uint32_t)length, 0); + } + ret = uapi_at_channel_data_recv(0, (uint8_t *)buffer, (uint32_t)length); + if (ret != 0) { + /* 前一个字符为'\r'时单独一个'\n'导致的CHANNEL_BUSY不打印 */ + if (g_at_pre_char != '\r' || length != 1 || data[0] != '\n' || ret != ERRCODE_AT_CHANNEL_BUSY) { + osal_printk("\r\nat_uart_rx_callback fail:0x%x\r\n", ret); + } + } + g_at_pre_char = data[length - 1]; + if(length > 0){ + ret = uart_sle_send_data((uint8_t *)buffer,(uint8_t)length); + if(ret != 0){ + osal_printk("\r\nsle_server_send_data_fail:%d\r\n", ret); + } +} + +} + +static void encode2byte_little(uint8_t *_ptr, uint16_t data) +{ + *(uint8_t *)((_ptr) + 1) = (uint8_t)((data) >> 0x8); + *(uint8_t *)(_ptr) = (uint8_t)(data); +} + +static void sle_uuid_set_base(SleUuid *out) +{ + errcode_t ret; + ret = memcpy_s(out->uuid, SLE_UUID_LEN, g_sle_uart_base, SLE_UUID_LEN); + if (ret != EOK) { + sample_at_log_print("%s sle_uuid_set_base memcpy fail\n", SLE_UART_SERVER_LOG); + out->len = 0; + return ; + } + out->len = UUID_LEN_2; +} + +static void sle_uuid_setu2(uint16_t u2, SleUuid *out) +{ + sle_uuid_set_base(out); + out->len = UUID_LEN_2; + encode2byte_little(&out->uuid[UUID_INDEX], u2); +} +static void sle_uart_uuid_print(SleUuid *uuid) +{ + if (uuid == NULL) { + sample_at_log_print("%s uuid_print,uuid is null\r\n", SLE_UART_SERVER_LOG); + return; + } + if (uuid->len == UUID_16BIT_LEN) { + sample_at_log_print("%s uuid: %02x %02x.\n", SLE_UART_SERVER_LOG, + uuid->uuid[14], uuid->uuid[15]); /* 14 15: uuid index */ + } else if (uuid->len == UUID_128BIT_LEN) { + sample_at_log_print("%s uuid: \n", SLE_UART_SERVER_LOG); /* 14 15: uuid index */ + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[0], uuid->uuid[1], + uuid->uuid[2], uuid->uuid[3]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[4], uuid->uuid[5], + uuid->uuid[6], uuid->uuid[7]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[8], uuid->uuid[9], + uuid->uuid[10], uuid->uuid[11]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[12], uuid->uuid[13], + uuid->uuid[14], uuid->uuid[15]); + } +} + +static void ssaps_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id, SsapcExchangeInfo *mtu_size, + errcode_t status) +{ + sample_at_log_print("%s ssaps ssaps_mtu_changed_cbk callback server_id:%x, conn_id:%x, mtu_size:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, mtu_size->mtuSize, status); + if (g_sle_pair_hdl == 0) { + g_sle_pair_hdl = conn_id + 1; + } +} + +static void ssaps_start_service_cbk(uint8_t server_id, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s start service cbk callback server_id:%d, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); +} +static void ssaps_add_service_cbk(uint8_t server_id, SleUuid *uuid, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add service cbk callback server_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_property_cbk(uint8_t server_id, SleUuid *uuid, uint16_t service_handle, + uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add property cbk callback server_id:%x, service_handle:%x,handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, service_handle, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_descriptor_cbk(uint8_t server_id, SleUuid *uuid, uint16_t service_handle, + uint16_t property_handle, errcode_t status) +{ + sample_at_log_print("%s add descriptor cbk callback server_id:%x, service_handle:%x, property_handle:%x, \ + status:%x\r\n", SLE_UART_SERVER_LOG, server_id, service_handle, property_handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_delete_all_service_cbk(uint8_t server_id, errcode_t status) +{ + sample_at_log_print("%s delete all service callback server_id:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, status); +} +static errcode_t sle_ssaps_register_cbks(SsapsReadRequestCallback ssaps_read_callback, SsapsWriteRequestCallback + ssaps_write_callback) +{ + errcode_t ret; + SsapsCallbacks ssaps_cbk = {0}; + ssaps_cbk.addServiceCb = ssaps_add_service_cbk; + ssaps_cbk.addPropertyCb = ssaps_add_property_cbk; + ssaps_cbk.addDescriptorCb = ssaps_add_descriptor_cbk; + ssaps_cbk.startServiceCb = ssaps_start_service_cbk; + ssaps_cbk.deleteAllServiceCb = ssaps_delete_all_service_cbk; + ssaps_cbk.mtuChangedCb = ssaps_mtu_changed_cbk; + ssaps_cbk.readRequestCb = ssaps_read_callback; + ssaps_cbk.writeRequestCb = ssaps_write_callback; + ret = SsapsRegisterCallbacks(&ssaps_cbk); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_ssaps_register_cbks,ssaps_register_callbacks fail :%x\r\n", SLE_UART_SERVER_LOG, + ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_service_add(void) +{ + errcode_t ret; + SleUuid service_uuid = {0}; + sle_uuid_setu2(SLE_UUID_SERVER_SERVICE, &service_uuid); + ret = SsapsAddServiceSync(g_server_id, &service_uuid, 1, &g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uuid add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_property_add(void) +{ + errcode_t ret; + SsapsPropertyInfo property = {0}; + SsapsDescInfo descriptor = {0}; + uint8_t ntf_value[] = { 0x01, 0x02 }; + + property.permissions = SLE_UUID_TEST_PROPERTIES; + property.operateIndication = SLE_UUID_TEST_OPERATION_INDICATION; + sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &property.uuid); + property.valueLen = OCTET_BIT_LEN; + property.value = (uint8_t *)osal_vmalloc(sizeof(g_sle_property_value)); + + if (property.value == NULL) { + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(property.value, sizeof(g_sle_property_value), g_sle_property_value, + sizeof(g_sle_property_value)) != EOK) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + ret = SsapsAddPropertySync(g_server_id, g_service_handle, &property, &g_property_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add property fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + descriptor.permissions = SLE_UUID_TEST_DESCRIPTOR; + descriptor.type = SSAP_DESCRIPTOR_CLIENT_CONFIGURATION; + descriptor.operateIndication = SLE_UUID_TEST_OPERATION_INDICATION; + descriptor.value = (uint8_t *)osal_vmalloc(sizeof(ntf_value)); + if (descriptor.value == NULL) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(descriptor.value, sizeof(ntf_value), ntf_value, sizeof(ntf_value)) != EOK) { + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + ret = SsapsAddDescriptorSync(g_server_id, g_service_handle, g_property_handle, &descriptor); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add descriptor fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uart_server_add(void) +{ + errcode_t ret; + sle_uuid_t app_uuid = {0}; + + sample_at_log_print("%s sle uart add service in\r\n", SLE_UART_SERVER_LOG); + app_uuid.len = sizeof(g_sle_uuid_app_uuid); + if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) { + return ERRCODE_SLE_FAIL; + } + ssapsRegisterServer(&app_uuid, &g_server_id); + + if (sle_uuid_server_service_add() != ERRCODE_SLE_SUCCESS) { + SsapsUnregisterServer(g_server_id); + return ERRCODE_SLE_FAIL; + } + if (sle_uuid_server_property_add() != ERRCODE_SLE_SUCCESS) { + SsapsUnregisterServer(g_server_id); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service, server_id:%x, service_handle:%x, property_handle:%x\r\n", + SLE_UART_SERVER_LOG, g_server_id, g_service_handle, g_property_handle); + ret = SsapsStartService(g_server_id, g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service out\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + +/* device通过handle向host发送数据:report */ +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint8_t len) +{ + SsapsNtfInd param = {0}; + uint8_t receive_buf[UART_BUFF_LENGTH] = { 0 }; /* max receive length. */ + param.handle = g_property_handle; + param.type = SSAP_PROPERTY_TYPE_VALUE; + param.value = receive_buf; + param.valueLen = len+1; + if (memcpy_s(param.value, param.valueLen, data, len) != EOK) { + return ERRCODE_SLE_FAIL; + } + return SsapsNotifyIndicate(g_server_id, g_sle_conn_hdl, ¶m); +} + +static void sle_connect_state_changed_cbk(uint16_t conn_id, const SleAddr *addr, + SleAcbStateType conn_state, SlePairStateType pair_state, SleDiscReasonType disc_reason) +{ + uint8_t sle_connect_state[] = "sle_dis_connect"; + sample_at_log_print("%s connect state changed callback conn_id:0x%02x, conn_state:0x%x, pair_state:0x%x, \ + disc_reason:0x%x\r\n", SLE_UART_SERVER_LOG,conn_id, conn_state, pair_state, disc_reason); + sample_at_log_print("%s connect state changed callback addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + if (conn_state == OH_SLE_ACB_STATE_CONNECTED) { + g_sle_conn_hdl = conn_id; + (void)uapi_uart_register_rx_callback(0, UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, + 16, server_uart_rx_callback); + } else if (conn_state == OH_SLE_ACB_STATE_DISCONNECTED) { + g_sle_conn_hdl = 0; + g_sle_pair_hdl = 0; + if (g_sle_uart_server_msg_queue != NULL) { + g_sle_uart_server_msg_queue(sle_connect_state, sizeof(sle_connect_state)); + } + } +} + +static void sle_pair_complete_cbk(uint16_t conn_id, const SleAddr *addr, errcode_t status) +{ + sample_at_log_print("%s pair complete conn_id:%02x, status:%x\r\n", SLE_UART_SERVER_LOG, + conn_id, status); + sample_at_log_print("%s pair complete addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + g_sle_pair_hdl = conn_id + 1; + +} + +void sle_uart_server_msg_precess(uint8_t *buffer_addr, uint16_t buffer_size) +{ + if((buffer_addr == NULL) || (buffer_size == 0)) { + printf("sle_uart_server_msg_precess buffer_addr is NULL or buffer_size is 0\r\n"); + return; + } + + if (strncmp(buffer_addr, "sle_dis_connect", buffer_size) == 0) { + sle_uart_server_adv_restart(); + } + +} + +static errcode_t sle_conn_register_cbks(void) +{ + errcode_t ret; + SleConnectionCallbacks conn_cbks = {0}; + conn_cbks.connectStateChangedCb = sle_connect_state_changed_cbk; + conn_cbks.pairCompleteCb = sle_pair_complete_cbk; + sle_uart_server_register_msg(sle_uart_server_msg_precess); + ret = SleConnectionRegisterCallbacks(&conn_cbks); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_conn_register_cbks,sle_connection_register_callbacks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + + + +void ssaps_read_request_callbacks(uint8_t server_id, uint16_t conn_id, ssaps_req_read_cb_t *read_cb_para, + errcode_t status){ + (void)server_id; + (void)conn_id; + (void)read_cb_para; + (void)status; + + } + +void ssaps_write_request_callbacks(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para, + errcode_t status){ + (void)server_id; + (void)conn_id; + (void)status; + write_cb_para->value[write_cb_para->length-1] = '\0'; + printf("client_send_data: %s\r\n",write_cb_para->value); + + } + + +/* 初始化uuid server */ +errcode_t sle_uart_server_init() +{ + errcode_t ret; + ret = sle_uart_announce_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_announce_register_cbks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_conn_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_conn_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_ssaps_register_cbks(ssaps_read_request_callbacks, ssaps_write_request_callbacks); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_ssaps_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = EnableSle(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,enable_sle fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + sample_at_log_print("%s init ok\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + +errcode_t sle_enable_server_cbk(void) +{ + errcode_t ret; + ret = sle_uart_server_add(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_add fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_uart_server_adv_init(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_adv_init fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + + + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg) +{ + g_sle_uart_server_msg_queue = sle_uart_server_msg; +} + + +int uart_sle_send_data(uint8_t *data,uint8_t length){ + int ret; + ret = sle_uart_server_send_report_by_handle(data,length); + return ret ; +} + + +static void SleTask( char* arg) +{ + (void)arg; + sle_uart_server_init(); + return NULL; +} + +static void SleServerExample(void) +{ + osThreadAttr_t attr; + + attr.name = "SleTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 2048; + attr.priority = 25; + + if (osThreadNew(SleTask, NULL, &attr) == NULL) { + printf("[SleExample] Falied to create SleTask!\n"); + }else printf("[SleExample] create SleTask successfully !\n"); +} + +SYS_RUN(SleServerExample); // if test add it \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.h b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.h new file mode 100644 index 0000000000000000000000000000000000000000..3ce4c53167cace1d29f59e50b58cc278e2af96bd --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server.h @@ -0,0 +1,66 @@ +/** +# Copyright (C) 2024 HiHope Open Source Organization . +# 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 SLE_UART_SERVER_H +#define SLE_UART_SERVER_H + +#include +#include "sle_ssap_server.h" +#include "errcode.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* Service UUID */ +#define SLE_UUID_SERVER_SERVICE 0x2222 + +/* Property UUID */ +#define SLE_UUID_SERVER_NTF_REPORT 0x2323 + +/* Property Property */ +#define SLE_UUID_TEST_PROPERTIES (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +/* Operation indication */ +#define SLE_UUID_TEST_OPERATION_INDICATION (SSAP_OPERATE_INDICATION_BIT_READ | SSAP_OPERATE_INDICATION_BIT_WRITE) + +/* Descriptor Property */ +#define SLE_UUID_TEST_DESCRIPTOR (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +errcode_t sle_uart_server_init(void); + +errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len); + +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint8_t len); + +uint16_t sle_uart_client_is_connected(void); + +typedef void (*sle_uart_server_msg_queue)(uint8_t *buffer_addr, uint16_t buffer_size); + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg); + +errcode_t sle_enable_server_cbk(void); + +int uart_sle_send_data(uint8_t *data,uint8_t length); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server_adv.c b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server_adv.c new file mode 100644 index 0000000000000000000000000000000000000000..1bde1af53a3df26aa10a247ddbce5dbe3855956a --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/rsu/sle_uart_server/sle_uart_server_adv.c @@ -0,0 +1,281 @@ +/** +# Copyright (C) 2024 HiHope Open Source Organization . +# 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 "securec.h" +#include "errcode.h" +#include "osal_addr.h" +#include "sle_common.h" +#include "sle_uart_server.h" +#include "sle_device_discovery.h" +#include "sle_errcode.h" +#include "osal_debug.h" +#include "osal_task.h" +#include "string.h" +#include "sle_uart_server_adv.h" + + #include "ohos_sle_common.h" +#include "ohos_sle_errcode.h" + #include "ohos_sle_ssap_server.h" +#include "ohos_sle_ssap_client.h" +#include "ohos_sle_device_discovery.h" +#include "ohos_sle_connection_manager.h" + +/* sle device name */ +#define NAME_MAX_LENGTH 16 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MIN_DEFAULT 0x64 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MAX_DEFAULT 0x64 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MIN_DEFAULT 0xC8 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MAX_DEFAULT 0xC8 +/* 超时时间5000ms,单位10ms */ +#define SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT 0x1F4 +/* 超时时间4990ms,单位10ms */ +#define SLE_CONN_MAX_LATENCY 0x1F3 +/* 广播发送功率 */ +#define SLE_ADV_TX_POWER 10 +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* 最大广播数据长度 */ +#define SLE_ADV_DATA_LEN_MAX 251 +/* 广播名称 */ +static uint8_t sle_local_name[NAME_MAX_LENGTH] = "rsu_sle_server"; +#define SLE_SERVER_INIT_DELAY_MS 1000 +//#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define sample_at_log_print(fmt, args...) printf("[%d]"fmt,__LINE__,##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" + +static uint16_t sle_set_adv_local_name(uint8_t *adv_data, uint16_t max_len) +{ + errno_t ret; + uint8_t index = 0; + + uint8_t *local_name = sle_local_name; + uint8_t local_name_len = sizeof(sle_local_name) - 1; + sample_at_log_print("%s local_name_len = %d\r\n", SLE_UART_SERVER_LOG, local_name_len); + sample_at_log_print("%s local_name: ", SLE_UART_SERVER_LOG); + for (uint8_t i = 0; i < local_name_len; i++) { + sample_at_log_print("0x%02x ", local_name[i]); + } + sample_at_log_print("\r\n"); + adv_data[index++] = local_name_len + 1; + adv_data[index++] = SLE_ADV_DATA_TYPE_COMPLETE_LOCAL_NAME; + ret = memcpy_s(&adv_data[index], max_len - index, local_name, local_name_len); + if (ret != EOK) { + sample_at_log_print("%s memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + return (uint16_t)index + local_name_len; +} + +static uint16_t sle_set_adv_data(uint8_t *adv_data) +{ + size_t len = 0; + uint16_t idx = 0; + errno_t ret = 0; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_disc_level = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_DISCOVERY_LEVEL, + .value = SLE_ANNOUNCE_LEVEL_NORMAL, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_disc_level, len); + if (ret != EOK) { + sample_at_log_print("%s adv_disc_level memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_access_mode = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_ACCESS_MODE, + .value = 0, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_access_mode, len); + if (ret != EOK) { + sample_at_log_print("%s adv_access_mode memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + return idx; +} + +static uint16_t sle_set_scan_response_data(uint8_t *scan_rsp_data) +{ + uint16_t idx = 0; + errno_t ret; + size_t scan_rsp_data_len = sizeof(struct sle_adv_common_value); + + struct sle_adv_common_value tx_power_level = { + .length = scan_rsp_data_len - 1, + .type = SLE_ADV_DATA_TYPE_TX_POWER_LEVEL, + .value = SLE_ADV_TX_POWER, + }; + ret = memcpy_s(scan_rsp_data, SLE_ADV_DATA_LEN_MAX, &tx_power_level, scan_rsp_data_len); + if (ret != EOK) { + sample_at_log_print("%s sle scan response data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += scan_rsp_data_len; + + /* set local name */ + idx += sle_set_adv_local_name(&scan_rsp_data[idx], SLE_ADV_DATA_LEN_MAX - idx); + return idx; +} + +static int sle_set_default_announce_param(void) +{ + errno_t ret; + SleAnnounceParam param = {0}; + + uint8_t index; + unsigned char local_addr[SLE_ADDR_LEN] = { 0x78, 0x70, 0x60, 0x88, 0x96, 0x45 }; + param.announceMode = SLE_ANNOUNCE_MODE_CONNECTABLE_SCANABLE; + param.announceHandle = SLE_ADV_HANDLE_DEFAULT; + param.announceGtRole = SLE_ANNOUNCE_ROLE_T_CAN_NEGO; + param.announceLevel = SLE_ANNOUNCE_LEVEL_NORMAL; + param.announceChannelMap = SLE_ADV_CHANNEL_MAP_DEFAULT; + param.announceIntervalMin = SLE_ADV_INTERVAL_MIN_DEFAULT; + param.announceIntervalMax = SLE_ADV_INTERVAL_MAX_DEFAULT; + param.connIntervalMin = SLE_CONN_INTV_MIN_DEFAULT; + param.connIntervalMax = SLE_CONN_INTV_MAX_DEFAULT; + param.connMaxLatency = SLE_CONN_MAX_LATENCY; + param.connSupervisionTimeout = SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT; + param.ownAddr.type = 0; + ret = memcpy_s(param.ownAddr.addr, SLE_ADDR_LEN, local_addr, SLE_ADDR_LEN); + + if (ret != EOK) { + sample_at_log_print("%s sle_set_default_announce_param data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + sample_at_log_print("%s sle_uart_local addr: ", SLE_UART_SERVER_LOG); + for (index = 0; index < SLE_ADDR_LEN; index++) { + sample_at_log_print("0x%02x ", param.ownAddr.addr[index]); + } + sample_at_log_print("\r\n"); + return SleSetAnnounceParam(param.announceHandle, ¶m); +} + +static int sle_set_default_announce_data(void) +{ + errcode_t ret; + uint8_t announce_data_len = 0; + uint8_t seek_data_len = 0; + SleAnnounceData data = {0}; + uint8_t adv_handle = SLE_ADV_HANDLE_DEFAULT; + uint8_t announce_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t seek_rsp_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t data_index = 0; + + announce_data_len = sle_set_adv_data(announce_data); + data.announceData = announce_data; + data.announceDataLen = announce_data_len; + + sample_at_log_print("%s data.announce_data_len = %d\r\n", SLE_UART_SERVER_LOG, data.announceDataLen); + sample_at_log_print("%s data.announce_data: ", SLE_UART_SERVER_LOG); + for (data_index = 0; data_index> (AHT20_STATUS_BUSY_SHIFT)); +} + +uint8_t aht20_status_mode(uint8_t status) +{ + return ((status & AHT20_STATUS_MODE_MASK) >> (AHT20_STATUS_MODE_SHIFT)); +} + +uint8_t aht20_status_cali(uint8_t status) +{ + return ((status & AHT20_STATUS_CALI_MASK) >> (AHT20_STATUS_CALI_SHIFT)); +} + +static uint32_t AHT20_Read(uint8_t *buffer, uint32_t buffLen) +{ + uint16_t dev_addr = AHT20_DEVICE_ADDR; + i2c_data_t data = {0}; + data.receive_buf = buffer; + data.receive_len = buffLen; + uint32_t retval = uapi_i2c_master_read(CONFIG_I2C_MASTER_BUS_ID, dev_addr, &data); + if (retval != 0) { + printf("I2cRead() failed, %0X!\n", retval); + return retval; + } + return 0; +} + +static uint32_t AHT20_Write(uint8_t *buffer, uint32_t buffLen) +{ + uint16_t dev_addr = AHT20_DEVICE_ADDR; + i2c_data_t data = {0}; + data.send_buf = buffer; + data.send_len = buffLen; + uint32_t retval = uapi_i2c_master_write(CONFIG_I2C_MASTER_BUS_ID, dev_addr, &data); + if (retval != 0) { + printf("I2cWrite(%02X) failed, %0X!\n", buffer[0], retval); + return retval; + } + return 0; +} + +// 发送获取状态命令 +static uint32_t AHT20_StatusCommand(void) +{ + uint8_t statusCmd[] = {AHT20_CMD_STATUS}; + return AHT20_Write(statusCmd, sizeof(statusCmd)); +} + +// 发送软复位命令 +static uint32_t AHT20_ResetCommand(void) +{ + uint8_t resetCmd[] = {AHT20_CMD_RESET}; + return AHT20_Write(resetCmd, sizeof(resetCmd)); +} + +// 发送初始化校准命令 +static uint32_t AHT20_CalibrateCommand(void) +{ + uint8_t clibrateCmd[] = {AHT20_CMD_CALIBRATION, AHT20_CMD_CALIBRATION_ARG0, AHT20_CMD_CALIBRATION_ARG1}; + return AHT20_Write(clibrateCmd, sizeof(clibrateCmd)); +} + +// 读取温湿度值之前, 首先要看状态字的校准使能位Bit[3]是否为 1(通过发送0x71可以获取一个字节的状态字), +// 如果不为1,要发送0xBE命令(初始化),此命令参数有两个字节, 第一个字节为0x08,第二个字节为0x00。 +uint32_t AHT20_Calibrate(void) +{ + uint32_t retval = 0; + uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = {AHT20_CMD_STATUS}; + memset_s(&buffer, sizeof(buffer), 0x0, sizeof(buffer)); + retval = AHT20_StatusCommand(); + if (retval != 0) { + return retval; + } + + retval = AHT20_Read(buffer, sizeof(buffer)); + if (retval != 0) { + return retval; + } + + if (aht20_status_busy(buffer[0]) || !aht20_status_cali(buffer[0])) { + retval = AHT20_ResetCommand(); + if (retval != 0) { + return retval; + } + osDelay(AHT20_STARTUP_TIME); + retval = AHT20_CalibrateCommand(); + osDelay(AHT20_CALIBRATION_TIME); + return retval; + } + return 0; +} + +// 发送 触发测量 命令,开始测量 +uint32_t AHT20_StartMeasure(void) +{ + uint8_t triggerCmd[] = {AHT20_CMD_TRIGGER, AHT20_CMD_TRIGGER_ARG0, AHT20_CMD_TRIGGER_ARG1}; + return AHT20_Write(triggerCmd, sizeof(triggerCmd)); +} + +// 接收测量结果,拼接转换为标准值 +uint32_t AHT20_GetMeasureResult(float *temp, float *humi) +{ + uint32_t retval = 0, i = 0; + if (temp == NULL || humi == NULL) { + return 0; + } + + uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = {0}; + memset_s(&buffer, sizeof(buffer), 0x0, sizeof(buffer)); + retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result + if (retval != 0) { + return retval; + } + + for (i = 0; aht20_status_busy(buffer[0]) && i < AHT20_MAX_RETRY; i++) { + osDelay(10); + retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result + if (retval != 0) { + return retval; + } + } + if (i >= AHT20_MAX_RETRY) { + printf("AHT20 device always busy!\r\n"); + return 0; + } + + uint32_t humiRaw = buffer[1]; + humiRaw = (humiRaw << 8) | buffer[2]; // 左移8位或buff[2]得到数据,具体可以看芯片手册 + humiRaw = (humiRaw << 4) | ((buffer[3] & 0xF0) >> 4); // 左移4位或buff[3]得到数据,具体可以看芯片手册 + *humi = humiRaw / (float)AHT20_RESLUTION * 100; // 100量程 + + uint32_t tempRaw = buffer[3] & 0x0F; + tempRaw = (tempRaw << 8) | buffer[4]; /* 左移8位或buff[4]得到数据,具体可以看芯片手册 */ + tempRaw = (tempRaw << 8) | buffer[5]; // 左移8位或buff[5]得到数据,具体可以看芯片手册 + *temp = tempRaw / (float)AHT20_RESLUTION * 200 - 50; /* 200 50量程 */ + return 0; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/environment/aht20.h b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/environment/aht20.h new file mode 100644 index 0000000000000000000000000000000000000000..6b4d163693cd213132a9c548650a9662d0b7a7da --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/environment/aht20.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 HiSilicon Technologies CO., 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 AHT20_H +#define AHT20_H + +#include + +uint32_t AHT20_Calibrate(void); + +uint32_t AHT20_StartMeasure(void); + +uint32_t AHT20_GetMeasureResult(float *temp, float *humi); + +#endif // AHT20_H \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car.h b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car.h new file mode 100644 index 0000000000000000000000000000000000000000..0dd24f3e0e1f9790226fd35c974ecee4e3869846 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car.h @@ -0,0 +1,47 @@ + + +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: Aircondition Sample Source. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ + +#ifndef _SLE_CAR_H_ +#define _SLE_CAR_H_ + +#include "sle_common.h" +#define MSG_LEN_MAX 32 + +// 车载设备控制指令枚举 +enum MSG_TYPE{ + NONE_TYPE = 0x58, //88 + DOOR_CHANGE = 0x59, + TRUNK_CHANGE = 0x5A, + AIR_CONDITION_CHANGE = 0x5B, + LIGHT_COLOR_CHANGE = 0x5C, + ENVIROMENT_CHANGE = 0x5D, + OBU_ONLINE = 0x5E, + OBU_OFFLINE = 0x5F +}; + +// 中控数据结构体 +typedef struct { + enum MSG_TYPE type; + uint8_t data[4]; +}sle_data_t; + +extern sle_data_t g_sle_data; + +extern unsigned char g_receive_msg[MSG_LEN_MAX]; +extern int g_aircondition_value; +extern unsigned char g_enviroment_temprature[MSG_LEN_MAX]; +extern int g_ambient_light_britness; + +extern unsigned char g_local_addr[SLE_ADDR_LEN]; // 设备地址,不同设备需要区分 + +void Aht20TestTask(void); + +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_aircondition.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_aircondition.c new file mode 100644 index 0000000000000000000000000000000000000000..765eb2e7908d76c45ea9e0ec24f4054f674d1b9f --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_aircondition.c @@ -0,0 +1,191 @@ + +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: Aircondition Sample Source. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ +#include "boards.h" +#include "pinctrl.h" +#include "gpio.h" +#include "soc_osal.h" +#include "app_init.h" +#include "sle_common.h" +#include "sle_car.h" +unsigned char g_local_addr[SLE_ADDR_LEN] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }; + + +#define AIRCONDITION_DURATION_MS 100 + +#define AIRCONDITION_TASK_PRIO 24 +#define AIRCONDITION_TASK_STACK_SIZE 0x1000 + +sle_data_t g_sle_data={ + .type = NONE_TYPE, + .data = {0,0,0,0} +}; + + +#include +#include +#include + +#include "pinctrl.h" +#include "pwm.h" +#include "tcxo.h" +#include "soc_osal.h" +#include "app_init.h" + +#define PWM_CHANNEL 1 +#define PWM_GROUP_ID 1 +#define TEST_TCXO_DELAY_1000MS 1000 + +#define PWM_TASK_PRIO 24 +#define PWM_TASK_STACK_SIZE 0x1000 +#define CONFIG_PWM_PIN 9 +#define CONFIG_PWM_PIN_MODE 1 + +static errcode_t pwm_sample_callback(uint8_t channel) +{ + osal_printk("PWM %d, cycle done. \r\n", channel); + return ERRCODE_SUCC; +} + + +static void *pwm_task(const char *arg) +{ + UNUSED(arg); + + pwm_config_t cfg_no_repeat = {2000, // 低电平时间 + g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000) + 0, // 相位偏移位 + 0xFF, // 发多少个波形 + true}; // 是否循环 + + uapi_pin_set_mode(CONFIG_PWM_PIN, CONFIG_PWM_PIN_MODE); + uapi_pwm_init(); + uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); + uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); + uapi_pwm_unregister_interrupt(PWM_GROUP_ID); + uapi_pwm_register_interrupt(PWM_GROUP_ID, pwm_sample_callback); + uint8_t channel_id = PWM_CHANNEL; + uapi_pwm_set_group(PWM_GROUP_ID, &channel_id, 1); // 每个通道对应一个bit位 + uapi_pwm_start(PWM_GROUP_ID); + // uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); + // uapi_pwm_close(PWM_CHANNEL); // 波形发送完毕停止 + // uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); + // uapi_pwm_deinit(); + while(1) + { + pwm_config_t cfg_no_repeat = {500, // 低电平时间 + g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000) + 0, // 相位偏移位 + 0xFF, // 发多少个波形 + true}; // 是否循环 + uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); + uapi_pwm_start(PWM_GROUP_ID); + printf("pwm_task g_ambient_light_britness: %d\r\n", g_ambient_light_britness); + osal_msleep(50); + } + return NULL; +} + +unsigned long hex_string_to_number(const char *hex_str) { + unsigned long result = 0; + int len = strlen(hex_str); + + for (int i = 0; i < len; i++) { + // 将16进制字符转换为对应的数值 + result = result * 16 + (unsigned char)strtol(&hex_str[i], NULL, 16); + } + + return result; +} + +int g_aircondition_value = 0; +int g_ambient_light_britness = 0; +int msg_proc_main(char *hex_str) { + // const char *hex_str = "1A3F"; + printf("Hex String: %s\r\n", hex_str); + if(hex_str[0] == '5' && hex_str[1] == 'b') // AIR_CONDITION_CHANGE + { + char value_str[3] = {0,0}; + sprintf(value_str,"%c%c",hex_str[2],hex_str[3]); + printf("Hex value_str: %s\r\n", value_str); + g_aircondition_value = hex_string_to_number(value_str); + printf("g_aircondition_value: %lu\r\n", g_aircondition_value); + } + else if(hex_str[0] == '5' && hex_str[1] == 'c') // LIGHT_COLOR_CHANGE + { + char value_str[3] = {0,0}; + sprintf(value_str,"%c%c",hex_str[6],hex_str[7]); + printf("Hex value_str: %s\r\n", value_str); + g_ambient_light_britness = hex_string_to_number(value_str); + printf("g_ambient_light_britness: %lu\r\n", g_ambient_light_britness); + } + return 0; +} + +static int aircondition_task(const char *arg) +{ + unused(arg); + + uapi_pin_set_mode(GPIO_01, HAL_PIO_FUNC_GPIO); + + uapi_gpio_set_dir(GPIO_01, GPIO_DIRECTION_OUTPUT); + uapi_gpio_set_val(GPIO_01, GPIO_LEVEL_LOW); + + // uapi_gpio_toggle(GPIO_01); + osal_printk("aircondition working.\r\n"); + + // 0-100 ... 2-50ms + while (1) { + if(g_aircondition_value <= 0 || g_aircondition_value > 2000) + { + uapi_gpio_set_val(GPIO_01, GPIO_LEVEL_LOW); + osal_msleep(1000); // 1000ms 低电平 + }else{ + uapi_gpio_set_val(GPIO_01, GPIO_LEVEL_LOW); + osal_msleep(200); // 200ms 低电平 + uapi_gpio_set_val(GPIO_01, GPIO_LEVEL_HIGH); + // 值越大,IO输出切换时间越长 + osal_msleep(g_aircondition_value); + } + // pwm_task(); + msg_proc_main((char *)g_receive_msg); + } + + return 0; +} + +static void aircondition_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)aircondition_task, 0, "AirconditionTask", AIRCONDITION_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, AIRCONDITION_TASK_PRIO); + osal_kfree(task_handle); + } + osal_kthread_unlock(); +} + +/* Run the aircondition_entry */ +app_run(aircondition_entry); + +static void pwm_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)pwm_task, 0, "PwmTask", PWM_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, PWM_TASK_PRIO); + osal_kfree(task_handle); + } + osal_kthread_unlock(); +} + +/* Run the pwm_entry. */ +app_run(pwm_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_ambient_light.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_ambient_light.c new file mode 100644 index 0000000000000000000000000000000000000000..83462ab353015a294f20487713867bec32812204 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_ambient_light.c @@ -0,0 +1,156 @@ + +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: Aircondition Sample Source. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ +#include "boards.h" +#include "pinctrl.h" +#include "gpio.h" +#include "soc_osal.h" +#include "app_init.h" +#include "sle_common.h" +#include "sle_car.h" +unsigned char g_local_addr[SLE_ADDR_LEN] = { 0x22, 0x22, 0x11, 0x11, 0x11, 0x11 }; + + +#define AMBIENT_LIGHT_DURATION_MS 100 + +#define AMBIENT_LIGHT_TASK_PRIO 24 +#define AMBIENT_LIGHT_TASK_STACK_SIZE 0x1000 + +sle_data_t g_sle_data={ + .type = NONE_TYPE, + .data = {0,0,0,0} +}; + + +#include +#include +#include + +#include "pinctrl.h" +#include "pwm.h" +#include "tcxo.h" +#include "soc_osal.h" +#include "app_init.h" + +#define PWM_CHANNEL 1 +#define PWM_GROUP_ID 1 +#define TEST_TCXO_DELAY_1000MS 1000 + +#define PWM_TASK_PRIO 24 +#define PWM_TASK_STACK_SIZE 0x1000 +#define CONFIG_PWM_PIN 9 +#define CONFIG_PWM_PIN_MODE 1 + +static errcode_t pwm_sample_callback(uint8_t channel) +{ + osal_printk("PWM %d, cycle done. \r\n", channel); + return ERRCODE_SUCC; +} +static void *pwm_task(const char *arg) +{ + UNUSED(arg); + + pwm_config_t cfg_no_repeat = {2000, // 低电平时间 + g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000) + 0, // 相位偏移位 + 0xFF, // 发多少个波形 + true}; // 是否循环 + + uapi_pin_set_mode(CONFIG_PWM_PIN, CONFIG_PWM_PIN_MODE); + uapi_pwm_init(); + uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); + uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); + uapi_pwm_unregister_interrupt(PWM_GROUP_ID); + uapi_pwm_register_interrupt(PWM_GROUP_ID, pwm_sample_callback); + uint8_t channel_id = PWM_CHANNEL; + uapi_pwm_set_group(PWM_GROUP_ID, &channel_id, 1); // 每个通道对应一个bit位 + uapi_pwm_start(PWM_GROUP_ID); + while(1) + { + pwm_config_t cfg_no_repeat = {500, // 低电平时间 + g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000) + 0, // 相位偏移位 + 0xFF, // 发多少个波形 + true}; // 是否循环 + uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); + uapi_pwm_start(PWM_GROUP_ID); + printf("pwm_task g_ambient_light_britness: %d\r\n", g_ambient_light_britness); + osal_msleep(50); + } + return NULL; +} + +unsigned long hex_string_to_number(const char *hex_str) { + unsigned long result = 0; + int len = strlen(hex_str); + + for (int i = 0; i < len; i++) { + // 将16进制字符转换为对应的数值 + result = result * 16 + (unsigned char)strtol(&hex_str[i], NULL, 16); + } + + return result; +} + +int g_ambient_light_britness = 0; +int msg_proc_main(char *hex_str) { + // const char *hex_str = "1A3F"; + printf("Hex String: %s\r\n", hex_str); + if (hex_str[0] == '5' && hex_str[1] == 'c') // LIGHT_COLOR_CHANGE + { + char value_str[3] = {0,0}; + sprintf(value_str,"%c%c",hex_str[6],hex_str[7]); + printf("Hex value_str: %s\r\n", value_str); + g_ambient_light_britness = hex_string_to_number(value_str); + printf("g_ambient_light_britness: %lu\r\n", g_ambient_light_britness); + } + return 0; +} + +static int ambient_light_task(const char *arg) +{ + unused(arg); + osal_printk("ambient_light working.\r\n"); + + while (1) { + msg_proc_main((char *)g_receive_msg); + } + + return 0; +} + +static void ambient_light_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)ambient_light_task, 0, "AirconditionTask", AMBIENT_LIGHT_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, AMBIENT_LIGHT_TASK_PRIO); + osal_kfree(task_handle); + } + osal_kthread_unlock(); +} + +/* Run the ambient_light_entry */ +app_run(ambient_light_entry); + +static void pwm_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)pwm_task, 0, "PwmTask", PWM_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, PWM_TASK_PRIO); + osal_kfree(task_handle); + } + osal_kthread_unlock(); +} + +/* Run the pwm_entry. */ +app_run(pwm_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_enviroment.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_enviroment.c new file mode 100644 index 0000000000000000000000000000000000000000..61960ca0e91ad1a7d6c7feed617d02babfff2f22 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_enviroment.c @@ -0,0 +1,95 @@ + +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: Aircondition Sample Source. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ + +#include "sle_uart_server.h" +#include "sle_common.h" +#include "sle_car.h" +unsigned char g_local_addr[SLE_ADDR_LEN] = { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 }; + +#include "pinctrl.h" +#include "i2c.h" +#include "osal_debug.h" +#include "soc_osal.h" +#include "aht20.h" +#include "app_init.h" + +#define CONFIG_I2C_SCL_MASTER_PIN 15 +#define CONFIG_I2C_SDA_MASTER_PIN 16 +#define CONFIG_I2C_MASTER_PIN_MODE 2 +#define I2C_MASTER_ADDR 0x0 +#define I2C_SLAVE1_ADDR 0x38 +#define I2C_SET_BANDRATE 400000 +#define I2C_TASK_STACK_SIZE 0x1000 +#define I2C_TASK_PRIO 17 + +const unsigned char headSize[] = {64, 64}; + +void app_i2c_init_pin(void) +{ + uapi_pin_set_mode(CONFIG_I2C_SCL_MASTER_PIN, CONFIG_I2C_MASTER_PIN_MODE); + uapi_pin_set_mode(CONFIG_I2C_SDA_MASTER_PIN, CONFIG_I2C_MASTER_PIN_MODE); +} + + unsigned char g_enviroment_temprature[MSG_LEN_MAX] = {0}; +void Aht20TestTask(void) +{ + uint32_t retval = 0; + float temp = 0.0f; + float humi = 0.0f; + static char templine[32] = {0}; + static char humiline[32] = {0}; + uint32_t baudrate = I2C_SET_BANDRATE; + uint32_t hscode = I2C_MASTER_ADDR; + app_i2c_init_pin(); + errcode_t ret = uapi_i2c_master_init(1, baudrate, hscode); + if (ret != 0) { + printf("i2c init failed, ret = %0x\r\n", ret); + } + while (AHT20_Calibrate() != 0) { + printf("AHT20 sensor init failed!\r\n"); + osal_mdelay(100); // 10ms在判断设备是否复位成功 + } + while (1) { + + retval = AHT20_StartMeasure(); + printf("AHT20_StartMeasure: %d\r\n", retval); + retval = AHT20_GetMeasureResult(&temp, &humi); + if (retval != 0) { + printf("get humidity data failed!\r\n"); + } + int ret = sprintf((char *)g_enviroment_temprature, ": %.2f", temp); + ret = sprintf(templine, ": %.2f", temp); + if (ret < 0) { + printf("temp failed\r\n"); + } + ret = sprintf(humiline, ": %.2f", humi); + if (ret < 0) { + printf("humi failed\r\n"); + } + printf("temp = %s, humi = %s\r\n", templine, humiline); + printf("g_enviroment_temprature = %s\r\n", g_enviroment_temprature); + // sle_uart_server_send_report_by_handle((uint8_t *)g_enviroment_temprature,strlen(g_enviroment_temprature)); // sle 发出去 + osal_mdelay(1000); // 1s监测一次 + } +} + +// void Aht20Test(void) +// { +// osal_task *task_handle = NULL; +// osal_kthread_lock(); +// task_handle = osal_kthread_create((osal_kthread_handler)Aht20TestTask, 0, "PwmTask", I2C_TASK_STACK_SIZE); +// if (task_handle != NULL) { +// osal_kthread_set_priority(task_handle, I2C_TASK_PRIO); +// osal_kfree(task_handle); +// } +// osal_kthread_unlock(); +// } + +// app_run(Aht20Test); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_key.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_key.c new file mode 100644 index 0000000000000000000000000000000000000000..0e348e1fdac44a03d8340ddb69eb33d07d00c579 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_key.c @@ -0,0 +1,21 @@ + + +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: Aircondition Sample Source. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ + +#include "sle_common.h" +#include "sle_car.h" +unsigned char g_local_addr[SLE_ADDR_LEN] = { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 }; + + +// todo +// 1. hi3863/hi2821e车钥匙 +// 低功耗透明触控屏、开关机按钮 +// 操控车辆 门窗 车灯 +// 寻车定位 rssi 相位差测距 \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_server.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_server.c new file mode 100644 index 0000000000000000000000000000000000000000..1b570eb10a403ef704c72d3c32c3b4c212525520 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_car_server.c @@ -0,0 +1,200 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: SLE UART Sample Source. \n + * + * History: \n + * 2023-07-17, Create file. \n + */ +// #define CONFIG_SAMPLE_SUPPORT_SLE_AROUND_DEVICES_SERVER +#include "common_def.h" +#include "soc_osal.h" +#include "app_init.h" +#include "pinctrl.h" +#include "uart.h" +// #include "pm_clock.h" +#include "sle_low_latency.h" +#include "securec.h" +#include "sle_uart_server.h" +#include "sle_uart_server_adv.h" +#include "sle_device_discovery.h" +#include "sle_errcode.h" + +#include "sle_car.h" +unsigned char g_receive_msg[MSG_LEN_MAX] = {0}; + +#define SLE_UART_TASK_PRIO 22 +#define SLE_UART_TASK_DURATION_MS 2000 +#define SLE_UART_BAUDRATE 115200 +#define SLE_UART_TRANSFER_SIZE 512 + +static uint8_t g_app_uart_rx_buff[SLE_UART_TRANSFER_SIZE] = { 0 }; + +static uart_buffer_config_t g_app_uart_buffer_config = { + .rx_buffer = g_app_uart_rx_buff, + .rx_buffer_size = SLE_UART_TRANSFER_SIZE +}; + +static void uart_init_pin(void) +{ + if (CONFIG_SLE_UART_BUS == 0) { + uapi_pin_set_mode(CONFIG_UART_TXD_PIN, PIN_MODE_1); + uapi_pin_set_mode(CONFIG_UART_RXD_PIN, PIN_MODE_1); + }else if (CONFIG_SLE_UART_BUS == 1) { + uapi_pin_set_mode(CONFIG_UART_TXD_PIN, PIN_MODE_1); + uapi_pin_set_mode(CONFIG_UART_RXD_PIN, PIN_MODE_1); + } +} + +static void uart_init_config(void) +{ + uart_attr_t attr = { + .baud_rate = SLE_UART_BAUDRATE, + .data_bits = UART_DATA_BIT_8, + .stop_bits = UART_STOP_BIT_1, + .parity = UART_PARITY_NONE + }; + + uart_pin_config_t pin_config = { + .tx_pin = CONFIG_UART_TXD_PIN, + .rx_pin = CONFIG_UART_RXD_PIN, + .cts_pin = PIN_NONE, + .rts_pin = PIN_NONE + }; + uapi_uart_deinit(CONFIG_SLE_UART_BUS); + uapi_uart_init(CONFIG_SLE_UART_BUS, &pin_config, &attr, NULL, &g_app_uart_buffer_config); + +} + +#define SLE_UART_SERVER_DELAY_COUNT 5 + +#define SLE_UART_TASK_STACK_SIZE 0x1200 +#define SLE_ADV_HANDLE_DEFAULT 1 +#define SLE_UART_SERVER_MSG_QUEUE_LEN 5 +#define SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE 32 +#define SLE_UART_SERVER_QUEUE_DELAY 0xFFFFFFFF +#define SLE_UART_SERVER_BUFF_MAX_SIZE 800 + +unsigned long g_sle_uart_server_msgqueue_id; +#define SLE_UART_SERVER_LOG "[sle uart server]" +static void ssaps_server_read_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_read_cb_t *read_cb_para, + errcode_t status) +{ + osal_printk("%s ssaps read request cbk callback server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, read_cb_para->handle, status); +} +static void ssaps_server_write_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para, + errcode_t status) +{ + osal_printk("%s ssaps write request callback cbk server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, write_cb_para->handle, status); + if ((write_cb_para->length > 0) && write_cb_para->value) { + osal_printk("\n sle uart recived data : %s\r\n", write_cb_para->value); + uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)write_cb_para->value, write_cb_para->length, 0); + sprintf((char *)g_receive_msg,"%s", write_cb_para->value); + osal_printk("\n g_receive_msg : %s\r\n", g_receive_msg); + } +} + +static void sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + unused(error); + if (sle_uart_client_is_connected()) { + sle_uart_server_send_report_by_handle(buffer, length); + } else { + osal_printk("%s sle client is not connected! \r\n", SLE_UART_SERVER_LOG); + } +} + + +static void sle_uart_server_create_msgqueue(void) +{ + if (osal_msg_queue_create("sle_uart_server_msgqueue", SLE_UART_SERVER_MSG_QUEUE_LEN, \ + (unsigned long *)&g_sle_uart_server_msgqueue_id, 0, SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE) != OSAL_SUCCESS) { + osal_printk("^%s sle_uart_server_create_msgqueue message queue create failed!\n", SLE_UART_SERVER_LOG); + } +} + +static void sle_uart_server_delete_msgqueue(void) +{ + osal_msg_queue_delete(g_sle_uart_server_msgqueue_id); +} + +static void sle_uart_server_write_msgqueue(uint8_t *buffer_addr, uint16_t buffer_size) +{ + osal_msg_queue_write_copy(g_sle_uart_server_msgqueue_id, (void *)buffer_addr, \ + (uint32_t)buffer_size, 0); +} + + +static int32_t sle_uart_server_receive_msgqueue(uint8_t *buffer_addr, uint32_t *buffer_size) +{ + return osal_msg_queue_read_copy(g_sle_uart_server_msgqueue_id, (void *)buffer_addr, \ + buffer_size, SLE_UART_SERVER_QUEUE_DELAY); +} +static void sle_uart_server_rx_buf_init(uint8_t *buffer_addr, uint32_t *buffer_size) +{ + *buffer_size = SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE; + (void)memset_s(buffer_addr, *buffer_size, 0, *buffer_size); +} + +static void *sle_uart_server_task(const char *arg) +{ + osal_printk("------------sle_uart_server_task--------------.\r\n"); + unused(arg); + uint8_t rx_buf[SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE] = {0}; + uint32_t rx_length = SLE_UART_SERVER_MSG_QUEUE_MAX_SIZE; + uint8_t sle_connect_state[] = "sle_dis_connect"; + + sle_uart_server_create_msgqueue(); + sle_uart_server_register_msg(sle_uart_server_write_msgqueue); + sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk); + + + /* UART pinmux. */ + uart_init_pin(); + + /* UART init config. */ + uart_init_config(); + + uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS); + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_server_read_int_handler); + if (ret != ERRCODE_SUCC) { + osal_printk("%s Register uart callback fail.[%x]\r\n", SLE_UART_SERVER_LOG, ret); + return NULL; + } + // Aht20TestTask(); + while (1) { + sle_uart_server_rx_buf_init(rx_buf, &rx_length); + sle_uart_server_receive_msgqueue(rx_buf, &rx_length); + if (strncmp((const char *)rx_buf, (const char *)sle_connect_state, sizeof(sle_connect_state)) == 0) { + ret = sle_start_announce(SLE_ADV_HANDLE_DEFAULT); + if (ret != ERRCODE_SLE_SUCCESS) { + osal_printk("%s sle_connect_state_changed_cbk,sle_start_announce fail :%02x\r\n", + SLE_UART_SERVER_LOG, ret); + } + } + // printf("g_enviroment_temprature sle_uart= %s\r\n", g_enviroment_temprature); + // sle_uart_server_send_report_by_handle((uint8_t *)g_enviroment_temprature,strlen(g_enviroment_temprature)); // sle 发出去 + osal_msleep(SLE_UART_TASK_DURATION_MS); + } + sle_uart_server_delete_msgqueue(); + return NULL; +} + +static void sle_uart_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)sle_uart_server_task, 0, "SLEUartServerTask", + SLE_UART_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, SLE_UART_TASK_PRIO); + } + osal_kthread_unlock(); +} + +/* Run the sle_uart_entry. */ +app_run(sle_uart_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.c new file mode 100644 index 0000000000000000000000000000000000000000..ebe6852d0402ba2e25e86562b3a591e5ced4cf2a --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.c @@ -0,0 +1,405 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: SLE UART Server Source. \n + * + * History: \n + * 2023-07-17, Create file. \n + */ +#include "common_def.h" +#include "securec.h" +#include "soc_osal.h" +#include "sle_errcode.h" +#include "sle_connection_manager.h" +#include "sle_device_discovery.h" +#include "sle_uart_server_adv.h" +#include "sle_uart_server.h" +#define OCTET_BIT_LEN 8 +#define UUID_LEN_2 2 +#define UUID_INDEX 14 +#define BT_INDEX_4 4 +#define BT_INDEX_0 0 +#define UART_BUFF_LENGTH 0x100 + +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* sle server app uuid for test */ +static char g_sle_uuid_app_uuid[UUID_LEN_2] = { 0x12, 0x34 }; +/* server notify property uuid for test */ +static char g_sle_property_value[OCTET_BIT_LEN] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; +/* sle connect acb handle */ +static uint16_t g_sle_conn_hdl = 0; +/* sle server handle */ +static uint8_t g_server_id = 0; +/* sle service handle */ +static uint16_t g_service_handle = 0; +/* sle ntf property handle */ +static uint16_t g_property_handle = 0; +/* sle pair acb handle */ +uint16_t g_sle_pair_hdl; + +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" +#define SLE_SERVER_INIT_DELAY_MS 1000 +static sle_uart_server_msg_queue g_sle_uart_server_msg_queue = NULL; +static uint8_t g_sle_uart_base[] = { 0x37, 0xBE, 0xA8, 0x80, 0xFC, 0x70, 0x11, 0xEA, \ + 0xB7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +uint16_t get_connect_id(void) +{ + return g_sle_conn_hdl; +} + +static void encode2byte_little(uint8_t *_ptr, uint16_t data) +{ + *(uint8_t *)((_ptr) + 1) = (uint8_t)((data) >> 0x8); + *(uint8_t *)(_ptr) = (uint8_t)(data); +} + +static void sle_uuid_set_base(sle_uuid_t *out) +{ + errcode_t ret; + ret = memcpy_s(out->uuid, SLE_UUID_LEN, g_sle_uart_base, SLE_UUID_LEN); + if (ret != EOK) { + sample_at_log_print("%s sle_uuid_set_base memcpy fail\n", SLE_UART_SERVER_LOG); + out->len = 0; + return ; + } + out->len = UUID_LEN_2; +} + +static void sle_uuid_setu2(uint16_t u2, sle_uuid_t *out) +{ + sle_uuid_set_base(out); + out->len = UUID_LEN_2; + encode2byte_little(&out->uuid[UUID_INDEX], u2); +} +static void sle_uart_uuid_print(sle_uuid_t *uuid) +{ + if (uuid == NULL) { + sample_at_log_print("%s uuid_print,uuid is null\r\n", SLE_UART_SERVER_LOG); + return; + } + if (uuid->len == UUID_16BIT_LEN) { + sample_at_log_print("%s uuid: %02x %02x.\n", SLE_UART_SERVER_LOG, + uuid->uuid[14], uuid->uuid[15]); /* 14 15: uuid index */ + } else if (uuid->len == UUID_128BIT_LEN) { + sample_at_log_print("%s uuid: \n", SLE_UART_SERVER_LOG); /* 14 15: uuid index */ + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[0], uuid->uuid[1], + uuid->uuid[2], uuid->uuid[3]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[4], uuid->uuid[5], + uuid->uuid[6], uuid->uuid[7]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[8], uuid->uuid[9], + uuid->uuid[10], uuid->uuid[11]); + sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[12], uuid->uuid[13], + uuid->uuid[14], uuid->uuid[15]); + } +} + +static void ssaps_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id, ssap_exchange_info_t *mtu_size, + errcode_t status) +{ + sample_at_log_print("%s ssaps ssaps_mtu_changed_cbk callback server_id:%x, conn_id:%x, mtu_size:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, conn_id, mtu_size->mtu_size, status); + if (g_sle_pair_hdl == 0) { + g_sle_pair_hdl = conn_id + 1; + } +} + +static void ssaps_start_service_cbk(uint8_t server_id, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s start service cbk callback server_id:%d, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); +} +static void ssaps_add_service_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add service cbk callback server_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_property_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t service_handle, + uint16_t handle, errcode_t status) +{ + sample_at_log_print("%s add property cbk callback server_id:%x, service_handle:%x,handle:%x, status:%x\r\n", + SLE_UART_SERVER_LOG, server_id, service_handle, handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_add_descriptor_cbk(uint8_t server_id, sle_uuid_t *uuid, uint16_t service_handle, + uint16_t property_handle, errcode_t status) +{ + sample_at_log_print("%s add descriptor cbk callback server_id:%x, service_handle:%x, property_handle:%x, \ + status:%x\r\n", SLE_UART_SERVER_LOG, server_id, service_handle, property_handle, status); + sle_uart_uuid_print(uuid); +} +static void ssaps_delete_all_service_cbk(uint8_t server_id, errcode_t status) +{ + sample_at_log_print("%s delete all service callback server_id:%x, status:%x\r\n", SLE_UART_SERVER_LOG, + server_id, status); +} +static errcode_t sle_ssaps_register_cbks(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback) +{ + errcode_t ret; + ssaps_callbacks_t ssaps_cbk = {0}; + ssaps_cbk.add_service_cb = ssaps_add_service_cbk; + ssaps_cbk.add_property_cb = ssaps_add_property_cbk; + ssaps_cbk.add_descriptor_cb = ssaps_add_descriptor_cbk; + ssaps_cbk.start_service_cb = ssaps_start_service_cbk; + ssaps_cbk.delete_all_service_cb = ssaps_delete_all_service_cbk; + ssaps_cbk.mtu_changed_cb = ssaps_mtu_changed_cbk; + ssaps_cbk.read_request_cb = ssaps_read_callback; + ssaps_cbk.write_request_cb = ssaps_write_callback; + ret = ssaps_register_callbacks(&ssaps_cbk); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_ssaps_register_cbks,ssaps_register_callbacks fail :%x\r\n", SLE_UART_SERVER_LOG, + ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_service_add(void) +{ + errcode_t ret; + sle_uuid_t service_uuid = {0}; + sle_uuid_setu2(SLE_UUID_SERVER_SERVICE, &service_uuid); + ret = ssaps_add_service_sync(g_server_id, &service_uuid, 1, &g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uuid add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uuid_server_property_add(void) +{ + errcode_t ret; + ssaps_property_info_t property = {0}; + ssaps_desc_info_t descriptor = {0}; + uint8_t ntf_value[] = { 0x01, 0x02 }; + + property.permissions = SLE_UUID_TEST_PROPERTIES; + property.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &property.uuid); + property.value = (uint8_t *)osal_vmalloc(sizeof(g_sle_property_value)); + if (property.value == NULL) { + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(property.value, sizeof(g_sle_property_value), g_sle_property_value, + sizeof(g_sle_property_value)) != EOK) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + ret = ssaps_add_property_sync(g_server_id, g_service_handle, &property, &g_property_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add property fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + descriptor.permissions = SLE_UUID_TEST_DESCRIPTOR; + descriptor.type = SSAP_DESCRIPTOR_CLIENT_CONFIGURATION; + descriptor.operate_indication = SLE_UUID_TEST_OPERATION_INDICATION; + descriptor.value = (uint8_t *)osal_vmalloc(sizeof(ntf_value)); + if (descriptor.value == NULL) { + osal_vfree(property.value); + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(descriptor.value, sizeof(ntf_value), ntf_value, sizeof(ntf_value)) != EOK) { + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + ret = ssaps_add_descriptor_sync(g_server_id, g_service_handle, g_property_handle, &descriptor); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add descriptor fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_FAIL; + } + osal_vfree(property.value); + osal_vfree(descriptor.value); + return ERRCODE_SLE_SUCCESS; +} + +static errcode_t sle_uart_server_add(void) +{ + errcode_t ret; + sle_uuid_t app_uuid = {0}; + + sample_at_log_print("%s sle uart add service in\r\n", SLE_UART_SERVER_LOG); + app_uuid.len = sizeof(g_sle_uuid_app_uuid); + if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) { + return ERRCODE_SLE_FAIL; + } + ssaps_register_server(&app_uuid, &g_server_id); + + if (sle_uuid_server_service_add() != ERRCODE_SLE_SUCCESS) { + ssaps_unregister_server(g_server_id); + return ERRCODE_SLE_FAIL; + } + if (sle_uuid_server_property_add() != ERRCODE_SLE_SUCCESS) { + ssaps_unregister_server(g_server_id); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service, server_id:%x, service_handle:%x, property_handle:%x\r\n", + SLE_UART_SERVER_LOG, g_server_id, g_service_handle, g_property_handle); + ret = ssaps_start_service(g_server_id, g_service_handle); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle uart add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret); + return ERRCODE_SLE_FAIL; + } + sample_at_log_print("%s sle uart add service out\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + +/* device通过uuid向host发送数据:report */ +errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len) +{ + errcode_t ret; + ssaps_ntf_ind_by_uuid_t param = {0}; + param.type = SSAP_PROPERTY_TYPE_VALUE; + param.start_handle = g_service_handle; + param.end_handle = g_property_handle; + param.value_len = len; + param.value = (uint8_t *)osal_vmalloc(len); + if (param.value == NULL) { + sample_at_log_print("%s send report new fail\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_FAIL; + } + if (memcpy_s(param.value, param.value_len, data, len) != EOK) { + sample_at_log_print("%s send input report memcpy fail\r\n", SLE_UART_SERVER_LOG); + osal_vfree(param.value); + return ERRCODE_SLE_FAIL; + } + sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, ¶m.uuid); + ret = ssaps_notify_indicate_by_uuid(g_server_id, g_sle_conn_hdl, ¶m); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_send_report_by_uuid,ssaps_notify_indicate_by_uuid fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + osal_vfree(param.value); + return ret; + } + osal_vfree(param.value); + return ERRCODE_SLE_SUCCESS; +} + +/* device通过handle向host发送数据:report */ +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint16_t len) +{ + ssaps_ntf_ind_t param = {0}; + uint8_t receive_buf[UART_BUFF_LENGTH] = { 0 }; /* max receive length. */ + param.handle = g_property_handle; + param.type = SSAP_PROPERTY_TYPE_VALUE; + param.value = receive_buf; + param.value_len = len; + if (memcpy_s(param.value, param.value_len, data, len) != EOK) { + return ERRCODE_SLE_FAIL; + } + return ssaps_notify_indicate(g_server_id, g_sle_conn_hdl, ¶m); +} + +#include "sle_car.h" +static void sle_connect_state_changed_cbk(uint16_t conn_id, const sle_addr_t *addr, + sle_acb_state_t conn_state, sle_pair_state_t pair_state, sle_disc_reason_t disc_reason) +{ + uint8_t sle_connect_state[] = "sle_dis_connect"; + sample_at_log_print("%s connect state changed callback conn_id:0x%02x, conn_state:0x%x, pair_state:0x%x, \ + disc_reason:0x%x\r\n", SLE_UART_SERVER_LOG,conn_id, conn_state, pair_state, disc_reason); + sample_at_log_print("%s connect state changed callback addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + conn_id = g_local_addr[0]/0x11; + osal_printk("sle_connect_state_changed_cbk conn_id = g_local_addr[0]/0x11:%d\r\n",conn_id); + if (conn_state == SLE_ACB_STATE_CONNECTED) { + g_sle_conn_hdl = conn_id; + } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) { + g_sle_conn_hdl = 0; + g_sle_pair_hdl = 0; + if (g_sle_uart_server_msg_queue != NULL) { + g_sle_uart_server_msg_queue(sle_connect_state, sizeof(sle_connect_state)); + } + } +} + +static void sle_pair_complete_cbk(uint16_t conn_id, const sle_addr_t *addr, errcode_t status) +{ + sample_at_log_print("%s pair complete conn_id:%02x, status:%x\r\n", SLE_UART_SERVER_LOG, + conn_id, status); + sample_at_log_print("%s pair complete addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG, + addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]); + g_sle_pair_hdl = conn_id + 1; + ssap_exchange_info_t parameter = { 0 }; + parameter.mtu_size = 520; + parameter.version = 1; + // g_server_id = 3; + ssaps_set_info(g_server_id, ¶meter); +} + +static errcode_t sle_conn_register_cbks(void) +{ + errcode_t ret; + sle_connection_callbacks_t conn_cbks = {0}; + conn_cbks.connect_state_changed_cb = sle_connect_state_changed_cbk; + conn_cbks.pair_complete_cb = sle_pair_complete_cbk; + ret = sle_connection_register_callbacks(&conn_cbks); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_conn_register_cbks,sle_connection_register_callbacks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + return ERRCODE_SLE_SUCCESS; +} + +uint16_t sle_uart_client_is_connected(void) +{ + return g_sle_pair_hdl; +} + +/* 初始化uuid server */ +errcode_t sle_uart_server_init(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback) +{ + errcode_t ret; + + /* 使能SLE */ + if (enable_sle() != ERRCODE_SUCC) { + sample_at_log_print("[SLE Server] sle enbale fail !\r\n"); + return -1; + } + + ret = sle_uart_announce_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_announce_register_cbks fail :%x\r\n", + SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_conn_register_cbks(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_conn_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_ssaps_register_cbks(ssaps_read_callback, ssaps_write_callback); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_ssaps_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_uart_server_add(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_add fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + ret = sle_uart_server_adv_init(); + if (ret != ERRCODE_SLE_SUCCESS) { + sample_at_log_print("%s sle_uart_server_init,sle_uart_server_adv_init fail :%x\r\n", SLE_UART_SERVER_LOG, ret); + return ret; + } + sample_at_log_print("%s init ok\r\n", SLE_UART_SERVER_LOG); + return ERRCODE_SLE_SUCCESS; +} + + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg) +{ + g_sle_uart_server_msg_queue = sle_uart_server_msg; +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.h b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.h new file mode 100644 index 0000000000000000000000000000000000000000..e72a3fea4586146d13003db86c024360982287c5 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server.h @@ -0,0 +1,60 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: SLE uart server Config. \n + * + * History: \n + * 2023-07-17, Create file. \n + */ + +#ifndef SLE_UART_SERVER_H +#define SLE_UART_SERVER_H + +#include +#include "sle_ssap_server.h" +#include "errcode.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* Service UUID */ +#define SLE_UUID_SERVER_SERVICE 0x2222 + +/* Property UUID */ +#define SLE_UUID_SERVER_NTF_REPORT 0x2323 + +/* Property Property */ +#define SLE_UUID_TEST_PROPERTIES (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +/* Operation indication */ +#define SLE_UUID_TEST_OPERATION_INDICATION (SSAP_OPERATE_INDICATION_BIT_READ | SSAP_OPERATE_INDICATION_BIT_WRITE) + +/* Descriptor Property */ +#define SLE_UUID_TEST_DESCRIPTOR (SSAP_PERMISSION_READ | SSAP_PERMISSION_WRITE) + +errcode_t sle_uart_server_init(ssaps_read_request_callback ssaps_read_callback, ssaps_write_request_callback + ssaps_write_callback); + +errcode_t sle_uart_server_send_report_by_uuid(const uint8_t *data, uint8_t len); + +errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint16_t len); + +uint16_t sle_uart_client_is_connected(void); + +typedef void (*sle_uart_server_msg_queue)(uint8_t *buffer_addr, uint16_t buffer_size); + +void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg); + + +uint16_t get_connect_id(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server_adv.c b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server_adv.c new file mode 100644 index 0000000000000000000000000000000000000000..d50974ba1886c62adddba35a7d205362b478c927 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_around_devices/sle_uart_server/sle_uart_server_adv.c @@ -0,0 +1,249 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: sle adv config for sle uart server. \n + * + * History: \n + * 2023-07-17, Create file. \n + */ +#include "securec.h" +#include "errcode.h" +#include "osal_addr.h" +#include "product.h" +#include "sle_common.h" +#include "sle_uart_server.h" +#include "sle_device_discovery.h" +#include "sle_errcode.h" +#include "osal_debug.h" +#include "osal_task.h" +#include "string.h" +#include "sle_uart_server_adv.h" + +/* sle device name */ +#define NAME_MAX_LENGTH 16 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MIN_DEFAULT 0x64 +/* 连接调度间隔12.5ms,单位125us */ +#define SLE_CONN_INTV_MAX_DEFAULT 0x64 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MIN_DEFAULT 0xC8 +/* 连接调度间隔25ms,单位125us */ +#define SLE_ADV_INTERVAL_MAX_DEFAULT 0xC8 +/* 超时时间5000ms,单位10ms */ +#define SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT 0x1F4 +/* 超时时间4990ms,单位10ms */ +#define SLE_CONN_MAX_LATENCY 0x1F3 +/* 广播发送功率 */ +#define SLE_ADV_TX_POWER 10 +/* 广播ID */ +#define SLE_ADV_HANDLE_DEFAULT 1 +/* 最大广播数据长度 */ +#define SLE_ADV_DATA_LEN_MAX 251 +/* 广播名称 */ +static uint8_t sle_local_name[NAME_MAX_LENGTH] = "sle_uart_server"; +#define SLE_SERVER_INIT_DELAY_MS 1000 +#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args) +#define SLE_UART_SERVER_LOG "[sle uart server]" + +static uint16_t sle_set_adv_local_name(uint8_t *adv_data, uint16_t max_len) +{ + errno_t ret; + uint8_t index = 0; + + uint8_t *local_name = sle_local_name; + uint8_t local_name_len = sizeof(sle_local_name) - 1; + sample_at_log_print("%s local_name_len = %d\r\n", SLE_UART_SERVER_LOG, local_name_len); + sample_at_log_print("%s local_name: ", SLE_UART_SERVER_LOG); + for (uint8_t i = 0; i < local_name_len; i++) { + sample_at_log_print("0x%02x ", local_name[i]); + } + sample_at_log_print("\r\n"); + adv_data[index++] = local_name_len + 1; + adv_data[index++] = SLE_ADV_DATA_TYPE_COMPLETE_LOCAL_NAME; + ret = memcpy_s(&adv_data[index], max_len - index, local_name, local_name_len); + if (ret != EOK) { + sample_at_log_print("%s memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + return (uint16_t)index + local_name_len; +} + +static uint16_t sle_set_adv_data(uint8_t *adv_data) +{ + size_t len = 0; + uint16_t idx = 0; + errno_t ret = 0; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_disc_level = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_DISCOVERY_LEVEL, + .value = SLE_ANNOUNCE_LEVEL_NORMAL, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_disc_level, len); + if (ret != EOK) { + sample_at_log_print("%s adv_disc_level memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + len = sizeof(struct sle_adv_common_value); + struct sle_adv_common_value adv_access_mode = { + .length = len - 1, + .type = SLE_ADV_DATA_TYPE_ACCESS_MODE, + .value = 0, + }; + ret = memcpy_s(&adv_data[idx], SLE_ADV_DATA_LEN_MAX - idx, &adv_access_mode, len); + if (ret != EOK) { + sample_at_log_print("%s adv_access_mode memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += len; + + return idx; +} + +static uint16_t sle_set_scan_response_data(uint8_t *scan_rsp_data) +{ + uint16_t idx = 0; + errno_t ret; + size_t scan_rsp_data_len = sizeof(struct sle_adv_common_value); + + struct sle_adv_common_value tx_power_level = { + .length = scan_rsp_data_len - 1, + .type = SLE_ADV_DATA_TYPE_TX_POWER_LEVEL, + .value = SLE_ADV_TX_POWER, + }; + ret = memcpy_s(scan_rsp_data, SLE_ADV_DATA_LEN_MAX, &tx_power_level, scan_rsp_data_len); + if (ret != EOK) { + sample_at_log_print("%s sle scan response data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + idx += scan_rsp_data_len; + + /* set local name */ + idx += sle_set_adv_local_name(&scan_rsp_data[idx], SLE_ADV_DATA_LEN_MAX - idx); + return idx; +} + +#include "sle_car.h" +static int sle_set_default_announce_param(void) +{ + errno_t ret; + sle_announce_param_t param = {0}; + uint8_t index; // mac地址需要与各设备匹配,不能一样 + // unsigned char local_addr[SLE_ADDR_LEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x01 }; + + param.announce_mode = SLE_ANNOUNCE_MODE_CONNECTABLE_SCANABLE; + param.announce_handle = SLE_ADV_HANDLE_DEFAULT; + param.announce_gt_role = SLE_ANNOUNCE_ROLE_T_CAN_NEGO; + param.announce_level = SLE_ANNOUNCE_LEVEL_NORMAL; + param.announce_channel_map = SLE_ADV_CHANNEL_MAP_DEFAULT; + param.announce_interval_min = SLE_ADV_INTERVAL_MIN_DEFAULT; + param.announce_interval_max = SLE_ADV_INTERVAL_MAX_DEFAULT; + param.conn_interval_min = SLE_CONN_INTV_MIN_DEFAULT; + param.conn_interval_max = SLE_CONN_INTV_MAX_DEFAULT; + param.conn_max_latency = SLE_CONN_MAX_LATENCY; + param.conn_supervision_timeout = SLE_CONN_SUPERVISION_TIMEOUT_DEFAULT; + param.announce_tx_power = 18; + param.own_addr.type = 0; + ret = memcpy_s(param.own_addr.addr, SLE_ADDR_LEN, g_local_addr, SLE_ADDR_LEN); + if (ret != EOK) { + sample_at_log_print("%s sle_set_default_announce_param data memcpy fail\r\n", SLE_UART_SERVER_LOG); + return 0; + } + sample_at_log_print("%s sle_uart_local addr: ", SLE_UART_SERVER_LOG); + for (index = 0; index < SLE_ADDR_LEN; index++) { + sample_at_log_print("0x%02x ", param.own_addr.addr[index]); + } + sample_at_log_print("\r\n"); + return sle_set_announce_param(param.announce_handle, ¶m); +} + +static int sle_set_default_announce_data(void) +{ + errcode_t ret; + uint8_t announce_data_len = 0; + uint8_t seek_data_len = 0; + sle_announce_data_t data = {0}; + uint8_t adv_handle = SLE_ADV_HANDLE_DEFAULT; + uint8_t announce_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t seek_rsp_data[SLE_ADV_DATA_LEN_MAX] = {0}; + uint8_t data_index = 0; + + announce_data_len = sle_set_adv_data(announce_data); + data.announce_data = announce_data; + data.announce_data_len = announce_data_len; + + sample_at_log_print("%s data.announce_data_len = %d\r\n", SLE_UART_SERVER_LOG, data.announce_data_len); + sample_at_log_print("%s data.announce_data: ", SLE_UART_SERVER_LOG); + for (data_index = 0; data_indexdata); + uapi_uart_write(CONFIG_SLE_UART_BUS_CLIENT, (uint8_t *)(data->data), data->data_len, 0); +} + +void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, + errcode_t status) +{ + unused(client_id); + unused(conn_id); + unused(status); + osal_printk("\n sle uart recived data : %s\r\n", data->data); + uapi_uart_write(CONFIG_SLE_UART_BUS_CLIENT, (uint8_t *)(data->data), data->data_len, 0); +} +// 串口收到数据,发送到client端 +static void sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error) +{ + unused(error); + + uint8_t *buff = (uint8_t *)buffer; + char num_str[2] = {buff[0], '\0'}; //把第一个字符当作conn_id + ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param(); + uint16_t g_sle_uart_conn_id = atoi(num_str); + if(buff[0]=='5' && buff[1]=='9') // door change + { + g_sle_uart_conn_id = 4; + } + else if(buff[0]=='5' && buff[1]=='a') // trunk change + { + g_sle_uart_conn_id = 5; + } + else if(buff[0]=='5' && buff[1]=='b') // aircondition change + { + g_sle_uart_conn_id = 1; + } + else if(buff[0]=='5' && buff[1]=='c') // ambient light change + { + g_sle_uart_conn_id = 1; + } + else if(buff[0]=='5' && buff[1]=='d') // enviroment change + { + g_sle_uart_conn_id = 3; + } + osal_printk("\n sle_uart_client_read_int_handler: %d\r\n", g_sle_uart_conn_id); + osal_printk("\n sle_uart_client_read: %s\r\n", buffer); + sle_uart_send_param->data_len = length; + sle_uart_send_param->data = (uint8_t *)buffer; + ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param); +} + +static void *sle_uart_client_task(const char *arg) +{ + osal_printk("------------sle_uart_client_task--------------.\r\n"); + unused(arg); + /* UART pinmux. */ + uart_init_pin(); + + /* UART init config. */ + uart_init_config(); + + uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS_CLIENT); + errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS_CLIENT, + UART_RX_CONDITION_FULL_OR_IDLE, + 1, sle_uart_client_read_int_handler); + sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb); + + if (ret != ERRCODE_SUCC) { + osal_printk("Register uart callback fail."); + return NULL; + } + + return NULL; +} +static void sle_uart_entry(void) +{ + osal_task *task_handle = NULL; + osal_kthread_lock(); + task_handle = osal_kthread_create((osal_kthread_handler)sle_uart_client_task, 0, "SLEUartDongleTask", + SLE_UART_TASK_STACK_SIZE); + if (task_handle != NULL) { + osal_kthread_set_priority(task_handle, SLE_UART_TASK_PRIO); + } + osal_kthread_unlock(); +} + +/* Run the sle_uart_entry. */ +app_run(sle_uart_entry); \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.c b/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.c new file mode 100644 index 0000000000000000000000000000000000000000..06044649a661a582a5e311f652b6e159acab6351 --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.c @@ -0,0 +1,269 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: SLE uart sample of client. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ +#include "common_def.h" +#include "soc_osal.h" +#include "securec.h" +#include "product.h" +#include "bts_le_gap.h" + +#include "sle_device_discovery.h" +#include "sle_connection_manager.h" +#include "sle_uart_client.h" +#define SLE_MTU_SIZE_DEFAULT 520 +#define SLE_SEEK_INTERVAL_DEFAULT 100 +#define SLE_SEEK_WINDOW_DEFAULT 100 +#define UUID_16BIT_LEN 2 +#define UUID_128BIT_LEN 16 +#define SLE_UART_TASK_DELAY_MS 1000 +#define SLE_UART_WAIT_SLE_CORE_READY_MS 5000 +#define SLE_UART_RECV_CNT 1000 +#define SLE_UART_LOW_LATENCY_2K 2000 +#ifndef SLE_UART_SERVER_NAME +#define SLE_UART_SERVER_NAME "sle_uart_server" +#endif +#define SLE_UART_CLIENT_LOG "[sle uart client]" +#define SLE_UART_CLIENT_MAX_CON 8 +static ssapc_find_service_result_t g_sle_uart_find_service_result = { 0 }; +static sle_announce_seek_callbacks_t g_sle_uart_seek_cbk = { 0 }; +static sle_connection_callbacks_t g_sle_uart_connect_cbk = { 0 }; +static ssapc_callbacks_t g_sle_uart_ssapc_cbk = { 0 }; +static sle_addr_t g_sle_uart_remote_addr = { 0 }; +ssapc_write_param_t g_sle_uart_send_param = { 0 }; +uint16_t g_sle_uart_conn_id[SLE_UART_CLIENT_MAX_CON] = { 0 }; +uint16_t g_sle_uart_conn_num = 0; + + +ssapc_write_param_t *get_g_sle_uart_send_param(void) +{ + return &g_sle_uart_send_param; +} + +void sle_uart_start_scan(void) +{ + sle_seek_param_t param = { 0 }; + param.own_addr_type = 0; + param.filter_duplicates = 0; + param.seek_filter_policy = 0; + param.seek_phys = 1; + param.seek_type[0] = 1; + param.seek_interval[0] = SLE_SEEK_INTERVAL_DEFAULT; + param.seek_window[0] = SLE_SEEK_WINDOW_DEFAULT; + sle_set_seek_param(¶m); + sle_start_seek(); +} + +static void sle_uart_client_sample_sle_enable_cbk(errcode_t status) +{ + osal_printk("sle enable: %d.\r\n", status); + sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb); + sle_uart_start_scan(); +} + +static void sle_uart_client_sample_seek_enable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_enable_cbk,status error\r\n", SLE_UART_CLIENT_LOG); + } +} +// 扫描结果回调 +static void sle_uart_client_sample_seek_result_info_cbk(sle_seek_result_info_t *seek_result_data) +{ + osal_printk("sle_sample_seek_result_info_cbk [%02x,%02x,%02x,%02x,%02x,%02x]\n",seek_result_data->addr.addr[0], + seek_result_data->addr.addr[1],seek_result_data->addr.addr[2],seek_result_data->addr.addr[3],seek_result_data->addr.addr[4], + seek_result_data->addr.addr[5]); + osal_printk("sle_sample_seek_result_info_cbk %s\r\n", seek_result_data->data); + if (seek_result_data != NULL) { + if(g_sle_uart_conn_num < SLE_UART_CLIENT_MAX_CON){ + if (strstr((const char *)seek_result_data->data, SLE_UART_SERVER_NAME) != NULL) { + osal_printk("will connect dev\n"); + (void)memcpy_s(&g_sle_uart_remote_addr, sizeof(sle_addr_t), &seek_result_data->addr, sizeof(sle_addr_t)); + sle_stop_seek(); + } + } + } +} +// 连接设备 +static void sle_uart_client_sample_seek_disable_cbk(errcode_t status) +{ + if (status != 0) { + osal_printk("%s sle_uart_client_sample_seek_disable_cbk,status error = %x\r\n", SLE_UART_CLIENT_LOG, status); + } else { + sle_connect_remote_device(&g_sle_uart_remote_addr); + } +} + +static void sle_uart_client_sample_seek_cbk_register(void) +{ + g_sle_uart_seek_cbk.sle_enable_cb = sle_uart_client_sample_sle_enable_cbk; + g_sle_uart_seek_cbk.seek_enable_cb = sle_uart_client_sample_seek_enable_cbk; + g_sle_uart_seek_cbk.seek_result_cb = sle_uart_client_sample_seek_result_info_cbk; + g_sle_uart_seek_cbk.seek_disable_cb = sle_uart_client_sample_seek_disable_cbk; + sle_announce_seek_register_callbacks(&g_sle_uart_seek_cbk); +} + +unsigned char g_devices_addr[8] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }; + +int get_server_conn_id(void) +{ + int conn_id = 0; + switch (g_sle_uart_remote_addr.addr[1]) + { + case 0x11: + /* code */ + conn_id = 1; + break; + case 0x22: + /* code */ + conn_id = 2; + break; + case 0x33: + /* code */ + conn_id = 3; + break; + case 0x44: + /* code */ + conn_id = 4; + break; + + default: + break; + } + return conn_id; +} +static void sle_uart_client_sample_connect_state_changed_cbk(uint16_t conn_id, const sle_addr_t *addr, + sle_acb_state_t conn_state, sle_pair_state_t pair_state, + sle_disc_reason_t disc_reason) +{ + unused(addr); + unused(pair_state); + osal_printk("%s conn state changed disc_reason:0x%x\r\n", SLE_UART_CLIENT_LOG, disc_reason); + if (conn_state == SLE_ACB_STATE_CONNECTED) { + osal_printk("%s SLE_ACB_STATE_CONNECTED\r\n", SLE_UART_CLIENT_LOG); + osal_printk("%s hellokun------oral conn_id--%d\r\n", SLE_UART_CLIENT_LOG, conn_id); + // id是按连接顺序分配的,顺序增加,不便于区分具体设备 + // g_sle_uart_conn_id[g_sle_uart_conn_num] = conn_id; + conn_id = get_server_conn_id(); + osal_printk("%s hellokun------conn_id--%d\r\n", SLE_UART_CLIENT_LOG, conn_id); + g_sle_uart_conn_id[g_sle_uart_conn_num] = conn_id; + ssap_exchange_info_t info = {0}; + info.mtu_size = SLE_MTU_SIZE_DEFAULT; + info.version = 1; + ssapc_exchange_info_req(1, g_sle_uart_conn_id[g_sle_uart_conn_num], &info); + g_sle_uart_conn_num++; + } else if (conn_state == SLE_ACB_STATE_NONE) { + osal_printk("%s SLE_ACB_STATE_NONE\r\n", SLE_UART_CLIENT_LOG); + } else if (conn_state == SLE_ACB_STATE_DISCONNECTED) { + osal_printk("%s SLE_ACB_STATE_DISCONNECTED\r\n", SLE_UART_CLIENT_LOG); + g_sle_uart_conn_num--; + sle_uart_start_scan(); + } else { + osal_printk("%s status error\r\n", SLE_UART_CLIENT_LOG); + } + sle_start_seek(); +} + +void sle_uart_client_sample_pair_complete_cbk(uint16_t conn_id, const sle_addr_t *addr, errcode_t status) +{ + osal_printk("%s pair complete conn_id:%d, addr:%02x***%02x%02x\n", SLE_UART_CLIENT_LOG, conn_id, + addr->addr[0], addr->addr[4], addr->addr[5]); + if (status == 0) { + ssap_exchange_info_t info = {0}; + info.mtu_size = SLE_MTU_SIZE_DEFAULT; + info.version = 1; + ssapc_exchange_info_req(0, g_sle_uart_conn_id[g_sle_uart_conn_num], &info); + } +} + +static void sle_uart_client_sample_connect_cbk_register(void) +{ + g_sle_uart_connect_cbk.connect_state_changed_cb = sle_uart_client_sample_connect_state_changed_cbk; + g_sle_uart_connect_cbk.pair_complete_cb = sle_uart_client_sample_pair_complete_cbk; + sle_connection_register_callbacks(&g_sle_uart_connect_cbk); +} + +static void sle_uart_client_sample_exchange_info_cbk(uint8_t client_id, uint16_t conn_id, ssap_exchange_info_t *param, + errcode_t status) +{ + osal_printk("%s exchange_info_cbk,pair complete client id:%d status:%d\r\n", + SLE_UART_CLIENT_LOG, client_id, status); + osal_printk("%s exchange mtu, mtu size: %d, version: %d.\r\n", SLE_UART_CLIENT_LOG, + param->mtu_size, param->version); + conn_id = get_server_conn_id(); + ssapc_find_structure_param_t find_param = { 0 }; + find_param.type = SSAP_FIND_TYPE_PROPERTY; + find_param.start_hdl = 1; + find_param.end_hdl = 0xFFFF; + ssapc_find_structure(0, conn_id, &find_param); +} + +static void sle_uart_client_sample_find_structure_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_service_result_t *service, + errcode_t status) +{ + osal_printk("%s find structure cbk client: %d conn_id:%d status: %d \r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, status); + osal_printk("%s find structure start_hdl:[0x%02x], end_hdl:[0x%02x], uuid len:%d\r\n", SLE_UART_CLIENT_LOG, + service->start_hdl, service->end_hdl, service->uuid.len); + g_sle_uart_find_service_result.start_hdl = service->start_hdl; + g_sle_uart_find_service_result.end_hdl = service->end_hdl; + memcpy_s(&g_sle_uart_find_service_result.uuid, sizeof(sle_uuid_t), &service->uuid, sizeof(sle_uuid_t)); +} + +static void sle_uart_client_sample_find_property_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_property_result_t *property, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_find_property_cbk, client id: %d, conn id: %d, operate ind: %d, " + "descriptors count: %d status:%d property->handle %d\r\n", SLE_UART_CLIENT_LOG, + client_id, conn_id, property->operate_indication, + property->descriptors_count, status, property->handle); + g_sle_uart_send_param.handle = property->handle; + g_sle_uart_send_param.type = SSAP_PROPERTY_TYPE_VALUE; +} + +static void sle_uart_client_sample_find_structure_cmp_cbk(uint8_t client_id, uint16_t conn_id, + ssapc_find_structure_result_t *structure_result, + errcode_t status) +{ + unused(conn_id); + osal_printk("%s sle_uart_client_sample_find_structure_cmp_cbk,client id:%d status:%d type:%d uuid len:%d \r\n", + SLE_UART_CLIENT_LOG, client_id, status, structure_result->type, structure_result->uuid.len); +} + +static void sle_uart_client_sample_write_cfm_cb(uint8_t client_id, uint16_t conn_id, + ssapc_write_result_t *write_result, errcode_t status) +{ + osal_printk("%s sle_uart_client_sample_write_cfm_cb, conn_id:%d client id:%d status:%d handle:%02x type:%02x\r\n", + SLE_UART_CLIENT_LOG, conn_id, client_id, status, write_result->handle, write_result->type); +} + +static void sle_uart_client_sample_ssapc_cbk_register(ssapc_notification_callback notification_cb, + ssapc_notification_callback indication_cb) +{ + g_sle_uart_ssapc_cbk.exchange_info_cb = sle_uart_client_sample_exchange_info_cbk; + g_sle_uart_ssapc_cbk.find_structure_cb = sle_uart_client_sample_find_structure_cbk; + g_sle_uart_ssapc_cbk.ssapc_find_property_cbk = sle_uart_client_sample_find_property_cbk; + g_sle_uart_ssapc_cbk.find_structure_cmp_cb = sle_uart_client_sample_find_structure_cmp_cbk; + g_sle_uart_ssapc_cbk.write_cfm_cb = sle_uart_client_sample_write_cfm_cb; + g_sle_uart_ssapc_cbk.notification_cb = notification_cb; + g_sle_uart_ssapc_cbk.indication_cb = indication_cb; + ssapc_register_callbacks(&g_sle_uart_ssapc_cbk); +} + + +void sle_uart_client_init(ssapc_notification_callback notification_cb, ssapc_indication_callback indication_cb) +{ + (void)osal_msleep(1000); /* 延时5s,等待SLE初始化完毕 */ + osal_printk("[SLE Client] try enable.\r\n"); + sle_uart_client_sample_seek_cbk_register(); + sle_uart_client_sample_connect_cbk_register(); + sle_uart_client_sample_ssapc_cbk_register(notification_cb, indication_cb); + if (enable_sle() != ERRCODE_SUCC) { + osal_printk("[SLE Client] sle enbale fail !\r\n"); + } +} \ No newline at end of file diff --git a/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.h b/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.h new file mode 100644 index 0000000000000000000000000000000000000000..af0359355f1c05ceafb22a5f4bcb926b06cfa5cd --- /dev/null +++ b/vendor/hellokun/demo/sparklink_car_net/sle_central_screen/sle_uart_client/sle_uart_client.h @@ -0,0 +1,33 @@ +/** + * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved. + * + * Description: SLE UART sample of client. \n + * + * History: \n + * 2023-04-03, Create file. \n + */ +#ifndef SLE_UART_CLIENT_H +#define SLE_UART_CLIENT_H + +#include "sle_ssap_client.h" + +// 车载设备控制指令枚举 +enum MSG_TYPE{ + NONE_TYPE = 0x58, //88 + DOOR_CHANGE = 0x59, + TRUNK_CHANGE = 0x5A, + AIR_CONDITION_CHANGE = 0x5B, + LIGHT_COLOR_CHANGE = 0x5C, + ENVIROMENT_CHANGE = 0x5D, + OBU_ONLINE = 0x5E, + OBU_OFFLINE = 0x5F +}; + +void sle_uart_client_init(ssapc_notification_callback notification_cb, ssapc_indication_callback indication_cb); + +void sle_uart_start_scan(void); + +ssapc_write_param_t *get_g_sle_uart_send_param(void); +void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status); +void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status); +#endif \ No newline at end of file