zte's code,first commit

Change-Id: I9a04da59e459a9bc0d67f101f700d9d7dc8d681b
diff --git a/ap/lib/libdmgr/dmgr_api.h b/ap/lib/libdmgr/dmgr_api.h
new file mode 100644
index 0000000..b00f39e
--- /dev/null
+++ b/ap/lib/libdmgr/dmgr_api.h
@@ -0,0 +1,315 @@
+#ifndef __DMGR_API_H

+#define __DMGR_API_H

+

+#include <stdarg.h>

+

+typedef int dmgr_t;

+struct notifier_block;

+

+enum NOTIFY {

+    NOTIFY_USER_BEGIN = 0,

+    NOTIFY_CHECKVERSION_PREPARE,

+    NOTIFY_CHECKVERSION_ABORTED,

+    NOTIFY_CHECKVERSION_FINISHED,

+    NOTIFY_DOWNLOAD_PREPARE,

+    NOTIFY_DOWNLOAD_ABORTED,

+    NOTIFY_DOWNLOAD_TRANSLATING,

+    NOTIFY_DOWNLOAD_FINISHED,

+    NOTIFY_DOWNLOAD_PARTTIAL_FINISHED,

+    NOTIFY_SOCKET_CONNECT_PREPARE,

+    NOTIFY_SOCKET_CONNECT_ABORTED,

+    NOTIFY_SOCKET_CONNECT_FINISHED,

+    NOTIFY_SOCKET_CONNECT_CLOSED,

+    NOTIFY_SOCKET_SEND_PREPARE,

+    NOTIFY_SOCKET_SEND_TRANSLATING,

+    NOTIFY_SOCKET_SEND_ABORTED,

+    NOTIFY_SOCKET_SEND_FINISHED,

+    NOTIFY_SOCKET_RECV_PREPARE,

+    NOTIFY_SOCKET_RECV_TRANSLATING,

+    NOTIFY_SOCKET_RECV_ABORTED,

+    NOTIFY_SOCKET_RECV_FINISHED,

+    NOTIFY_USER_END

+};

+

+enum NOTIFIER_ID {

+    NOTIFY_ID_CHECK_VERSION = 0,

+    NOTIFY_ID_DOWNLOAD,

+    NOTIFY_ID_SOCKET,

+    MAX_USER_NOTIFIERS

+};

+

+enum _DMGR_ERRNO {

+    E_OK = 0,

+    E_BEGIN = 300,

+    E_NOMEM,

+    E_INVAL_PARAM,

+    E_INVAL_OPERATION,

+    E_IO_FAILED,

+    E_IO_TIMEDOUT,

+    E_VERIFIED_FAILED,

+    E_CANCELLED,

+    E_NESTING_CALLED,

+    E_RESERVE1 = 400,

+    /* Error from server */

+    E_INVAL_TOKEN,

+    E_INVAL_PLATFORM,

+    E_PARAM_MISSING,

+    E_UNCONFIGURED_VERSION,

+    E_INTERNAL_ERROR,

+    E_RESERVE2 = 500,

+    E_URL_MALFORMAT,

+    E_COULDNT_RESOLVE_HOST,

+    E_COULDNT_CONNECT,

+    E_HTTP_RETURNED_ERROR,

+    E_RANGE_ERROR,

+    E_HTTP_POST_ERROR,

+    E_BAD_DOWNLOAD_RESUME,

+    E_ABORTED,

+    E_AGAIN,

+    E_TOO_MANY_REDIRECTS,

+    E_GOT_NOTHING,

+    E_SEND_ERROR,

+    E_RECV_ERROR,

+    E_NO_CONNECTION_AVAILABLE,

+    E_END

+};

+

+#define DMGR_ERRNO(err)                 (int)(-(err))

+#define SOCKET_WAIT_FOREVER             ((int)-1)

+

+#define VERINFO_FLAGS_FORCE_PACKAGE     (int)(0x01 << 0)

+typedef struct version_info {

+    char *version_name;

+    int  file_size;

+    char *delta_id;

+    char *md5sum;

+    char *delta_url;

+    char *release_note;

+    int flags;

+    char *upgrade_from_time;

+    char *upgrade_to_time;

+    char *upgrade_gap;

+    char *meta_data;

+    char *event_id;

+} version_info_t;

+

+typedef struct download_info {

+    long total_bytes;

+    long break_bytes;

+    long saved_bytes;

+    long now_bytes;

+} download_info_t;

+

+typedef struct error_value {

+    int error;

+} error_value_t;

+

+typedef struct socket_sbuf {

+    char *data;

+    size_t size;

+} socket_sbuf_t;

+

+typedef struct notifier_data {

+    union {

+        struct error_value errvalue;

+        struct download_info dlinfo;

+        struct version_info verinfo;

+        struct socket_sbufs {

+            struct socket_sbuf *sock_sbufs;

+            size_t count;

+        } ssbufs;

+    } u;

+} notifier_data_t;

+

+typedef int (*notifier_fn_t)(struct notifier_block *nb,

+                             unsigned long action,

+                             void *data);

+

+typedef struct socket_connect {

+    int (*create)(struct socket_connect *conn,

+                  char *hostname,

+                  int port,

+                  int recv_timeout,

+                  int send_timeout,

+                  int connect_timeout,

+                  int flags);

+    int (*destory)(struct socket_connect *conn);

+    int (*connect)(struct socket_connect *conn);

+    int (*send)(struct socket_connect *conn,

+                char *buf,

+                size_t size,

+                size_t *sent_size);

+    int (*send_sbufs)(struct socket_connect *conn,

+                      struct socket_sbuf *sbufs,

+                      size_t count,

+                      size_t *sent_size);

+    int (*recv)(struct socket_connect *conn,

+                char *buf,

+                size_t size,

+                size_t *recved_size);

+    int (*close)(struct socket_connect *conn);

+} socket_connect_t;

+

+typedef struct notifier_block {

+    notifier_fn_t notifier_call;

+    struct notifier_block *next;

+    int priority;

+    int alloc_set;

+} notifier_block_t;

+

+#define init_notifier_block(nb, call, pri) \

+    do {  \

+        (nb)->notifier_call = call; \

+        (nb)->next = NULL; \

+        (nb)->priority = pri; \

+        (nb)->alloc_set= 0; \

+    } while (0)

+

+#define SERVER_TRANSOPT_NONE        0

+#define SERVER_TRANSOPT_FOTA            ((0x00 << 0) & 0x0f)

+#define SERVER_TRANSOPT_RELIANCE        ((0x01 << 0) & 0x0f)

+#define SERVER_TRANSOPT_RELIANCE_ST     ((0x02 << 0) & 0x0f)

+#define SERVER_TRANSOPT_VEHICLE         ((0x03 << 0) & 0x0f)

+#define SERVER_TRANSOPT_IOT             ((0x04 << 0) & 0x0f)

+#define SERVER_TRANSOPT_HTTPS           ((0x01 << 4) & 0xf0)

+#define SERVER_TRANSOPT_HTTP            ((0x02 << 4) & 0xf0)

+#define SERVER_TRANSOPT_COMPACT_HTTP    ((0x03 << 4) & 0xf0)

+

+typedef struct policy_info {

+    int so_transfer_timeout;        /* The tcp/ip receive/send data timeout */

+    int so_connect_timeout;         /* The tcp/ip connection timeout */

+    int so_recv_buffer_size;        /* The max size of receive buffer */

+    int download_retry;             /* The retry count for download failed */

+    int download_retry_time;        /* The retry delay time(seconds) for download failed */

+    int server_transfer_opt;        /* The type or opt of sepcific server */

+    char *sstate_cached_path;       /* The path used for cache dmgr shared-state */

+    char *root_certificates;        /* Root certificates are self-signed and form

+                                       the basis of an X.509-based public key

+                                       infrastructure (PKI) */

+    char *sub_certificates;         /* The path used for cache dmgr shared-state */

+    int freespace_for_file_frac;    /* file bavail safe proportion */

+} policy_info_t;

+

+#define POLICY_INFO_INIT(name)      \

+    { .so_transfer_timeout = -1,     \

+      .so_connect_timeout = -1,      \

+      .so_recv_buffer_size = 8192,  \

+      .download_retry = 0,          \

+      .download_retry_time = 5,          \

+      .server_transfer_opt = SERVER_TRANSOPT_FOTA| \

+                             SERVER_TRANSOPT_COMPACT_HTTP, \

+      .sstate_cached_path = "/var", \

+      .root_certificates  = NULL,   \

+      .sub_certificates   = NULL, \

+      .freespace_for_file_frac = 90 \

+    }

