diff options
Diffstat (limited to 'utils/nwztools/plattools/dest_tool.c')
-rw-r--r-- | utils/nwztools/plattools/dest_tool.c | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/utils/nwztools/plattools/dest_tool.c b/utils/nwztools/plattools/dest_tool.c new file mode 100644 index 0000000000..7b6c0a4ff6 --- /dev/null +++ b/utils/nwztools/plattools/dest_tool.c | |||
@@ -0,0 +1,218 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2016 Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "nwz_lib.h" | ||
22 | #include <string.h> | ||
23 | #include <stdlib.h> | ||
24 | |||
25 | extern char **environ; | ||
26 | |||
27 | const char *white_list[] = | ||
28 | { | ||
29 | "NWZ-E463", "NWZ-E464", "NWZ-E465", | ||
30 | "NWZ-A863", "NWZ-A864", "NWZ-A865", "NWZ-A866", "NWZ-A867", | ||
31 | NULL, | ||
32 | }; | ||
33 | |||
34 | /* get model id from ICX_MODEL_ID environment variable */ | ||
35 | unsigned long find_model_id(void) | ||
36 | { | ||
37 | const char *mid = getenv("ICX_MODEL_ID"); | ||
38 | if(mid == NULL) | ||
39 | return 0; | ||
40 | char *end; | ||
41 | unsigned long v = strtoul(mid, &end, 0); | ||
42 | if(*end) | ||
43 | return 0; | ||
44 | else | ||
45 | return v; | ||
46 | } | ||
47 | |||
48 | unsigned long read32(unsigned char *buf) | ||
49 | { | ||
50 | return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24; | ||
51 | } | ||
52 | |||
53 | void write32(unsigned char *buf, unsigned long value) | ||
54 | { | ||
55 | buf[0] = value & 0xff; | ||
56 | buf[1] = (value >> 8) & 0xff; | ||
57 | buf[2] = (value >> 16) & 0xff; | ||
58 | buf[3] = (value >> 24) & 0xff; | ||
59 | } | ||
60 | |||
61 | struct | ||
62 | { | ||
63 | unsigned long dest; | ||
64 | const char *name; | ||
65 | } g_dest_list[] = | ||
66 | { | ||
67 | { 0, "J" }, | ||
68 | { 1, "U" }, | ||
69 | { 0x101, "U2" }, | ||
70 | { 0x201, "U3" }, | ||
71 | { 0x301, "CA" }, | ||
72 | { 2, "CEV" }, | ||
73 | { 0x102, "CE7" }, | ||
74 | { 3, "CEW" }, | ||
75 | { 0x103, "CEW2" }, | ||
76 | { 4, "CN" }, | ||
77 | { 5, "KR" }, | ||
78 | { 6, "E" }, | ||
79 | { 0x106, "MX" }, | ||
80 | { 0x206, "E2" }, | ||
81 | { 0x306, "MX3" }, | ||
82 | { 7, "TW" }, | ||
83 | }; | ||
84 | |||
85 | #define NR_DEST (sizeof(g_dest_list) / sizeof(g_dest_list[0])) | ||
86 | |||
87 | int get_dest_index(unsigned long dest) | ||
88 | { | ||
89 | for(size_t i = 0; i < NR_DEST; i++) | ||
90 | if(g_dest_list[i].dest == dest) | ||
91 | return i; | ||
92 | return -1; | ||
93 | } | ||
94 | |||
95 | const char *get_dest_name(unsigned long dest) | ||
96 | { | ||
97 | int index = get_dest_index(dest); | ||
98 | return index < 0 ? "NG" : g_dest_list[index].name; | ||
99 | } | ||
100 | |||
101 | int main(int argc, char **argv) | ||
102 | { | ||
103 | /* clear screen and display welcome message */ | ||
104 | nwz_lcdmsg(true, 0, 0, "destination tool"); | ||
105 | /* open input device */ | ||
106 | int input_fd = nwz_key_open(); | ||
107 | if(input_fd < 0) | ||
108 | { | ||
109 | nwz_lcdmsg(false, 3, 4, "Cannot open input device"); | ||
110 | sleep(2); | ||
111 | return 1; | ||
112 | } | ||
113 | unsigned long model_id = find_model_id(); | ||
114 | if(model_id == 0) | ||
115 | { | ||
116 | nwz_lcdmsg(false, 3, 4, "Cannot get model ID"); | ||
117 | sleep(2); | ||
118 | return 1; | ||
119 | } | ||
120 | const char *model_name = nwz_get_model_name(model_id); | ||
121 | if(model_name == NULL) | ||
122 | model_name = "Unknown"; | ||
123 | nwz_lcdmsgf(false, 0, 2, "Model ID: %#x", model_id); | ||
124 | nwz_lcdmsgf(false, 0, 3, "Model Name: %s", model_name); | ||
125 | nwz_lcdmsg(false, 0, 5, "BACK: quit"); | ||
126 | nwz_lcdmsg(false, 0, 6, "LEFT/RIGHT: change dest"); | ||
127 | nwz_lcdmsg(false, 0, 7, "PLAY/PAUSE: change sps"); | ||
128 | bool ok_model = false; | ||
129 | for(int i = 0; white_list[i]; i++) | ||
130 | if(strcmp(white_list[i], model_name) == 0) | ||
131 | ok_model = true; | ||
132 | /* display input state in a loop */ | ||
133 | while(1) | ||
134 | { | ||
135 | unsigned char nvp_buf[20]; | ||
136 | bool ok_nvp = false; | ||
137 | if(ok_model) | ||
138 | { | ||
139 | int fd = open("/dev/icx_nvp/011", O_RDONLY); | ||
140 | if(fd >= 0) | ||
141 | { | ||
142 | ssize_t cnt = read(fd, nvp_buf, sizeof(nvp_buf)); | ||
143 | if(cnt == (ssize_t)sizeof(nvp_buf)) | ||
144 | ok_nvp = true; | ||
145 | else | ||
146 | nwz_lcdmsg(false, 1, 9, "Cannot read NVP.\n"); | ||
147 | close(fd); | ||
148 | } | ||
149 | else | ||
150 | nwz_lcdmsg(false, 1, 9, "Cannot open NVP.\n"); | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | nwz_lcdmsg(false, 1, 9, "Your model is not supported.\n"); | ||
155 | nwz_lcdmsg(false, 1, 10, "Please contact a developer.\n"); | ||
156 | } | ||
157 | /* display information */ | ||
158 | if(ok_nvp) | ||
159 | { | ||
160 | unsigned long dest = read32(nvp_buf); | ||
161 | unsigned long sps = read32(nvp_buf + 4); | ||
162 | const char *dest_name = get_dest_name(dest); | ||
163 | const char *sps_name = sps ? "ON" : "OFF"; | ||
164 | nwz_lcdmsgf(false, 1, 9, "DEST: %s (%#x) ", dest_name, dest); | ||
165 | nwz_lcdmsgf(false, 1, 10, "SPS: %s (%d) ", sps_name, sps); | ||
166 | } | ||
167 | /* wait for event */ | ||
168 | int ret = nwz_key_wait_event(input_fd, -1); | ||
169 | if(ret != 1) | ||
170 | continue; | ||
171 | struct input_event evt; | ||
172 | if(nwz_key_read_event(input_fd, &evt) != 1) | ||
173 | continue; | ||
174 | /* only act on release */ | ||
175 | if(nwz_key_event_is_press(&evt)) | ||
176 | continue; | ||
177 | int keycode = nwz_key_event_get_keycode(&evt); | ||
178 | if(keycode == NWZ_KEY_BACK) | ||
179 | break; | ||
180 | bool write_nvp = false; | ||
181 | if(keycode == NWZ_KEY_LEFT || keycode == NWZ_KEY_RIGHT) | ||
182 | { | ||
183 | int dest_idx = get_dest_index(read32(nvp_buf)); | ||
184 | /* if destination is unknown, replace by the first one */ | ||
185 | if(dest_idx == -1) | ||
186 | dest_idx = 0; | ||
187 | if(keycode == NWZ_KEY_LEFT) | ||
188 | dest_idx--; | ||
189 | else | ||
190 | dest_idx++; | ||
191 | dest_idx = (dest_idx + NR_DEST) % NR_DEST; | ||
192 | write32(nvp_buf, g_dest_list[dest_idx].dest); | ||
193 | write_nvp = true; | ||
194 | } | ||
195 | else if(keycode == NWZ_KEY_PLAY) | ||
196 | { | ||
197 | /* change 0 to 1 and anything nonzero to 0 */ | ||
198 | write32(nvp_buf + 4, read32(nvp_buf + 4) == 0 ? 1 : 0); | ||
199 | write_nvp = true; | ||
200 | } | ||
201 | /* write nvp */ | ||
202 | if(ok_nvp && write_nvp) | ||
203 | { | ||
204 | int fd = open("/dev/icx_nvp/011", O_RDWR); | ||
205 | if(fd >= 0) | ||
206 | { | ||
207 | ssize_t cnt = write(fd, nvp_buf, sizeof(nvp_buf)); | ||
208 | if(cnt != (ssize_t)sizeof(nvp_buf)) | ||
209 | nwz_lcdmsg(false, 1, 12, "Cannot write NVP.\n"); | ||
210 | close(fd); | ||
211 | } | ||
212 | else | ||
213 | nwz_lcdmsg(false, 1, 12, "Cannot open NVP.\n"); | ||
214 | } | ||
215 | } | ||
216 | /* finish nicely */ | ||
217 | return 0; | ||
218 | } | ||