11. Use of M601 network

1 network API

1.1 Usage

The GPRS PDP context programming process is as follows:
Step 1: Register the PDP callback function. Call ZYF_GprsRegister function to register the callback function of GPRS.
Step 2: Set the PDP context. Call ZYF_GprsConfig function to configure GPRS PDP context, including APN name, user name and password.
Step 3: Activate PDP. Call the ZYF_GprsActivate function to activate the GPRS PDP context. The activation result usually has a notification message in the callback function Callback_GPRS_Actived, and it can also be queried synchronously. The maximum activation of PDP is about 180s.
Step 4: Check whether the PDP is activated successfully. Call ZYF_GprsGetStatus to query the PDP activation status.
Step 5: Obtain the local IP address. Call ZYF_GprsGetLocalIPAddress to get the local IP address. GPRS_ACTIVATED means activation is successful.
Step 6: PDP deactivation. Call ZYF_GprsDeactivate to close the GPRS PDP context. The deactivation result generally has a notification message in the callback function CallBack_GPRS_Deactived, and it can also be queried synchronously. The maximum deactivation time of GPRS is about 90 seconds.
Step 7: Check whether the PDP is activated successfully. Call ZYF_GprsGetStatus to query the PDP activation status. GPRS_DEACTIVATED indicates successful deactivation.

1.2 API functions

1.2.1 ZYF_GprsRegister
This function is used to register the callback function
of GPRS message · Function prototype
int32_t ZYF_GprsRegister(uint8_t contextId, ZYF_PdpCtxCb_t* callback_func);
· Parameter
contextId: [input] PDP context, can be set to 0 or 1.
callback_func: [output] points to the GPRS callback function interface, used to notify GPRS activation and deactivation results

· Return value
GPRS_PDP_SUCCESS: successful
GPRS_PDP_INVAL: invalid parameter
GPRS_PDP_ALREADY: already registered

1.2.2 ZYF_PdpActiveCallback
This function is the callback function of PDP activation result
· Function prototype
void (*ZYF_PdpActiveCallback)(uint8_t contextId, int32_t errCode);
· Parameter
contextId: PDP context ID, which is the same as the one used for calling ZYF_GprsActivate, can be set to 0 Or 1
errCode: GPRS activation result, GPRS_PDP_SUCCESS means activation is successful, otherwise it is failure.
· Return value
NONE


1.2.3 ZYF_PdpDeactiveCallback
This function is the callback function of the PDP deactivation result
· Function prototype
void (*ZYF_PdpDeactiveCallback)(uint8_t contextId, int32_t errCode);
· Parameter
contextId: PDP context ID, which is the same as the one used for calling ZYF_GprsActivate, can be set to 0 or 1
errCode: GPRS deactivation result.
· Return value
NONE

1.2.4 ZYF_GprsConfig
This function is used to configure APN parameters. APN parameters include APN name, password and authorization method (PAP/CHAP)

