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