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)26std::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() const37uint64_t AppToHostEvent::GetAppId() const { 38 return GetTypedData()->appId; 39 } 40 GetDataLen() const41uint8_t AppToHostEvent::GetDataLen() const { 42 return GetTypedData()->dataLen; 43 } 44 GetDataPtr() const45const uint8_t *AppToHostEvent::GetDataPtr() const { 46 return (reinterpret_cast<const uint8_t*>(GetTypedData()) 47 + sizeof(struct HostHubRawPacket)); 48 } 49 IsCalibrationEventForSensor(SensorType sensor_type) const50bool AppToHostEvent::IsCalibrationEventForSensor(SensorType sensor_type) const { 51 if (GetDataLen() < sizeof(struct SensorAppEventHeader)) { 52 return false; 53 } 54 55 // Make sure the app ID matches what we expect for the sensor type, bail out 56 // early if it doesn't 57 switch (sensor_type) { 58 case SensorType::Accel: 59 case SensorType::Gyro: 60 if (GetAppId() != kAppIdBoschBmi160Bmm150) { 61 return false; 62 } 63 break; 64 65 case SensorType::Proximity: 66 if (GetAppId() != kAppIdAmsTmd2772 && GetAppId() != kAppIdRohmRpr0521 && 67 GetAppId() != kAppIdAmsTmd4903) { 68 return false; 69 } 70 break; 71 72 case SensorType::Barometer: 73 if (GetAppId() != kAppIdBoschBmp280) { 74 return false; 75 } 76 break; 77 78 case SensorType::AmbientLightSensor: 79 if (GetAppId() != kAppIdAmsTmd4903) { 80 return false; 81 } 82 break; 83 84 default: 85 return false; 86 } 87 88 // If we made it this far, we only need to confirm the message ID 89 auto header = reinterpret_cast<const struct SensorAppEventHeader *>( 90 GetDataPtr()); 91 return (header->msgId == SENSOR_APP_MSG_CALIBRATION_RESULT); 92 } 93 IsValid() const94bool AppToHostEvent::IsValid() const { 95 const HostHubRawPacket *packet = GetTypedData(); 96 if (!packet) { 97 return false; 98 } 99 100 // dataLen should specify the amount of data that follows the event type 101 // and HostHubRawPacket headers 102 if (event_data.size() < (sizeof(uint32_t) + sizeof(struct HostHubRawPacket) 103 + packet->dataLen)) { 104 LOGW("Invalid/short AppToHost event of size %zu", event_data.size()); 105 return false; 106 } 107 108 return true; 109 } 110 GetTypedData() const111const HostHubRawPacket *AppToHostEvent::GetTypedData() const { 112 // After the event type header (uint32_t), we should have struct 113 // HostHubRawPacket 114 if (event_data.size() < sizeof(uint32_t) + sizeof(struct HostHubRawPacket)) { 115 LOGW("Invalid/short AppToHost event of size %zu", event_data.size()); 116 return nullptr; 117 } 118 return reinterpret_cast<const HostHubRawPacket *>( 119 event_data.data() + sizeof(uint32_t)); 120 } 121 122 } // namespace android 123