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