Fix device_info for v1/v2.

Change-Id: I77c1da11f042bf2523d5768b18c18b9a0e1e0c6e
diff --git a/mbtk/libmbtk_lib/common/mbtk_device_info.c b/mbtk/libmbtk_lib/common/mbtk_device_info.c
index 670a320..3924297 100755
--- a/mbtk/libmbtk_lib/common/mbtk_device_info.c
+++ b/mbtk/libmbtk_lib/common/mbtk_device_info.c
@@ -42,46 +42,6 @@
     mbtk_device_info_header_t info_header;
     memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
 
-    switch(item_type) {
-        case MBTK_DEVICE_INFO_ITEM_BASIC:
-        {
-            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
-                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_FOTA:
-        {
-            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
-                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_MODEM:
-        {
-            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
-                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_LOG:
-        {
-            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
-                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
-                return -1;
-            }
-            break;
-        }
-        default:
-        {
-            LOGE("Item type[%d] error.", item_type);
-            return -1;
-        }
-    }
-
     mbtk_partition_info_t *partition_info = mbtk_partition_get();
     if(partition_info == NULL) {
         LOGE("mbtk_partition_get() fail.");
@@ -121,12 +81,7 @@
         goto fail;
     }
 
-    if(info_header.version != MBTK_DEVICE_INFO_CURR_VERSION) {
-        LOGE("Version error : %d", info_header.version);
-        goto fail;
-    }
-
-    LOGD("Item count:%d", info_header.item_count);
+    LOGD("Dev info version : %d, Item count:%d", info_header.version, info_header.item_count);
 
     if(info_header.item_header[item_type].addr == 0) {
         LOGE("No found item : %d", item_type);
@@ -134,9 +89,110 @@
     }
 
     lseek(fd, info_header.item_header[item_type].addr, SEEK_SET);
-    if (read(fd, item_ptr, item_size) != item_size) {
-        LOGE("Read fail:%d", errno);
-        goto fail;
+
+
+    switch(item_type) {
+        case MBTK_DEVICE_INFO_ITEM_BASIC:
+        {
+            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_basic_t)) {
+                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
+                goto fail;
+            }
+
+            mbtk_device_info_basic_t *basic_ptr = (mbtk_device_info_basic_t*)item_ptr;
+            basic_ptr->version = (mbtk_device_info_version_enum)info_header.version;
+            if(basic_ptr->version == DEV_INFO_VERSION_V1) {
+                if (read(fd, &(basic_ptr->basic.v1), sizeof(mbtk_device_info_basic_v1_t)) !=
+                        sizeof(mbtk_device_info_basic_v1_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            } else {
+                if (read(fd, &(basic_ptr->basic.v2), sizeof(mbtk_device_info_basic_v2_t)) !=
+                        sizeof(mbtk_device_info_basic_v2_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_FOTA:
+        {
+            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_fota_t)) {
+                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
+                goto fail;
+            }
+
+            mbtk_device_info_fota_t *fota_ptr = (mbtk_device_info_fota_t*)item_ptr;
+            fota_ptr->version = (mbtk_device_info_version_enum)info_header.version;
+            if(fota_ptr->version == DEV_INFO_VERSION_V1) {
+                if (read(fd, &(fota_ptr->fota.v1), sizeof(mbtk_device_info_fota_v1_t)) !=
+                        sizeof(mbtk_device_info_fota_v1_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            } else {
+                if (read(fd, &(fota_ptr->fota.v2), sizeof(mbtk_device_info_fota_v2_t)) !=
+                        sizeof(mbtk_device_info_fota_v2_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_MODEM:
+        {
+            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_modem_t)) {
+                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
+                goto fail;
+            }
+
+            mbtk_device_info_modem_t *modem_ptr = (mbtk_device_info_modem_t*)item_ptr;
+            modem_ptr->version = (mbtk_device_info_version_enum)info_header.version;
+            if(modem_ptr->version == DEV_INFO_VERSION_V1) {
+                if (read(fd, &(modem_ptr->modem.v1), sizeof(mbtk_device_info_modem_v1_t)) !=
+                        sizeof(mbtk_device_info_modem_v1_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            } else {
+                if (read(fd, &(modem_ptr->modem.v2), sizeof(mbtk_device_info_modem_v2_t)) !=
+                        sizeof(mbtk_device_info_modem_v2_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_LOG:
+        {
+            if(item_ptr == NULL || item_size != sizeof(mbtk_device_info_log_t)) {
+                LOGE("ARG error:item-%d, item_size-%d", item_type, item_size);
+                goto fail;
+            }
+
+            mbtk_device_info_log_t *log_ptr = (mbtk_device_info_log_t*)item_ptr;
+            log_ptr->version = (mbtk_device_info_version_enum)info_header.version;
+            if(log_ptr->version == DEV_INFO_VERSION_V1) {
+                if (read(fd, &(log_ptr->log.v1), sizeof(mbtk_device_info_log_v1_t)) !=
+                        sizeof(mbtk_device_info_log_v1_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            } else {
+                if (read(fd, &(log_ptr->log.v2), sizeof(mbtk_device_info_log_v2_t)) !=
+                        sizeof(mbtk_device_info_log_v2_t)) {
+                    LOGE("Read fail:%d", errno);
+                    goto fail;
+                }
+            }
+            break;
+        }
+        default:
+        {
+            LOGE("Item type[%d] error.", item_type);
+            goto fail;
+        }
     }
 
     close(fd);
@@ -154,49 +210,9 @@
         return -1;
     }
 
-    switch(item_type) {
-        case MBTK_DEVICE_INFO_ITEM_BASIC:
-        {
-            if(item_size != sizeof(mbtk_device_info_basic_t)) {
-                LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_FOTA:
-        {
-            if(item_size != sizeof(mbtk_device_info_fota_t)) {
-                LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_MODEM:
-        {
-            if(item_size != sizeof(mbtk_device_info_modem_t)) {
-                LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
-                return -1;
-            }
-            break;
-        }
-        case MBTK_DEVICE_INFO_ITEM_LOG:
-        {
-            if(item_size != sizeof(mbtk_device_info_log_t)) {
-                LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
-                return -1;
-            }
-            break;
-        }
-        default:
-        {
-            LOGE("Item type[%d] error.\n\r", item_type);
-            return -1;
-        }
-    }
-
     mbtk_partition_info_t info;
     memset(&info, 0x0, sizeof(mbtk_partition_info_t));
-    if(mbtk_partition_get_by_name("device_info", &info)) {
+    if(mbtk_partition_get_by_name(MBTK_DEVICE_INFO_PARTITION_NAME, &info)) {
         LOGE("mbtk_partition_get_by_name() fail.");
         return -1;
     }
@@ -218,7 +234,7 @@
     char *mtd_buff = (char*)malloc(info.erase_size);
     if(mtd_buff == NULL) {
         LOGE("malloc() failed\n");
-		return -1;
+        return -1;
     }
     memset(mtd_buff, 0xFF, info.erase_size);
     int len = read(fd, mtd_buff, info.erase_size);
@@ -240,7 +256,101 @@
 	ioctl(fd, MEMERASE, &mtdEraseInfo);
 
     mbtk_device_info_header_t *info_header = (mbtk_device_info_header_t*)mtd_buff;
-    memcpy(mtd_buff + info_header->item_header[item_type].addr, item_ptr, item_size);
+    switch(item_type) {
+        case MBTK_DEVICE_INFO_ITEM_BASIC:
+        {
+            if(item_size != sizeof(mbtk_device_info_basic_t)) {
+                LOGE("item_size != sizeof(mbtk_device_info_basic_t)\n\r");
+                goto fail;
+            }
+
+            mbtk_device_info_basic_t *basic_ptr = (mbtk_device_info_basic_t*)item_ptr;
+            if(info_header->version != basic_ptr->version) {
+                LOGE("basic version error.");
+                goto fail;
+            }
+
+            if(basic_ptr->version == DEV_INFO_VERSION_V1) {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(basic_ptr->basic.v1),
+                    sizeof(mbtk_device_info_basic_v1_t));
+            } else {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(basic_ptr->basic.v2),
+                    sizeof(mbtk_device_info_basic_v2_t));
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_FOTA:
+        {
+            if(item_size != sizeof(mbtk_device_info_fota_t)) {
+                LOGE("item_size != sizeof(mbtk_device_info_fota_t)\n\r");
+                goto fail;
+            }
+
+            mbtk_device_info_fota_t *fota_ptr = (mbtk_device_info_fota_t*)item_ptr;
+            if(info_header->version != fota_ptr->version) {
+                LOGE("fota version error.");
+                goto fail;
+            }
+
+            if(fota_ptr->version == DEV_INFO_VERSION_V1) {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(fota_ptr->fota.v1),
+                    sizeof(mbtk_device_info_fota_v1_t));
+            } else {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(fota_ptr->fota.v2),
+                    sizeof(mbtk_device_info_fota_v2_t));
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_MODEM:
+        {
+            if(item_size != sizeof(mbtk_device_info_modem_t)) {
+                LOGE("item_size != sizeof(mbtk_device_info_modem_t)\n\r");
+                goto fail;
+            }
+
+            mbtk_device_info_modem_t *modem_ptr = (mbtk_device_info_modem_t*)item_ptr;
+            if(info_header->version != modem_ptr->version) {
+                LOGE("modem version error.");
+                goto fail;
+            }
+
+            if(modem_ptr->version == DEV_INFO_VERSION_V1) {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(modem_ptr->modem.v1),
+                    sizeof(mbtk_device_info_modem_v1_t));
+            } else {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(modem_ptr->modem.v2),
+                    sizeof(mbtk_device_info_modem_v2_t));
+            }
+            break;
+        }
+        case MBTK_DEVICE_INFO_ITEM_LOG:
+        {
+            if(item_size != sizeof(mbtk_device_info_log_t)) {
+                LOGE("item_size != sizeof(mbtk_device_info_log_t)\n\r");
+                goto fail;
+            }
+
+            mbtk_device_info_log_t *log_ptr = (mbtk_device_info_log_t*)item_ptr;
+            if(info_header->version != log_ptr->version) {
+                LOGE("log version error.");
+                goto fail;
+            }
+
+            if(log_ptr->version == DEV_INFO_VERSION_V1) {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(log_ptr->log.v1),
+                    sizeof(mbtk_device_info_log_v1_t));
+            } else {
+                memcpy(mtd_buff + info_header->item_header[item_type].addr, &(log_ptr->log.v2),
+                    sizeof(mbtk_device_info_log_v2_t));
+            }
+            break;
+        }
+        default:
+        {
+            LOGE("Item type[%d] error.\n\r", item_type);
+            goto fail;
+        }
+    }
 
     lseek(fd, 0, SEEK_SET);
     if (write(fd, mtd_buff, info.erase_size) != info.erase_size) {
@@ -279,20 +389,40 @@
         return -1;
     }
 
-    if(strlen((char*)info_basic.revision_out) > 0) {
-        memcpy(revision_out, info_basic.revision_out, strlen((char*)info_basic.revision_out));
-    }
-    if(strlen((char*)info_basic.revision_in) > 0) {
-        memcpy(revision_in, info_basic.revision_in, strlen((char*)info_basic.revision_in));
-    }
-    if(strlen((char*)info_basic.project_cust) > 0) {
-        memcpy(project_cust, info_basic.project_cust, strlen((char*)info_basic.project_cust));
-    }
-    if(strlen((char*)info_basic.build_time) > 0) {
-        memcpy(build_time, info_basic.build_time, strlen((char*)info_basic.build_time));
+    mbtk_modem_band_area_enum modem_band_area = MBTK_MODEM_BAND_AREA_ALL;
+    if(info_basic.version == DEV_INFO_VERSION_V1) {
+        if(strlen((char*)info_basic.basic.v1.revision_out) > 0) {
+            memcpy(revision_out, info_basic.basic.v1.revision_out, strlen((char*)info_basic.basic.v1.revision_out));
+        }
+        if(strlen((char*)info_basic.basic.v1.revision_in) > 0) {
+            memcpy(revision_in, info_basic.basic.v1.revision_in, strlen((char*)info_basic.basic.v1.revision_in));
+        }
+        if(strlen((char*)info_basic.basic.v1.project_cust) > 0) {
+            memcpy(project_cust, info_basic.basic.v1.project_cust, strlen((char*)info_basic.basic.v1.project_cust));
+        }
+        if(strlen((char*)info_basic.basic.v1.build_time) > 0) {
+            memcpy(build_time, info_basic.basic.v1.build_time, strlen((char*)info_basic.basic.v1.build_time));
+        }
+
+        modem_band_area = info_modem.modem.v1.band_area;
+    } else {
+        if(strlen((char*)info_basic.basic.v2.revision_out) > 0) {
+            memcpy(revision_out, info_basic.basic.v2.revision_out, strlen((char*)info_basic.basic.v2.revision_out));
+        }
+        if(strlen((char*)info_basic.basic.v2.revision_in) > 0) {
+            memcpy(revision_in, info_basic.basic.v2.revision_in, strlen((char*)info_basic.basic.v2.revision_in));
+        }
+        if(strlen((char*)info_basic.basic.v2.project_cust) > 0) {
+            memcpy(project_cust, info_basic.basic.v2.project_cust, strlen((char*)info_basic.basic.v2.project_cust));
+        }
+        if(strlen((char*)info_basic.basic.v2.build_time) > 0) {
+            memcpy(build_time, info_basic.basic.v2.build_time, strlen((char*)info_basic.basic.v2.build_time));
+        }
+
+        modem_band_area = info_modem.modem.v2.band_area;
     }
 
-    switch(info_modem.band_area) {
+    switch(modem_band_area) {
         case MBTK_MODEM_BAND_AREA_CN:
             memcpy(band_area, "CN", 2);
             break;