requests.cpp 14 KB

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