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
use std::{
    net::{IpAddr, SocketAddr},
    time::Duration
};

use crate::{
    build_copt_connect_request, build_s7_read,
    build_s7_setup, build_s7_write, error::*
};
use bytes::BytesMut;
use copt::{
    CoptDecoder, CoptFrame, Parameter, PduType,
    TpduSize
};
use log::debug;
use s7_comm::{
    AckData, DataItemVal, DataItemWriteResponse,
    Frame, S7CommDecoder
};
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::TcpStream,
    time::timeout
};
use tokio_util::codec::Decoder;
use tpkt::{TpktDecoder, TpktFrame};

mod param;
mod request_param;

pub use param::*;
pub use request_param::*;

pub struct S7Client {
    options: Options,
    connect: TcpStream
}

impl S7Client {
    pub async fn connect(
        options: Options
    ) -> Result<Self> {
        let connect =
            tokio::net::TcpStream::connect(
                SocketAddr::new(
                    options.address,
                    options.port
                )
            )
            .await?;
        let mut client =
            Self { options, connect };
        client.copt_connect().await?;
        client.s7_setup().await?;
        Ok(client)
    }

    async fn copt_connect(
        &mut self
    ) -> Result<()> {
        let frame =
            build_framed_copt_connect_request(
                &self.options
            )?;
        self.write_frame(frame).await?;
        let frame =
            self.read_frame().await?.payload();
        if let PduType::ConnectConfirm(comm) =
            &frame.pdu_type
        {
            debug!("{:?}", comm);
            for item in &comm.parameters {
                if let Parameter::TpduSize(size) =
                    item
                {
                    self.options.tpdu_size =
                        size.clone();
                }
            }
        } else {
            return Err(Error::ConnectErr(
                format!(
                    "should recv connect \
                     confirm, but not {:?}",
                    frame
                )
            ));
        }
        Ok(())
    }

    async fn s7_setup(&mut self) -> Result<()> {
        let frame =
            build_framed_s7_setup(&self.options)?;
        self.write_frame(frame).await?;
        let frame =
            self.read_frame().await?.payload();
        if let PduType::DtData(comm) =
            frame.pdu_type
        {
            if let Frame::AckData {
                ack_data,
                ..
            } = comm.payload()
            {
                if let AckData::SetupCommunication(data) = ack_data {
                        debug!("{:?}", data);
                        self.options.pdu_len = data.pdu_length();
                    }
            }
        } else {
            return Err(Error::ConnectErr(
                format!(
                    "should recv connect \
                     confirm, but not {:?}",
                    frame
                )
            ));
        }
        Ok(())
    }

    pub async fn write_db_bytes(
        &mut self,
        db_number: u16,
        byte_addr: u16,
        data: &[u8]
    ) -> Result<Vec<DataItemWriteResponse>> {
        let frame = build_s7_write()
            .pdu_ref(
                self.options.tpdu_size.pdu_ref()
            )
            .write_db_bytes(
                db_number, byte_addr, data
            )
            .build()?;

        self.write(frame).await
    }

    pub async fn write_db_bit(
        &mut self,
        db_number: u16,
        byte_addr: u16,
        bit_addr: u8,
        data: bool
    ) -> Result<Vec<DataItemWriteResponse>> {
        let frame = build_s7_write()
            .pdu_ref(
                self.options.tpdu_size.pdu_ref()
            )
            .write_db_bit(
                db_number, byte_addr, bit_addr,
                data
            )
            .build()?;
        self.write(frame).await
    }

    async fn write(
        &mut self,
        frame: BytesMut
    ) -> Result<Vec<DataItemWriteResponse>> {
        self.write_frame(frame).await?;
        let frame =
            self.read_frame().await?.payload();
        if let PduType::DtData(comm) =
            frame.pdu_type
        {
            if let Frame::AckData {
                ack_data,
                ..
            } = comm.payload()
            {
                if let AckData::WriteVar(data) =
                    ack_data
                {
                    return Ok(data.data_item());
                }
            }
        }
        return Err(Error::Err(format!(
            "should recv read var"
        )));
    }

    pub async fn read(
        &mut self,
        areas: Vec<Area>
    ) -> Result<Vec<DataItemVal>> {
        let frame = build_framed_s7_read(
            &self.options,
            areas
        )?;
        self.write_frame(frame).await?;
        let frame =
            self.read_frame().await?.payload();
        if let PduType::DtData(comm) =
            frame.pdu_type
        {
            if let Frame::AckData {
                ack_data,
                ..
            } = comm.payload()
            {
                if let AckData::ReadVar(data) =
                    ack_data
                {
                    return Ok(data.data_item());
                }
            }
        }
        return Err(Error::Err(format!(
            "should recv read var"
        )));
    }

    async fn write_frame(
        &mut self,
        framed: BytesMut
    ) -> Result<()> {
        timeout(
            self.options.write_timeout,
            self.connect.write_all(&framed)
        )
        .await
        .map_err(|_| Error::WriteTimeout)??;
        Ok(())
    }

    async fn read_frame(
        &mut self
    ) -> Result<TpktFrame<CoptFrame<Frame>>> {
        Ok(timeout(
            self.options.read_timeout,
            read_framed(&mut self.connect)
        )
        .await
        .map_err(|_| Error::WriteTimeout)??)
    }
}

#[derive(Debug, Clone)]
pub struct Options {
    pub read_timeout:  Duration,
    pub write_timeout: Duration,
    address:           IpAddr,
    port:              u16,
    pub conn_mode:     ConnectMode,
    pub tpdu_size:     TpduSize,
    //PDULength variable to store pdu length
    // after connect
    pdu_len:           u16
}

impl Options {
    pub fn new(
        address: IpAddr,
        port: u16,
        conn_mode: ConnectMode
    ) -> Options {
        Self {
            read_timeout: Duration::from_millis(
                500
            ),
            write_timeout: Duration::from_millis(
                500
            ),
            port,
            address,
            conn_mode,
            pdu_len: 480,
            tpdu_size: TpduSize::L2048
        }
    }
}

async fn read_framed(
    req: &mut TcpStream
) -> Result<TpktFrame<CoptFrame<Frame>>> {
    let mut buf = [0u8; 1000];
    let mut bytes = BytesMut::new();
    let mut decoder =
        TpktDecoder(CoptDecoder(S7CommDecoder));
    loop {
        let size = req.read(&mut buf).await?;
        bytes.extend_from_slice(
            buf[0..size].as_ref()
        );
        if let Some(frame) =
            decoder.decode(&mut bytes)?
        {
            return Ok(frame);
        }
    }
}

fn build_framed_s7_read(
    options: &Options,
    areas: Vec<Area>
) -> Result<BytesMut> {
    let mut builder = build_s7_read()
        .pdu_ref(options.tpdu_size.pdu_ref());
    for area in areas {
        builder = builder.add_item(area.into());
    }
    Ok(builder.build()?)
}

fn build_framed_copt_connect_request(
    options: &Options
) -> Result<BytesMut> {
    Ok(build_copt_connect_request()
        .source_ref([0, 1])
        .destination_ref([0, 0])
        .class_and_others(0, false, false)
        .pdu_size(TpduSize::L1024)
        .src_tsap(options.conn_mode.local_tsap())
        .dst_tsap(options.conn_mode.remote_tsap())
        .build_to_request()?)
}

fn build_framed_s7_setup(
    options: &Options
) -> Result<BytesMut> {
    Ok(build_s7_setup()
        .max_amq_called(1)
        .max_amq_calling(1)
        .pdu_length(options.pdu_len)
        .pdu_ref(options.tpdu_size.pdu_ref())
        .build()?)
}