xref: /openbmc/intel-ipmi-oem/include/spiDev.hpp (revision dcff15068d6e5fb30aa1367115c051178bde70d2)
1c8864062SRajashekar Gade Reddy /*
2c8864062SRajashekar Gade Reddy // Copyright (c) 2019 Intel Corporation
3c8864062SRajashekar Gade Reddy //
4c8864062SRajashekar Gade Reddy // Licensed under the Apache License, Version 2.0 (the "License");
5c8864062SRajashekar Gade Reddy // you may not use this file except in compliance with the License.
6c8864062SRajashekar Gade Reddy // You may obtain a copy of the License at
7c8864062SRajashekar Gade Reddy //
8c8864062SRajashekar Gade Reddy //      http://www.apache.org/licenses/LICENSE-2.0
9c8864062SRajashekar Gade Reddy //
10c8864062SRajashekar Gade Reddy // Unless required by applicable law or agreed to in writing, software
11c8864062SRajashekar Gade Reddy // distributed under the License is distributed on an "AS IS" BASIS,
12c8864062SRajashekar Gade Reddy // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c8864062SRajashekar Gade Reddy // See the License for the specific language governing permissions and
14c8864062SRajashekar Gade Reddy // limitations under the License.
15c8864062SRajashekar Gade Reddy */
16c8864062SRajashekar Gade Reddy 
17c8864062SRajashekar Gade Reddy #pragma once
18c8864062SRajashekar Gade Reddy #include <experimental/filesystem>
19c8864062SRajashekar Gade Reddy 
20c8864062SRajashekar Gade Reddy class SPIDev
21c8864062SRajashekar Gade Reddy {
22c8864062SRajashekar Gade Reddy   private:
23c8864062SRajashekar Gade Reddy     int fd = -1;
24c8864062SRajashekar Gade Reddy 
25c8864062SRajashekar Gade Reddy   public:
26c8864062SRajashekar Gade Reddy     SPIDev() = delete;
27c8864062SRajashekar Gade Reddy     SPIDev(const SPIDev&) = delete;
28c8864062SRajashekar Gade Reddy     SPIDev& operator=(const SPIDev&) = delete;
29c8864062SRajashekar Gade Reddy     SPIDev(SPIDev&&) = delete;
30c8864062SRajashekar Gade Reddy     SPIDev& operator=(SPIDev&&) = delete;
31c8864062SRajashekar Gade Reddy 
SPIDev(const std::string & spiDev)32c8864062SRajashekar Gade Reddy     SPIDev(const std::string& spiDev) :
33c8864062SRajashekar Gade Reddy         fd(open(spiDev.c_str(), O_RDWR | O_CLOEXEC))
34c8864062SRajashekar Gade Reddy     {
35c8864062SRajashekar Gade Reddy         if (fd < 0)
36c8864062SRajashekar Gade Reddy         {
37c8864062SRajashekar Gade Reddy             std::string msg = "Unable to open mtd device. errno=" +
38c8864062SRajashekar Gade Reddy                               std::string(std::strerror(errno));
39c8864062SRajashekar Gade Reddy             throw std::runtime_error(msg);
40c8864062SRajashekar Gade Reddy         }
41c8864062SRajashekar Gade Reddy     }
42c8864062SRajashekar Gade Reddy 
~SPIDev()43c8864062SRajashekar Gade Reddy     virtual ~SPIDev()
44c8864062SRajashekar Gade Reddy     {
45c8864062SRajashekar Gade Reddy         if (!(fd < 0))
46c8864062SRajashekar Gade Reddy         {
47c8864062SRajashekar Gade Reddy             close(fd);
48c8864062SRajashekar Gade Reddy         }
49c8864062SRajashekar Gade Reddy     }
50c8864062SRajashekar Gade Reddy 
spiReadData(uint32_t startAddr,size_t dataLen,void * dataRes)51*dcff1506SVernon Mauery     void spiReadData(uint32_t startAddr, size_t dataLen, void* dataRes)
52c8864062SRajashekar Gade Reddy     {
53c8864062SRajashekar Gade Reddy         if (lseek(fd, startAddr, SEEK_SET) < 0)
54c8864062SRajashekar Gade Reddy         {
55c8864062SRajashekar Gade Reddy             std::string msg = "Failed to do lseek on mtd device. errno=" +
56c8864062SRajashekar Gade Reddy                               std::string(std::strerror(errno));
57c8864062SRajashekar Gade Reddy             throw std::runtime_error(msg);
58c8864062SRajashekar Gade Reddy         }
59c8864062SRajashekar Gade Reddy 
60*dcff1506SVernon Mauery         if (read(fd, dataRes, dataLen) != static_cast<ssize_t>(dataLen))
61c8864062SRajashekar Gade Reddy         {
62c8864062SRajashekar Gade Reddy             std::string msg = "Failed to read on mtd device. errno=" +
63c8864062SRajashekar Gade Reddy                               std::string(std::strerror(errno));
64c8864062SRajashekar Gade Reddy             throw std::runtime_error(msg);
65c8864062SRajashekar Gade Reddy         }
66c8864062SRajashekar Gade Reddy 
67c8864062SRajashekar Gade Reddy         return;
68c8864062SRajashekar Gade Reddy     }
69c8864062SRajashekar Gade Reddy };
70