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 "apptohostevent.h"
18 
19 #include "contexthub.h"
20 #include "log.h"
21 
22 namespace android {
23 
24 /* AppToHostEvent *************************************************************/
25 
FromBytes(const std::vector<uint8_t> & buffer)26 std::unique_ptr<AppToHostEvent> AppToHostEvent::FromBytes(
27         const std::vector<uint8_t>& buffer) {
28     auto event = std::unique_ptr<AppToHostEvent>(new AppToHostEvent());
29     event->Populate(buffer);
30     if (!event->IsValid()) {
31         return nullptr;
32     }
33 
34     return event;
35 }
36 
GetAppId() const37 uint64_t AppToHostEvent::GetAppId() const {
38     return GetTypedData()->appId;
39 }
40 
GetDataLen() const41 uint8_t AppToHostEvent::GetDataLen() const {
42     return GetTypedData()->dataLen;
43 }
44 
GetDataPtr() const45 const uint8_t *AppToHostEvent::GetDataPtr() const {
46     return (reinterpret_cast<const uint8_t*>(GetTypedData())
47               + sizeof(struct HostHubRawPacket));
48 }
49 
CheckAppId(SensorType sensor_type) const50 bool AppToHostEvent::CheckAppId(SensorType sensor_type) const {
51     // Make sure the app ID matches what we expect for the sensor type, bail out
52     // early if it doesn't
53     switch (sensor_type) {
54       case SensorType::Accel:
55       case SensorType::Gyro:
56         if (GetAppId() != kAppIdBoschBmi160Bmm150 && GetAppId() != kAppIdSTMicroLsm6dsm &&
57             GetAppId() != kAppIdInvnIcm40600) {
58             return false;
59         }
60         break;
61 
62       case SensorType::Magnetometer:
63         if (GetAppId() != kAppIdSTMicroLsm6dsm && GetAppId() != kAppIdSTMicroMag40) {
64             return false;
65         }
66         break;
67 
68       case SensorType::Proximity:
69         if (GetAppId() != kAppIdAmsTmd2772 && GetAppId() != kAppIdRohmRpr0521 &&
70             GetAppId() != kAppIdAmsTmd4903) {
71             return false;
72         }
73         break;
74 
75       case SensorType::Barometer:
76         if (GetAppId() != kAppIdBoschBmp280 && GetAppId() != kAppIdSTMicroLps22hb) {
77             return false;
78         }
79         break;
80 
81       case SensorType::AmbientLightSensor:
82         if (GetAppId() != kAppIdAmsTmd4903) {
83             return false;
84         }
85         break;
86 
87       default:
88         return false;
89     }
90 
91     return true;
92 }
93 
CheckEventHeader(SensorType sensor_type) const94 bool AppToHostEvent::CheckEventHeader(SensorType sensor_type) const {
95     if (GetDataLen() < sizeof(struct SensorAppEventHeader)) {
96         return false;
97     }
98 
99     if (!CheckAppId(sensor_type)) {
100         return false;
101     }
102 
103     return true;
104 }
105 
IsCalibrationEventForSensor(SensorType sensor_type) const106 bool AppToHostEvent::IsCalibrationEventForSensor(SensorType sensor_type) const {
107     if (!CheckEventHeader(sensor_type)) {
108         return false;
109     }
110 
111     // If we made it this far, we only need to confirm the message ID
112     auto header = reinterpret_cast<const struct SensorAppEventHeader *>(
113         GetDataPtr());
114     return (header->msgId == SENSOR_APP_MSG_CALIBRATION_RESULT);
115 }
116 
IsTestEventForSensor(SensorType sensor_type) const117 bool AppToHostEvent::IsTestEventForSensor(SensorType sensor_type) const {
118     if (!CheckEventHeader(sensor_type)) {
119         return false;
120     }
121 
122     // If we made it this far, we only need to confirm the message ID
123     auto header = reinterpret_cast<const struct SensorAppEventHeader *>(
124         GetDataPtr());
125     return (header->msgId == SENSOR_APP_MSG_TEST_RESULT);
126 }
127 
IsValid() const128 bool AppToHostEvent::IsValid() const {
129     const HostHubRawPacket *packet = GetTypedData();
130     if (!packet) {
131         return false;
132     }
133 
134     // dataLen should specify the amount of data that follows the event type
135     // and HostHubRawPacket headers
136     if (event_data.size() < (sizeof(uint32_t) + sizeof(struct HostHubRawPacket)
137                                + packet->dataLen)) {
138         LOGW("Invalid/short AppToHost event of size %zu", event_data.size());
139         return false;
140     }
141 
142     return true;
143 }
144 
GetTypedData() const145 const HostHubRawPacket *AppToHostEvent::GetTypedData() const {
146     // After the event type header (uint32_t), we should have struct
147     // HostHubRawPacket
148     if (event_data.size() < sizeof(uint32_t) + sizeof(struct HostHubRawPacket)) {
149         LOGW("Invalid/short AppToHost event of size %zu", event_data.size());
150         return nullptr;
151     }
152     return reinterpret_cast<const HostHubRawPacket *>(
153         event_data.data() + sizeof(uint32_t));
154 }
155 
156 }  // namespace android
157