Skip to content

Commit

Permalink
Cleanup command processing
Browse files Browse the repository at this point in the history
move Command map to local variable, added deletion of command classes,
move Command interface to its own header
  • Loading branch information
nmwilson committed Feb 19, 2014
1 parent 2d3d46a commit 51eff7e
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/ChannelCMD.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <MsgHandler.h>
#include <ChannelCMD.h>
#include <IpmiCommandDefines.h>
#include <fstream>
Expand Down
4 changes: 3 additions & 1 deletion src/ChannelCMD.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef CHANNELCMD_H
#define CHANNELCMD_H

class GetChannelAuthCMD:public cmdProcessor{
#include <I_Command.h>

class GetChannelAuthCMD:public I_Command{
public:
int process( const unsigned char* request, unsigned char* response );
};
Expand Down
20 changes: 13 additions & 7 deletions src/ChassisCMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

using namespace IpmiCommandDefines;
extern std::ofstream log_file;
extern cmdMap cmds;


int GetChassisCapabCMD::process( const unsigned char* request, unsigned char* response ){
log_file << "Get Chassis Capabilities Command" << std::endl;
Expand All @@ -32,7 +30,7 @@ void GetChassisStatusCMD::setPowerState(unsigned char powerMask) {
curPowerState = curPowerState && powerMask;
}

int GetChassisStatusCMD::process( const unsigned char* request, unsigned char* response ){
int GetChassisStatusCMD::process( const unsigned char* request, unsigned char* response ){
if (request[0] == 0xFF ) setLastPowerEvent((int)response[0]);
else if (request[0] == 0xFE ) setPowerState((int)response[0]);
else {
Expand All @@ -45,24 +43,32 @@ int GetChassisStatusCMD::process( const unsigned char* request, unsigned char*

return 4;
}
response[0] = COMP_CODE_OK;
return 1;
}

int ChassisCntrlCMD::process( const unsigned char* request, unsigned char* response ){
ChassisCntrlCMD::ChassisCntrlCMD(GetChassisStatusCMD* chassisStatusCmd)
{
statusCmd_ = chassisStatusCmd;
}


int ChassisCntrlCMD::process( const unsigned char* request, unsigned char* response ){
log_file << "Chassis Control Command: " << std::flush;
if (request[DATA_START_INDEX] == 0x00) {
log_file << " power off " << std::endl;
cmds[0x01]->process(new unsigned char (0xFE), new unsigned char (0x1E));
statusCmd_->setPowerState(0x1E);
} else if (request[DATA_START_INDEX] == 0x01) {
log_file << " power on " << std::endl;
cmds[0x01]->process(new unsigned char (0xFF), new unsigned char (0x10));
statusCmd_->setLastPowerEvent(0x10);
}

response[0] = COMP_CODE_OK;

return 1;
}

int ChassisResetCMD::process( const unsigned char* request, unsigned char* response ){
int ChassisResetCMD::process( const unsigned char* request, unsigned char* response ){
response[0] = COMP_CODE_OK;

return 1;
Expand Down
16 changes: 10 additions & 6 deletions src/ChassisCMD.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef CHASSISCMD_H
#define CHASSISCMD_H

class cmdProcessor;
#include <I_Command.h>

class GetChassisCapabCMD:public cmdProcessor{
class GetChassisCapabCMD:public I_Command{
private:
// Capabilities Flag [7 - 4] = 0x0 (reserved)
// [3] = 1 ( power interlock )
Expand All @@ -22,7 +22,7 @@ class GetChassisCapabCMD:public cmdProcessor{
int process( const unsigned char* request, unsigned char* response );
};

class GetChassisStatusCMD:public cmdProcessor{
class GetChassisStatusCMD:public I_Command{
private:
//Current Power State [7] - reserved
// [6 - 5] = 11 (unknown)
Expand Down Expand Up @@ -60,17 +60,21 @@ class GetChassisStatusCMD:public cmdProcessor{
int process( const unsigned char* request, unsigned char* response );
};

class ChassisCntrlCMD:public cmdProcessor{
class ChassisCntrlCMD:public I_Command{
private:
GetChassisStatusCMD* statusCmd_;

public:
ChassisCntrlCMD(GetChassisStatusCMD* chassisStatusCmd);
int process( const unsigned char* request, unsigned char* response );
};

class ChassisResetCMD:public cmdProcessor{
class ChassisResetCMD:public I_Command{
public:
int process( const unsigned char* request, unsigned char* response );
};

class ChassisIdentifyCMD:public cmdProcessor{
class ChassisIdentifyCMD:public I_Command{
public:
int process( const unsigned char* request, unsigned char* response );
};
Expand Down
22 changes: 22 additions & 0 deletions src/I_Command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// I_Command.h
// J3NI
//
// Created by Neil on 2014-02-19.
// Copyright (c) 2014 Neil. All rights reserved.
//

#ifndef I_COMMAND_H
#define I_COMMAND_H

class I_Command{
public:
I_Command() {};
virtual ~I_Command() {};

virtual int process(const unsigned char* request,
unsigned char* response ) = 0;

};

#endif
2 changes: 2 additions & 0 deletions src/IpmiCommandDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace IpmiCommandDefines
const unsigned int MESSAGE_HEADER_LENGTH = 21;

const unsigned int COMP_CODE_OK = 0x00;

const unsigned int MAX_DATA_SIZE = 42;

}

Expand Down
43 changes: 29 additions & 14 deletions src/MsgHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,35 @@

using namespace IpmiCommandDefines;
extern std::ofstream log_file;
extern cmdMap cmds;

MsgHandler::CommandMap MsgHandler::commands_;

void MsgHandler::initCMD() {
cmds[0x38] = new GetChannelAuthCMD();
cmds[0x39] = new GetSessionChalCMD();
cmds[0x3a] = new ActSessionCMD();
cmds[0x3b] = new SetSessionPrivCMD();
cmds[0x3c] = new CloseSessionCMD();
// Chassis Commands
commands_[0x00] = new GetChassisCapabCMD();
GetChassisStatusCMD* chassisStatus = new GetChassisStatusCMD();
commands_[0x01] = chassisStatus;
commands_[0x02] = new ChassisCntrlCMD(chassisStatus);
commands_[0x03] = new ChassisResetCMD();
commands_[0x04] = new ChassisIdentifyCMD();

// Channel Commands
commands_[0x38] = new GetChannelAuthCMD();

cmds[0x00] = new GetChassisCapabCMD();
cmds[0x01] = new GetChassisStatusCMD();
cmds[0x02] = new ChassisCntrlCMD();
cmds[0x03] = new ChassisResetCMD();
cmds[0x04] = new ChassisIdentifyCMD();
//Session Commands
commands_[0x39] = new GetSessionChalCMD();
commands_[0x3a] = new ActSessionCMD();
commands_[0x3b] = new SetSessionPrivCMD();
commands_[0x3c] = new CloseSessionCMD();
}

void MsgHandler::clearCMD()
{
CommandMap::iterator it;
for(it = commands_.begin(); it != commands_.end(); it++)
{
delete it->second;
}
}

bool MsgHandler::isPing(const IpmiMessage& message)
Expand All @@ -46,11 +61,11 @@ void MsgHandler::pong(const IpmiMessage& message, IpmiMessage& response)
void MsgHandler::processRequest(const IpmiMessage& message,
IpmiMessage& response)
{
unsigned char * respData = new unsigned char[42];
unsigned char * respData = new unsigned char[MAX_DATA_SIZE];
int respLen = 1;

if ( cmds.find(message[COMMAND_INDEX]) != cmds.end() ) {
respLen = cmds[message[COMMAND_INDEX]]->process(message.message(), respData);
if ( commands_.find(message[COMMAND_INDEX]) != commands_.end() ) {
respLen = commands_[message[COMMAND_INDEX]]->process(message.message(), respData);
} else {
respData[0] = 0xFF;
}
Expand Down
27 changes: 14 additions & 13 deletions src/MsgHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

#include <map>

class I_Command;
class IpmiMessage;
class cmdProcessor;

typedef std::map<unsigned char, cmdProcessor*> cmdMap;

class cmdProcessor{
public:
virtual int process(const unsigned char* request, unsigned char* response ) = 0;
};

class MsgHandler {
private:

public:
static void initCMD();
static bool isPing(const IpmiMessage& message);
static void pong(const IpmiMessage& message, IpmiMessage& response);
static void processRequest(const IpmiMessage& message, IpmiMessage& response);
public:
typedef std::map<unsigned char, I_Command*> CommandMap;

private:
static CommandMap commands_;

public:
static void initCMD();
static void clearCMD();

static bool isPing(const IpmiMessage& message);
static void pong(const IpmiMessage& message, IpmiMessage& response);
static void processRequest(const IpmiMessage& message, IpmiMessage& response);
};

#endif
1 change: 0 additions & 1 deletion src/SessionCMD.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <MsgHandler.h>
#include <SessionCMD.h>
#include <IpmiCommandDefines.h>
#include <fstream>
Expand Down
10 changes: 6 additions & 4 deletions src/SessionCMD.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#ifndef SESSIONCMD_H
#define SESSIONCMD_H

class GetSessionChalCMD:public cmdProcessor{
#include <I_Command.h>

class GetSessionChalCMD:public I_Command{
public:
int process(const unsigned char* request, unsigned char* response );
};
class ActSessionCMD:public cmdProcessor{
class ActSessionCMD:public I_Command{
public:
int process(const unsigned char* request, unsigned char* response );
};
class SetSessionPrivCMD:public cmdProcessor{
class SetSessionPrivCMD:public I_Command{
public:
int process(const unsigned char* request, unsigned char* response );
};
class CloseSessionCMD:public cmdProcessor{
class CloseSessionCMD:public I_Command{
public:
int process(const unsigned char* request, unsigned char* response );
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ using namespace std;

// Setup local log file
ofstream log_file("J3NI_log_file.log", ios_base::out | ios_base::app );
cmdMap cmds;

int main(int args, char** argv)
{
Expand Down Expand Up @@ -84,6 +83,7 @@ int main(int args, char** argv)
udpDaemon->receiveData();
sleep(2);
}
MsgHandler::clearCMD();

delete udpDaemon;
return 0;
Expand Down

0 comments on commit 51eff7e

Please sign in to comment.