blob: c111f0d8b4ebca7901c265e24be0000c73d6fb6b [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
Prashant Malani324a4e12017-05-01 17:44:17 -070021class Client : public DeviceTracker::Observer {
Prashant Malanif07c9522017-04-11 14:54:29 -070022 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 Malani324a4e12017-05-01 17:44:17 -070052 // This function is a DeviceTracker::Observer override.
53 void OnDeviceAddedOrRemoved(const struct MidisDeviceInfo* dev_info,
54 bool added) override;
55
Prashant Malanif07c9522017-04-11 14:54:29 -070056 base::ScopedFD client_fd_;
57 brillo::MessageLoop::TaskId msg_taskid_;
Prashant Malanif9334732017-04-26 13:06:20 -070058 // The DeviceTracker can be guaranteed to exist for the lifetime of the
59 // service. As such, it is safe to maintain this pointer as a means to make
60 // updates and derive information regarding devices.
61 DeviceTracker* device_tracker_;
Prashant Malanif07c9522017-04-11 14:54:29 -070062 base::WeakPtrFactory<Client> weak_factory_;
63
64 DISALLOW_COPY_AND_ASSIGN(Client);
65};
66
67} // namespace midis
68
69#endif // MIDIS_CLIENT_H_