echonet 1.3.2

ECHONET Lite framework for Rust
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (C) 2022 The uecho-rs Authors All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use log::*;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};

use crate::message::ResponseErrorMessage;
use crate::node_profile::*;
use crate::object::*;
use crate::property::{Property, PropertyCode};
use crate::protocol::{ESV, Message, TID, TID_MAX, TID_MIN};
use crate::request_handler::*;
use crate::super_object::*;
use crate::transport::{Manager, NotifytManager, PORT};
use crate::transport::{Observer, ObserverObject};

/// Node represents an ECHONET-Lite node which contains ECHONET-Lite objects such the profiles and the devices.
pub struct Node {
    transport_mgr: Manager,
    objects: Vec<Object>,
    last_tid: TID,
    post_sender: Sender<Message>,
    request_mgr: RequestManager,
    notify_mgr: NotifytManager,
}

impl Node {
    pub fn new() -> Arc<Mutex<Node>> {
        let (tx, _): (Sender<Message>, Receiver<Message>) = mpsc::channel();
        let node = Arc::new(Mutex::new(Node {
            transport_mgr: Manager::new(),
            objects: Vec::new(),
            last_tid: TID_MIN,
            post_sender: tx,
            request_mgr: RequestManager::new(),
            notify_mgr: NotifytManager::new(),
        }));
        node.lock().unwrap().init_objects();
        node.lock()
            .unwrap()
            .add_request_handler(Arc::new(Mutex::new(node.clone())));
        node
    }

    fn init_objects(&mut self) {
        self.init_node_profile_object();
    }

    pub fn init_node_profile_object(&mut self) {
        self.add_object(NodeProfile::new());
    }

    pub fn add_object(&mut self, obj: Object) -> bool {
        self.objects.push(obj);
        self.update_node_profile();
        true
    }

    pub fn objects(&self) -> &Vec<Object> {
        return &self.objects;
    }

    pub fn find_object(&self, code: ObjectCode) -> Option<&Object> {
        for n in 0..self.objects.len() {
            if self.objects[n].code() == code {
                return Some(&self.objects[n]);
            }
        }
        None
    }

    pub fn find_object_mut(&mut self, code: ObjectCode) -> Option<&mut Object> {
        for n in 0..self.objects.len() {
            if self.objects[n].code() == code {
                return Some(&mut self.objects[n]);
            }
        }
        None
    }

    fn set_object_properties_data(&mut self, code: PropertyCode, data: &[u8]) {
        for obj in self.objects.iter_mut() {
            obj.set_property_data(code, data);
        }
    }

    fn set_object_properties_byte(&mut self, code: PropertyCode, v: u8) {
        let data: &[u8] = &[v];
        self.set_object_properties_data(code, data);
    }

    pub fn node_profile_object(&mut self) -> Option<&mut Object> {
        self.find_object_mut(NODE_PROFILE_OBJECT_CODE)
    }

    pub fn add_request_handler(&mut self, handler: RequestHandlerObject) -> bool {
        self.request_mgr.add_handler(handler.clone())
    }

    pub fn add_observer(&mut self, observer: ObserverObject) -> bool {
        self.notify_mgr.add_observer(observer.clone())
    }

    pub fn send_message(&mut self, to_addr: SocketAddr, msg: &mut Message) -> bool {
        self.update_message_trandaction_id(msg);
        self.transport_mgr.send(to_addr, msg)
    }

    pub fn post_message(&mut self, to_addr: SocketAddr, msg: &mut Message) -> Receiver<Message> {
        self.update_message_trandaction_id(msg);
        let (tx, rx): (Sender<Message>, Receiver<Message>) = mpsc::channel();
        self.post_sender = tx;
        self.transport_mgr.send(to_addr, msg);
        rx
    }

    pub fn notify(&mut self, msg: &mut Message) -> bool {
        self.update_message_trandaction_id(msg);
        self.transport_mgr.notify(msg)
    }

    fn create_annouce_property_message(&self, obj: &Object, prop: &Property) -> Message {
        let mut msg = Message::new();
        msg.set_esv(ESV::Notification);
        msg.set_deoj(NODE_PROFILE_OBJECT_CODE);
        msg.set_seoj(obj.code());
        msg.add_property(prop.into());
        msg
    }

    pub fn annouce_property(&mut self, obj: &Object, prop: &Property) -> bool {
        let mut msg = self.create_annouce_property_message(obj, prop);
        self.notify(&mut msg)
    }

    pub fn annouce(&mut self) -> bool {
        let node_profile_obj = self.find_object(NODE_PROFILE_OBJECT_CODE);
        if node_profile_obj.is_none() {
            return false;
        }
        let node_profile_obj = node_profile_obj.unwrap();
        let instance_list_prop =
            node_profile_obj.find_property(NODE_PROFILE_CLASS_INSTANCE_LIST_NOTIFICATION);
        if instance_list_prop.is_none() {
            return false;
        }
        let instance_list_prop = instance_list_prop.unwrap();
        let mut msg = self.create_annouce_property_message(node_profile_obj, instance_list_prop);
        self.notify(&mut msg)
    }

