1 /*
2 * Copyright (C) 2016 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 "nanomessage.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21
22 #include "apptohostevent.h"
23 #include "log.h"
24 #include "resetreasonevent.h"
25 #include "sensorevent.h"
26
27 namespace android {
28
29 /* HardwareVersionInfo ********************************************************/
30
Populate(const std::vector<uint8_t> & buffer)31 bool HardwareVersionInfo::Populate(const std::vector<uint8_t>& buffer) {
32 if (buffer.size() != sizeof(VersionInfo)) {
33 return false;
34 }
35
36 const uint8_t *data = buffer.data();
37 const VersionInfo *source = reinterpret_cast<const VersionInfo *>(data);
38 info = *source;
39 return true;
40 }
41
ToString() const42 std::string HardwareVersionInfo::ToString() const {
43 const char format_string[] = "Hardware version info:\n"
44 " Hardware type: %04x\n"
45 " Hardware version: %04x\n"
46 " Bootloader version: %04x\n"
47 " Operating system version: %04x\n"
48 " Variant version: %08x\n";
49
50 char buffer[1024];
51 snprintf(buffer, sizeof(buffer), format_string,
52 info.hardware_type,
53 info.hardware_version,
54 info.bootloader_version,
55 info.operating_system_version,
56 info.variant_version);
57 return std::string(buffer);
58 }
59
60 /* WriteEventResponse *********************************************************/
61
ToString() const62 std::string WriteEventResponse::ToString() const {
63 const char format_string[] = "Write event accepted: %s\n";
64
65 char buffer[128];
66 snprintf(buffer, sizeof(buffer), format_string,
67 response.accepted ? "true" : "false");
68 return std::string(buffer);
69 }
70
Populate(const std::vector<uint8_t> & buffer)71 bool WriteEventResponse::Populate(const std::vector<uint8_t>& buffer) {
72 if (buffer.size() != sizeof(Response)) {
73 return false;
74 }
75
76 const uint8_t *data = buffer.data();
77 const Response *source = reinterpret_cast<const Response *>(data);
78 response = *source;
79 return true;
80
81 }
82
83 /* ReadEventRequest ***********************************************************/
84
GetBytes() const85 std::vector<uint8_t> ReadEventRequest::GetBytes() const {
86 std::vector<uint8_t> buffer(sizeof(Request));
87
88 uint8_t *data = buffer.data();
89 Request *req = reinterpret_cast<Request *>(data);
90 *req = request;
91 return buffer;
92 }
93
ToString() const94 std::string ReadEventRequest::ToString() const {
95 const char format_string[] = "Read event at time: %" PRIx64 "\n";
96
97 char buffer[128];
98 snprintf(buffer, sizeof(buffer), format_string,
99 request.boot_time);
100 return std::string(buffer);
101 }
102
103 /* ReadEventResponse **********************************************************/
104
ToString() const105 std::string ReadEventResponse::ToString() const {
106 char buffer[32];
107 snprintf(buffer, sizeof(buffer), "ReadEventResponse %u\n", GetEventType());
108 return std::string(buffer);
109 }
110
FromBytes(const std::vector<uint8_t> & buffer)111 std::unique_ptr<ReadEventResponse> ReadEventResponse::FromBytes(
112 const std::vector<uint8_t>& buffer) {
113 // The first 4 bytes of any event must be the event type - use it to figure
114 // out which class to construct
115 uint32_t event_type = ReadEventResponse::EventTypeFromBuffer(buffer);
116 if (ReadEventResponse::IsSensorEvent(event_type)) {
117 return SensorEvent::FromBytes(buffer);
118 } else if (ReadEventResponse::IsAppToHostEvent(event_type)) {
119 return AppToHostEvent::FromBytes(buffer);
120 } else if (ReadEventResponse::IsResetReasonEvent(event_type)) {
121 return ResetReasonEvent::FromBytes(buffer);
122 } else {
123 LOGW("Received unexpected/unsupported event type %u", event_type);
124 return nullptr;
125 }
126 }
127
Populate(const std::vector<uint8_t> & buffer)128 bool ReadEventResponse::Populate(const std::vector<uint8_t>& buffer) {
129 if (buffer.size() < sizeof(Event)) {
130 return false;
131 }
132
133 event_data.resize(buffer.size());
134 std::copy(buffer.begin(), buffer.end(), event_data.begin());
135 return true;
136 }
137
IsAppToHostEvent() const138 bool ReadEventResponse::IsAppToHostEvent() const {
139 return ReadEventResponse::IsAppToHostEvent(GetEventType());
140 }
141
IsSensorEvent() const142 bool ReadEventResponse::IsSensorEvent() const {
143 return ReadEventResponse::IsSensorEvent(GetEventType());
144 }
145
IsResetReasonEvent() const146 bool ReadEventResponse::IsResetReasonEvent() const {
147 return ReadEventResponse::IsResetReasonEvent(GetEventType());
148 }
149
GetEventType() const150 uint32_t ReadEventResponse::GetEventType() const {
151 return ReadEventResponse::EventTypeFromBuffer(event_data);
152 }
153
IsSensorEvent(uint32_t event_type)154 bool ReadEventResponse::IsSensorEvent(uint32_t event_type) {
155 return (event_type >= static_cast<uint32_t>(EventType::FirstSensorEvent) &&
156 event_type <= static_cast<uint32_t>(EventType::LastSensorEvent));
157 }
158
IsAppToHostEvent(uint32_t event_type)159 bool ReadEventResponse::IsAppToHostEvent(uint32_t event_type) {
160 return (event_type == static_cast<uint32_t>(EventType::AppToHostEvent));
161 }
162
IsResetReasonEvent(uint32_t event_type)163 bool ReadEventResponse::IsResetReasonEvent(uint32_t event_type) {
164 return (event_type == static_cast<uint32_t>(EventType::ResetReasonEvent));
165 }
166
EventTypeFromBuffer(const std::vector<uint8_t> & buffer)167 uint32_t ReadEventResponse::EventTypeFromBuffer(const std::vector<uint8_t>& buffer) {
168 if (buffer.size() < sizeof(uint32_t)) {
169 LOGW("Invalid/short event of size %zu", buffer.size());
170 return 0;
171 }
172 return *reinterpret_cast<const uint32_t *>(buffer.data());
173 }
174
175 /* ConfigureSensorRequest *****************************************************/
176
ConfigureSensorRequest()177 ConfigureSensorRequest::ConfigureSensorRequest() {
178 config.event_type = static_cast<uint32_t>(EventType::ConfigureSensor);
179 }
180
FloatRateToFixedPoint(float rate)181 uint32_t ConfigureSensorRequest::FloatRateToFixedPoint(float rate) {
182 return rate * 1024.0f;
183 }
184
FixedPointRateToFloat(uint32_t rate)185 float ConfigureSensorRequest::FixedPointRateToFloat(uint32_t rate) {
186 return rate / 1024.0f;
187 }
188
189 // TODO(aarossig): Consider writing a template function for this.
GetBytes() const190 std::vector<uint8_t> ConfigureSensorRequest::GetBytes() const {
191 std::vector<uint8_t> buffer(sizeof(Configuration));
192
193 uint8_t *data = buffer.data();
194 Configuration *configuration = reinterpret_cast<Configuration *>(data);
195 *configuration = config;
196 buffer.insert(buffer.end(), extra_data_.begin(), extra_data_.end());
197
198 return buffer;
199 }
200
SetAdditionalData(const std::vector<uint8_t> & data)201 void ConfigureSensorRequest::SetAdditionalData(const std::vector<uint8_t>& data) {
202 extra_data_ = data;
203 }
204
ToString() const205 std::string ConfigureSensorRequest::ToString() const {
206 const char format_string[] = "Sensor configuration:\n"
207 " latency: %" PRIx64 "\n"
208 " rate (fixed point): %08x\n"
209 " sensor_type: %02x\n"
210 " command: %02x\n"
211 " flags: %04x\n";
212
213 char buffer[1024];
214 snprintf(buffer, sizeof(buffer), format_string,
215 config.latency,
216 config.rate,
217 config.sensor_type,
218 config.command,
219 config.flags);
220 return std::string(buffer);
221 }
222
GetEventType() const223 EventType ConfigureSensorRequest::GetEventType() const {
224 return static_cast<EventType>(config.event_type);
225 }
226
227 } // namespace android
228