<commit_before>/************************************************************
 * <bsn.cl fy=2014 v=onl>
 * 
 *        Copyright 2014, 2015 Big Switch Networks, Inc.       
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 *        http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 * 
 * </bsn.cl>
 ************************************************************
 *
 *
 *
 ***********************************************************/
#include <onlp/sys.h>
#include <onlp/platformi/sysi.h>
#include <onlplib/mmap.h>
#include <AIM/aim.h>
#include "onlp_log.h"
#include "onlp_int.h"
#include "onlp_locks.h"

static char*
platform_detect_fs__(int warn)
{
    /*
     * Check the filesystem for the platform identifier.
     */
    char* rv = NULL;
    if(ONLP_CONFIG_PLATFORM_FILENAME) {
        FILE* fp;
        if((fp=fopen(ONLP_CONFIG_PLATFORM_FILENAME, "r"))) {
            char platform[256];
            if(fgets(platform, sizeof(platform), fp) == platform) {
                /* TODO: Base this detection on the global platform registry. */
                if(platform[0]) {
                    if(platform[ONLP_STRLEN(platform)-1] == '\n') {
                        platform[ONLP_STRLEN(platform)-1] = 0;
                    }
                    rv = aim_strdup(platform);
                }
            }
            fclose(fp);
        }
        else {
            if(warn) {
                AIM_LOG_WARN("could not open platform filename '%s'", ONLP_CONFIG_PLATFORM_FILENAME);
            }
        }
    }
    return rv;
}

static char*
platform_detect__(void)
{
#if ONLP_CONFIG_INCLUDE_PLATFORM_STATIC == 1
    return ONLP_CONFIG_PLATFORM_STATIC ;
#endif
    return platform_detect_fs__(1);
}

static int
onlp_sys_init_locked__(void)
{
    int rv;

    const char* current_platform = platform_detect__();
    if(current_platform == NULL) {
        AIM_DIE("Could not determine the current platform.");
    }
    const char* current_interface = onlp_sysi_platform_get();
    if(current_interface == NULL) {
        AIM_DIE("The platform driver did not return an appropriate platform identifier.");
    }

    if(strcmp(current_interface, current_platform)) {
        /* They do not match. Ask the interface if it supports the current platform. */
        int rv = onlp_sysi_platform_set(current_platform);
        if(rv < 0) {
            AIM_DIE("The current platform interface (%s) does not support the current platform (%s). This is fatal.",
                    current_interface, current_platform);
        }
    }

    /* If we get here, its all good */
    rv = onlp_sysi_init();
    return rv;
}
ONLP_LOCKED_API0(onlp_sys_init);

static uint8_t*
onie_data_get__(int* free)
{
    void* pa;
    uint8_t* ma = NULL;
    int size;
    if(onlp_sysi_onie_data_phys_addr_get(&pa) == 0) {
        ma = onlp_mmap((off_t)pa, 64*1024, "onie_data_get__");
        *free = 0;
    }
    else if(onlp_sysi_onie_data_get(&ma, &size) == 0) {
        *free = 1;
    }
    else {
        ma = NULL;
        *free = 0;
    }
    return ma;
}

static int
onlp_sys_info_get_locked__(onlp_sys_info_t* rv)
{
    if(rv == NULL) {
        return -1;
    }

    memset(rv, 0, sizeof(*rv));

    /**
     * Get the system ONIE information.
     */
    int free;
    uint8_t* onie_data = onie_data_get__(&free);

    if(onie_data) {
        onlp_onie_decode(&rv->onie_info, onie_data, -1);
        if(free) {
            onlp_sysi_onie_data_free(onie_data);
        }
    }
    else {
        onlp_sysi_onie_info_get(&rv->onie_info);
    }

    /*
     * Query the sys oids
     */
    onlp_sysi_oids_get(rv->hdr.coids, AIM_ARRAYSIZE(rv->hdr.coids));

    /*
     * Platform Information
     */
    onlp_sysi_platform_info_get(&rv->platform_info);

    return 0;
}
ONLP_LOCKED_API1(onlp_sys_info_get,onlp_sys_info_t*,rv);

void
onlp_sys_info_free(onlp_sys_info_t* info)
{
    onlp_onie_info_free(&info->onie_info);
}

