phhusson / treble_experimentations

Notes about tinkering with Android Project Treble
3.38k stars 658 forks source link

[Redmi K20 Pro] Front cam pops up on first boot and won't retract #539

Open AndyCGYan opened 5 years ago

AndyCGYan commented 5 years ago

Device: Redmi K20 Pro (raphael) Treble status: Yes, A/B, native Stock Android version: 9 - stock fastboot ROM here

This device has a pop-up front camera, and I was worried about how it'd work before I got the device. That suspicion turned out to be true, but in an unexpected way - on first boot of the GSI, the front cam motor enables and pops the cam out on its own, and refuses to retract afterwards. This makes the phone unsafe to carry around as a daily driver.

For anyone curious about this device: most other basic functions (except auto brightness, which I don't use) seems to work alright as far as I tested - even in-display FP seems to work out-of-the-box (I still have the factory protection film on so can't test how accurate). Likely is because this device is quite similar to Mi 9 (flashing procedure also nearly the same). Just this front cam issue solved and I can have it as my new primary device.

If you need any logs or dumps, or maybe need info on other aspects of the device, I'll gladly provide them when I can.

AndyCGYan commented 5 years ago

Log excerpt from under MIUI, switching to and out of front camera for 3 times. The log is filled with a ton of other error messages, but there are keywords like "PopupCameraManagerService" and "motor_impl" which might be of interest. log.txt

phhusson commented 5 years ago

Have you got the kernel sources? Please grep for motor_impl and PopupCameraManagerService in /system: grep -r -e PopupCameraManagerService -e motor_impl /system And paste the result here If there are some files that matches in /system/framework, please attach the whole /system/framework here

Le dim. 2 juin 2019 à 02:08, Andy Yan notifications@github.com a écrit :

Log excerpt from under MIUI, switching to and out of front camera for 3 times. The log is filled with a ton of other error messages, but there are keywords like "PopupCameraManagerService" and "motor_impl" which might be of interest. log.txt https://github.com/phhusson/treble_experimentations/files/3244314/log.txt

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/phhusson/treble_experimentations/issues/539?email_source=notifications&email_token=AAAA4OTPWY2PWN7QRCRQPWLPYMFOZA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWXK4QQ#issuecomment-497987138, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OUBTF5IEUYOIQ6WES3PYMFOZANCNFSM4HSA47LQ .

AndyCGYan commented 5 years ago
  1. Kernel sources not available yet.
  2. Requested console output:
    raphael:/ # grep -r -e PopupCameraManagerService -e motor_impl /system 2>/dev/null
    Binary file /system/framework/oat/arm64/services.vdex matches
    Binary file /system/app/Cit/oat/arm64/Cit.vdex matches
    Binary file /system/vendor/lib64/hw/vendor.xiaomi.hardware.motor@1.0-impl.so matches
    Binary file /system/vendor/bin/hw/vendor.xiaomi.hardware.motor@1.0-service matches
    Binary file /system/vendor/lib/hw/vendor.xiaomi.hardware.motor@1.0-impl.so matches
  3. Full system+vendor dump here

Have you got the kernel sources? Please grep for motor_impl and PopupCameraManagerService in /system: grep -r -e PopupCameraManagerService -e motor_impl /system And paste the result here If there are some files that matches in /system/framework, please attach the whole /system/framework here Le dim. 2 juin 2019 à 02:08, Andy Yan notifications@github.com a écrit : Log excerpt from under MIUI, switching to and out of front camera for 3 times. The log is filled with a ton of other error messages, but there are keywords like "PopupCameraManagerService" and "motor_impl" which might be of interest. log.txt https://github.com/phhusson/treble_experimentations/files/3244314/log.txt — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#539?email_source=notifications&email_token=AAAA4OTPWY2PWN7QRCRQPWLPYMFOZA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWXK4QQ#issuecomment-497987138>, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OUBTF5IEUYOIQ6WES3PYMFOZANCNFSM4HSA47LQ .

AndyCGYan commented 5 years ago

In the meantime, since I don't care that much about a working front cam, is it possible to forcefully disable it somehow, so the OS doesn't even try to initialize it (which I guess causes it to pop up)? I tried deleting /vendor/etc/permissions/android.hardware.camera.front.xml but that's not enough to make the OS think there's no front cam.

