1*1285115cSWilliam A. Kennington III // Copyright 2021 Google LLC 2*1285115cSWilliam A. Kennington III // 3*1285115cSWilliam A. Kennington III // Licensed under the Apache License, Version 2.0 (the "License"); 4*1285115cSWilliam A. Kennington III // you may not use this file except in compliance with the License. 5*1285115cSWilliam A. Kennington III // You may obtain a copy of the License at 6*1285115cSWilliam A. Kennington III // 7*1285115cSWilliam A. Kennington III // http://www.apache.org/licenses/LICENSE-2.0 8*1285115cSWilliam A. Kennington III // 9*1285115cSWilliam A. Kennington III // Unless required by applicable law or agreed to in writing, software 10*1285115cSWilliam A. Kennington III // distributed under the License is distributed on an "AS IS" BASIS, 11*1285115cSWilliam A. Kennington III // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*1285115cSWilliam A. Kennington III // See the License for the specific language governing permissions and 13*1285115cSWilliam A. Kennington III // limitations under the License. 14*1285115cSWilliam A. Kennington III 15*1285115cSWilliam A. Kennington III #include "handler.hpp" 16*1285115cSWilliam A. Kennington III 17*1285115cSWilliam A. Kennington III #include <cstdint> 18*1285115cSWilliam A. Kennington III #include <memory> 19*1285115cSWilliam A. Kennington III #include <string> 20*1285115cSWilliam A. Kennington III #include <string_view> 21*1285115cSWilliam A. Kennington III #include <vector> 22*1285115cSWilliam A. Kennington III 23*1285115cSWilliam A. Kennington III namespace blobs 24*1285115cSWilliam A. Kennington III { 25*1285115cSWilliam A. Kennington III 26*1285115cSWilliam A. Kennington III namespace 27*1285115cSWilliam A. Kennington III { 28*1285115cSWilliam A. Kennington III constexpr std::string_view metricPath("/metric/snapshot"); 29*1285115cSWilliam A. Kennington III } // namespace 30*1285115cSWilliam A. Kennington III 31*1285115cSWilliam A. Kennington III bool MetricBlobHandler::canHandleBlob(const std::string& path) 32*1285115cSWilliam A. Kennington III { 33*1285115cSWilliam A. Kennington III return path == metricPath; 34*1285115cSWilliam A. Kennington III } 35*1285115cSWilliam A. Kennington III 36*1285115cSWilliam A. Kennington III // A blob handler may have multiple Blobs. For this blob handler, there is only 37*1285115cSWilliam A. Kennington III // one blob. 38*1285115cSWilliam A. Kennington III std::vector<std::string> MetricBlobHandler::getBlobIds() 39*1285115cSWilliam A. Kennington III { 40*1285115cSWilliam A. Kennington III return {std::string(metricPath)}; 41*1285115cSWilliam A. Kennington III } 42*1285115cSWilliam A. Kennington III 43*1285115cSWilliam A. Kennington III // BmcBlobDelete (7) is not supported. 44*1285115cSWilliam A. Kennington III bool MetricBlobHandler::deleteBlob(const std::string&) 45*1285115cSWilliam A. Kennington III { 46*1285115cSWilliam A. Kennington III return false; 47*1285115cSWilliam A. Kennington III } 48*1285115cSWilliam A. Kennington III 49*1285115cSWilliam A. Kennington III // BmcBlobStat (8) (global stat) is not supported. 50*1285115cSWilliam A. Kennington III bool MetricBlobHandler::stat(const std::string&, BlobMeta*) 51*1285115cSWilliam A. Kennington III { 52*1285115cSWilliam A. Kennington III return false; 53*1285115cSWilliam A. Kennington III } 54*1285115cSWilliam A. Kennington III 55*1285115cSWilliam A. Kennington III // Checks for a read-only flag. 56*1285115cSWilliam A. Kennington III bool MetricBlobHandler::isReadOnlyOpenFlags(const uint16_t flags) 57*1285115cSWilliam A. Kennington III { 58*1285115cSWilliam A. Kennington III if (((flags & blobs::OpenFlags::read) == blobs::OpenFlags::read) && 59*1285115cSWilliam A. Kennington III ((flags & blobs::OpenFlags::write) == 0)) 60*1285115cSWilliam A. Kennington III { 61*1285115cSWilliam A. Kennington III return true; 62*1285115cSWilliam A. Kennington III } 63*1285115cSWilliam A. Kennington III return false; 64*1285115cSWilliam A. Kennington III } 65*1285115cSWilliam A. Kennington III 66*1285115cSWilliam A. Kennington III // BmcBlobOpen(2) handler. 67*1285115cSWilliam A. Kennington III bool MetricBlobHandler::open(uint16_t session, uint16_t flags, 68*1285115cSWilliam A. Kennington III const std::string& path) 69*1285115cSWilliam A. Kennington III { 70*1285115cSWilliam A. Kennington III if (!isReadOnlyOpenFlags(flags)) 71*1285115cSWilliam A. Kennington III { 72*1285115cSWilliam A. Kennington III return false; 73*1285115cSWilliam A. Kennington III } 74*1285115cSWilliam A. Kennington III if (!canHandleBlob(path)) 75*1285115cSWilliam A. Kennington III { 76*1285115cSWilliam A. Kennington III return false; 77*1285115cSWilliam A. Kennington III } 78*1285115cSWilliam A. Kennington III if (path == metricPath) 79*1285115cSWilliam A. Kennington III { 80*1285115cSWilliam A. Kennington III std::unique_ptr<metric_blob::BmcHealthSnapshot> bhs = 81*1285115cSWilliam A. Kennington III std::make_unique<metric_blob::BmcHealthSnapshot>(); 82*1285115cSWilliam A. Kennington III bhs.get()->doWork(); 83*1285115cSWilliam A. Kennington III sessions[session] = nullptr; 84*1285115cSWilliam A. Kennington III sessions[session] = std::move(bhs); 85*1285115cSWilliam A. Kennington III return true; 86*1285115cSWilliam A. Kennington III } 87*1285115cSWilliam A. Kennington III return false; 88*1285115cSWilliam A. Kennington III } 89*1285115cSWilliam A. Kennington III 90*1285115cSWilliam A. Kennington III // BmcBlobRead(3) handler. 91*1285115cSWilliam A. Kennington III std::vector<uint8_t> MetricBlobHandler::read(uint16_t session, uint32_t offset, 92*1285115cSWilliam A. Kennington III uint32_t requestedSize) 93*1285115cSWilliam A. Kennington III { 94*1285115cSWilliam A. Kennington III auto it = sessions.find(session); 95*1285115cSWilliam A. Kennington III if (it == sessions.end()) 96*1285115cSWilliam A. Kennington III { 97*1285115cSWilliam A. Kennington III return {}; 98*1285115cSWilliam A. Kennington III } 99*1285115cSWilliam A. Kennington III 100*1285115cSWilliam A. Kennington III std::string_view result = it->second->read(offset, requestedSize); 101*1285115cSWilliam A. Kennington III return std::vector<uint8_t>(result.begin(), result.end()); 102*1285115cSWilliam A. Kennington III } 103*1285115cSWilliam A. Kennington III 104*1285115cSWilliam A. Kennington III // BmcBlobWrite(4) is not supported. 105*1285115cSWilliam A. Kennington III bool MetricBlobHandler::write(uint16_t, uint32_t, const std::vector<uint8_t>&) 106*1285115cSWilliam A. Kennington III { 107*1285115cSWilliam A. Kennington III return false; 108*1285115cSWilliam A. Kennington III } 109*1285115cSWilliam A. Kennington III 110*1285115cSWilliam A. Kennington III // BmcBlobWriteMeta(10) is not supported. 111*1285115cSWilliam A. Kennington III bool MetricBlobHandler::writeMeta(uint16_t, uint32_t, 112*1285115cSWilliam A. Kennington III const std::vector<uint8_t>&) 113*1285115cSWilliam A. Kennington III { 114*1285115cSWilliam A. Kennington III return false; 115*1285115cSWilliam A. Kennington III } 116*1285115cSWilliam A. Kennington III 117*1285115cSWilliam A. Kennington III // BmcBlobCommit(5) is not supported. 118*1285115cSWilliam A. Kennington III bool MetricBlobHandler::commit(uint16_t, const std::vector<uint8_t>&) 119*1285115cSWilliam A. Kennington III { 120*1285115cSWilliam A. Kennington III return false; 121*1285115cSWilliam A. Kennington III } 122*1285115cSWilliam A. Kennington III 123*1285115cSWilliam A. Kennington III // BmcBlobClose(6) handler. 124*1285115cSWilliam A. Kennington III bool MetricBlobHandler::close(uint16_t session) 125*1285115cSWilliam A. Kennington III { 126*1285115cSWilliam A. Kennington III auto itr = sessions.find(session); 127*1285115cSWilliam A. Kennington III if (itr == sessions.end()) 128*1285115cSWilliam A. Kennington III { 129*1285115cSWilliam A. Kennington III return false; 130*1285115cSWilliam A. Kennington III } 131*1285115cSWilliam A. Kennington III sessions.erase(itr); 132*1285115cSWilliam A. Kennington III return true; 133*1285115cSWilliam A. Kennington III } 134*1285115cSWilliam A. Kennington III 135*1285115cSWilliam A. Kennington III // BmcBlobSessionStat(9) handler 136*1285115cSWilliam A. Kennington III bool MetricBlobHandler::stat(uint16_t session, BlobMeta* meta) 137*1285115cSWilliam A. Kennington III { 138*1285115cSWilliam A. Kennington III auto it = sessions.find(session); 139*1285115cSWilliam A. Kennington III if (it == sessions.end()) 140*1285115cSWilliam A. Kennington III { 141*1285115cSWilliam A. Kennington III return false; 142*1285115cSWilliam A. Kennington III } 143*1285115cSWilliam A. Kennington III return it->second->stat(*meta); 144*1285115cSWilliam A. Kennington III } 145*1285115cSWilliam A. Kennington III 146*1285115cSWilliam A. Kennington III bool MetricBlobHandler::expire(uint16_t session) 147*1285115cSWilliam A. Kennington III { 148*1285115cSWilliam A. Kennington III return close(session); 149*1285115cSWilliam A. Kennington III } 150*1285115cSWilliam A. Kennington III 151*1285115cSWilliam A. Kennington III } // namespace blobs 152