15. M601 阿里云mqtt

1 alimqtt API

1.1 API函数

1.1.1 解析传入的阿里云三元组。

HAL_GetProductKey(DEMO_PRODUCT_KEY);

HAL_GetDeviceName(DEMO_DEVICE_NAME);

HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);

1.1.2 IOT_MQTT_Construct

此函数用来初始化部分信息结构体,并建立 MQTT 的连接。

·参数
pInitParams:MQTT client 端的结构体指针。

·返回值
NULL:构建失败
不是 NULL:MQTT client 的指针。

1.1.3 IOT_MQTT_Subscribe    

订阅指定的 MQTT topic。

·函数原型
int IOT_MQTT_Subscribe(void *handle, const char *topic_filter, iotx_mqtt_qos_t qos, iotx_mqtt_event_handle_func_fpt topic_handle_func, void *pcontext);
·参数
handle:IOT_MQTT_Construct()连接后的 MQTT client。
topic_filter:指定的 topic。
qos:MQTT 请求的 QOS 类型。
topic_handle_func:订阅 topic 用到的回调函数,用来处理该 topic 发过来的数据。
pcontext:指定上下文。 当调用“ topic_handle_func”时,它将被传回,一般为 NULL。
·返回值
@retval -1:订阅失败。
@retval> = 0:订阅成功。
该值是此请求的唯一 ID。当回调'iotx_mqtt_param_t:handle_event'时,该 ID 将被传回。

1.1.4 IOT_MQTT_Publish
发布数据到指定的 TOPIC。
·函数原型
int IOT_MQTT_Publish(void *handle, const char *topic_name, iotx_mqtt_topic_info_pt topic_msg);
·参数
handle:指定的 MQTT client
topic_name:指定的 topic 名字
topic_msg:指定的 topic 数据

·返回值
@retval -1:发布失败。
@retval 0:发布成功,QoS 为 0。
@retval> 0:发布成功,其中 QoS> = 0。
该值是此请求的唯一 ID。当回调'iotx_mqtt_param_t:handle_event'时,该 ID 将被传回。

2 阿里mqtt例程介绍

编译方法:.\examples\build\对应的.bat 文件双击执行或打开就可以编译。
生成文件:.\out\对应目录\hex\M601_example_**.pac

app_network.c 和 example_alimqtt.c:

#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);
}

#include "dev_sign_api.h"
#include "mqtt_api.h"
#include "mqtt_wrapper.h"
#include "zyf_app.h"
#include "zyf_trace.h"
#include "zyf_uart.h"
#include "zyf_thread.h"

#define __weak __attribute__((weak))


static Uart_Param_t g_uart1param;
static Uart_Param_t g_uart3param;



char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};

void mqtt_example(void *param);

void ZYF_IOTKIT_PdpActCallback(uint8_t status)
{
    if (APP_MSG_PDP_ACTIVE == status) {
        ZYF_ThreadCreate("mqtt_example", mqtt_example, NULL, ZYF_PRIORITY_NORMAL, 1024 * 5);
    } else {
        //fixme
    }
}

void ZYF_PdpActiveCnf(uint8_t contextId, int32_t errCode)
{
    ZYF_LOG("PDP: Active");

    ZYF_IOTKIT_PdpActCallback(APP_MSG_PDP_ACTIVE);
}

void ZYF_PdpDeactiveCnf(uint8_t contextId, int32_t errCode)
{
    ZYF_LOG("PDP: Deactive");
}
/*

#define ZYF_LOG(fmt, ...)  \
    do { \
        ZYF_LOG("[%s:%d]>"fmt, __func__, __LINE__, ##__VA_ARGS__); \
    } while(0)
*/

//#define ZYF_LOG ZYF_LOG
void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
    iotx_mqtt_topic_info_t     *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;
    char buf[128] = {0};

    switch (msg->event_type) {
        case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
            /* print topic name and topic message */
            ZYF_LOG("Message Arrived:");
            sprintf(buf, "Topic  : %.*s", topic_info->topic_len, topic_info->ptopic);
            ZYF_LOG("%s", buf);
            sprintf(buf, "Payload: %.*s", (int)topic_info->payload_len, topic_info->payload);
            ZYF_LOG("%s", buf);
            break;
        default:
            break;
    }
}

int example_subscribe(void *handle)
{
    int res = 0;
    const char *fmt = "/%s/%s/user/get";
    char *topic = NULL;
    int topic_len = 0;

    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        ZYF_LOG("memory not enough");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
    if (res < 0) {
        ZYF_LOG("subscribe failed");
        HAL_Free(topic);
        return -1;
    }
    ZYF_LOG("subscribe succeed");

    HAL_Free(topic);
    return 0;
}

int example_publish(void *handle)
{
    int             res = 0;
    const char     *fmt = "/%s/%s/user/update";
    char           *topic = NULL;
    int             topic_len = 0;
    char           *payload = "{\"message\":\"hello!\"}";

    topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
    topic = HAL_Malloc(topic_len);
    if (topic == NULL) {
        ZYF_LOG("memory not enough");
        return -1;
    }
    memset(topic, 0, topic_len);
    HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);

    //IOT_MQTT_Publish(void * handle, const char * topic_name, iotx_mqtt_topic_info_pt topic_msg)

    res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
    if (res < 0) {
        ZYF_LOG("publish failed, res = %d", res);
        HAL_Free(topic);
        return -1;
    }
    ZYF_LOG("publish OK %d", res);

    HAL_Free(topic);
    return 0;
}

void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
    uintptr_t packet_id = (uintptr_t)msg->msg;
    iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)msg->msg;

    switch (msg->event_type) {
        case IOTX_MQTT_EVENT_UNDEF:
            ZYF_LOG("undefined event occur.");
            break;

        case IOTX_MQTT_EVENT_DISCONNECT:
            ZYF_LOG("MQTT disconnect.");
            break;

        case IOTX_MQTT_EVENT_RECONNECT:
            ZYF_LOG("MQTT reconnect.");
            break;

        case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS:
            ZYF_LOG("subscribe success, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT:
            ZYF_LOG("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_SUBCRIBE_NACK:
            ZYF_LOG("subscribe nack, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS:
            ZYF_LOG("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
            ZYF_LOG("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_UNSUBCRIBE_NACK:
            ZYF_LOG("unsubscribe nack, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_PUBLISH_SUCCESS:
            ZYF_LOG("publish success, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_PUBLISH_TIMEOUT:
            ZYF_LOG("publish timeout, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_PUBLISH_NACK:
            ZYF_LOG("publish nack, packet-id=%u", (unsigned int)packet_id);
            break;

        case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
            ZYF_LOG("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
                          topic_info->topic_len,
                          topic_info->ptopic,
                          topic_info->payload_len,
                          topic_info->payload);
            break;

        case IOTX_MQTT_EVENT_BUFFER_OVERFLOW:
            ZYF_LOG("buffer overflow, %s", msg->msg);
            break;

        default:
            ZYF_LOG("Should NOT arrive here.");
            break;
    }
}


/*
 *  NOTE: About demo topic of /${productKey}/${deviceName}/user/get
 *
 *  The demo device has been configured in IoT console (https://iot.console.aliyun.com)
 *  so that its /${productKey}/${deviceName}/user/get can both be subscribed and published
 *
 *  We design this to completely demonstrate publish & subscribe process, in this way
 *  MQTT client can receive original packet sent by itself
 *
 *  For new devices created by yourself, pub/sub privilege also requires being granted
 *  to its /${productKey}/${deviceName}/user/get for successfully running whole example
 */
#if 0
int main(int argc, char *argv[])
#else 
void mqtt_example(void *param)
#endif
{
    void                   *pclient = NULL;
    int                     res = 0;
    int                     loop_cnt = 0;
    iotx_mqtt_param_t       mqtt_params;

    HAL_GetProductKey(DEMO_PRODUCT_KEY);
    HAL_GetDeviceName(DEMO_DEVICE_NAME);
    HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);

    ZYF_LOG("mqtt example");

    /* Initialize MQTT parameter */
    /*
     * Note:
     *
     * If you did NOT set value for members of mqtt_params, SDK will use their default values
     * If you wish to customize some parameter, just un-comment value assigning expressions below
     *
     **/
    memset(&mqtt_params, 0x0, sizeof(mqtt_params));

    /**
     *
     *  MQTT connect hostname string
     *
     *  MQTT server's hostname can be customized here
     *
     *  default value is ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com
     */
     
    mqtt_params.host = "a1rhfJArIBz.iot-as-mqtt.cn-shanghai.aliyuncs.com";

    /**
     *
     *  MQTT connect port number
     *
     *  TCP/TLS port which can be 443 or 1883 or 80 or etc, you can customize it here
     *
     *  default value is 1883 in TCP case, and 443 in TLS case
     */
    mqtt_params.port = 1883;

    /**
     *
     * MQTT request timeout interval
     *
     * MQTT message request timeout for waiting ACK in MQTT Protocol
     *
     * default value is 2000ms.
     */
    mqtt_params.request_timeout_ms = 2000;

    /**
     *
     * MQTT clean session flag
     *
     * If CleanSession is set to 0, the Server MUST resume communications with the Client based on state from
     * the current Session (as identified by the Client identifier).
     *
     * If CleanSession is set to 1, the Client and Server MUST discard any previous Session and Start a new one.
     *
     * default value is 0.
     */
    mqtt_params.clean_session = 0;

    /**
     *
     * MQTT keepAlive interval
     *
     * KeepAlive is the maximum time interval that is permitted to elapse between the point at which
     * the Client finishes transmitting one Control Packet and the point it starts sending the next.
     *
     * default value is 60000.
     */
    mqtt_params.keepalive_interval_ms = 60000;

    /**
     *
     * MQTT write buffer size
     *
     * Write buffer is allocated to place upstream MQTT messages, MQTT client will be limitted
     * to send packet no longer than this to Cloud
     *
     * default value is 1024.
     *
     */
    mqtt_params.write_buf_size = 1024;

    /**
     *
     * MQTT read buffer size
     *
     * Write buffer is allocated to place downstream MQTT messages, MQTT client will be limitted
     * to recv packet no longer than this from Cloud
     *
     * default value is 1024.
     *
     */
    mqtt_params.read_buf_size = 1024;

    /**
     *
     * MQTT event callback function
     *
     * Event callback function will be called by SDK when it want to notify user what is happening inside itself
     *
     * default value is NULL, which means PUB/SUB event won't be exposed.
     *
     */
    mqtt_params.handle_event.h_fp = example_event_handle;
    ZYF_LOG("before MQTT construct ");
    pclient = IOT_MQTT_Construct(&mqtt_params);
    if (NULL == pclient) {
        ZYF_LOG("MQTT construct failed");
        return ;
    }
    ZYF_LOG("MQTT construct succeed");

    res = example_subscribe(pclient);
    if (res < 0) {
    ZYF_LOG("MQTT example_subscribe failed");

        IOT_MQTT_Destroy(&pclient);
        return ;
    }
    ZYF_LOG("MQTT example_subscribe succeed");
    
//    Nordic_sleep_func();
    setmcu2poweroffM601(180);
    ZYF_SleepEnable();                      

    while (1) {

        if (0 == loop_cnt % 30) {
            ZYF_LOG("before example_publish:");
            example_publish(pclient);
        }
        ZYF_LOG("loop_cnt:%d",loop_cnt);
        IOT_MQTT_Yield(pclient, 500);

        ZYF_ThreadSleep(1000);

        loop_cnt += 1;
    }
}

void UartWriteCallBack(void* Param) // general com
{
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 
    if(Param == NULL)
    {
        return;
    }    

    ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n"));
    ZYF_UartWriteCallbackSwitch(uartparam->port,false);

}

void __weak UartReadCallBack(void* Param) // 
{
    uint32_t recvlen = 0;
    Uart_Param_t *uartparam = (Uart_Param_t *)Param; 

    /*
    UART_PORT1 = 0,
    UART_PORT2 = 1,
    UART_PORT3 = 2,
    */
    ZYF_LOG("Uart%d recv",uartparam->port);

    while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1))
    {
        ZYF_LOG("recv :%02x",uartparam->uartbuf[recvlen]);
        recvlen++;
    }
    ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen);
    ZYF_UartWriteCallbackSwitch(uartparam->port,true);
}


static void _AppUart1Init(void)
{
    int32_t ret;
    g_uart1param.port = UART_PORT1;
    ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param);
    ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param);
    ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE);

    ZYF_LOG("_AppUart1Init");
    return;
}


