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#import <Foundation/Foundation.h>
12#import <sys/sysctl.h>
13#if defined(WEBRTC_IOS)
14#import <UIKit/UIKit.h>
15#endif
16
17#include <memory>
18
19#include "helpers.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
22
23namespace webrtc {
24namespace ios {
25
26NSString* NSStringFromStdString(const std::string& stdString) {
27  // std::string may contain null termination character so we construct
28  // using length.
29  return [[NSString alloc] initWithBytes:stdString.data()
30                                  length:stdString.length()
31                                encoding:NSUTF8StringEncoding];
32}
33
34std::string StdStringFromNSString(NSString* nsString) {
35  NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
36  return std::string(reinterpret_cast<const char*>([charData bytes]),
37                     [charData length]);
38}
39
40bool CheckAndLogError(BOOL success, NSError* error) {
41  if (!success) {
42    NSString* msg =
43        [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
44                                   error.localizedDescription,
45                                   error.localizedFailureReason];
46    RTC_LOG(LS_ERROR) << StdStringFromNSString(msg);
47    return false;
48  }
49  return true;
50}
51
52// TODO(henrika): see if it is possible to move to GetThreadName in
53// platform_thread.h and base it on pthread methods instead.
54std::string GetCurrentThreadDescription() {
55  NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
56  return StdStringFromNSString(name);
57}
58
59#if defined(WEBRTC_IOS)
60std::string GetSystemName() {
61  NSString* osName = [[UIDevice currentDevice] systemName];
62  return StdStringFromNSString(osName);
63}
64
65std::string GetSystemVersionAsString() {
66  NSString* osVersion = [[UIDevice currentDevice] systemVersion];
67  return StdStringFromNSString(osVersion);
68}
69
70std::string GetDeviceType() {
71  NSString* deviceModel = [[UIDevice currentDevice] model];
72  return StdStringFromNSString(deviceModel);
73}
74
75bool GetLowPowerModeEnabled() {
76  return [NSProcessInfo processInfo].lowPowerModeEnabled;
77}
78#endif
79
80std::string GetDeviceName() {
81  size_t size;
82  sysctlbyname("hw.machine", NULL, &size, NULL, 0);
83  std::unique_ptr<char[]> machine;
84  machine.reset(new char[size]);
85  sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
86  return std::string(machine.get());
87}
88
89std::string GetProcessName() {
90  NSString* processName = [NSProcessInfo processInfo].processName;
91  return StdStringFromNSString(processName);
92}
93
94int GetProcessID() {
95  return [NSProcessInfo processInfo].processIdentifier;
96}
97
98std::string GetOSVersionString() {
99  NSString* osVersion =
100      [NSProcessInfo processInfo].operatingSystemVersionString;
101  return StdStringFromNSString(osVersion);
102}
103
104int GetProcessorCount() {
105  return [NSProcessInfo processInfo].processorCount;
106}
107
108}  // namespace ios
109}  // namespace webrtc
110