+

+#define POLICY_INFO(name)           \

+    struct policy_info name = POLICY_INFO_INIT(name)

+

+enum LOGGER {

+    LOGGER_STDIO = 0,

+    LOGGER_BUFFER,

+    LOGGER_FILE

+};

+

+enum LOG_LEVEL {

+    LOG_VERBOSE = 0,

+    LOG_DEBUG,

+    LOG_INFO,

+    LOG_WARNING,

+    LOG_ERROR,

+    LOG_FATAL,

+    LOG_OFF

+};

+

+enum LOG_TYPE {

+    LOG_TYPE_CHECK = 0,

+    LOG_TYPE_DOWNLOAD,

+    LOG_TYPE_HTTP,

+    LOG_TYPE_LAST

+};

+

+dmgr_t dmgr_alloc(struct policy_info *policy,

+                  enum LOG_LEVEL default_log_level,

+                  enum LOGGER logger, ...);

+int dmgr_free(dmgr_t dm_id);

+int dmgr_keepalive_with_server(dmgr_t dm_id);

+int dmgr_register_device_to_server(dmgr_t dm_id);

+int dmgr_register_socket(dmgr_t dm_id, struct socket_connect *conn);

+int dmgr_alloc_notifier(dmgr_t dm_id,

+                        enum NOTIFIER_ID nh_id,

+                        notifier_fn_t notifier_call,

+                        int priority,

+                        int extra_bytes);

+int dmgr_register_notifier(dmgr_t dm_id,

+                           struct notifier_block *nb,

+                           enum NOTIFIER_ID nh_id);

+int dmgr_register_deviceinfo(dmgr_t dm_id,

+                             const char *name,

+                             const char *value);

+int dmgr_register_salesinfo(dmgr_t dm_id,

+                            const char *name,

+                            const char *value);

+

+#define SERI_KEEP_URL               "KEEP_URL"

+#define SERI_REG_URL                "REG_URL"

+#define SERI_CHK_URL                "CHK_URL"

+#define SERI_DL_URL                 "DL_URL"

+#define SERI_REPORT_DLR_URL         "REPORT_DLR_URL"

+#define SERI_REPORT_UPGR_URL        "REPORT_UPGR_URL"

+#define SERI_REPORT_SALES_URL       "REPORT_SALES_URL"

+

+int dmgr_register_serverinfo(dmgr_t dm_id,

+                             const char *name,

+                             const char *value);

+

+/* The public for checking version */

+int dmgr_check_version(dmgr_t dm_id);

+int dmgr_async_check_version(dmgr_t dm_id);

+int dmgr_async_cancel_checking(dmgr_t dm_id);

+

+/* The public interface for downloading */

+#define DOWNLOAD_FILE_SEEK_BEGIN    ((long)0)

+#define DOWNLOAD_FILE_SEEK_END      ((long)-1)

+

+int dmgr_filmap_version(dmgr_t dm_id,

+                        struct version_info *version,

+                        char *file_path,

+                        long file_seek,

+                        long map_addr_start,

+                        long map_size);

+int dmgr_async_filmap_version(dmgr_t dm_id,

+                              struct version_info *version,

+                              char *file_path,

+                              long file_seek,

+                              long map_addr_start,

+                              long map_size);

+

+int dmgr_download_version(dmgr_t dm_id,

+                          struct version_info *version,

+                          char *file_path,

+                          long file_seek,

+                          long start_from,

+                          long max_size);

+int dmgr_async_download_version(dmgr_t dm_id,

+                                struct version_info *version,

+                                char *file_path,

+                                long file_seek,

+                                long start_from,

+                                long max_size);

+

+int dmgr_async_cancel_downloading(dmgr_t dm_id);

+

+/* The public for reporting interface */

+int dmgr_report_upgraded_version(dmgr_t dm_id,

+                                 struct version_info *version,

+                                 const char *upgraded_desc);

+

+int dmgr_report_sales_tracker(dmgr_t dm_id);

+int dmgr_set_log_level(int dm_id,

+                       enum LOG_TYPE type,

+                       enum LOG_LEVEL level);

+int dmgr_version(char *version_buf, int size);