static void _AppUart3Init(void)
{   
    g_uart3param.port = UART_PORT3;
    ZYF_UartRegister(UART_PORT3, UartReadCallBack,&g_uart3param);
    ZYF_UartWriteCbRegister(UART_PORT3,UartWriteCallBack,&g_uart3param);
    ZYF_UartOpen(UART_PORT3, 115200, ZYF_FC_NONE);
    ZYF_LOG("_AppUart3Init");

    return;
}



void AppUartInit(void)
{
    _AppUart1Init();
    _AppUart3Init();
}



void Alimqtt_Example(void * Param)
{
    ZYF_MsgQ_t *ptMsg;
    ZYF_AppMsg_t tMsg;
    int iRet = -1;
    ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t));
    ZYF_LOG("thread enter!");

    ZYF_NetworkInit();
    while (1) {
        ZYF_LOG("in while.");
        iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg);
        if (iRet < 0) {
            ZYF_LOG("Failed to get msg");
            ZYF_ThreadSleep(1000);
        }
    }

}

static void prvInvokeGlobalCtors(void)
{
    extern void (*__init_array_start[])();
    extern void (*__init_array_end[])();

    size_t count = __init_array_end - __init_array_start;
    for (size_t i = 0; i < count; ++i)
        __init_array_start[i]();
}


int appimg_enter(void *param)
{
    AppUartInit();
    ZYF_LOG("##SW:ZYF_1.0.0,\r\n");
    ZYF_LOG("application image enter, param 0x%x", param);

    prvInvokeGlobalCtors();

    ZYF_ThreadCreate("Alimqtt_Example", Alimqtt_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024);
    return 0;
}

void appimg_exit(void)
{
    OSI_LOGI(0, "application image exit");
}


猜你喜欢

转载自blog.csdn.net/w_hizyf_m/article/details/108357711