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 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 37 uint64_t AppToHostEvent::GetAppId() const { 38 return GetTypedData()->appId; 39 } 40 41 uint8_t AppToHostEvent::GetDataLen() const { 42 return GetTypedData()->dataLen; 43 } 44 45 const uint8_t *AppToHostEvent::GetDataPtr() const { 46 return (reinterpret_cast<const uint8_t*>(GetTypedData()) 47 + sizeof(struct HostHubRawPacket)); 48 } 49 50 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) { 57 return false; 58 } 59 break; 60 61 case SensorType::Proximity: 62 if (GetAppId() != kAppIdAmsTmd2772 && GetAppId() != kAppIdRohmRpr0521 && 63 GetAppId() != kAppIdAmsTmd4903) { 64 return false; 65 } 66 break; 67 68 case SensorType::Barometer: 69 if (GetAppId() != kAppIdBoschBmp280) { 70 return false; 71 } 72 break; 73 74 case SensorType::AmbientLightSensor: 75 if (GetAppId() != kAppIdAmsTmd4903) { 76 return false; 77 } 78 break; 79 80 default: 81 return false; 82 } 83 84 return true; 85 } 86 87 bool AppToHostEvent::CheckEventHeader(SensorType sensor_type) const { 88 if (GetDataLen() < sizeof(struct SensorAppEventHeader)) { 89 return false; 90 } 91 92 if (!CheckAppId(sensor_type)) { 93 return false; 94 } 95 96 return true; 97 } 98 99 bool AppToHostEvent::IsCalibrationEventForSensor(SensorType sensor_type) const { 100 if (!CheckEventHeader(sensor_type)) { 101 return false; 102 } 103 104 // If we made it this far, we only need to confirm the message ID 105 auto header = reinterpret_cast<const struct SensorAppEventHeader *>( 106 GetDataPtr()); 107 return (header->msgId == SENSOR_APP_MSG_CALIBRATION_RESULT); 108 } 109 110 bool AppToHostEvent::IsTestEventForSensor(SensorType sensor_type) const { 111 if (!CheckEventHeader(sensor_type)) { 112 return false; 113 } 114 115 // If we made it this far, we only need to confirm the message ID 116 auto header = reinterpret_cast<const struct SensorAppEventHeader *>( 117 GetDataPtr()); 118 return (header->msgId == SENSOR_APP_MSG_TEST_RESULT); 119 } 120 121 bool AppToHostEvent::IsValid() const { 122 const HostHubRawPacket *packet = GetTypedData(); 123 if (!packet) { 124 return false; 125 } 126 127 // dataLen should specify the amount of data that follows the event type 128 // and HostHubRawPacket headers 129 if (event_data.size() < (sizeof(uint32_t) + sizeof(struct HostHubRawPacket) 130 + packet->dataLen)) { 131 LOGW("Invalid/short AppToHost event of size %zu", event_data.size()); 132 return false; 133 } 134 135 return true; 136 } 137 138 const HostHubRawPacket *AppToHostEvent::GetTypedData() const { 139 // After the event type header (uint32_t), we should have struct 140 // HostHubRawPacket 141 if (event_data.size() < sizeof(uint32_t) + sizeof(struct HostHubRawPacket)) { 142 LOGW("Invalid/short AppToHost event of size %zu", event_data.size()); 143 return nullptr; 144 } 145 return reinterpret_cast<const HostHubRawPacket *>( 146 event_data.data() + sizeof(uint32_t)); 147 } 148 149 } // namespace android 150