ev3duder  0.3.0
EV3 Downloader/Uploader
dl.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <inttypes.h>
10 #include <errno.h>
11 
12 #include "ev3_io.h"
13 
14 #include "defs.h"
15 #include "packets.h"
16 #include "error.h"
17 #include "funcs.h"
18 
20 #define CHUNK_SIZE 1000 // EV3's HID driver doesn't do packets > 1024B
21 
26 int dl(const char *path, FILE *fp)
27 {
28  int res;
29  size_t path_sz = strlen(path) + 1;
30 
31  BEGIN_UPLOAD *bu = packet_alloc(BEGIN_UPLOAD, path_sz);
32  memcpy(bu->fileName, path, path_sz);
33  bu->maxBytes = CHUNK_SIZE;
34 
35  //print_bytes(bu, bu->packetLen + PREFIX_SIZE);
36  res = ev3_write(handle, (u8 *)bu, bu->packetLen + PREFIX_SIZE);
37  if (res < 0)
38  {
39  errmsg = "Unable to write BEGIN_UPLOAD.";
40  return ERR_COMM;
41  }
42  size_t file_chunksz = sizeof(BEGIN_UPLOAD_REPLY) + bu->maxBytes;
43  void *file_chunk = malloc(file_chunksz);
44 
45  BEGIN_UPLOAD_REPLY *burep = file_chunk;
46 
47  res = ev3_read_timeout(handle, (u8 *)burep, file_chunksz, TIMEOUT);
48  if (res <= 0)
49  {
50  errmsg = "Unable to read BEGIN_UPLOAD";
51  return ERR_COMM;
52  }
53 
54  if (burep->type == VM_ERROR)
55  {
56  errno = burep->ret;
57  fputs("Operation failed.\nlast_reply=", stderr);
58  print_bytes(burep, burep->packetLen);
59 
60 
61  errmsg = "`BEGIN_UPLOAD` was denied.";
62  return ERR_VM;
63  }
64  unsigned read_so_far = burep->packetLen + 2 - offsetof(BEGIN_UPLOAD_REPLY, bytes);
65  fwrite(burep->bytes, read_so_far, 1, fp);
66 
67  unsigned total = burep->fileSize;
68 
70  cu.fileHandle =burep->fileHandle;
72  //fprintf(stderr, "read %u from total %u bytes.\n", read_so_far, total);
73  CONTINUE_UPLOAD_REPLY *curep = file_chunk;
74  while(read_so_far < total)
75  {
76  res = ev3_write(handle, (u8*)&cu, sizeof cu);
77  if (res < 0)
78  {
79  errmsg = "Unable to write CONTINUE_UPLOAD";
80  return ERR_COMM;
81  }
82 
83  res = ev3_read_timeout(handle, (u8 *)curep, file_chunksz, TIMEOUT);
84  if (res <= 0)
85  {
86  errmsg = "Unable to read CONTINUE_UPLOAD_REPLY";
87  return ERR_COMM;
88  }
89  if (curep->type == VM_ERROR)
90  {
91  errno = curep->ret;
92  fputs("Operation failed.\nlast_reply=", stderr);
93  print_bytes(curep, curep->packetLen);
94 
95  errmsg = "`CONTINUE_UPLOAD` was denied.";
96  return ERR_VM;
97  }
98  size_t read_this_time = curep->packetLen + 2 - offsetof(CONTINUE_UPLOAD_REPLY, bytes);
99  fwrite(curep->bytes, read_this_time, 1, fp);
100  cu.fileHandle = curep->fileHandle;
101  read_so_far += read_this_time;
102  //fprintf(stderr, "read %u from total %u bytes.\n", read_so_far, total);
103  }
104 
105  errmsg = "`BEGIN_UPLOAD` was successful.";
106  return ERR_UNK;
107 
108 }
109 
110 
const CONTINUE_UPLOAD CONTINUE_UPLOAD_INIT
Definition: packets.c:22
EXTERN void * handle
Definition: ev3_io.h:17
char fileName[]
Definition: packets.h:123
EV3_REPLY_FIELDS u8 fileHandle
Definition: packets.h:156
EV3_COMMAND_FIELDS u16 maxBytes
Definition: packets.h:121
Definition: error.h:20
Error enumerations and decriptions.
int dl(const char *path, FILE *fp)
download remote source rem to local file loc
Definition: dl.c:26
#define print_bytes(buf, len)
Definition: defs.h:34
#define packet_alloc(type, extra)
Definition: packets.h:328
continue download from ev3
Definition: packets.h:140
EV3_REPLY_FIELDS u32 fileSize
Definition: packets.h:132
EXTERN int(* ev3_read_timeout)(void *, u8 *, size_t, int milliseconds)
Definition: ev3_io.h:14
#define CHUNK_SIZE
Transmission is done in units smaller or equal to CHUNK_SIZE.
Definition: dl.c:20
continue download from ev3 reply
Definition: packets.h:151
EV3_COMMAND_FIELDS u8 fileHandle
Definition: packets.h:145
#define TIMEOUT
Definition: defs.h:46
download from EV3 reply
Definition: packets.h:127
packed structs for the packets.
Definition: error.h:20
uint8_t u8
Definition: defs.h:9
Definition: error.h:20
download from EV3
Definition: packets.h:116
Definition: error.h:22
EXTERN const char * errmsg
global variable for last error message
Definition: error.h:25
contains declarations for ev3 commands
EXTERN int(* ev3_write)(void *, const u8 *, size_t)
Definition: ev3_io.h:13
#define PREFIX_SIZE
Definition: packets.h:54