Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/rsu-connector: add status iothub status file output #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rsu-connector/AzureSDKWrapper/CustomHSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class CustomHsm
/// @param dpsKey x509 Device Certificate in PEM format.
bool setKey( const std::vector<char>& dpsKey );
};
#endif // DEVICECONNECTOR_AZURESDKWRAPPER_CUSTOMHSM_H_
#endif // DEVICECONNECTOR_AZURESDKWRAPPER_CUSTOMHSM_H_
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ struct IMessageLifeTimeTracker : private NonCopyable<IMessageLifeTimeTracker>
virtual DeliveryState State() = 0;
};

#endif // DEVICECONNECTOR_AZURESDKWRAPPER_IMESSAGELIFETIMETRACKER_H_
#endif // DEVICECONNECTOR_AZURESDKWRAPPER_IMESSAGELIFETIMETRACKER_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/AzureSDKWrapper/IProvisioningClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ struct IProvisioningClient : private NonCopyable<IProvisioningClient>
virtual const std::string& IotHubUri() const = 0;
};

#endif // CONNECTOR_AZURESDKWRAPPER_IPROVISIONINGCLIENT_H_
#endif // CONNECTOR_AZURESDKWRAPPER_IPROVISIONINGCLIENT_H_
67 changes: 59 additions & 8 deletions src/rsu-connector/AzureSDKWrapper/IotHubClientWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <map>
#include <functional>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <mutex>
Expand Down Expand Up @@ -58,8 +59,8 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT_TAG ReactionToDisposition( MessageReacti

struct IotHubClientWrapper::IotHubClientWrapperImpl
{
IotHubClientWrapperImpl( const std::string& iotHubUri, const std::string& deviceId );
IotHubClientWrapperImpl( const std::string& connectionString );
IotHubClientWrapperImpl( const std::string& iotHubUri, const std::string& deviceId, const std::string& statusFile);
IotHubClientWrapperImpl( const std::string& connectionString, const std::string& statusFile);
~IotHubClientWrapperImpl();

void SetLogTraceOption( bool value );
Expand All @@ -83,6 +84,7 @@ struct IotHubClientWrapper::IotHubClientWrapperImpl
return MessageReaction::Abandoned;
} };

std::string statusFileName;
static void sStatusCallback( IOTHUB_CLIENT_CONNECTION_STATUS result,
IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason,
void* userContextCallback )
Expand All @@ -92,6 +94,50 @@ struct IotHubClientWrapper::IotHubClientWrapperImpl
spdlog::error( "Status callback with NULL user context." );
return;
}

IotHubClientWrapperImpl* ctx = static_cast<IotHubClientWrapperImpl*>( userContextCallback );

fstream status;
status.open( ctx->statusFileName, ios::out );
if ( !status )
{
spdlog::warn( "Status file cannot be created" );
}
else
{
switch ( reason )
{
case IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN:
status << "{ \"status\": \"EXPIRED_SAS_TOKEN\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_DEVICE_DISABLED:
status << "{ \"status\": \"DEVICE_DISABLED\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL:
status << "{ \"status\": \"BAD_CREDENTIAL\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_RETRY_EXPIRED:
status << "{ \"status\": \"RETRY_EXPIRED\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_NO_NETWORK:
status << "{ \"status\": \"NO_NETWORK\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR:
status << "{ \"status\": \"COMMUNICATION_ERROR\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_OK:
status << "{ \"status\": \"OK\" }\n";
break;
case IOTHUB_CLIENT_CONNECTION_NO_PING_RESPONSE:
status << "{ \"status\": \"NO_PING_RESPONSE\" }\n";
break;
default:
status << "{ \"status\": \"UNKNOWN\" }\n";
break;
}
status.close();
}

spdlog::info( "Received status {} reason {}", result, reason );
}

Expand Down Expand Up @@ -139,13 +185,16 @@ std::mutex IotHubClientWrapper::IotHubClientWrapperImpl::SendMessageLock;
std::vector<std::shared_ptr<IMessageLifeTimeTracker>> IotHubClientWrapper::IotHubClientWrapperImpl::SendMessageTrackers;