    pub fn has_interface(&self, addr: IpAddr) -> bool {
        self.transport_mgr.has_interface(addr)
    }

    pub fn is_running(&self) -> bool {
        self.transport_mgr.is_running()
    }

    pub fn start(&mut self) -> bool {
        // Starts the transport manager

        if !self.transport_mgr.is_running() {
            if !self.transport_mgr.start() {
                return false;
            }
        }

        // Turns on child objects

        self.set_object_properties_byte(OBJECT_OPERATING_STATUS, OBJECT_OPERATING_STATUS_ON);
        self.set_object_properties_byte(OBJECT_FAULT_STATUS, OBJECT_FAULT_STATUS_OFF);

        // Sets registerd observers to the transport manager.

        for observer in self.notify_mgr.observers() {
            self.transport_mgr.add_observer(observer.clone());
        }

        if !self.annouce() {
            return false;
        }
        true
    }

    pub fn stop(&mut self) -> bool {
        // Turns off child objects

        self.set_object_properties_byte(OBJECT_OPERATING_STATUS, OBJECT_OPERATING_STATUS_OFF);

        // Stops the transport manager

        if !self.transport_mgr.stop() {
            return false;
        }
        true
    }

    fn update_message_trandaction_id(&mut self, msg: &mut Message) {
        if msg.has_tid() {
            return;
        }
        msg.set_tid(self.next_tid());
    }

    fn next_tid(&mut self) -> TID {
        if TID_MAX <= self.last_tid {
            self.last_tid = TID_MIN;
        } else {
            self.last_tid += 1;
        }
        self.last_tid
    }

    fn is_last_message_response(&self, msg: &Message) -> bool {
        if msg.esv().is_request() {
            return false;
        }
        if msg.tid() != self.last_tid {
            return false;
        }
        true
    }

    fn send_post_reopnse(&mut self, msg: Message) {
        match self.post_sender.send(msg) {
            Ok(()) => {}
            Err(_err) => {
                // warn!("{}", err);
            }
        }
        let (tx, _): (Sender<Message>, Receiver<Message>) = mpsc::channel();
        self.post_sender = tx;
    }

    fn update_node_profile(&mut self) {
        let mut obj_codes = Vec::new();
        for obj in self.objects() {
            obj_codes.push(obj.code());
        }
        let node_prof_obj = self.node_profile_object();
        if node_prof_obj.is_none() {
            return;
        }
        let node_prof_obj = node_prof_obj.unwrap();
        let mut node_prof = NodeProfile::from(node_prof_obj);
        node_prof.update(&obj_codes);
    }

    // fn is_local_message(&mut self, msg: &Message) -> bool {
    //     self.transport_mgr.has_interface(msg.from().ip())
    // }

    // fn has_only_node_profile_object(&self) -> bool {
    //     if 1 < self.objects.len() {
    //         return false;
    //     }
    //     true
    // }