phhusson commented 5 years ago

Ok cool, sounds like we just need to reverse engineer vendor.xiaomi.hardware.motor from services.vdex.

I dont really know how you'd disable that though. Reversing the HAL sounds easier. Hopefully I'll have time to do that today or tomorrow

On stock ROM does camera pop at boot and then hide, or it never pops until camera starts?

AndyCGYan commented 5 years ago

Ok cool, sounds like we just need to reverse engineer vendor.xiaomi.hardware.motor from services.vdex.

Maybe too early to ask, but will the motor be triggered on calling front cam, or a separate app that summons the cam beforehand and hides it after? Personally I'm okay with both, front cam only gets used maybe weekly.

I dont really know how you'd disable that though. Reversing the HAL sounds easier. Hopefully I'll have time to do that today or tomorrow

You da man... Thanks!

On stock ROM does camera pop at boot and then hide, or it never pops until camera starts?

It never pops. Also, if for some reason the camera is already out (e.g. after flashing and booting the GSI), then at the end of the bootanimation it will be automatically retracted.

AndyCGYan commented 5 years ago

Oh yeah, forgot to mention that the regular K20 (davinci - not on the market yet) has the same pop-up mechanism, please add it into the device filter as well when you're ready to commit.

phhusson commented 5 years ago

Here is the reverse-engineered hal:

types.hal

package vendor.xiaomi.hardware.motor@1.0;

struct MotorEvent {
        int32_t vaalue;
        int32_t cookie;
};

IMotor.hal

package vendor.xiaomi.hardware.motor@1.0;

interface IMotor {
        popupMotor(int32_t cookie);
        takebackMotor(int32_t cookie);
        setMotorCallback(IMotorCallback motorcallback);
        init();
        release();
        getMotorStatus() generates (int32_t result);
        calibration();
        takebackMotorShortly();
};

IMotorCallback.hal

package vendor.xiaomi.hardware.motor@1.0;

interface IMotorCallback {
        oneway onNotify(MotorEvent event);
};
AndyCGYan commented 5 years ago

Here is the reverse-engineered hal:

Can I test it out on my side? I assume this goes somewhere into vendor/interfaces? Sorry, not familiar with actual hardware stuff...

EDIT: Gonna read how other HALs were added and do it similarly, hope it works...

phhusson commented 5 years ago

I recommend you to wait, but if you want to try it yourself, you need a "phh" AOSP tree, put the files I've mentioned in it a vendor/interfaces (it's Java like tree with folders matching package name), call hidl-gen -L androidbp on it, then do a little program to call that HAL, you can see samples in device/phh/treble/cmds/

Le dim. 2 juin 2019 à 16:30, Andy Yan notifications@github.com a écrit :

Here is the reverse-engineered hal:

Can I test it out on my side? I assume this goes somewhere into vendor/interfaces? Sorry, not familiar with actual hardware stuff...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/phhusson/treble_experimentations/issues/539?email_source=notifications&email_token=AAAA4OUFDCYOUYSWVZOSCTLPYPKRNA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWXW5IA#issuecomment-498036384, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OXSHEPNA44ZDJP2KGDPYPKRNANCNFSM4HSA47LQ .

AndyCGYan commented 5 years ago

I recommend you to wait, but if you want to try it yourself, you need a "phh" AOSP tree, put the files I've mentioned in it a vendor/interfaces (it's Java like tree with folders matching package name), call hidl-gen -L androidbp on it, then do a little program to call that HAL, you can see samples in device/phh/treble/cmds/

Well seems I'm the only guy who's after running GSI on this device right now, so of course I'll have to test somehow. Are you going to make a separate fork which I can sync from? But yeah, I just tried putting in just the vendor/interfaces files (btw, you forgot to import the callback in IMotor.hal) and generating bp, without calling it (I thought it was unnecessary), and yeah it didn't make a difference. I have no idea how to write the "calling" cmd though, so yeah for now I'll wait on you.

AndyCGYan commented 5 years ago

So uh, I got impatient, and after looking up some stuff and reading your codes, I wrote this:

#include <iostream>
#include <unistd.h>
#include <vendor/xiaomi/hardware/motor/1.0/IMotor.h>

