3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ps3dm_proxy.h"
27 #define PS3DM_SRTC_VERSION "0.0.1"
29 #define PS3DM_SRTC_LAID 0x1070000002000001ull
30 #define PS3DM_SRTC_PAID 0x10700003ff000001ull
41 static struct option long_opts
[] = {
42 { "help", no_argument
, NULL
, 'h' },
43 { "verbose", no_argument
, NULL
, 'v' },
44 { "version", no_argument
, NULL
, 'V' },
51 static void usage(void) {
53 "Usage: ps3dm_srtc [OPTIONS] DEVICE COMMAND [ARGS]\n"
56 " -h, --help Show this message and exit\n"
57 " -v, --verbose Increase verbosity\n"
58 " -V, --version Show version information and exit\n"
60 " get_time PARAM Returns RTC time\n"
62 "Simple example: Get RTC time:\n"
63 " ps3dm_srtc /dev/ps3dmproxy get_time 0\n");
69 static void version(void)
72 "ps3dm_srtc " PS3DM_SRTC_VERSION
"\n"
73 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
74 "This is free software. You may redistribute copies of it "
75 "under the terms of\n"
76 "the GNU General Public License 2 "
77 "<http://www.gnu.org/licenses/gpl2.html>.\n"
78 "There is NO WARRANTY, to the extent permitted by law.\n");
84 static int process_opts(int argc
, char **argv
, struct opts
*opts
)
88 while ((c
= getopt_long(argc
, argv
, "hvV", long_opts
, NULL
)) != -1) {
100 opts
->do_version
= 1;
104 fprintf(stderr
, "Invalid command option: %c\n", c
);
109 if (optind
>= argc
) {
110 fprintf(stderr
, "No device specified\n");
114 opts
->device_name
= argv
[optind
];
117 if (optind
>= argc
) {
118 fprintf(stderr
, "No command specified\n");
122 opts
->cmd
= argv
[optind
];
131 static int cmd_get_time(int fd
, struct opts
*opts
, int argc
, char **argv
)
136 struct ps3dm_hdr
*dm_hdr
;
137 struct ps3ss_hdr
*ss_hdr
;
138 struct ps3ss_srtc_get_time
*ss_srtc_get_time
;
141 if (optind
>= argc
) {
142 fprintf(stderr
, "No parameter specified\n");
146 param
= strtoul(argv
[optind
], &endptr
, 0);
147 if (*endptr
!= '\0') {
148 fprintf(stderr
, "Invalid parameter specified: %s\n", argv
[optind
]);
154 memset(buf
, 0, sizeof(buf
));
155 dm_hdr
= (struct ps3dm_hdr
*) buf
;
156 ss_hdr
= (struct ps3ss_hdr
*)(dm_hdr
+ 1);
157 ss_srtc_get_time
= (struct ps3ss_srtc_get_time
*)(ss_hdr
+ 1);
159 dm_hdr
->request_id
= 1;
160 dm_hdr
->function_id
= PS3SS_FID_SRTC
;
161 dm_hdr
->request_size
= PS3SS_HDR_SIZE
+ sizeof(struct ps3ss_srtc_get_time
);
162 dm_hdr
->response_size
= PS3SS_HDR_SIZE
+ sizeof(struct ps3ss_srtc_get_time
);
164 ss_hdr
->packet_id
= PS3SS_PID_SRTC_GET_TIME
;
165 ss_hdr
->function_id
= PS3SS_FID_SRTC
;
166 ss_hdr
->laid
= PS3DM_SRTC_LAID
;
167 ss_hdr
->paid
= PS3DM_SRTC_PAID
;
169 ss_srtc_get_time
->field0
= param
;
171 error
= ps3dm_proxy_do_request(fd
, dm_hdr
, PS3DM_HDR_SIZE
+ dm_hdr
->request_size
,
172 dm_hdr
, PS3DM_HDR_SIZE
+ dm_hdr
->response_size
);
175 fprintf(stderr
, "%s: %s\n", opts
->device_name
, strerror(errno
));
176 } else if (ss_hdr
->retval
!= 0) {
177 fprintf(stderr
, "%s: SS retval %d\n", opts
->device_name
, ss_hdr
->retval
);
180 fprintf(stdout
, "0x%016lx 0x%016lx\n", ss_srtc_get_time
->field8
,
181 ss_srtc_get_time
->field10
);
183 fprintf(stdout
, "\n");
192 int main(int argc
, char **argv
)
195 int fd
= 0, error
= 0;
197 memset(&opts
, 0, sizeof(opts
));
199 if (process_opts(argc
, argv
, &opts
)) {
208 } else if (opts
.do_version
) {
213 fd
= ps3dm_proxy_open(opts
.device_name
);
215 fprintf(stderr
, "%s: %s\n", opts
.device_name
, strerror(errno
));
220 if (!strcmp(opts
.cmd
, "get_time")) {
221 error
= cmd_get_time(fd
, &opts
, argc
, argv
);
234 ps3dm_proxy_close(fd
);