1/*
2 * Copyright (C) 2017 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 <condition_variable>
19#include <memory>
20#include <thread>
21#include <vector>
22
23#include <netlink/msg.h>
24
25namespace cvd {
26constexpr int kWifiSimVersion = 1;
27
28class Cmd {
29 public:
30  Cmd();
31  explicit Cmd(nlmsghdr* h);
32  explicit Cmd(nl_msg* h);
33  ~Cmd();
34
35  // Cmd() creates netlink request to be sent to kernel.
36  // Returns netlink message header structure.
37  nl_msg* Msg() const { return msg_; }
38
39  // Responses() holds execution until netlink responds to this message.
40  // Returns all netlink replies.
41  const std::vector<nl_msg*> Responses() const;
42
43  // OnResponse() handles data response from netlink.
44  // Returns value indicating 'done' state:
45  // - false, if more data is expected, or
46  // - true, if processing is complete and instance can be disposed of.
47  bool OnResponse(nl_msg* msg);
48
49  // Wait until message processing is complete.
50  void WaitComplete() const;
51
52 private:
53  nl_msg* msg_;
54  std::vector<nl_msg*> responses_;
55
56  mutable std::mutex ready_mutex_;
57  mutable std::condition_variable ready_signal_;
58
59  Cmd(const Cmd&) = delete;
60  Cmd& operator=(const Cmd&) = delete;
61};
62
63}  // namespace cvd
64