• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

reb1100.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002               reb1100.cpp  -  REB1100 communication class
00003                              -------------------
00004     begin                : Thu Mar 27 2003
00005     copyright            : (C) 2003 by Igor Izyumin
00006     email                : igor@mlug.missouri.edu
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     This program is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU General Public License for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with this program; if not, write to the Free Software
00023     Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
00024 
00025     **********************************************************************/
00026 
00027 #include "reb1100.h"
00028 
00029 REB1100::REB1100(){
00030     struct usb_bus *bus;
00031     struct usb_device *dev;
00032 
00033     usb_init();
00034 
00035     usb_find_busses();
00036     usb_find_devices();
00037 
00038     char string[256];
00039 
00040     int found = 0;
00041 
00042     /* find ebook device */
00043     for(bus = usb_busses; bus && !found; bus = bus->next) {
00044         for(dev = bus->devices; dev && !found; dev = dev->next) {
00045             if (dev->descriptor.idVendor == 0x993 && dev->descriptor.idProduct == 0x1) {
00046                 hDevice = usb_open(dev);
00047                 cerr << "Found eBook. Attempting to open... ";
00048                 found = 1;
00049                 if (hDevice) {
00050 //                  if (!usb_get_string_simple(hDevice, dev->descriptor.iSerialNumber, string, sizeof(string))) throw DevOpenError();
00051                     cerr << "Success.\n";
00052 //                  cerr << "Serial number: " << string << endl;
00053                 }
00054                 else throw DevOpenError();
00055             }
00056         }
00057     }
00058     if (!found) throw DevNotFoundError();
00059 
00060     if (!usb_set_configuration(hDevice, 0x0)) throw DevOpenError();
00061     if (!usb_claim_interface(hDevice, 0x1)) throw DevOpenError();
00062 
00063     memTarget = INTERNAL;
00064 }
00065 
00066 REB1100::~REB1100(){
00067     usb_release_interface(hDevice, 0x0);
00068     usb_close(hDevice);
00069 }
00070 
00071 void REB1100::getFile(string filename, string &data) {
00072     long flength = filename.length();
00073     char buf[4096];
00074     char zeros[4] = {0, 0, 0, 0};
00075     int ret;
00076     string request;
00077     // first four bytes are the length of the filename
00078     // (low-endian)
00079     char *byte = reinterpret_cast<char*>(&flength);
00080     request += *byte;
00081     byte++;
00082     request += *byte;
00083     byte++;
00084     request += *byte;
00085     byte++;
00086     request += *byte;
00087     // the rest is the filename
00088     request += filename;
00089     
00090     // send a USB control request to tell the device what file we want
00091     char *temp;
00092     temp = new char[request.length()];
00093     request.copy(temp, string::npos);
00094     ret = usb_control_msg(hDevice, 0x42, 0x01, 0x00, 0x00, temp, request.length(), 300);
00095     if (ret == -1) throw DevControlError();
00096     delete temp;
00097     temp = NULL;
00098     
00099     // read the return code
00100     ret = usb_control_msg(hDevice, 0xc2, 0x02, 0x00, 0x00, zeros, 4, 300);
00101     if (ret == -1) throw DevControlError();
00102     
00103     // read file from pipe
00104     do {
00105         ret = usb_bulk_read(hDevice, 2, buf, 4096, 1000);
00106         if (ret == -1) throw DevReadError();
00107         for(int i = 0; i < ret; i++) {
00108                 data += buf[i];
00109         }
00110     }
00111     while(ret == 4096);
00112 }
00113     
00114 void REB1100::sendFile(string filename, string &data) {
00115     string prefix = "";
00116     if (memTarget == MEMCARD) { // prefix with \SM\ when sending to memory card
00117         prefix = "\\SM\\";
00118     }
00119     filename = prefix + filename;
00120 
00121     long flength = data.length();
00122     long fnlength = filename.length();
00123     
00124     // prepare initial request
00125     string request;
00126     
00127     // first four bytes are the length of the file
00128     // (low-endian)
00129     char *byte = reinterpret_cast<char*>(&flength);
00130     request += *byte;
00131     byte++;
00132     request += *byte;
00133     byte++;
00134     request += *byte;
00135     byte++;
00136     request += *byte;
00137     
00138     // next four bytes are the length of the filename
00139     // (low-endian)
00140     byte = reinterpret_cast<char*>(&fnlength);
00141     request += *byte;
00142     byte++;
00143     request += *byte;
00144     byte++;
00145     request += *byte;
00146     byte++;
00147     request += *byte;
00148     
00149     // append filename
00150     request += filename;
00151     
00152     // send message to device
00153     int ret;
00154     char *temp;
00155     temp = new char[request.length()];
00156     request.copy(temp, string::npos);
00157     ret = usb_control_msg(hDevice, 0x42, 0x00, 0x00, 0x00, temp, request.length(), 3000);
00158     delete temp;
00159     temp = NULL;
00160     if (ret == -1) throw DevControlError();
00161     
00162     // read from device and check for error
00163     char temp2[4] = {0, 0, 0, 0};
00164     ret = usb_control_msg(hDevice, 0xc2, 0x03, 0x00, 0x00, temp2, 4, 3000);
00165     if (ret == -1) throw DevControlError();
00166     if (temp2[0] || temp2[1] || temp2[2] || temp2[3]) throw DevControlError();
00167     
00168     // now start bulk writing to the device
00169     string buf;
00170     int n, offset = 0;
00171     char *temp3;
00172     do {
00173         buf = data.substr(offset, 4096);
00174         n = buf.length();
00175         if (buf.length() > 0) {
00176             temp3 = new char[buf.length()];
00177             buf.copy(temp3, string::npos);
00178 //          cout << "Sending block (" << n << " bytes)\n";
00179             ret = usb_bulk_write(hDevice, 2, temp3, n, 3000);
00180             if (ret == -1) throw DevWriteError();
00181             delete temp3;
00182             temp3 = NULL;
00183             offset += 4096;
00184         }
00185     }
00186     while(offset + 1 < data.length());
00187     // send empty packet to signify end of file
00188     ret = usb_bulk_write(hDevice, 2, 0, 0, 3000);
00189     if (ret == -1) throw DevWriteError();
00190 }
00191 
00192 void REB1100::setTarget(bool target) {
00193     memTarget = target;
00194 }
00195 

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal