/
dummy_bme.c
344 lines (274 loc) · 8.08 KB
/
dummy_bme.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
@file dummy_bme.c
Dummy bme server for ad hoc testing of dsme.
<p>
Copyright (C) 2009-2010 Nokia Corporation.
@author Semi Malinen <semi.malinen@nokia.com>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme 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 Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include <sys/syslog.h>
#define BME_SRV_SOCK_PATH "/tmp/.bmesrv"
#define BME_SRV_COOKIE "BMentity"
#define BME_SERVER_BACKLOG 1 /* Max client connections */
#define FAILURE_RETRY_COUNT 3 /* We loop at most 3 times upon 'open' req */
#define EM_BATTERY_INFO_REQ 0x06
#define EM_BATTERY_TEMP 0x0004 /* -------------1-- */
#define bme_log(L, FMT ...) fprintf(stderr, FMT);
#include <sys/types.h>
typedef struct {
uint16_t type, subtype;
} tBMEmsgGeneric;
struct emsg_battery_info_req {
uint16_t type, subtype;
uint32_t flags;
};
/* Battery info reply */
struct emsg_battery_info_reply {
uint32_t a;
uint32_t flags;
uint16_t c;
uint16_t d;
uint16_t temp;
uint16_t f;
uint16_t g;
uint16_t h;
uint16_t i;
uint16_t j;
uint16_t k;
uint16_t l;
};
union emsg_battery_info {
struct emsg_battery_info_req request;
struct emsg_battery_info_reply reply;
};
union bme_ext_messages {
union emsg_battery_info battery_info;
};
enum {
UMSD,
CCSD,
NUM_POLL_FDS /* Number of descriptors */
};
static volatile sig_atomic_t sig_caught = 0;
/* Client's socket descriptor that is in listening state */
int umsfd = -1;
/* Function prototypes */
void bme_server_shutdown(void);
int bme_server_init(void);
int bme_handle_new_client(int fd);
int em_srv_battery_info_req(struct emsg_battery_info_req *reqp, int client);
int bme_extmsg_handler(int client);
int bme_reply_client(int client, int status, void *msg, int size);
int bme_send_status_to_client(int client, int status);
void bme_shutdown(void);
/*
* Initialize the server. Returns a negative value in case of failure.
*/
int bme_server_init(void)
{
static const char *sockname = BME_SRV_SOCK_PATH;
struct sockaddr_un addr;
int size;
/* Initialize the socket */
if ((umsfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
bme_log(LOG_ERR, "socket: %s", strerror(errno));
return -1;
}
unlink(sockname);
memset(&addr, 0, sizeof(addr));
addr.sun_family = PF_UNIX;
strncpy(addr.sun_path, sockname, sizeof(addr.sun_path) - 1);
size = strlen(addr.sun_path) + sizeof(addr.sun_family);
if (bind(umsfd, (struct sockaddr*) &addr, size) < 0) {
bme_log(LOG_ERR, "bind: %s", strerror(errno));
close(umsfd);
umsfd = -1;
return -1;
}
chmod(sockname, 0646);
if (listen(umsfd, BME_SERVER_BACKLOG) < 0) {
bme_log(LOG_ERR, "listen: %s", strerror(errno));
close(umsfd);
umsfd = -1;
unlink(sockname);
return -1;
}
return 0;
}
/*
* Shut down the server
*/
void bme_server_shutdown(void)
{
if (umsfd != -1) {
close(umsfd);
umsfd = -1;
}
}
/*
* Handle new client. Returns client's new fd if connection was successful,
* a negative value otherwise.
*/
int bme_handle_new_client(int fd)
{
struct sockaddr_un addr;
socklen_t addr_len;
int size, client;
char cookie[8];
addr_len = sizeof(addr);
client = accept(fd, (struct sockaddr*) &addr, &addr_len);
if (client < 0)
return -1;
/* Get the cookie, check it and acknowledge if cookie is correct */
size = sizeof(cookie);
if ((read(client, cookie, size) < size) ||
(strncmp(cookie, BME_SRV_COOKIE, size)) ||
(write(client, "\n", 1) < 1)) {
close (client);
return -1;
}
return client;
}
int em_srv_battery_info_req(struct emsg_battery_info_req *reqp, int client)
{
struct emsg_battery_info_reply reply;
uint32_t flags;
memset(&reply, 0, sizeof(reply));
flags = reply.flags = reqp->flags;
if (flags & EM_BATTERY_TEMP)
reply.temp = 52;
/* Send the reply */
bme_reply_client(client, 0, &reply, sizeof(reply));
return 1;
}
/*
* Handle client's request.
* Returns a negative value in case of an error, positive (or 0) otherwise.
*
* Currently we support only one client.
*/
int bme_extmsg_handler(int client)
{
int res = -1, n;
char buf[sizeof(union bme_ext_messages)];
void *msgp = buf;
tBMEmsgGeneric *gm = msgp;
if ((n = read(client, buf, sizeof(buf))) < sizeof(*gm)) {
bme_log(LOG_DEBUG, "got %d bytes instead of %zd from client's fd %d (possible disconnect)\n",
n, sizeof(*gm), client);
return -1;
}
bme_log(LOG_DEBUG, "message from client with fd=%d: type=%d, subtype=%d\n",
client, gm->type, gm->subtype);
switch (gm->type) {
case EM_BATTERY_INFO_REQ:
res = em_srv_battery_info_req(msgp, client);
break;
default:
bme_log(LOG_NOTICE, "unknown message: type=%d, subtype=%d\n",
gm->type, gm->subtype);
bme_send_status_to_client(client, -1);
}
return res;
}
/*
* Reply to the client.
* Status (32-bit value) is sent first, then reply message.
* Returns a negative value in case of error, positive value otherwise.
*/
int bme_reply_client(int client, int status, void *msg, int size)
{
if (write(client, &status, sizeof(status)) != sizeof(status))
return -1;
if (write(client, msg, size) != size)
return -1;
return size;
}
/*
* Send only a 32-bit status value to the client.
*/
int bme_send_status_to_client(int client, int status)
{
return write(client, &status, sizeof(status));
}
void bme_shutdown()
{
bme_server_shutdown();
bme_log(LOG_INFO, "BME shutdown complete\n"); /* debug */
}
int main(int argc, char *argv[])
{
struct pollfd ufds[NUM_POLL_FDS];
int res, exit_status = EXIT_FAILURE;
char *name;
name = strrchr(argv[0], '/');
name = name ? (name + 1) : argv[0];
if (bme_server_init() < 0) {
bme_log(LOG_ALERT, "cannot initialize server\n");
goto exit;
}
/* Prepare for poll() */
ufds[UMSD].fd = umsfd; /* External messages */
ufds[UMSD].events = POLLIN;
ufds[CCSD].fd = -1; /* Connected client */
ufds[CCSD].events = POLLIN;
/*
* Main loop. We poll socket descriptors, and call internal or external
* message handler when data for it is available.
*/
while (1) {
if ((res = poll(ufds, NUM_POLL_FDS, -1)) > 0) {
if (ufds[UMSD].revents & POLLIN) {
/* New client connection */
if ((ufds[CCSD].fd = bme_handle_new_client(umsfd)) == -1) {
bme_log(LOG_INFO, "can't accept client\n");
}
}
if ((ufds[CCSD].revents & POLLIN) && (ufds[CCSD].fd != -1)) {
/* External message */
if (bme_extmsg_handler(ufds[CCSD].fd) < 0) {
/* The connection was closed */
close(ufds[CCSD].fd);
ufds[CCSD].fd = -1;
}
}
} else {
/*
* poll() may be unblocked when asynchronous notification (signal)
* is received. That's a very normal situation.
*/
if (errno != EINTR)
bme_log(LOG_WARNING, "poll() returned %d, errno=%d\n", res, errno);
}
}
exit:
res = atexit(bme_shutdown);
if (res != 0) {
bme_log(LOG_NOTICE, "cannot set shutdown function, calling it explicitly\n");
bme_shutdown();
}
bme_log(LOG_INFO, "terminating with exit status %d\n", exit_status);
return exit_status;
}