… | |
… | |
18 | } |
18 | } |
19 | |
19 | |
20 | use common::sense; |
20 | use common::sense; |
21 | use Carp 'verbose'; |
21 | use Carp 'verbose'; |
22 | use Cwd (); |
22 | use Cwd (); |
|
|
23 | use Digest::MD5 (); |
23 | use EV; |
24 | use EV; |
24 | BEGIN { *time = \&EV::time } |
25 | BEGIN { *time = \&EV::time } |
25 | |
26 | |
26 | use List::Util qw(max min); |
27 | use List::Util qw(max min); |
27 | |
28 | |
… | |
… | |
527 | } |
528 | } |
528 | |
529 | |
529 | ############################################################################# |
530 | ############################################################################# |
530 | # Over-the-air updates |
531 | # Over-the-air updates |
531 | |
532 | |
532 | sub ota_update_finish { |
|
|
533 | $MESSAGE_DIST->message ({ type => "ota_update", markup => $_[0] }); |
|
|
534 | } |
|
|
535 | |
|
|
536 | sub ota_update_status { |
|
|
537 | } |
|
|
538 | |
|
|
539 | sub ota_update { |
533 | sub ota_update { |
540 | my ($face, $size) = @_; |
534 | my ($face, $size, $md5) = @_; |
541 | |
535 | |
542 | my $coro = Coro::async_pool { |
536 | my $coro = Coro::async_pool { |
543 | my $override = "$Urlader::EXE_DIR/override"; |
537 | my $override = "$Urlader::EXE_DIR/override"; |
544 | |
538 | |
545 | $MESSAGE_DIST->add_channel ({ |
539 | $MESSAGE_DIST->add_channel ({ |
… | |
… | |
551 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." }); |
545 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." }); |
552 | |
546 | |
553 | my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777; |
547 | my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777; |
554 | |
548 | |
555 | unless ($fh) { |
549 | unless ($fh) { |
556 | ota_update_finish "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!"; |
550 | $MESSAGE_DIST->message ({ type => "ota_update", markup => (DC::asxml "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!") }); |
557 | return; |
551 | return; |
558 | } |
552 | } |
559 | |
553 | |
560 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." }); |
554 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." }); |
561 | |
555 | |
562 | my $cv = AE::cv; |
556 | my $cv = AE::cv; |
563 | my $error; |
557 | my $error; |
564 | |
558 | |
565 | $cv->begin (Coro::rouse_cb); |
559 | $cv->begin (Coro::rouse_cb); |
566 | |
|
|
567 | $CONN->ask_face ( |
560 | $CONN->ask_face ( |
568 | $face, |
561 | $face, |
569 | -1000, |
562 | -1000, |
570 | sub { |
563 | sub { |
571 | $STATUSBOX->add ( |
564 | $STATUSBOX->add ( |
… | |
… | |
596 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." }); |
589 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." }); |
597 | Coro::AIO::aio_unlink "$override.tmp"; |
590 | Coro::AIO::aio_unlink "$override.tmp"; |
598 | return; |
591 | return; |
599 | } |
592 | } |
600 | |
593 | |
|
|
594 | { |
|
|
595 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "verifying update file..." }); |
|
|
596 | |
|
|
597 | my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_RDONLY, 0; |
|
|
598 | |
|
|
599 | if ($fh) { |
|
|
600 | $error ||= Coro::AIO::aio_stat "$override.tmp"; |
|
|
601 | $error ||= -s _ != $size; |
|
|
602 | $error ||= Coro::AIO::aio_readahead $fh, 0, $size; |
|
|
603 | |
|
|
604 | my $f_md5 = new Digest::MD5; |
|
|
605 | $f_md5->addfile ($fh); |
|
|
606 | $f_md5 = $f_md5->hexdigest; |
|
|
607 | $error ||= $md5 ne $f_md5; |
|
|
608 | } |
|
|
609 | } |
|
|
610 | |
|
|
611 | if ($error) { |
|
|
612 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "verification failed, update aborted." }); |
|
|
613 | Coro::AIO::aio_unlink "$override.tmp"; |
|
|
614 | return; |
|
|
615 | } |
|
|
616 | |
601 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." }); |
617 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." }); |
602 | |
618 | |
603 | if (Coro::AIO::aio_rename "$override.tmp", $override) { |
619 | if (Coro::AIO::aio_rename "$override.tmp", $override) { |
604 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." }); |
620 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." }); |
605 | Coro::AIO::aio_unlink "$override.tmp"; |
621 | Coro::AIO::aio_unlink "$override.tmp"; |
606 | } |
622 | } |
607 | |
623 | |
608 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "update successfull, changes become active at next start." }); |
624 | $MESSAGE_DIST->message ({ type => "ota_update", markup => "success - update becomes active after restarting." }); |
609 | }; |
625 | }; |
610 | |
626 | |
611 | $CONN->{ota_update} = Guard::guard { |
627 | $CONN->{ota_update} = Guard::guard { |
612 | $coro->cancel; |
628 | $coro->cancel; |
613 | }; |
629 | }; |
614 | } |
630 | } |
615 | |
631 | |
616 | sub ota_update_ask { |
632 | sub ota_update_ask { |
617 | my ($ok, $face, $ver, $size, $changes) = @_; |
633 | my ($ok, $face, $ver, $size, $md5, $changes) = @_; |
618 | |
634 | |
619 | $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel |
635 | $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel |
620 | x => "center", |
636 | x => "center", |
621 | y => "center", |
637 | y => "center", |
|
|
638 | z => 55, |
622 | max_w => $::WIDTH * 0.7, |
639 | force_w => $::WIDTH * 0.7, |
623 | max_h => $::WIDTH * 0.7, |
640 | force_h => $::HEIGHT * 0.7, |
624 | title => "Software update available", |
641 | title => "Software update available", |
625 | child => my $vbox = new DC::UI::VBox, |
642 | child => my $vbox = new DC::UI::VBox, |
626 | ; |
643 | ; |
627 | |
644 | |
628 | $vbox->add (new DC::UI::Label |
645 | $vbox->add (new DC::UI::Label |
629 | ellipsise => 0, |
646 | ellipsise => 0, |
630 | text => "The server offers a software update, " |
647 | text => "The server offers a software update, " |
631 | . "do you want to start downloading this update in the background?", |
648 | . "do you want to start downloading this update in the background?", |
632 | ); |
649 | ); |
633 | |
650 | |
634 | $vbox->add (new DC::UI::FancyFrame |
651 | $vbox->add (new DC::UI::FancyFrame |
635 | expand => 1, |
652 | expand => 1, |
636 | label => "Changes", |
653 | label => "Details", |
637 | child => (new DC::UI::TextScroller |
654 | child => (new DC::UI::TextScroller |
638 | expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4, |
655 | expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4, |
639 | par => [{ |
656 | par => [{ |
640 | markup => "<tt>Old revision: $Urlader::EXE_VER\n" |
657 | markup => "<tt>Old revision: $Urlader::EXE_VER\n" |
641 | . "New revision: $ver\n" |
658 | . "New revision: $ver\n" |
642 | . "Download size: $size bytes</tt>\n\n" |
659 | . "Download size: $size bytes</tt>\n\n" |
|
|
660 | . "Changes:\n\n" |
643 | . DC::asxml $changes |
661 | . DC::asxml $changes |
644 | }], |
662 | }], |
645 | ), |
663 | ), |
646 | ); |
664 | ); |
647 | |
665 | |
… | |
… | |
658 | $hbox->add (new DC::UI::Button |
676 | $hbox->add (new DC::UI::Button |
659 | expand => 1, |
677 | expand => 1, |
660 | text => "Yes, start downloading", |
678 | text => "Yes, start downloading", |
661 | on_activate => sub { |
679 | on_activate => sub { |
662 | $dialog->destroy; |
680 | $dialog->destroy; |
663 | ota_update $face, $size; |
681 | ota_update $face, $size, $md5; |
664 | 0 |
682 | 0 |
665 | }, |
683 | }, |
666 | ); |
684 | ); |
667 | |
685 | |
668 | $dialog->show; |
686 | $dialog->show; |
… | |
… | |
672 | return unless defined $Urlader::EXE_ID; |
690 | return unless defined $Urlader::EXE_ID; |
673 | |
691 | |
674 | ::message { markup => "Checking for software update..." }; |
692 | ::message { markup => "Checking for software update..." }; |
675 | |
693 | |
676 | $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub { |
694 | $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub { |
677 | my ($ok, $face, $ver, $size, $changes) = @_; |
695 | my ($ok, $face, $ver, $size, $md5, $changes) = @_; |
678 | |
696 | |
679 | if ($ok) { |
697 | if ($ok) { |
680 | if (defined $ver) { |
698 | if (defined $ver) { |
681 | ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." }; |
699 | ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." }; |
682 | &ota_update_ask; |
700 | &ota_update_ask; |
… | |
… | |
2453 | sub open_quit_dialog { |
2471 | sub open_quit_dialog { |
2454 | unless ($QUIT_DIALOG) { |
2472 | unless ($QUIT_DIALOG) { |
2455 | $QUIT_DIALOG = new DC::UI::Toplevel |
2473 | $QUIT_DIALOG = new DC::UI::Toplevel |
2456 | x => "center", |
2474 | x => "center", |
2457 | y => "center", |
2475 | y => "center", |
2458 | z => 50, |
2476 | z => 60, |
2459 | title => "Really Quit?", |
2477 | title => "Really Quit?", |
2460 | on_key_down => sub { |
2478 | on_key_down => sub { |
2461 | my ($dialog, $ev) = @_; |
2479 | my ($dialog, $ev) = @_; |
2462 | $ev->{sym} == 27 and $dialog->hide; |
2480 | $ev->{sym} == 27 and $dialog->hide; |
2463 | } |
2481 | } |