![](/media/cache/profiles/profile2.jpg.50x50_q85.jpg)
This software setup a central node of a star topology network
Dependencies: MQTT target_st_bluenrg
Fork of ble-star-mbed by
Diff: source/main.cpp
- Revision:
- 0:1902469bdd2d
- Child:
- 1:110b5e896bc9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main.cpp Tue Feb 20 11:21:41 2018 +0000 @@ -0,0 +1,152 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2015 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 "ble/DiscoveredCharacteristic.h" +#include "ble/DiscoveredService.h" +#include <UUID.h> +#include <BleMasterService.h> +#include <BleSlaveService.h> + +const char NAME_BLESTAR1[] = "BleStar1"; + +/*----------------------------------------------------------------------------*/ + + + + +/* scheduleBleEventsProcessing */ +void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { + BLE &ble = BLE::Instance(); + eventQ.call(Callback<void()>(&ble, &BLE::processEvents)); +} +/*----------------------------------------------------------------------------*/ +/* Complete the initialization of ble module */ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ + + + initProcess(); + ble_error_t a0, a1, a2, a3; + + 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; + } + + printf("\r\nBLE Init Completed\n"); + + + /* notification */ + //ble.gattServer().onUpdatesEnabled(onUpdatesEnabledCallback); + //ble.gattServer().onUpdatesDisabled(onUpdatesDisabledCallback); + + /* notification + attr writing */ + ble.gattServer().onDataWritten(AttributeModified_CB); + /* data read */ + ble.gattClient().onDataRead(readCharacteristicCallback); + /* when a peripheral node characteristics change */ + ble.gattClient().onHVX(onNotificationCallback); + /* when a peripheral descriptor is written */ + ble.gattClient().onDataWritten(perDescriptorWrittenCallback); + + + /* disconnection */ + ble.gap().onDisconnection(disconnectionCallback); + /* connection */ + ble.gap().onConnection(connectionCallback); + ble.gap().setScanParams(600, 200); //(scanInterval,scanWindow)ms + ble.gap().setScanTimeout(0x0003); //stop scanning after N sec + ble.gap().onTimeout(onStopScan); //callback when scan stops + + + + + /* Setup adv */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + a0 = ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, manuf_data, sizeof(manuf_data)); + a1 = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::UNKNOWN); + a2 = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)NAME_BLESTAR1, sizeof(NAME_BLESTAR1)); + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); //Advertising_Event_Type + ble.gap().setAdvertisingInterval(1000); //Adv_Interval + a3 = ble.gap().setAdvertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST); //Adv_Filter_Policy + if ((a0 != BLE_ERROR_NONE) || (a1 != BLE_ERROR_NONE) || (a2 != BLE_ERROR_NONE) || (a3 != BLE_ERROR_NONE)){ + printf("\r\nError setup ADV\n"); + } + + + addAllServices(); + printMacAddress(); + + + /* Start connection, service/chars discovery and enable notification */ + connectionProcess(); + + /* Start advertising from this point */ + //setSlaveDiscoverable();//DEBUG_ONLY + +} +/*----------------------------------------------------------------------------*/ + + + +void onBleInitError(BLE &ble, ble_error_t error) {} +/*----------------------------------------------------------------------------*/ + + + +int main() +{ + printf("\r\n\n/*******************************************************\n"); + printf("\r* *\n"); + printf("\r* FP-NET-BLESTAR1 (MBED) Expansion Software *\n"); + printf("\r* *\n"); + printf("\r*******************************************************/\n\n\n"); + + /* Create the ble instance */ + BLE &ble = BLE::Instance(); + + ble.onEventsToProcess(scheduleBleEventsProcessing); + + /* Uncommenting to debug the status*/ + //eventQ.call_every(20000, checkStatus); + + ble.init(bleInitComplete); + + + //dispatch events + eventQ.dispatch_forever(); + + return 0; +} +/*----------------------------------------------------------------------------*/ + + + + +