aboutsummaryrefslogtreecommitdiff
path: root/trygvisio_soil_moisture.ino
blob: 929f220dc1bce048b0bd1453cf2584a76d22bce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// See config.h on how to configure the sketch
#include "config-check.h"

#ifdef USE_LOW_POWER_MODE
#include <LowPower.h>
#endif
#ifdef PERSISTENT_CONFIGURATION_SUPPORT
#include <EEPROM.h>
#endif
#include <AltSoftSerial.h>
#include <SPI.h>

#include <lib_aci.h>
#include <aci_setup.h>
#include <aci_evts.h>

#include "services.h"
#include "app.h"
#include "Debug.h"

#ifdef PIPE_SOIL_MOISTURE_INTERMEDIATE_TEMPERATURE_SET
#define FEATURE_TEMPERATURE
#endif

#ifdef PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET
//#define FEATURE_HARDWARE_REVISION
#endif

#ifdef PIPE_BATTERY_BATTERY_LEVEL_SET
#define FEATURE_BATTERY_LEVEL
#endif

#define xx(s) x(s)
#define x(s) #s

#ifdef PIPE_DEVICE_INFORMATION_MODEL_NUMBER_STRING_SET
#define FEATURE_MODEL_NUMBER
const char model_number[] = "Board type id: " xx(SM_BOARD_VERSION);
#endif

#ifdef PIPE_DEVICE_INFORMATION_FIRMWARE_REVISION_STRING_SET
#define FEATURE_FIRMWARE_REVISION

#ifdef FIRMWARE_REVISION
const char firmware_revision[] = FIRMWARE_REVISION;
#else
const char firmware_revision[] = "Arduino: " __DATE__;
#endif

#endif // PIPE_DEVICE_INFORMATION_FIRMWARE_REVISION_STRING_SET

#undef x
#undef xx

#ifdef USE_LOW_POWER
static const bool use_low_power=1;
#else
static const bool use_low_power=0;
#endif

static void setup_rf();
static void show_pipes();

static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
static const hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] PROGMEM               = SETUP_MESSAGES_CONTENT;

// Pipes
static const uint8_t sm_pipe_set = PIPE_SOIL_MOISTURE_SOIL_MOISTURE_CONTROL_SET;
static const uint8_t sm_pipe_tx  = PIPE_SOIL_MOISTURE_SOIL_MOISTURE_CONTROL_TX;
static const uint8_t sm_pipe_rx  = PIPE_SOIL_MOISTURE_SOIL_MOISTURE_CONTROL_RX_ACK_AUTO;

static struct aci_state_t aci_state;
static hal_aci_evt_t aci_data;
static boolean timing_change_done = false;

void __ble_assert(const char *file, uint16_t line)
{
  debug.print("ERROR ");
  debug.print(file);
  debug.print(": ");
  debug.print(line);
  debug.print("\n");
  while (1) {};
}

static void go_to_sleep();

void setup() {
#ifdef TXLED0
  // An attempt to make sure that the RX and TX LEDs are turned off when running normally.
  TXLED0;
  TXLED1;
#endif
#if defined(BLEND_MICRO_8MHZ)
  // As the F_CPU = 8000000UL, the USB core make the PLLCSR = 0x02
  // But the external xtal is 16000000Hz, so correct it here.
  PLLCSR |= 0x10;             // Need 16 MHz xtal
  while (!(PLLCSR & (1<<PLOCK))) {}    // wait for lock pll
#elif defined(BLEND_MICRO_16MHZ)
  // The CPU clock in bootloader is 8MHz, change to 16MHz for sketches to run (i.e. overclock running at 3.3v).
  CLKPR = 0x80;
  CLKPR = 0;
#endif

  if (debug.sink == DEBUG_SINK_SERIAL) {
    debug.begin(115200);
  } else if (debug.sink == DEBUG_SINK_SOFTWARE_SERIAL) {
    debug.begin(9600);  
  }

#if WAIT_FOR_SERIAL_BEFORE_STARING
  // Wait until the serial port is available (useful only for the Leonardo)
  // As the Leonardo board is not reseted every time you open the Serial Monitor
#if defined (__AVR_ATmega32U4__)
  while (!debug) {}

  delay(1000);
#elif defined(__PIC32MX__)
  delay(1000);
#endif

#endif // WAIT_FOR_SERIAL_BEFORE_STARING

  setup_rf();
  sm_setup();
}

static void setup_rf() {
  debug.println(F("setup_rf()"));
  // Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001
  if (NULL != services_pipe_type_mapping) {
    aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0];
  }
  else {
    aci_state.aci_setup_info.services_pipe_type_mapping = NULL;
  }
  aci_state.aci_setup_info.number_of_pipes    = NUMBER_OF_PIPES;
  aci_state.aci_setup_info.setup_msgs         = (hal_aci_data_t*)setup_msgs;
  aci_state.aci_setup_info.num_setup_msgs     = NB_SETUP_MESSAGES;

  // Tell the ACI library, the MCU to nRF8001 pin connections.
  // The Active pin is optional and can be marked UNUSED

  // SPI_CLOCK_DIV8  = 2MHz SPI speed
  // SPI_CLOCK_DIV16 = 1MHz SPI speed

#if defined(ARDUINO_BLEND_MICRO)
  aci_state.aci_pins.board_name = REDBEARLAB_SHIELD_V1_1;
  aci_state.aci_pins.reqn_pin   = 6;
  aci_state.aci_pins.rdyn_pin   = 7;
  aci_state.aci_pins.mosi_pin   = MOSI;
  aci_state.aci_pins.miso_pin   = MISO;
  aci_state.aci_pins.sck_pin    = SCK;

  aci_state.aci_pins.spi_clock_divider      = SPI_CLOCK_DIV8;
  aci_state.aci_pins.reset_pin              = UNUSED;
  aci_state.aci_pins.active_pin             = UNUSED;
  aci_state.aci_pins.optional_chip_sel_pin  = UNUSED;

  aci_state.aci_pins.interface_is_interrupt = false;
  aci_state.aci_pins.interrupt_number       = 4;
#elif defined(ARDUINO_AVR_LEONARDO)
  // Assumes OLIMEXINO-32U4 and UEXT
  aci_state.aci_pins.board_name = BOARD_DEFAULT;
  aci_state.aci_pins.reqn_pin   = 2;
  aci_state.aci_pins.rdyn_pin   = 13;
  aci_state.aci_pins.mosi_pin   = MOSI;
  aci_state.aci_pins.miso_pin   = MISO;
  aci_state.aci_pins.sck_pin    = SCK;

  aci_state.aci_pins.spi_clock_divider      = SPI_CLOCK_DIV8;
  aci_state.aci_pins.reset_pin              = 3;
  aci_state.aci_pins.active_pin             = UNUSED;
  aci_state.aci_pins.optional_chip_sel_pin  = UNUSED;

  aci_state.aci_pins.interface_is_interrupt = false;
//  aci_state.aci_pins.interrupt_number       = 4;
#elif defined(ARDUINO_attiny)
#else
#error unknown board type
#endif

  // We reset the nRF8001 here by toggling the RESET line connected to the nRF8001
  // If the RESET line is not available we call the ACI Radio Reset to soft reset the nRF8001
  // then we initialize the data structures required to setup the nRF8001
  // The second parameter is for turning debug printing on for the ACI Commands and Events so they be printed on the Serial
  lib_aci_init(&aci_state, false);
  debug.println(F("lib_aci_init done"));
}

static bool rf_started     = false;
static bool setup_required = false;

