mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-17 23:48:13 +08:00
修改了Web后台的部分界面,增加了HAmqtt中的总电量传感器,后台新增mqtt上报频率设置
This commit is contained in:
18
mico-os/libraries/daemons/ble_access/ble_access.mk
Normal file
18
mico-os/libraries/daemons/ble_access/ble_access.mk
Normal file
@@ -0,0 +1,18 @@
|
||||
#
|
||||
# UNPUBLISHED PROPRIETARY SOURCE CODE
|
||||
# Copyright (c) 2016 MXCHIP Inc.
|
||||
#
|
||||
# The contents of this file may not be disclosed to third parties, copied or
|
||||
# duplicated in any form, in whole or in part, without the prior written
|
||||
# permission of MXCHIP Corporation.
|
||||
#
|
||||
|
||||
|
||||
NAME := Lib_Ble_Access_Core_Framework
|
||||
|
||||
GLOBAL_INCLUDES += .
|
||||
|
||||
$(NAME)_SOURCES := ble_access_core.c \
|
||||
ble_access_core_i.c
|
||||
|
||||
$(NAME)_COMPONENTS := daemons/bt_smart bluetooth/low_energy
|
||||
1384
mico-os/libraries/daemons/ble_access/ble_access_core.c
Normal file
1384
mico-os/libraries/daemons/ble_access/ble_access_core.c
Normal file
File diff suppressed because it is too large
Load Diff
482
mico-os/libraries/daemons/ble_access/ble_access_core.h
Normal file
482
mico-os/libraries/daemons/ble_access/ble_access_core.h
Normal file
@@ -0,0 +1,482 @@
|
||||
/*
|
||||
******************************************************************************
|
||||
* @file ble_access_core.h
|
||||
* @author Jian Zhang
|
||||
* @version V1.2.1
|
||||
* @date 26-Dec-2016
|
||||
* @file BLE ACCESS Protocol Components
|
||||
* ******************************************************************************
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 MXCHIP Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is furnished
|
||||
* to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
******************************************************************************
|
||||
* BLE Vendor Specific Device
|
||||
*
|
||||
* Features demonstrated
|
||||
* - Implement BLE_ACCESS Center Protocol developed by MXCHIP on MiCOKit-3239.
|
||||
* - You should see details about this protocol on mico.io.
|
||||
*
|
||||
******************************************************************************
|
||||
**/
|
||||
|
||||
#ifndef __BLE_ACCESS_CORE_H__
|
||||
#define __BLE_ACCESS_CORE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Configurations & Constants
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* The prefix of device address of peripheral device. */
|
||||
#define BLE_ACCESS_PREFIX_LEN 3
|
||||
#define BLE_ACCESS_PREFIX_D0BAE4 "\xD0\xBA\xE4"
|
||||
#define BLE_ACCESS_PREFIX_C89346 "\xC8\x93\x46"
|
||||
|
||||
/* The maximum length of a Bluetooth Device Name */
|
||||
#define BLE_ACCESS_DEVICE_NAME_MAX_LEN 16
|
||||
|
||||
/* Advertisement PACKET type */
|
||||
#define BLE_ACCESS_ADV_TYPE_INIT 0x01
|
||||
#define BLE_ACCESS_ADV_TYPE_RECONN 0x02
|
||||
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
* Host Request Command
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define BLE_ACCESS_STATUS_SUCCESS 0x00
|
||||
#define BLE_ACCESS_STATUS_FAILED 0x01
|
||||
#define BLE_ACCESS_STATUS_AUTH_FAILED 0x02
|
||||
#define BLE_ACCESS_STATUS_ADD_FAILED 0x03
|
||||
#define BLE_ACCESS_STATUS_SLAVE_REJECT 0x04
|
||||
#define BLE_ACCESS_STATUS_RECONN_FAILED 0x05
|
||||
#define BLE_ACCESS_STATUS_NO_RESOURCES 0x06
|
||||
#define BLE_ACCESS_STATUS_BUSY 0x07
|
||||
#define BLE_ACCESS_STATUS_TIMEOUT 0x08
|
||||
#define BLE_ACCESS_STATUS_SLAVE_REQ_DISC 0x09
|
||||
#define BLE_ACCESS_STATUS_INVALID_PARMS 0x0A
|
||||
#define BLE_ACCESS_STATUS_NO_CONNECTION 0x0B
|
||||
|
||||
/*
|
||||
* input: none
|
||||
* output: device_id,name,RSSI (BLE_ACCESS_EVENT_DEV_NEW)
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_SCAN 0x01
|
||||
|
||||
/*
|
||||
* input: device_id
|
||||
* output: device_id, status (BLE_ACCESS_EVENT_DEV_ADD)
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_ADD 0x02
|
||||
|
||||
/*
|
||||
* input: device_id, timeout
|
||||
* output: device_id, status (BLE_ACCESS_EVENT_DEV_DISC)
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_DISC 0x03
|
||||
|
||||
/*
|
||||
* input: device_id
|
||||
* output: device_id, status (BLE_ACCESS_EVENT_DEV_REMOVE)
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_REMOVE 0x04
|
||||
|
||||
/*
|
||||
* input: none
|
||||
* output: none
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_START_AUTO 0x05 /* Request to start auto connection procedure */
|
||||
|
||||
/*
|
||||
* input: none
|
||||
* output: none
|
||||
*/
|
||||
#define BLE_ACCESS_REQ_DEV_STOP_AUTO 0x06 /* Request to stop auto connection procedure */
|
||||
|
||||
/*
|
||||
* Request command parameters
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t device_id;
|
||||
union {
|
||||
/* used by BLE_ACCESS_REQ_DEV_SCAN */
|
||||
mico_bool_t start;
|
||||
/* used by BLE_ACCESS_REQ_DEV_DISC */
|
||||
uint32_t timeout;
|
||||
} p;
|
||||
} ble_access_cmd_parms_t;
|
||||
|
||||
/*
|
||||
* UUID type
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t len; /* UUID length */
|
||||
union {
|
||||
uint16_t uuid16; /* 16-bit UUID */
|
||||
uint32_t uuid32; /* 32-bit UUID */
|
||||
uint8_t uuid128[16]; /* 128-bit UUID */
|
||||
} uu;
|
||||
} ble_access_uuid_t;
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
/*
|
||||
* 3.1 Attribute Value of Service Definition
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t start_handle; /* Starting handle */
|
||||
uint16_t end_handle; /* Ending handle */
|
||||
ble_access_uuid_t uuid; /* UUID */
|
||||
} ble_access_attr_val_serv_t;
|
||||
|
||||
/*
|
||||
* 3.2 Attribute Value of Include Definition
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t included_service_handle; /* Included service handle */
|
||||
uint16_t end_group_handle; /* End group handle */
|
||||
ble_access_uuid_t uuid; /* UUID */
|
||||
} ble_access_attr_val_inc_t;
|
||||
|
||||
/*
|
||||
* 3.3.1 Attribute Value of Characteristic Declaration
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t properties; /* Properties */
|
||||
uint16_t value_handle; /* Value handle */
|
||||
ble_access_uuid_t uuid; /* UUID */
|
||||
|
||||
uint16_t descriptor_start_handle; /* Descriptor start handle. Additional field. Not in spec */
|
||||
uint16_t descriptor_end_handle; /* Descriptor end handle. Additional field. Not in spec */
|
||||
} ble_access_attr_val_char_t;
|
||||
|
||||
/*
|
||||
* 3.3.2 Attribute Value of Characteristic Value Declaration
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t value[1]; /* Start of value */
|
||||
} ble_access_attr_val_char_val_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.1 Attribute Value of Characteristic Extended Properties
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t properties; /* Properties */
|
||||
} ble_access_attr_val_ext_prop_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.2 Attribute Value of Characteristic User Description
|
||||
*/
|
||||
typedef struct {
|
||||
char string[1]; /* User description string */
|
||||
} ble_access_attr_val_usr_desc_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.3 Attribute Value of Client Characteristic Configuration
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t config_bits; /* Configuration bits */
|
||||
} ble_access_attr_val_cli_cfg_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.4 Attribute Value of Server Characteristic Configuration
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t config_bits; /* Configuration bits */
|
||||
} ble_access_attr_val_srv_cfg_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.5 Attribute Value of Characteristic Presentation Format
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t format; /* Format */
|
||||
uint8_t exponent; /* Exponent */
|
||||
uint16_t unit; /* Unit */
|
||||
uint8_t name_space; /* Namespace */
|
||||
uint16_t description; /* Description */
|
||||
} ble_access_attr_val_pres_format_t;
|
||||
|
||||
/*
|
||||
* 3.3.3.6 Attribute Value of Characteristic Aggregate Format
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t handle_list[1]; /* Handle list */
|
||||
} ble_access_attr_val_aggre_format_t;
|
||||
|
||||
/*
|
||||
* Vol 3 Part C 12.1 Attribute Value of Device Name Characteristic
|
||||
*/
|
||||
typedef struct {
|
||||
char device_name[1]; /* Maximum length is 248 bytes */
|
||||
} ble_access_attr_val_dev_name_t;
|
||||
|
||||
/*
|
||||
* Vol 3 Part C 12.2 Attribute Value of Appearance Characteristic
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t appearance; /* Enumerated value defined in "Assigned Numbers" */
|
||||
} ble_access_attr_val_appear_t;
|
||||
|
||||
/*
|
||||
* Vol 3 Part C 12.3 Attribute Value of Peripheral Privacy Flag Characteristic
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t periph_privacy_flag; /* Peripheral privacy flag: 0 if disabled; 1 if enabled */
|
||||
} ble_access_attr_val_privacy_flag_t;
|
||||
|
||||
/*
|
||||
* Vol 3 Part C 12.4 Attribute Value of Reconnection Address Characteristic
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t reconn_address[6]; /* Network-order reconnection address */
|
||||
} ble_access_attr_val_reconn_addr_t;
|
||||
|
||||
/*
|
||||
* Vol 3 Part C 12.5 Attribute Value of Peripheral Preferred Connection Parameters Characteristic
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t min_conn_interval; /* Minimum connection interval */
|
||||
uint16_t max_conn_interval; /* Maximum connection interval */
|
||||
uint16_t slave_latency; /* Slave latency */
|
||||
uint16_t conn_supervision_timeout_multiplier; /* Connection supervision timeout multiplier */
|
||||
} ble_access_attr_val_conn_parms_t;
|
||||
|
||||
/*
|
||||
* Attribute Structure
|
||||
*/
|
||||
typedef struct {
|
||||
void *next; // Not used.
|
||||
uint16_t handle; // attribute handle
|
||||
ble_access_uuid_t type; // attribute type
|
||||
uint8_t permission; // attribute permissions
|
||||
uint32_t value_length; // length of the attribute value. If no value, this equals 0
|
||||
uint32_t value_struct_size; // size of the value structure
|
||||
|
||||
/* Union of attribute values. Use the right format based on Attribute Type */
|
||||
union {
|
||||
/* Not support Long Value */
|
||||
uint8_t value[512];
|
||||
/* Attribute Value for Service */
|
||||
ble_access_attr_val_serv_t service;
|
||||
/* Attribute Value for Include */
|
||||
ble_access_attr_val_inc_t include;
|
||||
/* Attribute Value for Characteristic */
|
||||
ble_access_attr_val_char_t characteristic;
|
||||
/* Attribute Value for Characteristic Value */
|
||||
ble_access_attr_val_char_val_t characteristic_value;
|
||||
/* Attribute Value for Descriptor: Characteristic Extended Properties */
|
||||
ble_access_attr_val_ext_prop_t extended_properties;
|
||||
/* Attribute Value for Descriptor: Characteristic User_Description */
|
||||
ble_access_attr_val_usr_desc_t user_description;
|
||||
/* Attribute Value for Descriptor: Client Characteristic Configuration */
|
||||
ble_access_attr_val_cli_cfg_t client_config;
|
||||
/* Attribute Value for Descriptor: Server Characteristic Configuration */
|
||||
ble_access_attr_val_srv_cfg_t server_config;
|
||||
/* Attribute Value for Descriptor: Characteristic Presentation Format */
|
||||
ble_access_attr_val_pres_format_t presentation_format;
|
||||
/* Attribute Value for Descriptor: Characteristic Aggregate Format */
|
||||
ble_access_attr_val_aggre_format_t aggregate_format;
|
||||
/* Attribute Value for Characteristic Type: Device Name */
|
||||
ble_access_attr_val_dev_name_t device_name;
|
||||
/* Attribute Value for Characteristic Type: Appearance */
|
||||
ble_access_attr_val_appear_t appearance;
|
||||
/* Attribute Value for Characteristic Type: Peripheral Privacy Flag */
|
||||
ble_access_attr_val_privacy_flag_t periph_privacy_flag;
|
||||
/* Attribute Value for Characteristic Type: Reconnection Address */
|
||||
ble_access_attr_val_reconn_addr_t reconn_address;
|
||||
/* Attribute Value for Characteristic Type: Peripheral Preferred Connection Parameters */
|
||||
ble_access_attr_val_conn_parms_t periph_preferred_conn_params;
|
||||
} value;
|
||||
} ble_access_attribute_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
* Controller Command Response
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define BLE_ACCESS_EVENT_DEV_NEW 0x01 /* A new device is scanned. */
|
||||
#define BLE_ACCESS_EVENT_DEV_NEW_CMPL 0x02 /* The new device discovery procedure has completed. */
|
||||
#define BLE_ACCESS_EVENT_DEV_AUTHING 0x03 /* A new device is authing. */
|
||||
#define BLE_ACCESS_EVENT_DEV_ADD 0x04 /* A new device is or isn't added. */
|
||||
#define BLE_ACCESS_EVENT_DEV_CONN 0x05 /* A old device is re-connection */
|
||||
#define BLE_ACCESS_EVENT_DEV_DISC 0x06 /* A device request to disconnect */
|
||||
#define BLE_ACCESS_EVENT_GATT_CHAR 0x07 /* A characteristic event for ble_access_get_characteristics */
|
||||
#define BLE_ACCESS_EVENT_GATT_CHAR_CMPL 0x08 /* All characteristics event finished. */
|
||||
#define BLE_ACCESS_EVENT_GATT_NOTIFY 0x09 /* A notification from peer server */
|
||||
|
||||
/*
|
||||
* Used to descript a GATT Service Handle range and its type.
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t start_handle;
|
||||
uint16_t end_handle;
|
||||
ble_access_uuid_t type;
|
||||
} ble_access_serv_t;
|
||||
|
||||
/*
|
||||
* Event Handle Parameters
|
||||
*/
|
||||
typedef struct {
|
||||
/* Always valid */
|
||||
uint32_t device_id;
|
||||
uint8_t status;
|
||||
|
||||
union {
|
||||
/* BLE_ACCESS_BT_EVENT_DEV_ADD and BLE_ACCESS_BT_EVENT_DEV_CONN */
|
||||
struct {
|
||||
uint8_t serv_count;
|
||||
ble_access_serv_t *serv;
|
||||
} add;
|
||||
/* BLE_ACCESS_EVENT_CHAR and BLE_ACCESS_EVENT_GATT_NOTIFY */
|
||||
struct {
|
||||
ble_access_attribute_t *attr;
|
||||
} gatt;
|
||||
/* BLE_ACCESS_EVENT_DEV_NEW */
|
||||
struct {
|
||||
char *name;
|
||||
int8_t RSSI;
|
||||
} scan;
|
||||
/* BLE_ACCESS_EVENT_DEV_STATE */
|
||||
uint8_t state;
|
||||
} p;
|
||||
} ble_access_evt_parms_t;
|
||||
|
||||
/*
|
||||
* Event handle
|
||||
*/
|
||||
typedef void (*ble_access_event_callback_t)(uint8_t event, const ble_access_evt_parms_t *params);
|
||||
|
||||
/* Initialize Bluetooth Interface */
|
||||
extern OSStatus ble_access_bluetooth_init(void);
|
||||
|
||||
/* Start BLE Auto Connection Procedure */
|
||||
extern OSStatus ble_access_bluetooth_start(ble_access_event_callback_t callback);
|
||||
|
||||
/* Stop BLE Auto Connection Procedure */
|
||||
extern OSStatus ble_access_bluetooth_stop(void);
|
||||
|
||||
/* Send a command to Bluetooth Interface. */
|
||||
extern OSStatus ble_access_bluetooth_request(uint8_t request,
|
||||
const ble_access_cmd_parms_t *parms);
|
||||
|
||||
/*
|
||||
* Get an attribute of Characteristic with the UUID provided from the local attribute database.
|
||||
*
|
||||
* @param dev_id[in] The Peer Device ID.
|
||||
* @param serv[in] The GATT Service Handle Structure. (used start_handle & end_handle)
|
||||
* @param uuid[in] The Characteristic UUID.
|
||||
* @param attr[inout] This function calling result in attr->characteristic.
|
||||
*/
|
||||
extern OSStatus ble_access_get_characteritic_by_uuid(uint32_t dev_id,
|
||||
const ble_access_serv_t *serv,
|
||||
const ble_access_uuid_t *uuid,
|
||||
ble_access_attribute_t *attr);
|
||||
|
||||
/*
|
||||
* Find all characteristics for 'serv' from the local attribute database and generate
|
||||
* several EVENT 'BLE_ACCESS_EVENT_CHAR' and 'BLE_ACCESS_EVENT_CHAR_CMPL' to user application.
|
||||
*
|
||||
* @param dev_id[in] The Peer Device ID.
|
||||
* @param serv[in] The GATT Service Handle Structure. (used start_handle & end_handle)
|
||||
*/
|
||||
extern OSStatus ble_access_get_characteristics(uint32_t dev_id,
|
||||
const ble_access_serv_t *serv);
|
||||
|
||||
/*
|
||||
* Find and read attribute with the Handle provided from the Attribute Cache
|
||||
*
|
||||
* @param dev_id[in] The Peer Device ID.
|
||||
* @param handle[in] The Attribute Handle Value
|
||||
* @param attr[out] This function calling result. And user should make sure its is a valid pointer.
|
||||
* @param size[in] The 'attr' buffer size in bytes.
|
||||
*/
|
||||
extern OSStatus ble_access_get_attribute_by_handle(uint32_t dev_id,
|
||||
uint16_t handle,
|
||||
ble_access_attribute_t *attr);
|
||||
|
||||
/*
|
||||
* Update Characteristic Value in the Attribute To the server
|
||||
*
|
||||
* @param dev_id[in] The Peer device ID
|
||||
* @param handle[in] The characteristic value handle.
|
||||
* @param length[in] The lenght of Data to update characteristic value.
|
||||
* @param data[in] Point to the data to update characteristic value.
|
||||
*/
|
||||
extern OSStatus ble_access_update_characteristic_value(uint32_t dev_id, uint16_t handle, uint8_t length, uint8_t *data);
|
||||
|
||||
/*
|
||||
* Enable Characteristic Client Configuration Indication or Notification with the
|
||||
* attribute provided from the Attribute Cache
|
||||
*
|
||||
* @param dev_id[in] The Peer Device ID.
|
||||
* @param attr[int] This function calling result. And user should make sure its is a valid pointer.
|
||||
* @param notify[in] Notification(YES) or Indication(FALSE)
|
||||
*/
|
||||
extern OSStatus ble_access_enable_notification(uint32_t dev_id,
|
||||
const ble_access_attribute_t *attr,
|
||||
mico_bool_t notify);
|
||||
|
||||
/*
|
||||
* Compare two UUID type.
|
||||
*
|
||||
* @param uuid1[in]
|
||||
* @param uuid2[in]
|
||||
*
|
||||
* @return 0 if they are equal, otherwise non-zero.
|
||||
*/
|
||||
extern mico_bool_t ble_access_uuid_compare(const ble_access_uuid_t *uuid1, const ble_access_uuid_t *uuid2);
|
||||
|
||||
/*
|
||||
* Calculate device ID by Device Address
|
||||
*
|
||||
* @param addr[in] device address
|
||||
*
|
||||
* @return device ID or zero
|
||||
*/
|
||||
extern uint32_t ble_access_calculate_device_id(const mico_bt_device_address_t addr);
|
||||
|
||||
/*
|
||||
* Generate a device address by device ID
|
||||
*
|
||||
* @param addr[out] device address
|
||||
* @param device_id[in] device ID
|
||||
*
|
||||
* @return kNoErr if successful.
|
||||
*/
|
||||
extern OSStatus ble_access_generate_device_address(mico_bt_device_address_t addr, uint32_t device_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __BLE_ACCESS_CORE_H__ */
|
||||
648
mico-os/libraries/daemons/ble_access/ble_access_core_i.c
Normal file
648
mico-os/libraries/daemons/ble_access/ble_access_core_i.c
Normal file
@@ -0,0 +1,648 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ble_access_core_i.c
|
||||
* @author Jian Zhang
|
||||
* @version V1.2.1
|
||||
* @date 26-Dec-2016
|
||||
* @file BLE ACCESS Protocol Components
|
||||
* ******************************************************************************
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 MXCHIP Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is furnished
|
||||
* to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
******************************************************************************
|
||||
* BLE Vendor Specific Device
|
||||
*
|
||||
* Features demonstrated
|
||||
* - Implement BLE_ACCESS Center Protocol developed by MXCHIP on MiCOKit-3239.
|
||||
* - You should see details about this protocol on mico.io.
|
||||
*
|
||||
******************************************************************************
|
||||
**/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mico.h"
|
||||
#include "mico_bt.h"
|
||||
#include "mico_bt_cfg.h"
|
||||
#include "mico_bt_smart_interface.h"
|
||||
#include "mico_bt_smartbridge.h"
|
||||
#include "mico_bt_smartbridge_gatt.h"
|
||||
#include "sdpdefs.h"
|
||||
#include "gattdefs.h"
|
||||
|
||||
#include "StringUtils.h"
|
||||
#include "LinkListUtils.h"
|
||||
|
||||
#include "ble_access_core.h"
|
||||
#include "ble_access_core_i.h"
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Configurations & Constants
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Predefine Type
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Local Function Prototype
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Local Variables
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static ble_access_device_t ble_access_devices[MAX_CONCURRENT_CONNECTIONS];
|
||||
static mico_mutex_t ble_access_dev_mutex;
|
||||
|
||||
static linked_list_t ble_access_connecting_device_list;
|
||||
static mico_mutex_t ble_access_conn_dev_list_mutex;
|
||||
|
||||
static mico_worker_thread_t ble_access_worker_thread;
|
||||
static event_handler_t ble_access_timer_evt;
|
||||
|
||||
/* Prefix Address for valid device MAC Address */
|
||||
static uint8_t prefix_addr[][BLE_ACCESS_PREFIX_LEN] = {
|
||||
[0] = {
|
||||
BLE_ACCESS_PREFIX_D0BAE4
|
||||
},
|
||||
[1] = {
|
||||
BLE_ACCESS_PREFIX_C89346
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Global Function Definition
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* Worker thread
|
||||
*/
|
||||
|
||||
OSStatus ble_access_create_worker_thread(void)
|
||||
{
|
||||
return mico_rtos_create_worker_thread(&ble_access_worker_thread,
|
||||
MICO_DEFAULT_WORKER_PRIORITY,
|
||||
2048,
|
||||
20);
|
||||
}
|
||||
|
||||
OSStatus ble_access_send_aync_event(event_handler_t event_handle, void *arg)
|
||||
{
|
||||
return mico_rtos_send_asynchronous_event(&ble_access_worker_thread, event_handle, arg);
|
||||
}
|
||||
|
||||
/*
|
||||
* Timer Management
|
||||
*/
|
||||
|
||||
static void ble_access_timer_callback(void *arg)
|
||||
{
|
||||
if (ble_access_timer_evt) {
|
||||
ble_access_send_aync_event(ble_access_timer_evt, arg);
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus ble_access_start_timer(ble_access_device_t *dev, event_handler_t timer_event_handle, void *arg)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
require_action(dev != NULL && timer_event_handle != NULL, exit, err = kParamErr);
|
||||
|
||||
err = mico_rtos_init_timer(&dev->timer, 10000, ble_access_timer_callback, arg);
|
||||
require_noerr_string(err, exit, "Initialize a timer failed");
|
||||
|
||||
err = mico_rtos_start_timer(&dev->timer);
|
||||
require_noerr_action_string(err, exit, mico_rtos_deinit_timer(&dev->timer), "Start a timer failed");
|
||||
|
||||
ble_access_timer_evt = timer_event_handle;
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_stop_timer(ble_access_device_t *dev)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
require_action(dev != NULL, exit, err = kParamErr);
|
||||
|
||||
if (mico_rtos_is_timer_running(&dev->timer)) {
|
||||
mico_rtos_stop_timer(&dev->timer);
|
||||
}
|
||||
err = mico_rtos_deinit_timer(&dev->timer);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Local Device Pool Management
|
||||
*/
|
||||
|
||||
/* Initialize a devices pool */
|
||||
void ble_access_initialize_devices(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
uint8_t idx = 0;
|
||||
|
||||
for (idx = 0; idx < ble_access_array_size(ble_access_devices); idx++) {
|
||||
ble_access_devices[idx].used = FALSE;
|
||||
ble_access_devices[idx].device_id = 0;
|
||||
err = mico_bt_smartbridge_create_socket(&ble_access_devices[idx].socket);
|
||||
require_noerr_string(err, exit, "Create Sockets failed");
|
||||
}
|
||||
mico_rtos_init_mutex(&ble_access_dev_mutex);
|
||||
|
||||
exit:
|
||||
return;
|
||||
}
|
||||
|
||||
/* De-initialize a devices pool */
|
||||
void ble_access_deinit_devices(void)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
for (idx = 0; idx < ble_access_array_size(ble_access_devices); idx++) {
|
||||
mico_bt_smartbridge_delete_socket(&ble_access_devices[idx].socket);
|
||||
}
|
||||
mico_rtos_deinit_mutex(&ble_access_dev_mutex);
|
||||
}
|
||||
|
||||
ble_access_device_t *ble_access_find_device_by_address(const mico_bt_device_address_t address)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
ble_access_device_t *dev = NULL;
|
||||
uint32_t device_id = ble_access_calculate_device_id(address);
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_dev_mutex);
|
||||
for (idx = 0; idx < ble_access_array_size(ble_access_devices); idx++) {
|
||||
if (ble_access_devices[idx].used && device_id == ble_access_devices[idx].device_id) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (idx < ble_access_array_size(ble_access_devices)) {
|
||||
dev = &ble_access_devices[idx];
|
||||
}
|
||||
mico_rtos_unlock_mutex(&ble_access_dev_mutex);
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
/* Get a free socket from 'sockets' */
|
||||
ble_access_device_t *ble_access_get_free_device(void)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_dev_mutex);
|
||||
for (idx = 0; idx < ble_access_array_size(ble_access_devices); idx++) {
|
||||
if (!ble_access_devices[idx].used) {
|
||||
ble_access_devices[idx].used = TRUE;
|
||||
mico_rtos_unlock_mutex(&ble_access_dev_mutex);
|
||||
return &ble_access_devices[idx];
|
||||
}
|
||||
}
|
||||
mico_rtos_unlock_mutex(&ble_access_dev_mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ble_access_release_device(mico_bool_t free, const ble_access_device_t *device)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_dev_mutex);
|
||||
if (device != NULL) {
|
||||
for (idx = 0;
|
||||
idx < ble_access_array_size(ble_access_devices) && device != &ble_access_devices[idx];
|
||||
idx++);
|
||||
if (idx < ble_access_array_size(ble_access_devices) && free) {
|
||||
ble_access_devices[idx].device_id = 0;
|
||||
ble_access_devices[idx].used = FALSE;
|
||||
}
|
||||
}
|
||||
mico_rtos_unlock_mutex(&ble_access_dev_mutex);
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a BT device address by device_id and prefix.
|
||||
*
|
||||
* @param[output] addr - device address
|
||||
* @param[input] prefix_addr - a prefix address array.
|
||||
* @param[input] device_id - a device id.
|
||||
*
|
||||
* addr[0 - 2] = prefix_addr[0 - 2]
|
||||
* addr[3] = device_id[bit23:bit16]
|
||||
* addr[4] = device_id[bit15:bit8]
|
||||
* addr[5] = device_id[bit7:bit0]
|
||||
*
|
||||
* example:
|
||||
* prefix - { 0x20, 0x73, 0x6a }, device_id = 0x12112233,
|
||||
* ---->
|
||||
* addr = { 0x20, 0x73, 0x6a, 0x11, 0x22, 0x33 }
|
||||
*/
|
||||
OSStatus ble_access_generate_device_address(mico_bt_device_address_t addr, uint32_t device_id)
|
||||
{
|
||||
uint8_t idx_addr_type = 0;
|
||||
|
||||
/* Check Parameters */
|
||||
if (addr == NULL) {
|
||||
return kParamErr;
|
||||
}
|
||||
|
||||
idx_addr_type = (uint8_t)((device_id & 0xff000000) >> 24);
|
||||
if (idx_addr_type >= ble_access_array_size(prefix_addr)) {
|
||||
return kParamErr;
|
||||
}
|
||||
|
||||
/* Fill prefix address part */
|
||||
memcpy(addr, prefix_addr[idx_addr_type], 3);
|
||||
/* Fill the device id part (little endian) */
|
||||
addr[3] = (uint8_t)((device_id & 0x00ffffff) >> 16);
|
||||
addr[4] = (uint8_t)((device_id & 0x00ffffff) >> 8);
|
||||
addr[5] = (uint8_t)((device_id & 0x00ffffff));
|
||||
return kNoErr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate the device ID according device address.
|
||||
*
|
||||
* @param[input] addr - the device address
|
||||
*
|
||||
* return device ID.
|
||||
*
|
||||
* example:
|
||||
* addr = { 0x20, 0x73, 0x6a, 0x11, 0x22, 0x33 };
|
||||
* so, device_id = 0x112233.
|
||||
*/
|
||||
uint32_t ble_access_calculate_device_id(const mico_bt_device_address_t addr)
|
||||
{
|
||||
uint32_t id = 0x00000000;
|
||||
uint8_t idx;
|
||||
|
||||
/* check parameters */
|
||||
if (addr == NULL) {
|
||||
/* No allow device address. */
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for (idx = 0; idx < ble_access_array_size(prefix_addr); idx++) {
|
||||
if (memcmp(&addr[0], prefix_addr[idx], 3) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (idx >= ble_access_array_size(prefix_addr)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Calculate Device ID. */
|
||||
id |= (((uint32_t)idx) << 24);
|
||||
id |= (((uint32_t)addr[3]) << 16);
|
||||
id |= (((uint32_t)addr[4]) << 8);
|
||||
id |= (((uint32_t)addr[5]));
|
||||
|
||||
exit:
|
||||
return id;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get and parse manufacturer data from the Peer Device Advertisement data.
|
||||
*/
|
||||
OSStatus ble_access_get_manufactor_adv_data(uint8_t *eir_data,
|
||||
uint8_t eir_data_length,
|
||||
ble_access_manufactor_data_t *manufactor_data)
|
||||
{
|
||||
if (eir_data == NULL || eir_data_length < 14 || manufactor_data == NULL) {
|
||||
return kParamErr;
|
||||
}
|
||||
|
||||
/* Manufacture data
|
||||
*
|
||||
* ------------------------------------------------------------------------------------
|
||||
* | MXCHIP (6 bytes) | advType (1 byte) | directAddr (6 bytes) | alertState (1 byte) |
|
||||
* ------------------------------------------------------------------------------------
|
||||
*
|
||||
* advType: MXCHIP_ADV_TYPE_INIT or MXCHIP_ADV_TYPE_RECONN
|
||||
* directAddr: Directing device address. (used in MXCHIP_ADV_TYPE_RECONN)
|
||||
* alertState: Alert state. 00 or 01
|
||||
*
|
||||
*/
|
||||
memcpy(manufactor_data->mxchip, &eir_data[0], 6);
|
||||
manufactor_data->adv_type = eir_data[6];
|
||||
manufactor_data->alert_state = eir_data[13];
|
||||
memcpy(manufactor_data->direct_addr, &eir_data[7], 6);
|
||||
return kNoErr;
|
||||
}
|
||||
|
||||
/* Check wheter a ADV packet is valid type */
|
||||
OSStatus ble_access_check_adv_type(const uint8_t *adv_data,
|
||||
uint8_t length,
|
||||
uint8_t adv_type,
|
||||
ble_access_manufactor_data_t *manu_data)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
uint8_t *packet = NULL;
|
||||
uint8_t data_length = 0;
|
||||
ble_access_manufactor_data_t manufactor_data;
|
||||
|
||||
if (adv_data == NULL
|
||||
|| length == 0
|
||||
|| (adv_type != BLE_ACCESS_ADV_TYPE_INIT && adv_type != BLE_ACCESS_ADV_TYPE_RECONN)) {
|
||||
return MICO_FALSE;
|
||||
}
|
||||
|
||||
packet = mico_bt_ble_check_advertising_data((uint8_t *)adv_data,
|
||||
BTM_BLE_ADVERT_TYPE_MANUFACTURER,
|
||||
&data_length);
|
||||
if (packet == NULL || data_length == 0) {
|
||||
err = kUnknownErr;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
err = ble_access_get_manufactor_adv_data(packet,
|
||||
data_length,
|
||||
&manufactor_data);
|
||||
if (err != kNoErr) goto exit;
|
||||
|
||||
if (memcmp(manufactor_data.mxchip, BLE_ACCESS_MXCHIP_FLAG, 6) == 0) {
|
||||
if (manufactor_data.adv_type != adv_type) {
|
||||
err = kParamErr;
|
||||
} else {
|
||||
if (manu_data != NULL) {
|
||||
memcpy(manu_data, &manufactor_data, sizeof(ble_access_manufactor_data_t));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = kGeneralErr;
|
||||
}
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
void ble_access_set_scan_cfg(mico_bt_smart_scan_settings_t *scan_cfg, mico_bool_t is_auto_scanning)
|
||||
{
|
||||
require_string(scan_cfg != NULL, exit, "invalid parameters");
|
||||
|
||||
if (is_auto_scanning) {
|
||||
scan_cfg->interval = 2048;
|
||||
scan_cfg->window = 48;
|
||||
scan_cfg->duration_second = 0xffff;
|
||||
scan_cfg->type = BT_SMART_PASSIVE_SCAN;
|
||||
scan_cfg->filter_policy = FILTER_POLICY_WHITE_LIST;
|
||||
scan_cfg->filter_duplicates = DUPLICATES_FILTER_DISABLED;
|
||||
} else {
|
||||
scan_cfg->interval = 256;
|
||||
scan_cfg->window = 48;
|
||||
scan_cfg->duration_second = 30;
|
||||
scan_cfg->type = BT_SMART_ACTIVE_SCAN;
|
||||
scan_cfg->filter_policy = FILTER_POLICY_NONE;
|
||||
scan_cfg->filter_duplicates = DUPLICATES_FILTER_DISABLED;
|
||||
}
|
||||
|
||||
exit:
|
||||
return;
|
||||
}
|
||||
|
||||
mico_bool_t ble_access_uuid_compare(const ble_access_uuid_t *uuid1, const ble_access_uuid_t *uuid2)
|
||||
{
|
||||
if (!uuid1 || !uuid2) {
|
||||
return MICO_FALSE;
|
||||
}
|
||||
|
||||
if (uuid1->len == uuid2->len
|
||||
&& memcmp(&uuid1->uu, &uuid2->uu, uuid1->len) == 0) {
|
||||
|
||||
return MICO_TRUE;
|
||||
}
|
||||
|
||||
return MICO_FALSE;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_init( void )
|
||||
{
|
||||
mico_rtos_init_mutex(&ble_access_conn_dev_list_mutex);
|
||||
return linked_list_init(&ble_access_connecting_device_list);
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_deinit(void)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
mico_bt_smart_device_t *dev = NULL;
|
||||
|
||||
for(; ;) {
|
||||
err = ble_access_connect_list_get(&dev, NULL);
|
||||
if (err == kNoErr) {
|
||||
ble_access_connect_list_remove(dev);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return mico_rtos_deinit_mutex(&ble_access_conn_dev_list_mutex);
|
||||
}
|
||||
|
||||
mico_bool_t compare_device_by_address(linked_list_node_t* node_to_compare, void* user_data)
|
||||
{
|
||||
ble_access_connecting_device_t* device = (ble_access_connecting_device_t* )node_to_compare;
|
||||
mico_bt_device_address_t* device_address = (mico_bt_device_address_t *)user_data;
|
||||
|
||||
if (memcmp(device->device.address, device_address, BD_ADDR_LEN) == 0) {
|
||||
return TRUE;
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_add(const mico_bt_smart_device_t *remote_device, mico_bool_t is_reported)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t *device_found, *new_device;
|
||||
|
||||
require_action(remote_device != NULL, exit, err = kParamErr);
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_find_node(&ble_access_connecting_device_list,
|
||||
(linked_list_compare_callback_t)compare_device_by_address,
|
||||
(void *)remote_device->address,
|
||||
(linked_list_node_t**)&device_found);
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
|
||||
if (err != kNotFoundErr) {
|
||||
err = kAlreadyInUseErr;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
new_device = malloc(sizeof(ble_access_device_t));
|
||||
if (!new_device) {
|
||||
err = kNoMemoryErr;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
new_device->reported = is_reported;
|
||||
memcpy(&new_device->device, remote_device, sizeof(mico_bt_smart_device_t));
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_insert_node_at_rear(&ble_access_connecting_device_list, &new_device->this_node);
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_set_report(const mico_bt_smart_device_t *device, mico_bool_t is_reported)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t *device_found;
|
||||
|
||||
require_action(device != NULL, exit, err = kParamErr);
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_find_node(&ble_access_connecting_device_list,
|
||||
(linked_list_compare_callback_t)compare_device_by_address,
|
||||
(void *)device->address,
|
||||
(linked_list_node_t **)&device_found);
|
||||
if (err == kNoErr) {
|
||||
device_found->reported = is_reported;
|
||||
}
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_get(mico_bt_smart_device_t** device, mico_bool_t *reported)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t* current_device;
|
||||
|
||||
require_action(device != NULL, exit, err = kParamErr);
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_get_front_node(&ble_access_connecting_device_list, (linked_list_node_t **)¤t_device);
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
|
||||
if (err != kNoErr) goto exit;
|
||||
|
||||
*device = ¤t_device->device;
|
||||
if (reported) *reported = current_device->reported;
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_get_by_address(mico_bt_smart_device_t **device, mico_bool_t *reported, const mico_bt_device_address_t address)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t *current_device;
|
||||
|
||||
require_action(device != NULL && address != NULL, exit, err = kParamErr);
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_find_node(&ble_access_connecting_device_list,
|
||||
(linked_list_compare_callback_t)compare_device_by_address,
|
||||
(void *)address,
|
||||
(linked_list_node_t **)¤t_device);
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
|
||||
if (err != kNoErr) goto exit;
|
||||
|
||||
*device = ¤t_device->device;
|
||||
if (reported) *reported = current_device->reported;
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_find_by_address(const mico_bt_device_address_t address)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t *current_device;
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_find_node(&ble_access_connecting_device_list,
|
||||
(linked_list_compare_callback_t)compare_device_by_address,
|
||||
(void *)address,
|
||||
(linked_list_node_t **)¤t_device);
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ble_access_connect_list_remove(mico_bt_smart_device_t *device)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
ble_access_connecting_device_t* current_device;
|
||||
|
||||
mico_rtos_lock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
err = linked_list_find_node(&ble_access_connecting_device_list,
|
||||
(linked_list_compare_callback_t)compare_device_by_address,
|
||||
device->address,
|
||||
(linked_list_node_t**)¤t_device);
|
||||
if (err != kNoErr) goto exit;
|
||||
|
||||
err = linked_list_remove_node(&ble_access_connecting_device_list, ¤t_device->this_node);
|
||||
if (err != kNoErr) goto exit;
|
||||
|
||||
free(current_device);
|
||||
exit:
|
||||
mico_rtos_unlock_mutex(&ble_access_conn_dev_list_mutex);
|
||||
return err;
|
||||
}
|
||||
|
||||
const char *print_request_str(uint8_t request)
|
||||
{
|
||||
switch (request) {
|
||||
case BLE_ACCESS_REQ_DEV_SCAN:
|
||||
return "SCAN_REQ";
|
||||
case BLE_ACCESS_REQ_DEV_ADD:
|
||||
return "ADD_REQ";
|
||||
case BLE_ACCESS_REQ_DEV_DISC:
|
||||
return "DISC_REQ";
|
||||
case BLE_ACCESS_REQ_DEV_REMOVE:
|
||||
return "REMOVE_REQ";
|
||||
case BLE_ACCESS_REQ_DEV_START_AUTO:
|
||||
return "START_AUTO_REQ";
|
||||
case BLE_ACCESS_REQ_DEV_STOP_AUTO:
|
||||
return "STOP_AUTO_REQ";
|
||||
default:
|
||||
return "Unknown request";
|
||||
}
|
||||
}
|
||||
235
mico-os/libraries/daemons/ble_access/ble_access_core_i.h
Normal file
235
mico-os/libraries/daemons/ble_access/ble_access_core_i.h
Normal file
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file ble_access_core_i.h
|
||||
* @author Jian Zhang
|
||||
* @version V1.2.1
|
||||
* @date 26-Dec-2016
|
||||
* @file BLE ACCESS Protocol Components
|
||||
* ******************************************************************************
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 MXCHIP Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is furnished
|
||||
* to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
******************************************************************************
|
||||
* BLE Vendor Specific Device
|
||||
*
|
||||
* Features demonstrated
|
||||
* - Implement BLE_ACCESS Center Protocol developed by MXCHIP on MiCOKit-3239.
|
||||
* - You should see details about this protocol on mico.io.
|
||||
*
|
||||
******************************************************************************
|
||||
**/
|
||||
|
||||
#ifndef __BLE_ACCESS_CORE_I_H__
|
||||
#define __BLE_ACCESS_CORE_I_H__
|
||||
|
||||
#include "mico.h"
|
||||
#include "mico_bt.h"
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Configurations & Constants
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* Debug Logout */
|
||||
#define BLE_ACCESS_DEBUG 0
|
||||
|
||||
/* The maximum number of remote connections */
|
||||
#define MAX_CONCURRENT_CONNECTIONS 10
|
||||
|
||||
/* Protocal Flags */
|
||||
#define BLE_ACCESS_MXCHIP_FLAG "MXCHIP"
|
||||
|
||||
/*
|
||||
* Array index of Command Packet
|
||||
*
|
||||
* ----------------------------------------
|
||||
* | code | count | length | len1 | data1 |
|
||||
* ----------------------------------------
|
||||
*
|
||||
*/
|
||||
#define BLE_ACCESS_AUTH_IDX_CODE 0x00
|
||||
#define BLE_ACCESS_AUTH_IDX_COUNT 0x01
|
||||
#define BLE_ACCESS_AUTH_IDX_LENGTH 0x02
|
||||
#define BLE_ACCESS_AUTH_IDX_PARM1_LEN 0x04
|
||||
#define BLE_ACCESS_AUTH_IDX_PARM1_DAT 0x05
|
||||
|
||||
/*
|
||||
* Code Value
|
||||
*/
|
||||
#define BLE_ACCESS_CODE_START_AUTH 0x11
|
||||
#define BLE_ACCESS_CODE_AUTH_DATA 0x12
|
||||
#define BLE_ACCESS_CODE_AUTH_RESULT 0x13
|
||||
|
||||
/*
|
||||
* Res Value
|
||||
*/
|
||||
#define BLE_ACCESS_RES_START 0x21
|
||||
#define BLE_ACCESS_RES_AUTH 0x22
|
||||
|
||||
#define BLE_ACCESS_CODE_DISC 0x31
|
||||
#define BLE_ACCESS_RES_DISC 0x41
|
||||
|
||||
/* Response to Peer */
|
||||
//#define BLE_ACCESS_RSP_ALLOW_CONNECT 0x01
|
||||
//#define BLE_ACCESS_RSP_NOT_ALLOW_CONNECT 0x02
|
||||
/* Alert code to peer */
|
||||
#define BLE_ACCESS_RSP_ALERTING 0x03
|
||||
|
||||
/* Slave command to authentication */
|
||||
#define BLE_ACCESS_SLAVE_CMD_AUTH_START 0x11
|
||||
#define BLE_ACCESS_SLAVE_CMD_AUTH_DATA 0x12
|
||||
#define BLE_ACCESS_SLAVE_CMD_AUTH_RESULT 0x13
|
||||
|
||||
/* Master response to authentication */
|
||||
#define BLE_ACCESS_MASTER_RSP_AUTH_ACK 0x21
|
||||
#define BLE_ACCESS_MASTER_RSP_AUTH_DATA 0x22
|
||||
|
||||
/* EVENT Error Code */
|
||||
#define BLE_ACCESS_ERR_NO_ERR 0x00
|
||||
#define BLE_ACCESS_ERR_ENCRY_FAILED 0x01
|
||||
#define BLE_ACCESS_ERR_BUSY 0x02
|
||||
#define BLE_ACCESS_ERR_AUTH_FAILED 0x03
|
||||
#define BLE_ACCESS_ERR_TIMEOUT 0x04
|
||||
#define BLE_ACCESS_ERR_REJECT 0x05
|
||||
|
||||
#define BLE_ACCESS_ERR_ALERTING 0x10
|
||||
#define BLE_ACCESS_ERR_NO_ALERT 0x11
|
||||
|
||||
/* Control Point Service */
|
||||
// {7A414C35-605A-4319-AB3C-E945A9A42A70}
|
||||
#define UUID_MXCHIP_CONTROL_POINT_SERVICE 0x70,0x2a,0xa4,0xa9,0x45,0xe9,0x3c,0xab,0x19,0x43,0x5a,0x60,0x35,0x4c,0x41,0x7a
|
||||
// {B1CADCA2-9E80-4D24-9313-A45125A5F846}
|
||||
#define UUID_MXCHIP_CONTROL_POINT_CMD_CHAR 0x46,0xf8,0xa5,0x25,0x51,0xa4,0x13,0x93,0x24,0x4d,0x80,0x9e,0xa2,0xdc,0xca,0xb1
|
||||
// {E36945B5-30EE-4ABD-9445-4122CB40D175}
|
||||
#define UUID_MXCHIP_CONTROL_POINT_EVT_CHAR 0x75,0xd1,0x40,0xcb,0x22,0x41,0x45,0x94,0xbd,0x4a,0xee,0x30,0xb5,0x45,0x69,0xe3
|
||||
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Predefine Type
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* Device info ready to connect */
|
||||
typedef struct {
|
||||
linked_list_node_t this_node; /* Linked-list node of this device */
|
||||
mico_bt_smart_device_t device; /* Remote BT device */
|
||||
mico_bool_t reported; /* Is reported? */
|
||||
} ble_access_connecting_device_t;
|
||||
|
||||
/* The Peer device Advertisement data */
|
||||
typedef struct {
|
||||
uint8_t mxchip[6];
|
||||
uint8_t adv_type;
|
||||
uint8_t alert_state;
|
||||
mico_bt_device_address_t direct_addr;
|
||||
} ble_access_manufactor_data_t;
|
||||
|
||||
/*
|
||||
* A BLE Access Core BLE Device
|
||||
*/
|
||||
typedef struct {
|
||||
// Device ID
|
||||
uint32_t device_id;
|
||||
// BT Socket for Every BLE Connection
|
||||
mico_bt_smartbridge_socket_t socket;
|
||||
// A timer for Authentication Process
|
||||
mico_timer_t timer;
|
||||
// Authentication State
|
||||
uint8_t auth_state;
|
||||
// Is used ?
|
||||
mico_bool_t used;
|
||||
// Some handles for a service of a BLE Connection.
|
||||
struct {
|
||||
// Notify Characteristic Value handle in MXCHIP Service
|
||||
uint16_t notify_char_value_handle;
|
||||
// Control Point Characteristic Value handle in MXCHIP Service.
|
||||
uint16_t ctrl_evt_char_value_handle;
|
||||
} service;
|
||||
} ble_access_device_t;
|
||||
|
||||
/*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* Local Function Prototype
|
||||
*
|
||||
*-------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* Log output */
|
||||
#define ble_access_log(M, ...) custom_log("BLE_ACCESS", M, ##__VA_ARGS__)
|
||||
|
||||
/* Calculate An array size */
|
||||
#define ble_access_array_size(array) (sizeof(array)/sizeof(array[0]))
|
||||
|
||||
/*
|
||||
* Remote device list management
|
||||
*/
|
||||
extern OSStatus ble_access_connect_list_init (void);
|
||||
extern OSStatus ble_access_connect_list_deinit (void);
|
||||
|
||||
extern OSStatus ble_access_connect_list_add (const mico_bt_smart_device_t *remote_device, mico_bool_t is_reported);
|
||||
extern OSStatus ble_access_connect_list_get (mico_bt_smart_device_t **address, mico_bool_t *reported);
|
||||
extern OSStatus ble_access_connect_list_get_by_address (mico_bt_smart_device_t **device, mico_bool_t *reported, const mico_bt_device_address_t address);
|
||||
extern OSStatus ble_access_connect_list_find_by_address(const mico_bt_device_address_t address);
|
||||
extern OSStatus ble_access_connect_list_remove (mico_bt_smart_device_t* device);
|
||||
extern OSStatus ble_access_connect_list_set_report (const mico_bt_smart_device_t *device, mico_bool_t is_reported);
|
||||
|
||||
/*
|
||||
* Utils function
|
||||
*/
|
||||
|
||||
const char *print_request_str(uint8_t request);
|
||||
|
||||
extern OSStatus ble_access_get_manufactor_adv_data(uint8_t *eir_data,
|
||||
uint8_t eir_data_length,
|
||||
ble_access_manufactor_data_t *manufactor_data);
|
||||
|
||||
extern OSStatus ble_access_check_adv_type (const uint8_t *adv_data,
|
||||
uint8_t length,
|
||||
uint8_t adv_type,
|
||||
ble_access_manufactor_data_t *manu_data);
|
||||
|
||||
extern void ble_access_set_scan_cfg (mico_bt_smart_scan_settings_t *scan_cfg,
|
||||
mico_bool_t is_auto_scanning);
|
||||
|
||||
extern OSStatus ble_access_start_timer (ble_access_device_t *dev, event_handler_t timer_event_handle, void *arg);
|
||||
extern OSStatus ble_access_stop_timer (ble_access_device_t *dev);
|
||||
|
||||
/*
|
||||
* Local Devices Pool
|
||||
*/
|
||||
extern void ble_access_initialize_devices (void);
|
||||
extern void ble_access_deinit_devices (void);
|
||||
extern ble_access_device_t *ble_access_get_free_device (void);
|
||||
extern void ble_access_release_device (mico_bool_t free, const ble_access_device_t *device);
|
||||
//extern ble_access_device_t *ble_access_find_device_by_socket (const mico_bt_smartbridge_socket_t *socket);
|
||||
extern ble_access_device_t *ble_access_find_device_by_address (const mico_bt_device_address_t address);
|
||||
|
||||
extern OSStatus ble_access_create_worker_thread(void);
|
||||
extern OSStatus ble_access_send_aync_event(event_handler_t event_handle, void *arg);
|
||||
|
||||
#endif /* __BLE_ACCESS_CORE_I_H__ */
|
||||
Reference in New Issue
Block a user