Commit cf25edcf authored by zet's avatar zet

fist

parents
*.DS_Store
*.pyc
*.swp
.netrwhist
vim/bundle/YouCompleteMe/
vim/bundle/vim-airline-themes/
vim/bundle/vim-airline/
vim/bundle/ultisnips/
vim/bundle/vim-snippets/
venv/
TARGET=NRF52_DK
TOOLCHAIN=GCC_ARM
default: hello
hello:
mbed compile -m NRF52_DK -t GCC_ARM
flash:
@echo Flashing: BLE_LED.hex
nrfjprog --program BUILD/NRF52_DK/GCC_ARM/BLE_LED.hex -f nrf52 --sectorerase
nrfjprog --reset -f nrf52
clean:
rm -rf BUILD
#!/bin/bash -e
if [ $(which pip2) ]; then
PIP="pip2"
else
PIP="pip"
fi
if [ $(which virtualenv2) ]; then
VIRTUALENV="virtualenv2"
else
VIRTUALENV="virtualenv"
fi
$VIRTUALENV venv
source venv/bin/activate
$PIP install --upgrade mbed-cli
mbed-os @ 50b3418e
Subproject commit 50b3418e45484ebf442b88cd935a2d5355402d7d
https://github.com/ARMmbed/mbed-os/#50b3418e45484ebf442b88cd935a2d5355402d7d
{
"target_overrides": {
"K64F": {
"target.features_add": ["BLE"],
"target.extra_labels_add": ["ST_BLUENRG"],
"target.macros_add": ["IDB0XA1_D13_PATCH"]
},
"NUCLEO_F401RE": {
"target.features_add": ["BLE"],
"target.extra_labels_add": ["ST_BLUENRG"]
}
}
}
"""
mbed SDK
Copyright (c) 2016 ARM Limited
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.
"""
from os.path import join, abspath, dirname
#ROOT = abspath(join(dirname(__file__), "."))
##############################################################################
# Build System Settings
##############################################################################
#BUILD_DIR = abspath(join(ROOT, "build"))
# ARM
#ARM_PATH = "C:/Program Files/ARM"
# GCC ARM
#GCC_ARM_PATH = ""
# GCC CodeRed
#GCC_CR_PATH = "C:/code_red/RedSuite_4.2.0_349/redsuite/Tools/bin"
# IAR
#IAR_PATH = "C:/Program Files (x86)/IAR Systems/Embedded Workbench 7.0/arm"
# Goanna static analyser. Please overload it in private_settings.py
#GOANNA_PATH = "c:/Program Files (x86)/RedLizards/Goanna Central 3.2.3/bin"
#BUILD_OPTIONS = []
# mbed.org username
#MBED_ORG_USER = ""
{
"name": "ble-led",
"version": "0.0.1",
"description": "A simple service that demonstrates the use of a read-write characteristic to control a LED",
"licenses": [
{
"url": "https://spdx.org/licenses/Apache-2.0",
"type": "Apache-2.0"
}
],
"dependencies": {
"ble": "^2.0.0"
},
"targetDependencies": {},
"bin": "./source"
}
TARGET_ST_BLUENRG @ 6670a449
Subproject commit 6670a4495aafe1601a105ec9f6606f70b4c3424c
https://github.com/ARMmbed/ble-x-nucleo-idb0xa1/#6670a4495aafe1601a105ec9f6606f70b4c3424c
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* 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.
*/
#ifndef __BLE_LED_SERVICE_H__
#define __BLE_LED_SERVICE_H__
class LEDService {
public:
const static uint16_t LED_SERVICE_UUID = 0xA000;
const static uint16_t LED_STATE_CHARACTERISTIC_UUID = 0xA001;
LEDService(BLEDevice &_ble, bool initialValueForLEDCharacteristic) :
ble(_ble), ledState(LED_STATE_CHARACTERISTIC_UUID, &initialValueForLEDCharacteristic)
{
ledState.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM);
GattCharacteristic *charTable[] = {&ledState};
GattService ledService(LED_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
ble.addService(ledService);
}
GattAttribute::Handle_t getValueHandle() const
{
return ledState.getValueHandle();
}
private:
BLEDevice &ble;
ReadWriteGattCharacteristic<bool> ledState;
};
#endif /* #ifndef __BLE_LED_SERVICE_H__ */
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* 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.
*/
#include <events/mbed_events.h>
#include <mbed.h>
#include "ble/BLE.h"
#include "LEDService.h"
//debug output
Serial pc(USBTX, USBRX); // tx, rx
//設定輸出 LED1 and LED2
DigitalOut alivenessLED(LED1, 0);
DigitalOut actuatedLED(LED2, 0);
//設定device name and service uuid
const static char DEVICE_NAME[] = "LED";
static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
//thread 與排隊機制
static EventQueue eventQueue(
/* event count */ 10 * /* event size */ 32
);
LEDService *ledServicePtr;
void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
{
pc.printf("\rConnected!\r\n");
}
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
pc.printf("Disconnected!\r\n");
(void) params;
BLE::Instance().gap().startAdvertising();
}
void blinkCallback(void)
{
alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
}
void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
{
pc.printf("[*] Input passKey: ");
for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
pc.printf("%c ", passkey[i]);
}
pc.printf("\r\n");
}
void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
{
if (status == SecurityManager::SEC_STATUS_SUCCESS) {
pc.printf("[+] Security success\r\n");
} else {
pc.printf("[!] Security failed\r\n");
}
}
/**
* This callback allows the LEDService to receive updates to the ledState Characteristic.
*
* @param[in] params
* Information about the characterisitc being updated.
*/
void onDataWrittenCallback(const GattWriteCallbackParams *params) {
if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
actuatedLED = *(params->data);
pc.printf("read data: %d\r\n",*(params->data));
}
}
/**
* This function is called when the ble initialization process has failled
*/
void onBleInitError(BLE &ble, ble_error_t error)
{
/* Initialization error handling should go here */
}
/**
* Callback triggered when the ble initialization process has finished
*/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
/* Ensure that it is the default instance of BLE */
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
/* Initialize BLE security */
bool enableBonding = true;
bool requireMITM = true;
ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY);
/* Set callback functions */
ble.gap().onConnection(connectionCallback);
ble.gap().onDisconnection(disconnectionCallback);
ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
ble.gattServer().onDataWritten(onDataWrittenCallback);
bool initialValueForLEDCharacteristic = false;
ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
/* setup advertising */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ble.gap().startAdvertising();
}
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
BLE &ble = BLE::Instance();
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
}
int main()
{
eventQueue.call_every(700, blinkCallback);
BLE &ble = BLE::Instance();
ble.onEventsToProcess(scheduleBleEventsProcessing);
ble.init(bleInitComplete);
pc.printf("[*] system start ..... \r\n");
eventQueue.dispatch_forever();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment