Comunication with BLE is implemented.

This commit is contained in:
dxl
2023-03-14 16:15:06 +08:00
parent 5987d1c329
commit 3cd24ca1a0
5 changed files with 70 additions and 34 deletions
+20 -2
View File
@@ -561,6 +561,24 @@ static cmd_data_map_t m_data_cmd_map[] = {
};
/**
* @brief Auto select source to response
*
* @param resp data
*/
void auto_response_data(data_frame_tx_t* resp) {
// TODO Please select the reply source automatically according to the message source,
// and do not reply by checking the validity of the link layer by layer
if (is_usb_working()) {
usb_cdc_write(resp->buffer, resp->length);
} else if (is_nus_working()) {
nus_data_reponse(resp->buffer, resp->length);
} else {
NRF_LOG_ERROR("No connection valid found at response client.");
}
}
/**@brief Function for prcoess data frame(cmd)
*/
void on_data_frame_received(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) {
@@ -595,12 +613,12 @@ void on_data_frame_received(uint16_t cmd, uint16_t status, uint16_t length, uint
if (is_cmd_support) {
// check and response
if (response != NULL) {
usb_cdc_write(response->buffer, response->length);
auto_response_data(response);
}
} else {
// response cmd unsupport.
response = data_frame_make(cmd, STATUS_INVALID_CMD, 0, NULL);
usb_cdc_write(response->buffer, response->length);
auto_response_data(response);
NRF_LOG_INFO("Data frame cmd invalid: %d,", cmd);
}
}
+42 -32
View File
@@ -17,6 +17,7 @@
#include "syssleep.h"
#include "ble_main.h"
#include "dataframe.h"
#define NRF_LOG_MODULE_NAME ble_main
#include "nrf_log.h"
@@ -26,8 +27,6 @@ NRF_LOG_MODULE_REGISTER();
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define DEVICE_NAME "Nordic_UART" /**< Name of device. Will be included in the advertising data. */
#define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
@@ -52,7 +51,7 @@ static ble_uuid_t m_adv_uuids[] =
{
{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
};
bool g_is_ble_connected = false;
volatile bool g_is_ble_connected = false;
/**@brief Function for the GAP initialization.
@@ -68,9 +67,7 @@ static void gap_params_init(void)
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *) DEVICE_NAME,
strlen(DEVICE_NAME));
err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) DEVICE_NAME_STR, strlen(DEVICE_NAME_STR));
APP_ERROR_CHECK(err_code);
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
@@ -86,41 +83,54 @@ static void gap_params_init(void)
/**@brief Function for handling the data from the Nordic UART Service.
*
* @details This function will process the data received from the Nordic UART BLE Service and send
* it to the UART module.
* @details This function will process the data received from the Nordic UART BLE Service
*
* @param[in] p_evt Nordic UART Service event.
*/
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
// if (p_evt->type == BLE_NUS_EVT_RX_DATA)
// {
// uint32_t err_code;
// NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
// NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
// for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
// {
// do
// {
// // err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
// if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
// {
// NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
// APP_ERROR_CHECK(err_code);
// }
// } while (err_code == NRF_ERROR_BUSY);
// }
// if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
// {
// // while (app_uart_put('\n') == NRF_ERROR_BUSY);
// }
// }
if (p_evt->type == BLE_NUS_EVT_RX_DATA)
{
NRF_LOG_DEBUG("Received data from BLE NUS.");
NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
data_frame_receive((uint8_t*)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
}
}
/**@snippet [Handling the data received over BLE] */
void nus_data_reponse(uint8_t *p_data, uint16_t length) {
NRF_LOG_INFO("BLE nus service response data length: %d", length);
NRF_LOG_HEXDUMP_DEBUG(p_data, length);
ret_code_t err_code;
uint16_t remain = length;
uint16_t count = 0;
do {
remain = MIN(m_ble_nus_max_data_len, remain);
err_code = ble_nus_data_send(&m_nus, p_data + count, &remain, m_conn_handle);
// NRF_LOG_INFO("Data send length(amount): %d", remain);
if (err_code == NRF_SUCCESS) {
count += remain;
remain = length - count;
}
// NRF_LOG_INFO("Data send length(count): %d", count);
if (err_code == NRF_ERROR_BUSY) {
continue;
}
if ((err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND)) {
APP_ERROR_CHECK(err_code);
}
} while (count != length && g_is_ble_connected);
}
bool is_nus_working(void) {
return g_is_ble_connected;
}
/**@brief Function for handling Queued Write Module errors.
*
* @details A pointer to this function will be passed to each service which may need to inform the
+2
View File
@@ -3,5 +3,7 @@
void ble_slave_init(void);
void advertising_start(void);
void nus_data_reponse(uint8_t *p_data, uint16_t length);
bool is_nus_working(void);
#endif
+4
View File
@@ -181,3 +181,7 @@ int fputc(int ch, FILE *f){
return ch;
}
*/
bool is_usb_working(void) {
return g_usb_port_opened;
}
+2
View File
@@ -2,8 +2,10 @@
#define USB_MAIN_H
#include <stdint.h>
#include <stdbool.h>
void usb_cdc_init(void);
void usb_cdc_write(const void *p_buf, uint16_t length);
bool is_usb_working(void);
#endif