summaryrefslogtreecommitdiff
path: root/camlibs/digita/commands.c
blob: 0f1c9da13427eae36e13eddc31e238e007432b44 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * commands.c
 *
 *  Command set for the digita cameras
 *
 * Copyright 1999-2001 Johannes Erdfelt
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#include "digita.h"
#include "gphoto2-endian.h"

#define GP_MODULE "digita"

#include <gphoto2/gphoto2-port.h>

static void build_command(struct digita_command *cmd, int length, short command)
{
	memset(cmd, 0, sizeof(*cmd));

	/* Length is the sizeof the digita_command minus the length */
	/*  parameter, plus whatever other data we send */
	cmd->length = htobe32(sizeof(struct digita_command) -
		sizeof(unsigned int) + length);
	cmd->command = htobe16(command);
}

struct storage_status {
	struct digita_command cmd;

	unsigned int takencount;
	unsigned int availablecount;
	int rawcount;
};

int digita_get_storage_status(CameraPrivateLibrary *dev, int *taken,
	int *available, int *rawcount)
{
	struct digita_command cmd;
	struct storage_status ss;
	int ret;

	build_command(&cmd, 0, DIGITA_GET_STORAGE_STATUS);

	ret = dev->send(dev, &cmd, sizeof(cmd));
	if (ret < 0) {
		GP_DEBUG("digita_get_storage_status: error sending command (ret = %d)", ret);
		return -1;
	}

	ret = dev->read(dev, (unsigned char *)&ss, sizeof(ss));
	if (ret < 0) {
		GP_DEBUG("digita_get_storage_status: error getting count (ret = %d)", ret);
		return -1;
	}

	if (taken)
		*taken = be32toh(ss.takencount);
	if (available)
		*available = be32toh(ss.availablecount);
	if (rawcount)
		*rawcount = be32toh(ss.rawcount);

	return 0;
}

int digita_get_file_list(CameraPrivateLibrary *dev)
{
	struct digita_command cmd;
	struct get_file_list gfl;
	char *buffer;
	int ret, taken, buflen;
	int err;

	if (digita_get_storage_status(dev, &taken, NULL, NULL) < 0)
		return -1;

	dev->num_pictures = taken;

	buflen = (taken * sizeof(struct file_item)) + sizeof(cmd) + 4;
	buffer = malloc(buflen);
	if (!buffer) {
		GP_DEBUG("digita_get_file_list: error allocating %d bytes", buflen);
		return -1;
	}

	build_command(&gfl.cmd, sizeof(gfl) - sizeof(gfl.cmd), DIGITA_GET_FILE_LIST);
	gfl.listorder = htobe32(1);

	ret = dev->send(dev, (void *)&gfl, sizeof(gfl));
	if (ret < 0) {
		GP_DEBUG("digita_get_file_list: error sending command (ret = %d)", ret);
		err = -1;
		goto end;
	}

	ret = dev->read(dev, (void *)buffer, buflen);
	if (ret < 0) {
		GP_DEBUG("digita_get_file_list: error receiving data (ret = %d)", ret);
		err = -1;
		goto end;
	}

	if (dev->file_list)
		free(dev->file_list);

	dev->file_list = malloc(taken * sizeof(struct file_item));
	if (!dev->file_list) {
		GP_DEBUG("digita_get_file_list: error allocating file_list memory (ret = %d)", ret);
		err = -1;
		goto end;
	}

	memcpy(dev->file_list, buffer + sizeof(cmd) + 4, taken * sizeof(struct file_item));

	err = 0;
 end:
	free(buffer);

	return err;
}

#define GFD_BUFSIZE 19432
int digita_get_file_data(CameraPrivateLibrary *dev, int thumbnail,
	struct filename *filename, struct partial_tag *tag, void *buffer)
{
	struct get_file_data_send gfds;
	struct get_file_data_receive *gfdr;
	int ret;
	char *tbuf;

	build_command(&gfds.cmd, sizeof(gfds) - sizeof(gfds.cmd), DIGITA_GET_FILE_DATA);

	memcpy(&gfds.fn, filename, sizeof(gfds.fn));
	memcpy(&gfds.tag, tag, sizeof(gfds.tag));
	gfds.dataselector = htobe32(thumbnail);

	tbuf = malloc(GFD_BUFSIZE + sizeof(*gfdr));
	if (!tbuf) {
		GP_DEBUG("digita_get_file_data: unable to allocate %ud bytes",
			(unsigned int)(GFD_BUFSIZE + sizeof(*gfdr)));

		return -1;
	}
	gfdr = (struct get_file_data_receive *)tbuf;

	ret = dev->send(dev, &gfds, sizeof(gfds));
	if (ret < 0) {
		GP_DEBUG("digita_get_file_data: error sending command (ret = %d)", ret);
		free(tbuf);
		return -1;
	}

	ret = dev->read(dev, gfdr, GFD_BUFSIZE + sizeof(*gfdr));
	if (ret < 0) {
		GP_DEBUG("digita_get_file_data: error reading data (ret = %d)", ret);
		return -1;
	}

	if (gfdr->cmd.result) {
		GP_DEBUG("digita_get_file_data: bad result (%d)", gfdr->cmd.result);
		return gfdr->cmd.result;
	}

	memcpy(buffer, tbuf + sizeof(*gfdr), be32toh(gfdr->tag.length) + (thumbnail ? 16 : 0));
	memcpy(tag, &gfdr->tag, sizeof(*tag));

	free(tbuf);

	return 0;
}

int digita_delete_picture(CameraPrivateLibrary *dev, struct filename *filename)
{
	struct erase_file ef;
	struct digita_command response;
	int ret;

	build_command(&ef.cmd, sizeof(ef.fn), DIGITA_ERASE_FILE);

	memcpy(&ef.fn, filename, sizeof(ef.fn));
	ef.zero = 0;

	ret = dev->send(dev, (unsigned char *)&ef, sizeof(ef));
	if (ret < 0) {
		GP_DEBUG("error sending command (ret = %d)", ret);
		return -1;
	}

	ret = dev->read(dev, (unsigned char *)&response, sizeof(response));
	if (ret < 0) {
		GP_DEBUG("error reading reply (ret = %d)", ret);
		return -1;
	}

	return 0;
}