mirror of
https://github.com/Ai-Thinker-Open/Ai-Thinker-Open_RTL8710BX_ALIOS_SDK.git
synced 2026-07-06 02:45:37 +00:00
rel_1.6.0 init
This commit is contained in:
commit
27b3e2883d
19359 changed files with 8093121 additions and 0 deletions
|
|
@ -0,0 +1,67 @@
|
|||
#ifndef _BLE_ADV_DEFS_
|
||||
#define _BLE_ADV_DEFS_
|
||||
|
||||
/* EIR/AD data type definitions */
|
||||
#define EIRADV_DATA_FLAGS 0x01 /* AD flags */
|
||||
#define EIRADV_DATA_UUID16_SOME 0x02 /* 16-bit UUID, more available */
|
||||
#define EIRADV_DATA_UUID16_ALL 0x03 /* 16-bit UUID, all listed */
|
||||
#define EIRADV_DATA_UUID32_SOME 0x04 /* 32-bit UUID, more available */
|
||||
#define EIRADV_DATA_UUID32_ALL 0x05 /* 32-bit UUID, all listed */
|
||||
#define EIRADV_DATA_UUID128_SOME 0x06 /* 128-bit UUID, more available */
|
||||
#define EIRADV_DATA_UUID128_ALL 0x07 /* 128-bit UUID, all listed */
|
||||
#define EIRADV_DATA_NAME_SHORTENED 0x08 /* Shortened name */
|
||||
#define EIRADV_DATA_NAME_COMPLETE 0x09 /* Complete name */
|
||||
#define EIRADV_DATA_TX_POWER 0x0a /* Tx Power */
|
||||
#define EIRADV_DATA_SOLICIT16 0x14 /* Solicit UUIDs, 16-bit */
|
||||
#define EIRADV_DATA_SOLICIT128 0x15 /* Solicit UUIDs, 128-bit */
|
||||
#define EIRADV_DATA_SVC_DATA16 0x16 /* Service data, 16-bit UUID */
|
||||
#define EIRADV_DATA_GAP_APPEARANCE 0x19 /* GAP appearance */
|
||||
#define EIRADV_DATA_SOLICIT32 0x1f /* Solicit UUIDs, 32-bit */
|
||||
#define EIRADV_DATA_SVC_DATA32 0x20 /* Service data, 32-bit UUID */
|
||||
#define EIRADV_DATA_SVC_DATA128 0x21 /* Service data, 128-bit UUID */
|
||||
#define EIRADV_DATA_MESH_PROV 0x29 /* Mesh Provisioning PDU */
|
||||
#define EIRADV_DATA_MESH_MESSAGE 0x2a /* Mesh Networking PDU */
|
||||
#define EIRADV_DATA_MESH_BEACON 0x2b /* Mesh Beacon */
|
||||
#define EIRADV_DATA_MANUFACTURER_DATA 0xff /* Manufacturer Specific Data */
|
||||
|
||||
#define AD_FLAG_LIMITED 0x01 /* Limited Discoverable */
|
||||
#define AD_FLAG_GENERAL 0x02 /* General Discoverable */
|
||||
#define AD_FLAG_NO_BREDR 0x04 /* BR/EDR not supported */
|
||||
|
||||
/**
|
||||
* This structure holds the advertisement data.
|
||||
*
|
||||
* Note: The final advertisement data may consist of
|
||||
* multiple elements of this type.
|
||||
*/
|
||||
struct adv_data {
|
||||
uint8_t type;
|
||||
uint8_t data_len;
|
||||
const uint8_t *data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper to declare elements of adv_data arrays
|
||||
*
|
||||
* @param[in] _type Type of advertising data field
|
||||
* @param[in] _data Pointer to the data field payload
|
||||
* @param[in] _data_len Number of bytes behind the _data pointer
|
||||
*/
|
||||
#define ADV_DATA(_type, _data, _data_len) \
|
||||
{ \
|
||||
.type = (_type), \
|
||||
.data_len = (_data_len), \
|
||||
.data = (const uint8_t *)(_data), \
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to declare elements of adv_data arrays
|
||||
*
|
||||
* @param[in] _type Type of advertising data field
|
||||
* @param[in] _bytes Variable number of single-byte parameters
|
||||
*/
|
||||
#define ADV_DATA_BYTES(_type, _bytes...) \
|
||||
ADV_DATA(_type, ((uint8_t []) { _bytes }), \
|
||||
sizeof((uint8_t []) { _bytes }))
|
||||
|
||||
#endif
|
||||
1101
Living_SDK/framework/bluetooth/ble_app_framework/ble_app_framework.c
Normal file
1101
Living_SDK/framework/bluetooth/ble_app_framework/ble_app_framework.c
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _BLE_APP_FRAMEWORK_H_
|
||||
#define _BLE_APP_FRAMEWORK_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include "ble_app_framework_def.h"
|
||||
|
||||
/**
|
||||
* Initialize peripheral device.
|
||||
* This functions will initialize the BLE stack and peripheral,
|
||||
* as well as build up a GATT database.
|
||||
*
|
||||
* @param[in] p The parameters used to initialze the stack
|
||||
* and peripheral.
|
||||
* @param[in] c The callback function which will be called when
|
||||
* client device connected.
|
||||
* @param[in] disc The callback function which will be called when
|
||||
* client device disconnected.
|
||||
* @param[in] gatt_db The GATT database which is to be built.
|
||||
* @param[in] db_len The size of the GATT database.
|
||||
*
|
||||
* @return peripheral_hdl_t The peripheral device handle.
|
||||
*/
|
||||
peripheral_hdl_t
|
||||
ble_peripheral_init
|
||||
(
|
||||
peripheral_init_t *p,
|
||||
ble_peripheral_conn_cb_t c,
|
||||
ble_peripheral_disconn_cb_t disc,
|
||||
const uint8_t *gatt_db,
|
||||
int db_len
|
||||
);
|
||||
|
||||
/**
|
||||
* De-initialize the perripheral device.
|
||||
*
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
*/
|
||||
void ble_peripheral_deinit
|
||||
(
|
||||
peripheral_hdl_t hdl
|
||||
);
|
||||
|
||||
/**
|
||||
* Start the advertisement.
|
||||
*
|
||||
* @param[in] adv_handler The functio to be called when the
|
||||
advertisement completed.
|
||||
* @param[in] manufacture The string of the manufacture name.
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
*/
|
||||
void ble_adv_start
|
||||
(
|
||||
ble_adv_complete_cb_t adv_handler,
|
||||
const char *manufacture,
|
||||
peripheral_hdl_t hdl
|
||||
);
|
||||
|
||||
/**
|
||||
* Stop the advertisement.
|
||||
*/
|
||||
void ble_adv_stop();
|
||||
|
||||
/**
|
||||
* Add an GATT attribute.
|
||||
*
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
* @param[in] val_len The length of the attribute value in bytes.
|
||||
* @param[in] val The value of the attribute.
|
||||
*
|
||||
* @return ble_gatt_attr_t The GATT attribute structure.
|
||||
*/
|
||||
ble_gatt_attr_t *
|
||||
ble_attr_add
|
||||
(
|
||||
uint16_t hdl,
|
||||
uint16_t val_len,
|
||||
const uint8_t *val
|
||||
);
|
||||
|
||||
/**
|
||||
* Send indication of a GATT attribute value to client device.
|
||||
*
|
||||
* @param[in] attr The attribute structure.
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
* @param[in] len The length of the data to indicate.
|
||||
* @param[in] data The data to indicate.
|
||||
*/
|
||||
void ble_attr_indicate
|
||||
(
|
||||
ble_gatt_attr_t *attr,
|
||||
peripheral_hdl_t hdl,
|
||||
uint16_t len,
|
||||
const uint8_t *data
|
||||
);
|
||||
|
||||
/**
|
||||
* Send notification of a GATT attribute value to client device.
|
||||
*
|
||||
* @param[in] attr The attribute structure.
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
* @param[in] len The length of the data to nofity.
|
||||
* @param[in] data The data to notify.
|
||||
*/
|
||||
void ble_attr_notify
|
||||
(
|
||||
ble_gatt_attr_t *attr,
|
||||
peripheral_hdl_t hdl,
|
||||
uint16_t len,
|
||||
const uint8_t *data
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the advertisment data. This step is optional to user.
|
||||
* The default adv data will be used if adv data is not set explicitly
|
||||
* set by calling this func.
|
||||
*
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
* @param[in] ad The array of the adv data.
|
||||
* @param[in] ad_siz The number of elements in the ad data array.
|
||||
*/
|
||||
void ble_set_ad_data
|
||||
(
|
||||
peripheral_hdl_t hdl,
|
||||
const struct adv_data *ad,
|
||||
size_t ad_siz
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the scan response data. This step is optional to user.
|
||||
* The default sd data will be used if sd data is not set explicitly
|
||||
* set by calling this func.
|
||||
*
|
||||
* @param[in] hdl The peripheral device handle.
|
||||
* @param[in] sd The array of the scan response data.
|
||||
* @param[in] sd_siz The number of elements in the sd data array.
|
||||
*/
|
||||
void ble_set_sd_data
|
||||
(
|
||||
peripheral_hdl_t hdl,
|
||||
const struct adv_data *sd,
|
||||
size_t sd_siz
|
||||
);
|
||||
#endif
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
NAME := ble_app_framework
|
||||
|
||||
ifneq ($(no_ble_app_framework),1)
|
||||
$(NAME)_SOURCES := ble_app_framework.c
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += .
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _BLE_APP_FRAMEWORK_DEF_H_
|
||||
#define _BLE_APP_FRAMEWORK_DEF_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "ble_gatt_defs.h"
|
||||
#include "ble_adv_defs.h"
|
||||
|
||||
#ifndef UUID_SERVCLASS_GATT_SERVER
|
||||
#define UUID_SERVCLASS_GATT_SERVER 0x1801
|
||||
#endif
|
||||
#ifndef UUID_SERVCLASS_GAP_SERVER
|
||||
#define UUID_SERVCLASS_GAP_SERVER 0x1800
|
||||
#endif
|
||||
#ifndef UUID_SERVCLASS_DEVICE_INFO
|
||||
#define UUID_SERVCLASS_DEVICE_INFO 0x180A
|
||||
#endif
|
||||
#ifndef GATT_UUID_MANU_NAME
|
||||
#define GATT_UUID_MANU_NAME 0x2A29
|
||||
#endif
|
||||
#ifndef GATT_UUID_MODEL_NUMBER_STR
|
||||
#define GATT_UUID_MODEL_NUMBER_STR 0x2A24
|
||||
#endif
|
||||
#ifndef GATT_UUID_SYSTEM_ID
|
||||
#define GATT_UUID_SYSTEM_ID 0x2A23
|
||||
#endif
|
||||
|
||||
typedef struct peripheral_init_s {
|
||||
const char * dev_name;
|
||||
uint8_t client_links;
|
||||
uint8_t server_links;
|
||||
} peripheral_init_t;
|
||||
|
||||
typedef uint8_t ble_gatt_request_type_t;
|
||||
typedef uint8_t ble_gatt_status_t;
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct dlist_node {
|
||||
void *data;
|
||||
struct dlist_node *next;
|
||||
struct dlist_node *prev;
|
||||
} dlist_node_t;
|
||||
#pragma pack()
|
||||
|
||||
typedef struct ble_gatt_attr_s ble_gatt_attr_t;
|
||||
typedef ble_gatt_status_t \
|
||||
(*ble_peripheral_attr_handler)\
|
||||
(ble_gatt_attr_t *attribute,\
|
||||
ble_gatt_request_type_t op);
|
||||
|
||||
struct ble_gatt_attr_s {
|
||||
dlist_node_t this_node;
|
||||
uint16_t handle;
|
||||
uint16_t value_length;
|
||||
uint16_t value_buffer_length;
|
||||
uint8_t *p_value;
|
||||
ble_peripheral_attr_handler attribute_handler;
|
||||
};
|
||||
|
||||
typedef uint32_t peripheral_hdl_t;
|
||||
|
||||
typedef int (*ble_peripheral_conn_cb_t)(void);
|
||||
typedef int (*ble_peripheral_disconn_cb_t)(void);
|
||||
typedef int (*ble_adv_complete_cb_t)(void *arg);
|
||||
|
||||
#endif
|
||||
140
Living_SDK/framework/bluetooth/ble_app_framework/ble_gatt_defs.h
Normal file
140
Living_SDK/framework/bluetooth/ble_app_framework/ble_gatt_defs.h
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _BLE_GATT_DEFS_H_
|
||||
#define _BLE_GATT_DEFS_H_
|
||||
|
||||
#ifndef BLE_GATT_DB_DEFINITIONS
|
||||
#define BLE_GATT_DB_DEFINITIONS
|
||||
|
||||
/*
|
||||
* GATT attribute types
|
||||
*/
|
||||
#define GATT_UUID_PRI_SERVICE 0x2800
|
||||
#define GATT_UUID_SEC_SERVICE 0x2801
|
||||
#define GATT_UUID_INCLUDE_SERVICE 0x2802
|
||||
#define GATT_UUID_CHAR_DECLARE 0x2803 /* Characteristic Declaration*/
|
||||
|
||||
#define GATT_UUID_CHAR_EXT_PROP 0x2900 /* Characteristic Extended Properties */
|
||||
#define GATT_UUID_CHAR_DESCRIPTION 0x2901 /* Characteristic User Description*/
|
||||
#define GATT_UUID_CHAR_CLIENT_CONFIG 0x2902 /* Client Characteristic Configuration */
|
||||
#define GATT_UUID_CHAR_SRVR_CONFIG 0x2903 /* Server Characteristic Configuration */
|
||||
#define GATT_UUID_CHAR_PRESENT_FORMAT 0x2904 /* Characteristic Presentation Format*/
|
||||
#define GATT_UUID_CHAR_AGG_FORMAT 0x2905 /* Characteristic Aggregate Format*/
|
||||
#define GATT_UUID_CHAR_VALID_RANGE 0x2906 /* Characteristic Valid Range */
|
||||
#define GATT_UUID_EXT_RPT_REF_DESCR 0x2907
|
||||
#define GATT_UUID_RPT_REF_DESCR 0x2908
|
||||
|
||||
/*
|
||||
*GAP Profile Attributes
|
||||
*/
|
||||
#define GATT_UUID_GAP_DEVICE_NAME 0x2A00
|
||||
#define GATT_UUID_GAP_ICON 0x2A01
|
||||
#define GATT_UUID_GAP_PREF_CONN_PARAM 0x2A04
|
||||
#define GATT_UUID_GAP_CENTRAL_ADDR_RESOL 0x2AA6
|
||||
|
||||
/* Attribute Profile Attribute UUID */
|
||||
#define GATT_UUID_GATT_SRV_CHGD 0x2A05
|
||||
/* Attribute Protocol Test */
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
* GATT Database Defintions
|
||||
*****************************************************************************/
|
||||
/* The permission bits (see Vol 3, Part F, 3.3.1.1) */
|
||||
#define LEGATTDB_PERM_NONE (0x00)
|
||||
#define LEGATTDB_PERM_VARIABLE_LENGTH (0x1 << 0)
|
||||
#define LEGATTDB_PERM_READABLE (0x1 << 1)
|
||||
#define LEGATTDB_PERM_WRITE_CMD (0x1 << 2)
|
||||
#define LEGATTDB_PERM_WRITE_REQ (0x1 << 3)
|
||||
#define LEGATTDB_PERM_AUTH_READABLE (0x1 << 4)
|
||||
#define LEGATTDB_PERM_RELIABLE_WRITE (0x1 << 5)
|
||||
#define LEGATTDB_PERM_AUTH_WRITABLE (0x1 << 6)
|
||||
|
||||
#define LEGATTDB_PERM_WRITABLE (LEGATTDB_PERM_WRITE_CMD | LEGATTDB_PERM_WRITE_REQ| LEGATTDB_PERM_AUTH_WRITABLE)
|
||||
#define LEGATTDB_PERM_MASK (0x7f) /* All the permission bits. */
|
||||
#define LEGATTDB_PERM_SERVICE_UUID_128 (0x1 << 7)
|
||||
|
||||
|
||||
/* GATT Characteristic Properties */
|
||||
#define LEGATTDB_CHAR_PROP_BROADCAST (0x1 << 0)
|
||||
#define LEGATTDB_CHAR_PROP_READ (0x1 << 1)
|
||||
#define LEGATTDB_CHAR_PROP_WRITE_NO_RESPONSE (0x1 << 2)
|
||||
#define LEGATTDB_CHAR_PROP_WRITE (0x1 << 3)
|
||||
#define LEGATTDB_CHAR_PROP_NOTIFY (0x1 << 4)
|
||||
#define LEGATTDB_CHAR_PROP_INDICATE (0x1 << 5)
|
||||
#define LEGATTDB_CHAR_PROP_AUTHD_WRITES (0x1 << 6)
|
||||
#define LEGATTDB_CHAR_PROP_EXTENDED (0x1 << 7)
|
||||
|
||||
/* Conversion macros */
|
||||
#define BIT16_TO_8( val ) \
|
||||
(uint8_t)( (val) & 0xff),/* LSB */ \
|
||||
(uint8_t)(( (val) >> 8 ) & 0xff) /* MSB */
|
||||
|
||||
/* UUID lengths */
|
||||
#define LEGATTDB_UUID16_SIZE 2
|
||||
#define LEGATTDB_UUID128_SIZE 16
|
||||
|
||||
/* Service and Characteristic macros */
|
||||
#define ATTRIBUTE16( handle, permission, datalen, uuid ) \
|
||||
BIT16_TO_8(handle), \
|
||||
(uint8_t)(permission), \
|
||||
(uint8_t)(datalen + 2), \
|
||||
BIT16_TO_8(uuid)
|
||||
|
||||
#define PRIMARY_SERVICE_UUID16(handle, service) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
LEGATTDB_PERM_READABLE, \
|
||||
4, \
|
||||
BIT16_TO_8((GATT_UUID_PRI_SERVICE)), \
|
||||
BIT16_TO_8((service))
|
||||
|
||||
#define PRIMARY_SERVICE_UUID128(handle, service) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
LEGATTDB_PERM_READABLE, \
|
||||
18, \
|
||||
BIT16_TO_8(GATT_UUID_PRI_SERVICE), \
|
||||
service
|
||||
|
||||
#define CHARACTERISTIC_UUID16(handle, handle_value, uuid, properties, permission) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
LEGATTDB_PERM_READABLE, \
|
||||
0x07, \
|
||||
BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
|
||||
(uint8_t)(properties), \
|
||||
BIT16_TO_8((uint16_t)(handle_value)), \
|
||||
BIT16_TO_8(uuid), \
|
||||
BIT16_TO_8((uint16_t)(handle_value)), \
|
||||
(uint8_t)(permission), \
|
||||
(uint8_t)(LEGATTDB_UUID16_SIZE), \
|
||||
BIT16_TO_8(uuid)
|
||||
|
||||
#define CHARACTERISTIC_UUID128(handle, handle_value, uuid, properties, permission) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
LEGATTDB_PERM_READABLE, \
|
||||
21, \
|
||||
BIT16_TO_8(GATT_UUID_CHAR_DECLARE), \
|
||||
(uint8_t)(properties), \
|
||||
BIT16_TO_8((uint16_t)(handle_value)), \
|
||||
uuid, \
|
||||
BIT16_TO_8((uint16_t)(handle_value)), \
|
||||
(uint8_t)(permission | LEGATTDB_PERM_SERVICE_UUID_128), \
|
||||
(uint8_t)(LEGATTDB_UUID128_SIZE), \
|
||||
uuid
|
||||
|
||||
#define CHAR_DESCRIPTOR_UUID16(handle, uuid, permission) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
(uint8_t)(permission), \
|
||||
(uint8_t)(LEGATTDB_UUID16_SIZE), \
|
||||
BIT16_TO_8(uuid)
|
||||
|
||||
#define CHAR_DESCRIPTOR_UUID16_WRITABLE(handle, uuid, permission) \
|
||||
BIT16_TO_8((uint16_t)(handle)), \
|
||||
(uint8_t)(permission), \
|
||||
(uint8_t)(LEGATTDB_UUID16_SIZE), \
|
||||
(uint8_t)(0), \
|
||||
BIT16_TO_8(uuid)
|
||||
|
||||
#endif
|
||||
#endif
|
||||
25
Living_SDK/framework/bluetooth/breeze/Config.in
Normal file
25
Living_SDK/framework/bluetooth/breeze/Config.in
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
menuconfig AOS_COMP_BREEZE
|
||||
bool "Breeze SDK"
|
||||
default n
|
||||
select AOS_COMP_BLE if @CONDITION@
|
||||
select AOS_COMP_CHIP_CODE
|
||||
select AOS_COMP_BT_BREEZE_HAL
|
||||
help
|
||||
AliOS things Breeze SDK
|
||||
|
||||
if AOS_COMP_BREEZE
|
||||
# Configurations for comp breeze
|
||||
|
||||
config CONFIG_AIS_SECURE_ADV
|
||||
bool "Enable secure adv in Breeze"
|
||||
default y
|
||||
|
||||
config EN_COMBO_NET
|
||||
bool "Enable AWSS feature in Breeze"
|
||||
default y
|
||||
|
||||
config EN_AUTH
|
||||
bool "Enable authentication in Breeze"
|
||||
default y
|
||||
|
||||
endif
|
||||
194
Living_SDK/framework/bluetooth/breeze/README.md
Normal file
194
Living_SDK/framework/bluetooth/breeze/README.md
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
# README.md: Breeze
|
||||
|
||||
## Contents
|
||||
|
||||
```shell
|
||||
├── api
|
||||
│ ├── breeze_awss_export.c
|
||||
│ ├── breeze_export.c
|
||||
│ └── breeze_export.h
|
||||
├── breeze.mk
|
||||
├── core
|
||||
│ ├── auth.c
|
||||
│ ├── ble_service.c
|
||||
│ ├── core.c
|
||||
│ ├── extcmd.c
|
||||
│ ├── sha256.c
|
||||
│ ├── transport.c
|
||||
│ └── utils.c
|
||||
├── hal
|
||||
│ ├── ble
|
||||
│ │ ├── aes.c
|
||||
│ │ ├── aes_mbed.c
|
||||
│ │ ├── ali_crypto.h
|
||||
│ │ ├── ali_crypto_types.h
|
||||
│ │ ├── ble.mk
|
||||
│ │ ├── breeze_hal_ble.c
|
||||
│ │ ├── breeze_hal_os.c
|
||||
│ │ ├── breeze_hal_sec.c
|
||||
│ │ ├── include
|
||||
│ │ └── mbed_crypto.h
|
||||
│ └── include
|
||||
│ ├── breeze_hal_ble.h
|
||||
│ ├── breeze_hal_os.h
|
||||
│ └── breeze_hal_sec.h
|
||||
├── include
|
||||
│ ├── auth.h
|
||||
│ ├── ble_service.h
|
||||
│ ├── bzopt.h
|
||||
│ ├── common.h
|
||||
│ ├── core.h
|
||||
│ ├── extcmd.h
|
||||
│ ├── sha256.h
|
||||
│ ├── transport.h
|
||||
│ └── utils.h
|
||||
```
|
||||
|
||||
## Introduction
|
||||
|
||||
Breeze is a tiny and flexible SDK which provides secure BLE connection to Alibaba IoT cloud and services. It also offers an efficient way to operate WiFi provision process (i.e. obtaining SSID and password information via BLE connection). Besides, Breeze SDK is also designed to be able to provide add-on features, e.g. OTA.
|
||||
|
||||
Breeze SDK defines clear and simple APIs so it can be easily port to different platforms (OS, BLE stack, etc.).
|
||||
|
||||
### Features
|
||||
|
||||
- **Simple and Effifent BLE Data Path**. Breeze provides an efficent data path between BLE devices.
|
||||
- **Secure Connection to Alibaba Cloud**. Breeze provides secure connection to Alibaba IoT cloud services, either in the way of one secret per device, or one secret per product. This is an optional feature, which can be turned on or off according to user's configuration.
|
||||
- **Easy WiFi Provisioning**. Breeze provides an extremely easy process to do WiFi provision process. This is an optional feature, which can be turned on or off according to user's configuration.
|
||||
|
||||
### Dependencies
|
||||
|
||||
- **OS and BLE Stack**, i.e. `bluetooth.breeze.hal.ble` if AliOS Things and its BLE stack is used. Other OS and BLE stack from any vendor is also designed to be supported, as long as HAL APIs defined in `hal/include/breeze_hal_ble.h` and `hal/include/breeze_hal_os.h` are correctly implemented on top of vendor's stack.
|
||||
- **Security API**, i.e. `bluetooth.breeze.hal.ble` if mbedtls-based implementation inside AliOS Things is adopted. User is free to choose other implementation, as long as APIs defined in `hal/include/breeze_hal_sec.h` are correctly implemented.
|
||||
|
||||
## API
|
||||
|
||||
### breeze_start
|
||||
|
||||
Start breeze SDK services. This API is called by user to initialize and start breeze services.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :------- | :------------ | :----------------------------------------------------------- |
|
||||
| dev_conf | device_config | The information used to initialize Breeze SDK, including device triple, callbacks, model ID, etc. |
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, `-1` on failure.
|
||||
|
||||
### breeze_end
|
||||
|
||||
Stop breeze services. This API is called by user to stop the breeze services.
|
||||
|
||||
**Arguments**
|
||||
|
||||
None.
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, `-1` on failure.
|
||||
|
||||
### breeze_awss_init
|
||||
|
||||
Initialize breeze awss module. This API is available only when the **WiFi provisioning** feature is enabled.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :--- | :---------------- | :----------------------------------------------------------- |
|
||||
| cb | apinfo_ready_cb | The callback to be called by breeze SDK when AP info ready. |
|
||||
| info | breeze_dev_info_t | The device information (e.g. device name, device secret, product id, product key, product secret) required by breeze SDK. |
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, `-1` on failure.
|
||||
|
||||
### breeze_awss_start
|
||||
|
||||
Start breeze awss process. When this API is called, do not call breeze_start anymore. This API is available only when the **WiFi provisioning** feature is enabled.
|
||||
|
||||
**Arguments**
|
||||
|
||||
None.
|
||||
|
||||
**Return**
|
||||
|
||||
None.
|
||||
|
||||
### breeze_post
|
||||
|
||||
Post device status. This API can be used to update date to BLE server, in non-blocked way. This API uses BLE indicate way to send the data.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :----- | :------- | :--------------------------- |
|
||||
| buffer | uint8_t | Data to post. |
|
||||
| length | uint32_t | Length of the data, in byte. |
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, otherwise error code.
|
||||
|
||||
### breeze_post_fast
|
||||
|
||||
Post device status in fast way. This API is similiar with `breeze_post`. The difference is that BLE notify way is used to post the data in this API.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :----- | :------- | :--------------------------- |
|
||||
| buffer | uint8_t | Data to post. |
|
||||
| length | uint32_t | Length of the data, in byte. |
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, otherwise error code.
|
||||
|
||||
### breeze_post_ext
|
||||
|
||||
Post device status with command.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :----- | :------- | :----------------------------- |
|
||||
| buffer | uint8_t | Data to post. |
|
||||
| length | uint32_t | Length of the data, in byte. |
|
||||
| cmd | uint8_t | Command to post, 0 by default. |
|
||||
|
||||
**Return**
|
||||
|
||||
`0` on success, otherwise error code.
|
||||
|
||||
### breeze_append_adv_data
|
||||
|
||||
Append user specific data to the tail of the breeze advertising data.
|
||||
|
||||
**Arguments**
|
||||
|
||||
| name | type | description |
|
||||
| :--- | :------- | :--------------------------- |
|
||||
| data | uint8_t | Data to append. |
|
||||
| len | uint32_t | Length of the data, in byte. |
|
||||
|
||||
**Return**
|
||||
|
||||
None.
|
||||
|
||||
### breeze_append_adv_data
|
||||
|
||||
Restart BLE advertisement. This API will stop and then start the adv.
|
||||
|
||||
**Arguments**
|
||||
|
||||
None.
|
||||
|
||||
**Return**
|
||||
|
||||
None.
|
||||
|
||||
## Reference
|
||||
|
||||
None.
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "breeze_export.h"
|
||||
|
||||
static void breeze_awss_init_helper(struct device_config *init,
|
||||
breeze_dev_info_t *dinfo,
|
||||
dev_status_changed_cb status_change_cb,
|
||||
set_dev_status_cb set_cb,
|
||||
get_dev_status_cb get_cb,
|
||||
apinfo_ready_cb apinfo_rx_cb,
|
||||
ota_dev_cb ota_cb)
|
||||
{
|
||||
memset(init, 0, sizeof(struct device_config));
|
||||
init->status_changed_cb = status_change_cb;
|
||||
init->set_cb = set_cb;
|
||||
init->get_cb = get_cb;
|
||||
init->apinfo_cb = apinfo_rx_cb;
|
||||
init->ota_cb = ota_cb;
|
||||
|
||||
init->product_id = dinfo->product_id;
|
||||
|
||||
init->product_key_len = strlen(dinfo->product_key);
|
||||
memcpy(init->product_key, dinfo->product_key, init->product_key_len);
|
||||
|
||||
init->product_secret_len = strlen(dinfo->product_secret);
|
||||
memcpy(init->product_secret, dinfo->product_secret, init->product_secret_len);
|
||||
|
||||
memcpy(init->bd_adv_addr, dinfo->dev_adv_mac, BD_ADDR_LEN);
|
||||
|
||||
/* device name may be NULL */
|
||||
if (dinfo->device_name != NULL) {
|
||||
init->device_key_len = strlen(dinfo->device_name);
|
||||
memcpy(init->device_name, dinfo->device_name, init->device_key_len);
|
||||
} else {
|
||||
init->device_key_len = 0;
|
||||
}
|
||||
|
||||
/* device secret may be NULL */
|
||||
if (dinfo->device_secret != NULL) {
|
||||
init->device_secret_len = strlen(dinfo->device_secret);
|
||||
memcpy(init->device_secret, dinfo->device_secret, init->device_secret_len);
|
||||
} else {
|
||||
init->device_secret_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void breeze_awss_init(breeze_dev_info_t *info,
|
||||
dev_status_changed_cb status_change_cb,
|
||||
set_dev_status_cb set_cb,
|
||||
get_dev_status_cb get_cb,
|
||||
apinfo_ready_cb apinfo_rx_cb,
|
||||
ota_dev_cb ota_cb)
|
||||
{
|
||||
struct device_config brzinit;
|
||||
|
||||
breeze_awss_init_helper(&brzinit, info, status_change_cb, set_cb, get_cb, apinfo_rx_cb, ota_cb);
|
||||
|
||||
if (breeze_start(&brzinit) != 0) {
|
||||
BREEZE_ERR("breeze_start failed\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void breeze_awss_start()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void breeze_awss_stop()
|
||||
{
|
||||
breeze_end();
|
||||
}
|
||||
398
Living_SDK/framework/bluetooth/breeze/api/breeze_export.c
Normal file
398
Living_SDK/framework/bluetooth/breeze/api/breeze_export.c
Normal file
|
|
@ -0,0 +1,398 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "core.h"
|
||||
#include "transport.h"
|
||||
#include "breeze_export.h"
|
||||
#include "breeze_hal_ble.h"
|
||||
#include "breeze_hal_os.h"
|
||||
#include "bzopt.h"
|
||||
|
||||
static dev_status_changed_cb m_status_handler;
|
||||
static set_dev_status_cb m_ctrl_handler;
|
||||
static get_dev_status_cb m_query_handler;
|
||||
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
static apinfo_ready_cb m_apinfo_handler;
|
||||
#endif
|
||||
|
||||
#if BZ_ENABLE_OTA
|
||||
static ota_dev_cb m_ota_dev_handler;
|
||||
static bool g_disconnect_by_ota = false;
|
||||
#endif
|
||||
|
||||
struct adv_data_s {
|
||||
uint8_t data[MAX_VENDOR_DATA_LEN];
|
||||
uint32_t len;
|
||||
} user_adv = {{0}};
|
||||
|
||||
static void notify_status(breeze_event_t event)
|
||||
{
|
||||
if (m_status_handler != NULL) {
|
||||
m_status_handler(event);
|
||||
}
|
||||
}
|
||||
|
||||
static void event_handler(ali_event_t *p_event)
|
||||
{
|
||||
uint32_t err_code;
|
||||
bool b_notify_upper = false;
|
||||
#if BZ_ENABLE_OTA
|
||||
breeze_otainfo_t m_disc_evt;
|
||||
#endif
|
||||
|
||||
switch (p_event->type) {
|
||||
case BZ_EVENT_CONNECTED:
|
||||
notify_status(CONNECTED);
|
||||
#if BZ_ENABLE_OTA
|
||||
g_disconnect_by_ota = false;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BZ_EVENT_DISCONNECTED:
|
||||
core_reset();
|
||||
notify_status(DISCONNECTED);
|
||||
#if BZ_ENABLE_OTA
|
||||
m_disc_evt.type = OTA_EVT;
|
||||
m_disc_evt.cmd_evt.m_evt.evt = ALI_OTA_ON_DISCONNECTED;
|
||||
m_disc_evt.cmd_evt.m_evt.d = 0;
|
||||
b_notify_upper = true;
|
||||
if(g_disconnect_by_ota == true){
|
||||
//do nothing here as expected
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BZ_EVENT_AUTHENTICATED:
|
||||
notify_status(AUTHENTICATED);
|
||||
#if BZ_ENABLE_OTA
|
||||
m_disc_evt.type = OTA_EVT;
|
||||
m_disc_evt.cmd_evt.m_evt.evt = ALI_OTA_ON_AUTH_EVT;
|
||||
m_disc_evt.cmd_evt.m_evt.d = 1;
|
||||
b_notify_upper = true;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BZ_EVENT_TX_DONE:
|
||||
notify_status(TX_DONE);
|
||||
#if BZ_ENABLE_OTA
|
||||
uint8_t cmd = *p_event->rx_data.p_data;
|
||||
if (cmd == BZ_CMD_OTA_CHECK_RESULT || cmd == BZ_CMD_ERR || cmd == BZ_CMD_OTA_PUB_SIZE) {
|
||||
m_disc_evt.type = OTA_EVT;
|
||||
m_disc_evt.cmd_evt.m_evt.evt = ALI_OTA_ON_TX_DONE;
|
||||
m_disc_evt.cmd_evt.m_evt.d = cmd;
|
||||
b_notify_upper = true;
|
||||
}
|
||||
|
||||
/*there is a special case here to handle, for the last disconnected event caused by OTA,
|
||||
* advertising will be confusing, which will be postponed till reboot*/
|
||||
if(cmd ==BZ_CMD_OTA_CHECK_RESULT){
|
||||
g_disconnect_by_ota = true;
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BZ_EVENT_RX_INFO:
|
||||
if(p_event->rx_data.p_data != NULL){
|
||||
struct rx_cmd_post_t *r_cmd = (struct rx_cmd_post_t*) p_event->rx_data.p_data;
|
||||
uint8_t cmd = r_cmd ->cmd;
|
||||
if(cmd == BZ_CMD_QUERY){
|
||||
if (m_query_handler != NULL) {
|
||||
m_query_handler(r_cmd->p_rx_buf, r_cmd->buf_sz);
|
||||
}
|
||||
} else if(cmd == BZ_CMD_CTRL){
|
||||
if (m_ctrl_handler != NULL) {
|
||||
m_ctrl_handler (r_cmd->p_rx_buf, r_cmd->buf_sz);
|
||||
}
|
||||
}else if((cmd & BZ_CMD_TYPE_MASK) == BZ_CMD_TYPE_OTA){
|
||||
#if BZ_ENABLE_OTA
|
||||
m_disc_evt.type = OTA_CMD;
|
||||
m_disc_evt.cmd_evt.m_cmd.cmd = r_cmd->cmd;
|
||||
m_disc_evt.cmd_evt.m_cmd.frame = r_cmd->frame_seq;
|
||||
m_disc_evt.cmd_evt.m_cmd.len = r_cmd->buf_sz;
|
||||
memcpy(m_disc_evt.cmd_evt.m_cmd.data, r_cmd->p_rx_buf, r_cmd->buf_sz);
|
||||
b_notify_upper = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BZ_EVENT_APINFO:
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
if(m_apinfo_handler != NULL){
|
||||
m_apinfo_handler(p_event->rx_data.p_data);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
case BZ_EVENT_AC_AS:
|
||||
if (p_event->rx_data.p_data != NULL) {
|
||||
if ( (p_event->rx_data.p_data[0] == BZ_AC_AS_ADD)
|
||||
|| (p_event->rx_data.p_data[0] == BZ_AC_AS_UPDATE)) {
|
||||
notify_status(EVT_USER_BIND);
|
||||
} else if (p_event->rx_data.p_data[0] == BZ_AC_AS_DELETE) {
|
||||
notify_status(EVT_USER_UNBIND);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BZ_EVENT_AUTH_SIGN:
|
||||
if (p_event->rx_data.p_data != NULL) {
|
||||
if (p_event->rx_data.p_data[0] == BZ_AUTH_SIGN_NO_CHECK_PASS) {
|
||||
// do nothing, for future use
|
||||
} else if (p_event->rx_data.p_data[0] == BZ_AUTH_SIGN_CHECK_PASS) {
|
||||
notify_status(EVT_USER_SIGNED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
#if BZ_ENABLE_OTA
|
||||
case BZ_EVENT_ERR_DISCONT:
|
||||
m_disc_evt.type = OTA_EVT;
|
||||
m_disc_evt.cmd_evt.m_evt.evt = ALI_OTA_ON_DISCONTINUE_ERR;
|
||||
m_disc_evt.cmd_evt.m_evt.d = 0;
|
||||
b_notify_upper = true;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#if BZ_ENABLE_OTA
|
||||
if(b_notify_upper && (m_ota_dev_handler != NULL)){
|
||||
m_ota_dev_handler(&m_disc_evt);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int breeze_start(struct device_config *dev_conf)
|
||||
{
|
||||
uint32_t err_code;
|
||||
ali_init_t init_ali;
|
||||
|
||||
if ((dev_conf == NULL) || (dev_conf->status_changed_cb == NULL) ||
|
||||
(dev_conf->set_cb == NULL) || (dev_conf->get_cb == NULL)) {
|
||||
return -1;
|
||||
}
|
||||
m_status_handler = dev_conf->status_changed_cb;
|
||||
m_ctrl_handler = dev_conf->set_cb;
|
||||
m_query_handler = dev_conf->get_cb;
|
||||
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
if (dev_conf->apinfo_cb != NULL) {
|
||||
m_apinfo_handler = dev_conf->apinfo_cb;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BZ_ENABLE_OTA
|
||||
if (dev_conf->ota_cb != NULL) {
|
||||
m_ota_dev_handler = dev_conf->ota_cb;
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
#endif
|
||||
|
||||
memset(&init_ali, 0, sizeof(ali_init_t));
|
||||
init_ali.event_handler = event_handler;
|
||||
init_ali.model_id = dev_conf->product_id;
|
||||
init_ali.product_key.p_data = (uint8_t *)dev_conf->product_key;
|
||||
init_ali.product_key.length = dev_conf->product_key_len;
|
||||
init_ali.product_secret.p_data = (uint8_t *)dev_conf->product_secret;
|
||||
init_ali.product_secret.length = dev_conf->product_secret_len;
|
||||
init_ali.device_name.p_data = (uint8_t *)dev_conf->device_name;
|
||||
init_ali.device_name.length = dev_conf->device_key_len;
|
||||
init_ali.device_secret.p_data = (uint8_t *)dev_conf->device_secret;
|
||||
init_ali.device_secret.length = dev_conf->device_secret_len;
|
||||
init_ali.adv_mac = dev_conf->bd_adv_addr;
|
||||
init_ali.transport_timeout = BZ_TRANSPORT_TIMEOUT;
|
||||
init_ali.max_mtu = BZ_MAX_SUPPORTED_MTU;
|
||||
init_ali.user_adv_data = user_adv.data;
|
||||
init_ali.user_adv_len = user_adv.len;
|
||||
|
||||
err_code = core_init(&init_ali);
|
||||
return ((err_code == BZ_SUCCESS) ? 0 : -1);
|
||||
}
|
||||
|
||||
int breeze_end(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (ble_stack_deinit() != AIS_ERR_SUCCESS) {
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t breeze_post(uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
BREEZE_DEBUG("breeze_post");
|
||||
return transport_tx(TX_INDICATION, BZ_CMD_STATUS, buffer, length);
|
||||
}
|
||||
|
||||
|
||||
uint32_t breeze_post_fast(uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
BREEZE_DEBUG("breeze_post_fast");
|
||||
return transport_tx(TX_NOTIFICATION, BZ_CMD_STATUS, buffer, length);
|
||||
}
|
||||
|
||||
uint32_t breeze_post_ext(uint8_t cmd, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
BREEZE_DEBUG("breeze_post_ext");
|
||||
if (length == 0 || length > BZ_MAX_PAYLOAD_SIZE) {
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
|
||||
if (cmd == 0) {
|
||||
cmd = BZ_CMD_STATUS;
|
||||
}
|
||||
return transport_tx(TX_INDICATION, cmd, buffer, length);
|
||||
}
|
||||
|
||||
uint32_t breeze_post_ext_fast(uint8_t cmd, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
BREEZE_DEBUG("breeze_post_ext_fast");
|
||||
if (length == 0 || length > BZ_MAX_PAYLOAD_SIZE) {
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
|
||||
if (cmd == 0) {
|
||||
cmd = BZ_CMD_STATUS;
|
||||
}
|
||||
return transport_tx(TX_NOTIFICATION, cmd, buffer, length);
|
||||
}
|
||||
|
||||
void breeze_append_adv_data(uint8_t *data, uint32_t len)
|
||||
{
|
||||
if (data == NULL || len == 0 || len > MAX_VENDOR_DATA_LEN) {
|
||||
BREEZE_ERR("invalid adv data");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(user_adv.data, data, len);
|
||||
user_adv.len = len;
|
||||
}
|
||||
|
||||
void breeze_restart_advertising()
|
||||
{
|
||||
ais_err_t err;
|
||||
uint32_t size;
|
||||
|
||||
ais_adv_init_t adv_data = {
|
||||
.flag = AIS_AD_GENERAL | AIS_AD_NO_BREDR,
|
||||
.name = { .ntype = AIS_ADV_NAME_FULL, .name = "FY" },
|
||||
};
|
||||
|
||||
err = ble_advertising_stop();
|
||||
if (err != AIS_ERR_SUCCESS) {
|
||||
BREEZE_ERR("Failed to stop previous adv");
|
||||
return;
|
||||
}
|
||||
|
||||
adv_data.vdata.len = sizeof(adv_data.vdata.data);
|
||||
err = core_get_bz_adv_data(adv_data.vdata.data, &(adv_data.vdata.len));
|
||||
if (err) {
|
||||
BREEZE_ERR("%s %d fail", __func__, __LINE__);
|
||||
return;
|
||||
}
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
if(get_auth_update_status()){
|
||||
adv_data.vdata.data[BZ_FMSK_SECURITY_Pos] |= (1<<BZ_FMSK_SECRET_TYPE_Pos);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (user_adv.len > 0) {
|
||||
size = sizeof(adv_data.vdata.data) - adv_data.vdata.len;
|
||||
if (size < user_adv.len) {
|
||||
BREEZE_ERR("no space for user adv data (expected %d but"
|
||||
" only %d left)", user_adv.len, size);
|
||||
} else {
|
||||
memcpy(adv_data.vdata.data + adv_data.vdata.len,
|
||||
user_adv.data, user_adv.len);
|
||||
adv_data.vdata.len += user_adv.len;
|
||||
}
|
||||
}
|
||||
|
||||
ble_advertising_start(&adv_data);
|
||||
}
|
||||
|
||||
int breeze_start_advertising(uint8_t sub_type, uint8_t sec_type, uint8_t bind_state)
|
||||
{
|
||||
uint32_t size;
|
||||
ais_adv_init_t adv_data = {
|
||||
.flag = AIS_AD_GENERAL | AIS_AD_NO_BREDR,
|
||||
.name = { .ntype = AIS_ADV_NAME_FULL, .name = "FY" },
|
||||
};
|
||||
|
||||
core_create_bz_adv_data(sub_type, sec_type, bind_state);
|
||||
|
||||
adv_data.vdata.len = sizeof(adv_data.vdata.data);
|
||||
if (core_get_bz_adv_data(adv_data.vdata.data, &(adv_data.vdata.len))) {
|
||||
BREEZE_ERR("%s %d fail", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* append user adv data if any. */
|
||||
if (user_adv.len > 0) {
|
||||
size = sizeof(adv_data.vdata.data) - adv_data.vdata.len;
|
||||
if (size < user_adv.len) {
|
||||
BREEZE_ERR("no space for user adv data (expected %d but"
|
||||
" only %d left)", user_adv.len, size);
|
||||
} else {
|
||||
memcpy(adv_data.vdata.data + adv_data.vdata.len,
|
||||
user_adv.data, user_adv.len);
|
||||
adv_data.vdata.len += user_adv.len;
|
||||
}
|
||||
}
|
||||
|
||||
if (ble_advertising_start(&adv_data) != AIS_ERR_SUCCESS) {
|
||||
BREEZE_ERR("%s %d adv fail", __func__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int breeze_stop_advertising(void)
|
||||
{
|
||||
if (ble_advertising_stop() != AIS_ERR_SUCCESS) {
|
||||
BREEZE_ERR("stop adv fail");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void breeze_disconnect_ble(void)
|
||||
{
|
||||
ble_disconnect(AIS_BT_REASON_REMOTE_USER_TERM_CONN);
|
||||
}
|
||||
|
||||
uint8_t breeze_get_bind_state(void)
|
||||
{
|
||||
uint8_t kv_data[16 + 32 + 2] = {0};
|
||||
int kv_len = sizeof(kv_data);
|
||||
if (os_kv_get(BZ_AUTH_CODE_KV_PREFIX, kv_data, &kv_len) != 0) {
|
||||
BREEZE_DEBUG("no AC get from kv");
|
||||
return 0;
|
||||
} else {
|
||||
BREEZE_VERBOSE("AC from kv:");
|
||||
hex_byte_dump_verbose(kv_data, kv_len, 24);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int breeze_clear_bind_info(void)
|
||||
{
|
||||
int ret = 0;
|
||||
if (os_kv_del(BZ_AUTH_CODE_KV_PREFIX) != 0) {
|
||||
BREEZE_ERR("AC&AS clear failed");
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
349
Living_SDK/framework/bluetooth/breeze/api/breeze_export.h
Normal file
349
Living_SDK/framework/bluetooth/breeze/api/breeze_export.h
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BREEZE_API_EXPORT_H
|
||||
#define BREEZE_API_EXPORT_H
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bzopt.h"
|
||||
|
||||
#define BD_ADDR_LEN (6) /**< Length of Bluetooth Device Address. */
|
||||
#define STR_MODEL_LEN (20 + 1) /**< Reserved. */
|
||||
#define STR_SEC_LEN (40 + 1) /**< Length of device secret. */
|
||||
#define STR_PROD_SEC_LEN (32 + 1) /**< Length of product secret. */
|
||||
#define STR_PROD_KEY_LEN (20 + 1) /**< Length of product key. */
|
||||
#define STR_DEV_KEY_LEN (32 + 1) /**< Length of device name */
|
||||
|
||||
#ifndef MAX_TOKEN_PARAM_LEN
|
||||
#define MAX_TOKEN_PARAM_LEN 16
|
||||
#endif
|
||||
|
||||
/***** BLE STATUS ******/
|
||||
typedef enum {
|
||||
CONNECTED, // connect with phone success
|
||||
DISCONNECTED, // lost connection with phone
|
||||
AUTHENTICATED, // success authentication, security key auth
|
||||
TX_DONE, // send user payload data complete
|
||||
EVT_USER_BIND, // user binded, has AuthCode
|
||||
EVT_USER_UNBIND, // user unbind, no AuthCode
|
||||
EVT_USER_SIGNED, // user AuthCode sign pass
|
||||
NONE
|
||||
} breeze_event_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t protocol_ver; // ble awss protocol version
|
||||
char ssid[32 + 1]; // ble awss ap ssid
|
||||
char pw[64 * 2 + 1]; // ap password
|
||||
uint8_t bssid[6];
|
||||
uint8_t apptoken_len;
|
||||
uint8_t apptoken[MAX_TOKEN_PARAM_LEN];
|
||||
uint8_t token_type;
|
||||
uint8_t region_type;
|
||||
int region_id;
|
||||
char region_mqtturl[128];
|
||||
uint8_t rand[3];
|
||||
} breeze_apinfo_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t product_id;
|
||||
char *product_key;
|
||||
char *product_secret;
|
||||
char *device_name;
|
||||
char *device_secret;
|
||||
uint8_t *dev_adv_mac; // mac address filled in breeze adv data(maybe bt addr or wifi mac)
|
||||
} breeze_dev_info_t;
|
||||
|
||||
typedef enum {
|
||||
OTA_CMD = 1,
|
||||
OTA_EVT,
|
||||
} breeze_ota_info_type_t;
|
||||
|
||||
typedef enum {
|
||||
ALI_OTA_ON_AUTH_EVT,
|
||||
ALI_OTA_ON_TX_DONE,
|
||||
ALI_OTA_ON_DISCONNECTED,
|
||||
ALI_OTA_ON_DISCONTINUE_ERR,
|
||||
} ali_ota_evt_type_re_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t cmd;
|
||||
uint8_t frame;
|
||||
uint8_t data[BZ_MAX_PAYLOAD_SIZE];
|
||||
uint16_t len;
|
||||
} breeze_ota_cmd_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t evt;
|
||||
uint8_t d;
|
||||
} breeze_ota_evt_t;
|
||||
|
||||
typedef struct {
|
||||
breeze_ota_info_type_t type;
|
||||
union {
|
||||
breeze_ota_cmd_t m_cmd;
|
||||
breeze_ota_evt_t m_evt;
|
||||
} cmd_evt;
|
||||
} breeze_otainfo_t;
|
||||
|
||||
/**
|
||||
* @brief Callback when device status changed.
|
||||
*
|
||||
* @param[in] status @n Device Status.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note This API should be implemented by user, and will be called by SDK
|
||||
* when device statuc changed, e.g. bluetooth connection status change.
|
||||
*/
|
||||
typedef void (*dev_status_changed_cb)(breeze_event_t event);
|
||||
|
||||
/**
|
||||
* @brief Callback when there is device status to set.
|
||||
*
|
||||
* @param[in] buffer @n The data to be set.
|
||||
* @param[in] model @n Length of the data.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note This API should be implemented by user and will be called by SDK.
|
||||
*/
|
||||
typedef void (*set_dev_status_cb)(uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Callback when there is device status to get.
|
||||
*
|
||||
* @param[out] buffer @n The data of device status.
|
||||
* @param[out] model @n Length of the data.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note This API should be implemented by user and will be called by SDK.
|
||||
*/
|
||||
typedef void (*get_dev_status_cb)(uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Callback when there is AWSS info to get.
|
||||
*
|
||||
* @param[out] buffer @n The data struct of AP info.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note This API should be implemented by user and will be called by SDK.
|
||||
*/
|
||||
typedef void (*apinfo_ready_cb)(breeze_apinfo_t *ap);
|
||||
|
||||
/**
|
||||
* @brief Callback when device receive ota releated event.
|
||||
*
|
||||
* @param[out] ota_cmd @n ota cmd, e.g. 0x20, 0x22, 0x24, 0x28. pls refer to spec.
|
||||
* @param[out] num_frame @n frame number of ota data.
|
||||
* @param[out] buffer @n The data of device status.
|
||||
* @param[out] lenght @n Length of the data.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note This API should be implemented by user and will be called by SDK.
|
||||
*/
|
||||
|
||||
typedef void (*ota_dev_cb)(breeze_otainfo_t *otainfo);
|
||||
|
||||
/**
|
||||
* This structure includes the information which is
|
||||
* required to initialize the SDK.
|
||||
*/
|
||||
struct device_config
|
||||
{
|
||||
uint8_t bd_addr[BD_ADDR_LEN];
|
||||
uint8_t bd_adv_addr[BD_ADDR_LEN]; // mac address filled in breeze adv data(maybe bt addr or wifi mac)
|
||||
char model[STR_MODEL_LEN];
|
||||
uint32_t product_id;
|
||||
char product_key[STR_PROD_KEY_LEN];
|
||||
uint8_t product_key_len;
|
||||
char product_secret[STR_PROD_SEC_LEN];
|
||||
uint8_t product_secret_len;
|
||||
char device_name[STR_DEV_KEY_LEN];
|
||||
uint8_t device_key_len;
|
||||
char device_secret[STR_SEC_LEN];
|
||||
uint8_t device_secret_len;
|
||||
dev_status_changed_cb status_changed_cb;
|
||||
set_dev_status_cb set_cb;
|
||||
get_dev_status_cb get_cb;
|
||||
apinfo_ready_cb apinfo_cb;
|
||||
ota_dev_cb ota_cb;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Start breeze SDK services.
|
||||
*
|
||||
* @param[in] dev_info @n Device information
|
||||
* @return result 0:success -1 failed.
|
||||
* @see None.
|
||||
* @note This API is called by user to initialize and start breeze services.
|
||||
*/
|
||||
int breeze_start(struct device_config *dev_conf);
|
||||
|
||||
/**
|
||||
* @brief Stop breeze services.
|
||||
* @return result 0:success -1 failed.
|
||||
* @see None.
|
||||
* @note This API is called by user to stop the breeze services.
|
||||
*/
|
||||
int breeze_end(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize breeze awss module.
|
||||
*
|
||||
* @param[in] cb The callback to be called by breeze SDK when AP info ready.
|
||||
* @param[in] info The device information required by breeze SDK.
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
void breeze_awss_init(breeze_dev_info_t *info,
|
||||
dev_status_changed_cb status_change_cb,
|
||||
set_dev_status_cb set_cb,
|
||||
get_dev_status_cb get_cb,
|
||||
apinfo_ready_cb apinfo_rx_cb,
|
||||
ota_dev_cb ota_cb);
|
||||
|
||||
/**
|
||||
* @brief Start breeze awss process.
|
||||
*
|
||||
* @param None.
|
||||
* @return None.
|
||||
* @see None.
|
||||
*
|
||||
* @note When this API is called, do not call breeze_start anymore.
|
||||
*/
|
||||
void breeze_awss_start();
|
||||
|
||||
/**
|
||||
* @brief Stop breeze, include ble-awss, ble-breeze and ble-stack.
|
||||
*
|
||||
* @param None.
|
||||
* @return None.
|
||||
* @see None.
|
||||
*
|
||||
* @note When this API is called, do not call breeze anymore.
|
||||
*/
|
||||
void breeze_awss_stop();
|
||||
|
||||
/**
|
||||
* @brief Post device status.
|
||||
*
|
||||
* @param[in] buffer @n Data to post.
|
||||
* @param[in] model @n Length of the data.
|
||||
* @return result 0: success; others:err code.
|
||||
* @see None.
|
||||
* @note This API can be used to update date to server, in non-blocked way.
|
||||
* This API uses ble indicate way to send the data.
|
||||
*/
|
||||
uint32_t breeze_post(uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Post device status, in a fast way.
|
||||
*
|
||||
* @param[in] buffer @n Data to post.
|
||||
* @param[in] model @n Length of the data.
|
||||
* @return result 0: success; others:err code.
|
||||
* @see None.
|
||||
* @note This API is similiar with breeze_post. The difference is that
|
||||
* ble notify way is used to post the data.
|
||||
*/
|
||||
uint32_t breeze_post_fast(uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Post device status with cmd.
|
||||
*
|
||||
* @param[in] cmd @n cmda to post.0:default, other:for internal use
|
||||
* @param[in] buffer @n Data to post.
|
||||
* @param[in] model @n Length of the data.
|
||||
* @return result 0: success; others:err code.
|
||||
* @see None.
|
||||
* @note This API can be used to update date to server, in non-blocked way.
|
||||
* This API uses ble indicate way to send the data.
|
||||
*/
|
||||
uint32_t breeze_post_ext(uint8_t cmd, uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Post device status with cmd.
|
||||
*
|
||||
* @param[in] cmd @n cmda to post.0:default, other:for internal use
|
||||
* @param[in] buffer @n Data to post.
|
||||
* @param[in] model @n Length of the data.
|
||||
* @return result 0: success; others:err code.
|
||||
* @see None.
|
||||
* @note This API uses ble notification way to send the data.
|
||||
*/
|
||||
uint32_t breeze_post_ext_fast(uint8_t cmd, uint8_t *buffer, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Append user specific data to the tail of the breeze adv data.
|
||||
*
|
||||
* @param[in] data @n Data to append.
|
||||
* @param[in] len @n Data length.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note User can call this API if additional adv data is needed.
|
||||
* Breeze SDK has its own adv data and format, find more details
|
||||
* in Breeze spec.
|
||||
*/
|
||||
void breeze_append_adv_data(uint8_t *data, uint32_t len);
|
||||
|
||||
/**
|
||||
* @brief Restart BLE advertisement. This API will stop and then start the adv.
|
||||
*
|
||||
* @param None.
|
||||
* @return None.
|
||||
* @see None.
|
||||
* @note User can call this API if he/she wants to update the adv
|
||||
* content from time to time.
|
||||
*/
|
||||
void breeze_restart_advertising();
|
||||
|
||||
/**
|
||||
* @brief Start BLE advertisement. This API will start the adv.
|
||||
*
|
||||
* @param[in] sub_type @n device advertising type.
|
||||
* @param[in] sec_type @n security type, per-product or per-device.
|
||||
* @param[in] bind_state @n device bind state.
|
||||
* @return result 0-success, <0-fail.
|
||||
* @see None.
|
||||
* @note User can call this API if he/she wants to start the adv
|
||||
*/
|
||||
int breeze_start_advertising(uint8_t sub_type, uint8_t sec_type, uint8_t bind_state);
|
||||
|
||||
/**
|
||||
* @brief Stop BLE advertisement. This API will stop the adv.
|
||||
*
|
||||
* @param None.
|
||||
* @return result 0-success, <0-fail.
|
||||
* @see None.
|
||||
* @note User can call this API if he/she wants to stop the adv
|
||||
*/
|
||||
int breeze_stop_advertising(void);
|
||||
|
||||
/**
|
||||
* @brief get breeze device's bind state.
|
||||
*
|
||||
* @param None.
|
||||
* @return bind_state 0-not bind, 1-binded.
|
||||
* @see None.
|
||||
*/
|
||||
uint8_t breeze_get_bind_state(void);
|
||||
|
||||
/**
|
||||
* @brief clear breeze device's bind state.
|
||||
*
|
||||
* @param None.
|
||||
* @return result 0-success, <0-fail.
|
||||
* @see None.
|
||||
*/
|
||||
int breeze_clear_bind_info(void);
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BREEZE_API_EXPORT_H
|
||||
57
Living_SDK/framework/bluetooth/breeze/breeze.mk
Normal file
57
Living_SDK/framework/bluetooth/breeze/breeze.mk
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
|
||||
NAME := breeze
|
||||
|
||||
$(NAME)_MBINS_TYPE := kernel
|
||||
$(NAME)_VERSION := 1.0.0
|
||||
$(NAME)_SUMMARY := breeze provides secure BLE connection to Alibaba IoT cloud and services.
|
||||
|
||||
$(NAME)_SOURCES += core/core.c
|
||||
$(NAME)_SOURCES += core/transport.c
|
||||
$(NAME)_SOURCES += core/ble_service.c
|
||||
$(NAME)_SOURCES += core/sha256.c
|
||||
$(NAME)_SOURCES += core/utils.c
|
||||
#$(NAME)_SOURCES += core/extcmd.c
|
||||
|
||||
GLOBAL_INCLUDES += include hal/include api
|
||||
|
||||
$(NAME)_COMPONENTS := chip_code
|
||||
|
||||
btstack ?= zephyr
|
||||
ifeq (zephyr, $(btstack))
|
||||
$(NAME)_COMPONENTS += framework.bluetooth.breeze.hal.ble
|
||||
endif
|
||||
|
||||
secure_adv ?= 0
|
||||
ifeq ($(secure_adv), 1)
|
||||
GLOBAL_DEFINES += CONFIG_AIS_SECURE_ADV
|
||||
endif
|
||||
|
||||
model_sec ?= 0
|
||||
ifeq ($(model_sec), 1)
|
||||
GLOBAL_DEFINES += CONFIG_SEC_PER_PK_TO_DN
|
||||
endif
|
||||
|
||||
|
||||
$(NAME)_SOURCES += api/breeze_export.c
|
||||
|
||||
bz_en_auth ?= 1
|
||||
ifeq ($(bz_en_auth), 1)
|
||||
GLOBAL_DEFINES += EN_AUTH
|
||||
$(NAME)_SOURCES += core/auth.c
|
||||
ifeq ($(offline_auth), 1)
|
||||
GLOBAL_DEFINES += EN_AUTH_OFFLINE
|
||||
endif
|
||||
endif
|
||||
|
||||
bz_en_awss ?= 0
|
||||
ifeq ($(bz_en_awss), 1)
|
||||
GLOBAL_DEFINES += EN_COMBO_NET
|
||||
$(NAME)_SOURCES += core/extcmd.c
|
||||
$(NAME)_SOURCES += api/breeze_awss_export.c
|
||||
endif
|
||||
|
||||
bz_long_mtu ?= 1
|
||||
ifeq ($(bz_long_mtu), 1)
|
||||
GLOBAL_DEFINES += EN_LONG_MTU
|
||||
endif
|
||||
|
||||
488
Living_SDK/framework/bluetooth/breeze/core/auth.c
Normal file
488
Living_SDK/framework/bluetooth/breeze/core/auth.c
Normal file
|
|
@ -0,0 +1,488 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "sha256.h"
|
||||
|
||||
#include "auth.h"
|
||||
#include "core.h"
|
||||
#include "utils.h"
|
||||
#include "breeze_hal_ble.h"
|
||||
|
||||
extern core_t g_core;
|
||||
auth_t g_auth;
|
||||
|
||||
static uint8_t g_ds[BZ_DEV_DEVICE_SECRET_LEN] = { 0 };
|
||||
static uint8_t g_ds_len = 0;
|
||||
static uint8_t g_ps[BZ_DEV_PRODUCT_SECRET_LEN] = { 0 };
|
||||
static uint8_t g_ps_len = 0;
|
||||
bool g_dn_complete = false;
|
||||
|
||||
static void on_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_PROC_TIMER_2, BZ_ETIMEOUT);
|
||||
}
|
||||
|
||||
static void ikm_init(ali_init_t const *p_init)
|
||||
{
|
||||
if(g_auth.dyn_update_device_secret == true || g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN){
|
||||
g_ds_len = g_auth.device_secret_len;
|
||||
memcpy(g_ds, g_auth.device_secret, g_ds_len);
|
||||
}
|
||||
|
||||
g_ps_len = p_init->product_secret.length;
|
||||
memcpy(g_ps, p_init->product_secret.p_data, g_ps_len);
|
||||
|
||||
memcpy(g_auth.ikm + g_auth.ikm_len, p_init->product_secret.p_data, p_init->product_secret.length);
|
||||
g_auth.ikm_len += p_init->product_secret.length;
|
||||
|
||||
g_auth.ikm[g_auth.ikm_len++] = ',';
|
||||
}
|
||||
|
||||
static void update_aes_key(bool use_device_key)
|
||||
{
|
||||
uint8_t rand_backup[RANDOM_SEQ_LEN];
|
||||
SHA256_CTX context;
|
||||
uint8_t okm[SHA256_BLOCK_SIZE];
|
||||
|
||||
memcpy(rand_backup, g_auth.ikm + g_auth.ikm_len, RANDOM_SEQ_LEN);
|
||||
g_auth.ikm_len = 0;
|
||||
|
||||
if (use_device_key) {
|
||||
memcpy(g_auth.ikm + g_auth.ikm_len, g_ds, g_ds_len);
|
||||
g_auth.ikm_len += g_ds_len;
|
||||
} else {
|
||||
memcpy(g_auth.ikm + g_auth.ikm_len, g_ps, g_ps_len);
|
||||
g_auth.ikm_len += g_ps_len;
|
||||
}
|
||||
|
||||
g_auth.ikm[g_auth.ikm_len++] = ',';
|
||||
memcpy(g_auth.ikm + g_auth.ikm_len, rand_backup, sizeof(rand_backup));
|
||||
|
||||
sha256_init(&context);
|
||||
sha256_update(&context, g_auth.ikm, g_auth.ikm_len + RANDOM_SEQ_LEN);
|
||||
sha256_final(&context, okm);
|
||||
memcpy(g_auth.okm, okm, MAX_OKM_LEN);
|
||||
|
||||
// notify key updated
|
||||
transport_update_key(g_auth.okm);
|
||||
}
|
||||
|
||||
ret_code_t auth_init(ali_init_t const *p_init, tx_func_t tx_func)
|
||||
{
|
||||
int len;
|
||||
char tmp_ds[BZ_DEV_DEVICE_SECRET_LEN];
|
||||
ret_code_t ret = BZ_SUCCESS;
|
||||
|
||||
len = sizeof(tmp_ds);
|
||||
memset(tmp_ds, 0, len);
|
||||
memset(&g_auth, 0, sizeof(auth_t));
|
||||
g_auth.state = BZ_AUTH_STATE_IDLE;
|
||||
g_auth.tx_func = tx_func;
|
||||
g_auth.dyn_update_device_secret = false;
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
g_auth.offline_auth = false;
|
||||
#endif
|
||||
|
||||
if ((p_init->product_key.p_data != NULL) && (p_init->product_key.length > 0)) {
|
||||
g_auth.p_product_key = g_core.product_key;
|
||||
g_auth.product_key_len = g_core.product_key_len;
|
||||
}
|
||||
if ((p_init->device_name.p_data != NULL) && (p_init->device_name.length > 0)) {
|
||||
g_auth.p_device_name = g_core.device_name;
|
||||
g_auth.device_name_len = g_core.device_name_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* Secret have 2 resources: internal KV storge and external initialization. Consider below conditions:
|
||||
* 1.secret per device, no DS in KV: need 1. update DS from external initialization.
|
||||
* 2.secret per device, have DS in KV:if 1.DS is the same choose either one. 2.DS is not the same, report err or choose one.
|
||||
* 3.secret per product, no DS in KV, : Doing nothing.
|
||||
* 4.secret per producet, have DS in KV:need 1. update dynamic secret flag, update from interval KV storage.
|
||||
*/
|
||||
g_auth.device_secret_len = p_init->device_secret.length;
|
||||
memset(tmp_ds, 0, sizeof(tmp_ds));
|
||||
if(g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN){
|
||||
if(auth_get_device_secret(&tmp_ds, &len) != 0 ){
|
||||
// Read ds from kv fail, but ds set by upper layer
|
||||
memcpy(g_auth.device_secret, p_init->device_secret.p_data, BZ_DEV_DEVICE_SECRET_LEN);
|
||||
} else if(memcmp(tmp_ds, g_auth.device_secret, g_auth.device_secret_len) == 0){
|
||||
// Read ds from kv success, and ds in auth memory is the same
|
||||
memcpy(g_auth.device_secret, p_init->device_secret.p_data, BZ_DEV_DEVICE_SECRET_LEN);
|
||||
} else{
|
||||
BREEZE_ERR("DS from KV not match user's input %s", __func__);
|
||||
return BZ_EINVALIDPARAM;
|
||||
}
|
||||
} else if(g_auth.device_secret_len == 0){
|
||||
if(auth_get_device_secret(&tmp_ds, &len) == 0){ //case 4.
|
||||
g_auth.device_secret_len = len;
|
||||
memcpy(g_auth.device_secret, tmp_ds, len);
|
||||
g_auth.dyn_update_device_secret = true;
|
||||
} //case 3
|
||||
} else{
|
||||
BREEZE_ERR("Auth type not per product or per device %s", __func__);
|
||||
return BZ_EINVALIDPARAM;//err case when DS length is not BZ_DEV_DEVICE_SECRET_LEN or 0
|
||||
}
|
||||
|
||||
ikm_init(p_init);
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
auth_keys_init();
|
||||
#endif
|
||||
ret = os_timer_new(&g_auth.timer, on_timeout, &g_auth, BZ_AUTH_TIMEOUT);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void auth_reset(void)
|
||||
{
|
||||
g_auth.state = BZ_AUTH_STATE_IDLE;
|
||||
os_timer_stop(&g_auth.timer);
|
||||
}
|
||||
|
||||
void auth_rx_command(uint8_t cmd, uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
uint32_t err_code;
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
uint8_t rekey_rsp[] = {0x01, 0x02};
|
||||
uint8_t authkey[AUTH_KEY_LEN]={0};
|
||||
uint8_t authid[AUTH_ID_LEN] = {0};
|
||||
#endif
|
||||
|
||||
if (length == 0 || (cmd & BZ_CMD_TYPE_MASK) != BZ_CMD_AUTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (g_auth.state) {
|
||||
case BZ_AUTH_STATE_RAND_SENT:
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
if(cmd == BZ_CMD_AUTH_REKEY){/*check whether auth-key according to auth-id*/
|
||||
if(length != AUTH_ID_LEN) {
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_REKEY_RSP, &rekey_rsp[1] , 1);
|
||||
BREEZE_ERR("[BZ auth]: rx offline key len mismatch(%d)", err_code);
|
||||
return;
|
||||
}
|
||||
memcpy(authid, p_data, AUTH_ID_LEN);
|
||||
if(authkey_get(authid, authkey) == true){
|
||||
//("[BZ auth]: rx offline key\n");
|
||||
g_dn_complete = true;
|
||||
g_auth.state = BZ_AUTH_STATE_REQ_RECVD;
|
||||
g_auth.offline_auth = true;
|
||||
|
||||
/*1.update current auth key*/
|
||||
transport_update_key(authkey);
|
||||
/*2.response with "HI Client"*/
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_RSP, BZ_HI_CLIENT_STR, strlen(BZ_HI_CLIENT_STR));
|
||||
BREEZE_DEBUG("[BZ auth]: tx (%s) (%s)", BZ_HI_CLIENT_STR, err_code ? "fail": "success");
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SEND_RSP, err_code);
|
||||
return;
|
||||
}
|
||||
err_code = os_timer_start(&g_auth.timer);
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_PROC_TIMER_1, err_code);
|
||||
return;
|
||||
}
|
||||
} else{
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_REKEY_RSP, &rekey_rsp[0] , 1);
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SEND_RSP, err_code);
|
||||
return;
|
||||
}
|
||||
/*back to re-auth state*/
|
||||
os_timer_stop(&g_auth.timer);
|
||||
auth_connected();
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
if (cmd == BZ_CMD_AUTH_REQ &&
|
||||
memcmp(BZ_HI_SERVER_STR, p_data, MIN(length, strlen(BZ_HI_SERVER_STR))) == 0) {
|
||||
BREEZE_DEBUG("[BZ auth]: rx (%s)", BZ_HI_SERVER_STR);
|
||||
g_dn_complete = true;
|
||||
g_auth.state = BZ_AUTH_STATE_REQ_RECVD;
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_RSP, BZ_HI_CLIENT_STR, strlen(BZ_HI_CLIENT_STR));
|
||||
BREEZE_DEBUG("[BZ auth]: tx (%s) (%s)", BZ_HI_CLIENT_STR, err_code ? "fail": "success");
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SEND_RSP, err_code);
|
||||
return;
|
||||
}
|
||||
|
||||
err_code = os_timer_start(&g_auth.timer);
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_PROC_TIMER_1, err_code);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
g_auth.state = BZ_AUTH_STATE_FAILED;
|
||||
}
|
||||
break;
|
||||
|
||||
case BZ_AUTH_STATE_REQ_RECVD:
|
||||
if (cmd == BZ_CMD_AUTH_CFM && memcmp(BZ_OK_STR, p_data, MIN(length, strlen(BZ_OK_STR))) == 0) {
|
||||
BREEZE_DEBUG("[BZ auth]: rx (%s)", BZ_OK_STR);
|
||||
g_auth.state = BZ_AUTH_STATE_DONE;
|
||||
} else {
|
||||
g_auth.state = BZ_AUTH_STATE_FAILED;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
err_code = g_auth.tx_func(BZ_CMD_ERR, NULL, 0);
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SEND_ERROR, err_code);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (g_auth.state == BZ_AUTH_STATE_DONE) {
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
if(g_auth.offline_auth == false){
|
||||
store_auth_key();
|
||||
} else{
|
||||
g_auth.offline_auth = false;
|
||||
}
|
||||
#endif
|
||||
core_event_notify(BZ_EVENT_AUTHENTICATED, NULL, 0);
|
||||
os_timer_stop(&g_auth.timer);
|
||||
} else if (g_auth.state == BZ_AUTH_STATE_FAILED) {
|
||||
os_timer_stop(&g_auth.timer);
|
||||
ble_disconnect(AIS_BT_REASON_REMOTE_USER_TERM_CONN);
|
||||
}
|
||||
}
|
||||
void auth_connected(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
err_code = os_timer_start(&g_auth.timer);
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_PROC_TIMER_0, err_code);
|
||||
return;
|
||||
}
|
||||
|
||||
g_auth.state = BZ_AUTH_STATE_CONNECTED;
|
||||
get_random(g_auth.ikm + g_auth.ikm_len, RANDOM_SEQ_LEN);
|
||||
update_aes_key(false);
|
||||
}
|
||||
|
||||
void auth_service_enabled(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
g_auth.state = BZ_AUTH_STATE_SVC_ENABLED;
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_RAND, g_auth.ikm + g_auth.ikm_len, RANDOM_SEQ_LEN);
|
||||
BREEZE_DEBUG("[BZ auth]: tx rand(%s)", err_code ? "fail": "success");
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SVC_ENABLED, err_code);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void auth_tx_done(void)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
if(g_auth.state == BZ_AUTH_STATE_CONNECTED){
|
||||
auth_service_enabled();
|
||||
} else if (g_auth.state == BZ_AUTH_STATE_SVC_ENABLED) {
|
||||
g_auth.state = BZ_AUTH_STATE_RAND_SENT;
|
||||
|
||||
if((g_auth.dyn_update_device_secret == true || g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN)
|
||||
&& (g_auth.auth_type == BZ_AUTH_TYPE_PER_DEV)){
|
||||
err_code = g_auth.tx_func(BZ_CMD_AUTH_KEY, g_auth.p_device_name, g_auth.device_name_len);
|
||||
BREEZE_DEBUG("[BZ auth]: tx DN (%s)", err_code ? "fail": "success");
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_AUTH_SEND_KEY, err_code);
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else if (g_auth.state == BZ_AUTH_STATE_RAND_SENT) {
|
||||
if((g_auth.dyn_update_device_secret == true || g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN)
|
||||
&& (g_auth.auth_type == BZ_AUTH_TYPE_PER_DEV)){
|
||||
BREEZE_DEBUG("[BZ auth]: update AES-auth-key");
|
||||
update_aes_key(true);
|
||||
} else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool auth_is_authdone(void)
|
||||
{
|
||||
return (bool)(g_auth.state == BZ_AUTH_STATE_DONE);
|
||||
}
|
||||
|
||||
ret_code_t auth_get_device_name(uint8_t **pp_device_name, uint8_t *p_length)
|
||||
{
|
||||
if(g_auth.device_name_len){
|
||||
*pp_device_name = g_auth.p_device_name;
|
||||
*p_length = g_auth.device_name_len;
|
||||
return BZ_SUCCESS;
|
||||
} else{
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t auth_get_product_key(uint8_t **pp_prod_key, uint8_t *p_length)
|
||||
{
|
||||
*pp_prod_key = g_auth.p_product_key;
|
||||
*p_length = BZ_DEV_PRODUCT_KEY_LEN;
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
ret_code_t auth_get_device_secret(uint8_t *p_secret, int *p_length)
|
||||
{
|
||||
if(p_secret == NULL || p_length == NULL){
|
||||
return BZ_EINVALIDPARAM;
|
||||
}
|
||||
return os_kv_get(BZ_DEVICE_SECRET_STR, p_secret, p_length);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
static void make_seq_le(uint32_t *seq)
|
||||
{
|
||||
uint32_t test_num = 0x01020304;
|
||||
uint8_t *byte = (uint8_t *)(&test_num);
|
||||
|
||||
if (*byte == 0x04)
|
||||
return;
|
||||
uint32_t tmp = *seq;
|
||||
SET_U32_LE(seq, tmp);
|
||||
}
|
||||
|
||||
int auth_calc_adv_sign(uint32_t seq, uint8_t *sign)
|
||||
{
|
||||
SHA256_CTX context;
|
||||
uint8_t full_sign[32], i, *p;
|
||||
|
||||
make_seq_le(&seq);
|
||||
sha256_init(&context);
|
||||
|
||||
sha256_update(&context, BZ_DEVICE_NAME_STR, strlen(BZ_DEVICE_NAME_STR));
|
||||
sha256_update(&context, g_auth.p_device_name, g_auth.device_name_len);
|
||||
|
||||
if(g_auth.dyn_update_device_secret == true || g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN){
|
||||
sha256_update(&context, BZ_DEVICE_SECRET_STR, strlen(BZ_DEVICE_SECRET_STR));
|
||||
sha256_update(&context, g_auth.device_secret, BZ_DEV_DEVICE_SECRET_LEN);
|
||||
} else{
|
||||
sha256_update(&context, BZ_PRODUCT_SECRET_STR, strlen(BZ_PRODUCT_SECRET_STR));
|
||||
sha256_update(&context, g_ps, BZ_DEV_PRODUCT_SECRET_LEN);
|
||||
}
|
||||
|
||||
sha256_update(&context, BZ_PRODUCT_KEY_STR, strlen(BZ_PRODUCT_KEY_STR));
|
||||
sha256_update(&context, g_auth.p_product_key, BZ_DEV_PRODUCT_KEY_LEN);
|
||||
|
||||
sha256_update(&context, BZ_SEQUENCE_STR, strlen(BZ_SEQUENCE_STR));
|
||||
sha256_update(&context, &seq, sizeof(seq));
|
||||
|
||||
sha256_final(&context, full_sign);
|
||||
|
||||
memcpy(sign, full_sign, 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
bool get_auth_update_status(void)
|
||||
{
|
||||
return g_auth.dyn_update_device_secret;
|
||||
}
|
||||
|
||||
ret_code_t auth_secret_update_post_process(uint8_t* p_ds, uint16_t len)
|
||||
{
|
||||
/*
|
||||
* 1.update secret bit in adv, this will do in restart adv logic, not here
|
||||
* 2.update IKM with product secret for secret per device
|
||||
* 3.update g_auth.dyn_update_device_secret flag in g_auth struct
|
||||
*/
|
||||
g_auth.dyn_update_device_secret = true;
|
||||
g_ds_len = len;
|
||||
g_auth.device_secret_len = len;
|
||||
memcpy(g_ds, p_ds, len);
|
||||
memcpy(g_auth.device_secret, p_ds, len);
|
||||
|
||||
BREEZE_DEBUG("Auth updated :per product ->device %d len(%d)",g_auth.dyn_update_device_secret, g_ds_len);
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
void auth_keys_init(void)
|
||||
{
|
||||
auth_key_storage_t auth_keys;
|
||||
int len = sizeof(auth_keys);
|
||||
memset(&auth_keys, 0, len);
|
||||
if (os_kv_get(AUTH_KEY_KV_PREFIX, &auth_keys, &len) != 0){
|
||||
if(os_kv_set(AUTH_KEY_KV_PREFIX, &auth_keys, len, 1) != 0){
|
||||
BREEZE_ERR("[BZ auth]: init keys");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool authkey_set(uint8_t* authid, uint8_t* authkey)
|
||||
{
|
||||
int32_t ret;
|
||||
auth_key_storage_t auth_keys;
|
||||
int len = sizeof(auth_keys);
|
||||
if(authid == NULL || authkey == NULL){
|
||||
return false;
|
||||
}
|
||||
memset(&auth_keys, 0, len);
|
||||
if (os_kv_get(AUTH_KEY_KV_PREFIX, &auth_keys, &len) == 0){
|
||||
uint32_t index = auth_keys.index_to_update;
|
||||
memcpy(auth_keys.kv_pairs[index].auth_id, authid, AUTH_ID_LEN);
|
||||
memcpy(auth_keys.kv_pairs[index].auth_key, authkey, AUTH_KEY_LEN);
|
||||
auth_keys.index_to_update ++;
|
||||
if(index == MAX_AUTH_KEYS -1){
|
||||
auth_keys.index_to_update = 0;
|
||||
}
|
||||
ret = os_kv_set(AUTH_KEY_KV_PREFIX, &auth_keys, len, 1);
|
||||
BREEZE_ERR("[BZ auth]: keys KV set (%s)", ret ? "fail": "success");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool authkey_get(uint8_t* authid, uint8_t* authkey)
|
||||
{
|
||||
int32_t ret;
|
||||
uint32_t index = 0;
|
||||
auth_key_storage_t auth_keys;
|
||||
int len = sizeof(auth_keys);
|
||||
if(authid == NULL || authkey == NULL){
|
||||
return false;
|
||||
}
|
||||
if (os_kv_get(AUTH_KEY_KV_PREFIX, &auth_keys, &len) == 0){
|
||||
for(; index < MAX_AUTH_KEYS; ++index){
|
||||
if(!memcmp(auth_keys.kv_pairs[index].auth_id, authid, AUTH_ID_LEN)){
|
||||
memcpy(authkey, auth_keys.kv_pairs[index].auth_key, AUTH_KEY_LEN);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(index == MAX_AUTH_KEYS){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void store_auth_key(void)
|
||||
{
|
||||
uint8_t rand_backup[RANDOM_SEQ_LEN];
|
||||
SHA256_CTX context;
|
||||
uint8_t auth_id[AUTH_ID_LEN];
|
||||
uint8_t okm[SHA256_BLOCK_SIZE];
|
||||
memcpy(rand_backup, g_auth.ikm + g_auth.ikm_len, RANDOM_SEQ_LEN);
|
||||
sha256_init(&context);
|
||||
sha256_update(&context, rand_backup, sizeof(rand_backup));
|
||||
sha256_final(&context, okm);
|
||||
memcpy(auth_id, okm, AUTH_ID_LEN);
|
||||
authkey_set(auth_id, g_auth.okm);
|
||||
}
|
||||
|
||||
#endif
|
||||
177
Living_SDK/framework/bluetooth/breeze/core/ble_service.c
Normal file
177
Living_SDK/framework/bluetooth/breeze/core/ble_service.c
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "breeze_hal_ble.h"
|
||||
#include "ble_service.h"
|
||||
#include "common.h"
|
||||
#include "core.h"
|
||||
#include "bzopt.h"
|
||||
|
||||
ble_ais_t g_ais;
|
||||
|
||||
static void service_enabled(void)
|
||||
{
|
||||
if (g_ais.is_indication_enabled && g_ais.is_notification_enabled) {
|
||||
BREEZE_DEBUG("Let's notify that service is enabled");
|
||||
#ifdef EN_LONG_MTU
|
||||
trans_update_mtu();
|
||||
#endif
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_service_enabled();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void connected()
|
||||
{
|
||||
g_ais.conn_handle = BLE_CONN_HANDLE_MAGIC;
|
||||
g_ais.is_indication_enabled = false;
|
||||
g_ais.is_notification_enabled = false;
|
||||
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_connected();
|
||||
#endif
|
||||
core_event_notify(BZ_EVENT_CONNECTED, NULL, 0);
|
||||
}
|
||||
|
||||
static void disconnected()
|
||||
{
|
||||
g_ais.conn_handle = BLE_CONN_HANDLE_INVALID;
|
||||
g_ais.is_indication_enabled = false;
|
||||
g_ais.is_notification_enabled = false;
|
||||
core_event_notify(BZ_EVENT_DISCONNECTED, NULL, 0);
|
||||
}
|
||||
|
||||
static void ic_ccc_handler(ais_ccc_value_t val)
|
||||
{
|
||||
g_ais.is_indication_enabled = (val == AIS_CCC_VALUE_INDICATE ? true : false);
|
||||
service_enabled();
|
||||
}
|
||||
|
||||
static void nc_ccc_handler(ais_ccc_value_t val)
|
||||
{
|
||||
g_ais.is_notification_enabled = (val == AIS_CCC_VALUE_NOTIFY ? true : false);
|
||||
service_enabled();
|
||||
}
|
||||
|
||||
static size_t wc_write_handler(const void *buf, uint16_t len)
|
||||
{
|
||||
transport_rx((uint8_t *)buf, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
static size_t wwnrc_write_handler(const void *buf, uint16_t len)
|
||||
{
|
||||
transport_rx((uint8_t *)buf, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
ais_bt_init_t ais_attr_info = {
|
||||
/* service */
|
||||
AIS_UUID_DECLARE_16(BLE_UUID_AIS_SERVICE),
|
||||
/* rc */
|
||||
{ .uuid = AIS_UUID_DECLARE_16(BLE_UUID_AIS_RC),
|
||||
.prop = AIS_GATT_CHRC_READ,
|
||||
.perm = AIS_GATT_PERM_READ | AIS_GATT_PERM_READ_AUTHEN,
|
||||
.on_read = NULL,
|
||||
.on_write = NULL,
|
||||
.on_ccc_change = NULL },
|
||||
/* wc */
|
||||
{ .uuid = AIS_UUID_DECLARE_16(BLE_UUID_AIS_WC),
|
||||
.prop = AIS_GATT_CHRC_READ | AIS_GATT_CHRC_WRITE,
|
||||
.perm = AIS_GATT_PERM_READ | AIS_GATT_PERM_WRITE,
|
||||
.on_read = NULL,
|
||||
.on_write = wc_write_handler,
|
||||
.on_ccc_change = NULL },
|
||||
/* ic */
|
||||
{ .uuid = AIS_UUID_DECLARE_16(BLE_UUID_AIS_IC),
|
||||
.prop = AIS_GATT_CHRC_READ | AIS_GATT_CHRC_INDICATE,
|
||||
.perm = AIS_GATT_PERM_READ,
|
||||
.on_read = NULL,
|
||||
.on_write = NULL,
|
||||
.on_ccc_change = ic_ccc_handler },
|
||||
/* wwnrc */
|
||||
{ .uuid = AIS_UUID_DECLARE_16(BLE_UUID_AIS_WWNRC),
|
||||
.prop = AIS_GATT_CHRC_READ | AIS_GATT_CHRC_WRITE_WITHOUT_RESP,
|
||||
.perm = AIS_GATT_PERM_READ | AIS_GATT_PERM_WRITE,
|
||||
.on_read = NULL,
|
||||
.on_write = wwnrc_write_handler,
|
||||
.on_ccc_change = NULL },
|
||||
/* nc */
|
||||
{ .uuid = AIS_UUID_DECLARE_16(BLE_UUID_AIS_NC),
|
||||
.prop = AIS_GATT_CHRC_READ | AIS_GATT_CHRC_NOTIFY,
|
||||
.perm = AIS_GATT_PERM_READ,
|
||||
.on_read = NULL,
|
||||
.on_write = NULL,
|
||||
.on_ccc_change = nc_ccc_handler },
|
||||
connected,
|
||||
disconnected
|
||||
};
|
||||
|
||||
uint32_t ble_ais_init(const ble_ais_init_t *p_ais_init)
|
||||
{
|
||||
memset(&g_ais, 0, sizeof(ble_ais_t));
|
||||
g_ais.conn_handle = BLE_CONN_HANDLE_INVALID;
|
||||
g_ais.is_indication_enabled = false;
|
||||
g_ais.is_notification_enabled = false;
|
||||
g_ais.max_pkt_size = p_ais_init->mtu - 3;
|
||||
|
||||
return ble_stack_init(&ais_attr_info);
|
||||
}
|
||||
|
||||
uint32_t ble_ais_send_notification(uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (g_ais.conn_handle == BLE_CONN_HANDLE_INVALID ||
|
||||
g_ais.is_notification_enabled == false) {
|
||||
return BZ_EINVALIDSTATE;
|
||||
}
|
||||
|
||||
if (length > g_ais.max_pkt_size) {
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
|
||||
err = ble_send_notification(p_data, length);
|
||||
if (err) {
|
||||
return BZ_EGATTNOTIFY;
|
||||
} else {
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
extern transport_t g_transport;
|
||||
static void send_indication_done(uint8_t res)
|
||||
{
|
||||
os_mutex_lock(&(g_transport.tx.mutex_indicate_done), 1000);
|
||||
BREEZE_VERBOSE("sending ind done %d", res);
|
||||
if (res == BZ_SUCCESS) {
|
||||
transport_txdone(1);
|
||||
}
|
||||
os_mutex_unlock(&(g_transport.tx.mutex_indicate_done));
|
||||
}
|
||||
|
||||
uint32_t ble_ais_send_indication(uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (g_ais.conn_handle == BLE_CONN_HANDLE_INVALID ||
|
||||
g_ais.is_indication_enabled == false) {
|
||||
return BZ_EINVALIDSTATE;
|
||||
}
|
||||
|
||||
if (length > g_ais.max_pkt_size) {
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
err = ble_send_indication(p_data, length, send_indication_done);
|
||||
BREEZE_VERBOSE("sending ind:");
|
||||
hex_byte_dump_verbose(p_data, length, 24);
|
||||
|
||||
if (err) {
|
||||
return BZ_EGATTINDICATE;
|
||||
} else {
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
}
|
||||
271
Living_SDK/framework/bluetooth/breeze/core/core.c
Normal file
271
Living_SDK/framework/bluetooth/breeze/core/core.c
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "core.h"
|
||||
#include "transport.h"
|
||||
#include "auth.h"
|
||||
#include "extcmd.h"
|
||||
#include "common.h"
|
||||
#include "ble_service.h"
|
||||
#include "breeze_hal_ble.h"
|
||||
#include "bzopt.h"
|
||||
|
||||
#include "utils.h"
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
#include "sha256.h"
|
||||
#endif
|
||||
|
||||
core_t g_core;
|
||||
extern auth_t g_auth;
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
#define AIS_SEQ_KV_KEY "ais_adv_seq"
|
||||
#define AIS_SEQ_UPDATE_FREQ (1 * 60 * 60) /* in second uint */
|
||||
static uint32_t g_seq = 0;
|
||||
static os_timer_t g_secadv_timer;
|
||||
#endif
|
||||
|
||||
static ali_init_t const *g_ali_init;
|
||||
|
||||
void core_event_notify(uint8_t event_type, uint8_t *data, uint16_t length)
|
||||
{
|
||||
ali_event_t event;
|
||||
|
||||
event.type = event_type;
|
||||
event.rx_data.p_data = data;
|
||||
event.rx_data.length = length;
|
||||
g_core.event_handler(&event);
|
||||
}
|
||||
|
||||
static uint32_t tx_func_indicate(uint8_t cmd, uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
return transport_tx(TX_INDICATION, cmd, p_data, length);
|
||||
}
|
||||
|
||||
static uint32_t ais_init(ali_init_t const *p_init)
|
||||
{
|
||||
ble_ais_init_t init_ais;
|
||||
|
||||
memset(&init_ais, 0, sizeof(ble_ais_init_t));
|
||||
init_ais.mtu = p_init->max_mtu;
|
||||
return ble_ais_init(&init_ais);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
static void update_seq(void *arg1, void *arg2)
|
||||
{
|
||||
os_kv_set(AIS_SEQ_KV_KEY, &g_seq, sizeof(g_seq), 1);
|
||||
os_timer_start(&g_secadv_timer);
|
||||
}
|
||||
|
||||
static void init_seq_number(uint32_t *seq)
|
||||
{
|
||||
int len = sizeof(uint32_t);
|
||||
|
||||
if (!seq)
|
||||
return;
|
||||
|
||||
if (os_kv_get(AIS_SEQ_KV_KEY, seq, &len) != 0) {
|
||||
*seq = 0;
|
||||
len = sizeof(uint32_t);
|
||||
os_kv_set(AIS_SEQ_KV_KEY, seq, len, 1);
|
||||
}
|
||||
|
||||
os_timer_new(&g_secadv_timer, update_seq, NULL, AIS_SEQ_UPDATE_FREQ);
|
||||
os_timer_start(&g_secadv_timer);
|
||||
}
|
||||
|
||||
void set_adv_sequence(uint32_t seq)
|
||||
{
|
||||
g_seq = seq;
|
||||
os_kv_set(AIS_SEQ_KV_KEY, &g_seq, sizeof(g_seq), 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
ret_code_t core_init(ali_init_t const *p_init)
|
||||
{
|
||||
// breeze core base infomation init
|
||||
memset(&g_core, 0, sizeof(core_t));
|
||||
g_core.event_handler = p_init->event_handler;
|
||||
memcpy(g_core.adv_mac, p_init->adv_mac, BZ_BT_MAC_LEN);
|
||||
g_core.product_id = p_init->model_id;
|
||||
// core device info init
|
||||
if((p_init->product_key.p_data != NULL) && (p_init->product_key.length > 0)) {
|
||||
g_core.product_key_len = p_init->product_key.length;
|
||||
memcpy(g_core.product_key, p_init->product_key.p_data, g_core.product_key_len);
|
||||
}
|
||||
if((p_init->product_secret.p_data != NULL) && (p_init->product_secret.length > 0)) {
|
||||
g_core.product_secret_len = p_init->product_secret.length;
|
||||
memcpy(g_core.product_secret, p_init->product_secret.p_data, g_core.product_secret_len);
|
||||
}
|
||||
if((p_init->device_name.p_data != NULL) && (p_init->device_name.length > 0)) {
|
||||
g_core.device_name_len = p_init->device_name.length;
|
||||
memcpy(g_core.device_name, p_init->device_name.p_data, g_core.device_name_len);
|
||||
}
|
||||
if((p_init->device_secret.p_data != NULL) && (p_init->device_secret.length > 0)) {
|
||||
g_core.device_secret_len = p_init->device_secret.length;
|
||||
memcpy(g_core.device_secret, p_init->device_secret.p_data, g_core.device_secret_len);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
init_seq_number(&g_seq);
|
||||
#endif
|
||||
|
||||
ais_init(p_init);
|
||||
transport_init(p_init);
|
||||
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_init(p_init, tx_func_indicate);
|
||||
#endif
|
||||
|
||||
extcmd_init(p_init, tx_func_indicate);
|
||||
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void core_reset(void)
|
||||
{
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_reset();
|
||||
#endif
|
||||
transport_reset();
|
||||
g_core.admin_checkin = 0;
|
||||
g_core.guest_checkin = 0;
|
||||
}
|
||||
|
||||
void core_handle_err(uint8_t src, uint8_t code)
|
||||
{
|
||||
uint8_t err;
|
||||
|
||||
BREEZE_ERR("err at 0x%04x, code 0x%04x", src, code);
|
||||
switch (src & BZ_ERR_MASK) {
|
||||
case BZ_TRANS_ERR:
|
||||
if (code != BZ_EINTERNAL) {
|
||||
if (src == ALI_ERROR_SRC_TRANSPORT_FW_DATA_DISC) {
|
||||
core_event_notify(BZ_EVENT_ERR_DISCONT, NULL, 0);
|
||||
}
|
||||
err = transport_tx(TX_NOTIFICATION, BZ_CMD_ERR, NULL, 0);
|
||||
if (err != BZ_SUCCESS) {
|
||||
BREEZE_ERR("err at 0x%04x, code 0x%04x", ALI_ERROR_SRC_TRANSPORT_SEND, code);
|
||||
}
|
||||
}
|
||||
break;
|
||||
#if BZ_ENABLE_AUTH
|
||||
case BZ_AUTH_ERR:
|
||||
BREEZE_ERR("BZ_AUTH_ERR");
|
||||
auth_reset();
|
||||
if (code == BZ_ETIMEOUT) {
|
||||
ble_disconnect(AIS_BT_REASON_REMOTE_USER_TERM_CONN);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
case BZ_EXTCMD_ERR:
|
||||
BREEZE_ERR("BZ_EXTCMD_ERR");
|
||||
break;
|
||||
default:
|
||||
BREEZE_ERR("unknow bz err\r\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void core_create_bz_adv_data(uint8_t sub_type, uint8_t sec_type, uint8_t bind_state)
|
||||
{
|
||||
uint16_t idx;
|
||||
uint8_t version = 0;
|
||||
uint8_t fmsk = 0;
|
||||
char* p;
|
||||
char* ver_str;
|
||||
|
||||
SET_U16_LE(g_core.adv_data, BZ_ALI_COMPANY_ID);
|
||||
idx = sizeof(uint16_t);
|
||||
|
||||
// extract Breeze version from BZ_VERSION
|
||||
char t_ver_info[20] = { 0 };
|
||||
strncpy(t_ver_info, BZ_VERSION, sizeof(t_ver_info) - 1);
|
||||
p = strtok(t_ver_info, ".");
|
||||
while(p = strtok(NULL, ".")){
|
||||
ver_str = p;
|
||||
}
|
||||
version = (uint8_t)atoi(ver_str);
|
||||
g_core.adv_data[idx++] = (version<<BZ_SDK_VER_Pos) | (sub_type<<BZ_SUB_TYPE_Pos);
|
||||
|
||||
// FMSK byte
|
||||
fmsk = BZ_BLUETOOTH_VER << BZ_FMSK_BLUETOOTH_VER_Pos;
|
||||
#if BZ_ENABLE_OTA
|
||||
fmsk |= 1 << BZ_FMSK_OTA_Pos;
|
||||
#endif
|
||||
#if BZ_ENABLE_AUTH
|
||||
fmsk |= 1 << BZ_FMSK_SECURITY_Pos;
|
||||
if(sec_type == BZ_SEC_TYPE_DEVICE) {
|
||||
BREEZE_DEBUG("Breeze adv per device");
|
||||
fmsk |= 1 << BZ_FMSK_SECRET_TYPE_Pos;
|
||||
g_auth.auth_type = BZ_AUTH_TYPE_PER_DEV;
|
||||
} else if (sec_type == BZ_SEC_TYPE_PRODUCT) {
|
||||
BREEZE_DEBUG("Breeze adv per product");
|
||||
fmsk &= ~(1 << BZ_FMSK_SECRET_TYPE_Pos);
|
||||
g_auth.auth_type = BZ_AUTH_TYPE_PER_PK;
|
||||
} else {
|
||||
BREEZE_ERR("Breeze adv sec type err");
|
||||
g_auth.auth_type = BZ_AUTH_TYPE_NONE;
|
||||
}
|
||||
#endif
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
fmsk |= 1 << BZ_FMSK_SEC_ADV_Pos;
|
||||
#endif
|
||||
if(bind_state && (version >= 6)){
|
||||
BREEZE_DEBUG("Breeze binded");
|
||||
fmsk |= 1 << BZ_FMSK_BIND_STATE_Pos;
|
||||
} else{
|
||||
BREEZE_DEBUG("Breeze unbind");
|
||||
fmsk &= ~(1 << BZ_FMSK_BIND_STATE_Pos);
|
||||
}
|
||||
|
||||
g_core.adv_data[idx++] = fmsk;
|
||||
|
||||
SET_U32_LE(g_core.adv_data + idx, g_core.product_id);
|
||||
idx += sizeof(uint32_t);
|
||||
|
||||
if (g_core.adv_mac[0]==0 && g_core.adv_mac[1]==0 && g_core.adv_mac[2]==0
|
||||
&& g_core.adv_mac[3]==0 && g_core.adv_mac[4]==0 && g_core.adv_mac[5]==0) {
|
||||
ble_get_mac(g_core.adv_mac);
|
||||
}
|
||||
memcpy(&g_core.adv_data[idx], g_core.adv_mac, 6);
|
||||
idx += 6;
|
||||
g_core.adv_data_len = idx;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t core_get_bz_adv_data(uint8_t *p_data, uint16_t *length)
|
||||
{
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
if (*length < (g_core.adv_data_len + 4 + 4)) {
|
||||
#else
|
||||
if (*length < g_core.adv_data_len) {
|
||||
#endif
|
||||
return BZ_ENOMEM;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
uint8_t sign[4];
|
||||
uint32_t seq;
|
||||
|
||||
seq = (++g_seq);
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_calc_adv_sign(seq, sign);
|
||||
#endif
|
||||
memcpy(p_data, g_core.adv_data, g_core.adv_data_len);
|
||||
memcpy(p_data + g_core.adv_data_len, sign, 4);
|
||||
memcpy(p_data + g_core.adv_data_len + 4, &seq, 4);
|
||||
*length = g_core.adv_data_len + 4 + 4;
|
||||
#else
|
||||
memcpy(p_data, g_core.adv_data, g_core.adv_data_len);
|
||||
*length = g_core.adv_data_len;
|
||||
#endif
|
||||
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
947
Living_SDK/framework/bluetooth/breeze/core/extcmd.c
Normal file
947
Living_SDK/framework/bluetooth/breeze/core/extcmd.c
Normal file
|
|
@ -0,0 +1,947 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "extcmd.h"
|
||||
#include "auth.h"
|
||||
#include "common.h"
|
||||
#include "core.h"
|
||||
#include "utils.h"
|
||||
#include "sha256.h"
|
||||
#include "breeze_export.h"
|
||||
#include "chip_code.h"
|
||||
#include "bzopt.h"
|
||||
#include "breeze_hal_os.h"
|
||||
|
||||
#define SSID_READY 0x01
|
||||
#define PASSWORD_READY 0x02
|
||||
#define BSSID_READY 0x04
|
||||
#define APPTOKEN_READY 0x08
|
||||
#define REGION_ID_READY 0x10
|
||||
#define REGION_MQTT_URL_READY 0x20
|
||||
|
||||
#define BASIC_READY (SSID_READY | PASSWORD_READY)
|
||||
#define ALL_READY (SSID_READY | PASSWORD_READY | BSSID_READY | APPTOKEN_READY)
|
||||
|
||||
#define UTF8_MAX_SSID 32
|
||||
#define UTF8_MAX_PASSWORD 64
|
||||
|
||||
enum {
|
||||
BLE_AWSS_CTYPE_SSID = 0x01,
|
||||
BLE_AWSS_CTYPE_PASSWORD = 0x02,
|
||||
BLE_AWSS_CTYPE_BSSID = 0x03,
|
||||
BLE_AWSS_CTYPE_APPTOKEN = 0x04,
|
||||
BLE_AWSS_CTYPE_REGION_ID = 0x05,
|
||||
BLE_AWSS_CTYPE_REGION_MQTTURL = 0x06,
|
||||
BLE_AWSS_CTYPE_3B_RANDOM = 0x07,
|
||||
BLE_AWSS_CTYPE_TOKEN_TYPE = 0x08,
|
||||
BLE_AWSS_CTYPE_PROTOCOL_VER = 0xF1,
|
||||
};
|
||||
|
||||
enum {
|
||||
BZ_AUTH_INFO_AUTH_CODE = 0x01,
|
||||
BZ_AUTH_INFO_AUTH_SECRET = 0x02,
|
||||
BZ_AUTH_INFO_PROTOCOL_VER = 0xF1,
|
||||
};
|
||||
|
||||
enum {
|
||||
BZ_AUTH_SIGN_ACCESS_KEY = 0x01,
|
||||
BZ_AUTH_SIGN_RANDOM = 0x02,
|
||||
BZ_AUTH_SIGN_AUTH_SIGN = 0x03,
|
||||
BZ_AUTH_SIGN_PROTOCOL_VER = 0xF1,
|
||||
};
|
||||
|
||||
typedef ret_code_t (*ext_tlv_handler_t)(uint8_t *p_buff, uint8_t *p_blen,
|
||||
const uint8_t *p_data, uint8_t dlen);
|
||||
typedef struct {
|
||||
uint8_t tlv_type;
|
||||
ext_tlv_handler_t handler;
|
||||
} ext_tlv_type_handler_t;
|
||||
|
||||
extern core_t g_core;
|
||||
#if BZ_ENABLE_AUTH
|
||||
extern auth_t g_auth;
|
||||
#endif
|
||||
|
||||
extern ret_code_t auth_get_device_secret(uint8_t *p_secret, int *p_length);
|
||||
|
||||
extcmd_t g_extcmd;
|
||||
breeze_apinfo_t comboinfo;
|
||||
static uint8_t g_auth_kv_val[16 + 32 + 2] = {0};
|
||||
static int g_auth_kv_val_len = 0;
|
||||
static uint8_t g_auth_need_set = 0;
|
||||
const static char m_sdk_version[] = ":" BZ_VERSION;
|
||||
|
||||
static ret_code_t ext_cmd01_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd02_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd03_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd04_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd05_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
static ret_code_t ext_cmd06_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
static ret_code_t ext_cmd07_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
static ret_code_t ext_cmd09_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd0A_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
#endif
|
||||
|
||||
static ret_code_t ext_cmd0B_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
static ret_code_t ext_cmd0C_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen);
|
||||
|
||||
static const ext_tlv_type_handler_t
|
||||
m_tlv_type_handler_table[] = /**< TLV type handler table. */
|
||||
{ { 0x01, ext_cmd01_rsp},
|
||||
{ 0x02, ext_cmd02_rsp},
|
||||
{ 0x03, ext_cmd03_rsp},
|
||||
{ 0x04, ext_cmd04_rsp},
|
||||
{ 0x05, ext_cmd05_rsp},
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
{ 0x06, ext_cmd06_rsp},
|
||||
#endif
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
{ 0x07, ext_cmd07_rsp},
|
||||
#endif
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
{ 0x09, ext_cmd09_rsp},
|
||||
{ 0x0A, ext_cmd0A_rsp},
|
||||
#endif
|
||||
{ 0x0B, ext_cmd0B_rsp},
|
||||
{ 0x0C, ext_cmd0C_rsp},
|
||||
};
|
||||
|
||||
static ret_code_t ext_cmd01_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_ENOMEM;
|
||||
uint8_t len;
|
||||
|
||||
if (dlen > 0) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
} else if ((len = g_extcmd.tlv_01_rsp_len) <= *p_blen) {
|
||||
memcpy(p_buff, g_extcmd.tlv_01_rsp, len);
|
||||
*p_blen = len;
|
||||
err_code = BZ_SUCCESS;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
static ret_code_t ext_cmd02_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_ENOMEM;
|
||||
uint8_t len;
|
||||
|
||||
if (dlen > 0) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
} else if (g_extcmd.product_key_len == 0) {
|
||||
err_code = BZ_ENOTSUPPORTED;
|
||||
} else if ((len = g_extcmd.product_key_len) <= *p_blen) {
|
||||
memcpy(p_buff, g_extcmd.p_product_key, len);
|
||||
*p_blen = len;
|
||||
|
||||
err_code = BZ_SUCCESS;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
static ret_code_t ext_cmd03_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_ENOMEM;
|
||||
uint8_t len;
|
||||
|
||||
if (dlen > 0) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
} else if (g_extcmd.device_name_len == 0) {
|
||||
err_code = BZ_ENOTSUPPORTED;
|
||||
} else if ((len = g_extcmd.device_name_len) <= *p_blen) {
|
||||
memcpy(p_buff, g_extcmd.p_device_name, len);
|
||||
*p_blen = len;
|
||||
|
||||
err_code = BZ_SUCCESS;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
static ret_code_t ext_cmd04_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_ENOMEM;
|
||||
|
||||
if (dlen > 0) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
} else if (g_extcmd.device_secret_len == 0) {
|
||||
err_code = BZ_ENOTSUPPORTED;
|
||||
} else if (BZ_DEV_RANDOM_LEN <= *p_blen) {
|
||||
get_random(p_buff, BZ_DEV_RANDOM_LEN);
|
||||
*p_blen = BZ_DEV_RANDOM_LEN;
|
||||
err_code = BZ_SUCCESS;
|
||||
}
|
||||
return err_code;
|
||||
}
|
||||
|
||||
static void network_signature_calculate(uint8_t *p_buff)
|
||||
{
|
||||
uint8_t str_id[8], n;
|
||||
uint8_t random_str[32];
|
||||
SHA256_CTX context;
|
||||
uint8_t cli_id[4];
|
||||
|
||||
SET_U32_BE(cli_id, g_extcmd.model_id);
|
||||
hex2string(cli_id, sizeof(cli_id), str_id);
|
||||
sha256_init(&context);
|
||||
|
||||
sha256_update(&context, BZ_CLIENTID_STR, strlen(BZ_CLIENTID_STR));
|
||||
sha256_update(&context, str_id, sizeof(str_id));
|
||||
BREEZE_VERBOSE("%.*s", sizeof(str_id), str_id);
|
||||
|
||||
sha256_update(&context, BZ_DEVICE_NAME_STR, strlen(BZ_DEVICE_NAME_STR)); /* "deviceName" */
|
||||
sha256_update(&context, g_extcmd.p_device_name, g_extcmd.device_name_len);
|
||||
BREEZE_VERBOSE("%.*s", g_extcmd.device_name_len, g_extcmd.p_device_name);
|
||||
|
||||
#if BZ_ENABLE_AUTH
|
||||
if(g_auth.dyn_update_device_secret == true || g_auth.device_secret_len == BZ_DEV_DEVICE_SECRET_LEN){
|
||||
sha256_update(&context, BZ_DEVICE_SECRET_STR, strlen(BZ_DEVICE_SECRET_STR)); /* "deviceSecret" */
|
||||
sha256_update(&context, g_auth.device_secret, g_auth.device_secret_len);
|
||||
BREEZE_VERBOSE("%.*s", g_auth.device_secret_len, g_auth.device_secret);
|
||||
} else{
|
||||
sha256_update(&context, BZ_PRODUCT_SECRET_STR, strlen(BZ_PRODUCT_SECRET_STR)); /* "productSecret" */
|
||||
sha256_update(&context, g_core.product_secret, g_core.product_secret_len);
|
||||
BREEZE_VERBOSE("%.*s", g_core.product_secret_len, g_core.product_secret);
|
||||
}
|
||||
#else
|
||||
sha256_update(&context, BZ_DEVICE_SECRET_STR, strlen(BZ_DEVICE_SECRET_STR)); /* "deviceSecret" */
|
||||
sha256_update(&context, g_extcmd.p_device_secret, g_extcmd.device_secret_len);
|
||||
#endif
|
||||
|
||||
sha256_update(&context, BZ_PRODUCT_KEY_STR, strlen(BZ_PRODUCT_KEY_STR)); /* "productKey" */
|
||||
sha256_update(&context, g_extcmd.p_product_key, g_extcmd.product_key_len);
|
||||
BREEZE_VERBOSE("%.*s", g_extcmd.product_key_len, g_extcmd.p_product_key);
|
||||
|
||||
sha256_final(&context, p_buff);
|
||||
}
|
||||
|
||||
static ret_code_t ext_cmd05_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_ENOMEM;
|
||||
|
||||
if (dlen > 0) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
} else if (*p_blen >= SHA256_BLOCK_SIZE) {
|
||||
network_signature_calculate(p_buff);
|
||||
*p_blen = SHA256_BLOCK_SIZE;
|
||||
err_code = BZ_SUCCESS;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
static ret_code_t ext_cmd06_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
uint8_t idx = 0, tlvtype, tlvlen;
|
||||
static uint8_t ready_flag = 0;
|
||||
uint8_t rsp[] = { 0x01, 0x01, 0x01, 0x03, 0x01, 0x00 };
|
||||
|
||||
if (dlen < 2) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (*p_blen < sizeof(rsp)) {
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&comboinfo, 0, sizeof(comboinfo));
|
||||
comboinfo.region_type = 0xFF;
|
||||
while (idx < dlen) {
|
||||
tlvtype = p_data[idx++];
|
||||
tlvlen = p_data[idx++];
|
||||
|
||||
switch (tlvtype) {
|
||||
case BLE_AWSS_CTYPE_SSID: /* utf8 */
|
||||
if (tlvlen < 1 || tlvlen > UTF8_MAX_SSID) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
utf8_to_pw(&p_data[idx], tlvlen, comboinfo.ssid);
|
||||
ready_flag |= SSID_READY;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_PASSWORD: /* utf8 */
|
||||
if (tlvlen > UTF8_MAX_PASSWORD) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
utf8_to_pw(&p_data[idx], tlvlen, comboinfo.pw);
|
||||
ready_flag |= PASSWORD_READY;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_BSSID: /* 6-byte hex */
|
||||
if (tlvlen != 6) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(comboinfo.bssid, &p_data[idx], tlvlen);
|
||||
ready_flag |= BSSID_READY;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_APPTOKEN: /*16 bytes hex */
|
||||
if ((tlvlen > MAX_TOKEN_PARAM_LEN)||
|
||||
(!ready_flag & BSSID_READY)) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
comboinfo.apptoken_len = tlvlen;
|
||||
memcpy(comboinfo.apptoken, &p_data[idx], tlvlen);
|
||||
ready_flag |= APPTOKEN_READY;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_REGION_ID:
|
||||
if (tlvlen != 1) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(&(comboinfo.region_id), &p_data[idx], tlvlen);
|
||||
comboinfo.region_type = 0;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_REGION_MQTTURL:
|
||||
if (tlvlen >= 128) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(comboinfo.region_mqtturl, &p_data[idx], tlvlen);
|
||||
comboinfo.region_type = 1;
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_3B_RANDOM:
|
||||
if (tlvlen != 3) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(&(comboinfo.rand), &p_data[idx], tlvlen);
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_TOKEN_TYPE:
|
||||
if (tlvlen != 1) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(&(comboinfo.token_type), &p_data[idx], tlvlen);
|
||||
break;
|
||||
case BLE_AWSS_CTYPE_PROTOCOL_VER:
|
||||
if (tlvlen != 1) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
memcpy(&(comboinfo.protocol_ver), &p_data[idx], tlvlen);
|
||||
break;
|
||||
default:
|
||||
BREEZE_DEBUG("unknown apinfo type");
|
||||
break;
|
||||
}
|
||||
|
||||
idx += tlvlen;
|
||||
}
|
||||
|
||||
end:
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
rsp[2] = 2; /* set failure code */
|
||||
}
|
||||
rsp[5] = comboinfo.token_type;
|
||||
|
||||
/* rsp */
|
||||
memcpy(p_buff, rsp, sizeof(rsp));
|
||||
*p_blen = sizeof(rsp);
|
||||
|
||||
if (ready_flag & BASIC_READY) {
|
||||
core_event_notify(BZ_EVENT_APINFO, &comboinfo, sizeof(comboinfo));
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_AIS_SECURE_ADV
|
||||
static ret_code_t ext_cmd07_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
uint8_t ret_code = 1, i = 0;
|
||||
uint32_t seq = 0;
|
||||
|
||||
if (dlen != sizeof(seq)) {
|
||||
BREEZE_ERR("Error: invalid sequence data size");
|
||||
err_code = BZ_EDATASIZE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (*p_blen < 1) {
|
||||
BREEZE_ERR("Error: invalid sequence rsp buffer size");
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
BREEZE_DEBUG("The sequence bytes from cloud: %02x %02x %02x %02x", p_data[0],
|
||||
p_data[1], p_data[2], p_data[3]);
|
||||
|
||||
while (i < sizeof(seq)) {
|
||||
seq |= p_data[i] << (i << 3);
|
||||
i++;
|
||||
}
|
||||
|
||||
BREEZE_DEBUG("The sequence hex to be saved is %08x", seq);
|
||||
set_adv_sequence(seq);
|
||||
|
||||
end:
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
ret_code = 0; /* set failure code */
|
||||
}
|
||||
|
||||
/* rsp */
|
||||
p_buff[0] = ret_code;
|
||||
*p_blen = 1;
|
||||
|
||||
return err_code;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
bool ext_get_device_secret()
|
||||
{
|
||||
char tmp_secret[BZ_DEV_DEVICE_SECRET_LEN];
|
||||
int len = BZ_DEV_DEVICE_SECRET_LEN;
|
||||
if(auth_get_device_secret(&tmp_secret, &len) == 0){
|
||||
if(g_auth.device_secret_len == 0){
|
||||
g_auth.device_secret_len = len;
|
||||
memcpy(g_auth.device_secret, tmp_secret, len);
|
||||
} else if(len == BZ_DEV_DEVICE_SECRET_LEN && memcmp(tmp_secret, g_auth.device_secret, len) != 0){
|
||||
memcpy(g_auth.device_secret, tmp_secret, len);
|
||||
g_auth.dyn_update_device_secret = true;
|
||||
}
|
||||
return true;
|
||||
} else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ext_set_device_secret(const uint8_t* p_ds, uint8_t ds_len)
|
||||
{
|
||||
return (os_kv_set(BZ_DEVICE_SECRET_STR, p_ds, ds_len, 1) == 0)? true: false;
|
||||
}
|
||||
|
||||
bool ext_del_device_secret(void)
|
||||
{
|
||||
return (os_kv_del(BZ_DEVICE_SECRET_STR) == 0)? true: false;
|
||||
}
|
||||
|
||||
static ret_code_t ext_cmd09_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
if (dlen > 0 ) {
|
||||
BREEZE_ERR("Err:invalid product security update param");
|
||||
err_code = BZ_EDATASIZE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (*p_blen < 1) {
|
||||
BREEZE_ERR("Err:invalid product security update rsp buffer size");
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(ext_get_device_secret() == false){
|
||||
p_buff[0] = 0x01;
|
||||
} else{
|
||||
p_buff[0] = 0x00;
|
||||
}
|
||||
memcpy(p_buff+1, g_extcmd.p_device_name, g_extcmd.device_name_len);
|
||||
*p_blen = g_extcmd.device_name_len + 1;
|
||||
err_code = BZ_SUCCESS;
|
||||
|
||||
end:
|
||||
return err_code;
|
||||
}
|
||||
|
||||
static ret_code_t ext_cmd0A_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
if (*p_blen < 1) {
|
||||
BREEZE_ERR("Err:invalid DS update rsp buffer size");
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(dlen != BZ_DEV_DEVICE_SECRET_LEN){
|
||||
BREEZE_ERR("Err:invalid DS update len");
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if(ext_get_device_secret() == true){
|
||||
p_buff[0] = 0x01;
|
||||
} else if(ext_set_device_secret(p_data, dlen) == true){
|
||||
auth_secret_update_post_process(p_data, dlen);
|
||||
p_buff[0] = 0x00;
|
||||
} else{
|
||||
p_buff[0] = 0x02;
|
||||
}
|
||||
|
||||
*p_blen = 1;
|
||||
|
||||
end:
|
||||
return err_code;
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
static ret_code_t ext_cmd0B_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
uint8_t idx = 0, tlvtype, tlvlen;
|
||||
uint8_t rsp[] = { 0x01, 0x01, 0x01 };
|
||||
uint8_t auth_version = 0;
|
||||
uint8_t *p_auth_code = NULL;
|
||||
uint8_t auth_code_len = 0;
|
||||
uint8_t *p_auth_secret = NULL;
|
||||
uint8_t auth_secret_len = 0;
|
||||
uint8_t has_ac = 0;
|
||||
uint8_t auth_event = 0;
|
||||
|
||||
if (dlen < 2) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (*p_blen < sizeof(rsp)) {
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
g_auth_kv_val_len = sizeof(g_auth_kv_val);
|
||||
if (os_kv_get(BZ_AUTH_CODE_KV_PREFIX, g_auth_kv_val, &g_auth_kv_val_len) != 0) {
|
||||
BREEZE_DEBUG("no auth key get, AC can be add");
|
||||
} else {
|
||||
has_ac = 1;
|
||||
// Found AC, need manager checkin to modify it
|
||||
if (g_core.admin_checkin == 0) {
|
||||
err_code = BZ_ERROR_AC_AS_NO_PERMIT;
|
||||
BREEZE_DEBUG("no admin modify AC");
|
||||
goto end;
|
||||
} else {
|
||||
BREEZE_DEBUG("AC can be update");
|
||||
}
|
||||
}
|
||||
|
||||
while (idx < dlen) {
|
||||
tlvtype = p_data[idx++];
|
||||
tlvlen = p_data[idx++];
|
||||
|
||||
switch (tlvtype) {
|
||||
case BZ_AUTH_INFO_AUTH_CODE:
|
||||
if (tlvlen > 16) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
p_auth_code = p_data + idx;
|
||||
auth_code_len = tlvlen;
|
||||
BREEZE_VERBOSE("AC:%.*s", auth_code_len, p_auth_code);
|
||||
break;
|
||||
case BZ_AUTH_INFO_AUTH_SECRET:
|
||||
if (tlvlen < 1 || tlvlen > 32) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
p_auth_secret = p_data + idx;
|
||||
auth_secret_len = tlvlen;
|
||||
BREEZE_VERBOSE("AS:%.*s", auth_secret_len, p_auth_secret);
|
||||
break;
|
||||
case BZ_AUTH_INFO_PROTOCOL_VER:
|
||||
if (tlvlen != 1) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
auth_version = p_data[idx];
|
||||
break;
|
||||
default:
|
||||
BREEZE_DEBUG("unknown AC/AS type");
|
||||
break;
|
||||
}
|
||||
|
||||
idx += tlvlen;
|
||||
}
|
||||
|
||||
g_auth_kv_val_len = 0;
|
||||
if (auth_code_len == 0) {
|
||||
// need clear AC & AS
|
||||
if (has_ac) {
|
||||
if (os_kv_del(BZ_AUTH_CODE_KV_PREFIX) != 0) {
|
||||
BREEZE_ERR("AC&AS clear failed");
|
||||
err_code = BZ_ERROR_AC_AS_DELETE;
|
||||
} else {
|
||||
auth_event = BZ_AC_AS_DELETE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// need Add or Update AC & AS
|
||||
g_auth_kv_val[g_auth_kv_val_len++] = auth_code_len;
|
||||
memcpy(g_auth_kv_val + g_auth_kv_val_len, p_auth_code, auth_code_len);
|
||||
g_auth_kv_val_len += auth_code_len;
|
||||
if (auth_secret_len) {
|
||||
g_auth_kv_val[g_auth_kv_val_len++] = auth_secret_len;
|
||||
memcpy(g_auth_kv_val + g_auth_kv_val_len, p_auth_secret, auth_secret_len);
|
||||
g_auth_kv_val_len += auth_secret_len;
|
||||
} else {
|
||||
// do nothing, no auth secret found
|
||||
}
|
||||
g_auth_need_set = 1;
|
||||
|
||||
if (has_ac) {
|
||||
auth_event = BZ_AC_AS_UPDATE;
|
||||
} else {
|
||||
auth_event = BZ_AC_AS_ADD;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
rsp[2] = 2; /* set failure code */
|
||||
} else {
|
||||
// notify to upper layer
|
||||
core_event_notify(BZ_EVENT_AC_AS, &auth_event, sizeof(auth_event));
|
||||
}
|
||||
|
||||
/* rsp */
|
||||
memcpy(p_buff, rsp, sizeof(rsp));
|
||||
*p_blen = sizeof(rsp);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
extern void utils_hmac_sha1_raw(const char *msg, int msg_len, char *digest, const char *key, int key_len);
|
||||
extern void utils_hmac_sha1_base64(const char *msg, int msg_len, const char *key, int key_len, char *digest, int *digest_len);
|
||||
static ret_code_t ext_cmd0C_rsp(uint8_t *p_buff, uint8_t *p_blen, const uint8_t *p_data, uint8_t dlen)
|
||||
{
|
||||
ret_code_t err_code = BZ_SUCCESS;
|
||||
uint8_t idx = 0, tlvtype, tlvlen;
|
||||
uint8_t rsp[] = { 0x01, 0x01, 0x01 };
|
||||
uint8_t sign_version = 0;
|
||||
uint8_t access_key[17] = {0};
|
||||
uint8_t access_key_len = 0;
|
||||
uint8_t access_random[17] = {0};
|
||||
uint8_t access_random_len = 0;
|
||||
uint8_t auth_sign[20] = {0};
|
||||
uint8_t auth_sign_len = 0;
|
||||
uint8_t access_token[30];
|
||||
int access_token_len = sizeof(access_token);
|
||||
uint8_t auth_kv_data[16 + 32 + 2] = {0};
|
||||
int auth_kv_len = sizeof(auth_kv_data);
|
||||
uint8_t auth_code[17] = {0};
|
||||
uint8_t auth_code_len = 0;
|
||||
uint8_t auth_sec[33] = {0};
|
||||
uint8_t auth_sec_len = 0;
|
||||
char sign_out[20];
|
||||
uint8_t sign_event = 0;
|
||||
|
||||
if (dlen < 2) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (*p_blen < sizeof(rsp)) {
|
||||
err_code = BZ_ENOMEM;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (os_kv_get(BZ_AUTH_CODE_KV_PREFIX, auth_kv_data, &auth_kv_len) != 0) {
|
||||
BREEZE_DEBUG("no auth key get");
|
||||
g_core.admin_checkin = 1;
|
||||
g_core.guest_checkin = 1;
|
||||
sign_event = BZ_AUTH_SIGN_NO_CHECK_PASS;
|
||||
goto end;
|
||||
}
|
||||
if (auth_kv_len > 0) {
|
||||
auth_code_len = auth_kv_data[0];
|
||||
if (auth_kv_len < auth_code_len + 1) {
|
||||
BREEZE_ERR("auth kv len err(%d < %d)", auth_kv_len, auth_code_len + 1);
|
||||
err_code = BZ_ERROR_AC_AS_DATA_LEN;
|
||||
goto end;
|
||||
}
|
||||
memcpy(auth_code, auth_kv_data + 1, auth_code_len);
|
||||
if (auth_kv_len > (1 + auth_code_len)) {
|
||||
auth_sec_len = auth_kv_data[1 + auth_code_len];
|
||||
memcpy(auth_sec, auth_kv_data + 1 + auth_code_len + 1, auth_sec_len);
|
||||
} else {
|
||||
auth_sec_len = g_extcmd.device_secret_len;
|
||||
memcpy(auth_sec, g_extcmd.p_device_secret, auth_sec_len);
|
||||
}
|
||||
} else {
|
||||
BREEZE_ERR("auth kv len err(%d)", auth_kv_len);
|
||||
err_code = BZ_ERROR_AC_AS_DATA_LEN;
|
||||
goto end;
|
||||
}
|
||||
|
||||
while (idx < dlen) {
|
||||
tlvtype = p_data[idx++];
|
||||
tlvlen = p_data[idx++];
|
||||
|
||||
switch (tlvtype) {
|
||||
case BZ_AUTH_SIGN_ACCESS_KEY:
|
||||
if (tlvlen < 1 || tlvlen > 16) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
access_key_len = tlvlen;
|
||||
memcpy(access_key, &p_data[idx], tlvlen);
|
||||
BREEZE_VERBOSE("AK:%.*s", access_key_len, access_key);
|
||||
break;
|
||||
case BZ_AUTH_SIGN_RANDOM:
|
||||
if (tlvlen < 1 || tlvlen > 16) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
access_random_len = tlvlen;
|
||||
memcpy(access_random, &p_data[idx], tlvlen);
|
||||
BREEZE_VERBOSE("Rand:");
|
||||
hex_byte_dump_verbose(access_random, access_random_len, 24);
|
||||
break;
|
||||
case BZ_AUTH_SIGN_AUTH_SIGN:
|
||||
if (tlvlen < 1 || tlvlen > 32) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
auth_sign_len = tlvlen;
|
||||
memcpy(auth_sign, &p_data[idx], tlvlen);
|
||||
BREEZE_VERBOSE("ASign:");
|
||||
hex_byte_dump_verbose(auth_sign, auth_sign_len, 24);
|
||||
break;
|
||||
case BZ_AUTH_SIGN_PROTOCOL_VER:
|
||||
if (tlvlen != 1) {
|
||||
err_code = BZ_EINVALIDTLV;
|
||||
goto end;
|
||||
}
|
||||
sign_version = p_data[idx];
|
||||
break;
|
||||
default:
|
||||
BREEZE_DEBUG("unknown AuthSign type");
|
||||
break;
|
||||
}
|
||||
|
||||
idx += tlvlen;
|
||||
}
|
||||
if (!access_key_len || !access_random_len || !auth_sign_len) {
|
||||
err_code = BZ_ERROR_AUTH_DATA;
|
||||
goto end;
|
||||
}
|
||||
// check AccessKey
|
||||
if (memcmp(auth_code, access_key, auth_code_len) == 0) {
|
||||
// check AuthSign
|
||||
utils_hmac_sha1_base64((const char *)access_key, (int)access_key_len,
|
||||
(const char *)auth_sec, (int)auth_sec_len,
|
||||
(char *)access_token, &access_token_len);
|
||||
BREEZE_VERBOSE("AT:%.*s", access_token_len, access_token);
|
||||
utils_hmac_sha1_raw((const char *)access_random, (int)access_random_len,
|
||||
(char *)sign_out,
|
||||
(const char *)access_token, (int)access_token_len);
|
||||
BREEZE_VERBOSE("sign_out:");
|
||||
hex_byte_dump_verbose(sign_out, sizeof(sign_out), 24);
|
||||
if (memcmp(sign_out, auth_sign, auth_sign_len) == 0) {
|
||||
BREEZE_DEBUG("AC auth ok");
|
||||
if (access_key[access_key_len - 1] == '0') {
|
||||
g_core.admin_checkin = 1;
|
||||
} else if (access_key[access_key_len - 1] == '1') {
|
||||
g_core.guest_checkin = 1;
|
||||
}
|
||||
sign_event = BZ_AUTH_SIGN_CHECK_PASS;
|
||||
} else {
|
||||
BREEZE_DEBUG("AC auth fail");
|
||||
err_code = BZ_ERROR_AUTH_SIGN;
|
||||
}
|
||||
} else {
|
||||
BREEZE_DEBUG("AC auth fail");
|
||||
err_code = BZ_ERROR_AUTH_SIGN;
|
||||
}
|
||||
|
||||
end:
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
rsp[2] = 2; /* set failure code */
|
||||
} else {
|
||||
// notify to upper layer
|
||||
core_event_notify(BZ_EVENT_AUTH_SIGN, &sign_event, sizeof(sign_event));
|
||||
}
|
||||
|
||||
/* rsp */
|
||||
memcpy(p_buff, rsp, sizeof(rsp));
|
||||
*p_blen = sizeof(rsp);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
static void get_os_info(void)
|
||||
{
|
||||
uint8_t chip_code[4] = { 0 };
|
||||
uint8_t chip_id_str[8] = { 0 };
|
||||
const char *aostype = "AOS";
|
||||
uint8_t suffix_len = 0;
|
||||
char t_os_info[20] = { 0 };
|
||||
#ifdef BUILD_AOS
|
||||
strcpy(t_os_info, aos_version_get());
|
||||
char *m_os_type = strtok(t_os_info, "-");
|
||||
if (strcmp(aostype, m_os_type) == 0) {
|
||||
m_os_type = strtok(NULL, "-");
|
||||
m_os_type = strtok(NULL, "-");
|
||||
strcat(m_os_type, ":");
|
||||
BREEZE_TRACE("AOS version %s(%d)\n", m_os_type, strlen(m_os_type));
|
||||
|
||||
suffix_len = strlen(m_os_type);
|
||||
memcpy(g_extcmd.tlv_01_rsp, m_os_type, suffix_len);
|
||||
chip_code_st *p_chip_code_obj = get_chip_code(MCU_FAMILY);
|
||||
if (p_chip_code_obj != NULL) {
|
||||
chip_code[0] = (uint8_t)(p_chip_code_obj->vendor >> 8);
|
||||
chip_code[1] = (uint8_t)p_chip_code_obj->vendor;
|
||||
chip_code[2] = (uint8_t)(p_chip_code_obj->id >> 8);
|
||||
chip_code[3] = (uint8_t)p_chip_code_obj->id;
|
||||
}
|
||||
|
||||
hex2string(chip_code, sizeof(chip_code), chip_id_str);
|
||||
memcpy(g_extcmd.tlv_01_rsp + suffix_len, chip_id_str, sizeof(chip_id_str));
|
||||
suffix_len += sizeof(chip_id_str);
|
||||
memcpy(g_extcmd.tlv_01_rsp + suffix_len, m_sdk_version, sizeof(m_sdk_version) - 1);
|
||||
suffix_len += sizeof(m_sdk_version) - 1;
|
||||
g_extcmd.tlv_01_rsp[suffix_len] = '\0';
|
||||
strcat(g_extcmd.tlv_01_rsp, ":1");
|
||||
suffix_len = strlen(g_extcmd.tlv_01_rsp);
|
||||
}
|
||||
#else
|
||||
memcpy(g_extcmd.tlv_01_rsp, "NON-AOS", strlen("NON-AOS"));
|
||||
g_extcmd.tlv_01_rsp[suffix_len] = '\0';
|
||||
suffix_len = strlen("NON-AOS");
|
||||
#endif
|
||||
g_extcmd.tlv_01_rsp_len = suffix_len;
|
||||
}
|
||||
|
||||
ret_code_t extcmd_init(ali_init_t const *p_init, tx_func_t tx_func)
|
||||
{
|
||||
memset(&g_extcmd, 0, sizeof(extcmd_t));
|
||||
get_os_info();
|
||||
|
||||
if ((p_init->product_key.p_data != NULL) && (p_init->product_key.length > 0)) {
|
||||
g_extcmd.product_key_len = g_core.product_key_len;
|
||||
g_extcmd.p_product_key = g_core.product_key;
|
||||
}
|
||||
if ((p_init->device_name.p_data != NULL) && (p_init->device_name.length > 0)) {
|
||||
g_extcmd.device_name_len = g_core.device_name_len;
|
||||
g_extcmd.p_device_name = g_core.device_name;
|
||||
}
|
||||
if ((p_init->device_secret.p_data != NULL) && (p_init->device_secret.length > 0)) {
|
||||
g_extcmd.device_secret_len = g_core.device_secret_len;
|
||||
g_extcmd.p_device_secret = g_core.device_secret;
|
||||
}
|
||||
|
||||
g_extcmd.tx_func = tx_func;
|
||||
g_extcmd.model_id = p_init->model_id;
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
void extcmd_rx_command(uint8_t cmd, uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
if (length == 0 || cmd != BZ_CMD_EXT_DOWN) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *p_tx_buff = g_extcmd.tx_buff;
|
||||
uint8_t tx_buff_avail = sizeof(g_extcmd.tx_buff);
|
||||
uint8_t tx_buff_size;
|
||||
uint8_t tlv_mask, tlv_masked = 0;
|
||||
uint8_t tlv_type, tlv_len;
|
||||
|
||||
uint32_t err_code = BZ_SUCCESS;
|
||||
|
||||
g_auth_need_set = 0;
|
||||
|
||||
BREEZE_DEBUG("extcmd_rx_command");
|
||||
hex_byte_dump_debug(p_data, length, 24);
|
||||
|
||||
#if BZ_ENABLE_AUTH
|
||||
if (!auth_is_authdone()) {
|
||||
err_code = BZ_EINVALIDSTATE;
|
||||
}
|
||||
#endif
|
||||
|
||||
while (length > 0 && err_code == BZ_SUCCESS) {
|
||||
if (length >= 2) {
|
||||
/* get TLV type. */
|
||||
tlv_type = *p_data++;
|
||||
/* get TLV length. */
|
||||
tlv_len = *p_data++;
|
||||
|
||||
length -= 2;
|
||||
} else {
|
||||
err_code = BZ_EINVALIDLEN;
|
||||
break;
|
||||
}
|
||||
|
||||
/* each TLV type should not get repeated. */
|
||||
tlv_mask = (1 << tlv_type);
|
||||
if ((tlv_mask & tlv_masked) != 0) {
|
||||
err_code = BZ_EINVALIDDATA;
|
||||
break;
|
||||
}
|
||||
tlv_masked |= tlv_mask;
|
||||
|
||||
/* check that TLV length does not exceed input data boundary. */
|
||||
if (tlv_len > length) {
|
||||
err_code = BZ_EDATASIZE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (tx_buff_avail < 2) {
|
||||
err_code = BZ_ENOMEM;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t n, n_max = sizeof(m_tlv_type_handler_table) / sizeof(ext_tlv_type_handler_t);
|
||||
for (n = 0; n < n_max; n++) {
|
||||
if (m_tlv_type_handler_table[n].tlv_type == tlv_type) {
|
||||
tx_buff_size = tx_buff_avail - 2;
|
||||
err_code = m_tlv_type_handler_table[n].handler(p_tx_buff + 2, &tx_buff_size, p_data, tlv_len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (n >= n_max) {
|
||||
err_code = BZ_EINVALIDDATA;
|
||||
}
|
||||
|
||||
if (err_code == BZ_SUCCESS) {
|
||||
*(p_tx_buff + 0) = tlv_type;
|
||||
*(p_tx_buff + 1) = tx_buff_size;
|
||||
|
||||
p_tx_buff += (2 + tx_buff_size);
|
||||
tx_buff_avail -= (2 + tx_buff_size);
|
||||
|
||||
p_data += tlv_len;
|
||||
length -= tlv_len;
|
||||
}
|
||||
}
|
||||
|
||||
if (err_code == BZ_SUCCESS) {
|
||||
err_code = g_extcmd.tx_func(BZ_CMD_EXT_UP, g_extcmd.tx_buff, sizeof(g_extcmd.tx_buff) - tx_buff_avail);
|
||||
if (err_code == BZ_SUCCESS) {
|
||||
if (g_auth_need_set && g_auth_kv_val_len) {
|
||||
if (os_kv_set(BZ_AUTH_CODE_KV_PREFIX, g_auth_kv_val, g_auth_kv_val_len, 1) != 0) {
|
||||
BREEZE_ERR("AC&AS set failed");
|
||||
err_code = BZ_ERROR_AC_AS_STORE;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err_code = g_extcmd.tx_func(BZ_CMD_ERR, NULL, 0);
|
||||
}
|
||||
|
||||
if (err_code != BZ_SUCCESS) {
|
||||
core_handle_err(ALI_ERROR_SRC_EXT_SEND_RSP, err_code);
|
||||
}
|
||||
}
|
||||
148
Living_SDK/framework/bluetooth/breeze/core/sha256.c
Normal file
148
Living_SDK/framework/bluetooth/breeze/core/sha256.c
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "sha256.h"
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
|
||||
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
|
||||
|
||||
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
|
||||
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
|
||||
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
|
||||
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
|
||||
|
||||
/**************************** VARIABLES *****************************/
|
||||
static const WORD k[64] = {
|
||||
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
|
||||
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
|
||||
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
|
||||
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
|
||||
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
|
||||
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
|
||||
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
|
||||
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
|
||||
};
|
||||
|
||||
/*********************** FUNCTION DEFINITIONS ***********************/
|
||||
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
|
||||
{
|
||||
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
|
||||
|
||||
for (i = 0, j = 0; i < 16; ++i, j += 4)
|
||||
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
|
||||
for ( ; i < 64; ++i)
|
||||
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
|
||||
|
||||
a = ctx->state[0];
|
||||
b = ctx->state[1];
|
||||
c = ctx->state[2];
|
||||
d = ctx->state[3];
|
||||
e = ctx->state[4];
|
||||
f = ctx->state[5];
|
||||
g = ctx->state[6];
|
||||
h = ctx->state[7];
|
||||
|
||||
for (i = 0; i < 64; ++i) {
|
||||
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
|
||||
t2 = EP0(a) + MAJ(a,b,c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
ctx->state[0] += a;
|
||||
ctx->state[1] += b;
|
||||
ctx->state[2] += c;
|
||||
ctx->state[3] += d;
|
||||
ctx->state[4] += e;
|
||||
ctx->state[5] += f;
|
||||
ctx->state[6] += g;
|
||||
ctx->state[7] += h;
|
||||
}
|
||||
|
||||
void sha256_init(SHA256_CTX *ctx)
|
||||
{
|
||||
ctx->datalen = 0;
|
||||
ctx->bitlen = 0;
|
||||
ctx->state[0] = 0x6a09e667;
|
||||
ctx->state[1] = 0xbb67ae85;
|
||||
ctx->state[2] = 0x3c6ef372;
|
||||
ctx->state[3] = 0xa54ff53a;
|
||||
ctx->state[4] = 0x510e527f;
|
||||
ctx->state[5] = 0x9b05688c;
|
||||
ctx->state[6] = 0x1f83d9ab;
|
||||
ctx->state[7] = 0x5be0cd19;
|
||||
}
|
||||
|
||||
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
|
||||
{
|
||||
WORD i;
|
||||
|
||||
for (i = 0; i < len; ++i) {
|
||||
ctx->data[ctx->datalen] = data[i];
|
||||
ctx->datalen++;
|
||||
if (ctx->datalen == 64) {
|
||||
sha256_transform(ctx, ctx->data);
|
||||
ctx->bitlen += 512;
|
||||
ctx->datalen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
|
||||
{
|
||||
WORD i;
|
||||
|
||||
i = ctx->datalen;
|
||||
|
||||
// Pad whatever data is left in the buffer.
|
||||
if (ctx->datalen < 56) {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 56)
|
||||
ctx->data[i++] = 0x00;
|
||||
}
|
||||
else {
|
||||
ctx->data[i++] = 0x80;
|
||||
while (i < 64)
|
||||
ctx->data[i++] = 0x00;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
memset(ctx->data, 0, 56);
|
||||
}
|
||||
|
||||
// Append to the padding the total message's length in bits and transform.
|
||||
ctx->bitlen += ctx->datalen * 8;
|
||||
ctx->data[63] = ctx->bitlen;
|
||||
ctx->data[62] = ctx->bitlen >> 8;
|
||||
ctx->data[61] = ctx->bitlen >> 16;
|
||||
ctx->data[60] = ctx->bitlen >> 24;
|
||||
ctx->data[59] = ctx->bitlen >> 32;
|
||||
ctx->data[58] = ctx->bitlen >> 40;
|
||||
ctx->data[57] = ctx->bitlen >> 48;
|
||||
ctx->data[56] = ctx->bitlen >> 56;
|
||||
sha256_transform(ctx, ctx->data);
|
||||
|
||||
// Since this implementation uses little endian byte ordering and SHA uses big endian,
|
||||
// reverse all the bytes when copying the final state to the output hash.
|
||||
for (i = 0; i < 4; ++i) {
|
||||
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
|
||||
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
|
||||
}
|
||||
}
|
||||
491
Living_SDK/framework/bluetooth/breeze/core/transport.c
Normal file
491
Living_SDK/framework/bluetooth/breeze/core/transport.c
Normal file
|
|
@ -0,0 +1,491 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "transport.h"
|
||||
#include "core.h"
|
||||
#include "common.h"
|
||||
#include "utils.h"
|
||||
#include "ble_service.h"
|
||||
|
||||
#include "breeze_hal_sec.h"
|
||||
#include "breeze_hal_os.h"
|
||||
|
||||
#define HEADER_SIZE 4
|
||||
#define AES_BLK_SIZE 16
|
||||
|
||||
#define IS_ENC(data) ((data[0] & 0x10) != 0)
|
||||
#define MSG_ID(data) (data[0] & 0xf)
|
||||
#define CMD_TYPE(data) (data[1])
|
||||
#define TOTAL_FRAME(data) ((data[2] >> 4) & 0x0f)
|
||||
#define FRAME_SEQ(data) (data[2] & 0x0f)
|
||||
#define FRAME_LEN(data) (data[3])
|
||||
|
||||
#if BZ_ENABLE_AUTH
|
||||
extern bool g_dn_complete;
|
||||
#endif
|
||||
transport_t g_transport;
|
||||
struct rx_cmd_post_t rx_cmd_post;
|
||||
|
||||
static void reset_tx(void)
|
||||
{
|
||||
g_transport.tx.len = 0;
|
||||
g_transport.tx.bytes_sent = 0;
|
||||
g_transport.tx.msg_id = 0;
|
||||
g_transport.tx.cmd = 0;
|
||||
g_transport.tx.total_frame = 0;
|
||||
g_transport.tx.frame_seq = 0;
|
||||
g_transport.tx.pkt_req = 0;
|
||||
g_transport.tx.pkt_cfm = 0;
|
||||
if (g_transport.timeout != 0) {
|
||||
os_timer_stop(&g_transport.tx.timer);
|
||||
}
|
||||
}
|
||||
|
||||
static void reset_rx(void)
|
||||
{
|
||||
g_transport.rx.cmd = 0;
|
||||
g_transport.rx.total_frame = 0;
|
||||
g_transport.rx.frame_seq = 0;
|
||||
g_transport.rx.bytes_received = 0;
|
||||
if (g_transport.timeout != 0) {
|
||||
os_timer_stop(&g_transport.rx.timer);
|
||||
}
|
||||
}
|
||||
|
||||
static void on_tx_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
BREEZE_ERR("tx timeout");
|
||||
reset_tx();
|
||||
}
|
||||
|
||||
static void on_rx_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
BREEZE_ERR("rx timeout");
|
||||
reset_rx();
|
||||
}
|
||||
|
||||
static bool is_valid_rx_command(uint8_t cmd) {
|
||||
if (cmd == BZ_CMD_CTRL ||
|
||||
cmd == BZ_CMD_QUERY ||
|
||||
cmd == BZ_CMD_EXT_DOWN ||
|
||||
cmd == BZ_CMD_AUTH_REQ ||
|
||||
cmd == BZ_CMD_AUTH_CFM ||
|
||||
cmd == BZ_CMD_AUTH_REKEY ||
|
||||
cmd == BZ_CMD_OTA_VER_REQ ||
|
||||
cmd == BZ_CMD_OTA_REQ ||
|
||||
cmd == BZ_CMD_OTA_SIZE ||
|
||||
cmd == BZ_CMD_OTA_DONE ||
|
||||
cmd == BZ_CMD_OTA_DATA) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_valid_tx_command(uint8_t cmd) {
|
||||
if (cmd == BZ_CMD_STATUS ||
|
||||
cmd == BZ_CMD_REPLY ||
|
||||
cmd == BZ_CMD_EXT_UP ||
|
||||
cmd == BZ_CMD_AUTH_RAND ||
|
||||
cmd == BZ_CMD_AUTH_RSP ||
|
||||
cmd == BZ_CMD_AUTH_KEY ||
|
||||
cmd == BZ_CMD_AUTH_REKEY_RSP ||
|
||||
cmd == BZ_CMD_OTA_VER_RSP ||
|
||||
cmd == BZ_CMD_OTA_RSP ||
|
||||
cmd == BZ_CMD_OTA_PUB_SIZE ||
|
||||
cmd == BZ_CMD_OTA_CHECK_RESULT ||
|
||||
cmd == BZ_CMD_OTA_UPDATE_PROCESS ||
|
||||
cmd == BZ_CMD_ERR) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void do_encrypt(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t bytes_to_pad, blk_num = len >> 4;
|
||||
uint8_t *decrypt_buf;
|
||||
uint8_t encrypt_data[BZ_FRAME_SIZE_MAX];
|
||||
if(len > BZ_FRAME_SIZE_MAX){
|
||||
BREEZE_ERR("[BZ encry] data PDU length exceed");
|
||||
return;
|
||||
}
|
||||
|
||||
bytes_to_pad = (AES_BLK_SIZE - len % AES_BLK_SIZE) % AES_BLK_SIZE;
|
||||
if (bytes_to_pad) {
|
||||
memset(data + len, 0, bytes_to_pad);
|
||||
g_transport.tx.zeroes_padded = bytes_to_pad;
|
||||
blk_num++;
|
||||
g_transport.tx.buff[3] += bytes_to_pad;
|
||||
}
|
||||
BREEZE_VERBOSE("aes bf:%d", blk_num);
|
||||
hex_byte_dump_verbose(data, len, 24);
|
||||
ais_aes128_cbc_encrypt(g_transport.p_aes_ctx, data, blk_num, encrypt_data);
|
||||
memcpy(data, encrypt_data, blk_num << 4);
|
||||
BREEZE_VERBOSE("aes af:");
|
||||
hex_byte_dump_verbose(encrypt_data, blk_num << 4, 24);
|
||||
}
|
||||
|
||||
static void do_decrypt(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t blk_num = len >> 4;
|
||||
uint8_t *buffer;
|
||||
uint8_t decrypt_data[BZ_FRAME_SIZE_MAX];
|
||||
if(len > BZ_FRAME_SIZE_MAX){
|
||||
BREEZE_ERR("[BZ decry] data PDU length exceed");
|
||||
return;
|
||||
}
|
||||
|
||||
ais_aes128_cbc_decrypt(g_transport.p_aes_ctx, data, blk_num, decrypt_data);
|
||||
memcpy(data, decrypt_data, len);
|
||||
}
|
||||
|
||||
static uint32_t build_packet(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint32_t ret = BZ_SUCCESS;
|
||||
|
||||
g_transport.tx.zeroes_padded = 0;
|
||||
g_transport.tx.buff[0] = ((BZ_TRANSPORT_VER & 0x7) << 5) |
|
||||
((g_transport.tx.encrypted & 0x1) << 4) |
|
||||
(g_transport.tx.msg_id & 0xF);
|
||||
g_transport.tx.buff[1] = g_transport.tx.cmd;
|
||||
g_transport.tx.buff[2] = ((g_transport.tx.total_frame & 0x0F) << 4) |
|
||||
(g_transport.tx.frame_seq & 0x0F);
|
||||
g_transport.tx.buff[3] = len;
|
||||
|
||||
/* Payload */
|
||||
if (len != 0) {
|
||||
memcpy(g_transport.tx.buff + HEADER_SIZE, data, len);
|
||||
if (g_transport.tx.encrypted != 0) {
|
||||
do_encrypt(g_transport.tx.buff + HEADER_SIZE, len);
|
||||
}
|
||||
}
|
||||
#if BZ_ENABLE_AUTH
|
||||
if(g_dn_complete == false){
|
||||
g_transport.tx.buff[0] &= (~(0x01 <<4));
|
||||
}
|
||||
|
||||
if (g_dn_complete == true){
|
||||
g_transport.tx.buff[3] = len;
|
||||
}
|
||||
#else
|
||||
g_transport.tx.buff[0] &= (~(0x01 <<4));
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint16_t tx_bytes_left(void)
|
||||
{
|
||||
return (g_transport.tx.len - g_transport.tx.bytes_sent);
|
||||
}
|
||||
|
||||
static bool rx_frames_left(void)
|
||||
{
|
||||
return (g_transport.rx.total_frame != g_transport.rx.frame_seq);
|
||||
}
|
||||
|
||||
static ret_code_t send_fragment(void)
|
||||
{
|
||||
ret_code_t ret = BZ_SUCCESS;
|
||||
uint16_t len, pkt_len, bytes_left;
|
||||
uint16_t pkt_payload_len = g_transport.max_pkt_size - HEADER_SIZE;
|
||||
uint16_t pkt_sent = 0;
|
||||
|
||||
bytes_left = tx_bytes_left();
|
||||
if (g_transport.tx.encrypted != 0) {
|
||||
if (g_transport.tx.cmd == BZ_CMD_AUTH_KEY)
|
||||
pkt_payload_len = AES_BLK_SIZE;
|
||||
else
|
||||
pkt_payload_len &= ~(AES_BLK_SIZE - 1);
|
||||
}
|
||||
|
||||
do {
|
||||
len = MIN(bytes_left, pkt_payload_len);
|
||||
build_packet(g_transport.tx.data + g_transport.tx.bytes_sent, len);
|
||||
pkt_len = len + g_transport.tx.zeroes_padded + HEADER_SIZE;
|
||||
if (g_transport.tx.active_func == ble_ais_send_indication)
|
||||
os_mutex_lock(&(g_transport.tx.mutex_indicate_done), 1000);
|
||||
ret = g_transport.tx.active_func(g_transport.tx.buff, pkt_len);
|
||||
if (ret == BZ_SUCCESS) {
|
||||
g_transport.tx.pkt_req++;
|
||||
g_transport.tx.frame_seq++;
|
||||
g_transport.tx.bytes_sent += len;
|
||||
bytes_left = tx_bytes_left();
|
||||
pkt_sent++;
|
||||
}
|
||||
if (g_transport.tx.active_func == ble_ais_send_indication)
|
||||
os_mutex_unlock(&(g_transport.tx.mutex_indicate_done));
|
||||
if (ret != BZ_SUCCESS ||
|
||||
g_transport.tx.active_func == ble_ais_send_indication) {
|
||||
break;
|
||||
}
|
||||
} while (bytes_left > 0);
|
||||
|
||||
if ((bytes_left != 0) && (g_transport.timeout != 0)) {
|
||||
os_timer_start(&g_transport.tx.timer);
|
||||
}
|
||||
if (g_transport.tx.active_func == ble_ais_send_notification) {
|
||||
transport_txdone(pkt_sent);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void trans_rx_dispatcher(void)
|
||||
{
|
||||
if (!is_valid_rx_command(g_transport.rx.cmd)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if((g_transport.rx.cmd & BZ_CMD_TYPE_MASK) == BZ_CMD_AUTH){
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_rx_command(g_transport.rx.cmd, g_transport.rx.buff, g_transport.rx.bytes_received);
|
||||
#endif
|
||||
} else if(g_transport.rx.cmd == BZ_CMD_EXT_DOWN){
|
||||
#if BZ_ENABLE_COMBO_NET
|
||||
extcmd_rx_command(g_transport.rx.cmd, g_transport.rx.buff, g_transport.rx.bytes_received);
|
||||
#endif
|
||||
} else {
|
||||
rx_cmd_post.cmd = g_transport.rx.cmd;
|
||||
rx_cmd_post.frame_seq = g_transport.rx.frame_seq + 1;
|
||||
rx_cmd_post.p_rx_buf = g_transport.rx.buff;
|
||||
rx_cmd_post.buf_sz = g_transport.rx.bytes_received;
|
||||
core_event_notify(BZ_EVENT_RX_INFO, &rx_cmd_post, sizeof(rx_cmd_post));
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t transport_init(ali_init_t const *p_init)
|
||||
{
|
||||
/* Initialize context */
|
||||
memset(&g_transport, 0, sizeof(transport_t));
|
||||
g_transport.max_pkt_size = BZ_GATT_MTU_SIZE_DEFAULT - 3;
|
||||
g_transport.timeout = p_init->transport_timeout;
|
||||
|
||||
if (g_transport.tx.mutex_indicate_done == NULL) {
|
||||
os_mutex_new(&(g_transport.tx.mutex_indicate_done));
|
||||
}
|
||||
|
||||
if (g_transport.timeout != 0) {
|
||||
os_timer_new(&g_transport.tx.timer, on_tx_timeout, &g_transport, g_transport.timeout);
|
||||
os_timer_new(&g_transport.rx.timer, on_rx_timeout, &g_transport, g_transport.timeout);
|
||||
}
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
void transport_reset(void)
|
||||
{
|
||||
reset_tx();
|
||||
reset_rx();
|
||||
|
||||
ais_aes128_destroy(g_transport.p_aes_ctx);
|
||||
g_transport.p_aes_ctx = NULL;
|
||||
#if BZ_ENABLE_AUTH
|
||||
g_dn_complete = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
ret_code_t transport_tx(uint8_t tx_type, uint8_t cmd,
|
||||
uint8_t const *const p_data, uint16_t length)
|
||||
{
|
||||
uint16_t pkt_payload_len;
|
||||
|
||||
if(cmd != BZ_CMD_ERR){
|
||||
if (p_data == NULL && length != 0) {
|
||||
return BZ_ENULL;
|
||||
}
|
||||
if(length > BZ_MAX_PAYLOAD_SIZE){
|
||||
return BZ_EDATASIZE;
|
||||
}
|
||||
}
|
||||
|
||||
if (g_transport.p_key != NULL &&
|
||||
(cmd == BZ_CMD_STATUS || cmd == BZ_CMD_REPLY || cmd == BZ_CMD_EXT_UP ||
|
||||
((cmd & BZ_CMD_TYPE_MASK) == BZ_CMD_AUTH && ((cmd != BZ_CMD_AUTH_RAND) && (cmd != BZ_CMD_AUTH_REKEY_RSP))))) {
|
||||
g_transport.tx.encrypted = 1;
|
||||
#ifdef EN_LONG_MTU
|
||||
pkt_payload_len = g_transport.max_pkt_size - HEADER_SIZE;
|
||||
#else
|
||||
pkt_payload_len = (g_transport.max_pkt_size - HEADER_SIZE) & ~(AES_BLK_SIZE - 1);
|
||||
#endif
|
||||
if (cmd == BZ_CMD_AUTH_KEY)
|
||||
pkt_payload_len = AES_BLK_SIZE;
|
||||
} else {
|
||||
g_transport.tx.encrypted = 0;
|
||||
pkt_payload_len = g_transport.max_pkt_size - HEADER_SIZE;
|
||||
}
|
||||
BREEZE_VERBOSE("tx_encrypted %d", g_transport.tx.encrypted);
|
||||
|
||||
if (tx_bytes_left() != 0 ||
|
||||
g_transport.tx.pkt_req != g_transport.tx.pkt_cfm) {
|
||||
return BZ_EBUSY;
|
||||
}
|
||||
|
||||
g_transport.tx.data = (uint8_t *)p_data;
|
||||
g_transport.tx.len = length;
|
||||
g_transport.tx.bytes_sent = 0;
|
||||
g_transport.tx.cmd = cmd;
|
||||
g_transport.tx.frame_seq = 0;
|
||||
g_transport.tx.pkt_req = 0;
|
||||
g_transport.tx.pkt_cfm = 0;
|
||||
|
||||
if (cmd == BZ_CMD_REPLY || cmd == BZ_CMD_EXT_UP) {
|
||||
g_transport.tx.msg_id = g_transport.rx.msg_id;
|
||||
} else if (cmd == BZ_CMD_STATUS) {
|
||||
g_transport.tx.msg_id = 0;
|
||||
}
|
||||
BREEZE_VERBOSE("tx.msg_id %d", g_transport.tx.msg_id);
|
||||
|
||||
if(p_data != NULL && length != 0){
|
||||
g_transport.tx.total_frame = length / pkt_payload_len;
|
||||
if (g_transport.tx.total_frame * pkt_payload_len == length && length != 0) {
|
||||
g_transport.tx.total_frame--;
|
||||
}
|
||||
}
|
||||
BREEZE_VERBOSE("tx.total_frame %d", g_transport.tx.total_frame + 1);
|
||||
|
||||
if (tx_type == TX_NOTIFICATION) {
|
||||
g_transport.tx.active_func = ble_ais_send_notification;
|
||||
} else {
|
||||
g_transport.tx.active_func = ble_ais_send_indication;
|
||||
}
|
||||
|
||||
send_fragment();
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
void transport_rx(uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
uint16_t len, buff_left;
|
||||
uint32_t err_code;
|
||||
|
||||
if (length == 0) {
|
||||
return;
|
||||
} else if ((length - HEADER_SIZE + g_transport.rx.bytes_received) > RX_BUFF_LEN) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_RX_BUFF_SIZE, BZ_EDATASIZE);
|
||||
reset_rx();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rx_frames_left()) {
|
||||
if (FRAME_SEQ(p_data) != 0) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_1ST_FRAME, BZ_EINVALIDDATA);
|
||||
reset_rx();
|
||||
return;
|
||||
}
|
||||
|
||||
g_transport.rx.msg_id = MSG_ID(p_data);
|
||||
g_transport.rx.cmd = CMD_TYPE(p_data);
|
||||
g_transport.rx.total_frame = TOTAL_FRAME(p_data);
|
||||
g_transport.rx.frame_seq = 0;
|
||||
g_transport.rx.bytes_received = 0;
|
||||
} else {
|
||||
if ((g_transport.rx.msg_id != MSG_ID(p_data)) ||
|
||||
(g_transport.rx.cmd != CMD_TYPE(p_data)) ||
|
||||
(g_transport.rx.total_frame != TOTAL_FRAME(p_data)) ||
|
||||
(((g_transport.rx.frame_seq + 1) & 0xF) != FRAME_SEQ(p_data) &&
|
||||
g_transport.rx.cmd != BZ_CMD_OTA_DATA)) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_OTHER_FRAMES, BZ_EINVALIDDATA);
|
||||
reset_rx();
|
||||
return;
|
||||
} else if (((g_transport.rx.frame_seq + 1) & 0xF) != FRAME_SEQ(p_data) &&
|
||||
g_transport.rx.cmd == BZ_CMD_OTA_DATA) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_FW_DATA_DISC, BZ_EINVALIDDATA);
|
||||
reset_rx();
|
||||
return;
|
||||
} else {
|
||||
g_transport.rx.frame_seq = FRAME_SEQ(p_data);
|
||||
}
|
||||
}
|
||||
|
||||
if (IS_ENC(p_data) != 0) {
|
||||
if ((length - HEADER_SIZE) % 16 != 0) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_ENCRYPTED, BZ_EINVALIDDATA);
|
||||
reset_rx();
|
||||
return;
|
||||
}
|
||||
if (g_transport.p_key == NULL) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_ENCRYPTED, BZ_EFORBIDDEN);
|
||||
reset_rx();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ((length != HEADER_SIZE + FRAME_LEN(p_data) && IS_ENC(p_data) == 0)
|
||||
|| (length < HEADER_SIZE + FRAME_LEN(p_data) && IS_ENC(p_data) != 0)) {
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_OTHER_FRAMES, BZ_EDATASIZE);
|
||||
reset_rx();
|
||||
return;
|
||||
}
|
||||
|
||||
buff_left = RX_BUFF_LEN - g_transport.rx.bytes_received;
|
||||
if ((len = MIN(buff_left, FRAME_LEN(p_data))) > 0) {
|
||||
if (IS_ENC(p_data) != 0) {
|
||||
do_decrypt(p_data + HEADER_SIZE, length - HEADER_SIZE);
|
||||
}
|
||||
memcpy(g_transport.rx.buff + g_transport.rx.bytes_received, p_data + HEADER_SIZE, len);
|
||||
g_transport.rx.bytes_received += len;
|
||||
}
|
||||
if (!rx_frames_left()) {
|
||||
trans_rx_dispatcher();
|
||||
reset_rx();
|
||||
} else {
|
||||
if (g_transport.timeout != 0) {
|
||||
os_timer_start(&g_transport.rx.timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void transport_txdone(uint16_t pkt_sent)
|
||||
{
|
||||
uint32_t err_code = BZ_SUCCESS;
|
||||
uint16_t bytes_left;
|
||||
|
||||
g_transport.tx.pkt_cfm += pkt_sent;
|
||||
bytes_left = tx_bytes_left();
|
||||
if (bytes_left != 0) {
|
||||
send_fragment();
|
||||
} else if (g_transport.tx.pkt_req == g_transport.tx.pkt_cfm &&
|
||||
g_transport.tx.pkt_req != 0) {
|
||||
if (!is_valid_tx_command(g_transport.tx.cmd)) {
|
||||
return;
|
||||
}
|
||||
core_event_notify(BZ_EVENT_TX_DONE, &g_transport.tx.cmd, sizeof(g_transport.tx.cmd));
|
||||
reset_tx();
|
||||
#if BZ_ENABLE_AUTH
|
||||
auth_tx_done();
|
||||
#endif
|
||||
} else if (g_transport.tx.pkt_req < g_transport.tx.pkt_cfm) {
|
||||
BREEZE_VERBOSE("pkt_req %d, pkt_cfm %d", g_transport.tx.pkt_req, g_transport.tx.pkt_cfm);
|
||||
reset_tx();
|
||||
core_handle_err(ALI_ERROR_SRC_TRANSPORT_PKT_CFM_SENT, BZ_EINTERNAL);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t transport_update_key(uint8_t *key)
|
||||
{
|
||||
char *iv = "0123456789ABCDEF";
|
||||
|
||||
g_transport.p_key = key;
|
||||
if (g_transport.p_aes_ctx) {
|
||||
ais_aes128_destroy(g_transport.p_aes_ctx);
|
||||
g_transport.p_aes_ctx = NULL;
|
||||
}
|
||||
|
||||
g_transport.p_aes_ctx = ais_aes128_init(g_transport.p_key, iv);
|
||||
BREEZE_VERBOSE("aes key update");
|
||||
hex_byte_dump_verbose(g_transport.p_key, 16, 24);
|
||||
return BZ_SUCCESS;
|
||||
}
|
||||
|
||||
uint32_t trans_update_mtu(void)
|
||||
{
|
||||
uint16_t rounding_mtu;
|
||||
uint16_t max_payload_len = 0;
|
||||
ble_get_att_mtu(&rounding_mtu);
|
||||
max_payload_len = rounding_mtu - BZ_ATT_HDR_SIZE - BZ_FRAME_HDR_SIZE;
|
||||
g_transport.max_pkt_size = (uint16_t)(max_payload_len / BZ_ENCRY_BLOCK_LENGTH) * BZ_ENCRY_BLOCK_LENGTH + BZ_FRAME_HDR_SIZE;
|
||||
BREEZE_DEBUG("Breeze mtu:%d, mpu:%d", rounding_mtu, g_transport.max_pkt_size);
|
||||
return 0;
|
||||
}
|
||||
102
Living_SDK/framework/bluetooth/breeze/core/utils.c
Normal file
102
Living_SDK/framework/bluetooth/breeze/core/utils.c
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "bzopt.h"
|
||||
#include "utils.h"
|
||||
|
||||
uint8_t hex2ascii(uint8_t digit)
|
||||
{
|
||||
uint8_t val;
|
||||
|
||||
if (digit <= 9) {
|
||||
val = digit - 0x0 + '0';
|
||||
} else {
|
||||
val = digit - 0xA + 'A';
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
void hex2string(uint8_t *hex, uint32_t len, uint8_t *str)
|
||||
{
|
||||
uint32_t index;
|
||||
|
||||
for (index = 0; index < len; index++) {
|
||||
str[index * 2] = hex2ascii(hex[index] >> 4 & 0x0f);
|
||||
str[index * 2 + 1] = hex2ascii(hex[index] & 0x0f);
|
||||
}
|
||||
}
|
||||
|
||||
static void utf8_to_str(uint8_t *data, uint8_t len, char *result)
|
||||
{
|
||||
memcpy(result, data, len);
|
||||
}
|
||||
|
||||
void utf8_to_ssid(uint8_t *data, uint8_t len, char *ssid)
|
||||
{
|
||||
utf8_to_str(data, len, ssid);
|
||||
}
|
||||
|
||||
void utf8_to_pw(uint8_t *data, uint8_t len, char *pw)
|
||||
{
|
||||
utf8_to_str(data, len, pw);
|
||||
}
|
||||
|
||||
void get_random(uint8_t *random, uint8_t random_len)
|
||||
{
|
||||
uint8_t bytes_available = 0;
|
||||
uint32_t seed = os_now_ms();
|
||||
uint8_t byte[5];
|
||||
uint32_t result;
|
||||
uint16_t bytes_copy;
|
||||
|
||||
srand((unsigned int)seed);
|
||||
result = rand();
|
||||
|
||||
while (bytes_available < random_len) {
|
||||
seed += result;
|
||||
seed = seed % 9999;
|
||||
snprintf((char *)byte, sizeof(byte), "%04u", seed);
|
||||
bytes_copy = random_len - bytes_available;
|
||||
bytes_copy = (bytes_copy > 4) ? 4 : bytes_copy;
|
||||
memcpy(random + bytes_available, byte, bytes_copy);
|
||||
bytes_available += bytes_copy;
|
||||
}
|
||||
}
|
||||
|
||||
static void hex_byte_dump(uint8_t *data, int len, int tab_num)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x ", data[i]);
|
||||
|
||||
if (!((i + 1) % tab_num)) {
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
void hex_byte_dump_debug(uint8_t *data, int len, int tab_num)
|
||||
{
|
||||
#if (CONFIG_BLDTIME_MUTE_DBGLOG)
|
||||
|
||||
#else
|
||||
hex_byte_dump(data, len, tab_num);
|
||||
#endif
|
||||
}
|
||||
|
||||
void hex_byte_dump_verbose(uint8_t *data, int len, int tab_num)
|
||||
{
|
||||
#if defined(BZ_VERBOSE_DEBUG)
|
||||
hex_byte_dump(data, len, tab_num);
|
||||
#else
|
||||
|
||||
#endif
|
||||
}
|
||||
9
Living_SDK/framework/bluetooth/breeze/hal/ble/Config.in
Normal file
9
Living_SDK/framework/bluetooth/breeze/hal/ble/Config.in
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
menuconfig AOS_COMP_BT_BREEZE_HAL
|
||||
#bool "bt_breeze_hal"
|
||||
default n
|
||||
select AOS_COMP_BT
|
||||
help
|
||||
|
||||
if AOS_COMP_BT_BREEZE_HAL
|
||||
# Configurations for comp bt_breeze_hal
|
||||
endif
|
||||
41
Living_SDK/framework/bluetooth/breeze/hal/ble/README.md
Normal file
41
Living_SDK/framework/bluetooth/breeze/hal/ble/README.md
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# BLE Mesh Tmall Profile
|
||||
|
||||
## Contents
|
||||
|
||||
```shell
|
||||
├── aes.c
|
||||
├── aes_mbed.c
|
||||
├── ali_crypto.h
|
||||
├── ali_crypto_types.h
|
||||
├── aos.mk
|
||||
├── breeze_hal_ble.c
|
||||
├── breeze_hal_os.c
|
||||
├── breeze_hal_sec.c
|
||||
├── include
|
||||
│ └── mbedtls
|
||||
│ ├── aes.h
|
||||
│ ├── aesni.h
|
||||
│ ├── check_config.h
|
||||
│ ├── config.h
|
||||
│ └── padlock.h
|
||||
└── mbed_crypto.h
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## Introduction
|
||||
|
||||
This component is an AliOS Things bases implementation of Breeze HAL APIs..
|
||||
|
||||
## Features
|
||||
|
||||
- Breeze OS HAL implementation.
|
||||
- Breeze BLE stack HAL API implementation.
|
||||
- Breeze security HAL API implementation.
|
||||
|
||||
## API
|
||||
|
||||
Please refer to APIs in header file `breeze_hal_ble.h`, `breeze_hal_os.h`, and `breeze_hal_sec.h`.
|
||||
|
||||
## Reference
|
||||
|
||||
None.
|
||||
360
Living_SDK/framework/bluetooth/breeze/hal/ble/aes.c
Normal file
360
Living_SDK/framework/bluetooth/breeze/hal/ble/aes.c
Normal file
|
|
@ -0,0 +1,360 @@
|
|||
#include "mbed_crypto.h"
|
||||
#include "ali_crypto.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* pkcs5 only support 8 bytes block size
|
||||
* pcks7 support 8,16 bytes block size, so in aes pkcs5 equal pkcs7 */
|
||||
/*
|
||||
* output: buf
|
||||
* output_len: block size
|
||||
* data_len: cur_data_len */
|
||||
static void _add_pkcs_padding(unsigned char *output, size_t output_len,
|
||||
size_t data_len)
|
||||
{
|
||||
size_t padding_len = output_len - data_len;
|
||||
unsigned char i;
|
||||
|
||||
for (i = 0; i < padding_len; i++) {
|
||||
output[data_len + i] = (unsigned char)padding_len;
|
||||
}
|
||||
}
|
||||
|
||||
static int _get_pkcs_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t i, pad_idx;
|
||||
unsigned char padding_len, bad = 0;
|
||||
|
||||
if (NULL == input || NULL == data_len) {
|
||||
return ALI_CRYPTO_INVALID_ARG;
|
||||
}
|
||||
|
||||
padding_len = input[input_len - 1];
|
||||
*data_len = input_len - padding_len;
|
||||
|
||||
/* Avoid logical || since it results in a branch */
|
||||
bad |= padding_len > input_len;
|
||||
bad |= padding_len == 0;
|
||||
|
||||
/* The number of bytes checked must be independent of padding_len,
|
||||
* so pick input_len, which is usually 8 or 16 (one block) */
|
||||
pad_idx = input_len - padding_len;
|
||||
for (i = 0; i < input_len; i++) {
|
||||
bad |= (input[i] ^ padding_len) * (i >= pad_idx);
|
||||
}
|
||||
|
||||
return (ALI_CRYPTO_INVALID_PADDING * (bad != 0));
|
||||
}
|
||||
|
||||
static ali_crypto_result _ali_aes_cbc_final(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
sym_padding_t padding,
|
||||
aes_ctx_t *ctx)
|
||||
{
|
||||
int ret;
|
||||
int mode;
|
||||
size_t data_len;
|
||||
uint8_t *tmp_dst = NULL;
|
||||
|
||||
if (ctx == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_CONTEXT, "cbc_final: invalid context!\n");
|
||||
}
|
||||
|
||||
if (!(padding == SYM_NOPAD || padding == SYM_PKCS5_PAD)) {
|
||||
PRINT_RET(ALI_CRYPTO_NOSUPPORT,
|
||||
"ecb_final: only support no-padding and pkcs5/7!\n");
|
||||
}
|
||||
|
||||
if (padding == SYM_NOPAD) {
|
||||
if (src == NULL || src_size == 0) {
|
||||
if (dst_size != NULL) {
|
||||
*dst_size = 0;
|
||||
}
|
||||
return ALI_CRYPTO_SUCCESS;
|
||||
}
|
||||
} else if (padding == SYM_PKCS5_PAD) {
|
||||
/* pkcs5 finish must have input data */
|
||||
if (NULL == src || 0 == src_size) {
|
||||
if (dst_size != NULL) {
|
||||
*dst_size = 0;
|
||||
}
|
||||
return ALI_CRYPTO_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (dst_size == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG, "cbc_final: invalid arg!\n");
|
||||
}
|
||||
|
||||
if ((0 != *dst_size) && (dst == NULL)) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG, "cbc_final: invalid arg!\n");
|
||||
}
|
||||
if (ctx->is_enc) {
|
||||
mode = MBEDTLS_AES_ENCRYPT;
|
||||
} else {
|
||||
mode = MBEDTLS_AES_DECRYPT;
|
||||
}
|
||||
|
||||
if (padding == SYM_NOPAD) {
|
||||
if (src_size % AES_BLOCK_SIZE != 0) {
|
||||
PRINT_RET(ALI_CRYPTO_LENGTH_ERR,
|
||||
"cbc_final: no pad invalid size(%d vs %d)\n",
|
||||
(int)src_size, (int)*dst_size);
|
||||
}
|
||||
if (src_size > *dst_size) {
|
||||
*dst_size = src_size;
|
||||
PRINT_RET(ALI_CRYPTO_SHORT_BUFFER, "cbc_final: short buffer\n");
|
||||
} else {
|
||||
*dst_size = src_size;
|
||||
}
|
||||
} else if (padding == SYM_PKCS5_PAD) {
|
||||
if (ctx->is_enc) {
|
||||
if ((src_size + (AES_BLOCK_SIZE - src_size % AES_BLOCK_SIZE)) >
|
||||
*dst_size) {
|
||||
*dst_size =
|
||||
src_size + (AES_BLOCK_SIZE - src_size % AES_BLOCK_SIZE);
|
||||
PRINT_RET(ALI_CRYPTO_SHORT_BUFFER,
|
||||
"ecb_final: enc pkcs5 short buffer(%d vs %d)\n",
|
||||
(int)src_size, (int)*dst_size);
|
||||
}
|
||||
} else {
|
||||
if (src_size % AES_BLOCK_SIZE != 0) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_PADDING,
|
||||
"cbc_final: cipher size is not block align(%d)\n",
|
||||
(int)src_size);
|
||||
}
|
||||
if ((src_size - AES_BLOCK_SIZE) > *dst_size) {
|
||||
tmp_dst = OSA_malloc(src_size);
|
||||
if (NULL == tmp_dst) {
|
||||
PRINT_RET(ALI_CRYPTO_OUTOFMEM,
|
||||
"cbc_final: out of memory\n");
|
||||
}
|
||||
ret =
|
||||
mbedtls_aes_crypt_cbc(&(ctx->ctx), mode, src_size,
|
||||
(unsigned char *)ctx->iv, src, tmp_dst);
|
||||
if (0 != ret) {
|
||||
OSA_free(tmp_dst);
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: mbedtls_aes_crypt_cbc fail(%d)\n",
|
||||
ret);
|
||||
}
|
||||
|
||||
ret = _get_pkcs_padding(tmp_dst + src_size - AES_BLOCK_SIZE,
|
||||
AES_BLOCK_SIZE, &data_len);
|
||||
if (0 != ret) {
|
||||
OSA_free(tmp_dst);
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: get pkcs padding fail(0x%08x)\n",
|
||||
ret);
|
||||
}
|
||||
*dst_size = src_size - (AES_BLOCK_SIZE - data_len);
|
||||
|
||||
OSA_free(tmp_dst);
|
||||
tmp_dst = NULL;
|
||||
PRINT_RET(ALI_CRYPTO_SHORT_BUFFER,
|
||||
"cbc_final: dec pkcs short buffer(%d vs %d)\n",
|
||||
(int)src_size, (int)*dst_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (MBEDTLS_AES_ENCRYPT == mode) {
|
||||
/* encrypt, short buffer will be blocked above */
|
||||
size_t cur_len;
|
||||
uint8_t block[AES_BLOCK_SIZE];
|
||||
|
||||
cur_len = src_size & (~(AES_BLOCK_SIZE - 1));
|
||||
ret = mbedtls_aes_crypt_cbc(
|
||||
&(ctx->ctx), mode, cur_len, (unsigned char *)ctx->iv,
|
||||
(const unsigned char *)src, (unsigned char *)dst);
|
||||
if (0 != ret) {
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: mbedtls_aes_crypt_cbc fail(%d)\n", ret);
|
||||
}
|
||||
|
||||
if (padding == SYM_PKCS5_PAD) {
|
||||
OSA_memcpy(block, src + cur_len, src_size - cur_len);
|
||||
_add_pkcs_padding(block, AES_BLOCK_SIZE, src_size - cur_len);
|
||||
ret = mbedtls_aes_crypt_cbc(
|
||||
&(ctx->ctx), mode, AES_BLOCK_SIZE, (unsigned char *)ctx->iv,
|
||||
(const unsigned char *)block, (unsigned char *)(dst + cur_len));
|
||||
if (0 != ret) {
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: mbedtls_aes_crypt_cbc fail(%d)\n", ret);
|
||||
}
|
||||
*dst_size = cur_len + AES_BLOCK_SIZE;
|
||||
}
|
||||
} else {
|
||||
/* dencrypt */
|
||||
if (padding == SYM_NOPAD) {
|
||||
ret = mbedtls_aes_crypt_cbc(
|
||||
&(ctx->ctx), mode, src_size, (unsigned char *)ctx->iv,
|
||||
(const unsigned char *)src, (unsigned char *)dst);
|
||||
if (0 != ret) {
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: mbedtls_aes_crypt_cbc fail(%d)\n", ret);
|
||||
}
|
||||
|
||||
*dst_size = src_size;
|
||||
} else if (padding == SYM_PKCS5_PAD) {
|
||||
/* avoid dst size is not enougth */
|
||||
tmp_dst = OSA_malloc(src_size);
|
||||
if (NULL == tmp_dst) {
|
||||
PRINT_RET(ALI_CRYPTO_ERROR, "cbc_final: out of memory\n");
|
||||
}
|
||||
ret = mbedtls_aes_crypt_cbc(
|
||||
&(ctx->ctx), mode, src_size, (unsigned char *)ctx->iv,
|
||||
(const unsigned char *)src, (unsigned char *)tmp_dst);
|
||||
if (0 != ret) {
|
||||
OSA_free(tmp_dst);
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: mbedtls_aes_crypt_cbc fail(%d)\n", ret);
|
||||
}
|
||||
|
||||
ret = _get_pkcs_padding(tmp_dst + src_size - AES_BLOCK_SIZE,
|
||||
AES_BLOCK_SIZE, &data_len);
|
||||
if (0 != ret) {
|
||||
OSA_free(tmp_dst);
|
||||
PRINT_RET(ALI_CRYPTO_ERROR,
|
||||
"cbc_final: get pkcs padding fail(0x%08x)\n", ret);
|
||||
}
|
||||
if (*dst_size < src_size - (AES_BLOCK_SIZE - data_len)) {
|
||||
OSA_free(tmp_dst);
|
||||
*dst_size = src_size - (AES_BLOCK_SIZE - data_len);
|
||||
PRINT_RET(ALI_CRYPTO_SHORT_BUFFER,
|
||||
"cbc_final: dec pkcs short buffer\n");
|
||||
}
|
||||
OSA_memcpy(dst, tmp_dst, src_size - (AES_BLOCK_SIZE - data_len));
|
||||
*dst_size = src_size - (AES_BLOCK_SIZE - data_len);
|
||||
OSA_free(tmp_dst);
|
||||
}
|
||||
}
|
||||
|
||||
return (ali_crypto_result)ret;
|
||||
}
|
||||
|
||||
ali_crypto_result breeze_aes_get_ctx_size(aes_type_t type, size_t *size)
|
||||
{
|
||||
if (size == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG, "aes_get_ctx_size: bad input!\n");
|
||||
}
|
||||
|
||||
*size = sizeof(aes_ctx_t);
|
||||
|
||||
return ALI_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
ali_crypto_result breeze_aes_init(aes_type_t type, bool is_enc,
|
||||
const uint8_t *key1, const uint8_t *key2,
|
||||
size_t keybytes, const uint8_t *iv,
|
||||
void *context)
|
||||
{
|
||||
int ret = ALI_CRYPTO_SUCCESS;
|
||||
aes_ctx_t *aes_ctx;
|
||||
|
||||
(void)key2;
|
||||
if (key1 == NULL || context == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG, "ali_aes_init: bad input args!\n");
|
||||
}
|
||||
|
||||
if (keybytes != 16 && keybytes != 24 && keybytes != 32) {
|
||||
PRINT_RET(ALI_CRYPTO_LENGTH_ERR, "ali_aes_init: bad key lenth(%d)\n",
|
||||
(int)keybytes);
|
||||
}
|
||||
|
||||
aes_ctx = (aes_ctx_t *)context;
|
||||
if ((IS_VALID_CTX_MAGIC(aes_ctx->magic) &&
|
||||
aes_ctx->status != CRYPTO_STATUS_FINISHED) &&
|
||||
aes_ctx->status != CRYPTO_STATUS_CLEAN) {
|
||||
PRINT_RET(ALI_CRYPTO_ERR_STATE, "ali_aes_init: bad status(%d)\n",
|
||||
(int)aes_ctx->status);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case AES_CBC: {
|
||||
if (iv == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG,
|
||||
"ali_aes_init: cbc iv is null\n");
|
||||
}
|
||||
|
||||
OSA_memcpy(aes_ctx->iv, iv, 16);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_TYPE,
|
||||
"ali_aes_init: invalid aes type(%d)\n", type);
|
||||
}
|
||||
|
||||
mbedtls_aes_init(&(aes_ctx->ctx));
|
||||
aes_ctx->is_enc = is_enc;
|
||||
|
||||
if (aes_ctx->is_enc) {
|
||||
ret = mbedtls_aes_setkey_enc(&(aes_ctx->ctx), key1, keybytes * 8);
|
||||
} else {
|
||||
ret = mbedtls_aes_setkey_dec(&(aes_ctx->ctx), key1, keybytes * 8);
|
||||
}
|
||||
|
||||
if (ret != ALI_CRYPTO_SUCCESS) {
|
||||
PRINT_RET(ALI_CRYPTO_ERROR, "ALI_aes_init: start mode(%d) fail(%d)\n",
|
||||
type, ret);
|
||||
}
|
||||
|
||||
aes_ctx->offset = 0;
|
||||
aes_ctx->type = type;
|
||||
aes_ctx->status = CRYPTO_STATUS_INITIALIZED;
|
||||
INIT_CTX_MAGIC(aes_ctx->magic);
|
||||
|
||||
return ALI_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
ali_crypto_result breeze_aes_finish(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
sym_padding_t padding, void *context)
|
||||
{
|
||||
ali_crypto_result ret;
|
||||
aes_ctx_t *aes_ctx;
|
||||
|
||||
if ((src == NULL && src_size != 0) ||
|
||||
((dst_size != NULL) && (dst == NULL && *dst_size != 0)) ||
|
||||
context == NULL) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_ARG, "ali_aes_finish: bad input args!\n");
|
||||
}
|
||||
|
||||
aes_ctx = (aes_ctx_t *)context;
|
||||
if (!IS_VALID_CTX_MAGIC(aes_ctx->magic)) {
|
||||
PRINT_RET(ALI_CRYPTO_INVALID_CONTEXT, "ali_aes_finish: bad magic!\n");
|
||||
}
|
||||
|
||||
if ((aes_ctx->status != CRYPTO_STATUS_INITIALIZED) &&
|
||||
(aes_ctx->status != CRYPTO_STATUS_PROCESSING)) {
|
||||
PRINT_RET(ALI_CRYPTO_ERR_STATE, "ali_aes_finish: bad status(%d)\n",
|
||||
(int)aes_ctx->status);
|
||||
}
|
||||
|
||||
switch (aes_ctx->type) {
|
||||
case AES_CBC: {
|
||||
ret = _ali_aes_cbc_final(src, src_size, dst, dst_size, padding,
|
||||
aes_ctx);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PRINT_RET(ALI_CRYPTO_NOSUPPORT,
|
||||
"ali_aes_finish: invalid aes type(%d)\n", aes_ctx->type);
|
||||
}
|
||||
|
||||
if (ret != ALI_CRYPTO_SUCCESS) {
|
||||
mbedtls_aes_free(&(aes_ctx->ctx));
|
||||
PRINT_RET(ret, "ali_aes_finish: aes type(%d) final fail(%08x)\n",
|
||||
aes_ctx->type, ret);
|
||||
}
|
||||
|
||||
CLEAN_CTX_MAGIC(aes_ctx->magic);
|
||||
aes_ctx->status = CRYPTO_STATUS_FINISHED;
|
||||
aes_ctx->offset = 0;
|
||||
|
||||
mbedtls_aes_free(&(aes_ctx->ctx));
|
||||
return ALI_CRYPTO_SUCCESS;
|
||||
}
|
||||
1394
Living_SDK/framework/bluetooth/breeze/hal/ble/aes_mbed.c
Normal file
1394
Living_SDK/framework/bluetooth/breeze/hal/ble/aes_mbed.c
Normal file
File diff suppressed because it is too large
Load diff
756
Living_SDK/framework/bluetooth/breeze/hal/ble/ali_crypto.h
Normal file
756
Living_SDK/framework/bluetooth/breeze/hal/ble/ali_crypto.h
Normal file
|
|
@ -0,0 +1,756 @@
|
|||
#ifndef _ALI_CRYPTO_H_
|
||||
#define _ALI_CRYPTO_H_
|
||||
|
||||
#include "ali_crypto_types.h"
|
||||
|
||||
typedef enum _ali_crypto_result
|
||||
{
|
||||
ALI_CRYPTO_ERROR = (int)0xffff0000, /* Generic Error */
|
||||
ALI_CRYPTO_NOSUPPORT, /* Scheme not support */
|
||||
ALI_CRYPTO_INVALID_KEY, /* Invalid Key in asymmetric scheme: RSA/DSA/ECCP/DH
|
||||
etc */
|
||||
ALI_CRYPTO_INVALID_TYPE, /* Invalid
|
||||
aes_type/des_type/authenc_type/hash_type/cbcmac_type/cmac_type
|
||||
*/
|
||||
ALI_CRYPTO_INVALID_CONTEXT, /* Invalid context in multi-thread
|
||||
cipher/authenc/mac/hash etc */
|
||||
ALI_CRYPTO_INVALID_PADDING, /* Invalid
|
||||
sym_padding/rsassa_padding/rsaes_padding */
|
||||
ALI_CRYPTO_INVALID_AUTHENTICATION, /* Invalid authentication in
|
||||
AuthEnc(AES-CCM/AES-GCM)/asymmetric
|
||||
verify(RSA/DSA/ECCP DSA) */
|
||||
ALI_CRYPTO_INVALID_ARG, /* Invalid arguments */
|
||||
ALI_CRYPTO_INVALID_PACKET, /* Invalid packet in asymmetric enc/dec(RSA) */
|
||||
ALI_CRYPTO_LENGTH_ERR, /* Invalid Length in arguments */
|
||||
ALI_CRYPTO_OUTOFMEM, /* Memory alloc NULL */
|
||||
ALI_CRYPTO_SHORT_BUFFER, /* Output buffer is too short to store result */
|
||||
ALI_CRYPTO_NULL, /* NULL pointer in arguments */
|
||||
ALI_CRYPTO_ERR_STATE, /* Bad state in mulit-thread cipher/authenc/mac/hash
|
||||
etc */
|
||||
ALI_CRYPTO_SUCCESS = 0, /* Success */
|
||||
} ali_crypto_result;
|
||||
|
||||
#define AES_BLOCK_SIZE \
|
||||
16 /* don't change this value,since AES only support 16 byte block size */
|
||||
#define AES_IV_SIZE 16
|
||||
#define DES_BLOCK_SIZE 8
|
||||
#define DES_IV_SIZE 8
|
||||
|
||||
typedef enum _sym_padding_t
|
||||
{
|
||||
SYM_NOPAD = 0,
|
||||
SYM_PKCS5_PAD = 1,
|
||||
SYM_ZERO_PAD = 2,
|
||||
} sym_padding_t;
|
||||
|
||||
typedef enum _aes_type_t
|
||||
{
|
||||
AES_ECB = 0,
|
||||
AES_CBC = 1,
|
||||
AES_CTR = 2,
|
||||
AES_CTS = 3,
|
||||
AES_XTS = 4,
|
||||
AES_CFB8 = 6,
|
||||
AES_CFB128 = 7,
|
||||
} aes_type_t;
|
||||
|
||||
typedef enum _des_type_t
|
||||
{
|
||||
DES_ECB = 0,
|
||||
DES_CBC = 1,
|
||||
DES3_ECB = 2,
|
||||
DES3_CBC = 3,
|
||||
} des_type_t;
|
||||
|
||||
typedef enum _authenc_type_t
|
||||
{
|
||||
AES_CCM = 0,
|
||||
AES_GCM = 1,
|
||||
} authenc_type_t;
|
||||
|
||||
typedef enum _hash_type_t
|
||||
{
|
||||
HASH_NONE = 0,
|
||||
SHA1 = 1,
|
||||
SHA224 = 2,
|
||||
SHA256 = 3,
|
||||
SHA384 = 4,
|
||||
SHA512 = 5,
|
||||
MD5 = 6,
|
||||
} hash_type_t;
|
||||
|
||||
enum
|
||||
{
|
||||
MD5_HASH_SIZE = 16,
|
||||
SHA1_HASH_SIZE = 20,
|
||||
SHA224_HASH_SIZE = 28,
|
||||
SHA256_HASH_SIZE = 32,
|
||||
SHA384_HASH_SIZE = 48,
|
||||
SHA512_HASH_SIZE = 64,
|
||||
MAX_HASH_SIZE = 64,
|
||||
};
|
||||
|
||||
#define HASH_SIZE(type) \
|
||||
(((type) == SHA1) \
|
||||
? (SHA1_HASH_SIZE) \
|
||||
: (((type) == SHA224) \
|
||||
? (SHA224_HASH_SIZE) \
|
||||
: (((type) == SHA256) \
|
||||
? (SHA256_HASH_SIZE) \
|
||||
: (((type) == SHA384) \
|
||||
? (SHA384_HASH_SIZE) \
|
||||
: (((type) == SHA512) \
|
||||
? (SHA512_HASH_SIZE) \
|
||||
: (((type) == MD5) ? (MD5_HASH_SIZE) : (0)))))))
|
||||
|
||||
typedef enum _cbcmac_type_t
|
||||
{
|
||||
AESCBCMAC = 0,
|
||||
DESCBCMAC = 1,
|
||||
DES3CBCMAC = 2,
|
||||
} cbcmac_type_t;
|
||||
|
||||
typedef enum _cmac_type_t
|
||||
{
|
||||
AESCMAC = 0,
|
||||
} cmac_type_t;
|
||||
|
||||
typedef enum _rsa_key_attr_t
|
||||
{
|
||||
RSA_MODULUS = 0x130,
|
||||
RSA_PUBLIC_EXPONENT = 0x230,
|
||||
RSA_PRIVATE_EXPONENT = 0x330,
|
||||
RSA_PRIME1 = 0x430,
|
||||
RSA_PRIME2 = 0x530,
|
||||
RSA_EXPONENT1 = 0x630,
|
||||
RSA_EXPONENT2 = 0x730,
|
||||
RSA_COEFFICIENT = 0x830,
|
||||
} rsa_key_attr_t;
|
||||
|
||||
typedef enum _dh_key_attr_t
|
||||
{
|
||||
DH_PRIME = 0x140,
|
||||
DH_BASE = 0x240,
|
||||
DH_PRIVATE = 0x340,
|
||||
DH_PUBLIC = 0x440,
|
||||
DH_SUBPRIME = 0x540,
|
||||
DH_X_BITS = 0x640,
|
||||
} dh_key_attr_t;
|
||||
|
||||
typedef enum _dsa_key_attr_t
|
||||
{
|
||||
DSA_PRIME = 0x150,
|
||||
DSA_SUBPRIME = 0x250,
|
||||
DSA_BASE = 0x350,
|
||||
DSA_PRIVATE = 0x450,
|
||||
DSA_PUBLIC = 0x550,
|
||||
} dsa_key_attr_t;
|
||||
|
||||
typedef enum _rsa_pad_type_t
|
||||
{
|
||||
RSA_NOPAD = 0,
|
||||
|
||||
/* encrypt */
|
||||
RSAES_PKCS1_V1_5 = 10,
|
||||
RSAES_PKCS1_OAEP_MGF1 = 11,
|
||||
|
||||
/* sign */
|
||||
RSASSA_PKCS1_V1_5 = 20,
|
||||
RSASSA_PKCS1_PSS_MGF1 = 21,
|
||||
} rsa_pad_type_t;
|
||||
|
||||
typedef struct _rsa_padding_t
|
||||
{
|
||||
rsa_pad_type_t type;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
hash_type_t type;
|
||||
} rsaes_oaep;
|
||||
struct
|
||||
{
|
||||
hash_type_t type; /* md5/sha1/sha224/sha256/sha384/sha512 */
|
||||
} rsassa_v1_5;
|
||||
struct
|
||||
{
|
||||
hash_type_t type; /* sha1/sha224/sha256/sha384/sha512 */
|
||||
size_t salt_len;
|
||||
} rsassa_pss;
|
||||
} pad;
|
||||
} rsa_padding_t;
|
||||
|
||||
typedef enum _dsa_padding_t
|
||||
{
|
||||
DSA_SHA1 = 0,
|
||||
DSA_SHA224 = 1,
|
||||
DSA_SHA256 = 2,
|
||||
} dsa_padding_t;
|
||||
|
||||
enum
|
||||
{
|
||||
CRYPTO_STATUS_CLEAN = 0,
|
||||
CRYPTO_STATUS_INITIALIZED = 1,
|
||||
CRYPTO_STATUS_PROCESSING = 2,
|
||||
CRYPTO_STATUS_FINISHED = 3,
|
||||
};
|
||||
|
||||
/* internal data types */
|
||||
typedef struct __rsa_keypair rsa_keypair_t;
|
||||
typedef struct __rsa_pubkey rsa_pubkey_t;
|
||||
|
||||
typedef struct __dsa_keypair dsa_keypair_t;
|
||||
typedef struct __dsa_pubkey dsa_pubkey_t;
|
||||
|
||||
typedef struct __dh_keypair dh_keypair_t;
|
||||
typedef struct __dh_pubkey dh_pubkey_t;
|
||||
|
||||
typedef struct __ecc_keypair ecc_keypair_t;
|
||||
typedef struct __ecc_pubkey ecc_pubkey_t;
|
||||
|
||||
/********************************************************************/
|
||||
/* SYM */
|
||||
/********************************************************************/
|
||||
|
||||
/*
|
||||
* type[in]: must be AES_ECB/AES_CBC/AES_CTR/AES_CTS/AES_XTS
|
||||
* size[out]: check size != NULL
|
||||
* -- caller will alloc "size" memory as context buffer later
|
||||
*/
|
||||
ali_crypto_result breeze_aes_get_ctx_size(aes_type_t type, size_t *size);
|
||||
|
||||
/*
|
||||
* type[in]: must be AES_ECB/AES_CBC/AES_CTR/AES_CTS/AES_XTS
|
||||
* is_enc[in]: [true] for encrypt, [false] for decrypt
|
||||
* key1[in]: the encrypt key
|
||||
* key2[in]: the tweak encrypt key for XTS mode
|
||||
* keybytes[in]: the key length of the keys(each) in bytes, should be
|
||||
* 16/24/32 bytes iv[in]: only valid for
|
||||
* AES_CBC/AES_CTR/AES_CTS/AES_XTS
|
||||
* -- function can read 16 bytes from this address as the
|
||||
* internal iv context[in/out]: caller allocated memory used as internal
|
||||
* context, which size is got through breeze_aes_get_ctx_size
|
||||
* -- [in]: status of context should be CLEAN or FINISHED
|
||||
* -- [out]: status of context is changed to INITIALIZED
|
||||
*/
|
||||
ali_crypto_result ali_aes_init(aes_type_t type, bool is_enc,
|
||||
const uint8_t *key1, const uint8_t *key2,
|
||||
size_t keybytes, const uint8_t *iv,
|
||||
void *context);
|
||||
|
||||
/*
|
||||
* src[in]: plaintext for encrypt, ciphertext for decrypt
|
||||
* dst[out]: ciphertext for encrypt, plaintext for decrypt
|
||||
* size[in]: the number of bytes to process
|
||||
* -- ECB/CBC/CTS/XTS, must be multiple of the cipher block
|
||||
* size
|
||||
* -- CTR, any positive integer
|
||||
* context[in/out]: internal context
|
||||
* -- [in]: status of context should be INITED or PROCESSING
|
||||
* -- [out]: status of context is changed to PROCESSING
|
||||
*/
|
||||
ali_crypto_result ali_aes_process(const uint8_t *src, uint8_t *dst, size_t size,
|
||||
void *context);
|
||||
|
||||
/*
|
||||
* src[in]: source data, plaintext for encrypt/ciphertext for decrypt
|
||||
* -- may be NULL, which identify that no input data, only
|
||||
* terminate crypto src_size[in]: the number of bytes to process, src_size
|
||||
* == 0 if src == NULL
|
||||
* -- encrypt: SYM_NOPAD - must be multiple of the cipher
|
||||
* block size
|
||||
* -- decrypt: ECB/CBC - must be multiple of the cipher
|
||||
* block size dst[out]: destination data, which is used to save
|
||||
* processed data
|
||||
* -- may be NULL if no input src data(src == NULL &&
|
||||
* src_size == 0)
|
||||
* -- ciphertext for encrypt, plaintext for decrypt
|
||||
* -- if no SYM_NOPAD, should remove padding data
|
||||
* accordingly dst_size[in/out]: the length of processed data, may be NULL if
|
||||
* dst == NULL
|
||||
* -- [in]: buffer size
|
||||
* -- [out]: the actual encrypted/decrypted data size
|
||||
* padding[in]: padding type for aes mode
|
||||
* -- ECB/CBC: only support SYM_NOPAD
|
||||
* -- CTR/CTS/XTS: padding is ignored
|
||||
* context[in/out]: internal context
|
||||
* -- [in]: status of context should be INITED or
|
||||
* PROCESSING
|
||||
* -- [out]: status of context is changed to FINISHED
|
||||
*/
|
||||
ali_crypto_result ali_aes_finish(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
sym_padding_t padding, void *context);
|
||||
|
||||
ali_crypto_result ali_aes_reset(void *context);
|
||||
ali_crypto_result ali_aes_copy_context(void *dst_ctx, void *src_ctx);
|
||||
/* des include des3 */
|
||||
/*
|
||||
type: must be DES_ECB/DES_CBC/DES3_ECB/DES3_CBC
|
||||
size: check size != NULL
|
||||
*/
|
||||
ali_crypto_result ali_des_get_ctx_size(des_type_t type, size_t *size);
|
||||
|
||||
/*
|
||||
type: must be DES_ECB/DES_CBC/DES3_ECB/DES3_CBC
|
||||
is_enc: [true] for encrypt, [false] for decrypt.
|
||||
key: function will read 'keybytes' of data as key.
|
||||
keybytes: for DES_ECB/DES_CBC, must be 64.
|
||||
for DES3_ECB/DES3_CBC, must be 128 or 192.
|
||||
iv: for DES_ECB/DES3_ECB: must be NULL.
|
||||
for DES_CBC/DES3_CBC: function will read 8 bytes as algo iv.
|
||||
context: function will use size which return from function
|
||||
'ali_des_get_ctx_size' as internal context. function will check the [status[ of
|
||||
'context', must be CLEAN or FINISH. function will initialize the [status] of
|
||||
'context' to INIT. function will save the 'type', 'is_enc', or maybe 'iv',
|
||||
'key', 'keybytes' in 'context'. function will initialize the 'context' to a
|
||||
valid context.
|
||||
*/
|
||||
ali_crypto_result ali_des_init(des_type_t type, bool is_enc, const uint8_t *key,
|
||||
size_t keybytes, const uint8_t *iv,
|
||||
void *context);
|
||||
|
||||
/*
|
||||
src: function will read 'size' of data from this area as source data.
|
||||
MUST be NULL if 'size' is 0
|
||||
dst: function will write 'size' of data to this area as destination data.
|
||||
MUST be NULL if 'size' is 0
|
||||
size: the length of source data.
|
||||
must be multiple of 8 bytes. or 0.
|
||||
if size == 0, src MUST be NULL, dst MUST be NULL, return
|
||||
TEE_SUCCESS. context: function will use size which return from function
|
||||
'ali_des_get_ctx_size' as internal context. function will check it is a valid
|
||||
context. function will check the [status] of 'context', must be INIT or PROCESS.
|
||||
function will change the [status] of 'context' to PROCESS.
|
||||
function will do encrypt or decrypt indicated by the content in
|
||||
'context'.
|
||||
*/
|
||||
ali_crypto_result ali_des_process(const uint8_t *src, uint8_t *dst, size_t size,
|
||||
void *context);
|
||||
/*
|
||||
src: function will read 'src_size' of data from this area as source data.
|
||||
MUST be NULL if 'src_size' is 0.
|
||||
src_size: the length of source data. this have different rules for differnt
|
||||
'type' and 'padding'. a. for 'padding' is SYM_NOPAD: a.1 MUST be multiple of 16
|
||||
bytes. or 0. b. for other 'padding': b.1 can be any integer or 0. if 'src_size'
|
||||
== 0, 'src' MUST be NULL, 'dst' MUST be NULL, and this function will reaturn
|
||||
SUCCESS. dst: function will write certain length which is retuned by
|
||||
'dst_size' of data to this area as destination data. MUST be NULL if 'size' is 0
|
||||
dst_size: function will wirte some integer to this area to indicate the length
|
||||
of destination data. the return value depends on 'src_size' and 'padding' a.1
|
||||
for 'padding' is SYM_NOPAD, dst_size is equal to src_size. a.2 for other
|
||||
'padding', 'dst_size' is 16 bytes align up of 'src_size'. padding: the
|
||||
padding type of finish. can be anyone of SYM_NOPAD/SYM_PKCS5_PAD/SYM_ZERO_PAD.
|
||||
context: function will use size which return from function
|
||||
'ali_des_get_ctx_size' as internal context. function will check it is a valid
|
||||
context. function will check the [status] of 'context', must be INIT or PROCESS.
|
||||
function will change the [status] of 'context' to FINISH.
|
||||
function will do encrypt or decrypt indicated by the content in
|
||||
'context'. function MUST clean the content of context before this fucntion
|
||||
return.
|
||||
*/
|
||||
ali_crypto_result ali_des_finish(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
sym_padding_t padding, void *context);
|
||||
|
||||
ali_crypto_result ali_des_reset(void *context);
|
||||
ali_crypto_result ali_des_copy_context(void *dst_ctx, void *src_ctx);
|
||||
|
||||
/********************************************************************/
|
||||
/* Authenticated Encryption */
|
||||
/********************************************************************/
|
||||
/*
|
||||
type: MUST be AES_CCM/AES_GCM
|
||||
size: check size != NULL
|
||||
*/
|
||||
ali_crypto_result ali_authenc_get_ctx_size(authenc_type_t type, size_t *size);
|
||||
/*
|
||||
type: MUST be AES_CCM/AES_GCM
|
||||
is_enc: [true] for encrypt, [false] for decrypt.
|
||||
key: function will read 'keybytes' of data as key.
|
||||
keybytes: MUST be 16(128 bits)/24(256 bits)/32(512 bits).
|
||||
nonce: the operation 'nonce' for AES_CCM, the IV of AES_GCM.
|
||||
function will read 'nonce_len' of data as nonce or IV.
|
||||
nonce_len: the nonce length for AES_CCM, the IV length for AES_GCM.
|
||||
tag_len: the tag byte length.
|
||||
payload_len: only valid for AES_CCM, the payload length. Ignore for AES_GCM.
|
||||
aad_len: only valid for AES_CCM, the aad length. Ignore for AES_GCM.
|
||||
context: function will use size which return from function
|
||||
'ali_authenc_get_ctx_size' as internal context. function will check the [status]
|
||||
of 'context', must be CLEAN or FINISH. function will initialize the [status] of
|
||||
'context' to INIT. function will save the 'type', 'is_enc', or maybe 'nonce',
|
||||
'nonce_len', 'tag_len', 'payload_len', 'aad_len' in 'context'. function will
|
||||
initialize the 'context' to a valid context.
|
||||
*/
|
||||
ali_crypto_result ali_authenc_init(authenc_type_t type, bool is_enc,
|
||||
const uint8_t *key, size_t keybytes,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
size_t tag_len,
|
||||
size_t payload_len, /* valid only in CCM */
|
||||
size_t aad_len, /* valid only in CCM */
|
||||
void *context);
|
||||
/*
|
||||
aad: the address of aad.
|
||||
function will read 'aad_size' of data from this address as aad.
|
||||
aad_size: the length in bytes of aad.
|
||||
for AES_CCM:
|
||||
the total summary of 'aad_size' of multiple calling this
|
||||
function MUST equal to the 'aad_len' parameter in ali_authenc_init. context:
|
||||
function will use size which return from function 'ali_authenc_get_ctx_size' as
|
||||
internal context. function will check it is a valid context. function will check
|
||||
the [status] of 'context', must be INIT or UPDATE_AAD. function will change the
|
||||
[status] of 'context' to UPDATE_AAD.
|
||||
*/
|
||||
ali_crypto_result ali_authenc_update_aad(const uint8_t *aad, size_t aad_size,
|
||||
void *context);
|
||||
/*
|
||||
|
||||
src: function will read 'size' of data from this area as source data.
|
||||
MUST be NULL if 'size' is 0
|
||||
dst: function will write 'size' of data to this area as destination data.
|
||||
MUST be NULL if 'size' is 0
|
||||
size: the length of source data, can be any integer or 0.
|
||||
for AES_CCM.
|
||||
the total summary of 'size' of multiple calling this function
|
||||
MUST equal to the 'payload_len' parameter in ali_authenc_init. context: function
|
||||
will use size which return from function 'ali_authenc_get_ctx_size' as internal
|
||||
context. function will check it is a valid context. function will check the
|
||||
[status] of 'context', must be UPDATE_AAD or PROCESS. function will change the
|
||||
[status] of 'context' to PROCESS. function will do encrypt or decrypt indicated
|
||||
by the content in 'context'.
|
||||
*/
|
||||
ali_crypto_result ali_authenc_process(const uint8_t *src, uint8_t *dst,
|
||||
size_t size, void *context);
|
||||
/*
|
||||
src: function will read 'size' of data from this area as source data.
|
||||
MUST be NULL if 'src_size' is 0.
|
||||
src_size: the length of source data.
|
||||
if 'src_size' == 0, 'src' MUST be NULL, 'dst' MUST be NULL, and this
|
||||
function will reaturn SUCCESS. dst: function will write certain length
|
||||
which is retuned by 'dst_size' of data to this area as destination data. MUST be
|
||||
NULL if 'size' is 0 dst_size: function will wirte some integer to this area to
|
||||
indicate the length of destination data. tag: the tag returned by ae
|
||||
encrypt. tag_len: the tag length. context: function will use size which
|
||||
return from function 'ali_authenc_get_ctx_size' as internal context. function
|
||||
will check it is a valid context. function will check the [status] of 'context',
|
||||
must be UPDATE_AAD or PROCESS. function will change the [status] of 'context' to
|
||||
FINISH. the 'is_enc' indicated by the content in 'context' MUST be ture.
|
||||
function will do encrypt or decrypt indicated by the content in
|
||||
'context'. function MUST clean the content of context before this fucntion
|
||||
return.
|
||||
*/
|
||||
ali_crypto_result ali_authenc_enc_finish(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
uint8_t *tag, size_t *tag_len,
|
||||
void *context);
|
||||
/*
|
||||
src: function will read 'size' of data from this area as source data.
|
||||
MUST be NULL if 'src_size' is 0.
|
||||
src_size: the length of source data.
|
||||
if 'src_size' == 0, 'src' MUST be NULL, 'dst' MUST be NULL, and this
|
||||
function will reaturn SUCCESS. dst: function will write certain length
|
||||
which is retuned by 'dst_size' of data to this area as destination data. MUST be
|
||||
NULL if 'size' is 0 dst_size: function will wirte some integer to this area to
|
||||
indicate the length of destination data. tag: the tag parameter. function
|
||||
will read 'tag_len' of data from this address as the decrypt tag. tag_len: the
|
||||
tag length. context: function will use size which return from function
|
||||
'ali_authenc_get_ctx_size' as internal context. function will check it is a
|
||||
valid context. function will check the [status] of 'context', must be UPDATE_AAD
|
||||
or PROCESS. function will change the [status] of 'context' to FINISH. the
|
||||
'is_enc' indicated by the content in 'context' MUST be false. function will do
|
||||
encrypt or decrypt indicated by the content in 'context'. function MUST clean
|
||||
the content of context before this fucntion return.
|
||||
*/
|
||||
ali_crypto_result ali_authenc_dec_finish(const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
const uint8_t *tag, size_t tag_len,
|
||||
void *context);
|
||||
ali_crypto_result ali_authenc_reset(void *context);
|
||||
ali_crypto_result ali_authenc_copy_context(void *dst_ctx, void *src_ctx);
|
||||
/********************************************************************/
|
||||
/* HASH */
|
||||
/********************************************************************/
|
||||
ali_crypto_result ali_hash_get_ctx_size(hash_type_t type, size_t *size);
|
||||
ali_crypto_result ali_hash_init(hash_type_t type, void *context);
|
||||
ali_crypto_result ali_hash_update(const uint8_t *src, size_t size,
|
||||
void *context);
|
||||
ali_crypto_result ali_hash_final(uint8_t *dgst, void *context);
|
||||
ali_crypto_result ali_hash_reset(void *context);
|
||||
ali_crypto_result ali_hash_copy_context(void *dst_ctx, void *src_ctx);
|
||||
|
||||
ali_crypto_result ali_hash_digest(hash_type_t type, const uint8_t *src,
|
||||
size_t size, uint8_t *dgst);
|
||||
|
||||
/********************************************************************/
|
||||
/* MAC */
|
||||
/********************************************************************/
|
||||
/* hmac */
|
||||
ali_crypto_result ali_hmac_get_ctx_size(hash_type_t type, size_t *size);
|
||||
ali_crypto_result ali_hmac_init(hash_type_t type, const uint8_t *key,
|
||||
size_t keybytes, void *context);
|
||||
ali_crypto_result ali_hmac_update(const uint8_t *src, size_t size,
|
||||
void *context);
|
||||
ali_crypto_result ali_hmac_final(uint8_t *dgst, void *context);
|
||||
ali_crypto_result ali_hmac_reset(void *context);
|
||||
ali_crypto_result ali_hmac_copy_context(void *dst_ctx, void *src_ctx);
|
||||
ali_crypto_result ali_hmac_digest(hash_type_t type, const uint8_t *key,
|
||||
size_t keybytes, const uint8_t *src,
|
||||
size_t size, uint8_t *dgst);
|
||||
|
||||
/* cbcmac */
|
||||
ali_crypto_result ali_cbcmac_get_ctx_size(cbcmac_type_t type, size_t *size);
|
||||
ali_crypto_result ali_cbcmac_init(cbcmac_type_t type, const uint8_t *key,
|
||||
size_t keybytes, void *context);
|
||||
ali_crypto_result ali_cbcmac_update(const uint8_t *src, size_t size,
|
||||
void *context);
|
||||
ali_crypto_result ali_cbcmac_final(sym_padding_t padding, uint8_t *dgst,
|
||||
void *context);
|
||||
ali_crypto_result ali_cbcmac_reset(void *context);
|
||||
ali_crypto_result ali_cbcmac_copy_context(void *dst_ctx, void *src_ctx);
|
||||
ali_crypto_result ali_cbcmac_digest(cbcmac_type_t type, const uint8_t *key,
|
||||
size_t keybytes, const uint8_t *src,
|
||||
size_t size, sym_padding_t padding,
|
||||
uint8_t *dgst);
|
||||
|
||||
/* cmac */
|
||||
ali_crypto_result ali_cmac_get_ctx_size(cmac_type_t type, size_t *size);
|
||||
ali_crypto_result ali_cmac_init(cmac_type_t type, const uint8_t *key,
|
||||
size_t keybytes, void *context);
|
||||
ali_crypto_result ali_cmac_update(const uint8_t *src, size_t size,
|
||||
void *context);
|
||||
ali_crypto_result ali_cmac_final(sym_padding_t padding, uint8_t *dgst,
|
||||
void *context);
|
||||
ali_crypto_result ali_cmac_reset(void *context);
|
||||
ali_crypto_result ali_cmac_copy_context(void *dst_ctx, void *src_ctx);
|
||||
ali_crypto_result ali_cmac_digest(cmac_type_t type, const uint8_t *key,
|
||||
size_t keybytes, const uint8_t *src,
|
||||
size_t size, sym_padding_t padding,
|
||||
uint8_t *dgst);
|
||||
|
||||
/********************************************************************/
|
||||
/* ASYM */
|
||||
/********************************************************************/
|
||||
/* RSA */
|
||||
/*
|
||||
* e: Public exponent
|
||||
* d: Private exponent
|
||||
* n: Modulus
|
||||
*
|
||||
* Optional CRT parameters
|
||||
* p, q: N = pq
|
||||
* qp: 1/q mod p
|
||||
* dp: d mod (p-1)
|
||||
* dq: d mod (q-1)
|
||||
*/
|
||||
|
||||
/*
|
||||
* keybits[in]: key length in bits
|
||||
* size[out]: total size in bytes of rsa keypair
|
||||
*/
|
||||
ali_crypto_result ali_rsa_get_keypair_size(size_t keybits, size_t *size);
|
||||
|
||||
/*
|
||||
* keybits[in]: key length in bits
|
||||
* size[out]: total size in bytes of rsa public key
|
||||
*/
|
||||
ali_crypto_result ali_rsa_get_pubkey_size(size_t keybits, size_t *size);
|
||||
|
||||
/*
|
||||
* Initialize RSA keypair
|
||||
*
|
||||
* keybits[in]: rsa keypair length in bits
|
||||
* n/n_size[in]: rsa modulus data and size in bytes
|
||||
* e/e_size[in]: rsa public exponent data and size in bytes
|
||||
* d/d_size[in]: rsa private exponent data and size in bytes
|
||||
* p/p_size[in]: rsa prime1 data and size in bits, may be NULL/0
|
||||
* q/q_size[in]: rsa prime2 data and size in bits, may be NULL/0
|
||||
* dp/dp_size[in]: rsa exponent2 data and size in bits, may be NULL/0
|
||||
* dq/dq_size[in]: rsa exponent2 data and size in bits, may be NULL/0
|
||||
* dq/dq_size[in]: rsa coefficient data and size in bits, may be NULL/0
|
||||
* keypair[out]: output buffer, which is used to save initialized rsa key pair
|
||||
*/
|
||||
ali_crypto_result ali_rsa_init_keypair(
|
||||
size_t keybits, const uint8_t *n, size_t n_size, const uint8_t *e,
|
||||
size_t e_size, const uint8_t *d, size_t d_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *q, size_t q_size, const uint8_t *dp,
|
||||
size_t dp_size, const uint8_t *dq, size_t dq_size, const uint8_t *qp,
|
||||
size_t qp_size, rsa_keypair_t *keypair);
|
||||
|
||||
/*
|
||||
* Initialize RSA public key
|
||||
*
|
||||
* keybits[in]: rsa key length in bits
|
||||
* n/n_size[in]: rsa modulus data and size in bytes
|
||||
* e/e_size[in]: rsa public exponent data and size in bytes
|
||||
* pubkey[out]: output buffer, which is used to save initialized rsa public
|
||||
* key
|
||||
*/
|
||||
ali_crypto_result ali_rsa_init_pubkey(size_t keybits, const uint8_t *n,
|
||||
size_t n_size, const uint8_t *e,
|
||||
size_t e_size, rsa_pubkey_t *pubkey);
|
||||
|
||||
/*
|
||||
* Generate RSA keypair
|
||||
*
|
||||
* keybits[in]: rsa key length in bits
|
||||
* e[in]: optional, public exponent
|
||||
* e_size[in]: optional, public exponent size in bytes
|
||||
* keypair[out]: output buffer, which is used to save generated rsa key pair
|
||||
*/
|
||||
ali_crypto_result ali_rsa_gen_keypair(size_t keybits, const uint8_t *e,
|
||||
size_t e_size, rsa_keypair_t *keypair);
|
||||
|
||||
/*
|
||||
* Get key attribute
|
||||
*
|
||||
* attr[in]: rsa key attribute ID
|
||||
* keypair[in]: rsa keypair buffer
|
||||
* buffer[out]: buffer, which is used to save required attribute
|
||||
* size[in/out]: buffer max size and key attribute actual size in bytes
|
||||
*/
|
||||
ali_crypto_result ali_rsa_get_key_attr(rsa_key_attr_t attr,
|
||||
rsa_keypair_t *keypair, void *buffer,
|
||||
size_t *size);
|
||||
|
||||
ali_crypto_result ali_rsa_public_encrypt(const rsa_pubkey_t *pub_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
rsa_padding_t padding);
|
||||
ali_crypto_result ali_rsa_private_decrypt(const rsa_keypair_t *priv_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
uint8_t *dst, size_t *dst_size,
|
||||
rsa_padding_t padding);
|
||||
|
||||
/*
|
||||
* dig[in]: the digest to sign
|
||||
* dig_size[in]: the length of the digest to sign (byte)
|
||||
* sig[out]: the signature data
|
||||
* sig_size[in/out]: the buffer size and resulting size of signature
|
||||
*/
|
||||
ali_crypto_result ali_rsa_sign(const rsa_keypair_t *priv_key,
|
||||
const uint8_t *dig, size_t dig_size,
|
||||
uint8_t *sig, size_t *sig_size,
|
||||
rsa_padding_t padding);
|
||||
|
||||
/*
|
||||
* dig[in]: the digest of message that was signed
|
||||
* dig_size[in]: the digest size in bytes
|
||||
* sig[in]: the signature data
|
||||
* sig_size[in]: the length of the signature data (byte)
|
||||
*/
|
||||
ali_crypto_result ali_rsa_verify(const rsa_pubkey_t *pub_key,
|
||||
const uint8_t *dig, size_t dig_size,
|
||||
const uint8_t *sig, size_t sig_size,
|
||||
rsa_padding_t padding, bool *result);
|
||||
|
||||
/* DSA sign/verify */
|
||||
/*
|
||||
g: Generator of subgroup (public)
|
||||
p: Prime number (public)
|
||||
q: Order of subgroup (public)
|
||||
y: Public key
|
||||
x: Private key
|
||||
*/
|
||||
ali_crypto_result ali_dsa_get_keypair_size(size_t keybits, size_t *size);
|
||||
ali_crypto_result ali_dsa_get_pubkey_size(size_t keybits, size_t *size);
|
||||
ali_crypto_result ali_dsa_init_keypair(size_t keybits, const uint8_t *g,
|
||||
size_t g_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *q,
|
||||
size_t q_size, const uint8_t *y,
|
||||
size_t y_size, const uint8_t *x,
|
||||
size_t x_size, dsa_keypair_t *keypair);
|
||||
ali_crypto_result ali_dsa_init_pubkey(size_t keybits, const uint8_t *g,
|
||||
size_t g_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *q,
|
||||
size_t q_size, const uint8_t *y,
|
||||
size_t y_size, dsa_pubkey_t *pubkey);
|
||||
ali_crypto_result ali_dsa_gen_keypair(size_t keybit, const uint8_t *g,
|
||||
size_t g_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *q,
|
||||
size_t q_size, dsa_keypair_t *keypair);
|
||||
ali_crypto_result ali_dsa_sign(const dsa_keypair_t *priv_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
uint8_t *signature, size_t *sig_size,
|
||||
dsa_padding_t padding);
|
||||
ali_crypto_result ali_dsa_verify(const dsa_pubkey_t *pub_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
const uint8_t *signature, size_t sig_size,
|
||||
dsa_padding_t padding, bool *result);
|
||||
|
||||
ali_crypto_result ali_dsa_get_key_attr(dsa_key_attr_t attr,
|
||||
dsa_keypair_t *keypair, void *buffer,
|
||||
uint32_t *size);
|
||||
|
||||
/* DH derive shared secret */
|
||||
/*
|
||||
g: Generator of Z_p
|
||||
p: Prime modulus
|
||||
y: Public key
|
||||
x: Private key
|
||||
q: Optional
|
||||
xbits: Optional
|
||||
*/
|
||||
ali_crypto_result ali_dh_get_keypair_size(size_t keybits, size_t *size);
|
||||
ali_crypto_result ali_dh_get_pubkey_size(size_t keybits, size_t *size);
|
||||
ali_crypto_result ali_dh_init_keypair(size_t keybits, const uint8_t *g,
|
||||
size_t g_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *y,
|
||||
size_t y_size, const uint8_t *x,
|
||||
size_t x_size, const uint8_t *q,
|
||||
size_t q_size, /* optional */
|
||||
size_t xbits, /* optional */
|
||||
dh_keypair_t *keypair);
|
||||
ali_crypto_result ali_dh_init_pubkey(size_t keybits, const uint8_t *y,
|
||||
size_t y_size, dh_pubkey_t *pubkey);
|
||||
ali_crypto_result ali_dh_gen_keypair(size_t keybit, const uint8_t *g,
|
||||
size_t g_size, const uint8_t *p,
|
||||
size_t p_size, const uint8_t *q,
|
||||
size_t q_size, size_t xbits,
|
||||
dh_keypair_t *keypair);
|
||||
ali_crypto_result ali_dh_derive_secret(const dh_keypair_t *priv_key,
|
||||
const dh_pubkey_t *peer_pub_key,
|
||||
uint8_t *shared_secret,
|
||||
size_t *secret_size);
|
||||
ali_crypto_result ali_dh_get_key_attr(dh_key_attr_t attr, dh_keypair_t *keypair,
|
||||
void *buffer, uint32_t *size);
|
||||
|
||||
/*
|
||||
d: Private value
|
||||
x: Public value x
|
||||
y: Public value y
|
||||
curve: Curve type
|
||||
*/
|
||||
ali_crypto_result ali_ecc_get_keypair_size(size_t curve, size_t *size);
|
||||
ali_crypto_result ali_ecc_get_pubkey_size(size_t curve, size_t *size);
|
||||
ali_crypto_result ali_ecc_init_keypair(const uint8_t *x, size_t x_size,
|
||||
const uint8_t *y, size_t y_size,
|
||||
const uint8_t *d, size_t d_size,
|
||||
size_t curve, ecc_keypair_t *keypair);
|
||||
ali_crypto_result ali_ecc_init_pubkey(const uint8_t *x, size_t x_size,
|
||||
const uint8_t *y, size_t y_size,
|
||||
size_t curve, ecc_pubkey_t *pubkey);
|
||||
ali_crypto_result ali_ecc_gen_keypair(size_t curve, ecc_keypair_t *keypair);
|
||||
|
||||
/* ECDSA sign/verify */
|
||||
ali_crypto_result ali_ecdsa_sign(const ecc_keypair_t *priv_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
uint8_t *signature, size_t *sig_size);
|
||||
ali_crypto_result ali_ecdsa_verify(const ecc_pubkey_t *pub_key,
|
||||
const uint8_t *src, size_t src_size,
|
||||
const uint8_t *signature, size_t sig_size,
|
||||
bool *result);
|
||||
|
||||
/* ECDH derive shared secret */
|
||||
ali_crypto_result ali_ecdh_derive_secret(const ecc_keypair_t *priv_key,
|
||||
const ecc_pubkey_t *peer_pubkey_key,
|
||||
uint8_t *shared_secret,
|
||||
size_t *secret_size);
|
||||
|
||||
/* random generator */
|
||||
ali_crypto_result ali_seed(uint8_t *seed, size_t seed_len);
|
||||
ali_crypto_result ali_rand_gen(uint8_t *buf, size_t len);
|
||||
|
||||
ali_crypto_result ali_crypto_init(void);
|
||||
void ali_crypto_cleanup(void);
|
||||
|
||||
#endif /* _ALI_CRYPTO_H_ */
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef _ALI_CRYPTO_TYPES_H_
|
||||
#define _ALI_CRYPTO_TYPES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h> /* for size_t */
|
||||
#include <stdbool.h>
|
||||
|
||||
#if 0
|
||||
typedef unsigned char bool;
|
||||
#endif
|
||||
|
||||
#ifndef false
|
||||
#define false(0)
|
||||
#endif
|
||||
|
||||
#ifndef true
|
||||
#define true(1)
|
||||
#endif
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
|
||||
#endif /* _ALI_CRYPTO_TYPES_H_ */
|
||||
19
Living_SDK/framework/bluetooth/breeze/hal/ble/ble.mk
Normal file
19
Living_SDK/framework/bluetooth/breeze/hal/ble/ble.mk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
NAME := breeze_hal
|
||||
|
||||
$(NAME)_MBINS_TYPE := kernel
|
||||
$(NAME)_VERSION := 1.0.0
|
||||
$(NAME)_SUMMARY := This is an AliOS Things based implementation of Breeze HAP APIs..
|
||||
|
||||
$(NAME)_SOURCES := breeze_hal_ble.c breeze_hal_os.c
|
||||
|
||||
#$(NAME)_COMPONENTS := bluetooth.bt_host
|
||||
|
||||
enhanced_auth ?= 1
|
||||
ifeq ($(enhanced_auth),1)
|
||||
$(NAME)_SOURCES += breeze_hal_sec.c
|
||||
$(NAME)_SOURCES += aes.c
|
||||
$(NAME)_SOURCES += aes_mbed.c
|
||||
$(NAME)_INCLUDES += include/mbedtls
|
||||
endif
|
||||
|
||||
GLOBAL_DEFINES-y += MBEDTLS_AES_ROM_TABLES
|
||||
564
Living_SDK/framework/bluetooth/breeze/hal/ble/breeze_hal_ble.c
Normal file
564
Living_SDK/framework/bluetooth/breeze/hal/ble/breeze_hal_ble.c
Normal file
|
|
@ -0,0 +1,564 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <breeze_hal_ble.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
#include "aos/kernel.h"
|
||||
#include <aos/list.h>
|
||||
#include "breeze_hal_os.h"
|
||||
|
||||
struct bt_conn *g_conn = NULL;
|
||||
ais_bt_init_t * bt_init_info = NULL;
|
||||
|
||||
#define BT_UUID_AIS_SERVICE BT_UUID_DECLARE_16(BLE_UUID_AIS_SERVICE)
|
||||
#define BT_UUID_AIS_RC BT_UUID_DECLARE_16(BLE_UUID_AIS_RC)
|
||||
#define BT_UUID_AIS_WC BT_UUID_DECLARE_16(BLE_UUID_AIS_WC)
|
||||
#define BT_UUID_AIS_IC BT_UUID_DECLARE_16(BLE_UUID_AIS_IC)
|
||||
#define BT_UUID_AIS_WWNRC BT_UUID_DECLARE_16(BLE_UUID_AIS_WWNRC)
|
||||
#define BT_UUID_AIS_NC BT_UUID_DECLARE_16(BLE_UUID_AIS_NC)
|
||||
|
||||
static struct bt_gatt_ccc_cfg ais_ic_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
static struct bt_gatt_ccc_cfg ais_nc_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
|
||||
static void (*g_indication_txdone)(uint8_t res);
|
||||
|
||||
void ble_disconnect(uint8_t reason)
|
||||
{
|
||||
uint8_t zreason = 0;
|
||||
|
||||
if (!g_conn) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (reason) {
|
||||
case AIS_BT_REASON_REMOTE_USER_TERM_CONN:
|
||||
zreason = BT_HCI_ERR_REMOTE_USER_TERM_CONN;
|
||||
break;
|
||||
default:
|
||||
zreason = BT_HCI_ERR_UNSPECIFIED;
|
||||
break;
|
||||
}
|
||||
|
||||
bt_conn_disconnect(g_conn, zreason);
|
||||
}
|
||||
|
||||
static void connected(struct bt_conn *conn, uint8_t err)
|
||||
{
|
||||
if (err) {
|
||||
printf("Connection failed (err %u)\n", err);
|
||||
} else {
|
||||
printf("Connected\n");
|
||||
g_conn = conn;
|
||||
if (bt_init_info && (bt_init_info->on_connected)) {
|
||||
bt_init_info->on_connected();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void disconnected(struct bt_conn *conn, u8_t reason)
|
||||
{
|
||||
printf("Disconnected (reason %u)\n", reason);
|
||||
g_conn = NULL;
|
||||
if (bt_init_info && (bt_init_info->on_disconnected)) {
|
||||
bt_init_info->on_disconnected();
|
||||
}
|
||||
}
|
||||
|
||||
static void ais_nc_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
||||
uint16_t value)
|
||||
{
|
||||
ais_ccc_value_t val;
|
||||
|
||||
switch (value) {
|
||||
case BT_GATT_CCC_NOTIFY:
|
||||
printf("CCC cfg changed to NOTIFY (%d).\r\n", value);
|
||||
val = AIS_CCC_VALUE_NOTIFY;
|
||||
break;
|
||||
default:
|
||||
printf("%s CCC cfg changed to %d.\r\n", __func__, value);
|
||||
val = AIS_CCC_VALUE_NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bt_init_info && bt_init_info->nc.on_ccc_change) {
|
||||
bt_init_info->nc.on_ccc_change(val);
|
||||
}
|
||||
}
|
||||
|
||||
static void ais_ic_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
||||
uint16_t value)
|
||||
{
|
||||
ais_ccc_value_t val;
|
||||
|
||||
switch (value) {
|
||||
case BT_GATT_CCC_INDICATE:
|
||||
printf("CCC cfg changed to INDICATE (%d).\r\n", value);
|
||||
val = AIS_CCC_VALUE_INDICATE;
|
||||
break;
|
||||
default:
|
||||
printf("%s CCC cfg changed to %d.\r\n", __func__, value);
|
||||
val = AIS_CCC_VALUE_NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bt_init_info && bt_init_info->ic.on_ccc_change) {
|
||||
bt_init_info->ic.on_ccc_change(val);
|
||||
}
|
||||
}
|
||||
|
||||
static struct bt_conn_cb conn_callbacks = {
|
||||
.connected = connected,
|
||||
.disconnected = disconnected,
|
||||
};
|
||||
|
||||
static ssize_t read_ais_rc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
printf("%s length to read: %d, offset: %d\r\n", __func__, len, offset);
|
||||
if (bt_init_info && bt_init_info->rc.on_read) {
|
||||
ret = bt_init_info->rc.on_read(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t read_ais_wc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
printf("%s length to read: %d, offset: %d\r\n", __func__, len, offset);
|
||||
if (bt_init_info && bt_init_info->wc.on_read) {
|
||||
ret = bt_init_info->wc.on_read(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t write_ais_wc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, const void *buf,
|
||||
uint16_t len, uint16_t offset, uint8_t flags)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
if (bt_init_info && bt_init_info->wc.on_write) {
|
||||
ret = bt_init_info->wc.on_write(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t read_ais_ic(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
printf("%s length to read: %d, offset: %d\r\n", __func__, len, offset);
|
||||
if (bt_init_info && bt_init_info->ic.on_read) {
|
||||
ret = bt_init_info->ic.on_read(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t read_ais_wwnrc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
printf("%s length to read: %d, offset: %d\r\n", __func__, len, offset);
|
||||
if (bt_init_info && bt_init_info->wwnrc.on_read) {
|
||||
ret = bt_init_info->wwnrc.on_read(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t write_ais_wwnrc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, const void *buf,
|
||||
uint16_t len, uint16_t offset, uint8_t flags)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
if (bt_init_info && bt_init_info->wwnrc.on_write) {
|
||||
ret = bt_init_info->wwnrc.on_write(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t read_ais_nc(struct bt_conn * conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
|
||||
printf("%s length to read: %d, offset: %d\r\n", __func__, len, offset);
|
||||
if (bt_init_info && bt_init_info->nc.on_read) {
|
||||
ret = bt_init_info->nc.on_read(buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct bt_gatt_attr * ais_attrs = NULL;
|
||||
static struct bt_gatt_service ais_svc;
|
||||
|
||||
struct bt_uuid *bt_prisvc_uuid = BT_UUID_GATT_PRIMARY;
|
||||
struct bt_uuid *bt_chrc_uuid = BT_UUID_GATT_CHRC;
|
||||
struct bt_uuid *bt_ccc_uuid = BT_UUID_GATT_CCC;
|
||||
|
||||
static int setup_ais_service_attr(struct bt_gatt_attr *attr,
|
||||
struct bt_uuid * uuid)
|
||||
{
|
||||
attr->uuid = bt_prisvc_uuid;
|
||||
attr->perm = BT_GATT_PERM_READ;
|
||||
attr->read = bt_gatt_attr_read_service;
|
||||
attr->user_data = (void *)uuid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int setup_ais_char_attr(struct bt_gatt_attr *attr, struct bt_uuid *uuid,
|
||||
uint8_t prop)
|
||||
{
|
||||
struct bt_gatt_chrc *chrc = NULL;
|
||||
|
||||
chrc = (struct bt_gatt_chrc *)aos_malloc(sizeof(struct bt_gatt_chrc));
|
||||
if (!chrc) {
|
||||
printf("%s malloc failed\r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
chrc->uuid = uuid;
|
||||
chrc->properties = prop;
|
||||
|
||||
attr->uuid = bt_chrc_uuid;
|
||||
attr->perm = BT_GATT_PERM_READ;
|
||||
attr->read = bt_gatt_attr_read_chrc;
|
||||
attr->user_data = (void *)chrc;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int setup_ais_char_desc_attr(struct bt_gatt_attr *attr,
|
||||
struct bt_uuid *uuid, uint8_t perm,
|
||||
void *read, void *write, void *userdata)
|
||||
{
|
||||
attr->uuid = uuid;
|
||||
attr->perm = perm;
|
||||
attr->read = read;
|
||||
attr->write = write;
|
||||
attr->user_data = userdata;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int setup_ais_char_ccc_attr(struct bt_gatt_attr * attr,
|
||||
struct bt_gatt_ccc_cfg *cfg, size_t cfg_len,
|
||||
void *cfg_handler)
|
||||
{
|
||||
struct _bt_gatt_ccc *ccc = NULL;
|
||||
|
||||
ccc = (struct _bt_gatt_ccc *)aos_malloc(sizeof(struct _bt_gatt_ccc));
|
||||
if (!ccc) {
|
||||
printf("%s malloc failed.\r\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ccc->cfg = cfg;
|
||||
ccc->cfg_len = cfg_len;
|
||||
ccc->cfg_changed = cfg_handler;
|
||||
|
||||
attr->uuid = bt_ccc_uuid;
|
||||
attr->perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE;
|
||||
attr->read = bt_gatt_attr_read_ccc;
|
||||
attr->write = bt_gatt_attr_write_ccc;
|
||||
attr->user_data = (void *)ccc;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
SVC_ATTR_IDX = 0,
|
||||
RC_CHRC_ATTR_IDX,
|
||||
RC_DESC_ATTR_IDX,
|
||||
WC_CHRC_ATTR_IDX,
|
||||
WC_DESC_ATTR_IDX,
|
||||
IC_CHRC_ATTR_IDX,
|
||||
IC_DESC_ATTR_IDX,
|
||||
IC_CCC_ATTR_IDX,
|
||||
WWNRC_CHRC_ATTR_IDX,
|
||||
WWNRC_DESC_ATTR_IDX,
|
||||
NC_CHRC_ATTR_IDX,
|
||||
NC_DESC_ATTR_IDX,
|
||||
NC_CCC_ATTR_IDX,
|
||||
/* Add more entry here if necessary */
|
||||
AIS_ATTR_NUM,
|
||||
};
|
||||
|
||||
ais_err_t ble_stack_init(ais_bt_init_t *info)
|
||||
{
|
||||
int err;
|
||||
uint32_t attr_cnt = AIS_ATTR_NUM, size;
|
||||
ais_char_init_t *c;
|
||||
|
||||
bt_init_info = info;
|
||||
|
||||
hci_driver_init();
|
||||
ais_ota_bt_storage_init();
|
||||
err = bt_enable(NULL);
|
||||
if (err) {
|
||||
printf("Bluetooth init failed (err %d)\n", err);
|
||||
return AIS_ERR_STACK_FAIL;
|
||||
}
|
||||
printf("Bluetooth init succeed.\n");
|
||||
|
||||
size = attr_cnt * sizeof(struct bt_gatt_attr);
|
||||
ais_attrs = (struct bt_gatt_attr *)aos_malloc(size);
|
||||
if (!ais_attrs) {
|
||||
printf("%s %d memory allocate failed.\r\n", __func__, __LINE__);
|
||||
return AIS_ERR_MEM_FAIL;
|
||||
}
|
||||
|
||||
memset(ais_attrs, 0, size);
|
||||
|
||||
/* AIS primary service */
|
||||
setup_ais_service_attr(&ais_attrs[SVC_ATTR_IDX], info->uuid_svc);
|
||||
|
||||
/* rc */
|
||||
c = &(info->rc);
|
||||
setup_ais_char_attr(&ais_attrs[RC_CHRC_ATTR_IDX], c->uuid, c->prop);
|
||||
setup_ais_char_desc_attr(&ais_attrs[RC_DESC_ATTR_IDX], c->uuid, c->perm,
|
||||
read_ais_rc, NULL, NULL);
|
||||
|
||||
/* wc */
|
||||
c = &(info->wc);
|
||||
setup_ais_char_attr(&ais_attrs[WC_CHRC_ATTR_IDX], c->uuid, c->prop);
|
||||
setup_ais_char_desc_attr(&ais_attrs[WC_DESC_ATTR_IDX], c->uuid, c->perm,
|
||||
read_ais_wc, write_ais_wc, NULL);
|
||||
|
||||
/* ic */
|
||||
c = &(info->ic);
|
||||
setup_ais_char_attr(&ais_attrs[IC_CHRC_ATTR_IDX], c->uuid, c->prop);
|
||||
setup_ais_char_desc_attr(&ais_attrs[IC_DESC_ATTR_IDX], c->uuid, c->perm,
|
||||
read_ais_ic, NULL, NULL);
|
||||
setup_ais_char_ccc_attr(&ais_attrs[IC_CCC_ATTR_IDX], ais_ic_ccc_cfg,
|
||||
sizeof(ais_ic_ccc_cfg) / sizeof(ais_ic_ccc_cfg[0]),
|
||||
ais_ic_ccc_cfg_changed);
|
||||
|
||||
/* wwnrc */
|
||||
c = &(info->wwnrc);
|
||||
setup_ais_char_attr(&ais_attrs[WWNRC_CHRC_ATTR_IDX], c->uuid, c->prop);
|
||||
setup_ais_char_desc_attr(&ais_attrs[WWNRC_DESC_ATTR_IDX], c->uuid, c->perm,
|
||||
read_ais_wwnrc, write_ais_wwnrc, NULL);
|
||||
|
||||
/* nc */
|
||||
c = &(info->nc);
|
||||
setup_ais_char_attr(&ais_attrs[NC_CHRC_ATTR_IDX], c->uuid, c->prop);
|
||||
setup_ais_char_desc_attr(&ais_attrs[NC_DESC_ATTR_IDX], c->uuid, c->perm,
|
||||
read_ais_nc, NULL, NULL);
|
||||
setup_ais_char_ccc_attr(&ais_attrs[NC_CCC_ATTR_IDX], ais_nc_ccc_cfg,
|
||||
sizeof(ais_nc_ccc_cfg) / sizeof(ais_nc_ccc_cfg[0]),
|
||||
ais_nc_ccc_cfg_changed);
|
||||
|
||||
memset(&ais_svc, 0, sizeof(ais_svc));
|
||||
ais_svc.attrs = ais_attrs;
|
||||
ais_svc.attr_count = attr_cnt;
|
||||
|
||||
bt_conn_cb_register(&conn_callbacks);
|
||||
bt_gatt_service_register(&ais_svc);
|
||||
return AIS_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
ais_err_t ble_stack_deinit()
|
||||
{
|
||||
if (ais_attrs) {
|
||||
aos_free(ais_attrs);
|
||||
}
|
||||
|
||||
/* Free other memory here when necessary. */
|
||||
|
||||
return AIS_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
ais_err_t ble_send_notification(uint8_t *p_data, uint16_t length)
|
||||
{
|
||||
return bt_gatt_notify(NULL, &ais_attrs[NC_DESC_ATTR_IDX],
|
||||
(const void *)p_data, length);
|
||||
}
|
||||
|
||||
slist_t params_list;
|
||||
|
||||
typedef struct bt_gatt_indicate_param_s
|
||||
{
|
||||
slist_t next;
|
||||
struct bt_gatt_indicate_params *ind_params;
|
||||
} bt_gatt_indicate_param_t;
|
||||
|
||||
static void indicate_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
uint8_t err)
|
||||
{
|
||||
bt_gatt_indicate_param_t *param;
|
||||
|
||||
param = slist_first_entry(¶ms_list, bt_gatt_indicate_param_t, next);
|
||||
if (param) {
|
||||
aos_free(param->ind_params);
|
||||
slist_del(¶m->next, ¶ms_list);
|
||||
aos_free(param);
|
||||
}
|
||||
g_indication_txdone(0);
|
||||
}
|
||||
|
||||
ais_err_t ble_send_indication(uint8_t *p_data, uint16_t length, void (*txdone)(uint8_t res))
|
||||
{
|
||||
int err;
|
||||
struct bt_gatt_indicate_params *ind_params;
|
||||
bt_gatt_indicate_param_t * param;
|
||||
|
||||
param = aos_malloc(sizeof(bt_gatt_indicate_param_t));
|
||||
if (param == NULL) {
|
||||
return AIS_ERR_MEM_FAIL;
|
||||
}
|
||||
|
||||
ind_params = aos_malloc(sizeof(struct bt_gatt_indicate_params));
|
||||
if (ind_params == NULL) {
|
||||
aos_free(param);
|
||||
return AIS_ERR_MEM_FAIL;
|
||||
}
|
||||
|
||||
param->ind_params = ind_params;
|
||||
slist_add_tail(¶m->next, ¶ms_list);
|
||||
|
||||
ind_params->attr = &ais_attrs[IC_DESC_ATTR_IDX];
|
||||
ind_params->func = indicate_cb;
|
||||
ind_params->data = p_data;
|
||||
ind_params->len = length;
|
||||
|
||||
err = bt_gatt_indicate(NULL, ind_params);
|
||||
|
||||
if (err) {
|
||||
aos_free(param->ind_params);
|
||||
slist_del(¶m->next, ¶ms_list);
|
||||
aos_free(param);
|
||||
return AIS_ERR_GATT_INDICATE_FAIL;
|
||||
} else {
|
||||
g_indication_txdone = txdone;
|
||||
return AIS_ERR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct bt_data sd[] = {
|
||||
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME,
|
||||
sizeof(CONFIG_BT_DEVICE_NAME) - 1),
|
||||
};
|
||||
|
||||
ais_err_t ble_advertising_start(ais_adv_init_t *adv)
|
||||
{
|
||||
int err;
|
||||
uint8_t flag = 0, srv[] = { 0xb3, 0xfe }, ad_len = 3, sd_len = 1;
|
||||
struct bt_data ad[ad_len];
|
||||
struct bt_data sd[sd_len];
|
||||
|
||||
if (adv->flag & AIS_AD_GENERAL) {
|
||||
flag |= BT_LE_AD_GENERAL;
|
||||
}
|
||||
if (adv->flag & AIS_AD_NO_BREDR) {
|
||||
flag |= BT_LE_AD_NO_BREDR;
|
||||
}
|
||||
if (!flag) {
|
||||
printf("Invalid adv flag.\r\n");
|
||||
return AIS_ERR_INVALID_ADV_DATA;
|
||||
}
|
||||
|
||||
ad[0].type = BT_DATA_FLAGS;
|
||||
ad[0].data = &flag;
|
||||
ad[0].data_len = 1;
|
||||
|
||||
ad[1].type = BT_DATA_UUID16_ALL;
|
||||
ad[1].data = srv;
|
||||
ad[1].data_len = sizeof(srv);
|
||||
|
||||
if (adv->vdata.len != 0) {
|
||||
ad[2].type = 0xFF;
|
||||
ad[2].data_len = adv->vdata.len;
|
||||
ad[2].data = adv->vdata.data;
|
||||
} else {
|
||||
ad_len--;
|
||||
}
|
||||
|
||||
switch (adv->name.ntype) {
|
||||
case AIS_ADV_NAME_SHORT:
|
||||
sd[0].type = BT_DATA_NAME_SHORTENED;
|
||||
break;
|
||||
case AIS_ADV_NAME_FULL:
|
||||
sd[0].type = BT_DATA_NAME_COMPLETE;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid adv name type.\r\n");
|
||||
return AIS_ERR_INVALID_ADV_DATA;
|
||||
}
|
||||
|
||||
if (adv->name.name == NULL) {
|
||||
printf("Invalid adv device name.\r\n");
|
||||
return AIS_ERR_INVALID_ADV_DATA;
|
||||
}
|
||||
|
||||
sd[0].data = adv->name.name;
|
||||
sd[0].data_len = strlen(adv->name.name);
|
||||
|
||||
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ad_len, sd, sd_len);
|
||||
if (err) {
|
||||
printf("Advertising failed to start (err %d)\n", err);
|
||||
return AIS_ERR_ADV_FAIL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ais_err_t ble_advertising_stop()
|
||||
{
|
||||
int ret;
|
||||
ret = bt_le_adv_stop();
|
||||
return ret ? AIS_ERR_STOP_ADV_FAIL : 0;
|
||||
}
|
||||
|
||||
ais_err_t ble_get_mac(uint8_t *mac)
|
||||
{
|
||||
ais_err_t err;
|
||||
bt_addr_le_t laddr;
|
||||
|
||||
err = ais_ota_get_local_addr(&laddr);
|
||||
if (err != AIS_ERR_SUCCESS) {
|
||||
printf("Failed to get local addr.\r\n");
|
||||
} else {
|
||||
memcpy(mac, laddr.a.val, 6);
|
||||
printf("Local addr got (%02x:%02x:%02x:%02x:%02x:%02x).\n", mac[0],
|
||||
mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef EN_LONG_MTU
|
||||
int ble_get_att_mtu(uint16_t *att_mtu)
|
||||
{
|
||||
if(att_mtu == NULL || g_conn == NULL){
|
||||
BZ_LOG_E("Failed to get ble connection\r\n");
|
||||
return -1;
|
||||
}
|
||||
*att_mtu = bt_gatt_get_mtu(g_conn);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include "aos/kernel.h"
|
||||
#include <breeze_hal_os.h>
|
||||
|
||||
int os_timer_new(os_timer_t *timer, os_timer_cb_t cb, void *arg, int ms)
|
||||
{
|
||||
return aos_timer_new_ext(timer, cb, arg, ms, 0, 0);
|
||||
}
|
||||
|
||||
int os_timer_start(os_timer_t *timer)
|
||||
{
|
||||
return aos_timer_start(timer);
|
||||
}
|
||||
|
||||
int os_timer_stop(os_timer_t *timer)
|
||||
{
|
||||
return aos_timer_stop(timer);
|
||||
}
|
||||
|
||||
void os_timer_free(os_timer_t *timer)
|
||||
{
|
||||
aos_timer_free(timer);
|
||||
}
|
||||
|
||||
void os_reboot()
|
||||
{
|
||||
aos_reboot();
|
||||
}
|
||||
|
||||
void os_msleep(int ms)
|
||||
{
|
||||
aos_msleep(ms);
|
||||
}
|
||||
|
||||
long long os_now_ms()
|
||||
{
|
||||
return aos_now_ms();
|
||||
}
|
||||
|
||||
int os_kv_set(const char *key, const void *value, int len, int sync)
|
||||
{
|
||||
return aos_kv_set(key, value, len, sync);
|
||||
}
|
||||
|
||||
int os_kv_get(const char *key, void *buffer, int *buffer_len)
|
||||
{
|
||||
return aos_kv_get(key, buffer, buffer_len);
|
||||
}
|
||||
|
||||
int os_kv_del(const char *key)
|
||||
{
|
||||
return aos_kv_del(key);
|
||||
}
|
||||
|
||||
int os_rand(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
103
Living_SDK/framework/bluetooth/breeze/hal/ble/breeze_hal_sec.c
Normal file
103
Living_SDK/framework/bluetooth/breeze/hal/ble/breeze_hal_sec.c
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <breeze_hal_sec.h>
|
||||
|
||||
#include "aos/kernel.h"
|
||||
#include "aos/log.h"
|
||||
|
||||
#include "ali_crypto.h"
|
||||
|
||||
#define KEY_LEN 16
|
||||
|
||||
void *ais_aes128_init(const uint8_t *key, const uint8_t *iv)
|
||||
{
|
||||
ali_crypto_result result;
|
||||
void *aes_ctx;
|
||||
size_t aes_ctx_size, alloc_siz;
|
||||
uint8_t *p;
|
||||
|
||||
result = breeze_aes_get_ctx_size(AES_CBC, &aes_ctx_size);
|
||||
if (result != ALI_CRYPTO_SUCCESS) {
|
||||
LOGE("aos_awss", "get ctx size fail(%08x)", result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
alloc_siz = aes_ctx_size + KEY_LEN * 2 + sizeof(bool);
|
||||
aes_ctx = aos_malloc(alloc_siz);
|
||||
if (aes_ctx == NULL) {
|
||||
LOGE("aos_awss", "kmalloc(%d) fail", (int)aes_ctx_size);
|
||||
return NULL;
|
||||
}
|
||||
memset(aes_ctx, 0, alloc_siz);
|
||||
|
||||
p = (uint8_t *)aes_ctx + aes_ctx_size;
|
||||
memcpy(p, key, KEY_LEN);
|
||||
p += KEY_LEN;
|
||||
memcpy(p, iv, KEY_LEN);
|
||||
|
||||
return aes_ctx;
|
||||
}
|
||||
|
||||
int ais_aes128_destroy(void *aes)
|
||||
{
|
||||
if (aes) {
|
||||
aos_free(aes);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int platform_aes128_encrypt_decrypt(void *aes_ctx, const void *src,
|
||||
size_t siz, void *dst, bool is_enc)
|
||||
{
|
||||
ali_crypto_result result;
|
||||
size_t dlen, in_len = siz, ctx_siz;
|
||||
uint8_t *p, *key, *iv;
|
||||
if (aes_ctx == NULL) {
|
||||
LOGE("aos_awss", "platform_aes128_encrypt_decrypt aes_ctx is NULL");
|
||||
return -1;
|
||||
}
|
||||
result = breeze_aes_get_ctx_size(AES_CBC, &ctx_siz);
|
||||
if (result != ALI_CRYPTO_SUCCESS) {
|
||||
LOGE("aos_awss", "get ctx size fail(%08x)", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = (uint8_t *)aes_ctx + ctx_siz;
|
||||
key = p;
|
||||
p += KEY_LEN;
|
||||
iv = p;
|
||||
|
||||
in_len <<= 4;
|
||||
dlen = in_len;
|
||||
|
||||
result = breeze_aes_init(AES_CBC, is_enc, key, NULL, KEY_LEN, iv, aes_ctx);
|
||||
if (result != ALI_CRYPTO_SUCCESS) {
|
||||
LOGE("aos_awss", "breeze_aes_init fail(%08x)", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = breeze_aes_finish(src, in_len, dst, &dlen, SYM_NOPAD, aes_ctx);
|
||||
if (result != ALI_CRYPTO_SUCCESS) {
|
||||
LOGE("aos_awss", "aes_finish fail(%08x)", result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ais_aes128_cbc_encrypt(void *aes, const void *src, size_t block_num,
|
||||
void *dst)
|
||||
{
|
||||
return platform_aes128_encrypt_decrypt(aes, src, block_num, dst, true);
|
||||
}
|
||||
|
||||
int ais_aes128_cbc_decrypt(void *aes, const void *src, size_t block_num,
|
||||
void *dst)
|
||||
{
|
||||
return platform_aes128_encrypt_decrypt(aes, src, block_num, dst, false);
|
||||
}
|
||||
|
|
@ -0,0 +1,289 @@
|
|||
/**
|
||||
* \file aes.h
|
||||
*
|
||||
* \brief AES block cipher
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
#ifndef MBEDTLS_AES_H
|
||||
#define MBEDTLS_AES_H
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* padlock.c and aesni.c rely on these values! */
|
||||
#define MBEDTLS_AES_ENCRYPT 1
|
||||
#define MBEDTLS_AES_DECRYPT 0
|
||||
|
||||
#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
|
||||
#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH \
|
||||
-0x0022 /**< Invalid data input length. */
|
||||
|
||||
#if !defined(MBEDTLS_AES_ALT)
|
||||
// Regular implementation
|
||||
//
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief AES context structure
|
||||
*
|
||||
* \note buf is able to hold 32 extra bytes, which can be used:
|
||||
* - for alignment purposes if VIA padlock is used, and/or
|
||||
* - to simplify key expansion in the 256-bit case by
|
||||
* generating an extra round key
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int nr; /*!< number of rounds */
|
||||
uint32_t *rk; /*!< AES round keys */
|
||||
uint32_t buf[68]; /*!< unaligned data */
|
||||
} mbedtls_aes_context;
|
||||
|
||||
/**
|
||||
* \brief Initialize AES context
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
*/
|
||||
void mbedtls_aes_init(mbedtls_aes_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief Clear AES context
|
||||
*
|
||||
* \param ctx AES context to be cleared
|
||||
*/
|
||||
void mbedtls_aes_free(mbedtls_aes_context *ctx);
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (encryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key encryption key
|
||||
* \param keybits must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx,
|
||||
const unsigned char *key, unsigned int keybits);
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (decryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key decryption key
|
||||
* \param keybits must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx,
|
||||
const unsigned char *key, unsigned int keybits);
|
||||
|
||||
/**
|
||||
* \brief AES-ECB block encryption/decryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
/**
|
||||
* \brief AES-CBC buffer encryption/decryption
|
||||
* Length should be a multiple of the block
|
||||
* size (16 bytes)
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you
|
||||
* can call the function same function again on the following block(s) of
|
||||
* data and get the same result as if it was encrypted in one call. This
|
||||
* allows a "streaming" usage. If on the other hand you need to retain the
|
||||
* contents of the IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
|
||||
*/
|
||||
int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, int mode, size_t length,
|
||||
unsigned char iv[16], const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
/**
|
||||
* \brief AES-CFB128 buffer encryption/decryption.
|
||||
*
|
||||
* Note: Due to the nature of CFB you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and
|
||||
* MBEDTLS_AES_DECRYPT.
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you
|
||||
* can call the function same function again on the following block(s) of
|
||||
* data and get the same result as if it was encrypted in one call. This
|
||||
* allows a "streaming" usage. If on the other hand you need to retain the
|
||||
* contents of the IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv_off offset in IV (updated after use)
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx, int mode,
|
||||
size_t length, size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
|
||||
/**
|
||||
* \brief AES-CFB8 buffer encryption/decryption.
|
||||
*
|
||||
* Note: Due to the nature of CFB you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and
|
||||
* MBEDTLS_AES_DECRYPT.
|
||||
*
|
||||
* \note Upon exit, the content of the IV is updated so that you
|
||||
* can call the function same function again on the following block(s) of
|
||||
* data and get the same result as if it was encrypted in one call. This
|
||||
* allows a "streaming" usage. If on the other hand you need to retain the
|
||||
* contents of the IV, you should either save it manually or use the cipher
|
||||
* module instead.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx, int mode,
|
||||
size_t length, unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /*MBEDTLS_CIPHER_MODE_CFB */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
/**
|
||||
* \brief AES-CTR buffer encryption/decryption
|
||||
*
|
||||
* Warning: You have to keep the maximum use of your counter in mind!
|
||||
*
|
||||
* Note: Due to the nature of CTR you should use the same key schedule for
|
||||
* both encryption and decryption. So a context initialized with
|
||||
* mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and
|
||||
* MBEDTLS_AES_DECRYPT.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param length The length of the data
|
||||
* \param nc_off The offset in the current stream_block (for resuming
|
||||
* within current cipher stream). The offset pointer to
|
||||
* should be 0 at the start of a stream.
|
||||
* \param nonce_counter The 128-bit nonce and counter.
|
||||
* \param stream_block The saved stream-block for resuming. Is overwritten
|
||||
* by the function.
|
||||
* \param input The input data stream
|
||||
* \param output The output data stream
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx, size_t length,
|
||||
size_t *nc_off, unsigned char nonce_counter[16],
|
||||
unsigned char stream_block[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
/**
|
||||
* \brief Internal AES block encryption function
|
||||
* (Only exposed to allow overriding it,
|
||||
* see MBEDTLS_AES_ENCRYPT_ALT)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param input Plaintext block
|
||||
* \param output Output (ciphertext) block
|
||||
*/
|
||||
void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief Internal AES block decryption function
|
||||
* (Only exposed to allow overriding it,
|
||||
* see MBEDTLS_AES_DECRYPT_ALT)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param input Ciphertext block
|
||||
* \param output Output (plaintext) block
|
||||
*/
|
||||
void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#else /* MBEDTLS_AES_ALT */
|
||||
#include "aes_alt.h"
|
||||
#endif /* MBEDTLS_AES_ALT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int mbedtls_aes_self_test(int verbose);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* aes.h */
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
/**
|
||||
* \file aesni.h
|
||||
*
|
||||
* \brief AES-NI for hardware AES acceleration on some Intel processors
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
#ifndef MBEDTLS_AESNI_H
|
||||
#define MBEDTLS_AESNI_H
|
||||
|
||||
#include "aes.h"
|
||||
|
||||
#define MBEDTLS_AESNI_AES 0x02000000u
|
||||
#define MBEDTLS_AESNI_CLMUL 0x00000002u
|
||||
|
||||
#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
|
||||
(defined(__amd64__) || defined(__x86_64__)) && !defined(MBEDTLS_HAVE_X86_64)
|
||||
#define MBEDTLS_HAVE_X86_64
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVE_X86_64)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief AES-NI features detection routine
|
||||
*
|
||||
* \param what The feature to detect
|
||||
* (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
|
||||
*
|
||||
* \return 1 if CPU has support for the feature, 0 otherwise
|
||||
*/
|
||||
int mbedtls_aesni_has_support(unsigned int what);
|
||||
|
||||
/**
|
||||
* \brief AES-NI AES-ECB block en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 on success (cannot fail)
|
||||
*/
|
||||
int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx, int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief GCM multiplication: c = a * b in GF(2^128)
|
||||
*
|
||||
* \param c Result
|
||||
* \param a First operand
|
||||
* \param b Second operand
|
||||
*
|
||||
* \note Both operands and result are bit strings interpreted as
|
||||
* elements of GF(2^128) as per the GCM spec.
|
||||
*/
|
||||
void mbedtls_aesni_gcm_mult(unsigned char c[16], const unsigned char a[16],
|
||||
const unsigned char b[16]);
|
||||
|
||||
/**
|
||||
* \brief Compute decryption round keys from encryption round keys
|
||||
*
|
||||
* \param invkey Round keys for the equivalent inverse cipher
|
||||
* \param fwdkey Original round keys (for encryption)
|
||||
* \param nr Number of rounds (that is, number of round keys minus
|
||||
* one)
|
||||
*/
|
||||
void mbedtls_aesni_inverse_key(unsigned char *invkey,
|
||||
const unsigned char *fwdkey, int nr);
|
||||
|
||||
/**
|
||||
* \brief Perform key expansion (for encryption)
|
||||
*
|
||||
* \param rk Destination buffer where the round keys are written
|
||||
* \param key Encryption key
|
||||
* \param bits Key size in bits (must be 128, 192 or 256)
|
||||
*
|
||||
* \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int mbedtls_aesni_setkey_enc(unsigned char *rk, const unsigned char *key,
|
||||
size_t bits);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MBEDTLS_HAVE_X86_64 */
|
||||
|
||||
#endif /* MBEDTLS_AESNI_H */
|
||||
|
|
@ -0,0 +1,673 @@
|
|||
/**
|
||||
* \file check_config.h
|
||||
*
|
||||
* \brief Consistency checks for configuration options
|
||||
*
|
||||
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/*
|
||||
* It is recommended to include this file from your config.h
|
||||
* in order to catch dependency issues early.
|
||||
*/
|
||||
|
||||
#ifndef MBEDTLS_CHECK_CONFIG_H
|
||||
#define MBEDTLS_CHECK_CONFIG_H
|
||||
|
||||
/*
|
||||
* We assume CHAR_BIT is 8 in many places. In practice, this is true on our
|
||||
* target platforms, so not an issue, but let's just be extra sure.
|
||||
*/
|
||||
#include <limits.h>
|
||||
#if CHAR_BIT != 8
|
||||
#error "mbed TLS requires a platform with 8-bit chars"
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_C is required on Windows"
|
||||
#endif
|
||||
|
||||
/* Fix the config here. Not convenient to put an #ifdef _WIN32 in config.h as
|
||||
* it would confuse config.pl. */
|
||||
#if !defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && \
|
||||
!defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
|
||||
#define MBEDTLS_PLATFORM_SNPRINTF_ALT
|
||||
#endif
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#if defined(TARGET_LIKE_MBED) && \
|
||||
(defined(MBEDTLS_NET_C) || defined(MBEDTLS_TIMING_C))
|
||||
#error \
|
||||
"The NET and TIMING modules are not available for mbed OS - please use the network and timing functions provided by mbed OS"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_DEPRECATED_WARNING) && !defined(__GNUC__) && \
|
||||
!defined(__clang__)
|
||||
#error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_HAVE_TIME)
|
||||
#error "MBEDTLS_HAVE_TIME_DATE without MBEDTLS_HAVE_TIME does not make sense"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_AESNI_C) && !defined(MBEDTLS_HAVE_ASM)
|
||||
#error "MBEDTLS_AESNI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C)
|
||||
#error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_DHM_C) && !defined(MBEDTLS_BIGNUM_C)
|
||||
#error "MBEDTLS_DHM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CMAC_C) && !defined(MBEDTLS_AES_C) && \
|
||||
!defined(MBEDTLS_DES_C)
|
||||
#error "MBEDTLS_CMAC_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECDH_C) && !defined(MBEDTLS_ECP_C)
|
||||
#error "MBEDTLS_ECDH_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && \
|
||||
(!defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_ASN1_PARSE_C) || \
|
||||
!defined(MBEDTLS_ASN1_WRITE_C))
|
||||
#error "MBEDTLS_ECDSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECJPAKE_C) && \
|
||||
(!defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_MD_C))
|
||||
#error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) && !defined(MBEDTLS_HMAC_DRBG_C)
|
||||
#error "MBEDTLS_ECDSA_DETERMINISTIC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_C) && (!defined(MBEDTLS_BIGNUM_C) || \
|
||||
(!defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) && \
|
||||
!defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)))
|
||||
#error "MBEDTLS_ECP_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_C) && \
|
||||
(!defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA256_C))
|
||||
#error "MBEDTLS_ENTROPY_C defined, but not all prerequisites"
|
||||
#endif
|
||||
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_SHA512_C) && \
|
||||
defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 64)
|
||||
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(MBEDTLS_ENTROPY_C) && \
|
||||
(!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_ENTROPY_FORCE_SHA256)) && \
|
||||
defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) && (MBEDTLS_CTR_DRBG_ENTROPY_LEN > 32)
|
||||
#error "MBEDTLS_CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_ENTROPY_FORCE_SHA256) && \
|
||||
!defined(MBEDTLS_SHA256_C)
|
||||
#error "MBEDTLS_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY) && \
|
||||
(!defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES))
|
||||
#error "MBEDTLS_TEST_NULL_ENTROPY defined, but not all prerequisites"
|
||||
#endif
|
||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY) && \
|
||||
(defined(MBEDTLS_ENTROPY_NV_SEED) || \
|
||||
defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || defined(MBEDTLS_HAVEGE_C))
|
||||
#error "MBEDTLS_TEST_NULL_ENTROPY defined, but entropy sources too"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_GCM_C) && \
|
||||
(!defined(MBEDTLS_AES_C) && !defined(MBEDTLS_CAMELLIA_C))
|
||||
#error "MBEDTLS_GCM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_RANDOMIZE_JAC_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_ADD_MIXED_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_ADD_MIXED_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_DOUBLE_JAC_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) && \
|
||||
!defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_NORMALIZE_JAC_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) && \
|
||||
!defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_RANDOMIZE_MXZ_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) && !defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
#error "MBEDTLS_ECP_NORMALIZE_MXZ_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVEGE_C) && !defined(MBEDTLS_TIMING_C)
|
||||
#error "MBEDTLS_HAVEGE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HMAC_DRBG_C) && !defined(MBEDTLS_MD_C)
|
||||
#error "MBEDTLS_HMAC_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C))
|
||||
#error \
|
||||
"MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_X509_CRT_PARSE_C))
|
||||
#error \
|
||||
"MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(MBEDTLS_DHM_C)
|
||||
#error "MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && !defined(MBEDTLS_ECDH_C)
|
||||
#error \
|
||||
"MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_DHM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15))
|
||||
#error "MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_PKCS1_V15))
|
||||
#error \
|
||||
"MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDSA_C) || \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C))
|
||||
#error \
|
||||
"MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
|
||||
(!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
|
||||
!defined(MBEDTLS_PKCS1_V15))
|
||||
#error "MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
|
||||
(!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
|
||||
!defined(MBEDTLS_PKCS1_V15))
|
||||
#error "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
(!defined(MBEDTLS_ECJPAKE_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED))
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY))
|
||||
#error "MBEDTLS_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PADLOCK_C) && !defined(MBEDTLS_HAVE_ASM)
|
||||
#error "MBEDTLS_PADLOCK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PEM_PARSE_C) && !defined(MBEDTLS_BASE64_C)
|
||||
#error "MBEDTLS_PEM_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PEM_WRITE_C) && !defined(MBEDTLS_BASE64_C)
|
||||
#error "MBEDTLS_PEM_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PK_C) && \
|
||||
(!defined(MBEDTLS_RSA_C) && !defined(MBEDTLS_ECP_C))
|
||||
#error "MBEDTLS_PK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PK_PARSE_C) && !defined(MBEDTLS_PK_C)
|
||||
#error "MBEDTLS_PK_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && !defined(MBEDTLS_PK_C)
|
||||
#error "MBEDTLS_PK_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PKCS11_C) && !defined(MBEDTLS_PK_C)
|
||||
#error "MBEDTLS_PKCS11_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_EXIT_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_EXIT_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_EXIT) || defined(MBEDTLS_PLATFORM_EXIT_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_EXIT_MACRO and MBEDTLS_PLATFORM_STD_EXIT/MBEDTLS_PLATFORM_EXIT_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_TIME_ALT) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_HAVE_TIME))
|
||||
#error "MBEDTLS_PLATFORM_TIME_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_TIME_MACRO) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_HAVE_TIME))
|
||||
#error "MBEDTLS_PLATFORM_TIME_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_HAVE_TIME))
|
||||
#error "MBEDTLS_PLATFORM_TIME_TYPE_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_TIME_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_TIME) || defined(MBEDTLS_PLATFORM_TIME_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_TIME_MACRO and MBEDTLS_PLATFORM_STD_TIME/MBEDTLS_PLATFORM_TIME_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_TIME) || defined(MBEDTLS_PLATFORM_TIME_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_TIME_TYPE_MACRO and MBEDTLS_PLATFORM_STD_TIME/MBEDTLS_PLATFORM_TIME_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_FPRINTF) || \
|
||||
defined(MBEDTLS_PLATFORM_FPRINTF_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_FPRINTF_MACRO and MBEDTLS_PLATFORM_STD_FPRINTF/MBEDTLS_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY))
|
||||
#error "MBEDTLS_PLATFORM_FREE_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && defined(MBEDTLS_PLATFORM_STD_FREE)
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_STD_FREE cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
|
||||
!defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_CALLOC_MACRO must be defined if MBEDTLS_PLATFORM_FREE_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY))
|
||||
#error "MBEDTLS_PLATFORM_CALLOC_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
|
||||
defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
|
||||
!defined(MBEDTLS_PLATFORM_FREE_MACRO)
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_FREE_MACRO must be defined if MBEDTLS_PLATFORM_CALLOC_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_MEMORY) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_MEMORY defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_PRINTF) || \
|
||||
defined(MBEDTLS_PLATFORM_PRINTF_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_PRINTF_MACRO and MBEDTLS_PLATFORM_STD_PRINTF/MBEDTLS_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) && !defined(MBEDTLS_PLATFORM_C)
|
||||
#error "MBEDTLS_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_SNPRINTF) || \
|
||||
defined(MBEDTLS_PLATFORM_SNPRINTF_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_SNPRINTF_MACRO and MBEDTLS_PLATFORM_STD_SNPRINTF/MBEDTLS_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) && \
|
||||
!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
|
||||
#error "MBEDTLS_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
||||
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_CALLOC) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
||||
#error "MBEDTLS_PLATFORM_STD_CALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_FREE) && !defined(MBEDTLS_PLATFORM_MEMORY)
|
||||
#error "MBEDTLS_PLATFORM_STD_FREE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_EXIT) && !defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_EXIT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_TIME) && \
|
||||
(!defined(MBEDTLS_PLATFORM_TIME_ALT) || !defined(MBEDTLS_HAVE_TIME))
|
||||
#error "MBEDTLS_PLATFORM_STD_TIME defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_FPRINTF) && \
|
||||
!defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_PRINTF) && \
|
||||
!defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_PRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_SNPRINTF) && \
|
||||
!defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED) && \
|
||||
(!defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_ENTROPY_C))
|
||||
#error "MBEDTLS_ENTROPY_NV_SEED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) && !defined(MBEDTLS_ENTROPY_NV_SEED)
|
||||
#error "MBEDTLS_PLATFORM_NV_SEED_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) && \
|
||||
!defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_NV_SEED_READ defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) && \
|
||||
!defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
||||
#error "MBEDTLS_PLATFORM_STD_NV_SEED_WRITE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) || \
|
||||
defined(MBEDTLS_PLATFORM_NV_SEED_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_NV_SEED_READ_MACRO and MBEDTLS_PLATFORM_STD_NV_SEED_READ cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO) && \
|
||||
(defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) || \
|
||||
defined(MBEDTLS_PLATFORM_NV_SEED_ALT))
|
||||
#error \
|
||||
"MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO and MBEDTLS_PLATFORM_STD_NV_SEED_WRITE cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RSA_C) && \
|
||||
((!defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_PK_ALT)) || \
|
||||
(!defined(MBEDTLS_OID_C) && !defined(MBEDTLS_IOT_SPECIFIC)))
|
||||
#error "MBEDTLS_RSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RSA_C) && \
|
||||
(!defined(MBEDTLS_PKCS1_V21) && !defined(MBEDTLS_PKCS1_V15))
|
||||
#error "MBEDTLS_RSA_C defined, but none of the PKCS1 versions enabled"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
|
||||
(!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_PKCS1_V21))
|
||||
#error "MBEDTLS_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_SSL3) && \
|
||||
(!defined(MBEDTLS_MD5_C) || !defined(MBEDTLS_SHA1_C))
|
||||
#error "MBEDTLS_SSL_PROTO_SSL3 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1) && \
|
||||
(!defined(MBEDTLS_MD5_C) || !defined(MBEDTLS_SHA1_C))
|
||||
#error "MBEDTLS_SSL_PROTO_TLS1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
||||
(!defined(MBEDTLS_MD5_C) || !defined(MBEDTLS_SHA1_C))
|
||||
#error "MBEDTLS_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
|
||||
(!defined(MBEDTLS_SHA1_C) && !defined(MBEDTLS_SHA256_C) && \
|
||||
!defined(MBEDTLS_SHA512_C))
|
||||
#error "MBEDTLS_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_DTLS) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
#error "MBEDTLS_SSL_PROTO_DTLS defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_TLS_C)
|
||||
#error "MBEDTLS_SSL_CLI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
(!defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C))
|
||||
#error "MBEDTLS_SSL_TLS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_TLS_C)
|
||||
#error "MBEDTLS_SSL_SRV_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
(!defined(MBEDTLS_SSL_PROTO_SSL3) && !defined(MBEDTLS_SSL_PROTO_TLS1) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1) && !defined(MBEDTLS_SSL_PROTO_TLS1_2))
|
||||
#error "MBEDTLS_SSL_TLS_C defined, but no protocols are active"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
(defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
(defined(MBEDTLS_SSL_PROTO_TLS1) && defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
(defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
|
||||
(!defined(MBEDTLS_SSL_PROTO_TLS1) || !defined(MBEDTLS_SSL_PROTO_TLS1_1)))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && !defined(MBEDTLS_SSL_PROTO_DTLS)
|
||||
#error "MBEDTLS_SSL_DTLS_HELLO_VERIFY defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \
|
||||
!defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
|
||||
#error "MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
|
||||
(!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS))
|
||||
#error "MBEDTLS_SSL_DTLS_ANTI_REPLAY defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
|
||||
(!defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_PROTO_DTLS))
|
||||
#error "MBEDTLS_SSL_DTLS_BADMAC_LIMIT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
#error "MBEDTLS_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1) && !defined(MBEDTLS_SSL_PROTO_TLS1_1) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
#error "MBEDTLS_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TICKET_C) && !defined(MBEDTLS_CIPHER_C)
|
||||
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) && \
|
||||
!defined(MBEDTLS_SSL_PROTO_SSL3) && !defined(MBEDTLS_SSL_PROTO_TLS1)
|
||||
#error "MBEDTLS_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
|
||||
!defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
#error "MBEDTLS_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_THREADING_PTHREAD)
|
||||
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
|
||||
#error "MBEDTLS_THREADING_PTHREAD defined, but not all prerequisites"
|
||||
#endif
|
||||
#define MBEDTLS_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_THREADING_ALT)
|
||||
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
|
||||
#error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
#define MBEDTLS_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
|
||||
#error "MBEDTLS_THREADING_C defined, single threading implementation required"
|
||||
#endif
|
||||
#undef MBEDTLS_THREADING_IMPL
|
||||
|
||||
#if defined(MBEDTLS_VERSION_FEATURES) && !defined(MBEDTLS_VERSION_C)
|
||||
#error "MBEDTLS_VERSION_FEATURES defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_USE_C) && \
|
||||
((!defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_PK_ALT)) || \
|
||||
(!defined(MBEDTLS_OID_C) && !defined(MBEDTLS_IOT_SPECIFIC)) || \
|
||||
!defined(MBEDTLS_ASN1_PARSE_C) || !defined(MBEDTLS_PK_PARSE_C))
|
||||
#error "MBEDTLS_X509_USE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CREATE_C) && \
|
||||
(!defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_OID_C) || \
|
||||
!defined(MBEDTLS_ASN1_WRITE_C) || !defined(MBEDTLS_PK_WRITE_C))
|
||||
#error "MBEDTLS_X509_CREATE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && (!defined(MBEDTLS_X509_USE_C))
|
||||
#error "MBEDTLS_X509_CRT_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRL_PARSE_C) && (!defined(MBEDTLS_X509_USE_C))
|
||||
#error "MBEDTLS_X509_CRL_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CSR_PARSE_C) && (!defined(MBEDTLS_X509_USE_C))
|
||||
#error "MBEDTLS_X509_CSR_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_WRITE_C) && (!defined(MBEDTLS_X509_CREATE_C))
|
||||
#error "MBEDTLS_X509_CRT_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CSR_WRITE_C) && (!defined(MBEDTLS_X509_CREATE_C))
|
||||
#error "MBEDTLS_X509_CSR_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Avoid warning from -pedantic. This is a convenient place for this
|
||||
* workaround since this is included by every single file before the
|
||||
* #if defined(MBEDTLS_xxx_C) that results in emtpy translation units.
|
||||
*/
|
||||
typedef int mbedtls_iso_c_forbids_empty_translation_units;
|
||||
|
||||
#endif /* MBEDTLS_CHECK_CONFIG_H */
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* \file config.h
|
||||
*
|
||||
* \brief Configuration options (set of defines)
|
||||
*
|
||||
* This set of compile-time options may be used to enable
|
||||
* or disable features selectively, and reduce the global
|
||||
* memory footprint.
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#ifndef MBEDTLS_CONFIG_H
|
||||
#define MBEDTLS_CONFIG_H
|
||||
|
||||
/* crypto */
|
||||
#define MBEDTLS_OID_C
|
||||
|
||||
#define MBEDTLS_CIPHER_MODE_CTR
|
||||
|
||||
#define MBEDTLS_GENPRIME
|
||||
#define MBEDTLS_PKCS1_V21
|
||||
#define MBEDTLS_RSA_NO_CRT
|
||||
|
||||
//#define MBEDTLS_SHA512_C
|
||||
|
||||
/* System support */
|
||||
#define MBEDTLS_HAVE_ASM
|
||||
|
||||
/* mbed TLS feature support */
|
||||
#define MBEDTLS_CIPHER_MODE_CBC
|
||||
#define MBEDTLS_PKCS1_V15
|
||||
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
|
||||
#define MBEDTLS_SSL_PROTO_TLS1_2
|
||||
#define MBEDTLS_THREADING_C
|
||||
#define MBEDTLS_THREADING_ALT
|
||||
//#define MBEDTLS_THREADING_PTHREAD
|
||||
|
||||
/* mbed TLS modules */
|
||||
#define MBEDTLS_AES_C
|
||||
#define MBEDTLS_ASN1_PARSE_C
|
||||
#define MBEDTLS_BIGNUM_C
|
||||
#define MBEDTLS_CIPHER_C
|
||||
#define MBEDTLS_CIPHER_MODE_CFB
|
||||
#define MBEDTLS_MD_C
|
||||
#define MBEDTLS_MD5_C
|
||||
#define MBEDTLS_NET_C
|
||||
#define MBEDTLS_PK_C
|
||||
#define MBEDTLS_PK_PARSE_C
|
||||
#define MBEDTLS_RSA_C
|
||||
#define MBEDTLS_SHA1_C
|
||||
#define MBEDTLS_SHA256_C
|
||||
#define MBEDTLS_SSL_CLI_C
|
||||
#define MBEDTLS_SSL_TLS_C
|
||||
#define MBEDTLS_X509_CRT_PARSE_C
|
||||
#define MBEDTLS_X509_USE_C
|
||||
#define MBEDTLS_BASE64_C
|
||||
#define MBEDTLS_PEM_PARSE_C
|
||||
|
||||
/* mbed TLS debug */
|
||||
//#define MBEDTLS_DEBUG_C
|
||||
|
||||
/* OEM configure */
|
||||
#define MBEDTLS_IOT_SPECIFIC
|
||||
//#define MBEDTLS_PK_ALT
|
||||
//#define MBEDTLS_AES_ALT
|
||||
|
||||
#include "check_config.h"
|
||||
|
||||
#endif /* MBEDTLS_CONFIG_H */
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
* \file padlock.h
|
||||
*
|
||||
* \brief VIA PadLock ACE for HW encryption/decryption supported by some
|
||||
* processors
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
#ifndef MBEDTLS_PADLOCK_H
|
||||
#define MBEDTLS_PADLOCK_H
|
||||
|
||||
#include "aes.h"
|
||||
|
||||
#define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED \
|
||||
-0x0030 /**< Input data should be aligned. */
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(address_sanitizer)
|
||||
#define MBEDTLS_HAVE_ASAN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Some versions of ASan result in errors about not enough registers */
|
||||
#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \
|
||||
!defined(MBEDTLS_HAVE_ASAN)
|
||||
|
||||
#ifndef MBEDTLS_HAVE_X86
|
||||
#define MBEDTLS_HAVE_X86
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MBEDTLS_PADLOCK_RNG 0x000C
|
||||
#define MBEDTLS_PADLOCK_ACE 0x00C0
|
||||
#define MBEDTLS_PADLOCK_PHE 0x0C00
|
||||
#define MBEDTLS_PADLOCK_PMM 0x3000
|
||||
|
||||
#define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *)(16 + ((int32_t)x & ~15))
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief PadLock detection routine
|
||||
*
|
||||
* \param feature The feature to detect
|
||||
*
|
||||
* \return 1 if CPU has support for the feature, 0 otherwise
|
||||
*/
|
||||
int mbedtls_padlock_has_support(int feature);
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-ECB block en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx, int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16]);
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-CBC buffer en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx, int mode,
|
||||
size_t length, unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_X86 */
|
||||
|
||||
#endif /* padlock.h */
|
||||
116
Living_SDK/framework/bluetooth/breeze/hal/ble/mbed_crypto.h
Normal file
116
Living_SDK/framework/bluetooth/breeze/hal/ble/mbed_crypto.h
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#ifndef _MBED_CRYPTO_H_
|
||||
#define _MBED_CRYPTO_H_
|
||||
|
||||
#include "ali_crypto.h"
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "aes.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/sha1.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/sha256.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/sha512.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/md.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/hash.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/md5.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/rsa.h"
|
||||
//#include "../../../mbedtls/include/mbedtls/hmac.h"
|
||||
|
||||
#if CONFIG_DBG_CRYPT
|
||||
#define MBED_DBG_E(_f, ...) \
|
||||
printf("E %s %d: "_f, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||
#define MBED_DBG_I(_f, ...) \
|
||||
printf("I %s %d: "_f, __FUNCTION__, __LINE__, ##__VA_ARGS__)
|
||||
#else
|
||||
#define MBED_DBG_E(_f, _a...)
|
||||
#define MBED_DBG_I(_f, _a...)
|
||||
#endif
|
||||
|
||||
#define PRINT_RET(_ret, _f, ...) \
|
||||
do { \
|
||||
MBED_DBG_E(_f, ##__VA_ARGS__); \
|
||||
return (ali_crypto_result)_ret; \
|
||||
} while (0);
|
||||
|
||||
#define GO_RET(_ret, _f, ...) \
|
||||
do { \
|
||||
MBED_DBG_E(_f, ##__VA_ARGS__); \
|
||||
result = (ali_crypto_result)_ret; \
|
||||
goto _OUT; \
|
||||
} while (0);
|
||||
|
||||
#define INIT_CTX_MAGIC(m) (m = 0x12345678)
|
||||
#define IS_VALID_CTX_MAGIC(m) (0x12345678 == m)
|
||||
#define CLEAN_CTX_MAGIC(m) (m = 0x0)
|
||||
|
||||
|
||||
#define OSA_malloc(_size) malloc(_size)
|
||||
#define OSA_free(_ptr) free(_ptr)
|
||||
#define OSA_memcpy(_dst, _src, _size) memcpy(_dst, _src, _size)
|
||||
#define OSA_memset(_src, _val, _size) memset(_src, _val, _size)
|
||||
#define OSA_memcmp(_dst, _src, _size) memcmp(_dst, _src, _size)
|
||||
#define OSA_strlen(_str) strlen(_str)
|
||||
|
||||
enum
|
||||
{
|
||||
PK_PUBLIC = 0,
|
||||
PK_PRIVATE = 1
|
||||
};
|
||||
|
||||
typedef struct _cts_ctx_t
|
||||
{
|
||||
uint32_t is_ecb;
|
||||
} cts_ctx_t;
|
||||
|
||||
typedef struct _xts_ctx_t
|
||||
{
|
||||
uint8_t tweak[16];
|
||||
} xts_ctx_t;
|
||||
|
||||
typedef struct _aes_ctx_t
|
||||
{
|
||||
uint32_t magic;
|
||||
uint32_t status;
|
||||
aes_type_t type;
|
||||
uint32_t is_enc;
|
||||
uint8_t iv[AES_IV_SIZE];
|
||||
size_t offset;
|
||||
uint8_t stream_block[AES_BLOCK_SIZE];
|
||||
union
|
||||
{
|
||||
uint8_t sym_ctx[1];
|
||||
mbedtls_aes_context ctx;
|
||||
};
|
||||
} aes_ctx_t;
|
||||
|
||||
|
||||
typedef struct _des_ctx_t
|
||||
{
|
||||
uint32_t magic;
|
||||
uint32_t status;
|
||||
des_type_t type;
|
||||
uint32_t is_enc;
|
||||
union
|
||||
{
|
||||
uint8_t sym_ctx[1];
|
||||
};
|
||||
} des_ctx_t;
|
||||
|
||||
typedef struct _ae_ctx_t
|
||||
{
|
||||
uint32_t magic;
|
||||
uint32_t status;
|
||||
authenc_type_t type;
|
||||
uint32_t is_enc;
|
||||
uint32_t tag_len;
|
||||
} ae_ctx_t;
|
||||
|
||||
ali_crypto_result mbed_crypto_init(void);
|
||||
void mbed_crypto_cleanup(void);
|
||||
|
||||
#endif /* _MBED_CRYPTO_H_ */
|
||||
|
|
@ -0,0 +1,334 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _AIS_BLE_PORT_H_
|
||||
#define _AIS_BLE_PORT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* AIS - Alibaba IoT Service*/
|
||||
|
||||
/* UUIDs */
|
||||
#define BLE_UUID_AIS_SERVICE 0xFEB3 /* The UUID of the Alibaba IOT Service. */
|
||||
#define BLE_UUID_AIS_RC \
|
||||
0xFED4 /* The UUID of the "Read Characteristics" Characteristic. */
|
||||
#define BLE_UUID_AIS_WC \
|
||||
0xFED5 /* The UUID of the "Write Characteristics" Characteristic. */
|
||||
#define BLE_UUID_AIS_IC \
|
||||
0xFED6 /* The UUID of the "Indicate Characteristics" Characteristic. */
|
||||
#define BLE_UUID_AIS_WWNRC \
|
||||
0xFED7 /* The UUID of the "Write WithNoRsp Characteristics" \
|
||||
Characteristic. */
|
||||
#define BLE_UUID_AIS_NC \
|
||||
0xFED8 /* The UUID of the "Notify Characteristics" Characteristic. */
|
||||
|
||||
#define AIS_BT_MAC_LEN 6
|
||||
|
||||
#define ABIT(n) (1 << n)
|
||||
|
||||
/* Characteristic Properties Bit field values */
|
||||
typedef enum
|
||||
{
|
||||
/** @def AIS_GATT_CHRC_BROADCAST
|
||||
* @brief Characteristic broadcast property.
|
||||
*
|
||||
* If set, permits broadcasts of the Characteristic Value using Server
|
||||
* Characteristic Configuration Descriptor.
|
||||
*/
|
||||
AIS_GATT_CHRC_BROADCAST = ABIT(0),
|
||||
/** @def AIS_GATT_CHRC_READ
|
||||
* @brief Characteristic read property.
|
||||
*
|
||||
* If set, permits reads of the Characteristic Value.
|
||||
*/
|
||||
AIS_GATT_CHRC_READ = ABIT(1),
|
||||
/** @def AIS_GATT_CHRC_WRITE_WITHOUT_RESP
|
||||
* @brief Characteristic write without response property.
|
||||
*
|
||||
* If set, permits write of the Characteristic Value without response.
|
||||
*/
|
||||
AIS_GATT_CHRC_WRITE_WITHOUT_RESP = ABIT(2),
|
||||
/** @def AIS_GATT_CHRC_WRITE
|
||||
* @brief Characteristic write with response property.
|
||||
*
|
||||
* If set, permits write of the Characteristic Value with response.
|
||||
*/
|
||||
AIS_GATT_CHRC_WRITE = ABIT(3),
|
||||
/** @def AIS_GATT_CHRC_NOTIFY
|
||||
* @brief Characteristic notify property.
|
||||
*
|
||||
* If set, permits notifications of a Characteristic Value without
|
||||
* acknowledgment.
|
||||
*/
|
||||
AIS_GATT_CHRC_NOTIFY = ABIT(4),
|
||||
/** @def AIS_GATT_CHRC_INDICATE
|
||||
* @brief Characteristic indicate property.
|
||||
*
|
||||
* If set, permits indications of a Characteristic Value with
|
||||
* acknowledgment.
|
||||
*/
|
||||
AIS_GATT_CHRC_INDICATE = ABIT(5),
|
||||
/** @def AIS_GATT_CHRC_AUTH
|
||||
* @brief Characteristic Authenticated Signed Writes property.
|
||||
*
|
||||
* If set, permits signed writes to the Characteristic Value.
|
||||
*/
|
||||
AIS_GATT_CHRC_AUTH = ABIT(6),
|
||||
/** @def AIS_GATT_CHRC_EXT_PROP
|
||||
* @brief Characteristic Extended Properties property.
|
||||
*
|
||||
* If set, additional characteristic properties are defined in the
|
||||
* Characteristic Extended Properties Descriptor.
|
||||
*/
|
||||
AIS_GATT_CHRC_EXT_PROP = ABIT(7)
|
||||
} ais_char_prop_t;
|
||||
|
||||
/* GATT attribute permission bit field values */
|
||||
typedef enum
|
||||
{
|
||||
/** No operations supported, e.g. for notify-only */
|
||||
AIS_GATT_PERM_NONE = 0,
|
||||
/** Attribute read permission. */
|
||||
AIS_GATT_PERM_READ = ABIT(0),
|
||||
/** Attribute write permission. */
|
||||
AIS_GATT_PERM_WRITE = ABIT(1),
|
||||
/* Attribute read permission with encryption. */
|
||||
AIS_GATT_PERM_READ_ENCRYPT = ABIT(2),
|
||||
/* Attribute write permission with encryption. */
|
||||
AIS_GATT_PERM_WRITE_ENCRYPT = ABIT(3),
|
||||
/* Attribute read permission with authentication. */
|
||||
AIS_GATT_PERM_READ_AUTHEN = ABIT(4),
|
||||
/* Attribute write permission with authentication. */
|
||||
AIS_GATT_PERM_WRITE_AUTHEN = ABIT(5),
|
||||
/* Attribute prepare write permission. */
|
||||
AIS_GATT_PERM_PREPARE_WRITE = ABIT(6)
|
||||
} ais_attr_perm_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
AIS_ERR_SUCCESS = 0,
|
||||
AIS_ERR_STACK_FAIL,
|
||||
AIS_ERR_MEM_FAIL,
|
||||
AIS_ERR_INVALID_ADV_DATA,
|
||||
AIS_ERR_ADV_FAIL,
|
||||
AIS_ERR_STOP_ADV_FAIL,
|
||||
AIS_ERR_GATT_INDICATE_FAIL,
|
||||
AIS_ERR_GATT_NOTIFY_FAIL,
|
||||
/* Add more AIS error code hereafter */
|
||||
} ais_err_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
AIS_UUID_TYPE_16,
|
||||
AIS_UUID_TYPE_32,
|
||||
AIS_UUID_TYPE_128,
|
||||
} ais_uuid_type_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t type;
|
||||
} ais_uuid_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ais_uuid_t uuid;
|
||||
uint16_t val;
|
||||
} ais_uuid_16_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ais_uuid_t uuid;
|
||||
uint32_t val;
|
||||
} ais_uuid_32_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ais_uuid_t uuid;
|
||||
uint8_t val[16];
|
||||
} ais_uuid_128_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
AIS_CCC_VALUE_NONE = 0,
|
||||
AIS_CCC_VALUE_NOTIFY = 1,
|
||||
AIS_CCC_VALUE_INDICATE = 2
|
||||
} ais_ccc_value_t;
|
||||
|
||||
#define AIS_UUID_INIT_16(value) \
|
||||
{ \
|
||||
.uuid.type = AIS_UUID_TYPE_16, .val = (value), \
|
||||
}
|
||||
|
||||
#define AIS_UUID_INIT_32(value) \
|
||||
{ \
|
||||
.uuid.type = AIS_UUID_TYPE_32, .val = (value), \
|
||||
}
|
||||
|
||||
#define AIS_UUID_INIT_128(value...) \
|
||||
{ \
|
||||
.uuid.type = AIS_UUID_TYPE_128, .val = { value }, \
|
||||
}
|
||||
|
||||
#define AIS_UUID_DECLARE_16(value) \
|
||||
((ais_uuid_t *)(&(ais_uuid_16_t)AIS_UUID_INIT_16(value)))
|
||||
#define AIS_UUID_DECLARE_32(value) \
|
||||
((ais_uuid_t *)(&(bt_uuid_32_t)AIS_UUID_INIT_32(value)))
|
||||
#define AIS_UUID_DECLARE_128(value...) \
|
||||
((ais_uuid_t *)(&(bt_uuid_128_t)AIS_UUID_INIT_128(value)))
|
||||
|
||||
typedef void (*connected_callback_t)();
|
||||
typedef void (*disconnected_callback_t)();
|
||||
typedef size_t (*on_char_read_t)(void *buf, uint16_t len);
|
||||
typedef size_t (*on_char_write_t)(void *buf, uint16_t len);
|
||||
typedef void (*on_char_ccc_change_t)(ais_ccc_value_t value);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/* Characteristics UUID */
|
||||
ais_uuid_t *uuid;
|
||||
/* Characteristics property */
|
||||
uint8_t prop;
|
||||
/* Characteristics value attribute permission */
|
||||
uint8_t perm;
|
||||
/* Characteristics value read handler, NULL if not used */
|
||||
on_char_read_t on_read;
|
||||
/* Characteristics value write handler, NULL if not used */
|
||||
on_char_write_t on_write;
|
||||
/**
|
||||
* Characteristics value ccc changed handler.
|
||||
* Only applied to NOFITY and INDICATE type Characteristics,
|
||||
* NULL if not applied.
|
||||
*/
|
||||
on_char_ccc_change_t on_ccc_change;
|
||||
} ais_char_init_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/* AIS primamry service */
|
||||
ais_uuid_t *uuid_svc;
|
||||
/* AIS's Read Characteristics */
|
||||
ais_char_init_t rc;
|
||||
/* AIS's Write Characteristics */
|
||||
ais_char_init_t wc;
|
||||
/* AIS's Indicate Characteristics */
|
||||
ais_char_init_t ic;
|
||||
/* AIS's Write WithNoRsp Characteristics */
|
||||
ais_char_init_t wwnrc;
|
||||
/* AIS's Notify Characteristics */
|
||||
ais_char_init_t nc;
|
||||
/* Callback function when bluetooth connected */
|
||||
connected_callback_t on_connected;
|
||||
/* Callback function when bluetooth disconnected */
|
||||
disconnected_callback_t on_disconnected;
|
||||
} ais_bt_init_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
AIS_ADV_NAME_SHORT,
|
||||
AIS_ADV_NAME_FULL
|
||||
} ais_adv_name_type_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ais_adv_name_type_t ntype;
|
||||
char * name;
|
||||
} ais_adv_name_t;
|
||||
|
||||
/* DISCOVERABILITY MODES, spec v4.2, in 4.1 section */
|
||||
typedef enum
|
||||
{
|
||||
AIS_AD_LIMITED = ABIT(0), /* Limited Discoverable */
|
||||
AIS_AD_GENERAL = ABIT(1), /* General Discoverable */
|
||||
AIS_AD_NO_BREDR = ABIT(2) /* BR/EDR not supported */
|
||||
} ais_adv_flag_t;
|
||||
|
||||
#define MAX_VENDOR_DATA_LEN 24
|
||||
typedef struct
|
||||
{
|
||||
uint8_t data[MAX_VENDOR_DATA_LEN];
|
||||
uint16_t len;
|
||||
} ais_adv_vendor_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ais_adv_flag_t flag;
|
||||
ais_adv_name_t name;
|
||||
ais_adv_vendor_data_t vdata;
|
||||
/* Subject to add more hereafter in the future */
|
||||
} ais_adv_init_t;
|
||||
|
||||
enum
|
||||
{
|
||||
AIS_BT_REASON_REMOTE_USER_TERM_CONN = 0,
|
||||
/* Add more supported reasons here. */
|
||||
AIS_BT_REASON_UNSPECIFIED = 0x0f
|
||||
};
|
||||
|
||||
/**
|
||||
* API to initialize ble stack.
|
||||
* @parma[in] ais_init Bluetooth stack init parmaters.
|
||||
* @return 0 on success, error code if failure.
|
||||
*/
|
||||
ais_err_t ble_stack_init(ais_bt_init_t *ais_init);
|
||||
|
||||
/**
|
||||
* API to de-initialize ble stack.
|
||||
* @return 0 on success, error code if failure.
|
||||
*/
|
||||
ais_err_t ble_stack_deinit();
|
||||
|
||||
/**
|
||||
* API to send data via AIS's Notify Characteristics.
|
||||
* @parma[in] p_data data buffer.
|
||||
* @parma[in] length data length.
|
||||
* @return 0 on success, error code if failure.
|
||||
*/
|
||||
ais_err_t ble_send_notification(uint8_t *p_data, uint16_t length);
|
||||
|
||||
/**
|
||||
* API to send data via AIS's Indicate Characteristics.
|
||||
* @parma[in] p_data data buffer.
|
||||
* @parma[in] length data length.
|
||||
* @parma[in] txdone txdone callback.
|
||||
* @return 0 on success, erro code if failure.
|
||||
*/
|
||||
ais_err_t ble_send_indication(uint8_t *p_data, uint16_t length, void (*txdone)(uint8_t res));
|
||||
|
||||
/**
|
||||
* API to disconnect BLE connection.
|
||||
* @param[in] reason the reason to disconnect the connection.
|
||||
*/
|
||||
void ble_disconnect(uint8_t reason);
|
||||
|
||||
/**
|
||||
* API to start bluetooth advertising.
|
||||
* @return 0 on success, erro code if failure.
|
||||
*/
|
||||
ais_err_t ble_advertising_start(ais_adv_init_t *adv);
|
||||
|
||||
/**
|
||||
* API to stop bluetooth advertising.
|
||||
* @return 0 on success, erro code if failure.
|
||||
*/
|
||||
ais_err_t ble_advertising_stop();
|
||||
|
||||
/**
|
||||
* API to start bluetooth advertising.
|
||||
* @parma[out] mac the uint8_t[BD_ADDR_LEN] space the save the mac address.
|
||||
* @return 0 on success, erro code if failure.
|
||||
*/
|
||||
ais_err_t ble_get_mac(uint8_t *mac);
|
||||
|
||||
#ifdef EN_LONG_MTU
|
||||
/**
|
||||
* API to obtain ble link MTU, if ble stack didn't provide, use DEFAULT_ATT_MTU .
|
||||
* @parma[out] mac the uint8_t[BD_ADDR_LEN] space the save the mac address.
|
||||
* @return 0 on success, erro code if failure.
|
||||
*/
|
||||
|
||||
int ble_get_att_mtu(uint16_t *att_mtu);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _AIS_OS_H_
|
||||
#define _AIS_OS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
void *hdl;
|
||||
} os_hdl_t;
|
||||
|
||||
typedef os_hdl_t os_timer_t;
|
||||
|
||||
/* Timer callback */
|
||||
typedef void (*os_timer_cb_t)(void *, void *);
|
||||
|
||||
/**
|
||||
* This function will create a timer.
|
||||
*
|
||||
* @param[in] timer pointer to the timer.
|
||||
* @param[in] fn callbak of the timer.
|
||||
* @param[in] arg the argument of the callback.
|
||||
* @param[in] ms ms of the normal timer triger.
|
||||
* @param[in] repeat repeat or not when the timer is created.
|
||||
* @param[in] auto_run run auto or not when the timer is created.
|
||||
*
|
||||
* @return 0: success.
|
||||
*/
|
||||
int os_timer_new(os_timer_t *timer, os_timer_cb_t cb, void *arg, int ms);
|
||||
|
||||
/**
|
||||
* This function will start a timer.
|
||||
*
|
||||
* @param[in] timer pointer to the timer.
|
||||
*
|
||||
* @return 0: success.
|
||||
*/
|
||||
int os_timer_start(os_timer_t *timer);
|
||||
|
||||
/**
|
||||
* This function will stop a timer.
|
||||
*
|
||||
* @param[in] timer pointer to the timer.
|
||||
*
|
||||
* @return 0: success.
|
||||
*/
|
||||
int os_timer_stop(os_timer_t *timer);
|
||||
|
||||
/**
|
||||
* This function will delete a timer.
|
||||
*
|
||||
* @param[in] timer pointer to a timer.
|
||||
*/
|
||||
void os_timer_free(os_timer_t *timer);
|
||||
|
||||
/**
|
||||
* Reboot system.
|
||||
*/
|
||||
void os_reboot();
|
||||
|
||||
/**
|
||||
* Msleep.
|
||||
*
|
||||
* @param[in] ms sleep time in milliseconds.
|
||||
*/
|
||||
void os_msleep(int ms);
|
||||
|
||||
/**
|
||||
* Get current time in mini seconds.
|
||||
*
|
||||
* @return elapsed time in mini seconds from system starting.
|
||||
*/
|
||||
long long os_now_ms();
|
||||
|
||||
/**
|
||||
* Add a new KV pair.
|
||||
*
|
||||
* @param[in] key the key of the KV pair.
|
||||
* @param[in] value the value of the KV pair.
|
||||
* @param[in] len the length of the value.
|
||||
* @param[in] sync save the KV pair to flash right now (should always be 1).
|
||||
*
|
||||
* @return 0 on success, negative error on failure.
|
||||
*/
|
||||
int os_kv_set(const char *key, const void *value, int len, int sync);
|
||||
|
||||
/**
|
||||
* Get the KV pair's value stored in buffer by its key.
|
||||
*
|
||||
* @note: the buffer_len should be larger than the real length of the value,
|
||||
* otherwise buffer would be NULL.
|
||||
*
|
||||
* @param[in] key the key of the KV pair to get.
|
||||
* @param[out] buffer the memory to store the value.
|
||||
* @param[in-out] buffer_len in: the length of the input buffer.
|
||||
* out: the real length of the value.
|
||||
*
|
||||
* @return 0 on success, negative error on failure.
|
||||
*/
|
||||
int os_kv_get(const char *key, void *buffer, int *buffer_len);
|
||||
|
||||
/**
|
||||
* Delete the KV pair by its key.
|
||||
*
|
||||
* @param[in] key the key of the KV pair to delete.
|
||||
*
|
||||
* @return 0 on success, negative error on failure.
|
||||
*/
|
||||
int os_kv_del(const char *key);
|
||||
|
||||
/**
|
||||
* Generate random number.
|
||||
*
|
||||
* @return random value implemented by platform.
|
||||
*/
|
||||
int os_rand(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _AIS_SECURITY_H_
|
||||
#define _AIS_SECURITY_H_
|
||||
|
||||
/**
|
||||
* @brief Initialize the aes context, which includes key/iv info.
|
||||
* The aes context is implementation specific.
|
||||
*
|
||||
* @param[in] key:
|
||||
* @param[in] iv:
|
||||
* @param[in] dir: AIS_AES_ENCRYPTION or AIS_AES_DECRYPTION
|
||||
* @return p_ais_aes128_t
|
||||
@verbatim None
|
||||
@endverbatim
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
void *ais_aes128_init(const uint8_t *key, const uint8_t *iv);
|
||||
|
||||
/**
|
||||
* @brief Destroy the aes context.
|
||||
*
|
||||
* @param[in] aes: the aex context.
|
||||
* @return
|
||||
@verbatim
|
||||
= 0: succeeded
|
||||
= -1: failed
|
||||
@endverbatim
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
int ais_aes128_destroy(void *aes);
|
||||
|
||||
/**
|
||||
* @brief Do aes-128 cbc encryption.
|
||||
* No padding is required inside the implementation.
|
||||
*
|
||||
* @param[in] aes: AES handler
|
||||
* @param[in] src: plain data
|
||||
* @param[in] block_num: plain data number of 16 bytes size
|
||||
* @param[out] dst: cipher data
|
||||
* @return
|
||||
@verbatim
|
||||
= 0: succeeded
|
||||
= -1: failed
|
||||
@endverbatim
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
int ais_aes128_cbc_encrypt(void *aes, const void *src, size_t block_num,
|
||||
void *dst);
|
||||
|
||||
/**
|
||||
* @brief Do aes-128 cbc decryption.
|
||||
* No padding is required inside the implementation.
|
||||
*
|
||||
* @param[in] aes: AES handler
|
||||
* @param[in] src: cipher data
|
||||
* @param[in] block_num: plain data number of 16 bytes size
|
||||
* @param[out] dst: plain data
|
||||
* @return
|
||||
@verbatim
|
||||
= 0: succeeded
|
||||
= -1: failed
|
||||
@endverbatim
|
||||
* @see None.
|
||||
* @note None.
|
||||
*/
|
||||
int ais_aes128_cbc_decrypt(void *aes, const void *src, size_t block_num,
|
||||
void *dst);
|
||||
|
||||
#endif
|
||||
97
Living_SDK/framework/bluetooth/breeze/include/auth.h
Normal file
97
Living_SDK/framework/bluetooth/breeze/include/auth.h
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_AUTH_H
|
||||
#define BZ_AUTH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "common.h"
|
||||
#include "breeze_hal_os.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define RANDOM_SEQ_LEN 16
|
||||
#define MAX_OKM_LEN 16
|
||||
#define MAX_IKM_LEN (BZ_DEV_DEVICE_SECRET_LEN + BZ_BT_MAC_LEN + RANDOM_SEQ_LEN + 2)
|
||||
|
||||
enum {
|
||||
BZ_AUTH_STATE_IDLE, // Auth idle state
|
||||
BZ_AUTH_STATE_CONNECTED, // BLE link connected
|
||||
BZ_AUTH_STATE_SVC_ENABLED, // AIS service enabled
|
||||
BZ_AUTH_STATE_RAND_SENT, // Auth Random send out
|
||||
BZ_AUTH_STATE_REQ_RECVD, // Auth rx hisrv from peer
|
||||
BZ_AUTH_STATE_DONE, // Auth rx ok from peer
|
||||
BZ_AUTH_STATE_FAILED, // Auth failed
|
||||
};
|
||||
|
||||
enum {
|
||||
BZ_AUTH_TYPE_NONE,
|
||||
BZ_AUTH_TYPE_PER_PK,
|
||||
BZ_AUTH_TYPE_PER_DEV,
|
||||
};
|
||||
|
||||
typedef struct auth_s {
|
||||
uint8_t state; // Auth state
|
||||
tx_func_t tx_func; // Auth data send, use indication
|
||||
os_timer_t timer; // Auth timeout timer, start from send random
|
||||
uint8_t ikm[MAX_IKM_LEN]; // Auth sign calc input buffer
|
||||
uint16_t ikm_len; // Auth sign calc input buffer length
|
||||
uint8_t okm[MAX_OKM_LEN]; // Auth key output
|
||||
uint8_t *p_product_key;
|
||||
uint8_t product_key_len;
|
||||
uint8_t *p_device_name;
|
||||
uint8_t device_name_len;
|
||||
uint8_t device_secret[BZ_DEV_DEVICE_SECRET_LEN];
|
||||
uint8_t device_secret_len;
|
||||
bool dyn_update_device_secret;
|
||||
bool offline_auth;
|
||||
uint8_t auth_type;
|
||||
} auth_t;
|
||||
|
||||
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
#define MAX_AUTH_KEYS 5
|
||||
#define AUTH_ID_LEN 8
|
||||
#define AUTH_KEY_LEN 16
|
||||
struct auth_key_pair{
|
||||
uint8_t auth_id[AUTH_ID_LEN];
|
||||
uint8_t auth_key[AUTH_KEY_LEN];
|
||||
};
|
||||
typedef struct auth_key_storage_s{
|
||||
uint8_t index_to_update;
|
||||
struct auth_key_pair kv_pairs[MAX_AUTH_KEYS];
|
||||
} auth_key_storage_t;
|
||||
#endif
|
||||
|
||||
ret_code_t auth_init(ali_init_t const *p_init, tx_func_t tx_func);
|
||||
void auth_reset(void);
|
||||
void auth_rx_command(uint8_t cmd, uint8_t *p_data, uint16_t length);
|
||||
void auth_connected(void);
|
||||
void auth_service_enabled(void);
|
||||
void auth_tx_done(void);
|
||||
bool auth_is_authdone(void);
|
||||
|
||||
ret_code_t auth_get_device_name(uint8_t **pp_device_name, uint8_t *p_length);
|
||||
ret_code_t auth_get_product_key(uint8_t **pp_prod_key, uint8_t *p_length);
|
||||
ret_code_t auth_get_device_secret(uint8_t *p_secret, int *p_length);
|
||||
int auth_calc_adv_sign(uint32_t seq, uint8_t *sign);
|
||||
#ifdef CONFIG_SEC_PER_PK_TO_DN
|
||||
bool get_auth_update_status(void);
|
||||
ret_code_t auth_secret_update_post_process(uint8_t *p_ds, uint16_t length);
|
||||
#endif
|
||||
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
void auth_keys_init(void);
|
||||
bool authkey_set(uint8_t* authid, uint8_t* authkey);
|
||||
bool authkey_get(uint8_t* authid, uint8_t* authkey);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BZ_AUTH_H
|
||||
52
Living_SDK/framework/bluetooth/breeze/include/ble_service.h
Normal file
52
Living_SDK/framework/bluetooth/breeze/include/ble_service.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_AIS_H
|
||||
#define BZ_AIS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BLE_UUID_AIS_SERVICE 0xFEB3
|
||||
|
||||
typedef struct {
|
||||
uint16_t mtu;
|
||||
} ble_ais_init_t;
|
||||
|
||||
typedef struct ble_gatts_char_handles_s {
|
||||
uint16_t chrc_handle;
|
||||
uint16_t value_handle;
|
||||
uint16_t user_desc_handle;
|
||||
uint16_t cccd_handle;
|
||||
uint16_t sccd_handle;
|
||||
} ble_gatts_char_handles_t;
|
||||
|
||||
typedef struct ble_ais_s {
|
||||
uint16_t service_handle;
|
||||
ble_gatts_char_handles_t rc_handles; // Handles related to Read Characteristics
|
||||
ble_gatts_char_handles_t wc_handles; // Handles related to Write Characteristics
|
||||
ble_gatts_char_handles_t ic_handles; // Handles related to Indicate Characteristics
|
||||
ble_gatts_char_handles_t wwnrc_handles; // Handles related to Write WithNoRsp Characteristics
|
||||
ble_gatts_char_handles_t nc_handles; // Handles related to Notify Characteristics
|
||||
uint16_t conn_handle; // Handle of the current connection
|
||||
bool is_indication_enabled;
|
||||
bool is_notification_enabled;
|
||||
void *p_context;
|
||||
uint16_t max_pkt_size;
|
||||
} ble_ais_t;
|
||||
|
||||
uint32_t ble_ais_init(const ble_ais_init_t * p_ais_init);
|
||||
uint32_t ble_ais_send_notification(uint8_t * p_data, uint16_t length);
|
||||
uint32_t ble_ais_send_indication(uint8_t * p_data, uint16_t length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BZ_AIS_H
|
||||
90
Living_SDK/framework/bluetooth/breeze/include/bzopt.h
Normal file
90
Living_SDK/framework/bluetooth/breeze/include/bzopt.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BREEZE_OPT_H
|
||||
#define BREEZE_OPT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "iotx_log.h"
|
||||
|
||||
/* update to version 2.0.6 for supporting. */
|
||||
#define BZ_VERSION "2.0.6"
|
||||
|
||||
#define BZ_AUTH_TIMEOUT 30000 // not allowed to be 0
|
||||
|
||||
#define BZ_TRANSPORT_TIMEOUT 10000
|
||||
|
||||
#ifdef EN_LONG_MTU
|
||||
#define BZ_TRANSPORT_VER 1
|
||||
#else
|
||||
#define BZ_TRANSPORT_VER 0
|
||||
#endif
|
||||
|
||||
#define BZ_ATT_HDR_SIZE 3
|
||||
#define BZ_FRAME_HDR_SIZE 4
|
||||
#define BZ_ENCRY_BLOCK_LENGTH 16 // aes 128, 16 byte a block
|
||||
#define BZ_MAX_FRAME_NUMBER 16 // user payload max frame number
|
||||
#define BZ_GATT_MTU_SIZE_DEFAULT 23 // connect init before mtu exchange, use default mtu size
|
||||
#define BZ_GATT_MTU_SIZE_MAX 247 // when use long mtu, adjust this max mtu value with BLE stack config
|
||||
#define BZ_GATT_MTU_SIZE_LIMIT 103
|
||||
|
||||
/* Larger MTU size consumes more memory. If out of memory, you need to reduce the MTU size */
|
||||
#define BZ_FRAME_SIZE_DEFAULT (BZ_GATT_MTU_SIZE_DEFAULT - BZ_ATT_HDR_SIZE - BZ_FRAME_HDR_SIZE)
|
||||
#define BZ_FRAME_SIZE_MAX (BZ_GATT_MTU_SIZE_MAX - BZ_ATT_HDR_SIZE - BZ_FRAME_HDR_SIZE)
|
||||
#define BZ_FRAME_SIZE_LIMIT (BZ_GATT_MTU_SIZE_LIMIT - BZ_ATT_HDR_SIZE - BZ_FRAME_HDR_SIZE)
|
||||
|
||||
#ifdef EN_LONG_MTU
|
||||
#define BZ_MAX_PAYLOAD_SIZE (BZ_FRAME_SIZE_MAX * BZ_MAX_FRAME_NUMBER)
|
||||
#else
|
||||
#define BZ_MAX_PAYLOAD_SIZE (BZ_FRAME_SIZE_DEFAULT * BZ_MAX_FRAME_NUMBER)
|
||||
#endif
|
||||
|
||||
#ifdef EN_AUTH
|
||||
#define BZ_ENABLE_AUTH 1
|
||||
#else
|
||||
#define BZ_ENABLE_AUTH 0
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_AIS_OTA
|
||||
#define BZ_ENABLE_OTA 1
|
||||
#else
|
||||
#define BZ_ENABLE_OTA 0
|
||||
#endif
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
#define BZ_ENABLE_COMBO_NET 1
|
||||
#else
|
||||
#define BZ_ENABLE_COMBO_NET 0
|
||||
#endif
|
||||
|
||||
#define BREEZE_FLOW(...) log_flow("breeze", __VA_ARGS__)
|
||||
#define BREEZE_DEBUG(...) log_debug("breeze", __VA_ARGS__)
|
||||
#define BREEZE_INFO(...) log_info("breeze", __VA_ARGS__)
|
||||
#define BREEZE_WARN(...) log_warning("breeze", __VA_ARGS__)
|
||||
#define BREEZE_ERR(...) log_err("breeze", __VA_ARGS__)
|
||||
#define BREEZE_FATAL(...) log_crit("breeze", __VA_ARGS__)
|
||||
#define BREEZE_TRACE(...) log_crit("breeze", __VA_ARGS__)
|
||||
#define BREEZE_EMERG(...) log_emerg("breeze", __VA_ARGS__)
|
||||
//#define BZ_VERBOSE_DEBUG
|
||||
#if defined(BZ_VERBOSE_DEBUG)
|
||||
#define BREEZE_VERBOSE(...) log_crit("breeze", __VA_ARGS__)
|
||||
#else
|
||||
#define BREEZE_VERBOSE(...)
|
||||
#endif
|
||||
|
||||
#if defined(BLE_4_0)
|
||||
#define BZ_BLUETOOTH_VER 0x00
|
||||
#define BZ_MAX_SUPPORTED_MTU 23
|
||||
#elif defined(BLE_4_2)
|
||||
#define BZ_BLUETOOTH_VER 0x01
|
||||
#define BZ_MAX_SUPPORTED_MTU 247
|
||||
#elif defined(BLE_5_0)
|
||||
#define BZ_BLUETOOTH_VER 0x10
|
||||
#define BZ_MAX_SUPPORTED_MTU 247
|
||||
#else
|
||||
#define BZ_BLUETOOTH_VER 0x00
|
||||
#define BZ_MAX_SUPPORTED_MTU 23
|
||||
#endif
|
||||
|
||||
#endif // BREEZE_OPT_H
|
||||
186
Living_SDK/framework/bluetooth/breeze/include/common.h
Normal file
186
Living_SDK/framework/bluetooth/breeze/include/common.h
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_COMMON_H
|
||||
#define BZ_COMMON_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "bzopt.h"
|
||||
|
||||
/* Breeze Bluetooth and Device specified definition */
|
||||
#define BZ_BT_MAC_LEN 6
|
||||
#define BZ_DEV_PRODUCT_KEY_LEN 11
|
||||
#define BZ_DEV_PRODUCT_SECRET_LEN 16
|
||||
#define BZ_DEV_MAX_DEVICE_NAME_LEN 32
|
||||
#define BZ_DEV_DEVICE_SECRET_LEN 32
|
||||
#define BZ_DEV_RANDOM_LEN 16
|
||||
|
||||
/* Breeze core profile specified definition */
|
||||
#define BZ_CMD_TYPE_MASK 0xf0
|
||||
|
||||
#define BZ_CMD_CTRL 0x0
|
||||
#define BZ_CMD_STATUS 0x1
|
||||
#define BZ_CMD_QUERY 0x2
|
||||
#define BZ_CMD_REPLY 0x3
|
||||
#define BZ_CMD_EXT_DOWN 0xd
|
||||
#define BZ_CMD_EXT_UP 0xe
|
||||
#define BZ_CMD_ERR 0xf
|
||||
|
||||
#define BZ_CMD_AUTH 0x10
|
||||
#define BZ_CMD_AUTH_RAND 0x11
|
||||
#define BZ_CMD_AUTH_REQ 0x12
|
||||
#define BZ_CMD_AUTH_RSP 0x13
|
||||
#define BZ_CMD_AUTH_CFM 0x14
|
||||
#define BZ_CMD_AUTH_KEY 0x15
|
||||
#define BZ_CMD_AUTH_REKEY 0x16
|
||||
#define BZ_CMD_AUTH_REKEY_RSP 0x17
|
||||
|
||||
#define BZ_CMD_TYPE_OTA 0x20
|
||||
#define BZ_CMD_OTA_VER_REQ 0x20
|
||||
#define BZ_CMD_OTA_VER_RSP 0x21
|
||||
#define BZ_CMD_OTA_REQ 0x22
|
||||
#define BZ_CMD_OTA_RSP 0x23
|
||||
#define BZ_CMD_OTA_PUB_SIZE 0x24
|
||||
#define BZ_CMD_OTA_CHECK_RESULT 0x25
|
||||
#define BZ_CMD_OTA_UPDATE_PROCESS 0x26
|
||||
#define BZ_CMD_OTA_SIZE 0x27
|
||||
#define BZ_CMD_OTA_DONE 0x28
|
||||
#define BZ_CMD_OTA_DATA 0x2f
|
||||
|
||||
typedef uint8_t ret_code_t;
|
||||
|
||||
/* Error codes internal. */
|
||||
#define BZ_SUCCESS 0
|
||||
#define BZ_EINVALIDPARAM 1
|
||||
#define BZ_EDATASIZE 2
|
||||
#define BZ_EINVALIDSTATE 3
|
||||
#define BZ_EGATTNOTIFY 4
|
||||
#define BZ_EGATTINDICATE 5
|
||||
#define BZ_ETIMEOUT 6
|
||||
#define BZ_EBUSY 7
|
||||
#define BZ_EINVALIDDATA 8
|
||||
#define BZ_EINTERNAL 9
|
||||
#define BZ_EINVALIDADDR 10
|
||||
#define BZ_ENOTSUPPORTED 11
|
||||
#define BZ_ENOMEM 12
|
||||
#define BZ_EFORBIDDEN 13
|
||||
#define BZ_ENULL 14
|
||||
#define BZ_EINVALIDLEN 15
|
||||
#define BZ_EINVALIDTLV 16
|
||||
|
||||
#define BZ_ERR_MASK 0xf0
|
||||
#define BZ_TRANS_ERR 0x10
|
||||
#define ALI_ERROR_SRC_TRANSPORT_TX_TIMER 0x10
|
||||
#define ALI_ERROR_SRC_TRANSPORT_RX_TIMER 0x11
|
||||
#define ALI_ERROR_SRC_TRANSPORT_1ST_FRAME 0x12
|
||||
#define ALI_ERROR_SRC_TRANSPORT_OTHER_FRAMES 0x13
|
||||
#define ALI_ERROR_SRC_TRANSPORT_ENCRYPTED 0x14
|
||||
#define ALI_ERROR_SRC_TRANSPORT_RX_BUFF_SIZE 0x15
|
||||
#define ALI_ERROR_SRC_TRANSPORT_PKT_CFM_SENT 0x16
|
||||
#define ALI_ERROR_SRC_TRANSPORT_FW_DATA_DISC 0x17
|
||||
#define ALI_ERROR_SRC_TRANSPORT_SEND 0x18
|
||||
|
||||
#define BZ_AUTH_ERR 0x20
|
||||
#define ALI_ERROR_SRC_AUTH_SEND_RSP 0x20
|
||||
#define ALI_ERROR_SRC_AUTH_PROC_TIMER_0 0x21
|
||||
#define ALI_ERROR_SRC_AUTH_PROC_TIMER_1 0x22
|
||||
#define ALI_ERROR_SRC_AUTH_PROC_TIMER_2 0x23
|
||||
#define ALI_ERROR_SRC_AUTH_SVC_ENABLED 0x24
|
||||
#define ALI_ERROR_SRC_AUTH_SEND_ERROR 0x25
|
||||
#define ALI_ERROR_SRC_AUTH_SEND_KEY 0x26
|
||||
|
||||
#define BZ_EXTCMD_ERR 0x30
|
||||
#define ALI_ERROR_SRC_TYPE_EXT 0x30
|
||||
#define ALI_ERROR_SRC_EXT_SEND_RSP 0x31
|
||||
|
||||
#define BZ_BIND_ERR 0x40
|
||||
#define BZ_ERROR_AC_AS_DATA_LEN 0x40
|
||||
#define BZ_ERROR_AC_AS_NO_PERMIT 0x41
|
||||
#define BZ_ERROR_AC_AS_DELETE 0x42
|
||||
#define BZ_ERROR_AC_AS_STORE 0x43
|
||||
#define BZ_ERROR_AUTH_DATA 0x44
|
||||
#define BZ_ERROR_AUTH_SIGN 0x45
|
||||
|
||||
#define BLE_CONN_HANDLE_INVALID 0xffff
|
||||
#define BLE_CONN_HANDLE_MAGIC 0x1234
|
||||
|
||||
/* Breeze sign and kv-key related definition */
|
||||
#define BZ_CLIENTID_STR "clientId"
|
||||
#define BZ_SEQUENCE_STR "sequence"
|
||||
#define BZ_DEVICE_NAME_STR "deviceName"
|
||||
#define BZ_DEVICE_SECRET_STR "deviceSecret"
|
||||
#define BZ_PRODUCT_KEY_STR "productKey"
|
||||
#define BZ_PRODUCT_SECRET_STR "productSecret"
|
||||
#define BZ_HI_SERVER_STR "Hi,Server"
|
||||
#define BZ_HI_CLIENT_STR "Hi,Client"
|
||||
#define BZ_OK_STR "OK"
|
||||
|
||||
#ifdef EN_AUTH_OFFLINE
|
||||
#define AUTH_KEY_KV_PREFIX "AUTH_KEY_PAIRS"
|
||||
#endif
|
||||
#define BZ_AUTH_CODE_KV_PREFIX "AUTH_AC_AS" // AC_length(1Byte)+AC+AS_length+AS, when no AS, use DS
|
||||
|
||||
enum {
|
||||
BZ_EVENT_CONNECTED, // BLE connect
|
||||
BZ_EVENT_DISCONNECTED, // BLE disconnect
|
||||
BZ_EVENT_AUTHENTICATED, // Authenticated
|
||||
BZ_EVENT_TX_DONE, // User payload tx done
|
||||
BZ_EVENT_RX_INFO, // User payload received
|
||||
BZ_EVENT_APINFO, // Get AP info data, for ble-awss
|
||||
BZ_EVENT_AC_AS, // Get User bind data, for ble-bind
|
||||
BZ_EVENT_AUTH_SIGN, // Get User sign data, for ble-user-sign
|
||||
BZ_EVENT_ERR_DISCONT, // OTA transfer discontinue error occur
|
||||
};
|
||||
|
||||
// User bind data operaton result
|
||||
enum {
|
||||
BZ_AC_AS_ADD,
|
||||
BZ_AC_AS_UPDATE,
|
||||
BZ_AC_AS_DELETE,
|
||||
};
|
||||
|
||||
// User sign data operaton result
|
||||
enum {
|
||||
BZ_AUTH_SIGN_NO_CHECK_PASS,
|
||||
BZ_AUTH_SIGN_CHECK_PASS,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t *p_data;
|
||||
uint16_t length;
|
||||
} ali_data_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t type;
|
||||
ali_data_t rx_data;
|
||||
} ali_event_t;
|
||||
|
||||
typedef void (*ali_event_handler_t)(ali_event_t *p_event);
|
||||
|
||||
typedef struct {
|
||||
ali_event_handler_t event_handler;
|
||||
uint32_t model_id;
|
||||
ali_data_t product_key; // PK 11 to 20 bytes)
|
||||
ali_data_t product_secret; // secret 16 to 40 bytes
|
||||
ali_data_t device_name; // DN 20 to 32 bytes
|
||||
ali_data_t device_secret; // secret 16 to 40 bytes
|
||||
uint8_t *adv_mac; // mac address filled in breeze adv data(maybe bt addr or wifi mac)
|
||||
uint32_t transport_timeout; // Timeout of Tx/Rx, in number of ms. 0 if not used.
|
||||
uint16_t max_mtu; // Maximum MTU.
|
||||
uint8_t *user_adv_data; // User's adv data, if any.
|
||||
uint32_t user_adv_len; // User's adv data length
|
||||
} ali_init_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t src;
|
||||
uint8_t code;
|
||||
} bz_err_event_t;
|
||||
|
||||
typedef uint32_t (*tx_func_t)(uint8_t cmd, uint8_t *p_data, uint16_t length);
|
||||
|
||||
#endif // BZ_COMMON_H
|
||||
78
Living_SDK/framework/bluetooth/breeze/include/core.h
Normal file
78
Living_SDK/framework/bluetooth/breeze/include/core.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_CORE_H
|
||||
#define BZ_CORE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "ble_service.h"
|
||||
#include "transport.h"
|
||||
#include "auth.h"
|
||||
#include "extcmd.h"
|
||||
#include "bzopt.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
// Definition of breeze advertising data
|
||||
#define BZ_MAX_ADV_DATA_LEN (16) // fixed manufacturer advertising data length
|
||||
#define BZ_MANUFACT_SPEC_TYPE (0xFF) // fixed manufacturer adv data type
|
||||
#define BZ_ALI_COMPANY_ID (0x01A8) // company id
|
||||
// Vendor ID octet
|
||||
#define BZ_SDK_VER_Pos (0) // Breeze Version bit0-3
|
||||
#define BZ_SUB_TYPE_Pos (4) // Breeze adv subtype, bit4-7, subtype 0-15
|
||||
// Breeze subtype definition
|
||||
#define BZ_SUB_TYPE_BASIC (0) // Breeze basic adv
|
||||
#define BZ_SUB_TYPE_SEC_BEACON (1) // Breeze security adv
|
||||
#define BZ_SUB_TYPE_BLE_AWSS (2) // Breeze ble-AWSS adv
|
||||
#define BZ_SUB_TYPE_COMBO_AP_DISCONN (3) // Breeze combo AP disconnected adv
|
||||
#define BZ_SUB_TYPE_COMBO_AP_CONN (4) // Breeze combo AP connected adv
|
||||
// Function mack bit def
|
||||
#define BZ_FMSK_BLUETOOTH_VER_Pos (0) // FMSK bt version, bit 0-1
|
||||
#define BZ_FMSK_OTA_Pos (2) // FMSK OTA flag, bit2, 0-Breeze OTA unsupport, 1-Breeze OTA supported
|
||||
#define BZ_FMSK_SECURITY_Pos (3) // FMSK sec flag, bit3, 0-no security, 1-security supported
|
||||
#define BZ_FMSK_SECRET_TYPE_Pos (4) // FMSK sec type, bit4, mandatory if bit3==1, 0-perproduct, 1-perdevice
|
||||
#define BZ_FMSK_SEC_ADV_Pos (5) // FMSK sec adv type, bit5, 0-not secure adv, 1-secure adv
|
||||
#define BZ_FMSK_BIND_STATE_Pos (6) // FMSK bind state, bit6, 0-not bind, 1-binded
|
||||
// Breeze security type def
|
||||
#define BZ_SEC_TYPE_PRODUCT (0) // security type perproduct
|
||||
#define BZ_SEC_TYPE_DEVICE (1) // security type perdevice
|
||||
// Breeze bind state def
|
||||
#define BZ_BIND_STATE_UNBIND (0) // unbind state
|
||||
#define BZ_BIND_STATE_BIND (1) // bind state
|
||||
|
||||
typedef struct {
|
||||
ali_event_handler_t event_handler;
|
||||
uint8_t adv_data[BZ_MAX_ADV_DATA_LEN];
|
||||
uint16_t adv_data_len;
|
||||
uint8_t adv_mac[BZ_BT_MAC_LEN]; // mac address filled in breeze adv data(maybe bt addr or wifi mac)
|
||||
uint32_t product_id;
|
||||
uint8_t product_key[BZ_DEV_PRODUCT_KEY_LEN];
|
||||
uint8_t product_key_len;
|
||||
uint8_t product_secret[BZ_DEV_PRODUCT_SECRET_LEN];
|
||||
uint8_t product_secret_len;
|
||||
uint8_t device_name[BZ_DEV_MAX_DEVICE_NAME_LEN];
|
||||
uint8_t device_name_len;
|
||||
uint8_t device_secret[BZ_DEV_DEVICE_SECRET_LEN];
|
||||
uint8_t device_secret_len;
|
||||
uint8_t admin_checkin; // 0-not checkin, 1-checkin
|
||||
uint8_t guest_checkin; // 0-not checkin, 1-checkin
|
||||
} core_t;
|
||||
|
||||
ret_code_t core_init(ali_init_t const *p_init);
|
||||
void core_reset(void);
|
||||
void core_create_bz_adv_data(uint8_t sub_type, uint8_t sec_type, uint8_t bind_state);
|
||||
ret_code_t core_get_bz_adv_data(uint8_t *p_data, uint16_t *length);
|
||||
void core_event_notify(uint8_t evt_type, uint8_t *data, uint16_t length);
|
||||
void core_handle_err(uint8_t src, uint8_t code);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BZ_CORE_H
|
||||
41
Living_SDK/framework/bluetooth/breeze/include/extcmd.h
Normal file
41
Living_SDK/framework/bluetooth/breeze/include/extcmd.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_EXTCMD_H
|
||||
#define BZ_EXTCMD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#define BZ_EXT_MAX_TLV_01_RSP_LEN 26
|
||||
|
||||
#define BZ_EXT_TX_BUFF_LEN 255 /**< Tx buffer size. */
|
||||
|
||||
typedef struct extcmd_s {
|
||||
tx_func_t tx_func;
|
||||
uint8_t tlv_01_rsp[BZ_EXT_MAX_TLV_01_RSP_LEN];
|
||||
uint8_t tlv_01_rsp_len;
|
||||
uint32_t model_id;
|
||||
uint8_t*p_product_key;
|
||||
uint8_t product_key_len;
|
||||
uint8_t *p_device_secret;
|
||||
uint8_t device_secret_len;
|
||||
uint8_t *p_device_name;
|
||||
uint8_t device_name_len;
|
||||
uint8_t tx_buff[BZ_EXT_TX_BUFF_LEN];
|
||||
} extcmd_t;
|
||||
|
||||
ret_code_t extcmd_init(ali_init_t const *p_init, tx_func_t tx_func);
|
||||
void extcmd_rx_command(uint8_t cmd, uint8_t *p_data, uint16_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BZ_EXTCMD_H
|
||||
30
Living_SDK/framework/bluetooth/breeze/include/sha256.h
Normal file
30
Living_SDK/framework/bluetooth/breeze/include/sha256.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef SHA256_H
|
||||
#define SHA256_H
|
||||
|
||||
/*************************** HEADER FILES ***************************/
|
||||
#include <stddef.h>
|
||||
|
||||
/****************************** MACROS ******************************/
|
||||
#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
|
||||
|
||||
/**************************** DATA TYPES ****************************/
|
||||
typedef unsigned char BYTE; // 8-bit byte
|
||||
typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
|
||||
|
||||
typedef struct {
|
||||
BYTE data[64];
|
||||
WORD datalen;
|
||||
unsigned long long bitlen;
|
||||
WORD state[8];
|
||||
} SHA256_CTX;
|
||||
|
||||
/*********************** FUNCTION DECLARATIONS **********************/
|
||||
void sha256_init(SHA256_CTX *ctx);
|
||||
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
|
||||
void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
|
||||
|
||||
#endif // SHA256_H
|
||||
80
Living_SDK/framework/bluetooth/breeze/include/transport.h
Normal file
80
Living_SDK/framework/bluetooth/breeze/include/transport.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_TRANSPORT_H
|
||||
#define BZ_TRANSPORT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "common.h"
|
||||
#include "breeze_hal_os.h"
|
||||
#include "bzopt.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
enum {
|
||||
TX_NOTIFICATION,
|
||||
TX_INDICATION,
|
||||
};
|
||||
|
||||
typedef uint32_t (*transport_tx_func_t)(uint8_t *p_data, uint16_t length);
|
||||
|
||||
#define TX_BUFF_LEN (BZ_MAX_SUPPORTED_MTU - 3)
|
||||
#define RX_BUFF_LEN BZ_MAX_PAYLOAD_SIZE
|
||||
typedef struct transport_s {
|
||||
struct {
|
||||
uint8_t buff[TX_BUFF_LEN];
|
||||
uint8_t *data;
|
||||
uint16_t len;
|
||||
uint16_t bytes_sent;
|
||||
uint8_t encrypted;
|
||||
uint8_t msg_id;
|
||||
uint8_t cmd;
|
||||
uint8_t total_frame;
|
||||
uint8_t frame_seq;
|
||||
uint8_t zeroes_padded;
|
||||
uint16_t pkt_req;
|
||||
uint16_t pkt_cfm;
|
||||
os_timer_t timer;
|
||||
transport_tx_func_t active_func;
|
||||
void *mutex_indicate_done;
|
||||
} tx;
|
||||
struct {
|
||||
uint8_t buff[RX_BUFF_LEN];
|
||||
uint16_t buff_size;
|
||||
uint16_t bytes_received;
|
||||
uint8_t msg_id;
|
||||
uint8_t cmd;
|
||||
uint8_t total_frame;
|
||||
uint8_t frame_seq;
|
||||
os_timer_t timer;
|
||||
} rx;
|
||||
uint16_t max_pkt_size;
|
||||
void *p_key;
|
||||
uint16_t timeout;
|
||||
void *p_aes_ctx;
|
||||
} transport_t;
|
||||
|
||||
struct rx_cmd_post_t{
|
||||
uint8_t cmd;
|
||||
uint8_t frame_seq;
|
||||
uint8_t *p_rx_buf;
|
||||
uint16_t buf_sz;
|
||||
};
|
||||
|
||||
ret_code_t transport_init(ali_init_t const *p_init);
|
||||
void transport_reset(void);
|
||||
ret_code_t transport_tx(uint8_t tx_type, uint8_t cmd,
|
||||
uint8_t const *const p_data, uint16_t length);
|
||||
void transport_txdone(uint16_t pkt_sent);
|
||||
void transport_rx(uint8_t *p_data, uint16_t length);
|
||||
uint32_t transport_update_key(uint8_t *p_key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BZ_TRANSPORT_H
|
||||
39
Living_SDK/framework/bluetooth/breeze/include/utils.h
Normal file
39
Living_SDK/framework/bluetooth/breeze/include/utils.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef BZ_UTILS_H
|
||||
#define BZ_UTILS_H
|
||||
|
||||
#define SET_U16_LE(data, val) { \
|
||||
*(uint8_t *)(data) = (uint8_t)(val & 0xFF); \
|
||||
*((uint8_t *)(data) + 1) = (uint8_t)((val >> 8) & 0xFF); \
|
||||
}
|
||||
|
||||
#define SET_U32_LE(data, val) { \
|
||||
*(uint8_t *)(data) = (uint8_t)(val & 0xFF); \
|
||||
*((uint8_t *)(data) + 1) = (uint8_t)((val >> 8) & 0xFF); \
|
||||
*((uint8_t *)(data) + 2) = (uint8_t)((val >> 16) & 0xFF); \
|
||||
*((uint8_t *)(data) + 3) = (uint8_t)((val >> 24) & 0xFF); \
|
||||
}
|
||||
|
||||
#define SET_U32_BE(data, val) { \
|
||||
*(uint8_t *)(data) = (uint8_t)((val >> 24) & 0xFF); \
|
||||
*((uint8_t *)(data) + 1) = (uint8_t)((val >> 16) & 0xFF); \
|
||||
*((uint8_t *)(data) + 2) = (uint8_t)((val >> 8) & 0xFF); \
|
||||
*((uint8_t *)(data) + 3) = (uint8_t)(val & 0xFF); \
|
||||
}
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
uint8_t hex2ascii(uint8_t digit);
|
||||
void hex2string(uint8_t *hex, uint32_t len, uint8_t *str);
|
||||
void utf8_to_pw(uint8_t *data, uint8_t len, char *pw);
|
||||
|
||||
void get_random(uint8_t *random, uint8_t random_len);
|
||||
void hex_byte_dump_debug(uint8_t *data, int len, int tab_num);
|
||||
void hex_byte_dump_verbose(uint8_t *data, int len, int tab_num);
|
||||
|
||||
#endif // BZ_UTILS_H
|
||||
74
Living_SDK/framework/bluetooth/profile/bas.c
Normal file
74
Living_SDK/framework/bluetooth/profile/bas.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/** @file
|
||||
* @brief BAS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
static struct bt_gatt_ccc_cfg blvl_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
static u8_t simulate_blvl;
|
||||
static u8_t battery = 100;
|
||||
|
||||
static void blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
||||
u16_t value)
|
||||
{
|
||||
simulate_blvl = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
}
|
||||
|
||||
static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
const char *value = attr->user_data;
|
||||
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
|
||||
sizeof(*value));
|
||||
}
|
||||
|
||||
/* Battery Service Declaration */
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL,
|
||||
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_PERM_READ,
|
||||
read_blvl, NULL, &battery),
|
||||
BT_GATT_CCC(blvl_ccc_cfg, blvl_ccc_cfg_changed),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service bas_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
void bas_init(void)
|
||||
{
|
||||
bt_gatt_service_register(&bas_svc);
|
||||
}
|
||||
|
||||
void bas_notify(void)
|
||||
{
|
||||
if (!simulate_blvl) {
|
||||
return;
|
||||
}
|
||||
|
||||
battery--;
|
||||
if (!battery) {
|
||||
/* Software eco battery charger */
|
||||
battery = 100;
|
||||
}
|
||||
|
||||
bt_gatt_notify(NULL, &attrs[2], &battery, sizeof(battery));
|
||||
}
|
||||
20
Living_SDK/framework/bluetooth/profile/bas.h
Normal file
20
Living_SDK/framework/bluetooth/profile/bas.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/** @file
|
||||
* @brief BAS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void bas_init(void);
|
||||
void bas_notify(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
115
Living_SDK/framework/bluetooth/profile/cts.c
Normal file
115
Living_SDK/framework/bluetooth/profile/cts.c
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/** @file
|
||||
* @brief CTS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
static struct bt_gatt_ccc_cfg ct_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
static u8_t ct[10];
|
||||
static u8_t ct_update;
|
||||
|
||||
static void ct_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t value)
|
||||
{
|
||||
/* TODO: Handle value */
|
||||
}
|
||||
|
||||
static ssize_t read_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
const char *value = attr->user_data;
|
||||
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
|
||||
sizeof(ct));
|
||||
}
|
||||
|
||||
static ssize_t write_ct(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
const void *buf, u16_t len, u16_t offset,
|
||||
u8_t flags)
|
||||
{
|
||||
u8_t *value = attr->user_data;
|
||||
|
||||
if (offset + len > sizeof(ct)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
||||
}
|
||||
|
||||
memcpy(value + offset, buf, len);
|
||||
ct_update = 1;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Current Time Service Declaration */
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_CTS),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_CTS_CURRENT_TIME, BT_GATT_CHRC_READ |
|
||||
BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_WRITE),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_CTS_CURRENT_TIME,
|
||||
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
|
||||
read_ct, write_ct, ct),
|
||||
BT_GATT_CCC(ct_ccc_cfg, ct_ccc_cfg_changed),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service cts_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
static void generate_current_time(u8_t *buf)
|
||||
{
|
||||
u16_t year;
|
||||
|
||||
/* 'Exact Time 256' contains 'Day Date Time' which contains
|
||||
* 'Date Time' - characteristic contains fields for:
|
||||
* year, month, day, hours, minutes and seconds.
|
||||
*/
|
||||
|
||||
year = sys_cpu_to_le16(2015);
|
||||
memcpy(buf, &year, 2); /* year */
|
||||
buf[2] = 5; /* months starting from 1 */
|
||||
buf[3] = 30; /* day */
|
||||
buf[4] = 12; /* hours */
|
||||
buf[5] = 45; /* minutes */
|
||||
buf[6] = 30; /* seconds */
|
||||
|
||||
/* 'Day of Week' part of 'Day Date Time' */
|
||||
buf[7] = 1; /* day of week starting from 1 */
|
||||
|
||||
/* 'Fractions 256 part of 'Exact Time 256' */
|
||||
buf[8] = 0;
|
||||
|
||||
/* Adjust reason */
|
||||
buf[9] = 0; /* No update, change, etc */
|
||||
}
|
||||
|
||||
void cts_init(void)
|
||||
{
|
||||
/* Simulate current time for Current Time Service */
|
||||
generate_current_time(ct);
|
||||
|
||||
bt_gatt_service_register(&cts_svc);
|
||||
}
|
||||
|
||||
void cts_notify(void)
|
||||
{ /* Current Time Service updates only when time is changed */
|
||||
if (!ct_update) {
|
||||
return;
|
||||
}
|
||||
|
||||
ct_update = 0;
|
||||
bt_gatt_notify(NULL, &attrs[3], &ct, sizeof(ct));
|
||||
}
|
||||
20
Living_SDK/framework/bluetooth/profile/cts.h
Normal file
20
Living_SDK/framework/bluetooth/profile/cts.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/** @file
|
||||
* @brief CTS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void cts_init(void);
|
||||
void cts_notify(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
64
Living_SDK/framework/bluetooth/profile/dis.c
Normal file
64
Living_SDK/framework/bluetooth/profile/dis.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/** @file
|
||||
* @brief DIS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
static const char *dis_model;
|
||||
static const char *dis_manuf;
|
||||
|
||||
static ssize_t read_model(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, dis_model,
|
||||
strlen(dis_model));
|
||||
}
|
||||
|
||||
static ssize_t read_manuf(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, dis_manuf,
|
||||
strlen(dis_manuf));
|
||||
}
|
||||
|
||||
/* Device Information Service Declaration */
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_DIS),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_DIS_MODEL_NUMBER, BT_GATT_PERM_READ,
|
||||
read_model, NULL, NULL),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_DIS_MANUFACTURER_NAME,
|
||||
BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_DIS_MANUFACTURER_NAME, BT_GATT_PERM_READ,
|
||||
read_manuf, NULL, NULL),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service dis_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
void dis_init(const char *model, const char *manuf)
|
||||
{
|
||||
dis_model = model;
|
||||
dis_manuf = manuf;
|
||||
|
||||
bt_gatt_service_register(&dis_svc);
|
||||
}
|
||||
19
Living_SDK/framework/bluetooth/profile/dis.h
Normal file
19
Living_SDK/framework/bluetooth/profile/dis.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/** @file
|
||||
* @brief DIS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void dis_init(const char *model, const char *manuf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
170
Living_SDK/framework/bluetooth/profile/hog.c
Normal file
170
Living_SDK/framework/bluetooth/profile/hog.c
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
/** @file
|
||||
* @brief HoG Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
enum {
|
||||
HIDS_REMOTE_WAKE = BIT(0),
|
||||
HIDS_NORMALLY_CONNECTABLE = BIT(1),
|
||||
};
|
||||
|
||||
struct hids_info {
|
||||
u16_t version; /* version number of base USB HID Specification */
|
||||
u8_t code; /* country HID Device hardware is localized for. */
|
||||
u8_t flags;
|
||||
} __packed;
|
||||
|
||||
struct hids_report {
|
||||
u8_t id; /* report id */
|
||||
u8_t type; /* report type */
|
||||
} __packed;
|
||||
|
||||
static struct hids_info info = {
|
||||
.version = 0x0000,
|
||||
.code = 0x00,
|
||||
.flags = HIDS_NORMALLY_CONNECTABLE,
|
||||
};
|
||||
|
||||
enum {
|
||||
HIDS_INPUT = 0x01,
|
||||
HIDS_OUTPUT = 0x02,
|
||||
HIDS_FEATURE = 0x03,
|
||||
};
|
||||
|
||||
static struct hids_report input = {
|
||||
.id = 0x01,
|
||||
.type = HIDS_INPUT,
|
||||
};
|
||||
|
||||
static struct bt_gatt_ccc_cfg input_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
static u8_t simulate_input;
|
||||
static u8_t ctrl_point;
|
||||
static u8_t report_map[] = {
|
||||
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
|
||||
0x09, 0x02, /* Usage (Mouse) */
|
||||
0xA1, 0x01, /* Collection (Application) */
|
||||
0x09, 0x01, /* Usage (Pointer) */
|
||||
0xA1, 0x00, /* Collection (Physical) */
|
||||
0x05, 0x09, /* Usage Page (Button) */
|
||||
0x19, 0x01, /* Usage Minimum (0x01) */
|
||||
0x29, 0x03, /* Usage Maximum (0x03) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x95, 0x03, /* Report Count (3) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,...) */
|
||||
0x95, 0x01, /* Report Count (1) */
|
||||
0x75, 0x05, /* Report Size (5) */
|
||||
0x81, 0x03, /* Input (Const,Var,Abs,No Wrap,Linear,...) */
|
||||
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
|
||||
0x09, 0x30, /* Usage (X) */
|
||||
0x09, 0x31, /* Usage (Y) */
|
||||
0x15, 0x81, /* Logical Minimum (129) */
|
||||
0x25, 0x7F, /* Logical Maximum (127) */
|
||||
0x75, 0x08, /* Report Size (8) */
|
||||
0x95, 0x02, /* Report Count (2) */
|
||||
0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
|
||||
0xC0, /* End Collection */
|
||||
0xC0, /* End Collection */
|
||||
};
|
||||
|
||||
|
||||
static ssize_t read_info(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
|
||||
sizeof(struct hids_info));
|
||||
}
|
||||
|
||||
static ssize_t read_report_map(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, report_map,
|
||||
sizeof(report_map));
|
||||
}
|
||||
|
||||
static ssize_t read_report(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
|
||||
sizeof(struct hids_report));
|
||||
}
|
||||
|
||||
static void input_ccc_changed(const struct bt_gatt_attr *attr, u16_t value)
|
||||
{
|
||||
simulate_input = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
}
|
||||
|
||||
static ssize_t read_input_report(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr, void *buf,
|
||||
u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, NULL, 0);
|
||||
}
|
||||
|
||||
static ssize_t write_ctrl_point(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr,
|
||||
const void *buf, u16_t len, u16_t offset,
|
||||
u8_t flags)
|
||||
{
|
||||
u8_t *value = attr->user_data;
|
||||
|
||||
if (offset + len > sizeof(ctrl_point)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
||||
}
|
||||
|
||||
memcpy(value + offset, buf, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* HID Service Declaration */
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_INFO, BT_GATT_PERM_READ,
|
||||
read_info, NULL, &info),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_MAP, BT_GATT_PERM_READ,
|
||||
read_report_map, NULL, NULL),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT,
|
||||
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT, BT_GATT_PERM_READ_AUTHEN,
|
||||
read_input_report, NULL, NULL),
|
||||
BT_GATT_CCC(input_ccc_cfg, input_ccc_changed),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ,
|
||||
read_report, NULL, &input),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT,
|
||||
BT_GATT_CHRC_WRITE_WITHOUT_RESP),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_CTRL_POINT, BT_GATT_PERM_WRITE,
|
||||
NULL, write_ctrl_point, &ctrl_point),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service hog_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
void hog_init(void)
|
||||
{
|
||||
bt_gatt_service_register(&hog_svc);
|
||||
}
|
||||
19
Living_SDK/framework/bluetooth/profile/hog.h
Normal file
19
Living_SDK/framework/bluetooth/profile/hog.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/** @file
|
||||
* @brief HoG Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void hog_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
86
Living_SDK/framework/bluetooth/profile/hrs.c
Normal file
86
Living_SDK/framework/bluetooth/profile/hrs.c
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/** @file
|
||||
* @brief HRS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
static struct bt_gatt_ccc_cfg hrmc_ccc_cfg[BT_GATT_CCC_MAX] = {};
|
||||
static u8_t simulate_hrm;
|
||||
static u8_t heartrate = 90;
|
||||
static u8_t hrs_blsc;
|
||||
|
||||
static void hrmc_ccc_cfg_changed(const struct bt_gatt_attr *attr,
|
||||
u16_t value)
|
||||
{
|
||||
simulate_hrm = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
||||
}
|
||||
|
||||
static ssize_t read_blsc(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
||||
void *buf, u16_t len, u16_t offset)
|
||||
{
|
||||
return bt_gatt_attr_read(conn, attr, buf, len, offset, &hrs_blsc,
|
||||
sizeof(hrs_blsc));
|
||||
}
|
||||
|
||||
/* Heart Rate Service Declaration */
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_HRS),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_MEASUREMENT, BT_GATT_CHRC_NOTIFY),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HRS_MEASUREMENT, BT_GATT_PERM_READ, NULL,
|
||||
NULL, NULL),
|
||||
BT_GATT_CCC(hrmc_ccc_cfg, hrmc_ccc_cfg_changed),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_BODY_SENSOR, BT_GATT_CHRC_READ),
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HRS_BODY_SENSOR, BT_GATT_PERM_READ,
|
||||
read_blsc, NULL, NULL),
|
||||
BT_GATT_CHARACTERISTIC(BT_UUID_HRS_CONTROL_POINT, BT_GATT_CHRC_WRITE),
|
||||
/* TODO: Add write permission and callback */
|
||||
BT_GATT_DESCRIPTOR(BT_UUID_HRS_CONTROL_POINT, BT_GATT_PERM_READ, NULL,
|
||||
NULL, NULL),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service hrs_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
void hrs_init(u8_t blsc)
|
||||
{
|
||||
hrs_blsc = blsc;
|
||||
|
||||
bt_gatt_service_register(&hrs_svc);
|
||||
}
|
||||
|
||||
void hrs_notify(void)
|
||||
{
|
||||
static u8_t hrm[2];
|
||||
|
||||
/* Heartrate measurements simulation */
|
||||
if (!simulate_hrm) {
|
||||
return;
|
||||
}
|
||||
|
||||
heartrate++;
|
||||
if (heartrate == 160) {
|
||||
heartrate = 90;
|
||||
}
|
||||
|
||||
hrm[0] = 0x06; /* uint8, sensor contact */
|
||||
hrm[1] = heartrate;
|
||||
|
||||
bt_gatt_notify(NULL, &attrs[2], &hrm, sizeof(hrm));
|
||||
}
|
||||
20
Living_SDK/framework/bluetooth/profile/hrs.h
Normal file
20
Living_SDK/framework/bluetooth/profile/hrs.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/** @file
|
||||
* @brief HRS Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void hrs_init(u8_t blsc);
|
||||
void hrs_notify(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
82
Living_SDK/framework/bluetooth/profile/ipss.c
Normal file
82
Living_SDK/framework/bluetooth/profile/ipss.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/** @file
|
||||
* @brief IP Support Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2015-2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/types.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <misc/printk.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/conn.h>
|
||||
#include <bluetooth/uuid.h>
|
||||
#include <bluetooth/gatt.h>
|
||||
|
||||
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
|
||||
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
|
||||
#define UNKNOWN_APPEARANCE 0x0000
|
||||
|
||||
static struct bt_gatt_attr attrs[] = {
|
||||
/* IP Support Service Declaration */
|
||||
BT_GATT_PRIMARY_SERVICE(BT_UUID_IPSS),
|
||||
};
|
||||
|
||||
static struct bt_gatt_service ipss_svc = BT_GATT_SERVICE(attrs);
|
||||
|
||||
static const struct bt_data ad[] = {
|
||||
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
|
||||
BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x20, 0x18),
|
||||
};
|
||||
|
||||
static const struct bt_data sd[] = {
|
||||
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
|
||||
};
|
||||
|
||||
static void connected(struct bt_conn *conn, u8_t err)
|
||||
{
|
||||
if (err) {
|
||||
printk("Connection failed (err %u)\n", err);
|
||||
} else {
|
||||
printk("Connected\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void disconnected(struct bt_conn *conn, u8_t reason)
|
||||
{
|
||||
printk("Disconnected (reason %u)\n", reason);
|
||||
}
|
||||
|
||||
static struct bt_conn_cb conn_callbacks = {
|
||||
.connected = connected,
|
||||
.disconnected = disconnected,
|
||||
};
|
||||
|
||||
void ipss_init(void)
|
||||
{
|
||||
bt_gatt_service_register(&ipss_svc);
|
||||
|
||||
bt_conn_cb_register(&conn_callbacks);
|
||||
}
|
||||
|
||||
int ipss_advertise(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad),
|
||||
sd, ARRAY_SIZE(sd));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
Living_SDK/framework/bluetooth/profile/ipss.h
Normal file
20
Living_SDK/framework/bluetooth/profile/ipss.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/** @file
|
||||
* @brief IPSP Service sample
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2015-2016 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void ipss_init(void);
|
||||
int ipss_advertise(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
7
Living_SDK/framework/bluetooth/profile/profile.mk
Normal file
7
Living_SDK/framework/bluetooth/profile/profile.mk
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
NAME := profile
|
||||
|
||||
$(NAME)_MBINS_TYPE := kernel
|
||||
|
||||
$(NAME)_SOURCES := bas.c cts.c cts.h dis.c hrs.c
|
||||
|
||||
GLOBAL_INCLUDES += .
|
||||
Loading…
Add table
Add a link
Reference in a new issue