Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2020-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file libsumocpp2c.cpp 15 : /// @author Robert Hilbrich 16 : /// @author Matthias Schwamborn 17 : /// @date Mon, 17 Aug 2020 18 : /// 19 : // Implementation of the libsumo c++ to c wrapper 20 : /****************************************************************************/ 21 : 22 : #ifdef _MSC_VER 23 : // Avoid warnings in windows build because of strcpy instead of strcpy_s, 24 : // because the latter is not available on all platforms 25 : #define _CRT_SECURE_NO_WARNINGS 26 : #pragma warning(disable:4820 4514 5045 4668 4710) 27 : #endif 28 : 29 : #include <sstream> 30 : #include <iostream> 31 : #include <stdlib.h> 32 : #include <libsumo/TraCIConstants.h> 33 : #include <libsumo/TraCIDefs.h> 34 : #include <libsumo/Simulation.h> 35 : #include <libsumo/Vehicle.h> 36 : 37 : #include "libsumocpp2c.h" 38 : 39 : #define DELIMITER ' ' 40 : 41 : inline char* 42 120 : allocateAndCopyString(ModelInstance* comp, const std::string& s) { 43 : char* buf = NULL; 44 120 : buf = (char*)comp->allocateMemory(1 + s.length(), sizeof(char)); 45 120 : s.copy(buf, 1 + s.length()); 46 120 : return buf; 47 : } 48 : 49 : void 50 8 : libsumo_load(char* callOptions) { 51 : // Tokenize the string, because Simulation::load expects a vector 52 : std::vector<std::string> options; 53 16 : std::stringstream ss(callOptions); 54 : std::string temp_str; 55 40 : while (std::getline(ss, temp_str, DELIMITER)) { 56 32 : options.push_back(temp_str); 57 : } 58 : 59 : try { 60 8 : libsumo::Simulation::load(options); 61 0 : } catch (const libsumo::TraCIException& e) { 62 0 : std::cerr << "libsumo::Simulation::load() failed - reason: " << e.what() << std::endl; 63 0 : abort(); 64 0 : } 65 8 : } 66 : 67 : void 68 8 : libsumo_close(void) { 69 8 : libsumo::Simulation::close(); 70 8 : } 71 : 72 : void 73 420 : libsumo_step(double time) { 74 420 : libsumo::Simulation::step(time); 75 420 : } 76 : 77 : int 78 120 : libsumo_vehicle_getIDCount(void) { 79 120 : return libsumo::Vehicle::getIDCount(); 80 : } 81 : 82 : void 83 58 : libsumo_vehicle_moveToXY(const char* paramString) { 84 : try { 85 : std::vector<std::string> params; 86 116 : std::stringstream ss(paramString); 87 : std::string temp_str; 88 348 : while (std::getline(ss, temp_str, DELIMITER)) { 89 290 : params.push_back(temp_str); 90 : } 91 : char* pEnd; 92 : const std::string vehID = params[0]; 93 : const std::string edgeID = params[1]; 94 58 : int laneIndex = strtol(params[2].c_str(), &pEnd, 10); 95 58 : double x = strtod(params[3].c_str(), &pEnd); 96 58 : double y = strtod(params[4].c_str(), &pEnd); 97 58 : double angle = params.size() >= 6 ? strtod(params[5].c_str(), &pEnd) : libsumo::INVALID_DOUBLE_VALUE; 98 58 : int keepRoute = params.size() >= 7 ? strtol(params[6].c_str(), &pEnd, 10) : 1; 99 58 : double matchThreshold = params.size() >= 8 ? strtod(params[7].c_str(), &pEnd) : 100; 100 58 : libsumo::Vehicle::moveToXY(vehID, edgeID, laneIndex, x, y, angle, keepRoute, matchThreshold); 101 58 : } catch (const std::runtime_error& e) { 102 0 : std::cerr << "libsumo::Vehicle::moveToXY() failed - reason: " << e.what() << std::endl; 103 0 : abort(); 104 0 : } 105 58 : } 106 : 107 : void 108 2 : libsumo_vehicle_getParameterWithKey(ModelInstance* comp, const char** result) { 109 : try { 110 : std::vector<std::string> params; 111 4 : std::stringstream ss(comp->getterParameters); 112 : std::string temp_str; 113 6 : while (std::getline(ss, temp_str, DELIMITER)) { 114 4 : params.push_back(temp_str); 115 : } 116 : 117 : const std::string vehID = params[0]; 118 : const std::string key = params[1]; 119 2 : std::pair<std::string, std::string> p = libsumo::Vehicle::getParameterWithKey(vehID, key); 120 2 : const std::string resultString = p.first + DELIMITER + p.second; 121 2 : *result = allocateAndCopyString(comp, resultString); 122 4 : } catch (const std::runtime_error& e) { 123 0 : std::cerr << "libsumo::Vehicle::getParameterWithKey() failed - reason: " << e.what() << std::endl; 124 0 : abort(); 125 0 : } 126 2 : } 127 : 128 : void 129 60 : libsumo_vehicle_getLaneID(ModelInstance* comp, const char** result) { 130 : try { 131 : std::vector<std::string> params; 132 120 : std::stringstream ss(comp->getterParameters); 133 : std::string temp_str; 134 120 : while (std::getline(ss, temp_str, DELIMITER)) { 135 60 : params.push_back(temp_str); 136 : } 137 : 138 : const std::string vehID = params[0]; 139 60 : *result = allocateAndCopyString(comp, libsumo::Vehicle::getLaneID(vehID)); 140 60 : } catch (const std::runtime_error& e) { 141 0 : std::cerr << "libsumo::Vehicle::getLaneID() failed - reason: " << e.what() << std::endl; 142 0 : abort(); 143 0 : } 144 60 : } 145 : 146 : void 147 58 : libsumo_vehicle_getPosition(ModelInstance* comp, const char** result) { 148 : try { 149 : std::vector<std::string> params; 150 116 : std::stringstream ss(comp->getterParameters); 151 : std::string temp_str; 152 116 : while (std::getline(ss, temp_str, DELIMITER)) { 153 58 : params.push_back(temp_str); 154 : } 155 : 156 : const std::string vehID = params[0]; 157 58 : libsumo::TraCIPosition pos = libsumo::Vehicle::getPosition(vehID); 158 58 : std::ostringstream os; 159 116 : os << pos.x << DELIMITER << pos.y; 160 58 : *result = allocateAndCopyString(comp, os.str()); 161 116 : } catch (const std::runtime_error& e) { 162 0 : std::cerr << "libsumo::Vehicle::getPosition() failed - reason: " << e.what() << std::endl; 163 0 : abort(); 164 0 : } 165 58 : }