static void aci_loop() {
  uint8_t pipe_number;
//  int ret;

  // We enter the if statement only when there is a ACI event available to be processed
  if (lib_aci_event_get(&aci_state, &aci_data)) {
    aci_evt_t * aci_evt;
    aci_evt = &aci_data.evt;

    switch (aci_evt->evt_opcode) {
    case ACI_EVT_DEVICE_STARTED:
      debug.println(F("ACI_EVT_DEVICE_STARTED"));
      aci_state.data_credit_total = aci_evt->params.device_started.credit_available;

      debug.print(F("aci_state.data_credit_total="));
      debug.println(aci_state.data_credit_total, DEC);

      switch (aci_evt->params.device_started.device_mode) {
      case ACI_DEVICE_SETUP:
        debug.println(F("ACI_DEVICE_SETUP"));
        rf_started     = true;
        setup_required = true;
        break;

      case ACI_DEVICE_STANDBY:
        debug.println(F("ACI_DEVICE_STANDBY"));
        if (aci_evt->params.device_started.hw_error) {
          delay(20); // Magic number used to make sure the HW error event is handled correctly.
        }
        else {
#ifdef FEATURE_HARDWARE_REVISION
          lib_aci_device_version();
          debug.println(F("Requesting device version."));
#endif

#ifdef FEATURE_MODEL_NUMBER
          debug.print(F("Model number: "));
          debug.println(model_number);

          size_t model_number_size = min(PIPE_DEVICE_INFORMATION_MODEL_NUMBER_STRING_SET_MAX_SIZE,
            sizeof(model_number));
          lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_MODEL_NUMBER_STRING_SET,
            (uint8_t *) model_number, model_number_size);
#endif


#ifdef FEATURE_FIRMWARE_REVISION
          debug.print(F("Firmware revision: "));
          debug.println(firmware_revision);

          size_t firmware_revision_size = min(PIPE_DEVICE_INFORMATION_FIRMWARE_REVISION_STRING_SET_MAX_SIZE,
            sizeof(firmware_revision));
          lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_FIRMWARE_REVISION_STRING_SET,
            (uint8_t *) firmware_revision, firmware_revision_size);
#endif

          lib_aci_connect(180 /* in seconds */, 0x0050 /* advertising interval 50ms*/);
          // lib_aci_broadcast(10/* in seconds */, 0x0100 /* advertising interval 100ms */);
//          ret = lib_aci_open_adv_pipe(PIPE_BATTERY_BATTERY_LEVEL_BROADCAST);
//          ret = lib_aci_open_adv_pipe(PIPE_SOIL_MOISTURE_SOIL_MOISTURE_LEVEL_BROADCAST);
          debug.println(F("Advertising started"));
        }
        break;
      case ACI_DEVICE_INVALID:
      case ACI_DEVICE_TEST:
      case ACI_DEVICE_SLEEP:
        // Ignored
        break;
      }
      break;

    case ACI_EVT_CMD_RSP:
      // debug.println(F("ACI_EVT_CMD_RSP"));
      // debug.print(F("aci_evt->params.cmd_rsp.cmd_opcode="));
      // debug.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX);
      // debug.print(F("aci_evt->params.cmd_rsp.cmd_status="));
      // debug.println(aci_evt->params.cmd_rsp.cmd_status, HEX);

      //If an ACI command response event comes with an error -> stop
      if (aci_evt->params.cmd_rsp.cmd_status != ACI_STATUS_SUCCESS) {
        //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of
        //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE
        //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command//
        // debug.print(F("ACI Command "));
        // debug.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX);
        // debug.print(F("Evt Cmd respone: Status "));
        // debug.println(aci_evt->params.cmd_rsp.cmd_status, HEX);
      }
#ifdef FEATURE_HARDWARE_REVISION
      if (aci_evt->params.cmd_rsp.cmd_opcode == ACI_CMD_GET_DEVICE_VERSION) {
        debug.println(F("Got device version."));
        //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic
        lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
          (uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version),
          sizeof(aci_evt_cmd_rsp_params_get_device_version_t));
      }
#endif // FEATURE_HARDWARE_REVISION

#ifdef FEATURE_TEMPERATURE
      if (aci_evt->params.cmd_rsp.cmd_opcode == ACI_CMD_GET_TEMPERATURE) {
        debug.print(F("aci_evt->params.cmd_rsp.params.get_temperature="));
        debug.print(aci_evt->params.cmd_rsp.params.get_temperature.temperature_value, DEC);
        debug.println();

        int32_t t = aci_evt->params.cmd_rsp.params.get_temperature.temperature_value;

        // t is number of 1/4 degrees celcius.

        // Multiply t by 25 without having to include float support
        t = t * 16 + t * 8 + t;

        uint8_t exponent = -2;

        // example. reading=111. real temperature = 111 / 4 = 27.75 dec C
        // Calculation: t = 111, formula: t*25 * 10^exp => 2775 * 10^-2 = 27.75

        // The value of flags
        // bit 0: 0=Celcius, 1=Farenheight
        // bit 1: 0=time stamp field not present, 1=present
        // bit 2: 0=temperature type field not present, 1=present
        // bit 3-7: reserved
        // Reference: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
        struct {
          uint8_t  flags;
          uint32_t value;
          // time stamp
          // temperature type
        } temperature_measurement = {
          0,
          ((uint32_t)exponent) << 24 | (t & 0x00FFFFFF)
        };

        lib_aci_set_local_data(&aci_state, PIPE_SOIL_MOISTURE_INTERMEDIATE_TEMPERATURE_SET,
            (uint8_t *) &temperature_measurement,
            sizeof(temperature_measurement));
      }
