IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubClient_LL_MessageCallback(IOTHUB_CLIENT_LL_HANDLE handle, IOTHUB_MESSAGE_HANDLE message) { int result; /*Codes_SRS_IOTHUBCLIENT_LL_02_029: [If parameter handle is NULL then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ if (handle == NULL) { LogError("invalid argument"); result = IOTHUBMESSAGE_ABANDONED; } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)handle; /* Codes_SRS_IOTHUBCLIENT_LL_09_004: [IoTHubClient_LL_GetLastMessageReceiveTime shall return lastMessageReceiveTime in localtime] */ handleData->lastMessageReceiveTime = get_time(NULL); /*Codes_SRS_IOTHUBCLIENT_LL_02_030: [IoTHubClient_LL_MessageCallback shall invoke the last callback function (the parameter messageCallback to IoTHubClient_LL_SetMessageCallback) passing the message and the passed userContextCallback.]*/ if (handleData->messageCallback != NULL) { result = handleData->messageCallback(message, handleData->messageUserContextCallback); } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_032: [If the last callback function was NULL, then IoTHubClient_LL_MessageCallback shall return IOTHUBMESSAGE_ABANDONED.] */ LogError("user callback was NULL"); result = IOTHUBMESSAGE_ABANDONED; } } /*Codes_SRS_IOTHUBCLIENT_LL_02_031: [Then IoTHubClient_LL_MessageCallback shall return what the user function returns.]*/ return result; }
void IoTHubClient_LL_Destroy(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) { /*Codes_SRS_IOTHUBCLIENT_LL_02_009: [IoTHubClient_LL_Destroy shall do nothing if parameter iotHubClientHandle is NULL.]*/ if (iotHubClientHandle != NULL) { PDLIST_ENTRY unsend; /*Codes_SRS_IOTHUBCLIENT_LL_17_010: [IoTHubClient_LL_Destroy shall call the underlaying layer's _Unregister function] */ IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; handleData->IoTHubTransport_Unregister(handleData->deviceHandle); if (handleData->isSharedTransport == false) { /*Codes_SRS_IOTHUBCLIENT_LL_02_010: [If iotHubClientHandle was not created by IoTHubClient_LL_CreateWithTransport, IoTHubClient_LL_Destroy shall call the underlaying layer's _Destroy function.] */ handleData->IoTHubTransport_Destroy(handleData->transportHandle); } /*if any, remove the items currently not send*/ while ((unsend = DList_RemoveHeadList(&(handleData->waitingToSend))) != &(handleData->waitingToSend)) { IOTHUB_MESSAGE_LIST* temp = containingRecord(unsend, IOTHUB_MESSAGE_LIST, entry); /*Codes_SRS_IOTHUBCLIENT_LL_02_033: [Otherwise, IoTHubClient_LL_Destroy shall complete all the event message callbacks that are in the waitingToSend list with the result IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY.] */ if (temp->callback != NULL) { temp->callback(IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY, temp->context); } IoTHubMessage_Destroy(temp->messageHandle); free(temp); } /*Codes_SRS_IOTHUBCLIENT_LL_17_011: [IoTHubClient_LL_Destroy shall free the resources allocated by IoTHubClient (if any).] */ tickcounter_destroy(handleData->tickCounter); IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); free(handleData); } }
IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value) { IOTHUB_CLIENT_RESULT result; /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ if ( (iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL) ) { result = IOTHUB_CLIENT_INVALID_ARG; LogError("invalid argument (NULL)\r\n"); } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; /*Codes_SRS_IOTHUBCLIENT_LL_02_038: [Otherwise, IoTHubClient_LL shall call the function _SetOption of the underlying transport and return what that function is returning.] */ result = handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value); if (result != IOTHUB_CLIENT_OK) { LogError("underlying transport failed, returned = %s\r\n", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); } } return result; }
void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) { /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */ if (iotHubClientHandle != NULL) { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle); } }
IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config) { IOTHUB_CLIENT_LL_HANDLE result; /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClient_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/ if ( (config == NULL) || (config->protocol == NULL) ) { result = NULL; LogError("invalid configuration (NULL detected)\r\n"); } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA)); if (handleData == NULL) { LogError("malloc failed\r\n"); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_004: [Otherwise IoTHubClient_LL_Create shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/ IOTHUBTRANSPORT_CONFIG lowerLayerConfig; DList_InitializeListHead(&(handleData->waitingToSend)); handleData->IoTHubTransport_SetOption = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_SetOption; handleData->IoTHubTransport_Create = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Create; handleData->IoTHubTransport_Destroy = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Destroy; handleData->IoTHubTransport_Subscribe = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Subscribe; handleData->IoTHubTransport_Unsubscribe = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_Unsubscribe; handleData->IoTHubTransport_DoWork = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_DoWork; handleData->IoTHubTransport_GetSendStatus = ((TRANSPORT_PROVIDER*)config->protocol())->IoTHubTransport_GetSendStatus; handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; handleData->lastMessageReceiveTime = INDEFINITE_TIME; /*Codes_SRS_IOTHUBCLIENT_LL_02_006: [IoTHubClient_LL_Create shall populate a structure of type IOTHUBTRANSPORT_CONFIG with the information from config parameter and the previous DLIST and shall pass that to the underlying layer _Create function.]*/ lowerLayerConfig.upperConfig = config; lowerLayerConfig.waitingToSend = &(handleData->waitingToSend); /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClient_LL_Create shall fail and return NULL.] */ if ((handleData->transportHandle = handleData->IoTHubTransport_Create(&lowerLayerConfig)) == NULL) { LogError("underlying transport failed\r\n"); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */ result = handleData; } } } return result; }
void IoTHubClient_LL_DoWork(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) { /*Codes_SRS_IOTHUBCLIENT_LL_02_020: [If parameter iotHubClientHandle is NULL then IoTHubClient_LL_DoWork shall not perform any action.] */ if (iotHubClientHandle != NULL) { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; DoTimeouts(handleData); /*Codes_SRS_IOTHUBCLIENT_LL_02_021: [Otherwise, IoTHubClient_LL_DoWork shall invoke the underlaying layer's _DoWork function.]*/ handleData->IoTHubTransport_DoWork(handleData->transportHandle, iotHubClientHandle); } }
IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value) { IOTHUB_CLIENT_RESULT result; /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ if ( (iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL) ) { result = IOTHUB_CLIENT_INVALID_ARG; LogError("invalid argument (NULL)"); } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/ if (strcmp(optionName, "messageTimeout") == 0) { /*this is an option handled by IoTHubClient_LL*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/ handleData->currentMessageTimeout = *(const uint64_t*)value; result = IOTHUB_CLIENT_OK; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_038: [Otherwise, IoTHubClient_LL shall call the function _SetOption of the underlying transport and return what that function is returning.] */ result = handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value); if (result != IOTHUB_CLIENT_OK) { LogError("underlying transport failed, returned = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); } } } return result; }
IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetMessageCallback(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_MESSAGE_CALLBACK_ASYNC messageCallback, void* userContextCallback) { IOTHUB_CLIENT_RESULT result; /*Codes_SRS_IOTHUBCLIENT_LL_02_016: [IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_INVALID_ARG if parameter iotHubClientHandle is NULL.] */ if (iotHubClientHandle == NULL) { result = IOTHUB_CLIENT_INVALID_ARG; LOG_ERROR; } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; if (messageCallback == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_019: [If parameter messageCallback is NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Unsubscribe function and return IOTHUB_CLIENT_OK.] */ handleData->IoTHubTransport_Unsubscribe(handleData->deviceHandle); handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; result = IOTHUB_CLIENT_OK; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_017: [If parameter messageCallback is non-NULL then IoTHubClient_LL_SetMessageCallback shall call the underlying layer's _Subscribe function.]*/ if (handleData->IoTHubTransport_Subscribe(handleData->deviceHandle) == 0) { handleData->messageCallback = messageCallback; handleData->messageUserContextCallback = userContextCallback; result = IOTHUB_CLIENT_OK; } else { handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; /*Codes_SRS_IOTHUBCLIENT_LL_02_018: [If the underlying layer's _Subscribe function fails, then IoTHubClient_LL_SetMessageCallback shall fail and return IOTHUB_CLIENT_ERROR. Otherwise IoTHubClient_LL_SetMessageCallback shall succeed and return IOTHUB_CLIENT_OK.]*/ result = IOTHUB_CLIENT_ERROR; } } } return result; }
IOTHUB_CLIENT_RESULT IoTHubClient_LL_GetSendStatus(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) { IOTHUB_CLIENT_RESULT result; /* Codes_SRS_IOTHUBCLIENT_09_007: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */ if (iotHubClientHandle == NULL || iotHubClientStatus == NULL) { result = IOTHUB_CLIENT_INVALID_ARG; LOG_ERROR; } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; /* Codes_SRS_IOTHUBCLIENT_09_008: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there is currently no items to be sent] */ /* Codes_SRS_IOTHUBCLIENT_09_009: [IoTHubClient_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently items to be sent] */ result = handleData->IoTHubTransport_GetSendStatus(handleData->deviceHandle, iotHubClientStatus); } return result; }
IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config) { IOTHUB_CLIENT_LL_HANDLE result; /*Codes_SRS_IOTHUBCLIENT_LL_17_001: [IoTHubClient_LL_CreateWithTransport shall return NULL if config parameter is NULL, or protocol field is NULL or transportHandle is NULL.]*/ if ( (config == NULL) || (config->protocol == NULL) || (config->transportHandle == NULL) ) { result = NULL; LogError("invalid configuration (NULL detected)"); } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_002: [IoTHubClient_LL_CreateWithTransport shall allocate data for the IOTHUB_CLIENT_LL_HANDLE.]*/ IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA)); if (handleData == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_17_003: [If allocation fails, the function shall fail and return NULL.] */ LogError("malloc failed"); result = NULL; } else { handleData->uploadToBlobHandle = NULL; /*Codes_SRS_IOTHUBCLIENT_LL_02_047: [ IoTHubClient_LL_CreateWithTransport shall create a TICK_COUNTER_HANDLE. ]*/ if ((handleData->tickCounter = tickcounter_create()) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_048: [ If creating the handle fails, then IoTHubClient_LL_CreateWithTransport shall fail and return NULL ]*/ LogError("unable to get a tickcounter"); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_004: [IoTHubClient_LL_CreateWithTransport shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/ DList_InitializeListHead(&(handleData->waitingToSend)); setTransportProtocol(handleData, (TRANSPORT_PROVIDER*)config->protocol()); handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; handleData->lastMessageReceiveTime = INDEFINITE_TIME; handleData->transportHandle = config->transportHandle; IOTHUB_DEVICE_CONFIG deviceConfig; deviceConfig.deviceId = config->deviceId; deviceConfig.deviceKey = config->deviceKey; deviceConfig.deviceSasToken = config->deviceSasToken; /*Codes_SRS_IOTHUBCLIENT_LL_17_006: [IoTHubClient_LL_CreateWithTransport shall call the transport _Register function with the IOTHUB_DEVICE_CONFIG populated structure and waitingToSend list.]*/ if ((handleData->deviceHandle = handleData->IoTHubTransport_Register(config->transportHandle, &deviceConfig, handleData, &(handleData->waitingToSend))) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_17_007: [If the _Register function fails, this function shall fail and return NULL.]*/ LogError("Registering device in transport failed"); tickcounter_destroy(handleData->tickCounter); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_005: [IoTHubClient_LL_CreateWithTransport shall save the transport handle and mark this transport as shared.]*/ handleData->isSharedTransport = true; /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/ handleData->currentMessageTimeout = 0; result = handleData; } } } } return result; }
IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_Create(const IOTHUB_CLIENT_CONFIG* config) { IOTHUB_CLIENT_LL_HANDLE result; /*Codes_SRS_IOTHUBCLIENT_LL_02_001: [IoTHubClient_LL_Create shall return NULL if config parameter is NULL or protocol field is NULL.]*/ if( (config == NULL) || (config->protocol == NULL) ) { result = NULL; LogError("invalid configuration (NULL detected)"); } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA)); if (handleData == NULL) { LogError("malloc failed"); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_094: [ IoTHubClient_LL_Create shall create a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE from IOTHUB_CLIENT_CONFIG. ]*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_092: [ IoTHubClient_LL_CreateFromConnectionString shall create a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE from IOTHUB_CLIENT_CONFIG. ]*/ handleData->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(config); if (handleData->uploadToBlobHandle == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_093: [ If creating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateFromConnectionString shall fail and return NULL. ]*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_095: [ If creating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_Create shall fail and return NULL. ]*/ LogError("unable to IoTHubClient_LL_UploadToBlob_Create"); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_045: [ Otherwise IoTHubClient_LL_Create shall create a new TICK_COUNTER_HANDLE ]*/ if ((handleData->tickCounter = tickcounter_create()) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_046: [ If creating the TICK_COUNTER_HANDLE fails then IoTHubClient_LL_Create shall fail and return NULL. ]*/ IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); LogError("unable to get a tickcounter"); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_004: [Otherwise IoTHubClient_LL_Create shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/ IOTHUBTRANSPORT_CONFIG lowerLayerConfig; DList_InitializeListHead(&(handleData->waitingToSend)); setTransportProtocol(handleData, (TRANSPORT_PROVIDER*)config->protocol()); handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; handleData->lastMessageReceiveTime = INDEFINITE_TIME; /*Codes_SRS_IOTHUBCLIENT_LL_02_006: [IoTHubClient_LL_Create shall populate a structure of type IOTHUBTRANSPORT_CONFIG with the information from config parameter and the previous DLIST and shall pass that to the underlying layer _Create function.]*/ lowerLayerConfig.upperConfig = config; lowerLayerConfig.waitingToSend = &(handleData->waitingToSend); /*Codes_SRS_IOTHUBCLIENT_LL_02_007: [If the underlaying layer _Create function fails them IoTHubClient_LL_Create shall fail and return NULL.] */ if ((handleData->transportHandle = handleData->IoTHubTransport_Create(&lowerLayerConfig)) == NULL) { LogError("underlying transport failed"); IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); tickcounter_destroy(handleData->tickCounter); free(handleData); result = NULL; } else { IOTHUB_DEVICE_CONFIG deviceConfig; deviceConfig.deviceId = config->deviceId; deviceConfig.deviceKey = config->deviceKey; deviceConfig.deviceSasToken = config->deviceSasToken; /*Codes_SRS_IOTHUBCLIENT_LL_17_008: [IoTHubClient_LL_Create shall call the transport _Register function with a populated structure of type IOTHUB_DEVICE_CONFIG and waitingToSend list.] */ if ((handleData->deviceHandle = handleData->IoTHubTransport_Register(handleData->transportHandle, &deviceConfig, handleData, &(handleData->waitingToSend))) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_17_009: [If the _Register function fails, this function shall fail and return NULL.]*/ LogError("Registering device in transport failed"); handleData->IoTHubTransport_Destroy(handleData->transportHandle); IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); tickcounter_destroy(handleData->tickCounter); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_008: [Otherwise, IoTHubClient_LL_Create shall succeed and return a non-NULL handle.] */ handleData->isSharedTransport = false; /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/ handleData->currentMessageTimeout = 0; result = handleData; } } } } } } return result; }
IOTHUB_CLIENT_RESULT IoTHubClient_LL_SetOption(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const char* optionName, const void* value) { IOTHUB_CLIENT_RESULT result; /*Codes_SRS_IOTHUBCLIENT_LL_02_034: [If iotHubClientHandle is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.]*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_035: [If optionName is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ /*Codes_SRS_IOTHUBCLIENT_LL_02_036: [If value is NULL then IoTHubClient_LL_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ if ( (iotHubClientHandle == NULL) || (optionName == NULL) || (value == NULL) ) { result = IOTHUB_CLIENT_INVALID_ARG; LogError("invalid argument (NULL)"); } else { IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)iotHubClientHandle; /*Codes_SRS_IOTHUBCLIENT_LL_02_039: [ "messageTimeout" - once IoTHubClient_LL_SendEventAsync is called the message shall timeout after value miliseconds. Value is a pointer to a uint64. ]*/ if (strcmp(optionName, "messageTimeout") == 0) { /*this is an option handled by IoTHubClient_LL*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_043: [ Calling IoTHubClient_LL_SetOption with value set to "0" shall disable the timeout mechanism for all new messages. ]*/ handleData->currentMessageTimeout = *(const uint64_t*)value; result = IOTHUB_CLIENT_OK; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_099: [ IoTHubClient_LL_SetOption shall return according to the table below ]*/ IOTHUB_CLIENT_RESULT uploadToBlob_result; #ifndef DONT_USE_UPLOADTOBLOB uploadToBlob_result = IoTHubClient_LL_UploadToBlob_SetOption(handleData->uploadToBlobHandle, optionName, value); if(uploadToBlob_result == IOTHUB_CLIENT_ERROR) { LogError("unable to IoTHubClient_LL_UploadToBlob_SetOption"); result = IOTHUB_CLIENT_ERROR; } #else uploadToBlob_result = IOTHUB_CLIENT_INVALID_ARG; /*harmless value (IOTHUB_CLIENT_INVALID_ARG)in the case when uploadtoblob is not compiled in, otherwise whatever IoTHubClient_LL_UploadToBlob_SetOption returned*/ #endif /*DONT_USE_UPLOADTOBLOB*/ result = /*based on uploadToBlob_result value this is what happens:*/ /*IOTHUB_CLIENT_INVALID_ARG always returns what IoTHubTransport_SetOption returns*/ /*IOTHUB_CLIENT_ERROR always returns IOTHUB_CLIENT_ERROR */ /*IOTHUB_CLIENT_OK returns OK IOTHUB_CLIENT_OK if IoTHubTransport_SetOption returns OK or INVALID_ARG IOTHUB_CLIENT_ERROR if IoTHubTransport_SetOption returns ERROR*/ (uploadToBlob_result == IOTHUB_CLIENT_INVALID_ARG) ? handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) : (uploadToBlob_result == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR : (handleData->IoTHubTransport_SetOption(handleData->transportHandle, optionName, value) == IOTHUB_CLIENT_ERROR) ? IOTHUB_CLIENT_ERROR : IOTHUB_CLIENT_OK; if (result != IOTHUB_CLIENT_OK) { LogError("underlying transport failed, returned = %s", ENUM_TO_STRING(IOTHUB_CLIENT_RESULT, result)); } #ifndef DONT_USE_UPLOADTOBLOB } #endif } return result; }
IOTHUB_CLIENT_LL_HANDLE IoTHubClient_LL_CreateWithTransport(const IOTHUB_CLIENT_DEVICE_CONFIG * config) { IOTHUB_CLIENT_LL_HANDLE result; /*Codes_SRS_IOTHUBCLIENT_LL_17_001: [IoTHubClient_LL_CreateWithTransport shall return NULL if config parameter is NULL, or protocol field is NULL or transportHandle is NULL.]*/ if ( (config == NULL) || (config->protocol == NULL) || (config->transportHandle == NULL) || /*Codes_SRS_IOTHUBCLIENT_LL_02_098: [ IoTHubClient_LL_CreateWithTransport shall fail and return NULL if both config->deviceKey AND config->deviceSasToken are NULL. ]*/ ((config->deviceKey == NULL) && (config->deviceSasToken == NULL)) ) { result = NULL; LogError("invalid configuration (NULL detected)"); } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_002: [IoTHubClient_LL_CreateWithTransport shall allocate data for the IOTHUB_CLIENT_LL_HANDLE.]*/ IOTHUB_CLIENT_LL_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_HANDLE_DATA*)malloc(sizeof(IOTHUB_CLIENT_LL_HANDLE_DATA)); if (handleData == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_17_003: [If allocation fails, the function shall fail and return NULL.] */ LogError("malloc failed"); result = NULL; } else { handleData->transportHandle = config->transportHandle; setTransportProtocol(handleData, (TRANSPORT_PROVIDER*)config->protocol()); #ifndef DONT_USE_UPLOADTOBLOB const char* hostname = STRING_c_str(handleData->IoTHubTransport_GetHostname(handleData->transportHandle)); /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/ /*the first '.' says where the iothubname finishes*/ const char* whereIsDot = strchr(hostname, '.'); if (whereIsDot == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/ LogError("unable to determine the IoTHub name"); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/ char* IoTHubName = malloc(whereIsDot - hostname + 1); if (IoTHubName == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/ LogError("unable to malloc"); free(handleData); result = NULL; } else { const char* IotHubSuffix = whereIsDot + 1; memcpy(IoTHubName, hostname, whereIsDot - hostname); IoTHubName[whereIsDot - hostname ] = '\0'; IOTHUB_CLIENT_CONFIG temp; temp.deviceId = config->deviceId; temp.deviceKey = config->deviceKey; temp.deviceSasToken = config->deviceSasToken; temp.iotHubName = IoTHubName; temp.iotHubSuffix = IotHubSuffix; temp.protocol = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/ temp.protocolGatewayHostName = NULL; /*irrelevant to IoTHubClient_LL_UploadToBlob*/ /*Codes_SRS_IOTHUBCLIENT_LL_02_097: [ If creating the data structures fails or instantiating the IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE fails then IoTHubClient_LL_CreateWithTransport shall fail and return NULL. ]*/ handleData->uploadToBlobHandle = IoTHubClient_LL_UploadToBlob_Create(&temp); if (handleData->uploadToBlobHandle == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_096: [ IoTHubClient_LL_CreateWithTransport shall create the data structures needed to instantiate a IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE. ]*/ LogError("unable to IoTHubClient_LL_UploadToBlob_Create"); free(handleData); result = NULL; } else #endif { /*Codes_SRS_IOTHUBCLIENT_LL_02_047: [ IoTHubClient_LL_CreateWithTransport shall create a TICK_COUNTER_HANDLE. ]*/ if ((handleData->tickCounter = tickcounter_create()) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_02_048: [ If creating the handle fails, then IoTHubClient_LL_CreateWithTransport shall fail and return NULL ]*/ LogError("unable to get a tickcounter"); #ifndef DONT_USE_UPLOADTOBLOB IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); #endif free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_004: [IoTHubClient_LL_CreateWithTransport shall initialize a new DLIST (further called "waitingToSend") containing records with fields of the following types: IOTHUB_MESSAGE_HANDLE, IOTHUB_CLIENT_EVENT_CONFIRMATION_CALLBACK, void*.]*/ DList_InitializeListHead(&(handleData->waitingToSend)); handleData->messageCallback = NULL; handleData->messageUserContextCallback = NULL; handleData->lastMessageReceiveTime = INDEFINITE_TIME; IOTHUB_DEVICE_CONFIG deviceConfig; deviceConfig.deviceId = config->deviceId; deviceConfig.deviceKey = config->deviceKey; deviceConfig.deviceSasToken = config->deviceSasToken; /*Codes_SRS_IOTHUBCLIENT_LL_17_006: [IoTHubClient_LL_CreateWithTransport shall call the transport _Register function with the IOTHUB_DEVICE_CONFIG populated structure and waitingToSend list.]*/ if ((handleData->deviceHandle = handleData->IoTHubTransport_Register(config->transportHandle, &deviceConfig, handleData, &(handleData->waitingToSend))) == NULL) { /*Codes_SRS_IOTHUBCLIENT_LL_17_007: [If the _Register function fails, this function shall fail and return NULL.]*/ LogError("Registering device in transport failed"); #ifndef DONT_USE_UPLOADTOBLOB IoTHubClient_LL_UploadToBlob_Destroy(handleData->uploadToBlobHandle); #endif tickcounter_destroy(handleData->tickCounter); free(handleData); result = NULL; } else { /*Codes_SRS_IOTHUBCLIENT_LL_17_005: [IoTHubClient_LL_CreateWithTransport shall save the transport handle and mark this transport as shared.]*/ handleData->isSharedTransport = true; /*Codes_SRS_IOTHUBCLIENT_LL_02_042: [ By default, messages shall not timeout. ]*/ handleData->currentMessageTimeout = 0; result = handleData; } } } #ifndef DONT_USE_UPLOADTOBLOB free(IoTHubName); } } #endif } } return result; }