blob: 79dfd6a8965599c14b1d10795b90c690a652c855 [file] [log] [blame]
Chris Austenf79a21f2015-10-13 14:41:40 -05001#include <cstdlib>
2#include <cstring>
3#include <fstream>
4#include <iostream>
5#include <vector>
6#include <host-ipmid/ipmid-api.h>
7#include "oemhandler.h"
8
9using namespace std;
10
11const char* g_filepath = "/tmp/";
12
Patrick Williams24fa5a92015-10-30 14:53:57 -050013const char * getFilePath(void) { return g_filepath; }
Chris Austenf79a21f2015-10-13 14:41:40 -050014
15// Number of bytes without the IPMI completion code
16#define MAXRESPONSE 2
17
18
19
20// Returns the length of the file
21std::ifstream::pos_type filesize(const char* filename)
22{
23 std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
Patrick Williams24fa5a92015-10-30 14:53:57 -050024 return in.tellg();
Chris Austenf79a21f2015-10-13 14:41:40 -050025}
26
27
28// Compares a string to the data in a file.
29// Returns 0 if complete match
Patrick Williams24fa5a92015-10-30 14:53:57 -050030int compareData(const char *filename, const char *string, size_t len)
Chris Austenf79a21f2015-10-13 14:41:40 -050031{
32
Patrick Williams24fa5a92015-10-30 14:53:57 -050033 std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
34 std::streamsize size = in.tellg();
Chris Austenf79a21f2015-10-13 14:41:40 -050035
Patrick Williams24fa5a92015-10-30 14:53:57 -050036 std::vector<char> buffer(size);
Chris Austenf79a21f2015-10-13 14:41:40 -050037
Patrick Williams24fa5a92015-10-30 14:53:57 -050038 in.read(buffer.data(), size);
Chris Austenf79a21f2015-10-13 14:41:40 -050039
Patrick Williams24fa5a92015-10-30 14:53:57 -050040 if (!std::memcmp(string, buffer.data(), len))
41 return -1;
42
Chris Austenf79a21f2015-10-13 14:41:40 -050043
44 return 0;
45}
46
47
48void test_multiwrite(unsigned int segment, const char *pString) {
49
Patrick Williams24fa5a92015-10-30 14:53:57 -050050 uint8_t request[1024];
51 uint8_t response[MAXRESPONSE];
52 size_t len, totalString;
53 ipmi_ret_t rc;
54 uint16_t i=0, j;
Chris Austenf79a21f2015-10-13 14:41:40 -050055
Patrick Williams24fa5a92015-10-30 14:53:57 -050056 esel_request_t requestheader[] = {0,0,0,0,0,0};
57 esel_request_t *pReqHdr = requestheader;
58 uint8_t firstime[] = { 0x00, 0x00, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x20,
59 0x00, 0x04, 0x0c, 0x1e, 0x07, 0xaa, 0x00, 0x00};
Chris Austenf79a21f2015-10-13 14:41:40 -050060
Patrick Williams24fa5a92015-10-30 14:53:57 -050061 ipmi_request_t pRequest = request;
62 ipmi_response_t pResponse = response;
63 ipmi_data_len_t pLen = &len;
Chris Austenf79a21f2015-10-13 14:41:40 -050064
Patrick Williams24fa5a92015-10-30 14:53:57 -050065 totalString = strlen(pString);
Chris Austenf79a21f2015-10-13 14:41:40 -050066
Patrick Williams24fa5a92015-10-30 14:53:57 -050067 std::memcpy(pRequest, requestheader, sizeof(requestheader));
68 std::memcpy(&request[sizeof(requestheader)], firstime, sizeof(firstime));
Chris Austenf79a21f2015-10-13 14:41:40 -050069
Patrick Williams24fa5a92015-10-30 14:53:57 -050070 *pLen = sizeof(requestheader) + sizeof(firstime);
Chris Austenf79a21f2015-10-13 14:41:40 -050071
Patrick Williams24fa5a92015-10-30 14:53:57 -050072 rc = ipmi_ibm_oem_partial_esel(0x3E, 0xF0, pRequest, pResponse, pLen, NULL);
73 if (rc != IPMI_CC_OK) { printf("Error completion code returned %d\n", rc);}
74 if (len != 2) { printf("Error data buffer length failed len\n");}
75
76 pReqHdr->selrecordls = response[0];
77 pReqHdr->selrecordms = response[1];
Chris Austenf79a21f2015-10-13 14:41:40 -050078
79
Patrick Williams24fa5a92015-10-30 14:53:57 -050080 for (i=0; i<totalString; i+=segment) {
Chris Austenf79a21f2015-10-13 14:41:40 -050081
Patrick Williams24fa5a92015-10-30 14:53:57 -050082 pReqHdr->offsetls = (i&0x00FF);
83 pReqHdr->offsetms = ((i&0xFF00) >> 8);
Chris Austenf79a21f2015-10-13 14:41:40 -050084
Patrick Williams24fa5a92015-10-30 14:53:57 -050085 // printf("Record id msls 0x%02x%02x\n", pReqHdr->selrecordms, pReqHdr->selrecordls);
86 // printf("Offset 0x%04x , msls = 0x%02x%02x\n", i, pReqHdr->offsetms , pReqHdr->offsetls);
Chris Austenf79a21f2015-10-13 14:41:40 -050087
Patrick Williams24fa5a92015-10-30 14:53:57 -050088 if (i+segment > totalString) {
89 j = totalString-i;
90 } else {
91 j = segment;
92 }
Chris Austenf79a21f2015-10-13 14:41:40 -050093
Patrick Williams24fa5a92015-10-30 14:53:57 -050094 std::memcpy(pRequest, requestheader, sizeof(requestheader));
95 std::memcpy(&request[sizeof(requestheader)], pString+i, j);
96 len = sizeof(*requestheader) + j;
Chris Austenf79a21f2015-10-13 14:41:40 -050097
Patrick Williams24fa5a92015-10-30 14:53:57 -050098 rc = ipmi_ibm_oem_partial_esel(0x3E, 0xF0, pRequest, pResponse, pLen, NULL);
Chris Austenf79a21f2015-10-13 14:41:40 -050099
Patrick Williams24fa5a92015-10-30 14:53:57 -0500100 if (rc != IPMI_CC_OK) { printf("Error completion code returned %d\n", rc);}
101 if (len != 2) { printf("Error data buffer length failed\n");}
Chris Austenf79a21f2015-10-13 14:41:40 -0500102
Patrick Williams24fa5a92015-10-30 14:53:57 -0500103 pReqHdr->selrecordls = response[0];
104 pReqHdr->selrecordms = response[1];
Chris Austenf79a21f2015-10-13 14:41:40 -0500105
106
Patrick Williams24fa5a92015-10-30 14:53:57 -0500107 }
Chris Austenf79a21f2015-10-13 14:41:40 -0500108
Chris Austenf79a21f2015-10-13 14:41:40 -0500109
Patrick Williams24fa5a92015-10-30 14:53:57 -0500110 if (filesize("/tmp/esel0100") != (unsigned int) strlen(pString)) { printf("Error fileszie mismatch\n");}
Chris Austenf79a21f2015-10-13 14:41:40 -0500111
Patrick Williams24fa5a92015-10-30 14:53:57 -0500112 // /tmp/esel000 should be identical to the incoming string
113 rc = compareData("/tmp/esel0100",pString,strlen(pString));
114 if (rc != 0) {printf("Data miscompare %d\n",rc);}
115
116 return;
Chris Austenf79a21f2015-10-13 14:41:40 -0500117}
118
119
120
121// Method that gets called by shared libraries to get their command handlers registered
122void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
123 ipmi_context_t context, ipmid_callback_t handler)
124{
125}
126
127
128int main()
129{
130
Patrick Williams24fa5a92015-10-30 14:53:57 -0500131 const char* shortstring = "C";
132 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.";
Chris Austenf79a21f2015-10-13 14:41:40 -0500133
Patrick Williams24fa5a92015-10-30 14:53:57 -0500134 test_multiwrite(1, shortstring);
Chris Austenf79a21f2015-10-13 14:41:40 -0500135
Patrick Williams24fa5a92015-10-30 14:53:57 -0500136 test_multiwrite(10, longstring);
137 test_multiwrite(1, longstring);
138 test_multiwrite(100, longstring);
Chris Austenf79a21f2015-10-13 14:41:40 -0500139
Patrick Williams24fa5a92015-10-30 14:53:57 -0500140 return 0;
Chris Austenf79a21f2015-10-13 14:41:40 -0500141}