henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #if defined(WEBRTC_IOS) |
| 12 | |
| 13 | #import <Foundation/Foundation.h> |
| 14 | #import <sys/sysctl.h> |
| 15 | #import <UIKit/UIKit.h> |
| 16 | |
| 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/logging.h" |
| 19 | #include "webrtc/base/scoped_ptr.h" |
| 20 | #include "webrtc/modules/utility/interface/helpers_ios.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace ios { |
| 24 | |
| 25 | // TODO(henrika): move to shared location. |
| 26 | // See https://code.google.com/p/webrtc/issues/detail?id=4773 for details. |
| 27 | NSString* NSStringFromStdString(const std::string& stdString) { |
| 28 | // std::string may contain null termination character so we construct |
| 29 | // using length. |
| 30 | return [[NSString alloc] initWithBytes:stdString.data() |
| 31 | length:stdString.length() |
| 32 | encoding:NSUTF8StringEncoding]; |
| 33 | } |
| 34 | |
| 35 | std::string StdStringFromNSString(NSString* nsString) { |
| 36 | NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; |
| 37 | return std::string(reinterpret_cast<const char*>([charData bytes]), |
| 38 | [charData length]); |
| 39 | } |
| 40 | |
| 41 | bool CheckAndLogError(BOOL success, NSError* error) { |
| 42 | if (!success) { |
| 43 | NSString* msg = |
| 44 | [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code, |
| 45 | error.localizedDescription, |
| 46 | error.localizedFailureReason]; |
| 47 | LOG(LS_ERROR) << StdStringFromNSString(msg); |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | // TODO(henrika): see if it is possible to move to GetThreadName in |
| 54 | // platform_thread.h and base it on pthread methods instead. |
| 55 | std::string GetCurrentThreadDescription() { |
| 56 | NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; |
| 57 | return StdStringFromNSString(name); |
| 58 | } |
| 59 | |
| 60 | std::string GetSystemName() { |
| 61 | NSString* osName = [[UIDevice currentDevice] systemName]; |
| 62 | return StdStringFromNSString(osName); |
| 63 | } |
| 64 | |
| 65 | std::string GetSystemVersion() { |
| 66 | NSString* osVersion = [[UIDevice currentDevice] systemVersion]; |
| 67 | return StdStringFromNSString(osVersion); |
| 68 | } |
| 69 | |
| 70 | float GetSystemVersionAsFloat() { |
| 71 | NSString* osVersion = [[UIDevice currentDevice] systemVersion]; |
| 72 | return osVersion.floatValue; |
| 73 | } |
| 74 | |
| 75 | std::string GetDeviceType() { |
| 76 | NSString* deviceModel = [[UIDevice currentDevice] model]; |
| 77 | return StdStringFromNSString(deviceModel); |
| 78 | } |
| 79 | |
| 80 | std::string GetDeviceName() { |
| 81 | size_t size; |
| 82 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
| 83 | rtc::scoped_ptr<char[]> machine; |
| 84 | machine.reset(new char[size]); |
| 85 | sysctlbyname("hw.machine", machine.get(), &size, NULL, 0); |
| 86 | std::string raw_name(machine.get()); |
| 87 | if (!raw_name.compare("iPhone1,1")) |
| 88 | return std::string("iPhone 1G"); |
| 89 | if (!raw_name.compare("iPhone1,2")) |
| 90 | return std::string("iPhone 3G"); |
| 91 | if (!raw_name.compare("iPhone2,1")) |
| 92 | return std::string("iPhone 3GS"); |
| 93 | if (!raw_name.compare("iPhone3,1")) |
| 94 | return std::string("iPhone 4"); |
| 95 | if (!raw_name.compare("iPhone3,3")) |
| 96 | return std::string("Verizon iPhone 4"); |
| 97 | if (!raw_name.compare("iPhone4,1")) |
| 98 | return std::string("iPhone 4S"); |
| 99 | if (!raw_name.compare("iPhone5,1")) |
| 100 | return std::string("iPhone 5 (GSM)"); |
| 101 | if (!raw_name.compare("iPhone5,2")) |
| 102 | return std::string("iPhone 5 (GSM+CDMA)"); |
| 103 | if (!raw_name.compare("iPhone5,3")) |
| 104 | return std::string("iPhone 5c (GSM)"); |
| 105 | if (!raw_name.compare("iPhone5,4")) |
| 106 | return std::string("iPhone 5c (GSM+CDMA)"); |
| 107 | if (!raw_name.compare("iPhone6,1")) |
| 108 | return std::string("iPhone 5s (GSM)"); |
| 109 | if (!raw_name.compare("iPhone6,2")) |
| 110 | return std::string("iPhone 5s (GSM+CDMA)"); |
| 111 | if (!raw_name.compare("iPhone7,1")) |
| 112 | return std::string("iPhone 6 Plus"); |
| 113 | if (!raw_name.compare("iPhone7,2")) |
| 114 | return std::string("iPhone 6"); |
| 115 | if (!raw_name.compare("iPod1,1")) |
| 116 | return std::string("iPod Touch 1G"); |
| 117 | if (!raw_name.compare("iPod2,1")) |
| 118 | return std::string("iPod Touch 2G"); |
| 119 | if (!raw_name.compare("iPod3,1")) |
| 120 | return std::string("iPod Touch 3G"); |
| 121 | if (!raw_name.compare("iPod4,1")) |
| 122 | return std::string("iPod Touch 4G"); |
| 123 | if (!raw_name.compare("iPod5,1")) |
| 124 | return std::string("iPod Touch 5G"); |
| 125 | if (!raw_name.compare("iPad1,1")) |
| 126 | return std::string("iPad"); |
| 127 | if (!raw_name.compare("iPad2,1")) |
| 128 | return std::string("iPad 2 (WiFi)"); |
| 129 | if (!raw_name.compare("iPad2,2")) |
| 130 | return std::string("iPad 2 (GSM)"); |
| 131 | if (!raw_name.compare("iPad2,3")) |
| 132 | return std::string("iPad 2 (CDMA)"); |
| 133 | if (!raw_name.compare("iPad2,4")) |
| 134 | return std::string("iPad 2 (WiFi)"); |
| 135 | if (!raw_name.compare("iPad2,5")) |
| 136 | return std::string("iPad Mini (WiFi)"); |
| 137 | if (!raw_name.compare("iPad2,6")) |
| 138 | return std::string("iPad Mini (GSM)"); |
| 139 | if (!raw_name.compare("iPad2,7")) |
| 140 | return std::string("iPad Mini (GSM+CDMA)"); |
| 141 | if (!raw_name.compare("iPad3,1")) |
| 142 | return std::string("iPad 3 (WiFi)"); |
| 143 | if (!raw_name.compare("iPad3,2")) |
| 144 | return std::string("iPad 3 (GSM+CDMA)"); |
| 145 | if (!raw_name.compare("iPad3,3")) |
| 146 | return std::string("iPad 3 (GSM)"); |
| 147 | if (!raw_name.compare("iPad3,4")) |
| 148 | return std::string("iPad 4 (WiFi)"); |
| 149 | if (!raw_name.compare("iPad3,5")) |
| 150 | return std::string("iPad 4 (GSM)"); |
| 151 | if (!raw_name.compare("iPad3,6")) |
| 152 | return std::string("iPad 4 (GSM+CDMA)"); |
| 153 | if (!raw_name.compare("iPad4,1")) |
| 154 | return std::string("iPad Air (WiFi)"); |
| 155 | if (!raw_name.compare("iPad4,2")) |
| 156 | return std::string("iPad Air (Cellular)"); |
| 157 | if (!raw_name.compare("iPad4,4")) |
| 158 | return std::string("iPad mini 2G (WiFi)"); |
| 159 | if (!raw_name.compare("iPad4,5")) |
| 160 | return std::string("iPad mini 2G (Cellular)"); |
| 161 | if (!raw_name.compare("i386")) |
| 162 | return std::string("Simulator"); |
| 163 | if (!raw_name.compare("x86_64")) |
| 164 | return std::string("Simulator"); |
| 165 | LOG(LS_WARNING) << "Failed to find device name"; |
| 166 | return raw_name; |
| 167 | } |
| 168 | |
| 169 | } // namespace ios |
| 170 | } // namespace webrtc |
| 171 | |
| 172 | #endif // defined(WEBRTC_IOS) |