static BROKER_RESULT init_module(BROKER_MODULEINFO* module_info, const MODULE* module) { BROKER_RESULT result; /*Codes_SRS_BROKER_13_107: The function shall assign the `module` handle to `BROKER_MODULEINFO::module`.*/ module_info->module = (MODULE*)malloc(sizeof(MODULE)); if (module_info->module == NULL) { LogError("Allocate module failed"); result = BROKER_ERROR; } else { module_info->module->module_apis = module->module_apis; module_info->module->module_handle = module->module_handle; /*Codes_SRS_BROKER_13_099: [The function shall initialize BROKER_MODULEINFO::socket_lock with a valid lock handle.]*/ module_info->socket_lock = Lock_Init(); if (module_info->socket_lock == NULL) { /*Codes_SRS_BROKER_13_047: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/ LogError("Lock_Init for socket lock failed"); result = BROKER_ERROR; } else { char uuid[BROKER_GUID_SIZE]; memset(uuid, 0, BROKER_GUID_SIZE); /*Codes_SRS_BROKER_17_020: [ The function shall create a unique ID used as a quit signal. ]*/ if (UniqueId_Generate(uuid, BROKER_GUID_SIZE) != UNIQUEID_OK) { /*Codes_SRS_BROKER_13_047: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/ LogError("Lock_Init for socket lock failed"); Lock_Deinit(module_info->socket_lock); result = BROKER_ERROR; } else { module_info->quit_message_guid = STRING_construct(uuid); if (module_info->quit_message_guid == NULL) { /*Codes_SRS_BROKER_13_047: [ This function shall return BROKER_ERROR if an underlying API call to the platform causes an error or BROKER_OK otherwise. ]*/ LogError("String construct failed for module guid"); Lock_Deinit(module_info->socket_lock); result = BROKER_ERROR; } else { result = BROKER_OK; } } } } return result; }
static EXPECTED_RECEIVE_DATA* RecvTestData_Create(void) { EXPECTED_RECEIVE_DATA* result; result = (EXPECTED_RECEIVE_DATA*)malloc(sizeof(EXPECTED_RECEIVE_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { free(result); result = NULL; } else { char temp[1000]; char* tempString; result->wasFound = false; time_t t = time(NULL); (void)sprintf_s(temp, sizeof(temp), TEST_CMP_DATA_FMT, ctime(&t), g_uniqueTestId); result->compareDataSize = strlen(temp); tempString = (char*)malloc(result->compareDataSize+1); if (tempString == NULL) { (void)Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->compareData = tempString; (void)sprintf_s(temp, sizeof(temp), TEST_RECV_DATA_FMT, ctime(&t), g_uniqueTestId); tempString = (char*)malloc(strlen(temp) + 1); if (tempString == NULL) { (void)Lock_Deinit(result->lock); free((void*)result->compareData); free(result); result = NULL; } else { strcpy(tempString, temp); result->toBeSendSize = strlen(tempString); result->toBeSend = tempString; } } } } return result; }
static EXPECTED_RECEIVE_DATA* RecvMacroTestData_Create(void) { EXPECTED_RECEIVE_DATA* result; result = (EXPECTED_RECEIVE_DATA*)malloc(sizeof(EXPECTED_RECEIVE_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { free(result); result = NULL; } else { char* tempString; result->wasFound = false; tempString = (char*)malloc(TIME_DATA_LENGTH); if (tempString == NULL) { (void)Lock_Deinit(result->lock); free(result); result = NULL; } else { time_t t = time(NULL); (void)sprintf_s(tempString, TIME_DATA_LENGTH, "%.24s", ctime(&t) ); result->compareData = tempString; result->compareDataSize = strlen(result->compareData); size_t nLen = result->compareDataSize+strlen(TEST_MACRO_RECV_DATA_FMT)+4; tempString = (char*)malloc(nLen); if (tempString == NULL) { (void)Lock_Deinit(result->lock); free((void*)result->compareData); free(result); result = NULL; } else { (void)sprintf_s(tempString, nLen, TEST_MACRO_RECV_DATA_FMT, result->compareData, g_uniqueTestId); result->toBeSendSize = strlen(tempString); result->toBeSend = tempString; } } } } return result; }
static EXPECTED_RECEIVE_DATA* MessageData_Create(void) { EXPECTED_RECEIVE_DATA* result = (EXPECTED_RECEIVE_DATA*)malloc(sizeof(EXPECTED_RECEIVE_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { free(result); result = NULL; } else { char temp[1000]; char* tempString; time_t t = time(NULL); sprintf(temp, TEST_MESSAGE_DATA_FMT, ctime(&t), g_iotHubTestId); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { (void)Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->data = tempString; result->dataSize = strlen(result->data); result->wasFound = false; result->toBeSend = (const unsigned char*)tempString; result->toBeSendSize = strlen(tempString); } } } return result; }
void IoTHubTransport_Destroy(TRANSPORT_HANDLE transportHlHandle) { /*Codes_SRS_IOTHUBTRANSPORT_17_011: [ IoTHubTransport_Destroy shall do nothing if transportHlHandle is NULL. ]*/ if (transportHlHandle != NULL) { TRANSPORT_HANDLE_DATA * transportData = (TRANSPORT_HANDLE_DATA*)transportHlHandle; /*Codes_SRS_IOTHUBTRANSPORT_17_033: [ IoTHubTransport_Destroy shall lock the transport lock. ]*/ if (Lock(transportData->lockHandle) != LOCK_OK) { LogError("Unable to lock - will still attempt to end thread without thread safety"); stop_worker_thread(transportData); } else { stop_worker_thread(transportData); (void)Unlock(transportData->lockHandle); } wait_worker_thread(transportData); /*Codes_SRS_IOTHUBTRANSPORT_17_010: [ IoTHubTransport_Destroy shall free all resources. ]*/ Lock_Deinit(transportData->lockHandle); (transportData->IoTHubTransport_Destroy)(transportData->transportLLHandle); VECTOR_destroy(transportData->clients); free(transportHlHandle); } }
static void shutdown_a_thread(THREAD_CONTROL * theThreadControl) { int notUsed; THREAD_HANDLE theCurrentThread; /*Codes_SRS_OUTPROCESS_MODULE_17_027: [ This function shall ensure thread safety on execution. ]*/ if (Lock(theThreadControl->thread_lock) != LOCK_OK) { /*Codes_SRS_OUTPROCESS_MODULE_17_032: [ This function shall signal the messaging thread to close. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_049: [ This function shall signal the outgoing gateway message thread to close. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_050: [ This function shall signal the control thread to close. ]*/ LogError("not able to Lock, still setting the thread to finish"); theCurrentThread = theThreadControl->thread_handle; theThreadControl->thread_flag = THREAD_FLAG_STOP; } else { /*Codes_SRS_OUTPROCESS_MODULE_17_032: [ This function shall signal the messaging thread to close. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_049: [ This function shall signal the outgoing gateway message thread to close. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_050: [ This function shall signal the control thread to close. ]*/ theThreadControl->thread_flag = THREAD_FLAG_STOP; theCurrentThread = theThreadControl->thread_handle; (void)Unlock(theThreadControl->thread_lock); } /*Codes_SRS_OUTPROCESS_MODULE_17_033: [ This function shall wait for the messaging thread to complete. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_051: [ This function shall wait for the outgoing gateway message thread to complete. ]*/ /*Codes_SRS_OUTPROCESS_MODULE_17_052: [ This function shall wait for the control thread to complete. ]*/ if (theCurrentThread != NULL && ThreadAPI_Join(theCurrentThread, ¬Used) != THREADAPI_OK) { LogError("unable to ThreadAPI_Join message thread, still proceeding in _Destroy"); } /*Codes_SRS_OUTPROCESS_MODULE_17_034: [ This function shall release all resources created by this module. ]*/ (void)Lock_Deinit(theThreadControl->thread_lock); }
static EXPECTED_SEND_DATA* SendMacroTestData_Create(const char* pszTime) { EXPECTED_SEND_DATA* result = (EXPECTED_SEND_DATA*)malloc(sizeof(EXPECTED_SEND_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { free(result); result = NULL; } else { char temp[1000]; char* tempString; sprintf(temp, TEST_MACRO_CMP_DATA_FMT, g_uniqueTestId, pszTime); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->expectedString = tempString; result->wasFound = false; result->dataWasSent = false; } } } return result; }
static EXPECTED_SEND_DATA* EventData_Create(void) { EXPECTED_SEND_DATA* result = (EXPECTED_SEND_DATA*)malloc(sizeof(EXPECTED_SEND_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { ASSERT_FAIL("unable to Lock_Init"); } else { char temp[1000]; char* tempString; time_t t = time(NULL); sprintf(temp, TEST_EVENT_DATA_FMT, ctime(&t), g_iotHubTestId); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->expectedString = tempString; result->wasFound = false; result->dataWasRecv = false; } } } return result; }
static void deinit_module(BROKER_MODULEINFO* module_info) { /*Codes_SRS_BROKER_13_057: [The function shall free all members of the MODULE_INFO object.]*/ Lock_Deinit(module_info->socket_lock); STRING_delete(module_info->quit_message_guid); free(module_info->module); }
static void broker_decrement_ref(BROKER_HANDLE broker) { /*Codes_SRS_BROKER_13_058: [If `broker` is NULL the function shall do nothing.]*/ if (broker != NULL) { /*Codes_SRS_BROKER_13_111: [Otherwise, Broker_Destroy shall decrement the internal ref count of the message.]*/ /*Codes_SRS_BROKER_13_112: [If the ref count is zero then the allocated resources are freed.]*/ if (DEC_REF(BROKER_HANDLE_DATA, broker) == DEC_RETURN_ZERO) { BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker; if (singlylinkedlist_get_head_item(broker_data->modules) != NULL) { LogError("WARNING: There are still active modules attached to the broker and the broker is being destroyed."); } /* May want to do nn_shutdown first for cleanliness. */ nn_close(broker_data->publish_socket); STRING_delete(broker_data->url); singlylinkedlist_destroy(broker_data->modules); Lock_Deinit(broker_data->modules_lock); free(broker_data); } } else { LogError("broker handle is NULL"); } }
void JavaModuleHostManager_Destroy(JAVA_MODULE_HOST_MANAGER_HANDLE manager) { if (manager != NULL && manager != instance) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_009: [ The function shall do nothing if handle is NULL. ]*/ LogError("[FATAL]: JAVA_MODULE_HOST_MANAGER_HANDLE is invalid. JAVA_MODULE_HOST_MANAGER_HANDLE = [%p] while instance = [%p].", manager, instance); } else if(manager != NULL) { if (manager->module_count == 0) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_011: [ The function shall deinitialize handle->lock. ]*/ if (Lock_Deinit(manager->lock) != LOCK_OK) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_012: [ The function shall do nothing if lock deinitialization fails. ]*/ LogError("[FATAL]: Lock_Deinit() failed."); } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_013: [ The function shall free the handle parameter. ]*/ /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_032: [ The function shall destroy the JAVA_MODULE_HOST_CONFIG structure. ]*/ config_destroy(manager->config); free(manager); manager = NULL; instance = NULL; } } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_010: [ The function shall do nothing if handle->module_count is not 0. ]*/ LogInfo("Manager module count is not 0 (%i) and will not be destroyed.", manager->module_count); } } }
static EXPECTED_SEND_DATA* SendTestData_Create(void) { EXPECTED_SEND_DATA* result = (EXPECTED_SEND_DATA*)malloc(sizeof(EXPECTED_SEND_DATA)); if (result != NULL) { if ((result->lock = Lock_Init()) == NULL) { free(result); result = NULL; } else { char temp[1000]; char* tempString; time_t t = time(NULL); sprintf(temp, TEST_SEND_DATA_FMT, ctime(&t), g_uniqueTestId); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->expectedString = tempString; result->wasFound = false; result->dataWasSent = false; } } } return result; }
void gbnetwork_deinit(void) { if (gbnetworkState == GBNETWORK_STATE_INIT) { (void)Lock_Deinit(gbnetworkThreadSafeLock); } gbnetworkState = GBNETWORK_STATE_NOT_INIT; }
static void MessageData_Destroy(EXPECTED_RECEIVE_DATA* data) { if (data != NULL) { (void)Lock_Deinit(data->lock); free((void*)data->data); } free(data); }
static void ReceiveUserContext_Destroy(EXPECTED_RECEIVE_DATA* data) { if (data != NULL) { (void)Lock_Deinit(data->lock); free(data); } }
void gballoc_deinit(void) { if (gballocState == GBALLOC_STATE_INIT) { /* Codes_SRS_GBALLOC_01_028: [gballoc_deinit shall free all resources allocated by gballoc_init.] */ (void)Lock_Deinit(gballocThreadSafeLock); } gballocState = GBALLOC_STATE_NOT_INIT; }
int ProxyGateway_HaltWorkerThread ( REMOTE_MODULE_HANDLE remote_module ) { int result; /* Codes_SRS_PROXY_GATEWAY_027_045: [Prerequisite Check - If the `remote_module` parameter is `NULL`, then `ProxyGateway_HaltWorkerThread` shall return a non-zero value] */ if (NULL == remote_module) { LogError("%s: NULL parameter - remote_module!", __FUNCTION__); result = __LINE__; // Check to confirm message thread is running } else if (NULL == remote_module->message_thread) { /* Codes_SRS_PROXY_GATEWAY_027_046: [Prerequisite Check - If a worker thread does not exist, then `ProxyGateway_HaltWorkerThread` shall return a non-zero value] */ LogInfo("%s: Worker thread has not been initialized.", __FUNCTION__); result = __LINE__; /* Codes_SRS_PROXY_GATEWAY_027_047: [`ProxyGateway_HaltWorkerThread` shall obtain the thread mutex in order to signal the thread by calling `LOCK_RESULT Lock(LOCK_HANDLE handle)`] */ } else if (LOCK_OK != Lock(remote_module->message_thread->mutex)) { /* Codes_SRS_PROXY_GATEWAY_027_048: [If unable to obtain the mutex, then `ProxyGateway_HaltWorkerThread` shall return a non-zero value] */ LogError("%s: Unable to acquire mutex!", __FUNCTION__); result = __LINE__; } else { int thread_exit_result = -1; // Signal the message thread remote_module->message_thread->halt = true; /* Codes_SRS_PROXY_GATEWAY_027_049: [`ProxyGateway_HaltWorkerThread` shall release the thread mutex upon signalling by calling `LOCK_RESULT Unlock(LOCK_HANDLE handle)`] */ if (LOCK_OK != Unlock(remote_module->message_thread->mutex)) { /* Codes_SRS_PROXY_GATEWAY_027_050: [If unable to release the mutex, then `ProxyGateway_HaltWorkerThread` shall return a non-zero value] */ LogError("%s: Unable to release mutex!", __FUNCTION__); result = __LINE__; /* Codes_SRS_PROXY_GATEWAY_027_051: [`ProxyGateway_HaltWorkerThread` shall halt the thread by calling `THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE handle, int * res)`] */ } else if (THREADAPI_OK != ThreadAPI_Join(remote_module->message_thread->thread, &thread_exit_result)) { /* Codes_SRS_PROXY_GATEWAY_027_052: [If unable to join the thread, then `ProxyGateway_HaltWorkerThread` shall return a non-zero value] */ LogError("%s: Unable to halt message thread!", __FUNCTION__); result = __LINE__; } else { /* Codes_SRS_PROXY_GATEWAY_027_053: [`ProxyGateway_HaltWorkerThread` shall free the thread mutex by calling `LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle)`] */ /* Codes_SRS_PROXY_GATEWAY_027_054: [If unable to free the thread mutex, then `ProxyGateway_HaltWorkerThread` shall ignore the result and continue processing] */ (void)Lock_Deinit(remote_module->message_thread->mutex); /* Codes_SRS_PROXY_GATEWAY_027_055: [`ProxyGateway_HaltWorkerThread` shall free the memory allocated to the thread details] */ free(remote_module->message_thread); remote_module->message_thread = NULL; if (0 != thread_exit_result) { /* Codes_SRS_PROXY_GATEWAY_027_056: [If an error is returned from the worker thread, then `ProxyGateway_HaltWorkerThread` shall return the worker thread's error code] */ LogError("%s: Thread exited with fail code <%d>!", __FUNCTION__, thread_exit_result); result = thread_exit_result; } else { /* Codes_SRS_PROXY_GATEWAY_027_057: [If no errors are encountered, then `ProxyGateway_HaltWorkerThread` shall return zero] */ result = 0; } } } return result; }
IOTHUB_MESSAGING_CLIENT_HANDLE IoTHubMessaging_Create(IOTHUB_SERVICE_CLIENT_AUTH_HANDLE serviceClientHandle) { IOTHUB_MESSAGING_CLIENT_INSTANCE* result; /*Codes_SRS_IOTHUBMESSAGING_12_001: [ IoTHubMessaging_Create shall verify the serviceClientHandle input parameter and if it is NULL then return NULL. ]*/ if (serviceClientHandle == NULL) { LogError("serviceClientHandle input parameter cannot be NULL"); result = NULL; } else { /*Codes_SRS_IOTHUBMESSAGING_12_002: [ IoTHubMessaging_Create shall allocate a new IoTHubMessagingClient instance. ]*/ if ((result = (IOTHUB_MESSAGING_CLIENT_INSTANCE*)malloc(sizeof(IOTHUB_MESSAGING_CLIENT_INSTANCE))) == NULL) { /*Codes_SRS_IOTHUBMESSAGING_12_003: [If allocating memory for the new IoTHubMessagingClient instance fails, then IoTHubMessaging_Create shall return NULL. ]*/ LogError("malloc failed for IoTHubMessagingClient"); result = NULL; } else { /*Codes_SRS_IOTHUBMESSAGING_12_004: [IoTHubMessaging_Create shall create a lock object to be used later for serializing IoTHubMessagingClient calls. ]*/ result->LockHandle = Lock_Init(); if (result->LockHandle == NULL) { /*Codes_SRS_IOTHUBMESSAGING_12_005: [If creating the lock fails, then IoTHubMessaging_Create shall return NULL. ]*/ /*Codes_SRS_IOTHUBMESSAGING_12_008: [If IoTHubMessaging_Create fails, all resources allocated by it shall be freed. ]*/ LogError("Lock_Init failed"); free(result); result = NULL; } else { /*Codes_SRS_IOTHUBMESSAGING_12_006: [IoTHubMessaging_Create shall instantiate a new IoTHubMessaging_LL instance by calling IoTHubMessaging_LL_Create and passing the serviceClientHandle argument. ]*/ result->IoTHubMessagingHandle = IoTHubMessaging_LL_Create(serviceClientHandle); if (result->IoTHubMessagingHandle == NULL) { /*Codes_SRS_IOTHUBMESSAGING_12_007: [ If IoTHubMessaging_LL_Create fails, then IoTHubMessaging_Create shall return NULL. ]*/ /*Codes_SRS_IOTHUBMESSAGING_12_008: [If IoTHubMessaging_Create fails, all resources allocated by it shall be freed. ]*/ LogError("IoTHubMessaging_LL_Create failed"); Lock_Deinit(result->LockHandle); free(result); result = NULL; } else { result->StopThread = 0; result->ThreadHandle = NULL; } } } } return (IOTHUB_MESSAGING_CLIENT_HANDLE)result; }
void ModuleLoader_Destroy(void) { if (g_module_loaders.lock != NULL) { if (Lock(g_module_loaders.lock) != LOCK_OK) { LogError("Lock failed. Proceeding with destruction anyway."); } } if(g_module_loaders.module_loaders != NULL) { // free all module loader resources size_t length = VECTOR_size(g_module_loaders.module_loaders); for (size_t i = 0; i < length; i++) { MODULE_LOADER* loader = *((MODULE_LOADER **)VECTOR_element(g_module_loaders.module_loaders, i)); // NOTE: We free the configuration object even for default loaders because // the configuration may have been replaced by the gateway for default // loaders. /*Codes_SRS_MODULE_LOADER_13_046: [ ModuleLoader_Destroy shall invoke FreeConfiguration on every module loader's configuration field. ]*/ if (loader->configuration != NULL) { loader->api->FreeConfiguration(loader, loader->configuration); } // if this is not a default loader then free resources allocated in // add_loader_from_json if (ModuleLoader_IsDefaultLoader(loader->name) == false) { /*Codes_SRS_MODULE_LOADER_13_047: [ ModuleLoader_Destroy shall free the loader's name and the loader itself if it is not a default loader. ]*/ free((void *)loader->name); free(loader); } } /*Codes_SRS_MODULE_LOADER_13_048: [ ModuleLoader_Destroy shall destroy the loaders vector. ]*/ VECTOR_destroy(g_module_loaders.module_loaders); g_module_loaders.module_loaders = NULL; } if (g_module_loaders.lock != NULL) { if (Unlock(g_module_loaders.lock) != LOCK_OK) { LogError("Unlock failed."); } /*Codes_SRS_MODULE_LOADER_13_045: [ ModuleLoader_Destroy shall free g_module_loaders.lock if it is not NULL. ]*/ Lock_Deinit(g_module_loaders.lock); g_module_loaders.lock = NULL; } }
static void SendTestData_Destroy(EXPECTED_SEND_DATA* data) { if (data != NULL) { (void)Lock_Deinit(data->lock); if (data->expectedString != NULL) { free((void*)data->expectedString); } free(data); } }
static void physical_device_delete(PHYSICAL_DEVICE *physical_device) { if (physical_device->new_firmware_URI != NULL) { free(physical_device->new_firmware_URI); } if (Lock_Deinit(physical_device->status_lock) == LOCK_ERROR) { LogError("failed to release lock handle"); } free(physical_device); }
static EXPECTED_RECEIVE_DATA* MessageData_Create(void) { EXPECTED_RECEIVE_DATA* result; time_t t; if ((t = time(NULL)) == INDEFINITE_TIME) { LogError("Failed creating data for EXPECTED_RECEIVE_DATA (time(NULL) failed)"); result = NULL; } else { if ((result = (EXPECTED_RECEIVE_DATA*)malloc(sizeof(EXPECTED_RECEIVE_DATA))) == NULL) { LogError("MessageData_Create failed (malloc failure)"); } else { if ((result->lock = Lock_Init()) == NULL) { LogError("Failed initializing lock on MessageData_Create()."); free(result); result = NULL; } else { char temp[1000]; char* tempString; sprintf(temp, TEST_MESSAGE_DATA_FMT, ctime(&t), g_iotHubTestId); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { LogError("Failed allocating memory to create EXPECTED_RECEIVE_DATA"); (void)Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->data = tempString; result->dataSize = strlen(result->data); result->receivedByClient = false; result->data = (const char*)tempString; result->dataSize = strlen(tempString); } } } } return result; }
static EXPECTED_SEND_DATA* EventData_Create(void) { EXPECTED_SEND_DATA* result; time_t t; if ((t = time(NULL)) == INDEFINITE_TIME) { LogError("Failed creating data for EXPECTED_SEND_DATA (time(NULL) failed)"); result = NULL; } else { if ((result = (EXPECTED_SEND_DATA*)malloc(sizeof(EXPECTED_SEND_DATA))) == NULL) { LogError("EventData_Create failed (malloc failure)"); } else { if ((result->lock = Lock_Init()) == NULL) { LogError("Unable to Lock_Init"); } else { char temp[1000]; char* tempString; sprintf(temp, TEST_EVENT_DATA_FMT, ctime(&t), g_iotHubTestId); if ((tempString = (char*)malloc(strlen(temp) + 1)) == NULL) { Lock_Deinit(result->lock); free(result); result = NULL; } else { strcpy(tempString, temp); result->expectedString = tempString; result->wasFoundInHub = false; result->dataWasSent = false; result->timeSent = 0; result->timeReceived = 0; } } } } return result; }
static void RecvTestData_Destroy(EXPECTED_RECEIVE_DATA* data) { if (data != NULL) { (void)Lock_Deinit(data->lock); if (data->compareData != NULL) { free((void*)(data->compareData)); } if (data->toBeSend != NULL) { free((void*)data->toBeSend); } free(data); } }
void IoTHubMessaging_Destroy(IOTHUB_MESSAGING_CLIENT_HANDLE messagingClientHandle) { /*Codes_SRS_IOTHUBMESSAGING_12_009: [ IoTHubMessaging_Destroy shall do nothing if parameter messagingClientHandle is NULL. ]*/ if (messagingClientHandle == NULL) { LogError("messagingClientHandle input parameter is NULL"); } else { IOTHUB_MESSAGING_CLIENT_INSTANCE* messagingClientInstance = (IOTHUB_MESSAGING_CLIENT_INSTANCE*)messagingClientHandle; /*Codes_SRS_IOTHUBMESSAGING_12_011: [ IoTHubMessaging_Destroy shall destroy IoTHubMessagingHandle by call IoTHubMessaging_LL_Destroy. ]*/ IoTHubMessaging_LL_Destroy(messagingClientInstance->IoTHubMessagingHandle); /*Codes_SRS_IOTHUBMESSAGING_12_014: [ If the lock was allocated in IoTHubMessaging_Create, it shall be also freed. ]*/ Lock_Deinit(messagingClientInstance->LockHandle); free(messagingClientInstance); } }
int ProxyGateway_StartWorkerThread ( REMOTE_MODULE_HANDLE remote_module ) { int result; if (NULL == remote_module) { /* Codes_SRS_PROXY_GATEWAY_027_017: [Prerequisite Check - If the `remote_module` parameter is `NULL`, then `ProxyGateway_StartWorkerThread` shall do nothing and return a non-zero value] */ LogError("%s: NULL parameter - remote_module!", __FUNCTION__); result = __LINE__; } else if (NULL != remote_module->message_thread) { /* Codes_SRS_PROXY_GATEWAY_027_018: [Prerequisite Check - If a work thread already exist for the given handle, then `ProxyGateway_StartWorkerThread` shall do nothing and return zero] */ LogInfo("%s: Worker thread has already been initialized.", __FUNCTION__); result = 0; /* Codes_SRS_PROXY_GATEWAY_027_019: [`ProxyGateway_StartWorkerThread` shall allocate the memory required to support the worker thread] */ } else if (NULL == (remote_module->message_thread = (MESSAGE_THREAD_HANDLE)calloc(1, sizeof(MESSAGE_THREAD)))) { /* Codes_SRS_PROXY_GATEWAY_027_020: [If memory allocation fails for the worker thread data, then `ProxyGateway_StartWorkerThread` shall return a non-zero value] */ LogError("%s: Unable to allocate memory!", __FUNCTION__); result = __LINE__; /* Codes_SRS_PROXY_GATEWAY_027_021: [`ProxyGateway_StartWorkerThread` shall create a mutex by calling `LOCK_HANDLE Lock_Init(void)`] */ } else if (NULL == (remote_module->message_thread->mutex = Lock_Init())) { /* Codes_SRS_PROXY_GATEWAY_027_022: [If a mutex is unable to be created, then `ProxyGateway_StartWorkerThread` shall free any previously allocated memory and return a non-zero value] */ LogError("%s: Unable to create mutex!", __FUNCTION__); result = __LINE__; free(remote_module->message_thread); remote_module->message_thread = (MESSAGE_THREAD_HANDLE)NULL; /* Codes_SRS_PROXY_GATEWAY_027_023: [`ProxyGateway_StartWorkerThread` shall start a worker thread by calling `THREADAPI_RESULT ThreadAPI_Create(&THREAD_HANDLE threadHandle, THREAD_START_FUNC func, void * arg)` with an empty thread handle for `threadHandle`, a function that loops polling the messages for `func`, and `remote_module` for `arg`] */ } else if (THREADAPI_OK != ThreadAPI_Create(&remote_module->message_thread->thread, worker_thread, remote_module)) { /* Codes_SRS_PROXY_GATEWAY_027_024: [If the worker thread failed to start, then `ProxyGateway_StartWorkerThread` shall free any previously allocated memory and return a non-zero value] */ LogError("%s: Unable to create worker thread!", __FUNCTION__); result = __LINE__; (void)Lock_Deinit(remote_module->message_thread->mutex); free(remote_module->message_thread); remote_module->message_thread = (MESSAGE_THREAD_HANDLE)NULL; } else { /* Codes_SRS_PROXY_GATEWAY_027_025: [If no errors are encountered, then `ProxyGateway_StartWorkerThread` shall return zero] */ result = 0; } return result; }
BROKER_HANDLE Broker_Create(void) { BROKER_HANDLE_DATA* result; /*Codes_SRS_BROKER_13_067: [Broker_Create shall malloc a new instance of BROKER_HANDLE_DATA and return NULL if it fails.]*/ result = REFCOUNT_TYPE_CREATE(BROKER_HANDLE_DATA); if (result == NULL) { LogError("malloc returned NULL"); /*return as is*/ } else { /*Codes_SRS_BROKER_13_007: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules with a valid VECTOR_HANDLE.]*/ result->modules = singlylinkedlist_create(); if (result->modules == NULL) { /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/ LogError("VECTOR_create failed"); free(result); result = NULL; } else { /*Codes_SRS_BROKER_13_023: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules_lock with a valid LOCK_HANDLE.]*/ result->modules_lock = Lock_Init(); if (result->modules_lock == NULL) { /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/ LogError("Lock_Init failed"); singlylinkedlist_destroy(result->modules); free(result); result = NULL; } else { /*Codes_SRS_BROKER_17_001: [ Broker_Create shall initialize a socket for publishing messages. ]*/ result->publish_socket = nn_socket(AF_SP, NN_PUB); if (result->publish_socket < 0) { /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/ LogError("nanomsg puclish socket create failedL %d", result->publish_socket); singlylinkedlist_destroy(result->modules); Lock_Deinit(result->modules_lock); free(result); result = NULL; } else { result->url = construct_url(); if (result->url == NULL) { /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/ singlylinkedlist_destroy(result->modules); Lock_Deinit(result->modules_lock); nn_close(result->publish_socket); free(result); LogError("Unable to generate unique url."); result = NULL; } else { /*Codes_SRS_BROKER_17_004: [ Broker_Create shall bind the socket to the BROKER_HANDLE_DATA::url. ]*/ if (nn_bind(result->publish_socket, STRING_c_str(result->url)) < 0) { /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/ LogError("nanomsg bind failed"); singlylinkedlist_destroy(result->modules); Lock_Deinit(result->modules_lock); nn_close(result->publish_socket); STRING_delete(result->url); free(result); result = NULL; } } } } } } /*Codes_SRS_BROKER_13_001: [This API shall yield a BROKER_HANDLE representing the newly created message broker. This handle value shall not be equal to NULL when the API call is successful.]*/ return result; }
void MQTTAPI_Destroy(MQTTAPI_HANDLE instance) { PMQTTTAPI_HANDLE_DATA mqttHandleData = instance; /* Codes_SRS_MQTTAPI_04_021: [If parameter instance is NULL then MQTTAPI_Destroy shall take no action.] */ if (mqttHandleData != NULL) { /* Codes_SRS_MQTTAPI_04_022: [MQTTAPI_Destroy shall free all resources used by MQTTAPI_HANDLE.] */ STRING_delete(mqttHandleData->device_id); STRING_delete(mqttHandleData->device_key); STRING_delete(mqttHandleData->sasTokenSr); /* Codes_SRS_MQTTAPI_04_049: [If the instance is connected, MQTTAPI_Destroy shall disconnect the instance] */ if (mqttHandleData->subscribed) { MQTTAPI_Unsubscribe(mqttHandleData->subscribedTopicHandleData); } if (mqttHandleData->connected) { MQTTClient_disconnect(mqttHandleData->client, 0); } /* Codes_SRS_MQTTAPI_04_053: [If there are messages to be sent MQTTAPI_Destroy shall signalize the user that the message couldn’t be sent by reason of MQTTAPI_CONFIRMATION_BECAUSE_DESTROY] */ { PDLIST_ENTRY received; while ((received = DList_RemoveHeadList(&(mqttHandleData->messagesReceived))) != &(mqttHandleData->messagesReceived)) { MQTTAPI_MESSAGE_RECEIVED_LIST* temp = containingRecord(received, MQTTAPI_MESSAGE_RECEIVED_LIST, entry); if (mqttHandleData->maCallBack != NULL) { if (!mqttHandleData->maCallBack(mqttHandleData->maCallbackContext, temp->messageReceived)) { LogError("Client could not handle message, dropping it."); } } free(temp->messageReceived->payload); free(temp->messageReceived); free(temp); } } { PDLIST_ENTRY messageToSend; while ((messageToSend = DList_RemoveHeadList(&(mqttHandleData->messagesToSend))) != &(mqttHandleData->messagesToSend)) { MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(messageToSend, MQTTAPI_MESSAGE_SEND_LIST, entry); if (mqttHandleData->dcCallback != NULL) { mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY); MQTTClient_freeMessage(&(temp->messageToSend)); free(temp->messageToSend); STRING_delete(temp->topicName); free(temp); } } } { PDLIST_ENTRY currentListEntry; while ((currentListEntry = DList_RemoveHeadList(&(mqttHandleData->messagesSent))) != &(mqttHandleData->messagesSent)) { MQTTAPI_MESSAGE_SEND_LIST* temp = containingRecord(currentListEntry, MQTTAPI_MESSAGE_SEND_LIST, entry); if (mqttHandleData->dcCallback != NULL) { mqttHandleData->dcCallback(temp->context, MQTTAPI_CONFIRMATION_BECAUSE_DESTROY); MQTTClient_freeMessage(&(temp->messageToSend)); free(temp->messageToSend); STRING_delete(temp->topicName); free(temp); } } } MQTTClient_destroy(mqttHandleData->client); Lock_Deinit(mqttHandleData->LockHandle); free(mqttHandleData); } }
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; }
JAVA_MODULE_HOST_MANAGER_HANDLE JavaModuleHostManager_Create(JAVA_MODULE_HOST_CONFIG* config) { JAVA_MODULE_HOST_MANAGER_HANDLE_DATA* result; if (instance == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_002: [ The function shall allocate memory for the JAVA_MODULE_HOST_MANAGER_HANDLE if the global instance is NULL. ]*/ result = (JAVA_MODULE_HOST_MANAGER_HANDLE_DATA*)malloc(sizeof(JAVA_MODULE_HOST_MANAGER_HANDLE_DATA)); if (result == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_003: [ The function shall return NULL if memory allocation fails. ]*/ LogError("Failed to allocate memory for a JAVA_MODULE_HOST_MANAGER_HANDLE"); } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_004: [ The function shall initialize a new LOCK_HANDLE and set the JAVA_MODULE_HOST_MANAGER_HANDLE structures LOCK_HANDLE member. ]*/ if ((result->lock = Lock_Init()) == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_005: [ The function shall return NULL if lock initialization fails. ]*/ LogError("Lock_Init() failed."); free(result); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_028: [The function shall do a deep copy of the config parameter and keep a pointer to the copied JAVA_MODULE_HOST_CONFIG.]*/ result->config = config_copy(config); if (result->config == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_029: [If the deep copy fails, the function shall return NULL.]*/ LogError("Failed to copy the JAVA_MODULE_HOST_CONFIG structure."); Lock_Deinit(result->lock); free(result); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_006: [ The function shall set the module_count member variable to 0. ]*/ /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_007: [ The function shall shall set the global JAVA_MODULE_HOST_MANAGER_HANDLE instance. ]*/ result->module_count = 0; instance = result; } } } } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_030: [The function shall check against the singleton JAVA_MODULE_HOST_MANAGER_HANDLE instances JAVA_MODULE_HOST_CONFIG structure for equality in each field.]*/ if (config_options_compare(instance->config, config) != 0) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_031: [The function shall return NULL if the JAVA_MODULE_HOST_CONFIG structures do not match.]*/ LogError("JAVA_MODULE_HOST_CONFIG does not match the JAVA_MODULE_HOST_CONFIG known by this manager."); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_001: [ The function shall return the global JAVA_MODULE_HOST_MANAGER_HANDLE instance if it is not NULL. ]*/ result = instance; } } /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_008: [ The function shall return the global JAVA_MODULE_HOST_MANAGER_HANDLE instance. ]*/ return result; }