Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 19 | #include <cutils/klog.h> |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 20 | #include <getopt.h> |
| 21 | #include <stdlib.h> |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 22 | |
| 23 | #include "debug.h" |
Szymon Starzycki | e160f81 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 24 | #include "trigger.h" |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 25 | #include "socket_client.h" |
Szymon Starzycki | 781f9fc | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 26 | #include "secure.h" |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 27 | |
| 28 | unsigned int debug_level = DEBUG; |
| 29 | |
| 30 | void commands_init(); |
| 31 | void usb_init(); |
| 32 | void config_init(); |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 33 | int transport_socket_init(); |
Szymon Starzycki | 6581228 | 2013-09-13 15:37:08 -0700 | [diff] [blame] | 34 | int network_discovery_init(); |
Szymon Starzycki | 8c3263e | 2013-09-18 16:12:43 -0700 | [diff] [blame] | 35 | void ssh_server_start(); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 36 | |
| 37 | int main(int argc, char **argv) |
| 38 | { |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 39 | int socket_client = 0; |
| 40 | int c; |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 41 | int network = 1; |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 42 | |
| 43 | klog_init(); |
| 44 | klog_set_level(6); |
| 45 | |
| 46 | const struct option longopts[] = { |
| 47 | {"socket", no_argument, 0, 'S'}, |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 48 | {"nonetwork", no_argument, 0, 'n'}, |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 49 | {0, 0, 0, 0} |
| 50 | }; |
| 51 | |
| 52 | while (1) { |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 53 | c = getopt_long(argc, argv, "Sn", longopts, NULL); |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 54 | /* Alphabetical cases */ |
| 55 | if (c < 0) |
| 56 | break; |
| 57 | switch (c) { |
| 58 | case 'S': |
| 59 | socket_client = 1; |
| 60 | break; |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 61 | case 'n': |
| 62 | network = 0; |
| 63 | break; |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 64 | case '?': |
| 65 | return 1; |
| 66 | default: |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 71 | (void)argc; |
| 72 | (void)argv; |
| 73 | |
| 74 | klog_init(); |
| 75 | klog_set_level(6); |
| 76 | |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 77 | if (socket_client) { |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 78 | //TODO: Shouldn't we change current tty into raw mode? |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 79 | run_socket_client(); |
| 80 | } |
| 81 | else { |
Szymon Starzycki | 781f9fc | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 82 | cert_init_crypto(); |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 83 | config_init(); |
| 84 | load_trigger(); |
| 85 | commands_init(); |
| 86 | usb_init(); |
Szymon Starzycki | 8fa2f34 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 87 | |
| 88 | if (network) { |
| 89 | if (!transport_socket_init()) |
| 90 | exit(1); |
| 91 | ssh_server_start(); |
| 92 | network_discovery_init(); |
| 93 | } |
| 94 | |
Szymon Starzycki | b015b16 | 2013-09-05 14:26:28 -0700 | [diff] [blame] | 95 | while (1) { |
| 96 | sleep(1); |
| 97 | } |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 98 | } |
| 99 | return 0; |
| 100 | } |