using ::vendor::xiaomi::hardware::motor::V1_0::IMotor;
using ::vendor::xiaomi::hardware::motor::V1_0::IMotorCallback;
using ::vendor::xiaomi::hardware::motor::V1_0::MotorEvent;
using ::android::sp;
using ::android::hardware::Return;
using ::android::hardware::Void;

class MotorCallback: public IMotorCallback {
public:
    MotorCallback() {}
    ~MotorCallback() {}
    Return<void> onNotify(const MotorEvent& event) {
        return Void();
    }
};

int main(int argc, char **argv) {
    auto svc = IMotor::getService();
    if(svc == nullptr) {
        std::cerr << "Failed getting IMotor" << std::endl;
        return -1;
    }
    if(argc != 2) {
        fprintf(stderr, "Usage: %s <POPUP|TAKEBACK|SETCALLBACK|INIT|RELEASE>\n", argv[0]);
        return -2;
    }
    sp<MotorCallback> motorcallback = new MotorCallback();
    std::string action(argv[1]);
    if(action == "POPUP")
        svc->popupMotor(1);
    if(action == "TAKEBACK")
        svc->takebackMotor(1);
    if(action == "SETCALLBACK")
        svc->setMotorCallback(motorcallback);
    if(action == "INIT")
        svc->init();
    if(action == "RELEASE")
        svc->release();
}

The binary was built just fine, but when I called it in adb shell, it simply returns "Failed getting IMotor", meaning I don't even get to test how it works...

phhusson commented 5 years ago

Helper pushed in https://github.com/phhusson/device_phh_treble/commit/819cbe360d7722b9fa8a4e5bb8884947d6bf38f8 Interface pushed in https://github.com/phhusson/vendor_interfaces/commit/be5b914c0f9e7e2bdf735f6d8dee946133ebfb06

Here are prebuilts: https://treble.phh.me/vendor.xiaomi.hardware.motor@1.0.so https://treble.phh.me/xiaomi-motor

Now, push those two files in /data/local/tmp, then in adb do: LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/xiaomi-motor "command"

where command can be:

the "42" here are placeholders for integers. I don't know the meaning of those integers (it's called cookie in Xiaomi HAL), but my guess is that you're supposed to use incrementing value at every command.

So please test those various commands and answer what happens when doing what.

AndyCGYan commented 5 years ago

So please test those various commands and answer what happens when doing what.

Liked that "42" there :D

As for the test result:

Looking great so far!

P.S. I've already created a Wiki page for this device, will submit some other not-so-glaring issues as well while I have your attention.

phhusson commented 5 years ago

