// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/QMIThread.c
// Function: WdsStartNwInterfaceReq
static USHORT WdsStartNwInterfaceReq(PQMUX_MSG pMUXMsg, void *arg) {
    PQMIWDS_TECHNOLOGY_PREFERECE pTechPref;
    PQMIWDS_AUTH_PREFERENCE pAuthPref;
    PQMIWDS_USERNAME pUserName;
    PQMIWDS_PASSWD pPasswd;
    PQMIWDS_APNNAME pApnName;
    PQMIWDS_IP_FAMILY_TLV pIpFamily;
    USHORT TLVLength = 0;
    UCHAR *pTLV;
    PROFILE_T *profile = (PROFILE_T *)arg;
    const char *profile_user = profile->user;
    const char *profile_password = profile->password;
    int profile_auth = profile->auth;

    if (s_is_cdma && (profile_user == NULL || profile_user[0] == '\0') && (profile_password == NULL || profile_password[0] == '\0')) {
        profile_user = "ctnet@mycdma.cn";
        profile_password = "vnet.mobi";
        profile_auth = 2; //chap
    }

    pTLV = (UCHAR *)(&pMUXMsg->StartNwInterfaceReq + 1);
    pMUXMsg->StartNwInterfaceReq.Length = 0;

    // Set technology Preferece
    pTechPref = (PQMIWDS_TECHNOLOGY_PREFERECE)(pTLV + TLVLength);
    pTechPref->TLVType = 0x30;
    pTechPref->TLVLength = cpu_to_le16(0x01);
    if (s_is_cdma == 0)
        pTechPref->TechPreference = 0x01;
    else
        pTechPref->TechPreference = 0x02;
    TLVLength +=(le16_to_cpu(pTechPref->TLVLength) + sizeof(QCQMICTL_TLV_HDR));

    // Set APN Name
    if (profile->apn && !s_is_cdma) { //cdma no apn
        pApnName = (PQMIWDS_APNNAME)(pTLV + TLVLength);
        pApnName->TLVType = 0x14;
        pApnName->TLVLength = cpu_to_le16(strlen(profile->apn));
        qstrcpy((char *)&pApnName->ApnName, profile->apn);
        TLVLength +=(le16_to_cpu(pApnName->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set User Name
    if (profile_user) {
        pUserName = (PQMIWDS_USERNAME)(pTLV + TLVLength);
        pUserName->TLVType = 0x17;
        pUserName->TLVLength = cpu_to_le16(strlen(profile_user));
        qstrcpy((char *)&pUserName->UserName, profile_user);
        TLVLength += (le16_to_cpu(pUserName->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set Password
    if (profile_password) {
        pPasswd = (PQMIWDS_PASSWD)(pTLV + TLVLength);
        pPasswd->TLVType = 0x18;
        pPasswd->TLVLength = cpu_to_le16(strlen(profile_password));
        qstrcpy((char *)&pPasswd->Passwd, profile_password);
	TLVLength += (le16_to_cpu(pPasswd->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Set Auth Protocol
    if (profile_user && profile_password) {
        pAuthPref = (PQMIWDS_AUTH_PREFERENCE)(pTLV + TLVLength);
        pAuthPref->TLVType = 0x16;
        pAuthPref->TLVLength = cpu_to_le16(0x01);
        pAuthPref->AuthPreference = profile_auth; // 0 ~ None, 1 ~ Pap, 2 ~ Chap, 3 ~ MsChapV2
        TLVLength += (le16_to_cpu(pAuthPref->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    // Add IP Family Preference
    pIpFamily = (PQMIWDS_IP_FAMILY_TLV)(pTLV + TLVLength);
    pIpFamily->TLVType = 0x19;
    pIpFamily->TLVLength = cpu_to_le16(0x01);
    pIpFamily->IpFamily = profile->curIpFamily;
    TLVLength += (le16_to_cpu(pIpFamily->TLVLength) + sizeof(QCQMICTL_TLV_HDR));

    //Set Profile Index
    if (profile->pdp && !s_is_cdma) { //cdma only support one pdp, so no need to set profile index
        PQMIWDS_PROFILE_IDENTIFIER pProfileIndex = (PQMIWDS_PROFILE_IDENTIFIER)(pTLV + TLVLength);
        pProfileIndex->TLVLength = cpu_to_le16(0x01);
        pProfileIndex->TLVType = 0x31;
        pProfileIndex->ProfileIndex = profile->pdp;
        if (s_is_cdma && s_hdr_personality == 0x02) {
            pProfileIndex->TLVType = 0x32; //profile_index_3gpp2
            pProfileIndex->ProfileIndex = 101;
        }
        TLVLength += (le16_to_cpu(pProfileIndex->TLVLength) + sizeof(QCQMICTL_TLV_HDR));
    }

    return sizeof(QMIWDS_START_NETWORK_INTERFACE_REQ_MSG) + TLVLength;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r12
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %r15
movl 0x18(%rsi), %eax
cmpl $0x0, 0x1bd46(%rip)     # 0x29c34
je 0xdf17
testq %r14, %r14
je 0xdefb
cmpb $0x0, (%r14)
jne 0xdf17
testq %r15, %r15
je 0xdf06
cmpb $0x0, (%r15)
jne 0xdf17
pushq $0x2
popq %rax
leaq 0x10d0a(%rip), %r15     # 0x1ec1a
leaq 0x10cf3(%rip), %r14     # 0x1ec0a
movl %eax, 0x4(%rsp)
leaq 0x7(%r12), %rbp
andw $0x0, 0x5(%r12)
movb $0x30, 0x7(%r12)
pushq $0x1
popq %rdi
callq 0xe749
movw %ax, 0x8(%r12)
cmpl $0x1, 0x1bcf2(%rip)     # 0x29c34
movb $0x2, %cl
sbbb $0x0, %cl
movb %cl, 0xa(%r12)
movzwl %ax, %edi
callq 0xe73e
movl %eax, %r12d
leal 0x3(%r12), %r13d
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xdfad
cmpl $0x0, 0x1bcc9(%rip)     # 0x29c34
jne 0xdfad
movzwl %r13w, %eax
leaq (%rax,%rbp), %r13
addq $0x3, %r13
movb $0x14, -0x3(%r13)
callq 0x5160
movzwl %ax, %edi
callq 0xe749
movw %ax, -0x2(%r13)
movq (%rbx), %rsi
movq %r13, %rdi
callq 0xd9ad
movzwl -0x2(%r13), %edi
callq 0xe73e
leal (%r12,%rax), %r13d
addl $0x6, %r13d
testq %r14, %r14
je 0xdff7
movzwl %r13w, %eax
leaq (%rax,%rbp), %r12
addq $0x3, %r12
movb $0x17, -0x3(%r12)
movq %r14, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xe749
movw %ax, -0x2(%r12)
movq %r12, %rdi
movq %r14, %rsi
callq 0xd9ad
movzwl -0x2(%r12), %edi
callq 0xe73e
addl %eax, %r13d
addl $0x3, %r13d
testq %r15, %r15
je 0xe079
movzwl %r13w, %r13d
leaq 0x3(,%r13), %r12
addq %rbp, %r12
movb $0x18, -0x3(%r12)
movq %r15, %rdi
callq 0x5160
movzwl %ax, %edi
callq 0xe749
movw %ax, -0x2(%r12)
movq %r12, %rdi
movq %r15, %rsi
callq 0xd9ad
movzwl -0x2(%r12), %edi
callq 0xe73e
addl %eax, %r13d
addl $0x3, %r13d
testq %r14, %r14
je 0xe079
movzwl %r13w, %r14d
movb $0x16, (%rbp,%r14)
pushq $0x1
popq %rdi
callq 0xe749
movw %ax, 0x1(%rbp,%r14)
movl 0x4(%rsp), %ecx
movb %cl, 0x3(%rbp,%r14)
movzwl %ax, %edi
callq 0xe73e
addl %eax, %r13d
addl $0x3, %r13d
movzwl %r13w, %r14d
movb $0x19, (%rbp,%r14)
pushq $0x1
popq %rdi
callq 0xe749
movw %ax, 0x1(%rbp,%r14)
movb 0x116(%rbx), %cl
movb %cl, 0x3(%rbp,%r14)
movzwl %ax, %edi
callq 0xe73e
leal (%rax,%r13), %r14d
addl $0x3, %r14d
cmpl $0x0, 0x48(%rbx)
je 0xe108
cmpl $0x0, 0x1bb7b(%rip)     # 0x29c34
jne 0xe108
movzwl %r14w, %r15d
pushq $0x1
popq %rdi
callq 0xe749
movw %ax, 0x1(%rbp,%r15)
movb $0x31, (%rbp,%r15)
movb 0x48(%rbx), %cl
movb %cl, 0x3(%rbp,%r15)
cmpl $0x0, 0x1bb52(%rip)     # 0x29c34
je 0xe0f9
cmpl $0x2, 0x1bb4d(%rip)     # 0x29c38
jne 0xe0f9
movb $0x32, (%rbp,%r15)
movb $0x65, 0x3(%rbp,%r15)
movzwl %ax, %edi
callq 0xe73e
addl %eax, %r14d
addl $0x3, %r14d
addl $0x4, %r14d
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq