From a0fb0006ca571f37558fd6891f006b71bbb81f25 Mon Sep 17 00:00:00 2001 From: yudongdong Date: Fri, 7 May 2021 15:09:52 +0800 Subject: [PATCH] Add bluetooth p2p basic funciton --- applications/Makefile | 4 + applications/connection_demo/Makefile | 7 - .../connection_demo/bluetooth_demo/Makefile | 3 + .../bluetooth_demo/bluetooth_receive_demo.c | 88 ++++++++++ .../bluetooth_demo/bluetooth_send_demo.c | 65 ++++++++ applications/framework_init.c | 4 + framework/connection/Adapter/Kconfig | 4 + framework/connection/Adapter/Makefile | 12 +- .../connection/Adapter/bluetooth/Makefile | 3 + .../bluetooth/xs_adaper_bluetooth_register.c | 45 ++++++ .../Adapter/bluetooth/xs_adapter_bluetooth.c | 150 ++++++++++++++++++ .../Adapter/include/xs_adapter_bluetooth.h | 41 +++++ .../Adapter/include/xs_adapter_manager.h | 3 + .../Adapter/src/xs_adapter_manager.c | 38 +++++ 14 files changed, 451 insertions(+), 16 deletions(-) delete mode 100644 applications/connection_demo/Makefile create mode 100644 applications/connection_demo/bluetooth_demo/Makefile create mode 100644 applications/connection_demo/bluetooth_demo/bluetooth_receive_demo.c create mode 100644 applications/connection_demo/bluetooth_demo/bluetooth_send_demo.c create mode 100644 framework/connection/Adapter/bluetooth/Makefile create mode 100644 framework/connection/Adapter/bluetooth/xs_adaper_bluetooth_register.c create mode 100644 framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c create mode 100644 framework/connection/Adapter/include/xs_adapter_bluetooth.h diff --git a/applications/Makefile b/applications/Makefile index 22d87b21..a777aef6 100644 --- a/applications/Makefile +++ b/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 diff --git a/applications/connection_demo/Makefile b/applications/connection_demo/Makefile deleted file mode 100644 index c156bdfb..00000000 --- a/applications/connection_demo/Makefile +++ /dev/null @@ -1,7 +0,0 @@ - -SRC_DIR += - - - - -include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/applications/connection_demo/bluetooth_demo/Makefile b/applications/connection_demo/bluetooth_demo/Makefile new file mode 100644 index 00000000..efaa7edc --- /dev/null +++ b/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 \ No newline at end of file diff --git a/applications/connection_demo/bluetooth_demo/bluetooth_receive_demo.c b/applications/connection_demo/bluetooth_demo/bluetooth_receive_demo.c new file mode 100644 index 00000000..246a5436 --- /dev/null +++ b/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 +#include +#include +#include +#include +#include +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 +#include +#include +#include +#include +#include +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 + + diff --git a/applications/framework_init.c b/applications/framework_init.c index 20ef0327..855d2b86 100644 --- a/applications/framework_init.c +++ b/applications/framework_init.c @@ -17,6 +17,7 @@ extern int SensorFrameworkInit(void); extern int RegisterAdapterEthernet(void); extern int RegisterAdapterWifi(void); extern int RegisterAdapterZigbee(void); +extern int RegisterAdapterBluetooth(void); extern int LoraSx12xxSpiDeviceInit(); extern int D124VoiceInit(void); @@ -99,6 +100,9 @@ static struct InitDesc connection_desc[] = #ifdef CONNECTION_COMMUNICATION_ZIGBEE { "zigbee adpter", RegisterAdapterZigbee}, +#endif +#ifdef CONNECTION_COMMUNICATION_BLUETOOTH + { "bluetooth adpter", RegisterAdapterBluetooth}, #endif { "NULL", NULL }, }; diff --git a/framework/connection/Adapter/Kconfig b/framework/connection/Adapter/Kconfig index 04edaf67..4e72451a 100644 --- a/framework/connection/Adapter/Kconfig +++ b/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 diff --git a/framework/connection/Adapter/Makefile b/framework/connection/Adapter/Makefile index 78a7ea35..7b936696 100644 --- a/framework/connection/Adapter/Makefile +++ b/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 \ No newline at end of file diff --git a/framework/connection/Adapter/bluetooth/Makefile b/framework/connection/Adapter/bluetooth/Makefile new file mode 100644 index 00000000..1959f2d3 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/framework/connection/Adapter/bluetooth/xs_adaper_bluetooth_register.c b/framework/connection/Adapter/bluetooth/xs_adaper_bluetooth_register.c new file mode 100644 index 00000000..72a31d65 --- /dev/null +++ b/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 +#include +#include +/* 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; +} \ No newline at end of file diff --git a/framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c b/framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c new file mode 100644 index 00000000..8c8a21a0 --- /dev/null +++ b/framework/connection/Adapter/bluetooth/xs_adapter_bluetooth.c @@ -0,0 +1,150 @@ +/* +* 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 +#include "xs_adapter_bluetooth.h" +#include +#include +#include +#include + +#define SAMPLE_UART_NAME "/dev/uart3_dev3" + +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); +} diff --git a/framework/connection/Adapter/include/xs_adapter_bluetooth.h b/framework/connection/Adapter/include/xs_adapter_bluetooth.h new file mode 100644 index 00000000..872ce0a1 --- /dev/null +++ b/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 diff --git a/framework/connection/Adapter/include/xs_adapter_manager.h b/framework/connection/Adapter/include/xs_adapter_manager.h index ba2ce7a6..a9646d99 100644 --- a/framework/connection/Adapter/include/xs_adapter_manager.h +++ b/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 \ No newline at end of file diff --git a/framework/connection/Adapter/src/xs_adapter_manager.c b/framework/connection/Adapter/src/xs_adapter_manager.c index 4314da69..d8f3eb1e 100644 --- a/framework/connection/Adapter/src/xs_adapter_manager.c +++ b/framework/connection/Adapter/src/xs_adapter_manager.c @@ -26,6 +26,9 @@ #ifdef CONNECTION_COMMUNICATION_ZIGBEE #include #endif +#ifdef CONNECTION_COMMUNICATION_BLUETOOTH +#include +#endif #ifdef CONNECTION_COMMUNICATION_LORA #include #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;