Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 1 | #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 | |
| 9 | using namespace std; |
| 10 | |
| 11 | const char* g_filepath = "/tmp/"; |
| 12 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 13 | const char * getFilePath(void) { return g_filepath; } |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 14 | |
| 15 | // Number of bytes without the IPMI completion code |
| 16 | #define MAXRESPONSE 2 |
| 17 | |
| 18 | |
| 19 | |
| 20 | // Returns the length of the file |
| 21 | std::ifstream::pos_type filesize(const char* filename) |
| 22 | { |
| 23 | std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 24 | return in.tellg(); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | |
| 28 | // Compares a string to the data in a file. |
| 29 | // Returns 0 if complete match |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 30 | int compareData(const char *filename, const char *string, size_t len) |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 31 | { |
| 32 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 33 | std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); |
| 34 | std::streamsize size = in.tellg(); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 35 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 36 | std::vector<char> buffer(size); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 37 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 38 | in.read(buffer.data(), size); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 39 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 40 | if (!std::memcmp(string, buffer.data(), len)) |
| 41 | return -1; |
| 42 | |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void test_multiwrite(unsigned int segment, const char *pString) { |
| 49 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 50 | 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 55 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 56 | 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 60 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 61 | ipmi_request_t pRequest = request; |
| 62 | ipmi_response_t pResponse = response; |
| 63 | ipmi_data_len_t pLen = &len; |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 64 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 65 | totalString = strlen(pString); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 66 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 67 | std::memcpy(pRequest, requestheader, sizeof(requestheader)); |
| 68 | std::memcpy(&request[sizeof(requestheader)], firstime, sizeof(firstime)); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 69 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 70 | *pLen = sizeof(requestheader) + sizeof(firstime); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 71 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 72 | 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 78 | |
| 79 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 80 | for (i=0; i<totalString; i+=segment) { |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 81 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 82 | pReqHdr->offsetls = (i&0x00FF); |
| 83 | pReqHdr->offsetms = ((i&0xFF00) >> 8); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 84 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 85 | // 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 87 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 88 | if (i+segment > totalString) { |
| 89 | j = totalString-i; |
| 90 | } else { |
| 91 | j = segment; |
| 92 | } |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 93 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 94 | std::memcpy(pRequest, requestheader, sizeof(requestheader)); |
| 95 | std::memcpy(&request[sizeof(requestheader)], pString+i, j); |
| 96 | len = sizeof(*requestheader) + j; |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 97 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 98 | rc = ipmi_ibm_oem_partial_esel(0x3E, 0xF0, pRequest, pResponse, pLen, NULL); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 99 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 100 | 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 102 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 103 | pReqHdr->selrecordls = response[0]; |
| 104 | pReqHdr->selrecordms = response[1]; |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 105 | |
| 106 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 107 | } |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 108 | |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 109 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 110 | if (filesize("/tmp/esel0100") != (unsigned int) strlen(pString)) { printf("Error fileszie mismatch\n");} |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 111 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 112 | // /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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | // Method that gets called by shared libraries to get their command handlers registered |
| 122 | void ipmi_register_callback(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 123 | ipmi_context_t context, ipmid_callback_t handler) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | |
| 128 | int main() |
| 129 | { |
| 130 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 131 | 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 Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 133 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 134 | test_multiwrite(1, shortstring); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 135 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 136 | test_multiwrite(10, longstring); |
| 137 | test_multiwrite(1, longstring); |
| 138 | test_multiwrite(100, longstring); |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 139 | |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 140 | return 0; |
Chris Austen | f79a21f | 2015-10-13 14:41:40 -0500 | [diff] [blame] | 141 | } |