<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/44833/combining-multilink-central-with-usb-generic</link><description>Hey, 
 I&amp;#39;m using the nRF52840 dongle with SDK15.3. Am trying to make it a central device that is configured as a HID mouse when plugged. So I&amp;#39;m combining both multilink_central and USB_Generic examples. 
 
 am getting these errors: 
 
 am not sure if</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 30 Jul 2019 06:54:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/44833/combining-multilink-central-with-usb-generic" /><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/201118?ContentTypeID=1</link><pubDate>Tue, 30 Jul 2019 06:54:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9fc21805-4d5d-4925-a690-264600179090</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;Take a look &lt;a href="https://devzone.nordicsemi.com/nordic/short-range-guides/b/getting-started/posts/nrf52840-dongle-programming-tutorial"&gt;here&lt;/a&gt; and &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/49465/hex-code/197982#197982"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you have any more issues, could you please create a new ticket since this ticket is 4 months old? :-)&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/199641?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2019 03:03:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e276fea-3d46-4acc-b017-98e237938ba4</guid><dc:creator>Sensor_todd</dc:creator><description>&lt;p&gt;Hi Maher,&lt;/p&gt;
&lt;p&gt;I am trying to do something very similarr by combining USBD_ble_uart example and replacing the uart peripheral with uart central. It compiles without error and i can program it to an pca10059 dongle with s140, but the dongle appears not to boot (no lights at all) and doesnt show up as a usb device. I think I have all the correct settings enabled in sdk_config.h, but i think this is the most likely place i have an issue. What was it that got your project working in the end? Im wondering if there is something i missed that was the last piece you found to get it working.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Kind regards&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Todd&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176953?ContentTypeID=1</link><pubDate>Tue, 19 Mar 2019 11:09:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ade1c0bb-11aa-4e7c-9be5-35c8f542c0db</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;Can you make a new ticket on that question, since this ticket is about combining multilink_central and USBD.&lt;/p&gt;
&lt;p&gt;This will make it easier for other community members to search on the forum and find answers.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176684?ContentTypeID=1</link><pubDate>Mon, 18 Mar 2019 12:19:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a5a9312b-8fd0-4415-8cb9-c5b2b6951a36</guid><dc:creator>Maher Shakir</dc:creator><description>&lt;p&gt;I was finally able to combine both multilink_central and USBD. Now both the dev-kit and the dongle are paired and the dongle is seen as a mouse. Now my question is how to communicate with the dongle in such a way that makes move the mouse in accordance to the dev-kit (Same way as any commercial mouse with a dongle)? In a way, how to handle the information am sending to the dongle?&lt;/p&gt;
&lt;p&gt;Thank you Andreas,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176620?ContentTypeID=1</link><pubDate>Mon, 18 Mar 2019 08:20:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a34c21e7-2ac2-45db-bef7-520efba6f053</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;What errors do you get? Can you list them? Could i take a look at the project? I can make this ticket private if you only wish to share it with me.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176517?ContentTypeID=1</link><pubDate>Fri, 15 Mar 2019 19:34:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a740876f-b34a-4fb9-86b9-3d4d1e9a46dc</guid><dc:creator>Maher Shakir</dc:creator><description>&lt;p&gt;I am not sure of what is the actual problem with the my attempt to combine those two examples. but I tried to combine both USBD and and UART_C and it built no problems. but I is not seen as a mouse anymore. So when I compile only the USBD it is seen as a mouse (only when the processor is set 10056 not to 10059 for some reason, even-though am using the dongle)&lt;/p&gt;
&lt;p&gt;I guess am missing something or my main function is out of order, am not sure.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include &amp;lt;stddef.h&amp;gt; // added from USBD
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;app_uart.h&amp;quot;
#include &amp;quot;ble_db_discovery.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;app_util.h&amp;quot;
#include &amp;quot;bsp_btn_ble.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_gap.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;ble_nus_c.h&amp;quot;
#include &amp;quot;nrf_ble_gatt.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;nrf_ble_scan.h&amp;quot;

#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

// Added headers for USBD
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nrf_drv_usbd.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_drv_power.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;bsp_cli.h&amp;quot;
#include &amp;quot;nrf_cli.h&amp;quot;
#include &amp;quot;nrf_cli_uart.h&amp;quot;
// End of added headers

// Addedd diffinitions for USBD
#define BTN_MOUSE_MOVE BSP_BOARD_BUTTON_0
#define BTN_SYSTEM_OFF BSP_BOARD_BUTTON_1
#define LED_USB_STATUS BSP_BOARD_LED_0
#define LED_USB_POWER BSP_BOARD_LED_1
#define LED_RUNNING BSP_BOARD_LED_2
#define LED_ACTIVE BSP_BOARD_LED_3
NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16);
NRF_CLI_DEF(m_cli_uart,
            &amp;quot;uart_cli:~$ &amp;quot;,
            &amp;amp;m_cli_uart_transport.transport,
            &amp;#39;\r&amp;#39;,
            4);

static bool m_send_flag = 0;

#define BTN_DATA_SEND               0
#define BTN_DATA_KEY_RELEASE        (bsp_event_t)(BSP_EVENT_KEY_LAST + 1)

#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif

#define STARTUP_DELAY 100                                 // Number of microseconds to start USBD after powering up. (for port debouncing)
#define EP0_MAXPACKETSIZE NRF_DRV_USBD_EPSIZE             // Maximum size of the packed transfered by EP0

/** Device descriptor */
#define USBD_DEVICE_DESCRIPTOR \
    0x12,                        /* bLength | size of descriptor                                                  */\
    0x01,                        /* bDescriptorType | descriptor type                                             */\
    0x00, 0x02,                  /* bcdUSB | USB spec release (ver 2.0)                                           */\
    0x00,                        /* bDeviceClass &amp;#194;&amp;#166; class code (each interface specifies class information)        */\
    0x00,                        /* bDeviceSubClass &amp;#194;&amp;#166; device sub-class (must be set to 0 because class code is 0) */\
    0x00,                        /* bDeviceProtocol | device protocol (no class specific protocol)                */\
    EP0_MAXPACKETSIZE,           /* bMaxPacketSize0 | maximum packet size (64 bytes)                              */\
    0x15, 0x19,                  /* vendor ID  (0x1915 Nordic)                                                    */\
    0x0A, 0x52,                  /* product ID (0x520A nRF52 HID mouse on nrf_drv)                                */\
    0x01, 0x01,                  /* bcdDevice | final device release number in BCD Format                         */\
    USBD_STRING_MANUFACTURER_IX, /* iManufacturer | index of manufacturer string                                  */\
    USBD_STRING_PRODUCT_IX,      /* iProduct | index of product string                                            */\
    USBD_STRING_SERIAL_IX,       /* iSerialNumber | Serial Number string                                          */\
    0x01                         /* bNumConfigurations | number of configurations                                 */

#define DEVICE_SELF_POWERED 1
#define REMOTE_WU           1

#define USBD_CONFIG_DESCRIPTOR_SIZE   9
#define USBD_CONFIG_DESCRIPTOR_FULL_SIZE   (9 + (9 + 9 + 7))
#define USBD_CONFIG_DESCRIPTOR  \
    0x09,         /* bLength | length of descriptor                                             */\
    0x02,         /* bDescriptorType | descriptor type (CONFIGURATION)                          */\
    USBD_CONFIG_DESCRIPTOR_FULL_SIZE, 0x00,    /* wTotalLength | total length of descriptor(s)  */\
    0x01,         /* bNumInterfaces                                                             */\
    0x01,         /* bConfigurationValue                                                        */\
    0x00,         /* index of string Configuration | configuration string index (not supported) */\
    0x80| (((DEVICE_SELF_POWERED) ? 1U:0U)&amp;lt;&amp;lt;6) | (((REMOTE_WU) ? 1U:0U)&amp;lt;&amp;lt;5), /* bmAttributes    */\
    49            /* maximum power in steps of 2mA (98mA)                                       */

