/************************************************************
 * <bsn.cl fy=2014 v=onl>
 *
 *           Copyright 2014 Big Switch Networks, Inc.
 *           Copyright 2015 Accton Technology Corporation.
 *
 * 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/platformi/ledi.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <onlplib/mmap.h>

#include "platform_lib.h"

#define prefix_path "/sys/class/leds/accton_as6812_32x_led::"
#define filename    "brightness"

#define VALIDATE(_id)                           \
    do {                                        \
        if(!ONLP_OID_IS_LED(_id)) {             \
            return ONLP_STATUS_E_INVALID;       \
        }                                       \
    } while(0)

/* LED related data
 */
enum onlp_led_id
{
    LED_RESERVED = 0,
    LED_PSU1,
    LED_PSU2,
    LED_DIAG,
    LED_LOC,
    LED_FAN,
    LED_FAN1,
    LED_FAN2,
    LED_FAN3,
    LED_FAN4,
    LED_FAN5
};
        
enum led_light_mode {
    /* psu led mode */
    LED_MODE_PSU_OFF = 0,
    LED_MODE_PSU_GREEN,
    LED_MODE_PSU_AMBER, 
    LED_MODE_PSU_AUTO = 7,
    
    /* diag led mode*/
    LED_MODE_DIAG_OFF = 0,
    LED_MODE_DIAG_GREEN,
    LED_MODE_DIAG_AMBER, 
    LED_MODE_DIAG_GREEN_BLINK = 4,
    
    /* locator led mode*/
    LED_MODE_LOC_OFF = 0,
    LED_MODE_LOC_AMBER = 2,
    LED_MODE_LOC_AMBER_BLINKING = 5,

    /* fan led mode */
    LED_MODE_FAN_OFF = 0,
    LED_MODE_FAN_GREEN,
    LED_MODE_FAN_AMBER,
    LED_MODE_FAN_AUTO = 7,

    /* fanX led mode */
    LED_MODE_FANX_OFF = 0,
    LED_MODE_FANX_GREEN,
    LED_MODE_FANX_RED = 3
};

typedef struct led_light_mode_map {
    enum onlp_led_id id;
    enum led_light_mode driver_led_mode;
    enum onlp_led_mode_e onlp_led_mode;
} led_light_mode_map_t;

led_light_mode_map_t led_map[] = {
{LED_PSU1, LED_MODE_PSU_OFF,   ONLP_LED_MODE_OFF},
{LED_PSU1, LED_MODE_PSU_AMBER, ONLP_LED_MODE_ORANGE},
{LED_PSU1, LED_MODE_PSU_GREEN, ONLP_LED_MODE_GREEN},
{LED_PSU1, LED_MODE_PSU_AUTO,  ONLP_LED_MODE_AUTO},
{LED_PSU2, LED_MODE_PSU_OFF,   ONLP_LED_MODE_OFF},
{LED_PSU2, LED_MODE_PSU_AMBER, ONLP_LED_MODE_ORANGE},
{LED_PSU2, LED_MODE_PSU_GREEN, ONLP_LED_MODE_GREEN},
{LED_PSU2, LED_MODE_PSU_AUTO,  ONLP_LED_MODE_AUTO},
{LED_DIAG, LED_MODE_DIAG_OFF,   ONLP_LED_MODE_OFF},
{LED_DIAG, LED_MODE_DIAG_AMBER, ONLP_LED_MODE_ORANGE},
{LED_DIAG, LED_MODE_DIAG_GREEN, ONLP_LED_MODE_GREEN},
{LED_DIAG, LED_MODE_DIAG_GREEN_BLINK, ONLP_LED_MODE_GREEN_BLINKING},
{LED_LOC,  LED_MODE_LOC_OFF,   ONLP_LED_MODE_OFF},
{LED_LOC,  LED_MODE_LOC_AMBER,    ONLP_LED_MODE_ORANGE},
{LED_LOC,  LED_MODE_LOC_AMBER_BLINKING, ONLP_LED_MODE_ORANGE_BLINKING},
{LED_FAN,  LED_MODE_FAN_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN,  LED_MODE_FAN_AMBER, ONLP_LED_MODE_ORANGE},
{LED_FAN,  LED_MODE_FAN_GREEN, ONLP_LED_MODE_GREEN},
{LED_FAN,  LED_MODE_FAN_AUTO,  ONLP_LED_MODE_AUTO},
{LED_FAN1, LED_MODE_FANX_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN1, LED_MODE_FANX_RED, ONLP_LED_MODE_RED},
{LED_FAN1, LED_MODE_FANX_GREEN, ONLP_LED_MODE_GREEN},
{LED_FAN2, LED_MODE_FANX_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN2, LED_MODE_FANX_RED, ONLP_LED_MODE_RED},
{LED_FAN2, LED_MODE_FANX_GREEN, ONLP_LED_MODE_GREEN},
{LED_FAN3, LED_MODE_FANX_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN3, LED_MODE_FANX_RED, ONLP_LED_MODE_RED},
{LED_FAN3, LED_MODE_FANX_GREEN, ONLP_LED_MODE_GREEN},
{LED_FAN4, LED_MODE_FANX_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN4, LED_MODE_FANX_RED, ONLP_LED_MODE_RED},
{LED_FAN4, LED_MODE_FANX_GREEN, ONLP_LED_MODE_GREEN},
{LED_FAN5, LED_MODE_FANX_OFF,   ONLP_LED_MODE_OFF},
{LED_FAN5, LED_MODE_FANX_RED, ONLP_LED_MODE_RED},
{LED_FAN5, LED_MODE_FANX_GREEN, ONLP_LED_MODE_GREEN}};

