1 | /* |
2 | Copyright (c) 2012, Broadcom Europe Ltd |
3 | All rights reserved. |
4 | |
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the following conditions are met: |
7 | * Redistributions of source code must retain the above copyright |
8 | notice, this list of conditions and the following disclaimer. |
9 | * Redistributions in binary form must reproduce the above copyright |
10 | notice, this list of conditions and the following disclaimer in the |
11 | documentation and/or other materials provided with the distribution. |
12 | * Neither the name of the copyright holder nor the |
13 | names of its contributors may be used to endorse or promote products |
14 | derived from this software without specific prior written permission. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | |
29 | /* ---- Include Files ---------------------------------------------------- */ |
30 | |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <stdarg.h> |
34 | #include <string.h> |
35 | #include <time.h> |
36 | |
37 | #include "interface/vmcs_host/vc_vchi_gencmd.h" |
38 | #include "interface/vmcs_host/vc_gencmd_defs.h" |
39 | |
40 | void show_usage() |
41 | { |
42 | puts( "Usage: vcgencmd [-t] command" ); |
43 | puts( "Send a command to the VideoCore and print the result.\n" ); |
44 | puts( " -t Time how long the command takes to complete" ); |
45 | puts( " -h, --help Show this information\n" ); |
46 | puts( "Use the command 'vcgencmd commands' to get a list of available commands\n" ); |
47 | puts( "Exit status:" ); |
48 | puts( " 0 command completed successfully" ); |
49 | puts( " -1 problem with VCHI" ); |
50 | puts( " -2 VideoCore returned an error\n" ); |
51 | puts( "For further documentation please see" ); |
52 | puts( "https://www.raspberrypi.org/documentation/raspbian/applications/vcgencmd.md\n" ); |
53 | } |
54 | |
55 | int main( int argc, char **argv ) |
56 | { |
57 | VCHI_INSTANCE_T vchi_instance; |
58 | VCHI_CONNECTION_T *vchi_connection = NULL; |
59 | |
60 | if ( argc == 1 ) |
61 | { |
62 | // no arguments passed, so show basic usage |
63 | show_usage(); |
64 | return 0; |
65 | } |
66 | |
67 | vcos_init(); |
68 | |
69 | if ( vchi_initialise( &vchi_instance ) != 0) |
70 | { |
71 | fprintf( stderr, "VCHI initialization failed\n" ); |
72 | return -1; |
73 | } |
74 | |
75 | //create a vchi connection |
76 | if ( vchi_connect( NULL, 0, vchi_instance ) != 0) |
77 | { |
78 | fprintf( stderr, "VCHI connection failed\n" ); |
79 | return -1; |
80 | } |
81 | |
82 | vc_vchi_gencmd_init(vchi_instance, &vchi_connection, 1 ); |
83 | |
84 | if (argc > 1) |
85 | { |
86 | // first check if we were invoked with either -h or --help |
87 | // in which case show basic usage and exit |
88 | if( strcmp( argv[1], "-h" ) == 0 || strcmp( argv[1], "--help" ) == 0 ) |
89 | { |
90 | show_usage(); |
91 | return 0; |
92 | } |
93 | |
94 | int i = 1; |
95 | char buffer[ GENCMDSERVICE_MSGFIFO_SIZE ]; |
96 | size_t buffer_offset = 0; |
97 | clock_t before=0, after=0; |
98 | double time_diff; |
99 | uint32_t show_time = 0; |
100 | int ret; |
101 | |
102 | //reset the string |
103 | buffer[0] = '\0'; |
104 | |
105 | //first, strip out a potential leading -t |
106 | if( strcmp( argv[1], "-t" ) == 0 ) |
107 | { |
108 | show_time = 1; |
109 | i++; |
110 | } |
111 | |
112 | for (; i <= argc-1; i++) |
113 | { |
114 | buffer_offset = vcos_safe_strcpy( buffer, argv[i], sizeof(buffer), buffer_offset ); |
115 | buffer_offset = vcos_safe_strcpy( buffer, " " , sizeof(buffer), buffer_offset ); |
116 | } |
117 | |
118 | if( show_time ) |
119 | before = clock(); |
120 | |
121 | //send the gencmd for the argument |
122 | if (( ret = vc_gencmd_send( "%s" , buffer )) != 0 ) |
123 | { |
124 | printf( "vc_gencmd_send returned %d\n" , ret ); |
125 | } |
126 | |
127 | //get + print out the response! |
128 | if (( ret = vc_gencmd_read_response( buffer, sizeof( buffer ) )) != 0 ) |
129 | { |
130 | printf( "vc_gencmd_read_response returned %d\n" , ret ); |
131 | } |
132 | buffer[ sizeof(buffer) - 1 ] = 0; |
133 | |
134 | if( show_time ) |
135 | { |
136 | after = clock(); |
137 | time_diff = ((double) (after - before)) / CLOCKS_PER_SEC; |
138 | printf( "Time taken: %f seconds (%f msecs) (%f usecs)\n" , time_diff, time_diff * 1000, time_diff * 1000000 ); |
139 | } |
140 | |
141 | if ( buffer[0] != '\0' ) |
142 | { |
143 | if ( buffer[ strlen( buffer) - 1] == '\n' ) |
144 | { |
145 | fputs( buffer, stdout ); |
146 | } |
147 | else |
148 | { |
149 | if (strncmp( buffer, "error=" , 6) == 0 ) |
150 | { |
151 | fprintf (stderr, "%s\n" , buffer); |
152 | if ( strcmp( buffer, "error=1 error_msg=\"Command not registered\"" ) == 0 ) |
153 | { |
154 | fprintf( stderr, "Use 'vcgencmd commands' to get a list of commands\n" ); |
155 | } |
156 | return -2; |
157 | } |
158 | else |
159 | { |
160 | puts(buffer); |
161 | } |
162 | } |
163 | } |
164 | } |
165 | |
166 | vc_gencmd_stop(); |
167 | |
168 | //close the vchi connection |
169 | if ( vchi_disconnect( vchi_instance ) != 0) |
170 | { |
171 | fprintf( stderr, "VCHI disconnect failed\n" ); |
172 | return -1; |
173 | } |
174 | return 0; |
175 | } |
176 | |