IotHubClientWrapper::IotHubClientWrapperImpl::IotHubClientWrapperImpl( const std::string& iotHubUri,
const std::string& deviceId )
const std::string& deviceId,
const std::string& statusFile )
{
if ( iotHubUri.empty() || deviceId.empty() )
{
throw std::invalid_argument( "Connection String" );
}

statusFileName = statusFile;

IotHubClientHandle = IoTHubDeviceClient_CreateFromDeviceAuth( iotHubUri.c_str(), deviceId.c_str(), MQTT_Protocol );
if ( !IotHubClientHandle )
{
Expand All @@ -156,13 +205,15 @@ IotHubClientWrapper::IotHubClientWrapperImpl::IotHubClientWrapperImpl( const std
spdlog::debug( "IotHubClientWrapper" );
}

IotHubClientWrapper::IotHubClientWrapperImpl::IotHubClientWrapperImpl( const std::string& connectionString )
IotHubClientWrapper::IotHubClientWrapperImpl::IotHubClientWrapperImpl( const std::string& connectionString, const std::string& statusFile)
{
if ( connectionString.empty() )
{
throw std::invalid_argument( "Connection String" );
}

statusFileName = statusFile;

IotHubClientHandle = IoTHubDeviceClient_CreateFromConnectionString( connectionString.c_str(), MQTT_Protocol );
if ( !IotHubClientHandle )
{
Expand Down Expand Up @@ -528,14 +579,14 @@ void IotHubClientWrapper::IotHubClientWrapperImpl::sReportedStateCallback( int s
spdlog::info( "Send reported state response {}", statusCode );
}

IotHubClientWrapper::IotHubClientWrapper( const std::string& iotHubUri, const std::string& deviceId )
: _impl( std::make_shared<IotHubClientWrapperImpl>( iotHubUri, deviceId ) )
IotHubClientWrapper::IotHubClientWrapper( const std::string& iotHubUri, const std::string& deviceId, const std::string& statusFile )
: _impl( std::make_shared<IotHubClientWrapperImpl>( iotHubUri, deviceId, statusFile) )
{
spdlog::debug( "Created IotHub client." );
}

IotHubClientWrapper::IotHubClientWrapper( const std::string& connectionString )
: _impl( std::make_shared<IotHubClientWrapperImpl>( connectionString ) )
IotHubClientWrapper::IotHubClientWrapper( const std::string& connectionString, const std::string& statusFile )
: _impl( std::make_shared<IotHubClientWrapperImpl>( connectionString, statusFile ) )
{
spdlog::debug( "Created IotHub client." );
}
Expand Down
6 changes: 4 additions & 2 deletions src/rsu-connector/AzureSDKWrapper/IotHubClientWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class IotHubClientWrapper : public IIotHubClient, private NonCopyable<IotHubClie
/// @brief Construct a Iot Hub Client from device authentication.
/// @param iotHubUri witch receive from @ref ProvisioningClientWrapper.
/// @param deviceId witch receive from @ref ProvisioningClientWrapper.
IotHubClientWrapper( const std::string& iotHubUri, const std::string& deviceId );
/// @param statusFile specifies where the connection status should be saved.
IotHubClientWrapper( const std::string& iotHubUri, const std::string& deviceId, const std::string& statusFile );

/// @brief Construct a new Iot Hub Client by connectionString.
/// @param connectionString of IoT Hub
IotHubClientWrapper( const std::string& connectionString );
/// @param statusFile specifies where the connection status should be saved.
IotHubClientWrapper( const std::string& connectionString, const std::string& statusFile );

/// @brief Destroy the Iot Hub Client Wrapper object.
virtual ~IotHubClientWrapper() = default;
Expand Down
9 changes: 5 additions & 4 deletions src/rsu-connector/AzureSDKWrapper/IotHubFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ std::shared_ptr<IProvisioningClient> IotHubFactory::ProvisioningClient( const st
}

std::shared_ptr<IIotHubClient> IotHubFactory::IotHubClient( const std::string& iotHubUri,
const std::string& deviceId ) const
const std::string& deviceId,
const std::string& statusFile ) const
{
return std::make_shared<IotHubClientWrapper>( iotHubUri, deviceId );
return std::make_shared<IotHubClientWrapper>( iotHubUri, deviceId, statusFile );
}

std::shared_ptr<IIotHubClient> IotHubFactory::IotHubClient( const std::string& connectionString ) const
std::shared_ptr<IIotHubClient> IotHubFactory::IotHubClient( const std::string& connectionString, const std::string& statusFile ) const
{
return std::make_shared<IotHubClientWrapper>( connectionString );
return std::make_shared<IotHubClientWrapper>( connectionString, statusFile );
}
6 changes: 3 additions & 3 deletions src/rsu-connector/AzureSDKWrapper/IotHubFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class IotHubFactory
/// @brief Construct a IotHub Client from IotHub URI and DeviceId as returned from the ProvisioningClient.
/// @param iotHubUri The URI of the IotHub instance.
/// @param deviceId The device id to be used on this IotHub..
std::shared_ptr<IIotHubClient> IotHubClient( const std::string& iotHubUri, const std::string& deviceId ) const;
std::shared_ptr<IIotHubClient> IotHubClient( const std::string& iotHubUri, const std::string& deviceId, const std::string& statusFile ) const;

/// @brief Construct a IotHub Client from connection string as given by the Azure portal. This is only provided for testing purposes.
/// @param connectionString The IotHub connection string.
std::shared_ptr<IIotHubClient> IotHubClient( const std::string& connectionString ) const;
std::shared_ptr<IIotHubClient> IotHubClient( const std::string& connectionString, const std::string& statusFile ) const;

private:
struct IotHubFactoryImpl;
std::shared_ptr<IotHubFactoryImpl> _impl;
};
#endif // DEVICECONNECTOR_AZURESDKWRAPPER_IOTHUBFACTORY_H_
#endif // DEVICECONNECTOR_AZURESDKWRAPPER_IOTHUBFACTORY_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/AzureSDKWrapper/MethodRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class MethodRouter : private NonCopyable<MethodRouter>
std::shared_ptr<MethodRouterImpl> _impl;
};

#endif /* CONNECTOR_METHODROUTER_H_ */
#endif /* CONNECTOR_METHODROUTER_H_ */
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/AddPortForwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ int AddPortForwarding::Run( const std::string& argument, std::string& response )
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/AddPortForwarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class AddPortForwarding : public ICommand

} // namespace Command

