Browse Source

Add bluetooth function on kd233 from Yu Dongdong

it is OK
pull/3/head
xuedongliang 1 year ago
parent
commit
fc257d1f8b
  1. 4
      applications/Makefile
  2. 7
      applications/connection_demo/Makefile
  3. 3
      applications/connection_demo/bluetooth_demo/Makefile
  4. 88
      applications/connection_demo/bluetooth_demo/bluetooth_receive_demo.c
  5. 65
      applications/connection_demo/bluetooth_demo/bluetooth_send_demo.c
  6. 4
      applications/framework_init.c
  7. 4
      framework/connection/Adapter/Kconfig
  8. 12
      framework/connection/Adapter/Makefile
  9. 3
      framework/connection/Adapter/bluetooth/Makefile
  10. 45
      framework/connection/Adapter/bluetooth/xs_adaper_bluetooth_register.c
  11. 155
      framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c
  12. 41
      framework/connection/Adapter/include/xs_adapter_bluetooth.h
  13. 3
      framework/connection/Adapter/include/xs_adapter_manager.h
  14. 38
      framework/connection/Adapter/src/xs_adapter_manager.c

4
applications/Makefile

@ -49,6 +49,10 @@ endif
# SRC_DIR += connection_demo/4G_demo
# endif
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BLUETOOTH), y)
SRC_DIR += connection_demo/bluetooth_demo
endif
ifeq ($(CONFIG_APPLICATION_SENSOR),y)
SRC_DIR += sensor_app
endif

7
applications/connection_demo/Makefile

@ -1,7 +0,0 @@
SRC_DIR +=
include $(KERNEL_ROOT)/compiler.mk

3
applications/connection_demo/bluetooth_demo/Makefile

@ -0,0 +1,3 @@
SRC_FILES := bluetooth_receive_demo.c bluetooth_send_demo.c
# zigbee_send_demo.c zigbee_receive_demo.c
include $(KERNEL_ROOT)/compiler.mk

88
applications/connection_demo/bluetooth_demo/bluetooth_receive_demo.c

@ -0,0 +1,88 @@
/*
* 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: bluetooth_receive_demo.c
* @brief: using bluetooth to receive message
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2021/4/30
*
*/
#include <xs_adapter_bluetooth.h>
#include <string.h>
#include <xs_klist.h>
#include <xs_adapter_manager.h>
#include <user_api.h>
#include <string.h>
static int re_sem;
static int buff_sem;
/*Critical zone protection function for*/
void BluetoothWait(char *rev_buffer)
{
while(1){
if (strlen(rev_buffer)>1){
UserSemaphoreAbandon(re_sem);
break;
}
}
}
/* receive message from another bluetooth device*/
void BluetoothReceiveDemo(int argc, char *argv[])
{
adapter_t padapter = BluetoothAdapterFind("Bluetooth");
if (NONE == padapter){
KPrintf("adapter find failed!\n");
return;
}
/*Open adapter*/
if (0 != padapter->done.NetAiitOpen(padapter)){
KPrintf("adapter open failed!\n");
return;
}
char rev_buffer[NAME_NUM_MAX];
/* Initialize semaphore */
re_sem = UserSemaphoreCreate(0);
/* receive buffer from serial port */
padapter->done.NetAiitReceive(padapter,rev_buffer,strlen(rev_buffer),10000,false,NULL);
BluetoothWait(rev_buffer);
UserSemaphoreObtain(re_sem,-1);
printf("\n");
for (int i=0;i<strlen(rev_buffer);i++)
{
if(rev_buffer[i] != 0Xff)
printf("%c",rev_buffer[i]);
}
printf("\n");
}
#ifndef SEPARATE_COMPILE
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
BluetoothReceiveDemo, BluetoothReceiveDemo, bluetooth receive function );
#endif

65
applications/connection_demo/bluetooth_demo/bluetooth_send_demo.c