· Function prototype
int32_t ZYF_GprsConfig (uint8_t the contextId, ZYF_PdpCfg_t * CFG);
typedef struct { uint8_t apnName [MAX_GPRS_APN_LEN]; uint8_t apnUserId [MAX_GPRS_USER_NAME_LEN]; uint8_t apnPasswd [MAX_GPRS_PASSWORD_LEN]; uint8_t PdpType; uint8_t authType; PAP or CHAP // void * the Reserved1; // Qos void* Reserved2; // } ZYF_PdpCfg_t; · Parameter contextId: PDP context ID, which is the same as the one used for calling ZYF_GprsActivate, can be set to 0 or 1 cfg: APN parameter structure pointer apnName: APN name apnUserId: user name apnPasswd: password authtype: authorization method PAP or CHAP · Return value GPRS_PDP_SUCCESS: success GPRS_PDP_ERROR: parameter error

















1.2.5
The interface function ZYF_GprsActivate is used to activate a PDP context. The activation process takes a long time, up to about 150 seconds, depending on the current network situation. There will be a notification message in the ZYF_PdpActiveCallback callback function for activation or failure.
· Function prototype
int32_t ZYF_GprsActivate(uint8_t contextId);
· Parameter
contextId: PDP context ID, which is the same as the one used to call ZYF_GprsActivate, can be set to 0 or 1.
· Return value
GPRS_PDP_SUCCESS: success
GPRS_PDP_ALREADY: being activated or activated
GPRS_PDP_ERROR: failure

1.2.6 ZYF_GprsGetStatus
This function is used to query the activation status
of the PDP · Function prototype
int32_t ZYF_GprsGetStatus(uint8_t contextId, int32_t *pdp_status);
· Parameter
contextId: PDP context ID, which is the same as the one used for calling ZYF_GprsActivate, can be set to 0 or 1
pdp_status: GPRS_ACTIVATED means activated, GPRS_DEACTIVATED means deactivated
· Return value
GPRS_PDP_SUCCESS: success
GPRS_PDP_ERROR: error, not registered GPRS and set APN parameters, etc.

1.2.7 ZYF_GprsDeactivatePDP
This function is used to deactivate a PDP context. The deactivation process takes a long time, up to about 90 seconds, depending on the current network situation. There will be a notification message in the ZYF_PdpDeactiveCallback callback function for successful or failed deactivation.
* Function prototype
int32_t ZYF_GprsGetStatus (uint8_t contextId, int32_t * pdp_status);
· parameters
contextId: PDP context ID, with calls ZYF_GprsActivate used for the same, can be set to 0 or 1
pdp_status: GPRS_ACTIVATED represent activated, GPRS_DEACTIVATED said it had deactivated
· Return value
PRS_PDP_SUCCESS: success
GPRS_PDP_ERROR: invalid parameter
GPRS_PDP_ERROR: activation failure

1.2.8 ZYF_GprsGetLocalIPAddress
This function is used to query the local address

· Function prototype
int32_t ZYF_GprsGetLocalIPAddress(uint8_t contextId, uint8_t *ipAddr);
· Parameter
contextId: PDP context ID, which is the same as the one used for calling ZYF_GprsActivate, can be set to 0 or 1
ipAddr: Point to an array that stores local IP addresses (IPv4 type) Pointer
· Return value
GPRS_PDP_SUCCESS: Success
GPRS_PDP_ERROR: Error

2 Introduction to network routines

This chapter mainly introduces how to use app_network.c in SDK.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac

In the routine, first send AT commands to configure the basic parameters of the network connection through ZYF_RIL_NetworkInit(), and then configure the GPRS parameters through ZYF_NetworkActiveReq() in ZYF_NetworkThread(). Some functions that need to be used during GPRS registration are used in ZYF_NetworkActiveReq(). For details, please refer to the API introduction section above.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ril_network.h"
#include "zyf_error.h"
#include "zyf_trace.h"
#include "zyf_gprs.h"
#include "zyf_thread.h"
#include "zyf_atclient.h"
#include "zyf_system.h"
#include "app_network.h"


/* network */
static ZYF_NwStat_t s_tNwState;

extern void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode);
extern void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode);

static ZYF_PdpCtxCb_t s_tPdpCb = {
    .ZYF_PdpActiveCallback = ZYF_PdpActiveCnf,
    .ZYF_PdpDeactiveCallback = ZYF_PdpDeactiveCnf,
};


#define NW_GET_SIMCARD_STATE    0
#define NW_GET_GSM_STATE        1
#define NW_GET_GPRS_STATE       2
#define NW_GET_CSQ_VALUE        3
#define NW_PDP_CFG              4
#define NW_PDP_ACTIVE           5
#define NW_PDP_WAIT_ACTIVE      6
#define NW_PDP_GET_IPADDR       7

