Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoincorrect committed Mar 17, 2021
1 parent fb821c8 commit e422cf7
Show file tree
Hide file tree
Showing 14 changed files with 489 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
<folder Name="Sensors">
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_sampling.c" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_sampling.h" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_buffer.c" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_buffer.h" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_handle.c" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensor_handle.h" />
<file file_name="$(PROJ_ROOT)/Src/Sensors/sensors.h" />
Expand Down
123 changes: 68 additions & 55 deletions Embedded_system/smart_mask/src/ble/app_ble.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @file
* @brief app_ble (Bluetooth Application): Module that manages all the
* BLE related tasks besides services (that are managed by their specific
* modules)
*/

/*************************
* Includes
************************/
Expand Down Expand Up @@ -79,51 +86,19 @@ static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
// Buffer for storing an encoded scan data
static uint8_t m_enc_scan_response_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
/**@brief Struct that contains pointers to the encoded advertising data. */
/**
* @brief Struct that contains pointers to the encoded advertising data. */
static ble_gap_adv_data_t m_adv_data = {
.adv_data = {.p_data = m_enc_advdata, .len = BLE_GAP_ADV_SET_DATA_SIZE_MAX},
.scan_rsp_data = {.p_data = m_enc_scan_response_data,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX}};

/*************************
* Function Prototypes
* Static Functions
************************/

static void gap_params_init(void);
static void gatt_init(void);
static void advertising_init(void);
static void nrf_qwr_error_handler(uint32_t nrf_error);
static void output_write_handler(
uint16_t conn_handle, ble_sms_t * p_sms, uint8_t output_state);
static void services_init(void);
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt);
static void conn_params_error_handler(uint32_t nrf_error);
static void conn_params_init(void);
static void advertising_start(void);
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
static void ble_stack_init(void);

/*************************
* Function Definitions
************************/

ret_code_t app_ble_init(app_ble_conf_t * app_ble_conf)
{
m_app_ble_conf = app_ble_conf;
app_ble_conf->ble_conn_handle = &m_conn_handle;
app_ble_conf->ble_lbs = &m_lbs;
app_ble_conf->ble_sms = &m_sms;
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
advertising_start();
}


/**@brief Function for the GAP initialization.
/**
* @brief Function for the GAP initialization.
* @details This function sets up all the necessary GAP (Generic Access Profile)
* parameters of the device including the device name, appearance, and
* the preferred connection parameters.
Expand Down Expand Up @@ -152,7 +127,8 @@ static void gap_params_init(void)
}


/**@brief Function for initializing the GATT module.
/**
* @brief Function for initializing the GATT module.
*/
static void gatt_init(void)
{
Expand All @@ -173,7 +149,8 @@ static void gatt_init(void)
}


/**@brief Function for initializing the Advertising functionality.
/**
* @brief Function for initializing the Advertising functionality.
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack when starting
* advertising.
Expand Down Expand Up @@ -225,9 +202,11 @@ static void advertising_init(void)
}


/**@brief Function for handling Queued Write Module errors.
/**
* @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 application about an error.
*
* @param[in] nrf_error Error code containing information about what went
* wrong.
*/
Expand All @@ -237,7 +216,8 @@ static void nrf_qwr_error_handler(uint32_t nrf_error)
}


/**@brief Function for initializing services that will be used by the
/**
* @brief Function for initializing services that will be used by the
* application.
*/
static void services_init(void)
Expand Down Expand Up @@ -266,12 +246,14 @@ static void services_init(void)
}


/**@brief Function for handling the Connection Parameters Module.
/**
* @brief Function for handling the Connection Parameters Module.
* @details This function will be called for all events in the Connection
* Parameters Module that are passed to the application.
* @note All this function does is to disconnect. This could have been done by
* simply setting the disconnect_on_fail config parameter, but instead we use
* the event handler mechanism to demonstrate its use.
*
* @param[in] p_evt Event received from the Connection Parameters Module.
*/
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
Expand All @@ -287,7 +269,9 @@ static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
}


/**@brief Function for handling a Connection Parameters error.
/**
* @brief Function for handling a Connection Parameters error.
*
* @param[in] nrf_error Error code containing information about what went
* wrong.
*/
Expand All @@ -297,7 +281,8 @@ static void conn_params_error_handler(uint32_t nrf_error)
}


/**@brief Function for initializing the Connection Parameters module.
/**
* @brief Function for initializing the Connection Parameters module.
*/
static void conn_params_init(void)
{
Expand All @@ -320,7 +305,8 @@ static void conn_params_init(void)
}


/**@brief Function for starting advertising.
/**
* @brief Function for starting advertising.
*/
static void advertising_start(void)
{
Expand All @@ -334,7 +320,9 @@ static void advertising_start(void)
}


/**@brief Function for handling BLE events.
/**
* @brief Function for handling BLE events.
*
* @param[in] p_ble_evt Bluetooth stack event.
* @param[in] p_context Unused.
*/
Expand Down Expand Up @@ -414,15 +402,8 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
}
}

bool is_connected(void)
{
if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
return true;
else
return false;
}

