10000 Merge pull request #1 from nkolban/master · Netoperz/esp32-snippets@35c8cb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35c8cb7

Browse files
authored
Merge pull request nkolban#1 from nkolban/master
update upstream
2 parents 8dbdaa5 + f50bcb0 commit 35c8cb7

File tree

23 files changed

+1290
-24
lines changed

23 files changed

+1290
-24
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# FastFlash
2+
As we build ESP32 applications, we typically perform a compile, flash, test loop cycle. We compile an app, we flash the ESP32 with that app and then we test whether it works. We perform these actions over and over again. When we look at the time taken in each step, we see there is compile time on our PC and then the time taken to flash the PC. This story talks about the time taken to flash the PC.
3+
4+
The ESP32 is typically configured to flash at 115200 kbps. This is 115200 bits per second. If we think that a typical ESP32 application is 800KBytes then this requires a transmission of:
5+
6+
800000 * 9 = 7.2 million bits = 62.5 seconds
7+
8+
we can increase our baud rate up to 921600 = 7.8 seconds

cpp_utils/AWS.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,129 @@ AWS::AWS() {
1919
AWS::~AWS() {
2020
}
2121

22+
/**
23+
* Convert an AWS IoT error code to a string representation.
24+
* @param err The error code to be mapped.
25+
* @return A string representation of the error code.
26+
*/
27+
/* static */ std::string AWS::errorToString(IoT_Error_t err) {
28+
switch(err) {
29+
case NETWORK_PHYSICAL_LAYER_CONNECTED :
30+
return "NETWORK_PHYSICAL_LAYER_CONNECTED";
31+
case NETWORK_MANUALLY_DISCONNECTED :
32+
return "NETWORK_MANUALLY_DISCONNECTED";
33+
case NETWORK_ATTEMPTING_RECONNECT:
34+
return "NETWORK_ATTEMPTING_RECONNECT";
35+
case NETWORK_RECONNECTED:
36+
return "NETWORK_RECONNECTED";
37+
case MQTT_NOTHING_TO_READ :
38+
return "MQTT_NOTHING_TO_READ";
39+
case MQTT_CONNACK_CONNECTION_ACCEPTED:
40+
return "MQTT_CONNACK_CONNECTION_ACCEPTED";
41+
case SUCCESS :
42+
return "SUCCESS";
43+
case FAILURE:
44+
return "FAILURE";
45+
case NULL_VALUE_ERROR :
46+
return "NULL_VALUE_ERROR";
47+
case TCP_CONNECTION_ERROR :
48+
return "TCP_CONNECTION_ERROR";
49+
case SSL_CONNECTION_ERROR:
50+
return "SSL_CONNECTION_ERROR";
51+
case TCP_SETUP_ERROR :
52+
return "TCP_SETUP_ERROR";
53+
case NETWORK_SSL_CONNECT_TIMEOUT_ERROR :
54+
return "NETWORK_SSL_CONNECT_TIMEOUT_ERROR";
55+
case NETWORK_SSL_WRITE_ERROR :
56+
return "NETWORK_SSL_WRITE_ERROR";
57+
case NETWORK_SSL_INIT_ERROR :
58+
return "NETWORK_SSL_INIT_ERROR";
59+
case NETWORK_SSL_CERT_ERROR :
60+
return "NETWORK_SSL_CERT_ERROR";
61+
case NETWORK_SSL_WRITE_TIMEOUT_ERROR :
62+
return "NETWORK_SSL_WRITE_TIMEOUT_ERROR";
63+
case NETWORK_SSL_READ_TIMEOUT_ERROR :
64+
return "NETWORK_SSL_READ_TIMEOUT_ERROR";
65+
case NETWORK_SSL_READ_ERROR :
66+
return "NETWORK_SSL_READ_ERROR";
67+
case NETWORK_DISCONNECTED_ERROR :
68+
return "NETWORK_DISCONNECTED_ERROR";
69+
case NETWORK_RECONNECT_TIMED_OUT_ERROR:
70+
return "NETWORK_RECONNECT_TIMED_OUT_ERROR";
71+
case NETWORK_ALREADY_CONNECTED_ERROR :
72+
return "NETWORK_ALREADY_CONNECTED_ERROR";
73+
case NETWORK_MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED :
74+
return "NETWORK_MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED";
75+
case NETWORK_SSL_UNKNOWN_ERROR :
76+
return "NETWORK_SSL_UNKNOWN_ERROR";
77+
case NETWORK_PHYSICAL_LAYER_DISCONNECTED :
78+
return "NETWORK_PHYSICAL_LAYER_DISCONNECTED";
79+
case NETWORK_X509_ROOT_CRT_PARSE_ERROR :
80+
return "NETWORK_X509_ROOT_CRT_PARSE_ERROR";
81+
case NETWORK_X509_DEVICE_CRT_PARSE_ERROR :
82+
return "NETWORK_X509_DEVICE_CRT_PARSE_ERROR";
83+
case NETWORK_PK_PRIVATE_KEY_PARSE_ERROR :
84+
return "NETWORK_PK_PRIVATE_KEY_PARSE_ERROR";
85+
case NETWORK_ERR_NET_SOCKET_FAILED :
86+
return "NETWORK_ERR_NET_SOCKET_FAILED";
87+
case NETWORK_ERR_NET_UNKNOWN_HOST :
88+
return "NETWORK_ERR_NET_UNKNOWN_HOST";
89+
case NETWORK_ERR_NET_CONNECT_FAILED :
90+
return "NETWORK_ERR_NET_CONNECT_FAILED";
91+
case NETWORK_SSL_NOTHING_TO_READ :
92+
return "NETWORK_SSL_NOTHING_TO_READ";
93+
case MQTT_CONNECTION_ERROR :
94+
return "MQTT_CONNECTION_ERROR";
95+
case MQTT_CONNECT_TIMEOUT_ERROR :
96+
return "MQTT_CONNECT_TIMEOUT_ERROR";
97+
case MQTT_REQUEST_TIMEOUT_ERROR:
98+
return "MQTT_REQUEST_TIMEOUT_ERROR";
99+
case MQTT_UNEXPECTED_CLIENT_STATE_ERROR :
100+
return "MQTT_UNEXPECTED_CLIENT_STATE_ERROR";
101+
case MQTT_CLIENT_NOT_IDLE_ERROR :
102+
return "MQTT_CLIENT_NOT_IDLE_ERROR";
103+
case MQTT_RX_MESSAGE_PACKET_TYPE_INVALID_ERROR :
104+
return "MQTT_RX_MESSAGE_PACKET_TYPE_INVALID_ERROR";
105+
case MQTT_RX_BUFFER_TOO_SHORT_ERROR :
106+
return "MQTT_RX_BUFFER_TOO_SHORT_ERROR";
107+
case MQTT_TX_BUFFER_TOO_SHORT_ERROR :
108+
return "MQTT_TX_BUFFER_TOO_SHORT_ERROR";
109+
case MQTT_MAX_SUBSCRIPTIONS_REACHED_ERROR :
110+
return "MQTT_MAX_SUBSCRIPTIONS_REACHED_ERROR";
111+
case MQTT_DECODE_REMAINING_LENGTH_ERROR :
112+
return "MQTT_DECODE_REMAINING_LENGTH_ERROR";
113+
case MQTT_CONNACK_UNKNOWN_ERROR :
114+
return "MQTT_CONNACK_UNKNOWN_ERROR";
115+
case MQTT_CONNACK_UNACCEPTABLE_PROTOCOL_VERSION_ERROR :
116+
return "MQTT_CONNACK_UNACCEPTABLE_PROTOCOL_VERSION_ERROR";
117+
case MQTT_CONNACK_IDENTIFIER_REJECTED_ERROR:
118+
return "MQTT_CONNACK_IDENTIFIER_REJECTED_ERROR";
119+
case MQTT_CONNACK_SERVER_UNAVAILABLE_ERROR :
120+
return "MQTT_CONNACK_SERVER_UNAVAILABLE_ERROR";
121+
case MQTT_CONNACK_BAD_USERDATA_ERROR:
122+
return "MQTT_CONNACK_BAD_USERDATA_ERROR";
123+
case MQTT_CONNACK_NOT_AUTHORIZED_ERROR :
124+
return "MQTT_CONNACK_NOT_AUTHORIZED_ERROR";
125+
case JSON_PARSE_ERROR :
126+
return "JSON_PARSE_ERROR";
127+
case SHADOW_WAIT_FOR_PUBLISH :
128+
return "SHADOW_WAIT_FOR_PUBLISH";
129+
case SHADOW_JSON_BUFFER_TRUNCATED :
130+
return "SHADOW_JSON_BUFFER_TRUNCATED";
131+
case SHADOW_JSON_ERROR :
132+
return "SHADOW_JSON_ERROR";
133+
case MUTEX_INIT_ERROR :
134+
return "MUTEX_INIT_ERROR";
135+
case MUTEX_LOCK_ERROR:
136+
return "MUTEX_LOCK_ERROR";
137+
case MUTEX_UNLOCK_ERROR :
138+
return "MUTEX_UNLOCK_ERROR";
139+
case MUTEX_DESTROY_ERROR :
140+
return "MUTEX_DESTROY_ERROR";
141+
default:
142+
return "Unknown error!";
143+
}
144+
} // AWS#errorToString
22145

23146
/**
24147
* @brief Connect to the AWS IoT service.

cpp_utils/AWS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AWS {
2424

2525
void connect(std::string clientId);
2626
void disconnect();
27+
static std::string errorToString(IoT_Error_t err); // Convert an AWS IoT error code to a string representation.
2728
void init(std::string host=CONFIG_AWS_IOT_MQTT_HOST, uint16_t port=CONFIG_AWS_IOT_MQTT_PORT);
2829
void publish(std::string topic, std::string payload, QoS qos = QOS0);
2930
void subscribe(std::string topic);

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ void BLERemoteCharacteristic::gattClientEventHandler(
204204
// and unlock the semaphore to ensure that the requestor of the data can continue.
205205
if (evtParam->read.status == ESP_GATT_OK) {
206206
m_value = std::string((char*)evtParam->read.value, evtParam->read.value_len);
207+
if(m_rawData != nullptr)
208+
free(m_rawData);
209+
210+
m_rawData = (uint8_t*) calloc(evtParam->read.value_len, sizeof(uint8_t));
211+
memcpy(m_rawData, evtParam->read.value, evtParam->read.value_len);
207212
} else {
208213
m_value = "";
209214
}
@@ -475,7 +480,7 @@ void BLERemoteCharacteristic::registerForNotify(
475480
BLERemoteCharacteristic* pBLERemoteCharacteristic,
476481
uint8_t* pData,
477482
size_t length,
478-
bool isNotify)) {
483+
bool isNotify), bool notifications) {
479484
ESP_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str());
480485

481486
m_notifyCallback = notifyCallback; // Save the notification callback.
@@ -492,6 +497,12 @@ void BLERemoteCharacteristic::registerForNotify(
492497
if (errRc != ESP_OK) {
493498
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
494499
}
500+
501+
uint8_t val[] = {0x01, 0x00};
502+
if(!notifications)
503+
val[0] = 0x02;
504+
BLERemoteDescriptor *desc = getDescriptor(BLEUUID("0x2902"));
505+
desc->writeValue(val, 2);
495506
} // End Register
496507
else { // If we weren't passed a callback function, then this is an unregistration.
497508
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
@@ -503,6 +514,10 @@ void BLERemoteCharacteristic::registerForNotify(
503514
if (errRc != ESP_OK) {
504515
ESP_LOGE(LOG_TAG, "esp_ble_gattc_unregister_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
505516
}
517+
518+
uint8_t val[] = {0x00, 0x00};
519+
BLERemoteDescriptor *desc = getDescriptor(BLEUUID("0x2902"));
520+
desc->writeValue(val, 2);
506521
} // End Unregister
507522

508523
m_semaphoreRegForNotifyEvt.wait("registerForNotify");
@@ -603,4 +618,12 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
603618
writeValue(std::string((char *)data, length), response);
604619
} // writeValue
605620

621+
/**
622+
* @brief Read raw data from remote characteristic as hex bytes
623+
* @return return pointer data read
624+
*/
625+
uint8_t* BLERemoteCharacteristic::readRawData() {
626+
return m_rawData;
627+
}
628+
606629
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLERemoteCharacteristic.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class BLERemoteCharacteristic {
4444
uint8_t readUInt8(void);
4545
uint16_t readUInt16(void);
4646
uint32_t readUInt32(void);
47-
void registerForNotify(void (*notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify));
47+
void registerForNotify(void (*notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify), bool notifications = true);
4848
void writeValue(uint8_t* data, size_t length, bool response = false);
4949
void writeValue(std::string newValue, bool response = false);
5050
void writeValue(uint8_t newValue, bool response = false);
5151
std::string toString(void);
52+
uint8_t* readRawData();
5253

5354
private:
5455
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
@@ -76,6 +77,7 @@ class BLERemoteCharacteristic {
7677
FreeRTOS::Semaphore m_semaphoreRegForNotifyEvt = FreeRTOS::Semaphore("RegForNotifyEvt");
7778
FreeRTOS::Semaphore m_semaphoreWriteCharEvt = FreeRTOS::Semaphore("WriteCharEvt");
7879
std::string m_value;
80+
uint8_t *m_rawData;
7981
void (*m_notifyCallback)(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
8082

8183
// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.

cpp_utils/BLERemoteDescriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void BLERemoteDescriptor::writeValue(
148148
getHandle(),
149149
length, // Data length
150150
data, // Data
151-
ESP_GATT_WRITE_TYPE_NO_RSP,
151+
response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP,
152152
ESP_GATT_AUTH_REQ_NONE
153153
);
154154
if (errRc != ESP_OK) {

cpp_utils/BLESecurity.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ class BLESecurityCallbacks {
5151
* It requires that our device is capable to display this code to end user
5252
* @param
5353
*/
54-
virtual void onPassKeyNotify(uint32_t pass_key);
54+
virtual void onPassKeyNotify(uint32_t pass_key) = 0;
5555

5656
/**
5757
* @brief Here we can make decision if we want to let negotiate authorization with peer device or not
5858
* return Return true if we accept this peer device request
5959
*/
6060

61-
virtual bool onSecurityRequest();
61+
virtual bool onSecurityRequest() = 0 ;
6262
/**
6363
* Provide us information when authentication process is completed
6464
*/
65-
virtual void onAuthenticationComplete(esp_ble_auth_cmpl_t);
65+
virtual void onAuthenticationComplete(esp_ble_auth_cmpl_t) = 0;
6666

67-
virtual bool onConfirmPIN(uint32_t pin);
67+
virtual bool onConfirmPIN(uint32_t pin) = 0;
6868
}; // BLESecurityCallbacks
6969

7070
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEServer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ void BLEServer::handleGATTServerEvent(
220220
m_connId = param->connect.conn_id; // Save the connection id.
221221
if (m_pServerCallbacks != nullptr) {
222222
m_pServerCallbacks->onConnect(this);
223+
m_pServerCallbacks->onConnect(this, param);
223224
}
224225
m_connectedCount++; // Increment the number of connected devices count.
225226
break;
@@ -369,6 +370,12 @@ void BLEServerCallbacks::onConnect(BLEServer* pServer) {
369370
ESP_LOGD("BLEServerCallbacks", "<< onConnect()");
370371
} // onConnect
371372

373+
void BLEServerCallbacks::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) {
374+
ESP_LOGD("BLEServerCallbacks", ">> onConnect(): Default");
375+
ESP_LOGD("BLEServerCallbacks", "Device: %s", BLEDevice::toString().c_str());
376+
ESP_LOGD("BLEServerCallbacks", "<< onConnect()");
377+
} // onConnect
378+
372379

373380
void BLEServerCallbacks::onDisconnect(BLEServer* pServer) {
374381
ESP_LOGD("BLEServerCallbacks", ">> onDisconnect(): Default");

cpp_utils/BLEServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class BLEServerCallbacks {
105105
* @param [in] pServer A reference to the %BLE server that received the client connection.
106106
*/
107107
virtual void onConnect(BLEServer* pServer);
108-
108+
virtual void onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param);
109109
/**
110110
* @brief Handle an existing client disconnection.
111111
*

0 commit comments

Comments
 (0)
0