#define NW_RET_ON_GOING         0
#define NW_RET_ERR              1
#define NW_RET_PDP_ACTIVE       2

int ZYF_NetworkActiveAuto(ZYF_NwStat_t *ptNwStat, uint8_t chCtxId)
{ 
    ZYF_PdpCfg_t tPdpCtx;
    uint8_t ipAddr[4] = {0};
    int32_t nStatus;
    int32_t nRet = -1;

    if (chCtxId <= 0) {
        return GPRS_PDP_ERROR;
    }

    switch (ptNwStat->chState) {
        case NW_GET_SIMCARD_STATE:
            nRet = ZYF_IsSimValid();
            if (!nRet) {
                ZYF_LOG("get sim card statuts ok! => %d", ptNwStat->chSimStat);
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_GET_GSM_STATE;
            } else {
                ZYF_LOG("failed to get SIM status!");
                ZYF_ThreadSleep(2000);
            }
            break;
            
        case NW_GET_GSM_STATE:
            nRet = ZYF_GetCregStatus(&ptNwStat->chGsmReg);
            //nRet = ZYF_RIL_GetCregStatus(&ptNwStat->chGsmReg);
            if (ZYF_RET_OK == nRet && GSM_REG_HOME_NW == ptNwStat->chGsmReg) {
                ZYF_LOG("get gsm status ok! => %d", ptNwStat->chGsmReg);
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_GET_GPRS_STATE;
            } else {
                if (++ ptNwStat->chTimeOut >= 10) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                    ZYF_LOG("failed to get GSM status!");
                }
            }
            break;

        case NW_GET_GPRS_STATE:
            nRet = ZYF_GetCgregStatus(&ptNwStat->chGprsReg, ptNwStat->chPlmn, &ptNwStat->hwLac, &ptNwStat->hwCellId);
            //nRet = ZYF_RIL_GetCgregStatus(&ptNwStat->chGprsReg, &ptNwStat->chPlmn, &ptNwStat->hwLac, &ptNwStat->hwCellId);
            if (ZYF_RET_OK == nRet && GPRS_REG_HOME_NW == ptNwStat->chGprsReg) {
                ZYF_LOG("get gprs status ok! => %d", ptNwStat->chGprsReg);
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_GET_CSQ_VALUE;
            } else {
                if (++ ptNwStat->chTimeOut >= 10) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                    ZYF_LOG("failed to get GPRS status!");
                }
            }
            break;
            
        case NW_GET_CSQ_VALUE:
            nRet = ZYF_GetCsqValue((uint8_t *)&ptNwStat->chSigLvl);
            //nRet = ZYF_RIL_GetCsqValue((uint8_t *)&ptNwStat->chSigLvl);
            if (ZYF_RET_OK == nRet && (ptNwStat->chSigLvl >= 6 && ptNwStat->chSigLvl <= 31)) {
                ZYF_LOG("get signal quality ok! => %d", ptNwStat->chSigLvl);
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_PDP_CFG;
            } else {
                if (++ ptNwStat->chTimeOut >= 10) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                    ZYF_LOG("failed to signal quality!");
                }
            }
            break;
            
        case NW_PDP_CFG: 
            if ((GPRS_PDP_SUCCESS == ZYF_GprsGetStatus(chCtxId, &nStatus)) && 
                (GPRS_ACTIVATED == nStatus)) {
                if (ptNwStat->bIsActived) {
                    if (++ ptNwStat->chTimeOut >= 600) {
                        ptNwStat->chTimeOut = 0;
                        ptNwStat->chState = NW_GET_GPRS_STATE;
                    } else {
                        break;
                    }
                } else {
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                }
                return NW_RET_PDP_ACTIVE;
            }

            ptNwStat->bIsActived = false;
        
            ZYF_GprsRegister(chCtxId, &s_tPdpCb);
            //ZYF_RIL_GprsRegister(chCtxId, &s_tPdpCb);
            memset((void *)&tPdpCtx, 0, sizeof(ZYF_PdpCfg_t));
            strcpy((void *)tPdpCtx.apnName, "CMNET");
            tPdpCtx.PdpType = GPRS_PDP_TYPE_IP;
            tPdpCtx.authtype = 1;
            nRet = ZYF_GprsConfig(chCtxId, &tPdpCtx);
            //nRet = ZYF_RIL_GprsConfig(chCtxId, &tPdpCtx);
            if (GPRS_PDP_SUCCESS == nRet) {
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_PDP_ACTIVE;
            } else {
                if (++ ptNwStat->chTimeOut >= 10) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                }
            }
            break;

        case NW_PDP_ACTIVE: 
            nRet = ZYF_GprsActivate(chCtxId);
            //nRet = ZYF_RIL_GprsActivate(chCtxId);
            if (GPRS_PDP_SUCCESS == nRet) {
                ptNwStat->chTimeOut = 0;
                ptNwStat->chState = NW_PDP_WAIT_ACTIVE;
            } else {
                if (++ ptNwStat->chTimeOut >= 120) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                }
            }
            break;
            
        case NW_PDP_WAIT_ACTIVE:
            nRet = ZYF_GprsGetStatus(chCtxId, &nStatus);
            if (GPRS_PDP_SUCCESS == nRet && GPRS_ACTIVATED == nStatus) {
                ptNwStat->bIsActived = true;
                nRet = ZYF_GprsGetLocalIPAddress(chCtxId, &ipAddr[0]);
                if (GPRS_PDP_SUCCESS == nRet) {
                    ptNwStat->chTimeOut = 0;
                    if (ptNwStat->bIsActived) {
                        ptNwStat->chState = NW_GET_GPRS_STATE;
                    } else {
                        ptNwStat->chState = NW_GET_SIMCARD_STATE;
                    }
                    return NW_RET_PDP_ACTIVE;
                }
            }  else {
                if (++ ptNwStat->chTimeOut >= 10) {
                    ptNwStat->chTimeOut = 0;
                    ptNwStat->chState = NW_GET_SIMCARD_STATE;
                }
            } 
            break;
    }
    
    return NW_RET_ON_GOING;
}

