summaryrefslogtreecommitdiff
path: root/phone_remote.c
blob: 273e3d29f3c8f2ff104c0356af772099054f8ca7 (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
#include "phone_remote.h"
#include "ble.h"
#include "nrf_error.h"
#include "nrf_gpio.h"

#include <string.h>
#include <stdio.h>
#include <inttypes.h>

// 32D0xxxx-035D-59C5-70D3-BC8E4A1FD83F
#define TRYGVIS_IO_UUID_BASE {0x3f, 0xd8, 0x1f, 0x4a, 0x8e, 0xbc, 0xd3, 0x70, 0xc5, 0x59, 0x5d, 0x03,    0,    0, 0xd0, 0x32}

#define TRYGVIS_IO_UUID_SERVICE_PHONE_REMOTE               0x0020
#define TRYGVIS_IO_UUID_CHARACTERISTIC_PHONE_REMOTE_BUTTON 0x0021

static uint8_t trygvis_io_uuid_type;
static uint16_t pr_service_handle;
static ble_gatts_char_handles_t pr_char_handle;

//static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID;

// The value of the soil moisture control characteristic.
static uint8_t control_value[100] = {1, 2, 3};

#define dbg printf

uint32_t pr_add_services() {
  uint32_t err_code;

  dbg("pr: pr_add_services\r\n");

  ble_uuid128_t base_uuid = {TRYGVIS_IO_UUID_BASE};
  err_code = sd_ble_uuid_vs_add(&base_uuid, &trygvis_io_uuid_type);
  if (err_code != NRF_SUCCESS) {
      return err_code;
  }

  // -------------------------------------------------------------------------
  // Service
  // -------------------------------------------------------------------------

  ble_uuid_t ble_uuid_svc = {
      .type = trygvis_io_uuid_type,
      .uuid = TRYGVIS_IO_UUID_SERVICE_PHONE_REMOTE
  };

  err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid_svc, &pr_service_handle);
  if (err_code != NRF_SUCCESS) {
      return err_code;
  }

  // -------------------------------------------------------------------------
  // Characteristic
  // -------------------------------------------------------------------------

  ble_gatts_char_md_t char_md;

  memset(&char_md, 0, sizeof(char_md));

  char_md.char_props.read   = 1;
  char_md.char_props.write  = 1;
  char_md.char_props.notify = 1;

  ble_uuid_t ble_uuid_chr = {
      .type = trygvis_io_uuid_type,
      .uuid = TRYGVIS_IO_UUID_CHARACTERISTIC_PHONE_REMOTE_BUTTON
  };

  ble_gatts_attr_md_t attr_md;
  memset(&attr_md, 0, sizeof(attr_md));

  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
  attr_md.vlen       = 1;
  attr_md.vloc       = BLE_GATTS_VLOC_USER;

  ble_gatts_attr_t attr_char_value;
  memset(&attr_char_value, 0, sizeof(attr_char_value));

  attr_char_value.p_uuid       = &ble_uuid_chr;
  attr_char_value.p_attr_md    = &attr_md;
  attr_char_value.init_len     = 3;
  attr_char_value.init_offs    = 0;
  attr_char_value.max_len      = sizeof(control_value);
  attr_char_value.p_value      = control_value;

  return sd_ble_gatts_characteristic_add(pr_service_handle,
                                         &char_md,
                                         &attr_char_value,
                                         &pr_char_handle);
}