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);
27   static const NvramConfig* Get();
28   static void SaveToFile();
29 
30   NvramConfig(size_t num_instances);
31   NvramConfig(NvramConfig&&);
32   ~NvramConfig();
33   NvramConfig& operator=(NvramConfig&&);
34 
35   std::string ConfigFileLocation() const;
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   // A view into an existing modem simulator object for a particular instance.
46   class InstanceSpecific {
47   public:
48     int network_selection_mode() const;
49     void set_network_selection_mode(int mode);
50 
51     std::string operator_numeric() const;
52     void set_operator_numeric(std::string& operator_numeric);
53 
54     int modem_technoloy() const;
55     void set_modem_technoloy(int technoloy);
56 
57     int preferred_network_mode() const;
58     void set_preferred_network_mode(int mode);
59 
60     bool emergency_mode() const;
61     void set_emergency_mode(bool mode);
62 
63    private:
64     friend InstanceSpecific NvramConfig::ForInstance(int num) const;
65     friend std::vector<InstanceSpecific> NvramConfig::Instances() const;
66 
InstanceSpecific(const NvramConfig * config,const std::string & id)67     InstanceSpecific(const NvramConfig* config, const std::string& id)
68         : config_(config), id_(id) {}
69 
70     Json::Value* Dictionary();
71     const Json::Value* Dictionary() const;
72 
73     const NvramConfig* config_;
74     std::string id_;
75   };
76 
77  private:
78   static std::unique_ptr<NvramConfig> s_nvram_config;
79   size_t total_instances_;
80   std::unique_ptr<Json::Value> dictionary_;
81 
82   bool LoadFromFile(const char* file);
83   static NvramConfig* BuildConfigImpl(size_t num_instances);
84 
85   void InitDefaultNvramConfig();
86 
87   NvramConfig(const NvramConfig&) = delete;
88   NvramConfig& operator=(const NvramConfig&) = delete;
89 };
90 
91 }  // namespace cuttlefish
92