aboutsummaryrefslogtreecommitdiff
path: root/libs/kdbinder/kdbus/item.cpp
blob: 79a9ea1aa1934ee9cc2a159d77a8c97f30d47786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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.
 */

#include <kdbus/item.h>
#include <kdbus/globals.h>

#include <linux/kdbus.h>
#include <string>

namespace android {
namespace kdbus {

constexpr uint64_t Item::kComputeSizeAligned(uint64_t extra) {
  return kAlignTo8Bit(Item::kComputeSize(extra));
}

constexpr uint64_t Item::kComputeSize(uint64_t extra) {
  return offsetof(struct kdbus_item, data) + extra;
}


struct kdbus_item* Item::Next(struct kdbus_item *item) {
  return reinterpret_cast<struct kdbus_item *>(
      reinterpret_cast<uint8_t *>(item) + kAlignTo8Bit(item->size));
}

ItemBloomParameter::ItemBloomParameter(uint64_t size, uint64_t n_hash)
    : Item(Item::kComputeSizeAligned(sizeof(struct kdbus_bloom_parameter))),
      bloom_size_(size), n_hash_(n_hash) {}

struct kdbus_item* ItemBloomParameter::Queue(struct kdbus_item *item) const {
  struct kdbus_bloom_parameter bloom {
    .size = bloom_size_,
    .n_hash = n_hash_
  };

  item->type = KDBUS_ITEM_BLOOM_PARAMETER;
  item->size = size;
  memcpy(&item->bloom_parameter, &bloom, sizeof(struct kdbus_bloom_parameter));

  return Item::Next(item);
}

ItemStr::ItemStr(const std::string& str)
    : Item(Item::kComputeSize(strlen(str.c_str()) + 1)) {
  KDBUS_CHECK(strlen(str.c_str()) <= sizeof(str_));

  snprintf(str_, sizeof(str_), "%s", str.c_str());
}

struct kdbus_item* ItemMakeName::Queue(struct kdbus_item *item) const {
  item->type = KDBUS_ITEM_MAKE_NAME;
  item->size = size;
  strncpy(item->str, str_, strlen(str_) + 1);

  return Item::Next(item);
}

struct kdbus_item* ItemConnDescription::Queue(struct kdbus_item *item) const {
  item->type = KDBUS_ITEM_CONN_DESCRIPTION;
  item->size = size;
  strncpy(item->str, str_, strlen(str_) + 1);

  return Item::Next(item);
}

struct kdbus_item* ItemName::Queue(struct kdbus_item *item) const {
  item->type = KDBUS_ITEM_NAME;
  item->size = size;
  strncpy(item->str, str_, strlen(str_) + 1);

  return Item::Next(item);
}

ItemPayloadVec::ItemPayloadVec(const uint8_t *data, uint64_t size)
    : Item(Item::kComputeSizeAligned(sizeof(struct kdbus_vec))),
      vec_data_(data), vec_size_(size) {
}

struct kdbus_item* ItemPayloadVec::Queue(struct kdbus_item *item) const {
  item->type = KDBUS_ITEM_PAYLOAD_VEC;
  item->size = size;
  item->vec.size = vec_size_;
  item->vec.address = reinterpret_cast<uintptr_t>(vec_data_);
  return Item::Next(item);
}

ItemIdChange::ItemIdChange(uint64_t src_id)
    : Item(Item::kComputeSizeAligned(sizeof(struct kdbus_notify_id_change))),
      src_id_(src_id) {
}

struct kdbus_item* ItemIdRemove::Queue(struct kdbus_item *item) const {
  item->type = KDBUS_ITEM_ID_REMOVE;
  item->size = size;
  item->id_change.id = src_id_;
  item->id_change.flags = 0;

  return Item::Next(item);
}

}  // namespace kdbus
}  // namespace android