]> sigrok.org Git - pulseview.git/blame - android/assetreader.cpp
android: Read firmware resources from assets
[pulseview.git] / android / assetreader.cpp
CommitLineData
dddff2e7
DE
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "assetreader.hpp"
21#include <libsigrok/libsigrok.h>
22#include <memory>
23#include <QtCore/QDebug>
24#include <QtCore/QFile>
25
26using namespace pv;
27
28AndroidAssetReader::~AndroidAssetReader()
29{}
30
31void AndroidAssetReader::open(struct sr_resource *res, std::string name)
32{
33 if (res->type == SR_RESOURCE_FIRMWARE) {
34 const auto path = QString::fromStdString("assets:/sigrok-firmware/" + name);
35 std::unique_ptr<QFile> file {new QFile{path}};
36
37 if (!file->open(QIODevice::ReadOnly))
38 throw sigrok::Error{SR_ERR};
39
40 const auto size = file->size();
41 if (size < 0)
42 throw sigrok::Error{SR_ERR};
43
44 res->size = size;
45 res->handle = file.release();
46 } else {
47 qWarning() << "AndroidAssetReader: Unknown resource type" << res->type;
48 throw sigrok::Error{SR_ERR};
49 }
50}
51
52void AndroidAssetReader::close(struct sr_resource *res)
53{
54 if (!res->handle) {
55 qCritical("AndroidAssetReader: Invalid handle");
56 throw sigrok::Error{SR_ERR_ARG};
57 }
58 const std::unique_ptr<QFile> file {static_cast<QFile*>(res->handle)};
59 res->handle = nullptr;
60
61 file->close();
62}
63
64size_t AndroidAssetReader::read(const struct sr_resource *res, void *buf, size_t count)
65{
66 if (!res->handle) {
67 qCritical("AndroidAssetReader: Invalid handle");
68 throw sigrok::Error{SR_ERR_ARG};
69 }
70 auto *const file = static_cast<QFile*>(res->handle);
71
72 const auto n_read = file->read(static_cast<char*>(buf), count);
73 if (n_read < 0)
74 throw sigrok::Error{SR_ERR};
75
76 return n_read;
77}