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
|
#include <string.h>
#include <stdlib.h>
#include "uca-net-protocol.h"
static gboolean
send_default_message (GSocketConnection *connection, UcaNetMessageType type, GError **error)
{
GOutputStream *output;
UcaNetMessageDefault request;
output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
request.type = type;
if (!g_output_stream_write_all (output, &request, sizeof (request), NULL, NULL, error))
return FALSE;
if (!g_output_stream_flush (output, NULL, error))
return FALSE;
return TRUE;
}
static gboolean
handle_default_reply (GSocketConnection *connection, UcaNetMessageType type, GError **error)
{
GInputStream *input;
UcaNetDefaultReply reply;
input = g_io_stream_get_input_stream (G_IO_STREAM (connection));
if (g_input_stream_read_all (input, &reply, sizeof (reply), NULL, NULL, error)) {
g_assert (reply.type == type);
if (reply.error.occurred) {
g_set_error_literal (error, g_quark_from_string (reply.error.domain), reply.error.code, reply.error.message);
return FALSE;
}
return TRUE;
}
return FALSE;
}
gboolean
uca_net_client_get_property (GSocketConnection *connection, const gchar *name, GValue *value, GError **error)
{
UcaNetMessageGetPropertyRequest request;
UcaNetMessageGetPropertyReply reply;
GInputStream *input;
GOutputStream *output;
input = g_io_stream_get_input_stream (G_IO_STREAM (connection));
output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
if (g_input_stream_has_pending (input))
g_input_stream_clear_pending (input);
/* request */
request.type = UCA_NET_MESSAGE_GET_PROPERTY;
strncpy (request.property_name, name, sizeof (request.property_name));
if (!g_output_stream_write_all (output, &request, sizeof (request), NULL, NULL, error))
return FALSE;
/* reply */
if (g_input_stream_read (input, &reply, sizeof (reply), NULL, error) < 0)
return FALSE;
if (reply.type != request.type) {
if (*error != NULL)
/* FIXME: replace with correct error codes */
*error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_NOENT, "Reply does not match request");
return FALSE;
}
if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ENUM)) {
g_value_set_enum (value, atoi (reply.property_value));
}
else {
/* XXX: I'd like to avoid this and rather use g_value_transform(), however
* that call fails with Python and uca-camera-control but succeeds with
* uca-grab ... */
switch (G_VALUE_TYPE (value)) {
case G_TYPE_UINT:
g_value_set_uint (value, atol (reply.property_value));
break;
case G_TYPE_DOUBLE:
g_value_set_double (value, atof (reply.property_value));
break;
case G_TYPE_BOOLEAN:
g_value_set_boolean (value, g_strcmp0 (reply.property_value, "TRUE"));
break;
case G_TYPE_STRING:
g_value_set_string (value, reply.property_value);
break;
default:
g_warning ("Unsupported property type %s", G_VALUE_TYPE_NAME (value));
}
}
return TRUE;
}
gboolean
uca_net_client_set_property (GSocketConnection *connection, const gchar *name, const GValue *value, GError **error)
{
GOutputStream *output;
const gchar *str;
GValue str_value = {0};
UcaNetMessageSetPropertyRequest request = { .type = UCA_NET_MESSAGE_SET_PROPERTY };
output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
g_value_init (&str_value, G_TYPE_STRING);
if (g_type_is_a (G_VALUE_TYPE (value), G_TYPE_ENUM)) {
GValue int_value = {0};
g_value_init (&int_value, G_TYPE_INT);
g_value_transform (value, &int_value);
g_value_transform (&int_value, &str_value);
}
else {
g_value_transform (value, &str_value);
}
str = g_value_get_string (&str_value);
strncpy (request.property_name, name, sizeof (request.property_name));
strncpy (request.property_value, str, sizeof (request.property_value));
if (!g_output_stream_write_all (output, &request, sizeof (request), NULL, NULL, error))
return FALSE;
return handle_default_reply (connection, UCA_NET_MESSAGE_SET_PROPERTY, error);
}
static void
default_handshake (GSocketConnection *connection, UcaNetMessageType type, GError **error)
{
if (!send_default_message (connection, type, error))
return;
handle_default_reply (connection, type, error);
}
void
uca_net_client_start_recording (GSocketConnection *connection, GError **error)
{
default_handshake (connection, UCA_NET_MESSAGE_START_RECORDING, error);
}
void
uca_net_client_stop_recording (GSocketConnection *connection, GError **error)
{
default_handshake (connection, UCA_NET_MESSAGE_STOP_RECORDING, error);
}
void
uca_net_client_start_readout (GSocketConnection *connection, GError **error)
{
default_handshake (connection, UCA_NET_MESSAGE_START_READOUT, error);
}
void
uca_net_client_stop_readout (GSocketConnection *connection, GError **error)
{
default_handshake (connection, UCA_NET_MESSAGE_STOP_READOUT, error);
}
gboolean
uca_net_client_grab (GSocketConnection *connection, gpointer data, gsize size, GError **error)
{
GInputStream *input;
GOutputStream *output;
gsize transmitted;
gsize bytes_left;
UcaNetMessageGrabRequest request = { .type = UCA_NET_MESSAGE_GRAB, .size = size };
input = g_io_stream_get_input_stream (G_IO_STREAM (connection));
output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
/* request */
if (!g_output_stream_write_all (output, &request, sizeof (request), &transmitted, NULL, error)) {
return FALSE;
}
/* error reply */
if (handle_default_reply (connection, UCA_NET_MESSAGE_GRAB, error)) {
bytes_left = size;
while (bytes_left > 0) {
gssize read;
gchar *buffer;
buffer = (gchar *) data;
read = g_input_stream_read (input, &buffer[size - bytes_left], bytes_left, NULL, error);
if (read < 0)
return FALSE;
bytes_left -= read;
}
return TRUE;
}
return FALSE;
}
gboolean
uca_net_client_close (GSocketConnection *connection, GError **error)
{
GOutputStream *output;
UcaNetMessageDefault request = { .type = UCA_NET_MESSAGE_CLOSE_CONNECTION };
output = g_io_stream_get_output_stream (G_IO_STREAM (connection));
if (!g_output_stream_write_all (output, &request, sizeof (request), NULL, NULL, error))
return FALSE;
return TRUE;
}
|