summaryrefslogtreecommitdiffstats
path: root/hal/gonk/GonkSensorsHelpers.cpp
blob: ccc940c7c0831efe8958b84360d2c22316815a8c (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GonkSensorsHelpers.h"

namespace mozilla {
namespace hal {

//
// Unpacking
//

nsresult
UnpackPDU(DaemonSocketPDU& aPDU, SensorsEvent& aOut)
{
  nsresult rv = UnpackPDU(aPDU, aOut.mType);
  if (NS_FAILED(rv)) {
    return rv;
  }
  rv = UnpackPDU(aPDU, aOut.mTimestamp);
  if (NS_FAILED(rv)) {
    return rv;
  }
  rv = UnpackPDU(aPDU, aOut.mStatus);
  if (NS_FAILED(rv)) {
    return rv;
  }

  size_t i = 0;

  switch (aOut.mType) {
    case SENSORS_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
    case SENSORS_TYPE_GYROSCOPE_UNCALIBRATED:
      /* 6 data values */
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      /* fall through */
    case SENSORS_TYPE_ROTATION_VECTOR:
    case SENSORS_TYPE_GAME_ROTATION_VECTOR:
    case SENSORS_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
      /* 5 data values */
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      /* fall through */
    case SENSORS_TYPE_ACCELEROMETER:
    case SENSORS_TYPE_GEOMAGNETIC_FIELD:
    case SENSORS_TYPE_ORIENTATION:
    case SENSORS_TYPE_GYROSCOPE:
    case SENSORS_TYPE_GRAVITY:
    case SENSORS_TYPE_LINEAR_ACCELERATION:
      /* 3 data values */
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      /* fall through */
    case SENSORS_TYPE_LIGHT:
    case SENSORS_TYPE_PRESSURE:
    case SENSORS_TYPE_TEMPERATURE:
    case SENSORS_TYPE_PROXIMITY:
    case SENSORS_TYPE_RELATIVE_HUMIDITY:
    case SENSORS_TYPE_AMBIENT_TEMPERATURE:
    case SENSORS_TYPE_HEART_RATE:
    case SENSORS_TYPE_TILT_DETECTOR:
    case SENSORS_TYPE_WAKE_GESTURE:
    case SENSORS_TYPE_GLANCE_GESTURE:
    case SENSORS_TYPE_PICK_UP_GESTURE:
    case SENSORS_TYPE_WRIST_TILT_GESTURE:
    case SENSORS_TYPE_SIGNIFICANT_MOTION:
    case SENSORS_TYPE_STEP_DETECTED:
      /* 1 data value */
      rv = UnpackPDU(aPDU, aOut.mData.mFloat[i++]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      break;
    case SENSORS_TYPE_STEP_COUNTER:
      /* 1 data value */
      rv = UnpackPDU(aPDU, aOut.mData.mUint[0]);
      if (NS_FAILED(rv)) {
        return rv;
      }
      break;
    default:
      if (MOZ_HAL_IPC_UNPACK_WARN_IF(true, SensorsEvent)) {
        return NS_ERROR_ILLEGAL_VALUE;
      }
  }
  rv = UnpackPDU(aPDU, aOut.mDeliveryMode);
  if (NS_FAILED(rv)) {
    return rv;
  }
  return NS_OK;
}

} // namespace hal
} // namespace mozilla