<commit_before>#ifndef PROTOCOL_MESSAGES_HPP_
#define PROTOCOL_MESSAGES_HPP_

#include <cstdint>

namespace protocol {
namespace message {

struct heartbeat_message_t {
  enum { ID = 0x00 };

  std::uint8_t seq;
} __attribute__((packed));

struct log_message_t {
  enum { ID = 0x01 };

  char data[100];
} __attribute__((packed));

struct attitude_message_t {
  enum { ID = 0x02 };

  float dcm[9];
} __attribute__((packed));

struct set_arm_state_message_t {
  enum { ID = 0x03 };

  bool armed;
} __attribute__((packed));

struct set_control_mode_message_t {
  enum { ID = 0x04 };

  enum class ControlMode {
    MANUAL,
    OFFBOARD
  };

  ControlMode mode;
} __attribute__((packed));

struct offboard_attitude_control_message_t {
  enum { ID = 0x05 };

  float roll;
  float pitch;
  float yaw;
  float throttle;
  uint16_t buttons;   // Bitfield of buttons
  uint8_t mode;
} __attribute__((packed));

struct motor_throttle_message_t {
  enum { ID = 0x06 };

  float throttles[4];
} __attribute__((packed));

struct sensor_calibration_request_message_t {
  enum { ID = 0x07 };
} __attribute__((packed));

struct sensor_calibration_response_message_t {
  enum { ID = 0x08 };

  enum class SensorType {
    ACCEL,
    GYRO,
    MAG
  };

  SensorType type;
  float offsets[3];
} __attribute__((packed));

struct location_message_t {
  enum { ID = 0x09 };

  float lat;
  float lon;
  float alt;
} __attribute__((packed));

inline std::uint16_t length(int id) {
  // TODO(kyle): sizeof(empty struct) is 1 in C++...
  switch(id) {
    case heartbeat_message_t::ID:
      return sizeof(heartbeat_message_t);
    case log_message_t::ID:
      return sizeof(log_message_t);
    case attitude_message_t::ID:
      return sizeof(attitude_message_t);
    case set_arm_state_message_t::ID:
      return sizeof(set_arm_state_message_t);
    case set_control_mode_message_t::ID:
      return sizeof(set_control_mode_message_t);
    case offboard_attitude_control_message_t::ID:
      return sizeof(offboard_attitude_control_message_t);
    case motor_throttle_message_t::ID:
      return sizeof(motor_throttle_message_t);
    case sensor_calibration_request_message_t::ID:
      return sizeof(sensor_calibration_request_message_t);
    case sensor_calibration_response_message_t::ID:
      return sizeof(sensor_calibration_response_message_t);
    case location_message_t::ID:
      return sizeof(location_message_t);
  }

  return 0; // TODO(kyle): Return something more meaningful?
}

}
}

#endif // MESSAGES_HPP_

<commit_msg>Add message types.<commit_after>#ifndef PROTOCOL_MESSAGES_HPP_
#define PROTOCOL_MESSAGES_HPP_

#include <cstdint>

namespace protocol {
namespace message {

struct heartbeat_message_t {
  enum { ID = 0x00 };

  std::uint8_t seq;
} __attribute__((packed));

struct log_message_t {
  enum { ID = 0x01 };

  char data[100];
} __attribute__((packed));

struct attitude_message_t {
  enum { ID = 0x02 };

  float dcm[9];
} __attribute__((packed));

struct set_arm_state_message_t {
  enum { ID = 0x03 };

  bool armed;
} __attribute__((packed));

struct set_control_mode_message_t {
  enum { ID = 0x04 };

  enum class ControlMode {
    MANUAL,
    OFFBOARD
  };

  ControlMode mode;
} __attribute__((packed));

struct offboard_attitude_control_message_t {
  enum { ID = 0x05 };

  float roll;
  float pitch;
  float yaw;
  float throttle;
  uint16_t buttons;   // Bitfield of buttons
  uint8_t mode;
} __attribute__((packed));

struct motor_throttle_message_t {
  enum { ID = 0x06 };

  float throttles[4];
} __attribute__((packed));

struct sensor_calibration_request_message_t {
  enum { ID = 0x07 };
} __attribute__((packed));

struct sensor_calibration_response_message_t {
  enum { ID = 0x08 };

  enum class SensorType {
    ACCEL,
    GYRO,
    MAG
  };

  SensorType type;
  float offsets[3];
} __attribute__((packed));

struct location_message_t {
  enum { ID = 0x09 };

  float lat;
  float lon;
  float alt;
} __attribute__((packed));

struct imu_message_t {
  enum { ID = 0x0a };

  float gyro[3];
  float accel[3];
} __attribute__((packed));

struct system_message_t {
  enum { ID = 0x0b };

  uint8_t state;
  float motorDC;   // TODO(yoos): Hack for esra test launch
} __attribute__((packed));

inline std::uint16_t length(int id) {
  // TODO(kyle): sizeof(empty struct) is 1 in C++...
  switch(id) {
    case heartbeat_message_t::ID:
      return sizeof(heartbeat_message_t);
    case log_message_t::ID:
      return sizeof(log_message_t);
    case attitude_message_t::ID:
      return sizeof(attitude_message_t);
    case set_arm_state_message_t::ID:
      return sizeof(set_arm_state_message_t);
    case set_control_mode_message_t::ID:
      return sizeof(set_control_mode_message_t);
    case offboard_attitude_control_message_t::ID:
      return sizeof(offboard_attitude_control_message_t);
    case motor_throttle_message_t::ID:
      return sizeof(motor_throttle_message_t);
    case sensor_calibration_request_message_t::ID:
      return sizeof(sensor_calibration_request_message_t);
    case sensor_calibration_response_message_t::ID:
      return sizeof(sensor_calibration_response_message_t);
    case location_message_t::ID:
      return sizeof(location_message_t);
    case imu_message_t::ID:
      return sizeof(imu_message_t);
    case system_message_t::ID:
      return sizeof(system_message_t);
  }

  return 0; // TODO(kyle): Return something more meaningful?
}

}
}

#endif // MESSAGES_HPP_

<|endoftext|>


<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file provides the embedder's side of random webkit glue functions.

#include "build/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#endif

#include <vector>

#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/common/clipboard_messages.h"
#include "content/common/socket_stream_dispatcher.h"
#include "content/common/view_messages.h"
#include "content/plugin/npobject_util.h"
#include "content/renderer/render_thread.h"
#include "googleurl/src/url_util.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/scoped_clipboard_writer_glue.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/websocketstreamhandle_bridge.h"

#if !defined(DISABLE_NACL)
#include "native_client/src/shared/imc/nacl_imc.h"
#include "native_client/src/trusted/plugin/nacl_entry_points.h"
#endif

#if defined(OS_LINUX)
#include "content/renderer/renderer_sandbox_support_linux.h"
#endif

// This definition of WriteBitmapFromPixels uses shared memory to communicate
// across processes.
void ScopedClipboardWriterGlue::WriteBitmapFromPixels(const void* pixels,
                                                      const gfx::Size& size) {
  // Do not try to write a bitmap more than once
  if (shared_buf_)
    return;

  uint32 buf_size = 4 * size.width() * size.height();

  // Allocate a shared memory buffer to hold the bitmap bits.
#if defined(OS_POSIX)
  // On POSIX, we need to ask the browser to create the shared memory for us,
  // since this is blocked by the sandbox.
  base::SharedMemoryHandle shared_mem_handle;
  ViewHostMsg_AllocateSharedMemoryBuffer *msg =
      new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size,
                                                 &shared_mem_handle);
  if (RenderThread::current()->Send(msg)) {
    if (base::SharedMemory::IsHandleValid(shared_mem_handle)) {
      shared_buf_ = new base::SharedMemory(shared_mem_handle, false);
      if (!shared_buf_ || !shared_buf_->Map(buf_size)) {
        NOTREACHED() << "Map failed";
        return;
      }
    } else {
      NOTREACHED() << "Browser failed to allocate shared memory";
      return;
    }
  } else {
    NOTREACHED() << "Browser allocation request message failed";
    return;
  }
#else  // !OS_POSIX
  shared_buf_ = new base::SharedMemory;
  if (!shared_buf_->CreateAndMapAnonymous(buf_size)) {
    NOTREACHED();
    return;
  }
#endif

  // Copy the bits into shared memory
  memcpy(shared_buf_->memory(), pixels, buf_size);
  shared_buf_->Unmap();

  ui::Clipboard::ObjectMapParam size_param;
  const char* size_data = reinterpret_cast<const char*>(&size);
  for (size_t i = 0; i < sizeof(gfx::Size); ++i)
    size_param.push_back(size_data[i]);

  ui::Clipboard::ObjectMapParams params;

  // The first parameter is replaced on the receiving end with a pointer to
  // a shared memory object containing the bitmap. We reserve space for it here.
  ui::Clipboard::ObjectMapParam place_holder_param;
  params.push_back(place_holder_param);
  params.push_back(size_param);
  objects_[ui::Clipboard::CBF_SMBITMAP] = params;
}

