led: add led api

Change-Id: I6d124e6ad583e897adb22371cac6a695eec3b0cb
diff --git a/mbtk/mbtk_ril/src/main.c b/mbtk/mbtk_ril/src/main.c
index 4ed52ce..eeabc7c 100755
--- a/mbtk/mbtk_ril/src/main.c
+++ b/mbtk/mbtk_ril/src/main.c
@@ -25,6 +25,7 @@
 #include "mbtk_ntp.h"

 #include "mbtk_net_control.h"

 #include "info_data.h"

+#include "mbtk_led.h"

 

 #define TEMP_FAILURE_RETRY(exp) ({         \

     typeof (exp) _rc;                      \

@@ -419,6 +420,14 @@
             data[1] = 0;  // GMS/WCDMA

         } else {

             data[1] = 1;  // LTE

+            if(data[0] == 1)

+            {

+                mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);

+            }

+            else

+            {

+                mbtk_net_led_set(MBTK_NET_LED_SEARCH_NETWORK);

+            }

         }

 

         urc_msg_distribute(true, INFO_URC_MSG_NET_PS_REG_STATE, data, sizeof(data));

@@ -1804,6 +1813,10 @@
         LOGE("mbtk_info_server_start() fail.");

         return -1;

     }

+    

+#ifdef MBTK_PROJECT_T108

+    mbtk_led_init();

+#endif 

 

 #if 0

     if(uevent_main())

diff --git a/mbtk/mbtk_ril/src/mbtk_info_server.c b/mbtk/mbtk_ril/src/mbtk_info_server.c
index 861c64b..3fc1a86 100755
--- a/mbtk/mbtk_ril/src/mbtk_info_server.c
+++ b/mbtk/mbtk_ril/src/mbtk_info_server.c
@@ -19,6 +19,7 @@
 #include "mbtk_utils.h"
 #include "mbtk_ifc.h"
 #include "info_data.h"
+#include "mbtk_led.h"
 
 static int sock_listen_fd = -1;
 static int epoll_fd = -1;
@@ -5306,7 +5307,45 @@
                 }
                 break;
             }
