Example #1
0
/*****************************************************************************
 * Interface functions
 *****************************************************************************/
uint32_t mesh_gatt_init(uint32_t access_address, uint8_t channel,
		uint32_t interval_min_ms) {
	uint32_t error_code;
	mesh_metadata_char_t md_char;
	md_char.mesh_access_addr = access_address;
	md_char.mesh_interval_min_ms = interval_min_ms;
	md_char.mesh_channel = channel;

	ble_uuid_t ble_srv_uuid;
	ble_srv_uuid.type = BLE_UUID_TYPE_BLE;
	ble_srv_uuid.uuid = MESH_SRV_UUID;

	error_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
			&ble_srv_uuid, &m_mesh_service.service_handle);
	if (error_code != NRF_SUCCESS) {
		return NRF_ERROR_INTERNAL;
	}

	error_code = sd_ble_uuid_vs_add(&m_mesh_base_uuid, &m_mesh_base_uuid_type);
	if (error_code != NRF_SUCCESS) {
		return error_code;
	}

	error_code = mesh_md_char_add(&md_char);
	if (error_code != NRF_SUCCESS) {
		return error_code;
	}

	error_code = mesh_value_char_add();
	if (error_code != NRF_SUCCESS) {
		return error_code;
	}

	return NRF_SUCCESS;
}
uint32_t mesh_srv_init(uint8_t mesh_value_count, 
    uint32_t access_address, uint8_t channel, uint32_t adv_int_ms)
{
    if (mesh_value_count > MAX_VALUE_COUNT)
    {
        return NRF_ERROR_INVALID_PARAM;
    }
    
    if (is_initialized)
    {
        return NRF_ERROR_INVALID_STATE;
    }
    
    is_initialized = true;
    
    ble_enable_params_t ble_enable_params;
    ble_enable_params.gatts_enable_params.service_changed = 0;
    
    sd_ble_enable(&ble_enable_params);
    
    g_mesh_service.value_count = mesh_value_count;
    
    ble_uuid_t ble_srv_uuid;
    
    /* add the mesh base UUID */
    
    uint32_t error_code = sd_ble_uuid_vs_add(&mesh_base_uuid, &mesh_base_uuid_type);
    if (error_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }
    
    ble_srv_uuid.type = mesh_base_uuid_type;
    ble_srv_uuid.uuid = MESH_SRV_UUID;
    
    error_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, 
        &ble_srv_uuid, &g_mesh_service.service_handle);
    
    if (error_code != NRF_SUCCESS)
    {
        return NRF_ERROR_INTERNAL;
    }
    
    /* Add metadata characteristic */
    mesh_metadata_char_t mesh_metadata;
    mesh_metadata.mesh_access_addr = access_address;
    mesh_metadata.mesh_adv_int_ms = adv_int_ms;
    mesh_metadata.mesh_channel = channel;
    mesh_metadata.mesh_value_count = mesh_value_count;
    
    mesh_md_char_add(&mesh_metadata);
    
    uint32_t md_len = sizeof(mesh_char_metadata_t) * g_mesh_service.value_count;
    
    /* allocate metadata array */
    g_mesh_service.char_metadata = (mesh_char_metadata_t*) malloc(md_len);    
    memset(g_mesh_service.char_metadata, 0, md_len);
    
    /* add characteristics to mesh service */
    for (uint8_t i = 0; i < g_mesh_service.value_count; ++i)
    {
        error_code = mesh_value_char_add(i);
        
        if (error_code != NRF_SUCCESS)
        {
            return error_code;
        }   
    }
    
    trickle_setup(1000 * adv_int_ms, 600, 3);
    
    return NRF_SUCCESS;
}