Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Jul 17, 2024
1 parent 58130d0 commit 0330cfd
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 85 deletions.
28 changes: 14 additions & 14 deletions src/v2i-hub/SpatPlugin/src/NTCIP1202.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* NTCIP1202.cpp
/**
* Copyright (C) 2024 LEIDOS.
*
* Created on: Apr 3, 2017
* Author: ivp
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
#include <iostream>
#include <cstring>
#include "NTCIP1202.h"
#include <netinet/in.h>
#include <ctime>
#include <ratio>
#include <PluginLog.h>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string.hpp>


using namespace std;
using namespace boost::property_tree;
Expand Down
32 changes: 24 additions & 8 deletions src/v2i-hub/SpatPlugin/src/NTCIP1202.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
/*
* NTCIP1202.h
/**
* Copyright (C) 2024 LEIDOS.
*
* Created on: Apr 3, 2017
* Author: ivp
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

#ifndef SRC_NTCIP1202_H_
#define SRC_NTCIP1202_H_
#pragma once

#include <mutex>
#include <list>

#include <tmx/j2735_messages/SpatMessage.hpp>

#include <iostream>
#include <cstring>
#include <netinet/in.h>
#include <ctime>
#include <ratio>
#include <PluginLog.h>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string.hpp>
#include "carma-clock/carma_clock.h"

using namespace std;
Expand Down Expand Up @@ -124,4 +141,3 @@ class Ntcip1202



#endif /* SRC_NTCIP1202_H_ */
21 changes: 18 additions & 3 deletions src/v2i-hub/SpatPlugin/src/SignalControllerConnection.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/**
* Copyright (C) 2024 LEIDOS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
#include "SignalControllerConnection.h"

namespace SpatPlugin {

SignalControllerConnection::SignalControllerConnection(const std::string &localIp, unsigned int localPort, const std::string &signalGroupMapping, const std::string &scIp, unsigned int scSNMPPort, const std::string &intersectionName, unsigned int intersectionId) : spatPacketReceiver(std::make_unique<tmx::utils::UdpServer>(localIp, localPort)) ,scSNMPClient(std::make_unique<tmx::utils::snmp_client>(scIp, scSNMPPort ,"administrator", "", "", "")), signalGroupMapping(signalGroupMapping), intersectionName(intersectionName), intersectionId(intersectionId) {

};
bool SignalControllerConnection::initializeSignalControllerConnection() {
bool SignalControllerConnection::initializeSignalControllerConnection() const {
tmx::utils::snmp_response_obj resp;
resp.val_int = 2;
resp.type = tmx::utils::snmp_response_obj::response_type::INTEGER;
return scSNMPClient->process_snmp_request("1.3.6.1.4.1.1206.3.5.2.9.44.1.0", tmx::utils::request_type::SET, resp);
};

void SignalControllerConnection::receiveBinarySPAT(std::shared_ptr<SPAT> &spat, uint64_t timeMs ) {
void SignalControllerConnection::receiveBinarySPAT(const std::shared_ptr<SPAT> &spat, uint64_t timeMs ) const {
FILE_LOG(tmx::utils::logDEBUG) << "Receiving binary SPAT ..." << std::endl;
char buf[1000];
auto numBytes = spatPacketReceiver->TimedReceive(buf, 1000, 1000);
Expand All @@ -33,7 +48,7 @@ namespace SpatPlugin {
}
}

void SignalControllerConnection::receiveUPERSPAT(std::shared_ptr<tmx::messages::SpatEncodedMessage> &spatEncoded_ptr) {
void SignalControllerConnection::receiveUPERSPAT(std::shared_ptr<tmx::messages::SpatEncodedMessage> &spatEncoded_ptr) const {
FILE_LOG(tmx::utils::logDEBUG1) << "Receiving J2725 HEX SPAT ..." << std::endl;
auto payload = spatPacketReceiver->stringTimedReceive( 1000 );
auto index = payload.find("Payload=");
Expand Down
74 changes: 64 additions & 10 deletions src/v2i-hub/SpatPlugin/src/SignalControllerConnection.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2024 LEIDOS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
#pragma once

#include <UdpServer.h>
Expand All @@ -13,27 +28,66 @@


namespace SpatPlugin {

/**
* @brief Class to represent Traffic Signal Controller (TSC or SC) connection. Includes a UDP Server for listening for
* SPaT data broadcast from TSC. Also includes an SNMP Client for request or setting status of SNMP objects described
* in NTCIP 1202 (Object Definitions for Actuated Signal Controllers).
*/
class SignalControllerConnection
{
private:
// UDP Server socket listening for SPAT
/**
* @brief UDP Server for receiving SPaT packets from TSC.
*/
std::shared_ptr<tmx::utils::UdpServer> spatPacketReceiver;

/**
* @brief SNMP Client for requesting or setting status of SNMP Objects on
* TSC.
*/
std::shared_ptr<tmx::utils::snmp_client> scSNMPClient;

/**
* @brief String that describes phase to signal group mapping configured on
* the TSC. TODO: Remove in place of SNMP requests on tables.
*/
std::string signalGroupMapping;
/**
* @brief String name of intersection in SPaT messages.
*/
std::string intersectionName;
/**
* @brief Numeric identifier for intersection in SPaT messages.
*/
unsigned int intersectionId;

friend class TestSignalControllerConnection;

public:
/**
* @brief Constructor for Signal Controller Connection.
* @param localIp IP address of device connecting to signal controller. This will be the IP of the UDP Server listening for SPaT data.
* @param localPort port on which to listen for SPaT data.
* @param signalGroupMapping JSON mapping of phases to signal groups
* @param scIp IP address of TSC
* @param scSNMPPort port of SNMP Server on TSC
* @param intersectionName Name of intersection
* @param intersectionID Intersection ID.
*/
SignalControllerConnection(const std::string &localIp, unsigned int localPort, const std::string &signalGroupMapping, const std::string &scIp, unsigned int scSNMPPort, const std::string &intersectionName, unsigned int intersectionID);

bool initializeSignalControllerConnection();

void receiveBinarySPAT(std::shared_ptr<SPAT> &spat, uint64_t timeMs);

void receiveUPERSPAT(std::shared_ptr<tmx::messages::SpatEncodedMessage> &spatEncoded_ptr);
/**
* @brief Method attempts to send SNMP SET request to SNMP object to enable SPAT broadcasting.
* @return true if successful and false if not.
*/
bool initializeSignalControllerConnection() const;
/**
* @brief Method to receive SPaT data in binary format from TSC.
* @param spat an empty SPaT pointer to which the SPAT data will be written.
* @param timeMs current time in ms from epoch to use for message timestamp.
*/
void receiveBinarySPAT(const std::shared_ptr<SPAT> &spat, uint64_t timeMs) const;
/**
* @brief Method to receive SPaT data in UPER Hex format from TSC.
* @param spatEncoded_ptr Empty SpatEncodedMessage to which the UPER encoded SPaT data will be written.
*/
void receiveUPERSPAT(std::shared_ptr<tmx::messages::SpatEncodedMessage> &spatEncoded_ptr) const;
};
}
15 changes: 15 additions & 0 deletions src/v2i-hub/SpatPlugin/src/SpatPlugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2024 LEIDOS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
#include "SpatPlugin.h"


