/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { ASSERT_FAIL("unable to IoTHubMessage_GetByteArray"); } else { /*buffer is not zero terminated*/ char* buffer_string = (char*)malloc(size+1); ASSERT_IS_NOT_NULL(buffer_string); if (memcpy(buffer_string, buffer, size) == 0) { ASSERT_FAIL("memcpy failed for buffer"); } else { buffer_string[size] = '\0'; EXECUTE_COMMAND(userContextCallback, buffer_string); } free(buffer_string); } return IOTHUBMESSAGE_ACCEPTED; }
bool IsPointOnMyFlank(EntityId actorEntityID, const Vec3& actorPos, const Vec3& targetPos, const Vec3& pointPos) { CAIBattleFrontGroup* group = gGameAIEnv.battleFrontModule->GetGroupForEntity(actorEntityID); ASSERT_IS_NOT_NULL(group); if (!group || group->GetMemberCount() == 1) return true; // I'm alone so this is my flank const Vec3& groupAveragePos = group->GetAveragePosition(); // Set up a coordinate system where Y corresponds to the direction // from the battle group's center position towards the target Vec3 Y = (targetPos - groupAveragePos); Vec3 X = Vec3(0,0,-1).Cross(Y); // Calculate flank side sign Vec3 vGroupAveragePosToActor = actorPos - groupAveragePos; float flankSideSign = (vGroupAveragePosToActor.Dot(X) > 0.0f) ? 1.0f : -1.0f; // Flank side sign (-1 for left, +1 for right) // Calculate how far the point is on the X-axis Vec3 groupAveragePosToPoint = pointPos - groupAveragePos; float pointDistOnXAxis = groupAveragePosToPoint.Dot(X); bool pointIsOnMyFlank = pointDistOnXAxis * flankSideSign > 0.0f; return pointIsOnMyFlank; }
static PROV_DEVICE_LL_HANDLE create_dps_handle(PROV_DEVICE_TRANSPORT_PROVIDER_FUNCTION dps_transport) { PROV_DEVICE_LL_HANDLE result; result = Prov_Device_LL_Create(g_dps_uri, g_dps_scope_id, dps_transport); ASSERT_IS_NOT_NULL(result, "Failure create a DPS HANDLE"); return result; }
static void create_x509_enrollment_device() { INDIVIDUAL_ENROLLMENT_HANDLE indiv_enrollment = NULL; PROVISIONING_SERVICE_CLIENT_HANDLE prov_sc_handle = prov_sc_create_from_connection_string(g_prov_conn_string); ASSERT_IS_NOT_NULL(prov_sc_handle, "Failure creating provisioning service client"); prov_sc_set_trace(prov_sc_handle, TRACING_STATUS_ON); PROV_AUTH_HANDLE auth_handle = prov_auth_create(); ASSERT_IS_NOT_NULL(auth_handle, "Failure creating auth client"); char* registration_id = prov_auth_get_registration_id(auth_handle); ASSERT_IS_NOT_NULL(registration_id, "Failure prov_auth_get_common_name"); if (prov_sc_get_individual_enrollment(prov_sc_handle, registration_id, &indiv_enrollment) != 0) { char* x509_cert = prov_auth_get_certificate(auth_handle); ASSERT_IS_NOT_NULL(x509_cert, "Failure prov_auth_get_certificate"); STRING_HANDLE base64_cert = Azure_Base64_Encode_Bytes((const unsigned char*)x509_cert, strlen(x509_cert)); ASSERT_IS_NOT_NULL(base64_cert, "Failure Azure_Base64_Encode_Bytes"); ATTESTATION_MECHANISM_HANDLE attest_handle = attestationMechanism_createWithX509ClientCert(STRING_c_str(base64_cert), NULL); ASSERT_IS_NOT_NULL(attest_handle, "Failure hsm_client_riot_get_certificate "); indiv_enrollment = individualEnrollment_create(registration_id, attest_handle); ASSERT_IS_NOT_NULL(indiv_enrollment, "Failure hsm_client_riot_get_certificate "); ASSERT_ARE_EQUAL(int, 0, prov_sc_create_or_update_individual_enrollment(prov_sc_handle, &indiv_enrollment), "Failure prov_sc_create_or_update_individual_enrollment"); STRING_delete(base64_cert); free(x509_cert); }
static IOTHUB_CLIENT_RESULT my_IoTHubClient_SendReportedState(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* reportedState, size_t size, IOTHUB_CLIENT_REPORTED_STATE_CALLBACK reportedStateCallback, void* userContextCallback) { (void)(iotHubClientHandle); g_IoTHubClient_SendReportedState_reportedState = (char*)malloc(size+1); ASSERT_IS_NOT_NULL(g_IoTHubClient_SendReportedState_reportedState); (void)memcpy(g_IoTHubClient_SendReportedState_reportedState, reportedState, size); g_IoTHubClient_SendReportedState_reportedState[size] = '\0'; reportedStateCallback(201, userContextCallback); return IOTHUB_CLIENT_OK; }
/*typically "left" is the output of SERIALIZE... and right is hand-coded JSON*/ static bool areTwoJsonsEqual(const unsigned char* left, size_t leftSize, const char* right) { bool result; char* cloneOfLeft = (char*)malloc(leftSize + 1); /*because of out SERIALIZE... there is a byte array that is NOT '\0' terminated*/ ASSERT_IS_NOT_NULL(cloneOfLeft); (void)memcpy(cloneOfLeft, left, leftSize); cloneOfLeft[leftSize] = '\0'; JSON_Value* actualJson = json_parse_string((char*)cloneOfLeft); ASSERT_IS_NOT_NULL(actualJson); JSON_Value* expectedJson = json_parse_string(right); ASSERT_IS_NOT_NULL(expectedJson); result = (json_value_equals(expectedJson, actualJson) != 0); json_value_free(expectedJson); json_value_free(actualJson); free(cloneOfLeft); return result; }
/*this function "links" IoTHub to the serialization library*/ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { const unsigned char* buffer; size_t size; if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) { ASSERT_FAIL("unable to IoTHubMessage_GetByteArray"); } else { /*buffer is not zero terminated*/ STRING_HANDLE temp = STRING_construct_n((char*)buffer, size); ASSERT_IS_NOT_NULL(temp); EXECUTE_COMMAND(userContextCallback, STRING_c_str(temp)); STRING_delete(temp); } return IOTHUBMESSAGE_ACCEPTED; }
void create_tpm_enrollment_device(const char* prov_conn_string, bool use_tracing) { INDIVIDUAL_ENROLLMENT_HANDLE indiv_enrollment = NULL; PROVISIONING_SERVICE_CLIENT_HANDLE prov_sc_handle = prov_sc_create_from_connection_string(prov_conn_string); ASSERT_IS_NOT_NULL(prov_sc_handle, "Failure creating provisioning service client"); if (use_tracing) { prov_sc_set_trace(prov_sc_handle, TRACING_STATUS_ON); } #ifdef SET_TRUSTED_CERT_IN_SAMPLES ASSERT_ARE_EQUAL(PROV_DEVICE_RESULT, PROV_DEVICE_RESULT_OK, prov_sc_set_certificate(prov_sc_handle, certificates), "Failure setting Trusted Cert option"); #endif // SET_TRUSTED_CERT_IN_SAMPLES PROV_AUTH_HANDLE auth_handle = prov_auth_create(); ASSERT_IS_NOT_NULL(auth_handle, "Failure creating auth client"); char* registration_id = prov_auth_get_registration_id(auth_handle); ASSERT_IS_NOT_NULL(registration_id, "Failure prov_auth_get_common_name"); if (prov_sc_get_individual_enrollment(prov_sc_handle, registration_id, &indiv_enrollment) != 0) { BUFFER_HANDLE ek_handle = prov_auth_get_endorsement_key(auth_handle); ASSERT_IS_NOT_NULL(ek_handle, "Failure prov_auth_get_endorsement_key"); STRING_HANDLE ek_value = Azure_Base64_Encode(ek_handle); ASSERT_IS_NOT_NULL(ek_value, "Failure Base64_Encode Endorsement key"); ATTESTATION_MECHANISM_HANDLE attest_handle = attestationMechanism_createWithTpm(STRING_c_str(ek_value), NULL); ASSERT_IS_NOT_NULL(attest_handle, "Failure attestationMechanism_createWithTpm"); indiv_enrollment = individualEnrollment_create(registration_id, attest_handle); ASSERT_IS_NOT_NULL(indiv_enrollment, "Failure hsm_client_riot_get_certificate "); ASSERT_ARE_EQUAL(int, 0, prov_sc_create_or_update_individual_enrollment(prov_sc_handle, &indiv_enrollment), "Failure prov_sc_create_or_update_individual_enrollment"); BUFFER_delete(ek_handle); STRING_delete(ek_value); }
static void create_tpm_enrollment_device() { INDIVIDUAL_ENROLLMENT_HANDLE indiv_enrollment = NULL; PROVISIONING_SERVICE_CLIENT_HANDLE prov_sc_handle = prov_sc_create_from_connection_string(g_prov_conn_string); ASSERT_IS_NOT_NULL(prov_sc_handle, "Failure creating provisioning service client"); prov_sc_set_trace(prov_sc_handle, TRACING_STATUS_ON); PROV_AUTH_HANDLE auth_handle = prov_auth_create(); ASSERT_IS_NOT_NULL(auth_handle, "Failure creating auth client"); char* registration_id = prov_auth_get_registration_id(auth_handle); ASSERT_IS_NOT_NULL(registration_id, "Failure prov_auth_get_common_name"); if (prov_sc_get_individual_enrollment(prov_sc_handle, registration_id, &indiv_enrollment) != 0) { BUFFER_HANDLE ek_handle = prov_auth_get_endorsement_key(auth_handle); ASSERT_IS_NOT_NULL(ek_handle, "Failure prov_auth_get_endorsement_key"); STRING_HANDLE ek_value = Base64_Encode(ek_handle); ASSERT_IS_NOT_NULL(ek_value, "Failure Base64_Encode Endorsement key"); ATTESTATION_MECHANISM_HANDLE attest_handle = attestationMechanism_createWithTpm(STRING_c_str(ek_value), NULL); ASSERT_IS_NOT_NULL(attest_handle, "Failure attestationMechanism_createWithTpm"); indiv_enrollment = individualEnrollment_create(registration_id, attest_handle); ASSERT_IS_NOT_NULL(indiv_enrollment, "Failure hsm_client_riot_get_certificate "); BUFFER_delete(ek_handle); STRING_delete(ek_value); } free(registration_id); individualEnrollment_destroy(indiv_enrollment); prov_auth_destroy(auth_handle); prov_sc_destroy(prov_sc_handle); }
static void dps_registation_status(PROV_DEVICE_REG_STATUS reg_status, void* user_context) { (void)reg_status; ASSERT_IS_NOT_NULL(user_context, "user_context is NULL"); ThreadAPI_Sleep(500); }
static void RecvMessage(IOTHUB_PROVISIONED_DEVICE* deviceToUse) { // arrange IOTHUB_CLIENT_HANDLE iotHubClientHandle; IOTHUB_SERVICE_CLIENT_AUTH_HANDLE iotHubServiceClientHandle; IOTHUB_MESSAGING_CLIENT_HANDLE iotHubMessagingHandle; IOTHUB_MESSAGING_RESULT iotHubMessagingResult; IOTHUB_MESSAGE_RESULT iotHubMessageResult; EXPECTED_RECEIVE_DATA* receiveUserContext; IOTHUB_MESSAGE_HANDLE messageHandle; // act IoTHub_Init(); // Create Service Client iotHubServiceClientHandle = IoTHubServiceClientAuth_CreateFromConnectionString(IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo1)); ASSERT_IS_NOT_NULL(iotHubServiceClientHandle, "Could not initialize IoTHubServiceClient to send C2D messages to the device"); iotHubMessagingHandle = IoTHubMessaging_Create(iotHubServiceClientHandle); ASSERT_IS_NOT_NULL(iotHubMessagingHandle, "Could not initialize IoTHubMessaging to send C2D messages to the device"); iotHubMessagingResult = IoTHubMessaging_Open(iotHubMessagingHandle, openCompleteCallback, (void*)"Context string for open"); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessagingResult); // Create user context and message receiveUserContext = ReceiveUserContext_Create(); ASSERT_IS_NOT_NULL(receiveUserContext, "Could not create receive user context"); messageHandle = IoTHubMessage_CreateFromString(MSG_CONTENT1); ASSERT_IS_NOT_NULL(messageHandle, "Could not create IoTHubMessage to send C2D messages to the device"); iotHubMessageResult = IoTHubMessage_SetMessageId(messageHandle, MSG_ID1); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessageResult); iotHubMessageResult = IoTHubMessage_SetCorrelationId(messageHandle, MSG_CORRELATION_ID1); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessageResult); MAP_HANDLE mapHandle = IoTHubMessage_Properties(messageHandle); for (size_t i = 0; i < MSG_PROP_COUNT; i++) { if (Map_AddOrUpdate(mapHandle, MSG_PROP_KEYS[i], MSG_PROP_VALS[i]) != MAP_OK) { (void)printf("ERROR: Map_AddOrUpdate failed for property %zu!\r\n", i); } } iotHubMessagingResult = IoTHubMessaging_SendAsync(iotHubMessagingHandle, deviceToUse->deviceId, messageHandle, sendCompleteCallback, receiveUserContext); ASSERT_ARE_EQUAL(int, IOTHUB_MESSAGING_OK, iotHubMessagingResult, "IoTHubMessaging_SendAsync failed, could not send C2D message to the device"); iotHubClientHandle = IoTHubClient_CreateFromConnectionString(deviceToUse->connectionString, HTTP_Protocol); ASSERT_IS_NOT_NULL(iotHubClientHandle, "Failure creating Iothub Client"); if (deviceToUse->howToCreate == IOTHUB_ACCOUNT_AUTH_X509) { IOTHUB_CLIENT_RESULT result; result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_CERT, deviceToUse->certificate); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 certificate"); result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_PRIVATE_KEY, deviceToUse->primaryAuthentication); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 privateKey"); } IOTHUB_CLIENT_RESULT result = IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, receiveUserContext); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Failure setting message callback"); unsigned int minimumPollingTime = 1; /*because it should not wait*/ if (IoTHubClient_SetOption(iotHubClientHandle, OPTION_MIN_POLLING_TIME, &minimumPollingTime) != IOTHUB_CLIENT_OK) { printf("failure to set option \"MinimumPollingTime\"\r\n"); } time_t beginOperation, nowTime; beginOperation = time(NULL); while ( ( (nowTime = time(NULL)), (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME) //time box ) ) { if (Lock(receiveUserContext->lock) != LOCK_OK) { ASSERT_FAIL("unable ot lock"); } else { if (receiveUserContext->wasFound) { (void)Unlock(receiveUserContext->lock); break; } (void)Unlock(receiveUserContext->lock); } ThreadAPI_Sleep(100); } // assert ASSERT_IS_TRUE(receiveUserContext->wasFound, "Failure retrieving message that was sent to IotHub."); // was found is written by the callback... // cleanup IoTHubMessage_Destroy(messageHandle); IoTHubMessaging_Close(iotHubMessagingHandle); IoTHubMessaging_Destroy(iotHubMessagingHandle); IoTHubServiceClientAuth_Destroy(iotHubServiceClientHandle); IoTHubClient_Destroy(iotHubClientHandle); ReceiveUserContext_Destroy(receiveUserContext); }
static void SendEvent(IOTHUB_PROVISIONED_DEVICE* deviceToUse) { // arrange IOTHUB_CLIENT_HANDLE iotHubClientHandle; IOTHUB_MESSAGE_HANDLE msgHandle; EXPECTED_SEND_DATA* sendData = EventData_Create(); ASSERT_IS_NOT_NULL(sendData, "Failure creating data to be sent"); // Send the Event { IOTHUB_CLIENT_RESULT result; // Create the IoT Hub Data iotHubClientHandle = IoTHubClient_CreateFromConnectionString(deviceToUse->connectionString, HTTP_Protocol); ASSERT_IS_NOT_NULL(iotHubClientHandle, "Failure creating IothubClient handle"); msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)sendData->expectedString, strlen(sendData->expectedString)); ASSERT_IS_NOT_NULL(msgHandle, "Failure to create message handle"); if (deviceToUse->howToCreate == IOTHUB_ACCOUNT_AUTH_X509) { result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_CERT, deviceToUse->certificate); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 certificate"); result = IoTHubClient_SetOption(iotHubClientHandle, OPTION_X509_PRIVATE_KEY, deviceToUse->primaryAuthentication); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Could not set the device x509 privateKey"); } // act result = IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, ReceiveConfirmationCallback, sendData); ASSERT_ARE_EQUAL(IOTHUB_CLIENT_RESULT, IOTHUB_CLIENT_OK, result, "Failure calling IoTHubClient_SendEventAsync"); } time_t beginOperation, nowTime; beginOperation = time(NULL); while ( (nowTime = time(NULL)), (difftime(nowTime, beginOperation) < MAX_CLOUD_TRAVEL_TIME) // time box ) { if (Lock(sendData->lock) != LOCK_OK) { ASSERT_FAIL("unable to lock"); } else { if (sendData->dataWasRecv) { Unlock(sendData->lock); break; } Unlock(sendData->lock); } ThreadAPI_Sleep(100); } if (Lock(sendData->lock) != LOCK_OK) { ASSERT_FAIL("unable to lock"); } else { ASSERT_IS_TRUE(sendData->dataWasRecv, "Failure sending data to IotHub"); // was found is written by the callback... (void)Unlock(sendData->lock); } { IOTHUB_TEST_HANDLE iotHubTestHandle = IoTHubTest_Initialize(IoTHubAccount_GetEventHubConnectionString(g_iothubAcctInfo1), IoTHubAccount_GetIoTHubConnString(g_iothubAcctInfo1), deviceToUse->deviceId, IoTHubAccount_GetEventhubListenName(g_iothubAcctInfo1), IoTHubAccount_GetEventhubAccessKey(g_iothubAcctInfo1), IoTHubAccount_GetSharedAccessSignature(g_iothubAcctInfo1), IoTHubAccount_GetEventhubConsumerGroup(g_iothubAcctInfo1)); ASSERT_IS_NOT_NULL(iotHubTestHandle); IOTHUB_TEST_CLIENT_RESULT result = IoTHubTest_ListenForEventForMaxDrainTime(iotHubTestHandle, IoTHubCallback, IoTHubAccount_GetIoTHubPartitionCount(g_iothubAcctInfo1), sendData); ASSERT_ARE_EQUAL(IOTHUB_TEST_CLIENT_RESULT, IOTHUB_TEST_CLIENT_OK, result); IoTHubTest_Deinit(iotHubTestHandle); } // assert ASSERT_IS_TRUE(sendData->wasFound, "Failure receiving data from eventhub"); // was found is written by the callback...*/ // cleanup IoTHubMessage_Destroy(msgHandle); IoTHubClient_Destroy(iotHubClientHandle); EventData_Destroy(sendData); }