Merge pull request #8 from causten/rollingesels

Rollingesels
diff --git a/Makefile b/Makefile
index 61e97b0..eaa7324 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,5 @@
 CXX ?= $(CROSS_COMPILE)g++
 
-TESTER      = testit
-TESTER_OBJ  = testit.o oemhandler.o
-
 LIB_OEM_OBJ = oemhandler.o
 LIB_OEM     = liboemhandler.so
 
@@ -10,17 +7,13 @@
 CXXFLAGS += -fPIC -Wall
 
 
-all:  $(TESTER) $(LIB_OEM)
+all:  $(LIB_OEM)
 
 %.o: %.C
 	$(CXX) -c $< $(CXXFLAGS) -o $@
 
-
 $(LIB_OEM): $(LIB_OEM_OBJ)
 	$(CXX) $^ -shared $(LDFLAGS) -o $@
 
-$(TESTER): $(TESTER_OBJ)
-	$(CXX) $^ $(LDFLAGS) $(LIB_FLAG) -o $@ -ldl
-
 clean:
-	$(RM) $(LIB_OEM_OBJ) $(LIB_OEM) $(TESTER_OBJ) $(TESTER)
+	$(RM) $(LIB_OEM_OBJ) $(LIB_OEM)
diff --git a/oemhandler.C b/oemhandler.C
index ae3f506..f577d67 100644
--- a/oemhandler.C
+++ b/oemhandler.C
@@ -5,7 +5,7 @@
 
 void register_netfn_oem_partial_esel() __attribute__((constructor));
 
-const char *g_esel_path = "/tmp/";
+const char *g_esel_path = "/tmp/esel";
 uint16_t g_record_id = 0x0001;
 
 
@@ -16,7 +16,7 @@
 // of 0xDF and Event Message format of 0x04. The returned
 // Record ID should be used for all partial eSEL adds.
 //
-// This function creates a /tmp/esel# file to store the
+// This function creates a /tmp/esel file to store the
 // incoming partial esel.  It is the role of some other
 // function to commit the error log in to long term
 // storage.  Likely via the ipmi add_sel command.
@@ -25,62 +25,55 @@
                                      ipmi_request_t request, ipmi_response_t response,
                                      ipmi_data_len_t data_len, ipmi_context_t context)
 {
-    esel_request_t *reqptr = (esel_request_t*) request;
-    FILE *fp;
-    // TODO: Issue 5: This is not endian-safe.
-    short *recid  =  (short*) &reqptr->selrecordls;
-    short *offset =  (short*) &reqptr->offsetls;
-    uint8_t rlen;
-    ipmi_ret_t rc = IPMI_CC_OK;
-    char string[64];
-    const char *pio;
+	esel_request_t *reqptr = (esel_request_t*) request;
+	FILE *fp;
+	// TODO: Issue 5: This is not endian-safe.
+	short *recid  =  (short*) &reqptr->selrecordls;
+	short *offset =  (short*) &reqptr->offsetls;
+	uint8_t rlen;
+	ipmi_ret_t rc = IPMI_CC_OK;
+	const char *pio;
 
+	// OpenPOWER Host Interface spec says if RecordID and Offset are
+	// 0 then then this is a new request
+	if (!*recid && !*offset)
+		pio = "wb";
+	else
+		pio = "rb+";
 
-    if (!*recid && !*offset) {
-        // OpenPOWER Host Interface spec says if RecordID and Offset are
-        // 0 then then this is a new request
-        pio = "wb";
-        snprintf(string, sizeof(string), "%s%s%04x", g_esel_path, "esel", g_record_id);
-    } else {
-        pio = "rb+";
-        snprintf(string, sizeof(string), "%s%s%02x%02x", g_esel_path, "esel", reqptr->selrecordms, reqptr->selrecordls);
-    }
+	rlen = (*data_len) - (uint8_t) (sizeof(esel_request_t));
 
-    rlen = (*data_len) - (uint8_t) (sizeof(esel_request_t));
+	printf("IPMI PARTIAL ESEL for %s  Offset = %d Length = %d\n",
+		g_esel_path, *offset, rlen);
 
+	if ((fp = fopen(g_esel_path, pio)) != NULL) {
+		fseek(fp, *offset, SEEK_SET);
+		fwrite(reqptr+1,rlen,1,fp);
+		fclose(fp);
 
-    printf("IPMI PARTIAL ESEL for %s  Offset = %d Length = %d\n",
-        string, *offset, rlen);
+		*data_len = sizeof(g_record_id);
+		memcpy(response, &g_record_id, *data_len);
+	} else {
+		fprintf(stderr, "Error trying to perform %s for esel%s\n",pio, g_esel_path);
+		rc = IPMI_CC_INVALID;
+		*data_len     = 0;
+	}
 
+	// The first bit prepresents that this is the last partial packet
+	// coming down.  If that is the case advance the record id so we
+	// don't overlap logs.  This allows anyone to establish a log
+	// directory system.
+	if (reqptr->progress & 1 ) {
+		g_record_id++;
+	}
 
-    if ((fp = fopen(string, pio)) != NULL) {
-        fseek(fp, *offset, SEEK_SET);
-        fwrite(reqptr+1,rlen,1,fp);
-        fclose(fp);
-
-        *data_len = sizeof(g_record_id);
-        memcpy(response, &g_record_id, *data_len);
-    } else {
-        fprintf(stderr, "Error trying to perform %s for esel%s\n",pio, string);
-        rc = IPMI_CC_INVALID;
-        *data_len     = 0;
-    }
-
-    // The first bit prepresents that this is the last partial packet
-    // coming down.  If that is the case advance the record id so we
-    // don't overlap logs.  This allows anyone to establish a log
-    // directory system.
-    if (reqptr->progress & 1 ) {
-        g_record_id++;
-    }
-
-    return rc;
+	return rc;
 }
 
 
 void register_netfn_oem_partial_esel()
 {
-    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_OEM, IPMI_CMD_PESEL);
-    ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PESEL, NULL, ipmi_ibm_oem_partial_esel);
-    return;
+	printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_OEM, IPMI_CMD_PESEL);
+	ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PESEL, NULL, ipmi_ibm_oem_partial_esel);
+	return;
 }