#endif // COMMAND_ADDPORTFORWARDING_H_
#endif // COMMAND_ADDPORTFORWARDING_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/ClearPortForwardings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ int ClearPortForwardings::Run( const std::string& argument, std::string& respons
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/ClearPortForwardings.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class ClearPortForwardings : public ICommand

} // namespace Command

#endif // COMMAND_CLEARPORTFORWARDINGS_H_
#endif // COMMAND_CLEARPORTFORWARDINGS_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/ConfigureVPNConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ int ConfigureVPNConnection::Run( const std::string& argument, std::string& respo
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/ConfigureVPNConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class ConfigureVPNConnection : public ICommand

} // namespace Command

#endif // COMMAND_CONFIGUREVPNCONNECTION_H
#endif // COMMAND_CONFIGUREVPNCONNECTION_H
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/GetNetworkDetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class GetNetworkDetails : public ICommand

} // namespace Command

#endif // COMMAND_GETNETWORKDETAILS_H_
#endif // COMMAND_GETNETWORKDETAILS_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/GetPortForwardings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class GetPortForwardings : public ICommand

} // namespace Command

#endif // COMMAND_GETPORTTFORWARDINGS_H_
#endif // COMMAND_GETPORTTFORWARDINGS_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/GetVPNConfigurations.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class GetVPNConfigurations : public ICommand

} // namespace Command

#endif // COMMAND_GETVPNCONFIGURATIONS_H_
#endif // COMMAND_GETVPNCONFIGURATIONS_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/ICommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ struct ICommand

} // namespace Command

#endif // COMMAND_ICOMMAND_H_
#endif // COMMAND_ICOMMAND_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/IPortForwardingActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ struct IPortForwardingActor

} // namespace Command

