blob: 8b758aee7a12ed3f56f0b55c85a2981d20520e76 [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2007 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
Tom Cherrydcb3d152019-08-07 16:02:28 -070017#pragma once
18
19#include <pwd.h>
Tom Cherryed506f72017-05-25 15:58:59 -070020
Jooyung Han21cad322020-09-18 14:41:22 +090021#include <functional>
Tom Cherry7421fa12018-07-13 15:32:02 -070022#include <string>
23#include <vector>
24
Tom Cherrydcb3d152019-08-07 16:02:28 -070025#include "result.h"
Tom Cherryed506f72017-05-25 15:58:59 -070026#include "uevent.h"
Tom Cherry457e28f2018-08-01 13:12:20 -070027#include "uevent_handler.h"
Tom Cherryed506f72017-05-25 15:58:59 -070028
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherrydcb3d152019-08-07 16:02:28 -070032struct ExternalFirmwareHandler {
33 ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path)
34 : devpath(std::move(devpath)), uid(uid), handler_path(std::move(handler_path)) {}
35 std::string devpath;
36 uid_t uid;
37 std::string handler_path;
38};
39
Tom Cherry457e28f2018-08-01 13:12:20 -070040class FirmwareHandler : public UeventHandler {
41 public:
Tom Cherrydcb3d152019-08-07 16:02:28 -070042 FirmwareHandler(std::vector<std::string> firmware_directories,
43 std::vector<ExternalFirmwareHandler> external_firmware_handlers);
Tom Cherry457e28f2018-08-01 13:12:20 -070044 virtual ~FirmwareHandler() = default;
Tom Cherry7421fa12018-07-13 15:32:02 -070045
Tom Cherry457e28f2018-08-01 13:12:20 -070046 void HandleUevent(const Uevent& uevent) override;
47
48 private:
Tom Cherrydcb3d152019-08-07 16:02:28 -070049 friend void FirmwareTestWithExternalHandler(const std::string& test_name,
50 bool expect_new_firmware);
51
52 Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid,
53 const Uevent& uevent) const;
54 std::string GetFirmwarePath(const Uevent& uevent) const;
55 void ProcessFirmwareEvent(const std::string& root, const std::string& firmware) const;
Jooyung Han21cad322020-09-18 14:41:22 +090056 bool ForEachFirmwareDirectory(std::function<bool(const std::string&)> handler) const;
Tom Cherry457e28f2018-08-01 13:12:20 -070057
58 std::vector<std::string> firmware_directories_;
Tom Cherrydcb3d152019-08-07 16:02:28 -070059 std::vector<ExternalFirmwareHandler> external_firmware_handlers_;
Tom Cherry457e28f2018-08-01 13:12:20 -070060};
Tom Cherryed506f72017-05-25 15:58:59 -070061
Tom Cherry81f5d3e2017-06-22 12:53:17 -070062} // namespace init
63} // namespace android