/* * 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 /* BLE */ #include "nimble/ble.h" #include "host/ble_hs.h" #include "host/util/util.h" #include "image_map_config.h" /* Mandatory services. */ #include "services/gap/ble_svc_gap.h" #include "services/gatt/ble_svc_gatt.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" #include "nimble/ble.h" #ifdef NIMBLE_SPARK_SUP #include "nimble/pan107x/nimble_glue_spark.h" #else #include "nimble/pan107x/nimble_glue.h" #endif /* Application-specified header. */ #include "blecent.h" void ble_store_config_init(void); static int blecent_gap_event(struct ble_gap_event *event, void *arg); /** * Application callback. Called when the read of the ANS Supported New Alert * Category characteristic has completed. */ static int blecent_on_read(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { printf("Read complete; status=%d conn_handle=%d", error->status, conn_handle); if (error->status == 0) { printf(" attr_handle=%d value=", attr->handle); print_mbuf(attr->om); } printf("\n"); return 0; } /** * Application callback. Called when the write to the ANS Alert Notification * Control Point characteristic has completed. */ static int blecent_on_write(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { printf("Write complete; status=%d conn_handle=%d attr_handle=%d\n", error->status, conn_handle, attr->handle); return 0; } /** * Application callback. Called when the attempt to subscribe to notifications * for the ANS Unread Alert Status characteristic has completed. */ static int blecent_on_subscribe(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg) { printf("Subscribe complete; status=%d conn_handle=%d " "attr_handle=%d\n", error->status, conn_handle, attr->handle); return 0; } /** * Performs three concurrent GATT operations against the specified peer: * 1. Reads the ANS Supported New Alert Category characteristic. * 2. Writes the ANS Alert Notification Control Point characteristic. * 3. Subscribes to notifications for the ANS Unread Alert Status * characteristic. * * If the peer does not support a required service, characteristic, or * descriptor, then the peer lied when it claimed support for the alert * notification service! When this happens, or if a GATT procedure fails, * this function immediately terminates the connection. */ static void blecent_read_write_subscribe(const struct peer *peer) { const struct peer_chr *chr; const struct peer_dsc *dsc; uint8_t value[2]; int rc; /* Read the supported-new-alert-category characteristic. */ chr = peer_chr_find_uuid(peer, BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID), BLE_UUID16_DECLARE(BLECENT_CHR_SUP_NEW_ALERT_CAT_UUID)); if (chr == NULL) { printf("Error: Peer doesn't support the Supported New " "Alert Category characteristic\n"); goto err; } rc = ble_gattc_read(peer->conn_handle, chr->chr.val_handle, blecent_on_read, NULL); if (rc != 0) { printf("Error: Failed to read characteristic; rc=%d\n", rc); goto err; } /* Write two bytes (99, 100) to the alert-notification-control-point * characteristic. */ chr = peer_chr_find_uuid(peer, BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID), BLE_UUID16_DECLARE(BLECENT_CHR_ALERT_NOT_CTRL_PT)); if (chr == NULL) { printf("Error: Peer doesn't support the Alert " "Notification Control Point characteristic\n"); goto err; } value[0] = 99; value[1] = 100; rc = ble_gattc_write_flat(peer->conn_handle, chr->chr.val_handle, value, sizeof value, blecent_on_write, NULL); if (rc != 0) { printf("Error: Failed to write characteristic; rc=%d\n", rc); } /* Subscribe to notifications for the Unread Alert Status characteristic. * A central enables notifications by writing two bytes (1, 0) to the * characteristic's client-characteristic-configuration-descriptor (CCCD). */ dsc = peer_dsc_find_uuid(peer, BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID), BLE_UUID16_DECLARE(BLECENT_CHR_UNR_ALERT_STAT_UUID), BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16)); if (dsc == NULL) { printf("Error: Peer lacks a CCCD for the Unread Alert " "Status characteristic\n"); goto err; } value[0] = 1; value[1] = 0; rc = ble_gattc_write_flat(peer->conn_handle, dsc->dsc.handle, value, sizeof value, blecent_on_subscribe, NULL); if (rc != 0) { printf("Error: Failed to subscribe to characteristic; " "rc=%d\n", rc); goto err; } return; err: /* Terminate the connection. */ ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM); } /** * Called when service discovery of the specified peer has completed. */ static void blecent_on_disc_complete(const struct peer *peer, int status, void *arg) { if (status != 0) { /* Service discovery failed. Terminate the connection. */ printf("Error: Service discovery failed; status=%d " "conn_handle=%d\n", status, peer->conn_handle); ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM); return; } /* Service discovery has completed successfully. Now we have a complete * list of services, characteristics, and descriptors that the peer * supports. */ printf("Service discovery complete; status=%d " "conn_handle=%d\n", status, peer->conn_handle); /* Now perform three concurrent GATT procedures against the peer: read, * write, and subscribe to notifications. */ blecent_read_write_subscribe(peer); } /** * Initiates the GAP general discovery procedure. */ static void blecent_scan(void) { uint8_t own_addr_type; struct ble_gap_disc_params disc_params; 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; } /* Tell the controller to filter duplicates; we don't want to process * repeated advertisements from the same device. */ disc_params.filter_duplicates = 0; /** * Perform a passive scan. I.e., don't send follow-up scan requests to * each advertiser. */ disc_params.passive = 0; /* Use defaults for the rest of the parameters. */ disc_params.itvl = 100; disc_params.window = 100; disc_params.filter_policy = 0; disc_params.limited = 0; rc = ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params, blecent_gap_event, NULL); if (rc != 0) { printf("Error initiating GAP discovery procedure; rc=%d\n", rc); } } /** * Indicates whether we should tre to connect to the sender of the specified * advertisement. The function returns a positive result if the device * advertises connectability and support for the Alert Notification service. */ static int blecent_should_connect(const struct ble_gap_disc_desc *disc) { struct ble_hs_adv_fields fields; int rc; int i; /* The device has to be advertising connectability. */ if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND && disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) { return 0; } rc = ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data); if (rc != 0) { return rc; } /* The device has to advertise support for the Alert Notification * service (0x1811). */ for (i = 0; i < fields.num_uuids16; i++) { if (ble_uuid_u16(&fields.uuids16[i].u) == BLECENT_SVC_ALERT_UUID) { return 1; } } return 0; } /** * Connects to the sender of the specified advertisement of it looks * interesting. A device is "interesting" if it advertises connectability and * support for the Alert Notification service. */ static void blecent_connect_if_interesting(const struct ble_gap_disc_desc *disc) { uint8_t own_addr_type; int rc; // if (disc->data) /*Filter adv by rssi*/ if (disc->rssi < -70) { return; } /* Don't do anything if we don't care about this advertiser. */ if (!blecent_should_connect(disc)) { return; } /* Scanning must be stopped before a connection can be initiated. */ rc = ble_gap_disc_cancel(); if (rc != 0) { printf("Failed to cancel scan; rc=%d\n", rc); return; } /* Figure out address to use for connect (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; } /* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for * timeout. */ rc = ble_gap_connect(own_addr_type, &disc->addr, 30000, NULL, blecent_gap_event, NULL); if (rc != 0) { printf("Error: Failed to connect to device; addr_type=%d " "addr=%s\n; rc=%d", disc->addr.type, addr_str(disc->addr.val), rc); return; } } /** * The nimble host executes this callback when a GAP event occurs. The * application associates a GAP event callback with each connection that is * established. blecent uses the same callback for all connections. * * @param event The event being signalled. * @param arg Application-specified argument; unused by * blecent. * * @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 blecent_gap_event(struct ble_gap_event *event, void *arg) { struct ble_gap_conn_desc desc; struct ble_hs_adv_fields fields; int rc; switch (event->type) { case BLE_GAP_EVENT_DISC: rc = ble_hs_adv_parse_fields(&fields, event->disc.data, event->disc.length_data); if (rc != 0) { return 0; } /* An advertisment report was received during GAP discovery. */ print_adv_fields(&fields,&event->disc); /* Try to connect to the advertiser if it looks interesting. */ blecent_connect_if_interesting(&event->disc); return 0; case BLE_GAP_EVENT_CONNECT: /* A new connection was established or a connection attempt failed. */ if (event->connect.status == 0) { /* Connection successfully established. */ printf("Connection established "); rc = ble_gap_conn_find(event->connect.conn_handle, &desc); assert(rc == 0); print_conn_desc(&desc); printf("\n"); /* Remember peer. */ rc = peer_add(event->connect.conn_handle); if (rc != 0) { printf("Failed to add peer; rc=%d\n", rc); return 0; } /* Perform service discovery. */ rc = peer_disc_all(event->connect.conn_handle, blecent_on_disc_complete, NULL); if (rc != 0) { printf("Failed to discover services; rc=%d\n", rc); return 0; } } else { /* Connection attempt failed; resume scanning. */ printf("Error: Connection failed; status=%d\n", event->connect.status); blecent_scan(); } return 0; case BLE_GAP_EVENT_DISCONNECT: /* Connection terminated. */ printf("disconnect; reason=0x%02x\n", (uint8_t)event->disconnect.reason); print_conn_desc(&event->disconnect.conn); printf("\n"); /* Forget about peer. */ peer_delete(event->disconnect.conn.conn_handle); /* Resume scanning. */ blecent_scan(); return 0; case BLE_GAP_EVENT_DISC_COMPLETE: printf("discovery complete; reason=%d\n", event->disc_complete.reason); return 0; case BLE_GAP_EVENT_ENC_CHANGE: /* Encryption has been enabled or disabled for this connection. */ printf("encryption change event; status=%d ", event->enc_change.status); rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc); assert(rc == 0); print_conn_desc(&desc); return 0; case BLE_GAP_EVENT_NOTIFY_RX: /* Peer sent us a notification or indication. */ printf("received %s; conn_handle=%d attr_handle=%d " "attr_len=%d\n", event->notify_rx.indication ? "indication" : "notification", event->notify_rx.conn_handle, event->notify_rx.attr_handle, OS_MBUF_PKTLEN(event->notify_rx.om)); /* Attribute data is contained in event->notify_rx.attr_data. */ return 0; case BLE_GAP_EVENT_MTU: printf("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; default: return 0; } } static void blecent_on_reset(int reason) { printf("Resetting state; reason=%d\n", reason); } static void blecent_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 scanning for a peripheral to connect to. */ blecent_scan(); } static void print_version_info(void) { #include "app_version.h" struct smp_image_header *image_header_p = (void *)SIZE_BOOTLOADER; printf("APP version: %d.%d.%d\n", image_header_p->ih_ver.iv_major, image_header_p->ih_ver.iv_minor, image_header_p->ih_ver.iv_revision); } void hal_bsp_init(void) { CLK_APB1PeriphClockCmd(CLK_APB1Periph_SPI0, ENABLE); SYS_SET_MFP(P1, 1, SPI0_MOSI); SYS_SET_MFP(P1, 2, SPI0_MISO); SYS_SET_MFP(P1, 3, SPI0_CS); SYS_SET_MFP(P1, 4, SPI0_CLK); GPIO_EnableDigitalPath(P1, BIT4); //SPI CLK is input pin while act as master GPIO_EnableDigitalPath(P1, BIT3); //SPI CS is input pin while act as master GPIO_EnableDigitalPath(P1, BIT1); //SPI MOSI is input pin while act as master GPIO_EnableDigitalPath(P1, BIT2); //SPI MISO is input pin while act as master SPI_InitOptDef spiInitStruct = { .format = SPI_FormatMotorola, .dataSize = SPI_DataFrame_8b, .clkPhase = SPI_ClockPhaseFirstEdge, .clkPolarity = SPI_ClockPolarityLow, .baudrateDiv = SPI_BaudRateDiv_4 }; spiInitStruct.role = SPI_RoleSlave; SPI0_OBJ.initObj = spiInitStruct; HAL_SPI_Init(&SPI0_OBJ); } static uint16_t slave_send_buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, }; extern struct queue //½á¹¹ÌåÀàÐÍ { uint8_t totalnum;//»ùÕ¾×ÜÊý uint8_t flag_newanchor[anchor_max_num];//ÐÂÀÏ»ùÕ¾±íʾ uint16_t id[anchor_max_num]; /*ID*/ uint16_t time[anchor_max_num]; /*ʱ¼ä*/ uint16_t fail_num[anchor_max_num]; //uwb½ÓÊÜʧ°ÜµÄ´ÎÊý uint16_t rssi[anchor_max_num];//À¶ÑÀ²â³öµÄ¾àÀë uint8_t uwb_tongxun_flag[anchor_max_num];//ÓëuwbͨѶ³É¹¦Ê§°ÜµÄ±ê־λ uint16_t u16id[anchor_max_num]; /*u16ID*/ }blequeue; uint8_t zhuanhua_shuzu[100]; void zhuanhua_poll(void) { memcpy(zhuanhua_shuzu,blequeue.id,2*blequeue.totalnum); // for(int i;i<100;i++) // { // if(i%2) // { // zhuanhua_shuzu[i]=zhuanhua_shuzu[i-1]; // zhuanhua_shuzu[i]=zhuanhua_shuzu[i-1] // } // else // { // // } // } } void Spi_Zubao_Send(void) { if(blequeue.totalnum>10) {blequeue.totalnum=10;} memset(slave_send_buf,0,sizeof(slave_send_buf)); slave_send_buf[0]=0X55; slave_send_buf[1]=0XAA; slave_send_buf[2]=blequeue.totalnum*3+6; slave_send_buf[3]=blequeue.totalnum; memcpy(&slave_send_buf[4],blequeue.u16id,4*blequeue.totalnum); memcpy(&slave_send_buf[4+2*blequeue.totalnum],blequeue.rssi,2*blequeue.totalnum); slave_send_buf[4+3*blequeue.totalnum]=0x66; slave_send_buf[5+3*blequeue.totalnum]=0xBB; blequeue.totalnum=0; memset(blequeue.id,0,sizeof(blequeue.id)); memset(blequeue.u16id,0,sizeof(blequeue.u16id)); memset(blequeue.rssi,0,sizeof(blequeue.rssi)); } uint16_t slave_receive_buf[160] = {0, }; uint16_t master_send_receive_cnt = sizeof(slave_send_buf) / sizeof(slave_send_buf[0]); uint16_t timer; TimerHandle_t xTimerUser; int ai=0; uint8_t spi_length=20; void vTimerCallback( TimerHandle_t xTimer ) { timer++; // printf("slave start receive \n"); Spi_Zubao_Send(); // for(ai=0;ai=1) // { // timer=0; // HAL_SPI_SendData(&SPI0_OBJ, master_send_buf, master_send_receive_cnt,0); // } // } } void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) { printf("task overflow:%s\r\n", pcTaskName); while(1); }