1 /*
2  * Copyright (C) 2019 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 <common/libs/fs/shared_fd.h>
20 #include <stdint.h>
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #ifdef CUTTLEFISH_HOST
27 #include <host/libs/config/cuttlefish_config.h>
28 #endif
29 
30 namespace cvd {
31 
32 class DeviceConfig {
33  public:
34   /**
35    * WARNING: Consider the possibility of different endianness between host and
36    * guest when adding fields of more than one byte to this struct:
37    * This struct is meant to be sent from host to guest so the binary
38    * representation must be the same. There is a static test that checks for
39    * alignment problems, but there is no such thing for endianness.
40    */
41   struct RawData {
42     struct {
43       char ipaddr[16];  // xxx.xxx.xxx.xxx\0 = 16 bytes
44       char gateway[16];
45       char dns[16];
46       char broadcast[16];
47       uint8_t prefixlen;
48       uint8_t reserved[3];
49     } ril;
50     struct {
51       int32_t x_res;
52       int32_t y_res;
53       int32_t dpi;
54       int32_t refresh_rate;
55     } screen;
56   };
57 
58   static std::unique_ptr<DeviceConfig> Get();
59 
60   bool SendRawData(cvd::SharedFD fd);
61 
ril_address_and_prefix()62   const char* ril_address_and_prefix() const {
63     return ril_address_and_prefix_.c_str();
64   };
ril_ipaddr()65   const char* ril_ipaddr() const { return data_.ril.ipaddr; }
ril_gateway()66   const char* ril_gateway() const { return data_.ril.gateway; }
ril_dns()67   const char* ril_dns() const { return data_.ril.dns; }
ril_broadcast()68   const char* ril_broadcast() const { return data_.ril.broadcast; }
ril_prefixlen()69   int ril_prefixlen() const { return data_.ril.prefixlen; }
screen_x_res()70   int32_t screen_x_res() { return data_.screen.x_res; }
screen_y_res()71   int32_t screen_y_res() { return data_.screen.y_res; }
screen_dpi()72   int32_t screen_dpi() { return data_.screen.dpi; }
screen_refresh_rate()73   int32_t screen_refresh_rate() { return data_.screen.refresh_rate; }
74 
75  private:
76   void generate_address_and_prefix();
77 #ifdef CUTTLEFISH_HOST
78   DeviceConfig() = default;
79   bool InitializeNetworkConfiguration(const vsoc::CuttlefishConfig& config);
80   void InitializeScreenConfiguration(const vsoc::CuttlefishConfig& config);
81 #else
82   explicit DeviceConfig(const RawData& data);
83 #endif
84 
85   RawData data_;
86   std::string ril_address_and_prefix_;
87 };
88 
89 }  // namespace cvd
90