[GitHub] andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app

2018-02-20 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc 
app: Cycling Speed and Cadence app
URL: https://github.com/apache/mynewt-core/pull/819#discussion_r169242465
 
 

 ##
 File path: apps/blecsc/src/main.c
 ##
 @@ -0,0 +1,323 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 
+#include 
+#include 
+#include 
+
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+#include "console/console.h"
+#include "config/config.h"
+#include "nimble/ble.h"
+#include "host/ble_hs.h"
+#include "services/gap/ble_svc_gap.h"
+#include "blecsc_sens.h"
+
+/* Wheel size for simulation calculations */
+#define CSC_SIM_WHEEL_CIRCUMFERENCE_MM2000
+/* Simulated cadence lower limit */
+#define CSC_SIM_CRANK_RPM_MIN 20
+/* Simulated cadence upper limit */
+#define CSC_SIM_CRANK_RPM_MAX 100
+/* Simulated speed lower limit */
+#define CSC_SIM_SPEED_KPH_MIN 0
+/* Simulated speed upper limit */
+#define CSC_SIM_SPEED_KPH_MAX 35
+
+/* Log data */
+struct log blecsc_log;
+
+/* Noticication status */
+static bool notify_state = false;
+
+/* Connection handle */
+static uint16_t conn_handle;
+
+static uint8_t blecsc_addr_type;
+
+/* Advertised device name  */
+static const char *device_name = "blecsc_sensor";
+
+/* Advertised device appearance  */
+static const uint16_t device_appearance = 1157;
+
+/* Measurement and notification timer */
+static struct os_callout blecsc_measure_timer;
+
+/* Variable holds current CSC measurement state */
+static struct ble_csc_measurement_state csc_measurement_state;
+
+/* Variable holds simulted speed (kilometers per hour) */
+static uint16_t csc_sim_speed_kph = CSC_SIM_SPEED_KPH_MIN;
+
+/* Variable holds simulated cadence (RPM) */
+static uint8_t csc_sim_crank_rpm = CSC_SIM_CRANK_RPM_MIN;
+
+
+static int blecsc_gap_event(struct ble_gap_event *event, void *arg);
+
+
+/*
+ * Enables advertising with parameters:
+ * o General discoverable mode
+ * o Undirected connectable mode
+ */
+static void
+blecsc_advertise(void)
+{
+struct ble_gap_adv_params adv_params;
+struct ble_hs_adv_fields fields;
+int rc;
+
+/*
+ *  Set the advertisement data included in our advertisements:
+ * o Flags (indicates advertisement type and other general info)
+ * o Advertising tx power
+ * o Device name
+ */
+memset(, 0, sizeof(fields));
+
+/*
+ * Advertise two flags:
+ *  o Discoverability in forthcoming advertisement (general)
+ *  o BLE-only (BR/EDR unsupported)
+ */
+fields.flags = BLE_HS_ADV_F_DISC_GEN |
+   BLE_HS_ADV_F_BREDR_UNSUP;
+
+/*
+ * Indicate that the TX power level field should be included; have the
+ * stack fill this value automatically.  This is done by assigning the
+ * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
+ */
+fields.tx_pwr_lvl_is_present = 1;
+fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+
+fields.name = (uint8_t *)device_name;
+fields.name_len = strlen(device_name);
+fields.name_is_complete = 1;
+
+/*
+ * Set appearance to: Cycling Speed and Cadence Sensor.
+ */
+fields.appearance = device_appearance;
+fields.appearance_is_present = 1;
+
+rc = ble_gap_adv_set_fields();
+if (rc != 0) {
+BLECSC_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc);
+return;
+}
+
+/* Begin advertising */
+memset(_params, 0, sizeof(adv_params));
+adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
+adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+rc = ble_gap_adv_start(blecsc_addr_type, NULL, BLE_HS_FOREVER,
+   _params, blecsc_gap_event, NULL);
+if (rc != 0) {
+BLECSC_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
+return;
+}
+}
+
+
+/* Update simulated CSC measurements.
+ * Each call increments wheel and crank revolution counters by one and
+ * computes last event time in order to match simulated candence and speed.
+ * Last event time is expressedd in 1/1024th of second units.
+ *
+ * 60 * 1024
+ * crank_dt =

[GitHub] andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app

2018-02-19 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc 
app: Cycling Speed and Cadence app
URL: https://github.com/apache/mynewt-core/pull/819#discussion_r168956695
 
 

 ##
 File path: apps/blecsc/pkg.yml
 ##
 @@ -0,0 +1,40 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+pkg.name: apps/blecsc
+pkg.type: app
+pkg.description: BLE peripheral cycling speed and cadence sensor.
+pkg.author: "Maciej Jurczak"
+pkg.email: "mjurc...@gmail.com"
+pkg.homepage: "http://mynewt.apache.org/;
+pkg.keywords:
+
+pkg.deps:
+- boot/bootutil
+- kernel/os
+- net/nimble/controller
+- net/nimble/host
+- net/nimble/host/services/gap
+- net/nimble/host/services/gatt
+- net/nimble/host/store/config
+- net/nimble/transport/ram
 
 Review comment:
   use net/nimble/transport package instead - this way other transport can be 
configured via syscfg setting without need to change app code (useful for 
sample apps since they may be run on different targets)
   
   (see 
https://github.com/apache/mynewt-core/commit/2e594496c81634532b3f248907bd0cc701d6b505
 for reference)


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app

2018-02-19 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc 
app: Cycling Speed and Cadence app
URL: https://github.com/apache/mynewt-core/pull/819#discussion_r169005323
 
 

 ##
 File path: net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
 ##
 @@ -37,6 +37,8 @@ struct ble_hs_cfg;
 
 const char *ble_svc_gap_device_name(void);
 int ble_svc_gap_device_name_set(const char *name);
+int ble_svc_gap_device_appearance(void);
+int ble_svc_gap_device_appearance_set(const uint16_t appearance);
 
 Review comment:
   I'd actually make this a syscfg value (BLE_SVC_GAP_APPEARANCE)
   the reason for this is that appearance is read-only so it cannot (and should 
not) be changed in runtime.
   getter is ok though.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app

2018-02-19 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc 
app: Cycling Speed and Cadence app
URL: https://github.com/apache/mynewt-core/pull/819#discussion_r169005562
 
 

 ##
 File path: apps/blecsc/src/main.c
 ##
 @@ -0,0 +1,323 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 
+#include 
+#include 
+#include 
+
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+#include "console/console.h"
+#include "config/config.h"
+#include "nimble/ble.h"
+#include "host/ble_hs.h"
+#include "services/gap/ble_svc_gap.h"
+#include "blecsc_sens.h"
+
+/* Wheel size for simulation calculations */
+#define CSC_SIM_WHEEL_CIRCUMFERENCE_MM2000
+/* Simulated cadence lower limit */
+#define CSC_SIM_CRANK_RPM_MIN 20
+/* Simulated cadence upper limit */
+#define CSC_SIM_CRANK_RPM_MAX 100
+/* Simulated speed lower limit */
+#define CSC_SIM_SPEED_KPH_MIN 0
+/* Simulated speed upper limit */
+#define CSC_SIM_SPEED_KPH_MAX 35
+
+/* Log data */
+struct log blecsc_log;
+
+/* Noticication status */
+static bool notify_state = false;
+
+/* Connection handle */
+static uint16_t conn_handle;
+
+static uint8_t blecsc_addr_type;
+
+/* Advertised device name  */
+static const char *device_name = "blecsc_sensor";
+
+/* Advertised device appearance  */
+static const uint16_t device_appearance = 1157;
+
+/* Measurement and notification timer */
+static struct os_callout blecsc_measure_timer;
+
+/* Variable holds current CSC measurement state */
+static struct ble_csc_measurement_state csc_measurement_state;
+
+/* Variable holds simulted speed (kilometers per hour) */
+static uint16_t csc_sim_speed_kph = CSC_SIM_SPEED_KPH_MIN;
+
+/* Variable holds simulated cadence (RPM) */
+static uint8_t csc_sim_crank_rpm = CSC_SIM_CRANK_RPM_MIN;
+
+
+static int blecsc_gap_event(struct ble_gap_event *event, void *arg);
+
+
+/*
+ * Enables advertising with parameters:
+ * o General discoverable mode
+ * o Undirected connectable mode
+ */
+static void
+blecsc_advertise(void)
+{
+struct ble_gap_adv_params adv_params;
+struct ble_hs_adv_fields fields;
+int rc;
+
+/*
+ *  Set the advertisement data included in our advertisements:
+ * o Flags (indicates advertisement type and other general info)
+ * o Advertising tx power
+ * o Device name
+ */
+memset(, 0, sizeof(fields));
+
+/*
+ * Advertise two flags:
+ *  o Discoverability in forthcoming advertisement (general)
+ *  o BLE-only (BR/EDR unsupported)
+ */
+fields.flags = BLE_HS_ADV_F_DISC_GEN |
+   BLE_HS_ADV_F_BREDR_UNSUP;
+
+/*
+ * Indicate that the TX power level field should be included; have the
+ * stack fill this value automatically.  This is done by assigning the
+ * special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
+ */
+fields.tx_pwr_lvl_is_present = 1;
+fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+
+fields.name = (uint8_t *)device_name;
+fields.name_len = strlen(device_name);
+fields.name_is_complete = 1;
+
+/*
+ * Set appearance to: Cycling Speed and Cadence Sensor.
+ */
+fields.appearance = device_appearance;
+fields.appearance_is_present = 1;
+
+rc = ble_gap_adv_set_fields();
+if (rc != 0) {
+BLECSC_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc);
+return;
+}
+
+/* Begin advertising */
+memset(_params, 0, sizeof(adv_params));
+adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
+adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
+rc = ble_gap_adv_start(blecsc_addr_type, NULL, BLE_HS_FOREVER,
+   _params, blecsc_gap_event, NULL);
+if (rc != 0) {
+BLECSC_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
+return;
+}
+}
+
+
+/* Update simulated CSC measurements.
+ * Each call increments wheel and crank revolution counters by one and
+ * computes last event time in order to match simulated candence and speed.
+ * Last event time is expressedd in 1/1024th of second units.
+ *
+ * 60 * 1024
+ * crank_dt =

[GitHub] andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc app: Cycling Speed and Cadence app

2018-02-19 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #819: Feature/blecsc 
app: Cycling Speed and Cadence app
URL: https://github.com/apache/mynewt-core/pull/819#discussion_r168956962
 
 

 ##
 File path: apps/blecsc/src/gatt_svr.c
 ##
 @@ -0,0 +1,404 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 
+#include 
+#include 
+#include "host/ble_hs.h"
+#include "host/ble_uuid.h"
+#include "blecsc_sens.h"
+
+static const char *manuf_name = "Apache Mynewt";
+static const char *model_num = "Mynewt CSC Sensor";
+
+static const uint8_t csc_supported_sensor_locations[] = {
+SENSOR_LOCATION_FRONT_WHEEL,
+SENSOR_LOCATION_REAR_DROPOUT,
+SENSOR_LOCATION_CHAINSTAY,
+SENSOR_LOCATION_REAR_WHEEL
+};
+
+static uint8_t sensor_location = SENSOR_LOCATION_REAR_DROPOUT;
+static struct ble_csc_measurement_state * measurement_state;
+uint16_t csc_measurement_handle;
+uint16_t csc_control_point_handle;
+
+static int
+gatt_svr_chr_access_csc_measurement(uint16_t conn_handle, 
+uint16_t attr_handle,
+struct ble_gatt_access_ctxt *ctxt, 
+void *arg);
+  
+static int
+gatt_svr_chr_access_csc_feature(uint16_t conn_handle, 
+uint16_t attr_handle,
+struct ble_gatt_access_ctxt *ctxt, 
+void *arg);  
+
+static int
+gatt_svr_chr_access_sensor_location(uint16_t conn_handle, 
+uint16_t attr_handle,
+struct ble_gatt_access_ctxt *ctxt, 
+void *arg);
+  
+static int
+gatt_svr_chr_access_sc_control_point(uint16_t conn_handle, 
+ uint16_t attr_handle,
+ struct ble_gatt_access_ctxt *ctxt, 
+ void *arg);   

+
+static int
+gatt_svr_chr_access_device_info(uint16_t conn_handle, 
+uint16_t attr_handle,
+struct ble_gatt_access_ctxt *ctxt, 
+void *arg);
+
+static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
+{
+/* Service: Cycling Speed and Cadence */
+.type = BLE_GATT_SVC_TYPE_PRIMARY,
+.uuid = BLE_UUID16_DECLARE(GATT_CSC_UUID),
+.characteristics = (struct ble_gatt_chr_def[]) { {
+/* Characteristic: Cycling Speed and Cadence Measurement */
+.uuid = BLE_UUID16_DECLARE(GATT_CSC_MEASUREMENT_UUID),
+.access_cb = gatt_svr_chr_access_csc_measurement,
+.val_handle = _measurement_handle,
+.flags = BLE_GATT_CHR_F_NOTIFY,
+}, {
+/* Characteristic: Cycling Speed and Cadence features */
+.uuid = BLE_UUID16_DECLARE(GATT_CSC_FEATURE_UUID),
+.access_cb = gatt_svr_chr_access_csc_feature,
+.flags = BLE_GATT_CHR_F_READ,
+}, {
+/* Characteristic: Sensor Location */
+.uuid = BLE_UUID16_DECLARE(GATT_SENSOR_LOCATION_UUID),
+.access_cb = gatt_svr_chr_access_sensor_location,
+.flags = BLE_GATT_CHR_F_READ,
+}, {
+/* Characteristic: SC Control Point*/
+.uuid = BLE_UUID16_DECLARE(GATT_SC_CONTROL_POINT_UUID),
+.access_cb = gatt_svr_chr_access_sc_control_point,
+.val_handle = _control_point_handle,
+.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_INDICATE,
+}, {  
+0, /* No more characteristics in this service */
+}, }
+},
+
+{
+/* Service: Device Information */
+.type = BLE_GATT_SVC_TYPE_PRIMARY,
+.uuid = BLE_UUID16_DECLARE(GATT_DEVICE_INFO_UUID),
+.characteristics = (struct ble_gatt_chr_def[]) { {
+/* Characteristic: * Manufacturer name */
+.uuid = BLE_UUID16_DECLARE(GATT_MANUFACTURER_NAME_UUID),
+.access_cb =