@ -0,0 +1,65 @@
/*
* 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: bluetooth_send_demo.c
* @brief: using bluetooth to send message
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2021/4/30
*
*/
#include <xs_adapter_bluetooth.h>
#include <string.h>
#include <xs_klist.h>
#include <xs_adapter_manager.h>
#include <string.h>
#include <user_api.h>
adapter_t padapter;
/* a demo function to send message through command line using bluetooth*/
/* first open bluetooth to start demo*/
void BluetoothOpenDemo()
{
/*Find from the list of registered adapters*/
// adapter_t padapter = BluetoothAdapterFind("Bluetoot");
padapter = BluetoothAdapterFind("Bluetooth");
if (NONE == padapter){
printf("adapter find failed!\n");
return;
}
/*Open adapter*/
if (0 != padapter->done.NetAiitOpen(padapter)){
printf("adapter open failed!\n");
return;
}
}
#ifndef SEPARATE_COMPILE
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
BluetoothOpenDemo, BluetoothOpenDemo, bluetooth send function );
#endif
void BluetoothSendDemo(int argc, char *argv[])
{
/*Find from the list of registered adapters*/
bool v = false;
padapter->done.NetAiitSend(padapter, argv[1], strlen(argv[1]) ,true,10000,0, NULL,&v,NULL);
}
#ifndef SEPARATE_COMPILE
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
BluetoothSendDemo, BluetoothSendDemo, bluetooth send function );
#endif

4
applications/framework_init.c

@ -18,6 +18,7 @@ extern int RegisterAdapterEthernet(void);
extern int RegisterAdapterWifi(void);
extern int RegisterAdapterZigbee(void);
extern int RegisterAdapterNBIoT(void);
extern int RegisterAdapterBluetooth(void);
extern int LoraSx12xxSpiDeviceInit();
extern int D124VoiceInit(void);
@ -104,6 +105,9 @@ static struct InitDesc connection_desc[] =
#ifdef CONNECTION_COMMUNICATION_NB_IOT
{ "NB-IoT adpter", RegisterAdapterNBIoT},
#endif
#ifdef CONNECTION_COMMUNICATION_BLUETOOTH
{ "bluetooth adpter", RegisterAdapterBluetooth},
#endif
{ "NULL", NULL },
};

4
framework/connection/Adapter/Kconfig

@ -25,3 +25,7 @@ menuconfig CONNECTION_COMMUNICATION_NB_IOT
menuconfig CONNECTION_COMMUNICATION_4G
bool "Enable 4G"
default n
menuconfig CONNECTION_COMMUNICATION_BLUETOOTH
bool "Enable BlueTooth"
default n

12
framework/connection/Adapter/Makefile

@ -25,14 +25,8 @@ endif
# SRC_DIR += 4G
# endif
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BLUETOOTH), y)
# SRC_DIR += bluetooth
# endif
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_RS485), y)
# SRC_DIR += rs485
# endif
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BLUETOOTH), y)
SRC_DIR += bluetooth
endif
include $(KERNEL_ROOT)/compiler.mk

3
framework/connection/Adapter/bluetooth/Makefile

@ -0,0 +1,3 @@
SRC_FILES := xs_adapter_bluetooth.c xs_adaper_bluetooth_register.c
include $(KERNEL_ROOT)/compiler.mk

45
framework/connection/Adapter/bluetooth/xs_adaper_bluetooth_register.c

@ -0,0 +1,45 @@
/*
* 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: xs_AdaperBluetooth_register.c
* @brief: register Bluetooth in initialization
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2021/4/30
*
*/
#include <xs_adapter_bluetooth.h>
#include <xs_adapter_manager.h>
#include <string.h>
/* initialize to the register list*/
int RegisterAdapterBluetooth(void)
{
static struct AdapterBluetooth bluetooth_adapter;
memset(&bluetooth_adapter, 0, sizeof(bluetooth_adapter));
static struct AdapterDone bluetooth_send_done = {
.NetAiitOpen = BluetoothOpen,
.NetAiitClose = BluetoothClose,
.NetAiitSend = BluetoothSend,
.NetAiitReceive = BluetoothReceive,
.NetAiitJoin = NULL,
.NetAiitIoctl = NULL,
};
bluetooth_adapter.parent.done = bluetooth_send_done;
bluetooth_adapter.name = "Bluetooth";
BluetoothAdapterInit();
BluetoothAdapterRegister((adapter_t)&bluetooth_adapter);
return EOK;
}

