sgwdynspeed: add plugin
[olsrd.git] / lib / sgwdynspeed / src / sgwDynSpeed.c
1 #include "sgwDynSpeed.h"
2
3 /* Plugin includes */
4 #include "speedFile.h"
5 #include "configuration.h"
6
7 /* OLSRD includes */
8 #include "olsr.h"
9 #include "olsr_cookie.h"
10 #include "scheduler.h"
11
12 /* System includes */
13
14 /**
15  Report a plugin error.
16
17  @param useErrno
18  when true then errno is used in the error message; the error reason is also
19  reported.
20  @param format
21  a pointer to the format string
22  @param ...
23  arguments to the format string
24  */
25 void sgwDynSpeedError(bool useErrno, const char *format, ...) {
26         char strDesc[256];
27         char *stringErr = NULL;
28
29         if (useErrno) {
30                 stringErr = strerror(errno);
31         }
32
33         if ((format == NULL) || (*format == '\0')) {
34                 if (useErrno) {
35                         olsr_printf(0, "%s: %s\n", SGWDYNSPEED_PLUGIN_ABBR, stringErr);
36                 } else {
37                         olsr_printf(0, "%s: Unknown error\n", SGWDYNSPEED_PLUGIN_ABBR);
38                 }
39         } else {
40                 va_list arglist;
41
42                 va_start(arglist, format);
43                 vsnprintf(strDesc, sizeof(strDesc), format, arglist);
44                 va_end(arglist);
45
46                 strDesc[sizeof(strDesc) - 1] = '\0'; /* Ensures null termination */
47
48                 if (useErrno) {
49                         olsr_printf(0, "%s: %s: %s\n", SGWDYNSPEED_PLUGIN_ABBR, strDesc, stringErr);
50                 } else {
51                         olsr_printf(0, "%s: %s\n", SGWDYNSPEED_PLUGIN_ABBR, strDesc);
52                 }
53         }
54 }
55
56 /**
57  * Timer callback that reads the smart gateway speed file
58  */
59 static void smartgw_read_speed_file(void *context __attribute__ ((unused))) {
60         readSpeedFile(getSpeedFile());
61         return;
62 }
63
64 /** The timer cookie, used to trace back the originator in debug */
65 static struct olsr_cookie_info *smartgw_speed_file_timer_cookie = NULL;
66
67 /** The timer */
68 static struct timer_entry * smartgw_speed_file_timer = NULL;
69
70 /**
71  Initialise the plugin: check the configuration, initialise the NMEA parser,
72  create network interface sockets, hookup the plugin to OLSR and setup data
73  that can be setup in advance.
74
75  @return
76  - false upon failure
77  - true otherwise
78  */bool initSgwDynSpeed(void) {
79         char * speedFile;
80
81         if (!startSpeedFile()) {
82                 return false;
83         }
84
85         speedFile = getSpeedFile();
86         if (speedFile) {
87                 readSpeedFile(speedFile);
88
89                 if (smartgw_speed_file_timer_cookie == NULL) {
90                         smartgw_speed_file_timer_cookie = olsr_alloc_cookie("smartgw speed file", OLSR_COOKIE_TYPE_TIMER);
91                         if (smartgw_speed_file_timer_cookie == NULL) {
92                                 sgwDynSpeedError(false, "Could not allocate smart gateway speed file cookie");
93                                 return false;
94                         }
95                 }
96                 if (smartgw_speed_file_timer == NULL) {
97                         smartgw_speed_file_timer = olsr_start_timer(getSpeedFilePeriod(), 0, OLSR_TIMER_PERIODIC,
98                                         &smartgw_read_speed_file, NULL, smartgw_speed_file_timer_cookie);
99                         if (smartgw_speed_file_timer == NULL) {
100                                 sgwDynSpeedError(false, "Could not start smart gateway speed file timer");
101                                 return false;
102                         }
103                 }
104         }
105
106         return true;
107 }
108
109  /**
110   * stop the plugin, free resources
111   */
112 void stopSgwDynSpeed(void) {
113         if (smartgw_speed_file_timer != NULL) {
114                 olsr_stop_timer(smartgw_speed_file_timer);
115                 smartgw_speed_file_timer = NULL;
116         }
117         if (smartgw_speed_file_timer_cookie != NULL) {
118                 olsr_free_cookie(smartgw_speed_file_timer_cookie);
119                 smartgw_speed_file_timer_cookie = NULL;
120         }
121         stopSpeedFile();
122 }