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
/*
 * Copyright 2011-17 Fraunhofer ISE, energy & meteo Systems GmbH and other contributors
 *
 * 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.
 *
 */
package org.openmuc.josistack;
 
import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
 
import javax.net.SocketFactory;
 
import org.openmuc.jositransport.ClientTSap;
 
/**
 * This class implements the Service Access Point (SAP) for the Application Control Service Element (ACSE) protocol as
 * defined by ISO 8650 or ITU X.217/X.227. The ACSE provides services for establishing and releasing
 * application-associations. The class also realizes the lower ISO Presentation Layer as defined by ISO 8823/ITU X226
 * and the ISO Session Layer as defined by 8327/ITU X.225.
 * 
 */
public final class ClientAcseSap {
 
    public ClientTSap tSap = null;
 
    static final byte[] P_SEL_DEFAULT = { 0, 0, 0, 1 };
    static final byte[] S_SEL_DEFAULT = { 0, 1 };
 
    public byte[] pSelRemote = P_SEL_DEFAULT;
    public byte[] pSelLocal = P_SEL_DEFAULT;
    public byte[] sSelRemote = S_SEL_DEFAULT;
    public byte[] sSelLocal = S_SEL_DEFAULT;
 
    private int[] apTitleCalled = new int[] { 1, 1, 999, 1, 1 };
    private int[] apTitleCalling = new int[] { 1, 1, 999, 1 };
    private int aeQualifierCalled = 12;
    private int aeQualifierCalling = 12;
 
    /**
     * Use this constructor to create a client ACSE Service Access Point (SAP) that will start connections to remote
     * ACSE SAPs. Once constructed the AcseSAP contains a public TSAP that can be accessed to set its configuration.
     */
    public ClientAcseSap() {
        tSap = new ClientTSap();
    }
 
    public ClientAcseSap(SocketFactory socketFactory) {
        tSap = new ClientTSap(socketFactory);
    }
 
    public void setApTitleCalled(int[] title) {
        this.apTitleCalled = title;
    }
 
    public void setApTitleCalling(int[] title) {
        this.apTitleCalling = title;
    }
 
    public void setAeQualifierCalled(int qualifier) {
        this.aeQualifierCalled = qualifier;
    }
 
    public void setAeQualifierCalling(int qualifier) {
        this.aeQualifierCalling = qualifier;
    }
 
    /**
     * Associate to a remote ServerAcseSAP that is listening at the destination address.
     * 
     * @param address
     *            remote InetAddress
     * @param port
     *            remote port
     * @param localAddr
     *            local InetAddress
     * @param localPort
     *            local port
     * @param authenticationParameter
     *            an authentication parameter
     * @param apdu
     *            the payload to send with the association request
     * @return the association object
     * @throws IOException
     *             if an error occurs connecting
     */
    public AcseAssociation associate(InetAddress address, int port, InetAddress localAddr, int localPort,
            String authenticationParameter, ByteBuffer apdu) throws IOException {
        AcseAssociation acseAssociation = new AcseAssociation(null, pSelLocal);
        try {
            acseAssociation.startAssociation(apdu, address, port, localAddr, localPort, authenticationParameter,
                    sSelRemote, sSelLocal, pSelRemote, tSap, apTitleCalled, apTitleCalling, aeQualifierCalled,
                    aeQualifierCalling);
        } catch (IOException e) {
            acseAssociation.disconnect();
            throw e;
        }
        return acseAssociation;
    }
 
}