Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
chdk_pipe.c
Go to the documentation of this file.
1 /* execill - How a parent and child might communicate. */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <string.h>
12 
13 #include "chdk_pipe.h"
14 
15 #define READ 0
16 #define WRITE 1
17 #define MAX_FILENAME 255
18 #define SHELL "/root/develop/allthings_obc2014/src/popcorn/popcorn.sh"
19 
20 
21 const char *setup =
22  "lua props=require(\"propcase\");print(\"SetupScript\");set_prop(props.ISO_MODE,3200);set_prop(props.FLASH_MODE,2);set_prop(props.RESOLUTION,0);set_prop(props.DATE_STAMP,0);set_prop(props.AF_ASSIST_BEAM,0);set_prop(props.QUALITY,0);print(\"Ready\");\n";
23 
24 static int fo, fi;
25 static void wait_for_cmd(int timeout);
26 static void wait_for_img(char *filename, int timeout);
27 static pid_t popen2(const char *command, int *infp, int *outfp);
28 
29 /*void main(int argc, char ** argv, char ** envp)
30 {
31  int i;
32  char filename[MAX_FILENAME];
33 
34  // Initialize chdk pipe
35  chdk_pipe_init();
36 
37  // Start taking photos
38  for(i=0; i < 3; i++) {
39  chdk_pipe_shoot(filename);
40  printf("Shot image: %s\n", filename);
41  }
42 
43  // Initialize chdk pipe
44  chdk_pipe_deinit();
45 }*/
46 
50 void chdk_pipe_init(void)
51 {
52  /* Check if SHELL is started */
53  if (popen2(SHELL, &fi, &fo) <= 0) {
54  perror("Can't start SHELL");
55  exit(1);
56  }
57  wait_for_cmd(10);
58 
59  /* Connect to the camera */
60  write(fi, "connect\n", 8);
61  wait_for_cmd(10);
62 
63  /* Kill all running scripts */
64  //write(fi, "killscript\n", 11);
65  //wait_for_cmd(10);
66 
67  /* Start recording mode */
68  write(fi, "rec\n", 4);
69  wait_for_cmd(10);
70 
71  /* Start rsint mode */
72  write(fi, setup, strlen(setup));
73  wait_for_cmd(strlen(setup));
74 }
75 
79 void chdk_pipe_deinit(void)
80 {
81  /* Stop rsint mode */
82  //write(fi, "q\n", 2);
83  //wait_for_cmd(10);
84 
85  /* Quit SHELL */
86  write(fi, "quit\n", 3);
87 }
88 
92 void chdk_pipe_shoot(char *filename)
93 {
94  write(fi, "rs /root\n", 9);
95  wait_for_img(filename, 10);
96 }
97 
102 static void wait_for_img(char *filename, int timeout)
103 {
104  int hash_cnt = 0;
105  char ch;
106  int filename_idx = 0;
107 
108  while (hash_cnt < 4) {
109  if (read(fo, &ch, 1)) {
110  if (ch == '#') {
111  hash_cnt++;
112  } else if (hash_cnt >= 2 && ch != '#') {
113  filename[filename_idx++] = ch;
114  }
115  }
116  }
117 
118  filename[filename_idx] = 0;
120 }
121 
126 static void wait_for_cmd(int timeout)
127 {
128  char ch;
129  do {
130  read(fo, &ch, 1);
131  } while (ch != '>');
132 }
133 
137 static pid_t popen2(const char *command, int *infp, int *outfp)
138 {
139  int p_stdin[2], p_stdout[2];
140  pid_t pid;
141 
142  if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0) {
143  return -1;
144  }
145 
146  pid = fork();
147 
148  if (pid < 0) {
149  return pid;
150  } else if (pid == 0) {
151  close(p_stdin[WRITE]);
152  dup2(p_stdin[READ], READ);
153  close(p_stdout[READ]);
154  dup2(p_stdout[WRITE], WRITE);
155 
156  execl("/bin/sh", "sh", "-c", command, NULL);
157  perror("execl");
158  exit(1);
159  }
160 
161  if (infp == NULL) {
162  close(p_stdin[WRITE]);
163  } else {
164  *infp = p_stdin[WRITE];
165  }
166 
167  if (outfp == NULL) {
168  close(p_stdout[READ]);
169  } else {
170  *outfp = p_stdout[READ];
171  }
172 
173  return pid;
174 }
#define SHELL
Definition: chdk_pipe.c:18
static pid_t popen2(const char *command, int *infp, int *outfp)
Open a process with stdin and stdout.
Definition: chdk_pipe.c:137
static void wait_for_cmd(int timeout)
Wait for the commandline to be available TODO: add timeout.
Definition: chdk_pipe.c:126
const char * setup
Definition: chdk_pipe.c:21
void chdk_pipe_init(void)
Initialize the CHDK pipe.
Definition: chdk_pipe.c:50
static int fo
Definition: chdk_pipe.c:24
#define WRITE
Definition: chdk_pipe.c:16
static int fi
Definition: chdk_pipe.c:24
void chdk_pipe_shoot(char *filename)
Shoot an image.
Definition: chdk_pipe.c:92
#define READ
Definition: chdk_pipe.c:15
static void wait_for_img(char *filename, int timeout)
Wait for the image to be available TODO: add timeout.
Definition: chdk_pipe.c:102
void chdk_pipe_deinit(void)
Deinitialize CHDK pipe.
Definition: chdk_pipe.c:79
static float timeout