Home | History | Annotate | Download | only in dynamic_sensor
      1 /*
      2  * Copyright (C) 2017 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 "BaseDynamicSensorDaemon.h"
     18 #include "DynamicSensorManager.h"
     19 #include "utils/Log.h"
     20 
     21 namespace android {
     22 namespace SensorHalExt {
     23 
     24 bool BaseDynamicSensorDaemon::onConnectionChange(const std::string &deviceKey, bool connected) {
     25     bool ret = false;
     26     auto i = mDeviceKeySensorMap.find(deviceKey);
     27     if (connected) {
     28         if (i == mDeviceKeySensorMap.end()) {
     29             ALOGV("device %s is connected", deviceKey.c_str());
     30             // get sensors from implementation
     31             BaseSensorVector sensors = createSensor(deviceKey);
     32             if (sensors.empty()) {
     33                 ALOGI("no valid sensor is defined in device %s, ignore", deviceKey.c_str());
     34             } else {
     35                 ALOGV("discovered %zu sensors from device", sensors.size());
     36                 // build internal table first
     37                 auto result = mDeviceKeySensorMap.emplace(deviceKey, std::move(sensors));
     38                 // then register sensor to dynamic sensor manager, result.first is the iterator
     39                 // of key-value pair; result.first->second is the value, which is s.
     40                 for (auto &i : result.first->second) {
     41                     mManager.registerSensor(i);
     42                 }
     43                 ALOGV("device %s is registered", deviceKey.c_str());
     44                 ret = true;
     45             }
     46         } else {
     47             ALOGD("device %s already added and is connected again, ignore", deviceKey.c_str());
     48         }
     49     } else {
     50         ALOGV("device %s is disconnected", deviceKey.c_str());
     51         if (i != mDeviceKeySensorMap.end()) {
     52             BaseSensorVector sensors = i->second;
     53             for (auto &sensor : sensors) {
     54                 mManager.unregisterSensor(sensor);
     55             }
     56             mDeviceKeySensorMap.erase(i);
     57             // notify implementation
     58             removeSensor(deviceKey);
     59             ALOGV("device %s is unregistered", deviceKey.c_str());
     60             ret = true;
     61         } else {
     62             ALOGV("device not found in registry");
     63         }
     64     }
     65 
     66     return ret;
     67 }
     68 } // namespace SensorHalExt
     69 } // namespace android
     70 
     71