static char last_path[][10] =  /* must map with onlp_led_id */
{
    "reserved",
    "psu1",
    "psu2",
    "diag",
    "loc",
    "fan",
    "fan1",
    "fan2",
    "fan3",
    "fan4",
    "fan5"
};

/*
 * Get the information for the given LED OID.
 */
static onlp_led_info_t linfo[] =
{
    { }, /* Not used */
    {
        { ONLP_LED_ID_CREATE(LED_PSU1), "Chassis LED 1 (PSU1 LED)", 0 },
        ONLP_LED_STATUS_PRESENT,
        ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_ORANGE | ONLP_LED_CAPS_GREEN | ONLP_LED_CAPS_AUTO,
    },
    {
        { ONLP_LED_ID_CREATE(LED_PSU2), "Chassis LED 2 (PSU2 LED)", 0 },
        ONLP_LED_STATUS_PRESENT,
        ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_ORANGE | ONLP_LED_CAPS_GREEN | ONLP_LED_CAPS_AUTO,
    },
    {
        { ONLP_LED_ID_CREATE(LED_DIAG), "Chassis LED 3 (DIAG LED)", 0 },
        ONLP_LED_STATUS_PRESENT,
        ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_ORANGE | ONLP_LED_CAPS_GREEN | ONLP_LED_CAPS_GREEN_BLINKING,
    },
    {
        { ONLP_LED_ID_CREATE(LED_LOC), "Chassis LED 4 (LOC LED)", 0 },
        ONLP_LED_STATUS_PRESENT,
        ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_ORANGE | ONLP_LED_CAPS_ORANGE_BLINKING,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN), "Chassis LED 5 (FAN LED)", 0 },
        ONLP_LED_STATUS_PRESENT,
        ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_ORANGE | ONLP_LED_CAPS_GREEN | ONLP_LED_CAPS_AUTO,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN1), "Chassis LED 6 (FAN1 LED)", 0 },
        ONLP_LED_STATUS_PRESENT, 0,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN2), "Chassis LED 7 (FAN2 LED)", 0 },
        ONLP_LED_STATUS_PRESENT, 0,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN3), "Chassis LED 8 (FAN3 LED)", 0 },
        ONLP_LED_STATUS_PRESENT, 0,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN4), "Chassis LED 9 (FAN4 LED)", 0 },
        ONLP_LED_STATUS_PRESENT, 0,
    },
    {
        { ONLP_LED_ID_CREATE(LED_FAN5), "Chassis LED 10(FAN5 LED)", 0 },
        ONLP_LED_STATUS_PRESENT, 0,
    }
};

