692 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			692 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | 
						|
 | 
						|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | 
						|
be copied by any method or incorporated into another program without
 | 
						|
the express written consent of Aerospace C.Power. This Information or any portion
 | 
						|
thereof remains the property of Aerospace C.Power. The Information contained herein
 | 
						|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | 
						|
liability for its use in any way and conveys no license or title under
 | 
						|
any patent or copyright and makes no representation or warranty that this
 | 
						|
Information is free from patent or copyright infringement.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
/* os_ship header files */
 | 
						|
#include "os_task_api.h"
 | 
						|
#include "os_event_api.h"
 | 
						|
#include "os_timer_api.h"
 | 
						|
#include "os_utils_api.h"
 | 
						|
 | 
						|
/* iot common header files */
 | 
						|
#include "iot_plc_cco_api.h"
 | 
						|
#include "iot_module_api.h"
 | 
						|
#include "iot_queue_api.h"
 | 
						|
#include "iot_mem_pool_api.h"
 | 
						|
#include "iot_config_api.h"
 | 
						|
#include "iot_app_api.h"
 | 
						|
#include "iot_errno_api.h"
 | 
						|
#include "iot_io_api.h"
 | 
						|
#include "iot_dbglog_api.h"
 | 
						|
#include "iot_uart_api.h"
 | 
						|
#include "iot_task_api.h"
 | 
						|
#include "iot_oem_api.h"
 | 
						|
#include "iot_board_api.h"
 | 
						|
 | 
						|
#include "iot_grapp.h"
 | 
						|
#include "iot_plctxrx.h"
 | 
						|
#include "iot_proto_common.h"
 | 
						|
#include "iot_gr_upgrade.h"
 | 
						|
#include "iot_plc_led_api.h"
 | 
						|
 | 
						|
#if (IOT_LWIP_SUPPORT)
 | 
						|
#include "ge_socket_server.h"
 | 
						|
#include "ge_socket_client.h"
 | 
						|
#include "lwmqtt_main.h"
 | 
						|
#endif
 | 
						|
 | 
						|
#if (IOT_LED_CTRL_APP_ENABLE)
 | 
						|
#include "iot_light_ctrl_drv.h"
 | 
						|
#endif
 | 
						|
 | 
						|
#if IOT_GR_APP_ENABLE
 | 
						|
 | 
						|
#if (IOT_LWIP_SUPPORT && HW_PLATFORM != HW_PLATFORM_SIMU)
 | 
						|
extern void iot_lwip_init();
 | 
						|
#else
 | 
						|
#define iot_lwip_init()
 | 
						|
#define gs_server_init()
 | 
						|
#define gs_server_register_recv(recv)                   0
 | 
						|
#define gs_server_send(ptr, length)                     0
 | 
						|
#define lwmqtt_lwip_init()
 | 
						|
#define lwmqtt_message_received_from_ge(p_pkt_frame)    0
 | 
						|
#endif
 | 
						|
 | 
						|
extern void iot_print_config(bool_t enable);
 | 
						|
 | 
						|
extern void iot_cus_print_config(bool_t enable);
 | 
						|
 | 
						|
static void iot_grapp_uart_meter_port_func(uint8_t* buffer,
 | 
						|
    uint32_t buffer_len, bool_t is_full_frame,uint32_t invalid_data_len);
 | 
						|
 | 
						|
static char grapp_log_buf[IOT_GRAPP_LOG_BUF_LEN] = { 0 };
 | 
						|
 | 
						|
/* global gree app variable */
 | 
						|
greeplc_app_t *greeapp = NULL;
 | 
						|
/* uart configure */
 | 
						|
static grapp_uart_cfg_t gbl_grapp_uart_cfg =
 | 
						|
{
 | 
						|
    0,                                     // port
 | 
						|
    IOT_UART_BANDRATE_DEFAULT,             // baud
 | 
						|
    DEF_UART_PARITY,                       // parity
 | 
						|
    IOT_UART_DLEN_8_BITS,                  // data
 | 
						|
    IOT_UART_STOP_1_BITS,                  // stop
 | 
						|
    iot_grapp_uart_meter_port_func,        // pointer of callback when recv data
 | 
						|
    UART_RECV_SIZE_MAX                     // buffer size
 | 
						|
};
 | 
						|
 | 
						|
