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 #include "device_config.h"
18 
19 #include <sstream>
20 #include <type_traits>
21 
22 #include <glog/logging.h>
23 
24 namespace cvd {
25 
26 // TODO(jemoreira): Endianness when on arm64 guest and x86 host is a problem
27 // Raw data is sent through a vsocket from host to guest, this assert tries to
28 // ensure the binary representation of the struct is the same in both sides.
29 static constexpr int kRawDataSize = 68 + 16;  // ril + screen
30 static_assert(sizeof(DeviceConfig::RawData) == kRawDataSize &&
31                   std::is_trivial<DeviceConfig::RawData>().value,
32               "DeviceConfigRawData needs to be the same in host and guess, did "
33               "you forget to update the size?");
34 
35 namespace {
36 
37 static constexpr auto kDataSize = sizeof(DeviceConfig::RawData);
38 
39 }  // namespace
40 
SendRawData(cvd::SharedFD fd)41 bool DeviceConfig::SendRawData(cvd::SharedFD fd) {
42   std::size_t sent = 0;
43   auto buffer = reinterpret_cast<uint8_t*>(&data_);
44   while (sent < kDataSize) {
45     auto bytes = fd->Write(buffer + sent, kDataSize - sent);
46     if (bytes < 0) {
47       // Don't log here, let the caller do it.
48       return false;
49     }
50     sent += bytes;
51   }
52   return true;
53 }
54 
generate_address_and_prefix()55 void DeviceConfig::generate_address_and_prefix() {
56   std::ostringstream ss;
57   ss << ril_ipaddr() << "/" << ril_prefixlen();
58   ril_address_and_prefix_ = ss.str();
59 }
60 
61 }  // namespace cvd