Expand Down
129 changes: 84 additions & 45 deletions src/v2i-hub/SpatPlugin/src/SpatPlugin.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/*
* SpatPlugin.h
/**
* Copyright (C) 2024 LEIDOS.
*
* Created on: April 20, 2017
* Author: zinkg
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

#ifndef SPATPLUGIN_H_
#define SPATPLUGIN_H_
#pragma once

#include <atomic>
#include <array>
Expand All @@ -20,43 +27,75 @@
#include "SignalControllerConnection.h"

namespace SpatPlugin {

class SpatPlugin: public tmx::utils::PluginClientClockAware {

public:

SpatPlugin(const std::string &name);
virtual ~SpatPlugin();


protected:

void UpdateConfigSettings();

// Virtual method overrides.
void OnConfigChanged(const char *key, const char *value);
void OnStateChange(IvpPluginState state);

private:

std::mutex data_lock;

std::unique_ptr<tmx::utils::ThreadTimer> spatReceiverThread;

std::unique_ptr<SignalControllerConnection> scConnection;

std::string spatMode = "";

const char* keyConnectionStatus = "Connection Status";

const char* keySkippedMessages = "Skipped Messages";

uint skippedMessages = 0;

bool isConnected = false;

void processSpat();
};
/**
* @brief The SPaT Plugin is responsible for receiving information from the Traffic Signal Controller (TSC or SC) necessary
* for broadcasting Signal Phase and Timing (SPaT) messages. This includes querying any SNMP objects to determine
* TSC state and listen for any broadcast SPaT information from the TSC.
*/
class SpatPlugin: public tmx::utils::PluginClientClockAware {

public:
/**
* @brief Plugin Constructor.
* @param name Plugin Name.
*/
explicit SpatPlugin(const std::string &name);
/**
* @brief Plugin Destructor
*/
virtual ~SpatPlugin();


protected:
/**
* @brief Method to update plugin after configuration settings have changed.
*/
void UpdateConfigSettings();

// Virtual method overrides.
void OnConfigChanged(const char *key, const char *value) override;
void OnStateChange(IvpPluginState state) override;

private:
/**
* @brief Mutex for thread safety for configuration parameters.
*/
std::mutex data_lock;
/**
* @brief Thread timer used to periodically consume broadcast SPaT
* data from the TSC .
*/
std::unique_ptr<tmx::utils::ThreadTimer> spatReceiverThread;
/**
* @brief TSC Connection.
*/
std::unique_ptr<SignalControllerConnection> scConnection;
/**
* @brief String describing the expected format of received SPaT data.
*/
std::string spatMode = "";
/**
* @brief Key for state object describing TSC Connection Status.
*/
const char* keyConnectionStatus = "Connection Status";
/**
* @brief Key for counting the number of received packets from TSC that
* have been skipped due to errors.
*/
const char* keySkippedMessages = "Skipped Messages";
/**
* @brief Count of received packets from the TSC that have been skipped due to
* errors.
*/
uint skippedMessages = 0;
/**
* @brief Bool flag for TSC connection status.
*/
bool isConnected = false;
/**
* @brief Method to receive and process TSC broadcast SPaT data.
*/
void processSpat();
};
} /* namespace SpatPlugin */

#endif /* SPATPLUGIN_H_ */
Loading

0 comments on commit 0330cfd

Please sign in to comment.