Remove generated files
[framework/connectivity/libgphoto2.git] / camlibs / digita / commands.c
1 /*
2  * commands.c
3  *
4  *  Command set for the digita cameras
5  *
6  * Copyright 1999-2001 Johannes Erdfelt
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #ifdef OS2
32 #include <db.h>
33 #endif
34
35 #include "digita.h"
36 #include "gphoto2-endian.h"
37
38 #define GP_MODULE "digita"
39
40 #include <gphoto2/gphoto2-port.h>
41
42 static void build_command(struct digita_command *cmd, int length, short command)
43 {
44         memset(cmd, 0, sizeof(*cmd));
45
46         /* Length is the sizeof the digita_command minus the length */
47         /*  parameter, plus whatever other data we send */
48         cmd->length = htonl(sizeof(struct digita_command) -
49                 sizeof(unsigned int) + length);
50         cmd->command = htons(command);
51 }
52
53 struct storage_status {
54         struct digita_command cmd;
55
56         unsigned int takencount;
57         unsigned int availablecount;
58         int rawcount;
59 };
60
61 int digita_get_storage_status(CameraPrivateLibrary *dev, int *taken,
62         int *available, int *rawcount)
63 {
64         struct digita_command cmd;
65         struct storage_status ss;
66         int ret;
67
68         build_command(&cmd, 0, DIGITA_GET_STORAGE_STATUS);
69
70         ret = dev->send(dev, &cmd, sizeof(cmd));
71         if (ret < 0) {
72                 GP_DEBUG("digita_get_storage_status: error sending command (ret = %d)", ret);
73                 return -1;
74         }
75
76         ret = dev->read(dev, (unsigned char *)&ss, sizeof(ss));
77         if (ret < 0) {
78                 GP_DEBUG("digita_get_storage_status: error getting count (ret = %d)", ret);
79                 return -1;
80         }
81
82         if (taken)
83                 *taken = ntohl(ss.takencount);
84         if (available)
85                 *available = ntohl(ss.availablecount);
86         if (rawcount)
87                 *rawcount = ntohl(ss.rawcount);
88
89         return 0;
90 }
91
92 int digita_get_file_list(CameraPrivateLibrary *dev)
93 {
94         struct digita_command cmd;
95         struct get_file_list gfl;
96         char *buffer;
97         int ret, taken, buflen;
98         int err;
99
100         if (digita_get_storage_status(dev, &taken, NULL, NULL) < 0)
101                 return -1;
102
103         dev->num_pictures = taken;
104
105         buflen = (taken * sizeof(struct file_item)) + sizeof(cmd) + 4;
106         buffer = malloc(buflen);
107         if (!buffer) {
108                 GP_DEBUG("digita_get_file_list: error allocating %d bytes", buflen);
109                 return -1;
110         }
111
112         build_command(&gfl.cmd, sizeof(gfl) - sizeof(gfl.cmd), DIGITA_GET_FILE_LIST);
113         gfl.listorder = htonl(1);
114
115         ret = dev->send(dev, (void *)&gfl, sizeof(gfl));
116         if (ret < 0) {
117                 GP_DEBUG("digita_get_file_list: error sending command (ret = %d)", ret);
118                 err = -1;
119                 goto end;
120         }
121
122         ret = dev->read(dev, (void *)buffer, buflen);
123         if (ret < 0) {
124                 GP_DEBUG("digita_get_file_list: error receiving data (ret = %d)", ret);
125                 err = -1;
126                 goto end;
127         }
128
129         if (dev->file_list)
130                 free(dev->file_list);
131
132         dev->file_list = malloc(taken * sizeof(struct file_item));
133         if (!dev->file_list) {
134                 GP_DEBUG("digita_get_file_list: error allocating file_list memory (ret = %d)", ret);
135                 err = -1;
136                 goto end;
137         }
138
139         memcpy(dev->file_list, buffer + sizeof(cmd) + 4, taken * sizeof(struct file_item));
140
141         err = 0;
142  end:
143         free(buffer);
144
145         return err;
146 }
147
148 #define GFD_BUFSIZE 19432
149 int digita_get_file_data(CameraPrivateLibrary *dev, int thumbnail,
150         struct filename *filename, struct partial_tag *tag, void *buffer)
151 {
152         struct get_file_data_send gfds;
153         struct get_file_data_receive *gfdr;
154         int ret;
155         char *tbuf;
156
157         build_command(&gfds.cmd, sizeof(gfds) - sizeof(gfds.cmd), DIGITA_GET_FILE_DATA);
158
159         memcpy(&gfds.fn, filename, sizeof(gfds.fn));
160         memcpy(&gfds.tag, tag, sizeof(gfds.tag));
161         gfds.dataselector = htonl(thumbnail);
162
163         tbuf = malloc(GFD_BUFSIZE + sizeof(*gfdr));
164         if (!tbuf) {
165                 GP_DEBUG("digita_get_file_data: unable to allocate %ud bytes",
166                         (unsigned int)(GFD_BUFSIZE + sizeof(*gfdr)));
167
168                 return -1;
169         }
170         gfdr = (struct get_file_data_receive *)tbuf;
171
172         ret = dev->send(dev, &gfds, sizeof(gfds));
173         if (ret < 0) {
174                 GP_DEBUG("digita_get_file_data: error sending command (ret = %d)", ret);
175                 free(tbuf);
176                 return -1;
177         }
178
179         ret = dev->read(dev, gfdr, GFD_BUFSIZE + sizeof(*gfdr));
180         if (ret < 0) {
181                 GP_DEBUG("digita_get_file_data: error reading data (ret = %d)", ret);
182                 return -1;
183         }
184
185         if (gfdr->cmd.result) {
186                 GP_DEBUG("digita_get_file_data: bad result (%d)", gfdr->cmd.result);
187                 return gfdr->cmd.result;
188         }
189
190         memcpy(buffer, tbuf + sizeof(*gfdr), ntohl(gfdr->tag.length) + (thumbnail ? 16 : 0));
191         memcpy(tag, &gfdr->tag, sizeof(*tag));
192
193         free(tbuf);
194
195         return 0;
196 }
197
198 int digita_delete_picture(CameraPrivateLibrary *dev, struct filename *filename)
199 {
200         struct erase_file ef;
201         struct digita_command response;
202         int ret;
203
204         build_command(&ef.cmd, sizeof(ef.fn), DIGITA_ERASE_FILE);
205
206         memcpy(&ef.fn, filename, sizeof(ef.fn));
207         ef.zero = 0;
208
209         ret = dev->send(dev, (unsigned char *)&ef, sizeof(ef));
210         if (ret < 0) {
211                 GP_DEBUG("error sending command (ret = %d)", ret);
212                 return -1;
213         }
214
215         ret = dev->read(dev, (unsigned char *)&response, sizeof(response));
216         if (ret < 0) {
217                 GP_DEBUG("error reading reply (ret = %d)", ret);
218                 return -1;
219         }
220
221         return 0;
222 }
223