HomePort
daemon_api.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "hpd/hpd_api.h"
29 #include "daemon.h"
30 #include "log.h"
31 // Not the same as other queue.h
32 
34  if (!hpd) LOG_RETURN_E_NULL();
35  return daemon_alloc(hpd);
36 }
37 
39 {
40  if (!hpd) LOG_RETURN_E_NULL();
41  return daemon_free(hpd);
42 }
43 
45 {
46  if (!hpd || !loop) LOG_RETURN_E_NULL();
47  return daemon_get_loop(hpd, loop);
48 }
49 
50 hpd_error_t hpd_module(hpd_t *hpd, const char *id, const hpd_module_def_t *module_def)
51 {
52  hpd_module_t *module;
53  if (!hpd || !id || !module_def) LOG_RETURN_E_NULL();
54  if (hpd->configuration) LOG_RETURN(HPD_E_STATE, "Cannot add module while hpd is running.");
55  if (strchr(id, '-')) LOG_RETURN(HPD_E_ARGUMENT, "Module ids may not contain '-'.");
56  // TODO Create hpd and log as modules ?
57  if (strcmp(id, "hpd") == 0) LOG_RETURN(HPD_E_ARGUMENT, "Module ids cannot be 'hpd'.");
58  if (strcmp(id, "log") == 0) LOG_RETURN(HPD_E_ARGUMENT, "Module ids cannot be 'log'.");
59  TAILQ_FOREACH(module, &hpd->modules, HPD_TAILQ_FIELD)
60  if (strcmp(module->id, id) == 0)
61  LOG_RETURN(HPD_E_NOT_UNIQUE, "Module ids must be unique.");
62  return daemon_add_module(hpd, id, module_def);
63 }
64 
65 hpd_error_t hpd_module_add_option(const hpd_module_t *context, const char *name, const char *arg, int flags,
66  const char *doc)
67 {
68  if (!context || !name) LOG_RETURN_E_NULL();
69  hpd_t *hpd = context->hpd;
70  if (!hpd->options) LOG_RETURN(HPD_E_STATE, "Can only add options during on_create().");
71  size_t name_index = strlen(context->id)+1;
72  for (int i = 0; i < hpd->module_options_count; i++) {
73  if (name_index < strlen(hpd->options[i].name) && strcmp(&hpd->options[i].name[name_index], name) == 0)
74  LOG_RETURN(HPD_E_NOT_UNIQUE, "Option names must be unique within the module.");
75  }
76  return daemon_add_option(context, name, arg, flags, doc);
77 }
78 
79 hpd_error_t hpd_module_get_id(const hpd_module_t *context, const char **id)
80 {
81  if (!context || !id) LOG_RETURN_E_NULL();
82  return daemon_get_id(context, id);
83 }
84 
85 hpd_error_t hpd_start(hpd_t *hpd, int argc, char *argv[])
86 {
87  if (!hpd) LOG_RETURN_E_NULL();
88  return daemon_start(hpd, argc, argv);
89 }
90 
92 {
93  if (!hpd) LOG_RETURN_E_NULL();
94  if (!hpd->loop) LOG_RETURN_HPD_STOPPED();
95  return daemon_stop(hpd);
96 }
#define LOG_RETURN_E_NULL()
Definition: log.h:50
hpd_ev_loop_t * loop
Definition: daemon.h:51
hpd_modules_t modules
Definition: daemon.h:55
hpd_error_t hpd_start(hpd_t *hpd, int argc, char *argv[])
Definition: daemon_api.c:85
char * id
Definition: daemon.h:93
hpd_error_t daemon_stop(const hpd_t *hpd)
Definition: daemon.c:492
hpd_error_t hpd_get_loop(hpd_t *hpd, hpd_ev_loop_t **loop)
Definition: daemon_api.c:44
#define LOG_RETURN_HPD_STOPPED()
Definition: log.h:53
hpd_error_t hpd_alloc(hpd_t **hpd)
[hpd_t functions]
Definition: daemon_api.c:33
hpd_error_t daemon_get_loop(const hpd_t *hpd, hpd_ev_loop_t **loop)
Definition: daemon.c:504
hpd_error_t hpd_module_get_id(const hpd_module_t *context, const char **id)
Definition: daemon_api.c:79
hpd_error_t daemon_get_id(const hpd_module_t *context, const char **id)
Definition: daemon.c:498
#define HPD_TAILQ_FIELD
Definition: hpd_queue.h:37
hpd_argp_option_t * options
Definition: daemon.h:58
hpd_error_t daemon_start(hpd_t *hpd, int argc, char *argv[])
Definition: daemon.c:446
hpd_configuration_t * configuration
Definition: daemon.h:52
hpd_error_t daemon_free(hpd_t *hpd)
Definition: daemon.c:382
hpd_error_t daemon_add_option(const hpd_module_t *context, const char *name, const char *arg, int flags, const char *doc)
Definition: daemon.c:409
hpd_error_t daemon_add_module(hpd_t *hpd, const char *id, const hpd_module_def_t *module_def)
Definition: daemon.c:394
enum hpd_error hpd_error_t
Definition: hpd_types.h:167
[Application API Callbacks]
Definition: hpd_types.h:200
hpd_error_t hpd_stop(hpd_t *hpd)
Definition: daemon_api.c:91
#define LOG_RETURN(E, FMT,...)
Definition: log.h:48
Definition: daemon.h:50
hpd_error_t hpd_free(hpd_t *hpd)
Definition: daemon_api.c:38
static struct ev_loop * loop
hpd_t * hpd
Definition: daemon.h:90
hpd_error_t hpd_module_add_option(const hpd_module_t *context, const char *name, const char *arg, int flags, const char *doc)
[hpd_t functions]
Definition: daemon_api.c:65
hpd_error_t hpd_module(hpd_t *hpd, const char *id, const hpd_module_def_t *module_def)
Definition: daemon_api.c:50
struct ev_loop hpd_ev_loop_t
Definition: hpd_types.h:51
hpd_error_t daemon_alloc(hpd_t **hpd)
Definition: daemon.c:359
int module_options_count
Definition: daemon.h:56