blob: 5345b00c7d2b2260f3a148ba158d16d224af38a0 [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"
Prashant Malanid0c0f1a2017-07-20 15:50:08 -070017#include "midis/libmidis/clientlib.h"
Prashant Malanif9334732017-04-26 13:06:20 -070018
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:
Prashant Malani3e40a032017-05-07 21:42:56 -070023 using ClientDeletionCallback = base::Callback<void(uint32_t)>;
Prashant Malanif07c9522017-04-11 14:54:29 -070024 ~Client();
Prashant Malanif9334732017-04-26 13:06:20 -070025 static std::unique_ptr<Client> Create(base::ScopedFD fd,
Prashant Malani3e40a032017-05-07 21:42:56 -070026 DeviceTracker* device_tracker,
27 uint32_t client_id,
28 ClientDeletionCallback del_callback);
29 void NotifyDeviceAddedOrRemoved(struct MidisDeviceInfo* dev_info, bool added);
Prashant Malanif07c9522017-04-11 14:54:29 -070030
31 private:
Prashant Malani3e40a032017-05-07 21:42:56 -070032 Client(base::ScopedFD fd, DeviceTracker* device_tracker, uint32_t client_id,
33 ClientDeletionCallback del_cb);
Prashant Malanif07c9522017-04-11 14:54:29 -070034 // Start monitoring the client socket fd for messages from the client.
35 bool StartMonitoring();
36 // Stop the task which was watching the client socket df.
37 void StopMonitoring();
Prashant Malanif9334732017-04-26 13:06:20 -070038 // Main function to handle all messages sent by the client.
39 // Messages will be of the following format:
40 //
41 // |<--- 4 bytes --->|<---- 4 bytes ---->|<---- payload_size bytes --->|
42 // | message type | size of payload | message payload |
Prashant Malanif07c9522017-04-11 14:54:29 -070043 void HandleClientMessages();
44
Prashant Malanif9334732017-04-26 13:06:20 -070045 // Return the list of all devices available to the client.
46 // The message format will be the same as that used by Client messages.
47 void SendDevicesList();
48
49 // Prepare the payload for devices information.
50 // Payload structure:
51 //
52 // |<-- 1 byte -->|<-- sizeof(struct MidisDeviceInfo) bytes -->|....
53 // | num entries | device1_info | device2_info
54 // |....
55 uint32_t PrepareDeviceListPayload(uint8_t* payload_buf, size_t buf_len);
56
Prashant Malani324a4e12017-05-01 17:44:17 -070057 // This function is a DeviceTracker::Observer override.
58 void OnDeviceAddedOrRemoved(const struct MidisDeviceInfo* dev_info,
59 bool added) override;
60
Prashant Malani4acb19f2017-08-09 11:35:16 -070061 void HandleCloseDeviceMessage();
62
Prashant Malani3e40a032017-05-07 21:42:56 -070063 // On receipt of a REQUEST_PORT message header, this function contacts
64 // the requisite device, obtains an FD for the relevant subdevice, and
65 // returns the FD.
66 // The FD is returned via the cmsg mechanism. The buffer will contain a
67 // struct MidisRequestPort with information regarding the port requested.
68 // The cmsg header will contain information regarding the information
69 // about the FD to be sent over.
70 //
71 // Just as the sendmsg() protocol is used by the server to send the FD, so too
72 // the client must use the recvmsg() protocol to retrieve said FD.
73 void AddClientToPort();
74
75 void TriggerClientDeletion();
76
Prashant Malanif07c9522017-04-11 14:54:29 -070077 base::ScopedFD client_fd_;
78 brillo::MessageLoop::TaskId msg_taskid_;
Prashant Malanif9334732017-04-26 13:06:20 -070079 // The DeviceTracker can be guaranteed to exist for the lifetime of the
80 // service. As such, it is safe to maintain this pointer as a means to make
81 // updates and derive information regarding devices.
82 DeviceTracker* device_tracker_;
Prashant Malani3e40a032017-05-07 21:42:56 -070083 uint32_t client_id_;
84 base::Callback<void(uint32_t)> del_cb_;
Prashant Malanif07c9522017-04-11 14:54:29 -070085 base::WeakPtrFactory<Client> weak_factory_;
86
87 DISALLOW_COPY_AND_ASSIGN(Client);
88};
89
90} // namespace midis
91
92#endif // MIDIS_CLIENT_H_