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