framesmith 0.1.0

A Rust library for controlling Samsung Frame TVs over the local network
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;

use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use futures_util::{SinkExt, StreamExt};
use tokio::sync::Mutex;

use crate::Error;
use crate::frame_tv_transport::FrameTvTransport;
use crate::protocol::art_request::{ArtRequest, ArtRequestParams, ConnInfo};
use crate::protocol::art_response::{ArtResponse, ConnInfoResponse};
use crate::protocol::event;
use crate::protocol::ws_message::{WsIncoming, WsOutgoing};
use crate::transport::tcp_bulk_transfer;
use crate::transport::tls_config;

const WS_PORT: u16 = 8002;
const REMOTE_ENDPOINT: &str = "samsung.remote.control";
const ART_ENDPOINT: &str = "com.samsung.art-app";
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);

type WsStream =
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;

pub(crate) struct WsTransport {
    host: IpAddr,
    token: Arc<Mutex<Option<String>>>,
}

impl WsTransport {
    pub(crate) async fn connect(
        host: IpAddr,
        token: Option<String>,
        timeout: Duration,
    ) -> Result<Self, Error> {
        let token = Arc::new(Mutex::new(token));

        // Verify connectivity and retrieve auth token
        let ws_url = build_ws_url(host, REMOTE_ENDPOINT, &*token.lock().await);
        let connector = tls_config::make_tls_connector();

        let (ws_stream, _) = tokio::time::timeout(timeout, async {
            tokio_tungstenite::connect_async_tls_with_config(&ws_url, None, false, Some(connector))
                .await
        })
        .await
        .map_err(|_| Error::Timeout(timeout))?
        .map_err(|e| Error::WebSocket(Box::new(e)))?;

        let (_write, mut read) = ws_stream.split();

        if let Some(Ok(tokio_tungstenite::tungstenite::Message::Text(text))) = read.next().await
            && let Ok(incoming) = serde_json::from_str::<WsIncoming>(&text)
        {
            match incoming.event.as_deref() {
                Some(event::CHANNEL_UNAUTHORIZED) => {
                    return Err(Error::Unauthorized);
                }
                Some(event::CHANNEL_TIMEOUT) => {
                    return Err(Error::ConnectionFailed(
                        "TV rejected the connection (ms.channel.timeOut). \
                         Troubleshooting steps:\n\
                         1. Your device must be on the same local subnet as the TV \
                            (e.g. both on 192.168.1.x). The TV rejects WebSocket \
                            connections from other subnets entirely.\n\
                         2. Ensure IP Remote is enabled on the TV: \
                            Settings → Connection → Network → Expert Settings → IP Remote\n\
                         3. For first-time pairing, the TV screen must be on (not in \
                            standby or art mode) so the approval popup can appear."
                            .into(),
                    ));
                }
                Some(event::CHANNEL_CONNECT) => {
                    if let Some(data) = &incoming.data
                        && let Some(t) = data.get("token").and_then(|v| v.as_str())
                    {
                        *token.lock().await = Some(t.to_string());
                    }
                }
                _ => {}
            }
        }

        Ok(Self { host, token })
    }

    pub(crate) async fn get_token(&self) -> Option<String> {
        self.token.lock().await.clone()
    }

    /// Create a `WsTransport` from an already-known token, skipping the auth handshake.
    /// Used by `PersistentWsTransport` for one-shot upload/download operations.
    pub(crate) fn connect_from_token(host: IpAddr, token: Option<String>) -> Self {
        Self {
            host,
            token: Arc::new(Mutex::new(token)),
        }
    }

    async fn open_art_connection(&self) -> Result<WsStream, Error> {
        let token = self.token.lock().await.clone();
        let ws_url = build_ws_url(self.host, ART_ENDPOINT, &token);
        let connector = tls_config::make_tls_connector();

        let (ws_stream, _) = tokio::time::timeout(DEFAULT_TIMEOUT, async {
            tokio_tungstenite::connect_async_tls_with_config(&ws_url, None, false, Some(connector))
                .await
        })
        .await
        .map_err(|_| Error::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| Error::WebSocket(Box::new(e)))?;

        Ok(ws_stream)
    }

    async fn open_remote_connection(&self) -> Result<WsStream, Error> {
        let token = self.token.lock().await.clone();
        let ws_url = build_ws_url(self.host, REMOTE_ENDPOINT, &token);
        let connector = tls_config::make_tls_connector();

        let (ws_stream, _) = tokio::time::timeout(DEFAULT_TIMEOUT, async {
            tokio_tungstenite::connect_async_tls_with_config(&ws_url, None, false, Some(connector))
                .await
        })
        .await
        .map_err(|_| Error::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| Error::WebSocket(Box::new(e)))?;

        Ok(ws_stream)
    }
}

