-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvtunerd.c
195 lines (165 loc) · 5.79 KB
/
vtunerd.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <syslog.h>
#include <linux/dvb/version.h>
#include "vtunerd-service.h"
#include "vtuner-utils.h"
#ifndef BUILDVER
#define BUILDVER 0
#endif
int dbg_level = MSG_INFO;
unsigned int dbg_mask = MSG_ALL;
int use_syslog = 1;
#define DEBUGMAIN(msg, ...) DEBUG(MSG_MAIN, "[%d %s:%u] debug: " msg, getpid(), __FILE__, __LINE__, ## __VA_ARGS__)
#define MAX_SESSIONS 8
typedef struct vtuner_session {
vtuner_session_status_t status;
int tuner_type;
int tuner_group;
int adapter;
int frontend;
int demux;
int dvr;
pthread_t th;
struct sockaddr_in client_so;
} vtuner_session_t;
void *session_worker(void *data) {
vtuner_session_t *session = (vtuner_session_t*)data;
run_worker(session->adapter, session->frontend, session->demux, session->dvr, &session->client_so);
session->status = SST_IDLE;
}
int main(int argc, char **argv) {
int i, c;
vtuner_session_t session[MAX_SESSIONS];
int hw_count = 0;
int tuner_group = VTUNER_GROUPS_ALL;
int opt_err = 0;
unsigned short lport = 0;
char *lip = NULL;
openlog("vtunerd", LOG_PERROR, LOG_USER);
write_message(MSG_MAIN, MSG_ERROR, "vtuner server (vtunerd), part of vtuner project\n"
"Visit http://code.google.com/p/vtuner/ for more information\n"
"Copyright (C) 2009-11 Roland Mieslinger\n"
"This is free software; see the source for copying conditions.\n"
"There is NO warranty; not even for MERCHANTABILITY or FITNESS\n"
"FOR A PARTICULAR PURPOSE.\n");
// read only verbosity, to show value
while((c = getopt(argc, argv, "d:g:hl:p:u:v:")) != -1)
switch(c) {
case 'v': // verbosity
dbg_level = atoi(optarg);
break;
}
write_message(MSG_MAIN, MSG_ERROR, "Revision:%s%s DVB:%d.%d allow:%d.x NetProto:%d MsgSize:%d, Debug:0x%x\n", BUILDVER, MODFLAG, DVB_API_VERSION, DVB_API_VERSION_MINOR, HAVE_DVB_API_VERSION, VTUNER_PROTO_MAX, sizeof(vtuner_net_message_t), dbg_level);
for(i=0; i<MAX_SESSIONS; ++i) session[i].status = SST_IDLE;
// do real option parsing now
optind = 1; // reset getopt()
while((c = getopt(argc, argv, "d:g:hl:p:u:v:")) != -1) {
switch(c) {
case 'd': // device list (adap:fe:dmx:dvr)
{
session[hw_count].frontend = 0;
session[hw_count].demux = 0;
session[hw_count].dvr = 0;
if(sscanf(optarg, "%d:%d:%d:%d", &session[hw_count].adapter, &session[hw_count].frontend, &session[hw_count].demux, &session[hw_count].dvr) < 1) {
WARN(MSG_MAIN, "Device parameter mismatch. At least adapter index is required\n");
opt_err++;
break;
}
DEBUGMAIN("register device: adapter %d, frontend %d, demux %d, dvr %d\n",
session[hw_count].adapter,session[hw_count].frontend,session[hw_count].demux,session[hw_count].dvr);
hw_count++;
}
break;
case 'v': // verbosity
break; // already set
case 'g': // tuner group
sscanf(optarg, "%i", &tuner_group);
break;
case 'l': // listenning ip
lip = optarg;
break;
case 'p': // listenning port
lport = atoi(optarg);
break;
case 'u': // udp log
{
char ip[64];
int pnum;
if(sscanf(optarg, "%[^:]:%d", ip, &pnum) < 2) {
WARN(MSG_MAIN, "Invalid UDP logger parameter: %s\n", optarg);
break;
}
open_udplog(ip, pnum);
DEBUGMAIN("UDP logger to %s:%d opened\n", ip, pnum);
}
break;
case 'h': // help
write_message(MSG_MAIN, MSG_ERROR, "\nCommand line options:\n"
" -d devs_list : adapter[:frontend[:demux[:dvr]]] (default is 0:0:0:0)\n"
" -g group_mask : listen for group members requests only\n"
" -l ip_address : listen on local ip address (default is on ALL)\n"
" -p port_number : listen on local port (default is %d)\n"
" -u ip_address:port_number: send message log to udp://ip_address:port_number\n"
" -v level : verbosity level (1:err,2:warn,3:info,4:debug)\n",
VTUNER_DISCOVER_PORT);
exit(1);
break;
}
}
if(!hw_count && !opt_err) {
session[0].adapter = 0;
session[0].frontend = 0;
session[0].demux = 0;
session[0].dvr = 0;
hw_count = 1;
}
#if DVB_API_VERSION >= 5
INFO(MSG_MAIN, "S2API tuning support.\n");
#endif
udplog_enable(1);
for(i=0;i<hw_count;++i) {
vtuner_hw_t hw;
if(hw_init(&hw, session[i].adapter, session[i].frontend,
session[i].demux, session[i].dvr)) {
session[i].tuner_type = hw.type;
INFO(MSG_MAIN, "adapter:%d, frontend:%d, demux,%d, dvr:%d is type:%d\n",
session[i].adapter, session[i].frontend,
session[i].demux, session[i].dvr, session[i].tuner_type);
} else {
WARN(MSG_MAIN, "Failed to init adapter:%d, frontend:%d, demux,%d, dvr:%d\n",
session[i].adapter, session[i].frontend,
session[i].demux, session[i].dvr);
session[i].tuner_type = 0x00;
}
hw_free(&hw);
}
struct sockaddr_in client_so;
int tuner_type, proto = -1;
if (init_vtuner_service(lip, lport)) {
ERROR(MSG_MAIN, "Failed to init listening port '%s:%d'\n", lip ? : "*.*", lport);
exit(2);
}
while(fetch_request(&client_so, &proto, &tuner_type, &tuner_group)) {
INFO(MSG_MAIN, "received discover request proto%d, vtuner_type:%d group:0x%04X\n", proto, tuner_type, tuner_group);
for(i=0; i<hw_count; ++i) {
DEBUGMAIN("session status:%d session type:%d tuner_type:%d match:%d\n",
session[i].status, session[i].tuner_type, tuner_type,
(session[i].tuner_type & tuner_type) != 0);
if( (session[i].status == SST_IDLE) &&
((session[i].tuner_type & tuner_type) != 0) )
break;
}
if( i < hw_count) {
session[i].status = SST_BUSY;
memcpy((void*)&session[i].client_so, (void*)&client_so, sizeof(client_so));
pthread_create(&session[i].th, NULL, session_worker, (void*)&session[i]);
} else {
INFO(MSG_MAIN, "No idle device found\n");
}
}
}