blob: eca1d4670ed4b373876a4dd106825f4a05d22726 [file] [log] [blame]
Vivekbalachandar M7af3d832020-02-12 15:56:09 +05301#!/system/bin/sh
2
3# Copyright 2017-2020 Fairphone B.V.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18set -e
19set -u
20set -x
21
22readonly PERSIST_PATH="persist"
23readonly PROP_PATH="fp2.cam"
24
25readonly ANY_FRAGMENT="any"
26readonly SUBDEV_FRAGMENT="dev"
27readonly SENSOR_FRAGMENT="sensor"
28readonly CHANGED_FRAGMENT="changed"
29
30readonly POSITIONS="main front"
31
32readonly MAIN_SENSORS_PATTERN="(ov8865_q8v18a|ov12870)"
33readonly FRONT_SENSORS_PATTERN="(ov2685|ov5670)"
34
35readonly SYSFS_PATH="/sys/devices/fd8c0000.qcom,msm-cam/video4linux/*/name"
36
37
38function _detect_front_sensor() {
39 local sensor_name=`cat ${SYSFS_PATH} | grep -o -E "${FRONT_SENSORS_PATTERN}"`
40 setprop \
41 "${PROP_PATH}.front.${SENSOR_FRAGMENT}" \
42 "${sensor_name}"
43}
44
45
46function _detect_main_sensor() {
47 local sensor_name=`cat ${SYSFS_PATH} | grep -o -E "${MAIN_SENSORS_PATTERN}"`
48 setprop \
49 "${PROP_PATH}.main.${SENSOR_FRAGMENT}" \
50 "${sensor_name}"
51}
52
53
54# Compare persist.* property with new property to detect if main or front
55# Sensor has changed.
56#
57# Sets *.changed property to 1 for any sensor that has changed.
58#
59# Overwrites persist.* property with new sensor name.
60function _detect_and_persist_sensor_change() {
61 local changed_any_sensor=0
62
63 for pos in ${POSITIONS}; do
64 local changed_sensor=0
65
66 local property="${PROP_PATH}.${pos}.${SENSOR_FRAGMENT}"
67
68 local old_sensor="$(getprop "${PERSIST_PATH}.${property}")"
69 local new_sensor="$(getprop "${property}")"
70
71 # advocate change if:
72 # a sensor is detected in the current position
73 # AND a persist value is set (a camera was previously installed)
74 # AND a different sensor is detected than stored in the persist value
75 if [[ -n "${new_sensor}" ]] \
76 && [[ -n "${old_sensor}" ]] \
77 && [[ "${old_sensor}" != "${new_sensor}" ]] ; then
78 setprop "${PROP_PATH}.${pos}.${CHANGED_FRAGMENT}" "1"
79 changed_any_sensor=1
80 fi
81
82 # set new persist value with the new sensor name if it's non-empty
83 if [[ -n "${new_sensor}" ]]; then
84 setprop "${PERSIST_PATH}.${property}" "${new_sensor}"
85 fi
86 done
87
88 echo "${changed_any_sensor}"
89}
90
91
92function _camera_detect_service() {
93 _detect_main_sensor
94 _detect_front_sensor
95 local changed_any_sensor="$(_detect_and_persist_sensor_change)"
96 setprop "${PROP_PATH}.${ANY_FRAGMENT}.${CHANGED_FRAGMENT}" \
97 "${changed_any_sensor}"
98}
99
100_camera_detect_service