-
Notifications
You must be signed in to change notification settings - Fork 0
/
pktriggercord-servermode.c
280 lines (252 loc) · 8.68 KB
/
pktriggercord-servermode.c
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
/*
pkTriggerCord
Remote control of Pentax DSLR cameras.
Copyright (C) 2011-2014 Andras Salamon <[email protected]>
based on:
pslr-shoot
Command line remote control of Pentax DSLR cameras.
Copyright (C) 2009 Ramiro Barreiro <[email protected]>
With fragments of code from PK-Remote by Pontus Lidman.
<https://sourceforge.net/projects/pkremote>
This program 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 3 of the License, or
(at your option) any later version.
This program 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 General Public License
and GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WIN32
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include "pslr.h"
#include "pslr_lens.h"
long int timeval_diff(struct timeval *t2, struct timeval *t1) {
return (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
}
void camera_close(pslr_handle_t camhandle) {
pslr_disconnect(camhandle);
pslr_shutdown(camhandle);
}
pslr_handle_t camera_connect( char *model, char *device, int timeout, char *error_message ) {
struct timeval prev_time;
struct timeval current_time;
pslr_handle_t camhandle;
gettimeofday(&prev_time, NULL);
while (!(camhandle = pslr_init( model, device ))) {
gettimeofday(¤t_time, NULL);
DPRINT("diff: %f\n", timeval_diff(¤t_time, &prev_time) / 1000000.0);
if( timeout == 0 || timeout > timeval_diff(¤t_time, &prev_time) / 1000000.0 ) {
DPRINT("sleep 1 sec\n");
sleep_sec(1);
} else {
snprintf(error_message, 1000, "%d %ds timeout exceeded\n", 1, timeout);
return NULL;
}
}
DPRINT("before connect\n");
if (camhandle) {
if (pslr_connect(camhandle) ) {
snprintf(error_message, 1000, "%d Unknown Pentax camera found.\n",1);
return NULL;
}
}
return camhandle;
}
#ifndef WIN32
int client_sock;
void write_socket_answer( char *answer ) {
write(client_sock , answer , strlen(answer));
}
void write_socket_answer_bin( uint8_t *answer, uint32_t length ) {
write(client_sock , answer , length);
}
void strip(char *s) {
char *p2 = s;
while(*s != '\0') {
if(*s != '\r' && *s != '\n') {
*p2++ = *s++;
} else {
++s;
}
}
*p2 = '\0';
}
int servermode_socket(int servermode_timeout) {
int socket_desc, c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
char buf[2100];
pslr_handle_t camhandle=NULL;
pslr_status status;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1) {
fprintf(stderr, "Could not create socket");
}
DPRINT("Socket created\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) {
fprintf(stderr, "bind failed. Error");
return 1;
}
DPRINT("bind done\n");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
DPRINT("Waiting for incoming connections...\n");
c = sizeof(struct sockaddr_in);
while( true ) {
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
FD_SET(socket_desc, &rfds);
tv.tv_sec = servermode_timeout;
tv.tv_usec = 0;
retval = select(socket_desc+1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
DPRINT("select error\n");
exit(1);
} else if (retval) {
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0) {
fprintf(stderr, "accept failed");
return 1;
}
DPRINT("Connection accepted\n");
} else {
DPRINT("Timeout\n");
exit(0);
}
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 ) {
client_message[read_size]='\0';
strip( client_message );
DPRINT(":%s:\n",client_message);
if( !strcmp(client_message, "stopserver" ) ) {
if( camhandle ) {
camera_close(camhandle);
}
write_socket_answer("0\n");
exit(0);
} else if( !strcmp(client_message, "disconnect" ) ) {
if( camhandle ) {
camera_close(camhandle);
}
write_socket_answer("0\n");
} else if( !strcmp(client_message, "echo") ) {
sprintf( buf, "0 %s\n", client_message);
write_socket_answer(buf);
} else if( !strcmp(client_message, "connect") ) {
if( camhandle ) {
write_socket_answer("0\n");
} else if( (camhandle = camera_connect( NULL, NULL, -1, buf )) ) {
write_socket_answer("0\n");
} else {
write_socket_answer(buf);
}
} else if( !strcmp(client_message, "update_status") ) {
if( !pslr_get_status(camhandle, &status) ) {
sprintf( buf, "%d\n", 0);
} else {
sprintf( buf, "%d\n", 1);
}
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_camera_name") ) {
if( camhandle ) {
sprintf(buf, "%d %s\n", 0, pslr_camera_name(camhandle));
} else {
sprintf(buf, "%d not connected\n", 1);
}
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_lens_name") ) {
sprintf(buf, "%d %s\n", 0, get_lens_name(status.lens_id1, status.lens_id2));
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_current_shutter_speed") ) {
sprintf(buf, "%d %d/%d\n", 0, status.current_shutter_speed.nom, status.current_shutter_speed.denom);
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_current_aperture") ) {
sprintf(buf, "%d %s\n", 0, format_rational( status.current_aperture, "%.1f"));
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_current_iso") ) {
sprintf(buf, "%d %d\n", 0, status.current_iso);
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_bufmask") ) {
sprintf(buf, "%d %d\n", 0, status.bufmask);
write_socket_answer(buf);
} else if( !strcmp(client_message, "focus") ) {
pslr_focus(camhandle);
sprintf(buf, "%d\n", 0);
write_socket_answer(buf);
} else if( !strcmp(client_message, "shutter") ) {
pslr_shutter(camhandle);
sprintf(buf, "%d\n", 0);
write_socket_answer(buf);
} else if( !strcmp(client_message, "delete_buffer") ) {
// TODO: bufferindex
pslr_delete_buffer(camhandle,0);
sprintf(buf, "%d\n", 0);
write_socket_answer(buf);
} else if( !strcmp(client_message, "get_preview_buffer") ) {
// TODO: bufferindex
uint8_t *pImage;
uint32_t imageSize;
if( pslr_get_buffer(camhandle, 0, PSLR_BUF_PREVIEW, 4, &pImage, &imageSize) ) {
sprintf(buf, "%d %d\n", 1, imageSize);
write_socket_answer(buf);
} else {
sprintf(buf, "%d %d\n", 0, imageSize);
write_socket_answer(buf);
write_socket_answer_bin(pImage, imageSize);
}
} else if( !strcmp(client_message, "get_buffer") ) {
// TODO: bufferindex
uint32_t imageSize;
if( pslr_buffer_open(camhandle, 0, PSLR_BUF_DNG, 0) ) {
sprintf(buf, "%d\n", 1);
write_socket_answer(buf);
} else {
imageSize = pslr_buffer_get_size(camhandle);
sprintf(buf, "%d %d\n", 0, imageSize);
write_socket_answer(buf);
uint32_t current = 0;
while (1) {
uint32_t bytes;
uint8_t buf[65536];
bytes = pslr_buffer_read(camhandle, buf, sizeof (buf));
if (bytes == 0) {
break;
}
write_socket_answer_bin( buf, bytes);
current += bytes;
}
pslr_buffer_close(camhandle);
}
} else {
write_socket_answer("1 Invalid servermode command\n");
}
}
if(read_size == 0) {
DPRINT("Client disconnected\n");
fflush(stdout);
} else if(read_size == -1) {
fprintf(stderr, "recv failed\n");
}
}
return 0;
}
#endif