main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <malloc.h>
  5. #include <algorithm>
  6. #include <thread>
  7. #include <chrono>
  8. #include <sstream>
  9. #include <string>
  10. #include <iomanip>
  11. #include <cstdint>
  12. #include <bitset>
  13. #include <windows.h>
  14. #include "src/PassiveSocket.h"
  15. #include <stdio.h>
  16. #include "simplelogger.hpp"
  17. using namespace std;
  18. extern std::ostream out(std::cout.rdbuf());
  19. SimpleLogger* newlogger;
  20. /// functions declarations
  21. //////////////////////////////////////////////////////////////
  22. void get_API_version (CActiveSocket& SocketActive);
  23. void get_sw_revision(CActiveSocket& SocketActive);
  24. void get_gru_state(CActiveSocket& SocketActive);
  25. void socket_close(CActiveSocket SocketActive);
  26. vector <int32_t> get_unloaded_num(vector<int32_t> segment_status);
  27. void upload_traj(CActiveSocket& SocketActive, vector<vector<int32_t>> nodes);
  28. vector <int32_t> download_traject (CActiveSocket &SocketActive, int32_t points_cnt);
  29. vector<vector<int32_t>> get_nodes(const string& Traject_file_name);
  30. string hex_converting(int num);
  31. //////////////////////////////////////////////////////////////
  32. /// request functions
  33. //////////////////////////////////////////////////////////////
  34. namespace request
  35. {
  36. constexpr size_t MAX_PACKET{4096};
  37. const int32_t NODES_PER_PACKET{200}; // Number of points for 1 package
  38. const uint32_t PACKETS_WO_CONFIRM{1}; // How many packages could be sent without confirmation
  39. const double CONFIRM_TIMEOUT_SEC{0.1};
  40. const int32_t POINTS_PER_PACKET{500};
  41. string hex_converting(int32_t num)
  42. {
  43. stringstream mystream;
  44. mystream << hex << num;
  45. if(num < 0)
  46. {
  47. return mystream.str().substr(4);
  48. }
  49. else
  50. {
  51. return mystream.str();
  52. }
  53. }
  54. void ShowError(CSimpleSocket ss, string s)
  55. {
  56. *(newlogger) << " " << s << " : " << " = " << ss.DescribeError() << '\n';
  57. *(newlogger) << " IsSocketValid() = " << ss.IsSocketValid() << '\n';
  58. } //ShowError
  59. void get_API_version (CActiveSocket& SocketActive){ //This function send API version request and shows response
  60. uint8 buf[MAX_PACKET] ;
  61. buf[0] = uint8(0xAA);
  62. buf[1] = uint8(0xAA);
  63. buf[2] = uint8(0x0C);
  64. buf[3] = uint8(0x00);
  65. *(newlogger) << "GET API VERSION";
  66. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, 4) << '\n';
  67. ShowError(SocketActive, "SocketActive.Send");
  68. *(newlogger) << "listening..." << '\n';
  69. *(newlogger) << "SocketActive.Receive = " << hex <<SocketActive.Receive(MAX_PACKET, buf) << '\n';
  70. ShowError(SocketActive, "SocketActive.Receive");
  71. /// @return number of bytes actually received.
  72. /// @return of zero means the connection has been shutdown on the other side.
  73. /// @return of -1 means that an error has occurred.
  74. stringstream ss;
  75. for(int32_t i=0; i<SocketActive.GetBytesReceived(); i++)
  76. {
  77. //cout << " buf[" << ii << "] = " << buf[ii] << " " << endl;
  78. ss << hex << buf[i];
  79. } //for
  80. *(newlogger) << "Bytes recieved: " << ss.str() << '\n';
  81. }// get_API_version
  82. void get_sw_revision(CActiveSocket& SocketActive){ //This function send revision version request and shows response
  83. uint8 buf[MAX_PACKET] ;
  84. buf[0] = uint8(0xAA);
  85. buf[1] = uint8(0xAA);
  86. buf[2] = uint8(0x0D);
  87. buf[3] = uint8(0x00);
  88. *(newlogger) << "GET SW REVISION";
  89. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, 4) << '\n';
  90. ShowError(SocketActive, "SocketActive.Send");
  91. *(newlogger) << "listening..." << '\n';
  92. *(newlogger) << "SocketActive.Receive = " << hex <<SocketActive.Receive(MAX_PACKET, buf) << '\n';
  93. ShowError(SocketActive, "SocketActive.Receive");
  94. /// @return number of bytes actually received.
  95. /// @return of zero means the connection has been shutdown on the other side.
  96. /// @return of -1 means that an error has occurred.
  97. stringstream ss;
  98. for(int32_t ii=0; ii<SocketActive.GetBytesReceived(); ii++)
  99. {
  100. //cout << " buf[" << ii << "] = " << buf[ii] << " " << endl;
  101. ss << hex << buf[ii];
  102. } //for
  103. *(newlogger) << "Bytes Received : " << ss.str() << '\n';
  104. }//get_sw_revision
  105. void get_gru_state(CActiveSocket& SocketActive){ //This function send request of state gradient amplifier and shows response
  106. uint8 buf[MAX_PACKET] ;
  107. buf[0] = uint8(0xAA);
  108. buf[1] = uint8(0xAA);
  109. buf[2] = uint8(0x05);
  110. buf[3] = uint8(0x00);
  111. *(newlogger) << "GET GRU STATE";
  112. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, 4) << '\n';
  113. ShowError(SocketActive, "SocketActive.Send");
  114. *(newlogger) << "listening..." << '\n';
  115. *(newlogger) << "SocketActive.Receive = " << hex <<SocketActive.Receive(MAX_PACKET, buf) << '\n';
  116. ShowError(SocketActive, "SocketActive.Receive");
  117. /// @return number of bytes actually received.
  118. /// @return of zero means the connection has been shutdown on the other side.
  119. /// @return of -1 means that an error has occurred.
  120. stringstream ss;
  121. for(int32_t ii=0; ii<SocketActive.GetBytesReceived(); ii++)
  122. {
  123. //cout << " buf[" << ii << "] = " << buf[ii] << " " << endl;
  124. ss << hex << buf[ii];
  125. } //for
  126. *(newlogger) << "Bytes Received : " << ss.str() << '\n';
  127. }//get_gru_state
  128. void socket_close(CActiveSocket SocketActive){ // This function closes socket
  129. *(newlogger) << "SocketActive.Close() = " << SocketActive.Close() << '\n';
  130. ShowError(SocketActive, "SocketActive.Close");
  131. *(newlogger) << "closed" << '\n';
  132. }
  133. vector<vector<int32_t>> get_nodes(const string& Traject_file_name) {
  134. vector<vector<int32_t>> nodes;
  135. ifstream myfile(Traject_file_name);
  136. if (!myfile.is_open()) { // ��������, ������� �� ������ ����
  137. *(newlogger) << LogPref::Flag(ERROR) << "Unable to open file\n"; // ����� �� ��������� � �������
  138. }
  139. int32_t num1, num2;
  140. while(myfile >> num1 >> num2) {
  141. nodes.push_back({num1, num2});
  142. }
  143. return nodes;
  144. }//get nodes
  145. vector <int32_t> get_unloaded_num(vector<int32_t> segment_status){
  146. vector <int32_t> res;
  147. for (uint32_t i = 0; i< segment_status.size();i++){
  148. if (segment_status[i] != 0){
  149. res.push_back(i);
  150. }
  151. }
  152. return res;
  153. }//get_unloaded_num
  154. //function to upload a segment of traject
  155. void upload_segment(CActiveSocket &SocketActive, int32_t seg_num, bool need_confirm, vector<vector<int32_t>> nodes)
  156. {
  157. int32_t counter=0;
  158. uint8 buf[MAX_PACKET]{0};
  159. string str1 = hex_converting(seg_num);
  160. buf[0] = uint8(0xAA);
  161. buf[1] = uint8(0xAA);
  162. buf[2] = uint8(0x07);
  163. buf[3] = uint8(0x00);
  164. counter+=4;
  165. //data of segment traject
  166. need_confirm = true;
  167. if(need_confirm)
  168. {
  169. buf[4] = uint8(stoi(str1, nullptr, 16));
  170. buf[5] = uint8(0x80);
  171. counter+=2;
  172. }
  173. else{
  174. buf[4] = uint8(stoi(str1, nullptr, 16));
  175. counter++;
  176. }
  177. int32_t NULL32=0;
  178. int32_t NODES_SIZE = nodes.size();
  179. int32_t first_node_idx = max( seg_num * NODES_PER_PACKET - seg_num, NULL32 );
  180. int32_t last_node_idx = min( first_node_idx + NODES_PER_PACKET, NODES_SIZE) - 1;
  181. int32_t nodes_in_this_packet = last_node_idx - first_node_idx + 1;
  182. string str2 = hex_converting(nodes_in_this_packet);
  183. buf[counter++] = uint8(stoi(str2, nullptr, 16));
  184. buf[counter++] = uint8(0x00);
  185. for(int i = first_node_idx; i <last_node_idx+1; i++)
  186. {
  187. string hexString1 = hex_converting(nodes[i][0]);
  188. uint32_t tempcounter = counter;
  189. string temp1, temp2;
  190. for(int j = hexString1.length()-1; j > 0; j = j - 2)
  191. {
  192. buf[tempcounter++] = uint8(stoi(hexString1.substr(j-1,2), nullptr, 16) );
  193. }
  194. if(hexString1.length() % 2 != 0)
  195. {
  196. temp1 = hexString1[0];
  197. hexString1.erase(0);
  198. buf[tempcounter++] = uint8(stoi(temp1, nullptr, 16) );
  199. }
  200. counter+=4;
  201. uint32_t tempcounter2 = counter;
  202. string hexString2 = hex_converting(nodes[i][1]);
  203. for(int j = hexString2.length()-1; j > 0; j -= 2)
  204. {
  205. buf[tempcounter2++] = uint8(stoi(hexString2.substr(j-1,2), nullptr, 16) );
  206. }
  207. if(hexString2.length() % 2 != 0)
  208. {
  209. temp2 = hexString2[0];
  210. hexString1.erase(0);
  211. buf[tempcounter2++] = uint8(stoi(temp2, nullptr, 16) );
  212. }
  213. counter+=2;
  214. }
  215. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, counter) << '\n';
  216. ShowError(SocketActive, "SocketActive.Send");
  217. }
  218. void switch_func (string hexString, int32_t& counter, uint8 (&buf)[MAX_PACKET]){
  219. int tempcounter = counter;
  220. string temp1;
  221. for(int j = hexString.length()-1; j > 0; j=j-2)
  222. {
  223. buf[tempcounter++] = uint8(stoi(hexString.substr(j-1,2), nullptr, 16) );
  224. }
  225. if(hexString.length() % 2 != 0)
  226. {
  227. temp1 = hexString[0];
  228. hexString.erase(0);
  229. buf[tempcounter++] = uint8(stoi(temp1, nullptr, 16) );
  230. }
  231. }
  232. void upload_traj(CActiveSocket& SocketActive, vector<vector<int32_t>> nodes){
  233. //This function send trajectory to gradient amplifier
  234. int32_t counter = 0;
  235. uint8 buf[MAX_PACKET]{0} ;
  236. buf[0] = uint8(0xAA);
  237. buf[1] = uint8(0xAA);
  238. buf[2] = uint8(0x06);
  239. buf[3] = uint8(0x00);
  240. counter+=4;
  241. string hexString = hex_converting(nodes.size());
  242. string hexString2 = hex_converting(nodes[nodes.size()-1][0]);
  243. switch_func(hexString2, counter, buf);
  244. counter+=4;
  245. switch_func(hexString, counter, buf);
  246. counter+=4;
  247. *(newlogger) << "UPLOADING TRAJECTORY";
  248. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, 12) << '\n';
  249. ShowError(SocketActive, "SocketActive.Send");
  250. int32_t nodes_cnt = nodes.size();
  251. int32_t segments_cnt = nodes_cnt / NODES_PER_PACKET; // segments_count
  252. if (nodes_cnt % NODES_PER_PACKET != 0)
  253. segments_cnt += 1;
  254. vector<int32_t> segment_status(segments_cnt); //���������� �� ���������
  255. segment_status.assign(segments_cnt, -2);
  256. int32_t left_wo_confirm{PACKETS_WO_CONFIRM};
  257. //bool confirm_timeout_detected = false;
  258. //string prev_debug_info = " ";
  259. vector <int32_t> uploaded_nums;
  260. uploaded_nums = get_unloaded_num(segment_status);
  261. int32_t counter_uploaded_nums = uploaded_nums.size()-1;
  262. while (!uploaded_nums.empty()){
  263. int32_t seg_num;
  264. seg_num = uploaded_nums.front();
  265. if (segment_status[seg_num] != -2){
  266. *(newlogger) << "Repeating upload segment" << seg_num << "with status" <<segment_status[seg_num] << '\n';
  267. }//if
  268. bool need_confirm = false; //by default
  269. if (left_wo_confirm == 0)
  270. {
  271. left_wo_confirm = PACKETS_WO_CONFIRM;
  272. need_confirm = true;
  273. }
  274. else left_wo_confirm -= 1; //for next iteration
  275. if (seg_num == counter_uploaded_nums)
  276. need_confirm = true;
  277. upload_segment(SocketActive, seg_num, need_confirm, nodes);
  278. *(newlogger) << "listening..." << '\n\n';
  279. *(newlogger) << "SocketActive.Receive = " << hex <<SocketActive.Receive(MAX_PACKET, buf) << '\n';
  280. ShowError(SocketActive, "SocketActive.Receive");
  281. if(int32_t(buf[4]) == seg_num + 1)
  282. {
  283. uploaded_nums.erase(uploaded_nums.begin());
  284. *(newlogger) <<"Uploaded: " << seg_num + 1 << '\n';
  285. }
  286. else
  287. {
  288. *(newlogger) << LogPref::Flag(ERROR) << "Fatal error 404" << '\n';
  289. }
  290. }//while
  291. }//upload_traj
  292. void download_traject (CActiveSocket &SocketActive, int32_t points_cnt)
  293. {
  294. int32_t counter=0;
  295. vector<int32_t> points;
  296. uint8 buf[MAX_PACKET]{0} ;
  297. buf[0] = uint8(0xAA);
  298. buf[1] = uint8(0xAA);
  299. buf[2] = uint8(0x09);
  300. buf[3] = uint8(0x00);
  301. buf[4] = uint8(0x00);
  302. buf[5] = uint8(0x00);
  303. buf[6] = uint8(0x00);
  304. buf[7] = uint8(0x00);
  305. counter+=8;
  306. switch_func(hex_converting(points_cnt), counter, buf);
  307. *(newlogger) << "DOWNLOADING TRAJECTORY";
  308. *(newlogger) << "SocketActive.Send = " << SocketActive.Send(buf, 12) << '\n';
  309. ShowError(SocketActive, "SocketActive.Send");
  310. uint32_t expected_packets_cnt = points_cnt / POINTS_PER_PACKET;
  311. if (points_cnt % POINTS_PER_PACKET != 0)
  312. {
  313. expected_packets_cnt += 1;
  314. }
  315. vector<int32_t> downloaded_segments;
  316. downloaded_segments.assign(expected_packets_cnt, 0);
  317. points.assign(points_cnt, 0);
  318. int32_t for_cnt=0;
  319. int32_t skiped_nums;
  320. FILE *file;
  321. char filename[] = "downloaded_traj.txt";
  322. file = fopen(filename, "a");
  323. if (file == NULL)
  324. {
  325. printf("Error\n", filename);
  326. }
  327. int null_counter=0;
  328. while(!downloaded_segments.empty())
  329. {
  330. skiped_nums = for_cnt == 0 ? 10 : 12;
  331. for_cnt++;
  332. downloaded_segments.pop_back();
  333. *(newlogger) << "listening..." << '\n';
  334. *(newlogger) << "SocketActive.Receive = " << hex <<SocketActive.Receive(MAX_PACKET, buf) << '\n';
  335. ShowError(SocketActive, "SocketActive.Receive");
  336. for(int32_t ii=10; ii<SocketActive.GetBytesReceived(); ii+=2)
  337. {
  338. fprintf(file, "%d\n", int16_t(buf[ii+1] << 8 | buf[ii])) ;
  339. } //for
  340. }
  341. }
  342. } //namespace request
  343. //////////////////////////////////////////////////////////////
  344. int main(int argc, char** argv)
  345. {
  346. newlogger = new SimpleLogger(out, "gra", std::string("gralogs") + std::string(argv[2]));
  347. newlogger->enableConsoleOutput(true);
  348. string Traject_file_name;
  349. if(argc >= 3)
  350. Traject_file_name = argv[1];
  351. else
  352. {
  353. *(newlogger) << LogPref::Flag(ERROR) << "Wrong arguments!";
  354. return -1;
  355. }
  356. SetConsoleCP(866);
  357. SetConsoleOutputCP(866);
  358. int32_t points_cnt;
  359. CActiveSocket SocketActive( CSimpleSocket::CSocketType::SocketTypeUdp) ;
  360. *(newlogger) << "starting" << endl;
  361. // Initialize our socket object
  362. *(newlogger) << "SocketActive.Initialize = " << SocketActive.Initialize() << endl;
  363. request::ShowError(SocketActive, "SocketActive.Initialize");
  364. *(newlogger) << "SocketActive.Open = " << SocketActive.Open(argv[2], 5002) << '\n';
  365. request::ShowError(SocketActive, "SocketActive.Open");
  366. request::get_API_version(SocketActive); // request API version
  367. request::get_sw_revision(SocketActive); // request GRU software version
  368. request::get_gru_state(SocketActive); // request GRU state
  369. auto nodes = request::get_nodes(Traject_file_name); //filling vector nodes from data file
  370. int32_t TEMP_SIZE_NODE = nodes.size(); //using for download
  371. points_cnt = nodes[TEMP_SIZE_NODE-1][0]; //using for download
  372. for(auto &node : nodes)
  373. {
  374. *(newlogger) << node[0] << "\t" << node[1] << '\n'; //vector nodes, output
  375. }
  376. *(newlogger) << "Filled array:" << '\n';
  377. request::upload_traj(SocketActive, nodes); //uploading trajectory
  378. request::download_traject (SocketActive, points_cnt); // downloading trajectory
  379. request::socket_close(SocketActive); // socket closing
  380. newlogger->closeLogger();
  381. return 0;
  382. }//main