blob: 14637dfc9917f673b795c24f9ecc8f410a23478d [file] [log] [blame]
Prashant Malanif07c9522017-04-11 14:54:29 -07001// Copyright 2017 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MIDIS_CLIENT_H_
6#define MIDIS_CLIENT_H_
7
8#include <memory>
Prashant Malanif9334732017-04-26 13:06:20 -07009#include <vector>
Prashant Malanif07c9522017-04-11 14:54:29 -070010
11#include <base/files/scoped_file.h>
12#include <base/memory/weak_ptr.h>
13#include <brillo/message_loops/message_loop.h>
14
Prashant Malanif9334732017-04-26 13:06:20 -070015#include "midis/device.h"
16#include "midis/device_tracker.h"
17#include "midis/messages.h"
18
Prashant Malanif07c9522017-04-11 14:54:29 -070019namespace midis {
20
21class Client {
22 public:
23 ~Client();
Prashant Malanif9334732017-04-26 13:06:20 -070024 static std::unique_ptr<Client> Create(base::ScopedFD fd,
25 DeviceTracker* device_tracker);
Prashant Malanif07c9522017-04-11 14:54:29 -070026
27 private:
Prashant Malanif9334732017-04-26 13:06:20 -070028 Client(base::ScopedFD fd, DeviceTracker* device_tracker);
Prashant Malanif07c9522017-04-11 14:54:29 -070029 // Start monitoring the client socket fd for messages from the client.
30 bool StartMonitoring();
31 // Stop the task which was watching the client socket df.
32 void StopMonitoring();
Prashant Malanif9334732017-04-26 13:06:20 -070033 // Main function to handle all messages sent by the client.
34 // Messages will be of the following format:
35 //
36 // |<--- 4 bytes --->|<---- 4 bytes ---->|<---- payload_size bytes --->|
37 // | message type | size of payload | message payload |
Prashant Malanif07c9522017-04-11 14:54:29 -070038 void HandleClientMessages();
39
Prashant Malanif9334732017-04-26 13:06:20 -070040 // Return the list of all devices available to the client.
41 // The message format will be the same as that used by Client messages.
42 void SendDevicesList();
43
44 // Prepare the payload for devices information.
45 // Payload structure:
46 //
47 // |<-- 1 byte -->|<-- sizeof(struct MidisDeviceInfo) bytes -->|....
48 // | num entries | device1_info | device2_info
49 // |....
50 uint32_t PrepareDeviceListPayload(uint8_t* payload_buf, size_t buf_len);
51
Prashant Malanif07c9522017-04-11 14:54:29 -070052 base::ScopedFD client_fd_;
53 brillo::MessageLoop::TaskId msg_taskid_;
Prashant Malanif9334732017-04-26 13:06:20 -070054 // The DeviceTracker can be guaranteed to exist for the lifetime of the
55 // service. As such, it is safe to maintain this pointer as a means to make
56 // updates and derive information regarding devices.
57 DeviceTracker* device_tracker_;
Prashant Malanif07c9522017-04-11 14:54:29 -070058 base::WeakPtrFactory<Client> weak_factory_;
59
60 DISALLOW_COPY_AND_ASSIGN(Client);
61};
62
63} // namespace midis
64
65#endif // MIDIS_CLIENT_H_