1 /* library.c
2  *
3  * Copyright (C) 2003 Theodore Kilgore <kilgota@auburn.edu>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  */
20 
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <gphoto2/gphoto2.h>
28 
29 #ifdef ENABLE_NLS
30 #  include <libintl.h>
31 #  undef _
32 #  define _(String) dgettext (PACKAGE, String)
33 #  ifdef gettext_noop
34 #    define N_(String) gettext_noop (String)
35 #  else
36 #    define N_(String) (String)
37 #  endif
38 #else
39 #  define _(String) (String)
40 #  define N_(String) (String)
41 #endif
42 
43 #include "lg_gsm.h"
44 #include <gphoto2/gphoto2-port.h>
45 
46 #define GP_MODULE "lg_gsm"
47 
48 struct _CameraPrivateLibrary {
49 	Model model;
50 	Info info[40];
51 };
52 
53 static struct {
54    	char *name;
55 	CameraDriverStatus status;
56    	unsigned short idVendor;
57    	unsigned short idProduct;
58 } models[] = {
59         {"LG T5100", GP_DRIVER_STATUS_EXPERIMENTAL, 0x1004, 0x6005},
60 	{NULL,0,0,0}
61 };
62 
63 int
camera_id(CameraText * id)64 camera_id (CameraText *id)
65 {
66     	strcpy (id->text, "LG GSM camera");
67     	return GP_OK;
68 }
69 
70 int
camera_abilities(CameraAbilitiesList * list)71 camera_abilities (CameraAbilitiesList *list)
72 {
73 	int i;
74 	CameraAbilities a;
75 
76 	for (i = 0; models[i].name; i++) {
77 		memset (&a, 0, sizeof(a));
78 		strcpy (a.model, models[i].name);
79 		a.status = models[i].status;
80 		a.port   = GP_PORT_USB;
81 		a.speed[0] = 0;
82 		a.usb_vendor = models[i].idVendor;
83 		a.usb_product= models[i].idProduct;
84 		a.operations = GP_OPERATION_NONE;
85 		a.folder_operations = GP_FOLDER_OPERATION_NONE;
86 		a.file_operations   = GP_FILE_OPERATION_NONE;
87 		gp_abilities_list_append (list, a);
88 	}
89 	return GP_OK;
90 }
91 
92 static int
camera_summary(Camera * camera,CameraText * summary,GPContext * context)93 camera_summary (Camera *camera, CameraText *summary, GPContext *context)
94 {
95 
96 	char firmware[20];
97 	char firmware_version[20];
98 
99 	memcpy(firmware,&camera->pl->info[0],20);
100 	firmware[19] = 0;
101 	memcpy(firmware_version,&camera->pl->info[20],20);
102 	firmware_version[19] = 0;
103 
104 	sprintf (summary->text,_("Your USB camera seems to be a LG GSM.\n"
105 			"Firmware: %s\n"
106 			"Firmware Version: %s\n"
107 			), firmware, firmware_version);
108 
109 	return GP_OK;
110 }
111 
112 
113 static int
camera_about(Camera * camera,CameraText * about,GPContext * context)114 camera_about (Camera *camera, CameraText *about, GPContext *context)
115 {
116     	strcpy (about->text, _("LG GSM generic driver\n"
117 			    "Guillaume Bedot <littletux@zarb.org>\n"));
118     	return GP_OK;
119 }
120 
121 /*************** File and Downloading Functions *******************/
122 
123 static int
file_list_func(CameraFilesystem * fs,const char * folder,CameraList * list,void * data,GPContext * context)124 file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
125                 void *data, GPContext *context)
126 {
127 	Camera *camera = data;
128 	lg_gsm_list_files (camera->port, list);
129 
130 	return GP_OK;
131 }
132 
133 static int
get_file_func(CameraFilesystem * fs,const char * folder,const char * filename,CameraFileType type,CameraFile * file,void * user_data,GPContext * context)134 get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
135 	       CameraFileType type, CameraFile *file, void *user_data,
136 	       GPContext *context)
137 {
138 	Camera *camera = user_data;
139 
140         int k;
141 	char *data;
142 	unsigned int len;
143 
144 	k = gp_filesystem_number(camera->fs, "/", filename, context);
145 
146 	switch (type) {
147 	case GP_FILE_TYPE_PREVIEW:
148 		return GP_ERROR_NOT_SUPPORTED;
149 	case GP_FILE_TYPE_NORMAL:
150 		len = lg_gsm_get_picture_size (camera->port, k);
151 		GP_DEBUG("len = %i\n", len);
152 		data = malloc(len);
153 		if (!data) {
154 			GP_DEBUG("malloc failed\n");
155 			return GP_ERROR_NO_MEMORY;
156 		}
157 		lg_gsm_read_picture_data (camera->port, data, len, k);
158 		gp_file_append (file, data, len);
159 		free (data);
160 		break;
161 	default:
162 		return GP_ERROR_NOT_SUPPORTED;
163 
164 	}
165 
166 	return GP_OK;
167 }
168 
169 /*************** Exit and Initialization Functions ******************/
170 
171 static int
camera_exit(Camera * camera,GPContext * context)172 camera_exit (Camera *camera, GPContext *context)
173 {
174 	GP_DEBUG ("LG GSM camera_exit");
175 
176 	if (camera->pl) {
177 		free (camera->pl);
178 		camera->pl = NULL;
179 	}
180 
181 	return GP_OK;
182 }
183 
184 static CameraFilesystemFuncs fsfuncs = {
185 	.file_list_func = file_list_func,
186 	.get_file_func = get_file_func
187 };
188 
189 int
camera_init(Camera * camera,GPContext * context)190 camera_init(Camera *camera, GPContext *context)
191 {
192 	GPPortSettings settings;
193 	int ret = 0;
194 
195 	/* First, set up all the function pointers */
196 	camera->functions->summary      = camera_summary;
197 	camera->functions->about        = camera_about;
198 	camera->functions->exit	    = camera_exit;
199 
200 	GP_DEBUG ("Initializing the camera\n");
201 	ret = gp_port_get_settings(camera->port,&settings);
202 	if (ret < 0)
203 		return ret;
204 
205 	switch (camera->port->type) {
206 		case GP_PORT_SERIAL:
207 			return GP_ERROR;
208 		case GP_PORT_USB:
209 			settings.usb.config = 1;
210 			settings.usb.altsetting = 0;
211 			settings.usb.interface = 1;
212 			settings.usb.inep = 0x81;
213 			settings.usb.outep =0x02;
214 			break;
215 		default:
216 			return GP_ERROR;
217 	}
218 
219 	ret = gp_port_set_settings(camera->port,settings);
220 	if (ret < 0)
221 		return ret;
222 
223 	GP_DEBUG("interface = %i\n", settings.usb.interface);
224 	GP_DEBUG("inep = %x\n", settings.usb.inep);
225 	GP_DEBUG("outep = %x\n", settings.usb.outep);
226 
227         /* Tell the CameraFilesystem where to get lists from */
228 	gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
229 
230 	camera->pl = malloc (sizeof (CameraPrivateLibrary));
231 	if (!camera->pl)
232 		return GP_ERROR_NO_MEMORY;
233 	memset (camera->pl, 0, sizeof (CameraPrivateLibrary));
234 
235 	/* Connect to the camera */
236 	lg_gsm_init (camera->port, &camera->pl->model, camera->pl->info);
237 	return GP_OK;
238 }
239