I don't really understand why the attempt you pasted here didn't work (sorry i missed it when i wrote mine -_-')

Ok it's good news it works. The double takeback issue will be annoying... As for takeback vs takebackShortly, i still won't use the later one :P

My guess for init is that if we call init before framework is ready, then the camera won't popup on boot. You can try to call xiaomi-motor init from some system/etc/init/xxx.rc

I guess next step is to integrate that into cameraservice. Or we could have an intermediate version where it's crudely integrated into treble app.

AndyCGYan commented 5 years ago

Ok it's good news it works. The double takeback issue will be annoying... ... I guess next step is to integrate that into cameraservice. Or we could have an intermediate version where it's crudely integrated into treble app.

Hope you could sort out the former solution (it should pop-up/retract automatically then, right?), and also take care of the double action issue in the process. Also, I noticed that I can pair xiaomi-motor with the existing vendor.xiaomi.hardware.motor@1.0.so under /vendor/lib64: LD_LIBRARY_PATH=/vendor/lib64 xiaomi-motor "command". This should make for a crude but immediately usable intermediate solution for me - just drop xiaomi-motor into /system/bin and the cam can be controlled with a one-liner.

My guess for init is that if we call init before framework is ready, then the camera won't popup on boot. You can try to call xiaomi-motor init from some system/etc/init/xxx.rc

When writing the rc, what group should I assign vendor.xiaomi.hardware.motor to, and in which section (on boot?) should I trigger init? rc confuses me even more than writing the helper @_@

phhusson commented 5 years ago

I guess next step is to integrate that into cameraservice. Or we could have an intermediate version where it's crudely integrated into treble app.

Hope you could sort out the former solution (it should pop-up/retract automatically then, right?), and also take care of the double action issue in the process.

Well it's possible to have popup/retract automatically in both cases. cameraservice is less flexible but most likely more stable. treble_app would probably do it on camera app launch though

Also, I noticed that I can pair xiaomi-motor with the existing vendor.xiaomi.hardware.motor@1.0.so under /vendor/lib64: LD_LIBRARY_PATH=/vendor/lib64 xiaomi-motor "command". This should make for a crude but immediately usable intermediate solution for me - just drop xiaomi-motor into /system/bin and the cam can be controlled with a one-liner.

You should rather push the vendor.xiaomi.hardware.motor@1.0.so i've provided to system/lib64 if you do that simply xiaomi-motor will work (this is what will happen in my release)

My guess for init is that if we call init before framework is ready, then the camera won't popup on boot. You can try to call xiaomi-motor init from some system/etc/init/xxx.rc

When writing the rc, what group should I assign vendor.xiaomi.hardware.motor to, and in which section (on boot?) should I trigger init? rc confuses me even more than writing the helper @_@

no group, leave default ( = root) you'll need to set selinux label to u:r:phhsu_daemon:s0 (I think keyword for that is seclabel) declare it as a oneshot service, with class main. I think "class main" is the right time for that. If not, try other classes.

AndyCGYan commented 5 years ago

Well it's possible to have popup/retract automatically in both cases. cameraservice is less flexible but most likely more stable. treble_app would probably do it on camera app launch though

Good to know, either is good enough for me then.

You should rather push the vendor.xiaomi.hardware.motor@1.0.so i've provided to system/lib64 if you do that simply xiaomi-motor will work (this is what will happen in my release)

Yeah I made a build with your latest commits, and now the /system lib works as well. For now I've paired it with the app "Terminal Shortcut" that can execute the command for me one-tap (well, 2-tap). Also I realized that the reason that my prior try at writing the helper seems to fail is simply because I forgot to su before executing the command... Derp.

no group, leave default ( = root) you'll need to set selinux label to u:r:phhsu_daemon:s0 (I think keyword for that is seclabel) declare it as a oneshot service, with class main. I think "class main" is the right time for that. If not, try other classes.

I wrote this /system/etc/init/vendor.xiaomi.hardware.motor@1.0-service.rc:

service motor-hal-1-0 /vendor/bin/hw/vendor.xiaomi.hardware.motor@1.0-service
    class core
    user system
    seclabel u:r:phhsu_daemon:s0
    oneshot

on boot
    xiaomi-motor init

...which doesn't work (I tried both class main and class core). Maybe there's still some errors within what I wrote?

penn5 commented 5 years ago

When the device is in offline charging does the camera pop up?

penn5 commented 5 years ago

I wrote this /system/etc/init/vendor.xiaomi.hardware.motor@1.0-service.rc:

service motor-hal-1-0 /vendor/bin/hw/vendor.xiaomi.hardware.motor@1.0-service
    class core
    user system
    seclabel u:r:phhsu_daemon:s0
    oneshot

on boot
    xiaomi-motor init

...which doesn't work (I tried both class main and class core). Maybe there's still some errors within what I wrote?

You're missing the LD_LIBRARY_PATH Try using sh -c as the service setting Where command includes LD settings i.e. is the one you run in terminal And if it's in a class, no need to start on boot.

phhusson commented 5 years ago
service xiaomi-motor-init /system/bin/xiaomi-motor init
    class main
    seclabel u:r:phhsu_daemon:s0
    oneshot

on boot
    start xiaomi-motor-start
AndyCGYan commented 5 years ago

When the device is in offline charging does the camera pop up?

The device doesn't offline charge - it flashes a weird cyan-colored screen. But yeah, in that case it doesn't pop up.

You're missing the LD_LIBRARY_PATH Try using sh -c as the service setting Where command includes LD settings i.e. is the one you run in terminal And if it's in a class, no need to start on boot.

I tried with both LD_LIBRARY_PATH present and absent...

service xiaomi-motor-init /system/bin/xiaomi-motor init
    class main
    seclabel u:r:phhsu_daemon:s0
    oneshot

on boot
    start xiaomi-motor-start

Doesn't work... I should mention that the front cam pops up really early - right before the "Redmi" logo disappears and bootanimation comes up.

phhusson commented 5 years ago

Oh ok. Sounds like it's the kernel or even the bootloader that decides to do that. There isn't much we can do. Just call take back instead of init then

Le lun. 3 juin 2019 à 17:31, Andy Yan notifications@github.com a écrit :

When the device is in offline charging does the camera pop up?

The device doesn't offline charge - it flashes a weird cyan-colored screen. But yeah, in that case it doesn't pop up.

You're missing the LD_LIBRARY_PATH Try using sh -c as the service setting Where command includes LD settings i.e. is the one you run in terminal And if it's in a class, no need to start on boot.

I tried with both LD_LIBRARY_PATH present and absent...

service xiaomi-motor-init /system/bin/xiaomi-motor init class main seclabel u:r:phhsu_daemon:s0 oneshot

on boot start xiaomi-motor-start

Doesn't work... I should mention that the front cam pops up really early - right before the "Redmi" logo disappears.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/phhusson/treble_experimentations/issues/539?email_source=notifications&email_token=AAAA4OXMVO5HYA3EBQ5RRATPYU2OLA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWZY6ZI#issuecomment-498306917, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OSWFJJ42H5WUAOMGSDPYU2OLANCNFSM4HSA47LQ .

AndyCGYan commented 5 years ago

Oh ok. Sounds like it's the kernel or even the bootloader that decides to do that. There isn't much we can do. Just call take back instead of init then

I thought of this as well, but then how come MIUI doesn't pop it up...? Kinda confused there. Also, changing init to takeback <value> doesn't work either, so it seems the script just wasn't ran at all. To reiterate, I have the script as /system/etc/init/vendor.xiaomi.hardware.motor@1.0-service.rc and set proper permissions. Anything I'm missing?

phhusson commented 5 years ago

K, first thing to try is remove the on boot and the line after it

Then, if it still doesn't work, please provide kernel logs (dmesg)

AndyCGYan commented 5 years ago

K, first thing to try is remove the on boot and the line after it

Then, if it still doesn't work, please provide kernel logs (dmesg)

Yeah, still nada. dmesg (60 secs long - should be enough) here. Seems the "drv8846" keyword is particularly interesting - DRV8846 is a stepper motor driver. Should I also get dmesg on MIUI as comparison?

penn5 commented 5 years ago

At what bootstage does the camera pop up? Right as the boot anim begins? Or just as the lockscreen shows? Or before the boot animation?

AndyCGYan commented 5 years ago

At what bootstage does the camera pop up? Right as the boot anim begins? Or just as the lockscreen shows? Or before the boot animation?

As said earlier...

I should mention that the front cam pops up really early - right before the "Redmi" logo disappears and bootanimation comes up.

penn5 commented 5 years ago

oh damn that makes very little sense. perhaps you could remove the motor hal from /vendor (take backup!!!) and see if it still happens or use xiaomi-motor to takeback cam then run stop; start

AndyCGYan commented 5 years ago

oh damn that makes very little sense. perhaps you could remove the motor hal from /vendor (take backup!!!) and see if it still happens or use xiaomi-motor to takeback cam then run stop; start

Camera doesn't pop up after a hot reboot from stop; start. I then killed /vendor/lib*/(hw/)vendor.xiaomi.hardware.motor*.so and rebooted, and the camera doesn't pop up either.

penn5 commented 5 years ago

That's really weird. I guess the kernel has some kind of state machine that the HAL reads to decide whether to pop up?

AndyCGYan commented 5 years ago

That's really weird. I guess the kernel has some kind of state machine that the HAL reads to decide whether to pop up?

Probably. At this point I guess we'd have to wait for the international release of this device, so that the kernel source can be made available. Still, I don't really mind it popping up once on boot; connecting it to cameraserver is the main deal. For now I'll live with manual control - I'll set up the device as daily driver in a few hours and wait for progress.

AndyCGYan commented 5 years ago

@phhusson Is it considered OK if I make a pull request to device_phh_treble that enables building xiaomi_motor, given that it's ultimately a debug command? I'm thinking about pushing the audio fix along with this as well.

phhusson commented 5 years ago

Yup, go ahead. I already have multiple debug tools included.

Le mer. 5 juin 2019 à 20:19, Andy Yan notifications@github.com a écrit :

@phhusson https://github.com/phhusson Is it considered OK if I make a pull request to device_phh_treble that enables building xiaomi_motor, given that it's ultimately a debug command? I'm thinking about pushing the audio fix along with this as well.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/phhusson/treble_experimentations/issues/539?email_source=notifications&email_token=AAAA4OQHFILW5NWETQ6UCDTPZB65XA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXBTQDA#issuecomment-499333132, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OU6SFVSYEIBTRB6UCLPZB65XANCNFSM4HSA47LQ .

AndyCGYan commented 5 years ago

@phhusson Somewhat off-topic, but where can I ask you about something that probably doesn't qualify as an issue here?

AndyCGYan commented 5 years ago

Kernel source just dropped: https://github.com/MiCode/Xiaomi_Kernel_OpenSource/tree/raphael-p-oss - hopefully something helpful inside.

whitechampaca commented 5 years ago

I also had the same error. Can you show me how to fix it most easily? Thanks for the help.

YadominJinta commented 5 years ago

Can't tackback after I run ./xiaomi-motor tackbackShortly 1

YadominJinta commented 5 years ago

It works again after I reboot several times, and I notice ./xiaomi-motor doesn't need a root access(I use it in termux) ,which is not the same as wiki mentioned.

AndyCGYan commented 5 years ago

I notice ./xiaomi-motor doesn't need a root access(I use it in termux) ,which is not the same as wiki mentioned.

I remember back when we testing it did, but now that you mentioned it I tested again and you're right. Updated wiki page accordingly. Also, the takebackShortly command is pretty much useless, ignore it.

AndyCGYan commented 5 years ago

Well, today I got some free time and tried my hands at auto popup/takeback, and after an afternoon/night of attempts, I think I've written the dirtiest hack I've ever did... https://github.com/AndyCGYan/device_phh_treble/commit/b738dcd88511ceeae213e8d944c2793f54701b18 https://github.com/AndyCGYan/android_frameworks_av/commit/9896cab7db57b7daeb4a5b4518f9e5ef74533035 I don't know how to call HAL functions from within CameraService (or if that's even possible), so I had to rely on xiaomi-motor. In the end it worked out fine, and no double popup/takeback issues (since I always check for motorStatus). Can you please review this @phhusson - I hesitate to even submit this hack as a PR.

phhusson commented 5 years ago

I think doing a PR is better so I can comment directly on it? As for HAL integration, I can do that (and if it's a PR I can push directly to the PR :P)

Le lun. 1 juil. 2019 à 18:10, Andy Yan notifications@github.com a écrit :

Well, today I got some free time and tried my hands at auto popup/takeback, and after an afternoon/night of attempts, I think I've written the dirtiest hack I've ever did... AndyCGYan/device_phh_treble@b738dcd https://github.com/AndyCGYan/device_phh_treble/commit/b738dcd88511ceeae213e8d944c2793f54701b18 LineageOS/android_frameworks_av@9896cab https://github.com/LineageOS/android_frameworks_av/commit/9896cab7db57b7daeb4a5b4518f9e5ef74533035 I don't know how to call HAL functions from within CameraService (or if that's even possible), so I had to rely on xiaomi-motor. In the end it worked out fine, and no double popup/takeback issues (since I always check for motorStatus). Can you please review this @phhusson https://github.com/phhusson - I hesitate to even submit this hack as a PR.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/phhusson/treble_experimentations/issues/539?email_source=notifications&email_token=AAAA4OSX5RYDSPAD6UKTUGDP5IT6RA5CNFSM4HSA47L2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY6TYNI#issuecomment-507329589, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAA4OT5GMQCKK34E7Q7243P5IT6RANCNFSM4HSA47LQ .

phhusson commented 5 years ago

Except for SELinux policy, this should do the trick:

diff --git a/services/camera/libcameraservice/Android.mk b/services/camera/libcameraservice/Android.mk
index 96261ab0e..d5a40be93 100644
--- a/services/camera/libcameraservice/Android.mk
+++ b/services/camera/libcameraservice/Android.mk
@@ -81,7 +81,8 @@ LOCAL_SHARED_LIBRARIES:= \
     android.hardware.camera.device@1.0 \
     android.hardware.camera.device@3.2 \
     android.hardware.camera.device@3.3 \
-    android.hardware.camera.device@3.4
+    android.hardware.camera.device@3.4 \
+    vendor.xiaomi.hardware.motor@1.0

 LOCAL_EXPORT_SHARED_LIBRARY_HEADERS := libbinder libcamera_client libfmq

diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp
index 88f7be83b..e34297ce7 100644
--- a/services/camera/libcameraservice/CameraService.cpp
+++ b/services/camera/libcameraservice/CameraService.cpp
@@ -70,6 +70,8 @@
 #include "utils/CameraTraces.h"
 #include "utils/TagMonitor.h"

+#include <vendor/xiaomi/hardware/motor/1.0/IMotor.h>
+
 namespace {
     const char* kPermissionServiceName = "permission";
 }; // namespace anonymous
@@ -1441,6 +1443,15 @@ Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8&
         }
     } // lock is destroyed, allow further connect calls

