summaryrefslogtreecommitdiff
path: root/mcast/client/api_server.c
blob: c3d7617ca2f00f7b805e6742745aeb7ffbf79aa8 (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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * (c) BayCom GmbH, http://www.baycom.de, info@baycom.de
 *
 * See the COPYING file for copyright information and
 * how to reach the author.
 *
 */

#include "headers.h"

#if defined(API_SOCK) || defined(API_SHM) || defined (API_WIN)

static int process_cmd (api_cmd_t * api_cmd, tra_info_t * trl, netceiver_info_list_t * nci)
{
	if(api_cmd->magic != MCLI_MAGIC || api_cmd->version != MCLI_VERSION) {
	        api_cmd->magic = MCLI_MAGIC;
	        api_cmd->version = MCLI_VERSION;
	        api_cmd->state = API_RESPONSE;
	        return 0;
	}
	if (api_cmd->state != API_REQUEST) {
		return 0;
	}

	switch (api_cmd->cmd) {
	case API_GET_NC_NUM:
		api_cmd->parm[API_PARM_NC_NUM] = nci->nci_num;
		api_cmd->state = API_RESPONSE;
		break;

	case API_GET_NC_INFO:
		if (api_cmd->parm[API_PARM_NC_NUM] < nci->nci_num) {
			api_cmd->u.nc_info = nci->nci[api_cmd->parm[API_PARM_NC_NUM]];
			api_cmd->state = API_RESPONSE;
		} else {
			api_cmd->state = API_ERROR;
		}
		break;
	case API_GET_TUNER_INFO:
		if (api_cmd->parm[API_PARM_NC_NUM] < nci->nci_num) {
			if (api_cmd->parm[API_PARM_TUNER_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].tuner_num) {
				api_cmd->u.tuner_info = nci->nci[api_cmd->parm[API_PARM_NC_NUM]].tuner[api_cmd->parm[API_PARM_TUNER_NUM]];
				api_cmd->state = API_RESPONSE;
			} else {
				api_cmd->state = API_ERROR;
			}
		} else {
			api_cmd->state = API_ERROR;
		}
		break;
	case API_GET_SAT_LIST_INFO:
		if (api_cmd->parm[API_PARM_NC_NUM] < nci->nci_num) {
			if (api_cmd->parm[API_PARM_SAT_LIST_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list_num) {
				api_cmd->u.sat_list = nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]];
				api_cmd->state = API_RESPONSE;
			} else {
				api_cmd->state = API_ERROR;
			}
		} else {
			api_cmd->state = API_ERROR;
		}
		break;
	case API_GET_SAT_INFO:
		if (api_cmd->parm[API_PARM_NC_NUM] < nci->nci_num) {
			if (api_cmd->parm[API_PARM_SAT_LIST_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list_num) {
				if (api_cmd->parm[API_PARM_SAT_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]].sat_num) {
					api_cmd->u.sat_info = nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]].sat[api_cmd->parm[API_PARM_SAT_NUM]];
					api_cmd->state = API_RESPONSE;
				} else {
					api_cmd->state = API_ERROR;
				}
			} else {
				api_cmd->state = API_ERROR;
			}
		} else {
			api_cmd->state = API_ERROR;
		}
		break;

	case API_GET_SAT_COMP_INFO:
		if (api_cmd->parm[API_PARM_NC_NUM] < nci->nci_num) {
			if (api_cmd->parm[API_PARM_SAT_LIST_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list_num) {
				if (api_cmd->parm[API_PARM_SAT_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]].sat_num) {
					if (api_cmd->parm[API_PARM_SAT_COMP_NUM] < nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]].sat[api_cmd->parm[API_PARM_SAT_NUM]].comp_num) {
						api_cmd->u.sat_comp = nci->nci[api_cmd->parm[API_PARM_NC_NUM]].sat_list[api_cmd->parm[API_PARM_SAT_LIST_NUM]].sat[api_cmd->parm[API_PARM_SAT_NUM]].comp[api_cmd->parm[API_PARM_SAT_COMP_NUM]];
						api_cmd->state = API_RESPONSE;
					} else {
						api_cmd->state = API_ERROR;
					}
				} else {
					api_cmd->state = API_ERROR;
				}
			} else {
				api_cmd->state = API_ERROR;
			}
		} else {
			api_cmd->state = API_ERROR;
		}
		break;
	case API_GET_TRA_NUM:
		api_cmd->parm[API_PARM_TRA_NUM] = trl->tra_num;
		api_cmd->state = API_RESPONSE;
		break;
	case API_GET_TRA_INFO:
		if (api_cmd->parm[API_PARM_TRA_NUM] < trl->tra_num) {
			api_cmd->u.tra = trl->tra[api_cmd->parm[API_PARM_TRA_NUM]];
			api_cmd->state = API_RESPONSE;
		} else {
			api_cmd->state = API_ERROR;
		}
		break;
	default:
		api_cmd->state = API_ERROR;
	}
	return 1;
}
#endif
#ifdef API_SOCK
typedef struct
{
	pthread_t thread;
	int fd;
	struct sockaddr_un addr;
	socklen_t len;
	int run;
} sock_t;

