static VECTOR_HANDLE VECTOR_copy(VECTOR_HANDLE vector) { VECTOR_HANDLE new_vector = VECTOR_create(sizeof(STRING_HANDLE)); size_t size = VECTOR_size(vector); for (size_t index = 0; index < size && new_vector != NULL; index++) { STRING_HANDLE* str = VECTOR_element(vector, index); if (str == NULL) { VECTOR_destroy(new_vector); new_vector = NULL; } else { STRING_HANDLE new_str = STRING_clone(*str); if (new_str == NULL) { VECTOR_destroy(new_vector); new_vector = NULL; } else { if (VECTOR_push_back(new_vector, &new_str, 1) != 0) { STRING_delete(new_str); VECTOR_destroy(new_vector); new_vector = NULL; } } } } return new_vector; }
static CVECTOR *VECTOR_convert_to_complex(CVECTOR *_object) { CVECTOR *v = VECTOR_create(SIZE(THIS), TRUE, FALSE); int i; for (i = 0; i < SIZE(THIS); i++) gsl_vector_complex_set((gsl_vector_complex *)v->vector, i, gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0)); return v; }
static CVECTOR *VECTOR_copy(CVECTOR *_object) { CVECTOR *copy = VECTOR_create(SIZE(THIS), COMPLEX(THIS), FALSE); if (!COMPLEX(THIS)) gsl_vector_memcpy(VEC(copy), VEC(THIS)); else gsl_vector_complex_memcpy(CVEC(copy), CVEC(THIS)); return copy; }
static void IoTHubDeviceTwin_LL_CreatebasicModel_WithData15_inertPath(void) { STRICT_EXPECTED_CALL(Schema_GetSchemaForModel("basicModel_WithData15")); STRICT_EXPECTED_CALL(Schema_GetMetadata(TEST_SCHEMA_HANDLE)); STRICT_EXPECTED_CALL(Schema_GetModelByName(TEST_SCHEMA_HANDLE, "basicModel_WithData15")); STRICT_EXPECTED_CALL(CodeFirst_CreateDevice(TEST_SCHEMA_MODEL_TYPE_HANDLE, &ALL_REFLECTED(basic15), sizeof(basicModel_WithData15), true)); STRICT_EXPECTED_CALL(IoTHubClient_LL_SetDeviceTwinCallback(TEST_IOTHUB_CLIENT_LL_HANDLE, serializer_ingest, TEST_DEVICE_HANDLE)); STRICT_EXPECTED_CALL(IoTHubClient_LL_SetDeviceMethodCallback(TEST_IOTHUB_CLIENT_LL_HANDLE, deviceMethodCallback, TEST_DEVICE_HANDLE)); STRICT_EXPECTED_CALL(VECTOR_create(sizeof(SERIALIZER_DEVICETWIN_PROTOHANDLE))); STRICT_EXPECTED_CALL(VECTOR_push_back(IGNORED_PTR_ARG, IGNORED_PTR_ARG, 1)) .IgnoreArgument_handle() .IgnoreArgument_elements(); }
HTTPAPIEX_HANDLE HTTPAPIEX_Create(const char* hostName) { HTTPAPIEX_HANDLE result; /*Codes_SRS_HTTPAPIEX_02_001: [If parameter hostName is NULL then HTTPAPIEX_Create shall return NULL.]*/ if (hostName == NULL) { LogError("invalid (NULL) parameter\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_005: [If creating the handle fails for any reason, then HTTAPIEX_Create shall return NULL.] */ HTTPAPIEX_HANDLE_DATA* handleData = (HTTPAPIEX_HANDLE_DATA*)malloc(sizeof(HTTPAPIEX_HANDLE_DATA)); if (handleData == NULL) { LogError("malloc failed.\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_002: [Parameter hostName shall be saved.]*/ handleData->hostName = STRING_construct(hostName); if (handleData->hostName == NULL) { free(handleData); LogError("unable to STRING_construct\r\n"); result = NULL; } else { /*Codes_SRS_HTTPAPIEX_02_004: [Otherwise, HTTPAPIEX_Create shall return a HTTAPIEX_HANDLE suitable for further calls to the module.] */ handleData->savedOptions = VECTOR_create(sizeof(HTTPAPIEX_SAVED_OPTION)); if (handleData->savedOptions == NULL) { STRING_delete(handleData->hostName); free(handleData); result = NULL; } else { handleData->k = -1; handleData->httpHandle = NULL; result = handleData; } } } } return result; }
REPORTED_PROPERTIES_TRANSACTION_HANDLE DataPublisher_CreateTransaction_ReportedProperties(DATA_PUBLISHER_HANDLE dataPublisherHandle) { REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA* result; /*Codes_SRS_DATA_PUBLISHER_02_027: [ If argument dataPublisherHandle is NULL then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/ if (dataPublisherHandle == NULL) { LogError("invalid argument DATA_PUBLISHER_HANDLE dataPublisherHandle=%p", dataPublisherHandle); result = NULL; } else { /*Codes_SRS_DATA_PUBLISHER_02_028: [ DataPublisher_CreateTransaction_ReportedProperties shall create a VECTOR_HANDLE holding the individual elements of the transaction (DATA_MARSHALLER_VALUE). ]*/ result = (REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA*)malloc(sizeof(REPORTED_PROPERTIES_TRANSACTION_HANDLE_DATA)); if (result == NULL) { /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/ LogError("unable to malloc"); /*return as is */ } else { result->value = VECTOR_create(sizeof(DATA_MARSHALLER_VALUE*)); if (result->value == NULL) { /*Codes_SRS_DATA_PUBLISHER_02_029: [ If any error occurs then DataPublisher_CreateTransaction_ReportedProperties shall fail and return NULL. ]*/ LogError("unable to VECTOR_create"); free(result); result = NULL; } else { /*Codes_SRS_DATA_PUBLISHER_02_030: [ Otherwise DataPublisher_CreateTransaction_ReportedProperties shall succeed and return a non-NULL handle. ]*/ result->DataPublisherInstance = dataPublisherHandle; } } } return result; }
MODULE_LOADER_RESULT ModuleLoader_Initialize(void) { MODULE_LOADER_RESULT result; /*Codes_SRS_MODULE_LOADER_13_001: [ ModuleLoader_Initialize shall initialize g_module_loader.lock. ]*/ g_module_loaders.lock = Lock_Init(); if (g_module_loaders.lock == NULL) { LogError("Lock_Init failed"); /*Codes_SRS_MODULE_LOADER_13_002: [ ModuleLoader_Initialize shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/ result = MODULE_LOADER_ERROR; } else { /*Codes_SRS_MODULE_LOADER_13_003: [ ModuleLoader_Initialize shall acquire the lock on g_module_loader.lock. ]*/ if (Lock(g_module_loaders.lock) != LOCK_OK) { LogError("Lock failed"); Lock_Deinit(g_module_loaders.lock); g_module_loaders.lock = NULL; /*Codes_SRS_MODULE_LOADER_13_002: [ ModuleLoader_Initialize shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/ result = MODULE_LOADER_ERROR; } else { /*Codes_SRS_MODULE_LOADER_13_004: [ ModuleLoader_Initialize shall initialize g_module.module_loaders by calling VECTOR_create. ]*/ g_module_loaders.module_loaders = VECTOR_create(sizeof(MODULE_LOADER*)); if (g_module_loaders.module_loaders == NULL) { LogError("VECTOR_create failed"); Unlock(g_module_loaders.lock); Lock_Deinit(g_module_loaders.lock); g_module_loaders.lock = NULL; /*Codes_SRS_MODULE_LOADER_13_002: [ ModuleLoader_Initialize shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/ result = MODULE_LOADER_ERROR; } else { // add all supported module loaders const MODULE_LOADER* supported_loaders[] = { DynamicLoader_Get() #ifdef NODE_BINDING_ENABLED , NodeLoader_Get() #endif #ifdef JAVA_BINDING_ENABLED , JavaLoader_Get() #endif #ifdef DOTNET_BINDING_ENABLED , DotnetLoader_Get() #endif #ifdef DOTNET_CORE_BINDING_ENABLED , DotnetCoreLoader_Get() #endif }; size_t loaders_count = sizeof(supported_loaders) / sizeof(supported_loaders[0]); size_t i; for (i = 0; i < loaders_count; i++) { /*Codes_SRS_MODULE_LOADER_13_005: [ ModuleLoader_Initialize shall add the default support module loaders to g_module.module_loaders. ]*/ if (add_module_loader(supported_loaders[i]) != MODULE_LOADER_SUCCESS) { LogError("Could not add loader - %s", supported_loaders[i]->name); break; } } /*Codes_SRS_MODULE_LOADER_13_007: [ ModuleLoader_Initialize shall unlock g_module.lock. ]*/ Unlock(g_module_loaders.lock); // adding loaders failed if we bailed early from the loop above if (i < loaders_count) { ModuleLoader_Destroy(); /*Codes_SRS_MODULE_LOADER_13_002: [ ModuleLoader_Initialize shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/ result = MODULE_LOADER_ERROR; } else { /*Codes_SRS_MODULE_LOADER_13_006: [ ModuleLoader_Initialize shall return MODULE_LOADER_SUCCESS once all the default loaders have been added successfully. ]*/ result = MODULE_LOADER_SUCCESS; } } } } return result; }
static bool _convert(CVECTOR *_object, GB_TYPE type, GB_VALUE *conv) { if (THIS) { if (!COMPLEX(THIS)) { switch (type) { case GB_T_FLOAT: conv->_float.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_SINGLE: conv->_single.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_INTEGER: case GB_T_SHORT: case GB_T_BYTE: conv->_integer.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_LONG: conv->_long.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_STRING: case GB_T_CSTRING: conv->_string.value.addr = _to_string(THIS, type == GB_T_CSTRING); conv->_string.value.start = 0; conv->_string.value.len = GB.StringLength(conv->_string.value.addr); return FALSE; default: break; } } else { switch (type) { case GB_T_FLOAT: conv->_float.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_SINGLE: conv->_single.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_INTEGER: case GB_T_SHORT: case GB_T_BYTE: conv->_integer.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_LONG: conv->_long.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_STRING: case GB_T_CSTRING: conv->_string.value.addr = _to_string(THIS, type == GB_T_CSTRING); conv->_string.value.start = 0; conv->_string.value.len = GB.StringLength(conv->_string.value.addr); return FALSE; default: break; } } // Vector ---> Float[] if ((type == GB.FindClass("Float[]") || type == CLASS_Polynomial) && !COMPLEX(THIS)) { GB_ARRAY a; int i; double *data; GB.Array.New(&a, GB_T_FLOAT, SIZE(THIS)); data = (double *)GB.Array.Get(a, 0); for(i = 0; i < SIZE(THIS); i++) data[i] = gsl_vector_get(VEC(THIS), i); conv->_object.value = a; if (type != CLASS_Polynomial) return FALSE; } // Vector ---> Complex[] else if (type == GB.FindClass("Complex[]") || type == CLASS_Polynomial) { GB_ARRAY a; int i; void **data; CCOMPLEX *c; GB.Array.New(&a, CLASS_Complex, SIZE(THIS)); data = (void **)GB.Array.Get(a, 0); for(i = 0; i < SIZE(THIS); i++) { c = COMPLEX_create(COMPLEX(THIS) ? gsl_vector_complex_get(CVEC(THIS), i) : gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0)); data[i] = c; GB.Ref(c); } conv->_object.value = a; if (type != CLASS_Polynomial) return FALSE; } else return TRUE; // Vector ---> Polynomial if (type == CLASS_Polynomial) { void *unref = conv->_object.value; GB.Ref(unref); // Will be unref by the next GB.Conv() POLYNOMIAL_convert(FALSE, type, conv); GB.Unref(&unref); // Will be unref by the next GB.Conv() //GB.Conv(conv, type); //GB.UnrefKeep(&conv->_object.value, FALSE); // Will be ref again after the current GB.Conv() return FALSE; } } else if (type >= GB_T_OBJECT) { if (GB.Is(conv->_object.value, CLASS_Array)) { GB_ARRAY array = (GB_ARRAY)conv->_object.value; int size = GB.Array.Count(array); CVECTOR *v; int i; GB_VALUE temp; void *data; GB_TYPE atype = GB.Array.Type(array); // Float[] Integer[] ... ---> Vector if (atype > GB_T_BOOLEAN && atype <= GB_T_FLOAT) { v = VECTOR_create(size, FALSE, FALSE); for (i = 0; i < size; i++) { data = GB.Array.Get(array, i); GB.ReadValue(&temp, data, atype); GB.Conv(&temp, GB_T_FLOAT); gsl_vector_set(VEC(v), i, temp._float.value); } conv->_object.value = v; return FALSE; } // Variant[] ---> Vector else if (atype == GB_T_VARIANT) { CCOMPLEX *c; v = VECTOR_create(size, TRUE, FALSE); for (i = 0; i < size; i++) { GB.ReadValue(&temp, GB.Array.Get(array, i), atype); GB.BorrowValue(&temp); GB.Conv(&temp, CLASS_Complex); c = temp._object.value; if (c) gsl_vector_complex_set(CVEC(v), i, c->number); else gsl_vector_complex_set(CVEC(v), i, COMPLEX_zero); GB.ReleaseValue(&temp); } conv->_object.value = v; return FALSE; } // Complex[] ---> Vector else if (atype == CLASS_Complex) { CCOMPLEX *c; v = VECTOR_create(size, TRUE, FALSE); for (i = 0; i < size; i++) { c = *((CCOMPLEX **)GB.Array.Get(array, i)); if (c) gsl_vector_complex_set(CVEC(v), i, c->number); else gsl_vector_complex_set(CVEC(v), i, COMPLEX_zero); } conv->_object.value = v; return FALSE; } } // Float Integer... ---> Vector else if (type > GB_T_BOOLEAN && type <= GB_T_FLOAT) { CVECTOR *v = VECTOR_create(1, FALSE, FALSE); if (type == GB_T_FLOAT) gsl_vector_set(VEC(v), 0, conv->_float.value); else if (type == GB_T_SINGLE) gsl_vector_set(VEC(v), 0, conv->_single.value); else gsl_vector_set(VEC(v), 0, conv->_integer.value); conv->_object.value = v; return FALSE; } // Complex ---> Vector else if (type == CLASS_Complex) { CCOMPLEX *c = (CCOMPLEX *)conv->_object.value; CVECTOR *v = VECTOR_create(1, TRUE, FALSE); gsl_vector_complex_set(CVEC(v), 0, c->number); conv->_object.value = v; return FALSE; } } return TRUE; }
TRANSPORT_HANDLE IoTHubTransport_Create(IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol, const char* iotHubName, const char* iotHubSuffix) { TRANSPORT_HANDLE_DATA * result; if (protocol == NULL || iotHubName == NULL || iotHubSuffix == NULL) { /*Codes_SRS_IOTHUBTRANSPORT_17_002: [ If protocol is NULL, this function shall return NULL. ]*/ /*Codes_SRS_IOTHUBTRANSPORT_17_003: [ If iotHubName is NULL, this function shall return NULL. ]*/ /*Codes_SRS_IOTHUBTRANSPORT_17_004: [ If iotHubSuffix is NULL, this function shall return NULL. ]*/ LogError("Invalid NULL argument, protocol [%p], name [%p], suffix [%p].", protocol, iotHubName, iotHubSuffix); result = NULL; } else { /*Codes_SRS_IOTHUBTRANSPORT_17_032: [ IoTHubTransport_Create shall allocate memory for the transport data. ]*/ result = (TRANSPORT_HANDLE_DATA*)malloc(sizeof(TRANSPORT_HANDLE_DATA)); if (result == NULL) { /*Codes_SRS_IOTHUBTRANSPORT_17_040: [ If memory allocation fails, IoTHubTransport_Create shall return NULL. ]*/ LogError("Transport handle was not allocated."); } else { TRANSPORT_PROVIDER * transportProtocol = (TRANSPORT_PROVIDER*)(protocol()); IOTHUB_CLIENT_CONFIG upperConfig; upperConfig.deviceId = NULL; upperConfig.deviceKey = NULL; upperConfig.iotHubName = iotHubName; upperConfig.iotHubSuffix = iotHubSuffix; upperConfig.protocol = protocol; upperConfig.protocolGatewayHostName = NULL; IOTHUBTRANSPORT_CONFIG transportLLConfig; transportLLConfig.upperConfig = &upperConfig; transportLLConfig.waitingToSend = NULL; /*Codes_SRS_IOTHUBTRANSPORT_17_005: [ IoTHubTransport_Create shall create the lower layer transport by calling the protocol's IoTHubTransport_Create function. ]*/ result->transportLLHandle = transportProtocol->IoTHubTransport_Create(&transportLLConfig); if (result->transportLLHandle == NULL) { /*Codes_SRS_IOTHUBTRANSPORT_17_006: [ If the creation of the transport fails, IoTHubTransport_Create shall return NULL. ]*/ LogError("Lower Layer transport not created."); free(result); result = NULL; } else { /*Codes_SRS_IOTHUBTRANSPORT_17_007: [ IoTHubTransport_Create shall create the transport lock by Calling Lock_Init. ]*/ result->lockHandle = Lock_Init(); if (result->lockHandle == NULL) { /*Codes_SRS_IOTHUBTRANSPORT_17_008: [ If the lock creation fails, IoTHubTransport_Create shall return NULL. ]*/ LogError("transport Lock not created."); transportProtocol->IoTHubTransport_Destroy(result->transportLLHandle); free(result); result = NULL; } else { /*Codes_SRS_IOTHUBTRANSPORT_17_038: [ IoTHubTransport_Create shall call VECTOR_Create to make a list of IOTHUB_CLIENT_HANDLE using this transport. ]*/ result->clients = VECTOR_create(sizeof(IOTHUB_CLIENT_HANDLE)); if (result->clients == NULL) { /*Codes_SRS_IOTHUBTRANSPORT_17_039: [ If the Vector creation fails, IoTHubTransport_Create shall return NULL. ]*/ /*Codes_SRS_IOTHUBTRANSPORT_17_009: [ IoTHubTransport_Create shall clean up any resources it creates if the function does not succeed. ]*/ LogError("clients list not created."); Lock_Deinit(result->lockHandle); transportProtocol->IoTHubTransport_Destroy(result->transportLLHandle); free(result); result = NULL; } else { /*Codes_SRS_IOTHUBTRANSPORT_17_001: [ IoTHubTransport_Create shall return a non-NULL handle on success.]*/ result->stopThread = 1; result->workerThreadHandle = NULL; /* create thread when work needs to be done */ result->IoTHubTransport_SetOption = transportProtocol->IoTHubTransport_SetOption; result->IoTHubTransport_Create = transportProtocol->IoTHubTransport_Create; result->IoTHubTransport_Destroy = transportProtocol->IoTHubTransport_Destroy; result->IoTHubTransport_Register = transportProtocol->IoTHubTransport_Register; result->IoTHubTransport_Unregister = transportProtocol->IoTHubTransport_Unregister; result->IoTHubTransport_Subscribe = transportProtocol->IoTHubTransport_Subscribe; result->IoTHubTransport_Unsubscribe = transportProtocol->IoTHubTransport_Unsubscribe; result->IoTHubTransport_DoWork = transportProtocol->IoTHubTransport_DoWork; result->IoTHubTransport_GetSendStatus = transportProtocol->IoTHubTransport_GetSendStatus; } } } } } return result; }