async fn wait_for_art_ready(
    read: &mut futures_util::stream::SplitStream<WsStream>,
) -> Result<(), Error> {
    loop {
        match tokio::time::timeout(Duration::from_secs(5), read.next()).await {
            Ok(Some(Ok(tokio_tungstenite::tungstenite::Message::Text(text)))) => {
                if let Ok(msg) = serde_json::from_str::<WsIncoming>(&text) {
                    match msg.event.as_deref() {
                        Some(event::CHANNEL_READY) => return Ok(()),
                        Some(event::CHANNEL_CONNECT) => continue,
                        Some(event::CHANNEL_UNAUTHORIZED) => return Err(Error::Unauthorized),
                        other => {
                            tracing::debug!("art ready wait: ignoring event {:?}", other);
                            continue;
                        }
                    }
                } else {
                    tracing::debug!("art ready wait: unparseable text: {text}");
                }
            }
            Ok(Some(Ok(other))) => {
                tracing::debug!("art ready wait: non-text message: {other:?}");
                continue;
            }
            Ok(Some(Err(e))) => return Err(Error::WebSocket(Box::new(e))),
            Ok(None) => return Err(Error::ConnectionFailed("art connection closed".into())),
            Err(_) => {
                return Err(Error::ConnectionFailed(
                    "Timed out waiting for art endpoint to become ready. \
                     The TV must be in art mode (not on the home screen or \
                     showing other content) for art commands to work."
                        .into(),
                ));
            }
        }
    }
}

async fn read_d2d_response(
    read: &mut futures_util::stream::SplitStream<WsStream>,
    wait_for_event: Option<&str>,
) -> Result<ArtResponse, Error> {
    loop {
        match tokio::time::timeout(DEFAULT_TIMEOUT, read.next()).await {
            Ok(Some(Ok(tokio_tungstenite::tungstenite::Message::Text(text)))) => {
                tracing::debug!("d2d read: raw message: {text}");
                if let Ok(msg) = serde_json::from_str::<WsIncoming>(&text)
                    && msg.event.as_deref() == Some(event::D2D_SERVICE_MESSAGE)
                    && let Some(data) = msg.data
                {
                    let data_str = if data.is_string() {
                        data.as_str().unwrap().to_string()
                    } else {
                        data.to_string()
                    };

                    if let Ok(art_resp) = serde_json::from_str::<ArtResponse>(&data_str) {
                        tracing::debug!(
                            "d2d parsed: event={:?}, error_code={:?}, content_id={:?}",
                            art_resp.event,
                            art_resp.error_code,
                            art_resp.content_id
                        );
                        if let Some(ref error_code) = art_resp.error_code {
                            return Err(Error::TvError {
                                request: "art_request".to_string(),
                                code: error_code.to_string(),
                            });
                        }

                        if let Some(wait_event) = wait_for_event {
                            if art_resp.event.as_deref() == Some(wait_event) {
                                return Ok(art_resp);
                            }
                            continue;
                        }

                        return Ok(art_resp);
                    }
                }
            }
            Ok(Some(Ok(_))) => continue,
            Ok(Some(Err(e))) => return Err(Error::WebSocket(Box::new(e))),
            Ok(None) => return Err(Error::ConnectionFailed("connection closed".into())),
            Err(_) => return Err(Error::Timeout(DEFAULT_TIMEOUT)),
        }
    }
}

impl FrameTvTransport for WsTransport {
    async fn send_art_request(
        &self,
        request_json: &str,
        wait_for_event: Option<&str>,
    ) -> Result<ArtResponse, Error> {
        let ws = self.open_art_connection().await?;
        let (mut write, mut read) = ws.split();

        wait_for_art_ready(&mut read).await?;

        let outgoing = WsOutgoing::art_request(request_json);
        let msg_text = serde_json::to_string(&outgoing)?;
        write
            .send(tokio_tungstenite::tungstenite::Message::Text(
                msg_text.into(),
            ))
            .await
            .map_err(|e| Error::WebSocket(Box::new(e)))?;

        read_d2d_response(&mut read, wait_for_event).await
    }

    async fn send_remote_key(&self, key_code: &str) -> Result<(), Error> {
        let ws = self.open_remote_connection().await?;
        let (mut write, mut read) = ws.split();

        // Wait for connect event
        if let Some(Ok(tokio_tungstenite::tungstenite::Message::Text(text))) = read.next().await
            && let Ok(msg) = serde_json::from_str::<WsIncoming>(&text)
            && msg.event.as_deref() == Some(event::CHANNEL_UNAUTHORIZED)
        {
            return Err(Error::Unauthorized);
        }

        let outgoing = WsOutgoing::remote_key_click(key_code);
        let msg_text = serde_json::to_string(&outgoing)?;
        write
            .send(tokio_tungstenite::tungstenite::Message::Text(
                msg_text.into(),
            ))
            .await
            .map_err(|e| Error::WebSocket(Box::new(e)))?;

        Ok(())
    }

