Blame external/opae-test/framework/mock/fpgad_control.h

Packit 534379
// Copyright(c) 2019, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
#ifndef _FPGAD_CONTROL_H
Packit 534379
#define _FPGAD_CONTROL_H
Packit 534379
Packit 534379
#include <string.h>
Packit 534379
#include <stdlib.h>
Packit 534379
#include <unistd.h>
Packit 534379
Packit 534379
#include "gtest/gtest.h"
Packit 534379
Packit 534379
#include <thread>
Packit 534379
#include <chrono>
Packit 534379
Packit 534379
extern "C" {
Packit 534379
Packit 534379
#include "opae_int.h"
Packit 534379
#include "fpgad/api/logging.h"
Packit 534379
#include "fpgad/event_dispatcher_thread.h"
Packit 534379
#include "fpgad/monitor_thread.h"
Packit 534379
#include "fpgad/events_api_thread.h"
Packit 534379
Packit 534379
bool events_api_is_ready(void);
Packit 534379
bool monitor_is_ready(void);
Packit 534379
bool mon_consider_device(struct fpgad_config *c,
Packit 534379
                         fpga_token token);
Packit 534379
Packit 534379
extern fpgad_supported_device default_supported_devices_table[];
Packit 534379
Packit 534379
}
Packit 534379
Packit 534379
namespace opae {
Packit 534379
namespace testing {
Packit 534379
Packit 534379
class fpgad_control {
Packit 534379
 public:
Packit 534379
Packit 534379
  void fpgad_start()
Packit 534379
  {
Packit 534379
    strcpy(tmpfpgad_log_, "tmpfpgad-XXXXXX.log");
Packit 534379
    strcpy(tmpfpgad_pid_, "tmpfpgad-XXXXXX.pid");
Packit 534379
    close(mkstemps(tmpfpgad_log_, 4));
Packit 534379
    close(mkstemps(tmpfpgad_pid_, 4));
Packit 534379
Packit 534379
    memset(&fpgad_config_, 0, sizeof(fpgad_config_));
Packit 534379
    fpgad_config_.poll_interval_usec = 100 * 1000;
Packit 534379
    fpgad_config_.running = true;
Packit 534379
    fpgad_config_.api_socket = "/tmp/fpga_event_socket";
Packit 534379
    strcpy(fpgad_config_.logfile, tmpfpgad_log_);
Packit 534379
    strcpy(fpgad_config_.pidfile, tmpfpgad_pid_);
Packit 534379
    fpgad_config_.supported_devices = default_supported_devices_table;
Packit 534379
Packit 534379
    log_open(tmpfpgad_log_);
Packit 534379
Packit 534379
    dispatcher_config_.global = &fpgad_config_;
Packit 534379
    dispatcher_config_.sched_policy = SCHED_RR;
Packit 534379
    dispatcher_config_.sched_priority = 30;
Packit 534379
Packit 534379
    dispatcher_thr_ = std::thread(event_dispatcher_thread,
Packit 534379
                                  &dispatcher_config_);
Packit 534379
    while (!evt_dispatcher_is_ready()) {
Packit 534379
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
Packit 534379
    }
Packit 534379
Packit 534379
    monitor_config_.global = &fpgad_config_;
Packit 534379
    monitor_config_.sched_policy = SCHED_RR;
Packit 534379
    monitor_config_.sched_priority = 20;
Packit 534379
Packit 534379
    monitor_thr_ = std::thread(monitor_thread, &monitor_config_);
Packit 534379
    while (!monitor_is_ready()) {
Packit 534379
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
Packit 534379
    }
Packit 534379
Packit 534379
    events_config_.global = &fpgad_config_;
Packit 534379
    events_config_.sched_policy = SCHED_RR;
Packit 534379
    events_config_.sched_priority = 10;
Packit 534379
Packit 534379
    events_thr_ = std::thread(events_api_thread, &events_config_);
Packit 534379
    while (!events_api_is_ready()) {
Packit 534379
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
Packit 534379
    }
Packit 534379
  }
Packit 534379
Packit 534379
  void fpgad_stop()
Packit 534379
  {
Packit 534379
    fpgad_config_.running = false;
Packit 534379
Packit 534379
    events_thr_.join();
Packit 534379
    monitor_thr_.join();
Packit 534379
    dispatcher_thr_.join();
Packit 534379
Packit 534379
    log_close();
Packit 534379
Packit 534379
    if (!::testing::Test::HasFatalFailure() &&
Packit 534379
        !::testing::Test::HasNonfatalFailure()) {
Packit 534379
      unlink(tmpfpgad_log_);
Packit 534379
      unlink(tmpfpgad_pid_);
Packit 534379
    }
Packit 534379
  }
Packit 534379
Packit 534379
  bool fpgad_watch()
Packit 534379
  {
Packit 534379
    return mon_enumerate(&fpgad_config_) == 0;
Packit 534379
  }
Packit 534379
Packit 534379
  bool fpgad_watch(fpga_token token)
Packit 534379
  {
Packit 534379
    return mon_consider_device(&fpgad_config_, token);
Packit 534379
  }
Packit 534379
Packit 534379
  char tmpfpgad_log_[20];
Packit 534379
  char tmpfpgad_pid_[20];
Packit 534379
Packit 534379
 private:
Packit 534379
  struct fpgad_config fpgad_config_;
Packit 534379
  event_dispatcher_thread_config dispatcher_config_;
Packit 534379
  std::thread dispatcher_thr_;
Packit 534379
  monitor_thread_config monitor_config_;
Packit 534379
  std::thread monitor_thr_;
Packit 534379
  events_api_thread_config events_config_;
Packit 534379
  std::thread events_thr_;
Packit 534379
};
Packit 534379
Packit 534379
}  // end of namespace testing
Packit 534379
}  // end of namespace opae
Packit 534379
Packit 534379
#endif /* !_FPGAD_CONTROL_H */