forked from c4ev3/ev3duder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/a3f/ev3duder
Conflicts: src/main.c
- Loading branch information
Showing
13 changed files
with
383 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,6 @@ Temporary Items | |
ev3duder | ||
#doxygen | ||
doc | ||
#vim | ||
*.swp | ||
*.swo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "ev3_io.h" | ||
|
||
#include "defs.h" | ||
#include "systemcmd.h" | ||
#include "error.h" | ||
#include "funcs.h" | ||
|
||
#define CHUNK_SIZE 1000 // EV3's HID driver doesn't do packets > 1024B | ||
int dl( const char *path, FILE *fp) | ||
{ | ||
int res; | ||
size_t path_sz = strlen(path) + 1; | ||
if (!fp) | ||
fp = fopen(strrchr(path, '/') + 1, "w"); | ||
|
||
//TODO: read in chunks, whatif long isnt big enough | ||
BEGIN_UPLOAD *bu = packet_alloc(BEGIN_UPLOAD, path_sz); | ||
memcpy(bu->fileName, path, path_sz); | ||
bu->maxBytes = CHUNK_SIZE; | ||
|
||
print_bytes(bu, bu->packetLen + PREFIX_SIZE); | ||
res = ev3_write(handle, (u8 *)bu, bu->packetLen + PREFIX_SIZE); | ||
if (res < 0) | ||
{ | ||
errmsg = "Unable to write BEGIN_UPLOAD."; | ||
hiderr = ev3_error(handle); | ||
return ERR_HID; | ||
} | ||
fputs("Checking reply: \n", stderr); | ||
size_t file_chunksz = sizeof(BEGIN_UPLOAD_REPLY) + bu->maxBytes; | ||
void *file_chunk = malloc(file_chunksz); | ||
BEGIN_UPLOAD_REPLY *burep = file_chunk; | ||
|
||
res = ev3_read_timeout(handle, (u8 *)burep, file_chunksz, TIMEOUT); | ||
if (res <= 0) | ||
{ | ||
errmsg = "Unable to read BEGIN_UPLOAD"; | ||
hiderr = ev3_error(handle); | ||
return ERR_HID; | ||
} | ||
|
||
if (burep->type == VM_ERROR) | ||
{ | ||
if (burep->ret < ARRAY_SIZE(ev3_error_msgs)) | ||
hiderr = ev3_error_msgs[burep->ret]; | ||
else | ||
hiderr = L"ERROR_OUT_OF_BOUNDS"; | ||
fputs("Operation failed.\nlast_reply=", stderr); | ||
print_bytes(burep, burep->packetLen); | ||
|
||
|
||
errmsg = "`BEGIN_UPLOAD` was denied."; | ||
return ERR_VM; | ||
} | ||
fwrite(burep->bytes, CHUNK_SIZE, 1, fp); | ||
|
||
size_t read_so_far = burep->packetLen + 2 - offsetof(BEGIN_UPLOAD_REPLY, bytes); | ||
size_t total = burep->fileSize; | ||
free(burep); | ||
|
||
CONTINUE_UPLOAD cu = CONTINUE_UPLOAD_INIT; | ||
cu.fileHandle =burep->fileHandle; | ||
cu.maxBytes = CHUNK_SIZE + sizeof(BEGIN_UPLOAD_REPLY) - sizeof(CONTINUE_UPLOAD_REPLY); | ||
// fprintf(stderr, "read %zu from total %zu bytes.\n", read_so_far, total); | ||
CONTINUE_UPLOAD_REPLY *curep = file_chunk; | ||
int ret = curep->ret; | ||
while(ret != END_OF_FILE) | ||
{ | ||
res = ev3_write(handle, (u8*)&cu, sizeof cu); | ||
if (res < 0) | ||
{ | ||
errmsg = "Unable to write BEGIN_UPLOAD"; | ||
hiderr = ev3_error(handle); | ||
return ERR_HID; | ||
} | ||
|
||
res = ev3_read_timeout(handle, (u8 *)curep, file_chunksz, TIMEOUT); | ||
if (res <= 0) | ||
{ | ||
errmsg = "Unable to read BEGIN_UPLOAD_REPLY"; | ||
hiderr = ev3_error(handle); | ||
return ERR_HID; | ||
} | ||
if (curep->type == VM_ERROR) | ||
{ | ||
if (curep->ret < ARRAY_SIZE(ev3_error_msgs)) | ||
hiderr = ev3_error_msgs[curep->ret]; | ||
else | ||
hiderr = L"ERROR_OUT_OF_BOUNDS"; | ||
fputs("Operation failed.\nlast_reply=", stderr); | ||
print_bytes(curep, curep->packetLen); | ||
|
||
|
||
errmsg = "`BEGIN_UPLOAD` was denied."; | ||
return ERR_VM; | ||
} | ||
fwrite(burep->bytes, cu.maxBytes, 1, fp); | ||
cu.fileHandle = curep->fileHandle; | ||
size_t read_so_far = curep->packetLen + 2 - offsetof(CONTINUE_UPLOAD_REPLY, bytes); | ||
fflush(stdout); | ||
fprintf(stderr, "read %zu from total %zu bytes.\n", read_so_far, file_chunksz); | ||
ret = curep->ret; | ||
} | ||
|
||
errmsg = "`BEGIN_UPLOAD` was successful."; | ||
return ERR_UNK; | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.