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