+			case MBTK_INFO_ID_LED_REQ:
+            {
+                 if(pack->data_len == 0 || pack->data == NULL)
+                {
+                    err = MBTK_INFO_ERR_UNSUPPORTED;
+                    LOGE("led param is error.");
+                }
+                else
+                {
+                    char type = pack->data[0];
+                    char status = pack->data[1];
+                    LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
 
+                    if(type == MBTK_LED_TYPE_NET)
+                    {
+                        if(status == MBTK_LED_STATUS_CLOSE)
+                        {
+                            mbtk_net_led_set(MBTK_NET_LED_CLOSE);
+                        }
+                        else
+                        {
+                            mbtk_net_led_set(MBTK_NET_LED_OPEN);
+                        }
+                    }
+                    else
+                    {
+                        if(status == MBTK_LED_STATUS_CLOSE)
+                        {
+                            status_led_set(MBTK_STATUS_LED_CLOSE);
+                        }
+                        else
+                        {
+                            status_led_set(MBTK_STATUS_LED_OPEN);
+                        }
+                    }
+                    pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
+                }
+                break;
+            }
             default:
             {
                 err = MBTK_INFO_ERR_REQ_UNKNOWN;
diff --git a/mbtk/mbtk_ril/src/mbtk_led.c b/mbtk/mbtk_ril/src/mbtk_led.c
new file mode 100644
index 0000000..5c11d5e
--- /dev/null
+++ b/mbtk/mbtk_ril/src/mbtk_led.c
@@ -0,0 +1,294 @@
+#if 1
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pthread.h>
+#include <string.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#include "mbtk_led.h"
+#include "mbtk_log.h"
+
+/****************************DEFINE***************************************/
+#define MBTK_RESULT_FAIL   -1
+#define MBTK_RESULT_SUCCESS 0
+
+#define MBTK_NET_LED 99
+#define MBTK_STATUS_LED 8
+/****************************DEFINE***************************************/
+
+/****************************VARIABLE***************************************/
+static mbtk_net_led_type mbtk_net_led_state = MBTK_NET_LED_SEARCH_NETWORK;
+static mbtk_net_led_type mbtk_net_led_prev_state = MBTK_NET_LED_SEARCH_NETWORK;
+static mbtk_status_led_type mbtk_status_led_state = MBTK_STATUS_LED_CLOSE;
+/****************************VARIABLE***************************************/
+
+/******************************FUNC*****************************************/
+int mbtk_read_ccinet(void) 
+{
+    FILE *fp;
+    int ret = 0;
+    char buffer[1024]; 
+    char type[10]={0};
+    
+    fp = popen("ifconfig", "r"); 
+    if (fp == NULL) {
+        printf("popen fail\n");
+        return -1;
+    }
+  
+    fread(buffer,1, sizeof(buffer) -1, fp);
+    //LOGE("[led]buffer:%s\n", buffer);
+    pclose(fp); // 
+
+    char *p1 = strstr(buffer, "ccinet");
+    if(p1 == NULL)
+    {
+    	LOGE("[led]read ccinet is null\n");
+    	return -1;
+    }
+
+    return 0;
+}
+
+int mbtk_net_led_get(void)
+{
+    if(mbtk_net_led_state == MBTK_NET_LED_OPEN)
+    {
+        mbtk_net_led_state = mbtk_net_led_prev_state;
+    }
+
+    int ret = -1;
+    if(mbtk_net_led_state == MBTK_NET_LED_NET_CONNECT)
+    {
+        ret = mbtk_read_ccinet();
+        if(ret == 0)
+        {
+            mbtk_net_led_state = MBTK_NET_LED_DATA_CONNECT;
+        }
+    }
+    return mbtk_net_led_state;
+}
+
+void mbtk_net_led_set(mbtk_net_led_type status)
+{
+    if(mbtk_net_led_state == MBTK_NET_LED_CLOSE)
+    {
+        if(status == MBTK_NET_LED_CLOSE || status == MBTK_NET_LED_OPEN)
+        {
+            //
+        }
+        else
+        {
+            mbtk_net_led_prev_state = status;
+        }
+    }
+    else
+    {
+        if(status == MBTK_NET_LED_CLOSE)
+        {
+            mbtk_net_led_prev_state = mbtk_net_led_state;
+        }
+    }
+
+    mbtk_net_led_state = status;
+}
+
+static int mbtk_led_gpio_init(int gpio)
+{
+    char buffer[50]= {0};
+    char pin_index_buffer[5]= {0};
+    int fd =-1;
+    int result = -1;
+
+    memset(buffer,0,50);
+    sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
+    if(access(buffer , F_OK) != 0)
+    {
+        fd = open("/sys/class/gpio/export",O_WRONLY);
+        if(fd < -1)
+        {
+            LOGE("[led]Open export fail.", gpio);
+            return MBTK_RESULT_FAIL;
+        }
+
+        memset(pin_index_buffer,0,5);
+        sprintf(pin_index_buffer,"%d", gpio);
+        result = write(fd,pin_index_buffer,strlen(pin_index_buffer));
+        close(fd);
+        if(result < 0)
+        {
+            LOGE("[led]Gpio[%d] export fail.", gpio);
+            return MBTK_RESULT_FAIL;
+        }
+        fd = -1;
+    }
+
+    fd = open(buffer, O_WRONLY);
+    if(fd < -1)
+    {
+        LOGE("[led]Open gpio[%d] direct fail.", gpio);
+        return MBTK_RESULT_FAIL;
+    }
+
+    result = write(fd, "out",strlen("out"));
+    close(fd);
+    if(result < 0)
+    {
+        LOGE("[led]Set gpio[%d] direct fail.", gpio);
+        return MBTK_RESULT_FAIL;
+    }
+    return MBTK_RESULT_SUCCESS;
+}
+
+static int mbtk_led_gpio_level_set(int gpio, int value)
+{
+    char buffer[50]= {0};
+    int fd =-1;
+    int result =-1;
+
+    memset(buffer,0,50);
+    sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio);
+    fd = open(buffer,O_WRONLY);
+    if(fd < -1)
+    {
+        LOGE("[led]Open gpio[%d] value fail.", gpio);
+        return MBTK_RESULT_FAIL;
+    }
+    if(value == 0)
+    {
+        result = write(fd,"0",1);
+    }
+    else
+    {
+        result = write(fd,"1",1);
+    }
+    close(fd);
+    
+    if(result != 1)
+    {
+        LOGE("[led]Set gpio[%d] value fail.", gpio);
+        return MBTK_RESULT_FAIL;
+    }
+
+    return MBTK_RESULT_SUCCESS;
+}
+
+static void net_led_close(void)
+{
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 0);
+}
+
+static void net_led_open(void)
+{
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 1);
+}
+
+static void net_led_flicker_200ms(void)
+{
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 1);
+    usleep(200);
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 0);
+    usleep(200);
+}
+
+static void net_led_flicker_800ms(void)
+{
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 1);
+    usleep(800);
+    mbtk_led_gpio_level_set(MBTK_NET_LED, 0);
+    usleep(800);
+}
+
+static void* net_led_thread_run(void* arg)
+{
+    mbtk_net_led_type status = MBTK_NET_LED_CLOSE;
+    while(1)
+    {
+        status = mbtk_net_led_get();
+        //LOGE("[led] get net led status = [%d].", status);
+        switch(status)
+        {
+            case MBTK_NET_LED_CLOSE:
+            case MBTK_NET_LED_POWER:
+            {
+                net_led_close();
+                sleep(2);
+                break;
+            }
+            case MBTK_NET_LED_SEARCH_NETWORK:
+            {
+                net_led_open();
+                sleep(2);
+                break;
+            }
+            case MBTK_NET_LED_DATA_CONNECT:
+            {
+                net_led_flicker_200ms();
+                break;
+            }
+            case MBTK_NET_LED_NET_CONNECT:
+            {
+                net_led_flicker_800ms();
+                break;
+            }
+            default:
+            {
+                LOGE("[led]Uknown status.");
+                break;
+            }
+        }
+
+        sleep(1);
+    }
+    return NULL;
+}
+
+int status_led_set(mbtk_status_led_type status)
+{
+    int ret = MBTK_RESULT_FAIL;
+    ret = mbtk_led_gpio_level_set(MBTK_STATUS_LED, status);
+    if(ret != MBTK_RESULT_SUCCESS)
+    {
+        LOGE("[led]set status led fail.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    mbtk_status_led_state = status;
+    return MBTK_RESULT_SUCCESS;
+}
+
+
+int mbtk_led_init(void)
+{
+    int ret = MBTK_RESULT_FAIL;
+
+    mbtk_led_gpio_init(MBTK_STATUS_LED);
+    mbtk_led_gpio_init(MBTK_NET_LED);
+    status_led_set(MBTK_STATUS_LED_OPEN);
+    mbtk_net_led_set(MBTK_NET_LED_SEARCH_NETWORK);
+
+    pthread_attr_t thread_attr;
+    pthread_t net_led_thread_id;
+    pthread_t status_led_thread_id;
+    pthread_attr_init(&thread_attr);
+    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+    {
+        LOGE("[led] pthread_attr_setdetachstate() fail.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    if(pthread_create(&net_led_thread_id, &thread_attr, net_led_thread_run, NULL))
+    {
+        LOGE("[led] pthread_create() fail.");
+        return MBTK_RESULT_FAIL;
+    }
+
+    pthread_attr_destroy(&thread_attr);
+    return MBTK_RESULT_SUCCESS;
+}
+/******************************FUNC*****************************************/
+
+#endif