From 6d7141db0c73af34fd887fda9723c77aaaf1e349 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 29 May 2015 00:00:05 +0200 Subject: o Initial import of Phone Remote firmware, forked from the old soil moisture project. --- phone_remote.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 phone_remote.c (limited to 'phone_remote.c') diff --git a/phone_remote.c b/phone_remote.c new file mode 100644 index 0000000..c37123a --- /dev/null +++ b/phone_remote.c @@ -0,0 +1,91 @@ +#include "phone_remote.h" +#include "ble.h" +#include "nrf_error.h" +#include "nrf_gpio.h" + +#include +#include +#include + +// 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); +} -- cgit v1.2.3