blob: 42f169beb0ee57f0e28524c7789d08091486e8e3 [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_IPC_CLIENT_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_IPC_CLIENT_H_
#include <cstdint>
#include <memory>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
#include "tlbmc/hal/shared_mem/segment_manager.h"
#include "tlbmc/hal/shared_mem/sensors.h"
namespace milotic_tlbmc {
class IpcClient {
public:
virtual ~IpcClient() = default;
// Updates the sensor value.
// Returns whether the update is successful.
virtual absl::Status UpdateSensorValue(const std::string& sensor_name,
float value) = 0;
// Updates the sensor value and timestamp.
// Returns whether the update is successful.
virtual absl::Status UpdateSensorValue(const std::string& sensor_name,
float value, uint64_t timestamp) = 0;
};
// The class is thread-safe.
class SharedMemoryClient : public IpcClient {
private:
class Token;
public:
static IpcClient& GetInstance();
// Avoid permission issues in /run/.
static void SetUpInstanceForUnitTest();
SharedMemoryClient(Token token, const std::string& initialized_file_path,
std::unique_ptr<SegmentManager> segment_manager);
virtual ~SharedMemoryClient() = default;
// Updates the sensor value in the shared memory.
absl::Status UpdateSensorValue(const std::string& sensor_name,
float value) override;
absl::Status UpdateSensorValue(const std::string& sensor_name, float value,
uint64_t timestamp) override;
protected:
SharedMemoryClient(const std::string& initialized_file_path,
std::unique_ptr<SegmentManager> segment_manager);
// For testing only. Provides the ability to set `initialized_file_path` which
// works in test environment.
static IpcClient& GetInstance(const std::string& initialized_file_path);
SharedMemoryClient() = default;
SegmentManager* GetSegmentManager()
ABSL_LOCKS_EXCLUDED(segment_manager_mutex_);
void SetSegmentManager(std::unique_ptr<SegmentManager> segment_manager)
ABSL_LOCKS_EXCLUDED(segment_manager_mutex_);
IpcSensor* GetSensor(const std::string& sensor_name)
ABSL_LOCKS_EXCLUDED(sensors_mutex_);
private:
class Token {
private:
explicit Token() = default;
friend SharedMemoryClient;
};
const std::string initialized_file_path_;
absl::Mutex segment_manager_mutex_;
std::unique_ptr<SegmentManager> segment_manager_
ABSL_GUARDED_BY(segment_manager_mutex_);
absl::Mutex sensors_mutex_;
absl::flat_hash_map<std::string, IpcSensor*> sensors_
ABSL_GUARDED_BY(sensors_mutex_);
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_IPC_CLIENT_H_