// Define a destructor that makes IPCs to flush the contents to the
// system clipboard.
ScopedClipboardWriterGlue::~ScopedClipboardWriterGlue() {
  if (objects_.empty())
    return;

  if (shared_buf_) {
    RenderThread::current()->Send(
        new ClipboardHostMsg_WriteObjectsSync(objects_,
                shared_buf_->handle()));
    delete shared_buf_;
    return;
  }

  RenderThread::current()->Send(
      new ClipboardHostMsg_WriteObjectsAsync(objects_));
}

namespace webkit_glue {

void AppendToLog(const char* file, int line, const char* msg) {
  logging::LogMessage(file, line).stream() << msg;
}

base::StringPiece GetDataResource(int resource_id) {
  return ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
}

#if defined(OS_WIN)
HCURSOR LoadCursor(int cursor_id) {
  return ResourceBundle::GetSharedInstance().LoadCursor(cursor_id);
}
#endif

// Clipboard glue

ui::Clipboard* ClipboardGetClipboard() {
  return NULL;
}

bool ClipboardIsFormatAvailable(const ui::Clipboard::FormatType& format,
                                ui::Clipboard::Buffer buffer) {
  bool result;
  RenderThread::current()->Send(
      new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result));
  return result;
}

void ClipboardReadAvailableTypes(ui::Clipboard::Buffer buffer,
                                 std::vector<string16>* types,
                                 bool* contains_filenames) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
      buffer, types, contains_filenames));
}

void ClipboardReadText(ui::Clipboard::Buffer buffer, string16* result) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadText(buffer, result));
}

void ClipboardReadAsciiText(ui::Clipboard::Buffer buffer, std::string* result) {
  RenderThread::current()->Send(
      new ClipboardHostMsg_ReadAsciiText(buffer, result));
}

void ClipboardReadHTML(ui::Clipboard::Buffer buffer, string16* markup,
                       GURL* url) {
  RenderThread::current()->Send(
      new ClipboardHostMsg_ReadHTML(buffer, markup, url));
}

void ClipboardReadImage(ui::Clipboard::Buffer buffer, std::string* data) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadImage(buffer, data));
}

bool ClipboardReadData(ui::Clipboard::Buffer buffer, const string16& type,
                       string16* data, string16* metadata) {
  bool result = false;
  RenderThread::current()->Send(new ClipboardHostMsg_ReadData(
      buffer, type, &result, data, metadata));
  return result;
}

bool ClipboardReadFilenames(ui::Clipboard::Buffer buffer,
                            std::vector<string16>* filenames) {
  bool result;
  RenderThread::current()->Send(new ClipboardHostMsg_ReadFilenames(
      buffer, &result, filenames));
  return result;
}

void GetPlugins(bool refresh,
                std::vector<webkit::npapi::WebPluginInfo>* plugins) {
  if (!RenderThread::current()->plugin_refresh_allowed())
    refresh = false;
  RenderThread::current()->Send(new ViewHostMsg_GetPlugins(refresh, plugins));
}

bool IsProtocolSupportedForMedia(const GURL& url) {
  // If new protocol is to be added here, we need to make sure the response is
  // validated accordingly in the media engine.
  if (url.SchemeIsFile() || url.SchemeIs(chrome::kHttpScheme) ||
      url.SchemeIs(chrome::kHttpsScheme) ||
      url.SchemeIs(chrome::kDataScheme) ||
      url.SchemeIs(chrome::kExtensionScheme) ||
      url.SchemeIs(chrome::kBlobScheme))
    return true;
  return false;
}

// static factory function
ResourceLoaderBridge* ResourceLoaderBridge::Create(
    const ResourceLoaderBridge::RequestInfo& request_info) {
  return ChildThread::current()->CreateBridge(request_info);
}

// static factory function
WebSocketStreamHandleBridge* WebSocketStreamHandleBridge::Create(
    WebKit::WebSocketStreamHandle* handle,
    WebSocketStreamHandleDelegate* delegate) {
  SocketStreamDispatcher* dispatcher =
      ChildThread::current()->socket_stream_dispatcher();
  return dispatcher->CreateBridge(handle, delegate);
}

void CloseCurrentConnections() {
  RenderThread::current()->CloseCurrentConnections();
}

void SetCacheMode(bool enabled) {
  RenderThread::current()->SetCacheMode(enabled);
}

void ClearCache(bool preserve_ssl_host_info) {
  RenderThread::current()->ClearCache(preserve_ssl_host_info);
}

void ClearHostResolverCache() {
  RenderThread::current()->ClearHostResolverCache();
}

void ClearPredictorCache() {
  RenderThread::current()->ClearPredictorCache();
}

std::string GetProductVersion() {
  chrome::VersionInfo version_info;
  std::string product("Chrome/");
  product += version_info.is_valid() ? version_info.Version()
                                     : "0.0.0.0";
  return product;
}

bool IsSingleProcess() {
  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
}

void EnableSpdy(bool enable) {
  RenderThread::current()->EnableSpdy(enable);
}

void UserMetricsRecordAction(const std::string& action) {
  RenderThread::current()->Send(
      new ViewHostMsg_UserMetricsRecordAction(action));
}

#if !defined(DISABLE_NACL)
bool LaunchSelLdr(const char* alleged_url, int socket_count, void* imc_handles,
                  void* nacl_process_handle, int* nacl_process_id) {
  std::vector<nacl::FileDescriptor> sockets;
  base::ProcessHandle nacl_process;
  if (!RenderThread::current()->Send(
    new ViewHostMsg_LaunchNaCl(
        ASCIIToWide(alleged_url),
        socket_count,
        &sockets,
        &nacl_process,
        reinterpret_cast<base::ProcessId*>(nacl_process_id)))) {
    return false;
  }
  CHECK(static_cast<int>(sockets.size()) == socket_count);
  for (int i = 0; i < socket_count; i++) {
    static_cast<nacl::Handle*>(imc_handles)[i] =
        nacl::ToNativeHandle(sockets[i]);
  }
  *static_cast<nacl::Handle*>(nacl_process_handle) = nacl_process;
  return true;
}
#endif

#if defined(OS_LINUX)
int MatchFontWithFallback(const std::string& face, bool bold,
                          bool italic, int charset) {
  return renderer_sandbox_support::MatchFontWithFallback(
      face, bold, italic, charset);
}

bool GetFontTable(int fd, uint32_t table, uint8_t* output,
                  size_t* output_length) {
  return renderer_sandbox_support::GetFontTable(
      fd, table, output, output_length);
}
#endif

}  // namespace webkit_glue
<commit_msg>Enabled media source content from filesystem: schema.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file provides the embedder's side of random webkit glue functions.

#include "build/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#endif

#include <vector>

#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/common/clipboard_messages.h"
#include "content/common/socket_stream_dispatcher.h"
#include "content/common/view_messages.h"
#include "content/plugin/npobject_util.h"
#include "content/renderer/render_thread.h"
#include "googleurl/src/url_util.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/resource/resource_bundle.h"
#include "webkit/glue/scoped_clipboard_writer_glue.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/websocketstreamhandle_bridge.h"

#if !defined(DISABLE_NACL)
#include "native_client/src/shared/imc/nacl_imc.h"
#include "native_client/src/trusted/plugin/nacl_entry_points.h"
#endif

#if defined(OS_LINUX)
#include "content/renderer/renderer_sandbox_support_linux.h"
#endif

// This definition of WriteBitmapFromPixels uses shared memory to communicate
// across processes.
void ScopedClipboardWriterGlue::WriteBitmapFromPixels(const void* pixels,
                                                      const gfx::Size& size) {
  // Do not try to write a bitmap more than once
  if (shared_buf_)
    return;

  uint32 buf_size = 4 * size.width() * size.height();

  // Allocate a shared memory buffer to hold the bitmap bits.
#if defined(OS_POSIX)
  // On POSIX, we need to ask the browser to create the shared memory for us,
  // since this is blocked by the sandbox.
  base::SharedMemoryHandle shared_mem_handle;
  ViewHostMsg_AllocateSharedMemoryBuffer *msg =
      new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size,
                                                 &shared_mem_handle);
  if (RenderThread::current()->Send(msg)) {
    if (base::SharedMemory::IsHandleValid(shared_mem_handle)) {
      shared_buf_ = new base::SharedMemory(shared_mem_handle, false);
      if (!shared_buf_ || !shared_buf_->Map(buf_size)) {
        NOTREACHED() << "Map failed";
        return;
      }
    } else {
      NOTREACHED() << "Browser failed to allocate shared memory";
      return;
    }
  } else {
    NOTREACHED() << "Browser allocation request message failed";
    return;
  }
#else  // !OS_POSIX
  shared_buf_ = new base::SharedMemory;
  if (!shared_buf_->CreateAndMapAnonymous(buf_size)) {
    NOTREACHED();
    return;
  }