+

+#endif

diff --git a/ap/lib/libdmgr/inc/curl_adapter.h b/ap/lib/libdmgr/inc/curl_adapter.h
new file mode 100644
index 0000000..6886f4a
--- /dev/null
+++ b/ap/lib/libdmgr/inc/curl_adapter.h
@@ -0,0 +1,18 @@
+#ifndef _CURL_ADAPTER_H_
+#define _CURL_ADAPTER_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <dmgr_api.h>
+
+typedef size_t(*callbackFunc)(void *ptr, size_t size, size_t nmemb, void *stream);
+
+int http_post(const char* url, char* post_buf, policy_info_t *policy,
+              callbackFunc func, void *ptr);
+int http_get(const char*url, long resume_from, policy_info_t *policy,
+             callbackFunc func, void *ptr);
+
+int http_is_reponse_successful(long resp_code) ;
+long http_get_response_code();
+
+#endif // !_CURL_ADAPTER_H_
diff --git a/ap/lib/libdmgr/inc/fota_comm.h b/ap/lib/libdmgr/inc/fota_comm.h
new file mode 100644
index 0000000..5828218
--- /dev/null
+++ b/ap/lib/libdmgr/inc/fota_comm.h
@@ -0,0 +1,127 @@
+#ifndef _FOTA_COMM_H_
+#define _FOTA_COMM_H_
+
+
+#include "dmgr_api.h"
+
+#include "fota_protocol_util.h"
+#include "fota_http_util.h"
+
+#include "curl_adapter.h"
+#include "assert.h"
+#include <fcntl.h>
+
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+
+//#define MEM_DEBUG
+
+
+void dump_mem_info(void);
+
+#define fota_sleep     sleep
+#define fota_assert    assert
+#define fota_ftruncate ftruncate
+
+#ifdef MEM_DEBUG
+
+void* realloc_debug(void *ptr, size_t size, const char*func, long line);
+void* malloc_debug(size_t bytes, const char* func, long line);
+void free_debug(void*p, const char* func, long line);
+char* strdup_debug(const char* str, const char* func, long line);
+
+#define fota_malloc(size)    		malloc_debug(size, __FUNCTION__, __LINE__)
+#define fota_free(ptr)     		do{if(ptr) free_debug(ptr, __FUNCTION__, __LINE__); ptr=NULL;}while(0)
+#define fota_strdup(str)    		strdup_debug(str, __FUNCTION__, __LINE__)
+#define fota_realloc(ptr, size)   	realloc_debug(ptr, size, __FUNCTION__, __LINE__)
+#else
+#define fota_malloc(size)    		malloc(size)
+#define fota_free(ptr)     		do{if(ptr) free(ptr); ptr=NULL;}while(0)
+#define fota_strdup(str)    		strdup(str)
+#define fota_realloc(ptr, size)   	realloc(ptr, size)
+
+#endif
+
+
+#define FOTA_FAIL             -1
+#define FOTA_SUCCESS           0
+#define FOTA_ERROR            -1
+
+
+
+#define  LOGD(fmt, args ...) \
+    do { printf("[DMGR][%s-%d]:"fmt, __FUNCTION__, __LINE__, ##args); } while (0)
+
+
+
+#define FOTA_DEFAULT_URL    	"http://fotazxic.zte.com.cn/Fota/Download.do"
+
+#define DEFAULT_DL_URL 		"http://47.96.134.20/Fota/"
+
+#define FOTA_SERVER_DOMAIN_NAME "fotazxic.zte.com.cn"
+
+#define VERSION_FILE           	"version_file"
+#define REG_POLICY_INFO        	"policy_info"
+
+#define HTTP_LINE_ENDING        "\r\n"
+
+/* fota版本号 */
+#define FOTA_VERSION            1
+#define ZX_FOTA_DM_LIB_VERSION  "zte dmgr lib version 1.0.0"
+
+/* #define NB_7100 */
+#ifdef NB_7100
+#define PROTOCOL_VERSION  "2.0"
+#else
+#define PROTOCOL_VERSION  "2.1"
+#endif
+
+/* action code */
+#define SESSION_OPEN            0x8000
+#define REGISTER_DEVICE_INFO    0x8001
+#define REGISTER_POLICY_INFO    0x8002
+#define CHECK_VRESION        	0x8003
+#define DOWNLOAD_VRESION    	0x8004
+#define UPDATE_RESULT       	0x8005
+#define SESSION_CLOSE       	0x8006
+
+
+typedef enum {
+    UPGRADE_OK = 0,
+    UPGRADE_FAIL
+} UPGRADE_RESULT;
+
+
+typedef struct {
+    char            	*IMEI;
+    char            	*download_url;
+    char            	*version_path;
+    unsigned int     	devNum;
+    char            	*dev_info;
+    policy_info_t   	*policy_info;
+
+} config_info_t;
+
+
+typedef struct {
+    long 	resp_code;
+    int 	result_code;
+    int 	action_code;
+} post_result_t;
+
+
+
+typedef struct {
+    config_info_t       config_info;
+    notifier_block_t    *notifier_list[MAX_USER_NOTIFIERS];
+    post_result_t       post_result;
+    download_info_t 	download_info;
+
+} dmgr_info_t;
+
+extern dmgr_t g_dmgr;
+
+#endif /* _FOTA_COMM_H_ */
diff --git a/ap/lib/libdmgr/inc/fota_http_util.h b/ap/lib/libdmgr/inc/fota_http_util.h
new file mode 100644
index 0000000..cb487ff
--- /dev/null
+++ b/ap/lib/libdmgr/inc/fota_http_util.h
@@ -0,0 +1,6 @@
+#ifndef _FOTA_HTTP_UTIL_H_
+#define _FOTA_HTTP_UTIL_H_
+
+int http_response_decode(unsigned int responsecode);
+size_t save_data_to_file(char* filename, char *data, int len);
+#endif
diff --git a/ap/lib/libdmgr/inc/fota_protocol_util.h b/ap/lib/libdmgr/inc/fota_protocol_util.h
new file mode 100644
index 0000000..66aa1b9
--- /dev/null
+++ b/ap/lib/libdmgr/inc/fota_protocol_util.h
@@ -0,0 +1,82 @@
+#ifndef _FOTA_PROTOCOL_UTIL_H_
+#define _FOTA_PROTOCOL_UTIL_H_
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+
+#define LINE_BUF_SIZE 128
+
+typedef enum {
+    INT_ARGUMENT = 1,
+    DOUBLE_ARGUMENT,
+    STRING_ARGUMENT,
+    HEX_ARGUMENT,
+    CHARACTER_ARGUMENT,
+    POINTER_ARGUMENT,
+    MESSAGE_END
+} ArgumentType;
+
+typedef struct {
+    ArgumentType type;
+    union {
+        int     int_val;
+        double  double_val;
+        char    *string_val;
+        void    *pointer_val;
+        int     character_val;
+    } u;
+} ArgumentInfo;
+
+typedef struct {
+    char        *string;
+} VString;
+
+/*ЭÒéijÏîÖµÀàÐÍ*/
+typedef enum {
+    ITEMTYPE_DEC,
+    ITEMTYPE_HEX,
+    ITEMTYPE_STRING
+} ItemType;
+
+
+/*º¯ÊýÉùÃ÷*/
+char* protocol_package(char *description, ...);
+
+void format_arguments(VString *message, va_list ap);
+
+int get_action_code(char *content);
+
+int get_result_code(char *content);
+
+int get_version_info(char *content, version_info_t *versionInfo);
+
+int get_version_down_info(char *content, download_info_t *downloadInfo);
+
+void str_append(VString *message, char *str);
+
+void free_version_info(version_info_t *version_info);
+void print_version_info(version_info_t *version_info);
+
+
+
+
+/* notifier list */
+notifier_block_t* notifier_list_add(notifier_block_t* notifier_list, notifier_block_t* notifier_msg);
+
+int notifier_list_free(notifier_block_t* notifier_list);
+
+void notify_callback_process(dmgr_t dm_id, unsigned short event_id, unsigned short msgid, void* data);
+/* notifier list end */
+
+int version_md5_check(const char *filename, char *md5_expect);
+
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif
+
+
diff --git a/ap/lib/libdmgr/libdmgr.a b/ap/lib/libdmgr/libdmgr.a
new file mode 100644
index 0000000..05699e6
--- /dev/null
+++ b/ap/lib/libdmgr/libdmgr.a
Binary files differ