阿里云物模型层初始化代码实现

1 代码实现

直接代码实现:

aliyun.h:

#ifndef __ALIYUN_H
#define __ALIYUN_H

#include <stdint.h>
#include <stdbool.h>

#define PORT        1883
#define SERVER		"iot-as-mqtt.cn-shanghai.aliyuncs.com"  //"a1gPAyS0jai.iot-as-mqtt.cn-shanghai.aliyuncs.com"
#define TOPIC_SUB	"thing/model/down_raw"                  //"/sys/a1gPAyS0jai/s0001/thing/model/down_raw"
#define TOPIC_PUB	"thing/model/up_raw"                    //"/sys/a1gPAyS0jai/s0001/thing/model/up_raw"

typedef void(*aliyun_recv_callback)(uint8_t type,uint32_t id,uint8_t *pdata,uint16_t dada_len);

bool aliyun_init(char *prokey,char *devname,char *devsecret);
bool aliyun_deinit(void);

bool aliyun_online(void);
bool aliyun_offline(void);
bool aliyun_send(uint8_t type,uint32_t id,uint8_t *pdata,uint16_t dada_len);
bool aliyun_recv_register(aliyun_recv_callback cb);

#endif


aliyun.c:

#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "aliyun.h"
#include "at_net.h"
#include "dev_sim7020.h"
#include "app_debug.h"

#include "FreeRTOS.h"
#include "task.h"


#define MAX_SERVER_LEN  80
#define MAX_TOPIC_LEN   80


static bool is_inited=false;
typedef enum
{
    ONLINE_STATE_CONNECT,
    ONLINE_STATE_ERR,
}online_state_e;

struct aliyun_control
{
    char *prokey;
    char *devname;
    char *devsecret;

    char server[MAX_SERVER_LEN];
    char topic_sub[MAX_TOPIC_LEN];
    char topic_pub[MAX_TOPIC_LEN];
}ali_ctl;


bool aliyun_init(char *prokey,char *devname,char *devsecret)
{
    DEBUG("aliyun init");
    if(prokey==NULL)return false;
    if(devname==NULL)return false;
    if(devsecret==NULL)return false;
    memset(&ali_ctl,0,sizeof(ali_ctl));
    ali_ctl.prokey=prokey;
    ali_ctl.devname=devname;
    ali_ctl.devsecret=devsecret;
    snprintf(ali_ctl.server,MAX_SERVER_LEN,"%s.%s",prokey,SERVER);
    snprintf(ali_ctl.topic_sub,MAX_TOPIC_LEN,"/sys/%s/%s/%s",prokey,devname,TOPIC_SUB);
    snprintf(ali_ctl.topic_pub,MAX_TOPIC_LEN,"/sys/%s/%s/%s",prokey,devname,TOPIC_PUB);

    do
    {
        if(at_net_dev_register(&dev_sim7020)==false)break;
        if(at_net_init()==false)break;
        is_inited=true;
        DEBUG("aliyun init sucess");    
        return true;
    } while (0);
    aliyun_deinit();
    return false;
}
bool aliyun_deinit(void)
{
    is_inited=false;
    at_net_dev_unregister();
    memset(&ali_ctl,0,sizeof(ali_ctl));
    return true;
}

bool aliyun_online(void)
{
    uint8_t state=ONLINE_STATE_CONNECT;
    uint8_t err=0;

    while (1)
    {
        switch (state)
        {
            case ONLINE_STATE_CONNECT:
                if(at_net_mqtt_connect(ali_ctl.server,PORT,ali_ctl.prokey,ali_ctl.devname,ali_ctl.devsecret)==false)
                    state= ONLINE_STATE_ERR;   
                if(at_net_mqtt_sub(ali_ctl.topic_sub)==false)
                    state= ONLINE_STATE_ERR;
                return true;
            case ONLINE_STATE_ERR:
                err++;
                if(err>3)return false;
                if(at_net_deinit()==false)break;
                if(at_net_init()==false)break;
                state=ONLINE_STATE_CONNECT;
                break;
            default:
                return false;
        }
        vTaskDelay(10);
    }
    return false;   
}
bool aliyun_offline(void)
{
    if(is_inited==false)return false;
    at_net_mqtt_disconnect();
    return true;
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页