example: consolidate common initialization and cleanup code
Signed-off-by: Patrick McHardy <kaber@trash.net>
This commit is contained in:
parent
10bc9f41b6
commit
b870069f4c
|
@ -1,7 +1,7 @@
|
|||
LDFLAGS += -Wl,-rpath $(PWD)/src -Lsrc -ldect -levent
|
||||
PROGRAMS += cc ss mm-fp mm-pp discover hijack
|
||||
|
||||
common-obj += event_ops.o dummy_ops.o
|
||||
common-obj += common.o event_ops.o dummy_ops.o
|
||||
|
||||
cc-destdir := usr/bin
|
||||
cc-obj += $(common-obj)
|
||||
|
|
16
example/cc.c
16
example/cc.c
|
@ -263,18 +263,7 @@ static struct dect_ops ops = {
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
exit(1);
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
exit(1);
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
exit(1);
|
||||
|
||||
dect_common_init(&ops);
|
||||
#if 0
|
||||
//dect_lce_group_ring(dh, 0xf);
|
||||
dect_open_call(dh, &ipuis[PHONE1]);
|
||||
|
@ -283,7 +272,6 @@ int main(int argc, char **argv)
|
|||
dect_open_call(dh, &ipuis[PHONE2]);
|
||||
#endif
|
||||
dect_event_loop();
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
dect_common_cleanup(dh);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <dect/libdect.h>
|
||||
#include "common.h"
|
||||
|
||||
void pexit(const char *str)
|
||||
{
|
||||
perror(str);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void dect_common_init(struct dect_ops *ops)
|
||||
{
|
||||
dummy_ops_init(ops);
|
||||
|
||||
if (dect_event_ops_init(ops))
|
||||
pexit("dect_event_ops_init");
|
||||
|
||||
dh = dect_alloc_handle(ops);
|
||||
if (dh == NULL)
|
||||
pexit("dect_alloc_handle");
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
pexit("dect_init");
|
||||
}
|
||||
|
||||
void dect_common_cleanup(struct dect_handle *dh)
|
||||
{
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
}
|
|
@ -6,13 +6,18 @@
|
|||
#include <list.h>
|
||||
|
||||
extern struct dect_handle *dh;
|
||||
|
||||
extern int dect_event_ops_init(struct dect_ops *ops);
|
||||
extern void dect_event_loop(void);
|
||||
extern void dect_event_loop_stop(void);
|
||||
extern void dect_event_ops_cleanup(void);
|
||||
|
||||
extern void dummy_ops_init(struct dect_ops *ops);
|
||||
|
||||
extern void dect_common_init(struct dect_ops *ops);
|
||||
extern void dect_common_cleanup(struct dect_handle *dh);
|
||||
|
||||
extern void pexit(const char *str);
|
||||
|
||||
#include "../src/ccitt-adpcm/g72x.h"
|
||||
|
||||
struct dect_audio_handle {
|
||||
|
|
|
@ -9,22 +9,11 @@ static struct dect_ops ops;
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
exit(1);
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
exit(1);
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
exit(1);
|
||||
dect_common_init(&ops);
|
||||
|
||||
dect_lce_group_ring(dh, 0);
|
||||
dect_event_loop();
|
||||
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
dect_common_cleanup(dh);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,12 +14,6 @@
|
|||
|
||||
static struct dect_msg_buf msg = { .data = msg.head };
|
||||
|
||||
static void pexit(const char *str)
|
||||
{
|
||||
perror(str);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void mm_locate_ind(struct dect_handle *dh,
|
||||
struct dect_mm_endpoint *mme,
|
||||
struct dect_mm_locate_param *param)
|
||||
|
@ -112,17 +106,7 @@ int main(int argc, char **argv)
|
|||
slot = atoi(argv[1]);
|
||||
dect_build_msg(&msg, atoi(argv[2]), atoi(argv[3]));
|
||||
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
pexit("dect_event_ops_init");
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
pexit("dect_alloc_handle");
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
pexit("dect_init");
|
||||
dect_common_init(&ops);
|
||||
|
||||
dfd = dect_raw_socket(dh);
|
||||
if (dfd == NULL)
|
||||
|
@ -144,7 +128,6 @@ int main(int argc, char **argv)
|
|||
dect_unregister_fd(dh, dfd);
|
||||
dect_close(dh, dfd);
|
||||
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
dect_common_cleanup(dh);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -218,21 +218,12 @@ int main(int argc, char **argv)
|
|||
if (rand_fd < 0)
|
||||
exit(1);
|
||||
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
exit(1);
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
exit(1);
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
exit(1);
|
||||
dect_common_init(&ops);
|
||||
|
||||
dect_event_loop();
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
|
||||
dect_common_cleanup(dh);
|
||||
close(rand_fd);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -337,17 +337,7 @@ int main(int argc, char **argv)
|
|||
if (rand_fd < 0)
|
||||
exit(1);
|
||||
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
exit(1);
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
exit(1);
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
exit(1);
|
||||
dect_common_init(&ops);
|
||||
|
||||
mme = dect_mm_endpoint_alloc(dh, &ipui);
|
||||
if (mme == NULL)
|
||||
|
@ -372,8 +362,7 @@ int main(int argc, char **argv)
|
|||
mm_detach_req(dh, mme);
|
||||
}
|
||||
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
dect_common_cleanup(dh);
|
||||
close(rand_fd);
|
||||
return 0;
|
||||
}
|
||||
|
|
17
example/ss.c
17
example/ss.c
|
@ -64,22 +64,11 @@ static struct dect_ops ops = {
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
dummy_ops_init(&ops);
|
||||
|
||||
if (dect_event_ops_init(&ops) < 0)
|
||||
exit(1);
|
||||
|
||||
dh = dect_alloc_handle(&ops);
|
||||
if (dh == NULL)
|
||||
exit(1);
|
||||
|
||||
if (dect_init(dh) < 0)
|
||||
exit(1);
|
||||
dect_common_init(&ops);
|
||||
|
||||
dect_invoke_ss(dh, &ipui);
|
||||
|
||||
dect_event_loop();
|
||||
dect_close_handle(dh);
|
||||
dect_event_ops_cleanup();
|
||||
|
||||
dect_common_cleanup(dh);
|
||||
return 0;
|
||||
}
|
||||
|
|
Reference in New Issue