#endif // FEATURE_TEMPERATURE
      break;

    case ACI_EVT_CONNECTED:
      debug.println(F("ACI_EVT_CONNECTED"));
      timing_change_done              = false;
      aci_state.data_credit_available = aci_state.data_credit_total;
      debug.print(F("aci_state.data_credit_available="));
      debug.println(aci_state.data_credit_available, DEC);

#ifdef FEATURE_TEMPERATURE
      lib_aci_get_temperature();
#endif
      sm_on_connect();
      break;

    case ACI_EVT_PIPE_STATUS:
      debug.println(F("ACI_EVT_PIPE_STATUS"));
      show_pipes();
      break;

    case ACI_EVT_TIMING:
      debug.println(F("ACI_EVT_TIMING"));
      break;

    case ACI_EVT_DISCONNECTED:
      debug.println(F("ACI_EVT_DISCONNECTED"));

      lib_aci_connect(180 /* in seconds */, 0x0100 /* advertising interval 100ms*/);
      debug.println(F("Advertising started"));

      sm_on_disconnect();
      break;

    case ACI_EVT_DATA_RECEIVED:
      pipe_number = aci_evt->params.data_received.rx_data.pipe_number;

//      debug.print(F("ACI_EVT_DATA_RECEIVED: pipe_number="));
//      debug.println(pipe_number, DEC);

      if (pipe_number == sm_pipe_rx) {
        on_soil_moisture_ctrl(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len);
      }
      break;

    case ACI_EVT_DATA_CREDIT:
      debug.println(F("ACI_EVT_DATA_CREDIT"));
      aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
      debug.print(F("aci_state.data_credit_available="));
      debug.println(aci_state.data_credit_available, DEC);
      break;

    case ACI_EVT_PIPE_ERROR:
      debug.println(F("ACI_EVT_PIPE_ERROR"));
      //See the appendix in the nRF8001 Product Specication for details on the error codes
      debug.print(F("ACI Evt Pipe Error: Pipe #:"));
      debug.print(aci_evt->params.pipe_error.pipe_number, DEC);
      debug.print(F("  Pipe Error Code: 0x"));
      debug.println(aci_evt->params.pipe_error.error_code, HEX);

      // Increment the credit available as the data packet was not sent.
      // The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted
      // for the credit.
      if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) {
        aci_state.data_credit_available++;
      }
      break;

    case ACI_EVT_HW_ERROR:
      debug.println(F("ACI_EVT_HW_ERROR"));
      debug.print(F("HW error: "));
      debug.println(aci_evt->params.hw_error.line_num, DEC);

      for (uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++) {
        debug.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20];
      }
      debug.println();
      lib_aci_connect(180 /* in seconds */, 0x0050 /* advertising interval 50ms*/);
      debug.println(F("Advertising started"));
      break;

    case ACI_EVT_INVALID:
      debug.println(F("ACI_EVT_INVALID"));
      break;
    case ACI_EVT_ECHO:
      debug.println(F("ACI_EVT_ECHO"));
      break;
    case ACI_EVT_BOND_STATUS:
      debug.println(F("ACI_EVT_BOND_STATUS"));
      break;
    case ACI_EVT_DATA_ACK:
      debug.println(F("ACI_EVT_DATA_ACK"));
      break;
    case ACI_EVT_DISPLAY_PASSKEY:
      debug.println(F("ACI_EVT_DISPLAY_PASSKEY"));
      break;
    case ACI_EVT_KEY_REQUEST:
      debug.println(F("ACI_EVT_KEY_REQUEST"));
      break;
    }
  }
  else {
    // debug.println(F("No ACI Events available"));
    // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep
    // Arduino can go to sleep now
    // Wakeup from sleep from the RDYN line
  }

  /* setup_required is set to true when the device starts up and enters setup mode.
   * It indicates that do_aci_setup() should be called. The flag should be cleared if
   * do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE.
   */
  if (setup_required) {
    int ret = do_aci_setup(&aci_state);
    debug.print(F("do_aci_setup ret="));
    debug.println(ret, DEC);
    if (SETUP_SUCCESS == ret) {
      setup_required = false;
    }
  }
}