#define USBD_INTERFACE0_DESCRIPTOR  \
    0x09,         /* bLength                                                                          */\
    0x04,         /* bDescriptorType | descriptor type (INTERFACE)                                    */\
    0x00,         /* bInterfaceNumber                                                                 */\
    0x00,         /* bAlternateSetting                                                                */\
    0x01,         /* bNumEndpoints | number of endpoints (1)                                          */\
    0x03,         /* bInterfaceClass | interface class (3..defined by USB spec: HID)                  */\
    0x00,         /* bInterfaceSubClass |interface sub-class (0.. no boot interface)                  */\
    0x02,         /* bInterfaceProtocol | interface protocol (1..defined by USB spec: mouse)          */\
    0x00          /* interface string index (not supported)                                           */

/**
 * HID Table must normally be between Interface and EndPoint Descriptor
 * as written in HID spec&amp;#194;&amp;#167;7.1 but it doesn&amp;#39;t work with OSR2.1
 */
#define USBD_HID0_DESCRIPTOR  \
    0x09,         /* bLength | length of descriptor (9 bytes)                    */\
    0x21,         /* bHIDDescriptor | descriptor type (HID)                      */\
    0x11, 0x01,   /* HID wBcdHID | Spec version 01.11                            */\
    0x00,         /* bCountryCode | HW Target country                            */\
    0x01,         /* bNumDescriptors | Number of HID class descriptors to follow */\
    0x22,         /* bDescriptorType | Report descriptor type is 0x22 (report)   */\
    (uint8_t)(USBD_MOUSE_REPORT_DESCRIPTOR_SIZE),      /* Total length of Report descr., low byte */ \
    (uint8_t)(USBD_MOUSE_REPORT_DESCRIPTOR_SIZE / 256) /* Total length of Report descr., high byte */

#define USBD_ENDPOINT1_DESCRIPTOR  \
    0x07,         /* bLength | length of descriptor (7 bytes)                                     */\
    0x05,         /* bDescriptorType | descriptor type (ENDPOINT)                                 */\
    0x81,         /* bEndpointAddress | endpoint address (IN endpoint, endpoint 1)                */\
    0x03,         /* bmAttributes | endpoint attributes (interrupt)                               */\
    0x08,0x00,    /* bMaxPacketSizeLowByte,bMaxPacketSizeHighByte | maximum packet size (8 bytes) */\
    0x08          /* bInterval | polling interval (10ms)                                          */


/**
 * String config descriptor
 */
#define USBD_STRING_LANG_IX  0x00
#define USBD_STRING_LANG \
    0x04,         /* length of descriptor                   */\
    0x03,         /* descriptor type                        */\
    0x09,         /*                                        */\
    0x04          /* Supported LangID = 0x0409 (US-English) */

#define USBD_STRING_MANUFACTURER_IX  0x01
#define USBD_STRING_MANUFACTURER \
    42,           /* length of descriptor (? bytes)   */\
    0x03,         /* descriptor type                  */\
    &amp;#39;N&amp;#39;, 0x00,    /* Define Unicode String &amp;quot;Nordic Semiconductor  */\
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;i&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;S&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;i&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;u&amp;#39;, 0x00, \
    &amp;#39;c&amp;#39;, 0x00, \
    &amp;#39;t&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00

#define USBD_STRING_PRODUCT_IX  0x02
#define USBD_STRING_PRODUCT \
    72,           /* length of descriptor (? bytes)         */\
    0x03,         /* descriptor type                        */\
    &amp;#39;n&amp;#39;, 0x00,    /* generic unicode string for all devices */\
    &amp;#39;R&amp;#39;, 0x00, \
    &amp;#39;F&amp;#39;, 0x00, \
    &amp;#39;5&amp;#39;, 0x00, \
    &amp;#39;2&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;U&amp;#39;, 0x00, \
    &amp;#39;S&amp;#39;, 0x00, \
    &amp;#39;B&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;H&amp;#39;, 0x00, \
    &amp;#39;I&amp;#39;, 0x00, \
    &amp;#39;D&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;u&amp;#39;, 0x00, \
    &amp;#39;s&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;n&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;f&amp;#39;, 0x00, \
    &amp;#39;_&amp;#39;, 0x00, \
    &amp;#39;d&amp;#39;, 0x00, \
    &amp;#39;r&amp;#39;, 0x00, \
    &amp;#39;v&amp;#39;, 0x00, \
    &amp;#39; &amp;#39;, 0x00, \
    &amp;#39;D&amp;#39;, 0x00, \
    &amp;#39;e&amp;#39;, 0x00, \
    &amp;#39;m&amp;#39;, 0x00, \
    &amp;#39;o&amp;#39;, 0x00, \

#define USBD_STRING_SERIAL_IX  0x00

#define USBD_MOUSE_REPORT_DESCRIPTOR_SIZE  46
#define USBD_MOUSE_REPORT_DESCRIPTOR \
    0x05, 0x01,     /* usage page (generic desktop). Global item, applies to all subsequent items   */\
    0x09, 0x02,     /* usage (mouse). Local item                                                    */\
    0xA1, 0x01,     /* collection (application)                                                     */\
    0x09, 0x01,     /* usage (pointer)                                                              */\
    0xA1, 0x00,     /* collection (physical)                                                        */\
    0x05, 0x09,     /*   usage page (buttons). Global item, applies to all subsequent items         */\
    0x19, 0x01,     /*   usage minimum (1)                                                          */\
    0x29, 0x08,     /*   usage maximum (8)                                                          */\
    0x15, 0x00,     /*   logical minimum (0)                                                        */\
    0x25, 0x01,     /*   logical maximum (1)                                                        */\
    0x95, 0x08,     /*   report count (8)                                                           */\
    0x75, 0x01,     /*   report size (1)                                                            */\
    0x81, 0x02,     /*   input (data, var, abs)                                                     */\
    0x05, 0x01,     /*   usage page (generic desktop). Global item, applies to all subsequent items */\
    0x15, 0x81,     /*   logical minimum (-127)                                                     */\
    0x25, 0x7F,     /*   logical maximum (127)                                                      */\
    0x75, 0x08,     /*   report size (8)                                                            */\
    0x09, 0x30,     /*   usage (X)                                                                  */\
    0x09, 0x31,     /*   usage (Y)                                                                  */\
    0x09, 0x38,     /*   usage wheel                                                                */\
    0x95, 0x03,     /*   report count (3)                                                           */\
    0x81, 0x06,     /*   input (3 position bytes X, Y &amp;amp; roller)                                     */\
    0xC0,           /* end collection                                                               */\
    0xC0            /* End Collection                                                               */


static const uint8_t get_descriptor_device[] = {
    USBD_DEVICE_DESCRIPTOR
};

static const uint8_t get_descriptor_configuration[] = {
    USBD_CONFIG_DESCRIPTOR,
    USBD_INTERFACE0_DESCRIPTOR,
    USBD_HID0_DESCRIPTOR,
    USBD_ENDPOINT1_DESCRIPTOR
};
static const uint8_t get_descriptor_string_lang[] = {
    USBD_STRING_LANG
};
static const uint8_t get_descriptor_string_manuf[] = {
    USBD_STRING_MANUFACTURER
};
static const uint8_t get_descriptor_string_prod[] = {
    USBD_STRING_PRODUCT
};
static const uint8_t get_descriptor_report_interface_0[] = {
    USBD_MOUSE_REPORT_DESCRIPTOR
};

static const uint8_t get_config_resp_configured[]   = {1};
static const uint8_t get_config_resp_unconfigured[] = {0};

