ev3duder  0.3.0
EV3 Downloader/Uploader
run.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <hidapi/hidapi.h>
11 
12 #include "defs.h"
13 #include "packets.h"
14 #include "error.h"
15 #include "funcs.h"
16 #include "ev3_io.h"
17 
19 static const u8 run1[] = {0xC0, 0x08, 0x82, 0x01, 0x00, 0x84};
20 static const u8 run2[] = {0x60, 0x64, 0x03, 0x01, 0x60, 0x64, 0x00};
21 
30 int run(const char *exec, unsigned timeout)
31 {
32  timeout = timeout ?: TIMEOUT;
33  int res;
34  union {
35  VM_REPLY packet;
36  u8 bytes[1024];
37  }reply;
38  size_t exec_sz = strlen(exec) + 1;
39 
40  EXECUTE_FILE *run = packet_alloc(EXECUTE_FILE, sizeof (run1) + exec_sz + sizeof (run2));
41 
42  // *INDENT-OFF*
43  mempcpy(mempcpy(mempcpy((u8 *)&run->bytes, // run->bytes = [run1] + [exec] + [run2]
44  run1, sizeof run1),
45  exec, exec_sz),
46  run2, sizeof run2);
47  // *INDENT-ON*
48 
49  //FIXME: inquire whether start succeeded. check communicaton developer manual pdf (debug mode maybe?)
50  res = ev3_write(handle, (u8 *)run, run->packetLen + PREFIX_SIZE);
51  if (res < 0)
52  return ERR_COMM;
53 
54  res = ev3_read_timeout(handle, reply.bytes, sizeof reply + 2, timeout);
55  if (res < 0)
56  return ERR_COMM;
57  else if (res == 0 || (res > 0 && memcmp(&reply.packet, &EXECUTE_FILE_REPLY_SUCCESS, sizeof reply.packet) == 0))
58  {
59  errmsg = "`exec` has been successful.";
60  return ERR_UNK;
61  }
62  else if (ev3_close == (void (*)())hid_close) //FIXME: to more general solution after ev3 tunnel is fully implemented
63  {
64  u32 len;
65  if (sscanf((const char*)reply.bytes, "\t%08x\n", &len))
66  {
67  len += 10;
68  printf("%.*s", len > 1000 ? 1000 : len, (char*)reply.bytes + 10);
69  len-=1000;
70  while ((res = hid_read_timeout(handle, reply.bytes,
71  sizeof reply + 2, 100)) > 0 )
72  {
73  printf("%.*s", len > 1000 ? 1000 : len, (char*)reply.bytes);
74  len-=1000;
75  }
76  if (res != 0)
77  return ERR_COMM;
78  }
79  }
80 
81  errmsg = "`exec` status unknown.";
82  return ERR_UNK;
83 }
84 
EXTERN void * handle
Definition: ev3_io.h:17
EV3_VM_COMMAND_FIELDS u8 bytes[]
Definition: packets.h:289
Error enumerations and decriptions.
int run(const char *exec, unsigned timeout)
run rbf file
Definition: run.c:30
EXTERN void(* ev3_close)(void *)
Definition: ev3_io.h:16
#define packet_alloc(type, extra)
Definition: packets.h:328
uint32_t u32
Definition: defs.h:11
unsigned timeout
Definition: main.c:126
EXTERN int(* ev3_read_timeout)(void *, u8 *, size_t, int milliseconds)
Definition: ev3_io.h:14
#define TIMEOUT
Definition: defs.h:46
packed structs for the packets.
base reply packet
Definition: packets.h:314
Definition: error.h:20
uint8_t u8
Definition: defs.h:9
Definition: error.h:20
const VM_REPLY EXECUTE_FILE_REPLY_SUCCESS
Definition: packets.c:49
EXTERN const char * errmsg
global variable for last error message
Definition: error.h:25
contains declarations for ev3 commands
base packet
Definition: packets.h:285
EXTERN int(* ev3_write)(void *, const u8 *, size_t)
Definition: ev3_io.h:13
#define PREFIX_SIZE
Definition: packets.h:54