Skip to content

Commit

Permalink
Add flash reading component
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Apr 22, 2023
1 parent 8865325 commit 6012fb8
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
28 changes: 28 additions & 0 deletions components/hub_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
from esphome.components import web_server_base

DEPENDENCIES = ["libretuya"]
AUTO_LOAD = ["web_server_base"]

hub_api_ns = cg.esphome_ns.namespace("hub_api")
HubAPIHandler = hub_api_ns.class_("HubAPI", cg.Component)

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(HubAPIHandler),
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase
),
},
cv.only_with_arduino,
).extend(cv.COMPONENT_SCHEMA)


async def to_code(config):
paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])

var = cg.new_Pvariable(config[CONF_ID], paren)
await cg.register_component(var, config)
59 changes: 59 additions & 0 deletions components/hub_api/hub_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifdef USE_ARDUINO

#include "hub_api.h"
#include "esphome/core/application.h"

#include <Flash.h>

#undef min

namespace esphome {
namespace hub_api {

static const char *const TAG = "hub_api";

void HubAPI::handleRequest(AsyncWebServerRequest *req) {
if (req->url().substring(4) == "/flash_read") {
uint32_t offset, length;

if (req->hasParam("offset")) {
offset = req->getParam("offset")->value().toInt();
} else {
offset = 0;
}

if (req->hasParam("length")) {
length = req->getParam("length")->value().toInt();
} else {
length = FLASH_LENGTH;
}

auto callback = [offset, length](uint8_t *buffer, size_t maxLen, size_t position) -> size_t {
size_t blockStart = offset + position;
size_t blockSize = std::min(static_cast<size_t>(maxLen), static_cast<size_t>(length - position));
blockSize = std::min(blockSize, static_cast<size_t>(4096));

if (blockSize) {
ESP_LOGD(TAG, "Reading flash: offset=%06x, length=%u", blockStart, blockSize);
Flash.readBlock(blockStart, buffer, blockSize);
}

return blockSize;
};

req->send("application/octet-stream", length, callback);
return;
}

EMPTY:
#ifdef LT_BANNER_STR
req->send(200, "text/plain", LT_BANNER_STR);
#else
req->send(200, "text/plain", "LibreTuya " LT_VERSION_STR);
#endif
}

} // namespace hub_api
} // namespace esphome

#endif // USE_ARDUINO
46 changes: 46 additions & 0 deletions components/hub_api/hub_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#ifdef USE_ARDUINO

#include <map>
#include <utility>

#include "esphome/components/web_server_base/web_server_base.h"
#include "esphome/core/controller.h"
#include "esphome/core/component.h"

namespace esphome {
namespace hub_api {

class HubAPI : public AsyncWebHandler, public Component {
public:
HubAPI(web_server_base::WebServerBase *base) : base_(base) {}

bool canHandle(AsyncWebServerRequest *request) override {
if (request->method() == HTTP_GET) {
if (request->url().startsWith("/hub"))
return true;
}

return false;
}

void handleRequest(AsyncWebServerRequest *req) override;

void setup() override {
this->base_->init();
this->base_->add_handler(this);
}
float get_setup_priority() const override {
// After WiFi
return setup_priority::WIFI - 1.0f;
}

protected:
web_server_base::WebServerBase *base_;
};

} // namespace hub_api
} // namespace esphome

#endif // USE_ARDUINO
3 changes: 3 additions & 0 deletions yaml/kickstart-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ external_components:
source: github://libretuya/esphome-kickstart
components:
- pinscan
- hub_api
refresh: 10 min

logger:
Expand Down Expand Up @@ -53,6 +54,8 @@ pinscan:
pin_state_sensor:
name: -> Pin State

hub_api:

select:
- platform: template
id: mode
Expand Down

0 comments on commit 6012fb8

Please sign in to comment.