Expand description
Core library for communicating with TP-Link Kasa smart home devices.
This crate implements both the legacy TP-Link Smart Home Protocol (XOR cipher) and the newer KLAP protocol (AES encryption with authentication).
§Protocols
§Legacy Protocol (XOR)
Older devices use a simple XOR autokey cipher on TCP port 9999.
No authentication is required. Use send_command for quick access.
§KLAP Protocol
Newer firmware versions use the KLAP (Kasa Local Authentication Protocol)
over HTTP port 80. This requires TP-Link cloud credentials. Use the
transport module for KLAP support.
§Quick Start
For legacy devices (no credentials needed):
use kasa_core::{commands, send_command, DEFAULT_PORT, DEFAULT_TIMEOUT};
#[tokio::main]
async fn main() -> std::io::Result<()> {
let response = send_command(
"192.168.1.100",
DEFAULT_PORT,
DEFAULT_TIMEOUT,
commands::INFO,
).await?;
println!("{}", response);
Ok(())
}For newer devices with KLAP (credentials required):
use kasa_core::{Credentials, transport::{DeviceConfig, connect, Transport}};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = DeviceConfig::new("192.168.1.100")
.with_credentials(Credentials::new("user@example.com", "password"));
let transport = connect(config).await?;
let response = transport.send(r#"{"system":{"get_sysinfo":{}}}"#).await?;
println!("{}", response);
Ok(())
}§Auto-Detection
The transport::connect function automatically detects which protocol
a device uses and connects appropriately.
§Concurrency
When working with multiple devices, you can communicate with them concurrently. However, requests to a single device must be sequential - TP-Link devices cannot handle concurrent requests and will return errors.
use kasa_core::transport::{DeviceConfig, connect, TransportExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to multiple devices
let device1 = connect(DeviceConfig::new("192.168.1.100")).await?;
let device2 = connect(DeviceConfig::new("192.168.1.101")).await?;
// Query both devices concurrently (safe - different devices)
let (info1, info2) = tokio::join!(
device1.get_sysinfo(),
device2.get_sysinfo(),
);
println!("Device 1: {}", info1?.alias);
println!("Device 2: {}", info2?.alias);
Ok(())
}Re-exports§
pub use credentials::Credentials;pub use discovery::DiscoveredDevice;pub use error::Error;pub use transport::DeviceConfig;pub use transport::EncryptionType;pub use transport::Transport;pub use transport::TransportExt;pub use transport::connect;pub use crypto::xor::decrypt;pub use crypto::xor::encrypt;
Modules§
- commands
- Predefined JSON command strings for common TP-Link Kasa device operations.
- credentials
- Credentials management for TP-Link Kasa devices.
- crypto
- Cryptographic utilities for TP-Link Kasa device communication.
- discovery
- Discovery protocols for TP-Link Kasa devices.
- error
- Error types for kasa-core.
- response
- Typed response structures for Kasa device JSON responses.
- transport
- Transport layer for communicating with TP-Link Kasa devices.
Structs§
- Broadcast
Result - Result of a broadcast command to a single device.
- Wifi
Network - Information about a WiFi network from a scan.
Enums§
- KeyType
- Security type for WiFi networks.
Constants§
- DEFAULT_
DISCOVERY_ TIMEOUT - Default discovery timeout.
- DEFAULT_
PORT - Default TCP port for legacy TP-Link Kasa smart devices.
- DEFAULT_
TIMEOUT - Default connection timeout.
- VERSION
- The version of the kasa-core library.
Functions§
- broadcast
- Broadcasts a command to all discovered Kasa devices on the local network.
- discover
- Discovers Kasa devices on the local network using UDP broadcast.
- send_
command - Sends a command to a TP-Link Kasa device using the legacy XOR protocol.