00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00051 cerr << "Success.\n";
00052
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
00078
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
00088 request += filename;
00089
00090
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
00100 ret = usb_control_msg(hDevice, 0xc2, 0x02, 0x00, 0x00, zeros, 4, 300);
00101 if (ret == -1) throw DevControlError();
00102
00103
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) {
00117 prefix = "\\SM\\";
00118 }
00119 filename = prefix + filename;
00120
00121 long flength = data.length();
00122 long fnlength = filename.length();
00123
00124
00125 string request;
00126
00127
00128
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
00139
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
00150 request += filename;
00151
00152
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
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
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
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
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