bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 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 | |
| 18 | #include <string> |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <pthread.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/threads.h> |
| 24 | #include "MtpTypes.h" |
| 25 | #include "MtpPacket.h" |
| 26 | #include "MtpDataPacket.h" |
| 27 | #include "MtpDatabase.h" |
| 28 | #include "MtpRequestPacket.h" |
| 29 | #include "MtpResponsePacket.h" |
| 30 | #include "mtp_MtpDatabase.hpp" |
| 31 | #include "mtp_MtpServer.hpp" |
| 32 | #include "twrpMtp.hpp" |
| 33 | #include "MtpDebug.h" |
| 34 | |
| 35 | #ifdef TWRPMTP |
| 36 | static void usage(std::string prg) { |
| 37 | printf("Usage: %s <OPTIONS>\n", prg.c_str()); |
| 38 | printf("Options:\n"); |
| 39 | printf("\t-h, --help\t\tShow Usage\n"); |
| 40 | printf("\t-s1, --storage1 /path/to/dir\t\tDestination to first storage directory\n"); |
| 41 | printf("\t-s2, --storage2 /path/to/dir\t\tDestination to first storage directory\n"); |
| 42 | printf("\t-sN, --storageN /path/to/dir\t\tDestination to first storage directory\n"); |
| 43 | } |
| 44 | |
| 45 | int main(int argc, char* argv[]) { |
| 46 | printf("argc: %d\n", argc); |
| 47 | if (argc < 2) { |
| 48 | usage(argv[0]); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | std::vector <std::string> storages; |
| 53 | |
| 54 | for (int i = 1; i < argc; ++i) { |
| 55 | std::string arg = argv[i]; |
| 56 | if ((arg == "-h") || (arg == "--help")) { |
| 57 | usage(argv[0]); |
| 58 | } |
| 59 | else { |
| 60 | storages.push_back(arg); |
| 61 | } |
| 62 | } |
| 63 | printf("starting\n"); |
| 64 | twmtp_MtpServer* mtp = new twmtp_MtpServer(); |
| 65 | mtp->set_storages(storages); |
| 66 | mtp->start(); |
| 67 | return 0; |
| 68 | } |
| 69 | #endif //def TWRPMTP |
| 70 | |
Matt Mower | 72cf09d | 2015-11-12 01:29:58 -0600 | [diff] [blame] | 71 | twrpMtp::twrpMtp(int debug_enabled) { |
Ethan Yonker | 6d154c4 | 2014-09-03 14:19:43 -0500 | [diff] [blame] | 72 | if (debug_enabled) |
| 73 | MtpDebug::enableDebug(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 74 | mtpstorages = new storages; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 75 | mtp_read_pipe = -1; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int twrpMtp::start(void) { |
| 79 | MTPI("Starting MTP\n"); |
| 80 | twmtp_MtpServer *mtp = new twmtp_MtpServer(); |
| 81 | mtp->set_storages(mtpstorages); |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 82 | mtp->set_device_info(); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 83 | mtp->set_read_pipe(mtp_read_pipe); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 84 | mtp->start(); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 88 | pthread_t twrpMtp::threadserver(void) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 89 | pthread_t thread; |
| 90 | ThreadPtr mtpptr = &twrpMtp::start; |
| 91 | PThreadPtr p = *(PThreadPtr*)&mtpptr; |
| 92 | pthread_create(&thread, NULL, p, this); |
| 93 | return thread; |
| 94 | } |
| 95 | |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 96 | pid_t twrpMtp::forkserver(int mtppipe[2]) { |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 97 | pid_t pid; |
| 98 | if ((pid = fork()) == -1) { |
| 99 | MTPE("MTP fork failed.\n"); |
| 100 | return 0; |
| 101 | } |
| 102 | if (pid == 0) { |
| 103 | // Child process |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 104 | close(mtppipe[1]); // Child closes write side |
| 105 | mtp_read_pipe = mtppipe[0]; |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 106 | start(); |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 107 | MTPD("MTP child process exited.\n"); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 108 | close(mtppipe[0]); |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 109 | _exit(0); |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 110 | } else { |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 111 | MTPD("MTP child PID: %d\n", pid); |
Ethan Yonker | 8dfa777 | 2014-09-04 21:48:41 -0500 | [diff] [blame] | 112 | return pid; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
bigbiff | 7cb4c33 | 2014-11-26 20:36:07 -0500 | [diff] [blame] | 117 | void twrpMtp::addStorage(std::string display, std::string path, int mtpid, uint64_t maxFileSize) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 118 | s = new storage; |
| 119 | s->display = display; |
| 120 | s->mount = path; |
| 121 | s->mtpid = mtpid; |
bigbiff | 7cb4c33 | 2014-11-26 20:36:07 -0500 | [diff] [blame] | 122 | s->maxFileSize = maxFileSize; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 123 | MTPD("twrpMtp mtpid: %d\n", s->mtpid); |
| 124 | mtpstorages->push_back(s); |
| 125 | } |