diff --git a/testit.C b/testit.C
deleted file mode 100644
index 79dfd6a..0000000
--- a/testit.C
+++ /dev/null
@@ -1,141 +0,0 @@
-#include <cstdlib>
-#include <cstring>
-#include <fstream>
-#include <iostream>
-#include <vector>
-#include <host-ipmid/ipmid-api.h>
-#include "oemhandler.h"
-
-using namespace std;
-
-const char* g_filepath = "/tmp/";
-
-const char * getFilePath(void) {    return g_filepath; }
-
-// Number of bytes without the IPMI completion code
-#define MAXRESPONSE 2
-
-
-
-// Returns the length of the file
-std::ifstream::pos_type filesize(const char* filename)
-{
-    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
-    return in.tellg();
-}
-
-
-// Compares a string to the data in a file.
-// Returns 0 if complete match
-int compareData(const char *filename, const char *string, size_t len)
-{
-
-    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
-    std::streamsize size =  in.tellg();
-
-    std::vector<char>      buffer(size);
-
-    in.read(buffer.data(), size);
-
-    if (!std::memcmp(string, buffer.data(), len))
-        return -1;
-
-
-    return 0;
-}
-
-
-void test_multiwrite(unsigned int segment, const char *pString) {
-
-    uint8_t request[1024];
-    uint8_t response[MAXRESPONSE];
-    size_t len, totalString;
-    ipmi_ret_t rc;
-    uint16_t i=0, j;
-
-    esel_request_t  requestheader[]  = {0,0,0,0,0,0};
-    esel_request_t  *pReqHdr = requestheader;
-    uint8_t firstime[] = { 0x00, 0x00, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x20,
-                           0x00, 0x04, 0x0c, 0x1e, 0x07, 0xaa, 0x00, 0x00};
-
-    ipmi_request_t  pRequest  = request;
-    ipmi_response_t pResponse = response;
-    ipmi_data_len_t pLen      = &len;
-
-    totalString = strlen(pString);
-
-    std::memcpy(pRequest, requestheader, sizeof(requestheader));
-    std::memcpy(&request[sizeof(requestheader)], firstime, sizeof(firstime));
-
-    *pLen = sizeof(requestheader) + sizeof(firstime);
-
-    rc = ipmi_ibm_oem_partial_esel(0x3E, 0xF0, pRequest, pResponse, pLen, NULL);
-    if (rc  != IPMI_CC_OK) { printf("Error completion code returned %d\n", rc);}
-    if (len != 2)          { printf("Error data buffer length failed len\n");}
-
-    pReqHdr->selrecordls = response[0];
-    pReqHdr->selrecordms = response[1];
-
-
-    for (i=0; i<totalString; i+=segment) {
-
-        pReqHdr->offsetls = (i&0x00FF);
-        pReqHdr->offsetms = ((i&0xFF00) >> 8);
-
-        // printf("Record id msls 0x%02x%02x\n", pReqHdr->selrecordms, pReqHdr->selrecordls);
-        // printf("Offset 0x%04x , msls = 0x%02x%02x\n", i, pReqHdr->offsetms , pReqHdr->offsetls);
-
-        if (i+segment > totalString) {
-            j = totalString-i;
-        } else {
-            j = segment;
-        }
-
-        std::memcpy(pRequest, requestheader, sizeof(requestheader));
-        std::memcpy(&request[sizeof(requestheader)], pString+i, j);
-        len = sizeof(*requestheader) + j;
-
-        rc = ipmi_ibm_oem_partial_esel(0x3E, 0xF0, pRequest, pResponse, pLen, NULL);
-
-        if (rc  != IPMI_CC_OK) { printf("Error completion code returned %d\n", rc);}
-        if (len != 2)          { printf("Error data buffer length failed\n");}
-
-        pReqHdr->selrecordls = response[0];
-        pReqHdr->selrecordms = response[1];
-
-
-    }
-
-
-    if (filesize("/tmp/esel0100") !=  (unsigned int) strlen(pString)) { printf("Error fileszie mismatch\n");}
-
-    // /tmp/esel000 should be identical to the incoming string
-    rc = compareData("/tmp/esel0100",pString,strlen(pString));
-    if (rc != 0) {printf("Data miscompare %d\n",rc);}
-
-    return;
-}
-
-
-
-// Method that gets called by shared libraries to get their command handlers registered
-void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
-                       ipmi_context_t context, ipmid_callback_t handler)
-{
-}
-
-
-int main()
-{
-
-    const char* shortstring = "C";
-    const char* longstring  = "The President is very much a figurehead - he wields no real power whatsoever. He is apparently chosen by the government, but the qualities he is required to display are not those of leadership but those of finely judged outrage. For this reason the President is always a controversial choice, always an infuriating but fascinating character. His job is not to wield power but to draw attention away from it.";
-
-    test_multiwrite(1,  shortstring);
-
-    test_multiwrite(10, longstring);
-    test_multiwrite(1, longstring);
-    test_multiwrite(100, longstring);
-
-    return 0;
-}