Ejemplo n.º 1
0
/**@brief ICMP6 module event handler.
 *
 * @details Callback registered with the ICMP6 module to receive asynchronous events from
 *          the module, if the ICMP6_ENABLE_ALL_MESSAGES_TO_APPLICATION constant is not zero
 *          or the ICMP6_ENABLE_ND6_MESSAGES_TO_APPLICATION constant is not zero.
 */
uint32_t icmp6_handler(iot_interface_t * p_interface,
                       ipv6_header_t   * p_ip_header,
                       icmp6_header_t  * p_icmp_header,
                       uint32_t          process_result,
                       iot_pbuffer_t   * p_rx_packet)
{
    APPL_LOG("[APPL]: Got ICMP6 Application Handler Event on interface 0x%p\r\n", p_interface);

    APPL_LOG("[APPL]: Source IPv6 Address: ");
    APPL_ADDR(p_ip_header->srcaddr);
    APPL_LOG("[APPL]: Destination IPv6 Address: ");
    APPL_ADDR(p_ip_header->destaddr);
    APPL_LOG("[APPL]: Process result = 0x%08lx\r\n", process_result);

    switch (p_icmp_header->type)
    {
        case ICMP6_TYPE_ECHO_REPLY:
            APPL_LOG("[APPL]: ICMP6 Message Type = Echo Reply\r\n");

            if (m_app_state != APP_STATE_IDLE)
            {
                // Invert LED_FOUR.
                LEDS_INVERT(LED_FOUR);
            }

            // Reset echo request retransmission number.
            m_echo_req_retry_count = 0;
            break;
        default:
            break;
    }

    return NRF_SUCCESS;
}
Ejemplo n.º 2
0
/**@brief Function for application main entry.
 */
int main(void)
{
    // Initialize
    app_trace_init();
    leds_init();

    timers_init();
    buttons_init();
    ipv6_transport_init();
    ip_stack_init();

    uint32_t err_code = coap_init(17);
    APP_ERROR_CHECK(err_code);

    coap_endpoints_init();

    iot_timer_init();

    APPL_LOG("\r\n");
    APPL_LOG("[APPL]: Init complete.\r\n");

    // Start execution
    advertising_start();

    // Enter main loop
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 3
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;

    // Initialize.
    app_trace_init();
    leds_init();
    timers_init();
    gpiote_init();

    err_code = ipv6_transport_init();
    APP_ERROR_CHECK(err_code);

    // Initialize IP Stack.
    ip_stack_init();

    APPL_LOG("\r\n");
    APPL_LOG("[APPL]: Init complete.\r\n");

    // Start execution.
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        /* Sleep waiting for an application event. */
        err_code = sd_app_evt_wait();
        APP_ERROR_CHECK(err_code);
    }
}
Ejemplo n.º 4
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;

#ifdef ENABLE_DEBUG_LOG_SUPPORT
    app_trace_init();