#endif

  // Copy the bits into shared memory
  memcpy(shared_buf_->memory(), pixels, buf_size);
  shared_buf_->Unmap();

  ui::Clipboard::ObjectMapParam size_param;
  const char* size_data = reinterpret_cast<const char*>(&size);
  for (size_t i = 0; i < sizeof(gfx::Size); ++i)
    size_param.push_back(size_data[i]);

  ui::Clipboard::ObjectMapParams params;

  // The first parameter is replaced on the receiving end with a pointer to
  // a shared memory object containing the bitmap. We reserve space for it here.
  ui::Clipboard::ObjectMapParam place_holder_param;
  params.push_back(place_holder_param);
  params.push_back(size_param);
  objects_[ui::Clipboard::CBF_SMBITMAP] = params;
}

// Define a destructor that makes IPCs to flush the contents to the
// system clipboard.
ScopedClipboardWriterGlue::~ScopedClipboardWriterGlue() {
  if (objects_.empty())
    return;

  if (shared_buf_) {
    RenderThread::current()->Send(
        new ClipboardHostMsg_WriteObjectsSync(objects_,
                shared_buf_->handle()));
    delete shared_buf_;
    return;
  }

  RenderThread::current()->Send(
      new ClipboardHostMsg_WriteObjectsAsync(objects_));
}

namespace webkit_glue {

void AppendToLog(const char* file, int line, const char* msg) {
  logging::LogMessage(file, line).stream() << msg;
}

base::StringPiece GetDataResource(int resource_id) {
  return ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
}

#if defined(OS_WIN)
HCURSOR LoadCursor(int cursor_id) {
  return ResourceBundle::GetSharedInstance().LoadCursor(cursor_id);
}
#endif

// Clipboard glue

ui::Clipboard* ClipboardGetClipboard() {
  return NULL;
}

bool ClipboardIsFormatAvailable(const ui::Clipboard::FormatType& format,
                                ui::Clipboard::Buffer buffer) {
  bool result;
  RenderThread::current()->Send(
      new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result));
  return result;
}

void ClipboardReadAvailableTypes(ui::Clipboard::Buffer buffer,
                                 std::vector<string16>* types,
                                 bool* contains_filenames) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
      buffer, types, contains_filenames));
}

void ClipboardReadText(ui::Clipboard::Buffer buffer, string16* result) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadText(buffer, result));
}

void ClipboardReadAsciiText(ui::Clipboard::Buffer buffer, std::string* result) {
  RenderThread::current()->Send(
      new ClipboardHostMsg_ReadAsciiText(buffer, result));
}

void ClipboardReadHTML(ui::Clipboard::Buffer buffer, string16* markup,
                       GURL* url) {
  RenderThread::current()->Send(
      new ClipboardHostMsg_ReadHTML(buffer, markup, url));
}

void ClipboardReadImage(ui::Clipboard::Buffer buffer, std::string* data) {
  RenderThread::current()->Send(new ClipboardHostMsg_ReadImage(buffer, data));
}

bool ClipboardReadData(ui::Clipboard::Buffer buffer, const string16& type,
                       string16* data, string16* metadata) {
  bool result = false;
  RenderThread::current()->Send(new ClipboardHostMsg_ReadData(
      buffer, type, &result, data, metadata));
  return result;
}

bool ClipboardReadFilenames(ui::Clipboard::Buffer buffer,
                            std::vector<string16>* filenames) {
  bool result;
  RenderThread::current()->Send(new ClipboardHostMsg_ReadFilenames(
      buffer, &result, filenames));
  return result;
}

void GetPlugins(bool refresh,
                std::vector<webkit::npapi::WebPluginInfo>* plugins) {
  if (!RenderThread::current()->plugin_refresh_allowed())
    refresh = false;
  RenderThread::current()->Send(new ViewHostMsg_GetPlugins(refresh, plugins));
}

bool IsProtocolSupportedForMedia(const GURL& url) {
  // If new protocol is to be added here, we need to make sure the response is
  // validated accordingly in the media engine.
  if (url.SchemeIsFile() || url.SchemeIs(chrome::kHttpScheme) ||
      url.SchemeIs(chrome::kHttpsScheme) ||
      url.SchemeIs(chrome::kDataScheme) ||
      url.SchemeIs(chrome::kExtensionScheme) ||
      url.SchemeIs(chrome::kFileSystemScheme) ||
      url.SchemeIs(chrome::kBlobScheme))
    return true;
  return false;
}

// static factory function
ResourceLoaderBridge* ResourceLoaderBridge::Create(
    const ResourceLoaderBridge::RequestInfo& request_info) {
  return ChildThread::current()->CreateBridge(request_info);
}

// static factory function
WebSocketStreamHandleBridge* WebSocketStreamHandleBridge::Create(
    WebKit::WebSocketStreamHandle* handle,
    WebSocketStreamHandleDelegate* delegate) {
  SocketStreamDispatcher* dispatcher =
      ChildThread::current()->socket_stream_dispatcher();
  return dispatcher->CreateBridge(handle, delegate);
}

void CloseCurrentConnections() {
  RenderThread::current()->CloseCurrentConnections();
}

void SetCacheMode(bool enabled) {
  RenderThread::current()->SetCacheMode(enabled);
}

void ClearCache(bool preserve_ssl_host_info) {
  RenderThread::current()->ClearCache(preserve_ssl_host_info);
}

void ClearHostResolverCache() {
  RenderThread::current()->ClearHostResolverCache();
}

void ClearPredictorCache() {
  RenderThread::current()->ClearPredictorCache();
}

std::string GetProductVersion() {
  chrome::VersionInfo version_info;
  std::string product("Chrome/");
  product += version_info.is_valid() ? version_info.Version()
                                     : "0.0.0.0";
  return product;
}

bool IsSingleProcess() {
  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
}

void EnableSpdy(bool enable) {
  RenderThread::current()->EnableSpdy(enable);
}

void UserMetricsRecordAction(const std::string& action) {
  RenderThread::current()->Send(
      new ViewHostMsg_UserMetricsRecordAction(action));
}

#if !defined(DISABLE_NACL)
bool LaunchSelLdr(const char* alleged_url, int socket_count, void* imc_handles,
                  void* nacl_process_handle, int* nacl_process_id) {
  std::vector<nacl::FileDescriptor> sockets;
  base::ProcessHandle nacl_process;
  if (!RenderThread::current()->Send(
    new ViewHostMsg_LaunchNaCl(
        ASCIIToWide(alleged_url),
        socket_count,
        &sockets,
        &nacl_process,
        reinterpret_cast<base::ProcessId*>(nacl_process_id)))) {
    return false;
  }
  CHECK(static_cast<int>(sockets.size()) == socket_count);
  for (int i = 0; i < socket_count; i++) {
    static_cast<nacl::Handle*>(imc_handles)[i] =
        nacl::ToNativeHandle(sockets[i]);
  }
  *static_cast<nacl::Handle*>(nacl_process_handle) = nacl_process;
  return true;
}
#endif

#if defined(OS_LINUX)
int MatchFontWithFallback(const std::string& face, bool bold,
                          bool italic, int charset) {
  return renderer_sandbox_support::MatchFontWithFallback(
      face, bold, italic, charset);
}

bool GetFontTable(int fd, uint32_t table, uint8_t* output,
                  size_t* output_length) {
  return renderer_sandbox_support::GetFontTable(
      fd, table, output, output_length);
}
#endif

}  // namespace webkit_glue
<|endoftext|>


<commit_before>#ifndef INCLUDED_U5E_UTF8_ITERATOR
#define INCLUDED_U5E_UTF8_ITERATOR

#include <cmath>
#include <iterator>
#include <u5e/utf8_utils.hpp>
#include <u5e/codepoint.hpp>
#include <u5e/iterator_assertion.hpp>

namespace u5e {
  /**
   * u5e::utf8_iterator
   *
   * Iterates over codepoints on utf8 data.
   *
   * Since this iterator manipulates data in octets and offers
   * codepoints, it can't offer a mutable interface. For write access,
   * you need to use an utf8_output_iterator.
   */
  template <typename WRAPPEDITERATOR>
  class utf8_iterator_base {
  protected:
    iterator_assertion<WRAPPEDITERATOR, char> _assertions;
    WRAPPEDITERATOR raw_iterator_;

  public:
    typedef codepoint value_type;
    typedef const codepoint& reference;
    typedef int difference_type;
    typedef std::bidirectional_iterator_tag iterator_category;

    inline utf8_iterator_base(const WRAPPEDITERATOR raw_iterator)
      : raw_iterator_(raw_iterator) { };

    inline void forward_one_codepoint() {
      difference_type size = utf8_utils::codepoint_size(*raw_iterator_);
      raw_iterator_ += size;
    }

    inline bool rewind_to_start_of_codepoint(const char current_octet) {
      // when we do '*it = codepoint', we will leave the iterator
      // halfway into the next character
      if (__builtin_clz(~((current_octet)<<24)) == 1) {
        while ((*(raw_iterator_) & 0b11000000) == 0b10000000) {
          raw_iterator_--;
        }
        return true;
      } else {
        return false;
      }
    }

    inline void rewind_one_codepoint() {
      rewind_to_start_of_codepoint(*raw_iterator_);
      raw_iterator_--;
      while ((*(raw_iterator_) & 0b11000000) == 0b10000000) {
        raw_iterator_--;
      }
    }