static int driver_to_onlp_led_mode(enum onlp_led_id id, enum led_light_mode driver_led_mode)
{
    int i, nsize = sizeof(led_map)/sizeof(led_map[0]);
    
    for (i = 0; i < nsize; i++)
    {
        if (id == led_map[i].id && driver_led_mode == led_map[i].driver_led_mode)
        {
            return led_map[i].onlp_led_mode;
        }
    }
    
    return 0;
}

static int onlp_to_driver_led_mode(enum onlp_led_id id, onlp_led_mode_t onlp_led_mode)
{
    int i, nsize = sizeof(led_map)/sizeof(led_map[0]);
    
    for(i = 0; i < nsize; i++)
    {
        if (id == led_map[i].id && onlp_led_mode == led_map[i].onlp_led_mode)
        {
            return led_map[i].driver_led_mode;
        }
    }
    
    return 0;
}

/*
 * This function will be called prior to any other onlp_ledi_* functions.
 */
int
onlp_ledi_init(void)
{
    /* Turn off Diag LED */
    onlp_ledi_mode_set(ONLP_LED_ID_CREATE(LED_DIAG), ONLP_LED_MODE_OFF);
    return ONLP_STATUS_OK;
}

int
onlp_ledi_info_get(onlp_oid_t id, onlp_led_info_t* info)
{
    int  local_id;
	char data[2] = {0};
    char fullpath[50] = {0};
		
    VALIDATE(id);
	
    local_id = ONLP_OID_ID_GET(id);
    		
    /* get fullpath */
    sprintf(fullpath, "%s%s/%s", prefix_path, last_path[local_id], filename);
		
	/* Set the onlp_oid_hdr_t and capabilities */
    *info = linfo[ONLP_OID_ID_GET(id)];

    /* Set LED mode */
    if (deviceNodeReadString(fullpath, data, sizeof(data), 0) != 0) {
        DEBUG_PRINT("%s(%d)\r\n", __FUNCTION__, __LINE__);
        return ONLP_STATUS_E_INTERNAL;
    }

    info->mode = driver_to_onlp_led_mode(local_id, atoi(data));

    /* Set the on/off status */
    if (info->mode != ONLP_LED_MODE_OFF) {
        info->status |= ONLP_LED_STATUS_ON;
    }

    return ONLP_STATUS_OK;
}

/*
 * Turn an LED on or off.
 *
 * This function will only be called if the LED OID supports the ONOFF
 * capability.
 *
 * What 'on' means in terms of colors or modes for multimode LEDs is
 * up to the platform to decide. This is intended as baseline toggle mechanism.
 */
int
onlp_ledi_set(onlp_oid_t id, int on_or_off)
{
    VALIDATE(id);

    if (!on_or_off) {
        return onlp_ledi_mode_set(id, ONLP_LED_MODE_OFF);
    }

    return ONLP_STATUS_E_UNSUPPORTED;
}

/*
 * This function puts the LED into the given mode. It is a more functional
 * interface for multimode LEDs.
 *
 * Only modes reported in the LED's capabilities will be attempted.
 */
int
onlp_ledi_mode_set(onlp_oid_t id, onlp_led_mode_t mode)
{
    int  local_id;
    char fullpath[50] = {0};		

    VALIDATE(id);
	
    local_id = ONLP_OID_ID_GET(id);
    sprintf(fullpath, "%s%s/%s", prefix_path, last_path[local_id], filename);	
    
    if (deviceNodeWriteInt(fullpath, onlp_to_driver_led_mode(local_id, mode), 0) != 0)
    {
        return ONLP_STATUS_E_INTERNAL;
    }

    return ONLP_STATUS_OK;
}

/*
 * Generic LED ioctl interface.
 */
int
onlp_ledi_ioctl(onlp_oid_t id, va_list vargs)
{
    return ONLP_STATUS_E_UNSUPPORTED;
}