+
+    auto motorSvc = ::vendor::xiaomi::hardware::motor::V1_0::IMotor::getService();
+    if(motorSvc != nullptr) {
+           auto status = motorSvc->getMotorStatus();
+           if(status == 1 || status == 13) {
+                   motorSvc->popupMotor(1);
+           }
+    }
+
     // Important: release the mutex here so the client can call back into the service from its
     // destructor (can be at the end of the call)
     device = client;
AndyCGYan commented 5 years ago
+    vendor.xiaomi.hardware.motor@1.0

Oh shoot, no wonder I couldn't include the necessary header. Need to write extra sepolicy for this? What's status == 1 for? I've only observed 11 (up) and 13 (down) on my device. Will leave the testing for tomorrow - thanks for the super quick and informative reply!

phhusson commented 5 years ago

Need to write extra sepolicy for this?

I don't know, I guess you'll see :P

What's status == 1 for? I've only observed 11 (up) and 13 (down) on my device.

Ah my bad, that was a poor copy/translate/paste, I read your code as (motorstatus == 1 || motor status == 13) instead of (cameraid == 1 && motor status == 13)

AndyCGYan commented 5 years ago

I don't know, I guess you'll see :P

Indeed:

07-02 09:15:29.332   575   575 E SELinux : avc:  denied  { find } for interface=vendor.xiaomi.hardware.motor::IMotor sid=u:r:cameraserver:s0 pid=1148 scontext=u:r:cameraserver:s0 tcontext=u:object_r:hal_motor_hwservice:s0 tclass=hwservice_manager permissive=0