#endif

    APPL_LOG("START\r\n");

    // Initialize.
    timers_init();
    buttons_leds_init(&erase_bonds);
    adc_config();
    ble_stack_init();
    device_manager_init(erase_bonds);
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();

    // Start execution.
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    APPL_LOG("start advertising : %u\n", err_code);

    // Enter main loop.
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 5
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
            {
        case BLE_GAP_EVT_CONNECTED:
            APPL_LOG("BLE_GAP_EVT_CONNECTED : %u\n", p_ble_evt->header.evt_id);
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

            //err_code = app_timer_start(m_app_timer_id, APP_TIMER_ADC_INTERVAL, NULL);
            //APP_ERROR_CHECK(err_code);
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            APPL_LOG("BLE_GAP_EVT_DISCONNECTED : %u\n", p_ble_evt->header.evt_id);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            //nrf_adc_stop();
            //app_timer_stop(m_app_timer_id);
            break;

        default:
            // No implementation needed.
            break;
    }
}
Ejemplo n.º 6
0
void app_mqtt_evt_handler(const mqtt_client_t * p_client, const mqtt_evt_t * p_evt)
{
    uint32_t err_code;
    switch(p_evt->id)
    {
        case MQTT_EVT_CONNECTED:
        {
            APPL_LOG ("[APPL]: >> MQTT_EVT_CONNECTED\r\n");
            if(p_evt->result == MQTT_SUCCESS)
            {
                m_connection_state = true;
                m_display_state = LEDS_CONNECTED_TO_BROKER;
                err_code = app_timer_start(m_led_blink_timer, LED_BLINK_INTERVAL, NULL);
                APP_ERROR_CHECK(err_code);
            }
            break;
        }
        case MQTT_EVT_DISCONNECTED:
        {
            APPL_LOG ("[APPL]: >> MQTT_EVT_DISCONNECTED\r\n");
            m_connection_state = false;
            m_display_state = LEDS_IPV6_IF_UP;
            err_code = app_timer_start(m_led_blink_timer, LED_BLINK_INTERVAL, NULL);
            APP_ERROR_CHECK(err_code);
            break;
        }
        default:
            break;
    }
}
Ejemplo n.º 7
0
static void ip_app_handler(iot_interface_t * p_interface, ipv6_event_t * p_event)
{
    uint32_t    err_code;
    ipv6_addr_t src_addr;

    APPL_LOG("[APPL]: Got IP Application Handler Event on interface 0x%p\r\n", p_interface);

    switch (p_event->event_id)
    {
        case IPV6_EVT_INTERFACE_ADD:
#ifdef COMMISSIONING_ENABLED
            commissioning_joining_mode_timer_ctrl(JOINING_MODE_TIMER_STOP_RESET);
#endif // COMMISSIONING_ENABLED
            APPL_LOG("[APPL]: New interface added!\r\n");
            mp_interface = p_interface;

            m_display_state = LEDS_IPV6_IF_UP;

            APPL_LOG("[APPL]: Sending Router Solicitation to all routers!\r\n");

            // Create Link Local addresses
            IPV6_CREATE_LINK_LOCAL_FROM_EUI64(&src_addr, p_interface->local_addr.identifier);

            // Delay first solicitation due to possible restriction on other side.
            nrf_delay_ms(APP_RTR_SOLICITATION_DELAY);

            // Send Router Solicitation to all routers.
            err_code = icmp6_rs_send(p_interface,
                                     &src_addr,
                                     &m_local_routers_multicast_addr);
            APP_ERROR_CHECK(err_code);
        break;

        case IPV6_EVT_INTERFACE_DELETE:
#ifdef COMMISSIONING_ENABLED
            commissioning_joining_mode_timer_ctrl(JOINING_MODE_TIMER_START);
#endif // COMMISSIONING_ENABLED
            APPL_LOG("[APPL]: Interface removed!\r\n");
            mp_interface = NULL;

            m_display_state = LEDS_IPV6_IF_DOWN;

            // Stop application state machine timer.
            m_app_state = APP_STATE_IDLE;
            err_code = app_timer_stop(m_app_timer);
            APP_ERROR_CHECK(err_code);
            break;

        case IPV6_EVT_INTERFACE_RX_DATA:
            APPL_LOG("[APPL]: Got unsupported protocol data!\r\n");
            break;

        default:
            // Unknown event. Should not happen.
            break;
    }
}
Ejemplo n.º 8
0
/**@brief Function for handling button events.
 *
 * @param[in]   pin_no         The pin number of the button pressed.
 * @param[in]   button_action  The action performed on button.
 */
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    uint32_t err_code;
#ifdef COMMISSIONING_ENABLED
    if ((button_action == APP_BUTTON_PUSH) && (pin_no == ERASE_BUTTON_PIN_NO))
    {
        APPL_LOG("[APPL]: Erasing all commissioning settings from persistent storage... \r\n");
        commissioning_settings_clear();
        return;
    }
#endif // COMMISSIONING_ENABLED

    // Check if interface is UP.
    if(mp_interface == NULL)
    {
        return;
    }

    if (button_action == APP_BUTTON_PUSH)
    {
        switch (pin_no)
        {
            case START_BUTTON_PIN_NO:
            {
                APPL_LOG("[APPL]: Start button has been pushed.\r\n");

                // Change application state in case being in IDLE state.
                if(m_app_state == APP_STATE_IDLE)
                {
                    m_app_state = APP_STATE_QUERYING;

                    // Start application state machine timer.
                    err_code = app_timer_start(m_app_timer, APP_STATE_INTERVAL, NULL);
                    APP_ERROR_CHECK(err_code);
                }

                break;
            }
            case STOP_BUTTON_PIN_NO:
            {
                APPL_LOG("[APPL]: Stop button has been pushed.\r\n");
                LEDS_OFF((LED_THREE | LED_FOUR));

                // Back to IDLE state.
                m_app_state = APP_STATE_IDLE;

                // Stop application state machine timer.
                err_code = app_timer_stop(m_app_timer);
                APP_ERROR_CHECK(err_code);
                break;
            }

            default:
              break;
        }
    }
}
Ejemplo n.º 9
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    // Initialize
    app_trace_init();
    leds_init();

#ifdef COMMISSIONING_ENABLED
    err_code = pstorage_init();
    APP_ERROR_CHECK(err_code);
#endif // COMMISSIONING_ENABLED

    timers_init();
    buttons_init();

    static ipv6_medium_init_params_t ipv6_medium_init_params;
    memset(&ipv6_medium_init_params, 0x00, sizeof(ipv6_medium_init_params));
    ipv6_medium_init_params.ipv6_medium_evt_handler    = on_ipv6_medium_evt;
    ipv6_medium_init_params.ipv6_medium_error_handler  = on_ipv6_medium_error;
    ipv6_medium_init_params.use_scheduler              = false;
#ifdef COMMISSIONING_ENABLED
    ipv6_medium_init_params.commissioning_id_mode_cb   = commissioning_id_mode_cb;
    ipv6_medium_init_params.commissioning_power_off_cb = commissioning_power_off_cb;
#endif // COMMISSIONING_ENABLED

    err_code = ipv6_medium_init(&ipv6_medium_init_params, \
                                IPV6_MEDIUM_ID_BLE,       \
                                &m_ipv6_medium);
    APP_ERROR_CHECK(err_code);

    eui48_t ipv6_medium_eui48;
    err_code = ipv6_medium_eui48_get(m_ipv6_medium.ipv6_medium_instance_id, \
                                     &ipv6_medium_eui48);

    ipv6_medium_eui48.identifier[EUI_48_SIZE - 1] = 0x00;

    err_code = ipv6_medium_eui48_set(m_ipv6_medium.ipv6_medium_instance_id, \
                                     &ipv6_medium_eui48);
    APP_ERROR_CHECK(err_code);

    ip_stack_init();
    ip_stack_timer_init();
    dns_client_init();

    APPL_LOG("\r\n");
    APPL_LOG("[APPL]: Init complete.\r\n");

    // Start execution
    connectable_mode_enter();

    // Enter main loop
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 10
0
/**@brief IP stack event handler.
 *
 * @details Callback registered with the ICMP6 to receive asynchronous events from the module.
 */
void ip_app_handler(iot_interface_t * p_interface, ipv6_event_t * p_event)
{
    uint32_t err_code;

    APPL_LOG("[APPL]: Got IP Application Handler Event on interface 0x%p\r\n", p_interface);

    switch(p_event->event_id)
    {
        case IPV6_EVT_INTERFACE_ADD:
            APPL_LOG("[APPL]: New interface added!\r\n");

            err_code = udp6_socket_allocate(&m_udp_socket);
            APP_ERROR_CHECK(err_code);

            err_code = udp6_socket_bind(&m_udp_socket, IPV6_ADDR_ANY, HTONS(UDP_PORT));
            APP_ERROR_CHECK(err_code);

            err_code = udp6_socket_recv(&m_udp_socket, rx_udp_port_app_handler);
            APP_ERROR_CHECK(err_code);

            memset(&m_packet_buffer[0][0], 0x00, sizeof(m_packet_buffer));
            m_node_state    = APP_STATE_IPV6_IF_UP;
            m_display_state = LEDS_TX_ECHO_REQUEST;

            // IPv6 interface is up, start sending Echo Requests to peer.
            err_code = app_timer_start(m_tx_node_timer, APP_PING_INTERVAL, NULL);
            APP_ERROR_CHECK(err_code);

            break;
        case IPV6_EVT_INTERFACE_DELETE:
            err_code = app_timer_stop(m_tx_node_timer);
            APP_ERROR_CHECK(err_code);

            APPL_LOG("[APPL]: Interface removed!\r\n");

            err_code = udp6_socket_free(&m_udp_socket);
            APP_ERROR_CHECK(err_code);

            memset(&m_packet_buffer[0][0], 0x00, sizeof(m_packet_buffer));
            m_node_state    = APP_STATE_IPV6_IF_DOWN;
            m_display_state = LEDS_IPV6_IF_DOWN;

            break;
        case IPV6_EVT_INTERFACE_RX_DATA:
            APPL_LOG("[APPL]: Got unsupported protocol data!\r\n");
            break;

        default:
            //Unknown event. Should not happen.
            break;
    }
}
Ejemplo n.º 11
0
/**@brief Battery level Collector Handler. ****************************************
 */
static void bas_c_evt_handler(ble_bas_c_t * p_bas_c, ble_bas_c_evt_t * p_bas_c_evt)
{
    uint32_t err_code;

    switch (p_bas_c_evt->evt_type)
    {
        case BLE_BAS_C_EVT_DISCOVERY_COMPLETE:
            // Battery service discovered. Enable notification of Battery Level.
            APPL_LOG("[APPL]: Battery Service discovered. \r\n");

            APPL_LOG("[APPL]: Reading battery level. \r\n");

            err_code = ble_bas_c_bl_read(p_bas_c);
            APP_ERROR_CHECK(err_code);

            APPL_LOG("[APPL]: Enabling Battery Level Notification. \r\n");
            err_code = ble_bas_c_bl_notif_enable(p_bas_c);
            APP_ERROR_CHECK(err_code);
            printf("Battery service discovered \r\n");

            break;

        case BLE_BAS_C_EVT_BATT_NOTIFICATION:
        {
            APPL_LOG("[APPL]: Battery Level received %d %%\r\n", p_bas_c_evt->params.battery_level);
	    
	    uint8_t batteryLevel = p_bas_c_evt->params.battery_level;
	    // Normalize the battery level
	    //if(batteryLevel > 100) batteryLevel = 100;
	    batteryLevel = batteryLevel < 100 ? batteryLevel : 100;
            printf("batteryLevel = %d %%\r\n", batteryLevel);
	    while(app_pwm_channel_duty_set(&PWM1, 0, batteryLevel) == NRF_ERROR_BUSY)

            printf("Battery = %d %%\r\n", batteryLevel);
            break;
        }

        case BLE_BAS_C_EVT_BATT_READ_RESP:
        {
            APPL_LOG("[APPL]: Battery Level Read as %d %%\r\n", p_bas_c_evt->params.battery_level);

            //printf("Battery Read As = %d %%\r\n", p_bas_c_evt->params.battery_level);
            break;
        }

        default:
            break;
    }
}
Ejemplo n.º 12
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            APPL_LOG ("[APPL]: Connected.\r\n");
            break;
        case BLE_GAP_EVT_DISCONNECTED:
            APPL_LOG ("[APPL]: Disconnected.\r\n");
            advertising_start();
            break;
        default:
            break;
    }
}
Ejemplo n.º 13
0
/**@brief Publishes LED state to MQTT broker.
 *
 * @param[in]   led_state   LED state being published.
 */
static void app_mqtt_publish(bool led_state)
{
    mqtt_topic_t topic;
    mqtt_data_t  data;

    char topic_desc[] = "led/state";

    topic.p_topic = (uint8_t *)topic_desc;
    topic.topic_len = strlen(topic_desc);

    data.data_len = 1;
    data.p_data = (uint8_t *)&led_state;

    uint32_t err_code = mqtt_publish(&m_app_mqtt_id,&topic, &data);
    APPL_LOG("[APPL]: mqtt_publish result 0x%08lx\r\n", err_code);
    if (err_code == MQTT_SUCCESS)
    {
        LEDS_INVERT(LED_FOUR);
        m_led_state = !m_led_state;
    }
    else
    {
        // Flash LED_THREE if error occurs.
        LEDS_ON(LED_THREE);
        err_code = app_timer_start(m_led_blink_timer, LED_BLINK_INTERVAL, NULL);
        APP_ERROR_CHECK(err_code);
    }
}
Ejemplo n.º 14
0
/**@brief Function for initializing the BLE stack.
 *
 * @details Initializes the SoftDevice and the BLE event interrupt.
 */
static void ble_stack_init(void)
{
    uint32_t err_code;

    // Initialize the SoftDevice handler module.
    SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC, NULL);

#if defined(S110) || defined(S130)
    // Enable BLE stack.
    ble_enable_params_t ble_enable_params;
    memset(&ble_enable_params, 0, sizeof(ble_enable_params));
#ifdef S130
    //ble_enable_params.gatts_enable_params.attr_tab_size   = BLE_GATTS_ATTR_TAB_SIZE_MIN;    //これだと自分のATTが出てこなかった
    ble_enable_params.gatts_enable_params.attr_tab_size   = BLE_GATTS_ATTR_TAB_SIZE_MIN;    //これだと自分のATTが出てこなかった
    ble_enable_params.gap_enable_params.periph_conn_count = 1;
#endif
    ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
#ifdef S130
    uint32_t app_ram_base = (uint32_t)m_ble_buf;
    err_code = sd_ble_enable(&ble_enable_params, &app_ram_base);
    APPL_LOG("app_ram_base : %x\n", app_ram_base);
#else
    err_code = sd_ble_enable(&ble_enable_params);
#endif
    APP_ERROR_CHECK(err_code);
#endif

    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);

    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
    APP_ERROR_CHECK(err_code);
}
/**@brief Function for sending the configuration. */
uint32_t ble_agsensor_configuration_send(ble_agsensor_t * p_agsensor, ble_agsensor_configuration_t * p_configuration)
{
    uint32_t err_code = NRF_SUCCESS;

    if (p_agsensor->conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        ble_gatts_hvx_params_t hvx_params;
        uint8_t encoded_value[MAX_CONFIGURATION_LEN];
        uint16_t hvx_len;

        // Initialize value struct.
        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_len           = configuration_encode(p_configuration, encoded_value);
        hvx_params.handle = p_agsensor->configuration_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_INDICATION;
        hvx_params.p_len  = &hvx_len;
        hvx_params.offset = 0;
        hvx_params.p_data = encoded_value;

        err_code = sd_ble_gatts_hvx(p_agsensor->conn_handle, &hvx_params);
		APPL_LOG("ble_agsensor_configuration_send %d %d\r\n", (int)err_code, p_agsensor->conn_handle);
//		APPL_LOG("ble_agsensor_configuration_send %d %d %d %d\r\n", hvx_params.handle, hvx_params.type, (int)*hvx_params.p_len, hvx_params.offset);
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
    }

    return err_code;
}
Ejemplo n.º 16
0
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;

    app_trace_init();
    // Initialize.
    timers_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    device_manager_init(erase_bonds);
    gap_params_init();
    advertising_init();
    services_init();
    conn_params_init();

    // Start execution.
    application_timers_start();
    APPL_LOG("Start Advertising \r\n");
    printf("Dhavaltest\r\n");
    SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal Sample\r\n");
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);

    // Enter main loop.
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 17
0
uint32_t ser_sd_transport_cmd_write(const uint8_t *                p_buffer,
                                    uint16_t                       length,
                                    ser_sd_transport_rsp_handler_t cmd_rsp_decode_callback)
{
    uint32_t err_code = NRF_SUCCESS;

    m_rsp_wait        = true;
    m_rsp_dec_handler = cmd_rsp_decode_callback;
    err_code          = ser_hal_transport_tx_pkt_send(p_buffer, length);
    APP_ERROR_CHECK(err_code);

    /* Execute callback for response decoding only if one was provided.*/
    if ((err_code == NRF_SUCCESS) && cmd_rsp_decode_callback)
    {
        if (m_ot_rsp_wait_handler)
        {
            m_ot_rsp_wait_handler();
            m_ot_rsp_wait_handler = NULL;
        }

        m_os_rsp_wait_handler();
        err_code = m_return_value;
    }
    else
    {
        m_rsp_wait = false;
    }
    APPL_LOG("\r\n[SD_CALL_ID]: 0x%X, err_code= 0x%X\r\n", p_buffer[1], err_code);
    return err_code;
}
Ejemplo n.º 18
0
static void coap_response_handler(uint32_t status, void * arg, coap_message_t * p_response)
{
    if (status == NRF_SUCCESS)
    {
        if (coap_message_opt_present(p_response, COAP_OPT_OBSERVE) == NRF_SUCCESS)
        {
            // It is an notification or result of a GET response
            LEDS_ON(LED_FOUR);
        }
        else
        {
            LEDS_OFF(LED_FOUR);
        }


        APPL_LOG("[APPL]: Response Code : %d\r\n", p_response->header.code);
        if (p_response->header.code == COAP_CODE_205_CONTENT)
        {
            switch (p_response->p_payload[0])
            {
                case STATUS_ON:
                    LEDS_ON(LED_THREE);
                    break;

                case STATUS_OFF:
                    LEDS_OFF(LED_THREE);
                    break;

                default:
                    break;
            }
        }
    }
}
static uint32_t ble_agsensor_update_config (ble_agsensor_t * p_agsensor) {

    uint32_t err_code = NRF_SUCCESS;
	static ble_gatts_hvx_params_t hvx_params;
	if (isConfigSendWaiting) {

//		APPL_LOG("isConfigSendWaiting %d\r\n", (int)err_code, p_agsensor->conn_handle);
	    hvx_params.handle = p_agsensor->configuration_handles.value_handle;
	    hvx_params.type   = BLE_GATT_HVX_INDICATION;
	    hvx_params.p_len  = &hvx_p_config_len;
	    hvx_params.offset = 0;
	    hvx_params.p_data = hvx_encoded_buffer;

		err_code = sd_ble_gatts_hvx(p_agsensor->conn_handle, &hvx_params);
		isConfigSendWaiting = (!(err_code == NRF_SUCCESS));

		// TODO: this part does not work as it does not confirm that msg been sent
		if (err_code == NRF_ERROR_BUSY) {
			if (*(hvx_params.p_len) == hvx_p_config_len) {
				isConfigSendWaiting = false;
			}
		}

		APPL_LOG("ble_agsensor_update_config %d %d\r\n", (int)err_code, p_agsensor->conn_handle);

//		APPL_LOG("ble_agsensor_update_config %d %d %d %d\r\n", hvx_params.handle, hvx_params.type, (int)*hvx_params.p_len, hvx_params.offset);
//		APPL_LOG("ble_agsensor_update_config %d \r\n", (int)(*hvx_params.p_len));

	}
	return err_code;
}
Ejemplo n.º 20
0
/**@brief Send test data on the port.
 *
  * @details Sends TCP data in Request of size 8 in format described in description above.
 *
 * @param[in]   p_pcb     PCB identifier of the port.
 */
static void tcp_send_data(struct tcp_pcb * p_pcb, uint32_t sequence_number)
{
    err_t  err = ERR_OK;

    if (m_tcp_state != TCP_STATE_DATA_TX_IN_PROGRESS)
    {
        //Register callback to get notification of data reception is complete.
        tcp_sent(p_pcb, tcp_write_complete);
        uint8_t tcp_data[TCP_DATA_SIZE];

        tcp_data[0] = (uint8_t )((sequence_number >> 24) & 0x000000FF);
        tcp_data[1] = (uint8_t )((sequence_number >> 16) & 0x000000FF);
        tcp_data[2] = (uint8_t )((sequence_number >> 8)  & 0x000000FF);
        tcp_data[3] = (uint8_t )(sequence_number         & 0x000000FF);

        tcp_data[4] = 'P';
        tcp_data[5] = 'o';
        tcp_data[6] = 'n';
        tcp_data[7] = 'g';

        //Enqueue data for transmission.
        err = tcp_write(p_pcb, tcp_data, TCP_DATA_SIZE, 1);

        if (err != ERR_OK)
        {
            APPL_LOG ("[APPL]: Failed to send TCP packet, reason %d\r\n", err);
        }
        else
        {
            m_tcp_state = TCP_STATE_DATA_TX_IN_PROGRESS;
        }
    }
void commissioning_power_off_cb(bool power_off_on_failure)
{
    m_power_off_on_failure = power_off_on_failure;

    APPL_LOG("[APPL]: Commissioning: do power_off on failure: %s.\r\n", \
             m_power_off_on_failure ? "true" : "false");
}
Ejemplo n.º 22
0
/**@brief Heart Rate Collector Handler.
 */
static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt)
{
    uint32_t err_code;

    switch (p_hrs_c_evt->evt_type)
    {
        case BLE_HRS_C_EVT_DISCOVERY_COMPLETE:
            // Initiate bonding.
            err_code = dm_security_setup_req(&m_dm_device_handle);
            APP_ERROR_CHECK(err_code);

            // Heart rate service discovered. Enable notification of Heart Rate Measurement.
            err_code = ble_hrs_c_hrm_notif_enable(p_hrs_c);
            APP_ERROR_CHECK(err_code);

            printf("Heart rate service discovered \r\n");
            break;

        case BLE_HRS_C_EVT_HRM_NOTIFICATION:
        {
            APPL_LOG("[APPL]: HR Measurement received %d \r\n", p_hrs_c_evt->params.hrm.hr_value);

            printf("Heart Rate = %d\r\n", p_hrs_c_evt->params.hrm.hr_value);
            break;
        }

        default:
            break;
    }
}
Ejemplo n.º 23
0
/**@brief Function for error handling, which is called when an error has occurred.
 *
 * @warning This handler is an example only and does not fit a final product. You need to analyze
 *          how your product is supposed to react in case of error.
 *
 * @param[in] error_code  Error code supplied to the handler.
 * @param[in] line_num    Line number where the handler is called.
 * @param[in] p_file_name Pointer to the file name.
 */
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    LEDS_ON((LED_ONE | LED_TWO | LED_THREE | LED_FOUR));
    APPL_LOG("[** ASSERT **]: Error 0x%08lX, Line %ld, File %s\r\n", error_code, line_num, p_file_name);

    // Variables used to retain function parameter values when debugging.
    static volatile uint8_t  s_file_name[MAX_LENGTH_FILENAME];
    static volatile uint16_t s_line_num;
    static volatile uint32_t s_error_code;

    strncpy((char *)s_file_name, (const char *)p_file_name, MAX_LENGTH_FILENAME - 1);
    s_file_name[MAX_LENGTH_FILENAME - 1] = '\0';
    s_line_num                           = line_num;
    s_error_code                         = error_code;
    UNUSED_VARIABLE(s_file_name);
    UNUSED_VARIABLE(s_line_num);
    UNUSED_VARIABLE(s_error_code);

    // This call can be used for debug purposes during application development.
    // @note CAUTION: Activating this code will write the stack to flash on an error.
    //                This function should NOT be used in a final product.
    //                It is intended STRICTLY for development/debugging purposes.
    //                The flash write will happen EVEN if the radio is active, thus interrupting
    //                any communication.
    //                Use with care. Un-comment the line below to use.
    // ble_debug_assert_handler(error_code, line_num, p_file_name);

    // On assert, the system can only recover on reset.
    //NVIC_SystemReset();

    for(;;)
    {
        // Infinite loop.
    }
}
Ejemplo n.º 24
0
/**@brief Function for handling the advertising report BLE event.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void on_adv_report(const ble_evt_t * const p_ble_evt)
{
    uint32_t      err_code;
    uint8_array_t adv_data;
    uint8_array_t dev_name;
    bool          do_connect = false;

    // For readibility.
    const ble_gap_evt_t * const p_gap_evt    = &p_ble_evt->evt.gap_evt;
    const ble_gap_addr_t  * const peer_addr  = &p_gap_evt->params.adv_report.peer_addr;

    // Initialize advertisement report for parsing
    adv_data.p_data = (uint8_t *)p_gap_evt->params.adv_report.data;
    adv_data.size   = p_gap_evt->params.adv_report.dlen;


    //search for advertising names
    bool found_name = false;
    err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                                &adv_data,
                                &dev_name);
    if (err_code != NRF_SUCCESS)
    {
        // Look for the short local name if it was not found as complete
        err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, &adv_data, &dev_name);
        if (err_code != NRF_SUCCESS)
        {
            // If we can't parse the data, then exit
            return;
        }
        else
        {
            found_name = true;
        }
    }
    else
    {
        found_name = true;
    }
    if (found_name)
    {
        if (strlen(m_target_periph_name) != 0)
        {
            if(memcmp(m_target_periph_name, dev_name.p_data, dev_name.size) == 0)
            {
                do_connect = true;
            }
        }
    }

    if (do_connect)
    {
        // Initiate connection.
        err_code = sd_ble_gap_connect(peer_addr, &m_scan_param, &m_connection_param);
        if (err_code != NRF_SUCCESS)
        {
            APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n", err_code);
        }
    }
}
/**@brief Function for starting connectable mode.
 */
static void connectable_mode_enter(void)
{
    uint32_t err_code = ipv6_medium_connectable_mode_enter(m_ipv6_medium.ipv6_medium_instance_id);
    APP_ERROR_CHECK(err_code);

    APPL_LOG("[APPL]: Physical layer in connectable mode.\r\n");
    m_display_state = LEDS_CONNECTABLE_MODE;
}
Ejemplo n.º 26
0
/**@brief Function to handle interface up event. */
void nrf51_driver_interface_up(void)
{
    APPL_LOG ("[APPL]: IPv6 interface up.\r\n");

    sys_check_timeouts();

    m_display_state = LEDS_IPV6_IF_UP;
}
Ejemplo n.º 27
0
/**@brief Function for starting connectable mode.
 */
static void connectable_mode_enter(void)
{
    uint32_t err_code = ipv6_medium_connectable_mode_enter(m_ipv6_medium.ipv6_medium_instance_id);
    APP_ERROR_CHECK(err_code);

    APPL_LOG("[APPL]: Physical layer in connectable mode.\r\n");
    LEDS_OFF(LED_TWO);
    LEDS_ON(LED_ONE);
}
Ejemplo n.º 28
0
/**@brief Function to handle interface down event. */
void nrf_driver_interface_down(void)
{   
    APPL_LOG ("[APPL]: IPv6 interface down.\r\n");
#ifdef COMMISSIONING_ENABLED
    commissioning_joining_mode_timer_ctrl(JOINING_MODE_TIMER_START);
#endif // COMMISSIONING_ENABLED
    
    m_display_state = LEDS_IPV6_IF_DOWN;
}
Ejemplo n.º 29
0
/**@brief Function to handle interface up event. */
void nrf51_driver_interface_up(void)
{
    APPL_LOG ("[APPL]: IPv6 Interface Up.\r\n");

    sys_check_timeouts();

    // Set flag indicating interface state.
    m_interface_state = true;
    m_display_state = LEDS_IPV6_IF_UP;
}
Ejemplo n.º 30
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            APPL_LOG ("[APPL]: Connected.\r\n");
            m_display_state = LEDS_IPV6_IF_DOWN;

            break;
        case BLE_GAP_EVT_DISCONNECTED:
            APPL_LOG ("[APPL]: Disconnected.\r\n");
            m_display_state = LEDS_INACTIVE;
            advertising_start();

            break;
        default:
            break;
    }
}