ev3duder  0.3.0
EV3 Downloader/Uploader
ls.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.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 
35 #define MAX_READ 1024
36 int ls(const char *path)
37 {
38  int res;
39  size_t path_sz = strlen(path) + 1;
40  LIST_FILES *list = packet_alloc(LIST_FILES, path_sz);
41  list->maxBytes = MAX_READ;
42  memcpy(list->path, path, path_sz);
43 
44  print_bytes(list, list->packetLen + PREFIX_SIZE);
45  res = ev3_write(handle, (u8 *)list, list->packetLen + PREFIX_SIZE);
46  if (res < 0)
47  {
48  errmsg = "Unable to write LIST_FILES.";
49  return ERR_COMM;
50  }
51  fputs("Checking reply: \n", stderr);
52  size_t listrep_sz = sizeof(LIST_FILES_REPLY) + list->maxBytes;
53  LIST_FILES_REPLY *listrep = calloc(listrep_sz, 1);
54 
55  res = ev3_read_timeout(handle, (u8 *)listrep, listrep_sz, TIMEOUT);
56  if (res <= 0)
57  {
58  errmsg = "Unable to read LIST_FILES";
59  return ERR_COMM;
60  }
61 
62  if (listrep->type == VM_ERROR)
63  {
64  errno = listrep->ret;
65  fputs("Operation failed.\nlast_reply=", stderr);
66  print_bytes(listrep, listrep->packetLen);
67 
68  errmsg = "`LIST_FILES` was denied.";
69  return ERR_VM;
70  }
71 
72  fwrite(listrep->list, 1, listrep->packetLen - 10 <= MAX_READ ? listrep->packetLen - 10 : 1024, stdout); // No NUL Termination over Serial COM for whatever reason.
73  //
74  // Excerpt from the lms2012O sources: - LIST_FILES should work as long as list does not exceed 1014 bytes. CONTINUE_LISTFILES has NOT been implemented yet.
75 #if !LEGO_FIXED_CONTINUE_LIST_FILES
76 
77 #else
78  size_t read_so_far = listrep->packetLen + 2 - offsetof(LIST_FILES_REPLY, list);
79  size_t total = listrep->listSize;
81  listcon.handle =listrep->handle;
82  listcon.listSize = 100;
83  // fprintf(stderr, "read %zu from total %zu bytes.\n", read_so_far, total);
84  size_t listconrep_sz = sizeof(CONTINUE_LIST_FILES_REPLY) + MAX_READ;
85  CONTINUE_LIST_FILES_REPLY *listconrep = malloc(listconrep_sz);
86  int ret = listconrep->ret;
87  while(ret != END_OF_FILE)
88  {
89  CONTINUE_LIST_FILES_REPLY *listconrep = malloc(listconrep_sz);
90  res = ev3_write(handle, (u8*)&listcon, sizeof listcon);
91  if (res < 0)
92  {
93  errmsg = "Unable to write LIST_FILES";
94  return ERR_COMM;
95  }
96 
97  res = ev3_read_timeout(handle, (u8 *)listconrep, listconrep_sz, TIMEOUT);
98  if (res <= 0)
99  {
100  errmsg = "Unable to read LIST_FILES_REPLY";
101  return ERR_COMM;
102  }
103  if (listconrep->type == VM_ERROR)
104  {
105  errno = listconrep->ret;
106  fputs("Operation failed.\nlast_reply=", stderr);
107  print_bytes(listconrep, listconrep->packetLen);
108 
109 
110  errmsg = "`LIST_FILES` was denied.";
111  return ERR_VM;
112  }
113  fprintf(stdout, "%s", listconrep->list);
114  listcon.handle = listconrep->handle;
115  listcon.listSize = MAX_READ;
116  size_t read_so_far = listconrep->packetLen + 2 - offsetof(CONTINUE_LIST_FILES_REPLY, list);
117  fflush(stdout);
118  //fprintf(stderr, "read %zu from total %zu bytes.\n", read_so_far, listconrep_sz);
119  ret = listconrep->ret;
120  }
121 #endif
122 
123  errmsg = "`LIST_FILES` was successful.";
124  return ERR_UNK;
125 
126 }
127 
EXTERN void * handle
Definition: ev3_io.h:17
EV3_COMMAND_FIELDS u16 maxBytes
Definition: packets.h:170
u8 path[]
Definition: packets.h:172
Directory contents on EV3.
Definition: packets.h:195
Definition: error.h:20
Error enumerations and decriptions.
#define print_bytes(buf, len)
Definition: defs.h:34
#define packet_alloc(type, extra)
Definition: packets.h:328
EXTERN int(* ev3_read_timeout)(void *, u8 *, size_t, int milliseconds)
Definition: ev3_io.h:14
const CONTINUE_LIST_FILES CONTINUE_LIST_FILES_INIT
Definition: packets.c:28
#define TIMEOUT
Definition: defs.h:46
packed structs for the packets.
EV3_COMMAND_FIELDS u8 handle
Definition: packets.h:212
List files on EV3.
Definition: packets.h:166
Definition: error.h:20
uint8_t u8
Definition: defs.h:9
Definition: error.h:20
Definition: error.h:22
#define MAX_READ
List files and directories at path. path is always relative to /home/root/lms2012/prjs/sys/ ...
Definition: ls.c:35
EXTERN const char * errmsg
global variable for last error message
Definition: error.h:25
EV3_REPLY_FIELDS u8 handle
Definition: packets.h:222
contains declarations for ev3 commands
EXTERN int(* ev3_write)(void *, const u8 *, size_t)
Definition: ev3_io.h:13
int ls(const char *path)
list contents of remote directory rem
Definition: ls.c:36
#define PREFIX_SIZE
Definition: packets.h:54