ACSMDownloader

ACSMDownloader Git Source Tree

Root/src/acsmdownloader.cpp

Source at commit d5cdc0e334065db3eab216bd65d9a7789c48dbdc created 4 months 20 days ago.
By Grégory Soutadé, Rename main.cpp in acsmdownloader.cpp
1#include <unistd.h>
2#include <getopt.h>
3
4#include <iostream>
5#include <log.h>
6
7#include <QFile>
8#include <QCoreApplication>
9
10#include <dp.h>
11#include <rmsdk_wrapper.h>
12
13#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
14
15int verbose = INFO;
16
17static const char* deviceFile = "device.xml";
18static const char* activationFile = "activation.xml";
19static const char* devicekeyFile = 0;
20static const char* acsmFile = 0;
21static const char* outputFile = 0;
22static const char* outputDir = 0;
23static const char* defaultDirs[] = {
24 "./adobe-digital-editions/",
25 "./.adobe-digital-editions/"
26};
27
28class ACSMDownloader
29{
30public:
31 ACSMDownloader(QCoreApplication* app):
32app(app)
33 {
34
35 }
36
37 static void* run(void* param)
38 {
39ACSMDownloader* _this;
40
41LOG_FUNC();
42
43int ret = dp::platformInit(0xFFFFFFFF);
44
45if (ret) {
46 LOG(ERROR, "Error platform init " << ret);
47 _this->app->exit(ret);
48 return (void*)ret;
49}
50
51dp::cryptRegisterOpenSSL();
52dp::documentRegisterEPUB();
53dp::documentRegisterPDF();
54
55LOG(DEBUG, "Create Adobe objects");
56
57MockDRMProcessorClient processorClient(outputDir, outputFile);
58MockDevice device(&processorClient, deviceFile, activationFile, devicekeyFile);
59MockProvider provider(&device);
60device.setProvider(&provider);
61dpdev::DeviceProvider::addProvider(&provider);
62
63MockNetProvider netProvider;
64dpnet::NetProvider::setProvider(&netProvider);
65
66adept::DRMProviderImpl* _prov = rmsdk::getProvider();
67
68LOG(DEBUG, "Create DRM Processor");
69adept::DRMProcessorImpl* drmprocessor = _prov->createDRMProcessor(&processorClient, &device);
70
71processorClient.setProcessor(drmprocessor);
72
73unsigned char* buffer;
74int buffer_size;
75MockDevice::readFile(acsmFile, &buffer, &buffer_size, true);
76
77LOG(DEBUG, "Init workflow ");
78
79drmprocessor->initWorkflows(WORKFLOW_AUTH_SIGN_IN|WORKFLOW_FULFILLMENT|WORKFLOW_DOWNLOAD|WORKFLOW_NOTIFICATION, dp::Data(buffer, buffer_size));
80
81LOG(DEBUG, "Start work");
82drmprocessor->startWorkflows(WORKFLOW_AUTH_SIGN_IN|WORKFLOW_FULFILLMENT|WORKFLOW_DOWNLOAD|WORKFLOW_NOTIFICATION);
83delete[] buffer;
84
85LOG(DEBUG, "Bye bye");
86
87_this->app->exit(0);
88
89return 0;
90 }
91
92private:
93 QCoreApplication* app;
94};
95
96static const char* findFile(const char* filename)
97{
98 QFile file(filename);
99
100 if (file.exists())
101return strdup(filename);
102
103 for (int i=0; i<(int)ARRAY_SIZE(defaultDirs); i++)
104 {
105uft::String path = uft::String(defaultDirs[i]) + filename;
106file.setFileName(path.c_str());
107if (file.exists())
108 return strdup(path.c_str());
109 }
110
111 return 0;
112}
113
114static void usage(void)
115{
116 std::cout << "Download EPUB file from ACSM request file" << std::endl;
117
118 std::cout << "Usage: ./acsmdownloader [(-d|--device-file) device.xml] [(-a|--activation-file) activation.xml] [(-O|--output-dir) dir] [(-o|--output-file) output.epub] [(-v|--verbose)] [(-h|--help)] (-k|--device-key-file) devkey.bin (-f|--acsm-file) file.acsm" << std::endl << std::endl;
119
120 std::cout << " " << "-d|--device-file" << "\t" << "device.xml file from ereader" << std::endl;
121 std::cout << " " << "-a|--activation-file" << "\t" << "activation.xml file from ereader" << std::endl;
122 std::cout << " " << "-k|--device-key-file" << "\t" << "private device key file (eg devkey.bin) from ereader" << std::endl;
123 std::cout << " " << "-O|--output-dir" << "\t" << "Optional output directory were to put result (default ./)" << std::endl;
124 std::cout << " " << "-o|--output-file" << "\t" << "Optional output epub filename (default <title.epub>)" << std::endl;
125 std::cout << " " << "-f|--acsm-file" << "\t" << "ACSM request file for epub download" << std::endl;
126 std::cout << " " << "-v|--verbose" << "\t\t" << "Increase verbosity, can be set multiple times" << std::endl;
127 std::cout << " " << "-h|--help" << "\t\t" << "This help" << std::endl;
128
129 std::cout << std::endl;
130 std::cout << "Device file and activation file are optional. If not set, they are looked into :" << std::endl;
131 std::cout << " * Current directory" << std::endl;
132 std::cout << " * adobe-digital-editions directory" << std::endl;
133 std::cout << " * .adobe-digital-editions directory" << std::endl;
134}
135
136int main(int argc, char** argv)
137{
138 int c, ret = -1;
139
140 const char** files[] = {&devicekeyFile, &acsmFile};
141
142 while (1) {
143int option_index = 0;
144static struct option long_options[] = {
145 {"device-file", required_argument, 0, 'd' },
146 {"activation-file", required_argument, 0, 'a' },
147 {"device-key-file", required_argument, 0, 'k' },
148 {"output-dir", required_argument, 0, 'O' },
149 {"output-file", required_argument, 0, 'o' },
150 {"acsm-file", required_argument, 0, 'f' },
151 {"verbose", no_argument, 0, 'v' },
152 {"help", no_argument, 0, 'h' },
153 {0, 0, 0, 0 }
154};
155
156c = getopt_long(argc, argv, "d:a:k:O:o:f:vh",
157 long_options, &option_index);
158if (c == -1)
159 break;
160
161switch (c) {
162case 'd':
163 deviceFile = optarg;
164 break;
165case 'a':
166 activationFile = optarg;
167 break;
168case 'k':
169 devicekeyFile = optarg;
170 break;
171case 'f':
172 acsmFile = optarg;
173 break;
174case 'O':
175 outputDir = optarg;
176 break;
177case 'o':
178 outputFile = optarg;
179 break;
180case 'v':
181 verbose++;
182 break;
183case 'h':
184 usage();
185 return 0;
186 break;
187default:
188 usage();
189 return -1;
190}
191 }
192
193 if (!devicekeyFile || !acsmFile)
194 {
195usage();
196return -1;
197 }
198
199 deviceFile = findFile(deviceFile);
200 if (!deviceFile)
201 {
202deviceFile = (deviceFile)?:(char*)"device.xml";
203LOG(ERROR, "Error : " << deviceFile << " not found");
204return -1;
205 }
206
207 activationFile = findFile(activationFile);
208 if (!activationFile)
209 {
210free((void*)deviceFile);
211activationFile = (activationFile)?:(char*)"activation.xml";
212LOG(ERROR, "Error : " << activationFile << " not found");
213return -1;
214 }
215
216 QFile file;
217 int i;
218 for (i=0; i<(int)ARRAY_SIZE(files); i++)
219 {
220file.setFileName(*files[i]);
221if (!file.exists())
222{
223 LOG(ERROR, "Error : " << *files[i] << " doesn't exists");
224 return -1;
225}
226 }
227
228 LOG(INFO, "RMSDK Version " << dp::getVersionInfo("hobbes").utf8());
229
230 QCoreApplication app(argc, argv);
231
232 ACSMDownloader downloader(&app);
233 pthread_t thread;
234 pthread_create(&thread, NULL, ACSMDownloader::run, (void*)&downloader);
235
236 ret = app.exec();
237
238 free((void*)deviceFile);
239 free((void*)activationFile);
240
241 return ret;
242}

Archive Download this file

Branches