Change mbtk_ril for band set.

Change-Id: I2a05f2b8493f2d88750f499189c8c25d025a9f7f
diff --git a/mbtk/include/mbtk/mbtk_device.h b/mbtk/include/mbtk/mbtk_device.h
index fdda843..f25a7e6 100755
--- a/mbtk/include/mbtk/mbtk_device.h
+++ b/mbtk/include/mbtk/mbtk_device.h
@@ -92,4 +92,13 @@
     uint32 state;               //
 } mbtk_device_info_log_t;
 
+
+/*
+* MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t
+* MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t
+* MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t
+* MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t
+*/
+int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size);
+
 #endif /* _MBTK_DEVICE_H */
diff --git a/mbtk/mbtk_lib/Makefile b/mbtk/mbtk_lib/Makefile
index 3494835..e709749 100755
--- a/mbtk/mbtk_lib/Makefile
+++ b/mbtk/mbtk_lib/Makefile
@@ -84,7 +84,8 @@
 	src/mbtk_utils.c \
 	src/mbtk_fota.c \
 	src/ringbuffer.c \
-	src/mbtk_mtd.c
+	src/mbtk_mtd.c \
+	src/mbtk_device_info.c
 
 ifeq ($(BUILD_PLATFORM), asr1803)
 LOCAL_SRC_FILES +=  \
diff --git a/mbtk/mbtk_lib/src/mbtk_device_info.c b/mbtk/mbtk_lib/src/mbtk_device_info.c
index 86744a6..4aa87dc 100755
--- a/mbtk/mbtk_lib/src/mbtk_device_info.c
+++ b/mbtk/mbtk_lib/src/mbtk_device_info.c
@@ -17,6 +17,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include "mbtk_device.h"
 #include "mbtk_log.h"
