/* * 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 "bsp/bsp.h" #include "config/config.h" #include "console/console.h" #include "hal/hal_gpio.h" #include "hal/hal_system.h" #include "os/os.h" #include "modlog/modlog.h" #ifndef ARCH_sim /* BLE */ #include "nimble/ble.h" #include "host/ble_hs.h" #include "services/gap/ble_svc_gap.h" #include "smp_svr.h" #include "host/util/util.h" #else #include #endif /* !ARCH_sim */ /* smp_svr uses the first "peruser" log module. */ #define SMP_SVR_LOG_MODULE (LOG_MODULE_PERUSER + 0) /* Convenience macro for logging to the smp_svr module. */ #define SMP_SVR_LOG(lvl, ...) \ LOG_ ## lvl(&smp_svr_log, SMP_SVR_LOG_MODULE, __VA_ARGS__) /** Log data. */ struct log smp_svr_log; #ifndef ARCH_sim static int smp_svr_gap_event(struct ble_gap_event *event, void *arg); void print_addr(const void *addr) { const uint8_t *u8p; u8p = addr; SMP_SVR_LOG(INFO, "%02x:%02x:%02x:%02x:%02x:%02x", u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); } /** * Logs information about a connection to the console. */ static void smp_svr_print_conn_desc(struct ble_gap_conn_desc *desc) { SMP_SVR_LOG(INFO, "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); SMP_SVR_LOG(INFO, " our_id_addr_type=%d our_id_addr=", desc->our_id_addr.type); print_addr(desc->our_id_addr.val); SMP_SVR_LOG(INFO, " peer_ota_addr_type=%d peer_ota_addr=", desc->peer_ota_addr.type); print_addr(desc->peer_ota_addr.val); SMP_SVR_LOG(INFO, " peer_id_addr_type=%d peer_id_addr=", desc->peer_id_addr.type); print_addr(desc->peer_id_addr.val); SMP_SVR_LOG(INFO, " 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 smp_svr_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) { MODLOG_DFLT(ERROR, "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; rc = ble_gap_adv_set_fields(&fields); if (rc != 0) { SMP_SVR_LOG(ERROR, "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, smp_svr_gap_event, NULL); if (rc != 0) { SMP_SVR_LOG(ERROR, "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. * smp_svr 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 * smp_svr. * * @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. */ static int smp_svr_gap_event(struct ble_gap_event *event, void *arg) { struct ble_gap_conn_desc desc; int rc; switch (event->type) { case BLE_GAP_EVENT_CONNECT: /* A new connection was established or a connection attempt failed. */ SMP_SVR_LOG(INFO, "connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status); if (event->connect.status == 0) { rc = ble_gap_conn_find(event->connect.conn_handle, &desc); assert(rc == 0); smp_svr_print_conn_desc(&desc); } SMP_SVR_LOG(INFO, "\n"); if (event->connect.status != 0) { /* Connection failed; resume advertising. */ smp_svr_advertise(); } return 0; case BLE_GAP_EVENT_DISCONNECT: SMP_SVR_LOG(INFO, "disconnect; reason=%d ", event->disconnect.reason); smp_svr_print_conn_desc(&event->disconnect.conn); SMP_SVR_LOG(INFO, "\n"); /* Connection terminated; resume advertising. */ smp_svr_advertise(); return 0; case BLE_GAP_EVENT_CONN_UPDATE: /* The central has updated the connection parameters. */ SMP_SVR_LOG(INFO, "connection updated; status=%d ", event->conn_update.status); rc = ble_gap_conn_find(event->connect.conn_handle, &desc); assert(rc == 0); smp_svr_print_conn_desc(&desc); SMP_SVR_LOG(INFO, "\n"); return 0; case BLE_GAP_EVENT_ADV_COMPLETE: SMP_SVR_LOG(INFO, "advertise complete; reason=%d", event->adv_complete.reason); smp_svr_advertise(); return 0; case BLE_GAP_EVENT_ENC_CHANGE: /* Encryption has been enabled or disabled for this connection. */ SMP_SVR_LOG(INFO, "encryption change event; status=%d ", event->enc_change.status); rc = ble_gap_conn_find(event->connect.conn_handle, &desc); assert(rc == 0); smp_svr_print_conn_desc(&desc); SMP_SVR_LOG(INFO, "\n"); return 0; case BLE_GAP_EVENT_MTU: SMP_SVR_LOG(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value); return 0; case BLE_GAP_EVENT_REPEAT_PAIRING: /* We already have a bond with the peer, but it is attempting to * establish a new secure link. This app sacrifices security for * convenience: just throw away the old bond and accept the new link. */ /* Delete the old bond. */ rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); assert(rc == 0); ble_store_util_delete_peer(&desc.peer_id_addr); /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should * continue with the pairing operation. */ return BLE_GAP_REPEAT_PAIRING_RETRY; } return 0; } static void smp_svr_on_reset(int reason) { SMP_SVR_LOG(ERROR, "Resetting state; reason=%d\n", reason); } static void smp_svr_on_sync(void) { int rc; /* Make sure we have proper identity address set (public preferred) */ rc = ble_hs_util_ensure_addr(0); assert(rc == 0); /* Begin advertising. */ smp_svr_advertise(); } #endif /* !ARCH_sim */ /** * main * * The main task for the project. This function initializes the packages, * then starts serving events from default event queue. * * @return int NOTE: this function should never return! */ int main(int argc, char **argv) { #ifndef ARCH_sim int rc; #else mcu_sim_parse_args(argc, argv); #endif /* Initialize OS */ sysinit(); /* Initialize the smp_svr log. */ log_register("smp_svr", &smp_svr_log, &log_console_handler, NULL, LOG_SYSLEVEL); #ifndef ARCH_sim /* Initialize the NimBLE host configuration. */ ble_hs_cfg.reset_cb = smp_svr_on_reset; ble_hs_cfg.sync_cb = smp_svr_on_sync; ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; rc = gatt_svr_init(); assert(rc == 0); /* Set the default device name. */ rc = ble_svc_gap_device_name_set("smp_svr"); assert(rc == 0); #endif conf_load(); /* * As the last thing, process events from default event queue. */ while (1) { os_eventq_run(os_eventq_dflt_get()); } return 0; }