xref: /openbmc/phosphor-host-ipmid/include/ipmid/sessionhelper.hpp (revision 11d68897e483cfcf7c085aaa2b48a614a431508e)
1*11d68897SWilly Tu #pragma once
2*11d68897SWilly Tu 
3e7023926SRajashekar Gade Reddy #include <sstream>
4e7023926SRajashekar Gade Reddy #include <string>
5e7023926SRajashekar Gade Reddy 
6e7023926SRajashekar Gade Reddy /**
7e7023926SRajashekar Gade Reddy  * @brief parse session input payload.
8e7023926SRajashekar Gade Reddy  *
9e7023926SRajashekar Gade Reddy  * This function retrives the session id and session handle from the session
10e7023926SRajashekar Gade Reddy  * object path.
11e7023926SRajashekar Gade Reddy  * A valid object path will be in the form
12e7023926SRajashekar Gade Reddy  * "/xyz/openbmc_project/ipmi/session/channel/sessionId_sessionHandle"
13e7023926SRajashekar Gade Reddy  *
14e7023926SRajashekar Gade Reddy  * Ex: "/xyz/openbmc_project/ipmi/session/eth0/12a4567d_8a"
15e7023926SRajashekar Gade Reddy  * SessionId    : 0X12a4567d
16e7023926SRajashekar Gade Reddy  * SessionHandle: 0X8a
17e7023926SRajashekar Gade Reddy 
18e7023926SRajashekar Gade Reddy  * @param[in] objectPath - session object path
19e7023926SRajashekar Gade Reddy  * @param[in] sessionId - retrived session id will be asigned.
20e7023926SRajashekar Gade Reddy  * @param[in] sessionHandle - retrived session handle will be asigned.
21e7023926SRajashekar Gade Reddy  *
22e7023926SRajashekar Gade Reddy  * @return true if session id and session handle are retrived else returns
23e7023926SRajashekar Gade Reddy  * false.
24e7023926SRajashekar Gade Reddy  */
25e7023926SRajashekar Gade Reddy bool parseCloseSessionInputPayload(const std::string& objectPath,
26e7023926SRajashekar Gade Reddy                                    uint32_t& sessionId, uint8_t& sessionHandle)
27e7023926SRajashekar Gade Reddy {
28e7023926SRajashekar Gade Reddy     if (objectPath.empty())
29e7023926SRajashekar Gade Reddy     {
30e7023926SRajashekar Gade Reddy         return false;
31e7023926SRajashekar Gade Reddy     }
32e7023926SRajashekar Gade Reddy     // getting the position of session id and session handle string from
33e7023926SRajashekar Gade Reddy     // object path.
34e7023926SRajashekar Gade Reddy     std::size_t ptrPosition = objectPath.rfind("/");
35e7023926SRajashekar Gade Reddy     uint16_t tempSessionHandle = 0;
36e7023926SRajashekar Gade Reddy 
37e7023926SRajashekar Gade Reddy     if (ptrPosition != std::string::npos)
38e7023926SRajashekar Gade Reddy     {
39e7023926SRajashekar Gade Reddy         // get the sessionid & session handle string from the session object
40e7023926SRajashekar Gade Reddy         // path Ex: sessionIdString: "12a4567d_8a"
41e7023926SRajashekar Gade Reddy         std::string sessionIdString = objectPath.substr(ptrPosition + 1);
42e7023926SRajashekar Gade Reddy         std::size_t pos = sessionIdString.rfind("_");
43e7023926SRajashekar Gade Reddy 
44e7023926SRajashekar Gade Reddy         if (pos != std::string::npos)
45e7023926SRajashekar Gade Reddy         {
46e7023926SRajashekar Gade Reddy             // extracting the session handle
47e7023926SRajashekar Gade Reddy             std::string sessionHandleString = sessionIdString.substr(pos + 1);
48e7023926SRajashekar Gade Reddy             // extracting the session id
49e7023926SRajashekar Gade Reddy             sessionIdString = sessionIdString.substr(0, pos);
50e7023926SRajashekar Gade Reddy             // converting session id string  and session handle string to
51e7023926SRajashekar Gade Reddy             // hexadecimal.
52e7023926SRajashekar Gade Reddy             std::stringstream handle(sessionHandleString);
53e7023926SRajashekar Gade Reddy             handle >> std::hex >> tempSessionHandle;
54e7023926SRajashekar Gade Reddy             sessionHandle = tempSessionHandle & 0xFF;
55e7023926SRajashekar Gade Reddy             std::stringstream idString(sessionIdString);
56e7023926SRajashekar Gade Reddy             idString >> std::hex >> sessionId;
57e7023926SRajashekar Gade Reddy             return true;
58e7023926SRajashekar Gade Reddy         }
59e7023926SRajashekar Gade Reddy     }
60e7023926SRajashekar Gade Reddy     return false;
61e7023926SRajashekar Gade Reddy }
62e7023926SRajashekar Gade Reddy 
63e7023926SRajashekar Gade Reddy /**
64e7023926SRajashekar Gade Reddy  * @brief is session object matched.
65e7023926SRajashekar Gade Reddy  *
66e7023926SRajashekar Gade Reddy  * This function checks whether the objectPath contains reqSessionId and
67e7023926SRajashekar Gade Reddy  * reqSessionHandle, e.g., "/xyz/openbmc_project/ipmi/session/eth0/12a4567d_8a"
68e7023926SRajashekar Gade Reddy  * matches sessionId 0x12a4567d and sessionHandle 0x8a.
69e7023926SRajashekar Gade Reddy  *
70e7023926SRajashekar Gade Reddy  * @param[in] objectPath - session object path
71e7023926SRajashekar Gade Reddy  * @param[in] reqSessionId - request session id
72e7023926SRajashekar Gade Reddy  * @param[in] reqSessionHandle - request session handle
73e7023926SRajashekar Gade Reddy  *
74e7023926SRajashekar Gade Reddy  * @return true if the object is matched else return false
75e7023926SRajashekar Gade Reddy  **/
76e7023926SRajashekar Gade Reddy bool isSessionObjectMatched(const std::string objectPath,
77e7023926SRajashekar Gade Reddy                             const uint32_t reqSessionId,
78e7023926SRajashekar Gade Reddy                             const uint8_t reqSessionHandle)
79e7023926SRajashekar Gade Reddy {
80e7023926SRajashekar Gade Reddy     uint32_t sessionId = 0;
81e7023926SRajashekar Gade Reddy     uint8_t sessionHandle = 0;
82e7023926SRajashekar Gade Reddy 
83e7023926SRajashekar Gade Reddy     if (parseCloseSessionInputPayload(objectPath, sessionId, sessionHandle))
84e7023926SRajashekar Gade Reddy     {
85e7023926SRajashekar Gade Reddy         return (reqSessionId == sessionId) ||
86e7023926SRajashekar Gade Reddy                (reqSessionHandle == sessionHandle);
87e7023926SRajashekar Gade Reddy     }
88e7023926SRajashekar Gade Reddy 
89e7023926SRajashekar Gade Reddy     return false;
90e7023926SRajashekar Gade Reddy }
91