Showing
8 changed files
with
733 additions
and
0 deletions
code/modify/CMakeLists.txt
0 → 100644
This diff is collapsed. Click to expand it.
code/modify/Tools/ed25519sign
0 → 100755
No preview for this file type
code/modify/Tools/ed25519sign.cpp
0 → 100644
1 | +#include <openssl/pem.h> | ||
2 | +#include <openssl/evp.h> | ||
3 | +#include <openssl/bio.h> | ||
4 | +#include <openssl/err.h> | ||
5 | +#include <iostream> | ||
6 | +#include <iomanip> | ||
7 | +#include <fstream> | ||
8 | +#include <cstring> | ||
9 | +#include <algorithm> | ||
10 | + | ||
11 | + | ||
12 | +using namespace std; | ||
13 | + | ||
14 | + | ||
15 | +int generate_key() | ||
16 | +{ | ||
17 | + ofstream fout; | ||
18 | + BIO* bio; | ||
19 | + | ||
20 | + // generate key pair | ||
21 | + EVP_PKEY* key = NULL; | ||
22 | + EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, NULL); | ||
23 | + if(EVP_PKEY_keygen_init(pctx) != 1) | ||
24 | + { | ||
25 | + cout << "Error: EVP_PKEY_keygen_init" << endl; | ||
26 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
27 | + return 0; | ||
28 | + } | ||
29 | + if(EVP_PKEY_keygen(pctx, &key) != 1) | ||
30 | + { | ||
31 | + cout << "Error: EVP_PKEY_keygen" << endl; | ||
32 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
33 | + return 0; | ||
34 | + } | ||
35 | + EVP_PKEY_CTX_free(pctx); | ||
36 | + | ||
37 | + // save private key | ||
38 | + bio = BIO_new( BIO_s_mem() ); | ||
39 | + if(PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL) != 1) | ||
40 | + { | ||
41 | + cout << "Error: PEM_write_bio_PrivateKey" << endl; | ||
42 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
43 | + return 0; | ||
44 | + } | ||
45 | + unsigned char private_key[120] = { 0 }; | ||
46 | + BIO_read(bio, private_key, 120); | ||
47 | + BIO_free(bio); | ||
48 | + cout << private_key << endl; | ||
49 | + fout.open("private_key"); | ||
50 | + fout << private_key; | ||
51 | + fout.close(); | ||
52 | + | ||
53 | + // save public key | ||
54 | + bio = BIO_new( BIO_s_mem() ); | ||
55 | + if(PEM_write_bio_PUBKEY(bio, key) != 1) | ||
56 | + { | ||
57 | + cout << "Error: PEM_write_bio_PUBKEY" << endl; | ||
58 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
59 | + return 0; | ||
60 | + } | ||
61 | + EVP_PKEY_free(key); | ||
62 | + unsigned char public_key[120] = { 0 }; | ||
63 | + BIO_read(bio, public_key, 120); | ||
64 | + BIO_free(bio); | ||
65 | + cout << public_key << endl; | ||
66 | + fout.open("public_key"); | ||
67 | + fout << public_key; | ||
68 | + fout.close(); | ||
69 | + | ||
70 | + return 1; | ||
71 | +} | ||
72 | + | ||
73 | + | ||
74 | + | ||
75 | +int sign(char* signee_file, char* key_file) | ||
76 | +{ | ||
77 | + ifstream fin; | ||
78 | + ofstream fout; | ||
79 | + BIO* bio; | ||
80 | + EVP_PKEY* pkey = NULL; | ||
81 | + int signee_size = 0, key_size = 0; | ||
82 | + | ||
83 | + // open signee_file | ||
84 | + fin.open(signee_file); | ||
85 | + if (fin.fail()) { | ||
86 | + cout << "Error: Can't open signee file." << endl; | ||
87 | + return 0; | ||
88 | + } | ||
89 | + fin.seekg(0, ios::end); | ||
90 | + signee_size = fin.tellg(); | ||
91 | + fin.seekg(0, ios::beg); | ||
92 | + unsigned char* signee = new unsigned char[signee_size]; | ||
93 | + fin.read((char*)signee, signee_size); | ||
94 | + fin.close(); | ||
95 | + | ||
96 | + // open private key | ||
97 | + fin.open(key_file); | ||
98 | + if (fin.fail()) { | ||
99 | + cout << "Error: Can't open key file." << endl; | ||
100 | + return 0; | ||
101 | + } | ||
102 | + fin.seekg(0, ios::end); | ||
103 | + key_size = fin.tellg(); | ||
104 | + fin.seekg(0, ios::beg); | ||
105 | + unsigned char* key = new unsigned char[key_size]; | ||
106 | + fin.read((char*)key, key_size); | ||
107 | + fin.close(); | ||
108 | + cout << key << endl; | ||
109 | + bio = BIO_new( BIO_s_mem() ); | ||
110 | + BIO_write(bio, key, key_size); | ||
111 | + if(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL) == NULL) | ||
112 | + { | ||
113 | + cout << "Error: PEM_read_bio_PrivateKey" << endl; | ||
114 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
115 | + return 0; | ||
116 | + } | ||
117 | + BIO_free(bio); | ||
118 | + | ||
119 | + // File Signing, Make Signature | ||
120 | + unsigned char sig[65] = { 0 }; | ||
121 | + size_t sig_size = 64; | ||
122 | + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); | ||
123 | + if(EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1) | ||
124 | + { | ||
125 | + cout << "Error: EVP_DigestSignInit" << endl; | ||
126 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
127 | + return 0; | ||
128 | + } | ||
129 | + if(EVP_DigestSign(ctx, sig, &sig_size, signee, signee_size) != 1) | ||
130 | + { | ||
131 | + cout << "Error: EVP_DigestSign" << endl; | ||
132 | + cout << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
133 | + return 0; | ||
134 | + } | ||
135 | + EVP_MD_CTX_free(ctx); | ||
136 | + EVP_PKEY_free(pkey); | ||
137 | + | ||
138 | + fout.open("./signature"); | ||
139 | + cout << hex << setfill('0'); | ||
140 | + fout << hex << setfill('0'); | ||
141 | + for(int i = 0; i < sig_size; i++) | ||
142 | + { | ||
143 | + cout << setw(2) << (int)sig[i]; | ||
144 | + fout << setw(2) << (int)sig[i]; | ||
145 | + } | ||
146 | + cout << endl; | ||
147 | + fout.close(); | ||
148 | + | ||
149 | + return 1; | ||
150 | +} | ||
151 | + | ||
152 | + | ||
153 | +/* | ||
154 | +int verify(char* signee_file, char* sig_file, char* key_file) | ||
155 | +{ | ||
156 | + ifstream fin; | ||
157 | + BIO* bio; | ||
158 | + EVP_PKEY* pkey = NULL; | ||
159 | + int signee_size = 0, key_size = 0, sig_size = 0; | ||
160 | + | ||
161 | + // open signee_file | ||
162 | + fin.open(signee_file); | ||
163 | + fin.seekg(0, ios::end); | ||
164 | + signee_size = fin.tellg(); | ||
165 | + fin.seekg(0, ios::beg); | ||
166 | + unsigned char* signee = new unsigned char[signee_size]; | ||
167 | + fin.read((char*)signee, signee_size); | ||
168 | + fin.close(); | ||
169 | + | ||
170 | + // open public key | ||
171 | + fin.open(key_file); | ||
172 | + fin.seekg(0, ios::end); | ||
173 | + key_size = fin.tellg(); | ||
174 | + fin.seekg(0, ios::beg); | ||
175 | + unsigned char* key = new unsigned char[key_size]; | ||
176 | + fin.read((char*)key, key_size); | ||
177 | + fin.close(); | ||
178 | + cout << key << endl; | ||
179 | + bio = BIO_new( BIO_s_mem() ); | ||
180 | + BIO_write(bio, key, key_size); | ||
181 | + if(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL) == NULL) | ||
182 | + { | ||
183 | + cout << "Error: " << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
184 | + return 0; | ||
185 | + } | ||
186 | + BIO_free(bio); | ||
187 | + | ||
188 | + fin.open(sig_file); | ||
189 | + fin.seekg(0, ios::end); | ||
190 | + sig_size = fin.tellg(); | ||
191 | + fin.seekg(0, ios::beg); | ||
192 | + unsigned char* sig_char = new unsigned char[sig_size]; | ||
193 | + fin.read((char*)sig_char, sig_size); | ||
194 | + fin.close(); | ||
195 | + unsigned char sig[65] = { 0 }; | ||
196 | + char temp[3] = { 0 }; | ||
197 | + for(int i = 0; i < sig_size; i+=2) | ||
198 | + { | ||
199 | + temp[0] = sig_char[i]; | ||
200 | + temp[1] = sig_char[i+1]; | ||
201 | + sig[i/2] = (unsigned char) strtol(temp, NULL, 16); | ||
202 | + } | ||
203 | + sig_size /= 2; | ||
204 | + | ||
205 | + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); | ||
206 | + if(EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey) != 1) | ||
207 | + { | ||
208 | + cout << "Error: " << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
209 | + return 0; | ||
210 | + } | ||
211 | + if(EVP_DigestVerify(ctx, sig, sig_size, signee, signee_size) != 1) | ||
212 | + { | ||
213 | + cout << "Error: " << ERR_error_string(ERR_get_error(),NULL) << endl; | ||
214 | + return 0; | ||
215 | + } | ||
216 | + EVP_PKEY_free(pkey); | ||
217 | + | ||
218 | + cout << "Success" << endl; | ||
219 | + return 1; | ||
220 | +} | ||
221 | +*/ | ||
222 | + | ||
223 | + | ||
224 | +bool cmdOptionExists(char** begin, char** end, const string& option) | ||
225 | +{ | ||
226 | + return find(begin, end, option) != end; | ||
227 | +} | ||
228 | + | ||
229 | + | ||
230 | + | ||
231 | +char* getCmdOption(char** begin, char** end, const string& option) | ||
232 | +{ | ||
233 | + char** itr = find(begin, end, option); | ||
234 | + if (itr != end && ++itr != end) | ||
235 | + return *itr; | ||
236 | + return 0; | ||
237 | +} | ||
238 | + | ||
239 | + | ||
240 | + | ||
241 | +int main(int argc, char* argv[]) | ||
242 | +{ | ||
243 | + if (cmdOptionExists(argv, argv+argc, "-h")\ | ||
244 | + || cmdOptionExists(argv, argv+argc, "--help")) | ||
245 | + { | ||
246 | + cout << "Usage" << endl; | ||
247 | + cout << "Generate key: " << argv[0] << " --genkey" << endl; | ||
248 | + cout << "Signing file: " << argv[0] << " -f signee_path -k key_path" << endl; | ||
249 | + return 0; | ||
250 | + } | ||
251 | + | ||
252 | + if (cmdOptionExists(argv, argv+argc, "--genkey")) | ||
253 | + { | ||
254 | + generate_key(); | ||
255 | + return 0; | ||
256 | + } | ||
257 | + | ||
258 | + if (cmdOptionExists(argv, argv+argc, "-f")\ | ||
259 | + && cmdOptionExists(argv, argv+argc, "-k")) | ||
260 | + { | ||
261 | + char* signee_path = getCmdOption(argv, argv+argc, "-f"); | ||
262 | + char* key_path = getCmdOption(argv, argv+argc, "-k"); | ||
263 | + if (sign(signee_path, key_path) != 1) | ||
264 | + cout << "Error occured" << endl; | ||
265 | + return 0; | ||
266 | + } | ||
267 | + else | ||
268 | + { | ||
269 | + cout << "Usage" << endl; | ||
270 | + cout << "Generate key: " << argv[0] << " --genkey" << endl; | ||
271 | + cout << "Signing file: " << argv[0] << " -f signee_path -k key_path" << endl; | ||
272 | + } | ||
273 | +// generate_key(); | ||
274 | +// sign(argv[1], argv[2]); | ||
275 | +// verify(argv[1], argv[2], argv[3]); | ||
276 | + return 0; | ||
277 | +} |
1 | +/**************************************************************************** | ||
2 | + * | ||
3 | + * Copyright (c) 2015 Mark Charlebois. All rights reserved. | ||
4 | + * Copyright (c) 2016-2019 PX4 Development Team. All rights reserved. | ||
5 | + * | ||
6 | + * Redistribution and use in source and binary forms, with or without | ||
7 | + * modification, are permitted provided that the following conditions | ||
8 | + * are met: | ||
9 | + * | ||
10 | + * 1. Redistributions of source code must retain the above copyright | ||
11 | + * notice, this list of conditions and the following disclaimer. | ||
12 | + * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | + * notice, this list of conditions and the following disclaimer in | ||
14 | + * the documentation and/or other materials provided with the | ||
15 | + * distribution. | ||
16 | + * 3. Neither the name PX4 nor the names of its contributors may be | ||
17 | + * used to endorse or promote products derived from this software | ||
18 | + * without specific prior written permission. | ||
19 | + * | ||
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
31 | + * POSSIBILITY OF SUCH DAMAGE. | ||
32 | + * | ||
33 | + ****************************************************************************/ | ||
34 | + | ||
35 | +/** | ||
36 | + * @file simulator.cpp | ||
37 | + * | ||
38 | + * This module interfaces via MAVLink to a software in the loop simulator (SITL) | ||
39 | + * such as jMAVSim or Gazebo. | ||
40 | + */ | ||
41 | + | ||
42 | +#include <px4_platform_common/log.h> | ||
43 | +#include <px4_platform_common/tasks.h> | ||
44 | +#include <px4_platform_common/time.h> | ||
45 | +#include <systemlib/err.h> | ||
46 | +#include <drivers/drv_board_led.h> | ||
47 | +#include <openssl/evp.h> | ||
48 | +#include <openssl/pem.h> | ||
49 | +#include <openssl/bio.h> | ||
50 | +#include <openssl/err.h> | ||
51 | +#include <fstream> | ||
52 | +#include <cstring> | ||
53 | + | ||
54 | +#include "simulator.h" | ||
55 | + | ||
56 | +static px4_task_t g_sim_task = -1; | ||
57 | + | ||
58 | +Simulator *Simulator::_instance = nullptr; | ||
59 | + | ||
60 | +void Simulator::parameters_update(bool force) | ||
61 | +{ | ||
62 | + // check for parameter updates | ||
63 | + if (_parameter_update_sub.updated() || force) { | ||
64 | + // clear update | ||
65 | + parameter_update_s pupdate; | ||
66 | + _parameter_update_sub.copy(&pupdate); | ||
67 | + | ||
68 | + // update parameters from storage | ||
69 | + updateParams(); | ||
70 | + } | ||
71 | +} | ||
72 | + | ||
73 | +int Simulator::verify() | ||
74 | +{ | ||
75 | +/* | ||
76 | + char path[256]; | ||
77 | + if(getcwd(path, 256) == NULL) | ||
78 | + PX4_INFO("Test path: %s", strerror(errno)); | ||
79 | + else | ||
80 | + PX4_INFO("Test path: %s", path); | ||
81 | +*/ | ||
82 | + | ||
83 | + std::ifstream fin; | ||
84 | + BIO* bio; | ||
85 | + EVP_PKEY* pkey = NULL; | ||
86 | + | ||
87 | + const char* file_path = "../../bin/px4"; | ||
88 | +// const char* file_path = "../../../../Tools/test.txt"; | ||
89 | + const char* sig_path = "../../../../Tools/signature"; | ||
90 | + const char* key_path = "../../../../Tools/public_key"; | ||
91 | + | ||
92 | + int file_size = 0, sig_size = 0, key_size = 0; | ||
93 | + | ||
94 | + // open exe file | ||
95 | + fin.open(file_path); | ||
96 | + if (fin.fail()) { | ||
97 | + PX4_WARN("Error: Can't open exe file during signature verifying."); | ||
98 | + return 0; | ||
99 | + } | ||
100 | + fin.seekg(0, std::ios::end); | ||
101 | + file_size = fin.tellg(); | ||
102 | + fin.seekg(0, std::ios::beg); | ||
103 | + unsigned char* file = new unsigned char[file_size]; | ||
104 | + fin.read((char*)file, file_size); | ||
105 | + fin.close(); | ||
106 | + | ||
107 | + // open signature file | ||
108 | + fin.open(sig_path); | ||
109 | + if (fin.fail()) { | ||
110 | + PX4_WARN("Error: Can't open signature file during signature verifying."); | ||
111 | + return 0; | ||
112 | + } | ||
113 | + fin.seekg(0, std::ios::end); | ||
114 | + sig_size = fin.tellg(); | ||
115 | + fin.seekg(0, std::ios::beg); | ||
116 | + unsigned char* sig_char = new unsigned char[sig_size]; | ||
117 | + fin.read((char*)sig_char, sig_size); | ||
118 | + fin.close(); | ||
119 | + unsigned char sig[65] = { 0 }; | ||
120 | + char temp[3] = { 0 }; | ||
121 | + for (int i = 0; i < sig_size; i+=2) | ||
122 | + { | ||
123 | + temp[0] = sig_char[i]; | ||
124 | + temp[1] = sig_char[i+1]; | ||
125 | + sig[i/2] = (unsigned char)strtol(temp, NULL, 16); | ||
126 | + } | ||
127 | + delete[] sig_char; | ||
128 | + sig_size /= 2; | ||
129 | + | ||
130 | + // open public key file | ||
131 | + fin.open(key_path); | ||
132 | + if (fin.fail()) { | ||
133 | + PX4_WARN("Error: Can't open public key file during signature verifying."); | ||
134 | + return 0; | ||
135 | + } | ||
136 | + fin.seekg(0, std::ios::end); | ||
137 | + key_size = fin.tellg(); | ||
138 | + fin.seekg(0, std::ios::beg); | ||
139 | + unsigned char* key = new unsigned char[key_size]; | ||
140 | + fin.read((char*)key, key_size); | ||
141 | + fin.close(); | ||
142 | + | ||
143 | + // make key file to pkey | ||
144 | + bio = BIO_new( BIO_s_mem() ); | ||
145 | + BIO_write(bio, key, key_size); | ||
146 | + if (PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL) == NULL) { | ||
147 | + PX4_WARN("Error: PEM_read_bio_PUBKEY"); //ERR_error_string(ERR_get_error(), NULL) | ||
148 | + return 0; | ||
149 | + } | ||
150 | + delete[] key; | ||
151 | + BIO_free(bio); | ||
152 | + | ||
153 | + // verifying signature | ||
154 | + EVP_MD_CTX* ctx = EVP_MD_CTX_new(); | ||
155 | + if (EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey) != 1) { | ||
156 | + PX4_WARN("Error: EVP_DigestVerifyInit"); //ERR_error_string(ERR_get_error(), NULL) | ||
157 | + return 0; | ||
158 | + } | ||
159 | + if (EVP_DigestVerify(ctx, sig, sig_size, file, file_size) != 1) { | ||
160 | + return 0; | ||
161 | + } | ||
162 | + EVP_PKEY_free(pkey); | ||
163 | + EVP_MD_CTX_free(ctx); | ||
164 | + delete[] file; | ||
165 | + | ||
166 | + PX4_INFO("Success: Verifying Signature"); | ||
167 | + | ||
168 | + return 1; | ||
169 | +} | ||
170 | + | ||
171 | +int Simulator::start(int argc, char *argv[]) | ||
172 | +{ | ||
173 | + | ||
174 | + _instance = new Simulator(); | ||
175 | + | ||
176 | + if (_instance->verify() != 1) { | ||
177 | + PX4_ERR("Signature Verifying failed"); | ||
178 | + return 1; | ||
179 | + } | ||
180 | + | ||
181 | + if (_instance) { | ||
182 | + | ||
183 | + if (argc == 5 && strcmp(argv[3], "-u") == 0) { | ||
184 | + _instance->set_ip(InternetProtocol::UDP); | ||
185 | + _instance->set_port(atoi(argv[4])); | ||
186 | + } | ||
187 | + | ||
188 | + if (argc == 5 && strcmp(argv[3], "-c") == 0) { | ||
189 | + _instance->set_ip(InternetProtocol::TCP); | ||
190 | + _instance->set_port(atoi(argv[4])); | ||
191 | + } | ||
192 | + | ||
193 | + if (argc == 6 && strcmp(argv[3], "-t") == 0) { | ||
194 | + PX4_INFO("Simulator using TCP on remote host %s port %s", argv[4], argv[5]); | ||
195 | + PX4_WARN("Please ensure port %s is not blocked by a firewall.", argv[5]); | ||
196 | + _instance->set_ip(InternetProtocol::TCP); | ||
197 | + _instance->set_tcp_remote_ipaddr(argv[4]); | ||
198 | + _instance->set_port(atoi(argv[5])); | ||
199 | + } | ||
200 | + | ||
201 | + if (argc == 6 && strcmp(argv[3], "-h") == 0) { | ||
202 | + PX4_INFO("Simulator using TCP on remote host %s port %s", argv[4], argv[5]); | ||
203 | + PX4_WARN("Please ensure port %s is not blocked by a firewall.", argv[5]); | ||
204 | + _instance->set_ip(InternetProtocol::TCP); | ||
205 | + _instance->set_hostname(argv[4]); | ||
206 | + _instance->set_port(atoi(argv[5])); | ||
207 | + } | ||
208 | + | ||
209 | + _instance->run(); | ||
210 | + | ||
211 | + return 0; | ||
212 | + | ||
213 | + } else { | ||
214 | + PX4_WARN("Simulator creation failed"); | ||
215 | + return 1; | ||
216 | + } | ||
217 | +} | ||
218 | + | ||
219 | +static void usage() | ||
220 | +{ | ||
221 | + PX4_INFO("Usage: simulator {start -[spt] [-u udp_port / -c tcp_port] |stop|status}"); | ||
222 | + PX4_INFO("Start simulator: simulator start"); | ||
223 | + PX4_INFO("Connect using UDP: simulator start -u udp_port"); | ||
224 | + PX4_INFO("Connect using TCP: simulator start -c tcp_port"); | ||
225 | + PX4_INFO("Connect to a remote server using TCP: simulator start -t ip_addr tcp_port"); | ||
226 | + PX4_INFO("Connect to a remote server via hostname using TCP: simulator start -h hostname tcp_port"); | ||
227 | +} | ||
228 | + | ||
229 | +__BEGIN_DECLS | ||
230 | +extern int simulator_main(int argc, char *argv[]); | ||
231 | +__END_DECLS | ||
232 | + | ||
233 | + | ||
234 | +int simulator_main(int argc, char *argv[]) | ||
235 | +{ | ||
236 | + if (argc > 1 && strcmp(argv[1], "start") == 0) { | ||
237 | + | ||
238 | + if (g_sim_task >= 0) { | ||
239 | + PX4_WARN("Simulator already started"); | ||
240 | + return 0; | ||
241 | + } | ||
242 | + | ||
243 | + g_sim_task = px4_task_spawn_cmd("simulator", | ||
244 | + SCHED_DEFAULT, | ||
245 | + SCHED_PRIORITY_MAX, | ||
246 | + 1500, | ||
247 | + Simulator::start, | ||
248 | + argv); | ||
249 | + | ||
250 | +#if defined(ENABLE_LOCKSTEP_SCHEDULER) | ||
251 | + | ||
252 | + // We want to prevent the rest of the startup script from running until time | ||
253 | + // is initialized by the HIL_SENSOR messages from the simulator. | ||
254 | + while (true) { | ||
255 | + if (Simulator::getInstance() && Simulator::getInstance()->has_initialized()) { | ||
256 | + break; | ||
257 | + } | ||
258 | + | ||
259 | + system_usleep(100); | ||
260 | + } | ||
261 | + | ||
262 | +#endif | ||
263 | + | ||
264 | + } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { | ||
265 | + if (g_sim_task < 0) { | ||
266 | + PX4_WARN("Simulator not running"); | ||
267 | + return 1; | ||
268 | + | ||
269 | + } else { | ||
270 | + px4_task_delete(g_sim_task); | ||
271 | + g_sim_task = -1; | ||
272 | + } | ||
273 | + | ||
274 | + } else if (argc == 2 && strcmp(argv[1], "status") == 0) { | ||
275 | + if (g_sim_task < 0) { | ||
276 | + PX4_WARN("Simulator not running"); | ||
277 | + return 1; | ||
278 | + | ||
279 | + } else { | ||
280 | + PX4_INFO("running"); | ||
281 | + } | ||
282 | + | ||
283 | + } else { | ||
284 | + usage(); | ||
285 | + return 1; | ||
286 | + } | ||
287 | + | ||
288 | + return 0; | ||
289 | +} |
This diff is collapsed. Click to expand it.
code/original/CMakeLists.txt
0 → 100644
This diff is collapsed. Click to expand it.
1 | +/**************************************************************************** | ||
2 | + * | ||
3 | + * Copyright (c) 2015 Mark Charlebois. All rights reserved. | ||
4 | + * Copyright (c) 2016-2019 PX4 Development Team. All rights reserved. | ||
5 | + * | ||
6 | + * Redistribution and use in source and binary forms, with or without | ||
7 | + * modification, are permitted provided that the following conditions | ||
8 | + * are met: | ||
9 | + * | ||
10 | + * 1. Redistributions of source code must retain the above copyright | ||
11 | + * notice, this list of conditions and the following disclaimer. | ||
12 | + * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | + * notice, this list of conditions and the following disclaimer in | ||
14 | + * the documentation and/or other materials provided with the | ||
15 | + * distribution. | ||
16 | + * 3. Neither the name PX4 nor the names of its contributors may be | ||
17 | + * used to endorse or promote products derived from this software | ||
18 | + * without specific prior written permission. | ||
19 | + * | ||
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
31 | + * POSSIBILITY OF SUCH DAMAGE. | ||
32 | + * | ||
33 | + ****************************************************************************/ | ||
34 | + | ||
35 | +/** | ||
36 | + * @file simulator.cpp | ||
37 | + * | ||
38 | + * This module interfaces via MAVLink to a software in the loop simulator (SITL) | ||
39 | + * such as jMAVSim or Gazebo. | ||
40 | + */ | ||
41 | + | ||
42 | +#include <px4_platform_common/log.h> | ||
43 | +#include <px4_platform_common/tasks.h> | ||
44 | +#include <px4_platform_common/time.h> | ||
45 | +#include <systemlib/err.h> | ||
46 | +#include <drivers/drv_board_led.h> | ||
47 | + | ||
48 | +#include "simulator.h" | ||
49 | + | ||
50 | +static px4_task_t g_sim_task = -1; | ||
51 | + | ||
52 | +Simulator *Simulator::_instance = nullptr; | ||
53 | + | ||
54 | +void Simulator::parameters_update(bool force) | ||
55 | +{ | ||
56 | + // check for parameter updates | ||
57 | + if (_parameter_update_sub.updated() || force) { | ||
58 | + // clear update | ||
59 | + parameter_update_s pupdate; | ||
60 | + _parameter_update_sub.copy(&pupdate); | ||
61 | + | ||
62 | + // update parameters from storage | ||
63 | + updateParams(); | ||
64 | + } | ||
65 | +} | ||
66 | + | ||
67 | +int Simulator::start(int argc, char *argv[]) | ||
68 | +{ | ||
69 | + _instance = new Simulator(); | ||
70 | + | ||
71 | + if (_instance) { | ||
72 | + if (argc == 5 && strcmp(argv[3], "-u") == 0) { | ||
73 | + _instance->set_ip(InternetProtocol::UDP); | ||
74 | + _instance->set_port(atoi(argv[4])); | ||
75 | + } | ||
76 | + | ||
77 | + if (argc == 5 && strcmp(argv[3], "-c") == 0) { | ||
78 | + _instance->set_ip(InternetProtocol::TCP); | ||
79 | + _instance->set_port(atoi(argv[4])); | ||
80 | + } | ||
81 | + | ||
82 | + if (argc == 6 && strcmp(argv[3], "-t") == 0) { | ||
83 | + _instance->set_ip(InternetProtocol::TCP); | ||
84 | + _instance->set_tcp_remote_ipaddr(argv[4]); | ||
85 | + _instance->set_port(atoi(argv[5])); | ||
86 | + } | ||
87 | + | ||
88 | + _instance->run(); | ||
89 | + | ||
90 | + return 0; | ||
91 | + | ||
92 | + } else { | ||
93 | + PX4_WARN("Simulator creation failed"); | ||
94 | + return 1; | ||
95 | + } | ||
96 | +} | ||
97 | + | ||
98 | +static void usage() | ||
99 | +{ | ||
100 | + PX4_INFO("Usage: simulator {start -[spt] [-u udp_port / -c tcp_port] |stop|status}"); | ||
101 | + PX4_INFO("Start simulator: simulator start"); | ||
102 | + PX4_INFO("Connect using UDP: simulator start -u udp_port"); | ||
103 | + PX4_INFO("Connect using TCP: simulator start -c tcp_port"); | ||
104 | + PX4_INFO("Connect to a remote server using TCP: simulator start -t ip_addr tcp_port"); | ||
105 | +} | ||
106 | + | ||
107 | +__BEGIN_DECLS | ||
108 | +extern int simulator_main(int argc, char *argv[]); | ||
109 | +__END_DECLS | ||
110 | + | ||
111 | + | ||
112 | +int simulator_main(int argc, char *argv[]) | ||
113 | +{ | ||
114 | + if (argc > 1 && strcmp(argv[1], "start") == 0) { | ||
115 | + | ||
116 | + if (g_sim_task >= 0) { | ||
117 | + PX4_WARN("Simulator already started"); | ||
118 | + return 0; | ||
119 | + } | ||
120 | + | ||
121 | + g_sim_task = px4_task_spawn_cmd("simulator", | ||
122 | + SCHED_DEFAULT, | ||
123 | + SCHED_PRIORITY_MAX, | ||
124 | + 1500, | ||
125 | + Simulator::start, | ||
126 | + argv); | ||
127 | + | ||
128 | +#if defined(ENABLE_LOCKSTEP_SCHEDULER) | ||
129 | + | ||
130 | + // We want to prevent the rest of the startup script from running until time | ||
131 | + // is initialized by the HIL_SENSOR messages from the simulator. | ||
132 | + while (true) { | ||
133 | + if (Simulator::getInstance() && Simulator::getInstance()->has_initialized()) { | ||
134 | + break; | ||
135 | + } | ||
136 | + | ||
137 | + system_usleep(100); | ||
138 | + } | ||
139 | + | ||
140 | +#endif | ||
141 | + | ||
142 | + } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { | ||
143 | + if (g_sim_task < 0) { | ||
144 | + PX4_WARN("Simulator not running"); | ||
145 | + return 1; | ||
146 | + | ||
147 | + } else { | ||
148 | + px4_task_delete(g_sim_task); | ||
149 | + g_sim_task = -1; | ||
150 | + } | ||
151 | + | ||
152 | + } else if (argc == 2 && strcmp(argv[1], "status") == 0) { | ||
153 | + if (g_sim_task < 0) { | ||
154 | + PX4_WARN("Simulator not running"); | ||
155 | + return 1; | ||
156 | + | ||
157 | + } else { | ||
158 | + PX4_INFO("running"); | ||
159 | + } | ||
160 | + | ||
161 | + } else { | ||
162 | + usage(); | ||
163 | + return 1; | ||
164 | + } | ||
165 | + | ||
166 | + return 0; | ||
167 | +} |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment