DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (4a108e94d3e2)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <pthread.h>
#include <stdio.h>

#include "mozilla/DebugOnly.h"

#include "base/basictypes.h"
#include "base/thread.h"

#include "Hal.h"
#include "HalLog.h"
#include "HalSensor.h"
#include "hardware/sensors.h"
#include "nsThreadUtils.h"

using namespace mozilla::hal;

namespace mozilla {

// The value from SensorDevice.h (Android)
#define DEFAULT_DEVICE_POLL_RATE 200000000 /*200ms*/
// ProcessOrientation.cpp needs smaller poll rate to detect delay between
// different orientation angles
#define ACCELEROMETER_POLL_RATE 66667000 /*66.667ms*/

double radToDeg(double a) {
  return a * (180.0 / M_PI);
}

static SensorType
HardwareSensorToHalSensor(int type)
{
  switch(type) {
    case SENSOR_TYPE_ORIENTATION:
      return SENSOR_ORIENTATION;
    case SENSOR_TYPE_ACCELEROMETER:
      return SENSOR_ACCELERATION;
    case SENSOR_TYPE_PROXIMITY:
      return SENSOR_PROXIMITY;
    case SENSOR_TYPE_LIGHT:
      return SENSOR_LIGHT;
    case SENSOR_TYPE_GYROSCOPE:
      return SENSOR_GYROSCOPE;
    case SENSOR_TYPE_LINEAR_ACCELERATION:
      return SENSOR_LINEAR_ACCELERATION;
    default:
      return SENSOR_UNKNOWN;
  }
}

static SensorAccuracyType
HardwareStatusToHalAccuracy(int status) {
  return static_cast<SensorAccuracyType>(status);
}

static int
HalSensorToHardwareSensor(SensorType type)
{
  switch(type) {
    case SENSOR_ORIENTATION:
      return SENSOR_TYPE_ORIENTATION;
    case SENSOR_ACCELERATION:
      return SENSOR_TYPE_ACCELEROMETER;
    case SENSOR_PROXIMITY:
      return SENSOR_TYPE_PROXIMITY;
    case SENSOR_LIGHT:
      return SENSOR_TYPE_LIGHT;
    case SENSOR_GYROSCOPE:
      return SENSOR_TYPE_GYROSCOPE;
    case SENSOR_LINEAR_ACCELERATION:
      return SENSOR_TYPE_LINEAR_ACCELERATION;
    default:
      return -1;
  }
}

static int
SensorseventStatus(const sensors_event_t& data)
{
  int type = data.type;
  switch(type) {
    case SENSOR_ORIENTATION:
      return data.orientation.status;
    case SENSOR_LINEAR_ACCELERATION:
    case SENSOR_ACCELERATION:
      return data.acceleration.status;
    case SENSOR_GYROSCOPE:
      return data.gyro.status;
  }

  return SENSOR_STATUS_UNRELIABLE;
}

class SensorRunnable : public nsRunnable
{
public:
  SensorRunnable(const sensors_event_t& data, const sensor_t* sensors, ssize_t size)
  {
    mSensorData.sensor() = HardwareSensorToHalSensor(data.type);
    mSensorData.accuracy() = HardwareStatusToHalAccuracy(SensorseventStatus(data));
    mSensorData.timestamp() = data.timestamp;
    if (mSensorData.sensor() == SENSOR_GYROSCOPE) {
      // libhardware returns gyro as rad.  convert.
      mSensorValues.AppendElement(radToDeg(data.data[0]));
      mSensorValues.AppendElement(radToDeg(data.data[1]));
      mSensorValues.AppendElement(radToDeg(data.data[2]));
    } else if (mSensorData.sensor() == SENSOR_PROXIMITY) {
      mSensorValues.AppendElement(data.data[0]);
      mSensorValues.AppendElement(0);

      // Determine the maxRange for this sensor.
      for (ssize_t i = 0; i < size; i++) {
        if (sensors[i].type == SENSOR_TYPE_PROXIMITY) {
          mSensorValues.AppendElement(sensors[i].maxRange);
        }
      }
    } else if (mSensorData.sensor() == SENSOR_LIGHT) {
      mSensorValues.AppendElement(data.data[0]);
    } else {
      mSensorValues.AppendElement(data.data[0]);
      mSensorValues.AppendElement(data.data[1]);
      mSensorValues.AppendElement(data.data[2]);
    }
    mSensorData.values() = mSensorValues;
  }

  ~SensorRunnable() {}