@@ -24,9 +27,10 @@
 #include "mbtk_mtd.h"
 
 /*
-* mbtk_device_info_basic_t
-* mbtk_device_info_fota_t
-
+* MBTK_DEVICE_INFO_ITEM_BASIC - mbtk_device_info_basic_t
+* MBTK_DEVICE_INFO_ITEM_FOTA - mbtk_device_info_fota_t
+* MBTK_DEVICE_INFO_ITEM_MODEM - mbtk_device_info_modem_t
+* MBTK_DEVICE_INFO_ITEM_LOG - mbtk_device_info_log_t
 */
 int mbtk_dev_info_read(mbtk_device_info_item_enum item_type, void *item_ptr, int item_size)
 {
@@ -85,7 +89,7 @@
     while(i < MBTK_PARTITION_NUM_MAX) {
         if(partition_info[i].used && strcmp(partition_info[i].name, MBTK_DEVICE_INFO_PARTITION_NAME) == 0) {
             snprintf(dev, 32, "/dev/%s", partition_info[i].dev);
-            LOGD("%s -> %s", strcmp(partition_info[i].name, dev);
+            LOGD("%s -> %s", partition_info[i].name, dev);
             break;
         }
         i++;
diff --git a/mbtk/mbtk_ril/src/mbtk_info_server.c b/mbtk/mbtk_ril/src/mbtk_info_server.c
index 6a3454d..2659aaf 100755
--- a/mbtk/mbtk_ril/src/mbtk_info_server.c
+++ b/mbtk/mbtk_ril/src/mbtk_info_server.c
@@ -21,6 +21,7 @@
 #include "info_data.h"
 #include "mbtk_led.h"
 #include "cust_info.h"
+#include "mbtk_device.h"
 
 static int sock_listen_fd = -1;
 static int epoll_fd = -1;
@@ -6390,12 +6391,14 @@
 
 OK
 */
-static int band_config()
+static void* band_config_thread()
 {
     int err = -1;
     ATResponse *p_response = NULL;
 
+#if 0
     mbtk_cust_info_t cust_info;
+retry:
     memset(&cust_info, 0x0, sizeof(mbtk_cust_info_t));
     if(!custom_partition_read("cust_info", &cust_info)) {
         if(cust_info.band_type == 1) { // CN
@@ -6416,19 +6419,58 @@
     } else {
         err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
     }
+
     if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
     {
-        LOGE("*BAND exec error.");
-        err = -1;
-        goto error;
+        LOGE("*BAND exec error, will retry in 3s.");
+        sleep(3);
+        goto retry;
     }
 
     LOGD("Set Band Success.");
-    err = 0;
+    property_set("persist.mbtk.band_config", "ALL");
+#else
+    mbtk_device_info_modem_t info_modem;
+    memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
+    if(mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &info_modem, sizeof(mbtk_device_info_modem_t))) {
+        LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
+retry_default:
+        err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
+        if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
+        {
+            LOGE("*BAND exec error, will retry in 3s.");
+            sleep(3);
+            goto retry_default;
+        }
 
-error:
+        property_set("persist.mbtk.band_config", "ALL");
+    } else {
+        char cmd[128] = {0};
+        snprintf(cmd, 128, "AT*BAND=15,%d,%d,%d,%d", info_modem.band_gsm,
+            info_modem.band_wcdma, info_modem.band_tdlte, info_modem.band_fddlte);
+retry_by_dev:
+        err = at_send_command(cmd, &p_response);
+        if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
+        {
+            LOGE("*BAND exec error, will retry in 3s.");
+            sleep(3);
+            goto retry_by_dev;
+        }
+
+        if(info_modem.band_area == MBTK_MODEM_BAND_AREA_CN) {
+            property_set("persist.mbtk.band_config", "CN");
+        } else if(info_modem.band_area == MBTK_MODEM_BAND_AREA_EU) {
+            property_set("persist.mbtk.band_config", "EU");
+        } else {
+            property_set("persist.mbtk.band_config", "ALL");
+        }
+    }
+
+    LOGD("Set Band Success.");
+#endif
+
     at_response_free(p_response);
-    return err;
+    return NULL;
 }
 
 static void* net_monitor_thread(void* arg)
@@ -6605,17 +6647,6 @@
     {
         LOGE("SIM NOT READY!");
     }
-
-    // Set Band
-    // AT*BAND=15,78,147,482,134742231
-    char buff[10];
-    memset(buff, 0, 10);
-    property_get("persist.mbtk.band_config", buff, "0");
-    if(atoi(buff) == 0) {
-        if(!band_config()) { // Set band success.
-            property_set("persist.mbtk.band_config", "1");
-        }
-    }
 }
 
 int mbtk_info_server_start()
@@ -6704,6 +6735,19 @@
 
     ril_at_ready_process();
 
+    // Set Band
+    // AT*BAND=15,78,147,482,134742231
+    char buff[10];
+    memset(buff, 0, 10);
+    property_get("persist.mbtk.band_config", buff, "");
+    if(strlen(buff) == 0) {
+        pthread_t band_pid;
+        if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
+        {
+            LOG("pthread_create() fail.");
+        }
+    }
+
     if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
     {
         LOG("pthread_create() fail.");
diff --git a/mbtk/test/mbtk_device_info_test.c b/mbtk/test/mbtk_device_info_test.c
new file mode 100755
index 0000000..3dd9b2a
--- /dev/null
+++ b/mbtk/test/mbtk_device_info_test.c
@@ -0,0 +1,56 @@
+#include "mbtk_device.h"
+
+static char* band_2_str(mbtk_modem_band_area_enum band_area)
+{
+    switch(band_area)
+    {
+        case MBTK_MODEM_BAND_AREA_CN:
+            return "CN";
+        case MBTK_MODEM_BAND_AREA_EU:
+            return "EU";
+        default:
+            return "ALL";
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    mbtk_device_info_basic_t info_basic;
+    memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
+    int result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
+    if(result) {
+        printf("mbtk_dev_info_read(BASIC) fail.\n");
+        return -1;
+    }
+
+    /*
+    uint8 project[16];          // T108 / L508_X6
+        uint8 project_cust[16];     // T108_C1 / L508_X6_C1  (Refer to: Custom_Model in blf file.)
+        uint32 ab_support;          // 1 for ab
+        uint8 revision_out[48];     // L508_X6v01.01b04.00
+        uint8 revision_in[64];
+    */
+    printf("Project:%s\n", info_basic.project);
+    printf("Custom_Model:%s\n", info_basic.project_cust);
+    printf("Revision_Out:%s\n", info_basic.revision_out);
+    printf("Revision_In:%s\n", info_basic.revision_in);
+    printf("AB System:%s\n", info_basic.ab_support ? "Yes" : "No");
+
+    mbtk_device_info_modem_t info_modem;
+    memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
+    result = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &info_modem, sizeof(mbtk_device_info_modem_t));
+    if(result) {
+        printf("mbtk_dev_info_read(MODEM) fail.\n");
+        return -1;
+    }
+
+    printf("Band:%s\n", band_2_str(info_modem.band_area));
+    printf("Band GSM:0x%08x\n", info_modem.band_gsm);
+    printf("Band WCDMA:0x%08x\n", info_modem.band_wcdma);
+    printf("Band TDLTE:0x%08x\n", info_modem.band_tdlte);
+    printf("Band FDDLTE:0x%08x\n", info_modem.band_fddlte);
+
+    return 0;
+}
+
+