    const codepoint current_codepoint() {
      char first_octet = *raw_iterator_;
      if ((first_octet & 0b10000000) == 0) {
        return first_octet;
      } else {
        if (rewind_to_start_of_codepoint(first_octet)) {
          first_octet = *raw_iterator_;
        }
        WRAPPEDITERATOR copy_ = raw_iterator_;
        difference_type size = utf8_utils::codepoint_size(first_octet);
        unsigned char mask_first_octet = ~(0xFF<<(7-size));
        int value = (first_octet & mask_first_octet);
        while (--size) {
          value = value<<6 | (*(++copy_) & 0b00111111);
        }
        return value;
      }
    }

  };

  template <typename WRAPPEDITERATOR>
  class utf8_const_iterator
    : public utf8_iterator_base<WRAPPEDITERATOR> {
  public:
    typedef utf8_const_iterator pointer;

    inline utf8_const_iterator(const WRAPPEDITERATOR raw_iterator)
      : utf8_iterator_base<WRAPPEDITERATOR>(raw_iterator) { };

    inline utf8_const_iterator(const utf8_const_iterator& tocopy)
      : utf8_iterator_base<WRAPPEDITERATOR>(tocopy.raw_iterator_) { };

    inline utf8_const_iterator& operator++() {
      this->forward_one_codepoint();
      return *this;
    }

    inline utf8_const_iterator operator++(int junk) {
      utf8_const_iterator copy(this->raw_iterator_);
      ++(*this);
      return copy;
    }

    inline utf8_const_iterator& operator--() {
      this->rewind_one_codepoint();
      return *this;
    }

    inline utf8_const_iterator operator--(int junk) {
      utf8_const_iterator copy(this->raw_iterator_);
      --(*this);
      return copy;
    }

    inline bool operator==(const utf8_const_iterator& rhs) const {
      return this->raw_iterator_ == rhs.raw_iterator_;
    }

    inline bool operator!=(const utf8_const_iterator& rhs) const {
      return this->raw_iterator_ != rhs.raw_iterator_;
    }

    inline const codepoint operator*() {
      return this->current_codepoint();
    }

  };

  template <typename WRAPPEDITERATOR>
  class utf8_iterator
    : public utf8_iterator_base<WRAPPEDITERATOR> {
  public:
    typedef utf8_iterator pointer;

    inline utf8_iterator(const WRAPPEDITERATOR raw_iterator)
      : utf8_iterator_base<WRAPPEDITERATOR>(raw_iterator) {};

    inline utf8_iterator(const utf8_iterator& tocopy)
      : utf8_iterator_base<WRAPPEDITERATOR>(tocopy.raw_iterator_) {};

    inline utf8_iterator& operator++() {
      this->forward_one_codepoint();
      return *this;
    }

    inline utf8_iterator operator++(int junk) {
      utf8_iterator copy(this->raw_iterator_);
      ++(*this);
      return copy;
    }

    inline utf8_iterator& operator--() {
      this->rewind_one_codepoint();
      return *this;
    }

    inline utf8_iterator operator--(int junk) {
      utf8_iterator copy(this->raw_iterator_);
      --(*this);
      return copy;
    }

    inline bool operator==(const utf8_iterator& rhs) const {
      return this->raw_iterator_ == rhs.raw_iterator_;
    }

    inline bool operator!=(const utf8_iterator& rhs) const {
      return this->raw_iterator_ != rhs.raw_iterator_;
    }

    class proxyobject : public codepoint {
    private:
      utf8_iterator<WRAPPEDITERATOR>& ref;
    public:
      proxyobject(utf8_iterator<WRAPPEDITERATOR>& refin)
        :ref(refin) {
        utf8_iterator<WRAPPEDITERATOR> copy = refin;
        value = copy.current_codepoint().value;
      };
      proxyobject& operator=(const codepoint c) {
        int value = c.value; // operate on codepoint as integer
        int size = std::ceil((float)(32 - __builtin_clz(value)) / (float)6);
        if (size <= 1) {
          *(ref.raw_iterator_) = (value & 0xFF);
        } else {
          utf8_iterator<WRAPPEDITERATOR> copy = ref;
          unsigned char first_octet = (0xFF<<(8-size));
          first_octet |= ((value>>((size-1)*6)) & 0xFF);
          *(copy.raw_iterator_) = first_octet;
          copy.raw_iterator_++;
          while (--size) {
            unsigned char octet = 0b10000000;
            octet |= ((value>>((size-1)*6)) & 0b00111111);
            *(copy.raw_iterator_) = octet;
            copy.raw_iterator_++;
          }
        }
        return *this;
      }
    };

    // non const version returns a proxy object
    inline proxyobject operator*() {
      return proxyobject(*this);
    }

  };

};

#endif
<commit_msg>- replaced += with std::advance: allows for bidi iterator rather than random access iterator as underlying type for utf8_iterator<commit_after>#ifndef INCLUDED_U5E_UTF8_ITERATOR
#define INCLUDED_U5E_UTF8_ITERATOR

#include <cmath>
#include <iterator>
#include <u5e/utf8_utils.hpp>
#include <u5e/codepoint.hpp>
#include <u5e/iterator_assertion.hpp>

namespace u5e {
  /**
   * u5e::utf8_iterator
   *
   * Iterates over codepoints on utf8 data.
   *
   * Since this iterator manipulates data in octets and offers
   * codepoints, it can't offer a mutable interface. For write access,
   * you need to use an utf8_output_iterator.
   */
  template <typename WRAPPEDITERATOR>
  class utf8_iterator_base {
  protected:
    iterator_assertion<WRAPPEDITERATOR, char> _assertions;
    WRAPPEDITERATOR raw_iterator_;

  public:
    typedef codepoint value_type;
    typedef const codepoint& reference;
    typedef int difference_type;
    typedef std::bidirectional_iterator_tag iterator_category;

    inline utf8_iterator_base(const WRAPPEDITERATOR raw_iterator)
      : raw_iterator_(raw_iterator) { };

    inline void forward_one_codepoint() {
      difference_type size = utf8_utils::codepoint_size(*raw_iterator_);
      std::advance(raw_iterator_, size);
    }

    inline bool rewind_to_start_of_codepoint(const char current_octet) {
      // when we do '*it = codepoint', we will leave the iterator
      // halfway into the next character
      if (__builtin_clz(~((current_octet)<<24)) == 1) {
        while ((*(raw_iterator_) & 0b11000000) == 0b10000000) {
          raw_iterator_--;
        }
        return true;
      } else {
        return false;
      }
    }

    inline void rewind_one_codepoint() {
      rewind_to_start_of_codepoint(*raw_iterator_);
      raw_iterator_--;
      while ((*(raw_iterator_) & 0b11000000) == 0b10000000) {
        raw_iterator_--;
      }
    }

    const codepoint current_codepoint() {
      char first_octet = *raw_iterator_;
      if ((first_octet & 0b10000000) == 0) {
        return first_octet;
      } else {
        if (rewind_to_start_of_codepoint(first_octet)) {
          first_octet = *raw_iterator_;
        }
        WRAPPEDITERATOR copy_ = raw_iterator_;
        difference_type size = utf8_utils::codepoint_size(first_octet);
        unsigned char mask_first_octet = ~(0xFF<<(7-size));
        int value = (first_octet & mask_first_octet);
        while (--size) {
          value = value<<6 | (*(++copy_) & 0b00111111);
        }
        return value;
      }
    }

  };

  template <typename WRAPPEDITERATOR>
  class utf8_const_iterator
    : public utf8_iterator_base<WRAPPEDITERATOR> {
  public:
    typedef utf8_const_iterator pointer;

    inline utf8_const_iterator(const WRAPPEDITERATOR raw_iterator)
      : utf8_iterator_base<WRAPPEDITERATOR>(raw_iterator) { };

    inline utf8_const_iterator(const utf8_const_iterator& tocopy)
      : utf8_iterator_base<WRAPPEDITERATOR>(tocopy.raw_iterator_) { };

    inline utf8_const_iterator& operator++() {
      this->forward_one_codepoint();
      return *this;
    }

    inline utf8_const_iterator operator++(int junk) {
      utf8_const_iterator copy(this->raw_iterator_);
      ++(*this);
      return copy;
    }

    inline utf8_const_iterator& operator--() {
      this->rewind_one_codepoint();
      return *this;
    }

    inline utf8_const_iterator operator--(int junk) {
      utf8_const_iterator copy(this->raw_iterator_);
      --(*this);
      return copy;
    }

    inline bool operator==(const utf8_const_iterator& rhs) const {
      return this->raw_iterator_ == rhs.raw_iterator_;
    }

    inline bool operator!=(const utf8_const_iterator& rhs) const {
      return this->raw_iterator_ != rhs.raw_iterator_;
    }

    inline const codepoint operator*() {
      return this->current_codepoint();
    }

  };

