8000 sync · esp32vn/esp32-snippets@27872f7 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 27872f7

Browse files
author
kolban
committed
sync
1 parent 9bb7508 commit 27872f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2240
-204
lines changed

cpp_utils/BLEServer.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* BLEServer.cpp
3+
*
4+
* Created on: Apr 16, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "sdkconfig.h"
9+
#if defined(CONFIG_BT_ENABLED)
10+
#include <esp_log.h>
11+
#include <bt.h>
12+
#include <esp_bt_main.h>
13+
#include <esp_gap_ble_api.h>
14+
#include <esp_gatts_api.h>
15+
#include "BLEServer.h"
16+
#include "BLEUtils.h"
17+
#include <string>
18+
#include <gatt_api.h>
19+
#include <unordered_set>
20+
21+
static char tag[] = "BLEServer";
22+
23+
extern "C" {
24+
char *espToString(esp_err_t value);
25+
}
26+
27+
static esp_attr_value_t gatts_demo_char1_val;
28+
static uint8_t char1_str[] = {0x11,0x22,0x33};
29+
30+
31+
BLEServer::BLEServer(uint16_t appId, std::string deviceName) {
32+
m_appId = appId;
33+
m_deviceName = deviceName;
34+
m_profile.service_id.is_primary = true;
35+
m_profile.service_id.id.inst_id = 0;
36+
m_profile.service_id.id.uuid.len = ESP_UUID_LEN_16;
37+
m_profile.service_id.id.uuid.uuid.uuid16 = 0x1234;
38+
39+
gatts_demo_char1_val.attr_max_len = 0x40;
40+
gatts_demo_char1_val.attr_len = sizeof(char1_str);
41+
gatts_demo_char1_val.attr_value = char1_str;
42+
43+
uint8_t test_service_uuid128[32] = {
44+
/* LSB <--------------------------------------------------------------------------------> MSB */
45+
//first uuid, 16bit, [12],[13] is the value
46+
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0x00, 0x00,
47+
//second uuid, 32bit, [12], [13], [14], [15] is the value
48+
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAB, 0xCD, 0xAB, 0xCD,
49+
};
50+
setUUID(test_service_uuid128);
51+
esp_ble_gatts_app_register(m_appId);
52+
}
53+
54+
BLEServer::~BLEServer() {
55+
// TODO Auto-generated destructor stub
56+
}
57+
58+
void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
59+
esp_ble_gatts_cb_param_t* param) {
60+
ESP_LOGD(tag, "BLEServer ... handling GATT Server event!");
61+
62+
switch(event) {
63+
case ESP_GATTS_REG_EVT: {
64+
m_profile.gatts_if = gatts_if;
65+
ESP_LOGD(tag, "Registering device name: %s", m_deviceName.c_str());
66+
esp_err_t errRc = esp_ble_gap_set_device_name(m_deviceName.c_str());
67+
if (errRc != ESP_OK) {
68+
ESP_LOGE(tag, "esp_ble_gap_set_device_name: rc=%d %s", errRc, espToString(errRc));
69+
return;
70+
}
71+
72+
m_adv_data.set_scan_rsp = false;
73+
m_adv_data.include_name = true;
74+
m_adv_data.include_txpower = true;
75+
m_adv_data.min_interval = 0x20;
76+
m_adv_data.max_interval = 0x40;
77+
m_adv_data.appearance = 0x00;
78+
m_adv_data.manufacturer_len = 0;
79+
m_adv_data.p_manufacturer_data = NULL;
80+
m_adv_data.service_data_len = 0;
81+
m_adv_data.p_service_data = NULL;
82+
m_adv_data.service_uuid_len = 32;
83+
m_adv_data.p_service_uuid = m_uuid;
84+
m_adv_data.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
85+
ESP_LOGD(tag, "Setting advertizing data");
86+
errRc = esp_ble_gap_config_adv_data(&m_adv_data);
87+
if (errRc != ESP_OK) {
88+
ESP_LOGE(tag, "esp_ble_gap_config_adv_data: rc=%d %s", errRc, espToString(errRc));
89+
return;
90+
}
91+
92+
ESP_LOGD(tag, "Creating service");
93+
errRc = esp_ble_gatts_create_service(gatts_if, &m_profile.service_id, 4);
94+
if (errRc != ESP_OK) {
95+
ESP_LOGE(tag, "esp_ble_gatts_create_service: rc=%d %s", errRc, espToString(errRc));
96+
return;
97+
}
98+
break;
99+
} // ESP_GATTS_REG_EVT
100+
101+
case ESP_GATTS_CREATE_EVT: {
102+
m_profile.service_handle = param->create.service_handle;
103+
m_profile.char_uuid.len = ESP_UUID_LEN_16;
104+
m_profile.char_uuid.uuid.uuid16 = 0x99AA;
105+
ESP_LOGD(tag, "Starting service");
106+
esp_err_t errRc = esp_ble_gatts_start_service(m_profile.service_handle);
107+
if (errRc != ESP_OK) {
108+
ESP_LOGE(tag, "esp_ble_gatts_start_service: rc=%d %s", errRc, espToString(errRc));
109+
return;
110+
}
111+
ESP_LOGD(tag, "Adding characteristic");
112+
errRc = esp_ble_gatts_add_char(
113+
m_profile.service_handle,
114+
&m_profile.char_uuid,
115+
(esp_gatt_perm_t)(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
116+
(esp_gatt_char_prop_t)(ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY),
117+
&gatts_demo_char1_val,
118+
NULL);
119+
if (errRc != ESP_OK) {
120+
ESP_LOGE(tag, "esp_ble_gatts_add_char: rc=%d %s", errRc, espToString(errRc));
121+
return;
122+
}
123+
break;
124+
} // ESP_GATTS_CREATE_EVT
125+
default:
126+
break;
127+
}
128+
} // handleGATTServerEvent
129+
130+
131+
void BLEServer::handleGAPEvent(esp_gap_ble_cb_event_t event,
132+
esp_ble_gap_cb_param_t* param) {
133+
ESP_LOGD(tag, "BLEServer ... handling GAP event!");
134+
switch(event) {
135+
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: {
136+
esp_ble_adv_params_t adv_params;
137+
adv_params.adv_int_min = 0x20;
138+
adv_params.adv_int_max = 0x40;
139+
adv_params.adv_type = ADV_TYPE_IND;
140+
adv_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
141+
adv_params.channel_map = ADV_CHNL_ALL;
142+
adv_params.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
143+
ESP_LOGD(tag, "Starting advertizing");
144+
esp_err_t errRc = esp_ble_gap_start_advertising(&adv_params);
145+
if (errRc != ESP_OK) {
146+
ESP_LOGE(tag, "esp_ble_gap_start_advertising: rc=%d %s", errRc, espToString(errRc));
147+
return;
148+
}
149+
break;
150+
}
151+
default:
152+
break;
153+
}
154+
} // handleGAPEvent
155+
156+
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEServer.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* BLEServer.h
3+
*
4+
* Created on: Apr 16, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_BLESERVER_H_
9+
#define COMPONENTS_CPP_UTILS_BLESERVER_H_
10+
#include <string>
11+
#include <string.h>
12+
#include <esp_gatts_api.h>
13+
14+
struct profile_inst {
15+
esp_gatts_cb_t gatts_cb;
16+
uint16_t gatts_if;
17+
uint16_t app_id;
18+
uint16_t conn_id;
19+
uint16_t service_handle;
20+
esp_gatt_srvc_id_t service_id;
21+
uint16_t char_handle;
22+
esp_bt_uuid_t char_uuid;
23+
esp_gatt_perm_t perm;
24+
esp_gatt_char_prop_t property;
25+
uint16_t descr_handle;
26+
esp_bt_uuid_t descr_uuid;
27+
};
28+
29+
class BLEServer {
30+
public:
31+
BLEServer(uint16_t appId, std::string deviceName);
32+
virtual ~BLEServer();
33+
void setDeviceName(std::string deviceName) {
34+
m_deviceName = deviceName;
35+
}
36+
void handleGATTServerEvent(esp_gatts_cb_event_t event,
37+
esp_gatt_if_t gatts_if,
38+
esp_ble_gatts_cb_param_t *param);
39+
void handleGAPEvent(
40+
esp_gap_ble_cb_event_t event,
41+
esp_ble_gap_cb_param_t *param);
42+
void setUUID(uint8_t uuid[32]) {
43+
memcpy(m_uuid, uuid, 32);
44+
}
45+
private:
46+
std::string m_deviceName;
47+
uint16_t m_appId;
48+
uint8_t m_uuid[32];
49+
esp_ble_adv_data_t m_adv_data;
50+
struct profile_inst m_profile;
51+
};
52+
53+
#endif /* COMPONENTS_CPP_UTILS_BLESERVER_H_ */

cpp_utils/Doxyfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ PROJECT_LOGO =
5858
# entered, it will be relative to the location where doxygen was started. If
5959
# left blank the current directory will be used.
6060

61-
OUTPUT_DIRECTORY =
61+
OUTPUT_DIRECTORY = /home/kolban/esp32/esptest/apps/workspace/esp32-snippets/cpp_utils/docs
6262

6363
# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
6464
# directories (in 2 levels) under the output directory of each output format and
@@ -1656,7 +1656,7 @@ EXTRA_SEARCH_MAPPINGS =
16561656
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
16571657
# The default value is: YES.
16581658

1659-
GENERATE_LATEX = YES
1659+
GENERATE_LATEX = NO
16601660

16611661
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
16621662
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
@@ -2089,7 +2089,8 @@ INCLUDE_FILE_PATTERNS =
20892089
# recursively expanded use the := operator instead of the = operator.
20902090
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
20912091

2092-
PREDEFINED = CONFIG_BT_ENABLED
2092+
PREDEFINED = CONFIG_MONGOOSE_PRESENT \
2093+
CONFIG_WIFI_ENABLED
20932094

20942095
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
20952096
# tag can be used to specify a list of macro names that should be expanded. The

cpp_utils/FATFS_VFS.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* FATFS.cpp
3+
*
4+
* Created on: May 20, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "FATFS_VFS.h"
9+
#include <esp_err.h>
10+
extern "C" {
11+
#include <esp_vfs_fat.h>
12+
}
13+
14+
/**
15+
* @brief Constructor.
16+
* @param [in] mountPath The path in the VFS where the FAT file system should be mounted.
17+
* @param [in] partitionName The name of the partition used to store the FAT file system.
18+
*/
19+
FATFS_VFS::FATFS_VFS(std::string mountPath, std::string partitionName) {
20+
m_mountPath = mountPath;
21+
m_partitionName = partitionName;
22+
m_maxFiles = 4;
23+
m_wl_handle = WL_INVALID_HANDLE;
24+
} // FATFS_VFS
25+
26+
27+
FATFS_VFS::~FATFS_VFS() {
28+
} // ~FATFS_VFS
29+
30+
31+
/**
32+
* @brief Mount the FAT file system into VFS.
33+
* The FAT file system found in the partition is mounted into the VFS.
34+
* @return N/A.
35+
*/
36+
void FATFS_VFS::mount() {
37+
esp_vfs_fat_mount_config_t mountConfig;
38+
mountConfig.max_files = m_maxFiles;
39+
mountConfig.format_if_mount_failed = true;
40+
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_mount(m_mountPath.c_str(), m_partitionName.c_str(), &mountConfig, &m_wl_handle));
41+
} // mount
42+
43+
44+
/**
45+
* @brief Set the allowable number of concurrently open files.
46+
* @param [in] maxFiles Number of concurrently open files.
47+
* @return N/A.
48+
*/
49+
void FATFS_VFS::setMaxFiles(int maxFiles) {
50+
m_maxFiles = maxFiles;
51+
} // setMaxFiles
52+
53+
54+
/**
55+
* @brief Unmount a previously mounted file system.
56+
* @return N/A.
57+
*/
58+
void FATFS_VFS::unmount() {
59+
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount(m_mountPath.c_str(), m_wl_handle));
60+
} // unmount