    fn message_received(&mut self, req_msg: &Message) -> Option<Message> {
        // Handles only request messages.

        if req_msg.esv().is_response() {
            return None;
        }

        // FIXME: Ignores local message when the node has only the node profile object.
        // if self.is_local_message(&req_msg) && self.has_only_node_profile_object() {
        //     return None;
        // }

        // Part II ECHONET Lite Communication Middleware Specifications
        // 4.2.2 Basic Sequences for Object Control in General

        let dst_obj_code = req_msg.deoj();

        // (A) Processing when the controlled object does not exist

        let dst_obj = self.find_object(dst_obj_code);
        if dst_obj.is_none() {
            return None;
        }
        let dst_obj = dst_obj.unwrap();

        // (B) Processing when the controlled object exists, except when ESV = 0x60 to 0x63, 0x6E and 0x74

        if !req_msg.esv().is_request() {
            return None;
        }

        fn is_valid_request_message(
            dst_obj: &Object,
            req_esv: ESV,
            req_msg_props: &Vec<crate::protocol::Property>,
        ) -> bool {
            for req_msg_prop in req_msg_props.iter() {
                // (C) Processing when the controlled property exists but the controlled property doesn’t exist or can be processed only partially
                let dst_prop = dst_obj.find_property(req_msg_prop.code());
                if dst_prop.is_none() {
                    return false;
                }
                // (D) Processing when the controlled property exists but the stipulated service processing functions are not available
                let dst_prop = dst_prop.unwrap();
                match req_esv {
                    ESV::WriteRequest | ESV::WriteRequestResponseRequired => {
                        if !dst_prop.is_writable() {
                            return false;
                        }
                    }
                    ESV::ReadRequest
                    | ESV::NotificationRequest
                    | ESV::NotificationResponseRequired => {
                        if !dst_prop.is_readable() {
                            return false;
                        }
                    }
                    ESV::WriteReadRequest => {
                        if !dst_prop.is_writable() {
                            return false;
                        }
                        if !dst_prop.is_readable() {
                            return false;
                        }
                    }
                    _ => {
                        return false;
                    }
                }
                // (E) Processing when the controlled property exists and the stipulated service processing functions are available but the EDT size does not match
                match req_esv {
                    ESV::WriteRequest
                    | ESV::WriteRequestResponseRequired
                    | ESV::WriteReadRequest => {
                        if 0 < dst_prop.capacity() && (dst_prop.capacity() < req_msg_prop.size()) {
                            return false;
                        }
                    }
                    _ => {}
                }
            }
            true
        }

        match req_msg.esv() {
            ESV::WriteReadRequest => {
                if !is_valid_request_message(dst_obj, ESV::WriteRequest, req_msg.properties_set()) {
                    return Some(ResponseErrorMessage::from(req_msg));
                }
                if !is_valid_request_message(dst_obj, ESV::ReadRequest, req_msg.properties_get()) {
                    return Some(ResponseErrorMessage::from(req_msg));
                }
            }
            _ => {
                if !is_valid_request_message(dst_obj, req_msg.esv(), req_msg.properties()) {
                    return Some(ResponseErrorMessage::from(req_msg));
                }
            }
        }

        // (F) Processing when the controlled property exists, the stipulated service processing functions are available and also the EDT size matches

        // Verifies whether the request message is allowed by the registered request handlers.

        let dst_obj = self.find_object_mut(dst_obj_code);
        if dst_obj.is_none() {
            return None;
        }
        let dst_obj = dst_obj.unwrap();

        // FIXME: rustc --explain E0499
        // if !self.request_mgr.property_request_received(dst_copy_obj, req_msg) {
        let mut dst_copy_obj = dst_obj.clone();
        if !self
            .request_mgr
            .property_request_received(&mut dst_copy_obj, req_msg)
        {
            return Some(ResponseErrorMessage::from(req_msg));
        }

        let dst_obj = self.find_object_mut(dst_obj_code);
        if dst_obj.is_none() {
            return None;
        }
        let dst_obj = dst_obj.unwrap();

        for dst_copy_prop in dst_copy_obj.properties() {
            let dst_prop = dst_obj.find_property_mut(dst_copy_prop.code());
            if dst_prop.is_none() {
                continue;
            }
            let dst_prop = dst_prop.unwrap();
            dst_prop.set_data(dst_copy_prop.data());
        }

        // Writes request property data to the property.

        let dst_obj = self.find_object_mut(dst_obj_code);
        if dst_obj.is_none() {
            return None;
        }
        let dst_obj = dst_obj.unwrap();

        match req_msg.esv() {
            ESV::WriteRequest | ESV::WriteRequestResponseRequired => {
                for req_msg_prop in req_msg.properties() {
                    let dst_prop = dst_obj.find_property_mut(req_msg_prop.code());
                    if dst_prop.is_none() {
                        continue;
                    }
                    let dst_prop = dst_prop.unwrap();
                    dst_prop.set_data(&req_msg_prop.data().clone());
                }
            }
            ESV::WriteReadRequest => {
                for req_msg_prop in req_msg.properties_set() {
                    let dst_prop = dst_obj.find_property_mut(req_msg_prop.code());
                    if dst_prop.is_none() {
                        continue;
                    }
                    let dst_prop = dst_prop.unwrap();
                    dst_prop.set_data(&req_msg_prop.data().clone());
                }
            }
            _ => {}
        }

        // Generates a response message for the valid request message and returns the response message.

        dst_obj.message_received(req_msg)
    }
}

impl Observer for Arc<Mutex<Node>> {
    fn message_received(&mut self, req_msg: &Message) {
        let Ok(mut node) = self.try_lock() else {
            return;
        };
        if node.is_last_message_response(req_msg) {
            node.send_post_reopnse(req_msg.clone());
        }
        let res_msg = node.message_received(req_msg);
        if res_msg.is_some() {
            let mut res_msg = res_msg.unwrap();
            if res_msg.esv().is_response() {
                if res_msg.esv().is_unicast_response() {
                    node.send_message(SocketAddr::new(req_msg.from().ip(), PORT), &mut res_msg);
                } else {
                    node.notify(&mut res_msg);
                }
            } else {
                warn!("invalid ESV response ({}): {}", res_msg.esv(), res_msg)
            }
        }
    }
}

impl RequestHandler for Arc<Mutex<Node>> {
    fn property_request_received(
        &mut self,
        deoj: &mut Object,
        esv: ESV,
        prop: &crate::protocol::Property,
    ) -> bool {
        if deoj.code() != NODE_PROFILE_OBJECT_CODE {
            return false;
        }
        match esv {
            ESV::ReadRequest | ESV::NotificationRequest => {
                if deoj.find_property(prop.code()).is_none() {
                    return false;
                }
                true
            }
            _ => false,
        }
    }
}

impl Drop for Node {
    fn drop(&mut self) {
        self.stop();
    }
}