  template <typename WRAPPEDITERATOR>
  class utf8_iterator
    : public utf8_iterator_base<WRAPPEDITERATOR> {
  public:
    typedef utf8_iterator pointer;

    inline utf8_iterator(const WRAPPEDITERATOR raw_iterator)
      : utf8_iterator_base<WRAPPEDITERATOR>(raw_iterator) {};

    inline utf8_iterator(const utf8_iterator& tocopy)
      : utf8_iterator_base<WRAPPEDITERATOR>(tocopy.raw_iterator_) {};

    inline utf8_iterator& operator++() {
      this->forward_one_codepoint();
      return *this;
    }

    inline utf8_iterator operator++(int junk) {
      utf8_iterator copy(this->raw_iterator_);
      ++(*this);
      return copy;
    }

    inline utf8_iterator& operator--() {
      this->rewind_one_codepoint();
      return *this;
    }

    inline utf8_iterator operator--(int junk) {
      utf8_iterator copy(this->raw_iterator_);
      --(*this);
      return copy;
    }

    inline bool operator==(const utf8_iterator& rhs) const {
      return this->raw_iterator_ == rhs.raw_iterator_;
    }

    inline bool operator!=(const utf8_iterator& rhs) const {
      return this->raw_iterator_ != rhs.raw_iterator_;
    }

    class proxyobject : public codepoint {
    private:
      utf8_iterator<WRAPPEDITERATOR>& ref;
    public:
      proxyobject(utf8_iterator<WRAPPEDITERATOR>& refin)
        :ref(refin) {
        utf8_iterator<WRAPPEDITERATOR> copy = refin;
        value = copy.current_codepoint().value;
      };
      proxyobject& operator=(const codepoint c) {
        int value = c.value; // operate on codepoint as integer
        int size = std::ceil((float)(32 - __builtin_clz(value)) / (float)6);
        if (size <= 1) {
          *(ref.raw_iterator_) = (value & 0xFF);
        } else {
          utf8_iterator<WRAPPEDITERATOR> copy = ref;
          unsigned char first_octet = (0xFF<<(8-size));
          first_octet |= ((value>>((size-1)*6)) & 0xFF);
          *(copy.raw_iterator_) = first_octet;
          copy.raw_iterator_++;
          while (--size) {
            unsigned char octet = 0b10000000;
            octet |= ((value>>((size-1)*6)) & 0b00111111);
            *(copy.raw_iterator_) = octet;
            copy.raw_iterator_++;
          }
        }
        return *this;
      }
    };

    // non const version returns a proxy object
    inline proxyobject operator*() {
      return proxyobject(*this);
    }

  };

};

#endif
<|endoftext|>


<commit_before>//----------------------------------------------------------------------------
/// \file  variant_tree.hpp
//----------------------------------------------------------------------------
/// \brief This file contains a tree class that can hold variant values.
//----------------------------------------------------------------------------
// Author:  Serge Aleynikov
// Created: 2010-07-10
//----------------------------------------------------------------------------
/*
***** BEGIN LICENSE BLOCK *****

This file may be included in various open-source projects.

Copyright (C) 2010 Serge Aleynikov <saleyn@gmail.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

***** END LICENSE BLOCK *****
*/

#ifndef _UTIL_VARIANT_TREE_HPP_
#define _UTIL_VARIANT_TREE_HPP_

#include <util/variant.hpp>
#include <util/typeinfo.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/throw_exception.hpp>

namespace boost {
namespace property_tree {
    // Custom translator that works with util::variant instead of std::string.
    // This translator is used to read/write values from files.
    template <>
    struct translator_between<util::variant, std::string>
    {
        typedef translator_between<util::variant, std::string> type;
        typedef std::string     external_type;
        typedef util::variant   internal_type;

        boost::optional<external_type> get_value(const internal_type& value) const {
            return boost::optional<external_type>(
                value.type() == internal_type::TYPE_NULL ? "" : value.to_string());
        }

        boost::optional<internal_type> put_value(const external_type& value) const {
            try {
                long n = lexical_cast<long>(value);
                for(external_type::const_iterator it=value.begin(), end=value.end(); it!=end; ++it)
                    if (*it < '0' || *it > '9')
                        throw false;
                return boost::optional<internal_type>(n);
            } catch (...) {}
            try {
                double n = lexical_cast<double>(value);
                return boost::optional<internal_type>(n);
            } catch (...) {}
            if (value == "true" || value == "false")
                return boost::optional<internal_type>(value[0] == 't');
            return boost::optional<internal_type>(value);
        }
    };

} // namespace property_tree
} // namespace boost

namespace util {

namespace detail {

    // Custom translator that works with variant instead of std::string
    // This translator is used to get/put values through explicit get/put calls.
    template <class Ext>
    struct variant_translator
    {
        typedef Ext external_type;
        typedef variant internal_type;

        /*
        typedef boost::mpl::joint_view<variant::int_types,
                boost::mpl::vector<bool,double>
            > valid_non_string_types;
        */
        typedef variant::valid_types valid_types;

        external_type get_value(const internal_type& value) const {
            return value.get<external_type>();
        }

        template<typename T>
        typename boost::disable_if<
            boost::is_same<
                boost::mpl::end<valid_types>::type,
                boost::mpl::find<variant::valid_types, T>
            >,
            internal_type>::type
        put_value(T value) const {
            return variant(value);
        }
    };

    typedef boost::property_tree::basic_ptree<
            std::string,                    // Key type
            variant,                        // Data type
            std::less<std::string>          // Key comparison
    > basic_variant_tree;

} // namespace detail

class variant_tree : public detail::basic_variant_tree
{
    typedef detail::basic_variant_tree base;
    typedef variant_tree self_type;
public:
    typedef boost::property_tree::ptree_bad_path bad_path;

    variant_tree() {}

    template <typename Source>
    static void read_info(Source& src, variant_tree& tree) {
        boost::property_tree::info_parser::read_info(src, tree);
    }

    template <typename Target>
    static void write_info(Target& tar, variant_tree& tree) {
        boost::property_tree::info_parser::write_info(tar, tree);
    }

    template <typename Target, typename Settings>
    static void write_info(Target& tar, variant_tree& tree, const Settings& tt) {
        boost::property_tree::info_parser::write_info(tar, tree, tt);
    }

    template <class T>
    T get_value() const {
        using boost::throw_exception;

        if(boost::optional<T> o =
                base::get_value_optional<T>(detail::variant_translator<T>())) {
            return *o;
        }
        BOOST_PROPERTY_TREE_THROW(boost::property_tree::ptree_bad_data(
            std::string("conversion of data to type \"") +
            typeid(T).name() + "\" failed", base::data()));
    }

    template <class T>
    T get_value(const T& default_value) const {
        return base::get_value(default_value, detail::variant_translator<T>());
    }

    std::string get_value(const char* default_value) const {
        return base::get_value(std::string(default_value),
            detail::variant_translator<std::string>());
    }

    template <class T>
    boost::optional<T> get_value_optional() const {
        return base::get_value(detail::variant_translator<T>());
    }

    template <class T>
    void put_value(const T& value) {
        base::put_value(value, detail::variant_translator<T>());
    }

    template <class T>
    T get(const path_type& path) const {
        try {
            return base::get_child(path).BOOST_NESTED_TEMPLATE
                get_value<T>(detail::variant_translator<T>());
        } catch (boost::bad_get& e) {
            std::stringstream s;
            s << "Cannot convert value to type '" << type_to_string<T>() << "'";
            throw bad_path(s.str(), path);
        }
    }

    template <class T>
    T get(const path_type& path, const T& default_value) const {
        try {
            return base::get(path, default_value, detail::variant_translator<T>());
        } catch (boost::bad_get& e) {
            throw bad_path("Wrong or missing value type", path);
        }
    }

    std::string get(const path_type& path, const char* default_value) const {
        return base::get(path, std::string(default_value),
            detail::variant_translator<std::string>());
    }

    template <class T>
    boost::optional<T> get_optional(const path_type& path) const {
        return base::get_optional(path, detail::variant_translator<T>());
    }

    template <class T>
    void put(const path_type& path, const T& value) {
        base::put(path, value, detail::variant_translator<T>());
    }

    template <class T>
    self_type& add(const path_type& path, const T& value) {
        return static_cast<self_type&>(
            base::add(path, value, detail::variant_translator<T>()));
    }

    void swap(variant_tree& rhs) {
        base::swap(rhs);
    }
    void swap(boost::property_tree::basic_ptree<
        std::string, variant, std::less<std::string> >& rhs) {
        base::swap(rhs);
    }

    self_type &get_child(const path_type &path) {
        return static_cast<self_type&>(base::get_child(path));
    }

    /** Get the child at the given path, or throw @c ptree_bad_path. */
    const self_type &get_child(const path_type &path) const {
        return static_cast<const self_type&>(base::get_child(path));
    }

    /** Get the child at the given path, or return @p default_value. */
    self_type &get_child(const path_type &path, self_type &default_value) {
        return static_cast<self_type&>(base::get_child(path, default_value));
    }

    /** Get the child at the given path, or return @p default_value. */
    const self_type &get_child(const path_type &path,
                               const self_type &default_value) const {
        return static_cast<const self_type&>(base::get_child(path, default_value));
    }

    /** Get the child at the given path, or return boost::null. */
    boost::optional<self_type &> get_child_optional(const path_type &path) {
        boost::optional<base&> o = base::get_child_optional(path);
        if (!o)
            return boost::optional<self_type&>();
        return boost::optional<self_type&>(static_cast<self_type&>(*o));
    }

