//
// Created by yyqxiaoyin on 2019-04-16.
//

#import "UIDevice+MKRRadioManagerAdd.h"
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <mach/machine.h>
#import <SystemConfiguration/CaptiveNetwork.h>

#define IOS6_7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 6.0

@implementation UIDevice (MKRRadioManagerAdd)

+ (NSString *)currentDeviceInfo {
    NSString *identifierForVendor;
    if(IOS6_7) {
        identifierForVendor  = [[UIDevice currentDevice].identifierForVendor UUIDString];
    } else {
        identifierForVendor = [self currentDeviceMacAddress];
    }
    UIDevice *device_=[UIDevice currentDevice];
    NSString *devInfo = [NSString stringWithFormat:@"device_model=%@;device_brand=%@;cpu_id=%@;cpu_type=%@;manufacturers=%@;IMEI_code=%@;phone_number=%@;country_number=%@",
                                                   device_.name,
                                                   @"666",
                                                   @"的的的",
                                                   [self getCPUType],
                                                   @"Apple Inc.",
                                                   identifierForVendor,
                                                   @"null",
                                                   device_.localizedModel
    ];
    return devInfo;
}

+(NSString*)getCPUType {
    NSMutableString *cpu = [[NSMutableString alloc] init];
    size_t size;
    cpu_type_t type;
    cpu_subtype_t subtype;
    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);
    size = sizeof(subtype);
    sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);
    if (type == CPU_TYPE_X86) {
        [cpu appendString:@"x86 "];
    } else if (type == CPU_TYPE_ARM) {
        if(subtype == CPU_SUBTYPE_ARM_V6){
            [cpu appendString:@"ARMV6 "];
        }else if(subtype == CPU_SUBTYPE_ARM_V7 ||subtype == CPU_SUBTYPE_ARM_V7EM||subtype == CPU_SUBTYPE_ARM_V7F||subtype == CPU_SUBTYPE_ARM_V7K||subtype == CPU_SUBTYPE_ARM_V7M){
            [cpu appendString:@"ARMV7 "];
        }else if(subtype == CPU_SUBTYPE_ARM_V7S){
            [cpu appendString:@"ARMV7s "];
        }else{
            [cpu appendString:@"ARM"];
            [cpu appendFormat:@",Type:%d",subtype];
        }
    }else if(type == CPU_TYPE_ARM64){
        [cpu appendString:@"ARM64"];
    }
    return cpu;
}


+ (NSString *)currentDeviceMacAddress {

    int                 mgmtInfoBase[6];
    char                *msgBuffer = NULL;
    size_t              length;
    unsigned char       macAddress[6];
    struct if_msghdr    *interfaceMsgStruct;
    struct sockaddr_dl  *socketStruct;
    NSString            *errorFlag = NULL;

    // Setup the management Information Base (mib)
    mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
    mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
    mgmtInfoBase[2] = 0;
    mgmtInfoBase[3] = AF_LINK;        // Request link layer information
    mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces

    // With all configured interfaces requested, get handle index
    if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
        errorFlag = @"if_nametoindex failure";
    else
    {
        // Get the size of the data available (store in len)
        if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
            errorFlag = @"sysctl mgmtInfoBase failure";
        else
        {
            // Alloc memory based on above call
            if ((msgBuffer = malloc(length)) == NULL)
                errorFlag = @"buffer allocation failure";
            else
            {
                // Get system information, store in buffer
                if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
                    errorFlag = @"sysctl msgBuffer failure";
            }
        }
    }

    // Befor going any further...
    if (errorFlag != NULL)
    {
        free(msgBuffer);
        return errorFlag;
    }

    // Map msgbuffer to interface message structure
    interfaceMsgStruct = (struct if_msghdr *) msgBuffer;

    // Map to link-level socket structure
    socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

    // Copy link layer address data in socket structure to an array
    memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);

    // Read from char array into a string object, into traditional Mac address format
    NSString *macAddressString = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",
                                                            macAddress[0], macAddress[1], macAddress[2],
                                                            macAddress[3], macAddress[4], macAddress[5]];
    // Release the buffer memory
    free(msgBuffer);

    return macAddressString;
}

+ (NSString *)currentSSID{
    NSString *ssid = @"Not Found";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil) {
        CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict != nil) {
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict);
            ssid = [dict valueForKey:@"SSID"];
        }
    }
    return ssid;
}

@end
