forked from xuos/xiuos
Add adapter.c
This commit is contained in:
parent
9158349752
commit
994658c08c
|
@ -11,16 +11,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file: xs_klist.h
|
* @file: list.h
|
||||||
* @brief: function declaration and structure defintion of linklist
|
* @brief: function declaration and structure defintion of linklist
|
||||||
* @version: 1.0
|
* @version: 1.0
|
||||||
* @author: AIIT XUOS Lab
|
* @author: AIIT XUOS Lab
|
||||||
* @date: 2020/3/2
|
* @date: 2020/6/8
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __XS_KLIST_H__
|
#ifndef __LIST_H__
|
||||||
#define __XS_KLIST_H__
|
#define __LIST_H__
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
SRC_FILES := adapter.c
|
||||||
|
|
||||||
|
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -0,0 +1,264 @@
|
||||||
|
/*
|
||||||
|
* 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 adapter.c
|
||||||
|
* @brief Implement the communication adapter framework management and API
|
||||||
|
* @version 1.0
|
||||||
|
* @author AIIT XUOS Lab
|
||||||
|
* @date 2021.05.10
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <adapter.h>
|
||||||
|
|
||||||
|
static DoubleLinklistType adapter_list;
|
||||||
|
|
||||||
|
static int adapter_list_lock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Init adapter framework
|
||||||
|
* @return 0
|
||||||
|
*/
|
||||||
|
int AdapterFrameworkInit(void)
|
||||||
|
{
|
||||||
|
InitDoubleLinkList(&adapter_list);
|
||||||
|
|
||||||
|
adapter_list_lock = KMutexCreate();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Find adapter device by name
|
||||||
|
* @param name - name string
|
||||||
|
* @return adapter device pointer
|
||||||
|
*/
|
||||||
|
struct Adapter *AdapterDeviceFind(const char *name)
|
||||||
|
{
|
||||||
|
struct Adapter *ret = NULL;
|
||||||
|
struct SysDoubleLinklistNode *node;
|
||||||
|
|
||||||
|
if (name == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
UserMutexObtain(adapter_list_lock, -1);
|
||||||
|
DOUBLE_LINKLIST_FOR_EACH(node, &adapter_list) {
|
||||||
|
struct Adapter *adapter =CONTAINER_OF(node,
|
||||||
|
struct Adapter, link);
|
||||||
|
if (strncmp(adapter->name, name, NAME_NUM_MAX) == 0) {
|
||||||
|
ret = adapter;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UserMutexAbandon(adapter_list_lock);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Register the adapter to the linked list
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @return success: 0 , failure: -1
|
||||||
|
*/
|
||||||
|
int AdapterDeviceRegister(struct Adapter *adapter)
|
||||||
|
{
|
||||||
|
if (adapter == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (AdapterDeviceFindByName(adapter->name) != NULL) {
|
||||||
|
printf("%s: sensor with the same name already registered\n", __func__);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
UserMutexObtain(adapter_list_lock, -1);
|
||||||
|
DoubleLinkListInsertNodeAfter(&adapter_list, &adapter->link);
|
||||||
|
UserMutexAbandon(adapter_list_lock);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Unregister the adapter from the linked list
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @return 0
|
||||||
|
*/
|
||||||
|
int AdapterDeviceUnregister(struct Adapter *adapter)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
UserMutexObtain(adapter_list, -1);
|
||||||
|
DoubleLinkListRmNode(&adapter->link);
|
||||||
|
UserMutexAbandon(adapter_list);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Open adapter device
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @return success: 0 , failure: other
|
||||||
|
*/
|
||||||
|
int AdapterDeviceOpen(struct Adapter *adapter)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
struct IpProtocolDone *ip_done = NULL;
|
||||||
|
struct PrivProtocolDone *priv_done = NULL;
|
||||||
|
|
||||||
|
switch (adapter->net_protocol)
|
||||||
|
{
|
||||||
|
case PRIVATE_PROTOCOL:
|
||||||
|
priv_done = (struct PrivProtocolDone *)adapter->done;
|
||||||
|
if (priv_done->open == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
result = priv_done->open(adapter);
|
||||||
|
if (result == 0) {
|
||||||
|
printf("Device %s open success.\n", adapter->name);
|
||||||
|
}else{
|
||||||
|
if (adapter->fd) {
|
||||||
|
PrivClose(adapter->fd);
|
||||||
|
adapter->fd = 0;
|
||||||
|
}
|
||||||
|
printf("Device %s open failed(%d).\n", adapter->name, result);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IP_PROTOCOL:
|
||||||
|
ip_done = (struct IpProtocolDone *)adapter->done;
|
||||||
|
if (ip_done->open == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
result = ip_done->open(adapter);
|
||||||
|
if (result == 0) {
|
||||||
|
printf("Device %s open success.\n", adapter->name);
|
||||||
|
}else{
|
||||||
|
if (adapter->fd) {
|
||||||
|
PrivClose(adapter->fd);
|
||||||
|
adapter->fd = 0;
|
||||||
|
}
|
||||||
|
printf("Device %s open failed(%d).\n", adapter->name, result);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Close adapter device
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @return success: 0 , failure: other
|
||||||
|
*/
|
||||||
|
int AdapterDeviceClose(struct Adapter *adapter)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
struct IpProtocolDone *ip_done = NULL;
|
||||||
|
struct PrivProtocolDone *priv_done = NULL;
|
||||||
|
|
||||||
|
switch (adapter->net_protocol)
|
||||||
|
{
|
||||||
|
case PRIVATE_PROTOCOL:
|
||||||
|
priv_done = (struct PrivProtocolDone *)adapter->done;
|
||||||
|
if (priv_done->close == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
result = priv_done->close(adapter);
|
||||||
|
if (result == 0)
|
||||||
|
printf("%s successfully closed.\n", adapter->name);
|
||||||
|
else
|
||||||
|
printf("Closed %s failure.\n", adapter->name);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IP_PROTOCOL:
|
||||||
|
ip_done = (struct IpProtocolDone *)adapter->done;
|
||||||
|
if (ip_done->close == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
result = ip_done->close(adapter);
|
||||||
|
if (result == 0)
|
||||||
|
printf("%s successfully closed.\n", adapter->name);
|
||||||
|
else
|
||||||
|
printf("Closed %s failure.\n", adapter->name);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Read data from adapter
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @param dst - buffer to save data
|
||||||
|
* @param len - buffer length
|
||||||
|
* @return gotten data length
|
||||||
|
*/
|
||||||
|
ssize_t AdapterDeviceRead(struct Adapter *adapter, void *dst, size_t len)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (adapter->done->read == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return adapter->done->read(adapter, dst, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Write data to adapter
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @param src - data buffer
|
||||||
|
* @param len - data length
|
||||||
|
* @return length of data written
|
||||||
|
*/
|
||||||
|
ssize_t AdapterDeviceWrite(struct Adapter *adapter, const void *src, size_t len)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (adapter->done->write == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return adapter->done->write(adapter, src, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Configure adapter
|
||||||
|
* @param adapter - adapter device pointer
|
||||||
|
* @param cmd - command
|
||||||
|
* @param args - command parameter
|
||||||
|
* @return success: 0 , failure: other
|
||||||
|
*/
|
||||||
|
int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args)
|
||||||
|
{
|
||||||
|
if (!adapter)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (adapter->done->ioctl == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return adapter->done->ioctl(adapter, cmd, args);
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
/*
|
||||||
|
* 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 adapter.h
|
||||||
|
* @brief Structure and function declarations of the communication adapter framework
|
||||||
|
* @version 1.0
|
||||||
|
* @author AIIT XUOS Lab
|
||||||
|
* @date 2021.05.10
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ADAPTER_H
|
||||||
|
#define ADAPTER_H
|
||||||
|
|
||||||
|
#include <list.h>
|
||||||
|
#include <transform.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define ADAPTER_BUFFSIZE 64
|
||||||
|
|
||||||
|
#define ADAPTER_LORA_FUNC ((uint32_t)(1 << ATAPTER_LORA))
|
||||||
|
#define ADAPTER_4G_FUNC ((uint32_t)(1 << ADAPTER_4G))
|
||||||
|
#define ADAPTER_NBIOT_FUNC ((uint32_t)(1 << ADAPTER_NBIOT))
|
||||||
|
#define ADAPTER_WIFI_FUNC ((uint32_t)(1 << ADAPTER_WIFI))
|
||||||
|
#define ADAPTER_ETHERNET_FUNC ((uint32_t)(1 << ADAPTER_ETHERNET))
|
||||||
|
#define ADAPTER_BLUETOOTH_FUNC ((uint32_t)(1 << ADAPTER_BLUETOOTH))
|
||||||
|
#define ADAPTER_ZIGBEE_FUNC ((uint32_t)(1 << ADAPTER_ZIGBEE))
|
||||||
|
#define ADAPTER_5G_FUNC ((uint32_t)(1 << ADAPTER_5G))
|
||||||
|
|
||||||
|
struct Adapter;
|
||||||
|
|
||||||
|
struct Socket
|
||||||
|
{
|
||||||
|
int id;
|
||||||
|
struct Adapter *adapter;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum AdapterType
|
||||||
|
{
|
||||||
|
ADAPTER_LORA = 0,
|
||||||
|
ADAPTER_4G ,
|
||||||
|
ADAPTER_NBIOT ,
|
||||||
|
ADAPTER_WIFI ,
|
||||||
|
ADAPTER_ETHERNET ,
|
||||||
|
ADAPTER_BLUETOOTH ,
|
||||||
|
ADAPTER_ZIGBEE ,
|
||||||
|
ADAPTER_5G ,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum NetProtocolType
|
||||||
|
{
|
||||||
|
PRIVATE_PROTOCOL = 1,
|
||||||
|
IP_PROTOCOL,
|
||||||
|
PROTOCOL_NONE,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum NetRoleType
|
||||||
|
{
|
||||||
|
CLIENT = 1,
|
||||||
|
SERVER,
|
||||||
|
ROLE_NONE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AdapterProductInfo
|
||||||
|
{
|
||||||
|
uint32_t functions;
|
||||||
|
const char *vendor_name;
|
||||||
|
const char *model_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct IpProtocolDone
|
||||||
|
{
|
||||||
|
int (*open)(struct Adapter *adapter);
|
||||||
|
int (*close)(struct Adapter *adapter);
|
||||||
|
int (*ioctl)(struct Adapter *adapter, int cmd, void *args);
|
||||||
|
int (*connect)(struct Adapter *adapter, const char *ip, const char *port, uint8_t ip_type);
|
||||||
|
int (*send)(struct Socket *socket, const void *buf, size_t len);
|
||||||
|
int (*recv)(struct Socket *socket, void *buf, size_t len);
|
||||||
|
int (*disconnect)(struct Socket *socket);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PrivProtocolDone
|
||||||
|
{
|
||||||
|
int (*open)(struct Adapter *adapter);
|
||||||
|
int (*close)(struct Adapter *adapter);
|
||||||
|
int (*ioctl)(struct Adapter *adapter, int cmd, void *args);
|
||||||
|
int (*join)(struct Adapter *adapter, const char *priv_net_group);
|
||||||
|
int (*send)(struct Adapter *adapter, const void *buf, size_t len);
|
||||||
|
int (*recv)(struct Adapter *adapter, void *buf, size_t len);
|
||||||
|
int (*disconnect)(struct Adapter *adapter);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Adapter
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
struct AdapterProductInfo *info;
|
||||||
|
|
||||||
|
enum NetProtocolType net_protocol;
|
||||||
|
enum NetRoleType net_role;
|
||||||
|
|
||||||
|
char buffer[ADAPTER_BUFFSIZE];
|
||||||
|
|
||||||
|
void *done;
|
||||||
|
|
||||||
|
struct SysDoubleLinklistNode link;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -38,7 +38,7 @@ int PrivOpen(const char *path, int flags, ...)
|
||||||
return open(path, flags, ...);
|
return open(path, flags, ...);
|
||||||
}
|
}
|
||||||
|
|
||||||
int PrivClose(int fd, void *buf, size_t len)
|
int PrivClose(int fd)
|
||||||
{
|
{
|
||||||
return close(fd);
|
return close(fd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ struct PrivIoctlCfg
|
||||||
|
|
||||||
int PrivOpen(const char *path, int flags, ...);
|
int PrivOpen(const char *path, int flags, ...);
|
||||||
|
|
||||||
int PrivClose(int fd, void *buf, size_t len);
|
int PrivClose(int fd);
|
||||||
|
|
||||||
int PrivRead(int fd, void *buf, size_t len);
|
int PrivRead(int fd, void *buf, size_t len);
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,6 @@ config APP_DIR
|
||||||
option env="SRC_APP_DIR"
|
option env="SRC_APP_DIR"
|
||||||
default "."
|
default "."
|
||||||
|
|
||||||
source "$APP_DIR/Applications/Kconfig"
|
source "$APP_DIR/Framework/Kconfig"
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
|
Loading…
Reference in New Issue