  NS_IMETHOD Run()
  {
    NotifySensorChange(mSensorData);
    return NS_OK;
  }

private:
  SensorData mSensorData;
  InfallibleTArray<float> mSensorValues;
};

namespace hal_impl {

static DebugOnly<int> sSensorRefCount[NUM_SENSOR_TYPE];
static base::Thread* sPollingThread;
static sensors_poll_device_t* sSensorDevice;
static sensors_module_t* sSensorModule;

static void
PollSensors()
{
  const size_t numEventMax = 16;
  sensors_event_t buffer[numEventMax];
  const sensor_t* sensors;
  int size = sSensorModule->get_sensors_list(sSensorModule, &sensors);

  do {
    // didn't check sSensorDevice because already be done on creating pollingThread.
    int n = sSensorDevice->poll(sSensorDevice, buffer, numEventMax);
    if (n < 0) {
      HAL_ERR("Error polling for sensor data (err=%d)", n);
      break;
    }

    for (int i = 0; i < n; ++i) {
      // FIXME: bug 802004, add proper support for the magnetic field sensor.
      if (buffer[i].type == SENSOR_TYPE_MAGNETIC_FIELD)
        continue;

      // Bug 938035, transfer HAL data for orientation sensor to meet w3c spec
      // ex: HAL report alpha=90 means East but alpha=90 means West in w3c spec
      if (buffer[i].type == SENSOR_TYPE_ORIENTATION) {
        buffer[i].orientation.azimuth = 360 - buffer[i].orientation.azimuth;
        buffer[i].orientation.pitch = -buffer[i].orientation.pitch;
        buffer[i].orientation.roll = -buffer[i].orientation.roll;
      }

      if (HardwareSensorToHalSensor(buffer[i].type) == SENSOR_UNKNOWN) {
        // Emulator is broken and gives us events without types set
        int index;
        for (index = 0; index < size; index++) {
          if (sensors[index].handle == buffer[i].sensor) {
            break;
          }
        }
        if (index < size &&
            HardwareSensorToHalSensor(sensors[index].type) != SENSOR_UNKNOWN) {
          buffer[i].type = sensors[index].type;
        } else {
          HAL_LOG("Could not determine sensor type of event");
          continue;
        }
      }

      NS_DispatchToMainThread(new SensorRunnable(buffer[i], sensors, size));
    }
  } while (true);
}

static void
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId)
{
  int index = HardwareSensorToHalSensor(aSensor.type);

  MOZ_ASSERT(sSensorRefCount[index] || aActivate);

  sSensorDevice->activate(sSensorDevice, aSensor.handle, aActivate);

  if (aActivate) {
    if (aSensor.type == SENSOR_TYPE_ACCELEROMETER) {
      sSensorDevice->setDelay(sSensorDevice, aSensor.handle,
                   ACCELEROMETER_POLL_RATE);
    } else {
      sSensorDevice->setDelay(sSensorDevice, aSensor.handle,
                   DEFAULT_DEVICE_POLL_RATE);
    }
  }

  if (aActivate) {
    sSensorRefCount[index]++;
  } else {
    sSensorRefCount[index]--;
  }
}

static void
SetSensorState(SensorType aSensor, bool activate)
{
  int type = HalSensorToHardwareSensor(aSensor);
  const sensor_t* sensors = nullptr;

  int size = sSensorModule->get_sensors_list(sSensorModule, &sensors);
  for (ssize_t i = 0; i < size; i++) {
    if (sensors[i].type == type) {
      SwitchSensor(activate, sensors[i], pthread_self());
      break;
    }
  }
}

void
EnableSensorNotifications(SensorType aSensor)
{
  if (!sSensorModule) {
    hw_get_module(SENSORS_HARDWARE_MODULE_ID,
                       (hw_module_t const**)&sSensorModule);
    if (!sSensorModule) {
      HAL_ERR("Can't get sensor HAL module\n");
      return;
    }

    sensors_open(&sSensorModule->common, &sSensorDevice);
    if (!sSensorDevice) {
      sSensorModule = nullptr;
      HAL_ERR("Can't get sensor poll device from module \n");
      return;
    }

    sensor_t const* sensors;
    int count = sSensorModule->get_sensors_list(sSensorModule, &sensors);
    for (size_t i=0 ; i<size_t(count) ; i++) {
      sSensorDevice->activate(sSensorDevice, sensors[i].handle, 0);
    }
  }

  if (!sPollingThread) {
    sPollingThread = new base::Thread("GonkSensors");
    MOZ_ASSERT(sPollingThread);
    // sPollingThread never terminates because poll may never return
    sPollingThread->Start();
    sPollingThread->message_loop()->PostTask(FROM_HERE,
                                     NewRunnableFunction(PollSensors));
  }

  SetSensorState(aSensor, true);
}

void
DisableSensorNotifications(SensorType aSensor)
{
  if (!sSensorModule) {
    return;
  }
  SetSensorState(aSensor, false);
}

} // hal_impl
} // mozilla