328 lines
9.4 KiB
C
328 lines
9.4 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.
|
|
|
|
****************************************************************************/
|
|
#include <string.h>
|
|
|
|
/* os_ship header files */
|
|
#include "os_lock_api.h"
|
|
#include "os_timer_api.h"
|
|
|
|
#include "lwip/netif.h"
|
|
#include "lwip/sockets.h"
|
|
#include "netif/netif_eth.h"
|
|
|
|
#include "iot_rtc_api.h"
|
|
#include "iot_pkt_api.h"
|
|
#include "iot_plc_api.h"
|
|
#include "iot_gpio_api.h"
|
|
#include "iot_task_api.h"
|
|
#include "iot_utils_api.h"
|
|
#include "iot_errno_api.h"
|
|
#include "iot_cctt_comm_module.h"
|
|
#include "iot_cctt_config.h"
|
|
#include "iot_plc_msg_api.h"
|
|
#include "iot_uart_api.h"
|
|
#include "iot_htm_ext_api.h"
|
|
#include "iot_rtc_api.h"
|
|
#include "iot_cctt_utils.h"
|
|
#include "iot_rtc_ext_api.h"
|
|
#include "iot_cctt_prio_channel.h"
|
|
#include "iot_cctt_app_at_manager.h"
|
|
#include "iot_cctt_app_monitor.h"
|
|
#include "iot_cctt_app_cli_agent.h"
|
|
|
|
#if IOT_APP_SELECTION == IOT_APP_DEF_22_GE_MICRO_CCTT
|
|
#include "iot_cctt_app_iot.h"
|
|
#endif
|
|
|
|
#if IOT_APP_SELECTION == IOT_APP_DEF_25_AT_MICRO_CCTT
|
|
#include "iot_cctt_app_at.h"
|
|
#endif
|
|
|
|
#if IOT_MICRO_CCTT_TASK_ENABLE
|
|
|
|
#define IOT_CCTT_TASK_ID (IOT_CUST_TASK_MID)
|
|
|
|
#define CCTT_TSK_PRINT(fmt, args...) \
|
|
iot_cus_printf("[cctt task]%s:" fmt "\n", __FUNCTION__, ##args)
|
|
|
|
|
|
/**
|
|
* dectect whether enter to factory test mode
|
|
* 1. config ip address to 192.168.1.2
|
|
* 2. try to connect with 192.168.1.1:5000
|
|
* 3. enter factory test mode if success, normal boot otherwise
|
|
*
|
|
* @param none
|
|
* @return ERR_OK if ftm mode is needed, ERR_FAIL otherwise
|
|
*/
|
|
uint8_t factory_test_mode_detect(void)
|
|
{
|
|
uint8_t ret = ERR_FAIL;
|
|
|
|
uint8_t eth_mac[IOT_MAC_ADDR_LEN];
|
|
uint8_t ip[] = {192, 168, 1, 2};
|
|
uint8_t netmask[] = {255, 255, 255, 0};
|
|
uint8_t gateway[] = {192, 168, 1, 1};
|
|
int32_t socket = -1, socket_option;
|
|
const char *svr_addr_str = "192.168.1.1";
|
|
uint16_t svr_tcp_port = 5000;
|
|
struct sockaddr_in sock_addr;
|
|
socklen_t sock_len = sizeof(sock_addr);
|
|
int error = 0;
|
|
socklen_t err_len = sizeof(error);
|
|
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
|
|
fd_set rset, wset;
|
|
int n;
|
|
uint8_t retry_cnt = 3;
|
|
|
|
CCTT_TSK_PRINT("start to detect factory test mode!");
|
|
|
|
/* set mac address and ip address */
|
|
iot_cctt_utils_get_eth_mac(eth_mac);
|
|
netif_eth_set_mac_addr(eth_mac);
|
|
netif_eth_set_ip_addr(ip, netmask, gateway);
|
|
|
|
retry:
|
|
/* create socket */
|
|
socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
|
|
if (-1 == socket) {
|
|
CCTT_TSK_PRINT("create socket failed!");
|
|
goto out;
|
|
}
|
|
|
|
/* set socket to non-block */
|
|
socket_option = lwip_fcntl(socket, F_GETFL, 0);
|
|
if(ERR_OK != lwip_fcntl(socket, F_SETFL, socket_option | O_NONBLOCK)) {
|
|
CCTT_TSK_PRINT("set socket nonblock failed!");
|
|
goto out;
|
|
}
|
|
|
|
/* try to connect with remote factory test server */
|
|
sock_addr.sin_family = AF_INET;
|
|
sock_addr.sin_port = htons(svr_tcp_port);
|
|
sock_addr.sin_addr.s_addr = inet_addr(svr_addr_str);
|
|
if (0 == lwip_connect(socket, (struct sockaddr *)&sock_addr, sock_len)) {
|
|
ret = ERR_OK;
|
|
CCTT_TSK_PRINT("connect ftm detect server success!");
|
|
goto out;
|
|
} else {
|
|
lwip_getsockopt(socket, SOL_SOCKET, SO_ERROR, (void*)&error, &err_len);
|
|
if ((0 != error) && (EINPROGRESS != error)) {
|
|
CCTT_TSK_PRINT("connect ftm detect server failed!error=%d", error);
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
/* wait connect success or timeout or error */
|
|
FD_ZERO(&rset);
|
|
FD_ZERO(&wset);
|
|
FD_SET(socket, &rset);
|
|
FD_SET(socket, &wset);
|
|
n = lwip_select(socket+1, &rset, &wset, NULL, &tv);
|
|
if ((1 == n) && FD_ISSET(socket, &wset)) {
|
|
CCTT_TSK_PRINT("connect ftm detect server success!!");
|
|
ret = ERR_OK;
|
|
goto out;
|
|
} else {
|
|
CCTT_TSK_PRINT("connect ftm detect server failed!n=%d", n);
|
|
}
|
|
|
|
out:
|
|
if (-1 != socket) {
|
|
lwip_close(socket);
|
|
}
|
|
if ((ret != ERR_OK) && (retry_cnt--)) {
|
|
CCTT_TSK_PRINT("frm detect svr fail,retry it,remain cnt=%u", retry_cnt);
|
|
os_delay(1000);
|
|
goto retry;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief: initialise ethernet lwip ip parameter
|
|
* need to execute before creating lwip communication object
|
|
*
|
|
* @param none
|
|
* @return ERR_OK if success, ERR_FAIL otherwise
|
|
*/
|
|
uint8_t iot_cctt_lwip_dev_init(void)
|
|
{
|
|
uint8_t ret = ERR_FAIL;
|
|
lwip_eth_cfg_t lwip_dev_cfg = {0};
|
|
|
|
iot_cctt_cfg_get_eth_ip_config(lwip_dev_cfg.eth_ip,
|
|
lwip_dev_cfg.eth_netmask, lwip_dev_cfg.eth_gateway);
|
|
iot_cctt_utils_get_eth_mac(lwip_dev_cfg.eth_mac_addr);
|
|
CCTT_TSK_PRINT("ethernet MAC Address %02X:%02X:%02X:%02X:%02X:%02X",
|
|
lwip_dev_cfg.eth_mac_addr[0], lwip_dev_cfg.eth_mac_addr[1],
|
|
lwip_dev_cfg.eth_mac_addr[2], lwip_dev_cfg.eth_mac_addr[3],
|
|
lwip_dev_cfg.eth_mac_addr[4], lwip_dev_cfg.eth_mac_addr[5]);
|
|
if (0 == comm_lwip_dev_init(&lwip_dev_cfg)) {
|
|
ret = ERR_OK;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief: initialise 4G device parameter
|
|
* need to execute before creating lwi4g lte communication object
|
|
*
|
|
* @param none
|
|
* @return ERR_OK if success, ERR_FAIL otherwise
|
|
*/
|
|
uint8_t iot_cctt_4g_dev_init(void)
|
|
{
|
|
uint8_t ret = ERR_FAIL;
|
|
uint8_t rst_pin;
|
|
lte_dev_t lte_dev_cfg = {0};
|
|
|
|
rst_pin = iot_cctt_utils_get_gpio(IOT_CCTT_GPIO_ID_4G_RST);
|
|
|
|
lte_dev_cfg.uart_port = UART_CCTT_4G_PORT;
|
|
lte_dev_cfg.power_pin = rst_pin;
|
|
|
|
iot_cctt_cfg_get_4g_config((uint8_t *)lte_dev_cfg.apn,
|
|
(uint8_t *)lte_dev_cfg.user, (uint8_t *)lte_dev_cfg.password);
|
|
if (0 == comm_lte_dev_init(<e_dev_cfg)) {
|
|
ret = ERR_OK;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief: initialise gpio module, and initialise led state
|
|
*
|
|
* @param none
|
|
* @return ERR_OK if success, ERR_FAIL otherwise
|
|
*/
|
|
uint8_t iot_cctt_gpio_init(void)
|
|
{
|
|
uint8_t gpio_num;
|
|
|
|
iot_gpio_module_init();
|
|
|
|
/* eth reset output high */
|
|
gpio_num = iot_cctt_utils_get_gpio(IOT_CCTT_GPIO_ID_ETH_RST);
|
|
iot_gpio_open_as_output(gpio_num);
|
|
iot_gpio_value_set(gpio_num, 1);
|
|
iot_gpio_close(gpio_num);
|
|
|
|
/* turn off upgrade LED */
|
|
gpio_num = iot_cctt_utils_get_gpio(IOT_CCTT_GPIO_ID_UPGD_LED);
|
|
iot_gpio_open_as_output(gpio_num);
|
|
iot_gpio_value_set(gpio_num, 1);
|
|
iot_gpio_close(gpio_num);
|
|
|
|
/* turn off uplink LED */
|
|
gpio_num = iot_cctt_utils_get_gpio(IOT_CCTT_GPIO_ID_UPLINK_LED);
|
|
iot_gpio_open_as_output(gpio_num);
|
|
iot_gpio_value_set(gpio_num, 1);
|
|
iot_gpio_close(gpio_num);
|
|
|
|
/* feed external watch dog once here */
|
|
gpio_num = iot_cctt_utils_get_gpio(IOT_CCTT_GPIO_ID_EXT_WDI);
|
|
iot_gpio_open_as_output(gpio_num);
|
|
iot_gpio_value_set(gpio_num, 1);
|
|
os_delay(100);
|
|
iot_gpio_value_set(gpio_num, 0);
|
|
iot_gpio_close(gpio_num);
|
|
|
|
return ERR_OK;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief: task entry of cctt boot, the task will terminate self after booting
|
|
* the initialization needs to execute in task environment,
|
|
* reason: fatory test mode detection need lwip support,
|
|
* so this task and lwip task must execute concurrently,
|
|
* execute in task environment of app_cus_task_entry
|
|
* cannot meet above requirement
|
|
*
|
|
* @param arg: unused
|
|
* @return none
|
|
*/
|
|
void iot_cctt_task_entry(void *arg)
|
|
{
|
|
iot_time_tm_t tm = {0};
|
|
|
|
iot_cctt_gpio_init();
|
|
iot_rtc_get(&tm);
|
|
iot_htm_ext_init();
|
|
iot_cctt_config_init();
|
|
comm_mdl_initialize();
|
|
iot_cctt_prio_chl_init();
|
|
|
|
if (ERR_OK == factory_test_mode_detect()) {
|
|
CCTT_TSK_PRINT("enter factory test mode!!");
|
|
iot_cctt_cfg_set_to_ftm_cfg();
|
|
} else {
|
|
CCTT_TSK_PRINT("micro cctt normal boot!!");
|
|
}
|
|
|
|
/* initialize communication devices */
|
|
iot_cctt_lwip_dev_init();
|
|
iot_cctt_4g_dev_init();
|
|
|
|
#if IOT_APP_SELECTION == IOT_APP_DEF_22_GE_MICRO_CCTT
|
|
CCTT_TSK_PRINT("boot application cctt iot");
|
|
iot_cctt_app_iot_boot();
|
|
#endif
|
|
|
|
#if IOT_APP_SELECTION == IOT_APP_DEF_25_AT_MICRO_CCTT
|
|
CCTT_TSK_PRINT("boot application cctt at");
|
|
iot_cctt_app_at_boot();
|
|
#endif
|
|
|
|
/* boot cli agent application here */
|
|
CCTT_TSK_PRINT("boot application cli agent");
|
|
iot_cctt_app_cli_agent_boot();
|
|
|
|
/* boot monitor application here */
|
|
CCTT_TSK_PRINT("boot application monitor");
|
|
iot_cctt_app_mtr_boot();
|
|
|
|
/* boot manager application here */
|
|
CCTT_TSK_PRINT("boot application manager");
|
|
iot_cctt_app_mgr_boot();
|
|
|
|
CCTT_TSK_PRINT("micro cctt task done!! delete self");
|
|
os_delete_task(os_get_current_task_handle());
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief: micro cctt application task entry
|
|
*
|
|
* @param none
|
|
* @return ERR_OK if success, ERR_FAIL otherwise
|
|
*/
|
|
uint32_t app_micro_cctt_task_entry()
|
|
{
|
|
uint32_t ret = ERR_OK;
|
|
|
|
os_create_task(iot_cctt_task_entry, NULL, OS_TASK_PRIO_HIGHEST);
|
|
|
|
return ret;
|
|
}
|
|
|
|
#endif /* end of IOT_MICRO_CCTT_TASK_ENABLE */
|