cpp_utils/FATFS_VFS.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* FATFS.h
3+
*
4+
* Created on: May 20, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_FATFS_VFS_H_
9+
#define COMPONENTS_CPP_UTILS_FATFS_VFS_H_
10+
#include <string>
11+
extern "C" {
12+
#include <esp_vfs_fat.h>
13+
}
14+
/**
15+
* @brief Provide access to the FAT file system on %SPI flash.
16+
*
17+
* A typical example would be:
18+
*
19+
* @code{.cpp}
20+
* FATFS_VFS *fs = new FATFS_VFS("/spiflash", "storage");
21+
* fs->mount();
22+
* // Perform file I/O
23+
* fs->unmount();
24+
* delete fs;
25+
* @endcode
26+
*
27+
*/
28+
class FATFS_VFS {
29+
public:
30+
FATFS_VFS(std::string mountPath, std::string partitionName);
31+
virtual ~FATFS_VFS();
32+
void mount();
33+
void setMaxFiles(int maxFiles);
34+
void unmount();
35+
private:
36+
wl_handle_t m_wl_handle;
37+
std::string m_mountPath;
38+
std::string m_partitionName;
39+
int m_maxFiles;
40+
};
41+
42+
#endif /* COMPONENTS_CPP_UTILS_FATFS_VFS_H_ */

0 commit comments

Comments
 (0)
0