b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * MARVELL CONFIDENTIAL |
| 5 | * Copyright 2012-2014 Marvell International Ltd All Rights Reserved. |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Marvell International Ltd or its |
| 8 | * suppliers or licensors. Title to the Material remains with Marvell International Ltd |
| 9 | * or its suppliers and licensors. The Material contains trade secrets and |
| 10 | * proprietary and confidential information of Marvell or its suppliers and |
| 11 | * licensors. The Material is protected by worldwide copyright and trade secret |
| 12 | * laws and treaty provisions. No part of the Material may be used, copied, |
| 13 | * reproduced, modified, published, uploaded, posted, transmitted, distributed, |
| 14 | * or disclosed in any way without Marvell's prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Marvell in writing. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <apather.h> |
| 25 | |
| 26 | PathCmd cmd; |
| 27 | PathsList paths_list; |
| 28 | CommandRet command_ret; |
| 29 | |
| 30 | static int client_socket = -1; |
| 31 | #define PATHSTATUS_MAX 2048 |
| 32 | char pathstatus[PATHSTATUS_MAX] = {0}; |
| 33 | |
| 34 | static int list_paths() { |
| 35 | int i = 0; |
| 36 | printf("Paths list:\n"); |
| 37 | for(i = 0; i < paths_list.num; i++) { |
| 38 | printf(" : %s\n", paths_list.path[i]); |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int init_socket() { |
| 44 | |
| 45 | struct sockaddr_in client_addr; |
| 46 | bzero(&client_addr, sizeof(client_addr)); |
| 47 | client_addr.sin_family = AF_INET; |
| 48 | client_addr.sin_addr.s_addr = htons(INADDR_ANY); |
| 49 | client_addr.sin_port = htons(0); |
| 50 | |
| 51 | client_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 52 | if (client_socket < 0) { |
| 53 | printf("Failed to create socket ...\n"); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | struct linger so_linger; |
| 58 | so_linger.l_onoff = 1; |
| 59 | so_linger.l_linger = 0; |
| 60 | |
| 61 | setsockopt(client_socket, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger)); |
| 62 | |
| 63 | if (bind(client_socket, (struct sockaddr*)&client_addr, sizeof(client_addr))) { |
| 64 | printf("Failed to bind the socket ...\n"); |
| 65 | goto init_error; |
| 66 | } |
| 67 | |
| 68 | struct sockaddr_in server_addr; |
| 69 | bzero(&server_addr, sizeof(server_addr)); |
| 70 | server_addr.sin_family = AF_INET; |
| 71 | server_addr.sin_addr.s_addr = htons(INADDR_ANY); |
| 72 | server_addr.sin_port = htons(SERVER_PORT); |
| 73 | |
| 74 | socklen_t server_addr_length = sizeof(server_addr); |
| 75 | if (connect(client_socket, (struct sockaddr*)&server_addr, server_addr_length) < 0) { |
| 76 | printf("Failed to connect the socket ...\n"); |
| 77 | goto init_error; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | |
| 82 | init_error: |
| 83 | close(client_socket); |
| 84 | return -1; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | static int help() { |
| 89 | printf("Usage: apather <options> [command]\n"); |
| 90 | printf("\nAvailable options:\n"); |
| 91 | printf(" -h, --help show this help\n"); |
| 92 | printf(" -l, --list list all paths from xml\n"); |
| 93 | printf(" -e, --enable <path> path to enable\n"); |
| 94 | printf(" -d, --disable <path> path to disable\n"); |
| 95 | printf(" -s, --setvolume <path> path to setvolume\n"); |
| 96 | printf(" -m, --mute <path> path to mute/unmute\n"); |
| 97 | printf(" -v, --volume <value> volume in dec to set\n"); |
| 98 | printf(" -t, --status path status\n"); |
| 99 | printf(" -c, --camera camera to use back mic\n"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int main(int argc, char *argv[]) { |
| 104 | int need_help = 0; |
| 105 | int op = 0; |
| 106 | char path[MAX_PATH] = "\0"; |
| 107 | unsigned int volume = 0; |
| 108 | int enable = -1; |
| 109 | int use_camera = 0; |
| 110 | memset(&cmd, 0, sizeof(cmd)); |
| 111 | |
| 112 | static const struct option long_options[] = |
| 113 | { |
| 114 | {"help", 0, NULL, 'h'}, |
| 115 | {"list", 0, NULL, 'l'}, |
| 116 | {"status", 0, NULL, 't'}, |
| 117 | {"enable", 1, NULL, 'e'}, |
| 118 | {"disable", 1, NULL, 'd'}, |
| 119 | {"setvolume", 1, NULL, 's'}, |
| 120 | {"mute", 1, NULL, 'm'}, |
| 121 | {"volume", 1, NULL, 'v'}, |
| 122 | {"camera", 1, NULL, 'c'}, |
| 123 | {NULL, 0, NULL, 0}, |
| 124 | }; |
| 125 | |
| 126 | if (argc < 2) { |
| 127 | help(); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | while (1) { |
| 132 | int c; |
| 133 | if ((c = getopt_long(argc, argv, "hlte:d:s:m:v:c", long_options, NULL)) < 0) |
| 134 | break; |
| 135 | switch (c) { |
| 136 | case 'h': |
| 137 | help(); |
| 138 | return 0; |
| 139 | case 'l': |
| 140 | op = OP_LIST; |
| 141 | break; |
| 142 | case 't': |
| 143 | op = OP_STATUS; |
| 144 | break; |
| 145 | case 'e': |
| 146 | strncpy(path, optarg, MAX_PATH); |
| 147 | op = OP_ENABLE; |
| 148 | break; |
| 149 | case 'd': |
| 150 | strncpy(path, optarg, MAX_PATH); |
| 151 | op = OP_DISABLE; |
| 152 | break; |
| 153 | case 's': |
| 154 | strncpy(path, optarg, MAX_PATH); |
| 155 | op = OP_SETVOLUME; |
| 156 | break; |
| 157 | case 'm': |
| 158 | strncpy(path, optarg, MAX_PATH); |
| 159 | op = OP_MUTE; |
| 160 | break; |
| 161 | case 'v': |
| 162 | volume = atol(optarg); |
| 163 | break; |
| 164 | case 'c': |
| 165 | use_camera = 1; |
| 166 | break; |
| 167 | default: |
| 168 | fprintf(stderr, "Invalid argument.\n"); |
| 169 | need_help++; |
| 170 | } |
| 171 | if (need_help) { |
| 172 | help(); |
| 173 | return 1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | memset(&command_ret, 0, sizeof(command_ret)); |
| 178 | command_ret.ret = -1; |
| 179 | |
| 180 | if (op != 0) { |
| 181 | strncpy(cmd.path, path, MAX_PATH); |
| 182 | //cmd.volume = volume ? volume : 0xFF; |
| 183 | cmd.volume = volume; |
| 184 | cmd.op = op; |
| 185 | cmd.camera = use_camera; |
| 186 | if (init_socket() < 0) { |
| 187 | return -1; |
| 188 | } |
| 189 | int ret = send(client_socket, &cmd, sizeof(cmd), 0); |
| 190 | if (ret < 0) { |
| 191 | printf("Failed to send data to server ...\n"); |
| 192 | goto socket_error; |
| 193 | } |
| 194 | if (op == OP_LIST) { |
| 195 | ret = recv(client_socket, &paths_list, sizeof(paths_list), 0); |
| 196 | if (ret < 0) { |
| 197 | printf("Failed to receive paths list from server ...\n"); |
| 198 | goto socket_error; |
| 199 | } |
| 200 | list_paths(); |
| 201 | } else if (op == OP_STATUS) { |
| 202 | ret = recv(client_socket, pathstatus, PATHSTATUS_MAX, 0); |
| 203 | if (ret < 0) { |
| 204 | printf("Failed to receive paths status from server ...\n"); |
| 205 | goto socket_error; |
| 206 | } |
| 207 | printf("%s", pathstatus); |
| 208 | } else { |
| 209 | ret = recv(client_socket, &command_ret, sizeof(command_ret), 0); |
| 210 | if (ret < 0) { |
| 211 | printf("Failed to receive command return from server ...\n"); |
| 212 | goto socket_error; |
| 213 | } |
| 214 | if (command_ret.ret != ACM_RC_OK) { |
| 215 | printf("Failed to run the command. ErrorCode: \"%s\"\n", RetErrorString[command_ret.ret].info); |
| 216 | } |
| 217 | printf("%s\n", command_ret.comment); |
| 218 | } |
| 219 | close(client_socket); |
| 220 | } |
| 221 | return 0; |
| 222 | |
| 223 | socket_error: |
| 224 | close(client_socket); |
| 225 | return -1; |
| 226 | } |