    /** Get the child at the given path, or return boost::null. */
    boost::optional<const self_type &>
    get_child_optional(const path_type &path) const {
        boost::optional<const base&> o = base::get_child_optional(path);
        if (!o)
            return boost::optional<const self_type&>();
        return boost::optional<const self_type&>(static_cast<const self_type&>(*o));
    }

    self_type &put_child(const path_type &path, const self_type &value) {
        return static_cast<self_type&>(base::put_child(path, value));
    }
};

} // namespace util

#endif // _UTIL_VARIANT_TREE_HPP_
<commit_msg>Fixed casting problem<commit_after>//----------------------------------------------------------------------------
/// \file  variant_tree.hpp
//----------------------------------------------------------------------------
/// \brief This file contains a tree class that can hold variant values.
//----------------------------------------------------------------------------
// Author:  Serge Aleynikov
// Created: 2010-07-10
//----------------------------------------------------------------------------
/*
***** BEGIN LICENSE BLOCK *****

This file may be included in various open-source projects.

Copyright (C) 2010 Serge Aleynikov <saleyn@gmail.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

***** END LICENSE BLOCK *****
*/

#ifndef _UTIL_VARIANT_TREE_HPP_
#define _UTIL_VARIANT_TREE_HPP_

#include <util/variant.hpp>
#include <util/typeinfo.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/throw_exception.hpp>

namespace boost {
namespace property_tree {
    // Custom translator that works with util::variant instead of std::string.
    // This translator is used to read/write values from files.
    template <>
    struct translator_between<util::variant, std::string>
    {
        typedef translator_between<util::variant, std::string> type;
        typedef std::string     external_type;
        typedef util::variant   internal_type;

        boost::optional<external_type> get_value(const internal_type& value) const {
            return boost::optional<external_type>(
                value.type() == internal_type::TYPE_NULL ? "" : value.to_string());
        }

        boost::optional<internal_type> put_value(const external_type& value) const {
            try {
                long n = lexical_cast<long>(value);
                for(external_type::const_iterator it=value.begin(), end=value.end(); it!=end; ++it)
                    if (*it < '0' || *it > '9')
                        throw false;
                return boost::optional<internal_type>(n);
            } catch (...) {}
            try {
                double n = lexical_cast<double>(value);
                return boost::optional<internal_type>(n);
            } catch (...) {}
            if (value == "true" || value == "false")
                return boost::optional<internal_type>(value[0] == 't');
            return boost::optional<internal_type>(value);
        }
    };

} // namespace property_tree
} // namespace boost

namespace util {

namespace detail {

    // Custom translator that works with variant instead of std::string
    // This translator is used to get/put values through explicit get/put calls.
    template <class Ext>
    struct variant_translator
    {
        typedef Ext external_type;
        typedef variant internal_type;

        /*
        typedef boost::mpl::joint_view<variant::int_types,
                boost::mpl::vector<bool,double>
            > valid_non_string_types;
        */
        typedef variant::valid_types valid_types;

        external_type get_value(const internal_type& value) const {
            return value.get<external_type>();
        }

        template<typename T>
        typename boost::disable_if<
            boost::is_same<
                boost::mpl::end<valid_types>::type,
                boost::mpl::find<variant::valid_types, T>
            >,
            internal_type>::type
        put_value(T value) const {
            return variant(value);
        }
    };

    typedef boost::property_tree::basic_ptree<
            std::string,                    // Key type
            variant,                        // Data type
            std::less<std::string>          // Key comparison
    > basic_variant_tree;

} // namespace detail

class variant_tree : public detail::basic_variant_tree
{
    typedef detail::basic_variant_tree base;
    typedef variant_tree self_type;
public:
    typedef boost::property_tree::ptree_bad_path bad_path;
    typedef std::pair<std::string, variant_tree> value_type;

    variant_tree() {}
    variant_tree(const detail::basic_variant_tree& a_rhs)
        : detail::basic_variant_tree(a_rhs)
    {}

    template <typename Source>
    static void read_info(Source& src, variant_tree& tree) {
        boost::property_tree::info_parser::read_info(src, tree);
    }

    template <typename Target>
    static void write_info(Target& tar, variant_tree& tree) {
        boost::property_tree::info_parser::write_info(tar, tree);
    }

    template <typename Target, typename Settings>
    static void write_info(Target& tar, variant_tree& tree, const Settings& tt) {
        boost::property_tree::info_parser::write_info(tar, tree, tt);
    }

    template <class T>
    T get_value() const {
        using boost::throw_exception;

        if(boost::optional<T> o =
                base::get_value_optional<T>(detail::variant_translator<T>())) {
            return *o;
        }
        BOOST_PROPERTY_TREE_THROW(boost::property_tree::ptree_bad_data(
            std::string("conversion of data to type \"") +
            typeid(T).name() + "\" failed", base::data()));
    }

    template <class T>
    T get_value(const T& default_value) const {
        return base::get_value(default_value, detail::variant_translator<T>());
    }

    std::string get_value(const char* default_value) const {
        return base::get_value(std::string(default_value),
            detail::variant_translator<std::string>());
    }

    template <class T>
    boost::optional<T> get_value_optional() const {
        return base::get_value(detail::variant_translator<T>());
    }

    template <class T>
    void put_value(const T& value) {
        base::put_value(value, detail::variant_translator<T>());
    }

    template <class T>
    T get(const path_type& path) const {
        try {
            return base::get_child(path).BOOST_NESTED_TEMPLATE
                get_value<T>(detail::variant_translator<T>());
        } catch (boost::bad_get& e) {
            std::stringstream s;
            s << "Cannot convert value to type '" << type_to_string<T>() << "'";
            throw bad_path(s.str(), path);
        }
    }

    template <class T>
    T get(const path_type& path, const T& default_value) const {
        try {
            return base::get(path, default_value, detail::variant_translator<T>());
        } catch (boost::bad_get& e) {
            throw bad_path("Wrong or missing value type", path);
        }
    }

    std::string get(const path_type& path, const char* default_value) const {
        return base::get(path, std::string(default_value),
            detail::variant_translator<std::string>());
    }

    template <class T>
    boost::optional<T> get_optional(const path_type& path) const {
        return base::get_optional(path, detail::variant_translator<T>());
    }

    template <class T>
    void put(const path_type& path, const T& value) {
        base::put(path, value, detail::variant_translator<T>());
    }

    template <class T>
    self_type& add(const path_type& path, const T& value) {
        return static_cast<self_type&>(
            base::add(path, value, detail::variant_translator<T>()));
    }

    void swap(variant_tree& rhs) {
        base::swap(rhs);
    }
    void swap(boost::property_tree::basic_ptree<
        std::string, variant, std::less<std::string> >& rhs) {
        base::swap(rhs);
    }

    self_type &get_child(const path_type &path) {
        return static_cast<self_type&>(base::get_child(path));
    }

    /** Get the child at the given path, or throw @c ptree_bad_path. */
    const self_type &get_child(const path_type &path) const {
        return static_cast<const self_type&>(base::get_child(path));
    }

    /** Get the child at the given path, or return @p default_value. */
    self_type &get_child(const path_type &path, self_type &default_value) {
        return static_cast<self_type&>(base::get_child(path, default_value));
    }

    /** Get the child at the given path, or return @p default_value. */
    const self_type &get_child(const path_type &path,
                               const self_type &default_value) const {
        return static_cast<const self_type&>(base::get_child(path, default_value));
    }

    /** Get the child at the given path, or return boost::null. */
    boost::optional<self_type &> get_child_optional(const path_type &path) {
        boost::optional<base&> o = base::get_child_optional(path);
        if (!o)
            return boost::optional<self_type&>();
        return boost::optional<self_type&>(static_cast<self_type&>(*o));
    }

    /** Get the child at the given path, or return boost::null. */
    boost::optional<const self_type &>
    get_child_optional(const path_type &path) const {
        boost::optional<const base&> o = base::get_child_optional(path);
        if (!o)
            return boost::optional<const self_type&>();
        return boost::optional<const self_type&>(static_cast<const self_type&>(*o));
    }

    self_type &put_child(const path_type &path, const self_type &value) {
        return static_cast<self_type&>(base::put_child(path, value));
    }
};

} // namespace util

#endif // _UTIL_VARIANT_TREE_HPP_
<|endoftext|>


<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/initfiles/p9_vas_scom.C $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "p9_vas_scom.H"
#include <stdint.h>
#include <stddef.h>
#include <fapi2.H>

using namespace fapi2;

constexpr uint64_t literal_0x00200102000D7FFF = 0x00200102000D7FFF;
constexpr uint64_t literal_0x0000000000000000 = 0x0000000000000000;
constexpr uint64_t literal_0x00DF0201C0000000 = 0x00DF0201C0000000;
constexpr uint64_t literal_0x0080000000000000 = 0x0080000000000000;
constexpr uint64_t literal_0x1 = 0x1;

