1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "drmproperty.h"
18 #include "drmdevice.h"
19 
20 #include <errno.h>
21 #include <stdint.h>
22 #include <string>
23 
24 #include <xf86drmMode.h>
25 #include <log/log.h>
26 #include <inttypes.h>
27 
28 namespace android {
29 
DrmPropertyEnum(drm_mode_property_enum * e)30 DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
31     : value_(e->value), name_(e->name) {
32 }
33 
~DrmPropertyEnum()34 DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() {
35 }
36 
DrmProperty(drmModePropertyPtr p,uint64_t value)37 DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
38     : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") {
39   Init(p, value);
40 }
41 
Init(drmModePropertyPtr p,uint64_t value)42 void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
43   id_ = p->prop_id;
44   flags_ = p->flags;
45   name_ = p->name;
46   value_ = value;
47 
48   for (int i = 0; i < p->count_values; ++i)
49     values_.push_back(p->values[i]);
50 
51   for (int i = 0; i < p->count_enums; ++i)
52     enums_.push_back(DrmPropertyEnum(&p->enums[i]));
53 
54   for (int i = 0; i < p->count_blobs; ++i)
55     blob_ids_.push_back(p->blob_ids[i]);
56 
57   if ((flags_ & DRM_MODE_PROP_RANGE) ||
58       (flags_ & DRM_MODE_PROP_SIGNED_RANGE))
59     type_ = DRM_PROPERTY_TYPE_INT;
60   else if (flags_ & DRM_MODE_PROP_ENUM)
61     type_ = DRM_PROPERTY_TYPE_ENUM;
62   else if (flags_ & DRM_MODE_PROP_OBJECT)
63     type_ = DRM_PROPERTY_TYPE_OBJECT;
64   else if (flags_ & DRM_MODE_PROP_BLOB)
65     type_ = DRM_PROPERTY_TYPE_BLOB;
66   else if (flags_ & DRM_MODE_PROP_BITMASK)
67     type_ = DRM_PROPERTY_TYPE_BITMASK;
68 }
69 
id() const70 uint32_t DrmProperty::id() const {
71   return id_;
72 }
73 
name() const74 std::string DrmProperty::name() const {
75   return name_;
76 }
77 
value() const78 std::tuple<int, uint64_t> DrmProperty::value() const {
79   if (type_ == DRM_PROPERTY_TYPE_BLOB)
80     return std::make_tuple(0, value_);
81 
82   if (values_.size() == 0)
83     return std::make_tuple(-ENOENT, 0);
84 
85   switch (type_) {
86     case DRM_PROPERTY_TYPE_INT:
87       return std::make_tuple(0, value_);
88 
89     case DRM_PROPERTY_TYPE_BITMASK:
90       return std::make_tuple(0, value_);
91 
92     case DRM_PROPERTY_TYPE_ENUM:
93       if (value_ >= enums_.size())
94         return std::make_tuple(-ENOENT, 0);
95 
96       return std::make_tuple(0, enums_[value_].value_);
97 
98     case DRM_PROPERTY_TYPE_OBJECT:
99       return std::make_tuple(0, value_);
100 
101     default:
102       return std::make_tuple(-EINVAL, 0);
103   }
104 }
105 
printProperty() const106 void DrmProperty::printProperty() const
107 {
108     ALOGD("=====================================================");
109     ALOGD("name: %s, type(%d), value_(%" PRId64 ")", name_.c_str(), type_, value_);
110     ALOGD("values.size(%zu)",  values_.size());
111     for(uint32_t i = 0; i < values_.size(); i++) {
112         ALOGD("[%d] %" PRId64 "", i, values_[i]);
113     }
114     ALOGD("enums.size(%zu)",  enums_.size());
115     uint32_t i = 0;
116     for (auto const &it : enums_) {
117         ALOGD("[%d] %s %" PRId64 "", i++, it.name_.c_str(), it.value_);
118     }
119 }
120 
is_immutable() const121 bool DrmProperty::is_immutable() const {
122   return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
123 }
124 
is_range() const125 bool DrmProperty::is_range() const {
126   return id_ && (flags_ & DRM_MODE_PROP_RANGE);
127 }
128 
range_min() const129 std::tuple<int, uint64_t> DrmProperty::range_min() const {
130   if (!is_range())
131     return std::make_tuple(-EINVAL, 0);
132   if (values_.size() < 1)
133     return std::make_tuple(-ENOENT, 0);
134 
135   return std::make_tuple(0, values_[0]);
136 }
137 
range_max() const138 std::tuple<int, uint64_t> DrmProperty::range_max() const {
139   if (!is_range())
140     return std::make_tuple(-EINVAL, 0);
141   if (values_.size() < 2)
142     return std::make_tuple(-ENOENT, 0);
143 
144   return std::make_tuple(0, values_[1]);
145 }
146 
GetEnumValueWithName(std::string name) const147 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
148     std::string name) const {
149   for (auto it : enums_) {
150     if (it.name_.compare(name) == 0) {
151       return std::make_tuple(it.value_, 0);
152     }
153   }
154 
155   return std::make_tuple(UINT64_MAX, -EINVAL);
156 }
157 
UpdateValue(uint64_t value)158 void DrmProperty::UpdateValue(uint64_t value) {
159   value_ = value;
160 }
161 }  // namespace android
162