static void ZYF_NetworkActiveReq(uint8_t chCtxId, bool bAuto)
{
    ZYF_NwStat_t *ptNwStat = &s_tNwState;
    ZYF_PdpCfg_t tPdpCtx;
    
    if (bAuto) {
        ZYF_NetworkActiveAuto(ptNwStat, chCtxId);
    } else {
        ptNwStat->chState = NW_GET_SIMCARD_STATE;
        ptNwStat->chTimeOut = 0;
            
        ZYF_GprsRegister(chCtxId, &s_tPdpCb);
        memset((void *)&tPdpCtx, 0, sizeof(ZYF_PdpCfg_t));
        strcpy((void *)tPdpCtx.apnName, "CMNET");
        tPdpCtx.PdpType = GPRS_PDP_TYPE_IP;
        ZYF_GprsConfig(chCtxId, &tPdpCtx);
        ZYF_GprsActivate(chCtxId);
    }
}

static void ZYF_NetworkThread(void *pvParam)
{
    ZYF_LOG("thread enter!");
    
    while (1) {
        ZYF_NetworkActiveReq(1, true);
        ZYF_ThreadSleep(500);
    }
}

void ZYF_NetworkInit(void)
{
    ZYF_RIL_NetworkInit();
  
    memset((void *)&s_tNwState, 0, sizeof(ZYF_NwStat_t));
    s_tNwState.chState = NW_GET_SIMCARD_STATE;
    
    ZYF_ThreadCreate("ZYF_NetworkThread", ZYF_NetworkThread, NULL, ZYF_PRIORITY_NORMAL, 1024 * 16);
}

 

Guess you like

Origin blog.csdn.net/w_hizyf_m/article/details/107226598