1 /*
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 
17 #pragma once
18 
19 #include <pwd.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include "result.h"
25 #include "uevent.h"
26 #include "uevent_handler.h"
27 
28 namespace android {
29 namespace init {
30 
31 struct ExternalFirmwareHandler {
ExternalFirmwareHandlerExternalFirmwareHandler32     ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path)
33         : devpath(std::move(devpath)), uid(uid), handler_path(std::move(handler_path)) {}
34     std::string devpath;
35     uid_t uid;
36     std::string handler_path;
37 };
38 
39 class FirmwareHandler : public UeventHandler {
40   public:
41     FirmwareHandler(std::vector<std::string> firmware_directories,
42                     std::vector<ExternalFirmwareHandler> external_firmware_handlers);
43     virtual ~FirmwareHandler() = default;
44 
45     void HandleUevent(const Uevent& uevent) override;
46 
47   private:
48     friend void FirmwareTestWithExternalHandler(const std::string& test_name,
49                                                 bool expect_new_firmware);
50 
51     Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid,
52                                            const Uevent& uevent) const;
53     std::string GetFirmwarePath(const Uevent& uevent) const;
54     void ProcessFirmwareEvent(const std::string& root, const std::string& firmware) const;
55 
56     std::vector<std::string> firmware_directories_;
57     std::vector<ExternalFirmwareHandler> external_firmware_handlers_;
58 };
59 
60 }  // namespace init
61 }  // namespace android
62