celix_status_t deviceManager_matchAttachDriver(device_manager_pt manager, driver_loader_pt loader, array_list_pt driverIds, array_list_pt included, array_list_pt excluded, void *service, service_reference_pt reference) { celix_status_t status = CELIX_SUCCESS; array_list_pt references = NULL; int i; for (i = 0; i < arrayList_size(excluded); i++) { void *exclude = arrayList_get(excluded, i); arrayList_removeElement(included, exclude); } for (i = 0; i < arrayList_size(driverIds); i++) { char *id = arrayList_get(driverIds, i); logHelper_log(manager->loghelper, OSGI_LOGSERVICE_INFO, "DEVICE_MANAGER: Driver found: %s", id); } status = driverLoader_loadDrivers(loader, manager->locators, driverIds, &references); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(references); i++) { service_reference_pt reference = arrayList_get(references, i); driver_attributes_pt attributes = hashMap_get(manager->drivers, reference); if (attributes != NULL) { arrayList_add(included, attributes); } } driver_matcher_pt matcher = NULL; status = driverMatcher_create(manager->context, &matcher); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(included); i++) { driver_attributes_pt attributes = arrayList_get(included, i); int match = 0; celix_status_t substatus = driverAttributes_match(attributes, reference, &match); if (substatus == CELIX_SUCCESS) { logHelper_log(manager->loghelper, OSGI_LOGSERVICE_INFO, "DEVICE_MANAGER: Found match: %d", match); if (match <= OSGI_DEVICEACCESS_DEVICE_MATCH_NONE) { continue; } driverMatcher_add(matcher, match, attributes); } else { // Ignore } } match_pt match = NULL; status = driverMatcher_getBestMatch(matcher, reference, &match); if (status == CELIX_SUCCESS) { if (match == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { driver_attributes_pt finalAttributes = hashMap_get(manager->drivers, match->reference); if (finalAttributes == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { char *newDriverId = NULL; status = driverAttributes_attach(finalAttributes, reference, &newDriverId); if (status == CELIX_SUCCESS) { if (newDriverId != NULL) { array_list_pt ids = NULL; arrayList_create(&ids); arrayList_add(ids, newDriverId); arrayList_add(excluded, finalAttributes); status = deviceManager_matchAttachDriver(manager, loader, ids, included, excluded, service, reference); } else { // Attached, unload unused drivers status = driverLoader_unloadDrivers(loader, finalAttributes); } } } } } } driverMatcher_destroy(&matcher); } if (references != NULL) { arrayList_destroy(references); } return status; }
celix_status_t deviceManager_matchAttachDriver(device_manager_pt manager, apr_pool_t *attachPool, driver_loader_pt loader, array_list_pt driverIds, array_list_pt included, array_list_pt excluded, void *service, service_reference_pt reference) { celix_status_t status = CELIX_SUCCESS; array_list_pt references = NULL; int i; for (i = 0; i < arrayList_size(excluded); i++) { void *exclude = arrayList_get(excluded, i); arrayList_removeElement(included, exclude); } for (i = 0; i < arrayList_size(driverIds); i++) { char *id = arrayList_get(driverIds, i); printf("DEVICE_MANAGER: Driver found: %s\n", id); } status = driverLoader_loadDrivers(loader, attachPool, manager->locators, driverIds, &references); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(references); i++) { service_reference_pt reference = arrayList_get(references, i); driver_attributes_pt attributes = hashMap_get(manager->drivers, reference); if (attributes != NULL) { arrayList_add(included, attributes); } } driver_matcher_pt matcher = NULL; status = driverMatcher_create(attachPool, manager->context, &matcher); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(included); i++) { driver_attributes_pt attributes = arrayList_get(included, i); int match = 0; celix_status_t substatus = driverAttributes_match(attributes, reference, &match); if (substatus == CELIX_SUCCESS) { printf("DEVICE_MANAGER: Found match: %d\n", match); if (match <= OSGI_DEVICEACCESS_DEVICE_MATCH_NONE) { continue; } driverMatcher_add(matcher, match, attributes); } else { // Ignore } } match_pt match = NULL; status = driverMatcher_getBestMatch(matcher, attachPool, reference, &match); if (status == CELIX_SUCCESS) { if (match == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { service_registration_pt registration = NULL; status = serviceReference_getServiceRegistration(match->reference, ®istration); if (status == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { char *driverId = properties_get(properties, (char *) OSGI_DEVICEACCESS_DRIVER_ID); driver_attributes_pt finalAttributes = hashMap_get(manager->drivers, match->reference); if (finalAttributes == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { char *newDriverId = NULL; status = driverAttributes_attach(finalAttributes, reference, &newDriverId); if (status == CELIX_SUCCESS) { if (newDriverId != NULL) { array_list_pt ids = NULL; arrayList_create(&ids); arrayList_add(ids, newDriverId); arrayList_add(excluded, finalAttributes); status = deviceManager_matchAttachDriver(manager, attachPool, loader, ids, included, excluded, service, reference); } else { // Attached, unload unused drivers status = driverLoader_unloadDrivers(loader, finalAttributes); } } } } } } } } } if (references != NULL) { arrayList_destroy(references); } return status; }
static celix_status_t deviceManager_attachAlgorithm(device_manager_pt manager, service_reference_pt ref, void *service) { celix_status_t status = CELIX_SUCCESS; driver_loader_pt loader = NULL; status = driverLoader_create(manager->context, &loader); if (status == CELIX_SUCCESS) { array_list_pt included = NULL; array_list_pt excluded = NULL; array_list_pt driverIds = NULL; hashMap_put(manager->devices, ref, service); status = arrayList_create(&included); if (status == CELIX_SUCCESS) { status = arrayList_create(&excluded); if (status == CELIX_SUCCESS) { properties_pt properties = properties_create(); unsigned int size = 0; char **keys; serviceReference_getPropertyKeys(ref, &keys, &size); for (int i = 0; i < size; i++) { char* key = keys[i]; const char* value = NULL; serviceReference_getProperty(ref, key, &value); properties_set(properties, key, value); } status = driverLoader_findDrivers(loader, manager->locators, properties, &driverIds); if (status == CELIX_SUCCESS) { hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers); while (hashMapIterator_hasNext(iter)) { driver_attributes_pt driverAttributes = hashMapIterator_nextValue(iter); arrayList_add(included, driverAttributes); // Each driver that already is installed can be removed from the list char *id = NULL; celix_status_t substatus = driverAttributes_getDriverId(driverAttributes, &id); if (substatus == CELIX_SUCCESS) { // arrayList_removeElement(driverIds, id); array_list_iterator_pt idsIter = arrayListIterator_create(driverIds); while (arrayListIterator_hasNext(idsIter)) { char *value = arrayListIterator_next(idsIter); if (strcmp(value, id) == 0) { arrayListIterator_remove(idsIter); } } arrayListIterator_destroy(idsIter); } if(id != NULL){ free(id); } } hashMapIterator_destroy(iter); status = deviceManager_matchAttachDriver(manager, loader, driverIds, included, excluded, service, ref); } arrayList_destroy(driverIds); properties_destroy(properties); arrayList_destroy(excluded); } arrayList_destroy(included); } } driverLoader_destroy(&loader); return status; }
static celix_status_t deviceManager_attachAlgorithm(device_manager_pt manager, service_reference_pt ref, void *service) { celix_status_t status = CELIX_SUCCESS; apr_pool_t *attachPool = NULL; apr_status_t aprStatus = apr_pool_create(&attachPool, manager->pool); if (aprStatus != APR_SUCCESS) { status = CELIX_ILLEGAL_STATE; } else { driver_loader_pt loader = NULL; status = driverLoader_create(attachPool, manager->context, &loader); if (status == CELIX_SUCCESS) { array_list_pt included = NULL; array_list_pt excluded = NULL; array_list_pt driverIds = NULL; hashMap_put(manager->devices, ref, service); status = arrayList_create(&included); if (status == CELIX_SUCCESS) { status = arrayList_create(&excluded); if (status == CELIX_SUCCESS) { service_registration_pt registration = NULL; status = serviceReference_getServiceRegistration(ref, ®istration); if (status == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { status = driverLoader_findDrivers(loader, attachPool, manager->locators, properties, &driverIds); if (status == CELIX_SUCCESS) { hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers); while (hashMapIterator_hasNext(iter)) { driver_attributes_pt driverAttributes = hashMapIterator_nextValue(iter); arrayList_add(included, driverAttributes); // Each driver that already is installed can be removed from the list char *id = NULL; celix_status_t substatus = driverAttributes_getDriverId(driverAttributes, &id); if (substatus == CELIX_SUCCESS) { // arrayList_removeElement(driverIds, id); array_list_iterator_pt idsIter = arrayListIterator_create(driverIds); while (arrayListIterator_hasNext(idsIter)) { char *value = arrayListIterator_next(idsIter); if (strcmp(value, id) == 0) { arrayListIterator_remove(idsIter); } } arrayListIterator_destroy(idsIter); } else { // Ignore } } hashMapIterator_destroy(iter); status = deviceManager_matchAttachDriver(manager, attachPool, loader, driverIds, included, excluded, service, ref); arrayList_destroy(driverIds); } } } arrayList_destroy(excluded); } arrayList_destroy(included); } } apr_pool_destroy(attachPool); } return status; }