This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.c
156 lines (138 loc) · 3.68 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* Copyright (C) 2018 Miguel Angel Nubla <[email protected]>
*/
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "config.h"
#include "common.h"
#include "client.h"
#include "rtsp.h"
void print_usage() {
printf(
"Usage: videop2proxy --ip CAMERA_IP --token CAMERA_HEX_TOKEN [...] \n"
"\n"
"Options:\n"
" --ip IP [Required] Camera ip address.\n"
" --token HEX_TOKEN [Required] Camera miio token.\n"
"\n"
"Modes:\n"
#ifdef ENABLE_RTSP
" --rtsp PORT Enable RTSP server.\n"
#endif
" --stdout Enable output to stdout.\n"
);
}
int tryConnect(char* ip, char* token) {
char cmd[256];
snprintf(cmd, sizeof cmd,
"python3 -c \""
"import miio;"
"result = miio.device.Device('%s', '%s').send('get_ipcprop', ['all']);"
"print(result['p2p_id']);"
"print(result['avID']);"
"print(result['avPass']);"
"\""
, ip, token);
FILE *fp;
char line[1035];
fp = popen(cmd, "r");
if (fp == NULL) {
DPRINTF("Failed to run command: %s\n", cmd);
return 1;
}
char p2p_id[1035];
char username[1035];
char password[1035];
for(int i = 1; i <= 3; i = i + 1 ){
if (fgets(line, sizeof(line), fp) == NULL) {
DPRINTF("Can't read output line %d from command: %s\n", i, cmd);
DPRINTF("Error connecting to camera, make sure ip and token are correct.\n");
return 1;
}
line[strcspn(line, "\n")] = 0;
switch(i) {
case 1:
strncpy(p2p_id, line, sizeof(line));
break;
case 2:
strncpy(username, line, sizeof(line));
break;
case 3:
strncpy(password, line, sizeof(line));
break;
}
}
pclose(fp);
return clientRun(p2p_id, username, password);
}
int main(int argc, char *argv[]) {
char *ip = "", *token = "";
MODE_RTSP = -1, MODE_STDOUT = -1;
static struct option long_options[] = {
{"ip", required_argument, 0, 'i' },
{"token", required_argument, 0, 't' },
#ifdef ENABLE_RTSP
{"rtsp", required_argument, 0, 'r' },
#endif
{"stdout", no_argument, 0, 's' },
{0, 0, 0, 0 }
};
int opt= 0;
int long_index =0;
while ((opt = getopt_long(argc, argv,"i:t:rs",
long_options, &long_index )) != -1) {
switch (opt) {
case 'i' : ip = optarg;
break;
case 't' : token = optarg;
break;
case 'r' : MODE_RTSP = atoi(optarg);
break;
case 's' : MODE_STDOUT = 1;
break;
default: print_usage();
exit(EXIT_FAILURE);
}
}
if (MODE_RTSP < 0 && MODE_STDOUT < 0) {
printf("ERROR: You must specify at least one mode.\n");
print_usage();
exit(EXIT_FAILURE);
}
#ifdef ENABLE_RTSP
if (MODE_RTSP >= 0)
{
char template[] = "/tmp/videop2proxy.XXXXXX";
char* tmpDir = mkdtemp(template);
char* tmpFile = "/fifo";
MODE_RTSP_FIFO_FILE = malloc(strlen(tmpDir) + strlen(tmpDir) + 1);
strcat(MODE_RTSP_FIFO_FILE, tmpDir);
strcat(MODE_RTSP_FIFO_FILE, tmpFile);
mkfifo(MODE_RTSP_FIFO_FILE, 0600);
pthread_t ThreadRTSP = 0;
int ret;
if ((ret=pthread_create(&ThreadRTSP, NULL, &min, NULL)))
{
DPRINTF("Create RTSP thread failed\n");
return 1;
}
MODE_RTSP_FIFO = open(MODE_RTSP_FIFO_FILE, O_WRONLY);
}
#endif
DPRINTF("Starting proxy...\n");
int delay = 10;
while (1)
{
tryConnect(ip, token);
DPRINTF("Error, waiting %d seconds and trying again.\n", delay);
sleep(delay);
}
}