-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibsane_wrapper_fake.h
95 lines (77 loc) · 3.59 KB
/
libsane_wrapper_fake.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef LORGNETTE_LIBSANE_WRAPPER_FAKE_H_
#define LORGNETTE_LIBSANE_WRAPPER_FAKE_H_
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "lorgnette/libsane_wrapper.h"
namespace lorgnette {
class LibsaneWrapperFake : public LibsaneWrapper {
public:
LibsaneWrapperFake() = default;
LibsaneWrapperFake(const LibsaneWrapperFake&) = delete;
LibsaneWrapperFake& operator=(const LibsaneWrapperFake&) = delete;
~LibsaneWrapperFake() override = default;
static std::unique_ptr<LibsaneWrapper> Create();
SANE_Status sane_init(SANE_Int* version_code,
SANE_Auth_Callback authorize) override;
void sane_exit(void) override;
SANE_Status sane_get_devices(const SANE_Device*** device_list,
SANE_Bool local_only) override;
SANE_Status sane_open(SANE_String_Const name, SANE_Handle* h) override;
void sane_close(SANE_Handle h) override;
const SANE_Option_Descriptor* sane_get_option_descriptor(SANE_Handle h,
SANE_Int n) override;
SANE_Status sane_control_option(
SANE_Handle h, SANE_Int n, SANE_Action a, void* v, SANE_Int* i) override;
SANE_Status sane_get_parameters(SANE_Handle h, SANE_Parameters* p) override;
SANE_Status sane_start(SANE_Handle h) override;
SANE_Status sane_read(SANE_Handle h,
SANE_Byte* buf,
SANE_Int maxlen,
SANE_Int* len) override;
void sane_cancel(SANE_Handle h) override;
SANE_Status sane_set_io_mode(SANE_Handle h, SANE_Bool m) override;
// Creates a handle that will be returned by sane_open(`name`).
SANE_Handle CreateScanner(const std::string& name);
// Sets descriptors that will be returned by sane_get_option_descriptor().
void SetDescriptors(SANE_Handle handle,
const std::vector<SANE_Option_Descriptor>& descriptors);
// Sets parameters that will be returned by sane_get_parameters().
void SetParameters(SANE_Handle handle,
const std::optional<SANE_Parameters>& parameters);
// Adds a response for a sane_read() call. At most `len` bytes will be
// produced when the matching call happens.
void AddSaneReadResponse(SANE_Handle handle,
SANE_Status status,
SANE_Int len);
// Assigns memory for option `field` on `handle`. The memory pointed to by
// `value` must be large enough to contain whatever the option type demands
// and must remain valid until this object is destroyed. The contents of
// `value` will be set and retrieved through sane_control_option().
void SetOptionValue(SANE_Handle handle, size_t field, void* value);
void SetSaneStartResult(SANE_Handle handle, SANE_Status result);
void SetSupportsNonBlocking(SANE_Handle handle, bool support);
protected:
using SaneReadResponse = std::pair<SANE_Status, SANE_Int>;
struct FakeScanner {
std::string name;
SANE_Handle handle;
std::vector<SANE_Option_Descriptor> descriptors;
std::vector<std::optional<void*>> values;
SANE_Status sane_start_result;
bool supports_nonblocking;
std::optional<SANE_Parameters> parameters;
uint8_t current_data_val;
std::vector<SaneReadResponse> read_responses;
size_t current_read_response;
};
std::unordered_map<SANE_Handle, FakeScanner> scanners_;
};
} // namespace lorgnette
#endif // LORGNETTE_LIBSANE_WRAPPER_FAKE_H_