    async fn upload_image(
        &self,
        image_data: &[u8],
        file_type: &str,
        matte_id: Option<&str>,
    ) -> Result<ArtResponse, Error> {
        let ws = self.open_art_connection().await?;
        let (mut write, mut read) = ws.split();

        wait_for_art_ready(&mut read).await?;

        let now = chrono_free_date();
        let art_id = uuid::Uuid::new_v4().to_string();
        let conn_info = ConnInfo::with_id(art_id.clone());
        let req = ArtRequest::new_with_id(
            art_id,
            "send_image",
            ArtRequestParams::SendImage {
                file_type: file_type.to_string(),
                image_date: now,
                matte_id: matte_id.unwrap_or("none").to_string(),
                portrait_matte_id: "shadowbox_polar".to_string(),
                file_size: image_data.len() as u64,
                conn_info,
            },
        );
        let req_json = req.to_json()?;
        let outgoing = WsOutgoing::art_request(&req_json);
        let msg_text = serde_json::to_string(&outgoing)?;
        write
            .send(tokio_tungstenite::tungstenite::Message::Text(
                msg_text.into(),
            ))
            .await
            .map_err(|e| Error::WebSocket(Box::new(e)))?;

        // Wait for ready_to_use event with conn_info
        let conn_resp: ConnInfoResponse = loop {
            let art_resp = read_d2d_response(&mut read, Some("ready_to_use")).await?;
            if let Some(ref ci) = art_resp.conn_info {
                break serde_json::from_str(ci)?;
            }
        };

        // Upload via TCP (TLS if TV indicates secured)
        let sec_key = conn_resp.key.as_deref().unwrap_or("");
        let secured = conn_resp.secured.unwrap_or(false);
        tracing::debug!(
            "upload conn_info: ip={}, port={}, secured={}, key={:?}",
            conn_resp.ip,
            conn_resp.port,
            secured,
            conn_resp.key
        );
        let _tcp_stream = tcp_bulk_transfer::upload_image(
            &conn_resp.ip,
            conn_resp.port,
            sec_key,
            file_type,
            image_data,
            secured,
        )
        .await?;

        // Wait for image_added confirmation (keep TCP stream alive until response)
        let resp = read_d2d_response(&mut read, Some("image_added")).await?;
        drop(_tcp_stream);
        Ok(resp)
    }

    async fn download_thumbnail(&self, content_id: &str) -> Result<Vec<u8>, Error> {
        let ws = self.open_art_connection().await?;
        let (mut write, mut read) = ws.split();

        wait_for_art_ready(&mut read).await?;

        let art_id = uuid::Uuid::new_v4().to_string();
        let conn_info = ConnInfo::with_id(art_id.clone());
        let req = ArtRequest::new_with_id(
            art_id,
            "get_thumbnail",
            ArtRequestParams::GetThumbnail {
                content_id: content_id.to_string(),
                conn_info,
            },
        );
        let req_json = req.to_json()?;
        let outgoing = WsOutgoing::art_request(&req_json);
        let msg_text = serde_json::to_string(&outgoing)?;
        write
            .send(tokio_tungstenite::tungstenite::Message::Text(
                msg_text.into(),
            ))
            .await
            .map_err(|e| Error::WebSocket(Box::new(e)))?;

        // Wait for response with conn_info
        let conn_resp: ConnInfoResponse = loop {
            let art_resp = read_d2d_response(&mut read, None).await?;
            if let Some(ref ci) = art_resp.conn_info {
                break serde_json::from_str(ci)?;
            }
        };

        let secured = conn_resp.secured.unwrap_or(false);
        tcp_bulk_transfer::download_thumbnail(&conn_resp.ip, conn_resp.port, secured).await
    }

    async fn tv_display_size(&self) -> Result<(u32, u32), Error> {
        // Frame TVs are 4K (3840x2160)
        // Could potentially query via device info in the future
        Ok((3840, 2160))
    }
}

fn build_ws_url(host: IpAddr, endpoint: &str, token: &Option<String>) -> String {
    let name = BASE64.encode("framesmith");
    let mut url = format!("wss://{host}:{WS_PORT}/api/v2/channels/{endpoint}?name={name}");
    if let Some(token) = token {
        url.push_str(&format!("&token={token}"));
    }
    url
}

fn chrono_free_date() -> String {
    use std::time::SystemTime;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    let secs = now.as_secs();
    let days = secs / 86400;
    let time_of_day = secs % 86400;
    let hours = time_of_day / 3600;
    let minutes = (time_of_day % 3600) / 60;
    let seconds = time_of_day % 60;

    let mut year = 1970i64;
    let mut remaining_days = days as i64;
    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if remaining_days < days_in_year {
            break;
        }
        remaining_days -= days_in_year;
        year += 1;
    }

    let month_days = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    let mut month = 1u32;
    for &md in &month_days {
        if remaining_days < md {
            break;
        }
        remaining_days -= md;
        month += 1;
    }
    let day = remaining_days + 1;

    format!("{year:04}:{month:02}:{day:02} {hours:02}:{minutes:02}:{seconds:02}")
}

fn is_leap_year(year: i64) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}