1 //
2 // Copyright (C) 2020 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 #pragma once
17 
18 #include <json/json.h>
19 
20 namespace cuttlefish {
21 
22 // Holds the configuration of modem simulator.
23 class NvramConfig {
24 
25  public:
26   static void InitNvramConfigService(size_t num_instances, int sim_type);
27   static const NvramConfig* Get();
28   static void SaveToFile();
29 
30   NvramConfig(size_t num_instances, int sim_type);
31   NvramConfig(NvramConfig&&);
32   ~NvramConfig();
33   NvramConfig& operator=(NvramConfig&&);
34 
35   static std::string ConfigFileLocation();
36   // Saves the configuration object in a file
37   bool SaveToFile(const std::string& file) const;
38 
39   class InstanceSpecific;
40 
41   InstanceSpecific ForInstance(int instance_num) const;
42 
43   std::vector<InstanceSpecific> Instances() const;
44 
45   int sim_type() const;
46 
47   // A view into an existing modem simulator object for a particular instance.
48   class InstanceSpecific {
49   public:
50     int network_selection_mode() const;
51     void set_network_selection_mode(int mode);
52 
53     std::string operator_numeric() const;
54     void set_operator_numeric(std::string& operator_numeric);
55 
56     int modem_technoloy() const;
57     void set_modem_technoloy(int technoloy);
58 
59     int preferred_network_mode() const;
60     void set_preferred_network_mode(int mode);
61 
62     bool emergency_mode() const;
63     void set_emergency_mode(bool mode);
64 
65    private:
66     friend InstanceSpecific NvramConfig::ForInstance(int num) const;
67     friend std::vector<InstanceSpecific> NvramConfig::Instances() const;
68 
InstanceSpecific(const NvramConfig * config,const std::string & id)69     InstanceSpecific(const NvramConfig* config, const std::string& id)
70         : config_(config), id_(id) {}
71 
72     Json::Value* Dictionary();
73     const Json::Value* Dictionary() const;
74 
75     const NvramConfig* config_;
76     std::string id_;
77   };
78 
79  private:
80   static std::unique_ptr<NvramConfig> s_nvram_config;
81   size_t total_instances_;
82   int sim_type_;
83   std::unique_ptr<Json::Value> dictionary_;
84 
85   bool LoadFromFile(const char* file);
86   static NvramConfig* BuildConfigImpl(size_t num_instances, int sim_type);
87 
88   void InitDefaultNvramConfig();
89 
90   NvramConfig(const NvramConfig&) = delete;
91   NvramConfig& operator=(const NvramConfig&) = delete;
92 };
93 
94 }  // namespace cuttlefish
95