forked from xuos/xiuos
				
			add BC28 of nbiot for adapter
This commit is contained in:
		
							parent
							
								
									2aa5c0f0ba
								
							
						
					
					
						commit
						a4f24a29fc
					
				| 
						 | 
				
			
			@ -52,8 +52,15 @@ typedef struct AdapterProductInfo *AdapterProductInfoType;
 | 
			
		|||
 | 
			
		||||
struct Socket
 | 
			
		||||
{
 | 
			
		||||
    int id;
 | 
			
		||||
    struct Adapter *adapter;
 | 
			
		||||
    uint8_t type;                ///< socket type:DGRAM->UDP,STREAM->TCP
 | 
			
		||||
    uint8_t protocal;            ///< udp or tcp
 | 
			
		||||
    unsigned short listen_port;  ///< 0-65535
 | 
			
		||||
    uint8_t socket_id;           ///< socket id
 | 
			
		||||
    uint8_t recv_control;        ///< receive control
 | 
			
		||||
    uint8_t af_type;               ///< IPv4 or IPv6
 | 
			
		||||
    char * src_ip_addr;          ///< source P address  
 | 
			
		||||
    char * dst_ip_addr;          ///< destination IP address
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum AdapterType
 | 
			
		||||
| 
						 | 
				
			
			@ -156,7 +163,7 @@ struct Adapter
 | 
			
		|||
    struct AdapterProductInfo *info;
 | 
			
		||||
    ATAgentType agent;
 | 
			
		||||
 | 
			
		||||
    //struct Socket *socket;
 | 
			
		||||
    struct Socket socket;
 | 
			
		||||
 | 
			
		||||
    enum NetProtocolType net_protocol;
 | 
			
		||||
    enum NetRoleType net_role;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
config ADAPTER_NBIOT_BC28
 | 
			
		||||
        string "BC28 adapter name"
 | 
			
		||||
        default "bc28"
 | 
			
		||||
 | 
			
		||||
if ADD_XIUOS_FETURES
 | 
			
		||||
        config ADAPTER_BC28_PWRKEY
 | 
			
		||||
                int "BC28 PWRKEY pin number"
 | 
			
		||||
                default "100"
 | 
			
		||||
 | 
			
		||||
        config ADAPTER_BC28_PIN_DRIVER
 | 
			
		||||
                string "BC28 device pin driver path"
 | 
			
		||||
                default "/dev/pin_dev"
 | 
			
		||||
 | 
			
		||||
        config ADAPTER_BC28_DRIVER_EXTUART
 | 
			
		||||
                bool "Using extra uart to support nbiot"
 | 
			
		||||
                default n
 | 
			
		||||
 | 
			
		||||
        config ADAPTER_BC28_DRIVER
 | 
			
		||||
                string "BC28 device uart driver path"
 | 
			
		||||
                default "/dev/uart2_dev2"
 | 
			
		||||
                depends on !ADAPTER_BC28_DRIVER_EXTUART
 | 
			
		||||
 | 
			
		||||
        if ADAPTER_BC28_DRIVER_EXTUART                    
 | 
			
		||||
                config ADAPTER_BC28_DRIVER
 | 
			
		||||
                        string "BC28 device extra uart driver path"
 | 
			
		||||
                        default "/dev/extuart_dev0"
 | 
			
		||||
 | 
			
		||||
                config ADAPTER_BC28_DRIVER_EXT_PORT
 | 
			
		||||
                        int "if BC28 device using extuart, choose port"
 | 
			
		||||
                        default "0"
 | 
			
		||||
        endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
if ADD_NUTTX_FETURES
 | 
			
		||||
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
if ADD_RTTHREAD_FETURES
 | 
			
		||||
 | 
			
		||||
endif
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
SRC_FILES := bc28.c
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,512 @@
 | 
			
		|||
/*
 | 
			
		||||
* Copyright (c) 2020 AIIT XUOS Lab
 | 
			
		||||
* XiUOS is licensed under Mulan PSL v2.
 | 
			
		||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
 | 
			
		||||
* You may obtain a copy of Mulan PSL v2 at:
 | 
			
		||||
*        http://license.coscl.org.cn/MulanPSL2
 | 
			
		||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 | 
			
		||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 | 
			
		||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 | 
			
		||||
* See the Mulan PSL v2 for more details.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @file bc28.c
 | 
			
		||||
 * @brief Implement the connection nbiot adapter function, using BC28 device
 | 
			
		||||
 * @version 1.1
 | 
			
		||||
 * @author AIIT XUOS Lab
 | 
			
		||||
 * @date 2021.09.15
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <adapter.h>
 | 
			
		||||
#include <at_agent.h>
 | 
			
		||||
#include "../adapter_nbiot.h"
 | 
			
		||||
 | 
			
		||||
#define SOCKET_PROTOCOL_TCP  (6)
 | 
			
		||||
#define SOCKET_PROTOCOL_UDP  (17)
 | 
			
		||||
 | 
			
		||||
#define NET_TYPE_AF_INET   (0)
 | 
			
		||||
#define NET_TYPE_AF_INET6  (1)
 | 
			
		||||
 | 
			
		||||
#define SOCKET_INVALID_ID  (-1)
 | 
			
		||||
 | 
			
		||||
static int BC28UartOpen(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    if (NULL == adapter) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Open device in read-write mode */
 | 
			
		||||
    adapter->fd = PrivOpen(ADAPTER_BC28_DRIVER,O_RDWR);
 | 
			
		||||
    if (adapter->fd < 0) {
 | 
			
		||||
        printf("BC28UartOpen get serial %s fd error\n", ADAPTER_BC28_DRIVER);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    /* set serial config, serial_baud_rate = 9600 */
 | 
			
		||||
 | 
			
		||||
    struct SerialDataCfg cfg;
 | 
			
		||||
    memset(&cfg, 0 ,sizeof(struct SerialDataCfg));
 | 
			
		||||
 | 
			
		||||
    cfg.serial_baud_rate = BAUD_RATE_9600;
 | 
			
		||||
    cfg.serial_data_bits = DATA_BITS_8;
 | 
			
		||||
    cfg.serial_stop_bits = STOP_BITS_1;
 | 
			
		||||
    cfg.serial_parity_mode = PARITY_NONE;
 | 
			
		||||
    cfg.serial_bit_order = BIT_ORDER_LSB;
 | 
			
		||||
    cfg.serial_invert_mode = NRZ_NORMAL;
 | 
			
		||||
    cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
 | 
			
		||||
 | 
			
		||||
    /*aiit board use ch438, so it needs more serial configuration*/
 | 
			
		||||
#ifdef ADAPTER_BC28_DRIVER_EXTUART
 | 
			
		||||
    cfg.ext_uart_no         = ADAPTER_BC28_DRIVER_EXT_PORT;
 | 
			
		||||
    cfg.port_configure      = PORT_CFG_INIT;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &cfg;
 | 
			
		||||
 | 
			
		||||
    PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
 | 
			
		||||
    PrivTaskDelay(1000);
 | 
			
		||||
 | 
			
		||||
    printf("NBIot uart config ready\n");
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void BC28PowerSet(void)
 | 
			
		||||
{
 | 
			
		||||
    int pin_fd;
 | 
			
		||||
    pin_fd = PrivOpen(ADAPTER_BC28_PIN_DRIVER, O_RDWR);
 | 
			
		||||
 | 
			
		||||
    struct PinParam pin_param;
 | 
			
		||||
    pin_param.cmd = GPIO_CONFIG_MODE;
 | 
			
		||||
    pin_param.mode = GPIO_CFG_OUTPUT; 
 | 
			
		||||
    pin_param.pin = ADAPTER_BC28_PWRKEY;
 | 
			
		||||
 | 
			
		||||
    struct PrivIoctlCfg ioctl_cfg;
 | 
			
		||||
    ioctl_cfg.ioctl_driver_type = PIN_TYPE;
 | 
			
		||||
    ioctl_cfg.args = &pin_param;
 | 
			
		||||
    PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
 | 
			
		||||
 | 
			
		||||
    struct PinStat pin_stat;
 | 
			
		||||
    pin_stat.pin = ADAPTER_BC28_PWRKEY;
 | 
			
		||||
    pin_stat.val = GPIO_HIGH;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    PrivTaskDelay(200);//at least 200ms
 | 
			
		||||
 | 
			
		||||
    pin_stat.val = GPIO_LOW;
 | 
			
		||||
    PrivWrite(pin_fd, &pin_stat, 1);
 | 
			
		||||
 | 
			
		||||
    PrivClose(pin_fd);
 | 
			
		||||
}
 | 
			
		||||
/**
 | 
			
		||||
 * @description: NBIoT device create a socket connection
 | 
			
		||||
 * @param adapter - NBIoT adapter AT
 | 
			
		||||
 * @param socket_fd - socket file description
 | 
			
		||||
 * @param type - socket type
 | 
			
		||||
 * @param af_type - IPv4 or IPv6
 | 
			
		||||
 * @return success: EOK, failure: -ERROR
 | 
			
		||||
 */
 | 
			
		||||
int NBIoTSocketCreate(struct Adapter *adapter, struct Socket *socket )
 | 
			
		||||
{
 | 
			
		||||
    int32 result = 0;
 | 
			
		||||
 | 
			
		||||
    if (!adapter || !socket){
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ATReplyType reply = CreateATReply(64);
 | 
			
		||||
    if (NULL == reply) {
 | 
			
		||||
        printf("at create failed ! \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ( socket->af_type == NET_TYPE_AF_INET6 ) {
 | 
			
		||||
        printf("IPv6 not surport !\n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *str_af_type = "AF_INET";
 | 
			
		||||
    char *str_type;
 | 
			
		||||
    char str_fd[3] = {0};
 | 
			
		||||
    char *str_protocol ;
 | 
			
		||||
    char at_cmd[64] = {0};
 | 
			
		||||
    char listen_port[] = {0};
 | 
			
		||||
 | 
			
		||||
    if (socket->socket_id >= 0 && socket->socket_id < 7) {
 | 
			
		||||
        itoa(socket->socket_id, str_fd, 10);
 | 
			
		||||
        adapter->socket.socket_id = socket->socket_id;
 | 
			
		||||
    } else {
 | 
			
		||||
        printf("surport max 0-6, socket_id = [%d] is error!\n",socket->socket_id);
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if( socket->listen_port >= 0 && socket->listen_port <= 65535){
 | 
			
		||||
        itoa(socket->listen_port, listen_port, 10);
 | 
			
		||||
    } 
 | 
			
		||||
    
 | 
			
		||||
    adapter->socket.af_type = NET_TYPE_AF_INET;
 | 
			
		||||
 | 
			
		||||
    if (socket->type == SOCKET_TYPE_STREAM) {   //tcp = AT+NSOCR=STREAM,6,0,1,AF_INET
 | 
			
		||||
        adapter->socket.protocal = SOCKET_PROTOCOL_TCP;
 | 
			
		||||
        adapter->socket.type = SOCKET_TYPE_STREAM;
 | 
			
		||||
        str_type = "STREAM";
 | 
			
		||||
        char *str_protocol = "6";
 | 
			
		||||
 | 
			
		||||
    } else if ( socket->type == SOCKET_TYPE_DGRAM ){  //udp
 | 
			
		||||
        adapter->socket.type = SOCKET_TYPE_DGRAM;
 | 
			
		||||
        adapter->socket.protocal = SOCKET_PROTOCOL_UDP;
 | 
			
		||||
        str_type = "DGRAM";
 | 
			
		||||
        char *str_protocol = "17";
 | 
			
		||||
 | 
			
		||||
    } else {
 | 
			
		||||
        printf("error socket type \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    memcpy(at_cmd, "AT+NSOCR=", 9);
 | 
			
		||||
    strcat(at_cmd, str_type);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, str_protocol);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, listen_port);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, str_fd);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, str_af_type);
 | 
			
		||||
    strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
    printf("cmd : %s\n", at_cmd);
 | 
			
		||||
    ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
    PrivTaskDelay(3000);
 | 
			
		||||
    printf("bak : ");
 | 
			
		||||
    for(int i = 0; i < strlen(reply->reply_buffer); i++)
 | 
			
		||||
        printf(" 0x%02x", reply->reply_buffer[i]);
 | 
			
		||||
    printf("\n");
 | 
			
		||||
 | 
			
		||||
__exit:
 | 
			
		||||
    if (reply) {
 | 
			
		||||
        DeleteATReply(reply);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @description: NBIoT device close a socket connection
 | 
			
		||||
 * @param adapter - NBIoT adapter AT
 | 
			
		||||
 * @param socket_fd - socket file description
 | 
			
		||||
 * @return success: EOK, failure: -ERROR
 | 
			
		||||
 */
 | 
			
		||||
int NBIoTSocketDelete(struct Adapter *adapter )
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
 | 
			
		||||
    if (!adapter){
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ATReplyType reply = CreateATReply(64);
 | 
			
		||||
    if (NULL == reply) {
 | 
			
		||||
        printf("at create failed ! \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (adapter->socket.socket_id >= 7) {
 | 
			
		||||
        printf("socket fd error \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char str_fd[2] = {0};
 | 
			
		||||
    char at_cmd[16] = {0};
 | 
			
		||||
    itoa(adapter->socket.socket_id, str_fd, 10);
 | 
			
		||||
 | 
			
		||||
    memcpy(at_cmd, "AT+NSOCL=", 9);
 | 
			
		||||
    strcat(at_cmd, str_fd);
 | 
			
		||||
    strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
    printf("cmd : %s\n", at_cmd);
 | 
			
		||||
    ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
    PrivTaskDelay(300);
 | 
			
		||||
 | 
			
		||||
    adapter->socket.socket_id = SOCKET_INVALID_ID;
 | 
			
		||||
 | 
			
		||||
__exit:
 | 
			
		||||
    if (reply) {
 | 
			
		||||
        DeleteATReply(reply);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Open(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    struct Socket create_socket;
 | 
			
		||||
 | 
			
		||||
    if (NULL == adapter) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    /*step1: open BC8 serial port*/
 | 
			
		||||
    ret = BC28UartOpen(adapter);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        printf("bc18 setup failed.\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
     /*step2: init AT agent*/
 | 
			
		||||
    if (!adapter->agent) {
 | 
			
		||||
        char *agent_name = "niot_device";
 | 
			
		||||
        if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
 | 
			
		||||
            printf("at agent init failed !\n");
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        ATAgentType at_agent = GetATAgent(agent_name);
 | 
			
		||||
        adapter->agent = at_agent;
 | 
			
		||||
    }
 | 
			
		||||
    create_socket.type = SOCKET_TYPE_STREAM;
 | 
			
		||||
    create_socket.listen_port = 0;
 | 
			
		||||
    create_socket.socket_id = 0;
 | 
			
		||||
    create_socket.af_type = NET_TYPE_AF_INET;
 | 
			
		||||
 | 
			
		||||
    /*step3: create a tcp socket default */
 | 
			
		||||
    ret = NBIoTSocketCreate(adapter, &create_socket);
 | 
			
		||||
    if(ret < 0){
 | 
			
		||||
        printf("NBIot create tcp socket failed.\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("NBiot BC28 open successful\n");
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Close(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    NBIoTSocketDelete(adapter);
 | 
			
		||||
    PrivClose(adapter->fd);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Ioctl(struct Adapter *adapter, int cmd, void *args)
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    switch (cmd)
 | 
			
		||||
    {
 | 
			
		||||
        case CONFIG_NBIOT_RESET: /* reset nbiot */
 | 
			
		||||
            BC28PowerSet();
 | 
			
		||||
            break;
 | 
			
		||||
        case CONFIG_NBIOT_CREATE_SOCKET: /* create tcp/UDP socket */
 | 
			
		||||
            if(!args){
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
            struct Socket *create_socket = ( struct Socket *) args;
 | 
			
		||||
            ret = NBIoTSocketCreate(adapter, create_socket);
 | 
			
		||||
            break;
 | 
			
		||||
        case CONFIG_NBIOT_DELETE_SOCKET: /* close socket */
 | 
			
		||||
            ret = NBIoTSocketDelete(adapter);
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            ret = -1;
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
 | 
			
		||||
{
 | 
			
		||||
    int result = 0;
 | 
			
		||||
 | 
			
		||||
    ATReplyType reply = CreateATReply(64);
 | 
			
		||||
    if (NULL == reply) {
 | 
			
		||||
        printf("at create failed ! \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (adapter->socket.socket_id > 6) {
 | 
			
		||||
        printf("socket fd error \n");
 | 
			
		||||
        result = -1;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ( ip_type != SOCKET_TYPE_STREAM) {
 | 
			
		||||
        printf("socket type error \n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char at_cmd[64] = {0};
 | 
			
		||||
    char str_fd[2] = {0};
 | 
			
		||||
 | 
			
		||||
    itoa(adapter->socket.socket_id, str_fd, 10);
 | 
			
		||||
 | 
			
		||||
    memcpy(at_cmd, "AT+NSOCO=", 9);
 | 
			
		||||
    strcat(at_cmd, str_fd);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, ip);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, port);
 | 
			
		||||
    strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
    printf("cmd : %s\n", at_cmd);
 | 
			
		||||
    ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
    PrivTaskDelay(300);
 | 
			
		||||
 | 
			
		||||
__exit:
 | 
			
		||||
    if (reply) {
 | 
			
		||||
        DeleteATReply(reply);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t result = 0;
 | 
			
		||||
 | 
			
		||||
    ATReplyType reply = CreateATReply(64);
 | 
			
		||||
    if (NULL == reply) {
 | 
			
		||||
        printf("at create failed ! \n");
 | 
			
		||||
        result = -ERROR;
 | 
			
		||||
        goto __exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (adapter->socket.type == SOCKET_TYPE_STREAM ) {
 | 
			
		||||
        
 | 
			
		||||
        char at_cmd[64] = {0};
 | 
			
		||||
        char str_fd[2] = {0};
 | 
			
		||||
        char size[2] = {0};
 | 
			
		||||
 | 
			
		||||
        itoa(adapter->socket.socket_id, str_fd, 10);
 | 
			
		||||
        size[0] = len + '0';
 | 
			
		||||
 | 
			
		||||
        memcpy(at_cmd, "AT+NSOSD=", 9);
 | 
			
		||||
        strcat(at_cmd, str_fd);
 | 
			
		||||
        strcat(at_cmd, ",");
 | 
			
		||||
        strcat(at_cmd, size);
 | 
			
		||||
        strcat(at_cmd, ",");
 | 
			
		||||
        strcat(at_cmd, buf);
 | 
			
		||||
        strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
        printf("cmd : %s\n", at_cmd);
 | 
			
		||||
        ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
        PrivTaskDelay(300);
 | 
			
		||||
 | 
			
		||||
    } else if(adapter->socket.type == SOCKET_TYPE_DGRAM ) {
 | 
			
		||||
        char at_cmd[64] = {0};
 | 
			
		||||
        char str_fd[2] = {0};
 | 
			
		||||
        char listen_port[] = {0};
 | 
			
		||||
 | 
			
		||||
        itoa(adapter->socket.socket_id, str_fd, 10);
 | 
			
		||||
 | 
			
		||||
        itoa(adapter->socket.listen_port, listen_port, 10);
 | 
			
		||||
 | 
			
		||||
        memcpy(at_cmd, "AT+NSOST=", 9);
 | 
			
		||||
        strcat(at_cmd, str_fd);
 | 
			
		||||
        strcat(at_cmd, ",");
 | 
			
		||||
        strcat(at_cmd, adapter->socket.dst_ip_addr);
 | 
			
		||||
        strcat(at_cmd, ",");
 | 
			
		||||
        strcat(at_cmd, listen_port);
 | 
			
		||||
        strcat(at_cmd, ",");
 | 
			
		||||
        strcat(at_cmd, buf);
 | 
			
		||||
        strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
        printf("cmd : %s\n", at_cmd);
 | 
			
		||||
        ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
        PrivTaskDelay(300);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
__exit:
 | 
			
		||||
    if (reply) {
 | 
			
		||||
        DeleteATReply(reply);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Recv(struct Adapter *adapter, void *buf, size_t len)
 | 
			
		||||
{
 | 
			
		||||
    char at_cmd[64] = {0};
 | 
			
		||||
    char str_fd[2] = {0};
 | 
			
		||||
    char size[2] = {0};
 | 
			
		||||
 | 
			
		||||
    ATReplyType reply = CreateATReply(64);
 | 
			
		||||
    if (NULL == reply) {
 | 
			
		||||
        printf("at create failed ! \n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    itoa(adapter->socket.socket_id, str_fd, 10);
 | 
			
		||||
    size[0] = len + '0';
 | 
			
		||||
 | 
			
		||||
    memcpy(at_cmd, "AT+NSORF=", 9);
 | 
			
		||||
    strcat(at_cmd, str_fd);
 | 
			
		||||
    strcat(at_cmd, ",");
 | 
			
		||||
    strcat(at_cmd, size);
 | 
			
		||||
    strcat(at_cmd, "\n");
 | 
			
		||||
 | 
			
		||||
    printf("cmd : %s\n", at_cmd);
 | 
			
		||||
    ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
 | 
			
		||||
    PrivTaskDelay(300);
 | 
			
		||||
 | 
			
		||||
    buf = GetReplyText(reply);
 | 
			
		||||
    if (!buf) {
 | 
			
		||||
        printf("%s %n get reply failed.\n",__func__,__LINE__);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (reply) {
 | 
			
		||||
        DeleteATReply(reply);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int BC28Disconnect(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    if (!adapter) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return NBIoTSocketDelete(adapter);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct IpProtocolDone BC28_done = 
 | 
			
		||||
{
 | 
			
		||||
    .open = BC28Open,
 | 
			
		||||
    .close = BC28Close,
 | 
			
		||||
    .ioctl = BC28Ioctl,
 | 
			
		||||
    .setup = NULL,
 | 
			
		||||
    .setdown = NULL,
 | 
			
		||||
    .setaddr = NULL,
 | 
			
		||||
    .setdns = NULL,
 | 
			
		||||
    .setdhcp = NULL,
 | 
			
		||||
    .ping = NULL,
 | 
			
		||||
    .netstat = NULL,
 | 
			
		||||
    .connect = BC28Connect,
 | 
			
		||||
    .send = BC28Send,
 | 
			
		||||
    .recv = BC28Recv,
 | 
			
		||||
    .disconnect = BC28Disconnect,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
AdapterProductInfoType BC28Attach(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
 | 
			
		||||
    if (!product_info) {
 | 
			
		||||
        printf("BC28Attach malloc product_info error\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    memset(product_info, 0, sizeof(struct AdapterProductInfo));
 | 
			
		||||
 | 
			
		||||
    strncpy(product_info->model_name, ADAPTER_NBIOT_BC28,sizeof(product_info->model_name));
 | 
			
		||||
    product_info->model_done = (void *)&BC28_done;
 | 
			
		||||
 | 
			
		||||
    BC28PowerSet();
 | 
			
		||||
    return product_info;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
if CONNECTION_ADAPTER_NB
 | 
			
		||||
    config ADAPTER_BC28
 | 
			
		||||
            bool "Using nbiot adapter device BC28"
 | 
			
		||||
            default y
 | 
			
		||||
    
 | 
			
		||||
    if ADAPTER_BC28
 | 
			
		||||
        source "$APP_DIR/Framework/connection/nbiot/BC28/Kconfig"
 | 
			
		||||
    endif
 | 
			
		||||
        
 | 
			
		||||
endif
 | 
			
		||||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
SRC_FILES := adapter_nbiot.c
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_ADAPTER_BC28),y)
 | 
			
		||||
	SRC_DIR += BC28
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(KERNEL_ROOT)/compiler.mk
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,3 +17,128 @@
 | 
			
		|||
 * @author AIIT XUOS Lab
 | 
			
		||||
 * @date 2021.06.25
 | 
			
		||||
 */
 | 
			
		||||
#include <adapter.h>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef ADAPTER_BC28
 | 
			
		||||
extern AdapterProductInfoType BC28Attach(struct Adapter *adapter);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define ADAPTER_NBIOT_NAME "nbiot"
 | 
			
		||||
 | 
			
		||||
static int AdapterNbiotRegister(struct Adapter *adapter)
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    strncpy(adapter->name, ADAPTER_NBIOT_NAME, NAME_NUM_MAX); 
 | 
			
		||||
 
 | 
			
		||||
    adapter->net_protocol = IP_PROTOCOL;
 | 
			
		||||
    adapter->net_role = CLIENT;
 | 
			
		||||
 | 
			
		||||
    adapter->adapter_status = UNREGISTERED;
 | 
			
		||||
 | 
			
		||||
    ret = AdapterDeviceRegister(adapter);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        printf("AdapterNbiot register error\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int AdapterNbiotInit(void)
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    struct Adapter *adapter = malloc(sizeof(struct Adapter));
 | 
			
		||||
    if (!adapter) {
 | 
			
		||||
        printf("malloc adapter failed.\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    memset(adapter, 0, sizeof(struct Adapter));
 | 
			
		||||
    ret = AdapterNbiotRegister(adapter);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        printf("register nbiot adapter error\n");
 | 
			
		||||
        free(adapter);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
#ifdef ADAPTER_BC28
 | 
			
		||||
    AdapterProductInfoType product_info = BC28Attach(adapter);
 | 
			
		||||
    if (!product_info) {
 | 
			
		||||
        printf("bc28 attach error\n");
 | 
			
		||||
        free(adapter);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    adapter->product_info_flag = 1;
 | 
			
		||||
    adapter->info = product_info;
 | 
			
		||||
    adapter->done = product_info->model_done;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/******************TEST*********************/
 | 
			
		||||
// int openzigbee(void)
 | 
			
		||||
// {
 | 
			
		||||
//     int ret = 0;
 | 
			
		||||
 | 
			
		||||
//     struct Adapter* adapter =  AdapterDeviceFindByName(ADAPTER_ZIGBEE_NAME);
 | 
			
		||||
 | 
			
		||||
// #ifdef ADAPTER_E18
 | 
			
		||||
//     ret = AdapterDeviceOpen(adapter);
 | 
			
		||||
//     if(ret < 0){
 | 
			
		||||
//         printf("open adapter failed\n");
 | 
			
		||||
//         return -1;
 | 
			
		||||
//     }
 | 
			
		||||
 | 
			
		||||
//     adapter->info->work_mode = 1;
 | 
			
		||||
//     ret = AdapterDeviceControl(adapter, CONFIG_ZIGBEE_NET_MODE,NULL);
 | 
			
		||||
//     if(ret < 0){
 | 
			
		||||
//         printf("control adapter failed\n");
 | 
			
		||||
//         return -1;
 | 
			
		||||
//     }
 | 
			
		||||
//     ret = AdapterDeviceJoin(adapter, NULL);
 | 
			
		||||
//     if(ret < 0){
 | 
			
		||||
//         printf("join adapter failed\n");
 | 
			
		||||
//         return -1;
 | 
			
		||||
//     }
 | 
			
		||||
// #endif
 | 
			
		||||
 | 
			
		||||
//     return 0;    
 | 
			
		||||
// }
 | 
			
		||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, openzigbee, openzigbee, show adapter zigbee information);
 | 
			
		||||
 | 
			
		||||
// int sendzigbee(int argc, char *argv[])
 | 
			
		||||
// {
 | 
			
		||||
//     const char *send_msg = argv[1];
 | 
			
		||||
//     int ret = 0;
 | 
			
		||||
 | 
			
		||||
//     struct Adapter* adapter =  AdapterDeviceFindByName(ADAPTER_ZIGBEE_NAME);
 | 
			
		||||
 | 
			
		||||
//     printf("send argv1 %s\n",argv[1]);
 | 
			
		||||
//     ret = AdapterDeviceSend(adapter, send_msg, strlen(send_msg));
 | 
			
		||||
//     if(ret < 0){
 | 
			
		||||
//         printf(" adapter send failed\n");
 | 
			
		||||
//         return -1;
 | 
			
		||||
//     }
 | 
			
		||||
//     printf("zigbee send msg %s\n", send_msg);
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
//     return 0;    
 | 
			
		||||
// }
 | 
			
		||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, sendzigbee, sendzigbee, show adapter zigbee information);
 | 
			
		||||
 | 
			
		||||
// int recvzigbee(void)
 | 
			
		||||
// {
 | 
			
		||||
//     char recv_msg[128];
 | 
			
		||||
//     struct Adapter* adapter =  AdapterDeviceFindByName(ADAPTER_ZIGBEE_NAME);
 | 
			
		||||
//     memset(recv_msg,0,128);
 | 
			
		||||
//     AdapterDeviceRecv(adapter, recv_msg, 128);
 | 
			
		||||
//     PrivTaskDelay(2000);
 | 
			
		||||
//     printf("zigbee recv msg %s\n", recv_msg);
 | 
			
		||||
    
 | 
			
		||||
//     return 0;    
 | 
			
		||||
// }
 | 
			
		||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, recvzigbee, recvzigbee, show adapter zigbee information);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
#ifndef ADAPTER_NBIOT_H
 | 
			
		||||
#define ADAPTER_NBIOT_H
 | 
			
		||||
 | 
			
		||||
#define CONFIG_NBIOT_RESET            (0)
 | 
			
		||||
#define CONFIG_NBIOT_CREATE_SOCKET    (1)
 | 
			
		||||
#define CONFIG_NBIOT_DELETE_SOCKET    (2)
 | 
			
		||||
 | 
			
		||||
#define SOCKET_TYPE_DGRAM   (0)
 | 
			
		||||
#define SOCKET_TYPE_STREAM  (1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -438,6 +438,7 @@ AdapterProductInfoType E18Attach(struct Adapter *adapter)
 | 
			
		|||
        printf("E18Attach malloc product_info error\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    memset(product_info, 0, sizeof(struct AdapterProductInfo));
 | 
			
		||||
    
 | 
			
		||||
    strncpy(product_info->model_name, ADAPTER_ZIGBEE_E18,sizeof(product_info->model_name));
 | 
			
		||||
    product_info->model_done = (void *)&E18_done;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue