View Javadoc
1   package org.metricshub.ipmi.core.sm.states;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import org.metricshub.ipmi.core.coding.commands.session.OpenSession;
26  import org.metricshub.ipmi.core.coding.payload.PlainMessage;
27  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
28  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
29  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
30  import org.metricshub.ipmi.core.coding.protocol.decoder.PlainCommandv20Decoder;
31  import org.metricshub.ipmi.core.coding.protocol.decoder.ProtocolDecoder;
32  import org.metricshub.ipmi.core.coding.protocol.decoder.Protocolv20Decoder;
33  import org.metricshub.ipmi.core.coding.rmcp.RmcpMessage;
34  import org.metricshub.ipmi.core.coding.security.CipherSuite;
35  import org.metricshub.ipmi.core.common.TypeConverter;
36  import org.metricshub.ipmi.core.sm.StateMachine;
37  import org.metricshub.ipmi.core.sm.actions.ErrorAction;
38  import org.metricshub.ipmi.core.sm.actions.ResponseAction;
39  import org.metricshub.ipmi.core.sm.events.DefaultAck;
40  import org.metricshub.ipmi.core.sm.events.StateMachineEvent;
41  import org.metricshub.ipmi.core.sm.events.Timeout;
42  
43  /**
44   * Waiting for the {@link OpenSession} response.<br>
45   * <ul>
46   * <li>Transition to {@link OpenSessionComplete} on {@link DefaultAck}</li>
47   * <li>Transition to {@link Authcap} on {@link Timeout}</li>
48   * </ul>
49   */
50  public class OpenSessionWaiting extends State {
51  
52  	private int tag;
53  
54  	public OpenSessionWaiting(int tag) {
55  		this.tag = tag;
56  	}
57  
58  	@Override
59  	public void doTransition(StateMachine stateMachine, StateMachineEvent machineEvent) {
60  		if (machineEvent instanceof DefaultAck) {
61  			stateMachine.setCurrent(new OpenSessionComplete());
62  		} else if (machineEvent instanceof Timeout) {
63  			stateMachine.setCurrent(new Authcap());
64  		} else {
65  			stateMachine.doExternalAction(new ErrorAction(new IllegalArgumentException("Invalid transition")));
66  		}
67  	}
68  
69  	@Override
70  	public void doAction(StateMachine stateMachine, RmcpMessage message) {
71  		if (ProtocolDecoder.decodeAuthenticationType(message) != AuthenticationType.RMCPPlus) {
72  			return; // this isn't IPMI v2.0 message so we ignore it
73  		}
74  		PlainCommandv20Decoder decoder = new PlainCommandv20Decoder(CipherSuite.getEmpty());
75  		if (Protocolv20Decoder.decodePayloadType(message.getData()[1]) != PayloadType.RmcpOpenSessionResponse) {
76  			return;
77  		}
78  		IpmiMessage ipmiMessage = null;
79  		try {
80  			ipmiMessage = decoder.decode(message);
81  			OpenSession openSession = new OpenSession(CipherSuite.getEmpty());
82  			if (openSession.isCommandResponse(ipmiMessage)
83  					&& TypeConverter.byteToInt(((PlainMessage) ipmiMessage.getPayload()).getPayloadData()[0]) == tag) {
84  				stateMachine.doExternalAction(new ResponseAction(openSession.getResponseData(ipmiMessage)));
85  			}
86  		} catch (Exception e) {
87  			stateMachine.setCurrent(new Authcap());
88  			stateMachine.doExternalAction(new ErrorAction(e));
89  		}
90  	}
91  }