fapi2::ReturnCode p9_vas_scom(const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& TGT0,
                              const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>& TGT1)
{
    {
        fapi2::ATTR_EC_Type   l_chip_ec;
        fapi2::ATTR_NAME_Type l_chip_id;
        FAPI_TRY(FAPI_ATTR_GET_PRIVILEGED(fapi2::ATTR_NAME, TGT0, l_chip_id));
        FAPI_TRY(FAPI_ATTR_GET_PRIVILEGED(fapi2::ATTR_EC, TGT0, l_chip_ec));
        fapi2::ATTR_PROC_FABRIC_PUMP_MODE_Type l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_PUMP_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE));
        fapi2::buffer<uint64_t> l_scom_buffer;
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011803ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x00200102000D7FFF );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011803ull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011806ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x0000000000000000 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011806ull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011807ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x00DF0201C0000000 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011807ull, l_scom_buffer));
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180aull, l_scom_buffer ));

                l_scom_buffer.insert<8, 31, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180aull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180bull, l_scom_buffer ));

                l_scom_buffer.insert<8, 28, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180bull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180eull, l_scom_buffer ));

                l_scom_buffer.insert<8, 44, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180eull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180full, l_scom_buffer ));

                l_scom_buffer.insert<8, 44, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180full, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301184dull, l_scom_buffer ));

                l_scom_buffer.insert<19, 1, 63, uint64_t>(literal_0x1 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301184dull, l_scom_buffer));
            }
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x301184eull, l_scom_buffer ));

            constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_ADDR_BAR_MODE_OFF = 0x0;
            l_scom_buffer.insert<13, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_ADDR_BAR_MODE_OFF );

            if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_GROUP))
            {
                constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_ON = 0x1;
                l_scom_buffer.insert<14, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_ON );
            }
            else if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_NODE))
            {
                constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_OFF = 0x0;
                l_scom_buffer.insert<14, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_OFF );
            }

            FAPI_TRY(fapi2::putScom(TGT0, 0x301184eull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x301184full, l_scom_buffer ));

            l_scom_buffer.insert<0, 1, 63, uint64_t>(literal_0x1 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x301184full, l_scom_buffer));
        }

    };
fapi_try_exit:
    return fapi2::current_err;
}
<commit_msg>Logic flushed to 0s, xFC provides better performance and is intended value<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/initfiles/p9_vas_scom.C $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "p9_vas_scom.H"
#include <stdint.h>
#include <stddef.h>
#include <fapi2.H>

using namespace fapi2;

constexpr uint64_t literal_0x00200102000D7FFF = 0x00200102000D7FFF;
constexpr uint64_t literal_0x0000000000000000 = 0x0000000000000000;
constexpr uint64_t literal_0x00DF0201C0000000 = 0x00DF0201C0000000;
constexpr uint64_t literal_0x0080000000000000 = 0x0080000000000000;
constexpr uint64_t literal_0x1 = 0x1;
constexpr uint64_t literal_0xFC = 0xFC;

fapi2::ReturnCode p9_vas_scom(const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& TGT0,
                              const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>& TGT1)
{
    {
        fapi2::ATTR_EC_Type   l_chip_ec;
        fapi2::ATTR_NAME_Type l_chip_id;
        FAPI_TRY(FAPI_ATTR_GET_PRIVILEGED(fapi2::ATTR_NAME, TGT0, l_chip_id));
        FAPI_TRY(FAPI_ATTR_GET_PRIVILEGED(fapi2::ATTR_EC, TGT0, l_chip_ec));
        fapi2::ATTR_PROC_FABRIC_PUMP_MODE_Type l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE;
        FAPI_TRY(FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_PUMP_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE));
        fapi2::buffer<uint64_t> l_scom_buffer;
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011803ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x00200102000D7FFF );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011803ull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011806ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x0000000000000000 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011806ull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x3011807ull, l_scom_buffer ));

            l_scom_buffer.insert<0, 54, 0, uint64_t>(literal_0x00DF0201C0000000 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x3011807ull, l_scom_buffer));
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180aull, l_scom_buffer ));

                l_scom_buffer.insert<8, 31, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180aull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180bull, l_scom_buffer ));

                l_scom_buffer.insert<8, 28, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180bull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180eull, l_scom_buffer ));

                l_scom_buffer.insert<8, 44, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180eull, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301180full, l_scom_buffer ));

                l_scom_buffer.insert<8, 44, 8, uint64_t>(literal_0x0080000000000000 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301180full, l_scom_buffer));
            }
        }
        {
            if (((l_chip_id == 0x5) && (l_chip_ec == 0x10)) )
            {
                FAPI_TRY(fapi2::getScom( TGT0, 0x301184dull, l_scom_buffer ));

                l_scom_buffer.insert<19, 1, 63, uint64_t>(literal_0x1 );
                FAPI_TRY(fapi2::putScom(TGT0, 0x301184dull, l_scom_buffer));
            }
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x301184eull, l_scom_buffer ));

            constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_ADDR_BAR_MODE_OFF = 0x0;
            l_scom_buffer.insert<13, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_ADDR_BAR_MODE_OFF );

            if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_GROUP))
            {
                constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_ON = 0x1;
                l_scom_buffer.insert<14, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_ON );
            }
            else if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_NODE))
            {
                constexpr auto l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_OFF = 0x0;
                l_scom_buffer.insert<14, 1, 63, uint64_t>(l_VA_VA_SOUTH_VA_EG_EG_SCF_SKIP_G_OFF );
            }

            l_scom_buffer.insert<20, 8, 56, uint64_t>(literal_0xFC );
            l_scom_buffer.insert<28, 8, 56, uint64_t>(literal_0xFC );
            FAPI_TRY(fapi2::putScom(TGT0, 0x301184eull, l_scom_buffer));
        }
        {
            FAPI_TRY(fapi2::getScom( TGT0, 0x301184full, l_scom_buffer ));

            l_scom_buffer.insert<0, 1, 63, uint64_t>(literal_0x1 );
            FAPI_TRY(fapi2::putScom(TGT0, 0x301184full, l_scom_buffer));
        }

    };
fapi_try_exit:
    return fapi2::current_err;
}
<|endoftext|>


<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/nest/p9_pcie_scominit.H $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
//-----------------------------------------------------------------------------------
///
/// @file p9_pcie_scominit.H
/// @brief Perform PCIE Phase1 init sequence (FAPI2)
///
// *HWP HWP Owner: Christina Graves clgraves@us.ibm.com
// *HWP FW Owner: Thi Tran thi@us.ibm.com
// *HWP Team: Nest
// *HWP Level: 1
// *HWP Consumed by: HB

#ifndef _P9_PCIE_SCOMINIT_H_
#define _P9_PCIE_SCOMINIT_H_

//-----------------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------------
#include <fapi2.H>