155
framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c

@ -0,0 +1,155 @@
/*
* 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: xs_Adapterxs_adapter_bluetooth.c
* @brief: bluetooth open close function
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2021/4/30
*
*/
#include <xs_adapter_manager.h>
#include "xs_adapter_bluetooth.h"
#include <user_api.h>
#include <bus_serial.h>
#include <dev_serial.h>
#include <string.h>
#ifdef BOARD_K210_EVB
#define SAMPLE_UART_NAME "/dev/uart3_dev3"
#endif
#ifdef BOARD_STM32F407_EVB
#define SAMPLE_UART_NAME "/dev/usart3_dev3"
#endif
static int serial_fd;
static int32_t bluetooth_receive;
static int rx_sem;
char bluetooth_buffer[NAME_NUM_MAX ]={0};
/* initialize srial port to open bluetooth*/
int BluetoothOpen(struct Adapter *padapter)
{
/* Open device in read-write mode */
serial_fd = open(SAMPLE_UART_NAME,O_RDWR);
/* set serial config, serial_baud_rate = 115200 */
struct SerialDataCfg cfg;
cfg.serial_baud_rate = BAUD_RATE_115200;
cfg.serial_data_bits = DATA_BITS_8;
cfg.serial_stop_bits = STOP_BITS_1;
cfg.serial_parity_mode = PARITY_NONE;
cfg.serial_bit_order = 0;
cfg.serial_invert_mode = 0;
cfg.serial_buffer_size = 128;
ioctl(serial_fd, 0, &cfg);
UserTaskDelay(1000);
printf("Bluetooth ready\n");
return 0;
}
/* send message to srial port*/
int BluetoothSend(struct Adapter *padapter, const char* data, int len, bool block, int time_out, int delay, send_success cb, void* param, void* reserved)
{
write(serial_fd,data,strlen(data));
return 0;
}
/*thread to read message from srial port*/
void SerialThreadEntry(void *parameter)
{
char ch;
int i = 0;
int n;
int run = 0;
while (1){
n = read(serial_fd,&ch,1);
if (n>0){
if (ch == '~'){
UserSemaphoreAbandon(rx_sem);
run = 1;
break;
}
bluetooth_buffer[i++] = ch;
}
if (run ==1)
break;
}
}
int BluetoothReceive(struct Adapter *padapter, char* rev_buffer, int buffer_len,int time_out, bool block, void* reserved)
{
x_err_t ret = EOK;
/* Set callback function */
/* Create thread serial */
UtaskType recv;
recv.name[0] = 'z';
recv.func_entry = SerialThreadEntry;
recv.func_param = NONE;
recv.stack_size = 1024;
recv.prio = 25;
memset(bluetooth_buffer, 0, sizeof(bluetooth_buffer));
/* Initialize semaphore */
rx_sem = UserSemaphoreCreate(0);
bluetooth_receive = UserTaskCreate(recv);
UserTaskStartup(bluetooth_receive);
/*copy to the receive buffer*/
UserSemaphoreObtain(rx_sem,-1);
memcpy(rev_buffer,bluetooth_buffer,strlen(bluetooth_buffer)+1 );
return ret;
}
void BluetoothSettingDemo(int argc, char *argv[])
{
adapter_t padapter = BluetoothAdapterFind("Bluetooth");
if (NONE == padapter){
KPrintf("adapter find failed!\n");
return;
}
/*Open adapter*/
if (0 != padapter->done.NetAiitOpen(padapter)){
KPrintf("adapter open failed!\n");
return;
}
BluetoothOpen(padapter);
/*Bluetooth communication settings*/
/*it can be changed if needed*/
char* set5 = "AT";
write(serial_fd,set5,strlen(set5));
UserTaskDelay(1000);
printf("bluetooth setting success!\n");
}
#ifndef SEPARATE_COMPILE
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
BluetoothSettingDemo, BluetoothSettingDemo, bluetooth send function );
#endif
void BluetoothClose(struct Adapter *padapter)
{
UserTaskDelete(bluetooth_receive);
close(serial_fd);
}

