debian: bumped opensync 0.4x plugin to compat 6 (with similar fixes)
[barry/progweb.git] / src / m_javaloader.cc
blobcafe0a808f138e95356851865f000a98132e1aae
1 ///
2 /// \file m_javaloader.cc
3 /// Mode class for the JavaLoader mode
4 ///
6 /*
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2008-2009, Nicolas VIVIEN
10 Some parts are inspired from m_desktop.h
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
25 #include "m_javaloader.h"
26 #include "data.h"
27 #include "protocol.h"
28 #include "protostructs.h"
29 #include "packet.h"
30 #include "endian.h"
31 #include "error.h"
32 #include "usbwrap.h"
33 #include "controller.h"
34 #include "cod.h"
35 #include <stdexcept>
36 #include <sstream>
37 #include <iomanip>
38 #include <vector>
39 #include <string.h>
40 #include <time.h>
41 #include <stdio.h>
42 #include "ios_state.h"
44 #include "debug.h"
46 using namespace std;
48 namespace Barry {
51 ///////////////////////////////////////////////////////////////////////////////
52 // JLScreenInfo class
54 JLScreenInfo::JLScreenInfo()
58 JLScreenInfo::~JLScreenInfo()
64 ///////////////////////////////////////////////////////////////////////////////
65 // JLDirectory class
67 JLDirectory::JLDirectory(int level)
68 : m_level(level)
72 JLDirectory::~JLDirectory()
76 void JLDirectory::ParseTable(const Data &table_packet)
78 m_idTable.clear();
80 size_t count = table_packet.GetSize() / 2;
81 uint16_t *item = (uint16_t*) table_packet.GetData();
82 for( size_t i = 0; i < count; i++, item++ ) {
83 m_idTable.push_back( be_btohs(*item) );
87 void JLDirectory::Dump(std::ostream &os) const
89 ios_format_state state(os);
91 int indent = m_level * 2;
93 os << setfill(' ') << setw(indent) << "";
94 os << "Directory: " << m_idTable.size() << "/" << size() << " entries\n";
96 const_iterator i = begin(), e = end();
97 for( ; i != e; ++i ) {
98 os << setfill(' ') << setw(indent + 2) << "";
99 os << *i << "\n";
105 ///////////////////////////////////////////////////////////////////////////////
106 // JLDirectoryEntry class
108 JLDirectoryEntry::JLDirectoryEntry(int level)
109 : m_level(level)
110 , SubDir(level + 1)
114 void JLDirectoryEntry::Parse(uint16_t id, const Data &entry_packet)
116 size_t needed = SB_JLDIRENTRY_HEADER_SIZE;
117 size_t have = entry_packet.GetSize();
118 if( have < needed )
119 throw BadSize("JLDE:Parse(1)", have, needed);
121 const unsigned char *ptr = entry_packet.GetData();
122 Protocol::JLDirEntry *entry = (Protocol::JLDirEntry*) ptr;
124 Id = id;
125 Timestamp = be_btohl(entry->timestamp);
127 uint16_t len = be_btohs(entry->filename_size);
128 needed += len;
129 if( have < needed )
130 throw BadSize("JLDE:Parse(2)", have, needed);
131 Name.assign((char *)entry->filename, len);
133 // need parsed data + string size
134 ptr += needed;
135 needed += 2;
136 if( have < needed )
137 throw BadSize("JLDE:Parse(3)", have, needed);
139 len = be_btohs( *((uint16_t*)(ptr)) );
140 ptr += sizeof(uint16_t);
141 needed += len;
142 if( have < needed )
143 throw BadSize("JLDE:Parse(4)", have, needed);
144 Version.assign((char*)ptr, len);
146 // need parsed data + string size
147 ptr += len;
148 needed += sizeof(uint32_t);
149 if( have < needed )
150 throw BadSize("JLDE:Parse(5)", have, needed);
151 CodSize = be_btohl( *((uint32_t*)(ptr)) );
154 void JLDirectoryEntry::Dump(std::ostream &os) const
156 ios_format_state state(os);
158 os << left << setfill(' ') << setw(50) << Name;
160 os << "\n";
161 os << left << setw(28) << " ";
163 os << "0x" << setfill('0') << setw(4) << hex << Id;
164 os << " " << setw(10) << Version;
165 os << " " << setw(7) << std::dec << CodSize;
167 std::string ts = ctime(&Timestamp);
168 ts.erase(ts.size() - 1);
169 os << " " << ts;
171 if( SubDir.size() )
172 os << "\n" << SubDir;
176 ///////////////////////////////////////////////////////////////////////////////
177 // JLEventlog class
179 void JLEventlog::Dump(std::ostream &os) const
181 const_iterator i = begin(), e = end();
182 for( ; i != e; ++i ) {
183 (*i).Dump(os);
188 ///////////////////////////////////////////////////////////////////////////////
189 // JLEventlogEntry class, static members
192 // Note! These functions currently only pass the same values through.
193 // In actuality, these are technically two different values:
194 // one on the raw protocol side, and the other part of the
195 // guaranteed Barry API. If the Blackberry ever changes the
196 // meanings for these codes, do the translation here.
199 JLEventlogEntry::Severity_t JLEventlogEntry::SeverityProto2Rec(unsigned int s)
201 return (Severity_t)s;
204 unsigned int JLEventlogEntry::SeverityRec2Proto(Severity_t s)
206 return s;
209 JLEventlogEntry::ViewerType_t JLEventlogEntry::ViewerTypeProto2Rec(unsigned int v)
211 return (ViewerType_t)v;
214 unsigned int JLEventlogEntry::ViewerTypeRec2Proto(ViewerType_t v)
216 return v;
220 ///////////////////////////////////////////////////////////////////////////////
221 // JLEventlogEntry class
223 void JLEventlogEntry::Parse(uint16_t size, const char* buf)
225 // example of a single log entry
226 //guid:92E11214401C3 time:0x11F133E6470 severity:0 type:2 app:UI data:GS-D 2c89868b
228 std::string src = std::string(buf, size);
229 std::istringstream ss(src);
231 ss.ignore(5); // skip "guid:"
232 ss >> Guid;
233 if( ss.fail() )
234 throw BadData("JLEventlogEntry:Parse bad guid field");
236 ss.ignore(6); // skip " time:"
237 ss >> hex >> MSTimestamp;
238 if( ss.fail() )
239 throw BadData("JLEventlogEntry:Parse bad time field");
241 ss.ignore(10); // skip " severity:"
242 unsigned int severity;
243 ss >> severity;
244 Severity = SeverityProto2Rec(severity);
245 if( ss.fail() )
246 throw BadData("JLEventlogEntry:Parse bad severity field");
248 ss.ignore(6); // skip " type:"
249 unsigned int type;
250 ss >> type;
251 Type = ViewerTypeProto2Rec(type);
252 if( ss.fail() )
253 throw BadData("JLEventlogEntry:Parse bad type field");
255 ss.ignore(5); // skip " app:"
256 ss >> App;
257 if( ss.fail() )
258 throw BadData("JLEventlogEntry:Parse bad app field");
260 ss.ignore(6); // skip " data:"
262 // use stringbuf to extract rest of data from stream
263 stringbuf databuf;
264 ss >> &databuf;
265 if( ss.fail() )
266 throw BadData("JLEventlogEntry:Parse bad data field");
268 Data = databuf.str();
271 std::string JLEventlogEntry::GetFormattedTimestamp() const
273 char buf[21];
274 struct tm split;
275 time_t timestamp = (time_t) (MSTimestamp / 1000);
277 if( localtime_r(&timestamp, &split) == NULL )
278 return "";
280 if( strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S.", &split) == 0 )
281 return "";
283 std::ostringstream oss;
284 oss << buf << (MSTimestamp % 1000);
285 return oss.str();
288 void JLEventlogEntry::Dump(std::ostream &os) const
290 ios_format_state state(os);
292 static const char *SeverityNames[] = { "Always Log", "Severe Error", "Error",
293 "Warning", "Information", "Debug Info"};
294 static const char *ViewerTypes[] = { "", "Number", "String", "Exception" };
296 os << "guid:" << Guid;
297 os << " time:" << GetFormattedTimestamp();
298 os << " severity:" << SeverityNames[Severity];
299 os << " type:" << ViewerTypes[Type];
300 os << " app:" << App;
301 os << " data:" << Data << endl;
305 ///////////////////////////////////////////////////////////////////////////////
306 // JLDeviceInfo class
308 void JLDeviceInfo::Dump(std::ostream &os) const
310 ios_format_state state(os);
312 os << left << setfill(' ') << setw(17) << "Hardware Id:";
313 os << "0x" << hex << HardwareId << endl;
315 os << left << setfill(' ') << setw(17) << "PIN:";
316 os << "0x" << Pin.Str() << endl;
318 os << left << setfill(' ') << setw(17) << "OS Version:";
319 os << dec << OsVersion.Major << '.' << OsVersion.Minor << '.' << OsVersion.SubMinor << '.' << OsVersion.Build << endl;
321 os << left << setfill(' ') << setw(17) << "VM Version:";
322 os << dec << VmVersion.Major << '.' << VmVersion.Minor << '.' << VmVersion.SubMinor << '.' << VmVersion.Build << endl;
324 os << left << setfill(' ') << setw(17) << "Radio ID:";
325 os << "0x" << hex << RadioId << endl;
327 os << left << setfill(' ') << setw(17) << "Vendor ID:";
328 os << dec << VendorId << endl;
330 // WAF = Wireless Access Families
331 os << left << setfill(' ') << setw(17) << "Active Wireless Access Families:";
332 os << "0x" << hex << ActiveWafs << endl;
334 os << left << setfill(' ') << setw(17) << "OS Metrics:" << endl;
335 os << OsMetrics;
337 os << left << setfill(' ') << setw(17) << "Bootrom Metrics:" << endl;
338 os << BootromMetrics;
342 namespace Mode {
344 ///////////////////////////////////////////////////////////////////////////////
345 // JavaLoader Mode class
347 JavaLoader::JavaLoader(Controller &con)
348 : Mode(con, Controller::JavaLoader)
349 , m_StreamStarted(false)
353 JavaLoader::~JavaLoader()
355 if( m_StreamStarted )
356 StopStream();
359 ///////////////////////////////////////////////////////////////////////////////
360 // protected members
363 ///////////////////////////////////////////////////////////////////////////////
364 // public API
366 void JavaLoader::OnOpen()
368 Data response;
369 m_socket->Receive(response, -1);
372 // These commands are sent to prepare the data stream
373 void JavaLoader::StartStream()
375 Data cmd(-1, 8), data(-1, 8), response;
376 JLPacket packet(cmd, data, response);
378 packet.Hello();
379 m_socket->Packet(packet);
381 if( packet.Command() != SB_COMMAND_JL_HELLO_ACK ) {
382 ThrowJLError("JavaLoader::StartStream Hello", packet.Command());
385 packet.SetUnknown1();
386 m_socket->Packet(packet);
388 if( packet.Command() != SB_COMMAND_JL_ACK ) {
389 ThrowJLError("JavaLoader::StartStream Unknown1", packet.Command());
392 m_StreamStarted = true;
396 // This function permits to send a COD application
397 // WARNING : Before, you have to call the "Start" function,
398 // After, you have to call the "Stop" function.
400 // From the USB traces, the max size of packet is : 0x07FC
401 // Packet header :
402 // 04 00 08 00 68 00 F8 07
403 // ^^^^^ : about size
404 // ^^ : command
405 // ^^ : size of packet header
406 // ^^^^^ : socket
407 // Response :
408 // 00 00 0C 00 13 04 01 00 0A 00 00 00
409 // Packet format :
410 // 04 00 FC 07 DB 9D 95 2B 57 .... E6 FD
411 // ^^^^^ ............. ^^^^^ : data (the file content)
412 // ^^^^^ : packet size
413 // ^^^^^ : socket
416 // WARNING : A COD file starts with the integer 0xDEC0FFFF (FIXME)
417 // An application can contain several COD parts. In this case we can read a header (start with PK)
418 // In this sample, we have to skip the file header :
419 // 00000000 50 4B 03 04 0A 00 00 00 00 00 A0 00 51 35 BA 9F 99 5D 30 CE PK..........Q5...]0.
420 // 00000014 00 00 30 CE 00 00 15 00 04 00 4D 65 74 72 6F 56 69 65 77 65 ..0.......MetroViewe
421 // 00000028 72 2E 50 61 72 69 73 2E 63 6F 64 FE CA 00 00 DE C0 FF FF 00 r.Paris.cod.........
422 // ^^ Start of data sent !
423 // 0000003C 00 00 00 00 00 00 00 0F 10 34 45 00 00 00 00 00 00 00 00 21 .........4E........!
424 // 00000050 00 FF FF FF FF FF FF FF FF FF FF 4E 00 9C 08 68 C5 00 00 F0 ...........N...h....
425 // 00000064 B8 BC C0 A1 C0 14 00 81 00 00 01 01 04 0E 3F 6D 00 02 00 6D ..............?m...m
426 void JavaLoader::SendStream(std::istream &input, size_t module_size)
428 char buffer[MAX_PACKET_DATA_SIZE - SB_JLPACKET_HEADER_SIZE];
429 size_t max_data_size = sizeof(buffer);
431 size_t remaining = module_size;
433 Data cmd(-1, 8), data(-1, 8), response;
434 JLPacket packet(cmd, data, response);
436 packet.SetCodSize(module_size);
437 m_socket->Packet(packet);
439 if( packet.Command() != SB_COMMAND_JL_ACK ) {
440 ThrowJLError("JavaLoader::SendStream set code size", packet.Command());
443 while( remaining > 0 ) {
444 size_t size = min(remaining, max_data_size);
446 input.read(buffer, size);
447 if( input.fail() || (size_t)input.gcount() != size ) {
448 throw Error("JavaLoader::SendStream input stream read failed");
451 packet.PutData(buffer, size);
452 m_socket->Packet(packet);
454 if( packet.Command() == SB_COMMAND_JL_NOT_ENOUGH_MEMORY ) {
455 throw Error("JavaLoader::SendStream not enough memory to install the application");
458 if( packet.Command() != SB_COMMAND_JL_ACK ) {
459 ThrowJLError("JavaLoader::SendStream send data", packet.Command());
462 remaining -= size;
466 void JavaLoader::LoadApp(std::istream &input)
468 uint32_t module_size;
469 while( (module_size = SeekNextCod(input)) != 0 ) {
470 SendStream(input, module_size);
475 // StopStream
477 /// Must be called at the end of a JavaLoader session. The JL_GOODBYE
478 /// command is sent to the device. When the device responds with
479 /// RESET_REQUIRED the device reset command will be sent when the
480 /// socket is closed.
482 /// \return true when a device reset was required
484 bool JavaLoader::StopStream()
486 Data cmd(-1, 8), data(-1, 8), response;
488 JLPacket packet(cmd, data, response);
489 packet.Goodbye();
490 try {
491 m_socket->Packet(packet);
492 } catch( BadPacket &bp ) {
493 // on some devices, such as the 7750 and the 7130,
494 // the Goodbye command receives NOT_SUPPORTED
495 // instead of the usual ACK... this is not an
496 // error, so catch that case here and ignore it.
497 // otherwise, throw it to higher levels
498 if( bp.response() != SB_COMMAND_JL_NOT_SUPPORTED )
499 throw;
502 m_StreamStarted = false;
504 if( packet.Command() == SB_COMMAND_JL_RESET_REQUIRED ) {
505 m_socket->ResetOnClose(true);
506 return true;
508 else if( packet.Command() != SB_COMMAND_JL_ACK &&
509 packet.Command() != SB_COMMAND_JL_NOT_SUPPORTED )
511 ThrowJLError("JavaLoader::StopStream", packet.Command());
514 return false;
517 void JavaLoader::SetTime(time_t when)
519 Data cmd(-1, 8), data(-1, 8), response;
521 JLPacket packet(cmd, data, response);
522 packet.SetTime(when);
523 m_socket->Packet(packet);
524 if( packet.Command() != SB_COMMAND_JL_ACK ) {
525 ThrowJLError("JavaLoader::SetTime", packet.Command());
529 void JavaLoader::ThrowJLError(const std::string &msg, uint8_t cmd)
531 std::ostringstream oss;
532 oss << msg << ": unexpected packet command code: 0x"
533 << std::hex << (unsigned int) cmd;
534 throw Error(oss.str());
537 void JavaLoader::GetDirectoryEntries(JLPacket &packet,
538 uint8_t entry_cmd,
539 JLDirectory &dir,
540 bool include_subdirs)
542 JLDirectory::TableIterator i = dir.TableBegin(), e = dir.TableEnd();
543 for( ; i != e; ++i ) {
544 packet.GetDirEntry(entry_cmd, *i);
545 m_socket->Packet(packet);
546 if( packet.Command() != SB_COMMAND_JL_ACK ) {
547 ThrowJLError("JavaLoader::GetDirectoryEntries", packet.Command());
550 Data &response = packet.GetReceive();
551 m_socket->Receive(response);
552 JLDirectoryEntry entry(dir.Level());
553 Protocol::CheckSize(response, 4);
554 entry.Parse(*i, Data(response.GetData() + 4, response.GetSize() - 4));
556 if( include_subdirs ) {
557 packet.GetSubDir(*i);
558 GetDir(packet, SB_COMMAND_JL_GET_SUBDIR_ENTRY, entry.SubDir, false);
561 // add to list
562 dir.push_back(entry);
566 void JavaLoader::GetDir(JLPacket &packet,
567 uint8_t entry_cmd,
568 JLDirectory &dir,
569 bool include_subdirs)
571 m_socket->Packet(packet);
572 if( packet.Command() != SB_COMMAND_JL_ACK ) {
573 ThrowJLError("JavaLoader::GetDir", packet.Command());
576 // ack response will contain length of module ID array in next packet
577 unsigned int expect = packet.Size();
579 if( expect > 0 ) {
580 Data &response = packet.GetReceive();
581 m_socket->Receive(response);
582 Protocol::CheckSize(response, 4);
583 dir.ParseTable(Data(response.GetData() + 4, response.GetSize() - 4));
584 GetDirectoryEntries(packet, entry_cmd, dir, include_subdirs);
588 void JavaLoader::GetDirectory(JLDirectory &dir, bool include_subdirs)
590 Data cmd(-1, 8), data(-1, 8), response;
591 JLPacket packet(cmd, data, response);
593 packet.GetDirectory();
594 GetDir(packet, SB_COMMAND_JL_GET_DATA_ENTRY, dir, include_subdirs);
598 // This function permits to receive a ScreenShot (maybe other...)
599 // WARNING : Before, you have to call the "Start" function,
600 // After, you have to call the "Stop" function.
602 // From the USB traces, the max size of packet is : 0x07FC
603 // When you are ready, we send the packet :
604 // 04 00 08 00 68 00 00 00
605 // Then, we receive an acknoledge and the data.
606 // The data is composed of two packets : header and content.
607 // Packet header :
608 // 04 00 08 00 6E 00 F8 07
609 // ^^^^^ : size + 4 bytes
610 // ^^ : command
611 // ^^^^^ : size of packet header
612 // ^^^^^ : socket
613 // Packet content :
614 // 04 00 FC 07 DB 9D 95 2B 57 .... E6 FD
615 // ^^^^^ ............. ^^^^^ : data (the file content)
616 // ^^^^^ : packet size (0x07FC = 0x7F8 + 4)
617 // ^^^^^ : socket
620 // GetScreenshot
622 /// Downloads screenshot from device, and fills info with size data
623 /// and the given Data buffer image with the bitmap.
625 void JavaLoader::GetScreenshot(JLScreenInfo &info, Data &image)
627 // start fresh
628 image.Zap();
630 Data cmd(-1, 8), data(-1, 8), response;
631 JLPacket packet(cmd, data, response);
633 // Send the screenshot command :
634 // 00000000: 04 00 08 00 87 00 04 00
635 packet.GetScreenshot();
637 m_socket->Packet(packet);
639 if( packet.Command() != SB_COMMAND_JL_ACK ) {
640 ThrowJLError("JavaLoader::GetScreenshot", packet.Command());
643 // Get Info :
644 // 00000000: 04 00 14 00 00 05 46 00 40 03 01 68 01 e0 00 10 ......F.@..h....
645 // ^^^^^x^^^^^ : width x height
646 // ^^^^^ : packet size
647 // ^^^^^ : socket ID
648 // 00000010: 00 00 00 00 ....
650 m_socket->Receive(response);
652 // Parse response...
653 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + SB_JLSCREENINFO_SIZE);
654 MAKE_JLPACKET(rpack, response);
656 info.width = be_btohs(rpack->u.screeninfo.width);
657 info.height = be_btohs(rpack->u.screeninfo.height);
660 // Read stream
661 for (;;) {
662 // Send the packet :
663 // 04 00 08 00 68 00 00 00
664 packet.GetData();
666 m_socket->Packet(packet);
668 // Read and parse the response
669 // 04 00 08 00 64 00 00 00
670 // or
671 // 04 00 08 00 6e 00 f8 07
673 if( packet.Command() == SB_COMMAND_JL_ACK )
674 return;
676 if( packet.Command() != SB_COMMAND_JL_GET_DATA_ENTRY ) {
677 ThrowJLError("JavaLoader::GetScreenShot ", packet.Command());
680 // Read the size of next packet
681 size_t expect = packet.Size();
684 // Read the stream
685 m_socket->Receive(response);
688 // Save data in buffer
689 Protocol::CheckSize(response, 4);
690 const unsigned char *pd = response.GetData();
691 size_t bytereceived = response.GetSize() - 4;
694 // Check the size read into the previous packet
695 if( expect != bytereceived ) {
696 ThrowJLError("JavaLoader::GetScreenShot expect", expect);
700 // Copy data
701 unsigned char *buffer = image.GetBuffer(image.GetSize() + bytereceived);
702 memcpy(buffer + image.GetSize(), pd + 4, bytereceived);
704 // New size
705 image.ReleaseBuffer(image.GetSize() + bytereceived);
709 void JavaLoader::DoErase(uint8_t cmd, const std::string &cod_name)
711 Data command(-1, 8), data(-1, 8), response;
713 JLPacket packet(command, data, response);
715 // set filename, device responds with an ID
716 packet.SetCodFilename(cod_name);
717 m_socket->Packet(packet);
718 if( packet.Command() == SB_COMMAND_JL_COD_NOT_FOUND ) {
719 throw Error(string("JavaLoader::DoErase: module ") + cod_name + " not found");
721 if( packet.Command() != SB_COMMAND_JL_ACK ) {
722 ThrowJLError("JavaLoader::DoErase", packet.Command());
725 // make sure there is an ID coming
726 if( packet.Size() != 2 )
727 throw Error("JavaLoader::DoErase: expected code not available");
729 // get ID
730 m_socket->Receive(response);
731 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + sizeof(uint16_t));
732 MAKE_JLPACKET(jpack, response);
733 uint16_t id = be_btohs(jpack->u.id);
735 // send erase command, with application ID
736 packet.Erase(cmd, id);
737 m_socket->Packet(packet);
738 if( packet.Command() == SB_COMMAND_JL_COD_IN_USE ) {
739 throw Error("JavaLoader::DoErase: COD file in use.");
741 if( packet.Command() != SB_COMMAND_JL_ACK ) {
742 ThrowJLError("JavaLoader::DoErase", packet.Command());
746 void JavaLoader::Erase(const std::string &cod_name)
748 DoErase(SB_COMMAND_JL_ERASE, cod_name);
751 void JavaLoader::ForceErase(const std::string &cod_name)
753 DoErase(SB_COMMAND_JL_FORCE_ERASE, cod_name);
756 void JavaLoader::GetEventlog(JLEventlog &log)
758 Data command(-1, 8), data(-1, 8), response;
759 JLPacket packet(command, data, response);
761 packet.GetEventlog();
763 m_socket->Packet(packet);
765 if( packet.Command() != SB_COMMAND_JL_ACK ) {
766 ThrowJLError("JavaLoader::GetEventlog", packet.Command());
769 m_socket->Receive(response);
770 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + sizeof(uint16_t));
772 // number of eventlog entries
773 MAKE_JLPACKET(jpack, response);
774 uint16_t count = be_btohs(jpack->u.response.expect);
776 for( uint16_t i = 0; i < count; ++ i ) {
777 packet.GetEventlogEntry(i);
779 m_socket->Packet(packet);
781 if( packet.Command() != SB_COMMAND_JL_ACK ) {
782 ThrowJLError("JavaLoader::GetEventlog", packet.Command());
785 m_socket->Receive(response);
786 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + SB_JLEVENTLOG_ENTRY_HEADER_SIZE);
788 MAKE_JLPACKET(jpack, response);
789 uint16_t size = be_btohs(jpack->u.logentry.size);
791 JLEventlogEntry entry;
792 entry.Parse(size, (const char *)(response.GetData() + SB_JLPACKET_HEADER_SIZE + SB_JLEVENTLOG_ENTRY_HEADER_SIZE));
794 log.push_back(entry);
798 void JavaLoader::ClearEventlog()
800 Data command(-1, 8), data(-1, 8), response;
801 JLPacket packet(command, data, response);
803 packet.ClearEventlog();
804 m_socket->Packet(packet);
806 if( packet.Command() != SB_COMMAND_JL_ACK ) {
807 ThrowJLError("JavaLoader::ClearEventlog", packet.Command());
811 void JavaLoader::SaveData(JLPacket &packet, uint16_t id, CodFileBuilder &builder, std::ostream &output)
813 packet.SaveModule(id);
814 m_socket->Packet(packet);
816 if( packet.Command() != SB_COMMAND_JL_ACK ) {
817 ThrowJLError("JavaLoader::SaveData", packet.Command());
820 // get total size of cod file or this sibling cod file
821 Data &response = packet.GetReceive();
822 m_socket->Receive(response);
823 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + sizeof(uint32_t));
824 MAKE_JLPACKET(jpack, response);
825 uint32_t total_size = be_btohl(jpack->u.cod_size);
827 // allocate buffer to hold data for this sibling
828 Data buffer(-1, total_size);
829 uint32_t offset = 0;
831 for( ;; ) {
832 packet.GetData();
833 m_socket->Packet(packet);
835 if( packet.Command() == SB_COMMAND_JL_ACK )
836 break;
838 if( packet.Command() != SB_COMMAND_JL_GET_DATA_ENTRY ) {
839 ThrowJLError("JavaLoader::SaveData", packet.Command());
842 // expected size of data in response packet
843 unsigned int expect = packet.Size();
845 m_socket->Receive(response);
846 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + expect);
848 memcpy(buffer.GetBuffer(offset + expect) + offset,
849 response.GetData() + SB_JLPACKET_HEADER_SIZE,
850 expect);
852 offset += expect;
853 buffer.ReleaseBuffer(offset);
856 builder.WriteNextHeader(output, buffer.GetData(), buffer.GetSize());
857 output.write((const char *)buffer.GetData(), buffer.GetSize());
860 void JavaLoader::Save(const std::string &cod_name, std::ostream &output)
862 Data command(-1, 8), data(-1, 8), response;
864 JLPacket packet(command, data, response);
866 // set filename, device responds with an ID
867 packet.SetCodFilename(cod_name);
868 m_socket->Packet(packet);
870 if( packet.Command() == SB_COMMAND_JL_COD_NOT_FOUND ) {
871 throw Error(string("JavaLoader::Save: module ") + cod_name + " not found");
874 if( packet.Command() != SB_COMMAND_JL_ACK ) {
875 ThrowJLError("JavaLoader::Save", packet.Command());
878 // make sure there is an ID coming
879 if( packet.Size() != 2 )
880 throw Error("JavaLoader::Save: expected module ID");
882 // get ID
883 m_socket->Receive(response);
884 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + sizeof(uint16_t));
885 MAKE_JLPACKET(jpack, response);
886 uint16_t id = be_btohs(jpack->u.id);
888 // get list of sibling modules
889 packet.GetSubDir(id);
890 m_socket->Packet(packet);
892 if( packet.Command() != SB_COMMAND_JL_ACK ) {
893 ThrowJLError("JavaLoader::Save", packet.Command());
896 // expected number of module ID's
897 unsigned int expect = packet.Size();
899 // get list of sibling module ID's
900 m_socket->Receive(response);
901 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + expect);
903 // copy array of module ID's since we reuse the response packet buffer
904 size_t count = expect / 2;
905 const uint16_t *begin = (const uint16_t*) (response.GetData() + SB_JLPACKET_HEADER_SIZE);
906 const uint16_t *end = begin + count;
907 vector<uint16_t> ids(begin, end);
909 CodFileBuilder builder(cod_name, count);
911 // save each block of data
912 for( size_t i = 0; i < count; i++ ) {
913 SaveData(packet, be_btohs(ids[i]), builder, output);
916 builder.WriteFooter(output);
919 void JavaLoader::DeviceInfo(JLDeviceInfo &info)
921 Data command(-1, 8), data(-1, 8), response;
922 JLPacket packet(command, data, response);
924 packet.DeviceInfo();
926 m_socket->Packet(packet);
928 if( packet.Command() != SB_COMMAND_JL_ACK ) {
929 ThrowJLError("JavaLoader::DeviceInfo", packet.Command());
932 m_socket->Receive(response);
934 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE + SB_JLDEVICEINFO_SIZE);
935 MAKE_JLPACKET(rpack, response);
937 info.HardwareId = be_btohl(rpack->u.devinfo.hardware_id);
938 info.Pin = be_btohl(rpack->u.devinfo.pin);
939 info.OsVersion = be_btohl(rpack->u.devinfo.os_version);
940 info.VmVersion = be_btohl(rpack->u.devinfo.vm_version);
941 info.RadioId = be_btohl(rpack->u.devinfo.radio_id);
942 info.VendorId = be_btohl(rpack->u.devinfo.vendor_id);
943 info.ActiveWafs = be_btohl(rpack->u.devinfo.active_wafs);
945 packet.OsMetrics();
947 m_socket->Packet(packet);
949 if( packet.Command() != SB_COMMAND_JL_ACK ) {
950 ThrowJLError("JavaLoader::DeviceInfo", packet.Command());
953 m_socket->Receive(response);
954 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE);
956 size_t offset = SB_JLPACKET_HEADER_SIZE;
957 size_t size = response.GetSize()-offset;
958 unsigned char* buf = info.OsMetrics.GetBuffer(size);
959 memcpy(buf, response.GetData()+offset, size);
960 info.OsMetrics.ReleaseBuffer(size);
962 packet.BootromMetrics();
964 m_socket->Packet(packet);
966 if( packet.Command() != SB_COMMAND_JL_ACK ) {
967 ThrowJLError("JavaLoader::DeviceInfo", packet.Command());
970 m_socket->Receive(response);
971 Protocol::CheckSize(response, SB_JLPACKET_HEADER_SIZE);
973 offset = SB_JLPACKET_HEADER_SIZE;
974 size = response.GetSize()-offset;
975 buf = info.BootromMetrics.GetBuffer(size);
976 memcpy(buf, response.GetData()+offset, size);
977 info.BootromMetrics.ReleaseBuffer(size);
980 void JavaLoader::Wipe(bool apps, bool fs)
982 Data command(-1, 8), data(-1, 8), response;
983 JLPacket packet(command, data, response);
985 if( apps ) {
986 packet.WipeApps();
987 m_socket->Packet(packet);
989 if( packet.Command() != SB_COMMAND_JL_ACK ) {
990 ThrowJLError("JavaLoader::WipeApps", packet.Command());
994 if( fs ) {
995 packet.WipeFs();
996 m_socket->Packet(packet);
998 if( packet.Command() != SB_COMMAND_JL_ACK ) {
999 ThrowJLError("JavaLoader::WipeFs", packet.Command());
1004 void JavaLoader::LogStackTraces()
1006 Data command(-1, 8), data(-1, 8), response;
1007 JLPacket packet(command, data, response);
1009 packet.LogStackTraces();
1010 m_socket->Packet(packet);
1012 if( packet.Command() != SB_COMMAND_JL_ACK ) {
1013 ThrowJLError("JavaLoader::LogStackTraces", packet.Command());
1017 void JavaLoader::ResetToFactory()
1019 Data command(-1, 8), data(-1, 8), response;
1020 JLPacket packet(command, data, response);
1022 packet.ResetToFactory();
1023 m_socket->Packet(packet);
1025 if( packet.Command() != SB_COMMAND_JL_ACK ) {
1026 ThrowJLError("JavaLoader::ResetToFactory", packet.Command());
1030 }} // namespace Barry::Mode