]> git.basschouten.com Git - openhab-addons.git/blob
6ccbed0f1b56d1c4818a36a0f770a133067e288e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal.bridge.slip;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velux.internal.bridge.common.Login;
17 import org.openhab.binding.velux.internal.bridge.slip.utils.KLF200Response;
18 import org.openhab.binding.velux.internal.bridge.slip.utils.Packet;
19 import org.openhab.binding.velux.internal.things.VeluxKLFAPI.Command;
20 import org.openhab.binding.velux.internal.things.VeluxKLFAPI.CommandNumber;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Protocol specific bridge communication supported by the Velux bridge:
26  * <B>Authenticate / login</B>
27  * <P>
28  * Common Message semantic: Communication with the bridge and (optionally) storing returned information within the class
29  * itself.
30  * <P>
31  * As 3rd level class it defines informations how to send query and receive answer through the
32  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
33  * as described by the interface {@link org.openhab.binding.velux.internal.bridge.slip.SlipBridgeCommunicationProtocol
34  * SlipBridgeCommunicationProtocol}.
35  * <P>
36  * Methods in addition to the mentioned interface:
37  * <UL>
38  * <LI>{@link #setPassword(String)} to define the authentication reqPassword to be used.</LI>
39  * <LI>{@link #getAuthToken()} to retrieve the authentication reqPassword.</LI>
40  * </UL>
41  *
42  * @see Login
43  * @see SlipBridgeCommunicationProtocol
44  *
45  *
46  * @author Guenther Schreiner - Initial contribution.
47  */
48 @NonNullByDefault
49 class SClogin extends Login implements SlipBridgeCommunicationProtocol {
50     private final Logger logger = LoggerFactory.getLogger(SClogin.class);
51
52     private static final String DESCRIPTION = "Authenticate / login";
53     private static final Command COMMAND = Command.GW_PASSWORD_ENTER_REQ;
54
55     /*
56      * ===========================================================
57      * Message Content Parameters
58      */
59
60     private String reqPassword = "";
61
62     /*
63      * ===========================================================
64      * Message Objects
65      */
66
67     private byte[] requestData = new byte[0];
68
69     /*
70      * ===========================================================
71      * Result Objects
72      */
73
74     private boolean success = false;
75     private boolean finished = false;
76
77     /*
78      * ===========================================================
79      * Methods required for interface {@link SlipBridgeCommunicationProtocol}.
80      */
81
82     @Override
83     public String name() {
84         return DESCRIPTION;
85     }
86
87     @Override
88     public CommandNumber getRequestCommand() {
89         return COMMAND.getCommand();
90     }
91
92     @Override
93     public byte[] getRequestDataAsArrayOfBytes() {
94         requestData = new byte[32];
95         byte[] password = reqPassword.getBytes();
96         System.arraycopy(password, 0, requestData, 0, password.length);
97         return requestData;
98     }
99
100     @Override
101     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
102         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
103         success = false;
104         finished = true;
105         Packet responseData = new Packet(thisResponseData);
106         switch (Command.get(responseCommand)) {
107             case GW_PASSWORD_ENTER_CFM:
108                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 1)) {
109                     break;
110                 }
111                 int cfmStatus = responseData.getOneByteValue(0);
112                 switch (cfmStatus) {
113                     case 0:
114                         logger.debug("setResponse(): bridge connection successfully established (login succeeded).");
115                         success = true;
116                         break;
117                     case 1:
118                         logger.warn("setResponse(): bridge connection successfully established but login failed.");
119                         break;
120                     default:
121                         logger.warn("setResponse(): returned status={} (not defined).", cfmStatus);
122                         break;
123                 }
124                 break;
125
126             default:
127                 KLF200Response.errorLogging(logger, responseCommand);
128                 finished = true;
129         }
130         KLF200Response.outroLogging(logger, success, finished);
131     }
132
133     @Override
134     public boolean isCommunicationFinished() {
135         return finished;
136     }
137
138     @Override
139     public boolean isCommunicationSuccessful() {
140         return success;
141     }
142
143     /*
144      * ===========================================================
145      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
146      */
147
148     @Override
149     public void setPassword(String thisPassword) {
150         logger.trace("setPassword({}) called.", thisPassword.replaceAll(".", "*"));
151         reqPassword = thisPassword;
152         return;
153     }
154
155     @Override
156     public String getAuthToken() {
157         logger.trace("getAuthToken() called, returning {}.", reqPassword.replaceAll(".", "*"));
158         return reqPassword;
159     }
160 }