raiden00pl commented on code in PR #2807: URL: https://github.com/apache/nuttx-apps/pull/2807#discussion_r1910138761
########## examples/nimble_bleprph/nimble_bleprph_main.c: ########## @@ -0,0 +1,447 @@ +/**************************************************************************** + * apps/examples/nimble_bleprph/nimble_bleprph_main.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> + +/* BLE */ + +#include "nimble/ble.h" +#include "nimble/nimble_port.h" +#include "host/ble_hs.h" +#include "host/util/util.h" +#include "services/gap/ble_svc_gap.h" +#include "services/bas/ble_svc_bas.h" + +/* Application-specified header. */ + +#include "bleprph.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Not used now */ + +#define TASK_DEFAULT_PRIORITY CONFIG_EXAMPLES_NIMBLE_BLEPRPH_PRIORITY +#define TASK_DEFAULT_STACK NULL +#define TASK_DEFAULT_STACK_SIZE 0 + +/**************************************************************************** + * External Functions Prototypes + ****************************************************************************/ + +void ble_hci_sock_ack_handler(FAR void *param); +void ble_hci_sock_set_device(int dev); + +/**************************************************************************** + * Private Functions Prototypes + ****************************************************************************/ + +static void bleprph_advertise(void); +static void nimble_host_task(FAR void *param); +static FAR void *ble_hci_sock_task(FAR void *param); +static FAR void *ble_host_task(FAR void *param); +static int bleprph_gap_event(struct ble_gap_event *event, void *arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/** + * Logs information about a connection to the console. + */ + +static void +bleprph_print_conn_desc(struct ble_gap_conn_desc *desc) +{ + printf("handle=%d our_ota_addr_type=%d our_ota_addr=", + desc->conn_handle, desc->our_ota_addr.type); + print_addr(desc->our_ota_addr.val); + printf(" our_id_addr_type=%d our_id_addr=", + desc->our_id_addr.type); + print_addr(desc->our_id_addr.val); + printf(" peer_ota_addr_type=%d peer_ota_addr=", + desc->peer_ota_addr.type); + print_addr(desc->peer_ota_addr.val); + printf(" peer_id_addr_type=%d peer_id_addr=", + desc->peer_id_addr.type); + print_addr(desc->peer_id_addr.val); + printf(" conn_itvl=%d conn_latency=%d supervision_timeout=%d " + "encrypted=%d authenticated=%d bonded=%d\n", + desc->conn_itvl, desc->conn_latency, + desc->supervision_timeout, + desc->sec_state.encrypted, + desc->sec_state.authenticated, + desc->sec_state.bonded); +} + +/** + * Enables advertising with the following parameters: + * o General discoverable mode. + * o Undirected connectable mode. + */ + +static void +bleprph_advertise(void) +{ + uint8_t own_addr_type; + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + const char *name; + int rc; + + /* Figure out address to use while advertising (no privacy for now) */ + + rc = ble_hs_id_infer_auto(0, &own_addr_type); + if (rc != 0) + { + printf("error determining address type; rc=%d\n", rc); + return; + } + + /** + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info). + * o Advertising tx power. + * o Device name. + * o 16-bit service UUIDs (alert notifications). + */ + + memset(&fields, 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 assiging 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; + + name = ble_svc_gap_device_name(); + fields.name = (uint8_t *)name; + fields.name_len = strlen(name); + fields.name_is_complete = 1; + + fields.uuids16 = (ble_uuid16_t[]) + { + BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID) + }; + fields.num_uuids16 = 1; + fields.uuids16_is_complete = 1; + + rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) + { + printf("error setting advertisement data; rc=%d\n", rc); + return; + } + + /* Begin advertising. */ + + memset(&adv_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(own_addr_type, NULL, BLE_HS_FOREVER, + &adv_params, bleprph_gap_event, NULL); + if (rc != 0) + { + printf("error enabling advertisement; rc=%d\n", rc); + return; + } +} + +/** + * The nimble host executes this callback when a GAP event occurs. + * The application associates a GAP event callback with each connection + * that forms. bleprph uses the same callback for all connections. + * + * @param event The type of event being signalled. + * @param ctxt Various information pertaining to the event. + * @param arg Application-specified argument; unuesd by + * bleprph. + * + * @return 0 if the application successfully handled the + * event; nonzero on failure. The semantics + * of the return code is specific to the + * particular GAP event being signalled. + */ Review Comment: doxygen is not allowed. The correct comment block for function looks like this: ``` /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** * Name: <Static function name> * * Description: * Description of the operation of the static function. * * Input Parameters: * A list of input parameters, one-per-line, appears here along with a * description of each input parameter. * * Returned Value: * Description of the value returned by this function (if any), * including an enumeration of all possible error values. * * Assumptions/Limitations: * Anything else that one might need to know to use this function. * ****************************************************************************/ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org