41
framework/connection/Adapter/include/xs_adapter_bluetooth.h

@ -0,0 +1,41 @@
/*
* 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: xs_adapter_bluetooth.h
* @brief: head file
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2021/4/25
*
*/
#ifndef XS_ADAPTER_BLUETOOTH_H
#define XS_ADAPTER_BLUETOOTH_H
#include "xs_adapter.h"
struct AdapterBluetooth {
struct Adapter parent; /* inherit from Adapter */
const char * name; /* name of the adapter instance */
const char * device_type; /* type of the adapter instance */
};
typedef struct AdapterBluetooth *AdapterBluetooth_t;
int BluetoothOpen(struct Adapter *padapter);
int BluetoothSend(struct Adapter *padapter, const char* data, int len, bool block, int time_out, int delay, send_success cb, void* param, void* reserved);
int BluetoothReceive(struct Adapter *padapter, char* rev_buffer, int buffer_len,int time_out, bool block, void* reserved);
void BluetoothClose(struct Adapter *padapter);
#endif

3
framework/connection/Adapter/include/xs_adapter_manager.h

@ -36,5 +36,8 @@ void ZigbeeAdapterInit();
void ZigbeeAdapterRegister(adapter_t padapter);
void* ZigbeeAdapterFind(char* name);
void BluetoothAdapterInit();
void BluetoothAdapterRegister(adapter_t padapter);
void* BluetoothAdapterFind(char* name);
#endif

38
framework/connection/Adapter/src/xs_adapter_manager.c

@ -26,6 +26,9 @@
#ifdef CONNECTION_COMMUNICATION_ZIGBEE
#include <xs_adapter_zigbee.h>
#endif
#ifdef CONNECTION_COMMUNICATION_BLUETOOTH
#include <xs_adapter_bluetooth.h>
#endif
#ifdef CONNECTION_COMMUNICATION_LORA
#include <xs_adapter_lora.h>
#endif
@ -63,6 +66,41 @@ void* ZigbeeAdapterFind(char* name)
}
#endif
// Bluetooth Adapter List
#ifdef CONNECTION_COMMUNICATION_BLUETOOTH
static DoubleLinklistType bluetooth_adapter_list;
void BluetoothAdapterInit()
{
InitDoubleLinkList(&bluetooth_adapter_list);
}
void BluetoothAdapterRegister(adapter_t padapter)
{
DoubleLinkListInsertNodeAfter(&bluetooth_adapter_list, &(padapter->link));
}
void* BluetoothAdapterFind(char* name)
{
DoubleLinklistType* pnode = NONE;
DoubleLinklistType* phead = &bluetooth_adapter_list;
struct AdapterBluetooth* padapter = NONE;
for(pnode = phead->node_next; pnode != phead; pnode = pnode->node_next) {
padapter = (struct AdapterBluetooth*)SYS_DOUBLE_LINKLIST_ENTRY(pnode, struct Adapter, link);
// KPrintf("BluetoothReceiveDemo bbb\n");
if (0 == strcmp(padapter->name, name)){
return padapter;
}
}
return padapter;
}
#endif
// Lora Adapter List
#ifdef CONNECTION_COMMUNICATION_LORA
static DoubleLinklistType lora_adapter_list;

Loading…
Cancel
Save