#endif // COMMAND_IPORTFORWARDINGACTOR_H_
#endif // COMMAND_IPORTFORWARDINGACTOR_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/IVPNActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ struct IVPNActor

} // namespace Command

#endif // COMMAND_IVPNACTOR_H_
#endif // COMMAND_IVPNACTOR_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/RemovePortForwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ int RemovePortForwarding::Run( const std::string& argument, std::string& respons
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/RemovePortForwarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class RemovePortForwarding : public ICommand

} // namespace Command

#endif // COMMAND_REMOVEPORTFORWARDING_H_
#endif // COMMAND_REMOVEPORTFORWARDING_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/RestartConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class RestartConnector : public ICommand

} // namespace Command

#endif // COMMAND_RESTARTCONNECTOR_H_
#endif // COMMAND_RESTARTCONNECTOR_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/StartVPNConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ int StartVPNConnection::Run( const std::string& argument, std::string& response
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/StartVPNConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ class StartVPNConnection : public ICommand

} // namespace Command

#endif // COMMAND_STARTVPNCONNECTION_H
#endif // COMMAND_STARTVPNCONNECTION_H
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/StopVPNConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ int StopVPNConnection::Run( const std::string& argument, std::string& response )
return 0;
}

} // namespace Command
} // namespace Command
2 changes: 1 addition & 1 deletion src/rsu-connector/Commands/StopVPNConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ class StopVPNConnection : public ICommand

} // namespace Command

#endif // COMMAND_STOPVPNCONNECTION_H
#endif // COMMAND_STOPVPNCONNECTION_H
2 changes: 1 addition & 1 deletion src/rsu-connector/DeviceTwin/IDeviceInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ struct IDeviceInformation
virtual bool VPNConnectionActive() = 0;
};

#endif // CONNECTOR_IDEVICEINFORMATION_H_
#endif // CONNECTOR_IDEVICEINFORMATION_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/HttpHandler/HttpHandlerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ std::shared_ptr<IHttpServer> HttpHandlerFactory::Server( const std::string& ipAd
std::shared_ptr<IHttpClient> HttpHandlerFactory::Client( const std::string& ipAddress, uint16_t port ) const
{
return std::make_shared<HttpClient>( ipAddress, port );
}
}
2 changes: 1 addition & 1 deletion src/rsu-connector/HttpHandler/HttpHandlerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ class HttpHandlerFactory
/// @param port Port the client connects to.
std::shared_ptr<IHttpClient> Client( const std::string& ipAddress, uint16_t port ) const;
};
#endif // CONNECTOR_HTTPHANDLER_HTTPHANDLERFACTORY_H_
#endif // CONNECTOR_HTTPHANDLER_HTTPHANDLERFACTORY_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/HttpHandler/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class HttpServer : public IHttpServer
std::shared_ptr<HttpServerImpl> _impl;
};

#endif // CONNECTOR_HTTPHANDLER_IHTTPSERVER_H_
#endif // CONNECTOR_HTTPHANDLER_IHTTPSERVER_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/HttpHandler/IHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ struct IHttpServer : private NonCopyable<IHttpServer>
virtual void AddHandler( const std::string& method, const std::string& path, HttpServerHandlerType handler ) = 0;
};

#endif // CONNECTOR_HTTPHANDLER_IHTTPSERVER_H_
#endif // CONNECTOR_HTTPHANDLER_IHTTPSERVER_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/JobScheduler/JobScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ class JobScheduler : public IJobScheduler
std::shared_ptr<JobSchedulerImpl> _impl;
};

#endif // DEVICECONNECTOR_JOBSCHEDULER_JOBSCHEDULER_H_
#endif // DEVICECONNECTOR_JOBSCHEDULER_JOBSCHEDULER_H_
2 changes: 1 addition & 1 deletion src/rsu-connector/common/DestroyNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ class DestroyNotification
std::function<void()> destroyNotification{ []() {} };
};

#endif /* CONNECTOR_COMMON_DESTROYNOTIFICATIONE_H_ */
#endif /* CONNECTOR_COMMON_DESTROYNOTIFICATIONE_H_ */
Loading