-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe32if.cpp
More file actions
1504 lines (1215 loc) · 40.4 KB
/
Copy pathe32if.cpp
File metadata and controls
1504 lines (1215 loc) · 40.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "e32if.h"
#include "exception.h"
#include "packet.h"
#include "udp_socket.h"
#include "tcp_socket.h"
#include "bt_socket.h"
#include "util.h"
#include "crypt.h"
#include <dbus-tiny.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <thread>
#include <chrono>
#include <boost/format.hpp>
#include <boost/regex.hpp>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>
#include <boost/json.hpp>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <string.h>
#include <Magick++.h>
using namespace std::chrono_literals;
namespace po = boost::program_options;
static const char *info_board_match_string = "firmware date: ([A-Za-z0-9: ]+), transport mtu: ([0-9]+), display area: ([0-9]+)x([0-9]+)";
E32If::E32If()
{
channel = nullptr;
raw = false;
verbose = false;
debug = false;
mtu = 512;
transport = transport_none;
encryption_key = "default";
stretch_image = false;
}
E32If::~E32If()
{
if(channel)
{
delete channel;
channel = nullptr;
}
}
void E32If::process(const std::string &data, const std::string &oob_data, std::string &reply_data, std::string *reply_oob_data_in,
const char *match, std::vector<std::string> *string_value, std::vector<int> *int_value, int timeout) const
{
std::string packet;
std::string reply_oob_data;
boost::smatch capture;
boost::regex re(match ? match : "");
unsigned int captures;
if((data.length() > this->mtu) || (oob_data.length() > this->mtu))
throw(hard_exception((boost::format("process: data size too large %d/%d/%d") % data.length() % oob_data.length() % this->mtu)));
packet = Packet::encapsulate(!this->raw, data, oob_data);
if(debug)
std::cout << Util::dumper("process: send data", packet) << std::endl;
channel->send(packet, timeout);
channel->receive(packet, timeout);
if(debug)
std::cout << Util::dumper("process: receive data", packet) << std::endl;
Packet::decapsulate(!this->raw, packet, reply_data, reply_oob_data);
if(reply_oob_data_in)
*reply_oob_data_in = reply_oob_data;
if(match && !boost::regex_match(reply_data, capture, re))
throw(transient_exception(boost::format("received string does not match: \"%s\" vs. \"%s\"") % Util::dumper("reply", reply_data) % match));
if(string_value || int_value)
{
if(string_value)
string_value->clear();
if(int_value)
int_value->clear();
captures = 0;
for(const auto &it : capture)
{
if(captures++ == 0)
continue;
if(string_value)
string_value->push_back(it);
if(int_value)
{
try
{
int_value->push_back(stoi(it, 0, 0));
}
catch(...)
{
int_value->push_back(0);
}
}
}
}
if(debug)
{
std::cout << Util::dumper("process: reply data", reply_data) << std::endl;
if(reply_oob_data.length())
std::cout << reply_oob_data.length () << " bytes OOB data received" << std::endl;
}
}
std::string E32If::get()
{
std::string rv = output;
output.clear();
return(rv);
}
std::string E32If::hostname() const
{
return(this->host);
}
void E32If::run(const std::vector<std::string> &args)
{
_run(args);
}
void E32If::run(int argc, const char * const *argv)
{
int ix;
std::vector<std::string> args;
for(ix = 1; ix < argc; ix++)
args.push_back(std::string(argv[ix]));
_run(args);
}
void E32If::run(const std::string &args)
{
std::vector<std::string> args_split;
args_split = po::split_unix(args);
_run(args_split);
}
void E32If::_run(const std::vector<std::string> &argv_in)
{
po::options_description options("e32if usage");
try
{
bool option_raw = false;
bool option_noprobe = false;
unsigned int force_mtu = 0;
bool option_verbose = false;
bool option_debug = false;
bool option_stretch_image = false;
std::vector<std::string> argv;
std::vector<std::string> host_args;
std::vector<std::string> proxy_signal_ids;
std::string args;
std::string option_command_port;
std::string filename;
std::string directory;
std::string start_string;
std::string transport_string = "udp";
unsigned int timeout = 0;
std::string page_id;
std::string page_text;
bool cmd_ota = false;
bool cmd_text = false;
bool cmd_image = false;
bool cmd_write_file = false;
bool cmd_read_file = false;
bool cmd_proxy = false;
bool cmd_perf_test_write = false;
bool cmd_perf_test_read = false;
unsigned int selected;
for(std::vector<std::string>::const_iterator arg = argv_in.begin(); arg != argv_in.end(); arg++)
{
if(arg->length() && (arg->at(0) == '@'))
{
std::string include_filename;
std::ifstream file;
std::stringstream stream;
std::string contents;
typedef boost::char_separator<char> separator_t;
separator_t separator(" \t\n");
typedef boost::tokenizer<separator_t> tokenizer_t;
tokenizer_t tokenizer(std::string(""), separator);
include_filename = arg->substr(1, std::string::npos);
file.open(include_filename);
if(!file.is_open())
{
std::cerr << "warning: cannot open include file \"" << include_filename << "\"\n";
continue;
}
stream << file.rdbuf();
contents = stream.str();
tokenizer.assign(contents);
for(tokenizer_t::const_iterator token = tokenizer.begin(); token != tokenizer.end(); token++)
argv.push_back(*token);
file.close();
}
else
argv.push_back(*arg);
}
options.add_options()
("text", po::bool_switch(&cmd_text)->implicit_value(true), "add text page")
("image", po::bool_switch(&cmd_image)->implicit_value(true), "add image page")
("ota", po::bool_switch(&cmd_ota)->implicit_value(true), "OTA write")
("write-file", po::bool_switch(&cmd_write_file)->implicit_value(true), "WRITE file")
("read-file", po::bool_switch(&cmd_read_file)->implicit_value(true), "READ file")
("proxy", po::bool_switch(&cmd_proxy)->implicit_value(true), "run proxy")
("proxy-signal-id", po::value<std::vector<std::string> >(&proxy_signal_ids), "dbus signals to listen to")
("host", po::value<std::vector<std::string> >(&host_args)->required(), "host to connect to")
("verbose", po::bool_switch(&option_verbose)->implicit_value(true), "verbose output")
("debug", po::bool_switch(&option_debug)->implicit_value(true), "packet trace etc.")
("stretch", po::bool_switch(&option_stretch_image)->implicit_value(true), "stretch images")
("transport", po::value<std::string>(&transport_string), "select transport: udp (default), tcp or bluetooth (bt)")
("page-id", po::value<std::string>(&page_id), "name of info page")
("page-text", po::value<std::string>(&page_text), "contents of text page")
("timeout", po::value<unsigned int>(&timeout), "timeout of text page in seconds")
("filename", po::value<std::string>(&filename), "file")
("directory", po::value<std::string>(&directory), "remote directory")
("command-port", po::value<std::string>(&option_command_port)->default_value("24"), "command port to connect to")
("raw", po::bool_switch(&option_raw)->implicit_value(true), "do not use packet encapsulation")
("noprobe", po::bool_switch(&option_noprobe)->implicit_value(true), "skip probe for board information (mtu)")
("mtu", po::value<unsigned int>(&force_mtu), "force mtu")
("key", po::value<std::string>(&encryption_key), "set key for bluetooth")
("pw", po::bool_switch(&cmd_perf_test_write)->implicit_value(true), "performance test WRITE")
("pr", po::bool_switch(&cmd_perf_test_read)->implicit_value(true), "performance test READ");
po::positional_options_description positional_options;
positional_options.add("host", -1);
po::variables_map varmap;
auto parsed = po::command_line_parser(argv).options(options).positional(positional_options).run();
po::store(parsed, varmap);
po::store(po::parse_environment(options, "E32IF_"), varmap);
po::notify(varmap);
auto it = host_args.begin();
this->host = *(it++);
auto it1 = it;
for(; it != host_args.end(); it++)
{
if(it != it1)
args.append(" ");
args.append(*it);
}
if((this->host.length() == 17) && (this->host.at(2) == ':') && (this->host.at(5) == ':') &&
(this->host.at(8) == ':') && (this->host.at(11) == ':') && (this->host.at(14) == ':'))
transport_string = "bluetooth";
selected = 0;
if(cmd_text)
selected++;
if(cmd_image)
selected++;
if(cmd_ota)
selected++;
if(cmd_read_file)
selected++;
if(cmd_proxy)
selected++;
if(cmd_write_file)
selected++;
if(cmd_perf_test_read)
{
option_command_port = "19"; // chargen
selected++;
}
if(cmd_perf_test_write)
{
option_command_port = "9"; // discard
selected++;
}
if(selected > 1)
throw(hard_exception("specify one command"));
if((transport_string == "bt") || (transport_string == "bluetooth"))
this->transport = transport_bluetooth;
else
if(transport_string == "tcp")
this->transport = transport_tcp_ip;
else
if(transport_string == "udp")
this->transport = transport_udp_ip;
else
throw(hard_exception("unknown transport, use bluetooth/bt, udp or ip"));
this->raw = option_raw;
this->verbose = option_verbose;
this->debug = option_debug;
this->noprobe = option_noprobe;
this->stretch_image = option_stretch_image;
this->command_port = option_command_port;
struct timeval tv;
gettimeofday(&tv, nullptr);
if(channel)
{
delete channel;
channel = nullptr;
}
switch(this->transport)
{
case(transport_tcp_ip):
{
channel = new TCPSocket(option_verbose, option_debug);
break;
}
case(transport_udp_ip):
{
channel = new UDPSocket(option_verbose, option_debug);
break;
}
case(transport_bluetooth):
{
channel = new BTSocket(option_verbose, option_debug);
break;
}
default:
{
throw(hard_exception("e32if: unknown transport"));
}
}
if(cmd_proxy)
this->run_proxy(proxy_signal_ids, force_mtu);
else
{
channel->connect(this->host, this->command_port, this->encryption_key);
if(!noprobe)
{
std::string reply;
std::vector<int> int_value;
std::vector<std::string> string_value;
process("info-board", "", reply, nullptr, info_board_match_string, &string_value, &int_value);
if(force_mtu)
this->mtu = force_mtu;
else
mtu = int_value[1];
x_size = int_value[2];
y_size = int_value[3];
if(verbose)
{
std::cout << "transport: " << transport_string << std::endl;
std::cout << "mtu: " << mtu << std::endl;
std::cout << "display dimensions: " << x_size << "x" << y_size << std::endl;
}
}
else
{
x_size = 0;
y_size = 0;
}
if(selected == 0)
output = this->send_text(args);
else
if(cmd_perf_test_write)
output = this->perf_test_write();
else
if(cmd_perf_test_read)
output = this->perf_test_read();
else
if(cmd_text)
this->text(page_id, timeout, page_text);
else
if(cmd_image)
this->image(page_id, timeout, directory, filename, stretch_image);
else
if(cmd_ota)
this->ota(filename);
else
if(cmd_read_file)
this->read_file(directory, filename);
else
if(cmd_write_file)
this->write_file(directory, filename);
channel->disconnect();
}
}
catch(const DbusTinyException &e)
{
throw((boost::format("e32if: DbusTiny exception: %s\n%s") % e.what()).str());
}
catch(const po::error &e)
{
throw((boost::format("e32if: program option exception: %s\n%s") % e.what() % options).str());
}
catch(const hard_exception &e)
{
throw((boost::format("e32if: error: %s") % e.what()).str());
}
catch(const transient_exception &e)
{
throw((boost::format("e32if: transient exception: %s") % e.what()).str());
}
catch(const e32if_exception &e)
{
throw((boost::format("e32if: unknown generic e32if exception: %s") % e.what()).str());
}
catch(const std::exception &e)
{
throw((boost::format("e32if: standard exception: %s") % e.what()).str());
}
catch(const std::string &e)
{
throw((boost::format("e32if: unknown standard string exception: %s ") % e).str());
}
catch(const char *e)
{
throw((boost::format("e32if: unknown string exception: %s") % e).str());
}
catch(...)
{
throw(std::string("e32if: unknown exception"));
}
}
void E32If::ota(std::string filename) const
{
int file_fd, chunk_size;
unsigned int offset, length;
struct timeval time_start, time_now;
std::string command;
std::string reply;
std::vector<std::string> string_value;
std::vector<int> int_value;
struct stat stat;
std::string local_hash;
std::string local_hash_text;
std::string remote_hash_text;
std::string buffer;
std::string firmware_version;
int seconds, useconds;
unsigned int attempt;
double duration;
Crypt::SHA256 sha;
if(filename.empty())
throw(hard_exception("filename required"));
if((file_fd = open(filename.c_str(), O_RDONLY, 0)) < 0)
throw(hard_exception("file not found"));
try
{
fstat(file_fd, &stat);
length = stat.st_size;
if(length < 32)
throw(hard_exception("file too short (< 32 bytes)"));
gettimeofday(&time_start, 0);
process((boost::format("ota-start %u") % length).str(), "", reply, nullptr, "OK start write ota to partition ([0-9]+)/([a-zA-Z0-9_ -]+)", &string_value, &int_value, 10000);
std::cout << (boost::format("start ota to [%u]: \"%s\", length: %u kB\n") % int_value[0] % string_value[1] % (length / 1024));
sha.init();
for(offset = 0; offset < length;)
{
if(offset == (length - 32))
chunk_size = length - offset;
else
if((offset + this->mtu) >= (length - 32))
chunk_size = length - 32 - offset;
else
chunk_size = this->mtu;
buffer.resize(chunk_size);
if((chunk_size = ::read(file_fd, buffer.data(), buffer.size())) <= 0)
throw(hard_exception("i/o error in read"));
buffer.resize(chunk_size);
offset += chunk_size;
if(offset < length)
sha.update(buffer);
command = (boost::format("ota-write %u %u") % chunk_size % ((offset >= length) ? 1 : 0)).str();
gettimeofday(&time_now, 0);
seconds = time_now.tv_sec - time_start.tv_sec;
useconds = time_now.tv_usec - time_start.tv_usec;
duration = seconds + (useconds / 1000000.0);
std::cout << boost::format("sent %4u kbytes in %3.0f seconds at rate %3.0f kbytes/s, sent %3u kB, %3u%% \r") %
(offset / 1024) %
duration %
(offset / 1024 / duration) %
(offset / 1024) %
(offset * 100 / length);
std::cout.flush();
process(command, buffer, reply, nullptr, "OK write ota", nullptr, nullptr, 10000);
}
}
catch(...)
{
std::cout << std::endl;
close(file_fd);
throw;
}
close(file_fd);
std::cout << std::endl;
process("ota-finish", "", reply, nullptr, "OK finish ota, checksum: ([^ ]+)", &string_value);
remote_hash_text = string_value[0];
local_hash = sha.finish();
local_hash_text = Crypt::hash_to_text(local_hash);
if(local_hash_text != remote_hash_text)
throw(hard_exception(boost::format("incorrect checksum, local: %s, remote: %s") % local_hash_text % remote_hash_text));
std::cout << "checksum OK: " << local_hash_text << "\n";
process((boost::format("ota-commit %s") % local_hash_text).str(), "", reply, nullptr, "OK commit ota");
std::cout << "OTA write finished, rebooting" << std::endl;
try
{
process("reset", "", reply, nullptr, nullptr, nullptr, nullptr, 0);
}
catch(const transient_exception &e)
{
if(verbose)
{
std::cout << " reset returned transient error: " << e.what();
std::cout << std::endl;
}
}
catch(const hard_exception &e)
{
if(verbose)
{
std::cout << " reset returned error: " << e.what();
std::cout << std::endl;
}
}
std::cout << "reconnecting";
std::cout.flush();
channel->disconnect();
for(attempt = 0; attempt < 16; attempt++)
{
try
{
channel->connect();
process("info-board", "", reply, nullptr, info_board_match_string, &string_value, &int_value);
firmware_version = string_value[0];
}
catch(const transient_exception &e)
{
std::cout << ".";
std::cout.flush();
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
break;
}
if(attempt >= 16)
throw(hard_exception("ota: reconnect failed"));
std::cout << "\nconnected\n";
std::cout << "reboot finished, confirming boot slot" << std::endl;
process("ota-confirm", "", reply, nullptr, "OK confirm ota");
std::cout << boost::format("firmware version: %s") % firmware_version << std::endl;
}
std::string E32If::send_text(std::string arg) const
{
std::string send_data;
std::string reply;
std::string reply_oob;
std::string local_output;
process(arg, "", reply, &reply_oob);
local_output.append(reply);
if(reply_oob.length() > 0)
{
unsigned int length = 0;
local_output.append((boost::format("\n%u bytes of OOB data: ") % reply_oob.length()).str());
for(const auto &it : reply_oob)
{
if((length++ % 20) == 0)
local_output.append("\n ");
local_output.append((boost::format("0x%02x ") % (((unsigned int)it) & 0xff)).str());
}
}
local_output.append("\n");
return(local_output);
}
void E32If::read_file(std::string directory, std::string filename)
{
int file_fd;
unsigned int offset;
struct timeval time_start, time_now;
std::string command;
std::string reply;
std::string oob;
std::vector<std::string> string_value;
std::vector<int> int_value;
std::string partition;
Crypt::SHA256 sha;
std::string local_hash_text;
std::string remote_hash_text;
unsigned int pos;
unsigned int chunk_size;
unsigned int received_chunk_size;
int length;
int seconds, useconds;
double duration;
if(filename.empty())
throw(hard_exception("filename required"));
if(directory.empty())
throw(hard_exception("remote directory (fs) required"));
if((file_fd = open(filename.c_str(), O_WRONLY | O_CREAT, 0666)) < 0)
throw(hard_exception("can't create file"));
if((pos = filename.find_last_of('/')) != std::string::npos)
filename = filename.substr(pos + 1);
filename = directory + "/" + filename;
try
{
chunk_size = this->mtu;
gettimeofday(&time_start, 0);
sha.init();
for(offset = 0;;)
{
command = (boost::format("fs-read %u %s %s") % chunk_size % offset % filename).str();
if(!debug)
{
gettimeofday(&time_now, 0);
seconds = time_now.tv_sec - time_start.tv_sec;
useconds = time_now.tv_usec - time_start.tv_usec;
duration = seconds + (useconds / 1000000.0);
std::cout << boost::format("received %4u kbytes in %3.0f seconds at rate %3.0f kbytes/s, received %3u kB, \r") %
(offset / 1024) % duration % (offset / 1024 / duration) % (offset / 1024);
std::cout.flush();
}
oob.clear();
process(command, "", reply, &oob, "OK chunk read: ([0-9]+)", nullptr, &int_value);
received_chunk_size = int_value[0];
if(received_chunk_size != oob.length())
throw(hard_exception(boost::format("chunk size [%u] differs from oob size [%u]") % received_chunk_size % oob.length()));
if(received_chunk_size == 0)
break;
if((length = ::write(file_fd, oob.data(), oob.length())) != (int)oob.length())
throw(hard_exception("i/o error in write"));
offset += length;
sha.update(oob);
}
}
catch(...)
{
std::cout << std::endl;
close(file_fd);
throw;
}
close(file_fd);
local_hash_text = Crypt::hash_to_text(sha.finish());
process(std::string("fs-checksum ") + filename, "", reply, nullptr, "OK checksum: ([0-9a-f]+)", &string_value, &int_value);
remote_hash_text = string_value[0];
if(local_hash_text != remote_hash_text)
throw(hard_exception(boost::format("checksum failed: SHA256 hash differs, local: %u, remote: %s") % local_hash_text % remote_hash_text));
std::cout << std::endl;
}
unsigned int E32If::write_file(std::string directory, std::string filename)
{
std::string swap_filename;
int file_fd;
unsigned int offset, length;
struct timeval time_start, time_now;
std::string command;
std::string reply;
std::vector<std::string> string_value;
std::vector<int> int_value;
struct stat stat;
std::string partition;
Crypt::SHA256 sha;
std::string local_hash_text;
std::string remote_hash_text;
unsigned int pos;
unsigned int chunk_size;
int read_chunk_size;
std::string buffer;
int seconds, useconds;
double duration;
if(filename.empty())
throw(hard_exception("filename required"));
if(directory.empty())
throw(hard_exception("remote directory (fs) required"));
if((file_fd = open(filename.c_str(), O_RDONLY, 0)) < 0)
throw(hard_exception("file not found"));
if((pos = filename.find_last_of('/')) != std::string::npos)
filename = filename.substr(pos + 1);
filename = directory + "/" + filename;
swap_filename = filename + ".tmp";
try
{
chunk_size = this->mtu;
buffer.resize(chunk_size);
fstat(file_fd, &stat);
length = stat.st_size;
gettimeofday(&time_start, 0);
sha.init();
process((boost::format("fs-rename %s %s") % filename % swap_filename).str(), std::string(""), reply);
for(offset = 0; offset < length;)
{
if((read_chunk_size = ::read(file_fd, buffer.data(), buffer.size())) <= 0)
throw(hard_exception("i/o error in read"));
buffer.resize(read_chunk_size);
command = (boost::format("fs-write %u %u %s") % ((offset == 0) ? 0 : 1) % read_chunk_size % swap_filename).str();
offset += read_chunk_size;
sha.update(buffer);
if(!debug)
{
gettimeofday(&time_now, 0);
seconds = time_now.tv_sec - time_start.tv_sec;
useconds = time_now.tv_usec - time_start.tv_usec;
duration = seconds + (useconds / 1000000.0);
std::cout << boost::format("sent %4u kbytes in %3.0f seconds at rate %3.0f kbytes/s, sent %3u kB, %3u%% \r") %
(offset / 1024) % duration % (offset / 1024 / duration) % (offset / 1024) % (offset * 100 / length);
std::cout.flush();
}
process(command, buffer, reply, nullptr, "OK file length: ([0-9]+)", &string_value, &int_value);
if(int_value[0] != (int)offset)
throw(hard_exception(boost::format("remote file length [%u] != local offset [%u]\n") % int_value[0] % offset));
}
}
catch(...)
{
std::cout << std::endl;
close(file_fd);
throw;
}
close(file_fd);
local_hash_text = Crypt::hash_to_text(sha.finish());
process(std::string("fs-checksum ") + swap_filename, "", reply, nullptr, "OK checksum: ([0-9a-f]+)", &string_value, &int_value);
remote_hash_text = string_value[0];
if(local_hash_text != remote_hash_text)
{
process((boost::format("fs-remove %s %s") % swap_filename).str(), std::string(""), reply);
throw(hard_exception(boost::format("checksum failed: SHA256 hash differs, local: %u, remote: %s") % local_hash_text % remote_hash_text));
}
process((boost::format("fs-rename %s %s") % swap_filename % filename).str(), std::string(""), reply);
std::cout << std::endl;
return(length);
}
E32If::ProxyThread::ProxyThread(E32If &e32if_in, const std::vector<std::string> &signal_ids_in) : e32if(e32if_in), signal_ids(signal_ids_in)
{
}
void E32If::ProxyThread::operator()()
{
std::string message_type;
std::string message_interface;
std::string message_method;
std::string error;
std::string reply;
std::string time_string;
std::string service = (boost::format("%s.%s") % dbus_service_id % e32if.hostname()).str();
try
{
DbusTinyServer dbus_tiny_server(service);
for(const auto &it : signal_ids)
{
if(e32if.verbose)
std::cout << "adding signal filter: " << it << std::endl;
dbus_tiny_server.register_signal((boost::format("%s.%s.%s") % dbus_service_id % "signal" % it).str());
}
for(;;)
{
try
{
dbus_tiny_server.get_message(message_type, message_interface, message_method);
if(message_type == "method call")
{
if(e32if.verbose)
std::cout << boost::format("message received, interface: %s, method: %s\n") % message_interface % message_method;
if(message_interface == "org.freedesktop.DBus.Introspectable")
{
if(message_method == "Introspect")
{
reply += std::string("") +
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n" +
"<node>\n" +
" <interface name=\"org.freedesktop.DBus.Introspectable\">\n" +
" <method name=\"Introspect\">\n" +
" <arg name=\"xml\" type=\"s\" direction=\"out\"/>\n" +
" </method>\n" +
" </interface>\n" +
" <interface name=\"" + service + "\">\n" +
" <method name=\"dump\">\n" +
" <arg name=\"info\" type=\"s\" direction=\"out\"/>\n" +
" </method>\n" +
" <method name=\"get_sensor_data\">\n" +
" <arg name=\"module\" type=\"u\" direction=\"in\"/>\n" +
" <arg name=\"bus\" type=\"u\" direction=\"in\"/>\n" +
" <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" +
" <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" +
" <arg name=\"time\" type=\"t\" direction=\"out\"/>\n" +
" <arg name=\"id\" type=\"u\" direction=\"out\"/>\n" +
" <arg name=\"address\" type=\"u\" direction=\"out\"/>\n" +
" <arg name=\"unity\" type=\"s\" direction=\"out\"/>\n" +
" <arg name=\"value\" type=\"d\" direction=\"out\"/>\n" +
" </method>\n" +
" <method name=\"push_command\">\n" +
" <arg name=\"command\" type=\"s\" direction=\"in\"/>\n" +
" <arg name=\"status\" type=\"s\" direction=\"out\"/>\n" +
" </method>\n" +
" </interface>\n";
for(const auto &it : signal_ids)
{
reply += std::string("") +
" <interface name=\"" + dbus_service_id + ".signal." + it + "\">\n" +
" <signal name=\"push_command\">\n" +
" <arg name=\"command\" type=\"s\"/>\n" +
" </signal>\n" +
" </interface>\n";
}
reply += "</node>\n";
dbus_tiny_server.send_string(reply);
reply.clear();
}
else
throw(transient_exception(dbus_tiny_server.inform_error(std::string("unknown introspection method called"))));
}
else
{
if((message_interface == dbus_service_id) || (message_interface == ""))
{
if(message_method == "dump")
{
reply += (boost::format("CONNECTED: %s\n\n") % (e32if.proxy_connected ? "yes" : "no")).str();
reply += "SENSOR DATA\n\n";
for(const auto &it : e32if.proxy_sensor_data)
{
Util::time_to_string(time_string, it.second.time);
reply += (boost::format("> %1u %1u %-16s %-16s / %2u @ %02x %8.2f %-3s %s\n") %
it.first.module % it.first.bus % it.first.name % it.first.type %
it.second.id % it.second.address % it.second.value % it.second.unity % time_string).str();
}
reply += "\nCOMMANDS\n\n";
for(const auto &it : e32if.proxy_commands)
{
Util::time_to_string(time_string, it.time);
reply += (boost::format("> %s from %s at %s\n") % it.command % it.source % time_string).str();
}
dbus_tiny_server.send_string(reply);