it2s-itss-facilities/src/infrastructure.c

589 lines
23 KiB
C

#include "infrastructure.h"
#include "facilities.h"
#include <unistd.h>
#include <it2s-tender/space.h>
#include <it2s-tender/time.h>
#include <it2s-asn/ivim/ISO14823Code.h>
const ivi_diid_ssp_bm_t DIID_SSP_BM_MAP[] = {
{"Vienna Convention Code for road sign", IVI_DIID_ViennaCodeConvention, 0x800000},
{"ISO 14823 traffic sign pictogram (danger/warning)", IVI_DIID_TrafficSignPictogramDangerWarning, 0x400000},
{"ISO 14823 traffic sign pictogram (regulatory)", IVI_DIID_TrafficSignPictogramRegulatory, 0x200000},
{"ISO 14823 traffic sign pictogram (informative)", IVI_DIID_TrafficSignPictogramInformative, 0x100000},
{"ISO 14823 public facilities pictogram", IVI_DIID_ServiceCategoryCodePublicFacilitiesPictogram, 0x080000},
{"ISO 14283 ambient or road conditions pictogram (ambient condition)", IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramAmbientCondition, 0x040000},
{"ISO 14283 ambient or road conditions pictogram (road condition)", IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramRoadCondition, 0x020000},
{"ITIS codes", IVI_DIID_itisCodes, 0x010000},
{"Lane status", IVI_DIID_laneStatus, 0x008000},
{"Road configuration container", IVI_DIID_rcc, 0x004000},
{"Text container", IVI_DIID_tc, 0x002000},
{"Layout container", IVI_DIID_lac, 0x001000},
{"IVI Status (negation)", IVI_DIID_StatusNegation, 0x000800},
};
static int permissions_check(int diid, uint8_t* permissions, uint8_t permissions_len) {
/* IVIM SSP scheme
*
* byte | description
* ---------------------------------
* 0 | SSP version control
* 1-3 | Service Provider ID
* 4-5 | Service-specific parameters
* 6-30 | Reserved
*/
if (permissions_len < 6) {
log_debug("[infrastrucutre] [ivi] permissions length too small");
return 0;
}
uint16_t perms_int = *(((uint16_t*) permissions)+2);
uint32_t perm_val;
for (int i = 0; i < 13; ++i) {
if (diid == DIID_SSP_BM_MAP[i].diid) {
perm_val = DIID_SSP_BM_MAP[i].bitmap_val;
perm_val = (perm_val>>8) | (perm_val<<8);
break;
}
}
if ((perm_val & perms_int) == perm_val) return 1;
else return 0;
}
static enum SERVICE_EVAL_R service_check(enum SERVICE_TYPE type, void* its_msg, uint8_t* ssp, uint16_t ssp_len) {
int rv = 0;
infrastructure_t* infrastructure = &facilities.infrastructure;
uint64_t now = itss_time_get();
switch (type) {
case SERVICE_IVI:
break;
default:
return -1;
}
IVIM_t* ivim = (IVIM_t*) its_msg;
uint64_t timestamp, valid_to, valid_from;
if (!ivim->ivi.mandatory.timeStamp) {
timestamp = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.timeStamp, (unsigned long long*) &timestamp);
}
if (!ivim->ivi.mandatory.validFrom) {
valid_from = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, (unsigned long long*) &valid_from);
}
if (!ivim->ivi.mandatory.validTo) {
valid_to = valid_from + infrastructure->default_service_duration;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo, (unsigned long long*) &valid_to);
}
if (now >= valid_to) {
return SERVICE_PASSED;
}
uint32_t validity_duration = valid_to - valid_from;
// Check permissions
if (ssp) {
if (ivim->ivi.optional) {
for (int i = 0; i < ivim->ivi.optional->list.count; ++i) {
switch (ivim->ivi.optional->list.array[i]->present) {
case IviContainer_PR_giv:
;
GeneralIviContainer_t* giv = &ivim->ivi.optional->list.array[i]->choice.giv;
for (int j = 0; j < giv->list.count; ++j) {
GicPart_t* gicp = giv->list.array[j];
for (int k = 0; k < gicp->roadSignCodes.list.count; ++k) {
struct RSCode__code* code = &gicp->roadSignCodes.list.array[k]->code;
switch (code->present) {
case RSCode__code_PR_viennaConvention:
if (!permissions_check(IVI_DIID_ViennaCodeConvention, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case RSCode__code_PR_iso14823:
switch (code->choice.iso14823.pictogramCode.serviceCategoryCode.present) {
case ISO14823Code__pictogramCode__serviceCategoryCode_PR_trafficSignPictogram:
switch (code->choice.iso14823.pictogramCode.serviceCategoryCode.choice.trafficSignPictogram) {
case ISO14823Code__pictogramCode__serviceCategoryCode__trafficSignPictogram_dangerWarning:
if (!permissions_check(IVI_DIID_TrafficSignPictogramDangerWarning, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case ISO14823Code__pictogramCode__serviceCategoryCode__trafficSignPictogram_regulatory:
if (!permissions_check(IVI_DIID_TrafficSignPictogramRegulatory, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case ISO14823Code__pictogramCode__serviceCategoryCode__trafficSignPictogram_informative:
if (!permissions_check(IVI_DIID_TrafficSignPictogramInformative, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
}
break;
case ISO14823Code__pictogramCode__serviceCategoryCode_PR_publicFacilitiesPictogram:
if (!permissions_check(IVI_DIID_ServiceCategoryCodePublicFacilitiesPictogram, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case ISO14823Code__pictogramCode__serviceCategoryCode_PR_ambientOrRoadConditionPictogram:
switch (code->choice.iso14823.pictogramCode.serviceCategoryCode.choice.ambientOrRoadConditionPictogram) {
case ISO14823Code__pictogramCode__serviceCategoryCode__ambientOrRoadConditionPictogram_ambientCondition:
if (!permissions_check(IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramAmbientCondition, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case ISO14823Code__pictogramCode__serviceCategoryCode__ambientOrRoadConditionPictogram_roadCondition:
if (!permissions_check(IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramRoadCondition, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
}
break;
default:
break;
}
break;
case RSCode__code_PR_itisCodes:
if (!permissions_check(IVI_DIID_itisCodes, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
default:
break;
} // end RSCode switch
} // end roadSIgn codes for
if (gicp->laneStatus) {
if (!permissions_check(IVI_DIID_laneStatus, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
}
} // end giv list for
break;
case IviContainer_PR_rcc:
if (!permissions_check(IVI_DIID_rcc, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case IviContainer_PR_tc:
if (!permissions_check(IVI_DIID_tc, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
case IviContainer_PR_lac:
if (!permissions_check(IVI_DIID_lac, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
break;
default:
break;
} // end optional container switch
} // end optional list for
} // end optional if
if (ivim->ivi.mandatory.iviStatus == IviStatus_negation) {
if (!permissions_check(IVI_DIID_StatusNegation, ssp, ssp_len)) {return SERVICE_BAD_PERMISSIONS;}
}
} // end parameters check
bool is_new = true;
bool is_update = false;
bool exists_vacancy = false;
pthread_mutex_lock(&infrastructure->lock);
for (int i = 0; i < infrastructure->n_max_services; ++i) {
if (infrastructure->services[i]->enabled) {
if (infrastructure->services[i]->ivi_id_number == ivim->ivi.mandatory.iviIdentificationNumber) {
is_new = false;
if (timestamp > infrastructure->services[i]->timestamp) is_update = true;
break;
}
}
}
if (is_new) {
for (int i = 0; i < infrastructure->n_max_services; ++i) {
if (!infrastructure->services[i]->enabled) {
exists_vacancy = true;
break;
}
}
}
pthread_mutex_unlock(&infrastructure->lock);
if (is_update) {
switch (ivim->ivi.mandatory.iviStatus) {
case 0:
break;
case 1:
return SERVICE_UPDATE;
case 2:
return SERVICE_CANCELLATION;
case 3:
return SERVICE_NEGATION;
default:
return SERVICE_INVALID;
}
}
if (!is_new) {
return SERVICE_REPEATED;
}
if (!exists_vacancy) {
return SERVICE_NUMBER_EXCEEDED;
}
return SERVICE_NEW;
}
static int service_add(enum SERVICE_TYPE type, void* its_msg, uint64_t* id) {
infrastructure_t* infrastructure = &facilities.infrastructure;
switch (type) {
case SERVICE_IVI:
break;
default:
return -1;
}
IVIM_t* ivim = (IVIM_t*) its_msg;
uint64_t now = itss_time_get();
uint64_t timestamp, valid_to, valid_from;
if (!ivim->ivi.mandatory.timeStamp) {
timestamp = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.timeStamp, (unsigned long long*) &timestamp);
}
if (!ivim->ivi.mandatory.validFrom) {
valid_from = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, (unsigned long long*) &valid_from);
}
if (!ivim->ivi.mandatory.validTo) {
valid_to = valid_from + infrastructure->default_service_duration;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo, (unsigned long long*) &valid_to);
}
pthread_mutex_lock(&infrastructure->lock);
int index = -1;
for (int i = 0; i < infrastructure->n_max_services; ++i) {
if (!infrastructure->services[i]->enabled) {
index = i;
break;
}
}
uint8_t state;
switch (ivim->ivi.mandatory.iviStatus) {
case 0:
case 1:
state = SERVICE_ACTIVE;
break;
case 2:
state = SERVICE_CANCELLED;
break;
case 3:
state = SERVICE_NEGATED;
break;
}
switch (state) {
case SERVICE_ACTIVE:
++infrastructure->n_active_services;
break;
case EVENT_CANCELLED:
++infrastructure->n_cancelled_services;
break;
case EVENT_NEGATED:
++infrastructure->n_negated_services;
break;
}
if (index != -1) {
infrastructure->services[index]->ivim = ivim;
infrastructure->services[index]->id = *id;
infrastructure->services[index]->enabled = true;
infrastructure->services[index]->state = state;
infrastructure->services[index]->station_id = ivim->header.stationID;
infrastructure->services[index]->valid_from = valid_from;
infrastructure->services[index]->valid_to = valid_to;
infrastructure->services[index]->timestamp = timestamp;
infrastructure->services[index]->ivi_id_number = ivim->ivi.mandatory.iviIdentificationNumber;
}
pthread_mutex_unlock(&infrastructure->lock);
if (index == -1) return 1; // Max services reached
else return 0; // Services added to db
}
static int service_update(enum SERVICE_TYPE type, void* its_msg, uint64_t* id) {
infrastructure_t* infrastructure = &facilities.infrastructure;
switch (type) {
case SERVICE_IVI:
break;
default:
return -1;
}
IVIM_t* ivim = (IVIM_t*) its_msg;
uint64_t now = itss_time_get();
uint64_t timestamp, valid_to, valid_from;
if (!ivim->ivi.mandatory.timeStamp) {
timestamp = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.timeStamp,(unsigned long long*) &timestamp);
}
if (!ivim->ivi.mandatory.validFrom) {
valid_from = now;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, (unsigned long long*) &valid_from);
}
if (!ivim->ivi.mandatory.validTo) {
valid_to = valid_from + infrastructure->default_service_duration;
} else {
asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo,(unsigned long long*) &valid_to);
}
uint8_t state;
switch (ivim->ivi.mandatory.iviStatus) {
case 0:
case 1:
state = SERVICE_ACTIVE;
break;
case 2:
state = SERVICE_CANCELLED;
break;
case 3:
state = SERVICE_NEGATED;
break;
}
int index = -1;
pthread_mutex_lock(&infrastructure->lock);
for (int i = 0; i < infrastructure->n_max_services; ++i) {
if (infrastructure->services[i]->enabled) {
if (infrastructure->services[i]->ivi_id_number == ivim->ivi.mandatory.iviIdentificationNumber) {
index = i;
break;
}
}
}
switch (infrastructure->services[index]->state) {
case SERVICE_ACTIVE:
--infrastructure->n_active_services;
break;
case SERVICE_CANCELLED:
--infrastructure->n_cancelled_services;
break;
case SERVICE_NEGATED:
--infrastructure->n_negated_services;
break;
}
switch (state) {
case SERVICE_ACTIVE:
++infrastructure->n_active_services;
break;
case SERVICE_CANCELLED:
++infrastructure->n_cancelled_services;
break;
case SERVICE_NEGATED:
++infrastructure->n_negated_services;
break;
}
if (index != -1) {
*id = infrastructure->services[index]->id;
infrastructure->services[index]->enabled = true;
infrastructure->services[index]->state = state;
infrastructure->services[index]->station_id = ivim->header.stationID;
infrastructure->services[index]->valid_from = valid_from;
infrastructure->services[index]->valid_to = valid_to;
infrastructure->services[index]->timestamp = timestamp;
if (state == SERVICE_CANCELLED || state == SERVICE_NEGATED) { // Keep terminated events for 10 mins
infrastructure->services[index]->valid_to = now + 600 * 1000;
}
ASN_STRUCT_FREE(asn_DEF_IVIM, infrastructure->services[index]->ivim);
infrastructure->services[index]->ivim = ivim;
}
pthread_mutex_unlock(&infrastructure->lock);
if (index == -1) return 1; // Event not found
else return 0; // Event updated
}
enum SERVICE_EVAL_R service_eval(enum SERVICE_TYPE type, void* its_msg, uint64_t* id, uint8_t* ssp, uint16_t ssp_len) {
int rv = 0;
infrastructure_t* infrastructure = &facilities.infrastructure;
switch (rv = service_check(type, its_msg, ssp, ssp_len)) {
case SERVICE_NEW:
log_debug("[infrastructure] new service received");
if (service_add(type, its_msg, id)) {
log_debug("[infrastructure] failed adding service, max services reached");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
rv = -1;
}
break;
case SERVICE_INVALID:
log_debug("[infrastructure] invalid service received, ignoring");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
break;
case SERVICE_PASSED:
log_debug("[infrastructure] old service received, ignoring");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
break;
case SERVICE_CANCELLATION:
log_debug("[infrastructure] service cancellation received");
if (service_update(type, its_msg, id)) {
log_debug("[infrastructure] failed cancelling service, event not found");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
rv = -1;
}
break;
case SERVICE_NEGATION:
log_debug("[infrastructure] service negation received");
if (service_update(type, its_msg, id)) {
log_debug("[infrastructure] failed negating service, service not found");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
rv = -1;
}
break;
case SERVICE_UPDATE:
log_debug("[infrastructure] service update received");
if (service_update(type, its_msg, id)) {
log_debug("[infrastructure] failed updating service, service not found");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
rv = -1;
}
break;
case SERVICE_REPEATED:
log_debug("[infrastructure] repeated service received or timeStamp doesn't allow an update, ignoring");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
break;
case SERVICE_NUMBER_EXCEEDED:
log_debug("[infrastructure] max services reached, ignoring");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
break;
case SERVICE_BAD_PERMISSIONS:
log_debug("[infrastructure] invalid service permissions, ignoring");
ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg);
break;
}
return rv;
}
int infrastructure_init() {
return 0;
}
void* infrastructure_service() {
struct timespec systemtime;
uint64_t now;
infrastructure_t *infrastructure = &facilities.infrastructure;
pthread_mutex_init(&infrastructure->lock, NULL);
infrastructure->services = malloc(infrastructure->n_max_services * sizeof(void*));
for (int i = 0; i < infrastructure->n_max_services; ++i) {
infrastructure->services[i] = calloc(1, sizeof(service_t));
}
uint32_t sleep4us = 1e6;
uint32_t sleep_count = 0;
while (!facilities.exit) {
now = itss_time_get();
int n_awaiting_services = 0;
pthread_mutex_lock(&infrastructure->lock);
for (int i = 0; i < infrastructure->n_max_services; ++i) {
if (infrastructure->services[i]->enabled) {
if (now > infrastructure->services[i]->valid_to) { // Remove service
log_debug("[infrastructure] removed service %d (expiration)", i);
infrastructure->services[i]->enabled = false;
ASN_STRUCT_FREE(asn_DEF_IVIM, infrastructure->services[i]->ivim);
switch (infrastructure->services[i]->state) {
case SERVICE_ACTIVE:
--infrastructure->n_active_services;
break;
case SERVICE_CANCELLED:
--infrastructure->n_cancelled_services;
break;
case SERVICE_NEGATED:
--infrastructure->n_negated_services;
break;
}
} else {
switch (infrastructure->services[i]->state) {
case SERVICE_ACTIVE:
if (now > infrastructure->services[i]->valid_from) {
log_debug("[infrastructure] service %d expiring in %ld second(s)", i, (infrastructure->services[i]->valid_to - now)/1000);
} else {
log_debug("[infrastructure] service %d starting in %ld seconds with a duration of %ld seconds", i, (infrastructure->services[i]->valid_from - now)/1000, (infrastructure->services[i]->valid_to - infrastructure->services[i]->valid_from)/1000);
++n_awaiting_services;
}
break;
case SERVICE_CANCELLED:
break;
case SERVICE_NEGATED:
break;
}
}
}
}
if (sleep_count * sleep4us > 5e6) { /* Print info every 5 seconds */
log_info("[infrastructure] services :: [ %d active | %d awaiting | %d cancelled | %d negated ]", infrastructure->n_active_services - n_awaiting_services, n_awaiting_services, infrastructure->n_cancelled_services, infrastructure->n_negated_services);
sleep_count = 0;
}
pthread_mutex_unlock(&infrastructure->lock);
++sleep_count;
usleep(sleep4us);
}
return NULL;
}