38#include <esp_system.h>
139 return mNetwork.setCredentials(ssid, password);
152 if (!userId.isEmpty()) {
153 mCredentials.setOwnerId(userId);
154 mCredentials.store();
171 bool addCustomPage(
const String &path,
const uint8_t *gzData,
size_t gzLen,
const String &label =
"",
const char *contentType =
"text/html") {
172 return mNetwork.addCustomPage(path, gzData, gzLen, label, contentType);
184 bool addCustomRoute(
const String &path, WebRequestMethodComposite method, ArRequestHandlerFunction handler) {
185 return mNetwork.addCustomRoute(path, method, handler);
197 scheduler.
push(mNetwork);
198 scheduler.
push(mMQTT);
200 scheduler.
push(
"lisp_cleanup",
getLisp().getCleanupTask());
203 mTopDevice.setScheduler(scheduler);
205 if (mpNetworkDevice) {
206 scheduler.
push(*mpNetworkDevice);
208 UNIOT_LOG_WARN(
"configure the network controller before pushing to the scheduler");
223 if (mpNetworkDevice) {
224 mpNetworkDevice->attach();
228 mLispDevice.runStoredCode();
255 if (mpNetworkDevice) {
258 UNIOT_LOG_WARN(
"configure the network controller before registering to the event bus");
286 if (mpNetworkDevice) {
324 uint8_t activeLevelBtn = LOW,
325 uint8_t pinLed = UINT8_MAX,
326 uint8_t activeLevelLed = HIGH,
329 bool registerLispBtn =
true) {
330 if (mpNetworkDevice) {
336 auto ctrlBtn = mpNetworkDevice->getButton();
337 if (ctrlBtn && registerLispBtn) {
351 mLispDevice.setEventInterceptor(interceptor);
359 mLispDevice.setStartHook(hook);
367 mLispDevice.setStopHook(hook);
380 mLispDevice.publishLispEvent(eventID, value);
391 : mNetwork(mCredentials),
392 mMQTT(mCredentials, [this](
CBORObject &info) {
393 auto obj = info.putMap(
"primitives");
396 auto registers = info.putMap(
"misc").putMap(
"registers");
399 info.put(
"timestamp",
static_cast<int64_t
>(
Date::now()));
400 info.put(
"creator", mCredentials.
getCreatorId().c_str());
401 info.put(
"public_key", mCredentials.
getPublicKey().c_str());
402 info.put(
"mqtt_size", MQTT_MAX_PACKET_SIZE);
403 info.put(
"debug", UNIOT_LOG_ENABLED);
405 info.put(
"lisp_version", LISP_VERSION);
411 info.put(
"mcu",
"ESP8266");
412 info.put(
"reset_reason",
static_cast<int64_t
>(ESP.getResetInfoPtr()->reason));
414 info.put(
"mcu", ESP.getChipModel());
415 info.put(
"reset_reason",
static_cast<int64_t
>(esp_reset_reason()));
418 mpNetworkDevice(
nullptr) {
430 inline void _initMqtt() {
432 mMQTT.addDevice(mTopDevice);
433 mMQTT.addDevice(mLispDevice);
434 mTopDevice.syncSubscriptions();
435 mLispDevice.syncSubscriptions();
443 inline void _initTasks() {
453 inline void _initPrimitives() {
467 analogWriteRange(1023);
469 analogWriteResolution(10);
476 analogReadResolution(10);
490 inline void _initListeners() {
495 UNIOT_LOG_DEBUG(
"network connected, ip: %s", WiFi.localIP().toString().c_str());
538 if (mCredentials.isOwnerChanged()) {
540 mMQTT.renewSubscriptions();
541 mCredentials.resetOwnerChanged();
556 Credentials mCredentials;
557 NetworkScheduler mNetwork;
559 TopDevice mTopDevice;
560 LispDevice mLispDevice;
Defines the interface for components that can connect to an EventBus.
High-level network management controller with user interface.
Bytes & terminate()
Ensures the byte array is null-terminated.
Definition Bytes.h:281
const char * c_str() const
Gets the byte array as a C string.
Definition Bytes.h:306
bool setUserId(const String &userId)
Set the user identifier for device association.
Definition AppKit.h:151
MQTTKit & getMQTT()
Get the MQTT communication kit instance.
Definition AppKit.h:117
void setLispStopHook(LispStopHook hook)
Set the hook called when a Lisp script stops.
Definition AppKit.h:366
bool addCustomRoute(const String &path, WebRequestMethodComposite method, ArRequestHandlerFunction handler)
Register a custom HTTP route on the configuration portal.
Definition AppKit.h:184
bool setWiFiCredentials(const String &ssid, const String &password)
Set WiFi network credentials.
Definition AppKit.h:138
bool addCustomPage(const String &path, const uint8_t *gzData, size_t gzLen, const String &label="", const char *contentType="text/html")
Register a custom gzipped page on the configuration portal.
Definition AppKit.h:171
virtual void pushTo(TaskScheduler &scheduler) override
Add all managed tasks to the scheduler.
Definition AppKit.h:196
void setLispEventInterceptor(LispEventInterceptor interceptor)
Set event interceptor for Lisp interpreter.
Definition AppKit.h:350
void publishLispEvent(const String &eventID, int32_t value)
Publish an event to the Lisp interpreter.
Definition AppKit.h:379
virtual void registerWithBus(CoreEventBus &eventBus) override
Register all components with the event bus.
Definition AppKit.h:239
unLisp & getLisp()
Get the Lisp interpreter instance.
Definition AppKit.h:109
void configureNetworkController(uint8_t pinBtn=UINT8_MAX, uint8_t activeLevelBtn=LOW, uint8_t pinLed=UINT8_MAX, uint8_t activeLevelLed=HIGH, uint8_t maxRebootCount=5, uint32_t rebootWindowMs=10000, bool registerLispBtn=true)
Configure the NetworkController with individual parameters.
Definition AppKit.h:323
virtual void unregisterFromBus(CoreEventBus &eventBus) override
Unregister all components from the event bus.
Definition AppKit.h:270
void setLispStartHook(LispStartHook hook)
Set the hook called when a Lisp script starts.
Definition AppKit.h:358
const Credentials & getCredentials()
Get the device credentials.
Definition AppKit.h:125
virtual void attach() override
Initialize and attach all components.
Definition AppKit.h:218
void configureNetworkController(const NetworkControllerConfig &config)
Configure the NetworkController using a config structure.
Definition AppKit.h:299
Definition CBORObject.h:40
Manages device identity and cryptographic credentials for Uniot devices.
Definition Credentials.h:61
const String & getCreatorId() const
Gets the creator ID.
Definition Credentials.h:160
const String & getPublicKey() const
Gets the device's public key as a hexadecimal string.
Definition Credentials.h:178
static time_t now()
Returns the current Unix timestamp.
Definition Date.h:75
bool registerEntity(EventEntity< T_topic, T_msg, T_data > *entity)
Registers an entity (emitter or listener) with this EventBus.
Definition EventBus.cpp:44
void unregisterEntity(EventEntity< T_topic, T_msg, T_data > *entity)
Unregisters an entity from this EventBus.
Definition EventBus.cpp:53
bool closeDataChannel(T_topic topic)
Closes a previously opened data channel.
Definition EventBus.cpp:66
bool openDataChannel(T_topic topic, size_t limit)
Opens a data channel for a specific topic with a size limit.
Definition EventBus.cpp:61
Interface for connecting components to the TaskScheduler.
Definition ISchedulerConnectionKit.h:35
static RegisterManager & getRegisterManager()
Gets the static register manager instance.
Definition PrimitiveExpeditor.h:71
void serializeRegisters(CBORObject &obj)
Serializes all registers (GPIO and Object) to a CBOR object.
Definition RegisterManager.h:165
bool link(const String &name, RecordPtr link, uint32_t id=FOURCC(____))
Links an object to a named register.
Definition RegisterManager.h:111
Singleton(const Singleton &)=delete
static unLisp & getInstance()
Definition Singleton.h:73
Definition TaskScheduler.h:183
TaskScheduler::TaskPtr getCleanupTask()
Definition unLisp.h:221
void serializePrimitives(CBORObject &obj)
Serialize all registered primitive functions to CBOR format.
Definition unLisp.h:349
unLisp * pushPrimitive(Primitive *primitive)
Add a new primitive function to the Lisp environment.
Definition unLisp.h:329
Object aread(Root root, VarObject env, VarObject list)
Analog read primitive function for reading analog input.
Definition LispPrimitives.cpp:74
Object bclicked(Root root, VarObject env, VarObject list)
Button clicked primitive function for checking button click status.
Definition LispPrimitives.cpp:91
Object dwrite(Root root, VarObject env, VarObject list)
Definition LispPrimitives.cpp:23
Object awrite(Root root, VarObject env, VarObject list)
Analog write primitive function for setting PWM output.
Definition LispPrimitives.cpp:57
Object dread(Root root, VarObject env, VarObject list)
Digital read primitive function for reading pin input state.
Definition LispPrimitives.cpp:40
#define UNIOT_CORE_VERSION
Version of Uniot Core, reported in the device's status packet.
Definition Common.h:48
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:305
#define UNIOT_WIFI_REBOOT_WINDOW_MS
How long after boot a reboot still counts towards the reset, in ms.
Definition Common.h:115
auto MakeUnique(Args &&...args) -> decltype(std::make_unique< T >(std::forward< Args >(args)...))
Creates a unique pointer instance, alias for std::make_unique.
Definition Common.h:332
#define FOURCC(name)
Creates a FourCC constant from a string literal.
Definition Common.h:260
#define UNIOT_MQTT_PORT
TCP port of the MQTT broker.
Definition Common.h:63
#define UNIOT_LISP_HEAP
Bytes of heap the Lisp interpreter is given at startup.
Definition Common.h:130
#define UNIOT_WIFI_REBOOT_RESET_COUNT
Consecutive reboots that reset the stored network configuration.
Definition Common.h:104
#define UNIOT_MQTT_HOST
Hostname of the MQTT broker the device connects to.
Definition Common.h:55
#define UNIOT_LISP_MAX_EVAL_STACK
Bytes of C stack an evaluation may spend before a script is abandoned.
Definition Common.h:158
IEventBusConnectionKit< unsigned int, int, Bytes > ICoreEventBusConnectionKit
A specific instance of IEventBusConnectionKit for core system events.
Definition IEventBusConnectionKit.h:87
EventBus< unsigned int, int, Bytes > CoreEventBus
Standard EventBus configuration used throughout the core system.
Definition EventBus.h:211
#define UNIOT_LOG_INFO(...)
Log an INFO level message Used for general information about system operation. Only compiled if UNIOT...
Definition Logger.h:315
#define UNIOT_LOG_WARN(...)
Log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:294
#define UNIOT_LOG_DEBUG(...)
Log an DEBUG level message Used for general information about system operation. Only compiled if UNIO...
Definition Logger.h:340
TaskScheduler & push(const char *name, TaskPtr task)
Add a named task to the scheduler.
Definition TaskScheduler.h:233
std::function< void(LispStartReason)> LispStartHook
Called after the interpreter is built and before the script is evaluated.
Definition LispHelper.h:186
std::function< void(LispStopReason)> LispStopHook
Called before the interpreter is destroyed.
Definition LispHelper.h:192
constexpr const char * dread
Primitive for digital read operations.
Definition DefaultPrimitives.h:59
constexpr const char * aread
Primitive for analog read operations.
Definition DefaultPrimitives.h:73
constexpr const char * awrite
Primitive for analog write operations.
Definition DefaultPrimitives.h:66
constexpr const char * dwrite
Primitive for digital write operations.
Definition DefaultPrimitives.h:52
constexpr const char * bclicked
Primitive for detecting button click events.
Definition DefaultPrimitives.h:80
@ OUT_LISP_LOG
Channel for Lisp log messages and debug information.
Definition LispEvents.h:74
@ IN_EVENT
Channel for incoming events from the application to Lisp.
Definition LispEvents.h:77
@ OUT_LISP
Channel for standard Lisp output (stdout equivalent)
Definition LispEvents.h:73
@ OUT_LISP_ERR
Channel for Lisp error messages and exceptions.
Definition LispEvents.h:75
@ OUT_EVENT
Channel for outgoing events from Lisp to the application.
Definition LispEvents.h:76
@ CONNECTION
MQTT connection state changes and operations.
Definition MQTTEvents.h:61
@ SUCCESS
MQTT connection established successfully.
Definition MQTTEvents.h:73
@ FAILED
MQTT connection failed or was lost.
Definition MQTTEvents.h:72
@ DISCONNECTING
Currently disconnecting from network.
Definition NetworkEvents.h:88
@ SUCCESS
Network operation completed successfully.
Definition NetworkEvents.h:86
@ ACCESS_POINT
Device is operating in access point mode.
Definition NetworkEvents.h:90
@ AVAILABLE
Configured network is available for connection.
Definition NetworkEvents.h:91
@ DISCONNECTED
Network connection has been lost or terminated.
Definition NetworkEvents.h:89
@ FAILED
Network operation failed (connection, scan, etc.)
Definition NetworkEvents.h:85
@ CONNECTING
Currently attempting to connect to network.
Definition NetworkEvents.h:87
@ CONNECTION
WiFi connection state changes and operations.
Definition NetworkEvents.h:74
@ OUT_SSID
Channel for broadcasting current SSID information.
Definition NetworkEvents.h:63
Contains all classes and functions related to the Uniot Core.
std::function< bool(const LispEvent &event)> LispEventInterceptor
Definition LispDevice.h:41
Configuration parameters for NetworkController.
Definition AppKit.h:95
uint8_t pinBtn
Button pin (UINT8_MAX means not used)
Definition AppKit.h:96
bool registerLispBtn
Whether to register the button with the Lisp interpreter.
Definition AppKit.h:102
uint8_t activeLevelLed
Active level for LED (LOW or HIGH)
Definition AppKit.h:99
uint8_t maxRebootCount
Maximum number of consecutive reboots.
Definition AppKit.h:100
uint8_t activeLevelBtn
Active level for button (LOW or HIGH)
Definition AppKit.h:97
uint32_t rebootWindowMs
Time window in ms for counting reboots.
Definition AppKit.h:101
uint8_t pinLed
LED pin (UINT8_MAX means not used)
Definition AppKit.h:98