void
onlp_sys_dump(onlp_oid_t id, aim_pvs_t* pvs, uint32_t flags)
{
    int rv;
    iof_t iof;
    onlp_sys_info_t si;

    onlp_oid_dump_iof_init_default(&iof, pvs);

    if(ONLP_OID_TYPE_GET(id) != ONLP_OID_TYPE_SYS) {
        return;
    }

    iof_push(&iof, "System Information");
    rv = onlp_sys_info_get(&si);
    if(rv < 0) {<commit_msg>Fix minor memory leak.<commit_after>/************************************************************
 * <bsn.cl fy=2014 v=onl>
 *
 *        Copyright 2014, 2015 Big Switch Networks, Inc.
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *        http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *
 * </bsn.cl>
 ************************************************************
 *
 *
 *
 ***********************************************************/
#include <onlp/sys.h>
#include <onlp/platformi/sysi.h>
#include <onlplib/mmap.h>
#include <AIM/aim.h>
#include "onlp_log.h"
#include "onlp_int.h"
#include "onlp_locks.h"

static char*
platform_detect_fs__(int warn)
{
    /*
     * Check the filesystem for the platform identifier.
     */
    char* rv = NULL;
    if(ONLP_CONFIG_PLATFORM_FILENAME) {
        FILE* fp;
        if((fp=fopen(ONLP_CONFIG_PLATFORM_FILENAME, "r"))) {
            char platform[256];
            if(fgets(platform, sizeof(platform), fp) == platform) {
                /* TODO: Base this detection on the global platform registry. */
                if(platform[0]) {
                    if(platform[ONLP_STRLEN(platform)-1] == '\n') {
                        platform[ONLP_STRLEN(platform)-1] = 0;
                    }
                    rv = aim_strdup(platform);
                }
            }
            fclose(fp);
        }
        else {
            if(warn) {
                AIM_LOG_WARN("could not open platform filename '%s'", ONLP_CONFIG_PLATFORM_FILENAME);
            }
        }
    }
    return rv;
}

static char*
platform_detect__(void)
{
#if ONLP_CONFIG_INCLUDE_PLATFORM_STATIC == 1
    return aim_strdup(ONLP_CONFIG_PLATFORM_STATIC);
#endif
    return platform_detect_fs__(1);
}

static int
onlp_sys_init_locked__(void)
{
    int rv;

    const char* current_platform = platform_detect__();
    if(current_platform == NULL) {
        AIM_DIE("Could not determine the current platform.");
    }
    const char* current_interface = onlp_sysi_platform_get();
    if(current_interface == NULL) {
        AIM_DIE("The platform driver did not return an appropriate platform identifier.");
    }

    if(strcmp(current_interface, current_platform)) {
        /* They do not match. Ask the interface if it supports the current platform. */
        int rv = onlp_sysi_platform_set(current_platform);
        if(rv < 0) {
            AIM_DIE("The current platform interface (%s) does not support the current platform (%s). This is fatal.",
                    current_interface, current_platform);
        }
    }

    /* If we get here, its all good */
    aim_free((char*)current_platform);
    rv = onlp_sysi_init();
    return rv;
}
ONLP_LOCKED_API0(onlp_sys_init);

static uint8_t*
onie_data_get__(int* free)
{
    void* pa;
    uint8_t* ma = NULL;
    int size;
    if(onlp_sysi_onie_data_phys_addr_get(&pa) == 0) {
        ma = onlp_mmap((off_t)pa, 64*1024, "onie_data_get__");
        *free = 0;
    }
    else if(onlp_sysi_onie_data_get(&ma, &size) == 0) {
        *free = 1;
    }
    else {
        ma = NULL;
        *free = 0;
    }
    return ma;
}

static int
onlp_sys_info_get_locked__(onlp_sys_info_t* rv)
{
    if(rv == NULL) {
        return -1;
    }

    memset(rv, 0, sizeof(*rv));

    /**
     * Get the system ONIE information.
     */
    int free;
    uint8_t* onie_data = onie_data_get__(&free);

    if(onie_data) {
        onlp_onie_decode(&rv->onie_info, onie_data, -1);
        if(free) {
            onlp_sysi_onie_data_free(onie_data);
        }
    }
    else {
        onlp_sysi_onie_info_get(&rv->onie_info);
    }

    /*
     * Query the sys oids
     */
    onlp_sysi_oids_get(rv->hdr.coids, AIM_ARRAYSIZE(rv->hdr.coids));

    /*
     * Platform Information
     */
    onlp_sysi_platform_info_get(&rv->platform_info);

    return 0;
}
ONLP_LOCKED_API1(onlp_sys_info_get,onlp_sys_info_t*,rv);

void
onlp_sys_info_free(onlp_sys_info_t* info)
{
    onlp_onie_info_free(&info->onie_info);
    onlp_sysi_platform_info_free(&info->platform_info);
}

void
onlp_sys_dump(onlp_oid_t id, aim_pvs_t* pvs, uint32_t flags)
{
    int rv;
    iof_t iof;
    onlp_sys_info_t si;

    onlp_oid_dump_iof_init_default(&iof, pvs);

    if(ONLP_OID_TYPE_GET(id) != ONLP_OID_TYPE_SYS) {
        return;
    }

    iof_push(&iof, "System Information");
    rv = onlp_sys_info_get(&si);
    if(rv < 0) {
{'fraction_non_alphanumeric': 0.14516636926629578, 'fraction_numerical': 0.010286554004408524, 'mean_word_length': 2.6547756041426926, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '900166', 'n_tokens_mistral': 3317, 'n_tokens_neox': 3205, 'n_words': 711}