Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 1 | // 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 Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 9 | #include <vector> |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/files/scoped_file.h> |
| 12 | #include <base/memory/weak_ptr.h> |
| 13 | #include <brillo/message_loops/message_loop.h> |
| 14 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 15 | #include "midis/device.h" |
| 16 | #include "midis/device_tracker.h" |
Prashant Malani | d0c0f1a | 2017-07-20 15:50:08 -0700 | [diff] [blame] | 17 | #include "midis/libmidis/clientlib.h" |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 18 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 19 | namespace midis { |
| 20 | |
Prashant Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 21 | class Client : public DeviceTracker::Observer { |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 22 | public: |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 23 | using ClientDeletionCallback = base::Callback<void(uint32_t)>; |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 24 | ~Client(); |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 25 | static std::unique_ptr<Client> Create(base::ScopedFD fd, |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 26 | DeviceTracker* device_tracker, |
| 27 | uint32_t client_id, |
| 28 | ClientDeletionCallback del_callback); |
| 29 | void NotifyDeviceAddedOrRemoved(struct MidisDeviceInfo* dev_info, bool added); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 30 | |
| 31 | private: |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 32 | Client(base::ScopedFD fd, DeviceTracker* device_tracker, uint32_t client_id, |
| 33 | ClientDeletionCallback del_cb); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 34 | // 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 Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 38 | // 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 43 | void HandleClientMessages(); |
| 44 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 45 | // 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 Malani | 324a4e1 | 2017-05-01 17:44:17 -0700 | [diff] [blame] | 57 | // This function is a DeviceTracker::Observer override. |
| 58 | void OnDeviceAddedOrRemoved(const struct MidisDeviceInfo* dev_info, |
| 59 | bool added) override; |
| 60 | |
Prashant Malani | 4acb19f | 2017-08-09 11:35:16 -0700 | [diff] [blame^] | 61 | void HandleCloseDeviceMessage(); |
| 62 | |
Prashant Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 63 | // 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 77 | base::ScopedFD client_fd_; |
| 78 | brillo::MessageLoop::TaskId msg_taskid_; |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 79 | // 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 Malani | 3e40a03 | 2017-05-07 21:42:56 -0700 | [diff] [blame] | 83 | uint32_t client_id_; |
| 84 | base::Callback<void(uint32_t)> del_cb_; |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 85 | base::WeakPtrFactory<Client> weak_factory_; |
| 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 88 | }; |
| 89 | |
| 90 | } // namespace midis |
| 91 | |
| 92 | #endif // MIDIS_CLIENT_H_ |