static const uint8_t get_status_device_resp_nrwu[] = {
    ((DEVICE_SELF_POWERED) ? 1 : 0), //LSB first: self-powered, no remoteWk
    0
};
static const uint8_t get_status_device_resp_rwu[]  = {
    ((DEVICE_SELF_POWERED) ? 1 : 0) | 2, //LSB first: self-powered, remoteWk
    0
};

static const uint8_t get_status_interface_resp[] = {0, 0};
static const uint8_t get_status_ep_halted_resp[] = {1, 0};
static const uint8_t get_status_ep_active_resp[] = {0, 0};


#define GET_CONFIG_DESC_SIZE    sizeof(get_descriptor_configuration)
#define GET_INTERFACE_DESC_SIZE 9
#define GET_HID_DESC_SIZE       9
#define GET_ENDPOINT_DESC_SIZE  7

#define get_descriptor_interface_0 \
    &amp;amp;get_descriptor_configuration[9]
#define get_descriptor_hid_0       \
    &amp;amp;get_descriptor_configuration[9+GET_INTERFACE_DESC_SIZE]
#define get_descriptor_endpoint_1  \
    &amp;amp;get_descriptor_configuration[9+GET_INTERFACE_DESC_SIZE+GET_HID_DESC_SIZE]

// End of added diffs for USBD

#define APP_BLE_CONN_CFG_TAG    1                                       /**&amp;lt; Tag that refers to the BLE stack configuration set with @ref sd_ble_cfg_set. The default tag is @ref BLE_CONN_CFG_TAG_DEFAULT. */
#define APP_BLE_OBSERVER_PRIO   3                                       /**&amp;lt; BLE observer priority of the application. There is no need to modify this value. */

#define UART_TX_BUF_SIZE        256                                     /**&amp;lt; UART TX buffer size. */
#define UART_RX_BUF_SIZE        256                                     /**&amp;lt; UART RX buffer size. */

#define NUS_SERVICE_UUID_TYPE   BLE_UUID_TYPE_VENDOR_BEGIN              /**&amp;lt; UUID type for the Nordic UART Service (vendor specific). */

#define ECHOBACK_BLE_UART_DATA  1                                       /**&amp;lt; Echo the UART data that is received over the Nordic UART Service (NUS) back to the sender. */


// Added functions for USBD
/**
 * @brief USB configured flag
 *
 * The flag that is used to mark the fact that USB is configured and ready
 * to transmit data
 */
static volatile bool m_usbd_configured = false;

/**
 * @brief USB suspended
 *
 * The flag that is used to mark the fact that USB is suspended and requires wake up
 * if new data is available.
 *
 * @note This variable is changed from the main loop.
 */
static bool m_usbd_suspended = false;

/**
 * @brief Mark the fact if remote wake up is enabled
 *
 * The internal flag that marks if host enabled the remote wake up functionality in this device.
 */
static
#if REMOTE_WU
    volatile // Disallow optimization only if Remote wakeup is enabled
#endif
bool m_usbd_rwu_enabled = false;

/**
 * @brief Current mouse position
 *
 * The index of current mouse position that would be changed to real offset.
 */
static volatile uint8_t m_mouse_position = 0;

/**
 * @brief The flag for mouse position send pending
 *
 * Setting this flag means that USB endpoint is busy by sending
 * last mouse position.
 */
static volatile bool m_send_mouse_position = false;

/**
 * @brief The requested suspend state
 *
 * The currently requested suspend state based on the events
 * received from USBD library.
 * If the value here is different than the @ref m_usbd_suspended
 * the state changing would be processed inside main loop.
 */
static volatile bool m_usbd_suspend_state_req = false;

/**
 * @brief System OFF request flag
 *
 * This flag is used in button event processing and marks the fact that
 * system OFF should be activated from main loop.
 */
static volatile bool m_system_off_req = false;


/**
 * @brief Setup all the endpoints for selected configuration
 *
 * Function sets all the endpoints for specific configuration.
 *
 * @note
 * Setting the configuration index 0 means technically disabling the HID interface.
 * Such configuration should be set when device is starting or USB reset is detected.
 *
 * @param index Configuration index
 *
 * @retval NRF_ERROR_INVALID_PARAM Invalid configuration
 * @retval NRF_SUCCESS             Configuration successfully set
 */
static ret_code_t ep_configuration(uint8_t index)
{
    if ( index == 1 )
    {
        nrf_drv_usbd_ep_dtoggle_clear(NRF_DRV_USBD_EPIN1);
        nrf_drv_usbd_ep_stall_clear(NRF_DRV_USBD_EPIN1);
        nrf_drv_usbd_ep_enable(NRF_DRV_USBD_EPIN1);
        m_usbd_configured = true;
        nrf_drv_usbd_setup_clear();
    }
    else if ( index == 0 )
    {
        nrf_drv_usbd_ep_disable(NRF_DRV_USBD_EPIN1);
        m_usbd_configured = false;
        nrf_drv_usbd_setup_clear();
    }
    else
    {
        return NRF_ERROR_INVALID_PARAM;
    }
    return NRF_SUCCESS;
}

/**
 * @name Processing setup requests
 *
 * @{
 */
/**
 * @brief Respond on ep 0
 *
 * Auxiliary function for sending respond on endpoint 0
 * @param[in] p_setup Pointer to setup data from current setup request.
 *                    It would be used to calculate the size of data to send.
 * @param[in] p_data  Pointer to the data to send.
 * @param[in] size    Number of bytes to send.
 * @note Data pointed by p_data has to be available till the USBD_EVT_BUFREADY event.
 */
static void respond_setup_data(
    nrf_drv_usbd_setup_t const * const p_setup,
    void const * p_data, size_t size)
{
    /* Check the size against required response size */
    if (size &amp;gt; p_setup-&amp;gt;wLength)
    {
        size = p_setup-&amp;gt;wLength;
    }
    ret_code_t ret;
    nrf_drv_usbd_transfer_t transfer =
    {
        .p_data = {.tx = p_data},
        .size = size
    };
    ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &amp;amp;transfer);
    if (ret != NRF_SUCCESS)
    {
        NRF_LOG_ERROR(&amp;quot;Transfer starting failed: %d&amp;quot;, (uint32_t)ret);
    }
    ASSERT(ret == NRF_SUCCESS);
    UNUSED_VARIABLE(ret);
}


