summaryrefslogtreecommitdiff
path: root/remotecfg/main.c (plain)
blob: ad7546fe4f1ea216c894fd44637db6c55229c91c
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <errno.h>
10#include <libgen.h>
11
12#include "common.h"
13
14#define DEVICE_FILE "/dev/amremote"
15#define SYSDIR "/sys/class/remote/amremote/"
16
17unsigned char dbg_flag = 0;
18
19static void usage(char *name)
20{
21 fprintf(stderr,"Usage: %s [-c cfg_file] [-t tab_file] [-d]\n", name);
22 exit(EXIT_FAILURE);
23}
24
25static void printTabFile(S_TAB_FILE_T *tabFile)
26{
27 int i;
28
29 prDbg("custom_name = %s\n", tabFile->tab.custom_name);
30 prDbg("fn_key_scancode = 0x%x\n", tabFile->tab.cursor_code.fn_key_scancode);
31 prDbg("cursor_left_scancode = 0x%x\n", tabFile->tab.cursor_code.cursor_left_scancode);
32 prDbg("cursor_right_scancode = 0x%x\n", tabFile->tab.cursor_code.cursor_right_scancode);
33 prDbg("cursor_up_scancode = 0x%x\n", tabFile->tab.cursor_code.cursor_up_scancode);
34 prDbg("cursor_down_scancode = 0x%x\n", tabFile->tab.cursor_code.cursor_down_scancode);
35 prDbg("cursor_ok_scancode = 0x%x\n", tabFile->tab.cursor_code.cursor_ok_scancode);
36 prDbg("custom_code = 0x%x\n", tabFile->tab.custom_code);
37 prDbg("release_delay = %d\n", tabFile->tab.release_delay);
38 prDbg("map_size = %d\n", tabFile->tab.map_size);
39 for (i = 0; i < tabFile->tab.map_size; i++)
40 prDbg("key[%d] = 0x%x\n", i, tabFile->tab.codemap[i].code);
41
42}
43
44static void printCfgFile(S_CFG_FILE_T *cfgFile)
45{
46 prDbg("work_mode = %d\n", cfgFile->workMode);
47 prDbg("repeat_enable = %d\n", cfgFile->repeatEnable);
48 prDbg("debug_enable = %d\n", cfgFile->debugEnable);
49 prDbg("max_frame_time = %d\n", cfgFile->sw_data.max_frame_time);
50}
51
52static int handleCfgFile(char *name, char *val, void *data)
53{
54 S_CFG_FILE_T *cfgFile = (S_CFG_FILE_T *)data;
55
56 if (MATCH("work_mode", name))
57 cfgFile->workMode = strtoul(val, NULL, 0);
58 else if (MATCH("repeat_enable", name))
59 cfgFile->repeatEnable = strtoul(val, NULL, 0);
60 else if (MATCH("debug_enable", name))
61 cfgFile->debugEnable = strtoul(val, NULL, 0);
62 else if (MATCH("max_frame_time", name))
63 cfgFile->sw_data.max_frame_time = strtoul(val, NULL, 0);
64 else
65 return FAIL;
66
67 return SUCC;
68
69}
70
71static int handleTabFile(char *name, char *val, void *data)
72{
73 S_TAB_FILE_T *tabFile = (S_TAB_FILE_T *)data;
74
75 if (MATCH("custom_name", name))
76 strncpy(tabFile->tab.custom_name, val, CUSTOM_NAME_LEN);
77 else if (MATCH("fn_key_scancode", name))
78 tabFile->tab.cursor_code.fn_key_scancode = strtoul(val, NULL, 0);
79 else if (MATCH("cursor_left_scancode", name))
80 tabFile->tab.cursor_code.cursor_left_scancode = strtoul(val, NULL, 0);
81 else if (MATCH("cursor_right_scancode", name))
82 tabFile->tab.cursor_code.cursor_right_scancode = strtoul(val, NULL, 0);
83 else if (MATCH("cursor_up_scancode", name))
84 tabFile->tab.cursor_code.cursor_up_scancode = strtoul(val, NULL, 0);
85 else if (MATCH("cursor_down_scancode", name))
86 tabFile->tab.cursor_code.cursor_down_scancode = strtoul(val, NULL, 0);
87 else if (MATCH("cursor_ok_scancode", name))
88 tabFile->tab.cursor_code.cursor_ok_scancode = strtoul(val, NULL, 0);
89 else if (MATCH("custom_code", name))
90 tabFile->tab.custom_code = strtoul(val, NULL, 0);
91 else if (MATCH("release_delay", name))
92 tabFile->tab.release_delay = strtoul(val, NULL, 0);
93 else if (MATCH("mapcode", name)) {
94 tabFile->tab.codemap[tabFile->tab.map_size].code = atoi(val);
95 tabFile->tab.map_size ++;
96 }
97 else
98 return FAIL;
99
100 return SUCC;
101}
102
103int SetConfigFile(int devfd, char *cfgdir)
104{
105 int ret = 0;
106 S_CFG_FILE_T *cfgFile = NULL;
107
108 prDbg("cfgdir = %s\n", cfgdir);
109 cfgFile = malloc(sizeof(S_CFG_FILE_T));
110 if (!cfgFile) {
111 fprintf(stderr, "failed to allocate memory: %s\n",
112 strerror(errno));
113 ret = -1;
114 goto err;
115 }
116 memset(cfgFile, 0, sizeof(S_CFG_FILE_T));
117 if (ParseFile((const char *)cfgdir,
118 handleCfgFile, (void *)cfgFile) < 0) {
119 fprintf(stderr, "failed to parse file: %s\n", cfgdir);
120 ret = -1;
121 goto err;
122 }
123 printCfgFile(cfgFile);
124 SetCfgPara(devfd, SYSDIR, cfgFile);
125err:
126 if (cfgFile)
127 free(cfgFile);
128 return ret;
129}
130
131int SetTabFile(int devfd, char *tabdir)
132{
133 int ret = 0;
134 S_TAB_FILE_T *tabFile = NULL;
135
136 prDbg("tabdir = %s\n", tabdir);
137 tabFile = (S_TAB_FILE_T *)malloc(sizeof(S_TAB_FILE_T) + (MAX_KEYMAP_SIZE << 2));
138 if (!tabFile)
139 {
140 fprintf(stderr, "failed to allocate memory: %s\n", strerror(errno));
141 ret = -1;
142 goto err;
143 }
144 memset(tabFile, 0, sizeof(S_TAB_FILE_T) + (MAX_KEYMAP_SIZE << 2));
145 memset(&tabFile->tab.cursor_code, 0xff, sizeof(struct cursor_codemap));
146 if (ParseFile((const char *)tabdir, handleTabFile, (void *)tabFile) < 0)
147 {
148 fprintf(stderr, "failed to parse file: %s\n", tabdir);
149 ret = -1;
150 goto err;
151 }
152 printTabFile(tabFile);
153 SetTabPara(devfd, tabFile);
154err:
155 if (tabFile)
156 free(tabFile);
157 return ret;
158}
159
160int main(int argc, char *argv[])
161{
162 int ch;
163 int devfd = -1;
164 int ret;
165 char *cfgfile = NULL;
166 char *tabfile = NULL;
167
168 opterr = 0; /*disable the 'getopt' debug info*/
169 while ((ch = getopt(argc, argv, "dc:t:")) != -1) {
170 switch (ch) {
171 case 'd':
172 dbg_flag = 1;
173 break;
174 case 'c':
175 cfgfile = optarg;
176 break;
177 case 't':
178 tabfile = optarg;
179 break;
180 case '?':
181 usage(basename(argv[0]));
182 break;
183 }
184
185 }
186
187 devfd = OpenDevice(DEVICE_FILE);
188 if (devfd< 0)
189 return FAIL;
190
191 if (CheckVersion(devfd) < 0)
192 goto err;
193
194 if (cfgfile)
195 if (SetConfigFile(devfd, cfgfile))
196 goto err;
197
198 if (tabfile)
199 if (SetTabFile(devfd, tabfile))
200 goto err;
201err:
202 CloseDevice(devfd);
203 return 0;
204}
205