static void *sock_cmd_loop (void *p)
{
	sock_t *s = (sock_t *) p;
	api_cmd_t sock_cmd;
	int n;
	netceiver_info_list_t *nc_list=nc_get_list();
	tra_info_t *tra_list=tra_get_list();

	dbg ("new api client connected\n");
	s->run = 1;
	while (s->run){
		n = recv (s->fd, &sock_cmd, sizeof (api_cmd_t), 0);
		if (n == sizeof (api_cmd_t)) {
			nc_lock_list();
			process_cmd (&sock_cmd, tra_list, nc_list);
			nc_unlock_list();
			send (s->fd, &sock_cmd, sizeof (api_cmd_t), 0);
		} else {
  	            sock_cmd.magic = MCLI_MAGIC;
                    sock_cmd.version = MCLI_VERSION;
                    sock_cmd.state = API_RESPONSE;
                    send (s->fd, &sock_cmd, sizeof (api_cmd_t), 0);
                    break;
		}
		pthread_testcancel();
	}

	close (s->fd);
	pthread_detach (s->thread);
	free (s);
	return NULL;
}

static void *sock_cmd_listen_loop (void *p)
{
	sock_t tmp;
	sock_t *s = (sock_t *) p;
	dbg ("sock api listen loop started\n");
	s->run = 1;

	while (s->run) {
		tmp.len = sizeof (struct sockaddr_un);
		tmp.fd = accept (s->fd, (struct sockaddr*)&tmp.addr, &tmp.len);
		if (tmp.fd >= 0) {
			sock_t *as = (sock_t *) malloc (sizeof (sock_t));
			if (as == NULL) {
				err ("Cannot get memory for socket\n");
			}
			*as=tmp;
			as->run = 0;
			pthread_create (&as->thread, NULL, sock_cmd_loop, as);
		} else {
			break;
		}
		pthread_testcancel();
	}
	pthread_detach (s->thread);
	return NULL;
}

