• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "logevent.h"
25 #include "resetreasonevent.h"
26 #include "sensorevent.h"
27 
28 namespace android {
29 
30 /* HardwareVersionInfo ********************************************************/
31 
Populate(const std::vector<uint8_t> & buffer)32 bool HardwareVersionInfo::Populate(const std::vector<uint8_t>& buffer) {
33     if (buffer.size() != sizeof(VersionInfo)) {
34         return false;
35     }
36 
37     const uint8_t *data = buffer.data();
38     const VersionInfo *source = reinterpret_cast<const VersionInfo *>(data);
39     info = *source;
40     return true;
41 }
42 
ToString() const43 std::string HardwareVersionInfo::ToString() const {
44     const char format_string[] = "Hardware version info:\n"
45         "    Hardware type: %04x\n"
46         "    Hardware version: %04x\n"
47         "    Bootloader version: %04x\n"
48         "    Operating system version: %04x\n"
49         "    Variant version: %08x\n";
50 
51     char buffer[1024];
52     snprintf(buffer, sizeof(buffer), format_string,
53         info.hardware_type,
54         info.hardware_version,
55         info.bootloader_version,
56         info.operating_system_version,
57         info.variant_version);
58     return std::string(buffer);
59 }
60 
61 /* WriteEventResponse *********************************************************/
62 
ToString() const63 std::string WriteEventResponse::ToString() const {
64     const char format_string[] = "Write event accepted: %s\n";
65 
66     char buffer[128];
67     snprintf(buffer, sizeof(buffer), format_string,
68         response.accepted ? "true" : "false");
69     return std::string(buffer);
70 }
71 
Populate(const std::vector<uint8_t> & buffer)72 bool WriteEventResponse::Populate(const std::vector<uint8_t>& buffer) {
73     if (buffer.size() != sizeof(Response)) {
74         return false;
75     }
76 
77     const uint8_t *data = buffer.data();
78     const Response *source = reinterpret_cast<const Response *>(data);
79     response = *source;
80     return true;
81 
82 }
83 
84 /* ReadEventRequest ***********************************************************/
85 
GetBytes() const86 std::vector<uint8_t> ReadEventRequest::GetBytes() const {
87     std::vector<uint8_t> buffer(sizeof(Request));
88 
89     uint8_t *data = buffer.data();
90     Request *req = reinterpret_cast<Request *>(data);
91     *req = request;
92     return buffer;
93 }
94 
ToString() const95 std::string ReadEventRequest::ToString() const {
96     const char format_string[] = "Read event at time: %" PRIx64 "\n";
97 
98     char buffer[128];
99     snprintf(buffer, sizeof(buffer), format_string,
100         request.boot_time);
101     return std::string(buffer);
102 }
103 
104 /* ReadEventResponse **********************************************************/
105 
ToString() const106 std::string ReadEventResponse::ToString() const {
107     char buffer[32];
108     snprintf(buffer, sizeof(buffer), "ReadEventResponse %u\n", GetEventType());
109     return std::string(buffer);
110 }
111 
FromBytes(const std::vector<uint8_t> & buffer)112 std::unique_ptr<ReadEventResponse> ReadEventResponse::FromBytes(
113         const std::vector<uint8_t>& buffer) {
114     // The first 4 bytes of any event must be the event type - use it to figure
115     // out which class to construct
116     uint32_t event_type = ReadEventResponse::EventTypeFromBuffer(buffer);
117     if (ReadEventResponse::IsSensorEvent(event_type)) {
118         return SensorEvent::FromBytes(buffer);
119     } else if (ReadEventResponse::IsAppToHostEvent(event_type)) {
120         return AppToHostEvent::FromBytes(buffer);
121     } else if (ReadEventResponse::IsResetReasonEvent(event_type)) {
122         return ResetReasonEvent::FromBytes(buffer);
123     } else if (ReadEventResponse::IsLogEvent(event_type)) {
124         return LogEvent::FromBytes(buffer);
125     } else {
126         LOGW("Received unexpected/unsupported event type %u", event_type);
127         return nullptr;
128     }
129 }
130 
Populate(const std::vector<uint8_t> & buffer)131 bool ReadEventResponse::Populate(const std::vector<uint8_t>& buffer) {
132     if (buffer.size() < sizeof(Event)) {
133         return false;
134     }
135 
136     event_data.resize(buffer.size());
137     std::copy(buffer.begin(), buffer.end(), event_data.begin());
138     return true;
139 }
140 
IsAppToHostEvent() const141 bool ReadEventResponse::IsAppToHostEvent() const {
142     return ReadEventResponse::IsAppToHostEvent(GetEventType());
143 }
144 
IsSensorEvent() const145 bool ReadEventResponse::IsSensorEvent() const {
146     return ReadEventResponse::IsSensorEvent(GetEventType());
147 }
148 
IsResetReasonEvent() const149 bool ReadEventResponse::IsResetReasonEvent() const {
150     return ReadEventResponse::IsResetReasonEvent(GetEventType());
151 }
152 
IsLogEvent() const153 bool ReadEventResponse::IsLogEvent() const {
154     return ReadEventResponse::IsLogEvent(GetEventType());
155 }
156 
GetEventType() const157 uint32_t ReadEventResponse::GetEventType() const {
158     return ReadEventResponse::EventTypeFromBuffer(event_data);
159 }
160 
IsSensorEvent(uint32_t event_type)161 bool ReadEventResponse::IsSensorEvent(uint32_t event_type) {
162     return (event_type >= static_cast<uint32_t>(EventType::FirstSensorEvent) &&
163             event_type <= static_cast<uint32_t>(EventType::LastSensorEvent));
164 }
165 
IsAppToHostEvent(uint32_t event_type)166 bool ReadEventResponse::IsAppToHostEvent(uint32_t event_type) {
167     return (event_type == static_cast<uint32_t>(EventType::AppToHostEvent));
168 }
169 
IsResetReasonEvent(uint32_t event_type)170 bool ReadEventResponse::IsResetReasonEvent(uint32_t event_type) {
171     return (event_type == static_cast<uint32_t>(EventType::ResetReasonEvent));
172 }
173 
IsLogEvent(uint32_t event_type)174 bool ReadEventResponse::IsLogEvent(uint32_t event_type) {
175     return (event_type == static_cast<uint32_t>(EventType::LogEvent));
176 }
177 
EventTypeFromBuffer(const std::vector<uint8_t> & buffer)178 uint32_t ReadEventResponse::EventTypeFromBuffer(const std::vector<uint8_t>& buffer) {
179     if (buffer.size() < sizeof(uint32_t)) {
180         LOGW("Invalid/short event of size %zu", buffer.size());
181         return 0;
182     }
183     return *reinterpret_cast<const uint32_t *>(buffer.data());
184 }
185 
186 /* ConfigureSensorRequest *****************************************************/
187 
ConfigureSensorRequest()188 ConfigureSensorRequest::ConfigureSensorRequest() {
189     config.event_type = static_cast<uint32_t>(EventType::ConfigureSensor);
190 }
191 
FloatRateToFixedPoint(float rate)192 uint32_t ConfigureSensorRequest::FloatRateToFixedPoint(float rate) {
193     return rate * 1024.0f;
194 }
195 
FixedPointRateToFloat(uint32_t rate)196 float ConfigureSensorRequest::FixedPointRateToFloat(uint32_t rate) {
197     return rate / 1024.0f;
198 }
199 
200 // TODO(aarossig): Consider writing a template function for this.
GetBytes() const201 std::vector<uint8_t> ConfigureSensorRequest::GetBytes() const {
202     std::vector<uint8_t> buffer(sizeof(Configuration));
203 
204     uint8_t *data = buffer.data();
205     Configuration *configuration = reinterpret_cast<Configuration *>(data);
206     *configuration = config;
207     buffer.insert(buffer.end(), extra_data_.begin(), extra_data_.end());
208 
209     return buffer;
210 }
211 
SetAdditionalData(const std::vector<uint8_t> & data)212 void ConfigureSensorRequest::SetAdditionalData(const std::vector<uint8_t>& data) {
213     extra_data_ = data;
214 }
215 
ToString() const216 std::string ConfigureSensorRequest::ToString() const {
217     const char format_string[] = "Sensor configuration:\n"
218         "    latency: %" PRIx64 "\n"
219         "    rate (fixed point): %08x\n"
220         "    sensor_type: %02x\n"
221         "    command: %02x\n"
222         "    flags: %04x\n";
223 
224     char buffer[1024];
225     snprintf(buffer, sizeof(buffer), format_string,
226             config.latency,
227             config.rate,
228             config.sensor_type,
229             config.command,
230             config.flags);
231     return std::string(buffer);
232 }
233 
GetEventType() const234 EventType ConfigureSensorRequest::GetEventType() const {
235     return static_cast<EventType>(config.event_type);
236 }
237 
238 /* BridgeVersionInfoRequest ***************************************************/
239 
GetBytes() const240 std::vector<uint8_t> BridgeVersionInfoRequest::GetBytes() const {
241     struct VersionInfoRequestEvent : public Event {
242         struct BrHostEventTx event_data;
243     } __attribute__((packed));
244 
245     std::vector<uint8_t> buffer(sizeof(VersionInfoRequestEvent));
246 
247     std::fill(buffer.begin(), buffer.end(), 0);
248     auto event = reinterpret_cast<VersionInfoRequestEvent *>(buffer.data());
249     event->event_type = static_cast<uint32_t>(EventType::AppFromHostEvent);
250     event->event_data.hdr.appId   = kAppIdBridge;
251     event->event_data.hdr.dataLen = sizeof(BrHostEventData);
252     event->event_data.data.msgId  = BRIDGE_HOST_EVENT_MSG_VERSION_INFO;
253 
254     return buffer;
255 }
256 
GetEventType() const257 EventType BridgeVersionInfoRequest::GetEventType() const {
258     return EventType::AppFromHostEvent;
259 }
260 
ToString() const261 std::string BridgeVersionInfoRequest::ToString() const {
262     return std::string("Bridge version info request\n");
263 }
264 
265 }  // namespace android
266