//-----------------------------------------------------------------------------------
// Helper Macros
//-----------------------------------------------------------------------------------
#define SET_REG_RMW_WITH_SINGLE_ATTR_8(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_8)); \
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_8, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_RMW_WITH_SINGLE_ATTR_16(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_16)); \
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_16, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_WR_WITH_SINGLE_ATTR_16(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    l_buf = 0; \
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_16)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_16, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_RMW(in_value, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(in_value, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_WR(in_value, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(l_buf.insertFromRight(in_value, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

//-----------------------------------------------------------------------------------
// Structure definitions
//-----------------------------------------------------------------------------------

//function pointer typedef definition for HWP call support
typedef fapi2::ReturnCode (*p9_pcie_scominit_FP_t) (const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>&);

//-----------------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------------

const uint8_t NUM_PCS_CONFIG = 4;
const uint8_t NUM_PCIE_LANES = 16;
const uint8_t NUM_M_CONFIG = 4;
const uint8_t PEC0_IOP_CONFIG_START_BIT = 13;
const uint8_t PEC1_IOP_CONFIG_START_BIT = 14;
const uint8_t PEC2_IOP_CONFIG_START_BIT = 10;
const uint8_t PEC0_IOP_BIT_COUNT = 1;
const uint8_t PEC1_IOP_BIT_COUNT = 2;
const uint8_t PEC2_IOP_BIT_COUNT = 3;
const uint8_t PEC0_IOP_SWAP_START_BIT = 11;
const uint8_t PEC1_IOP_SWAP_START_BIT = 12;
const uint8_t PEC2_IOP_SWAP_START_BIT = 7;
const uint8_t PEC0_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC1_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC2_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC_IOP_REFCLOCK_ENABLE_START_BIT = 32;
const uint8_t PEC_IOP_PMA_RESET_START_BIT = 29;
const uint8_t PEC_IOP_PIPE_RESET_START_BIT = 28;
const uint8_t PEC_IOP_HSS_PORT_READY_START_BIT = 58;

const uint64_t  PEC_IOP_PLLA_VCO_COURSE_CAL_REGISTER1 = 0x800005010D010C3F;
const uint64_t  PEC_IOP_PLLB_VCO_COURSE_CAL_REGISTER1 = 0x800005410D010C3F;
const uint64_t  PEC_IOP_RX_DFE_FUNC_REGISTER1 = 0x8000049F0D010C3F;
const uint64_t  PEC_IOP_RX_DFE_FUNC_REGISTER2 = 0x800004A00D010C3F;

const uint64_t  RX_VGA_CTRL3_REGISTER[NUM_PCIE_LANES] =
{
    0x8000008D0D010C3F,
    0x800000CD0D010C3F,
    0x8000018D0D010C3F,
    0x800001CD0D010C3F,
    0x8000028D0D010C3F,
    0x800002CD0D010C3F,
    0x8000038D0D010C3F,
    0x800003CD0D010C3F,
    0x8000088D0D010C3F,
    0x800008CD0D010C3F,
    0x8000098D0D010C3F,
    0x800009CD0D010C3F,
    0x80000A8D0D010C3F,
    0x80000ACD0D010C3F,
    0x80000B8D0D010C3F,
    0x80000BCD0D010C3F,
};

const uint64_t  RX_LOFF_CNTL_REGISTER[NUM_PCIE_LANES] =
{
    0x800000A60D010C3F,
    0x800000E60D010C3F,
    0x800001A60D010C3F,
    0x800001E60D010C3F,
    0x800002A60D010C3F,
    0x800002E60D010C3F,
    0x800003A60D010C3F,
    0x800003E60D010C3F,
    0x800008A60D010C3F,
    0x800008E60D010C3F,
    0x800009A60D010C3F,
    0x800009E60D010C3F,
    0x80000AA60D010C3F,
    0x80000AE60D010C3F,
    0x80000BA60D010C3F,
    0x80000BE60D010C3F,

};

const uint32_t PCS_CONFIG_MODE0 = 0xA006;
const uint32_t PCS_CONFIG_MODE1 = 0xA805;
const uint32_t PCS_CONFIG_MODE2 = 0xB071;
const uint32_t PCS_CONFIG_MODE3 = 0xB870;

const uint32_t MAX_NUM_POLLS = 100; //Maximum number of iterations (So, 400ns * 100 = 40us before timeout)
const uint64_t PMA_RESET_NANO_SEC_DELAY = 400; //400ns to wait for PMA RESET to go through
const uint64_t PMA_RESET_CYC_DELAY = 400; //400ns to wait for PMA RESET to go through

extern "C" {

//-----------------------------------------------------------------------------------
// Function prototype
//-----------------------------------------------------------------------------------

/// @brief Perform PCIE Phase1 init sequence
/// @param[in] i_target => P9 chip target
/// @return FAPI_RC_SUCCESS if the setup completes successfully,
//
    fapi2::ReturnCode p9_pcie_scominit(const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& i_target);
} //extern"C"

#endif //_P9_PCIE_SCOMINIT_H_
<commit_msg>p9_pcie_scominit PEC0 swap bit position fixed<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/nest/p9_pcie_scominit.H $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
//-----------------------------------------------------------------------------------
///
/// @file p9_pcie_scominit.H
/// @brief Perform PCIE Phase1 init sequence (FAPI2)
///
// *HWP HWP Owner: Christina Graves clgraves@us.ibm.com
// *HWP FW Owner: Thi Tran thi@us.ibm.com
// *HWP Team: Nest
// *HWP Level: 1
// *HWP Consumed by: HB

#ifndef _P9_PCIE_SCOMINIT_H_
#define _P9_PCIE_SCOMINIT_H_

//-----------------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------------
#include <fapi2.H>

//-----------------------------------------------------------------------------------
// Helper Macros
//-----------------------------------------------------------------------------------
#define SET_REG_RMW_WITH_SINGLE_ATTR_8(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_8)); \
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_8, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_RMW_WITH_SINGLE_ATTR_16(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_16)); \
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_16, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_WR_WITH_SINGLE_ATTR_16(in_attr_name, in_reg_name, in_start_bit, in_bit_count)\
    l_buf = 0; \
    FAPI_TRY(FAPI_ATTR_GET(in_attr_name, l_pec_chiplets, l_attr_16)); \
    FAPI_TRY(l_buf.insertFromRight(l_attr_16, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_RMW(in_value, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(fapi2::getScom(l_pec_chiplets, in_reg_name, l_buf)); \
    FAPI_TRY(l_buf.insertFromRight(in_value, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

#define SET_REG_WR(in_value, in_reg_name, in_start_bit, in_bit_count)\
    FAPI_TRY(l_buf.insertFromRight(in_value, in_start_bit, in_bit_count)); \
    FAPI_DBG("pec%i: %#lx", l_pec_id, l_buf()); \
    FAPI_TRY(fapi2::putScom(l_pec_chiplets, in_reg_name, l_buf));

//-----------------------------------------------------------------------------------
// Structure definitions
//-----------------------------------------------------------------------------------

//function pointer typedef definition for HWP call support
typedef fapi2::ReturnCode (*p9_pcie_scominit_FP_t) (const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>&);

//-----------------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------------

const uint8_t NUM_PCS_CONFIG = 4;
const uint8_t NUM_PCIE_LANES = 16;
const uint8_t NUM_M_CONFIG = 4;
const uint8_t PEC0_IOP_CONFIG_START_BIT = 13;
const uint8_t PEC1_IOP_CONFIG_START_BIT = 14;
const uint8_t PEC2_IOP_CONFIG_START_BIT = 10;
const uint8_t PEC0_IOP_BIT_COUNT = 1;
const uint8_t PEC1_IOP_BIT_COUNT = 2;
const uint8_t PEC2_IOP_BIT_COUNT = 3;
const uint8_t PEC0_IOP_SWAP_START_BIT = 12;
const uint8_t PEC1_IOP_SWAP_START_BIT = 12;
const uint8_t PEC2_IOP_SWAP_START_BIT = 7;
const uint8_t PEC0_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC1_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC2_IOP_IOVALID_ENABLE_START_BIT = 4;
const uint8_t PEC_IOP_REFCLOCK_ENABLE_START_BIT = 32;
const uint8_t PEC_IOP_PMA_RESET_START_BIT = 29;
const uint8_t PEC_IOP_PIPE_RESET_START_BIT = 28;
const uint8_t PEC_IOP_HSS_PORT_READY_START_BIT = 58;

const uint64_t  PEC_IOP_PLLA_VCO_COURSE_CAL_REGISTER1 = 0x800005010D010C3F;
const uint64_t  PEC_IOP_PLLB_VCO_COURSE_CAL_REGISTER1 = 0x800005410D010C3F;
const uint64_t  PEC_IOP_RX_DFE_FUNC_REGISTER1 = 0x8000049F0D010C3F;
const uint64_t  PEC_IOP_RX_DFE_FUNC_REGISTER2 = 0x800004A00D010C3F;

const uint64_t  RX_VGA_CTRL3_REGISTER[NUM_PCIE_LANES] =
{
    0x8000008D0D010C3F,
    0x800000CD0D010C3F,
    0x8000018D0D010C3F,
    0x800001CD0D010C3F,
    0x8000028D0D010C3F,
    0x800002CD0D010C3F,
    0x8000038D0D010C3F,
    0x800003CD0D010C3F,
    0x8000088D0D010C3F,
    0x800008CD0D010C3F,
    0x8000098D0D010C3F,
    0x800009CD0D010C3F,
    0x80000A8D0D010C3F,
    0x80000ACD0D010C3F,
    0x80000B8D0D010C3F,
    0x80000BCD0D010C3F,
};

const uint64_t  RX_LOFF_CNTL_REGISTER[NUM_PCIE_LANES] =
{
    0x800000A60D010C3F,
    0x800000E60D010C3F,
    0x800001A60D010C3F,
    0x800001E60D010C3F,
    0x800002A60D010C3F,
    0x800002E60D010C3F,
    0x800003A60D010C3F,
    0x800003E60D010C3F,
    0x800008A60D010C3F,
    0x800008E60D010C3F,
    0x800009A60D010C3F,
    0x800009E60D010C3F,
    0x80000AA60D010C3F,
    0x80000AE60D010C3F,
    0x80000BA60D010C3F,
    0x80000BE60D010C3F,

};

const uint32_t PCS_CONFIG_MODE0 = 0xA006;
const uint32_t PCS_CONFIG_MODE1 = 0xA805;
const uint32_t PCS_CONFIG_MODE2 = 0xB071;
const uint32_t PCS_CONFIG_MODE3 = 0xB870;

const uint32_t MAX_NUM_POLLS = 100; //Maximum number of iterations (So, 400ns * 100 = 40us before timeout)
const uint64_t PMA_RESET_NANO_SEC_DELAY = 400; //400ns to wait for PMA RESET to go through
const uint64_t PMA_RESET_CYC_DELAY = 400; //400ns to wait for PMA RESET to go through

extern "C" {

//-----------------------------------------------------------------------------------
// Function prototype
//-----------------------------------------------------------------------------------

/// @brief Perform PCIE Phase1 init sequence
/// @param[in] i_target => P9 chip target
/// @return FAPI_RC_SUCCESS if the setup completes successfully,
//
    fapi2::ReturnCode p9_pcie_scominit(const fapi2::Target<fapi2::TARGET_TYPE_PROC_CHIP>& i_target);
} //extern"C"

#endif //_P9_PCIE_SCOMINIT_H_
<|endoftext|>