Updadate connection string and DPS to work with latest IoT Hub

This commit is contained in:
Mei Wilson 2019-08-26 18:51:20 -07:00
Родитель 2a1f15a2a4 9de174cc18
Коммит 50662dff67
10 изменённых файлов: 59 добавлений и 84 удалений

Просмотреть файл

@ -54,14 +54,15 @@ To try out Azure IoT Plug and Play bridge, follow the steps bellow:
Using Connection string (Note: the symmetic_key must match the SAS key in the connection string):
```JSON
{
"connection_parameters": {
"connection_type" : "connection_string",
"connection_string" : "[CONNECTION STRING]",
"device_capability_model_uri":"[DEVICE CAPABILITY MODEL ID]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[DEVICE KEY from CONNECTION STRING]"
{
"connection_parameters": {
"connection_type" : "connection_string",
"connection_string" : "[CONNECTION STRING]",
"device_capability_model_uri": "[To fill in]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[DEVICE KEY]"
}
}
}
}
@ -70,23 +71,23 @@ To try out Azure IoT Plug and Play bridge, follow the steps bellow:
```JSON
{
"connection_parameters": {
"connection_type" : "dps",
"device_capability_model_uri":"[DEVICE CAPABILITY MODEL ID]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[DEVICE KEY]"
},
"dps_parameters" : {
"global_prov_uri" : "[GLOBAL PROVISIONING URI]",
"id_scope": "[IOT HUB ID SCOPE]",
"device_id": "[DEVICE ID]",
"model_repository_uri": "[To fill in]"
"connection_parameters": {
"connection_type" : "dps",
"device_capability_model_uri": "[To fill in]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[DEVICE KEY]"
},
"dps_parameters" : {
"global_prov_uri" : "[GLOABL PROVISIONING URI]",
"id_scope": "[IOT HUB ID SCOPE]",
"device_id": "[DEVICE ID]"
}
}
}
}
```
> Note: If using Azure IoT Central, the primary connection fields you will need to change in the default config file are id_scope, device_id, symmetric_key, and device_capability_model_uri. Refer to the [Azure IoT Central documentation on device connectivity](https://docs.microsoft.com/en-us/azure/iot-central/concepts-connectivity-pnp?toc=/azure/iot-central-pnp/toc.json&bc=/azure/iot-central-pnp/breadcrumb/toc.json) for how to generate the id_scope, device_id, and symmetric_key for your device. For Azure IoT Central, device_capability_model_uri must match the Device Template ID.
> Note: If using Azure IoT Central, the primary connection fields you will need to change in the default config file are id_scope, device_id, symmetric_key, and device_capability_model_id. Refer to the [Azure IoT Central documentation on device connectivity](https://docs.microsoft.com/en-us/azure/iot-central/concepts-connectivity) for how to generate the id_scope, device_id, and symmetric_key for your device. The device_capability_model_uri is the "Id" that is listed for your device's Device Capability Model in Azure IoT Central.
* Start PnpBridge by running it in a command prompt.

Просмотреть файл

@ -5,7 +5,7 @@
"connection_parameters": {
"connection_type" : "connection_string",
"connection_string" : "[To fill in]",
"device_capability_model_uri": "[To fill in]",
"device_capability_model_uri": "[To fill in]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[To fill in]"
@ -13,8 +13,7 @@
"dps_parameters" : {
"global_prov_uri" : "[To fill in]",
"id_scope": "[To fill in]",
"device_id": "[To fill in]",
"model_repository_uri": "[To fill in]"
"device_id": "[To fill in]"
}
},
"trace_on": false

Просмотреть файл

@ -17,11 +17,6 @@
#include "azure_c_shared_utility/const_defines.h"
// DigitalTwin interface name from service perspective.
static const char DigitalTwinSampleEnvironmentalSensor_InterfaceId[] = "urn:contoso:com:EnvironmentalSensor:1";
static const char DigitalTwinSampleEnvironmentalSensor_ComponentName[] = "environmentalSensor";
//
// Telemetry names for this interface.
//
@ -533,7 +528,7 @@ DIGITALTWIN_INTERFACE_CLIENT_HANDLE DigitalTwinSampleEnvironmentalSensor_CreateI
if ((result = DigitalTwin_InterfaceClient_Create(InterfaceId, ComponentName, DigitalTwinSampleEnvironmentalSensor_InterfaceRegisteredCallback, (void*)&digitaltwinSample_EnvironmentalSensorState, &interfaceHandle)) != DIGITALTWIN_CLIENT_OK)
{
LogError("ENVIRONMENTAL_SENSOR_INTERFACE: Unable to allocate interface client handle for interfaceId=<%s>, componentName=<%s>, error=<%s>", DigitalTwinSampleEnvironmentalSensor_InterfaceId, DigitalTwinSampleEnvironmentalSensor_ComponentName, MU_ENUM_TO_STRING(DIGITALTWIN_CLIENT_RESULT, result));
LogError("ENVIRONMENTAL_SENSOR_INTERFACE: Unable to allocate interface client handle for interfaceId=<%s>, componentName=<%s>, error=<%s>", InterfaceId, ComponentName, MU_ENUM_TO_STRING(DIGITALTWIN_CLIENT_RESULT, result));
interfaceHandle = NULL;
}
else if ((result = DigitalTwin_InterfaceClient_SetPropertiesUpdatedCallback(interfaceHandle, DigitalTwinSampleEnvironmentalSensor_ProcessPropertyUpdate)) != DIGITALTWIN_CLIENT_OK)
@ -550,7 +545,7 @@ DIGITALTWIN_INTERFACE_CLIENT_HANDLE DigitalTwinSampleEnvironmentalSensor_CreateI
}
else
{
LogInfo("ENVIRONMENTAL_SENSOR_INTERFACE: Created DIGITALTWIN_INTERFACE_CLIENT_HANDLE. interfaceId=<%s>, componentName=<%s>, handle=<%p>", DigitalTwinSampleEnvironmentalSensor_InterfaceId, DigitalTwinSampleEnvironmentalSensor_ComponentName, interfaceHandle);
LogInfo("ENVIRONMENTAL_SENSOR_INTERFACE: Created DIGITALTWIN_INTERFACE_CLIENT_HANDLE. interfaceId=<%s>, componentName=<%s>, handle=<%p>", InterfaceId, ComponentName, interfaceHandle);
digitaltwinSample_EnvironmentalSensorState.interfaceClientHandle = interfaceHandle;
}

Просмотреть файл

@ -47,7 +47,7 @@ typedef struct _DPS_PARAMETERS {
const char* GlobalProvUri;
const char* IdScope;
const char* DeviceId;
const char* ModelRepositoryUri;
const char* DcmModelId;
} DPS_PARAMETERS;
// Transport used to connect with IoTHub Device/Module

Просмотреть файл

@ -94,7 +94,6 @@ int Map_GetIndexValueFromKey(MAP_HANDLE handle, const char* key);
#define PNP_CONFIG_CONNECTION_DPS_ID_SCOPE "id_scope"
#define PNP_CONFIG_CONNECTION_DPS_DEVICE_ID "device_id"
#define PNP_CONFIG_CONNECTION_DEVICE_CAPS_MODEL_URI "device_capability_model_uri"
#define PNP_CONFIG_CONNECTION_DPS_MODEL_REPO_URI "model_repository_uri"
#define PNP_CONFIG_CONNECTION_AUTH_PARAMETERS "auth_parameters"
#define PNP_CONFIG_CONNECTION_AUTH_TYPE "auth_type"

Просмотреть файл

@ -5,7 +5,7 @@
"connection_parameters": {
"connection_type" : "connection_string",
"connection_string" : "[To fill in]",
"device_capability_model_uri": "[To fill in]",
"device_capability_model_uri": "[To fill in]",
"auth_parameters" : {
"auth_type" : "symmetric_key",
"symmetric_key" : "[To fill in]"
@ -13,8 +13,7 @@
"dps_parameters" : {
"global_prov_uri" : "[To fill in]",
"id_scope": "[To fill in]",
"device_id": "[To fill in]",
"model_repository_uri": "[To fill in]"
"device_id": "[To fill in]"
}
},
"trace_on": false

Просмотреть файл

@ -98,12 +98,7 @@ PCONNECTION_PARAMETERS PnpBridgeConfig_GetConnectionDetails(JSON_Object* Connect
LEAVE;
}
dpsParams->ModelRepositoryUri = json_object_get_string(dpsSettings, PNP_CONFIG_CONNECTION_DPS_MODEL_REPO_URI);
if (NULL == dpsParams->ModelRepositoryUri) {
LogError("%s is missing in config", PNP_CONFIG_CONNECTION_DPS_MODEL_REPO_URI);
result = PNPBRIDGE_INVALID_ARGS;
LEAVE;
}
dpsParams->DcmModelId = connParams->DeviceCapabilityModelUri;
}
}

Просмотреть файл

@ -44,8 +44,7 @@ static const int pnpSampleDevice_dpsRegistrationMaxPolls = 60;
static const char* pnpSample_CustomProvisioningData = "{ \
\"__iot:interfaces\": \
{ \
\"CapabilityModelUri\": \"%s\", \
\"ModelRepositoryUri\": \"%s\" \
\"CapabilityModelId\": \"%s\" \
} \
}";
#endif // ENABLE_IOT_CENTRAL
@ -91,8 +90,7 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubViaProvisioning(bool TraceOn, PCON
const char* idScope = ConnectionParams->u1.Dps.IdScope;
const char* deviceId = ConnectionParams->u1.Dps.DeviceId;
const char* deviceKey = ConnectionParams->AuthParameters.u1.DeviceKey;
const char* dcmUri = ConnectionParams->DeviceCapabilityModelUri;
const char* ModelUri = ConnectionParams->u1.Dps.ModelRepositoryUri;
const char* dcmModelId = ConnectionParams->u1.Dps.DcmModelId;
SECURE_DEVICE_TYPE secureDeviceTypeForProvisioning;
IOTHUB_SECURITY_TYPE secureDeviceTypeForIotHub;
@ -112,15 +110,15 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubViaProvisioning(bool TraceOn, PCON
LEAVE;
}
size_t customProvDataLength = strlen(pnpSample_CustomProvisioningData) + strlen(dcmUri) + strlen(ModelUri) + 1;
size_t customProvDataLength = strlen(pnpSample_CustomProvisioningData) + strlen(dcmModelId) + 1;
customProvisioningData = calloc(1, customProvDataLength * sizeof(char));
if (NULL == customProvisioningData) {
LogError("Failed to allocate memory fo the customProvisiongData.");
LEAVE;
}
if (-1 == sprintf_s(customProvisioningData, customProvDataLength, pnpSample_CustomProvisioningData, dcmUri, ModelUri)) {
LogError("Failed to create the customProvisiongData. URI's is too long.");
if (-1 == sprintf_s(customProvisioningData, customProvDataLength, pnpSample_CustomProvisioningData, dcmModelId)) {
LogError("Failed to create the customProvisiongData. DcmModelId is too long.");
}
else if (IoTHub_Init() != 0) {
LogError("IoTHub_Init failed");
@ -175,7 +173,6 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubViaProvisioning(bool TraceOn, PCON
{
// The initial provisioning stage has succeeded. Now use this material to connect to IoTHub.
IOTHUB_CLIENT_RESULT iothubClientResult;
bool urlEncodeOn = true;
if (iothub_security_init(secureDeviceTypeForIotHub) != 0)
{
@ -191,15 +188,6 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubViaProvisioning(bool TraceOn, PCON
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
// Setting OPTION_AUTO_URL_ENCODE_DECODE is required by callers only for private-preview.
// https://github.com/Azure/azure-iot-sdk-c-pnp/issues/2 tracks making underlying PnP set this automatically.
else if ((iothubClientResult = IoTHubDeviceClient_SetOption(deviceHandle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn)) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option %s, error=%d", OPTION_AUTO_URL_ENCODE_DECODE, iothubClientResult);
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
else
{
// Never log the complete connection string , as this contains information
@ -255,8 +243,6 @@ IotComms_PnPInterfaceRegisteredCallback(
Unlock(registrationContext->Lock);
}
#define DIGITALTWIN_SAMPLE_DEVICE_CAPABILITY_MODEL_URI "http://azureiot.com/TestDeviceCapabilityModule/1.0.0"
// Invokes PnP_DeviceClient_RegisterInterfacesAsync, which indicates to Azure IoT which PnP interfaces this device supports.
// The PnP Handle *is not valid* until this operation has completed (as indicated by the callback appPnpInterfacesRegistered being invoked).
// In this sample, we block indefinitely but production code should include a timeout.
@ -325,7 +311,6 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubDeviceHandle(bool TraceOn, const c
{
IOTHUB_DEVICE_HANDLE deviceHandle = NULL;
IOTHUB_CLIENT_RESULT iothubClientResult;
bool urlEncodeOn = true;
// TODO: PnP SDK should auto-set OPTION_AUTO_URL_ENCODE_DECODE for MQTT as its strictly required. Need way for IoTHub handle to communicate this back.
if (IoTHub_Init() != 0) {
@ -342,12 +327,6 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubDeviceHandle(bool TraceOn, const c
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
else if ((iothubClientResult = IoTHubDeviceClient_SetOption(deviceHandle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn)) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option %s, error=%d\n", OPTION_AUTO_URL_ENCODE_DECODE, iothubClientResult);
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
if (deviceHandle == NULL)
{
@ -365,15 +344,25 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotDevice(bool TraceOn, PCONNECTION_PARA
if (ConnectionParams->AuthParameters.AuthType == AUTH_TYPE_SYMMETRIC_KEY) {
IOTHUB_DEVICE_HANDLE handle = NULL;
char* connectionString;
char* format = "%s;SharedAccessKey=%s";
connectionString = (char*) malloc(strlen(ConnectionParams->AuthParameters.u1.DeviceKey) +
strlen(ConnectionParams->u1.ConnectionString) + strlen(format) + 1);
sprintf(connectionString, format, ConnectionParams->u1.ConnectionString, ConnectionParams->AuthParameters.u1.DeviceKey);
char* format = NULL;
if (NULL != strstr(ConnectionParams->u1.ConnectionString, "SharedAccessKey=")) {
LogInfo("WARNING: SharedAccessKey is included in connection string. Ignoring "
PNP_CONFIG_CONNECTION_AUTH_TYPE_DEVICE_SYMM_KEY " in config file.");
connectionString = (char*) ConnectionParams->u1.ConnectionString;
}
else {
format = "%s;SharedAccessKey=%s";
connectionString = (char*) malloc(strlen(ConnectionParams->AuthParameters.u1.DeviceKey) +
strlen(ConnectionParams->u1.ConnectionString) + strlen(format) + 1);
sprintf(connectionString, format, ConnectionParams->u1.ConnectionString, ConnectionParams->AuthParameters.u1.DeviceKey);
}
handle = IotComms_InitializeIotHubDeviceHandle(TraceOn, connectionString);
free(connectionString);
if (NULL != format) {
free(connectionString);
}
return handle;
}

Просмотреть файл

@ -30,6 +30,7 @@ PnpBridge_Initialize(
PNPBRIDGE_RESULT result = PNPBRIDGE_OK;
PPNP_BRIDGE pbridge = NULL;
JSON_Value* config = NULL;
bool lockAcquired = false;
TRY {
g_PnpBridgeState = PNP_BRIDGE_UNINITIALIZED;
@ -56,11 +57,12 @@ PnpBridge_Initialize(
LEAVE;
}
Lock(pbridge->ExitLock);
lockAcquired = true;
// Get the JSON VALUE of configuration file
result = PnpBridgeConfig_GetJsonValueFromConfigFile("config.json", &config);
if (PNPBRIDGE_OK != result) {
LogError("Failed to retrieve the configuration.");
LogError("Failed to retrieve the bridge configuration from config.json file.");
LEAVE;
}
@ -90,6 +92,9 @@ PnpBridge_Initialize(
FINALLY
{
if (PNPBRIDGE_OK != result) {
if (lockAcquired) {
Unlock(pbridge->ExitLock);
}
PnpBridge_Release(pbridge);
*PnpBridge = NULL;
}

Просмотреть файл

@ -126,19 +126,12 @@
},
"device_id": {
"type": "string"
},
"device_capability_model_uri": {
"type": "string"
},
"model_repository_uri": {
"type": "string"
}
},
"required": [
"global_prov_uri",
"id_scope",
"device_id",
"device_capability_model_uri",
"model_repository_uri"
]
},