Skip to content
Snippets Groups Projects
Commit 5fba2cfc authored by Dzulfikar Ahmad  Samhan's avatar Dzulfikar Ahmad Samhan
Browse files

Feat/ble

parent f7cca539
Branches
1 merge request!3Feat/ble
Showing
with 198 additions and 7 deletions
......@@ -12,7 +12,7 @@ CONFIG_MAIN_STACK_SIZE=2048
CONFIG_HEAP_MEM_POOL_SIZE=512
CONFIG_THREAD_NAME=y
CONFIG_ASSERT=y
CONFIG_NO_OPTIMIZATIONS=y
CONFIG_NO_OPTIMIZATIONS=n
# Peripheral
# CONFIG_ADC=y
......@@ -31,6 +31,10 @@ CONFIG_ZBUS_LOG_LEVEL_INF=y
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
CONFIG_ZBUS_CHANNEL_NAME=y
#ble
CONFIG_BT=y
CONFIG_BT_OBSERVER=y
# PM
# CONFIG_PM=y
# CONFIG_PM_DEVICE=y
......
......@@ -5,6 +5,8 @@
#include "main-handler.h"
#include "debug-handler.h"
#include "driver-hook.h"
#include "ble-scanner.h"
#include "device-parser.h"
#include "utilities.h"
......@@ -12,6 +14,7 @@ LOG_MODULE_REGISTER(main);
DriverMessage_t defaultDriverMsg;
StateMessage_t defaultStateMsg;
BleData_t defaultBleData;
ZBUS_CHAN_DEFINE(driverRequestChannel, /* Name */
DriverMessage_t, /* Message type */
......@@ -29,6 +32,14 @@ ZBUS_CHAN_DEFINE(driverResponseChannel, /* Name */
ZBUS_MSG_INIT(defaultDriverMsg)
);
ZBUS_CHAN_DEFINE(bleScanChannel, /* Name */
BleData_t, /* Message type */
NULL, /* Validator */
NULL, /* User data */
ZBUS_OBSERVERS_EMPTY, /* observers */
ZBUS_MSG_INIT(defaultBleData)
);
ZBUS_CHAN_DEFINE(stateChannel, /* Name */
StateMessage_t, /* Message type */
NULL, /* Validator */
......@@ -40,8 +51,10 @@ ZBUS_CHAN_DEFINE(stateChannel, /* Name */
int main()
{
MainHandler mainHandler;
DriverHook DriverHook;
DriverHook driverHook;
DebugHandler debugHanddler;
BleScanner bleScanner;
DeviceParser deviceParser;
while(true) {
// LOG_INF("Hello World! run app\n");
......
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
class BleScanner {
public:
/** Constructor.
*
**/
BleScanner();
/** Destructor.
*
**/
~BleScanner();
static void DriverRequestListener(const struct zbus_channel *chan);
private:
static void BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad);
};
\ No newline at end of file
......@@ -19,6 +19,7 @@ public:
static void DriverResponseListener(const struct zbus_channel *chan);
static void StateListener(const struct zbus_channel *chan);
static void BleListener(const struct zbus_channel *chan);
private:
static void DebugThread(DebugHandler *context);
......
#include <zephyr/kernel.h>
#include "utilities.h"
typedef struct BleSensorData {
uint8_t tempSensor;
} BleSensorData_t;
class DeviceParser {
public:
/** Constructor.
*
**/
DeviceParser();
/** Destructor.
*
**/
~DeviceParser();
static void BleScanListener(const struct zbus_channel *chan);
bool BleDeviceParser(const BleData_t *data, BleSensorData_t *parsedData);
private:
};
\ No newline at end of file
......@@ -7,6 +7,7 @@ typedef enum DriverNumb{
SENSOR_1,
SENSOR_2,
SENSOR_3,
BLUETOOTH,
ACTUATOR_1,
} DriverNumb_e;
......@@ -24,4 +25,9 @@ typedef struct DriverMessage {
typedef struct StateMessage {
StateFlag_e flag;
std::vector<uint8_t> value;
} StateMessage_t;
\ No newline at end of file
} StateMessage_t;
typedef struct BleData {
std::string address;
std::vector<uint8_t> data;
} BleData_t;
\ No newline at end of file
add_subdirectory(driver-hook)
add_subdirectory(handler)
\ No newline at end of file
add_subdirectory(handler)
add_subdirectory(ble-scanner)
\ No newline at end of file
zephyr_library_sources(src/ble-scanner.cpp)
\ No newline at end of file
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
#include <stdio.h>
#include <string.h>
#include "utilities.h"
#include "ble-scanner.h"
LOG_MODULE_REGISTER(BleScanner);
ZBUS_CHAN_DECLARE(driverRequestChannel, bleScanChannel);
static BleScanner *context;
static struct bt_le_scan_param scan_param = {
.type = BT_LE_SCAN_TYPE_PASSIVE,
.options = BT_LE_SCAN_OPT_NONE,
.interval = BT_GAP_SCAN_FAST_INTERVAL,
.window = BT_GAP_SCAN_FAST_WINDOW,
};
// ZBUS_LISTENER_DEFINE(bleScanListener, context->DriverRequestListener);
BleScanner::BleScanner(){
context = this;
int err = bt_enable(NULL);
LOG_INF("start bt");
if (err != 0) {
printk("Bluetooth init failed (err %d)\n", err);
return;
}
// zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT);
//TODO: remove when bus communication has fully established
bt_le_scan_start(&scan_param, BleDevicefound);
}
void BleScanner::BleDevicefound(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad){
char addr_str[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
BleData_t sentMessage = {
.address = std::string(addr_str, sizeof(addr_str)),
.data = std::vector<uint8_t>(ad->data, ad->data + ad->len)
};
zbus_chan_pub(&bleScanChannel, &sentMessage, K_MSEC(200));
}
// void DriverRequestListener(const struct zbus_channel *chan){
// const DriverMessage_t *msg;
// if (&driverRequestChannel == chan) {
// msg = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access
// if(msg->number == BLUETOOTH){
// if(msg->value[0]){
// bt_le_scan_start(&scan_param, bleDevicefound);
// }
// }
// }
// else{
// return;
// }
// }
add_subdirectory(main-handler)
add_subdirectory(debug-handler)
\ No newline at end of file
add_subdirectory(debug-handler)
add_subdirectory(device-parser)
\ No newline at end of file
......@@ -12,7 +12,7 @@
#define DEBUG_THREAD_PRIORITY 5
LOG_MODULE_REGISTER(Debug_Handler);
ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel);
ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel, bleScanChannel);
K_THREAD_STACK_DEFINE(debugThreadStack, DEBUG_THREAD_STACK_SIZE);
......@@ -23,6 +23,7 @@ static DebugHandler *context;
ZBUS_LISTENER_DEFINE(debugDriverListener, context->DriverResponseListener);
ZBUS_LISTENER_DEFINE(stateListener, context->StateListener);
ZBUS_LISTENER_DEFINE(bleListener, context->BleListener);
DebugHandler::DebugHandler(){
context = this;
......@@ -33,6 +34,7 @@ DebugHandler::DebugHandler(){
k_thread_suspend(debugThreadId);
zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT);
zbus_chan_add_obs(&stateChannel, &stateListener, K_NO_WAIT);
zbus_chan_add_obs(&bleScanChannel, &bleListener, K_NO_WAIT);
}
void DebugHandler::Init(){
......@@ -61,4 +63,13 @@ void DebugHandler::StateListener(const struct zbus_channel *chan){
state = (StateMessage_t *)zbus_chan_const_msg(chan); // Direct message access
LOG_INF("From listener -> state =%d, value 0=%d", state->flag, state->value[0]);
}
}
void DebugHandler::BleListener(const struct zbus_channel *chan){
const BleData_t *ble;
if (&bleScanChannel == chan) {
ble = (BleData_t *)zbus_chan_const_msg(chan); // Direct message access
LOG_INF("Ble -> address =%s", ble->address.c_str());
LOG_HEXDUMP_INF(ble->data.data(), ble->data.size(), "data raw");
}
}
\ No newline at end of file
zephyr_library_sources(src/device-parser.cpp)
\ No newline at end of file
#include "device-parser.h"
#include <zephyr/logging/log.h>
#include <zephyr/zbus/zbus.h>
LOG_MODULE_REGISTER(DeviceParser);
ZBUS_CHAN_DECLARE(bleScanChannel);
static DeviceParser *context;
ZBUS_LISTENER_DEFINE(scanListener, context->BleScanListener);
DeviceParser::DeviceParser(){
context = this;
zbus_chan_add_obs(&bleScanChannel, &scanListener, K_NO_WAIT);
}
void DeviceParser::BleScanListener(const struct zbus_channel *chan){
const BleData_t *msg;
if (&bleScanChannel == chan) {
msg = (BleData_t *)zbus_chan_const_msg(chan); // Direct message access
BleSensorData_t parsedData;
if(context->BleDeviceParser(msg, &parsedData)){
LOG_INF("Device %s has data %d", msg->address.c_str(), parsedData.tempSensor);
}
}
else{
return;
}
}
bool DeviceParser::BleDeviceParser(const BleData_t *data, BleSensorData_t *parsedData){
if(data->data.size() >= 8){
if(data->data[5] == 0x1a && data->data[6] == 0x18){
parsedData->tempSensor = data->data[7];
return true;
}
}
return false;
}
\ No newline at end of file
......@@ -40,7 +40,7 @@ MainHandler::MainHandler(){
k_sem_init(&responseWait, 0, 1);
// zbus_chan_add_obs(&driverResponseChannel, &sensorSubs, K_MSEC(200));
zbus_chan_add_obs(&driverResponseChannel, &driverListener, K_NO_WAIT);
// zbus_chan_add_obs(&driverResponseChannel, &driverListener, K_NO_WAIT);
}
void MainHandler::Init(){
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment