#include "infrastructure.h" #include "facilities.h" #include #include #define syslog_info(msg, ...) syslog(LOG_INFO, msg, ##__VA_ARGS__) #define syslog_emerg(msg, ...) syslog(LOG_EMERG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #define syslog_err(msg, ...) syslog(LOG_ERR, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #ifndef NDEBUG #define syslog_debug(msg, ...) syslog(LOG_DEBUG, "%s:%d " msg "", __func__, __LINE__, ##__VA_ARGS__) #else #define syslog_debug(msg, ...) #endif 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) { syslog_debug("[facilities] [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; } } if ((perm_val & perms_int) == perm_val) return 1; else return 0; } static enum SERVICE_EVAL_RESULT service_check(infrastructure_t* infrastructure, enum SERVICE_TYPE type, void* its_msg, ServiceSpecificPermissions_t* ssp) { int rv = 0; struct timespec systemtime; clock_gettime(CLOCK_REALTIME, &systemtime); long now = (long) (systemtime.tv_sec * 1000 + systemtime.tv_nsec / 1E6); now = now - 1072915200000; // Convert EPOCH to 2004/01/01 00:00:000 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, ×tamp); } if (!ivim->ivi.mandatory.validFrom) { valid_from = now; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, &valid_from); } if (!ivim->ivi.mandatory.validTo) { valid_to = valid_from + infrastructure->default_service_duration; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo, &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->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} 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 Pictogram_trafficSign_dangerWarning: if (!permissions_check(IVI_DIID_TrafficSignPictogramDangerWarning, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case Pictogram_trafficSign_regulatory: if (!permissions_check(IVI_DIID_TrafficSignPictogramRegulatory, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case Pictogram_trafficSign_informative: if (!permissions_check(IVI_DIID_TrafficSignPictogramInformative, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; } break; case ISO14823Code__pictogramCode__serviceCategoryCode_PR_publicFacilitiesPictogram: if (!permissions_check(IVI_DIID_ServiceCategoryCodePublicFacilitiesPictogram, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case ISO14823Code__pictogramCode__serviceCategoryCode_PR_ambientOrRoadConditionPictogram: switch (code->choice.iso14823.pictogramCode.serviceCategoryCode.choice.ambientOrRoadConditionPictogram) { case Pictogram_conditionsSign_ambientCondition: if (!permissions_check(IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramAmbientCondition, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case Pictogram_conditionsSign_roadCondition: if (!permissions_check(IVI_DIID_ServiceCategoryCodeAmbientOrRoadConditionPictogramRoadCondition, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; } break; default: break; } break; case RSCode__code_PR_itisCodes: if (!permissions_check(IVI_DIID_itisCodes, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; default: break; } // end RSCode switch } // end roadSIgn codes for if (gicp->laneStatus) { if (!permissions_check(IVI_DIID_laneStatus, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} } } // end giv list for break; case IviContainer_PR_rcc: if (!permissions_check(IVI_DIID_rcc, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case IviContainer_PR_tc: if (!permissions_check(IVI_DIID_tc, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} break; case IviContainer_PR_lac: if (!permissions_check(IVI_DIID_lac, ssp->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} 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->choice.bitmapSsp.buf, ssp->choice.bitmapSsp.size)) {return SERVICE_INVALID;} } } // 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(infrastructure_t* infrastructure, enum SERVICE_TYPE type, void* its_msg, int64_t* id) { switch (type) { case SERVICE_IVI: break; default: return -1; } IVIM_t* ivim = (IVIM_t*) its_msg; struct timespec systemtime; clock_gettime(CLOCK_REALTIME, &systemtime); long now = (long) (systemtime.tv_sec * 1000 + systemtime.tv_nsec / 1E6); now = now - 1072915200000; // Convert EPOCH to 2004/01/01 00:00:000 uint64_t timestamp, valid_to, valid_from; if (!ivim->ivi.mandatory.timeStamp) { timestamp = now; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.timeStamp, ×tamp); } if (!ivim->ivi.mandatory.validFrom) { valid_from = now; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, &valid_from); } if (!ivim->ivi.mandatory.validTo) { valid_to = valid_from + infrastructure->default_service_duration; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo, &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]->id = rand(); *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; 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(infrastructure_t* infrastructure, enum SERVICE_TYPE type, void* its_msg, int64_t* id) { switch (type) { case SERVICE_IVI: break; default: return -1; } IVIM_t* ivim = (IVIM_t*) its_msg; struct timespec systemtime; clock_gettime(CLOCK_REALTIME, &systemtime); long now = (long) (systemtime.tv_sec * 1000 + systemtime.tv_nsec / 1E6); now = now - 1072915200000; // Convert EPOCH to 2004/01/01 00:00:000 uint64_t timestamp, valid_to, valid_from; if (!ivim->ivi.mandatory.timeStamp) { timestamp = now; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.timeStamp, ×tamp); } if (!ivim->ivi.mandatory.validFrom) { valid_from = now; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validFrom, &valid_from); } if (!ivim->ivi.mandatory.validTo) { valid_to = valid_from + infrastructure->default_service_duration; } else { asn_INTEGER2ulong((INTEGER_t*) ivim->ivi.mandatory.validTo, &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_RESULT service_eval(infrastructure_t* infrastructure, enum SERVICE_TYPE type, void* its_msg, int64_t* id, ServiceSpecificPermissions_t* ssp) { int rv = 0; switch (rv = service_check(infrastructure, type, its_msg, ssp)) { case SERVICE_NEW: syslog_debug("[facilities] [infrastructure] new service received"); if (service_add(infrastructure, type, its_msg, id)) { syslog_debug("[facilities] [infrastructure] failed adding service, max services reached"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); rv = -1; } break; case SERVICE_INVALID: syslog_debug("[facilities] [infrastructure] invalid service received, ignoring"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); break; case SERVICE_PASSED: syslog_debug("[facilities] [infrastructure] old service received, ignoring"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); break; case SERVICE_CANCELLATION: syslog_debug("[facilities] [infrastructure] service cancellation received"); if (service_update(infrastructure, type, its_msg, id)) { syslog_debug("[facilities] [infrastructure] failed cancelling service, event not found"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); rv = -1; } break; case SERVICE_NEGATION: syslog_debug("[facilities] [infrastructure] service negation received"); if (service_update(infrastructure, type, its_msg, id)) { syslog_debug("[facilities] [infrastructure] failed negating service, service not found"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); rv = -1; } break; case SERVICE_UPDATE: syslog_debug("[facilities] [infrastructure] service update received"); if (service_update(infrastructure, type, its_msg, id)) { syslog_debug("[facilities] [infrastructure] failed updating service, service not found"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); rv = -1; } break; case SERVICE_REPEATED: syslog_debug("[facilities] [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: syslog_debug("[facilities] [infrastructure] max services reached, ignoring"); ASN_STRUCT_FREE(asn_DEF_IVIM, its_msg); break; } return rv; } void* infrastructure_service(void *fc) { facilities_t *facilities = (facilities_t *) fc; 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)); } while (!facilities->exit) { clock_gettime(CLOCK_REALTIME, &systemtime); now = (long)(systemtime.tv_sec * 1000 + systemtime.tv_nsec / 1E6); now = now - 1072915200000; // Convert EPOCH to 2004/01/01 00:00:000 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 syslog_debug("[facilities] [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) { syslog_debug("[facilities] [infrastructure] service %d expiring in %ld second(s)", i, (infrastructure->services[i]->valid_to - now)/1000); } else { syslog_debug("[facilities] [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; } } } } syslog_info("[facilities] [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); pthread_mutex_unlock(&infrastructure->lock); usleep(1000000); } return NULL; }