void iot_grapp_data_print(const char* str, uint8_t* buf, uint32_t len)
 | 
						|
{
 | 
						|
    uint32_t offset = 0;
 | 
						|
    offset = iot_sprintf(grapp_log_buf, "%s[len:%d]", str, len);
 | 
						|
    for (uint32_t i = 0; i < len; ++i) {
 | 
						|
        offset += iot_sprintf(grapp_log_buf + offset, "%02X ", buf[i]);
 | 
						|
        if (IOT_GRAPP_LOG_BUF_LEN <= offset + 4) {
 | 
						|
            break;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    grapp_log_buf[offset] = 0;
 | 
						|
    iot_cus_printf("%s\n", grapp_log_buf);
 | 
						|
}
 | 
						|
 | 
						|
void iot_grapp_modify_uart_param(uint8_t baud_idx, uint8_t parity,
 | 
						|
    uint8_t data_bit, uint8_t stop_bit)
 | 
						|
{
 | 
						|
    uint32_t uart_baud_rate[] = PROTO_GE_UART_BAUD_VALUE;
 | 
						|
 | 
						|
    if (baud_idx >= PROTO_GE_UART_BAUD_IDX_MAX) {
 | 
						|
        baud_idx = 0;
 | 
						|
    }
 | 
						|
    if (iot_proto_uart_param_check(uart_baud_rate[baud_idx], parity, data_bit,
 | 
						|
        stop_bit) == true) {
 | 
						|
        gbl_grapp_uart_cfg.baud = uart_baud_rate[baud_idx];
 | 
						|
        gbl_grapp_uart_cfg.parity = parity;
 | 
						|
        gbl_grapp_uart_cfg.data = data_bit;
 | 
						|
        gbl_grapp_uart_cfg.stop = stop_bit;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
static iot_uart_h iot_grapp_open_uart(void)
 | 
						|
{
 | 
						|
    iot_uart_h uart = 0;
 | 
						|
    grapp_uart_cfg_t* uart_cfg = &gbl_grapp_uart_cfg;
 | 
						|
 | 
						|
    uart_cfg->port = iot_board_get_uart(UART_CUS_PORT_0);
 | 
						|
 | 
						|
    if (uart_cfg->port >= iot_uart_get_max_port_num()) {
 | 
						|
        iot_cus_printf("[ERR] %s Invalid UART#%d !!", __FUNCTION__,
 | 
						|
            uart_cfg->port);
 | 
						|
        return NULL;
 | 
						|
    }
 | 
						|
 | 
						|
    uart = iot_uart_open(uart_cfg->port,uart_cfg->rec_ptr,
 | 
						|
        uart_cfg->recbuf_sz, NULL);
 | 
						|
    return uart;
 | 
						|
}
 | 
						|
 | 
						|
static void iot_grapp_close_uart(void)
 | 
						|
{
 | 
						|
    iot_uart_close(greeapp->uart_com);
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
bool_t iot_grapp_set_uart(iot_uart_h uart, grapp_uart_cfg_t *cfg)
 | 
						|
{
 | 
						|
    bool_t ret = false;
 | 
						|
    grapp_uart_cfg_t* uart_cfg = cfg;
 | 
						|
 | 
						|
    iot_cus_printf("set uart:%d [%d, %d, %d, %d]\n", uart_cfg->port,
 | 
						|
        uart_cfg->baud, uart_cfg->parity, uart_cfg->data, uart_cfg->stop);
 | 
						|
    ret = iot_uart_set_config(uart, uart_cfg->baud, uart_cfg->parity,
 | 
						|
        uart_cfg->data, uart_cfg->stop);
 | 
						|
 | 
						|
    iot_uart_set_threshold(uart,  UART_THR_RXTIMEOUT,
 | 
						|
        IOT_UART_DEFAULT_THDVALUE);
 | 
						|
    iot_uart_set_threshold(uart, UART_THR_NO_FMT_TIMEOUT,
 | 
						|
        IOT_UART_DEFAULT_THDVALUE);
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
bool_t iot_grapp_set_uart_config(iot_uart_h uart)
 | 
						|
{
 | 
						|
    return iot_grapp_set_uart(uart, &gbl_grapp_uart_cfg);
 | 
						|
}
 | 
						|
 | 
						|
/* uart recv callback function for uart task receiving data from uart 0 */
 | 
						|
static void iot_grapp_uart_meter_port_func(uint8_t* buffer,
 | 
						|
    uint32_t buffer_len, bool_t is_full_frame,uint32_t invalid_data_len)
 | 
						|
{
 | 
						|
    (void)invalid_data_len;
 | 
						|
    (void)is_full_frame ;
 | 
						|
 | 
						|
    if(0 == buffer_len){
 | 
						|
        return ;
 | 
						|
    }
 | 
						|
    iot_plc_led_request(IOT_PLC_LED_REQ_PLC_485_RX);
 | 
						|
    iot_grapp_data_print("uart recv", buffer, buffer_len);
 | 
						|
    /* distinguish AT or MCU mode */
 | 
						|
    if (GR_MCU_OP_MODE == greeapp->uart_mode) {
 | 
						|
        if (greeapp->uart_dispatch_fntable[GR_MCU_OP_MODE]) {
 | 
						|
            greeapp->uart_dispatch_fntable[GR_MCU_OP_MODE](buffer,
 | 
						|
                (uint16_t)buffer_len);
 | 
						|
        }
 | 
						|
    } else if (GR_AT_OP_MODE == greeapp->uart_mode) {
 | 
						|
        if (greeapp->uart_dispatch_fntable[GR_AT_OP_MODE]) {
 | 
						|
            greeapp->uart_dispatch_fntable[GR_AT_OP_MODE](buffer,
 | 
						|
                (uint16_t)buffer_len);
 | 
						|
        }
 | 
						|
    } else if (GR_DEV_TEST_OP_MODE == greeapp->uart_mode) {
 | 
						|
        if (greeapp->uart_dispatch_fntable[GR_DEV_TEST_OP_MODE]) {
 | 
						|
            greeapp->uart_dispatch_fntable[GR_DEV_TEST_OP_MODE](buffer,
 | 
						|
                (uint16_t)buffer_len);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
uint32_t iot_grapp_sendto_mainboard_fn(iot_pkt_t *data)
 | 
						|
{
 | 
						|
    uint8_t ret = ERR_FAIL;
 | 
						|
 | 
						|
    if (iot_hwver_is_ledc_v3_0_jy() &&
 | 
						|
        (iot_board_get_board_id() == CUS_BOARD_ID_LEDC_V3_0) &&
 | 
						|
        (IOT_PLC_DEV_ROLE_STA == prototask_contxt.local_dev.nw_role)) {
 | 
						|
        if (greeapp->uart_com == NULL) {
 | 
						|
            iot_proto_mainboard_data_pend(data);
 | 
						|
            proto_post_data_to_edge_msg_handle(EDGE_INPUT_REQ_STOPID_ID,
 | 
						|
                NULL, 0);
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        iot_edge_delay_start();
 | 
						|
    }
 | 
						|
    iot_plc_led_request(IOT_PLC_LED_REQ_PLC_485_TX);
 | 
						|
    ret = iot_uart_send(greeapp->uart_com, data, NULL);
 | 
						|
 | 
						|
out:
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
#if (SUPPORT_HOST_PORT_ETH)
 | 
						|
/* eth recv callback function for eth task receiving data from eth */
 | 
						|
int8_t iot_grapp_eth_meter_port_func(int8_t *buffer, uint16_t buffer_len)
 | 
						|
{
 | 
						|
    if ((NULL == buffer) || (0 == buffer_len)) {
 | 
						|
        return ERR_INVAL;
 | 
						|
    }
 | 
						|
 | 
						|
    iot_grapp_data_print("eth recv", (uint8_t *)buffer, buffer_len);
 | 
						|
    /* distinguish AT or MCU mode */
 | 
						|
    if (GR_MCU_OP_MODE == greeapp->uart_mode) {
 | 
						|
        if (greeapp->eth_dispatch_fntable[GR_MCU_OP_MODE]) {
 | 
						|
            greeapp->eth_dispatch_fntable[GR_MCU_OP_MODE]((uint8_t *)buffer,
 | 
						|
                buffer_len);
 | 
						|
        }
 | 
						|
    } else if (GR_AT_OP_MODE == greeapp->uart_mode) {
 | 
						|
        if (greeapp->eth_dispatch_fntable[GR_AT_OP_MODE]) {
 | 
						|
            greeapp->eth_dispatch_fntable[GR_AT_OP_MODE]((uint8_t *)buffer,
 | 
						|
                buffer_len);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t iot_grapp_sendto_mainboard_eth_fn(iot_pkt_t *data)
 | 
						|
{
 | 
						|
    if (NULL == data) {
 | 
						|
        return ERR_INVAL;
 | 
						|
    }
 | 
						|
 | 
						|
    int32_t ret = -1;
 | 
						|
    if ((ret = gs_server_send((void *)iot_pkt_data(data),
 | 
						|
         (uint16_t)iot_pkt_data_len(data))) <= 0) {
 | 
						|
        return ERR_FAIL;
 | 
						|
    } else {
 | 
						|
        return ERR_OK;
 | 
						|
    }
 | 
						|
}
 | 
						|
#else
 | 
						|
int8_t iot_grapp_eth_meter_port_func(int8_t *buffer, uint16_t buffer_len)
 | 
						|
{
 | 
						|
    (void)buffer;
 | 
						|
    (void)buffer_len;
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
uint32_t iot_grapp_sendto_mainboard_eth_fn(iot_pkt_t *data)
 | 
						|
{
 | 
						|
    (void)data;
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
#endif /* end SUPPORT_HOST_PORT_ETH */
 | 
						|
 | 
						|
/* get uart mode info from oem base config */
 | 
						|
#if (IOT_PSRAM_ENABLE)
 | 
						|
uint8_t iot_get_oem_uart_mode(void)
 | 
						|
{
 | 
						|
    iot_oem_base_cfg_t *oem_base_cfg;
 | 
						|
    iot_oem_get_base_cfg(&oem_base_cfg);
 | 
						|
 | 
						|
    iot_cus_printf("[grapp]get iotapp_mode=%d\n", oem_base_cfg->iotapp_mode);
 | 
						|
 | 
						|
    if (oem_base_cfg->iotapp_mode >= GR_MCU_OP_MODE &&
 | 
						|
        oem_base_cfg->iotapp_mode <= GR_AT_OP_MODE) {
 | 
						|
        return oem_base_cfg->iotapp_mode;
 | 
						|
    } else {
 | 
						|
        return GR_MCU_OP_MODE;
 | 
						|
    }
 | 
						|
}
 | 
						|
#else
 | 
						|
uint8_t iot_get_oem_uart_mode(void)
 | 
						|
{
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
    return GR_AT_OP_MODE;
 | 
						|
#else
 | 
						|
    return GR_MCU_OP_MODE;
 | 
						|
#endif
 | 
						|
}
 | 
						|
#endif /*end IOT_PSRAM_ENABLE */
 | 
						|
 | 
						|
/* get host port info from oem base config */
 | 
						|
uint8_t iot_get_oem_host_port(void)
 | 
						|
{
 | 
						|
#if (SUPPORT_HOST_PORT_ETH)
 | 
						|
    iot_oem_base_cfg_t *oem_base_cfg;
 | 
						|
    iot_oem_get_base_cfg(&oem_base_cfg);
 | 
						|
 | 
						|
    iot_cus_printf("[grapp]get host_port=%d\n", oem_base_cfg->host_port);
 | 
						|
    if (oem_base_cfg->host_port >= HOST_PORT_UART &&
 | 
						|
        oem_base_cfg->host_port <= HOST_PORT_ETH) {
 | 
						|
        return oem_base_cfg->host_port;
 | 
						|
    } else {
 | 
						|
        return HOST_PORT_UART;
 | 
						|
    }
 | 
						|
#else
 | 
						|
    return HOST_PORT_UART;
 | 
						|
#endif /* end SUPPORT_HOST_PORT_ETH */
 | 
						|
}
 | 
						|
 | 
						|
#if (IOT_GE_INCLUDE_LWMQTT)
 | 
						|
uint32_t iot_grapp_lwmqtt_message_from_ge(iot_pkt_t *p_pkt)
 | 
						|
{
 | 
						|
    if (NULL == p_pkt) {
 | 
						|
        return ERR_FAIL;
 | 
						|
    }
 | 
						|
 | 
						|
    return lwmqtt_message_received_from_ge(p_pkt);
 | 
						|
}
 | 
						|
 | 
						|
uint8_t iot_grapp_lwmqtt_message_to_ge(uint8_t *mq_str, uint16_t str_len)
 | 
						|
{
 | 
						|
    /* TODO : GE only for now, AT not implamented. */
 | 
						|
    if (greeapp->mqtt_dispatch_fntable[GR_MCU_OP_MODE]) {
 | 
						|
        greeapp->mqtt_dispatch_fntable[GR_MCU_OP_MODE](mq_str, str_len);
 | 
						|
    }
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
#endif /* end IOT_GE_INCLUDE_LWMQTT */
 | 
						|
 | 
						|
#if IOT_GE_EXT_SDK_ENABLE
 | 
						|
uint8_t iot_cus_task_message_to_ge(uint8_t *mq_str, uint16_t str_len) {
 | 
						|
    /* TODO : GE only for now, AT not implamented. */
 | 
						|
    if (greeapp->app_dispatch_fntable[GR_MCU_OP_MODE]) {
 | 
						|
        greeapp->app_dispatch_fntable[GR_MCU_OP_MODE](mq_str, str_len);
 | 
						|
    }
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
 | 
						|
bool_t iot_grapp_init_success()
 | 
						|
{
 | 
						|
    if (NULL == greeapp) {
 | 
						|
        return false;
 | 
						|
    } else {
 | 
						|
        return greeapp->grapp_init_success;
 | 
						|
    }
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
#if (IOT_LED_CTRL_APP_ENABLE && PLC_SUPPORT_STA_ROLE)
 | 
						|
 | 
						|
#if HW_PLATFORM != HW_PLATFORM_SIMU
 | 
						|
uint32_t iot_grapp_ledc_app_message_from_ge(iot_pkt_t * p_pkt) {
 | 
						|
 | 
						|
    if (NULL == p_pkt) {
 | 
						|
        return ERR_FAIL;
 | 
						|
    }
 | 
						|
 | 
						|
    return lwmqtt_message_received_from_ge(p_pkt);
 | 
						|
}
 | 
						|
 | 
						|
uint8_t iot_grapp_ledc_app_message_to_ge(uint8_t *mq_str, uint16_t str_len) {
 | 
						|
    /* TODO : GE only for now, AT not implamented. */
 | 
						|
    if (greeapp->app_dispatch_fntable[GR_MCU_OP_MODE]) {
 | 
						|
        greeapp->app_dispatch_fntable[GR_MCU_OP_MODE](mq_str, str_len);
 | 
						|
    }
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
#else
 | 
						|
 | 
						|
#define iot_grapp_ledc_app_message_to_ge(mq_str, len)     0
 | 
						|
 | 
						|
uint32_t iot_grapp_ledc_app_message_from_ge(iot_pkt_t * p_pkt)
 | 
						|
{
 | 
						|
    (void)p_pkt;
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
 | 
						|
#endif /* end  HW_PLATFORM != HW_PLATFORM_SIMU */
 | 
						|
#endif /* end (IOT_LED_CTRL_APP_ENABLE && PLC_SUPPORT_STA_ROLE) */
 | 
						|
 | 
						|
static uint32_t iot_gr_app_init(void)
 | 
						|
{
 | 
						|
    uint32_t ret = ERR_OK;
 | 
						|
    iot_plc_app_t app;
 | 
						|
 | 
						|
    app.app_id  = IOT_GR_APP_ID;
 | 
						|
    app.param   = NULL;
 | 
						|
    app.prio    = APP_GR_PRIORITY;
 | 
						|
    /* msg from cvg layer callback */
 | 
						|
    app.recv    = iot_plctxrx_msdu_from_cvg;
 | 
						|
 | 
						|
    /* alloc memory first. */
 | 
						|
    greeapp = os_mem_malloc(IOT_GREE_APP_MID, sizeof(*greeapp));
 | 
						|
 | 
						|
    if (!greeapp) {
 | 
						|
        ret = ERR_NOMEM;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
 | 
						|
    os_mem_set(greeapp, 0x0, sizeof(*greeapp));
 | 
						|
 | 
						|
#if (IOT_LED_CTRL_APP_ENABLE) // LEDC APP
 | 
						|
    /* Fix opcode */
 | 
						|
    greeapp->uart_mode = GR_MCU_OP_MODE;
 | 
						|
#if PLC_SUPPORT_CCO_ROLE
 | 
						|
    /* TODO : CCO need CUSTOM_TASK yet. */
 | 
						|
    greeapp->host_port = HOST_PORT_UART;
 | 
						|
#else
 | 
						|
    greeapp->host_port = HOST_PORT_CUSTOM_TASK;
 | 
						|
#endif
 | 
						|
 | 
						|
#else
 | 
						|
#if IOT_GE_EXT_SDK_ENABLE || IOT_MICRO_CCTT_TASK_ENABLE
 | 
						|
    /* get uart mode from oem info */
 | 
						|
    greeapp->uart_mode = GR_MCU_OP_MODE;
 | 
						|
    /* get host port info */
 | 
						|
    greeapp->host_port = HOST_PORT_CUSTOM_TASK;
 | 
						|
#elif IOT_GE_CKQ_MODE_ENABLE
 | 
						|
    /* init uart mode info */
 | 
						|
    greeapp->uart_mode = GR_DEV_TEST_OP_MODE;
 | 
						|
    /* init host port info */
 | 
						|
    greeapp->host_port = HOST_PORT_UART;
 | 
						|
#else
 | 
						|
    /* get uart mode from oem info */
 | 
						|
    greeapp->uart_mode = iot_get_oem_uart_mode();
 | 
						|
    greeapp->host_port = iot_get_oem_host_port();
 | 
						|
#endif
 | 
						|
#endif /* end IOT_LED_CTRL_APP_ENABLE */
 | 
						|
 | 
						|
    /* 1.initialize proto layer thread */
 | 
						|
    if (greeapp->uart_mode == GR_MCU_OP_MODE) {
 | 
						|
        ret = iot_proto_task_init();
 | 
						|
    }
 | 
						|
 | 
						|
    if (ret)
 | 
						|
        goto error_0;
 | 
						|
 | 
						|
    /* 2.initialize plctxrx layer thread */
 | 
						|
    ret = iot_plctxrx_task_init();
 | 
						|
    if (ret)
 | 
						|
        goto error_1;
 | 
						|
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
    /* 3.initialize at layer thread */
 | 
						|
    if (greeapp->uart_mode == GR_AT_OP_MODE) {
 | 
						|
        greeapp->at_task = iot_at_task_init();
 | 
						|
        if (NULL == greeapp->at_task) {
 | 
						|
            goto error_2;
 | 
						|
        }
 | 
						|
        greeapp->at_context = iot_at_context_get();
 | 
						|
    }
 | 
						|
#endif
 | 
						|
#if IOT_GE_CKQ_MODE_ENABLE
 | 
						|
    /* 4.initialize dev test layer thread */
 | 
						|
    greeapp->dev_test_task = iot_dev_test_task_init();
 | 
						|
    if (NULL == greeapp->dev_test_task) {
 | 
						|
        goto error_3;
 | 
						|
    }
 | 
						|
    greeapp->devtest_context = iot_dev_test_context_get();
 | 
						|
#endif
 | 
						|
    /* register callback function each other
 | 
						|
       iot_proto_plc_data_recv_func -- receive data from txrx layer
 | 
						|
       iot_proto_plctxrx_cmd_resp_recv_func --receive cmdrsp/ind from txrx layer
 | 
						|
       iot_proto_update_passwd update local passwd to proto layer
 | 
						|
    */
 | 
						|
    iot_plctxrx_proto_register(iot_proto_plc_data_recv_func,
 | 
						|
        iot_proto_plctxrx_cmd_resp_recv_func, iot_proto_update_vendr);
 | 
						|
 | 
						|
    /* plctxrx_cmd_send_mssage -- send txrx cmd api for proto layer.
 | 
						|
       gree_send_cbk send txrx data -- send txrx data api for proto layer
 | 
						|
    */
 | 
						|
    iot_proto_plctxrx_register(plctxrx_cmd_send_mssage,
 | 
						|
        iot_plctxrx_proto_data_send_cbk);
 | 
						|
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
    /* plctxrx provided function. registed to at. */
 | 
						|
    iot_at_command_fn_register_send_to_plc(plctxrx_cmd_send_mssage);
 | 
						|
#endif
 | 
						|
 | 
						|
    /* at provided function, registed to plctxrx. receive command response */
 | 
						|
    iot_plctxrx_at_register(iot_at_command_plc_send_response_to_at);
 | 
						|
 | 
						|
#if IOT_GE_CKQ_MODE_ENABLE
 | 
						|
    /* plctxrx provided function. registed to dev test. */
 | 
						|
    iot_dev_test_fn_register_send_to_plc(plctxrx_cmd_send_mssage,
 | 
						|
        iot_plctxrx_proto_data_send_cbk);
 | 
						|
 | 
						|
    /* dev test provided function, registed to plctxrx.
 | 
						|
       iot_dev_test_plc_data_recv_func -- receive data from txrx layer
 | 
						|
       iot_dev_test_plctxrx_cmd_resp_func -- receive cmdrsp/ind from txrx layer
 | 
						|
    */
 | 
						|
    iot_plctxrx_dev_test_register(iot_dev_test_plc_data_recv_func,
 | 
						|
        iot_dev_test_plctxrx_cmd_resp_func);
 | 
						|
#endif
 | 
						|
 | 
						|
    /* distinguish UART or ETH host port */
 | 
						|
    if (greeapp->host_port == HOST_PORT_UART) {
 | 
						|
        /* hook api for put strings to communicate uart port (COM0) */
 | 
						|
        iot_grapp_reg_fn_receive_from_ge(HOST_PORT_UART,
 | 
						|
            iot_grapp_sendto_mainboard_fn);
 | 
						|
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
        iot_at_command_fn_register_response_to_uart(
 | 
						|
            iot_grapp_sendto_mainboard_fn);
 | 
						|
#endif
 | 
						|
 | 
						|
#if IOT_GE_CKQ_MODE_ENABLE
 | 
						|
        iot_dev_test_fn_register_send_to_uart(iot_grapp_sendto_mainboard_fn);
 | 
						|
#endif
 | 
						|
 | 
						|
        /* open and config uart */
 | 
						|
        greeapp->uart_com = iot_grapp_open_uart();
 | 
						|
        if (!greeapp->uart_com) {
 | 
						|
            iot_cus_printf("[grapp][err]uart open fail.\n");
 | 
						|
            ret = ERR_FAIL;
 | 
						|
            goto error_4;
 | 
						|
        } else {
 | 
						|
            /* set rs485 mode for JY ledc3.0 */
 | 
						|
            if ((CUS_BOARD_ID_LEDC_V3_0 == iot_board_get_board_id()) &&
 | 
						|
                iot_hwver_is_ledc_v3_0_jy()) {
 | 
						|
                iot_uart_enable_rs485(greeapp->uart_com,
 | 
						|
                    iot_board_get_gpio(GPIO_RS485_TXE));
 | 
						|
                iot_cus_printf("set rs485 mode\n");
 | 
						|
            }
 | 
						|
            iot_cus_printf("[grapp]uart open OK.\n");
 | 
						|
        }
 | 
						|
 | 
						|
        if (iot_grapp_set_uart_config(greeapp->uart_com)) {
 | 
						|
            iot_cus_printf("[grapp]uart config OK.\n");
 | 
						|
        } else {
 | 
						|
            iot_cus_printf("[grapp][err]uart config fail.\n");
 | 
						|
            ret = ERR_FAIL;
 | 
						|
            goto error_5;
 | 
						|
        }
 | 
						|
 | 
						|
        /* register callback function offered
 | 
						|
         * by proto layer for uart data recv
 | 
						|
         */
 | 
						|
        greeapp->uart_dispatch_fntable[GR_MCU_OP_MODE] = \
 | 
						|
            iot_grapp_get_fn_send_to_ge(HOST_PORT_UART);
 | 
						|
 | 
						|
        /* register callback function offered
 | 
						|
         * by proto layer for uart data recv
 | 
						|
         */
 | 
						|
        greeapp->uart_dispatch_fntable[GR_AT_OP_MODE] = \
 | 
						|
            iot_at_command_proto_send_to_at;
 | 
						|
 | 
						|
#if IOT_GE_CKQ_MODE_ENABLE
 | 
						|
        /* register callback function offered
 | 
						|
         * by dev test for uart data recv
 | 
						|
         */
 | 
						|
        greeapp->uart_dispatch_fntable[GR_DEV_TEST_OP_MODE] = \
 | 
						|
            iot_dev_test_uart_meter_port_func;
 | 
						|
#endif
 | 
						|
    } else if (greeapp->host_port == HOST_PORT_ETH) {
 | 
						|
        /* init tcpip stack */
 | 
						|
        iot_lwip_init();
 | 
						|
        /* open and config eth */
 | 
						|
        gs_server_init();
 | 
						|
        /* hook api for put strings to communicate eth */
 | 
						|
        iot_grapp_reg_fn_receive_from_ge(HOST_PORT_ETH,
 | 
						|
            iot_grapp_sendto_mainboard_eth_fn);
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
        iot_at_command_fn_register_response_to_uart(
 | 
						|
            iot_grapp_sendto_mainboard_eth_fn);
 | 
						|
#endif
 | 
						|
        /*
 | 
						|
         * register eth recv callback function for eth task receiving
 | 
						|
         * data from eth
 | 
						|
         */
 | 
						|
        if (-1 == gs_server_register_recv(iot_grapp_eth_meter_port_func)) {
 | 
						|
            goto error_5;
 | 
						|
        }
 | 
						|
        /* register callback function offered
 | 
						|
         * by proto layer for eth data recv
 | 
						|
         */
 | 
						|
        greeapp->eth_dispatch_fntable[GR_MCU_OP_MODE] = \
 | 
						|
            iot_grapp_get_fn_send_to_ge(HOST_PORT_ETH);
 | 
						|
        /* register callback function offered
 | 
						|
         * by proto layer for eth data recv
 | 
						|
         */
 | 
						|
        greeapp->eth_dispatch_fntable[GR_AT_OP_MODE] = \
 | 
						|
            iot_at_command_proto_send_to_at;
 | 
						|
    } else if (greeapp->host_port == HOST_PORT_MQTT) {
 | 
						|
#if (IOT_GE_INCLUDE_LWMQTT)
 | 
						|
        iot_lwip_init();
 | 
						|
 | 
						|
        lwmqtt_lwip_init();
 | 
						|
 | 
						|
        iot_grapp_reg_fn_receive_from_ge(HOST_PORT_MQTT,
 | 
						|
            iot_grapp_lwmqtt_message_from_ge);
 | 
						|
 | 
						|
        lwmqtt_register_fn_message_to_ge((void *)iot_grapp_lwmqtt_message_to_ge);
 | 
						|
 | 
						|
        greeapp->mqtt_dispatch_fntable[GR_MCU_OP_MODE] = \
 | 
						|
            iot_grapp_get_fn_send_to_ge(HOST_PORT_MQTT);
 | 
						|
#endif /* end IOT_GE_INCLUDE_LWMQTT */
 | 
						|
    } else if (greeapp->host_port == HOST_PORT_CUSTOM_TASK) {
 | 
						|
#if (IOT_LED_CTRL_APP_ENABLE && PLC_SUPPORT_STA_ROLE)
 | 
						|
        iot_led_ctrl_task_init();
 | 
						|
        iot_led_ctrl_register_get_data_fn(iot_grapp_ledc_app_message_to_ge);
 | 
						|
        iot_grapp_reg_fn_receive_from_ge(HOST_PORT_CUSTOM_TASK,
 | 
						|
            iot_led_crtl_msg_post_to_led);
 | 
						|
        greeapp->app_dispatch_fntable[GR_MCU_OP_MODE] =
 | 
						|
            iot_grapp_get_fn_send_to_ge(HOST_PORT_CUSTOM_TASK);
 | 
						|
#else
 | 
						|
        greeapp->app_dispatch_fntable[GR_MCU_OP_MODE] =
 | 
						|
            iot_grapp_get_fn_send_to_ge(HOST_PORT_CUSTOM_TASK);
 | 
						|
#endif /* end (IOT_LED_CTRL_APP_ENABLE && PLC_SUPPORT_STA_ROLE) */
 | 
						|
    } else {
 | 
						|
        IOT_ASSERT(0);
 | 
						|
        /* Do nothing. */
 | 
						|
    }
 | 
						|
 | 
						|
    /* register app to cvg layer */
 | 
						|
    greeapp->app_handle = iot_plc_register_app(&app);
 | 
						|
    if (greeapp->app_handle == NULL) {
 | 
						|
        ret = ERR_FAIL;
 | 
						|
        goto error_5;
 | 
						|
    }
 | 
						|
 | 
						|
#if PLC_SUPPORT_CCO_ROLE && ENABLE_GREE_UPGRADE
 | 
						|
    /* begin of online upgrade initialize gree upgrade info */
 | 
						|
    iot_proto_upgrade_initialize();
 | 
						|
#endif
 | 
						|
    /* grapp init successful */
 | 
						|
    greeapp->grapp_init_success = true;
 | 
						|
    goto out;
 | 
						|
 | 
						|
error_5:
 | 
						|
    /* distinguish UART or ETH host port */
 | 
						|
    if (greeapp->host_port == HOST_PORT_UART) {
 | 
						|
        iot_grapp_close_uart();
 | 
						|
    } else {
 | 
						|
        goto error_4;
 | 
						|
    }
 | 
						|
error_4:
 | 
						|
#if IOT_GE_CKQ_MODE_ENABLE
 | 
						|
    iot_dev_test_task_deinit();
 | 
						|
error_3:
 | 
						|
#endif
 | 
						|
#if INCLUDE_AT_COMMAND_MODULE
 | 
						|
    iot_at_task_deinit();
 | 
						|
#else
 | 
						|
    goto error_2; /* Clear building error. */
 | 
						|
#endif
 | 
						|
error_2:
 | 
						|
    iot_plctxrx_task_deinit();
 | 
						|
error_1:
 | 
						|
    iot_proto_task_deinit();
 | 
						|
error_0:
 | 
						|
    os_mem_free(greeapp);
 | 
						|
 | 
						|
out:
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
 * app_entry: entry for ping app
 | 
						|
 * @return:
 | 
						|
 *  ERR_PENDING -   if application want to delay the plc network formation.
 | 
						|
 *  otherwise   -   plc network formation will be started automatically.
 | 
						|
 */
 | 
						|
uint32_t app_gr_app_entry(void)
 | 
						|
{
 | 
						|
    uint32_t ret = ERR_PENDING;
 | 
						|
 | 
						|
    /* enable cvg blower layer log */
 | 
						|
    iot_print_config(1);
 | 
						|
    /* enable app log */
 | 
						|
    iot_cus_print_config(1);
 | 
						|
 | 
						|
    if (iot_gr_app_init()) {
 | 
						|
        ret = ERR_OK;
 | 
						|
    }
 | 
						|
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
#endif
 | 
						|
 |