summaryrefslogtreecommitdiff
path: root/camlibs/sx330z/sx330z.c
blob: 12e29f08282cb784148c418b48be11d475517f0f (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* sx330z.c
 *
 * Copyright © 2002 Dominik Kuhlen <dkuhlen@fhm.edu>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */



#include "sx330z.h"

/* sprintf */
#include "stdio.h"

/* malloc */
#include <stdlib.h>

/* strncmp */
#include <string.h>

#include <gphoto2-endian.h>

/*convert to correct endianness */
int 
sx330z_fill_req(int8_t *buf,struct traveler_req *req)
{
 *((int16_t*)buf)=htole16(req->always1);	buf+=2;
 *((int16_t*)buf)=htole16(req->requesttype);	buf+=2;
 *((int32_t*)buf)=htole32(req->data);		buf+=4;
 *((int32_t*)buf)=htole32(req->timestamp);	buf+=4;
 *((int32_t*)buf)=htole32(req->offset);		buf+=4;
 *((int32_t*)buf)=htole32(req->size);		buf+=4;
 memcpy(buf,req->filename,0x0c);
 return(GP_OK);
} /* sx330z_fill_req */

/*convert to correct endianness */
int 
sx330z_fill_ack(int8_t *buf,struct traveler_ack *ack)
{
 ack->always3=*((uint32_t*)buf);	buf+=4;
 ack->timestamp=*((uint32_t*)buf);	buf+=4;
 ack->size=*((uint32_t*)buf);		buf+=4;
 ack->dontknow=*((uint32_t*)buf);	buf+=4;
 return(GP_OK);
} /* sx330z_fill_ack */

/*convert to correct endianness */
int 
sx330z_fill_toc_page(int8_t *buf,struct traveler_toc_page *toc)
{
 int cnt;
 toc->data0=*((uint32_t*)buf);		buf+=4;
 toc->data1=*((uint32_t*)buf);		buf+=4;
 toc->always1=*((uint16_t*)buf);	buf+=2;
 toc->numEntries=*((uint16_t*)buf);	buf+=2;
 for (cnt=0;cnt<25;cnt++)
 {
  memcpy(toc->entries[cnt].name,buf,12);	buf+=12;
  toc->entries[cnt].time=*((uint32_t*)buf);	buf+=4;
  toc->entries[cnt].size=*((uint32_t*)buf);	buf+=4;
 }
 return(GP_OK);
} /* sx330z_fill_toc_page */

/*
 * SX330z initialization
 *  (not really an initialization, but lets check if we have contact )
 */
int 
sx330z_init(Camera *camera,GPContext *context)
{
/* struct traveler_ack ack;*/
 uint8_t trxbuf[0x10];
 int ret;
 ret=gp_port_usb_msg_read(camera->port,USB_REQ_RESERVED,SX330Z_REQUEST_INIT,0,trxbuf,0x10);
 if (ret!=0x10) return(GP_ERROR); /* more specific about error ? */  
 return(GP_OK); 
} /* sx330z_init */


/*
 * Read block described by req 
 */
int 
sx330z_read_block(Camera *camera,GPContext *context,struct traveler_req *req,char *buf)
{
 int ret;
 uint8_t trxbuf[0x20];
 /* 1. send request */
 sx330z_fill_req(trxbuf,req);
 ret=gp_port_usb_msg_write(camera->port,
    USB_REQ_RESERVED,req->requesttype,0,trxbuf,0x20);
  if (ret!=0x20) return(GP_ERROR_IO_WRITE);
 /* 2. read data */
 ret=gp_port_read(camera->port,buf,req->size);
  if (ret!=req->size)return(GP_ERROR_IO_READ);
 /* 3. read Ack */
 ret=gp_port_read(camera->port,trxbuf,0x10);
  if (ret!=0x10) return(GP_ERROR); 
 /* FIXME : Security check ???*/
 return(GP_OK);
} /* read block */


/*
 * Get TOC size
 */
int 
sx330z_get_toc_num_pages(Camera *camera,GPContext *context,unsigned int *pages)
{
 struct traveler_ack ack;
 uint8_t trxbuf[0x10];
 int ret;

 ret=gp_port_usb_msg_read(camera->port,
    USB_REQ_RESERVED,SX330Z_REQUEST_TOC_SIZE,0,trxbuf,0x10);
 if (ret!=0x10) return(GP_ERROR);

 sx330z_fill_ack(trxbuf,&ack); 		/* convert endianness */

 *pages=ack.size/0x200+1;		/* TOC Pages */

 /* bug in camera ??*/
 if (ack.size==0x200) (*pages)--;
 if ((ack.size>0x200)&&
    (((ack.size-0xc) % 0x200)==0)) (*pages)--;
  
 return(GP_OK);
} /* get toc size */

/*
 * Get TOC 
 * Read a single TOC page 
 * specified by "page"
 */
int 
sx330z_get_toc_page(Camera *camera,GPContext *context,struct traveler_toc_page *TOC,int page)
{
 int ret;
 struct traveler_req req;
 char tocbuf[0x200];
 
 req.always1=1;
 req.requesttype=SX330Z_REQUEST_TOC;	/* 0x03 */ 
 req.offset=0x200*page;			/* offset */
 req.timestamp=0x123;			/* ? */
 req.size=0x200;			/* 512 Bytes / tocpage*/
 req.data=0;				/* ? */
 memset(req.filename,0,12);		/* ? */
 
 ret=sx330z_read_block(camera,context,&req,tocbuf);
 if (ret<0) return(ret);
 
 sx330z_fill_toc_page(tocbuf,TOC); /* convert */
 
 /*  TOC sanity check   */
 if ((TOC->numEntries<0)||(TOC->numEntries>25)) return(GP_ERROR_CORRUPTED_DATA);
 return(GP_OK);
} /* get toc */


/*
 *  get data 
 * could be  Image / Thumbnail 
 */
int 
sx330z_get_data(Camera *camera,GPContext *context, const char *filename,
   		 char **data,unsigned long int *size,int thumbnail)
{
 char *dptr;
 int pages,cnt,ret;
 struct traveler_req req;
 int found;
 int tocpages,tcnt,ecnt;
 struct traveler_toc_page toc;
 int id;/* progress ? */ 
 pages=0;
 found=0;
 memcpy(req.filename,filename,12);
 
 if (thumbnail==SX_THUMBNAIL) 
 {
  if (camera->pl->usb_product == USB_PRODUCT_MD9700)
    pages=7; /* first 28k only*/
   else  
    pages=5; /* first 20k only */
  req.filename[0]='T';		/* 'T'humbnail indicator ?*/
  id=gp_context_progress_start(context,0x1000*pages,"Thumbnail %.4s _",&filename[4]);
 } else
 {
  /* I don't like this solution ... */
  ret=sx330z_get_toc_num_pages(camera,context,&tocpages);	/* number of toc pages */
  if (ret!=GP_OK) return(ret);
  for (tcnt=0;(tcnt<tocpages)&&(!found);tcnt++)
  {
   ret=sx330z_get_toc_page(camera,context,&toc,tcnt);
   for (ecnt=0;ecnt<toc.numEntries;ecnt++)
   {
    if (strncmp(toc.entries[ecnt].name,filename,8)==0)
     {
      found=1;
      *size=toc.entries[ecnt].size;
      break;
     }
   } /* */
  } /* load all toc pages */
  /*return(GP_ERROR);*/
  if (!found) return(GP_ERROR);
  if (((*size % 4096) != 0) || (*size==0)) return(GP_ERROR);  /* sanity check */
  pages=*size/0x1000;
  id=gp_context_progress_start(context,*size,"Picture %.4s _",&filename[4]);
 } /* real image */
  
 *size=4096*pages; 
 *data=malloc(*size);
 dptr=*data;
 /* load all parts */
 for (cnt=0;cnt<pages;cnt++)
 {
  req.always1=1;
  req.requesttype=SX330Z_REQUEST_IMAGE;		/* Imagedata */
  req.offset=cnt*0x1000;
  req.size=0x1000;
  req.timestamp=0x0 +cnt*0x41;			/* timestamp (doesn't matter)?*/
  req.data=0;
  gp_context_progress_update(context,id,(cnt+1)*0x1000);
  sx330z_read_block(camera,context,&req,dptr);  /* read data */
  dptr+=4096;
 }/* download imageparts */     
 gp_context_progress_stop(context,id);
 return(GP_OK);
} /* sx330z_get_data */



/*
 *  delete file 
 */
int 
sx330z_delete_file(Camera *camera,GPContext *context,const char *filename)
{
 struct traveler_req req;
/* struct traveler_ack ack;*/
 char trxbuf[0x20];
 int ret,id;
 req.always1=1;
 req.requesttype=SX330Z_REQUEST_DELETE;					/* Delete */
 req.offset=0x0;
 req.size=0x000;
 req.timestamp=0x0;							/* timestamp (doesn't matter)?*/
 req.data=0;
 sprintf(req.filename,"%.8s",filename);
 sprintf(&req.filename[8],"jpg");					/* discard . */
 id=gp_context_progress_start(context,2,"Deleting %s",filename);	/* start context */
 /* send delete request */
 sx330z_fill_req(trxbuf,&req);
 ret=gp_port_usb_msg_write(camera->port,
    	USB_REQ_RESERVED,SX330Z_REQUEST_DELETE,0,trxbuf,0x20);		
 if (ret!=0x20) return(GP_ERROR);						/* simple error handling */
 gp_context_progress_update(context,id,1);				/* update context */
 /* read 16 Byte acknowledge packet */
 ret=gp_port_usb_msg_read(camera->port,
    	USB_REQ_RESERVED,SX330Z_REQUEST_DELETE,0,trxbuf,0x10);
 if (ret!=0x10) return(GP_ERROR);
 
 gp_context_progress_stop(context,id);					/* stop context */
 return(GP_OK);
} /* sx330z delete file */