| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "misc/lc15bts_mgr.h" |
| 21 |
#include "misc/lc15bts_swd.h" |
| 22 |
#include <osmocom/core/logging.h> |
| 23 |
|
| 24 |
|
| 25 |
#include <systemd/sd-daemon.h> |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define SWD_PERIOD 30 |
| 31 |
|
| 32 |
static void swd_start(struct lc15bts_mgr_instance *mgr); |
| 33 |
static void swd_process(struct lc15bts_mgr_instance *mgr); |
| 34 |
static void swd_close(struct lc15bts_mgr_instance *mgr); |
| 35 |
static void swd_state_reset(struct lc15bts_mgr_instance *mgr, int reason); |
| 36 |
static int swd_run(struct lc15bts_mgr_instance *mgr, int from_loop); |
| 37 |
static void swd_loop_run(void *_data); |
| 38 |
|
| 39 |
enum swd_state { |
| 40 |
SWD_INITIAL, |
| 41 |
SWD_IN_PROGRESS, |
| 42 |
}; |
| 43 |
|
| 44 |
enum swd_result { |
| 45 |
SWD_FAIL_START, |
| 46 |
SWD_FAIL_NOTIFY, |
| 47 |
SWD_SUCCESS, |
| 48 |
}; |
| 49 |
|
| 50 |
static void swd_start(struct lc15bts_mgr_instance *mgr) |
| 51 |
{ |
| 52 |
swd_process(mgr); |
| 53 |
} |
| 54 |
|
| 55 |
static void swd_process(struct lc15bts_mgr_instance *mgr) |
| 56 |
{ |
| 57 |
int rc = 0, notify = 0; |
| 58 |
|
| 59 |
|
| 60 |
if (mgr->swd.swd_eventmasks == mgr->swd.swd_events) { |
| 61 |
|
| 62 |
rc = sd_notify(0, "WATCHDOG=1"); |
| 63 |
LOGP(DSWD, LOGL_NOTICE, "Watchdog notification attempt\n"); |
| 64 |
notify = 1; |
| 65 |
} |
| 66 |
else { |
| 67 |
LOGP(DSWD, LOGL_NOTICE, "Missing watchdog events: e:0x%016llx,m:0x%016llx\n",mgr->swd.swd_events,mgr->swd.swd_eventmasks); |
| 68 |
} |
| 69 |
|
| 70 |
if (rc < 0) { |
| 71 |
LOGP(DSWD, LOGL_ERROR, |
| 72 |
"Failed to notify system service watchdog: %d\n", rc); |
| 73 |
swd_state_reset(mgr, SWD_FAIL_NOTIFY); |
| 74 |
return; |
| 75 |
} |
| 76 |
else { |
| 77 |
|
| 78 |
if (notify) { |
| 79 |
mgr->swd.swd_events = 0; |
| 80 |
|
| 81 |
if (mgr->swd.swd_events != 0) |
| 82 |
mgr->swd.swd_events = 0; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
swd_state_reset(mgr, SWD_SUCCESS); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static void swd_close(struct lc15bts_mgr_instance *mgr) |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
static void swd_state_reset(struct lc15bts_mgr_instance *mgr, int outcome) |
| 95 |
{ |
| 96 |
if (mgr->swd.swd_from_loop) { |
| 97 |
mgr->swd.swd_timeout.data = mgr; |
| 98 |
mgr->swd.swd_timeout.cb = swd_loop_run; |
| 99 |
osmo_timer_schedule(&mgr->swd.swd_timeout, SWD_PERIOD, 0); |
| 100 |
} |
| 101 |
|
| 102 |
mgr->swd.state = SWD_INITIAL; |
| 103 |
swd_close(mgr); |
| 104 |
} |
| 105 |
|
| 106 |
static int swd_run(struct lc15bts_mgr_instance *mgr, int from_loop) |
| 107 |
{ |
| 108 |
if (mgr->swd.state != SWD_INITIAL) { |
| 109 |
LOGP(DSWD, LOGL_ERROR, "Swd is already in progress.\n"); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
mgr->swd.swd_from_loop = from_loop; |
| 114 |
|
| 115 |
|
| 116 |
mgr->swd.state = SWD_IN_PROGRESS; |
| 117 |
swd_start(mgr); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void swd_loop_run(void *_data) |
| 122 |
{ |
| 123 |
int rc; |
| 124 |
struct lc15bts_mgr_instance *mgr = _data; |
| 125 |
|
| 126 |
LOGP(DSWD, LOGL_NOTICE, "Going to check for watchdog notification.\n"); |
| 127 |
rc = swd_run(mgr, 1); |
| 128 |
if (rc != 0) { |
| 129 |
swd_state_reset(mgr, SWD_FAIL_START); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
int lc15bts_swd_init(struct lc15bts_mgr_instance *mgr, int swd_num_events) |
| 138 |
{ |
| 139 |
|
| 140 |
if (swd_num_events < 1 || swd_num_events > 64) |
| 141 |
return(-1); |
| 142 |
|
| 143 |
mgr->swd.state = SWD_INITIAL; |
| 144 |
mgr->swd.swd_timeout.data = mgr; |
| 145 |
mgr->swd.swd_timeout.cb = swd_loop_run; |
| 146 |
osmo_timer_schedule(&mgr->swd.swd_timeout, 0, 0); |
| 147 |
|
| 148 |
if (swd_num_events == 64){ |
| 149 |
mgr->swd.swd_eventmasks = 0xffffffffffffffffULL; |
| 150 |
} |
| 151 |
else { |
| 152 |
mgr->swd.swd_eventmasks = ((1ULL << swd_num_events) - 1); |
| 153 |
} |
| 154 |
mgr->swd.swd_events = 0; |
| 155 |
mgr->swd.num_events = swd_num_events; |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
int lc15bts_swd_event(struct lc15bts_mgr_instance *mgr, enum mgr_swd_events swd_event) |
| 167 |
{ |
| 168 |
|
| 169 |
if ((int)(swd_event) < 0 || (int)(swd_event) >= mgr->swd.num_events) |
| 170 |
return(-1); |
| 171 |
|
| 172 |
mgr->swd.swd_events = mgr->swd.swd_events | ((unsigned long long int)(1) << (int)(swd_event)); |
| 173 |
|
| 174 |
|
| 175 |
LOGP(DSWD, LOGL_DEBUG,"Swd event notified: %d\n", (int)(swd_event)); |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |