examples: Remove heap allocation from C++ examples

Cleanup of UPM C++ examples.  Switched from heap allocation to
stack allocation when possible.  This simplifies the samples since it
removes the need for explicit memory management.  A script was used to
identify and replace pointer use.  To simplify the replace script, I
re-formatted the C++ examples using the UPM .clang-format file.
Unfortuantely this changes the look of the UPM C++ examples to a large
degree.  However, examples will now have a standard look/feel and
uniform formatting.

    * Ran clang-format w/provided UPM .clang-format file
    * Removed new's/delete's whenever possible (left those in interface
      examples)
    * Added IIO sensor library implementation of callback void* arg
    * Converted all sleeps to upm defined delays (added header when
      necessary)
    * Scrubbed CXX example includes

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2017-08-30 15:00:29 -07:00
committed by Mihai Tudor Panu
parent bd6e4ec786
commit 5cefe7f5f3
290 changed files with 7976 additions and 8520 deletions

View File

@ -22,21 +22,25 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include "nrf8001.hpp"
#include "nrf8001-broadcast.hpp"
#include <lib_aci.h>
#include <aci_setup.h>
#include <signal.h>
#include <assert.h>
#include <iostream>
#include <lib_aci.h>
#include <signal.h>
#include <stdio.h>
#include "aci_evts.h"
#include "hal_aci_tl.h"
#include "nrf8001-broadcast.hpp"
#include "nrf8001.hpp"
#include "upm_utilities.h"
#ifdef SERVICES_PIPE_TYPE_MAPPING_CONTENT
static services_pipe_type_mapping_t
services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] =
SERVICES_PIPE_TYPE_MAPPING_CONTENT;
#else
#define NUMBER_OF_PIPES 0
static services_pipe_type_mapping_t * services_pipe_type_mapping = NULL;
#define NUMBER_OF_PIPES 0
static services_pipe_type_mapping_t* services_pipe_type_mapping = NULL;
#endif
/**
@ -48,7 +52,7 @@ static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT;
* aci_struct that will contain
* total initial credits
* current credit
* current state of the aci (setup/standby/active/sleep)
* current state of the aci (setup/standby/active/upm_delay)
* open remote pipe pending
* close remote pipe pending
* Current pipe available bitmap
@ -62,7 +66,7 @@ static struct aci_state_t aci_state;
/**
* Temporary buffers for sending ACI commands
*/
static hal_aci_evt_t aci_data;
static hal_aci_evt_t aci_data;
void
sig_handler(int signo)
@ -74,90 +78,93 @@ sig_handler(int signo)
}
void
init_aci_setup () {
init_aci_setup()
{
/**
* Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001
*/
* Point ACI data structures to the the setup data that the nRFgo studio
* generated for the nRF8001
*/
// abort if this is NULL
assert(services_pipe_type_mapping != NULL);
aci_state.aci_setup_info.services_pipe_type_mapping =
&services_pipe_type_mapping[0];
aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
aci_state.aci_setup_info.setup_msgs = setup_msgs;
aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
aci_state.aci_setup_info.number_of_pipes = NUMBER_OF_PIPES;
aci_state.aci_setup_info.setup_msgs = setup_msgs;
aci_state.aci_setup_info.num_setup_msgs = NB_SETUP_MESSAGES;
}
int
main(int argc, char **argv)
main(int argc, char** argv)
{
//! [Interesting]
init_aci_setup ();
init_local_interfaces (&aci_state, 10, 8, 4);
init_aci_setup();
init_local_interfaces(&aci_state, 10, 8, 4);
while (1) {
static bool setup_required = false;
if (lib_aci_event_get (&aci_state, &aci_data)) {
aci_evt_t * aci_evt;
if (lib_aci_event_get(&aci_state, &aci_data)) {
aci_evt_t* aci_evt;
aci_evt = &aci_data.evt;
switch(aci_evt->evt_opcode) {
switch (aci_evt->evt_opcode) {
/**
As soon as you reset the nRF8001 you will get an ACI Device Started Event
*/
As soon as you reset the nRF8001 you will get an ACI Device Started Event
*/
case ACI_EVT_DEVICE_STARTED: {
aci_state.data_credit_available = aci_evt->params.device_started.credit_available;
switch(aci_evt->params.device_started.device_mode) {
aci_state.data_credit_available =
aci_evt->params.device_started.credit_available;
switch (aci_evt->params.device_started.device_mode) {
case ACI_DEVICE_SETUP:
/**
When the device is in the setup mode
*/
printf ("Evt Device Started: Setup\n");
When the device is in the setup mode
*/
printf("Evt Device Started: Setup\n");
setup_required = true;
break;
break;
case ACI_DEVICE_STANDBY:
printf ("Evt Device Started: Standby\n");
lib_aci_broadcast(10/* in seconds */, 0x0100 /* advertising interval 100ms */);
printf ("Broadcasting started\n");
break;
printf("Evt Device Started: Standby\n");
lib_aci_broadcast(10 /* in seconds */,
0x0100 /* advertising interval 100ms */);
printf("Broadcasting started\n");
break;
default:
break;
break;
}
}
break; //ACI Device Started Event
} break; // ACI Device Started Event
case ACI_EVT_CMD_RSP:
if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) {
printf ("ACI_EVT_CMD_RSP\n");
while (1);
printf("ACI_EVT_CMD_RSP\n");
while (1)
;
}
break;
break;
case ACI_EVT_CONNECTED:
printf ("ACI_EVT_CONNECTED\n");
printf("ACI_EVT_CONNECTED\n");
break;
case ACI_EVT_PIPE_STATUS:
printf ("ACI_EVT_PIPE_STATUS\n");
printf("ACI_EVT_PIPE_STATUS\n");
break;
case ACI_EVT_DISCONNECTED:
if (ACI_STATUS_ERROR_ADVT_TIMEOUT == aci_evt->params.disconnected.aci_status) {
printf ("Broadcasting timed out\n");
printf("Broadcasting timed out\n");
} else {
printf ("Evt Disconnected. Link Loss\n");
printf("Evt Disconnected. Link Loss\n");
}
break;
case ACI_EVT_DATA_RECEIVED:
printf ("ACI_EVT_DATA_RECEIVED\n");
printf("ACI_EVT_DATA_RECEIVED\n");
break;
case ACI_EVT_HW_ERROR:
printf ("ACI_EVT_HW_ERROR\n");
printf("ACI_EVT_HW_ERROR\n");
break;
default:
break;
@ -169,10 +176,10 @@ main(int argc, char **argv)
setup_required = false;
}
}
usleep (100);
upm_delay_us(100);
}
close_local_interfaces (&aci_state);
close_local_interfaces(&aci_state);
//! [Interesting]