26 if (sensor ==
nullptr)
29 const int16_t value_length = 4;
31 auto rel_offset = value_offset - response_offset;
32 if (rel_offset <= -value_length)
37 auto start_offset = std::max(0, rel_offset);
38 auto end_offset = std::min((int16_t) (rel_offset + value_length),
length);
39 auto copy_length = end_offset - start_offset;
40 auto buffer_offset = std::max(-rel_offset, 0);
41 std::memcpy(this->
buffer_ + buffer_offset, response + start_offset, copy_length);
43 if (rel_offset + value_length <=
length) {
46 *((int32_t *) buffer) = ntohl(*((int32_t *) buffer));
47 float fvalue = *((
float *) buffer);
59 if (length < GENI_RESPONSE_HEADER_LENGTH) {
63 if (response[0] != 36 || response[2] != 248 || response[3] != 231 || response[4] != 10) {
64 ESP_LOGW(TAG,
"[%s] response bytes %d %d %d %d %d don't match GENI HEADER", this->
parent_->
address_str(),
65 response[0], response[1], response[2], response[3], response[4]);
70 std::memcpy(this->
response_type_, response + 5, GENI_RESPONSE_TYPE_LENGTH);
73 auto extract_publish_sensor_value = [response,
length,
this](int16_t value_offset,
sensor::Sensor *sensor,
80 extract_publish_sensor_value(GENI_RESPONSE_FLOW_OFFSET, this->
flow_sensor_, 3600.0F);
81 extract_publish_sensor_value(GENI_RESPONSE_HEAD_OFFSET, this->
head_sensor_, .0001F);
84 extract_publish_sensor_value(GENI_RESPONSE_POWER_OFFSET, this->
power_sensor_, 1.0F);
85 extract_publish_sensor_value(GENI_RESPONSE_CURRENT_OFFSET, this->
current_sensor_, 1.0F);
86 extract_publish_sensor_value(GENI_RESPONSE_MOTOR_SPEED_OFFSET, this->
speed_sensor_, 1.0F);
87 extract_publish_sensor_value(GENI_RESPONSE_VOLTAGE_AC_OFFSET, this->
voltage_sensor_, 1.0F);
98 case ESP_GATTC_OPEN_EVT: {
99 if (param->open.status == ESP_GATT_OK) {
106 case ESP_GATTC_CONNECT_EVT: {
107 if (std::memcmp(param->connect.remote_bda, this->parent_->get_remote_bda(), 6) != 0)
109 auto ret = esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT);
111 ESP_LOGW(TAG,
"esp_ble_set_encryption failed, status=%x", ret);
115 case ESP_GATTC_DISCONNECT_EVT: {
131 case ESP_GATTC_SEARCH_CMPL_EVT: {
133 if (chr ==
nullptr) {
134 ESP_LOGE(TAG,
"[%s] No GENI service found at device, not an Alpha3..?", this->
parent_->
address_str());
140 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d",
status);
145 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
146 this->
node_state = espbt::ClientState::ESTABLISHED;
150 case ESP_GATTC_NOTIFY_EVT: {
151 if (param->notify.handle == this->geni_handle_) {
170 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
176 uint8_t geni_request_flow_head[] = {39, 7, 231, 248, 10, 3, 93, 1, 33, 82, 31};
177 this->
send_request_(geni_request_flow_head,
sizeof(geni_request_flow_head));
182 uint8_t geni_request_power[] = {39, 7, 231, 248, 10, 3, 87, 0, 69, 138, 205};
183 this->
send_request_(geni_request_power,
sizeof(geni_request_power));