BLE mbed Endpoint network stack for mbedConnectorInterface. The stack makes use of a special BLE Socket abstraction to create socket() semantics over BLE.
Dependencies: libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler
bt_network/Location/BLELocation.cpp@38:30e71f1206b1, 2015-11-03 (annotated)
- Committer:
- ansond
- Date:
- Tue Nov 03 17:03:51 2015 +0000
- Revision:
- 38:30e71f1206b1
- Parent:
- 36:aa73681951ad
merged with IoS and android tweaks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 32:8176cfb315f2 | 1 | /** |
ansond | 32:8176cfb315f2 | 2 | * @file BLEBLEBLELocation.cpp |
ansond | 32:8176cfb315f2 | 3 | * @brief mbed CoAP Endpoint BLE BLELocation class |
ansond | 32:8176cfb315f2 | 4 | * @author Doug Anson/Chris Paola |
ansond | 32:8176cfb315f2 | 5 | * @version 1.0 |
ansond | 32:8176cfb315f2 | 6 | * @see |
ansond | 32:8176cfb315f2 | 7 | * |
ansond | 32:8176cfb315f2 | 8 | * Copyright (c) 2014 |
ansond | 32:8176cfb315f2 | 9 | * |
ansond | 32:8176cfb315f2 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 32:8176cfb315f2 | 11 | * you may not use this file except in compliance with the License. |
ansond | 32:8176cfb315f2 | 12 | * You may obtain a copy of the License at |
ansond | 32:8176cfb315f2 | 13 | * |
ansond | 32:8176cfb315f2 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 32:8176cfb315f2 | 15 | * |
ansond | 32:8176cfb315f2 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 32:8176cfb315f2 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 32:8176cfb315f2 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 32:8176cfb315f2 | 19 | * See the License for the specific language governing permissions and |
ansond | 32:8176cfb315f2 | 20 | * limitations under the License. |
ansond | 32:8176cfb315f2 | 21 | */ |
ansond | 32:8176cfb315f2 | 22 | |
ansond | 32:8176cfb315f2 | 23 | #include "BLELocation.h" |
ansond | 33:4f6929e123f2 | 24 | |
ansond | 33:4f6929e123f2 | 25 | // UartRPCFunctions |
ansond | 33:4f6929e123f2 | 26 | #include "UartRPCFunctions.h" |
ansond | 33:4f6929e123f2 | 27 | |
ansond | 33:4f6929e123f2 | 28 | #ifdef DBG |
ansond | 33:4f6929e123f2 | 29 | #undef DBG |
ansond | 33:4f6929e123f2 | 30 | #endif |
ansond | 33:4f6929e123f2 | 31 | #define DBG std::printf |
ansond | 32:8176cfb315f2 | 32 | |
ansond | 32:8176cfb315f2 | 33 | // Constructor |
ansond | 32:8176cfb315f2 | 34 | BLELocation::BLELocation(const RawSerial *pc) : Connector::Location(pc) { |
ansond | 32:8176cfb315f2 | 35 | } |
ansond | 32:8176cfb315f2 | 36 | |
ansond | 32:8176cfb315f2 | 37 | // Destructor |
ansond | 32:8176cfb315f2 | 38 | BLELocation::~BLELocation() { |
ansond | 32:8176cfb315f2 | 39 | } |
ansond | 32:8176cfb315f2 | 40 | |
ansond | 33:4f6929e123f2 | 41 | // dispatch our location update request |
ansond | 33:4f6929e123f2 | 42 | void BLELocation::dispatchUpdateRequest() { |
ansond | 33:4f6929e123f2 | 43 | //this->m_pc->printf("BLELocation:dispatchUpdateRequest: dispatching location update request...\r\n"); |
ansond | 33:4f6929e123f2 | 44 | ble_rpc_get_location(this); |
ansond | 33:4f6929e123f2 | 45 | } |
ansond | 33:4f6929e123f2 | 46 | |
ansond | 36:aa73681951ad | 47 | // set the default location |
ansond | 36:aa73681951ad | 48 | void BLELocation::setDefault(float latitude,float longitude,float altitude,float speed) { |
ansond | 36:aa73681951ad | 49 | sprintf(this->m_latitude,"%.5f",latitude); |
ansond | 36:aa73681951ad | 50 | sprintf(this->m_longitude,"%.5f",longitude); |
ansond | 36:aa73681951ad | 51 | sprintf(this->m_msl_altitude_m,"%.1f",altitude); |
ansond | 36:aa73681951ad | 52 | sprintf(this->m_speed,"%.1f",speed); |
ansond | 36:aa73681951ad | 53 | } |
ansond | 36:aa73681951ad | 54 | |
ansond | 33:4f6929e123f2 | 55 | // process the location update response |
ansond | 33:4f6929e123f2 | 56 | void BLELocation::processLocationUpdateResponse(char *response,int response_length) { |
ansond | 36:aa73681951ad | 57 | float latitude = 0.0; |
ansond | 36:aa73681951ad | 58 | float longitude = 0.0; |
ansond | 36:aa73681951ad | 59 | float altitude = 0.0; |
ansond | 36:aa73681951ad | 60 | float speed = 0.0; |
ansond | 36:aa73681951ad | 61 | bool present = false; |
ansond | 36:aa73681951ad | 62 | |
ansond | 36:aa73681951ad | 63 | if (response != NULL && response_length > 0) { |
ansond | 33:4f6929e123f2 | 64 | //DBG("BLELocation:processLocationUpdateResponse: data(%d): %s\r\n",response_length,response); |
ansond | 33:4f6929e123f2 | 65 | for(int i=0;i<response_length;++i) { |
ansond | 36:aa73681951ad | 66 | if (response[i] == ':') { |
ansond | 36:aa73681951ad | 67 | response[i] = ' '; |
ansond | 36:aa73681951ad | 68 | present = true; |
ansond | 36:aa73681951ad | 69 | } |
ansond | 33:4f6929e123f2 | 70 | } |
ansond | 36:aa73681951ad | 71 | |
ansond | 36:aa73681951ad | 72 | // fill with defaults if not presented with new ones... |
ansond | 36:aa73681951ad | 73 | if (!present) { |
ansond | 36:aa73681951ad | 74 | sprintf(response,"%s %s %s %s",this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed); |
ansond | 36:aa73681951ad | 75 | } |
ansond | 36:aa73681951ad | 76 | |
ansond | 36:aa73681951ad | 77 | // parse |
ansond | 33:4f6929e123f2 | 78 | sscanf((char *)response,"%f%f%f%f",&latitude,&longitude,&altitude,&speed); |
ansond | 36:aa73681951ad | 79 | this->setDefault(latitude,longitude,altitude,speed); |
ansond | 33:4f6929e123f2 | 80 | |
ansond | 33:4f6929e123f2 | 81 | // check for disabled location |
ansond | 33:4f6929e123f2 | 82 | if (latitude < 1.0 && longitude < 1.0 && speed < 0.0 && altitude < 0) { |
ansond | 33:4f6929e123f2 | 83 | DBG("BLELocation: Location reporting is DISABLED in UART_PROXY...\r\n"); |
ansond | 33:4f6929e123f2 | 84 | ::Connector::Location::initBuffers(); |
ansond | 33:4f6929e123f2 | 85 | } |
ansond | 36:aa73681951ad | 86 | else if (!present) { |
ansond | 36:aa73681951ad | 87 | // using cached values |
ansond | 36:aa73681951ad | 88 | DBG("BLELocation (CACHED): latitude=%s longtiude=%s altitude=%s speed=%s\r\n", |
ansond | 36:aa73681951ad | 89 | this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed); |
ansond | 36:aa73681951ad | 90 | } |
ansond | 33:4f6929e123f2 | 91 | else { |
ansond | 36:aa73681951ad | 92 | // latest values |
ansond | 36:aa73681951ad | 93 | DBG("BLELocation (CURRENT): latitude=%s longtiude=%s altitude=%s speed=%s\r\n", |
ansond | 33:4f6929e123f2 | 94 | this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed); |
ansond | 33:4f6929e123f2 | 95 | } |
ansond | 33:4f6929e123f2 | 96 | } |
ansond | 33:4f6929e123f2 | 97 | else { |
ansond | 33:4f6929e123f2 | 98 | // no reponse |
ansond | 33:4f6929e123f2 | 99 | DBG("BLELocation: EMPTY response received! Assuming DISABLED.\r\n"); |
ansond | 33:4f6929e123f2 | 100 | ::Connector::Location::initBuffers(); |
ansond | 33:4f6929e123f2 | 101 | } |
ansond | 33:4f6929e123f2 | 102 | } |
ansond | 33:4f6929e123f2 | 103 | |
ansond | 32:8176cfb315f2 | 104 | // update our location |
ansond | 32:8176cfb315f2 | 105 | void BLELocation::updateLocation() { |
ansond | 33:4f6929e123f2 | 106 | // dispatch an update request |
ansond | 33:4f6929e123f2 | 107 | this->dispatchUpdateRequest(); |
ansond | 32:8176cfb315f2 | 108 | } |