static void go_to_sleep() {
#ifdef USE_LOW_POWER
#if defined(__AVR_ATmega32U4__)
  LowPower.idle(SLEEP_1S, ADC_OFF, TIMER4_OFF, TIMER3_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART1_OFF, TWI_OFF, USB_OFF);
}
#else
#  warning No sleep support for current CPU architecture.
#endif
#endif // USE_LOW_POWER
}

static uint8_t value = 0;
void loop() {
  static unsigned long last = 0, now;

  aci_loop();

  if (debug.available()) {
    debug.write(debug.read());
  }

  now = millis();
  if (now - last > 3000) {
    last = now;

    if (!rf_started) {
      static int count            = 0;
      static bool reset_attempted = false;
      count++;

      if (!reset_attempted) {
        if (count == 3) {
          reset_attempted = true;
          debug.println(F("RF did not start, resetting RF"));

          // asm volatile ("jmp 0");
          // lib_aci_pin_reset();
          setup_rf();
          count = 0;
          return;
        } else {
          debug.println(F("waiting for RF to start"));
        }
      }
      /**/
    }
    else if (!setup_required) {
      value++;
    }
  }

  sm_loop();

  if (use_low_power) {
#ifdef SM_DEBUG
    debug.println(F("Sleeping..."));
    debug.flush();
#endif // SM_DEBUG
    go_to_sleep();
  }
}

static void show_pipes() {
  for (uint8_t i = 1; i <= NUMBER_OF_PIPES; i++) {
    uint8_t x = lib_aci_is_pipe_available(&aci_state, i);
    debug.print(F("pipe #"));
    debug.print(i, DEC);
    debug.print(F(", available=?"));
    debug.println(x, DEC);
  }
}

#ifdef USE_BATTERY
void notify_battery_level(uint8_t value) {
  static const uint8_t pipe = PIPE_BATTERY_BATTERY_LEVEL_SET;

  debug.print(F("notify_battery_level, value="));
  debug.println(value, DEC);

  value = value % 101;

  lib_aci_send_data(pipe, &value, 1);
}
#endif // USE_BATTERY

void notify_soil_moisture(const struct sm_res &res, uint8_t body_len) {

  uint8_t *data = (uint8_t *)&res;
  uint8_t len   = SM_RES_HEADER_SIZE + body_len;

//  debug.print(F("notify_soil_moisture, code="));
//  debug.print(res.code, DEC);
//  debug.print(F(", body_len="));
//  debug.println(body_len, DEC);

//  debug.print(F("aci_state.data_credit_available="));
//  debug.println(aci_state.data_credit_available, DEC);

  bool available = lib_aci_is_pipe_available(&aci_state, sm_pipe_tx);
//  debug.print(F("pipe available="));
//  debug.println(available, DEC);

  // This should probably be an explicit part of the API, but for now it makes it easier to implement a synchronous interface.
  lib_aci_set_local_data(&aci_state, sm_pipe_set, (uint8_t *)&res, len);

  // There is no need to lod messages that won't be sent.
  if (!available) {
    return;
  }

#ifdef SM_DEBUG
  debug.println("write_res");
  write_res(res);
#endif

  if (aci_state.data_credit_available == 0) {
#ifdef SM_DEBUG
    debug.println("Not enough credits to send notification.");
#endif
    return;
  }

  boolean sent = lib_aci_send_data(sm_pipe_tx, data, len);
  if (sent) {
    aci_state.data_credit_available--;
  } else {
#ifdef SM_DEBUG
    debug.println("Sending failed");
#endif
  }
}