/** React to GetStatus */
static void usbd_setup_GetStatus(nrf_drv_usbd_setup_t const * const p_setup)
{
    switch (p_setup-&amp;gt;bmRequestType)
    {
    case 0x80: // Device
        if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0)
        {
            respond_setup_data(
                p_setup,
                m_usbd_rwu_enabled ? get_status_device_resp_rwu : get_status_device_resp_nrwu,
                sizeof(get_status_device_resp_nrwu));
            return;
        }
        break;
    case 0x81: // Interface
        if (m_usbd_configured) // Respond only if configured
        {
            if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0) // Only interface 0 supported
            {
                respond_setup_data(
                    p_setup,
                    get_status_interface_resp,
                    sizeof(get_status_interface_resp));
                return;
            }
        }
        break;
    case 0x82: // Endpoint
        if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == 0) // Endpoint 0
        {
            respond_setup_data(
                p_setup,
                get_status_ep_active_resp,
                sizeof(get_status_ep_active_resp));
            return;
        }
        if (m_usbd_configured) // Other endpoints responds if configured
        {
            if (((p_setup-&amp;gt;wIndex) &amp;amp; 0xff) == NRF_DRV_USBD_EPIN1)
            {
                if (nrf_drv_usbd_ep_stall_check(NRF_DRV_USBD_EPIN1))
                {
                    respond_setup_data(
                        p_setup,
                        get_status_ep_halted_resp,
                        sizeof(get_status_ep_halted_resp));
                    return;
                }
                else
                {
                    respond_setup_data(
                        p_setup,
                        get_status_ep_active_resp,
                        sizeof(get_status_ep_active_resp));
                    return;
                }
            }
        }
        break;
    default:
        break; // Just go to stall
    }
    NRF_LOG_ERROR(&amp;quot;Unknown status: 0x%2x&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_ClearFeature(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x02) // standard request, recipient=endpoint
    {
        if ((p_setup-&amp;gt;wValue) == 0)
        {
            if ((p_setup-&amp;gt;wIndex) == NRF_DRV_USBD_EPIN1)
            {
                nrf_drv_usbd_ep_stall_clear(NRF_DRV_USBD_EPIN1);
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    else if ((p_setup-&amp;gt;bmRequestType) ==  0x0) // standard request, recipient=device
    {
        if (REMOTE_WU)
        {
            if ((p_setup-&amp;gt;wValue) == 1) // Feature Wakeup
            {
                m_usbd_rwu_enabled = false;
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    NRF_LOG_ERROR(&amp;quot;Unknown feature to clear&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetFeature(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x02) // standard request, recipient=endpoint
    {
        if ((p_setup-&amp;gt;wValue) == 0) // Feature HALT
        {
            if ((p_setup-&amp;gt;wIndex) == NRF_DRV_USBD_EPIN1)
            {
                nrf_drv_usbd_ep_stall(NRF_DRV_USBD_EPIN1);
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    else if ((p_setup-&amp;gt;bmRequestType) ==  0x0) // standard request, recipient=device
    {
        if (REMOTE_WU)
        {
            if ((p_setup-&amp;gt;wValue) == 1) // Feature Wakeup
            {
                m_usbd_rwu_enabled = true;
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    NRF_LOG_ERROR(&amp;quot;Unknown feature to set&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_GetDescriptor(nrf_drv_usbd_setup_t const * const p_setup)
{
    //determine which descriptor has been asked for
    switch ((p_setup-&amp;gt;wValue) &amp;gt;&amp;gt; 8)
    {
    case 1: // Device
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            respond_setup_data(
                p_setup,
                get_descriptor_device,
                sizeof(get_descriptor_device));
            return;
        }
        break;
    case 2: // Configuration
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            respond_setup_data(
                p_setup,
                get_descriptor_configuration,
                GET_CONFIG_DESC_SIZE);
            return;
        }
        break;
    case 3: // String
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Select the string
            switch ((p_setup-&amp;gt;wValue) &amp;amp; 0xFF)
            {
            case USBD_STRING_LANG_IX:
                respond_setup_data(
                    p_setup,
                    get_descriptor_string_lang,
                    sizeof(get_descriptor_string_lang));
                return;
            case USBD_STRING_MANUFACTURER_IX:
                respond_setup_data(
                    p_setup,
                    get_descriptor_string_manuf,
                    sizeof(get_descriptor_string_manuf));
                return;
            case USBD_STRING_PRODUCT_IX:
                respond_setup_data(p_setup,
                    get_descriptor_string_prod,
                    sizeof(get_descriptor_string_prod));
                return;
            default:
                break;
            }
        }
        break;
    case 4: // Interface
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Which interface?
            if ((((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0))
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_interface_0,
                    GET_INTERFACE_DESC_SIZE);
                return;
            }
        }
        break;
    case 5: // Endpoint
        if ((p_setup-&amp;gt;bmRequestType) == 0x80)
        {
            // Which endpoint?
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 1)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_endpoint_1,
                    GET_ENDPOINT_DESC_SIZE);
                return;
            }
        }
        break;
    case 0x21: // HID
        if ((p_setup-&amp;gt;bmRequestType) == 0x81)
        {
            // Which interface
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_hid_0,
                    GET_HID_DESC_SIZE);
                return;
            }
        }
        break;
    case 0x22: // HID report
        if ((p_setup-&amp;gt;bmRequestType) == 0x81)
        {
            // Which interface?
            if (((p_setup-&amp;gt;wValue) &amp;amp; 0xFF) == 0)
            {
                respond_setup_data(
                    p_setup,
                    get_descriptor_report_interface_0,
                    sizeof(get_descriptor_report_interface_0));
                return;
            }
        }
        break;
    default:
        break; // Not supported - go to stall
    }

    NRF_LOG_ERROR(&amp;quot;Unknown descriptor requested: 0x%2x, type: 0x%2x or value: 0x%2x&amp;quot;,
        p_setup-&amp;gt;wValue &amp;gt;&amp;gt; 8,
        p_setup-&amp;gt;bmRequestType,
        p_setup-&amp;gt;wValue &amp;amp; 0xFF);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_GetConfig(nrf_drv_usbd_setup_t const * const p_setup)
{
    if (m_usbd_configured)
    {
        respond_setup_data(
            p_setup,
            get_config_resp_configured,
            sizeof(get_config_resp_configured));
    }
    else
    {
        respond_setup_data(
            p_setup,
            get_config_resp_unconfigured,
            sizeof(get_config_resp_unconfigured));
    }
}

static void usbd_setup_SetConfig(nrf_drv_usbd_setup_t const * const p_setup)
{
    if ((p_setup-&amp;gt;bmRequestType) == 0x00)
    {
        // accept only 0 and 1
        if (((p_setup-&amp;gt;wIndex) == 0) &amp;amp;&amp;amp; ((p_setup-&amp;gt;wLength) == 0) &amp;amp;&amp;amp;
            ((p_setup-&amp;gt;wValue) &amp;lt;= UINT8_MAX))
        {
            if (NRF_SUCCESS == ep_configuration((uint8_t)(p_setup-&amp;gt;wValue)))
            {
                nrf_drv_usbd_setup_clear();
                return;
            }
        }
    }
    NRF_LOG_ERROR(&amp;quot;Wrong configuration: Index: 0x%2x, Value: 0x%2x.&amp;quot;,
        p_setup-&amp;gt;wIndex,
        p_setup-&amp;gt;wValue);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetIdle(nrf_drv_usbd_setup_t const * const p_setup)
{
    if (p_setup-&amp;gt;bmRequestType == 0x21)
    {
        //accept any value
        nrf_drv_usbd_setup_clear();
        return;
    }
    NRF_LOG_ERROR(&amp;quot;Set Idle wrong type: 0x%2x.&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetInterface(
    nrf_drv_usbd_setup_t const * const p_setup)
{
    //no alternate setting is supported - STALL always
    NRF_LOG_ERROR(&amp;quot;No alternate interfaces supported.&amp;quot;);
    nrf_drv_usbd_setup_stall();
}

static void usbd_setup_SetProtocol(
    nrf_drv_usbd_setup_t const * const p_setup)
{
    if (p_setup-&amp;gt;bmRequestType == 0x21)
    {
        //accept any value
        nrf_drv_usbd_setup_clear();
        return;
    }
    NRF_LOG_ERROR(&amp;quot;Set Protocol wrong type: 0x%2x.&amp;quot;, p_setup-&amp;gt;bmRequestType);
    nrf_drv_usbd_setup_stall();
}

/** @} */ /* End of processing setup requests functions */


static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
{
    switch (p_event-&amp;gt;type)
    {
    case NRF_DRV_USBD_EVT_SUSPEND:
        NRF_LOG_INFO(&amp;quot;SUSPEND state detected&amp;quot;);
        m_usbd_suspend_state_req = true;
        break;
    case NRF_DRV_USBD_EVT_RESUME:
        NRF_LOG_INFO(&amp;quot;RESUMING from suspend&amp;quot;);
        m_usbd_suspend_state_req = false;
        break;
    case NRF_DRV_USBD_EVT_WUREQ:
        NRF_LOG_INFO(&amp;quot;RemoteWU initiated&amp;quot;);
        m_usbd_suspend_state_req = false;
        break;
    case NRF_DRV_USBD_EVT_RESET:
        {
            ret_code_t ret = ep_configuration(0);
            ASSERT(ret == NRF_SUCCESS);
            UNUSED_VARIABLE(ret);
            m_usbd_suspend_state_req = false;
            break;
        }
    case NRF_DRV_USBD_EVT_SOF:
        {
            static uint32_t cycle = 0;
            ++cycle;
            if ((cycle % (m_usbd_configured ? 500 : 100)) == 0)
            {
                bsp_board_led_invert(LED_USB_STATUS);
            }
            break;
        }
    case NRF_DRV_USBD_EVT_EPTRANSFER:
        if (NRF_DRV_USBD_EPIN1 == p_event-&amp;gt;data.eptransfer.ep)
        {
            m_send_mouse_position = false;
        }
        else
        if (NRF_DRV_USBD_EPIN0 == p_event-&amp;gt;data.eptransfer.ep)
        {
            if (NRF_USBD_EP_OK == p_event-&amp;gt;data.eptransfer.status)
            {
                if (!nrf_drv_usbd_errata_154())
                {
                    /* Transfer ok - allow status stage */
                    nrf_drv_usbd_setup_clear();
                }
            }
            else if (NRF_USBD_EP_ABORTED == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Just ignore */
                NRF_LOG_INFO(&amp;quot;Transfer aborted event on EPIN0&amp;quot;);
            }
            else
            {
                NRF_LOG_ERROR(&amp;quot;Transfer failed on EPIN0: %d&amp;quot;, p_event-&amp;gt;data.eptransfer.status);
                nrf_drv_usbd_setup_stall();
            }
        }
        else
        if (NRF_DRV_USBD_EPOUT0 == p_event-&amp;gt;data.eptransfer.ep)
        {
            /* NOTE: No EPOUT0 data transfers are used.
             * The code is here as a pattern how to support such a transfer. */
            if (NRF_USBD_EP_OK == p_event-&amp;gt;data.eptransfer.status)
            {
                /* NOTE: Data values or size may be tested here to decide if clear or stall.
                 * If errata 154 is present the data transfer is acknowledged by the hardware. */
                if (!nrf_drv_usbd_errata_154())
                {
                    /* Transfer ok - allow status stage */
                    nrf_drv_usbd_setup_clear();
                }
            }
            else if (NRF_USBD_EP_ABORTED == p_event-&amp;gt;data.eptransfer.status)
            {
                /* Just ignore */
                NRF_LOG_INFO(&amp;quot;Transfer aborted event on EPOUT0&amp;quot;);
            }
            else
            {
                NRF_LOG_ERROR(&amp;quot;Transfer failed on EPOUT0: %d&amp;quot;, p_event-&amp;gt;data.eptransfer.status);
                nrf_drv_usbd_setup_stall();
            }
        }
        else
        {
            /* Nothing to do */
        }
        break;
    case NRF_DRV_USBD_EVT_SETUP:
        {
            nrf_drv_usbd_setup_t setup;
            nrf_drv_usbd_setup_get(&amp;amp;setup);
            switch (setup.bmRequest)
            {
            case 0x00: // GetStatus
                usbd_setup_GetStatus(&amp;amp;setup);
                break;
            case 0x01: // CleartFeature
                usbd_setup_ClearFeature(&amp;amp;setup);
                break;
            case 0x03: // SetFeature
                usbd_setup_SetFeature(&amp;amp;setup);
                break;
            case 0x05: // SetAddress
                //nothing to do, handled by hardware; but don&amp;#39;t STALL
                break;
            case 0x06: // GetDescriptor
                usbd_setup_GetDescriptor(&amp;amp;setup);
                break;
            case 0x08: // GetConfig
                usbd_setup_GetConfig(&amp;amp;setup);
                break;
            case 0x09: // SetConfig
                usbd_setup_SetConfig(&amp;amp;setup);
                break;
            //HID class
            case 0x0A: // SetIdle
                usbd_setup_SetIdle(&amp;amp;setup);
                break;
            case 0x0B: // SetProtocol or SetInterface
                if (setup.bmRequestType == 0x01) // standard request, recipient=interface
                {
                    usbd_setup_SetInterface(&amp;amp;setup);
                }
                else if (setup.bmRequestType == 0x21) // class request, recipient=interface
                {
                    usbd_setup_SetProtocol(&amp;amp;setup);
                }
                else
                {
                    NRF_LOG_ERROR(&amp;quot;Command 0xB. Unknown request: 0x%2x&amp;quot;, setup.bmRequestType);
                    nrf_drv_usbd_setup_stall();
                }
                break;
            default:
                NRF_LOG_ERROR(&amp;quot;Unknown request: 0x%2x&amp;quot;, setup.bmRequest);
                nrf_drv_usbd_setup_stall();
                return;
            }
            break;
        }
    default:
        break;
    }
}


static void move_mouse_pointer(void)
{
    static uint32_t databuffer;

    if (!m_usbd_configured)
        return;
    if (!m_send_mouse_position)
    {
        switch (m_mouse_position &amp;amp; 0x3)
        {
        case 0:
            /* X = 10, rest all are unchanged */
            databuffer = 0x00000A00;
            break;
        case 1:
            /* Y = 10, rest all are unchanged */
            databuffer = 0x000A0000;
            break;
        case 2:
            /* X = -10, rest all are unchanged */
            databuffer = 0x0000F600;
            break;
        case 3:
            /* Y = -10, rest all are unchanged */
            databuffer = 0x00F60000;
            break;
        }
        m_mouse_position++;

        /* Send data */
        static const nrf_drv_usbd_transfer_t transfer =
        {
            .p_data = {.tx = &amp;amp;databuffer},
            .size = sizeof(databuffer)
        };
        m_send_mouse_position = true;
        UNUSED_RETURN_VALUE(nrf_drv_usbd_ep_transfer(
            NRF_DRV_USBD_EPIN1,
            &amp;amp;transfer));
    }
}

static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
{
    switch (event)
    {
    case NRF_DRV_POWER_USB_EVT_DETECTED:
        NRF_LOG_INFO(&amp;quot;USB power detected&amp;quot;);
        if (!nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_enable();
        }
        break;
    case NRF_DRV_POWER_USB_EVT_REMOVED:
        NRF_LOG_INFO(&amp;quot;USB power removed&amp;quot;);
        m_usbd_configured = false;
        m_send_mouse_position = false;
        if (nrf_drv_usbd_is_started())
        {
            nrf_drv_usbd_stop();
        }
        if (nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_disable();
        }
        /* Turn OFF LEDs */
        bsp_board_led_off(LED_USB_STATUS);
        bsp_board_led_off(LED_USB_POWER);
        break;
    case NRF_DRV_POWER_USB_EVT_READY:
        NRF_LOG_INFO(&amp;quot;USB ready&amp;quot;);
        bsp_board_led_on(LED_USB_POWER);
        if (!nrf_drv_usbd_is_started())
        {
            nrf_drv_usbd_start(true);
        }
        break;
    default:
        ASSERT(false);
    }
}

static void bsp_evt_handler(bsp_event_t evt)
{
    switch ((unsigned int)evt)
    {
    case BSP_EVENT_SYSOFF:
    {
        m_system_off_req = true;
        break;
    }
    case CONCAT_2(BSP_EVENT_KEY_, BTN_DATA_SEND):
    {
        m_send_flag = 1;
        break;
    }
    
    case BTN_DATA_KEY_RELEASE:
    {
        m_send_flag = 0;
        break;
    }
    default:
        return;
    }
}

static void init_power_clock(void)
{
    ret_code_t ret;
    /* Initializing power and clock */
    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
    ret = nrf_drv_power_init(NULL);
    APP_ERROR_CHECK(ret);
    nrf_drv_clock_hfclk_request(NULL);
    nrf_drv_clock_lfclk_request(NULL);
    while (!(nrf_drv_clock_hfclk_is_running() &amp;amp;&amp;amp;
            nrf_drv_clock_lfclk_is_running()))
    {
        /* Just waiting */
    }

    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    /* Avoid warnings if assertion is disabled */
    UNUSED_VARIABLE(ret);
}

static void init_bsp(void)
{
    ret_code_t ret;
    ret = bsp_init(BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(ret);

    ret = bsp_event_to_button_action_assign(
        BTN_SYSTEM_OFF,
        BSP_BUTTON_ACTION_RELEASE,
        BSP_EVENT_SYSOFF);
    APP_ERROR_CHECK(ret);
    ret = bsp_event_to_button_action_assign(BTN_DATA_SEND,
                                            BSP_BUTTON_ACTION_RELEASE,
                                            BTN_DATA_KEY_RELEASE);
    APP_ERROR_CHECK(ret);
    /* Avoid warnings if assertion is disabled */
    UNUSED_VARIABLE(ret);
}

static void init_cli(void)
{
    ret_code_t ret;
    ret = bsp_cli_init(bsp_evt_handler);
    APP_ERROR_CHECK(ret);
    nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
//    uart_config.pseltxd = TX_PIN_NUMBER;
//    uart_config.pselrxd = RX_PIN_NUMBER;
    uart_config.hwfc    = NRF_UART_HWFC_DISABLED;
    ret = nrf_cli_init(&amp;amp;m_cli_uart, &amp;amp;uart_config, true, true, NRF_LOG_SEVERITY_INFO);
    APP_ERROR_CHECK(ret);
    ret = nrf_cli_start(&amp;amp;m_cli_uart);
    APP_ERROR_CHECK(ret);
}

static void log_resetreason(void)
{
    /* Reset reason */
    uint32_t rr = nrf_power_resetreas_get();
    NRF_LOG_INFO(&amp;quot;Reset reasons:&amp;quot;);
    if (0 == rr)
    {
        NRF_LOG_INFO(&amp;quot;- NONE&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_RESETPIN_MASK))
    {
        NRF_LOG_INFO(&amp;quot;- RESETPIN&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_DOG_MASK     ))
    {
        NRF_LOG_INFO(&amp;quot;- DOG&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_SREQ_MASK    ))
    {
        NRF_LOG_INFO(&amp;quot;- SREQ&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_LOCKUP_MASK  ))
    {
        NRF_LOG_INFO(&amp;quot;- LOCKUP&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_OFF_MASK     ))
    {
        NRF_LOG_INFO(&amp;quot;- OFF&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_LPCOMP_MASK  ))
    {
        NRF_LOG_INFO(&amp;quot;- LPCOMP&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_DIF_MASK     ))
    {
        NRF_LOG_INFO(&amp;quot;- DIF&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_NFC_MASK     ))
    {
        NRF_LOG_INFO(&amp;quot;- NFC&amp;quot;);
    }
    if (0 != (rr &amp;amp; NRF_POWER_RESETREAS_VBUS_MASK    ))
    {
        NRF_LOG_INFO(&amp;quot;- VBUS&amp;quot;);
    }
}

// End of added functions for USBD



BLE_NUS_C_DEF(m_ble_nus_c);                                             /**&amp;lt; BLE Nordic UART Service (NUS) client instance. */
NRF_BLE_GATT_DEF(m_gatt);                                               /**&amp;lt; GATT module instance. */
BLE_DB_DISCOVERY_DEF(m_db_disc);                                        /**&amp;lt; Database discovery module instance. */
NRF_BLE_SCAN_DEF(m_scan);                                               /**&amp;lt; Scanning Module instance. */

static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; /**&amp;lt; Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */

/**@brief NUS UUID. */
static ble_uuid_t const m_nus_uuid =
{
    .uuid = BLE_UUID_NUS_SERVICE,
    .type = NUS_SERVICE_UUID_TYPE
};


/**@brief Function for handling asserts in the SoftDevice.
 *
 * @details This function is called in case of an assert in the SoftDevice.
 *
 * @warning This handler is only an example and is not meant for the final product. You need to analyze
 *          how your product is supposed to react in case of assert.
 * @warning On assert from the SoftDevice, the system can only recover on reset.
 *
 * @param[in] line_num     Line number of the failing assert call.
 * @param[in] p_file_name  File name of the failing assert call.
 */
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
    app_error_handler(0xDEADBEEF, line_num, p_file_name);
}


/**@brief Function for starting scanning. */
static void scan_start(void)
{
    ret_code_t ret;

    ret = nrf_ble_scan_start(&amp;amp;m_scan);
    APP_ERROR_CHECK(ret);

    ret = bsp_indication_set(BSP_INDICATE_SCANNING);
    APP_ERROR_CHECK(ret);
}


/**@brief Function for handling Scanning Module events.
 */
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
    ret_code_t err_code;

    switch(p_scan_evt-&amp;gt;scan_evt_id)
    {
         case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
         {
              err_code = p_scan_evt-&amp;gt;params.connecting_err.err_code;
              APP_ERROR_CHECK(err_code);
         } break;

         case NRF_BLE_SCAN_EVT_CONNECTED:
         {
              ble_gap_evt_connected_t const * p_connected =
                               p_scan_evt-&amp;gt;params.connected.p_connected;
             // Scan is automatically stopped by the connection.
             NRF_LOG_INFO(&amp;quot;Connecting to target %02x%02x%02x%02x%02x%02x&amp;quot;,
                      p_connected-&amp;gt;peer_addr.addr[0],
                      p_connected-&amp;gt;peer_addr.addr[1],
                      p_connected-&amp;gt;peer_addr.addr[2],
                      p_connected-&amp;gt;peer_addr.addr[3],
                      p_connected-&amp;gt;peer_addr.addr[4],
                      p_connected-&amp;gt;peer_addr.addr[5]
                      );
         } break;

         case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
         {
             NRF_LOG_INFO(&amp;quot;Scan timed out.&amp;quot;);
             scan_start();
         } break;

         default:
             break;
    }
}


/**@brief Function for initializing the scanning and setting the filters.
 */
static void scan_init(void)
{
    ret_code_t          err_code;
    nrf_ble_scan_init_t init_scan;

    memset(&amp;amp;init_scan, 0, sizeof(init_scan));

    init_scan.connect_if_match = true;
    init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;

    err_code = nrf_ble_scan_init(&amp;amp;m_scan, &amp;amp;init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filter_set(&amp;amp;m_scan, SCAN_UUID_FILTER, &amp;amp;m_nus_uuid);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filters_enable(&amp;amp;m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling database discovery events.
 *
 * @details This function is a callback function to handle events from the database discovery module.
 *          Depending on the UUIDs that are discovered, this function forwards the events
 *          to their respective services.
 *
 * @param[in] p_event  Pointer to the database discovery event.
 */
static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
{
    ble_nus_c_on_db_disc_evt(&amp;amp;m_ble_nus_c, p_evt);
}


/**@brief Function for handling characters received by the Nordic UART Service (NUS).
 *
 * @details This function takes a list of characters of length data_len and prints the characters out on UART.
 *          If @ref ECHOBACK_BLE_UART_DATA is set, the data is sent back to sender.
 */
static void ble_nus_chars_received_uart_print(uint8_t * p_data, uint16_t data_len)
{
    ret_code_t ret_val;

    NRF_LOG_DEBUG(&amp;quot;Receiving data.&amp;quot;);
    NRF_LOG_HEXDUMP_DEBUG(p_data, data_len);

    for (uint32_t i = 0; i &amp;lt; data_len; i++)
    {
        do
        {
            ret_val = app_uart_put(p_data[i]);
            if ((ret_val != NRF_SUCCESS) &amp;amp;&amp;amp; (ret_val != NRF_ERROR_BUSY))
            {
                NRF_LOG_ERROR(&amp;quot;app_uart_put failed for index 0x%04x.&amp;quot;, i);
                APP_ERROR_CHECK(ret_val);
            }
        } while (ret_val == NRF_ERROR_BUSY);
    }
    if (p_data[data_len-1] == &amp;#39;\r&amp;#39;)
    {
        while (app_uart_put(&amp;#39;\n&amp;#39;) == NRF_ERROR_BUSY);
    }
    if (ECHOBACK_BLE_UART_DATA)
    {
        // Send data back to the peripheral.
        do
        {
            ret_val = ble_nus_c_string_send(&amp;amp;m_ble_nus_c, p_data, data_len);
            if ((ret_val != NRF_SUCCESS) &amp;amp;&amp;amp; (ret_val != NRF_ERROR_BUSY))
            {
                NRF_LOG_ERROR(&amp;quot;Failed sending NUS message. Error 0x%x. &amp;quot;, ret_val);
                APP_ERROR_CHECK(ret_val);
            }
        } while (ret_val == NRF_ERROR_BUSY);
    }
}


/**@brief   Function for handling app_uart events.
 *
 * @details This function receives a single character from the app_uart module and appends it to
 *          a string. The string is sent over BLE when the last character received is a
 *          &amp;#39;new line&amp;#39; &amp;#39;\n&amp;#39; (hex 0x0A) or if the string reaches the maximum data length.
 */
void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint16_t index = 0;
    uint32_t ret_val;

    switch (p_event-&amp;gt;evt_type)
    {
        /**@snippet [Handling data from UART] */
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&amp;amp;data_array[index]));
            index++;

            if ((data_array[index - 1] == &amp;#39;\n&amp;#39;) || (index &amp;gt;= (m_ble_nus_max_data_len)))
            {
                NRF_LOG_DEBUG(&amp;quot;Ready to send data over BLE NUS&amp;quot;);
                NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                do
                {
                    ret_val = ble_nus_c_string_send(&amp;amp;m_ble_nus_c, data_array, index);
                    if ( (ret_val != NRF_ERROR_INVALID_STATE) &amp;amp;&amp;amp; (ret_val != NRF_ERROR_RESOURCES) )
                    {
                        APP_ERROR_CHECK(ret_val);
                    }
                } while (ret_val == NRF_ERROR_RESOURCES);

                index = 0;
            }
            break;

        /**@snippet [Handling data from UART] */
        case APP_UART_COMMUNICATION_ERROR:
            NRF_LOG_ERROR(&amp;quot;Communication error occurred while handling UART.&amp;quot;);
            APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            NRF_LOG_ERROR(&amp;quot;Error occurred in FIFO module used by UART.&amp;quot;);
            APP_ERROR_HANDLER(p_event-&amp;gt;data.error_code);
            break;

        default:
            break;
    }
}


/**@brief Callback handling Nordic UART Service (NUS) client events.
 *
 * @details This function is called to notify the application of NUS client events.
 *
 * @param[in]   p_ble_nus_c   NUS client handle. This identifies the NUS client.
 * @param[in]   p_ble_nus_evt Pointer to the NUS client event.
 */

/**@snippet [Handling events from the ble_nus_c module] */
static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt)
{
    ret_code_t err_code;

    switch (p_ble_nus_evt-&amp;gt;evt_type)
    {
        case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
            NRF_LOG_INFO(&amp;quot;Discovery complete.&amp;quot;);
            err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt-&amp;gt;conn_handle, &amp;amp;p_ble_nus_evt-&amp;gt;handles);
            APP_ERROR_CHECK(err_code);

            err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
            APP_ERROR_CHECK(err_code);
            NRF_LOG_INFO(&amp;quot;Connected to device with Nordic UART Service.&amp;quot;);
            break;

        case BLE_NUS_C_EVT_NUS_TX_EVT:
            ble_nus_chars_received_uart_print(p_ble_nus_evt-&amp;gt;p_data, p_ble_nus_evt-&amp;gt;data_len);
            break;

        case BLE_NUS_C_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected.&amp;quot;);
            scan_start();
            break;
    }
}
/**@snippet [Handling events from the ble_nus_c module] */


/**
 * @brief Function for handling shutdown events.
 *
 * @param[in]   event       Shutdown type.
 */
static bool shutdown_handler(nrf_pwr_mgmt_evt_t event)
{
    ret_code_t err_code;

    err_code = bsp_indication_set(BSP_INDICATE_IDLE);
    APP_ERROR_CHECK(err_code);

    switch (event)
    {
        case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
            // Prepare wakeup buttons.
            err_code = bsp_btn_ble_sleep_mode_prepare();
            APP_ERROR_CHECK(err_code);
            break;

        default:
            break;
    }

    return true;
}

NRF_PWR_MGMT_HANDLER_REGISTER(shutdown_handler, APP_SHUTDOWN_HANDLER_PRIORITY);


/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t            err_code;
    ble_gap_evt_t const * p_gap_evt = &amp;amp;p_ble_evt-&amp;gt;evt.gap_evt;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            err_code = ble_nus_c_handles_assign(&amp;amp;m_ble_nus_c, p_ble_evt-&amp;gt;evt.gap_evt.conn_handle, NULL);
            APP_ERROR_CHECK(err_code);

            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);

            // start discovery of services. The NUS Client waits for a discovery result
            err_code = ble_db_discovery_start(&amp;amp;m_db_disc, p_ble_evt-&amp;gt;evt.gap_evt.conn_handle);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:

            NRF_LOG_INFO(&amp;quot;Disconnected. conn_handle: 0x%x, reason: 0x%x&amp;quot;,
                         p_gap_evt-&amp;gt;conn_handle,
                         p_gap_evt-&amp;gt;params.disconnected.reason);
            break;

        case BLE_GAP_EVT_TIMEOUT:
            if (p_gap_evt-&amp;gt;params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
            {
                NRF_LOG_INFO(&amp;quot;Connection Request timed out.&amp;quot;);
            }
            break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported.
            err_code = sd_ble_gap_sec_params_reply(p_ble_evt-&amp;gt;evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
            // Accepting parameters requested by peer.
            err_code = sd_ble_gap_conn_param_update(p_gap_evt-&amp;gt;conn_handle,
                                                    &amp;amp;p_gap_evt-&amp;gt;params.conn_param_update_request.conn_params);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            NRF_LOG_DEBUG(&amp;quot;PHY update request.&amp;quot;);
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt-&amp;gt;evt.gap_evt.conn_handle, &amp;amp;phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Client Timeout.&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Server Timeout.&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            break;
    }
}


/**@brief Function for initializing the BLE stack.
 *
 * @details Initializes the SoftDevice and the BLE event interrupt.
 */
static void ble_stack_init(void)
{
    ret_code_t err_code;

    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    uint32_t ram_start = 0;
    err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    // Enable BLE stack.
    err_code = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(err_code);

    // Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}


/**@brief Function for handling events from the GATT library. */
void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
{
    if (p_evt-&amp;gt;evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)
    {
        NRF_LOG_INFO(&amp;quot;ATT MTU exchange completed.&amp;quot;);

        m_ble_nus_max_data_len = p_evt-&amp;gt;params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
        NRF_LOG_INFO(&amp;quot;Ble NUS max data length set to 0x%X(%d)&amp;quot;, m_ble_nus_max_data_len, m_ble_nus_max_data_len);
    }
}


/**@brief Function for initializing the GATT library. */
void gatt_init(void)
{
    ret_code_t err_code;

    err_code = nrf_ble_gatt_init(&amp;amp;m_gatt, gatt_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_gatt_att_mtu_central_set(&amp;amp;m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling events from the BSP module.
 *
 * @param[in] event  Event generated by button press.
 */
void bsp_event_handler(bsp_event_t event)
{
    ret_code_t err_code;

    switch (event)
    {
        case BSP_EVENT_SLEEP:
            nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
            break;

        case BSP_EVENT_DISCONNECT:
            err_code = sd_ble_gap_disconnect(m_ble_nus_c.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            if (err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;

        default:
            break;
    }
}

/**@brief Function for initializing the UART. */
static void uart_init(void)
{
    ret_code_t err_code;

    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
        .baud_rate    = UART_BAUDRATE_BAUDRATE_Baud115200
    };

    APP_UART_FIFO_INIT(&amp;amp;comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);

    APP_ERROR_CHECK(err_code);
}

/**@brief Function for initializing the Nordic UART Service (NUS) client. */
static void nus_c_init(void)
{
    ret_code_t       err_code;
    ble_nus_c_init_t init;

    init.evt_handler = ble_nus_c_evt_handler;

    err_code = ble_nus_c_init(&amp;amp;m_ble_nus_c, &amp;amp;init);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for initializing buttons and leds. */
static void buttons_leds_init(void)
{
    ret_code_t err_code;
    bsp_event_t startup_event;

    err_code = bsp_init(BSP_INIT_LEDS, bsp_event_handler);
    APP_ERROR_CHECK(err_code);

    err_code = bsp_btn_ble_init(NULL, &amp;amp;startup_event);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for initializing the timer. */
static void timer_init(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for initializing the nrf log module. */
static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}


/**@brief Function for initializing power management.
 */
static void power_management_init(void)
{
    ret_code_t err_code;
    err_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(err_code);
}


/** @brief Function for initializing the database discovery module. */
static void db_discovery_init(void)
{
    ret_code_t err_code = ble_db_discovery_init(db_disc_handler);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling the idle state (main loop).
 *
 * @details Handles any pending log operations, then sleeps until the next event occurs.
 */
static void idle_state_handle(void)
{
    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }
}


int main(void)
  {
    // From USBD main 
    ret_code_t ret;
    UNUSED_RETURN_VALUE(NRF_LOG_INIT(NULL));

    init_power_clock();
    init_bsp();
    init_cli();
    log_resetreason();
    nrf_power_resetreas_clear(nrf_power_resetreas_get());
    /* USB work starts right here */
    ret = nrf_drv_usbd_init(usbd_event_handler);
    APP_ERROR_CHECK(ret);

    /* Configure selected size of the packed on EP0 */
    nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPOUT0, EP0_MAXPACKETSIZE);
    nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPIN0, EP0_MAXPACKETSIZE);

    /* Configure LED and button */
    bsp_board_init(BSP_INIT_LEDS);
    bsp_board_led_on(LED_RUNNING);
    bsp_board_led_on(LED_ACTIVE);


    if (USBD_POWER_DETECTION)
    {
        static const nrf_drv_power_usbevt_config_t config =
        {
            .handler = power_usb_event_handler
        };
        ret = nrf_drv_power_usbevt_init(&amp;amp;config);
        APP_ERROR_CHECK(ret);
    }
    else
    {
        NRF_LOG_INFO(&amp;quot;No USB power detection enabled\r\nStarting USB now&amp;quot;);
        nrf_delay_us(STARTUP_DELAY);
        if (!nrf_drv_usbd_is_enabled())
        {
            nrf_drv_usbd_enable();
            ret = ep_configuration(0);
            APP_ERROR_CHECK(ret);
        }
        /* Wait for regulator power up */
        while (NRF_DRV_POWER_USB_STATE_CONNECTED
              ==
              nrf_drv_power_usbstatus_get())
        {
            /* Just waiting */
        }

        if (NRF_DRV_POWER_USB_STATE_READY == nrf_drv_power_usbstatus_get())
        {
            if (!nrf_drv_usbd_is_started())
            {
                nrf_drv_usbd_start(true);
            }
        }
        else
        {
            nrf_drv_usbd_disable();
        }
    }
    // End of the first part of the USBD main

    // Initialize.
    log_init();
    timer_init();
    uart_init();
    buttons_leds_init();
    db_discovery_init();
    power_management_init();
    ble_stack_init();
    gatt_init();
    nus_c_init();
    scan_init();

    // Start execution.
    printf(&amp;quot;BLE UART central example started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;BLE UART central example started.&amp;quot;);
    scan_start();

    // Added second part of the main
        while (true)
    {
        if (m_system_off_req)
        {
            NRF_LOG_INFO(&amp;quot;Going to system OFF&amp;quot;);
            NRF_LOG_FLUSH();
            bsp_board_led_off(LED_RUNNING);
            bsp_board_led_off(LED_ACTIVE);
            nrf_power_system_off();
        }
        if (m_usbd_suspended != m_usbd_suspend_state_req)
        {
            if (m_usbd_suspend_state_req)
            {
                m_usbd_suspended = nrf_drv_usbd_suspend();
                if (m_usbd_suspended)
                {
                    bsp_board_leds_off();
                }
            }
            else
            {
                m_usbd_suspended = false;
            }
        }

        if (m_usbd_configured)
        {
            if (m_send_flag)
            {
                if (m_usbd_suspended)
                {
                    if (m_usbd_rwu_enabled)
                    {
                        UNUSED_RETURN_VALUE(nrf_drv_usbd_wakeup_req());
                    }
                }
                else
                {
                    NRF_LOG_INFO(&amp;quot;   TX pointer&amp;quot;);
                    move_mouse_pointer();
                }
            }
        }

         nrf_cli_process(&amp;amp;m_cli_uart);
        UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
        bsp_board_led_off(LED_RUNNING);
        /* Even if we miss an event enabling USB,
         * USB event would wake us up. */
        __WFE();
        /* Clear SEV flag if CPU was woken up by event */
        __SEV();
        __WFE();
        bsp_board_led_on(LED_RUNNING);

    // End of second part of the main

    // Enter main loop.
    
    for (;;)
    {
        idle_state_handle();
    }
 }
 }
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Help is appreciated Andreas,&lt;/p&gt;
&lt;p&gt;Maher&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176202?ContentTypeID=1</link><pubDate>Thu, 14 Mar 2019 13:21:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d4177780-33db-42ec-91b3-f512fcfd297b</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;If you use the nrfx driver (you have the file nrfx_usbd.c) in your project, you also have to enable that.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#ifndef NRFX_USBD_ENABLED
#define NRFX_USBD_ENABLED 1&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176159?ContentTypeID=1</link><pubDate>Thu, 14 Mar 2019 11:55:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a773add0-aa4c-4a70-bfcd-f6d39eb9d519</guid><dc:creator>Maher Shakir</dc:creator><description>&lt;p&gt;Thanks for your reply. Yes I have that enabled. This is the config.h: &lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/1031.sdk_5F00_config.h"&gt;devzone.nordicsemi.com/.../1031.sdk_5F00_config.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The problems are:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/app_usbd.o: In function `sustate_set&amp;#39;:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/app_usbd.o: In function `app_usbd_init&amp;#39;:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/main.o: In function `init_cli&amp;#39;:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/main.o: In function `idle_handle&amp;#39;:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/main.o: In function `hid_user_ev_handler&amp;#39;:&lt;/p&gt;
&lt;p&gt;1&amp;gt; Output/ble_app_multilink_central_pca10056_s140 Release/Obj/main.o: In function `main&amp;#39;:&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: combining multilink central with usb generic</title><link>https://devzone.nordicsemi.com/thread/176078?ContentTypeID=1</link><pubDate>Thu, 14 Mar 2019 07:06:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71cf7ede-1f55-46a1-be4a-821262c7d513</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi Maher.&lt;/p&gt;
&lt;p&gt;Have you enabled the USBD driver?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// &amp;lt;e&amp;gt; USBD_ENABLED - nrf_drv_usbd - Software Component
//==========================================================
#ifndef USBD_ENABLED
#define USBD_ENABLED 1
#endif

// &amp;lt;e&amp;gt; APP_USBD_ENABLED - app_usbd - USB Device library
//==========================================================
#ifndef APP_USBD_ENABLED
#define APP_USBD_ENABLED 1
#endif&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>