/**@brief Function for initializing the BLE stack.
/**
* @brief Function for initializing the BLE stack.
*
* @details Initializes the SoftDevice and the BLE event interrupt.
*/
Expand All @@ -442,4 +423,36 @@ static void ble_stack_init(void)
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(
m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

/*************************
* Public Functions
************************/

ret_code_t app_ble_init(app_ble_conf_t * app_ble_conf)
{
m_app_ble_conf = app_ble_conf;
app_ble_conf->ble_conn_handle = &m_conn_handle;
app_ble_conf->ble_lbs = &m_lbs;
app_ble_conf->ble_sms = &m_sms;
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
advertising_start();
}

/**
* @brief Function to check whether the bluetooth state is connected.
*
* @retval true if bluetooth is connecter, false otherwise
*/
bool app_ble_is_connected(void)
{
if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
return true;
else
return false;
}
3 changes: 1 addition & 2 deletions Embedded_system/smart_mask/src/ble/app_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

typedef struct
{
// ble_lbs_led_write_handler_t led_write_handler;
ble_sms_sensor_ctrl_write_cb sensor_ctrl_write;
uint16_t * ble_conn_handle;
ble_sms_t * ble_sms;
Expand All @@ -16,6 +15,6 @@ typedef struct

ret_code_t app_ble_init(app_ble_conf_t * app_ble_conf);

bool is_connected(void);
bool app_ble_is_connected(void);

#endif
78 changes: 71 additions & 7 deletions Embedded_system/smart_mask/src/ble/services/ble_sms.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
// BLE SENSORS MEASUREMENT SERVICE
/**
* @file
* @brief ble_sms (BLE SENSORS MEASUREMENT SERVICE): Module that manages
* the sensor measurements bluetooth low energy service. take care of
* the sensor data transfer and sensor control.
*/

/*************************
* Includes
************************/

#include "ble_sms.h"
#include "app_error.h"
Expand All @@ -7,7 +17,12 @@
#include "sdk_common.h"
#include "sensor_handle.h"

/**@brief Function for handling the Write event.
/*************************
* Static Functions
************************/

/**
* @brief Function for handling the Write event.
*
* @param[in] p_sms LED Button Service structure.
* @param[in] p_ble_evt Event received from the BLE stack.
Expand Down Expand Up @@ -39,7 +54,15 @@ static uint32_t on_write(ble_sms_t * p_sms, ble_evt_t const * p_ble_evt)
return NRF_SUCCESS;
}


/**
* @brief Function for handling the application's BLE stack events.
*
* @details This function handles all events from the BLE stack that are of
* interest to the Sensor Measurement Service.
*
* @param[in] p_ble_evt Event received from the BLE stack.
* @param[in] p_context Sensor Measurement Service structure.
*/
void ble_sms_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
ble_sms_t * p_sms = (ble_sms_t *)p_context;
Expand All @@ -54,7 +77,16 @@ void ble_sms_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
}
}


/**
* @brief Function for sending a sensor notification.
*
* @param[in] conn_handle Handle of the peripheral connection to which the
* sensor state notification will be sent.
* @param[in] p_sms Sensor Measurement Service Button Service structure.
* @param[in] value Pointer to sensor values.
*
* @retval NRF_SUCCESS on success, otherwise an error code is returned
*/
uint32_t ble_sms_on_sensors_update(
uint16_t conn_handle, ble_sms_t * p_sms, sensor_t sensor)
{
Expand All @@ -69,7 +101,7 @@ uint32_t ble_sms_on_sensors_update(


uint16_t amount = SENSOR_VAL_AMOUNT_NOTIF;
ret = get_sensor_values(sensor, vals, amount);
ret = sensor_handle_get_values(sensor, vals, amount);
APP_ERROR_CHECK(ret);

params.p_data = (uint8_t *)vals;
Expand Down Expand Up @@ -98,7 +130,16 @@ uint32_t ble_sms_on_sensors_update(
// *(params.p_data + 19) << 8 | *(params.p_data + 18));
}


/**
* @brief Add a sensor value characteristic
*
* @param[in] p_sms pointer to sms service
* @param[in] uuid uuid to be used by the new characteristic
* @param[in] p_char_handle pointer to the function that will handle ble
* characteristic event
*
* @retval NRF_SUCCESS on success, otherwise an error code is returned
*/
static uint32_t add_sensor_vals_char(
ble_sms_t * p_sms, uint16_t uuid, ble_gatts_char_handles_t * p_char_handle)
{
Expand Down Expand Up @@ -126,7 +167,20 @@ static uint32_t add_sensor_vals_char(
return NRF_SUCCESS;
}


/*************************
* Public Functions
************************/

/**
* @brief Add a sensor control characteristic
*
* @param[in] p_sms pointer to sms service
* @param[in] uuid uuid to be used by the new characteristic
* @param[in] p_char_handle pointer to the function that will handle ble
* characteristic event
*
* @retval NRF_SUCCESS on success, otherwise an error code is returned
*/
static uint32_t add_sensor_ctrl_char(ble_sms_t * p_sms, uint8_t uuid,
ble_gatts_char_handles_t * p_char_handle, sensor_ctrl_t * sensor_ctrl)
{
Expand All @@ -151,7 +205,17 @@ static uint32_t add_sensor_ctrl_char(ble_sms_t * p_sms, uint8_t uuid,
return err_code;
}


/**
* @brief Function for initializing the Sensor Measurement Service.
*
* @param[out] p_sms Sensor Measurement Service structure. This structure
* must be supplied by the application. It is initialized by this function and
* will later be used to identify this particular service instance.
* @param[in] p_sms_init Information needed to initialize the service.
*
* @retval NRF_SUCCESS If the service was initialized successfully. Otherwise,
* an error code is returned.
*/
uint32_t ble_sms_init(ble_sms_t * p_sms, const ble_sms_init_t * p_sms_init)
{
uint32_t err_code;
Expand Down
Loading

0 comments on commit e422cf7

Please sign in to comment.