Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler
Revision 0:7809547930d9, committed 2015-02-11
- Comitter:
- ansond
- Date:
- Wed Feb 11 22:13:25 2015 +0000
- Child:
- 1:faa02c5fd781
- Commit message:
- initial BLE network support
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NSDL/libnsdl.lib Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ansond/code/libnsdl/#9cb268b96ffb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_support.cpp Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,202 @@
+// NSDL library support functions
+
+#include "mbed.h"
+#include "nsdl_support.h"
+
+#include "mbedConnectorInterface.h"
+
+// we have to redefine DBG as its used differently here...
+#ifdef DBG
+ #undef DBG
+#endif
+#define DBG std::printf
+
+#include "rtos.h"
+
+#include "UDPSocket.h"
+#include "Endpoint.h"
+
+Endpoint nsp;
+UDPSocket server;
+
+char null_endpoint_name[] = "";
+char null_domain[] = "";
+uint8_t null_ep_type[] = "";
+uint8_t null_lifetime_ptr[] = "";
+
+void *nsdl_alloc(uint16_t size) {
+ void *chunk = NULL;
+ if (size > 0) chunk = malloc(size);
+ if (chunk != NULL && size > 0) memset(chunk,0,size);
+ return chunk;
+}
+
+void nsdl_free(void* ptr_to_free) {
+ if (ptr_to_free != NULL) free(ptr_to_free);
+}
+
+/*
+ * Create a static resoure
+ * Only get is allowed
+ */
+void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
+{
+ resource_structure->access = SN_GRS_GET_ALLOWED;
+ resource_structure->mode = SN_GRS_STATIC;
+ resource_structure->pathlen = pt_len;
+ resource_structure->path = pt;
+ resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+ resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+ resource_structure->resource = rsc;
+ resource_structure->resourcelen = rsc_len;
+ sn_nsdl_create_resource(resource_structure);
+}
+
+void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
+{
+ resource_structure->access = (sn_grs_resource_acl_e)access_right;
+ resource_structure->resource = 0;
+ resource_structure->resourcelen = 0;
+ resource_structure->sn_grs_dyn_res_callback = callback_ptr;
+ resource_structure->mode = SN_GRS_DYNAMIC;
+ resource_structure->pathlen = pt_len;
+ resource_structure->path = pt;
+ resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+ resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+ resource_structure->resource_parameters_ptr->observable = is_observable;
+ sn_nsdl_create_resource(resource_structure);
+}
+
+sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t *domain, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) {
+ if (endpoint_structure == NULL) {
+ endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
+ }
+
+ if (endpoint_structure != NULL) {
+ memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
+ endpoint_structure->endpoint_name_ptr = name;
+ endpoint_structure->endpoint_name_len = strlen((char*)name);
+ endpoint_structure->domain_name_ptr = domain;
+ endpoint_structure->domain_name_len = strlen((char *)domain);
+ endpoint_structure->type_ptr = typename_ptr;
+ endpoint_structure->type_len = strlen((char*)typename_ptr);
+ endpoint_structure->lifetime_ptr = lifetime_ptr;
+ endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
+ }
+
+ return endpoint_structure;
+}
+
+void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) {
+ if (endpoint_structure != NULL) {
+ if (*endpoint_structure != NULL) {
+ nsdl_free(*endpoint_structure);
+ *endpoint_structure = NULL;
+ }
+ }
+}
+
+static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
+ //DBG("NSP: sending %d bytes...\r\n",data_len);
+ int sent = server.sendTo(nsp, (char*)data_ptr, data_len);
+ return 1;
+}
+
+static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) {
+ // Rx callback process it...
+ //DBG("NSP: received data. processing...\r\n");
+ return 0;
+}
+
+void register_endpoint(bool init) {
+ sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+ if (init) {
+ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+ if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+ DBG("NSP initial registration failed\r\n");
+ else
+ DBG("NSP initial registration OK\r\n");
+ }
+ else {
+ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_domain, (uint8_t*)null_endpoint_name, null_ep_type, null_lifetime_ptr);
+ if(sn_nsdl_update_registration(endpoint_ptr) != 0)
+ DBG("NSP re-registration failed\r\n");
+ else
+ DBG("NSP re-registration OK\r\n");
+ }
+ nsdl_clean_register_endpoint(&endpoint_ptr);
+}
+
+void registration_update_thread(void const *args) {
+ int count = -1;
+ int registration_time = 15; // about 30 seconds if iteration every 2 seconds
+
+ // first we want to wait a bit... let the endpoint crank up...
+ Thread::wait(NSP_RD_UPDATE_PERIOD); // wait about 30 seconds, then go ahead and start the re-registration interval...
+
+ // now loop this thread forever sleeping and re-registering at the right iteration.
+ while(true) {
+ Thread::wait(MAIN_LOOP_SLEEP);
+ ++count;
+ if (count%registration_time == 0) {
+ // re-registration time!
+ count = 0;
+ DBG("NSP: (re)registering...\r\n");
+ register_endpoint(false);
+ DBG("NSP: (re)registering complete.\r\n");
+ }
+ }
+}
+
+void nsdl_init() {
+ sn_nsdl_mem_s memory_cbs;
+
+ // initilize the UDP channel
+ server.init();
+ server.bind(nsp_port);
+
+ /* Initialize libNsdl */
+ memset(&memory_cbs,0,sizeof(memory_cbs));
+ memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
+ memory_cbs.sn_nsdl_free = &nsdl_free;
+ if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
+ DBG("NSP: libNsdl init failed.\r\n");
+ }
+ else {
+ DBG("NSP: libNsdl init successful.\r\n");
+ }
+}
+
+void nsdl_set_nsp_address(void) {
+ char NSP_address_str[16];
+
+ /* Set nsp address for library */
+ sprintf(NSP_address_str,"%d.%d.%d.%d",NSP_address_bytes[0],NSP_address_bytes[1],NSP_address_bytes[2],NSP_address_bytes[3]);
+ DBG("NSP: libNsdl NSP_ADDRESS: %s port: %d\r\n",NSP_address_str,nsp_port);
+ set_NSP_address(NSP_address_bytes, nsp_port, SN_NSDL_ADDRESS_TYPE_IPV4);
+ nsp.set_address(NSP_address_str,nsp_port);
+}
+
+// NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing...
+void nsdl_event_loop() {
+ sn_nsdl_addr_s received_packet_address;
+ Endpoint from;
+ uint8_t nsp_received_address[4];
+ char nsp_buffer[1024];
+
+ memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
+ memset(nsp_received_address, 0, sizeof(nsp_received_address));
+ received_packet_address.addr_ptr = nsp_received_address;
+
+ // start the registration update thread.. it will wait a bit while the endpoint gins up...
+ Thread registration_thread(registration_update_thread);
+
+ // FOREVER: main loop for event processing
+ while(true) {
+ //DBG("NSP: waiting for data...\r\n");
+ int n = server.receiveFrom(from,nsp_buffer,sizeof(nsp_buffer));
+
+ //DBG("NSP: received %d bytes... processing..\r\n.",n);
+ if (n >= 0) sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address);
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NSDL/nsdl_support.h Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,49 @@ +// Support functions for the NSDL library + +#ifndef __NSDL_SUPPORT_H__ +#define __NSDL_SUPPORT_H__ + +#include "mbed.h" +#include <stdint.h> + +#include "sn_nsdl.h" +#include "sn_coap_header.h" +#include "sn_coap_protocol.h" +#include "sn_nsdl_lib.h" + +#include "mbedConnectorInterface.h" + +// ************************* MDS CONFIGURATION ********************************* + +extern uint8_t NSP_address_bytes[NSP_IP_ADDRESS_LENGTH]; // which MDS instance we want to bind to... +extern uint8_t endpoint_name[NODE_NAME_LENGTH]; // our NODE name +extern uint8_t domain_name[NSP_DOMAIN_LENGTH]; // our MDS domain name +extern int nsp_port; // our MDS UDP port number +extern uint8_t ep_type[NSP_ENDPOINT_TYPE_LENGTH]; // our NODE type +extern uint8_t lifetime_ptr[NSP_LIFE_TIME_LENGTH]; // MDS lifetime + +// ************************* MDS CONFIGURATION ********************************* + + +#define MAIN_LOOP_SLEEP 2000 + +typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); +typedef void (*sn_update_observation_t)(sn_coap_hdr_s *,sn_coap_hdr_s *); + +// external methods +extern "C" void *nsdl_alloc(uint16_t size); +extern "C" void nsdl_free(void* ptr_to_free); +extern void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len); +extern void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right); +extern "C" sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t *domain, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr); +extern "C" void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure); +extern "C" void nsdl_init(); +extern "C" void nsdl_event_loop(); +extern "C" void nsdl_reg_update(); +extern "C" void configure_endpoint(); +extern void NSP_registration(); +extern "C" void register_endpoint(bool init); +extern void registration_update_thread(void const *args); +extern "C" void nsdl_set_nsp_address(void); + +#endif // __NSDL_SUPPORT_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bt_network/BLE_API.lib Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#1407d2f1ce3c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/Endpoint.cpp Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,48 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+#include <cstring>
+#include <cstdio>
+
+Endpoint::Endpoint() {
+ reset_address();
+}
+Endpoint::~Endpoint() {}
+
+void Endpoint::reset_address(void) {
+ // XXX
+ _ipAddress[0] = '\0';
+}
+
+#include "stdio.h"
+
+int Endpoint::set_address(const char* host, const int port) {
+ // XXX
+ return 0;
+}
+
+char* Endpoint::get_address() {
+ // XXX
+ return NULL;
+}
+
+int Endpoint::get_port() {
+ // XXX
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/Endpoint.h Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,61 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef ENDPOINT_H
+#define ENDPOINT_H
+
+class UDPSocket;
+
+/**
+IP Endpoint (address, port)
+*/
+class Endpoint {
+ friend class UDPSocket;
+
+public:
+ /** IP Endpoint (address, port)
+ */
+ Endpoint(void);
+
+ ~Endpoint(void);
+
+ /** Reset the address of this endpoint
+ */
+ void reset_address(void);
+
+ /** Set the address of this endpoint
+ \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS).
+ \param port The endpoint port
+ \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+ */
+ int set_address(const char* host, const int port);
+
+ /** Get the IP address of this endpoint
+ \return The IP address of this endpoint.
+ */
+ char* get_address(void);
+
+ /** Get the port of this endpoint
+ \return The port of this endpoint
+ */
+ int get_port(void);
+
+protected:
+ char _ipAddress[17];
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/Socket.cpp Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,62 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "Socket/Socket.h"
+#include <cstring>
+
+using std::memset;
+
+Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
+
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+ _blocking = blocking;
+ _timeout = timeout;
+}
+
+int Socket::init_socket(int type) {
+ // XXX
+ return 0;
+}
+
+int Socket::select(struct timeval *timeout, bool read, bool write) {
+ // XXX
+ return 0;
+}
+
+int Socket::wait_readable(TimeInterval& timeout) {
+ return select(&timeout._time, true, false);
+}
+
+int Socket::wait_writable(TimeInterval& timeout) {
+ return select(&timeout._time, false, true);
+}
+
+int Socket::close(bool shutdown) {
+ // XXX
+ return 0;
+}
+
+Socket::~Socket() {
+ close(); //Don't want to leak
+}
+
+TimeInterval::TimeInterval(unsigned int ms) {
+ _time.tv_sec = ms / 1000;
+ _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/Socket.h Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,94 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#include "bt_socket_stubs.h"
+
+class TimeInterval;
+
+/** Socket file descriptor and select wrapper
+ */
+class Socket {
+public:
+ /** Socket
+ */
+ Socket();
+
+ /** Set blocking or non-blocking mode of the socket and a timeout on
+ blocking socket operations
+ \param blocking true for blocking mode, false for non-blocking mode.
+ \param timeout timeout in ms [Default: (1500)ms].
+ */
+ void set_blocking(bool blocking, unsigned int timeout=1500);
+
+ /** Set socket options
+ \param level stack level (see: lwip/sockets.h)
+ \param optname option ID
+ \param optval option value
+ \param socklen_t length of the option value
+ \return 0 on success, -1 on failure
+ */
+ int set_option(int level, int optname, const void *optval, socklen_t optlen);
+
+ /** Get socket options
+ \param level stack level (see: lwip/sockets.h)
+ \param optname option ID
+ \param optval buffer pointer where to write the option value
+ \param socklen_t length of the option value
+ \return 0 on success, -1 on failure
+ */
+ int get_option(int level, int optname, void *optval, socklen_t *optlen);
+
+ /** Close the socket
+ \param shutdown free the left-over data in message queues
+ */
+ int close(bool shutdown=true);
+
+ ~Socket();
+
+protected:
+ int _sock_fd;
+ int init_socket(int type);
+
+ int wait_readable(TimeInterval& timeout);
+ int wait_writable(TimeInterval& timeout);
+
+ bool _blocking;
+ unsigned int _timeout;
+
+private:
+ int select(struct timeval *timeout, bool read, bool write);
+};
+
+/** Time interval class used to specify timeouts
+ */
+class TimeInterval {
+ friend class Socket;
+
+public:
+ /** Time Interval
+ \param ms time interval expressed in milliseconds
+ */
+ TimeInterval(unsigned int ms);
+
+private:
+ struct timeval _time;
+};
+
+#endif /* SOCKET_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/UDPSocket.cpp Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,89 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "Socket/UDPSocket.h"
+
+#include <cstring>
+
+using std::memset;
+
+UDPSocket::UDPSocket() {
+}
+
+int UDPSocket::init(void) {
+ // XXX
+ //return init_socket(SOCK_DGRAM);
+
+ return 0;
+}
+
+// Server initialization
+int UDPSocket::bind(int port) {
+ /* XXX
+ if (init_socket(SOCK_DGRAM) < 0)
+ return -1;
+
+ struct sockaddr_in localHost;
+ std::memset(&localHost, 0, sizeof(localHost));
+
+ localHost.sin_family = AF_INET;
+ localHost.sin_port = htons(port);
+ localHost.sin_addr.s_addr = INADDR_ANY;
+
+ if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
+ close();
+ return -1;
+ }
+ */
+
+ return 0;
+}
+
+// -1 if unsuccessful, else number of bytes written
+int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
+ if (_sock_fd < 0)
+ return -1;
+
+ if (!_blocking) {
+ TimeInterval timeout(_timeout);
+ if (wait_writable(timeout) != 0)
+ return 0;
+ }
+
+ // XXX return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
+ return 0;
+}
+
+// -1 if unsuccessful, else number of bytes received
+int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
+ /* XXX
+ if (_sock_fd < 0)
+ return -1;
+
+ if (!_blocking) {
+ TimeInterval timeout(_timeout);
+ if (wait_readable(timeout) != 0)
+ return 0;
+ }
+ remote.reset_address();
+ socklen_t remoteHostLen = sizeof(remote._remoteHost);
+ return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
+ */
+
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/UDPSocket.h Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,75 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UDPSOCKET_H
+#define UDPSOCKET_H
+
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+
+/**
+UDP Socket
+*/
+class UDPSocket : public Socket {
+
+public:
+ /** Instantiate an UDP Socket.
+ */
+ UDPSocket();
+
+ /** Init the UDP Client Socket without binding it to any specific port
+ \return 0 on success, -1 on failure.
+ */
+ int init(void);
+
+ /** Bind a UDP Server Socket to a specific port
+ \param port The port to listen for incoming connections on
+ \return 0 on success, -1 on failure.
+ */
+ int bind(int port);
+
+ /** Join the multicast group at the given address
+ \param address The address of the multicast group
+ \return 0 on success, -1 on failure.
+ */
+ int join_multicast_group(const char* address);
+
+ /** Set the socket in broadcasting mode
+ \return 0 on success, -1 on failure.
+ */
+ int set_broadcasting(bool broadcast=true);
+
+ /** Send a packet to a remote endpoint
+ \param remote The remote endpoint
+ \param packet The packet to be sent
+ \param length The length of the packet to be sent
+ \return the number of written bytes on success (>=0) or -1 on failure
+ */
+ int sendTo(Endpoint &remote, char *packet, int length);
+
+ /** Receive a packet from a remote endpoint
+ \param remote The remote endpoint
+ \param buffer The buffer for storing the incoming packet data. If a packet
+ is too long to fit in the supplied buffer, excess bytes are discarded
+ \param length The length of the buffer
+ \return the number of received bytes on success (>=0) or -1 on failure
+ */
+ int receiveFrom(Endpoint &remote, char *buffer, int length);
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bt_network/Socket/bt_socket_stubs.cpp Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,2 @@ +#include "bt_socket_stubs.h" +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bt_network/Socket/bt_socket_stubs.h Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,16 @@
+#ifndef __BT_SOCKET_STUBS_H__
+#define __BT_SOCKET_STUBS_H__
+
+#include "mbed.h"
+
+// timeval
+struct timeval {
+ uint32_t tv_sec;
+ uint32_t tv_usec;
+};
+
+
+// socket length is simply an unsigned int.
+typedef uint32_t socklen_t;
+
+#endif // __BT_SOCKET_STUBS_H__
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bt_network/mbed-rtos.lib Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ab4200083b07
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bt_network/nRF51822.lib Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#0e7a9efee6d7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configuration.h Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,9 @@ +#ifndef __CONFIGURATION_H_ +#define __CONFIGURATION_H_ + +/************** Ethernet CONFIGURATION PARAMETERS ************************/ + + +/************** Ethernet CONFIGURATION PARAMETERS ************************/ + +#endif // __CONFIGURATION_H_ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbedEndpointNetworkStubs.h Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,46 @@ + /** + * @file mbedEndpointNetworkStubs.h + * @brief mbed Endpoint Network Stubs header - these must be implemented in any lower network layer interfacing with mbedConnectorInterface + * @author Doug Anson + * @version 1.0 + * @see + * + * Copyright (c) 2014 + * + * 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 __MBED_ENDPOINT_NETWORK_STUBS_H__ + #define __MBED_ENDPOINT_NETWORK_STUBS_H__ + + #include "configuration.h" + #include "nsdl_support.h" + + // we have to redefine DBG as its used differently here... + #ifdef DBG + #undef DBG + #endif + #define DBG printf + + // Linkage to the lower network APIs and interfaces + extern "C" void net_stubs_pre_plumb_network(bool canActAsRouterNode); // before endpoint is configured... + extern "C" void net_stubs_post_plumb_network(void); // after endpoint is configured... + extern "C" void net_stubs_create_main_loop(void); + extern "C" void net_stubs_register_endpoint(); + extern "C" void net_stubs_begin_main_loop(void); + + // Utils.cpp functions... + extern void utils_configure_endpoint(void); + extern void utils_init_and_register_endpoint(void); + + #endif // __MBED_ENDPOINT_NETWORK_STUBS_H__ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/network_stubs/network_stubs.cpp Wed Feb 11 22:13:25 2015 +0000
@@ -0,0 +1,62 @@
+/**
+ * @file network_stubs.cpp
+ * @brief mbed Endpoint network stubs implementation (Ethernet)
+ * @author Doug Anson
+ * @version 1.0
+ * @see
+ *
+ * Copyright (c) 2014
+ *
+ * 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 "network_stubs.h"
+
+extern "C" {
+
+// plumb out the network
+void net_stubs_pre_plumb_network(bool canActAsRouterNode)
+{
+ // we have to defer all Ethernet until we are configured..
+ ;
+}
+
+// called after the endpoint is configured...
+void net_stubs_post_plumb_network(void)
+{
+}
+
+// create a suitable main event loop for this specific network
+void net_stubs_create_main_loop(void)
+{
+ // nothing to do for Ethernet endpoints - we are using the Threaded loop in nsdl_support.cpp
+ ;
+}
+
+// register the endpoint
+void net_stubs_register_endpoint(void)
+{
+ // NSP registration
+ DBG("net_stubs_register_endpoint: calling NSP registration...\r\n");
+ register_endpoint(true);
+ DBG("net_stubs_register_endpoint: NSP registration completed\r\n");
+}
+
+// begin the main loop for processing network events
+void net_stubs_begin_main_loop(void)
+{
+ // NDSL main loop
+ nsdl_event_loop();
+}
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/network_stubs/network_stubs.h Wed Feb 11 22:13:25 2015 +0000 @@ -0,0 +1,40 @@ +/** + * @file network_stubs.h + * @brief mbed Endpoint Network Stubs header (Ethernet) + * @author Doug Anson + * @version 1.0 + * @see + * + * Copyright (c) 2014 + * + * 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 __BT_STUBS_H__ +#define __BT_STUBS_H__ + +// mbed support +#include "mbed.h" + +// NSP support +#include "nsdl_support.h" + +// configure the endpoint +extern void configure_endpoint(); + +// we have to redefine DBG as its used differently here... +#ifdef DBG + #undef DBG +#endif +#define DBG printf + +#endif // __BT_STUBS_H__ \ No newline at end of file