static sock_t s;
int api_sock_init (const char *cmd_sock_path)
{
	s.addr.sun_family = AF_UNIX;
	strcpy (s.addr.sun_path, cmd_sock_path);
	s.len = sizeof(struct sockaddr_un); //strlen (cmd_sock_path) + sizeof (s.addr.sun_family);

	if ((s.fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
		warn ("Cannot get socket %d\n", errno);
		return -1;
	}
	unlink (cmd_sock_path);
	if (bind (s.fd, (struct sockaddr*)&s.addr, s.len) < 0) {
		warn ("Cannot bind control socket\n");
		return -1;
	}
	if (chmod(cmd_sock_path, S_IRWXU|S_IRWXG|S_IRWXO)) {
		warn ("Cannot chmod 777 socket %s\n", cmd_sock_path);
	}
	if (listen (s.fd, 5) < 0) {
		warn ("Cannot listen on socket\n");
		return -1;
	}
	return pthread_create (&s.thread, NULL, sock_cmd_listen_loop, &s);
}

void api_sock_exit (void)
{
	//FIXME memory leak on exit in context structres
	s.run=0;
	close(s.fd);
	
	if(pthread_exist(s.thread) && !pthread_cancel (s.thread)) {
		pthread_join (s.thread, NULL);
	}
}
#endif
#ifdef API_SHM
static api_cmd_t *api_cmd = NULL;
static pthread_t api_cmd_loop_thread;

static void *api_cmd_loop (void *p)
{
	netceiver_info_list_t *nc_list=nc_get_list();
	tra_info_t *tra_list=tra_get_list();
	while (1) {
		nc_lock_list();
		process_cmd (api_cmd, tra_list, nc_list);
		nc_unlock_list();
		usleep (1);
		pthread_testcancel();
	}

	return NULL;
}

int api_shm_init (void)
{
	int fd = shm_open (API_SHM_NAMESPACE, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
	if (fd == -1) {
		warn ("Cannot get a shared memory handle\n");
		return -1;
	}
	if (ftruncate (fd, sizeof (api_cmd_t)) == -1) {
		err ("Cannot truncate shared memory\n");
	}
	api_cmd = mmap (NULL, sizeof (api_cmd_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (api_cmd == MAP_FAILED) {
		err ("MMap of shared memory region failed\n");
	}
	close (fd);

	memset (api_cmd, 0, sizeof (api_cmd_t));

	pthread_create (&api_cmd_loop_thread, NULL, api_cmd_loop, NULL);
	return 0;
}

void api_shm_exit (void)
{
	if(pthread_exist(api_cmd_loop_thread) && !pthread_cancel (api_cmd_loop_thread)) {
		pthread_join (api_cmd_loop_thread, NULL);
	}
	shm_unlink (API_SHM_NAMESPACE);
}
#endif
#ifdef API_WIN
 
void *api_cmd_loop(void *lpvParam) 
{ 
	netceiver_info_list_t *nc_list=nc_get_list();
	tra_info_t *tra_list=tra_get_list();
	api_cmd_t sock_cmd;
	DWORD cbBytesRead, cbWritten; 
	BOOL fSuccess; 
	HANDLE hPipe; 
  
	hPipe = (HANDLE) lpvParam; 
 
	while (1) { 
		fSuccess = ReadFile( 
			hPipe,        // handle to pipe 
			&sock_cmd,    // buffer to receive data 
			sizeof(sock_cmd), // size of buffer 
			&cbBytesRead, // number of bytes read 
			NULL);        // not overlapped I/O 

		if (! fSuccess || cbBytesRead == 0) {
			break;
		}

		if (cbBytesRead == sizeof (api_cmd_t)) {
			nc_lock_list();
			process_cmd (&sock_cmd, tra_list, nc_list);
			nc_unlock_list();
			
			fSuccess = WriteFile( 
				hPipe,        // handle to pipe 
				&sock_cmd,      // buffer to write from 
				sizeof(sock_cmd), // number of bytes to write 
				&cbWritten,   // number of bytes written 
				NULL);        // not overlapped I/O 

			if (! fSuccess || sizeof(sock_cmd) != cbWritten) {
				break; 
			}
		} else {
  	            sock_cmd.magic = MCLI_MAGIC;
                    sock_cmd.version = MCLI_VERSION;
                    sock_cmd.state = API_RESPONSE;
			fSuccess = WriteFile( 
				hPipe,        // handle to pipe 
				&sock_cmd,      // buffer to write from 
				sizeof(sock_cmd), // number of bytes to write 
				&cbWritten,   // number of bytes written 
				NULL);        // not overlapped I/O 

			if (! fSuccess || sizeof(sock_cmd) != cbWritten) {
				break; 
			}
			break;
		}
	} 
  
   FlushFileBuffers(hPipe); 
   DisconnectNamedPipe(hPipe); 
   CloseHandle(hPipe); 

   return NULL;
}

#define BUFSIZE 2048
void *api_listen_loop(void *p)
{	
	BOOL fConnected; 
	pthread_t api_cmd_loop_thread; 
	HANDLE hPipe;
	LPTSTR lpszPipename=(LPTSTR)p;
  
	while(1) {
		hPipe = CreateNamedPipe( 
					lpszPipename,             // pipe name 
					PIPE_ACCESS_DUPLEX,       // read/write access 
					PIPE_TYPE_MESSAGE |       // message type pipe 
					PIPE_READMODE_MESSAGE |   // message-read mode 
					PIPE_WAIT,                // blocking mode 
					PIPE_UNLIMITED_INSTANCES, // max. instances  
					BUFSIZE,                  // output buffer size 
					BUFSIZE,                  // input buffer size 
					0,                        // client time-out 
					NULL);                    // default security attribute 

		if (hPipe == INVALID_HANDLE_VALUE) {
			err ("CreatePipe failed"); 
			return NULL;
		}
		pthread_testcancel();
		fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); 
 
		if (fConnected) { 
			if(pthread_create(&api_cmd_loop_thread, NULL, api_cmd_loop, hPipe)) {
				err ("CreateThread failed"); 
				return NULL;
			} else { 
				pthread_detach(api_cmd_loop_thread);
			}
		} 
		else {
			CloseHandle(hPipe); 
		}
	} 
	return NULL; 
} 

pthread_t api_listen_loop_thread;

int api_init (LPTSTR cmd_pipe_path)
{
	return pthread_create (&api_listen_loop_thread, NULL, api_listen_loop, cmd_pipe_path);
}

void api_exit (void)
{
	if(pthread_exist(api_listen_loop_thread) && !pthread_cancel (api_listen_loop_thread)) {
		TerminateThread(pthread_getw32threadhandle_np(api_listen_loop_thread),0);
		pthread_join (api_listen_loop_thread, NULL);
	}
}
 
#endif