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" |
| 17 | #include "midis/messages.h" |
| 18 | |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 19 | namespace midis { |
| 20 | |
| 21 | class Client { |
| 22 | public: |
| 23 | ~Client(); |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 24 | static std::unique_ptr<Client> Create(base::ScopedFD fd, |
| 25 | DeviceTracker* device_tracker); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 26 | |
| 27 | private: |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 28 | Client(base::ScopedFD fd, DeviceTracker* device_tracker); |
Prashant Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 29 | // 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 Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 33 | // 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 38 | void HandleClientMessages(); |
| 39 | |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 40 | // 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 52 | base::ScopedFD client_fd_; |
| 53 | brillo::MessageLoop::TaskId msg_taskid_; |
Prashant Malani | f933473 | 2017-04-26 13:06:20 -0700 | [diff] [blame] | 54 | // 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 Malani | f07c952 | 2017-04-11 14:54:29 -0700 | [diff] [blame] | 58 | base::WeakPtrFactory<Client> weak_factory_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(Client); |
| 61 | }; |
| 62 | |
| 63 | } // namespace midis |
| 64 | |
| 65 | #endif // MIDIS_CLIENT_H_ |