It does work well when I use a permissive kernel, so I'll be submitting that part of the solution as a PR shortly.

I also tried to write sepolicy, but since I've never done it before, I could only imitate what I read, and for now I couldn't get it working - it builds but summoning the cam doesn't work, with this spit out in log:

07-02 11:11:30.172  1135  2057 W ServiceManagement: getService: unable to call into hwbinder service for vendor.xiaomi.hardware.motor@1.0::IMotor/default.
AndyCGYan commented 5 years ago

So after more swimming in codes, this here is what I wrote... 0001-sepolicy-Grant-cameraserver-permission-to-access-mot.patch.txt But I really don't know jack about SELinux, and that doesn't solve the problem - the "unable to call into hwbinder service" error is still here. Please assist with this last piece of the puzzle if you can...

Meanwhile I'll shift my focus to running takeback on boot somehow. Maybe I missed something earlier.

AndyCGYan commented 5 years ago

So in trying to figure out running xiaomi-motor on boot, I realized I don't have PHH SU (since I run LOS), and turned to embedding the service into init.rc. Doing so results in SELinux denials reported in logcat, which means I just had to figure out sepolicy. And I did. https://github.com/phhusson/platform_system_sepolicy/pull/1 Please review this and see if there's any better way of doing it.

The irony is, this ended up achieving auto popup/takeback, but running xiaomi-motor on boot still doesn't work - no more SELinux denials, but the camera doesn't retract either, and I don't see any meaningful output from logcat/dmesg.

AndyCGYan commented 5 years ago

Now that there are proper ROMs, I've moved on from GSIs. It's been a wild ride... There are some stuff from LOS that I think are worth taking a look though: https://review.lineageos.org/q/topic:%22camera-motor%22+(status:open%20OR%20status:merged) - proper, universal camera motor HAL https://review.lineageos.org/q/topic:%22fod%22+(status:open%20OR%20status:merged) - better in-display FP implementation with proper icon, brightness and orientation-independent facola position @phhusson