blob: 8f17abe86308f7aba3f942bc9d740c76de1c8e61 (
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
117
118
|
/*
* Copyright (C) 2017 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 "HidDefs.h"
#include "HidLocal.h"
#include "HidLog.h"
#include <cstddef>
namespace HidUtil {
constexpr uint32_t INVALID_USAGE = 0xFFFF;
constexpr uint32_t INVALID_DESIGNATOR = 0xFFFF;
constexpr uint32_t INVALID_STRING = 0xFFFF;
uint32_t HidLocal::getUsage(size_t index) const {
if (usage.empty()) {
return INVALID_USAGE;
}
return (index >= usage.size()) ? usage.back() : usage[index];
}
uint32_t HidLocal::getDesignator(size_t index) const {
if (designator.empty()) {
return INVALID_DESIGNATOR;
}
return (index >= designator.size()) ? designator.back() : designator[index];
}
uint32_t HidLocal::getString(size_t index) const {
if (string.empty()) {
return INVALID_STRING;
}
return (index >= string.size()) ? string.back() : string[index];
}
void HidLocal::clear() {
*this = HidLocal();
}
bool HidLocal::append(const HidItem &i) {
using namespace HidDef::LocalTag;
bool ret = true;
unsigned unsignedInteger;
bool unsignedError = !i.dataAsUnsigned(&unsignedInteger);
bool valueError = false;
switch (i.tag) {
case USAGE:
usage.push_back(unsignedInteger);
valueError = unsignedError;
break;
case USAGE_MINIMUM:
usageMin = unsignedInteger;
valueError = unsignedError;
break;
case USAGE_MAXIMUM:
if (!usageMin.isSet()) {
LOG_E << "usage min not set when saw usage max " << i << LOG_ENDL;
ret = false;
} else {
uint32_t usagemax = unsignedInteger;
valueError = unsignedError;
for (size_t j = usageMin.get(0); j <= usagemax; ++j) {
usage.push_back(j);
}
usageMin.clear();
}
break;
case STRING_INDEX:
string.push_back(unsignedInteger);
valueError = unsignedError;
break;
case STRING_MINIMUM:
stringMin = unsignedInteger;
valueError = unsignedError;
break;
case STRING_MAXIMUM: {
if (!usageMin.isSet()) {
LOG_E << "string min not set when saw string max " << i << LOG_ENDL;
ret = false;
} else {
uint32_t stringMax = unsignedInteger;
valueError = unsignedError;
for (size_t j = stringMin.get(0); j <= stringMax; ++j) {
string.push_back(j);
}
stringMin.clear();
}
break;
}
case DELIMITOR:
delimeter = unsignedInteger;
valueError = unsignedError;
break;
default:
LOG_E << "unknown local tag, " << i << LOG_ENDL;
ret = false;
}
if (valueError) {
LOG_E << "Cannot get unsigned data at " << i << LOG_ENDL;
ret = false;
}
return ret;
}
} //namespace HidUtil
|