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

Committer:
ansond
Date:
Sat Feb 14 07:36:17 2015 +0000
Revision:
2:30f4a0dab604
Parent:
0:7809547930d9
Child:
6:98af441fd960
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:7809547930d9 1 /* Copyright (C) 2012 mbed.org, MIT License
ansond 0:7809547930d9 2 *
ansond 0:7809547930d9 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:7809547930d9 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ansond 0:7809547930d9 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:7809547930d9 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:7809547930d9 7 * furnished to do so, subject to the following conditions:
ansond 0:7809547930d9 8 *
ansond 0:7809547930d9 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:7809547930d9 10 * substantial portions of the Software.
ansond 0:7809547930d9 11 *
ansond 0:7809547930d9 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:7809547930d9 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:7809547930d9 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:7809547930d9 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:7809547930d9 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:7809547930d9 17 */
ansond 0:7809547930d9 18 #include "Socket/Socket.h"
ansond 0:7809547930d9 19 #include <cstring>
ansond 0:7809547930d9 20
ansond 0:7809547930d9 21 using std::memset;
ansond 0:7809547930d9 22
ansond 2:30f4a0dab604 23 extern "C" void ble_build_disconnection_record();
ansond 2:30f4a0dab604 24 extern "C" void ble_send_disconnection_record();
ansond 2:30f4a0dab604 25
ansond 0:7809547930d9 26 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
ansond 0:7809547930d9 27
ansond 0:7809547930d9 28 }
ansond 0:7809547930d9 29
ansond 0:7809547930d9 30 void Socket::set_blocking(bool blocking, unsigned int timeout) {
ansond 0:7809547930d9 31 _blocking = blocking;
ansond 0:7809547930d9 32 _timeout = timeout;
ansond 0:7809547930d9 33 }
ansond 0:7809547930d9 34
ansond 0:7809547930d9 35 int Socket::init_socket(int type) {
ansond 2:30f4a0dab604 36 // Not used for BLE shim
ansond 0:7809547930d9 37 return 0;
ansond 0:7809547930d9 38 }
ansond 0:7809547930d9 39
ansond 0:7809547930d9 40 int Socket::select(struct timeval *timeout, bool read, bool write) {
ansond 2:30f4a0dab604 41 // Not used for BLE shim...
ansond 0:7809547930d9 42 return 0;
ansond 0:7809547930d9 43 }
ansond 0:7809547930d9 44
ansond 0:7809547930d9 45 int Socket::wait_readable(TimeInterval& timeout) {
ansond 0:7809547930d9 46 return select(&timeout._time, true, false);
ansond 0:7809547930d9 47 }
ansond 0:7809547930d9 48
ansond 0:7809547930d9 49 int Socket::wait_writable(TimeInterval& timeout) {
ansond 0:7809547930d9 50 return select(&timeout._time, false, true);
ansond 0:7809547930d9 51 }
ansond 0:7809547930d9 52
ansond 0:7809547930d9 53 int Socket::close(bool shutdown) {
ansond 2:30f4a0dab604 54 ble_build_disconnection_record();
ansond 2:30f4a0dab604 55 ble_send_disconnection_record();
ansond 0:7809547930d9 56 return 0;
ansond 0:7809547930d9 57 }
ansond 0:7809547930d9 58
ansond 0:7809547930d9 59 Socket::~Socket() {
ansond 0:7809547930d9 60 close(); //Don't want to leak
ansond 0:7809547930d9 61 }
ansond 0:7809547930d9 62
ansond 0:7809547930d9 63 TimeInterval::TimeInterval(unsigned int ms) {
ansond 0:7809547930d9 64 _time.tv_sec = ms / 1000;
ansond 0:7809547930d9 65 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
ansond 0:7809547930d9 66 }