owwrite/
owwrite.rs

1//! **owwrite** -- _Rust version_
2//!
3//! ## Write a value to owserver ( to a specific 1-wire device file )
4//!
5//! **owwrite** is a tool in the 1-wire file system **OWFS**
6//!
7//! This Rust version of **owwrite** is part of **owrust** -- the _Rust language_ OWFS programs
8//! * **OWFS** [documentation](https://owfs.org) and [code](https://github.com/owfs/owfs)
9//! * **owrust** [repository](https://github.com/alfille/owrust)
10//!
11//! ## SYNTAX
12//! ```
13//! owwrite [OPTIONS] PATH VALUE
14//! ```
15//!
16//! ## OPTIONS
17//! * `-s IP:port` (default `localhost:4304`)
18//! * `--hex       read the value in hexidecimal
19//! * `--size n    write only n bytes
20//! * `--offset m  start writing at byte m
21//! * -h           for full list of options
22//!
23//! ## PATH
24//! * 1-wire path to a file
25//! * No Default
26//!
27//! ## VALUE
28//! * Text (a byte string)
29//! * Hexidecimal bytes ( e.g. `03A3FF` is a 3 byte value )
30//!   * upper and lower case a-f allowed
31//!   * no 0x prefix should be used
32//!   * no spaces between bytes
33//!
34//! ### More than one PATH / VALUE pair allowed
35//!
36//! **owwrite** only works on files.
37//!
38//! ## USAGE
39//! * owserver must be running in a network-accessible location
40//! * `owwrite` is a command line program
41//! * errors to stderr
42//!
43//! ## EXAMPLE
44//! Read, write and reread a temperature high limit
45//! ```
46//! owread /10.67c6697351ff/temphigh
47//!      18.4024
48//! owwrite /10.67c6697351ff/temphigh 45
49//! owread /10.67c6697351ff/temphigh
50//!      45.0
51//! ```
52//! {c} 2025 Paul H Alfille -- MIT Licence
53
54// owrust project
55// https://github.com/alfille/owrust
56//
57// This is a Rust version of my C owfs code for talking to 1-wire devices via owserver
58// Basically owserver can talk to the physical devices, and provides network access via my "owserver protocol"
59//
60// MIT Licence
61// {c} 2025 Paul H Alfille
62
63use owrust::parse_args::{OwWrite, Parser};
64
65fn main() {
66    let mut owserver = owrust::new(); // create structure for owserver communication
67    let prog = OwWrite;
68
69    // configure and get paths
70    match prog.command_line(&mut owserver) {
71        Ok(paths) => {
72            if paths.is_empty() {
73                // No path
74                eprintln!("Not enough arguments");
75            } else if !paths.len().is_multiple_of(2) {
76                eprintln!("Path and value not paired");
77            } else {
78                // for each path/value pair in command line
79                for chunk in paths.chunks(2) {
80                    from_path(&mut owserver, &chunk[0], &chunk[1]);
81                }
82            }
83        }
84        Err(e) => {
85            eprintln!("owread trouble {}", e);
86        }
87    }
88}
89
90// print 1-wire file contents (e.g. a sensor reading)
91fn from_path(owserver: &mut owrust::OwMessage, path: &String, value: &String) {
92    match owserver.write(path, value.as_bytes()) {
93        Ok(_) => (),
94        Err(e) => {
95            eprintln!(
96                "Trouble with write -- path {} value {} Error {}",
97                path, value, e
98            );
99        }
100    }
101}