Event mgmt pt II

This commit is contained in:
emanuel 2020-10-16 19:22:32 +01:00
parent 817bacca16
commit 0518f44b9f
4 changed files with 102 additions and 26 deletions

View File

@ -204,7 +204,7 @@ void *ca_service(void *fc) {
syslog_debug("[facilities] -> [transport] BDR (%ldB)", enc.encoded); syslog_debug("[facilities] -> [transport] BDR (%ldB)", enc.encoded);
zmq_send(facilities->transport_socket, bdr_oer, enc.encoded, 0); zmq_send(facilities->transport_socket, bdr_oer, enc.encoded, 0);
zmq_recv(facilities->transport_socket, &code, 1, 0); zmq_recv(facilities->transport_socket, &code, 1, 0);
sleep(1); sleep(1000);
} }
ASN_STRUCT_FREE(asn_DEF_BTPDataRequest, bdr); ASN_STRUCT_FREE(asn_DEF_BTPDataRequest, bdr);

View File

@ -14,10 +14,9 @@
#define syslog_debug(msg, ...) #define syslog_debug(msg, ...)
#endif #endif
enum EVENT_CHECK_RESULT event_check(den_t *den, DENM_t *denm) { static enum EVENT_CHECK_RESULT event_check(den_t *den, DENM_t *denm) {
int rv = 0; int rv = 0;
uint64_t e_detection_time, e_reference_time; uint64_t e_detection_time, e_reference_time;
asn_INTEGER2ulong((INTEGER_t*) &denm->denm.management.detectionTime, &e_detection_time); asn_INTEGER2ulong((INTEGER_t*) &denm->denm.management.detectionTime, &e_detection_time);
asn_INTEGER2ulong((INTEGER_t*) &denm->denm.management.referenceTime, &e_reference_time); asn_INTEGER2ulong((INTEGER_t*) &denm->denm.management.referenceTime, &e_reference_time);
@ -102,7 +101,7 @@ enum EVENT_CHECK_RESULT event_check(den_t *den, DENM_t *denm) {
return EVENT_NEW; return EVENT_NEW;
} }
int event_add(den_t *den, DENM_t *denm) { static int event_add(den_t *den, DENM_t *denm) {
struct timespec systemtime; struct timespec systemtime;
clock_gettime(CLOCK_REALTIME, &systemtime); clock_gettime(CLOCK_REALTIME, &systemtime);
@ -165,7 +164,7 @@ int event_add(den_t *den, DENM_t *denm) {
else return 0; // Event added to db else return 0; // Event added to db
} }
int event_update(den_t *den, DENM_t *denm) { static int event_update(den_t *den, DENM_t *denm) {
struct timespec systemtime; struct timespec systemtime;
clock_gettime(CLOCK_REALTIME, &systemtime); clock_gettime(CLOCK_REALTIME, &systemtime);
@ -222,6 +221,63 @@ int event_update(den_t *den, DENM_t *denm) {
else return 0; // Event updated else return 0; // Event updated
} }
void event_manage(den_t *den, DENM_t *denm) {
int rv;
switch (event_check(den, denm)) {
case EVENT_NEW:
rv = event_add(den, denm);
syslog_debug("[facilities] [den] new event received");
if (rv) {
syslog_debug("[facilities] [den] failed adding event, max events reached");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
rv = 1;
}
break;
case EVENT_INVALID:
syslog_debug("[facilities] [den] invalid event received, ignoring");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
break;
case EVENT_PASSED:
syslog_debug("[facilities] [den] old event received, ignoring");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
break;
case EVENT_CANCELLATION:
syslog_debug("[facilities] [den] event cancellation received");
rv = event_update(den, denm);
if (rv) {
syslog_debug("[facilities] [den] failed cancelling event, event not found");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
rv = 1;
}
break;
case EVENT_NEGATION:
syslog_debug("[facilities] [den] event negation received");
rv = event_update(den, denm);
if (rv) {
syslog_debug("[facilities] [den] failed negating event, event not found");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
rv = 1;
}
break;
case EVENT_UPDATE:
syslog_debug("[facilities] [den] event update received");
rv = event_update(den, denm);
if (rv) {
syslog_debug("[facilities] [den] failed updating event, event not found");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
}
break;
case EVENT_REPEATED:
syslog_debug("[facilities] [den] repeated event received, ignoring");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
break;
case EVENT_NUMBER_EXCEEDED:
syslog_debug("[facilities] [den] max events reached, ignoring");
ASN_STRUCT_FREE(asn_DEF_DENM, denm);
break;
}
}
void* den_service(void *fc) { void* den_service(void *fc) {
facilities_t *facilities = (facilities_t *) fc; facilities_t *facilities = (facilities_t *) fc;

View File

@ -30,7 +30,7 @@ typedef struct event {
typedef struct den { typedef struct den {
event_t ** events; event_t ** events;
uint32_t sn; uint32_t sn;
uint16_t no_active_events; uint16_t no_stored_events;
uint16_t no_max_events; uint16_t no_max_events;
pthread_mutex_t lock; pthread_mutex_t lock;
} den_t; } den_t;
@ -46,7 +46,14 @@ enum EVENT_CHECK_RESULT {
EVENT_NUMBER_EXCEEDED EVENT_NUMBER_EXCEEDED
}; };
enum EVENT_CHECK_RESULT check_event(den_t *den, DENM_t *denm); /**
* Evaluate a DENM event.
* Does all the checks to a DENM and adds it to the database. *Don't* free the DENM struct after calling this function
* @param den the DEN service struct
* @param denm the DENM to evaluate
* @return none
*/
void event_manage(den_t *den, DENM_t *denm);
void* den_service(void *fc); void* den_service(void *fc);

View File

@ -46,33 +46,46 @@ static int transport_indication(facilities_t *facilities, uint8_t *msg, uint32_t
syslog_debug("[facilities] <- BDI (%ldB)", bdi->data.size); syslog_debug("[facilities] <- BDI (%ldB)", bdi->data.size);
// Parse message // Parse message
asn_TYPE_descriptor_t *msg_descriptor; asn_TYPE_descriptor_t *its_msg_descriptor;
void *msg_struct; void *its_msg;
switch (bdi->destinationPort) { switch (bdi->destinationPort) {
case Port_cam: case Port_cam:
msg_descriptor = &asn_DEF_CAM; its_msg_descriptor = &asn_DEF_CAM;
msg_struct = calloc(1, sizeof(CAM_t)); its_msg = calloc(1, sizeof(CAM_t));
break; break;
case Port_denm: case Port_denm:
msg_descriptor = &asn_DEF_DENM; its_msg_descriptor = &asn_DEF_DENM;
msg_struct = calloc(1, sizeof(DENM_t)); its_msg = calloc(1, sizeof(DENM_t));
break; break;
case Port_ivim: case Port_ivim:
msg_descriptor = &asn_DEF_IVIM; its_msg_descriptor = &asn_DEF_IVIM;
msg_struct = calloc(1, sizeof(IVIM_t)); its_msg = calloc(1, sizeof(IVIM_t));
break; break;
default: default:
syslog_debug("[facilities] messsage with unhandled BTP port received, ignoring"); syslog_debug("[facilities] messsage with unhandled BTP port received, ignoring");
goto cleanup; goto cleanup;
} }
dec = uper_decode_complete(NULL, msg_descriptor, (void**) msg_struct, bdi->data.buf, bdi->data.size); dec = uper_decode_complete(NULL, its_msg_descriptor, (void**) &its_msg, bdi->data.buf, bdi->data.size);
if (dec.code) { if (dec.code) {
syslog_debug("[facilities] invalid %s received", msg_descriptor->name); syslog_debug("[facilities] invalid %s received", its_msg_descriptor->name);
rv = 1; rv = 1;
goto cleanup; goto cleanup;
} }
// Manage message
if (bdi->destinationPort == Port_denm) {
#ifdef DEBUG
uint8_t* xml_denm = malloc(4096);
asn_enc_rval_t rve = xer_encode_to_buffer(xml_denm, 4096, 0x02, &asn_DEF_DENM, its_msg);
syslog_debug("DENM XER %d: %.*s", (int)rve.encoded, (int)rve.encoded , xml_denm);
free(xml_denm);
#endif
event_manage(facilities->den, its_msg);
}
// Forward to app // Forward to app
fdi = calloc(1, sizeof(FacilitiesDataIndication_t)); fdi = calloc(1, sizeof(FacilitiesDataIndication_t));
@ -90,9 +103,9 @@ static int transport_indication(facilities_t *facilities, uint8_t *msg, uint32_t
zmq_recv(facilities->app_socket, &code, 1, 0); zmq_recv(facilities->app_socket, &code, 1, 0);
cleanup: cleanup:
if (bdi->destinationPort != Port_denm) ASN_STRUCT_FREE(*its_msg_descriptor, its_msg);
ASN_STRUCT_FREE(asn_DEF_BTPDataIndication, bdi); ASN_STRUCT_FREE(asn_DEF_BTPDataIndication, bdi);
ASN_STRUCT_FREE(asn_DEF_FacilitiesDataIndication, fdi); ASN_STRUCT_FREE(asn_DEF_FacilitiesDataIndication, fdi);
ASN_STRUCT_FREE(*msg_descriptor, msg_struct);
return rv; return rv;
} }