Browse Source

新增树莓派AI语音板的初始代码

corvin 5 years ago
parent
commit
2cd7952549
100 changed files with 10202 additions and 0 deletions
  1. 30 0
      Makefile
  2. 28 0
      comm.mk
  3. 47 0
      example/Makefile
  4. 349 0
      example/record.c
  5. 82 0
      example/record.py
  6. 178 0
      include/libduer-device/include/baidu_json.h
  7. 44 0
      include/libduer-device/include/device_vad.h
  8. 36 0
      include/libduer-device/include/lightduer_adapter.h
  9. 93 0
      include/libduer-device/include/lightduer_aes.h
  10. 60 0
      include/libduer-device/include/lightduer_bind_device.h
  11. 70 0
      include/libduer-device/include/lightduer_bitmap.h
  12. 246 0
      include/libduer-device/include/lightduer_ca.h
  13. 78 0
      include/libduer-device/include/lightduer_ca_conf.h
  14. 193 0
      include/libduer-device/include/lightduer_coap.h
  15. 161 0
      include/libduer-device/include/lightduer_coap_defs.h
  16. 76 0
      include/libduer-device/include/lightduer_coap_ep.h
  17. 35 0
      include/libduer-device/include/lightduer_coap_trace.h
  18. 165 0
      include/libduer-device/include/lightduer_connagent.h
  19. 66 0
      include/libduer-device/include/lightduer_data_cache.h
  20. 495 0
      include/libduer-device/include/lightduer_dcs.h
  21. 126 0
      include/libduer-device/include/lightduer_dcs_alert.h
  22. 610 0
      include/libduer-device/include/lightduer_dcs_local.h
  23. 106 0
      include/libduer-device/include/lightduer_dcs_router.h
  24. 64 0
      include/libduer-device/include/lightduer_debug.h
  25. 91 0
      include/libduer-device/include/lightduer_dev_info.h
  26. 213 0
      include/libduer-device/include/lightduer_ds_log.h
  27. 274 0
      include/libduer-device/include/lightduer_ds_log_audio.h
  28. 73 0
      include/libduer-device/include/lightduer_ds_log_audio_player.h
  29. 58 0
      include/libduer-device/include/lightduer_ds_log_bind.h
  30. 207 0
      include/libduer-device/include/lightduer_ds_log_ca.h
  31. 58 0
      include/libduer-device/include/lightduer_ds_log_cache.h
  32. 100 0
      include/libduer-device/include/lightduer_ds_log_dcs.h
  33. 77 0
      include/libduer-device/include/lightduer_ds_log_e2e.h
  34. 135 0
      include/libduer-device/include/lightduer_ds_log_http.h
  35. 87 0
      include/libduer-device/include/lightduer_ds_log_recorder.h
  36. 89 0
      include/libduer-device/include/lightduer_ds_report.h
  37. 82 0
      include/libduer-device/include/lightduer_ds_report_ca.h
  38. 82 0
      include/libduer-device/include/lightduer_engine.h
  39. 39 0
      include/libduer-device/include/lightduer_event_emitter.h
  40. 55 0
      include/libduer-device/include/lightduer_events.h
  41. 267 0
      include/libduer-device/include/lightduer_flash_strings.h
  42. 43 0
      include/libduer-device/include/lightduer_handler.h
  43. 45 0
      include/libduer-device/include/lightduer_hashcode.h
  44. 298 0
      include/libduer-device/include/lightduer_http_client.h
  45. 33 0
      include/libduer-device/include/lightduer_http_client_ops.h
  46. 45 0
      include/libduer-device/include/lightduer_http_dns_client_ops.h
  47. 93 0
      include/libduer-device/include/lightduer_interactive_class.h
  48. 64 0
      include/libduer-device/include/lightduer_key.h
  49. 43 0
      include/libduer-device/include/lightduer_lib.h
  50. 83 0
      include/libduer-device/include/lightduer_log.h
  51. 253 0
      include/libduer-device/include/lightduer_memory.h
  52. 56 0
      include/libduer-device/include/lightduer_memory_hm_keywords.h
  53. 85 0
      include/libduer-device/include/lightduer_mutex.h
  54. 69 0
      include/libduer-device/include/lightduer_net_ntp.h
  55. 97 0
      include/libduer-device/include/lightduer_net_trans_aes_cbc_encrypted.h
  56. 95 0
      include/libduer-device/include/lightduer_net_trans_encrypted.h
  57. 188 0
      include/libduer-device/include/lightduer_net_transport.h
  58. 130 0
      include/libduer-device/include/lightduer_net_transport_wrapper.h
  59. 68 0
      include/libduer-device/include/lightduer_net_util.h
  60. 57 0
      include/libduer-device/include/lightduer_network_defs.h
  61. 72 0
      include/libduer-device/include/lightduer_nsdl_adapter.h
  62. 207 0
      include/libduer-device/include/lightduer_ota_decompression.h
  63. 229 0
      include/libduer-device/include/lightduer_ota_downloader.h
  64. 37 0
      include/libduer-device/include/lightduer_ota_http_downloader.h
  65. 288 0
      include/libduer-device/include/lightduer_ota_installer.h
  66. 37 0
      include/libduer-device/include/lightduer_ota_local_downloader.h
  67. 153 0
      include/libduer-device/include/lightduer_ota_notifier.h
  68. 110 0
      include/libduer-device/include/lightduer_ota_package_info.h
  69. 180 0
      include/libduer-device/include/lightduer_ota_unpacker.h
  70. 159 0
      include/libduer-device/include/lightduer_ota_updater.h
  71. 86 0
      include/libduer-device/include/lightduer_ota_verifier.h
  72. 43 0
      include/libduer-device/include/lightduer_ota_zlib.h
  73. 63 0
      include/libduer-device/include/lightduer_queue_cache.h
  74. 54 0
      include/libduer-device/include/lightduer_random.h
  75. 37 0
      include/libduer-device/include/lightduer_random_impl.h
  76. 30 0
      include/libduer-device/include/lightduer_session.h
  77. 52 0
      include/libduer-device/include/lightduer_sleep.h
  78. 54 0
      include/libduer-device/include/lightduer_snprintf.h
  79. 45 0
      include/libduer-device/include/lightduer_speex.h
  80. 30 0
      include/libduer-device/include/lightduer_statistics.h
  81. 76 0
      include/libduer-device/include/lightduer_store_voice.h
  82. 196 0
      include/libduer-device/include/lightduer_system_info.h
  83. 52 0
      include/libduer-device/include/lightduer_thread.h
  84. 37 0
      include/libduer-device/include/lightduer_thread_impl.h
  85. 53 0
      include/libduer-device/include/lightduer_timers.h
  86. 54 0
      include/libduer-device/include/lightduer_timestamp.h
  87. 101 0
      include/libduer-device/include/lightduer_types.h
  88. 78 0
      include/libduer-device/include/lightduer_voice.h
  89. 51 0
      include/snowboy/include/snowboy-detect-c-wrapper.h
  90. 226 0
      include/snowboy/include/snowboy-detect.h
  91. 41 0
      led_test.py
  92. BIN
      libs/libduer-device.a
  93. BIN
      libs/libsnowboy-detect-c.so
  94. 1 0
      profile
  95. BIN
      resources/16.mp3
  96. BIN
      resources/60.mp3
  97. BIN
      resources/61.mp3
  98. BIN
      resources/62.mp3
  99. BIN
      resources/common.res
  100. BIN
      resources/left.mp3

+ 30 - 0
Makefile

@@ -0,0 +1,30 @@
+include comm.mk
+
+CFLAGS += -D_GNU_SOURCE -lrt
+CFLAGS += -std=c99
+
+TARGET := AIVoiceSystem
+
+OBJFILES = src/duerapp.o
+OBJFILES += src/duerapp_alert.o
+OBJFILES += src/duerapp_event.o
+OBJFILES += src/duerapp_media.o
+OBJFILES += src/duerapp_profile_config.o
+OBJFILES += src/duerapp_recorder.o
+OBJFILES += src/button.o
+
+CFLAGS += $(shell pkg-config --cflags --libs gstreamer-1.0)
+LDLIBS += -lm \
+    -lrt \
+    -lasound \
+	-lwiringPi \
+    $(shell pkg-config --cflags --libs gstreamer-1.0)
+
+all: $(TARGET)
+
+$(TARGET) : $(OBJFILES)
+	$(CC) $(OBJFILES) $(CFLAGS) $(LDLIBS) -o $(TARGET)
+
+clean:
+	-rm -f *.o  $(TARGET) $(OBJFILES)
+

+ 28 - 0
comm.mk

@@ -0,0 +1,28 @@
+TOPDIR := ./
+DYNAMIC := False
+CC :=
+CXX :=
+LDFLAGS :=
+LDLIBS :=
+
+CFLAGS :=
+CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0
+
+ifeq ($(DYNAMIC), True)
+  CFLAGS += -fPIC
+  CXXFLAGS += -fPIC
+  CFLAGS += BUILD_DUEROS_DYNAMIC
+endif
+
+CC := gcc
+CXX := g++
+CFLAGS += -I$(TOPDIR)/include -I$(TOPDIR)/include/libduer-device/include -I$(TOPDIR)/include/snowboy/include -Wall -L ./libs/
+CXXFLAGS += -I$(TOPDIR)/include -I$(TOPDIR)/include/libduer-device/include -I$(TOPDIR)/include/snowboy/include -std=c++0x -Wall -Wno-sign-compare \
+      -Wno-unused-local-typedefs -Winit-self -rdynamic
+      
+LDLIBS +=  -Wl,-rpath=./libs -lduer-device -lsnowboy-detect-c
+
+# Set optimization level.
+CFLAGS += -O3
+CXXFLAGS += -O3
+

+ 47 - 0
example/Makefile

@@ -0,0 +1,47 @@
+TOPDIR := ../
+DYNAMIC := False
+CC :=
+CXX :=
+LDFLAGS :=
+LDLIBS :=
+
+CFLAGS :=
+CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0
+
+ifeq ($(DYNAMIC), True)
+  CFLAGS += -fPIC
+  CXXFLAGS += -fPIC
+  CFLAGS += BUILD_DUEROS_DYNAMIC
+endif
+
+CC := gcc
+CXX := g++
+CFLAGS += -I$(TOPDIR)/include -I$(TOPDIR)/include/libduer-device/include -I$(TOPDIR)/include/snowboy/include -Wall -L ./libs/
+CXXFLAGS += -I$(TOPDIR)/include -I$(TOPDIR)/include/libduer-device/include -I$(TOPDIR)/include/snowboy/include -std=c++0x -Wall -Wno-sign-compare \
+      -Wno-unused-local-typedefs -Winit-self -rdynamic
+ 
+# Set optimization level.
+CFLAGS += -O3
+CXXFLAGS += -O3
+
+CFLAGS += -D_GNU_SOURCE -lrt
+CFLAGS += -std=c99
+
+TARGET := record
+
+OBJFILES = record.o
+
+CFLAGS += $(shell pkg-config --cflags --libs gstreamer-1.0)
+LDLIBS += -lm \
+    -lrt \
+    -lasound \
+    $(shell pkg-config --cflags --libs gstreamer-1.0)
+
+all: $(TARGET)
+
+$(TARGET) : $(OBJFILES)
+	$(CC) $(OBJFILES) $(CFLAGS) $(LDLIBS) -o $(TARGET)
+
+clean:
+	-rm -f *.o *.wav $(TARGET) $(OBJFILES)
+

+ 349 - 0
example/record.c

@@ -0,0 +1,349 @@
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <pthread.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <alsa/asoundlib.h>
+#include <semaphore.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+
+#define ALSA_PCM_NEW_HW_PARAMS_API
+#define SAMPLE_RATE         			(16000)
+#define FRAMES_INIT         			(640*4)
+#define CHANNEL 	 	  			(4)
+#define FRAMES_SIZE  	  			((16/8) *CHANNEL)
+#define PCM_STREAM_CAPTURE_DEVICE	"hw:2,0"
+//#define PCM_STREAM_CAPTURE_DEVICE	"default"
+
+typedef struct{
+    int dir;
+    int size;
+    unsigned int val;
+    snd_pcm_t *handle;
+    snd_pcm_uframes_t frames;
+    snd_pcm_hw_params_t *params;
+}rec_config_t;
+
+static rec_config_t *s_index = NULL;
+
+
+typedef struct _pcm_header_t {
+    char    pcm_header[4];
+    size_t  pcm_length;
+    char    format[8];
+    int     bit_rate;
+    short   pcm;
+    short   channel;
+    int     sample_rate;
+    int     byte_rate;
+    short   block_align;
+    short   bits_per_sample;
+    char    fix_data[4];
+    size_t  data_length;
+} pcm_header_t;
+
+static pcm_header_t s_pcm_header = {
+    {'R', 'I', 'F', 'F'},
+    (size_t)-1,
+    {'W', 'A', 'V', 'E', 'f', 'm', 't', ' '},
+    0x10,
+    0x01,
+    0x01,
+#if 1 //def SAMPLE_RATE_16K
+    0x3E80,
+    0x7D00,
+#else //8k
+    0x1F40,
+    0x3E80,
+#endif // SAMPLE_RATE_16K
+    0x02,
+    0x10,
+    {'d', 'a', 't', 'a'},
+    (size_t)-1
+};
+
+
+typedef struct _REC_FILE{
+	FILE    *_file;
+	pcm_header_t _hdr;
+}REC_FILE;
+
+REC_FILE  *s_rec_files[CHANNEL+1];
+
+REC_FILE * duer_store_voice_start(int channel_id)
+{
+	REC_FILE *file=NULL;
+
+       printf("start");
+       file = (REC_FILE*)malloc(sizeof(REC_FILE));
+       if(file==NULL){
+	      return NULL;
+	}
+
+    char _name[64];
+    snprintf(_name, sizeof(_name), "./channel-%d.wav", channel_id);
+    file->_file = fopen(_name, "wb");
+    if (!file->_file ) {
+        printf("can't open file %s", _name);
+        return  NULL;
+    } else {
+        printf("begin write to file:%s", _name);
+    }
+
+    memcpy(&file->_hdr,&s_pcm_header,sizeof(s_pcm_header));
+    fwrite(&s_pcm_header, 1, sizeof(s_pcm_header), file->_file);
+    file->_hdr.data_length = 0;
+    file->_hdr.pcm_length = sizeof(s_pcm_header) - 8;
+
+    return  file;
+}
+
+int duer_store_voice_write(REC_FILE *file,const void *data, uint32_t size)
+{
+    if (file&&file->_file) {
+        fwrite(data, 1, size, file->_file);
+        file->_hdr.data_length += size;
+    }
+
+    return 0;
+}
+
+int duer_store_voice_end(REC_FILE *file)
+{
+    if (file&&file->_file) {
+        file->_hdr.pcm_length += s_pcm_header.data_length;
+        fseek(file->_file, 0, SEEK_SET);
+        fwrite(&file->_hdr, 1, sizeof(file->_hdr), file->_file);
+        fclose(file->_file);
+        file->_file = NULL;
+    }
+    return 0;
+}
+
+int read_pcm_mono_data(int16_t *in,int ilen,int16_t *out,int channel_cnt)
+{
+	int i=0;
+	if((ilen%channel_cnt)){
+			   printf("invalid pcm data lenght!\n");
+		       return -1;
+	 }
+
+	 for(i=0;i<ilen/channel_cnt;i++){
+			uint16_t mono_data = 0;
+			int j=0;
+			for(j=0;j<channel_cnt;j++){
+				  out[i] += in[channel_cnt*i+j];
+			}
+			out[i] /= channel_cnt;
+	}
+
+	  return ilen/channel_cnt;
+}
+
+
+int read_pcm_channel_data(int16_t *in,int ilen,int16_t *out,int channel_id,int channel_cnt)
+{
+        int i=0;
+
+	    if((ilen%channel_cnt)){
+		     printf("invalid pcm data lenght!\n");
+		     return -1;
+		}
+
+		if(channel_id<0||channel_id>=channel_cnt){
+		        printf("please input invalid channel id \n");
+		        return -1;
+		}
+
+		for(i=0;i<ilen/channel_cnt;i++){
+			out[i] = in[i*channel_cnt+channel_id];
+		}
+
+		return  ilen/channel_cnt;
+}
+
+static void   recording_pcm_data()
+{
+    int16_t *buffer = NULL;
+    int16_t *mono_buffer = NULL;
+    int mono_data_size = 0;
+
+    snd_pcm_hw_params_get_period_size(s_index->params, &(s_index->frames), &(s_index->dir));
+    if (s_index->frames < 0) {
+        printf("Get period size failed!");
+        return;
+    }
+
+    printf("frames %d dir %d\n",s_index->frames,s_index->dir);
+    s_index->size = s_index->frames * FRAMES_SIZE;
+
+    if (buffer) {
+        free(buffer);
+        buffer = NULL;
+    }
+
+    buffer = (int16_t *)malloc(s_index->size);
+    if (!buffer) {
+        printf("malloc buffer failed!\n");
+    } else {
+        memset(buffer, 0, s_index->size);
+    }
+
+    mono_buffer = (int16_t *)malloc(s_index->size);
+    if (!mono_buffer) {
+        printf("malloc buffer failed!\n");
+    } else {
+        memset(mono_buffer, 0, s_index->size);
+    }
+
+    while (1)
+    {
+	 int i=0;
+        int ret = snd_pcm_readi(s_index->handle, buffer, s_index->frames);
+
+        if (ret == -EPIPE) {
+            printf("an overrun occurred!\n");
+            snd_pcm_prepare(s_index->handle);
+	        continue;
+        } else if (ret < 0) {
+            printf("read: %s\n", snd_strerror(ret));
+	    continue;
+        } else if (ret != (int)s_index->frames) {
+            printf("read %d frames!\n", ret);
+	    continue;
+        } else {
+            // do nothing
+	       printf("ret=%d %d\n",ret,s_index->size);
+        }
+
+	for(i=0;i<CHANNEL+1;i++){
+		    if(i==CHANNEL){
+                           mono_data_size = read_pcm_mono_data(buffer,s_index->size>>1,mono_buffer,CHANNEL);
+		    }else{
+	                   mono_data_size = read_pcm_channel_data(buffer,s_index->size>>1,mono_buffer,i,CHANNEL);
+		    }
+		    duer_store_voice_write(s_rec_files[i],mono_buffer,mono_data_size<<1);
+	}
+    }
+
+    if (buffer) {
+        free(buffer);
+        buffer = NULL;
+    }
+
+    if(mono_buffer){
+         free(mono_buffer);
+	     mono_buffer=NULL;
+    }
+
+    snd_pcm_drain(s_index->handle);
+    snd_pcm_close(s_index->handle);
+
+    if(s_index) {
+        free(s_index);
+        s_index = NULL;
+    }
+
+	return;
+}
+
+static int duer_open_alsa_pcm()
+{
+    int ret = 0;
+
+     s_index = (rec_config_t *)malloc(sizeof(rec_config_t));
+    if (!s_index) {
+	printf("malloc fail\n");
+        return -1;
+    }
+
+    memset(s_index, 0, sizeof(rec_config_t));
+    s_index->frames = FRAMES_INIT;
+    s_index->val = SAMPLE_RATE; // pcm sample rate
+
+    int result = (snd_pcm_open(&(s_index->handle), PCM_STREAM_CAPTURE_DEVICE, SND_PCM_STREAM_CAPTURE, 0));
+    if (result < 0){
+        printf("\n\n****unable to open pcm device: %s*********\n\n", snd_strerror(ret));
+        ret = -1;
+    }
+
+    return ret;
+}
+
+static int duer_set_pcm_params()
+{
+    int ret = 0;
+
+    snd_pcm_hw_params_alloca(&(s_index->params));
+    snd_pcm_hw_params_any(s_index->handle, s_index->params);
+    snd_pcm_hw_params_set_access(s_index->handle, s_index->params,
+                                 SND_PCM_ACCESS_RW_INTERLEAVED);
+    snd_pcm_hw_params_set_format(s_index->handle, s_index->params,
+                                 SND_PCM_FORMAT_S16_LE);
+    snd_pcm_hw_params_set_channels(s_index->handle, s_index->params,
+                                   CHANNEL);
+    snd_pcm_hw_params_set_rate_near(s_index->handle, s_index->params,
+                                    &(s_index->val), &(s_index->dir));
+    snd_pcm_hw_params_set_period_size_near(s_index->handle, s_index->params,
+                                           &(s_index->frames), &(s_index->dir));
+
+    int result = snd_pcm_hw_params(s_index->handle, s_index->params);
+    if (result < 0)    {
+        printf("unable to set hw parameters: %s\n", snd_strerror(result));
+        ret = -1;
+    }
+
+    return ret;
+}
+
+void record_stop(int signo)
+{
+     int i=0;
+
+     printf("oops! stop!!!\n");
+     for(i=0;i<CHANNEL+1;i++){
+	     if(s_rec_files[i] != NULL){
+		       duer_store_voice_end(s_rec_files[i]);
+		 }
+	}
+
+     _exit(0);
+}
+
+int main(int argc, char *argv[])
+{
+        int ret=0;
+	int i;
+
+	for(i=0;i<CHANNEL+1;i++){
+	     s_rec_files[i] = NULL;
+	      s_rec_files[i] =  duer_store_voice_start(i);
+	}
+
+	signal(SIGINT, record_stop);
+	printf("%s %d\n",__FUNCTION__,__LINE__);
+	ret = duer_open_alsa_pcm();
+        if (ret != 0) {
+	        printf("open pcm failed\n");
+	        return -1;
+	}
+
+	printf("%s %d\n",__FUNCTION__,__LINE__);
+        ret = duer_set_pcm_params();
+        if (ret != 0) {
+	        printf("duer_set_pcm_params failed\n");
+		return -1;
+	 }
+
+	 printf("%s %d\n",__FUNCTION__,__LINE__);
+        recording_pcm_data();
+
+       return 0;
+}
+

+ 82 - 0
example/record.py

@@ -0,0 +1,82 @@
+#coding:utf-8
+import pyaudio
+import wave
+import json
+import signal
+import sys
+import os
+
+RECORD_RATE = 16000
+RECORD_CHANNELS = 2
+RECORD_WIDTH = 2
+CHUNK = 1024
+RECORD_SECONDS = 60
+WAVE_OUTPUT_FILENAME = "./output.wav"
+#RECORD_DEVICE_NAME = "seeed-2mic-voicecard"
+RECORD_DEVICE_NAME = "USB Camera-B4.09.24.1"
+
+p = pyaudio.PyAudio()
+stream = p.open(
+            rate=RECORD_RATE,
+            format=p.get_format_from_width(RECORD_WIDTH),
+            channels=RECORD_CHANNELS,
+            input=True,
+            start=False)
+
+wave_file = wave.open(WAVE_OUTPUT_FILENAME, "wb")
+
+def record():
+    wave_file.setnchannels(RECORD_CHANNELS)
+    wave_file.setsampwidth(2)
+    wave_file.setframerate(RECORD_RATE)
+    stream.start_stream()
+    print("* recording")
+    for i in range(0, int(RECORD_RATE / CHUNK * RECORD_SECONDS)):
+        data = stream.read(CHUNK)
+        wave_file.writeframes(data)
+        
+    print("* done recording")
+    stream.stop_stream()
+    wave_file.close()
+    # audio_data should be raw_data
+    return("record end")
+
+def sigint_handler(signum, frame):
+    stream.stop_stream()
+    stream.close()
+    p.terminate()
+    wave_file.close()
+    print 'catched interrupt signal!'
+    sys.exit(0)
+
+# 注册ctrl-c中断
+signal.signal(signal.SIGINT, sigint_handler)
+
+print p.get_device_count()
+
+device_index=-1
+
+for index in range(0,p.get_device_count()):
+    info=p.get_device_info_by_index(index)
+    device_name = info.get("name")
+    print device_name
+    print "\n"
+    if device_name.find(RECORD_DEVICE_NAME) != -1:
+        device_index=index
+        break
+
+if device_index != -1:
+    print "find the device"
+    stream.close()
+    stream = p.open(
+            rate=RECORD_RATE,
+            format=p.get_format_from_width(RECORD_WIDTH),
+            channels=RECORD_CHANNELS,
+            input=True,
+            input_device_index = device_index,
+            start=False)
+else:
+    print "don't find the device"
+    
+record()
+

+ 178 - 0
include/libduer-device/include/baidu_json.h

@@ -0,0 +1,178 @@
+/*
+  Copyright (c) 2009 Dave Gamble
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#ifndef baidu_json__h
+#define baidu_json__h
+
+#include "stdlib.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* baidu_json Types: */
+#define baidu_json_False  (1 << 0)
+#define baidu_json_True   (1 << 1)
+#define baidu_json_NULL   (1 << 2)
+#define baidu_json_Number (1 << 3)
+#define baidu_json_String (1 << 4)
+#define baidu_json_Array  (1 << 5)
+#define baidu_json_Object (1 << 6)
+
+#define baidu_json_IsReference 256
+#define baidu_json_StringIsConst 512
+
+/* The baidu_json structure: */
+typedef struct baidu_json
+{
+    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
+    struct baidu_json *next;
+    struct baidu_json *prev;
+    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
+    struct baidu_json *child;
+
+    /* The type of the item, as above. */
+    int type;
+
+    /* The item's string, if type==baidu_json_String */
+    char *valuestring;
+    /* The item's number, if type==baidu_json_Number */
+    int valueint;
+    /* The item's number, if type==baidu_json_Number */
+    double valuedouble;
+
+    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
+    char *string;
+} baidu_json;
+
+typedef struct baidu_json_Hooks
+{
+      void *(*malloc_fn)(size_t sz);
+      void (*free_fn)(void *ptr);
+} baidu_json_Hooks;
+
+/* Supply malloc, realloc and free functions to baidu_json */
+extern void baidu_json_InitHooks(baidu_json_Hooks* hooks);
+extern int baidu_json_Uninit(void);
+
+/* Supply a block of JSON, and this returns a baidu_json object you can interrogate. Call baidu_json_Delete when finished. */
+extern baidu_json *baidu_json_Parse(const char *value);
+/* Render a baidu_json entity to text for transfer/storage. Free the char* when finished. */
+extern char  *baidu_json_Print(const baidu_json *item);
+/* Render a baidu_json entity to text for transfer/storage without any formatting. Free the char* when finished. */
+extern char  *baidu_json_PrintUnformatted(const baidu_json *item);
+/* Render a baidu_json entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
+extern char *baidu_json_PrintBuffered(const baidu_json *item, int prebuffer, int fmt);
+/* Delete a baidu_json entity and all subentities. */
+extern void   baidu_json_Delete(baidu_json *c);
+
+/* Returns the number of items in an array (or object). */
+extern int	  baidu_json_GetArraySize(const baidu_json *array);
+/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
+extern baidu_json *baidu_json_GetArrayItem(const baidu_json *array, int item);
+/* Get item "string" from object. Case insensitive. */
+extern baidu_json *baidu_json_GetObjectItem(const baidu_json *object, const char *string);
+extern int baidu_json_HasObjectItem(const baidu_json *object, const char *string);
+/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when baidu_json_Parse() returns 0. 0 when baidu_json_Parse() succeeds. */
+extern const char *baidu_json_GetErrorPtr(void);
+
+/* These calls create a baidu_json item of the appropriate type. */
+extern baidu_json *baidu_json_CreateNull(void);
+extern baidu_json *baidu_json_CreateTrue(void);
+extern baidu_json *baidu_json_CreateFalse(void);
+extern baidu_json *baidu_json_CreateBool(int b);
+extern baidu_json *baidu_json_CreateNumber(double num);
+// len indicate the length of string, if len <=0, the length will be got throuth strlen(string)
+extern baidu_json *baidu_json_CreateString(const char *string, size_t len);
+extern baidu_json *baidu_json_CreateArray(void);
+extern baidu_json *baidu_json_CreateObject(void);
+
+/* These utilities create an Array of count items. */
+extern baidu_json *baidu_json_CreateIntArray(const int *numbers, int count);
+extern baidu_json *baidu_json_CreateFloatArray(const float *numbers, int count);
+extern baidu_json *baidu_json_CreateDoubleArray(const double *numbers, int count);
+extern baidu_json *baidu_json_CreateStringArray(const char **strings, int count);
+
+/* Append item to the specified array/object. */
+extern void baidu_json_AddItemToArray(baidu_json *array, baidu_json *item);
+extern void	baidu_json_AddItemToObject(baidu_json *object, const char *string, baidu_json *item);
+extern void	baidu_json_AddItemToObjectCS(baidu_json *object, const char *string, baidu_json *item);	/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the baidu_json object */
+/* Append reference to item to the specified array/object. Use this when you want to add an existing baidu_json to a new baidu_json, but don't want to corrupt your existing baidu_json. */
+extern void baidu_json_AddItemReferenceToArray(baidu_json *array, baidu_json *item);
+extern void	baidu_json_AddItemReferenceToObject(baidu_json *object, const char *string, baidu_json *item);
+
+/* Remove/Detatch items from Arrays/Objects. */
+extern baidu_json *baidu_json_DetachItemFromArray(baidu_json *array, int which);
+extern void   baidu_json_DeleteItemFromArray(baidu_json *array, int which);
+extern baidu_json *baidu_json_DetachItemFromObject(baidu_json *object, const char *string);
+extern void   baidu_json_DeleteItemFromObject(baidu_json *object, const char *string);
+
+/* Update array items. */
+extern void baidu_json_InsertItemInArray(baidu_json *array, int which, baidu_json *newitem); /* Shifts pre-existing items to the right. */
+extern void baidu_json_ReplaceItemInArray(baidu_json *array, int which, baidu_json *newitem);
+extern void baidu_json_ReplaceItemInObject(baidu_json *object,const char *string,baidu_json *newitem);
+
+/* Duplicate a baidu_json item */
+extern baidu_json *baidu_json_Duplicate(const baidu_json *item, int recurse);
+/* Duplicate will create a new, identical baidu_json item to the one you pass, in new memory that will
+need to be released. With recurse!=0, it will duplicate any children connected to the item.
+The item->next and ->prev pointers are always zero on return from Duplicate. */
+
+/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
+/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error. If not, then baidu_json_GetErrorPtr() does the job. */
+extern baidu_json *baidu_json_ParseWithOpts(const char *value, const char **return_parse_end, int require_null_terminated);
+
+extern void baidu_json_Minify(char *json);
+
+extern void baidu_json_release(void *ptr);
+
+/* Macros for creating things quickly. */
+#define baidu_json_AddNullToObject(object,name) baidu_json_AddItemToObject(object, name, baidu_json_CreateNull())
+#define baidu_json_AddTrueToObject(object,name) baidu_json_AddItemToObject(object, name, baidu_json_CreateTrue())
+#define baidu_json_AddFalseToObject(object,name) baidu_json_AddItemToObject(object, name, baidu_json_CreateFalse())
+#define baidu_json_AddBoolToObject(object,name,b) baidu_json_AddItemToObject(object, name, baidu_json_CreateBool(b))
+#define baidu_json_AddNumberToObject(object,name,n) baidu_json_AddItemToObject(object, name, baidu_json_CreateNumber(n))
+#define baidu_json_AddStringToObject(object,name,s) baidu_json_AddItemToObject(object, name, baidu_json_CreateString(s, 0))
+#define baidu_json_AddStringToObjectWithLength(object,name,s,len) baidu_json_AddItemToObject(object, name, baidu_json_CreateString(s, len))
+
+/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the baidu_json object */
+#define baidu_json_AddNullToObjectCS(object,name) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateNull())
+#define baidu_json_AddTrueToObjectCS(object,name) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateTrue())
+#define baidu_json_AddFalseToObjectCS(object,name) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateFalse())
+#define baidu_json_AddBoolToObjectCS(object,name,b) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateBool(b))
+#define baidu_json_AddNumberToObjectCS(object,name,n) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateNumber(n))
+#define baidu_json_AddStringToObjectCS(object,name,s) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateString(s, 0))
+#define baidu_json_AddStringToObjectCSWithLength(object,name,s,len) baidu_json_AddItemToObjectCS(object, name, baidu_json_CreateString(s, len))
+
+/* When assigning an integer value, it needs to be propagated to valuedouble too. */
+#define baidu_json_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
+#define baidu_json_SetNumberValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))
+
+/* Macro for iterating over an array */
+#define baidu_json_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 44 - 0
include/libduer-device/include/device_vad.h

@@ -0,0 +1,44 @@
+#ifndef DUER_DEVICE_VAD_H
+#define DUER_DEVICE_VAD_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Copyright 2017 Baidu Inc. All Rights Reserved.
+// Author: liuzhiyu (liuzhiyu04@baidu.com)
+//
+// device vad
+
+// default , do not modify
+#define SAMPLE_RATE (16000)
+#define VAD_10MS_SIZE (SAMPLE_RATE/100*2)
+#define VAD_20MS_SIZE (SAMPLE_RATE/100*4)
+#define VAD_FRAME_MS (80) // 80ms
+#define VAD_INPUT_MS (20) // 20ms
+#define VAD_CACHE_SIZE (VAD_FRAME_MS/VAD_INPUT_MS)
+#define VAD_VOICE_NUM_IN_FRAME (2) // if a frame which is  80ms len,  have >= 20ms voice, detect speaking
+// can change
+#define VAD_IGNORE_HOTWORD_TIME (2 * VAD_FRAME_MS) // 2*80 ms
+#define VAD_SPEAKING_SILENCE_VALID_TIME  (6 * VAD_FRAME_MS) // 6*80 ms
+#define VAD_VOICE_VALID_TIME (5 * VAD_FRAME_MS) // 5*80 ms
+
+// human stop speak
+int vad_stop_speak(void);
+
+void vad_stop_speak_done(void);
+
+// run vad state machine
+int device_vad(char *buff, size_t length);
+
+// when wakeup use this function
+void vad_set_wakeup(void);
+
+// when cloud vad work, use this function
+void vad_set_playing(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 36 - 0
include/libduer-device/include/lightduer_adapter.h

@@ -0,0 +1,36 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: baidu_ca_adapter.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Adapt the IoT CA to different platform.
+ */
+#ifndef BAIDU_DUER_IOT_CA_ADAPTER_BAIDU_CA_ADAPTER_H
+#define BAIDU_DUER_IOT_CA_ADAPTER_BAIDU_CA_ADAPTER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void baidu_ca_adapter_initialize(void);
+
+extern void baidu_ca_adapter_finalize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_IOT_CA_ADAPTER_BAIDU_CA_ADAPTER_H*/

+ 93 - 0
include/libduer-device/include/lightduer_aes.h

@@ -0,0 +1,93 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_aes.h
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: Provide the aes encryption(only CBC mode support now).
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_AES_H
+#define BAIDU_DUER_LIGHTDUER_AES_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// aes context, real type depend on the implementation
+typedef void *duer_aes_context;
+
+// get the initialized aes context
+duer_aes_context duer_aes_context_init(void);
+
+/*
+ *set the key used in the encryption and decryption
+ *@param ctx, got from duer_aes_context_init
+ *@param key, the key will be used,
+ *@param keybits, the length of @key in bits, should be one of 128, 192, 256
+ *@return, DUER_OK on success, or other error code
+ */
+int duer_aes_setkey(duer_aes_context ctx,
+                    const unsigned char *key,
+                    unsigned int keybits);
+
+/*
+ *set the iv used in the encryption and decryption
+ *@param ctx, got from duer_aes_context_init
+ *@param iv, the iv will be used, it's 16 bytes length.
+ *@return, DUER_OK on success, or other error code
+ */
+int duer_aes_setiv(duer_aes_context ctx, unsigned char iv[16]);
+
+/*
+ *encrypt the input info, use the key/iv from @duer_aes_setkey and @duer_aes_setiv
+ *@param ctx, got from duer_aes_context_init
+ *@param length, the length of the input, should be multiple of 16
+ *@param input, the content will be encrypted
+ *#param output, the encrypted content
+ *@return, DUER_OK on success, or other error code
+ */
+int duer_aes_cbc_encrypt(duer_aes_context ctx,
+                         size_t length,
+                         const unsigned char* input,
+                         unsigned char* output);
+
+/*
+ *decrypt the input info, use the key/iv from @duer_aes_setkey and @duer_aes_setiv
+ *@param ctx, got from duer_aes_context_init
+ *@param length, the length of the input, should be multiple of 16
+ *@param input, the content will be decrypted
+ *#param output, the decrypted content
+ *@return, DUER_OK on success, or other error code
+ */
+int duer_aes_cbc_decrypt(duer_aes_context ctx,
+                         size_t length,
+                         const unsigned char* input,
+                         unsigned char* output);
+
+/*
+ *destroy the context
+ *@param ctx, got from duer_aes_context_init
+ *@return, DUER_OK on success, or other error code
+ */
+int duer_aes_context_destroy(duer_aes_context ctx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_AES_H*/

+ 60 - 0
include/libduer-device/include/lightduer_bind_device.h

@@ -0,0 +1,60 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_bind_device.h
+ * Auth: Chen Xihao (chenxihao@baidu.com)
+ * Desc: Support WeChat Subscription to bind device
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_BIND_DEVICE_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_BIND_DEVICE_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DESC:
+ * Start the task to bind device.
+ *
+ * PARAM:
+ * @param[in] uuid: the uuid of device
+ * @param[in] bind_token: the bind token of device
+ * @param[in] lifecycle: the lifecycle of task in seconds, at least 60s
+ *
+ * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+duer_status_t duer_start_bind_device_task(const char *uuid,
+        const char *bind_token, size_t lifecycle);
+
+/**
+ * DESC:
+ * Stop the task that bind device.
+ *
+ * PARAM: none
+ *
+ * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+duer_status_t duer_stop_bind_device_task(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_BIND_DEVICE_H */
+

+ 70 - 0
include/libduer-device/include/lightduer_bitmap.h

@@ -0,0 +1,70 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Zhang Leliang(zhangleliang@baidu.com)
+//
+// Description: bitmap cache for fix-sized objects.
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_FRAMEWORK_UTILS_LIGHTDUER_BITMAP_H
+
+#include "baidu_json.h"
+#include "lightduer_mutex.h"
+
+//#define BIT_MAP_TYPE unsigned char
+typedef struct bitmap_objects_s {
+    int num_of_objs;
+    int num_of_bitmaps;
+    duer_mutex_t bitmap_lock;
+    unsigned char *bitmaps;
+    void *objects;
+    int element_size;
+} bitmap_objects_t;
+
+// int dump_bitmap(bitmap_objects_t *bop, const char *tag);
+
+// int first_set_index(bitmap_objects_t *buffer);
+
+/**
+ * init the bitmap structure
+ * @object_num, the number of elements which will be pre-allocated
+ * @bop, the address of the bitmap structure
+ * @element_size, the element size of the object, in Bytes
+ * @return, -1 fail, 0 ok
+ */
+int init_bitmap(int object_num, bitmap_objects_t *bop, int element_size);
+
+/**
+ * release the resource abtained in the init_bitmap
+ * @bop, the address of the bitmap sructure
+ * @return, -1 fail, 0 ok
+ */
+int uninit_bitmap(bitmap_objects_t *bop);
+
+/**
+ * allocate one element from the pre-allocated buffer in bop
+ * @bop, the address of the bitmap structure
+ * @return, NULL fail, the address of the element OK.
+ */
+void *alloc_obj(bitmap_objects_t *bop);
+
+/**
+ *  free the element got by alloc_obj
+ * @bop, the address of the bitmap structure
+ * @obj, the address of the element got from alloc_obj
+ * @return, 0 OK, -2 the address is not got from the buffer in bop,-1 fail,
+ */
+int free_obj(bitmap_objects_t *bop, void *obj);
+
+#endif // BAIDU_DUER_LIBDUER_DEVICE_FRAMEWORK_UTILS_LIGHTDUER_BITMAP_H

+ 246 - 0
include/libduer-device/include/lightduer_ca.h

@@ -0,0 +1,246 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Desc: Provide the API for external applications.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_H
+
+#include "lightduer_types.h"
+#include "lightduer_coap.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Acquire the handler
+ *
+ * @Param soc_ctx, in socket context
+ * @Return duer_handler, the global context handler
+ */
+DUER_EXT duer_handler baidu_ca_acquire(duer_transevt_func soc_ctx);
+
+/*
+ * Acquire the resources for response
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param list_res, in, resource list
+ * @Param list_res_size, in resource list length
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_add_resources(duer_handler hdlr,
+                                              const duer_res_t list_res[],
+                                              duer_size_t list_res_size);
+/*
+ * Load the configuration infomation
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param data, in, the configuration data
+ * @Param size, in, the data size
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_load_configuration(duer_handler hdlr, const void *data, duer_size_t size);
+
+DUER_EXT duer_status_t baidu_ca_unload_configuration(duer_handler hdlr);
+
+/*
+ * Start run the Baidu CA, prepare the environment.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_start(duer_handler hdlr);
+
+/*
+ *
+ * Determine the CA is started.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_bool, DUER_TRUE if started, else return DUER_FALSE.
+ */
+DUER_EXT duer_bool baidu_ca_is_started(duer_handler hdlr);
+
+/*
+ *
+ * Determine the CA is stopped.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_bool, DUER_TRUE if stopped, else return DUER_FALSE.
+ */
+DUER_EXT duer_bool baidu_ca_is_stopped(duer_handler hdlr);
+
+/*
+ * Set the Reporter report response callback.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param f_response, in, the callback for notify user the report data response
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_report_set_response_callback(duer_handler hdlr,
+                                                             duer_notify_f f_response,
+                                                             duer_context context);
+
+/*
+ * Set the transmit data callback.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param f_transmit, in, the callback for transmit encoded data.
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_report_set_data_tx_callback(duer_handler hdlr, duer_transmit_f f_transmit);
+
+/*
+ * Build the message body that will be reported.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param data, in, the message report data
+ * @Param size, in, the data size
+ * @Param confirmable, in, the report data QoS
+ * @Return duer_msg_t *, in, the generated message body,
+ *         it SHOULD be released by ${link baidu_ca_release_message}
+ */
+DUER_EXT duer_msg_t* baidu_ca_build_report_message(duer_handler hdlr,
+                                                   duer_bool confirmable);
+
+/*
+ * Build the message body that will be responsed to remote.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param msg, in, the message that remote requested
+ * @Param msg_code, out, the response message code
+ * @Return duer_msg_t *, in, the generated message body,
+ *         it SHOULD be released by ${link baidu_ca_release_message}
+ */
+DUER_EXT duer_msg_t* baidu_ca_build_response_message(duer_handler hdlr,
+                                                     const duer_msg_t* msg,
+                                                     duer_u8_t msg_code);
+
+/*
+ * Build the seperate response to the request with the token.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param ptoken, in, the token used in the message
+ * @Param token_len, in, the length of the token
+ * @Param msg_code, in, the response message code
+ * @Param confirmable, in,
+ * @Return duer_msg_t *, out, the generated message body,
+ *         it SHOULD be released by ${link baidu_ca_release_message}
+ */
+DUER_EXT duer_msg_t* baidu_ca_build_seperate_response_message(duer_handler hdlr,
+                                                              const char *ptoken,
+                                                              duer_size_t token_len,
+                                                              int msg_code,
+                                                              duer_bool confirmable);
+
+/*
+ * Release the message that generated by baidu_ca_build_XXXX_message.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param msg, in, the message that remote requested
+ */
+DUER_EXT void baidu_ca_release_message(duer_handler hdlr, duer_msg_t* msg);
+
+/*
+ * Send the message
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param msg, in, the msg will be sent
+ * @Param addr, in, the remote addr
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_send_data(duer_handler hdlr,
+                                          const duer_msg_t* msg,
+                                          const duer_addr_t* addr);
+
+/*
+ * Send the user data
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param data, in, the user data
+ * @Param size, in, the data size
+ * @Param addr, in, the remote addr
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_send_data_directly(duer_handler hdlr,
+                                          const void *data,
+                                          duer_size_t szie,
+                                          const duer_addr_t* addr);
+
+/*
+ * When the message data has ready to be received
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Param addr, in, the remote addr
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_data_available(duer_handler hdlr,
+                                               const duer_addr_t* addr);
+
+/*
+ * Execute the cached CoAP data, such as blockwise, resending...
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_exec(duer_handler hdlr);
+
+/*
+ * Stop the Baidu CA.
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_stop(duer_handler hdlr);
+
+/*
+ * Release the handler
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return duer_status_t, in, the operation result
+ */
+DUER_EXT duer_status_t baidu_ca_release(duer_handler hdlr);
+
+/*
+ * Obtain the uuid
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return const char *, The UUID string
+ */
+DUER_EXT const char *baidu_ca_get_uuid(duer_handler hdlr);
+
+/*
+ * Obtain the rsa cacrt
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return const char *, The RSA CACRT string
+ */
+DUER_EXT const char *baidu_ca_get_rsa_cacrt(duer_handler hdlr);
+
+/*
+ * Obtain the bindToken
+ *
+ * @Param hdlr, in, the handler will be operated
+ * @Return const char *, The bindToken string
+ */
+DUER_EXT const char *baidu_ca_get_bind_token(duer_handler hdlr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_H

+ 78 - 0
include/libduer-device/include/lightduer_ca_conf.h

@@ -0,0 +1,78 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Baidu CA configuration.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_CONF_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_CONF_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void* duer_conf_handler;
+
+/*
+ * Create the configuation from buffer
+ *
+ * @Param data, in, the configuation stream
+ * @Param size, in, the data size
+ * @Return duer_conf_handler, the handler for operating the configuation
+ */
+DUER_INT duer_conf_handler duer_conf_create(const void* data, duer_size_t size);
+
+/*
+ * Get the string from the configuration by the key
+ *
+ * @Param hdlr, in, the handler for configuation
+ * @Param key, in, the key of the item
+ * @Return const char *, the string value of the key
+ */
+DUER_INT const char* duer_conf_get_string(duer_conf_handler hdlr, const char* key);
+
+/*
+ * Get the unsigned short from the configuration by the key
+ *
+ * @Param hdlr, in, the handler for configuation
+ * @Param key, in, the key of the item
+ * @Return duer_u16_t, the unsigned short value of the key
+ */
+DUER_INT duer_u16_t duer_conf_get_ushort(duer_conf_handler hdlr, const char* key);
+
+/*
+ * Get the unsigned int from the configuration by the key
+ *
+ * @Param hdlr, in, the handler for configuation
+ * @Param key, in, the key of the item
+ * @Return duer_u32_t, the unsigned int value of the key
+ */
+DUER_INT duer_u32_t duer_conf_get_uint(duer_conf_handler hdlr, const char* key);
+
+/*
+ * Get the string from the configuration by the key
+ *
+ * @Param hdlr, in, the handler for configuation
+ */
+DUER_INT void duer_conf_destroy(duer_conf_handler hdlr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_CONF_H

+ 193 - 0
include/libduer-device/include/lightduer_coap.h

@@ -0,0 +1,193 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The CoAP adapter.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H
+
+#include "lightduer_types.h"
+#include "lightduer_lib.h"
+#include "lightduer_network_defs.h"
+#include "lightduer_coap_defs.h"
+#include "lightduer_coap_ep.h"
+
+#define DUER_COAP_MESSAGE_ID_INVALID     (0)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void* duer_coap_handler;
+
+typedef duer_status_t (*duer_coap_result_f)(duer_context ctx,
+                                            duer_coap_handler hdlr,
+                                            const duer_msg_t* msg,
+                                            const duer_addr_t* addr);
+
+/*
+ * Acquire the coap handler.
+ *
+ * @Param f_result, in, the callback for user handling events
+ * @Param context, in, the user context for f_result callback
+ * @Param ctx_context, in, the user socket context
+ * @Param key_info, something generated on up layer, but used on down layer
+ * @Return duer_coap_handler, the coap context
+ */
+DUER_INT duer_coap_handler duer_coap_acquire(duer_coap_result_f f_result,
+                                             duer_context context,
+                                             duer_transevt_func ctx_context,
+                                             const void *key_info);
+
+/*
+ * Set tx function callback
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param tx, in, the tx function callback
+ * @Return duer_status_t, the connect status
+ */
+DUER_INT duer_status_t duer_coap_set_tx_callback(duer_coap_handler hdlr, duer_transmit_f tx);
+
+/*
+ * Connect to remote CoAP server
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param addr, in, the remote server address
+ * @Param data, in, the security suite data
+ * @Param size, in, the data size
+ * @Return duer_status_t, the connect status
+ */
+DUER_INT duer_status_t duer_coap_connect(duer_coap_handler hdlr,
+                                         const duer_addr_t* addr,
+                                         const void* data,
+                                         duer_size_t size);
+
+/*
+ * Disconnect from remote CoAP server
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Return duer_status_t, the connect status
+ */
+DUER_INT duer_status_t duer_coap_disconnect(duer_coap_handler hdlr);
+
+/*
+ * Add the resource for CoAP request from other endpoints
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param res, in, the resource information
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_resource_add(duer_coap_handler hdlr,
+                                              const duer_res_t* res);
+
+/*
+ * Remove the resource for CoAP request from other endpoints
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param path, in, the resource path
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_resource_remove(duer_coap_handler coap,
+                                                 const char* path);
+
+/*
+ * Register endpoint by LWM2M
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param ep, in, the endpoint information
+ * @Return duer_status_t, if > 0, the result is the register message id, else is the error status
+ */
+DUER_INT duer_status_t duer_coap_register(duer_coap_handler coap,
+                                          const duer_coap_ep_t* ep);
+
+/*
+ * Unregister the endpoint by LWM2M
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Return duer_status_t, if > 0, the result is the unregister message id, else is the error status
+ */
+DUER_INT duer_status_t duer_coap_unregister(duer_coap_handler coap);
+
+/*
+ * Update registration by LWM2M
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param lifetime, in, the endpoint lifetime
+ * @Return duer_status_t, if > 0, the result is the update registration message id,
+ *                       else is the error status
+ */
+DUER_INT duer_status_t duer_coap_update_registration(duer_coap_handler coap,
+                                                     duer_u32_t lifetime);
+
+/*
+ * Send the CoAP message
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param msg, in, the CoAP message
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_send(duer_coap_handler coap, const duer_msg_t* msg);
+
+/*
+ * Send the data directly
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param data, in, the user data
+ * @Param size, in, the data size
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_send_data(duer_coap_handler hdlr, const void *data, duer_size_t size);
+
+/*
+ * Receive the CoAP message
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_data_available(duer_coap_handler coap);
+
+/*
+ * Execute the cached CoAP data
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param timestamp, in, the timestamp for mark the message
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_exec(duer_coap_handler hdlr, duer_u32_t timestamp);
+
+/*
+ * Release the CoAP context
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Return duer_status_t, the result
+ */
+DUER_INT duer_status_t duer_coap_release(duer_coap_handler coap);
+
+/*
+ * Set read timeout
+ *
+ * @Param hdlr, in, the CoAP context
+ * @Param timeout, in, the timeout value by milliseconds
+ * @Return duer_bool, if match return DUER_TRUE, else return DUER_FALSE
+ */
+DUER_INT duer_status_t duer_coap_set_read_timeout(duer_coap_handler coap, duer_u32_t timeout);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_H

+ 161 - 0
include/libduer-device/include/lightduer_coap_defs.h

@@ -0,0 +1,161 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: CoAP common definitions
+
+#ifndef LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H
+#define LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H
+
+#include "lightduer_types.h"
+#include "lightduer_network_defs.h"
+#include "lightduer_net_transport.h"
+
+/*
+ * The message codes.
+ */
+typedef enum {
+    DUER_MSG_EMPTY_MESSAGE                       = 0,
+    // Request message code
+    DUER_MSG_REQ_GET                             = 1,
+    DUER_MSG_REQ_POST                            = 2,
+    DUER_MSG_REQ_PUT                             = 3,
+    DUER_MSG_REQ_DELETE                          = 4,
+
+    // Response message code
+    DUER_MSG_RSP_CREATED                         = 65,   // 2.01
+    DUER_MSG_RSP_DELETED                         = 66,   // 2.02
+    DUER_MSG_RSP_VALID                           = 67,   // 2.03
+    DUER_MSG_RSP_CHANGED                         = 68,   // 2.04
+    DUER_MSG_RSP_CONTENT                         = 69,   // 2.05
+    DUER_MSG_RSP_CONTINUE                        = 95,   // 2.31
+    DUER_MSG_RSP_BAD_REQUEST                     = 128,  // 4.00
+    DUER_MSG_RSP_UNAUTHORIZED                    = 129,  // 4.01
+    DUER_MSG_RSP_BAD_OPTION                      = 130,  // 4.02
+    DUER_MSG_RSP_FORBIDDEN                       = 131,  // 4.03
+    DUER_MSG_RSP_NOT_FOUND                       = 132,  // 4.04
+    DUER_MSG_RSP_METHOD_NOT_ALLOWED              = 133,  // 4.05
+    DUER_MSG_RSP_NOT_ACCEPTABLE                  = 134,  // 4.06
+    DUER_MSG_RSP_REQUEST_ENTITY_INCOMPLETE       = 136,  // 4.08
+    DUER_MSG_RSP_PRECONDITION_FAILED             = 140,  // 4.12
+    DUER_MSG_RSP_REQUEST_ENTITY_TOO_LARGE        = 141,  // 4.13
+    DUER_MSG_RSP_UNSUPPORTED_CONTENT_FORMAT      = 143,  // 4.15
+    DUER_MSG_RSP_INTERNAL_SERVER_ERROR           = 160,  // 5.00
+    DUER_MSG_RSP_NOT_IMPLEMENTED                 = 161,  // 5.01
+    DUER_MSG_RSP_BAD_GATEWAY                     = 162,  // 5.02
+    DUER_MSG_RSP_SERVICE_UNAVAILABLE             = 163,  // 5.03
+    DUER_MSG_RSP_GATEWAY_TIMEOUT                 = 164,  // 5.04
+    DUER_MSG_RSP_PROXYING_NOT_SUPPORTED          = 165,  // 5.05
+    DUER_MSG_RSP_INVALID                         = 0xFF, // used in seperate response
+
+} duer_msg_code_e;
+
+/**
+ * CoAP Message type, used in CoAP Header
+ */
+typedef enum {
+    DUER_MSG_TYPE_CONFIRMABLE       = 0x00, // Reliable Request messages
+    DUER_MSG_TYPE_NON_CONFIRMABLE   = 0x10, // Non-reliable Request and Response messages
+    DUER_MSG_TYPE_ACKNOWLEDGEMENT   = 0x20, // Response to a Confirmable Request
+    DUER_MSG_TYPE_RESET             = 0x30  // Answer a Bad Request
+} duer_msg_type_e;
+
+/*
+ * The resource operation permission
+ */
+typedef enum {
+    DUER_RES_OP_GET      = 0x01,     // Get operation allowed
+    DUER_RES_OP_PUT      = 0x02,     // Put operation allowed
+    DUER_RES_OP_POST     = 0x04,     // Post operation allowed
+    DUER_RES_OP_DELETE   = 0x08      // Delete operation allowed
+} duer_resource_operation_e;
+
+/*
+ * The resource mode
+ */
+typedef enum {
+    DUER_RES_MODE_STATIC,            // Static resources have some value that doesn't change
+    DUER_RES_MODE_DYNAMIC,           // Dynamic resources are handled in application
+} duer_resource_mode_e;
+
+typedef struct _duer_context_s duer_context_t;
+
+typedef int (*duer_report_callback)(duer_context_t *);
+
+struct _duer_context_s {
+    void *                  _param;
+
+    int                     _status;
+
+    duer_report_callback    _on_report_start;
+    duer_report_callback    _on_report_finish;
+};
+
+/*
+ * The message definition
+ */
+typedef struct _duer_message_s {
+    duer_u16_t token_len;
+
+    duer_u8_t  msg_type;
+    duer_u8_t  msg_code;
+    duer_u16_t msg_id;
+
+    duer_u16_t path_len;
+    duer_u16_t query_len;
+    duer_u16_t payload_len;
+
+    duer_u8_t* token;
+    duer_u8_t* path;
+    duer_u8_t* query;
+    duer_u8_t* payload;
+
+    duer_u32_t create_timestamp;
+    duer_context_t *context;
+} duer_msg_t;
+
+/*
+ * The status notification to user.
+ */
+typedef duer_status_t (*duer_notify_f)(duer_context ctx,
+                                       duer_msg_t* msg,
+                                       duer_addr_t* addr);
+
+/*
+ * The transmit coap data callback.
+ */
+typedef duer_status_t (*duer_transmit_f)(duer_trans_handler hdlr, const void *data, duer_size_t size, duer_addr_t *addr);
+
+/*
+ * The resource for user
+ */
+typedef struct _duer_resource_s {
+    duer_u8_t    mode: 2;    // the resource mode, SEE in ${link duer_resource_mode_e}
+    duer_u8_t    allowed: 6; // operation permission, SEE in ${link duer_resource_operation_e}
+    char*       path;       // the resource path identify
+
+    union {
+        duer_notify_f   f_res;   // dynamic resource handle function, NULL if static
+        struct {
+            void*       data;   // static resource value data, NULL if dynamic
+            duer_size_t  size;   // static resource size
+        } s_res;
+    } res;
+} duer_res_t;
+
+#define DUER_MESSAGE_IS_RESPONSE(_code)     ((_code) > DUER_MSG_REQ_DELETE)
+
+#endif // LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_COAP_DEFS_H

+ 76 - 0
include/libduer-device/include/lightduer_coap_ep.h

@@ -0,0 +1,76 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The CoAP endpoint definitions.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_EP_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_EP_H
+
+#include "lightduer_lib.h"
+
+#define LIFETIME_MAX_LEN    (10)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Endpoint registration parameters
+ */
+typedef struct _baidu_ca_coap_endpoint_s {
+    char*          name_ptr;           // Endpoint name
+    duer_size_t     name_len;
+
+    char*          type_ptr;           // Endpoint type
+    duer_size_t     type_len;
+
+    char*          lifetime_ptr;       // Endpoint lifetime in seconds. eg. "1200" = 1200 seconds
+    duer_size_t     lifetime_len;
+
+    duer_addr_t*    address; // Endpoint address to be accessed by the server, optional, default NULL
+} duer_coap_ep_t, *duer_coap_ep_ptr;
+
+/*
+ * Create the endpoint.
+ *
+ * @Param hdlr, in, the global context for baidu ca
+ * @Param name, in, the endpoint name
+ * @Param type, in, the endpoint type
+ * @Param lifetime, in, the lifetime of the endpoint
+ * @Param addr, in, the endpoint address to be accessed by the server, optional, default NULL
+ * @Return duer_coap_ep_ptr, the endpoint context pointer
+ */
+DUER_INT duer_coap_ep_ptr duer_coap_ep_create(const char* name,
+                                              const char* type,
+                                              duer_u32_t lifetime,
+                                              const duer_addr_t* addr);
+
+
+/*
+ * Destroy the endpoint that created by ${link duer_coap_ep_create}.
+ *
+ * @Param hdlr, in, the global context for baidu ca
+ * @Param ptr, in, the endpoint context pointer
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_coap_ep_destroy(duer_coap_ep_ptr ptr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_COAP_EP_H

+ 35 - 0
include/libduer-device/include/lightduer_coap_trace.h

@@ -0,0 +1,35 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Adapter for mbed-trace.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MBEDTRACE_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MBEDTRACE_H
+
+#include "lightduer_types.h"
+
+/*
+ * Enable the mbed trace
+ */
+DUER_INT void duer_coap_trace_enable(void);
+
+/*
+ * Disable the mbed trace
+ */
+DUER_INT void duer_coap_trace_disable(void);
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MBEDTRACE_H

+ 165 - 0
include/libduer-device/include/lightduer_connagent.h

@@ -0,0 +1,165 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_connagent.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer APIS.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_H
+
+#include "lightduer_types.h"
+#include "lightduer_network_defs.h"
+#include "lightduer_coap_defs.h"
+#include "baidu_json.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_event_id_enum {
+    DUER_EVENT_STARTED,
+    DUER_EVENT_STOPPED
+} duer_event_id;
+
+typedef struct _duer_event_data_s {
+    int     _event;     //< Event id, see in @{link duer_event_id}
+    int     _code;    //< the return code
+    void *  _object;    //< The return content
+} duer_event_t;
+
+/**
+ * The action result callback
+ */
+typedef void (*duer_event_callback)(duer_event_t *);
+
+/**
+ * Initialize the environment.
+ */
+void duer_initialize(void);
+
+/**
+ * Set all actions event callback
+ *
+ * @param callback, the event change result callback.
+ */
+void duer_set_event_callback(duer_event_callback callback);
+
+/**
+ * Start the LightDuer egnine.
+ *
+ * @param data, the configuration data
+ * @param size, the data size
+ * @return int, the start result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_start(const void *data, size_t size);
+
+/**
+ * Add route resources for Server requested, this should be called after CA started.
+ *
+ * @param msg, const duer_res_t *, the resources list
+ * @param length, size_t, the resources length
+ * @return int, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_add_resources(const duer_res_t *res, size_t length);
+
+/**
+ * Call it when data is ready.
+ *
+ * @return int, the start result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_data_available(void);
+
+/**
+ * Send data to server.
+ *
+ * @param data, const baidu_json *, the data point values.
+ * @return int, the report data result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_data_report(const baidu_json *data);
+
+/**
+ * Send data to server.
+ *
+ * @param data, const baidu_json *, the data point values.
+ * @return int, the report data result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_data_report_async(duer_context_t *context, const baidu_json *data);
+
+/**
+ * Response the request from Origin Server.
+ *
+ * @param msg, const duer_msg_t *, the request message structure.
+ * @param msg_code, int, the message code, see in @{link duer_msg_code_e}.
+ * @param data, const void *, the payload data.
+ * @param size, size_t, the payload size.
+ * @return int, the send response result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ * @Note: if response ACK without response data,
+ *        set msg_code = DUER_MSG_EMPTY_MESSAGE, and data = NULL, size = 0
+ */
+int duer_response(const duer_msg_t *msg, int msg_code, const void *data, duer_size_t size);
+
+/**
+ * the seperate response message for an request
+ * @param token, const char* , the token got from the corresponding request
+ * @param token_len, duer_size_t, the length of the token
+ * @param msg_code, int, the message code, see in @{link duer_msg_code_e}
+ * @param data, const baidu_json *, the payload of the message
+ * @return int, the send response result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_seperate_response(const char *token,
+                           duer_size_t token_len,
+                           int msg_code,
+                           const baidu_json *data);
+
+/**
+ * Stop the LightDuer engine.
+ *
+ * @return int, the stop result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_stop(void);
+
+/**
+ * Stop the LightDuer engine.
+ * @param reason, int, the stop code which indicate the stop reason
+ * @return int, the stop result, success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_stop_with_reason(int reason);
+
+/**
+ * clear the queue cache data.
+ * @param timestamp, clean the message with create_timestamp before timestamp
+ */
+int duer_clear_data(duer_u32_t timestamp);
+
+/**
+ * the callback to handle coap response message.
+ */
+void duer_coap_response_callback(duer_context ctx,
+                                 duer_msg_t* msg,
+                                 duer_addr_t* addr);
+
+/**
+ * Finalize the environment.
+ */
+int duer_finalize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_H*/

+ 66 - 0
include/libduer-device/include/lightduer_data_cache.h

@@ -0,0 +1,66 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_data_cache.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer stored the send data.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_DATA_CACHE_H
+#define BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_DATA_CACHE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct _duer_transmit_data_s {
+    void *  data;
+    size_t  size;
+} duer_dcache_item;
+
+void duer_dcache_initialize(void);
+
+/**
+ * @param data, the memory address
+ * @param size, the size of memory
+ * @param need_copy, indicate whether the memroy @data need another copy
+ *                   DUER_FALSE means the invoker transfer the memory ownership,
+ *                   DUER_TRUE is default value, which means invoker still own the memory,
+ *                   so invoker have the responsibility to release it when no long used.
+ */
+duer_status_t duer_dcache_push(const void *data, size_t size, duer_bool need_copy);
+
+duer_dcache_item *duer_dcache_top(void);
+
+void duer_dcache_pop(void);
+
+void *duer_dcache_create_iterator(void);
+
+void *duer_dcache_get_next(void *iterator);
+
+duer_dcache_item *duer_dcache_get_data(void *iterator);
+
+size_t duer_dcache_length(void);
+
+void duer_dcache_clear(void);
+
+void duer_dcache_finalize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_DATA_CACHE_H*/

+ 495 - 0
include/libduer-device/include/lightduer_dcs.h

@@ -0,0 +1,495 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_dcs_router.h
+ * Auth: Gang Chen (chengang12@baidu.com)
+ * Desc: Light duer DCS APIS.
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_DCS_H
+#define BAIDU_DUER_LIGHTDUER_DCS_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+    DCS_PLAY_CMD,
+    DCS_PAUSE_CMD,
+    DCS_PREVIOUS_CMD,
+    DCS_NEXT_CMD,
+    DCS_PLAY_CONTROL_EVENT_NUMBER,
+} duer_dcs_play_control_cmd_t;
+
+typedef enum {
+    DCS_MEDIA_ERROR_UNKNOWN,               // unknown error
+    // server invalid response, such as bad request, forbidden, not found .etc
+    DCS_MEDIA_ERROR_INVALID_REQUEST,
+    DCS_MEDIA_ERROR_SERVICE_UNAVAILABLE,   // device cannot connect to server
+    DCS_MEDIA_ERROR_INTERNAL_SERVER_ERROR, // server failed to handle device's request
+    DCS_MEDIA_ERROR_INTERNAL_DEVICE_ERROR, // device internal error
+} duer_dcs_audio_error_t;
+
+typedef enum {
+    DCS_RECOMMEND_POWER_ON,
+    DCS_RECOMMEND_TIME_NUMBER,
+} duer_dcs_recommend_time_t;
+
+typedef struct {
+    const char *url;
+    int offset;
+    const char *audio_item_id;
+} duer_dcs_audio_info_t;
+
+enum duer_dcs_device_capability {
+    DCS_TTS_HTTPS_PROTOCAL_SUPPORTED = 0x01, // the device support https protocal to playing tts
+    DCS_WECHAT_SUPPORTED             = 0x02, // the device support wechat
+};
+
+/**
+ * Initialize the dcs framework.
+ *
+ * @return none.
+ */
+void duer_dcs_framework_init(void);
+
+/**
+ * Uninitialize the DCS module.
+ *
+ * @return none.
+ */
+void duer_dcs_uninitialize(void);
+
+/**
+ * DESC:
+ * Initialize dcs voice input interface.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_voice_input_init(void);
+
+/**
+ * DESC:
+ * Notify DCS when recorder start to record.
+ *
+ * PARAM: none
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_on_listen_started(void);
+
+/**
+ * DESC:
+ * There is no VAD in translate scene,
+ * hence developer needs to call this function to notify DCS framework after recording finished.
+ *
+ * PARAM: none
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_on_listen_stopped(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to start recording.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_listen_handler(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to stop recording.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_stop_listen_handler(void);
+
+/**
+ * DESC:
+ * Initialize dcs voice output interface
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_voice_output_init(void);
+
+/**
+ * DESC:
+ *
+ * It should be called when speech finished, used to notify DCS level.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_speech_on_finished(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to play speech.
+ *
+ * PARAM:
+ * @param[in] url: the url of the speech need to play
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_speak_handler(const char *url);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to stop speech.
+ *
+ * PARAM:
+ * @param: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_stop_speak_handler(void);
+
+/**
+ * DESC:
+ * Initialize dcs speaker controler interface to enable volume control function.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_speaker_control_init(void);
+
+/**
+ * DESC:
+ * Notify DCS when volume changed
+ *
+ * PARAM: none
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_on_volume_changed(void);
+
+/**
+ * DESC:
+ * Notify DCS when mute state changed.
+ *
+ * PARAM: none
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_on_mute(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface, it is used to get volume state.
+ *
+ * @param[out] volume: current volume value.
+ * @param[out] is_mute: current mute state.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_get_speaker_state(int *volume, duer_bool *is_mute);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to set volume.
+ *
+ * PARAM:
+ * @param[in] volume: the value of the volume need to set
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_volume_set_handler(int volume);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to adjust volume.
+ *
+ * PARAM:
+ * @param[in] volume: the value need to adjusted.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_volume_adjust_handler(int volume);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to change mute state.
+ *
+ * PARAM:
+ * @param[in] is_mute: set/discard mute.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_mute_handler(duer_bool is_mute);
+
+/**
+ * DESC:
+ * Initialize dcs audio player interface.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_audio_player_init(void);
+
+/**
+ * DESC:
+ * User can also use this API to subscribe FM.
+ *
+ * PARAM[in] url: the url identify the FM.
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_on_link_clicked(const char *url);
+
+/**
+ * DESC:
+ * Notify DCS when an audio is finished.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_on_finished(void);
+
+/**
+ * DESC:
+ * Notify DCS when failed to play audio.
+ *
+ * PARAM[in] type: the error type
+ * PARAM[in] msg: the error message
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_audio_play_failed(duer_dcs_audio_error_t type, const char *msg);
+
+/**
+ * DESC:
+ * Report StreamMetadataExtracted event if metadata is found in the audio.
+ *
+ * PARAM[in] metadata: the metadata need to report, its layout is:
+ *                     "metadata": {
+ *                         "{{STRING}}": "{{STRING}}",
+ *                         "{{STRING}}": {{BOOLEAN}}
+ *                         "{{STRING}}": "{{STRING NUMBER}}"
+ *                         ...
+ *                     }
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_audio_report_metadata(baidu_json *metadata);
+
+/**
+ * DESC:
+ * Notify DCS when audio is stuttered.
+ *
+ * PARAM[in] is_stuttuered: DUER_TRUE when stutter started(download speed lower than play speed),
+ *                          DUER_FALSE when stutter finished(the audio resume play).
+ */
+int duer_dcs_audio_on_stuttered(duer_bool is_stuttered);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to play audio.
+ *
+ * PARAM:
+ * @param[in] audio_info: the info of the audio need to play
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_play_handler(const duer_dcs_audio_info_t *audio_info);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to stop audio player.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_stop_handler(void);
+
+/**
+ * DESC:
+ * Notify DCS when an audio is stopped not by DCS API.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_on_stopped(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to resume audio play.
+ *
+ * PARAM:
+ * @param[in] audio_info: the info of the audio need to resumed
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_resume_handler(const duer_dcs_audio_info_t *audio_info);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to pause audio play.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_pause_handler(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface, it's used to get the audio play progress.
+ *
+ * PARAM: none
+ *
+ * @RETURN: the play position of the audio.
+ */
+int duer_dcs_audio_get_play_progress(void);
+
+/**
+ * DESC:
+ * Realize play control(play, pause, next/previous audio) by send command to DCS.
+ *
+ * PARAM[in] cmd: command type.
+ *
+ * @RETURN: 0 when success, negative when fail.
+ */
+int duer_dcs_send_play_control_cmd(duer_dcs_play_control_cmd_t cmd);
+
+/**
+ * DESC:
+ * Report current state after device boot.
+ *
+ * PARAM: none
+ *
+ * @RETURN: 0 if succuss, negative if failed.
+ */
+int duer_dcs_sync_state(void);
+
+/**
+ * DESC:
+ * Sending an "Exited" event to close the multi dialogue.
+ *
+ * @RETURN: 0 if succuss, negative if failed.
+ */
+int duer_dcs_close_multi_dialog(void);
+
+/**
+ * DESC:
+ * Initialize dcs screen interface.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_screen_init(void);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to get the ASR result.
+ *
+ * PARAM[in] text: the ASR text result.
+ * PARAM[in] type: "INTERMEDIATE" or "FINAL".
+ *
+ * @RETURN: DUER_OK if success,
+ *          DUER_MSG_RSP_BAD_REQUEST if the payload is invalid,
+ *          DUER_ERR_FAILED if other error happened.
+ */
+duer_status_t duer_dcs_input_text_handler(const char *text, const char *type);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to get render card.
+ *
+ * PARAM[in] payload: the information of the render card, please reference the DCS document.
+ *
+ * @RETURN: DUER_OK if success,
+ *          DUER_MSG_RSP_BAD_REQUEST if the payload is invalid,
+ *          DUER_ERR_FAILED if other error happened.
+ */
+duer_status_t duer_dcs_render_card_handler(baidu_json *payload);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to open/close bluetooth.
+ *
+ * PARAM[in] is_switch: open bluetooth if is_switch is DUER_TRUE, otherwise close bluetooth.
+ * PARAM[in] target: Reserved parameter, currently its value is "default".
+ *
+ * @RETURN: None
+ */
+void duer_dcs_bluetooth_set_handler(duer_bool is_switch, const char *target);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to connect/disconnect bluetooth to device.
+ *
+ * PARAM[in] is_connect: connect bluetooth if is_connect is DUER_TRUE, otherwise disconnect bluetooth.
+ * PARAM[in] target: Reserved parameter, currently its value is "default".
+ *
+ * @RETURN: None
+ */
+void duer_dcs_bluetooth_connect_handler(duer_bool is_connect, const char *target);
+
+/**
+ * DESC:
+ * Initialize device control interface, such as control the bluetooth.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none
+ */
+void duer_dcs_device_control_init(void);
+
+/**
+ * DESC:
+ * Used to declare the device capability, such as: whether https or wechat are supported.
+ * It is unnecessary if the device don't have the capability defined in duer_dcs_device_capability.
+ * @PARAM[in] capability: the device capability, it's the or value of the member in
+ *                        duer_dcs_device_capability.
+ * @RETURN:   DUER_OK if success, negative if failed.
+ */
+duer_status_t duer_dcs_capability_declare(duer_u32_t capability);
+
+/**
+ * DESC:
+ * Sending RecommendationRequested event, and DCS server will recommend a resource to device.
+ * Currently, only the scene of "POWER_ON" is supported, that means this function should be called
+ * only one time(when device power_on).
+ *
+ * @PARAM[in] type: type of the scene.
+ * @RETURN: 0 if succuss, negative if failed.
+ */
+int duer_dcs_recommend_request(duer_dcs_recommend_time_t time);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_DCS_H*/
+

+ 126 - 0
include/libduer-device/include/lightduer_dcs_alert.h

@@ -0,0 +1,126 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_dcs_alert.h
+ * Auth: Gang Chen (chengang12@baidu.com)
+ * Desc: Light duer alert APIs.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DCS_ALERT_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DCS_ALERT_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+    SET_ALERT_SUCCESS,
+    SET_ALERT_FAIL,
+    DELETE_ALERT_SUCCESS,
+    DELETE_ALERT_FAIL,
+    ALERT_START,
+    ALERT_STOP,
+} duer_dcs_alert_event_type;
+
+typedef struct {
+    char *type;    // "ALARM" or "TIMER"
+    char *time;    // the time of the alert, ISO 8601 format.
+    char *token;   // the alert token, it is the unique identification of an alert
+} duer_dcs_alert_info_type;
+
+/**
+ * DESC:
+ * Init alert.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_alert_init(void);
+
+/**
+ * DESC:
+ * Report alert event
+ *
+ * PARAM:
+ * @param[in] token: the token of the alert.
+ * @param[in] type: event type, refer to duer_alert_event_type.
+ *
+ * @return: 0 when success, negative value when failed.
+ */
+int duer_dcs_report_alert_event(const char *token, duer_dcs_alert_event_type type);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to set alert.
+ *
+ * PARAM:
+ * @param[in] directive: the set alert directive.
+ *
+ * @RETURN: DUER_OK if success,
+ *          DUER_MSG_RSP_BAD_REQUEST if the directive is invalid,
+ *          DUER_ERR_FAILED if other error happened.
+ */
+duer_status_t duer_dcs_tone_alert_set_handler(const baidu_json *directive);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to delete alert.
+ *
+ * PARAM:
+ * @param[in] token: the token of the alert need to delete.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_alert_delete_handler(const char *token);
+
+/**
+ * DESC:
+ * Add a alert info to the alert list, it should be used in duer_dcs_get_all_alert callback.
+ * This API could be used in duer_dcs_get_all_alert, so DCS can get all alerts info.
+ *
+ * PARAM:
+ * @param[out] alert_list: used to store the alert info.
+ * @param[in] alert_info: the info about the alert, such as token, schedule time.
+ * @param[in] is_active: whether this alert is sounding.
+ *
+ * @return: 0 when success, negative value when failed.
+ */
+int duer_insert_alert_list(baidu_json *alert_list,
+                            duer_dcs_alert_info_type *alert_info,
+                            duer_bool is_active);
+
+/**
+ * DESC:
+ * It's used to get all alert info by DCS.
+ * Developer can implement this interface by call duer_dcs_report_alert to report all alert info.
+ *
+ * PARAM:
+ * @param[out] alert_list: a json array used to store all alert info.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_get_all_alert(baidu_json *alert_list);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DCS_ALERT_H*/
+

+ 610 - 0
include/libduer-device/include/lightduer_dcs_local.h

@@ -0,0 +1,610 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_dcs_local.h
+ * Auth: Gang Chen (chengang12@baidu.com)
+ * Desc: Provide some functions for dcs module locally.
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_DCS_LOCAL_H
+#define BAIDU_DUER_LIGHTDUER_DCS_LOCAL_H
+
+#include <stdlib.h>
+#include <stdint.h>
+#include "baidu_json.h"
+#include "lightduer_types.h"
+#include "lightduer_log.h"
+#include "lightduer_dcs_router.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+    DCS_DIALOG_FINISHED,
+    DCS_RECORD_STARTED,
+    DCS_SPEECH_NEED_PLAY,
+    DCS_SPEECH_FINISHED,
+    DCS_AUDIO_NEED_PLAY,
+    DCS_AUDIO_FINISHED,
+    DCS_AUDIO_STOPPED,
+    DCS_NEED_OPEN_MIC,
+} duer_dcs_channel_switch_event_t;
+
+#ifdef DCS_CRITICAL_LOCK_DEBUG
+#define DUER_DCS_CRITICAL_ENTER() \
+    do {\
+        DUER_LOGI("%s entry", __FUNCTION__);\
+        duer_dcs_critical_enter_internal();\
+        DUER_LOGI("in");\
+    } while (0)
+
+#define DUER_DCS_CRITICAL_EXIT() \
+    do {\
+        DUER_LOGI("%s exit", __FUNCTION__);\
+        duer_dcs_critical_exit_internal();\
+        DUER_LOGI("out");\
+    } while (0)
+#else
+#define DUER_DCS_CRITICAL_ENTER() duer_dcs_critical_enter_internal()
+#define DUER_DCS_CRITICAL_EXIT() duer_dcs_critical_exit_internal()
+#endif
+
+// resouce path
+extern const char *DCS_DUER_DIRECTIVE_PATH;
+extern const char *DCS_DUER_PRIVATE_PATH;
+extern const char *DCS_IOTCLOUD_DIRECTIVE_PATH;
+extern const char *DCS_IOTCLOUD_EVENT_PATH;
+
+// namespace
+extern const char *DCS_AUDIO_PLAYER_NAMESPACE;
+extern const char *DCS_VOICE_OUTPUT_NAMESPACE;
+extern const char *DCS_VOICE_INPUT_NAMESPACE;
+extern const char *DCS_SCREEN_NAMESPACE;
+extern const char *DCS_SCREEN_EXTENDED_CARD_NAMESPACE;
+extern const char *DCS_IOT_CLOUD_SYSTEM_NAMESPACE;
+extern const char *DCS_PLAYBACK_CONTROL_NAMESPACE;
+extern const char *DCS_RECOMMEND_NAMESPACE;
+
+// message keys
+extern const char *DCS_DIRECTIVE_KEY;
+extern const char *DCS_CLIENT_CONTEXT_KEY;
+extern const char *DCS_EVENT_KEY;
+extern const char *DCS_HEADER_KEY;
+extern const char *DCS_PAYLOAD_KEY;
+
+// header keys
+extern const char *DCS_NAMESPACE_KEY;
+extern const char *DCS_NAME_KEY;
+extern const char *DCS_MESSAGE_ID_KEY;
+extern const char *DCS_DIALOG_REQUEST_ID_KEY;
+
+// payload keys
+extern const char *DCS_TOKEN_KEY;
+extern const char *DCS_TYPE_KEY;
+extern const char *DCS_CODE_KEY;
+extern const char *DCS_VOLUME_KEY;
+extern const char *DCS_URL_KEY;
+extern const char *DCS_ACTIVE_ALERTS_KEY;
+extern const char *DCS_TARGET_KEY;
+extern const char *DCS_SCHEDULED_TIME_KEY;
+extern const char *DCS_PLAYER_ACTIVITY_KEY;
+extern const char *DCS_ALL_ALERTS_KEY;
+extern const char *DCS_ERROR_KEY;
+extern const char *DCS_MUTE_KEY;
+extern const char *DCS_INITIATOR_KEY;
+extern const char *DCS_AUDIO_ITEM_KEY;
+extern const char *DCS_CLEAR_BEHAVIOR_KEY;
+extern const char *DCS_PLAY_BEHAVIOR_KEY;
+extern const char *DCS_INACTIVE_TIME_IN_SECONDS_KEY;
+extern const char *DCS_OFFSET_IN_MILLISECONDS_KEY;
+extern const char *DCS_TIMEOUT_IN_MILLISECONDS_KEY;
+extern const char *DCS_METADATA_KEY;
+extern const char *DCS_TEXT_KEY;
+extern const char *DCS_CONNECTION_SWITCH_KEY;
+extern const char *DCS_DESCRIPTION_KEY;
+extern const char *DCS_BLUETOOTH_KEY;
+extern const char *DCS_MUTED_KEY;
+extern const char *DCS_UNPARSED_DIRECTIVE_KEY;
+extern const char *DCS_FORMAT_KEY;
+extern const char *DCS_MESSAGE_KEY;
+extern const char *DCS_QUERY_KEY;
+extern const char *DCS_TIME_OF_RECOMMEND;
+
+// directive name
+extern const char *DCS_SPEAK_NAME;
+extern const char *DCS_PLAY_NAME;
+extern const char *DCS_STOP_NAME;
+extern const char *DCS_CLEAR_QUEUE_NAME;
+extern const char *DCS_LISTEN_NAME;
+extern const char *DCS_GET_STATUS_NAME;
+extern const char *DCS_STOP_SPEAK_NAME;
+extern const char *DCS_SET_BLUETOOTH_NAME;
+extern const char *DCS_RENDER_CARD_NAME;
+extern const char *DCS_SET_ALERT_NAME;
+extern const char *DCS_STOP_LISTEN_NAME;
+extern const char *DCS_SET_BLUETOOTH_CONNECTION_NAME;
+extern const char *DCS_SET_MUTE_NAME;
+extern const char *DCS_DELETE_ALERT_NAME;
+extern const char *DCS_RENDER_VOICE_INPUT_TEXT_NAME;
+extern const char *DCS_SET_VOLUME_NAME;
+extern const char *DCS_ADJUST_VOLUME_NAME;
+extern const char *DCS_TEXT_INPUT_NAME;
+extern const char *DCS_RENDER_WEATHER;
+extern const char *DCS_RENDER_PLAYER_INFO;
+extern const char *DCS_RENDER_AUDIO_LIST;
+extern const char *DCS_RENDER_ALBUM_LIST;
+
+// internal directive.
+extern const char *DCS_DIALOGUE_FINISHED_NAME;
+extern const char *DCS_THROW_EXCEPTION_NAME;
+extern const char *DCS_RESET_USER_INACTIVITY_NAME;
+extern const char *DCS_NOP_NAME;
+extern const char *DCS_IOT_CLOUD_CONTEXT;
+
+// event name
+extern const char *DCS_SYNCHRONIZE_STATE_NAME;
+extern const char *DCS_EXCEPTION_ENCOUNTERED_NAME;
+extern const char *DCS_VOLUME_STATE_NAME;
+extern const char *DCS_LISTEN_STARTED_NAME;
+extern const char *DCS_SPEECH_STARTED_NAME;
+extern const char *DCS_ALERTS_STATE_NAME;
+extern const char *DCS_LISTEN_TIMED_OUT_NAME;
+extern const char *DCS_EXITED_NAME;
+extern const char *DCS_PLAYBACK_STATE_NAME;
+extern const char *DCS_SPEECH_FINISHED_NAME;
+extern const char *DCS_VOLUME_CHANGED_NAME;
+extern const char *DCS_MUTE_CHANGED_NAME;
+extern const char *DCS_SPEECH_STATE_NAME;
+extern const char *DCS_LINK_CLICKED_NAME;
+extern const char *DCS_RECOMMEND_NAME;
+
+// internal exception type
+extern const char *DCS_UNEXPECTED_INFORMATION_RECEIVED_TYPE;
+extern const char *DCS_INTERNAL_ERROR_TYPE;
+extern const char *DCS_UNSUPPORTED_OPERATION_TYPE;
+
+/**
+ * DESC:
+ * Get current dialog request id.
+ *
+ * PARAM: none
+ *
+ * @RETURN: current dialog request id
+ */
+const char *duer_get_request_id_internal(void);
+
+/**
+ * DESC:
+ * Create a new dialog request id for each conversation.
+ *
+ * PARAM: none
+ *
+ * @RETURN: the new dialog request id
+ */
+const char *duer_create_request_id_internal(void);
+
+/**
+ * DESC:
+ * Get the client context.
+ *
+ * PARAM: none
+ *
+ * @RETURN: NULL if failed, pointer point to the client context if success.
+ */
+baidu_json *duer_get_client_context_internal(void);
+
+/**
+ * DESC:
+ * Check whether there is a speech waiting to play.
+ *
+ * PARAM: none
+ *
+ * @RETURN: DUER_TRUE if a speech is waiting to play, DUER_FALSE if not.
+ */
+duer_bool duer_speech_need_play_internal(void);
+
+/**
+ * DESC:
+ * Pause the audio player.
+ *
+ * PARAM is_breaking: breaking a audio or a new audio is pending
+ *
+ * @RETURN: none.
+ */
+void duer_pause_audio_internal(duer_bool is_breaking);
+
+/**
+ * DESC:
+ * Resume the audio player.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_resume_audio_internal(void);
+
+/**
+ * DESC:
+ * Notify that speech is stopped.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_speech_on_stop_internal(void);
+
+/**
+ * DESC:
+ * Report exception.
+ *
+ * @PARAM[in] directive: which directive cause to the exception
+ * @PARAM[in] directive_len: the length of directive, if directive_len <=0, the value got from strlen
+ * @PARAM[in] type: exception type
+ * @PARAM[in] msg: excetpion content
+ *
+ * @RETURN: 0 if success, negative if failed.
+ */
+int duer_report_exception_internal(const char* directive, size_t directive_len,
+                                   const char* type, const char* msg);
+
+/**
+ * DESC:
+ * get Report exception in baidu_json format.
+ *
+ * @PARAM[in] directive: which directive cause to the exception
+ * @PARAM[in] directive_len: the length of directive, if directive_len <=0, the value got from strlen
+ * @PARAM[in] type: exception type
+ * @PARAM[in] msg: excetpion content
+ *
+ * @RETURN: NULL if failed.
+ */
+baidu_json *duer_get_exception_internal(const char *directive, size_t directive_len,
+                                        const char *type, const char *msg);
+
+/**
+ * DESC:
+ * Declare the system interface.
+ *
+ * @PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_declare_sys_interface_internal(void);
+
+/**
+ * DESC:
+ * Used to reset user activety time.
+ *
+ * @PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_user_activity_internal(void);
+
+/**
+ * DESC:
+ * Returns a pointer to a new string which is a duplicate of the string 'str'.
+ *
+ * @PARAM[in] str: the string need to duplicated.
+ *
+ * @RETURN: a pointer to the duplicated string, or NULL if insufficient memory was available.
+ */
+char *duer_strdup_internal(const char *str);
+
+/**
+ * DESC:
+ * Used to check whether there is a multiple rounds dialogue.
+ *
+ * @PARAM: none
+ *
+ * @RETURN: DUER_TRUE if it is multiple round dialogue.
+ */
+duer_bool duer_is_multiple_round_dialogue(void);
+
+/**
+ * DESC:
+ * Checking whether a audio is playing.
+ *
+ * @RETURN: Reture DUER_TRUE if audio is playing, else returen DUER_FALSE.
+ */
+duer_bool duer_audio_is_playing_internal(void);
+
+/**
+ * DESC:
+ * Chosing the highest priority play channel.
+ *
+ * @RETURN: None.
+ */
+void duer_play_channel_control_internal(duer_dcs_channel_switch_event_t event);
+
+/**
+ * DESC:
+ * Checking whether a audio is paused by higher play channel.
+ *
+ * @RETURN: Reture DUER_TRUE if audio is paused, else returen DUER_FALSE.
+ */
+duer_bool duer_audio_is_paused_internal(void);
+
+/**
+ * DESC:
+ * Initialize the local resource, such as lock.
+ *
+ * @RETURN: None.
+ */
+void duer_dcs_local_init_internal(void);
+
+/**
+ * DESC:
+ * Checking whether the micphone is recording or not.
+ *
+ * @RETURN: Reture DUER_TRUE if audio is paused, else returen DUER_FALSE.
+ */
+duer_bool duer_is_recording_internal(void);
+
+/**
+ * DESC:
+ * Starting the audio player.
+ *
+ * @RETURN: None.
+ */
+void duer_start_audio_play_internal(void);
+
+/**
+ * DESC:
+ * DCS mode use it to report data.
+ *
+ * @PARAM[in] data: the report data.
+ * @PARAM[in] is_transparent: some items might be added to data if it is DUER_FALSE,
+ *                            such as the English to Chinese or Chinese to English translate flag.
+ *
+ * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+int duer_dcs_data_report_internal(baidu_json *data, duer_bool is_transparent);
+
+/**
+ * DESC:
+ * Open micphone.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_open_mic_internal(void);
+
+/**
+ * DESC:
+ * Cancel the cached directive.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_cancel_caching_directive_internal(void);
+
+/**
+ * DESC:
+ * Used to check should we resume audio after dialog finish or not.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: DUER_TRUE: don't resume audio until dialog finish directive executed.
+ */
+duer_bool duer_wait_dialog_finished_internal(void);
+
+/**
+ * DESC:
+ * Used to exit multi dialog by reporting Exited event.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: DUER_OK if success, negative if failed.
+ */
+int duer_dcs_sys_exit_internal(void);
+
+/**
+ * DESC:
+ * Used to notify DCS router that tts play finished.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_speak_directive_finished_internal(void);
+
+/**
+ * DESC:
+ * Enter the critical areas.
+ * We use the same lock to protect the most critical areas, as:
+ * 1. it isn't hithly concurrent program
+ * 2. the execute time of function in DCS module is short
+ * 3. same recource
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: DUER_OK if success, negative if failed.
+ */
+duer_status_t duer_dcs_critical_enter_internal(void);
+
+/**
+ * DESC:
+ * Exit the critical areas.
+ * We use the same lock to protect the most critical areas, as:
+ * 1. it isn't hithly concurrent program
+ * 2. the execute time of function in DCS module is short
+ * 3. same recource
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: DUER_OK if success, negative if failed.
+ */
+duer_status_t duer_dcs_critical_exit_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS voice output interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_voice_output_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS voice input interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_voice_input_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS system interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_sys_interface_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the speaker control interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_speaker_control_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS screen interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_screen_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Initialize the DCS play control interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_play_control_init_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS play control interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_play_control_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS device control interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_device_control_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS audio player interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_audio_player_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS alert interface.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_alert_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Used to stop the micphone when tts coming.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_listen_stop_internal(void);
+
+/**
+ * DESC:
+ * Uninitialize the DCS local resource.
+ *
+ * @PARAM: none.
+ *
+ * @RETURN: none.
+ */
+void duer_dcs_local_uninitialize_internal(void);
+
+/**
+ * DESC:
+ * Generate msg id
+ * @PARAM: none.
+ *
+ * @RETURN: the new msg id.
+ */
+const char *duer_generate_msg_id_internal(void);
+
+/**
+ * Add dcs directive.
+ *
+ * @param directive: the directive info, include directive name, handler, etc.
+ * @param count, how many directives will be added.
+ * @param name_space: the namespace of the directives needed to add
+ * @return 0 if success, negative if failed.
+ */
+duer_status_t duer_add_dcs_directive_internal(const duer_directive_list *directive,
+                                              size_t count,
+                                              const char *name_space);
+
+/**
+ * Register callback to get client context.
+ * Sometimes dcs need to know the device's state.
+ * Hence, some callbacks are needed to get client context.
+ *
+ * @param cb: the callback to get client context.
+ * @return 0 if success, negative if failed.
+ */
+duer_status_t duer_reg_client_context_cb_internal(dcs_client_context_handler cb);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_DCS_LOCAL_H*/

+ 106 - 0
include/libduer-device/include/lightduer_dcs_router.h

@@ -0,0 +1,106 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_dcs_router.h
+ * Auth: Gang Chen (chengang12@baidu.com)
+ * Desc: Light duer DCS APIS.
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_DCS_ROUTER_H
+#define BAIDU_DUER_LIGHTDUER_DCS_ROUTER_H
+
+#include <stdlib.h>
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * DCS directive handler. The handler will be implemented by developer.
+ *
+ * @PARAM[in] directive: the directive info, include directive name, handler, etc.
+ *
+ * @return: return code must be one of following values:
+ *      // no error.
+ *      DUER_OK                         = 0,
+ *      // directive format incorrect, such missing token in Play directive.
+ *      DUER_MSG_RSP_BAD_REQUEST        = 128,
+ *      // device doesn't support this directive,
+ *      DUER_MSG_RSP_NOT_FOUND          = 132,  // 4.04
+ *      // all other errors.
+ *      DUER_ERR_FAILED                 = -0x0001,
+ *      // directive parameter value invalid.
+ *      DUER_ERR_INVALID_PARAMETER      = -0x0010,
+ *      // memory overflow.
+ *      DUER_ERR_MEMORY_OVERLOW         = -0x0011,
+ */
+typedef duer_status_t (*dcs_directive_handler)(const baidu_json *directive);
+typedef baidu_json* (*dcs_client_context_handler)();
+
+typedef struct {
+    const char *directive_name;
+    dcs_directive_handler cb;
+} duer_directive_list;
+
+/**
+ * Add dcs directive.
+ *
+ * @param directive: the directive info, include directive name, handler, etc.
+ * @param count, how many directives will be added.
+ * @param name_space: the namespace of the directives needed to add
+ * @return 0 if success, negative if failed.
+ */
+duer_status_t duer_add_dcs_directive(const duer_directive_list *directive,
+                                     size_t count,
+                                     const char *name_space);
+
+/**
+ * Register callback to get client context.
+ * Sometimes dcs need to know the device's state.
+ * Hence, some callbacks are needed to get client context.
+ *
+ * @param cb: the callback to get client context.
+ * @return 0 if success, negative if failed.
+ */
+duer_status_t duer_reg_client_context_cb(dcs_client_context_handler cb);
+
+/**
+ * DESC:
+ * Used to create dcs event.
+ *
+ * @PARAM[in] namespace: the namespace of the event need to report.
+ * @PARAM[in] name: the name the event need to report.
+ * @PARAM[in] need_msg_id: including messageId item or not.
+ * @RETURN: pinter of the created dcs event if success, or NULL if failed.
+ */
+baidu_json *duer_create_dcs_event(const char *name_space, const char *name, duer_bool need_msg_id);
+
+/**
+ * DESC:
+ * Used to cancel a dcs dialog: the response of a dialogue will be ignored
+ * if user calling this API before receiving the response.
+ *
+ * @PARAM[in] none.
+ * @RETURN:   none.
+ */
+void duer_dcs_dialog_cancel(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_DCS_ROUTER_H*/

+ 64 - 0
include/libduer-device/include/lightduer_debug.h

@@ -0,0 +1,64 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The APIs for debug.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_DEBUG_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_DEBUG_H
+
+#include "lightduer_types.h"
+
+#ifndef DUER_DEBUG_BUF_SIZE
+#ifdef DUER_RELEASE_VERSION
+#define DUER_DEBUG_BUF_SIZE (64)
+#else
+#define DUER_DEBUG_BUF_SIZE  (512)
+#endif
+#endif // DUER_DEBUG_BUF_SIZE
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * The debug output
+ */
+typedef void (*duer_debug_f)(duer_context ctx,
+                            duer_u32_t level,
+                            const char* file,
+                            duer_u32_t line,
+                            const char* fmt);
+
+/*
+ * Set the debug output
+ *
+ * @Param ctx, duer_context, the debug context for user
+ * @Param f_debug, duer_debug_f, the debug output function
+ */
+DUER_EXT void baidu_ca_debug_init(duer_context ctx, duer_debug_f f_debug);
+
+DUER_INT void duer_debug_print(duer_u32_t level, const char* file,
+                             duer_u32_t line, const char* msg);
+
+DUER_INT void duer_debug(duer_u32_t level, const char* file, duer_u32_t line,
+                       const char* fmt, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_DEBUG_H

+ 91 - 0
include/libduer-device/include/lightduer_dev_info.h

@@ -0,0 +1,91 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_dev_info.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: Device information
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DEV_INFO_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DEV_INFO_H
+
+#include "lightduer_system_info.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct DevInfo {
+    char firmware_version[FIRMWARE_VERSION_LEN + 1];
+};
+
+struct DevInfoOps {
+/*
+ * Provide firmware information about the current system
+ */
+    int (*get_firmware_version)(char *firmware_version);
+/*
+ * Set firmware information about the current system
+ */
+    int (*set_firmware_version)(char const *firmware_version);
+};
+
+/*
+ * Set the firmware version of the current system
+ *
+ * @param firmware_version:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_set_firmware_version(char const *firmware_version);
+
+/*
+ * Get the firmware version of the current system
+ *
+ * @param firmware_version: size > FIRMWARE_VERSION_LEN
+ * @param len:              string length
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_get_firmware_version(char *firmware_version, size_t len);
+
+/*
+ * Report device info to Duer Cloud
+ *
+ * @param info:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_report_device_info(void);
+
+/*
+ * Register device info ops
+ *
+ * @param ops:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_register_device_info_ops(struct DevInfoOps *ops);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_DEV_INFO_H

+ 213 - 0
include/libduer-device/include/lightduer_ds_log.h

@@ -0,0 +1,213 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: device log report
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ds_log_version_enum {
+    DUER_DS_LOG_VERSION_UNKNOWN  = 0x00,
+    DUER_DS_LOG_VERSION_1_0      = 0x01,
+    DUER_DS_LOG_VERSION_MAX      = 0x08
+} duer_ds_log_version_enum_t;
+
+typedef enum _duer_ds_log_level_enum {
+    DUER_DS_LOG_LEVEL_FATAL = 0x01,
+    DUER_DS_LOG_LEVEL_ERROR = 0x02,
+    DUER_DS_LOG_LEVEL_WARN  = 0x03,
+    DUER_DS_LOG_LEVEL_INFO  = 0x04,
+    DUER_DS_LOG_LEVEL_DEBUG = 0x05,
+    DUER_DS_LOG_LEVEL_MAX   = 0x08
+} duer_ds_log_level_enum_t;
+
+#ifndef DUER_DS_LOG_DEFAULT_REPORT_LEVEL
+#define DUER_DS_LOG_DEFAULT_REPORT_LEVEL DUER_DS_LOG_LEVEL_INFO
+#endif
+
+#ifndef DUER_DS_LOG_DEFAULT_CACHE_LEVEL
+#define DUER_DS_LOG_DEFAULT_CACHE_LEVEL DUER_DS_LOG_DEFAULT_REPORT_LEVEL
+#endif
+
+/*
+ * update the report level,
+ *        DUER_DS_LOG_LEVEL_FATAL <= log_level <= DUER_DS_LOG_LEVEL_DEBUG
+ *        if < DUER_DS_LOG_LEVEL_FATAL, set to DUER_DS_LOG_LEVEL_FATAL
+ *        if > DUER_DS_LOG_LEVEL_DEBUG, set to DUER_DS_LOG_LEVEL_DEBUG
+ * @param log_level
+ * @return DUER_OK on success, other fail
+ *
+ */
+duer_status_t duer_ds_log_set_report_level(duer_ds_log_level_enum_t log_level);
+
+/**
+ * init the control point for setting report level
+ */
+void duer_ds_log_init(void);
+
+typedef enum _duer_ds_log_module_enum {
+    DUER_DS_LOG_MODULE_CA       = 0x01,
+    DUER_DS_LOG_MODULE_RECORDER = 0x02,
+    DUER_DS_LOG_MODULE_MEDIA    = 0x03,
+    DUER_DS_LOG_MODULE_HTTP     = 0x04,
+    DUER_DS_LOG_MODULE_DCS      = 0x05,
+    DUER_DS_LOG_MODULE_SPEEX    = 0x06,
+    DUER_DS_LOG_MODULE_OTA      = 0x07,
+    DUER_DS_LOG_MODULE_SYSTEM   = 0x08,
+    DUER_DS_LOG_MODULE_ANALYSIS = 0x09,
+    DUER_DS_LOG_MODULE_BIND     = 0x0A,
+    DUER_DS_LOG_MODULE_MAX      = 0x20
+} duer_ds_log_module_enum_t;
+
+typedef enum _duer_ds_log_family_enum {
+    DUER_DS_LOG_FAMILY_UNKNOWN  = 0x00,
+    DUER_DS_LOG_FAMILY_MEMORY   = 0x01,
+    DUER_DS_LOG_FAMILY_NETWORK  = 0x02,
+    DUER_DS_LOG_FAMILY_MAX      = 0x0F
+} duer_ds_log_family_enum_t;
+
+/**
+ *  {
+ *      "duer_trace_info": {
+ *          "code": [打点信息],
+ *          "message": [详细信息], // 可选
+ *          "ts": [当前时间戳]
+ *      }
+ *  }
+ * the code format:
+ *   0               1               2               3
+ *   0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+ *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *   |X| Ver |X| Lel |X X X|  Module | Family|      Code             |
+ *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ */
+
+#define MASK_VERSION   0x70000000
+#define BITS_VERSION   0x07
+#define OFFSET_VERSION 28
+#define MASK_LEVEL     0x07000000
+#define BITS_LEVEL     0x07
+#define OFFSET_LEVEL   24
+#define MASK_MODULE    0x001F0000
+#define BITS_MODULE    0x1F
+#define OFFSET_MODULE  16
+#define MASK_FAMILY    0x0000F000
+#define BITS_FAMILY    0x0F
+#define OFFSET_FAMILY  12
+#define MASK_CODE      0x00000FFF
+#define BITS_CODE      0x0FFF
+#define OFFSET_CODE    0
+
+/**
+  * generate the code in the above format from the input parameter
+  * @param log_version
+  * @param log_level
+  * @param log_module
+  * @param log_family
+  * @param log_code
+  * @return return the code in the above format
+  */
+duer_u32_t duer_ds_log_generate_code(duer_ds_log_version_enum_t log_version,
+                                     duer_ds_log_level_enum_t   log_level,
+                                     duer_ds_log_module_enum_t  log_module,
+                                     duer_ds_log_family_enum_t  log_family,
+                                     int log_code);
+
+/**
+ * @param log_code the code in the above format which is 32bits
+ * @return the version
+ */
+duer_u32_t duer_ds_log_get_log_version(duer_u32_t log_code);
+
+/**
+ * @param log_code the code in the above format which is 32bits
+ * @return the level
+ */
+duer_u32_t duer_ds_log_get_log_level(duer_u32_t log_code);
+
+/**
+ * @param log_code the code in the above format which is 32bits
+ * @return the module
+ */
+duer_u32_t duer_ds_log_get_log_module(duer_u32_t log_code);
+
+/**
+ * @param log_code the code in the above format which is 32bits
+ * @return the family
+ */
+duer_u32_t duer_ds_log_get_log_family(duer_u32_t log_code);
+
+/**
+ * @param log_code the code in the above format which is 32bits
+ * @return the code
+ */
+duer_u32_t duer_ds_log_get_log_code(duer_u32_t log_code);
+
+/**
+ *  #param log_version the version of the log
+ *  @param log_level the log level, Fatal, Error, Warn, Info, Debug
+ *  @param log_module the module code
+ *  @param log_family the family of this log
+ *  @param log_code the code for this info, maybe use to parse the content of message
+ *                  only the low 12bits use
+ *  @param log_message the content of the info, in json format
+ *         the caller should not try to release this if exist.
+ *  @return DUER_OK on success, or fail
+ *  an example:
+ *  {
+ *      "duer_trace_info": {
+ *          "code": 2001, //
+ *          "message": {
+ *              "reason": "REBOOT",
+ *              "count": 1
+ *          },
+ *          "ts": 220509360
+ *      }
+ *  }
+ */
+duer_status_t duer_ds_log_v_f(duer_ds_log_version_enum_t log_version,
+                              duer_ds_log_level_enum_t   log_level,
+                              duer_ds_log_module_enum_t  log_module,
+                              duer_ds_log_family_enum_t  log_family,
+                              int log_code,
+                              const baidu_json *log_message);
+
+duer_status_t duer_ds_log_f(duer_ds_log_level_enum_t   log_level,
+                            duer_ds_log_module_enum_t  log_module,
+                            duer_ds_log_family_enum_t  log_family,
+                            int log_code,
+                            const baidu_json *log_message);
+
+duer_status_t duer_ds_log(duer_ds_log_level_enum_t   log_level,
+                          duer_ds_log_module_enum_t  log_module,
+                          int log_code,
+                          const baidu_json *log_message);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_H

+ 274 - 0
include/libduer-device/include/lightduer_ds_log_audio.h

@@ -0,0 +1,274 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Chen Xihao(chenxihao@baidu.com)
+ * Desc: audio play related report log
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_AUDIO_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_AUDIO_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ds_log_audio_code {
+    DUER_DS_LOG_AUDIO_BUFFER_INFO               = 0x001,
+    DUER_DS_LOG_AUDIO_PLAY_START                = 0x002,
+    DUER_DS_LOG_AUDIO_PLAY_PAUSE                = 0x003,
+    DUER_DS_LOG_AUDIO_PLAY_RESUME               = 0x004,
+    DUER_DS_LOG_AUDIO_PLAY_STOP                 = 0x005,
+    DUER_DS_LOG_AUDIO_PLAY_FINISH               = 0x006,
+    DUER_DS_LOG_AUDIO_CODEC_VERSION             = 0x007,
+    DUER_DS_LOG_AUDIO_INFO                      = 0x008,
+    DUER_DS_LOG_AUDIO_M4A_HEAD_SIZE             = 0x009,
+    DUER_DS_LOG_AUDIO_CONTENT_TYPE              = 0x00a,
+    DUER_DS_LOG_AUDIO_DOWNLOAD_DELAY            = 0x00b,
+    DUER_DS_LOG_AUDIO_PLAY_DELAY                = 0x00c,
+
+    DUER_DS_LOG_AUDIO_NO_MEMORY                 = 0x301,
+    DUER_DS_LOG_AUDIO_UNSUPPORT_FORMAT          = 0x302,
+    DUER_DS_LOG_AUDIO_UNSUPPORT_BITRATE         = 0x303,
+    DUER_DS_LOG_AUDIO_HEAD_TOO_LARGE            = 0x304,
+    DUER_DS_LOG_AUDIO_DOWNLOAD_FAILED           = 0x305,
+    DUER_DS_LOG_AUDIO_PLAYER_ERROR              = 0x306,
+    DUER_DS_LOG_AUDIO_BUFFER_ERROR              = 0x307,
+    DUER_DS_LOG_AUDIO_CODEC_ERROR               = 0x308,
+    DUER_DS_LOG_AUDIO_NULL_POINTER              = 0x309,
+    DUER_DS_LOG_AUDIO_INVALID_CONTEXT           = 0x30a,
+    DUER_DS_LOG_AUDIO_URL_NO_DATA               = 0x30b,
+    DUER_DS_LOG_AUDIO_PLAY_FAILED               = 0x30c,
+
+    DUER_DS_LOG_AUDIO_M4A_INVALID_HEADER        = 0x401,
+    DUER_DS_LOG_AUDIO_M4A_PARSE_HEADER_ERROR    = 0x402,
+
+    DUER_DS_LOG_AUDIO_HLS_PARSE_M3U8_ERROR      = 0x501,
+    DUER_DS_LOG_AUDIO_HLS_DOWNLOAD_ERROR        = 0x502,
+    DUER_DS_LOG_AUDIO_HLS_RELOAD_ERROR          = 0x503,
+
+    DUER_DS_LOG_AUDIO_SOFT_DECODER_ERROR        = 0x601,
+
+    DUER_DS_LOG_AUDIO_USER_DEFINED_ERROR        = 0xf01,
+} duer_ds_log_audio_code_t;
+
+typedef struct duer_ds_audio_info_s {
+    duer_u32_t duration;
+    duer_u32_t block_count;
+    duer_u32_t max_codec_bitrate;
+    duer_u32_t min_codec_bitrate;
+    duer_u32_t avg_codec_bitrate;
+    duer_u32_t request_play_ts;
+    duer_u32_t request_download_ts;
+} duer_ds_audio_info_t;
+
+#define DUER_DS_LOG_AUDIO_MEMORY_OVERFLOW() duer_ds_log_audio_memory_overflow(__FILE__, __LINE__)
+
+#define DUER_DS_LOG_AUDIO_NULL_POINTER()    duer_ds_log_audio_null_pointer(__FILE__, __LINE__)
+
+#define DUER_DS_LOG_AUDIO_INVALID_CONTEXT() duer_ds_log_audio_invalid_context(__FILE__, __LINE__)
+
+/**
+ * common function to report the log of audio play moudle
+ */
+duer_status_t duer_ds_log_audio(duer_ds_log_audio_code_t log_code, const baidu_json *log_message);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_BUFFER_INFO
+ *  {
+ *     "size": [buffer size],
+ *     "type": [buffer type] // 0: heap, 1: data segment, 2: psram 3: file
+ *  }
+ */
+duer_status_t duer_ds_log_audio_buffer_info(duer_u32_t size, duer_u32_t type);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAY_START
+ *  {
+ *     "url": [url of audio],
+ *     "source_type": [source type]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_play_start(const char *url, duer_u32_t type);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAY_STOP
+ *  {
+ *     "duration": [播放时长],
+ *     "block_count": [卡顿次数],
+ *     "codec_bitrate": {
+ *         "max" : [最大码率],
+ *         "min" : [最小码率],
+ *         "avg" : [平均码率]
+ *     }
+ *  }
+ */
+duer_status_t duer_ds_log_audio_play_stop(duer_u32_t duration, duer_u32_t block_count,
+        duer_u32_t max_bitrate, duer_u32_t min_bitrate, duer_u32_t avg_bitrate);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAY_FINISH
+ *  {
+ *     "duration": [播放时长],
+ *     "block_count": [卡顿次数],
+ *     "codec_bitrate": {
+ *         "max" : [最大码率],
+ *         "min" : [最小码率],
+ *         "avg" : [平均码率]
+ *     }
+ *  }
+ */
+duer_status_t duer_ds_log_audio_play_finish(duer_u32_t duration, duer_u32_t block_count,
+        duer_u32_t max_bitrate, duer_u32_t min_bitrate, duer_u32_t avg_bitrate);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_CODEC_VERSION
+ *  {
+ *     "version": [codec版本号]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_codec_version(const char *version);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_INFO
+ *  {
+ *     "bitrate": [比特率],
+ *     "sample_rate": [采样率],
+ *     "bits_per_sample": [位宽],
+ *     "channels": [声道数]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_info(duer_u32_t bitrate,
+        duer_u32_t sample_rate, duer_u32_t bits_per_sample, duer_u32_t channels);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_M4A_HEAD_SIZE
+ *  {
+ *     "head_size": [m4a头大小]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_m4a_head_size(duer_u32_t size);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_CONTENT_TYPE
+ *  {
+ *     "content_type": [http下载类型]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_content_type(const char *type);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_DOWNLOAD_DELAY
+ *  {
+ *     "delay": [时延,单位ms]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_download_delay(void);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAY_DELAY
+ *  {
+ *     "delay": [时延,单位ms]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_play_delay(void);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_NO_MEMORY
+ *  {
+ *     "file": [发生内存分配错误的文件],
+ *     "line": [发生内存分配错误的行号]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_memory_overflow(const char *file, duer_u32_t line);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_NULL_POINTER
+ *  {
+ *     "file": the file name where error happens
+ *     "line": the line number where error happens
+ *  }
+ */
+duer_status_t duer_ds_log_audio_null_pointer(const char *file, duer_u32_t line);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_INVALID_CONTEXT
+ *  {
+ *     "file": the file name where error happens
+ *     "line": the line number where error happens
+ *  }
+ */
+duer_status_t duer_ds_log_audio_invalid_context(const char *file, duer_u32_t line);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_HLS_PARSE_M3U8_ERROR
+ *  {
+ *     "line": [解析出错的行号]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_hls_parse_m3u8_error(duer_u32_t line);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_FRAMEWORK_ERROR
+ *  {
+ *     "error_code": Internal error code for audio framework
+ *  }
+ */
+duer_status_t duer_ds_log_audio_framework_error(int error_code);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAYER_ERROR
+ *  {
+ *     "error_code": Internal error code for audio player
+ *  }
+ */
+duer_status_t duer_ds_log_audio_player_error(int error_code);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_PLAY_FAILED
+ *  {
+ *     "url": [音频url]
+ *  }
+ */
+duer_status_t duer_ds_log_audio_play_failed(const char *url);
+
+/**
+ * report the message for log code DUER_DS_LOG_AUDIO_USER_DEFINED_ERROR
+ *  {
+ *     "error_code": user defined error code
+ *  }
+ */
+duer_status_t duer_ds_log_audio_user_defined_error(int error_code);
+
+/**
+ * record the timestamp when request to play
+ */
+duer_status_t duer_ds_log_audio_request_play(void);
+
+/**
+ * record the timestamp when request to download
+ */
+duer_status_t duer_ds_log_audio_request_download(void);
+
+extern duer_ds_audio_info_t g_audio_infos;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_AUDIO_H

+ 73 - 0
include/libduer-device/include/lightduer_ds_log_audio_player.h

@@ -0,0 +1,73 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Su Hao(suhao@baidu.com)
+ * Desc: Record the audio player errors
+ */
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICES_STATUS_LIGHTDUER_DS_LOG_AUDIO_PLAYER_H
+#define BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICES_STATUS_LIGHTDUER_DS_LOG_AUDIO_PLAYER_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_dslog_audply_code_e {
+    DUER_DS_LOG_AUDPLY_FINISHED             = 0x001,
+    DUER_DS_LOG_AUDPLY_TERMINATED           = 0x002,
+
+    // Internal Errors
+    DUER_DS_LOG_AUDPLY_INTERNAL_ERROR       = 0x500,
+
+    // HTTP errors
+    DUER_DS_LOG_AUDPLY_HTTP_REQUEST_ERROR,
+    DUER_DS_LOG_AUDPLY_HTTP_RESPONSE_ERROR,
+
+    // File errors
+    DUER_DS_LOG_AUDPLY_FILE_ERROR,
+
+    // Codec errors
+    DUER_DS_LOG_AUDPLY_CODEC_ERROR,
+} duer_dslog_audply_code_t;
+
+/**
+ * common function to report the log of audio play moudle
+ *
+ * @param log_code, duer_dslog_audply_code_t, defined the error type
+ * @param uri, const char *, file path or url
+ * @param speed, float, download speed average
+ * @param code, int, internal error code, 0 means not report
+ * @param file, conat char *, file where the error occurs
+ * @param line, int, the file line where the error occurs
+ */
+duer_status_t duer_ds_log_audply(duer_dslog_audply_code_t log_code, const char *uri,
+        float speed, int code, const char *file, int line);
+
+#define DUER_DS_LOG_AUDPLY_INFO(_code, _uri, _speed)                    \
+        duer_ds_log_audply(_code, _uri, _speed, 0, NULL, 0)
+#define DUER_DS_LOG_AUDPLY_ERROR(_code, _uri, _speed, _errcode)         \
+        duer_ds_log_audply(_code, _uri, _speed, _errcode, __FILE__, __LINE__)
+#define DUER_DS_LOG_AUDPLY_ERROR_ONLY(_code, _uri, _speed, _errcode)    \
+        duer_ds_log_audply(_code, _uri, _speed, _errcode, NULL, 0)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICES_STATUS_LIGHTDUER_DS_LOG_AUDIO_PLAYER_H

+ 58 - 0
include/libduer-device/include/lightduer_ds_log_bind.h

@@ -0,0 +1,58 @@
+/**
+ * Copyright (2018) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Chen Xihao(chenxihao@baidu.com)
+ * Desc: bind device module related report log
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_BIND_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_BIND_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ds_log_bind_code {
+    DUER_DS_LOG_BIND_CONFIG_WIFI_AIRKISS        = 0x001,
+    DUER_DS_LOG_BIND_CONFIG_WIFI_SMARTCONFIG    = 0x002,
+    DUER_DS_LOG_BIND_CONFIG_WIFI_SONIC          = 0x003,
+    DUER_DS_LOG_BIND_CONFIG_WIFI_BT             = 0x004,
+    DUER_DS_LOG_BIND_TASK_START                 = 0x005,
+    DUER_DS_LOG_BIND_SEND_RESP                  = 0x006,
+    DUER_DS_LOG_BIND_TASK_END                   = 0x007,
+
+    DUER_DS_LOG_BIND_NO_MEMORY                  = 0x301,
+    DUER_DS_LOG_BIND_INVALID_PARAMS             = 0x302,
+    DUER_DS_LOG_BIND_START_TASK_FAILED          = 0x303,
+    DUER_DS_LOG_BIND_INIT_AES_FAILED            = 0x304,
+    DUER_DS_LOG_BIND_INIT_SOCKET_FAILED         = 0x305,
+    DUER_DS_LOG_BIND_BIND_SOCKET_FAILED         = 0x306,
+    DUER_DS_LOG_BIND_ENCRYPT_FAILED             = 0x307,
+    DUER_DS_LOG_BIND_SOCKET_ERROR               = 0x308,
+} duer_ds_log_bind_code_t;
+
+/**
+ * common function to report the log of bind device moudle
+ */
+duer_status_t duer_ds_log_bind(duer_ds_log_bind_code_t log_code);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_BIND_H

+ 207 - 0
include/libduer-device/include/lightduer_ds_log_ca.h

@@ -0,0 +1,207 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: ca related trace log
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_CA_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_CA_H
+
+#include "baidu_json.h"
+#include "lightduer_ds_log.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * tracelog for CA module
+ * @param log_level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca(duer_ds_log_level_enum_t log_level,
+                             int log_code,
+                             const baidu_json *log_message);
+
+/*
+ * log code.
+ */
+typedef enum _duer_ds_log_ca_enum {
+    DUER_DS_LOG_CA_INIT                   = 0x001,
+    DUER_DS_LOG_CA_START                  = 0x002,
+    DUER_DS_LOG_CA_START_STATE_CHANGE     = 0x003,
+    DUER_DS_LOG_CA_START_CONNECT          = 0x004,
+    DUER_DS_LOG_CA_STARTED                = 0x005,
+    DUER_DS_LOG_CA_REGISTER_CONTROL_POINT = 0x006,
+    DUER_DS_LOG_CA_CALL_CONTROL_POINT     = 0x007,
+    DUER_DS_LOG_CA_STOPPED                = 0x008,
+    DUER_DS_LOG_CA_CLEAR_DATA             = 0x009,
+    DUER_DS_LOG_CA_HTTP_DNS               = 0x00A,
+    DUER_DS_LOG_CA_ERR                    = 0x300,
+    DUER_DS_LOG_CA_START_PROFILE_ERR      = 0x301,
+    DUER_DS_LOG_CA_CONNECT_DNS_ERR        = 0x302,
+    DUER_DS_LOG_CA_CONNECT_IP_ERR         = 0x303,
+    DUER_DS_LOG_CA_START_STATE_REG_FAIL   = 0x304,
+    DUER_DS_LOG_CA_SEND_ERROR             = 0x305,
+    DUER_DS_LOG_CA_RECV_ERROR             = 0x306,
+    DUER_DS_LOG_CA_MBEDTLS_ERR            = 0x307,
+    DUER_DS_LOG_CA_TCPHEADER_ERR          = 0x308,
+    DUER_DS_LOG_CA_COAP_PARSE_ERR         = 0x309,
+    DUER_DS_LOG_CA_MALLOC_FAIL            = 0x400,
+    DUER_DS_LOG_CA_LOG_CODE_MAX           = 0xFFF
+} duer_ds_log_ca_enum_t;
+
+/**
+ * tracelog for CA module with FATAL log level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca_fatal(int log_code,
+                                   const baidu_json *log_message);
+
+/**
+ * tracelog for CA module with ERROR log level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca_error(int log_code,
+                                   const baidu_json *log_message);
+
+/**
+ * tracelog for CA module with WARN log level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca_warn(int log_code,
+                                  const baidu_json *log_message);
+
+/**
+ * tracelog for CA module with INFO log level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca_info(int log_code,
+                                  const baidu_json *log_message);
+
+/**
+ * tracelog for CA module with DEBUG log level
+ * @param log_code
+ * @param log_message
+ * @return
+ * more info pls see lightduer_ds_log.h
+ */
+duer_status_t duer_ds_log_ca_debug(int log_code,
+                                   const baidu_json *log_message);
+
+/**
+ * try to cache the log_code
+ * @param log_code the error code
+ */
+duer_status_t duer_ds_log_ca_cache_error(int log_code);
+
+/**
+ * @param from old state
+ * @param to new state
+ * report the message for log code DUER_DS_LOG_CA_START_STATE_CHANGE
+ *  {
+ *     "from": [旧状态],
+ *     "to": [新的状态]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_start_state_change(int from, int to);
+
+/**
+ * report the message for log code DUER_DS_LOG_CA_START_CONNECT
+ *  {
+ *     "host": [连接主机],
+ *     "port": [连接端口]
+ *  }
+ */
+
+duer_status_t duer_ds_log_ca_start_connect(const char *host, int port);
+
+/**
+ * report the message for log code DUER_DS_LOG_CA_STARTED
+ *  {
+ *     "reason": [启动原因],
+ *     "count": [启动次数],
+ *     "connect_time:" [成功连接花费时间 ms]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_started(duer_status_t reason, int count, duer_u32_t connect_time);
+
+/*
+ * control point type
+ */
+typedef enum _duer_ds_log_ca_controlpoint_type_enum {
+    DUER_CA_CP_TYPE_STATIC   = 0x000,
+    DUER_CA_CP_TYPE_DYNAMIC  = 0x001
+} duer_ds_log_ca_controlpoint_type_enum_t;
+/**
+ * report the message for log code DUER_DS_LOG_CA_REGISTER_CONTROL_POINT
+ *  {
+ *     "name": [制点名称],
+ *     "type": [STATIC|DYNAMIC]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_register_cp(const char *name,
+                                         duer_ds_log_ca_controlpoint_type_enum_t type);
+
+/**
+ * report the message for log code DUER_DS_LOG_CA_CALL_CONTROL_POINT
+ *  {
+ *     "name": [制点名称]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_call_cp(const char *name);
+
+/**
+ * report the message for log code DUER_DS_LOG_CA_MBEDTLS_ERR
+ *  {
+ *     "code": [错误代码]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_mbedtls_error(duer_s32_t code);
+
+/**
+ * report the message for log code DUER_DS_LOG_CA_MALLOC_FAIL
+ *  {
+ *     "file": [出错文件],
+       "line": [出错行号]
+ *  }
+ */
+duer_status_t duer_ds_log_ca_malloc_error(const char *file, duer_u32_t line);
+
+#define DUER_DS_LOG_CA_MEMORY_OVERFLOW() duer_ds_log_ca_malloc_error(__FILE__, __LINE__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_CA_H

+ 58 - 0
include/libduer-device/include/lightduer_ds_log_cache.h

@@ -0,0 +1,58 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: trace log cache, cache the trace log when ca disconnect
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_CACHE_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_CACHE_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * try to cache the tracelog info
+ *  @param code: the trace log code, see lightduer_ds_log.h for more info
+ *  @param message: the additional message for the @code
+ *  @param timestamp: the timestamp when generate the trace log
+ */
+duer_status_t duer_ds_log_cache_push(duer_u32_t code, const baidu_json *message, duer_u32_t timestamp);
+
+/**
+ *  invoke this after CA connected, try to report the tracelog cached when CA disconnect.
+ */
+duer_status_t duer_ds_log_cache_report(void);
+
+/**
+ * initialize the resource
+ */
+duer_status_t duer_ds_log_cache_initialize(void);
+
+/**
+ * release the resources
+ */
+duer_status_t duer_ds_log_cache_finalize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_CACHE_H

+ 100 - 0
include/libduer-device/include/lightduer_ds_log_dcs.h

@@ -0,0 +1,100 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Gang Chen(chengang12@baidu.com)
+ * Desc: dcs module log report
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_DCS_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_DCS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "lightduer_types.h"
+#include "baidu_json.h"
+
+typedef enum {
+    DUER_DS_LOG_DCS_DIRECTIVE_DROPPED     =  0x201,
+    DUER_DS_LOG_DCS_OLD_DIRECTIVE_DROPPED =  0x202,
+
+    DUER_DS_LOG_DCS_MEMORY_ERROR          =  0x301,
+    DUER_DS_LOG_DCS_HANDLER_UNREALIZED    =  0x302,
+    DUER_DS_LOG_DCS_REPORT_EVENT_FAILED   =  0x303,
+    DUER_DS_LOG_DCS_PARAM_ERROR           =  0x304,
+    DUER_DS_LOG_DCS_STOP_LISTEN_MISSING   =  0x305,
+
+    DUER_DS_LOG_DCS_ADD_DIRECTIVE_FAILED  =  0x401,
+} duer_ds_log_dcs_code_t;
+
+/**
+ * Report ds log for dcs module
+ */
+duer_status_t duer_ds_log_dcs(duer_ds_log_dcs_code_t log_code, const baidu_json *log_message);
+/**
+ * Report ds log with direction information:
+ *        {
+ *            "file" : "dcs.c",
+ *            "line" : 100
+ *        }
+ */
+duer_status_t duer_ds_log_dcs_report_with_dir(duer_ds_log_dcs_code_t log_code,
+                                                   const char *file,
+                                                   int line);
+/**
+ * Report ds log when directive is dropped:
+ *        {
+ *            "current_dialog_id" :   "aaaaaaabbbbbbbbb",      // current dialogId
+ *            "directive_dialog_id" : "bbbbbbbbbaaaaaaa",      // dialogId of this directive
+ *            "directive" : "play"                             // directive name
+ *        }
+ */
+duer_status_t duer_ds_log_dcs_directive_drop(const char *current_id,
+                                             const char *directive_id,
+                                             const char *name);
+/**
+ * Report ds log if the handler is not realized:
+ *        {
+ *            "handler_name" : "duer_dcs_speak_handler"
+ *        }
+ */
+duer_status_t duer_ds_log_dcs_handler_unrealize(const char *func);
+/**
+ * Report ds log if add directive failed:
+ *        {
+ *            "directive" : "play"
+ *        }
+ */
+duer_status_t duer_ds_log_dcs_add_directive_fail(const char *name);
+/**
+ * Report ds log if event report failed:
+ *        {
+ *            "event_name" : "SetAlertFailed"
+ *        }
+ */
+duer_status_t duer_ds_log_dcs_event_report_fail(const char *name);
+
+#define DUER_DS_LOG_REPORT_DCS_MEMORY_ERROR() \
+        duer_ds_log_dcs_report_with_dir(DUER_DS_LOG_DCS_MEMORY_ERROR, __FILE__, __LINE__)
+#define DUER_DS_LOG_REPORT_DCS_PARAM_ERROR() \
+        duer_ds_log_dcs_report_with_dir(DUER_DS_LOG_DCS_PARAM_ERROR, __FILE__, __LINE__)
+#define DUER_DS_LOG_REPORT_DCS_HANDLER_UNREALIZE() duer_ds_log_dcs_handler_unrealize(__func__)
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_DCS_H

+ 77 - 0
include/libduer-device/include/lightduer_ds_log_e2e.h

@@ -0,0 +1,77 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Su Hao(suhao@baidu.com)
+ * Desc: Record the end to end delay
+ */
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICE_STATUS_LIGHTDUER_DS_LOG_E2E_H
+#define BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICE_STATUS_LIGHTDUER_DS_LOG_E2E_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+    DUER_E2E_REQUEST,
+    DUER_E2E_RECORD_FINISH,
+    DUER_E2E_SEND,
+    DUER_E2E_RESPONSE,
+    DUER_E2E_PLAY,
+    DUER_E2E_CODEC,
+    DUER_E2E_EVENT_TOTAL
+} duer_ds_e2e_event_t;
+
+typedef const char* (*duer_ds_e2e_get_dialog_id_cb)(void);
+
+#ifdef DUER_STATISTICS_E2E
+
+void duer_ds_e2e_event(duer_ds_e2e_event_t evt);
+
+void duer_ds_e2e_set_report_codec_timestamp(void);
+
+void duer_ds_e2e_set_not_report_codec_timestamp(void);
+
+void duer_ds_e2e_set_vad_silence_time(duer_u32_t silence_time);
+
+void duer_ds_e2e_set_dialog_id_cb(duer_ds_e2e_get_dialog_id_cb cb);
+
+duer_bool duer_ds_e2e_wait_response(void);
+
+#else
+
+#define duer_ds_e2e_set_report_codec_timestamp(...)
+
+#define duer_ds_e2e_set_not_report_codec_timestamp(...)
+
+#define duer_ds_e2e_set_vad_silence_time(...)
+
+#define duer_ds_e2e_event(...)
+
+#define duer_ds_e2e_set_dialog_id_cb(...)
+
+#define duer_ds_e2e_wait_response(...)
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIBDUER_DEVICE_MODULES_DEVICE_STATUS_LIGHTDUER_DS_LOG_E2E_H

+ 135 - 0
include/libduer-device/include/lightduer_ds_log_http.h

@@ -0,0 +1,135 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Gang Chen(chengang12@baidu.com)
+ * Desc: http module log report
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_HTTP_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_HTTP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "lightduer_types.h"
+#include "stdbool.h"
+#include "baidu_json.h"
+
+typedef enum {
+    DUER_DS_LOG_HTTP_INIT                          =  0x101,
+    DUER_DS_LOG_HTTP_DESTROYED                     =  0x102,
+    DUER_DS_LOG_HTTP_COMMON_ERROR                  =  0x103,
+    DUER_DS_LOG_HTTP_PERSISTENT_CONN_TIMEOUT       =  0x104,
+    DUER_DS_LOG_HTTP_DOWNLOAD_STARTED              =  0x105,
+    DUER_DS_LOG_HTTP_DOWNLOAD_STOPPED              =  0x106,
+    DUER_DS_LOG_HTTP_DOWNLOAD_FINISHED             =  0x107,
+
+    DUER_DS_LOG_HTTP_DOWNLOAD_URL                  =  0x201,
+    DUER_DS_LOG_HTTP_ZERO_BYTE_DOWNLOAD            =  0x202,
+
+    DUER_DS_LOG_HTTP_SOCKET_CONN_FAILED            =  0x301,
+    DUER_DS_LOG_HTTP_URL_PARSE_FAILED              =  0x302,
+    DUER_DS_LOG_HTTP_SEND_FAILED                   =  0x303,
+    DUER_DS_LOG_HTTP_DOWNLOAD_FAILED               =  0x304,
+    DUER_DS_LOG_HTTP_INIT_FAILED                   =  0x305,
+    DUER_DS_LOG_HTTP_CONNECT_CLOSED_BY_SERVER      =  0x306,
+    DUER_DS_LOG_HTTP_REQUEST_SEND_FAILED           =  0x307,
+    DUER_DS_LOG_HTTP_HEADER_SEND_FAILED            =  0x308,
+    DUER_DS_LOG_HTTP_RESPONSE_RECEIVE_FAILED       =  0x309,
+    DUER_DS_LOG_HTTP_HEADER_RECEIVE_FAILED         =  0x30a,
+    DUER_DS_LOG_HTTP_CHUNK_SIZE_RECEIVE_FAILED     =  0x30b,
+    DUER_DS_LOG_HTTP_CHUNK_RECEIVE_FAILED          =  0x30c,
+    DUER_DS_LOG_HTTP_CONTENT_RECEIVE_FAILED        =  0x30d,
+    DUER_DS_LOG_HTTP_MEMORY_ERROR                  =  0x30e,
+    DUER_DS_LOG_HTTP_PARAM_ERROR                   =  0x310,
+    DUER_DS_LOG_HTTP_RECEIVE_FAILED                =  0x311,
+    DUER_DS_LOG_HTTP_REDIRECT_FAILED               =  0x312,
+    DUER_DS_LOG_HTTP_DNS_GET_IP                    =  0x313,
+
+    DUER_DS_LOG_HTTP_SOCKET_INIT_FAILED            =  0x401,
+    DUER_DS_LOG_HTTP_SOCKET_OPEN_FAILED            =  0x402,
+} duer_ds_log_http_code_t;
+
+// used to statistic the download info when download finished/stopped/failed
+typedef struct {
+    const char *url;
+    int        ret_code;
+    size_t     upload_size;
+    size_t     download_size;
+    int        download_speed;
+    int        resume_count;
+    size_t     content_len;
+    bool       is_chunked;
+} duer_ds_log_http_statistic_t;
+
+/**
+ * Report ds log for http module
+ */
+duer_status_t duer_ds_log_http(duer_ds_log_http_code_t log_code, const baidu_json *log_message);
+/**
+ * Report ds log with direction information:
+ *        {
+ *            "file" : "http.c",
+ *            "line" : 100
+ *        }
+ */
+duer_status_t duer_ds_log_http_report_with_dir(duer_ds_log_http_code_t log_code,
+                                              const char *file,
+                                              int line);
+/**
+ * Report ds log when http download function is exit, it include the statistic information.
+ */
+duer_status_t duer_ds_log_http_download_exit(duer_ds_log_http_code_t log_code,
+                                             duer_ds_log_http_statistic_t *statistic);
+/**
+ * Report ds log when http persistent is timeout and be closed.
+ */
+duer_status_t duer_ds_log_http_persisten_conn_timeout(const char *host);
+/**
+ * Report ds log with url:
+ *        {
+ *            "url" : "baidu.com"
+ *        }
+ */
+duer_status_t duer_ds_log_http_report_with_url(duer_ds_log_http_code_t log_code, const char *url);
+/**
+ * Report ds log with error code:
+ *        {
+ *            "err_code" : -3007
+ *        }
+ */
+duer_status_t duer_ds_log_http_report_err_code(duer_ds_log_http_code_t log_code, int err_code);
+/**
+ * Report ds log when redirect failed, it include the redirect location:
+ *        {
+ *            "location" : "http://test.com"
+ *        }
+ */
+duer_status_t duer_ds_log_http_redirect_fail(const char *location);
+
+#define DUER_DS_LOG_REPORT_HTTP_MEMORY_ERROR() \
+        duer_ds_log_http_report_with_dir(DUER_DS_LOG_HTTP_MEMORY_ERROR, __FILE__, __LINE__)
+#define DUER_DS_LOG_REPORT_HTTP_PARAM_ERROR() \
+        duer_ds_log_http_report_with_dir(DUER_DS_LOG_HTTP_PARAM_ERROR, __FILE__, __LINE__)
+#define DUER_DS_LOG_REPORT_HTTP_COMMON_ERROR() \
+        duer_ds_log_http_report_with_dir(DUER_DS_LOG_HTTP_COMMON_ERROR, __FILE__, __LINE__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_HTTP_H

+ 87 - 0
include/libduer-device/include/lightduer_ds_log_recorder.h

@@ -0,0 +1,87 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Yiheng Li(liyiheng01@baidu.com)
+ * Desc: recorder module log report
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_DS_LOG_RECORDER_H
+#define BAIDU_DUER_LIGHTDUER_DS_LOG_RECORDER_H
+
+#include "lightduer_types.h"
+#include "baidu_json.h"
+
+#define REC_LOG_MOD_NAME(name) (#name)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+    DUER_DS_LOG_REC_START = 0x001,
+    DUER_DS_LOG_REC_STOP = 0x002,
+}duer_ds_log_rec_code_t;
+
+/**
+ * update compress infomation,set start/end = duer_timestamp() before/after compress functions
+ */
+void duer_ds_rec_compress_info_update(const duer_u32_t start, const duer_u32_t end);
+
+/**
+ * update compress infomation,set start/end = duer_timestamp() before/after compress functions
+ */
+void duer_ds_rec_delay_info_update(const duer_u32_t request, const duer_u32_t send_start, const duer_size_t send_finish);
+
+/**
+ * report the log code DUER_DS_LOG_REC_START
+ * {
+ *     "id": [session id]
+ * }
+*/
+duer_status_t duer_ds_log_rec_start(duer_u32_t id);
+
+/**
+ * report the message for log code DUER_DS_LOG_REC_STOP
+ * {
+ *     "id": [session id],
+ *     "speex_compress" : {
+ *         "max" : [maximum consuming time of compressing one frame in speex],
+ *         "min" : [minimum consuming time of compressing one frame in speex],
+ *         "avg" : [average consuming time of compressing one frame in speex]
+ *     },
+ *     "request_delay": {
+ *         "max" : [maximum consuming time of request send a voice fragment in ca],
+ *         "min" : [minimum consuming time of request send a voice fragment in ca],
+ *         "avg" : [average consuming time of request send a voice fragment in ca]
+ *     },
+ *     "network_delay": {
+ *         "max" : [maximum consuming time of socket send a voice fragment],
+ *         "min" : [minimum consuming time of socket send a voice fragment],
+ *         "avg" : [average consuming time of socket send a voice fragment]
+ *     },
+ *     "total_delay": {
+ *         "max" : [maximum consuming time of send a voice fragment],
+ *         "min" : [minimum consuming time of send a voice fragment],
+ *         "avg" : [average consuming time of send a voice fragment]
+ *     },
+ * }
+ */
+duer_status_t duer_ds_log_rec_stop(duer_u32_t id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_LOG_RECORDER_H

+ 89 - 0
include/libduer-device/include/lightduer_ds_report.h

@@ -0,0 +1,89 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: device status report
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_REPORT_H
+#define BAIDU_DUER_LIGHTDUER_DS_REPORT_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * the return baidu_json* will be handled by the caller
+ * so the callee should not try to release the baidu_json*
+ */
+typedef baidu_json* (*duer_ds_report_function_t)(void);
+
+typedef enum _duer_ds_report_type_enum {
+    DUER_DS_ONCE,         // report only once
+    DUER_DS_PERIODIC,     // report periodically
+    DUER_DS_TOTAL
+} duer_ds_report_type_enum_t;
+
+/**
+ *  all the register functions will be invoked when try to report the device status.
+ *  all the modules which have status to report should call this to register the
+ *  functions.
+ */
+duer_status_t duer_ds_register_report_function(duer_ds_report_function_t report_function);
+
+/**
+ *  initiate a device status report, DUER_DS_ONCE or DUER_DS_PERIODIC decided by type
+ *  if the periodic timer already started, stop it , restart it with the new interval
+ *  @param type, DUER_DS_ONCE or DUER_DS_PERIODIC
+ *  @param interval, if the report type is DUER_DS_PERIODIC, then this indicate the interval
+ *                   0, means use the default value, now is 5min
+ *  @return, DUER_OK on success, or failed
+ *  report format:
+ *          duer_device_status contains an array
+ *          all the info collect from the functions registed is one item in the array.
+ *  {
+ *      "duer_device_status": [
+ *          {
+ *              "avg_connect_time": "500ms",
+ *              "disconnect_count": 0
+ *          },
+ *          {
+ *              "upload_size": "10Bytes",
+ *              "down_size": "20Bytes"
+ *          }
+ *      ]
+ *  }
+ */
+duer_status_t duer_ds_report_start(duer_ds_report_type_enum_t type, duer_u32_t interval);
+
+/**
+ *  try to stop the periodic report.
+ */
+duer_status_t duer_ds_report_stop(void);
+
+/**
+ *  try to destroy report list.
+ */
+duer_status_t duer_ds_report_destroy(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_REPORT_H

+ 82 - 0
include/libduer-device/include/lightduer_ds_report_ca.h

@@ -0,0 +1,82 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: device status ca report
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_DS_REPORT_CA_H
+#define BAIDU_DUER_LIGHTDUER_DS_REPORT_CA_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+duer_status_t duer_ds_report_ca_update_avg_latency(duer_u32_t al);
+
+duer_status_t duer_ds_report_ca_update_send_max_packet(duer_u32_t smp);
+
+duer_status_t duer_ds_report_ca_inc_valid_recv(void);
+
+duer_status_t duer_ds_report_ca_inc_recv_count(void);
+
+duer_status_t duer_ds_report_ca_update_recv_max_packet(duer_u32_t rmp);
+
+duer_status_t duer_ds_report_ca_inc_error_mbedtls(void);
+
+duer_status_t duer_ds_report_ca_inc_error_tcpheader(void);
+
+duer_status_t duer_ds_report_ca_inc_error_coap(void);
+
+duer_status_t duer_ds_report_ca_inc_error_connect(void);
+
+
+/**
+ * generate the ca status report
+ * this function used in @duer_ds_register_report_function
+ * "ca_status": {
+ *     "connection": {
+ *         "disconnect_time": 100,
+ *         "max_connect": 200,
+ *         "reconnect_count": 2
+ *     },
+ *     "send": {
+ *         "avg_latency": 20,
+ *         "max_packet": 1900
+ *     },
+ *     "recv": {
+ *         "valid_recv": 800,
+ *         "recv_count": 1000,
+ *         "max_packet": 1900
+ *     },
+ *     "error": {
+ *         "mbedtls": 0,
+ *         "tcp_header": 0,
+ *         "coap": 0,
+ *         "connect_fail": 0
+ *     }
+ * }
+ */
+baidu_json* duer_ds_report_ca_status(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_DS_REPORT_CA_H

+ 82 - 0
include/libduer-device/include/lightduer_engine.h

@@ -0,0 +1,82 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_engine.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer IoT CA engine.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_ENGINE_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_ENGINE_H
+
+#include "baidu_json.h"
+#include "lightduer_types.h"
+#include "lightduer_coap_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum duer_engine_events_enum {
+    DUER_EVT_CREATE,
+    DUER_EVT_START,
+    DUER_EVT_ADD_RESOURCES,
+    DUER_EVT_SEND_DATA,
+    DUER_EVT_DATA_AVAILABLE,
+    DUER_EVT_STOP,
+    DUER_EVT_DESTROY,
+    DUER_EVENTS_TOTAL
+};
+
+typedef void (*duer_engine_notify_func)(int event, int status, int what, void *object);
+
+void duer_engine_register_notify(duer_engine_notify_func func);
+
+void duer_engine_create(int what, void *object);
+
+void duer_engine_set_response_callback(int what, void *object);
+
+void duer_engine_start(int what, void *object);
+
+void duer_engine_add_resources(int what, void *object);
+
+void duer_engine_data_available(int what, void *object);
+
+int duer_engine_enqueue_report_data(duer_context_t *context, const baidu_json *data);
+
+void duer_engine_send(int what, void *object);
+
+void duer_engine_stop(int what, void *object);
+
+void duer_engine_destroy(int what, void *object);
+
+int duer_engine_qcache_length(void);
+
+duer_bool duer_engine_is_started(void);
+
+void duer_engine_clear_data(int what, void *object);
+
+const char *duer_engine_get_uuid(void);
+
+const char *duer_engine_get_bind_token(void);
+
+const char *duer_engine_get_rsa_cacrt(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_ENGINE_H*/

+ 39 - 0
include/libduer-device/include/lightduer_event_emitter.h

@@ -0,0 +1,39 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Zhang Leliang (zhangleliang@baidu.com)
+//
+// Description: parser for the args
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_MODULES_CONNAGNET_LIGHTDUER_EVENT_EMITTER_H
+#define BAIDU_DUER_LIBDUER_DEVICE_MODULES_CONNAGNET_LIGHTDUER_EVENT_EMITTER_H
+
+#include "lightduer_events.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void duer_emitter_create(void);
+
+int duer_emitter_emit(duer_events_func func, int what, void *object);
+
+void duer_emitter_destroy(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIBDUER_DEVICE_MODULES_CONNAGNET_LIGHTDUER_EVENT_EMITTER_H*/

+ 55 - 0
include/libduer-device/include/lightduer_events.h

@@ -0,0 +1,55 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_events.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer events looper.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_EVENTS_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_EVENTS_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void (*duer_events_func)(int what, void *object);
+
+typedef struct _duer_event_status_message_s {
+    int                 _status;
+    duer_events_func    _func;
+    int                 _what;
+    void *              _object;
+} duer_evtst_message_t;
+
+typedef void *duer_events_handler;
+
+duer_events_handler duer_events_create(const char *name, size_t stack_size, size_t queue_length);
+
+duer_events_handler duer_events_create_with_priority(const char *name, size_t stack_size,
+                                                     size_t queue_length, int priority);
+
+int duer_events_call(duer_events_handler handler, duer_events_func func, int what, void *object);
+
+void duer_events_destroy(duer_events_handler handler);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_EVENTS_H*/

+ 267 - 0
include/libduer-device/include/lightduer_flash_strings.h

@@ -0,0 +1,267 @@
+// Copyright (2018) Baidu Inc. All rights reserved.
+/**
+ * File: lightduer_flash_strings.h
+ * Auth: Sijun Li(lisijun@baidu.com)
+ * Desc: APIs of store string list to flash.
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_FLASH_STRINGS_H
+#define BAIDU_DUER_LIGHTDUER_FLASH_STRINGS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "lightduer_types.h"
+
+#define FLASH_MAGIC                     0x56BD50C4
+#define FLASH_MAGIC_BITMASK             0xffffffff
+#define FLASH_LEN_BITMASK               0xffffffff
+#define FLASH_INVALID_ADDR              0xffffffff
+
+typedef enum {
+    ALARM_FLASH             = 0,
+    MAX_FLASH_MODULE        = 1,
+}duer_flash_module;
+
+typedef enum {
+    DUER_ERR_FLASH_CORRUPT = -2,
+    DUER_ERR_FLASH_FULL = -3,
+}duer_flash_errcode;
+
+/*
+ * Configuration of flash operations.
+ * xxx_align_bits: Number of last bits of address to be aligned. 
+ *          For example, Bits=5 indicates header's last 5 bits are '0'.
+ */
+typedef struct {
+    // For erase.
+    int sector_align_bits;
+    // For write.
+    int page_align_bits;
+    // For read.
+    int word_align_bits;
+} duer_flash_config_t;
+
+typedef struct {
+    void *handle;
+    unsigned int len;
+    int max_ele_count;
+    unsigned int *ele_list;
+} duer_flash_context_t;
+
+typedef struct {
+    unsigned int magic;         // Magic number to check data valid;
+    unsigned int len;           // Length of payload string;
+} duer_flash_data_header;
+
+typedef char *(*duer_raw2string_func)(void *);
+typedef void (*duer_free_string_func)(char *);
+
+// Need users to implement to adapt different platform.
+/**
+ * DESC:
+ * Developer needs to implement this interface to init flash context.
+ * Must call duer_set_flash_config in this function.
+ *
+ * @PARAM[in] module: selected module, corresponding to context.
+ *
+ * @RETURN: pointer of created context.
+ */
+extern duer_flash_context_t *duer_flash_init(duer_flash_module module);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to read flash.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] addr: the address offset of flash to read.
+ * @PARAM[out] buf: the buffer to store read data.
+ * @PARAM[in] len: length of byte to read.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_flash_read(
+        duer_flash_context_t *ctx,
+        unsigned int addr,
+        void *buf,
+        unsigned int len);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to write flash.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] addr: the address offset of flash to write.
+ * @PARAM[in] buf: the buffer stored writing data.
+ * @PARAM[in] len: length of byte to write.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_flash_write(
+        duer_flash_context_t *ctx,
+        unsigned int addr,
+        void *buf,
+        unsigned int len);
+
+/**
+ * DESC:
+ * Developer needs to implement this interface to erase flash.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] addr: the address offset start of flash to erase.
+ * @PARAM[in] len: length of byte to erase.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_flash_erase(
+        duer_flash_context_t *ctx,
+        unsigned int addr,
+        unsigned int len);
+
+/**
+ * DESC:
+ * Should be called in duer_flash_init, 
+ * This function will only take effect when first time called.
+ * 
+ * PARAM config: configuration of hardware flash.
+ *
+ * @RETURN: none
+ */
+extern void duer_set_flash_config(const duer_flash_config_t *config);
+
+// High level APIs.
+/**
+ * DESC:
+ * Append one flash string element to flash data. if flash data is empty, the element will be 
+ * written to the begining of flash addr (address offset = 0).
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] raw_data: raw data of element needs to store to flash.
+ * @PARAM[in] raw2string_func: function to convert raw_data to string. if this param is NULL, 
+ *              treat raw_data as a string.
+ * @PARAM[in] free_string_func: function to free the string created by raw2string_func. Must
+ *              not NULL when raw2string_func not NULL.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_append_to_flash(
+        duer_flash_context_t *ctx,
+        void *raw_data,
+        duer_raw2string_func raw2string_func,
+        duer_free_string_func free_string_func);
+
+/**
+ * DESC:
+ * Get the first and last flash element address offset, and reset the status in ctx.
+ * MUST be called before duer_update_to_flash.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[out] p_first_ele_addr: the address offset of frist flash element.
+ * @PARAM[out] p_last_ele_addr: the address offset of last flash element.
+ *
+ * @RETURN: none
+ */
+extern void duer_update_to_flash_prepare(
+        duer_flash_context_t *ctx,
+        unsigned int *p_first_ele_addr,
+        unsigned int *p_last_ele_addr);
+
+/**
+ * DESC:
+ * Update to add one flash string element.
+ * MUST be called before duer_update_to_flash.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] raw_data: raw data of element needs to store to flash.
+ * @PARAM[in] raw2string_func: function to convert raw_data to string. if this param is NULL, 
+ *              treat raw_data as a string.
+ * @PARAM[in] free_string_func: function to free the string created by raw2string_func. Must
+ *              not NULL when raw2string_func not NULL.
+ * @PARAM[out] p_last_ele_addr: new address offset of last flash element, after updating.
+ *              this value will be used when updating next element.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_update_to_flash(
+        duer_flash_context_t *ctx,
+        void *raw_data,
+        duer_raw2string_func raw2string_func,
+        duer_free_string_func free_string_func,
+        unsigned int *p_last_ele_addr);
+
+/**
+ * DESC:
+ * Update the flash header, to mark the data added via duer_update_to_flash as new data,
+ * and mark as old data as deleted.
+ * Must be called after all elements updated to flash.
+ *
+ * Note: we keep this function out of duer_update_to_flash to prevent data lossing. For example,
+ *      if hardware reboot when updating flash, the old data will still be stored in flash 
+ *      because the header havn't updated.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] raw_data: raw data of element needs to store to flash.
+ * @PARAM[in] raw2string_func: function to convert raw_data to string. if this param is NULL, 
+ *              treat raw_data as a string.
+ * @PARAM[in] free_string_func: function to free the string created by raw2string_func. Must
+ *              not NULL when raw2string_func not NULL.
+ * @PARAM[out] p_last_ele_addr: new address offset of last flash element, after updating.
+ *              this value will be used when updating next element.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern void duer_update_flash_header(
+        duer_flash_context_t *ctx,
+        unsigned int first_ele_addr);
+
+/**
+ * DESC:
+ * Scan the flash area to retrieve flash element's address, and store in ctx.
+ *
+ * @PARAM[in] ctx: pointer of context.
+ *
+ * @RETURN: none
+ */
+extern void duer_get_all_ele_from_flash(
+        duer_flash_context_t *ctx);
+
+/**
+ * DESC:
+ * Get flash header of element, to retrieve information of element, like string length.
+ * SHOULD be called before duer_parse_flash_ele_to_string.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] flash_index: index of flash element to get.
+ * @PARAM[out] p_header: pointer to store header output.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern int duer_get_flash_header(
+        duer_flash_context_t *ctx,
+        unsigned int flash_index,
+        duer_flash_data_header *p_header);
+
+/**
+ * DESC:
+ * Get the string value of flash element.
+ * 
+ * @PARAM[in] ctx: pointer of context.
+ * @PARAM[in] flash_index: index of flash element to get.
+ * @PARAM[in] header: flash header value.
+ * @PARAM[out] payload_string: buffer to store string output. Must allocate memory according to 
+ *              length value stored in header, before call this function.
+ *
+ * @RETURN: 0 when success, else when fail.
+ */
+extern void duer_parse_flash_ele_to_string(
+        duer_flash_context_t *ctx,
+        unsigned int flash_index,
+        duer_flash_data_header data,
+        char *payload_string);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif//BAIDU_DUER_LIGHTDUER_FLASH_STRINGS_H
+

+ 43 - 0
include/libduer-device/include/lightduer_handler.h

@@ -0,0 +1,43 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Zhang Leliang (zhangleliang@baidu.com)
+//
+// Description: A common events handler for internal call in the task
+
+#ifdef DUER_EVENT_HANDLER
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_FRAMEWORK_UTILS_LIGHTDUER_HANDLER_H
+#define BAIDU_DUER_LIBDUER_DEVICE_FRAMEWORK_UTILS_LIGHTDUER_HANDLER_H
+
+#include "lightduer_events.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void duer_handler_create(void);
+
+int duer_handler_handle(duer_events_func func, int what, void *object);
+
+void duer_handler_destroy(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIBDUER_DEVICE_FRAMEWORK_UTILS_LIGHTDUER_EVENT_HANDLER_H*/
+
+#endif // #ifdef DUER_EVENT_HANDLER

+ 45 - 0
include/libduer-device/include/lightduer_hashcode.h

@@ -0,0 +1,45 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The hashcode algorithm
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_HASHCODE_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_HASHCODE_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Generate the hashcode by data
+ *
+ * @Param data, in, the data will be computed
+ * @Param size, in, the data size
+ * @Param seed, in, the hash seed
+ * @Return duer_u32_t, the hashcode value
+ */
+DUER_INT duer_u32_t duer_hashcode(const char* data,
+                               duer_size_t size,
+                               duer_u32_t seed);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_HASHCODE_H

+ 298 - 0
include/libduer-device/include/lightduer_http_client.h

@@ -0,0 +1,298 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_http_client.h
+ * Author: Pan Haijun, Gang Chen(chengang12@baidu.com)
+ * Desc: HTTP Client Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H
+#define BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "lightduer_timers.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define DUER_HTTP_CONTENT_TYPE_LEN_MAX  (32)  // the max length of "content type" section
+
+//http client results
+typedef enum {
+    DUER_HTTP_ERR_FAILED = -1,
+    DUER_HTTP_OK,                // Success
+    DUER_HTTP_PROCESSING,        // Processing
+    DUER_HTTP_ERR_PARSE,         // url Parse error
+    DUER_HTTP_ERR_DNS,           // Could not resolve name
+    DUER_HTTP_ERR_PRTCL,         // Protocol error
+    DUER_HTTP_ERR_NOT_FOUND,     // HTTP 404 Error
+    DUER_HTTP_ERR_REFUSED,       // HTTP 403 Error
+    DUER_HTTP_ERR_TIMEOUT,       // Connection timeout
+    DUER_HTTP_ERR_CONNECT,       // Connection error
+    DUER_HTTP_CLOSED,            // Connection was closed by remote host
+    DUER_HTTP_ERR_NOT_SUPPORT,   // not supported feature
+    DUER_HTTP_REDIRECTTION,      //take a redirection when http header contains 'Location'
+} duer_http_result_t;
+
+typedef enum {
+    DUER_HTTP_GET,     // Just 'GET' is supported, other methods are reserved
+    DUER_HTTP_POST,    // reserved
+    DUER_HTTP_PUT,     // reserved
+    DUER_HTTP_DELETE,  // reserved
+    DUER_HTTP_HEAD     // reserved
+} duer_http_method_t;
+
+typedef struct {
+    int  n_handle;
+    int  (*init)(void *socket_args);
+    int  (*open)(int socket_handle);
+    int  (*connect)(int socket_handle, const char *host, const int port);
+    void (*set_blocking)(int socket_handle, int blocking);
+    void (*set_timeout)(int socket_handle, int timeout);
+    int  (*recv)(int socket_handle, void *data, unsigned size);
+    int  (*send)(int socket_handle, const void *data, unsigned size);
+    int  (*close)(int socket_handle);
+    void (*destroy)(int socket_handle);
+} duer_http_socket_ops_t;
+
+//to tell data output callback user that if the current data block is first block or last block
+typedef enum {
+    DUER_HTTP_DATA_FIRST  = 0x1,
+    DUER_HTTP_DATA_MID    = 0x2,
+    DUER_HTTP_DATA_LAST   = 0x4
+} duer_http_data_pos_t;
+
+/**
+ *
+ * DESC:
+ * the callback to handler the data download by http
+ *
+ * PARAM:
+ * @param[in] p_user_ctx: usr ctx registed by user
+ * @param[in] pos:   to identify if it is data stream's start, or middle , or end of data stream
+ * @param[in] buf:   buffer stored media data
+ * @param[in] len:   data length in 'buf'
+ * @param[in] type:  data type to identify media or others
+ *
+ * @RETURN: negative number when failed
+ */
+typedef int (*duer_http_data_handler)(void *p_user_ctx, duer_http_data_pos_t pos,
+                                      const char *buf, size_t len, const char *type);
+/**
+ * DESC:
+ * callback to check whether need to stop getting data
+ *
+ * PARAM: none
+ *
+ * @RETURN 1: to stop; 0: no stop
+ */
+typedef int (*duer_http_stop_notify_cb_t)();
+
+/**
+ * DESC:
+ * sometimes other module need to know which url is used to download data.
+ * this callback is used to get the url
+ *
+ * PARAM:
+ * @param[in] url: the url used by http to download data
+ *
+ * RETURN: none
+ */
+typedef void (*duer_http_get_url_cb_t)(const char *url);
+
+typedef struct {
+    const char               **pps_custom_headers;
+    size_t                     sz_headers_count;
+    int                        n_http_response_code; // http response code
+    duer_http_socket_ops_t     ops;                  // socket operations
+    int                        scheme;               // http or https type,1 is https, 0 is http
+    duer_http_data_handler     data_hdlr_cb;         // callback for output data
+    void                      *p_data_hdlr_ctx;      // users args for data_hdlr_cb
+    int                        n_http_content_len;   // http content length
+    // http content type
+    char                       p_http_content_type[DUER_HTTP_CONTENT_TYPE_LEN_MAX];
+    char                      *p_location;           // http header "Location"
+    // a callback to check that stopping http client getting data or not
+    duer_http_stop_notify_cb_t check_stop_notify_cb;
+    // to get the url used to download, the last url will be returned if 302 location happen
+    duer_http_get_url_cb_t     get_url_cb;
+    int                        n_is_chunked;
+    size_t                     sz_chunk_size;
+    char                      *p_chunk_buf;
+    size_t                     recv_size;            // size of the data have been dwonload
+    // the count of http continuously try to resume from break-point
+    int                        resume_retry_count;
+    char                      *buf;                  // buf used to receive data
+    duer_http_data_pos_t       data_pos;             // play position
+    char                      *last_host;            // the lastest connected host
+    unsigned short             last_port;            // the lastest connected port
+    duer_timer_handler         connect_keep_timer;   // used to close persistent connect
+    size_t                     upload_size;          // size of the data have been upload
+} duer_http_client_t;
+
+/**
+ * DESC:
+ * init the http client
+ *
+ * PARAM:
+ * @param[in] p_client: pointer of the http client
+ *
+ * RETURN: DUER_HTTP_OK if success, DUER_HTTP_ERR_FAILED if failed
+ */
+duer_http_result_t duer_http_init(duer_http_client_t *p_client);
+
+/**
+ * DESC:
+ * destroy the http client
+ *
+ * PARAM:
+ * @param[in] p_client: pointer of the http client
+ *
+ * RETURN: none
+ */
+void duer_http_destroy(duer_http_client_t *p_client);
+
+/**
+ * DESC:
+ * to init http client socket operations
+ *
+ * PARAM:
+ * @param[in] p_client: pointer of the http client
+ * @param[in] p_ops: socket operations set
+ * @param[in] socket_args: args for ops "get_inst"
+ *
+ * RETURN: DUER_HTTP_OK if success, DUER_HTTP_ERR_FAILED if failed
+ */
+duer_http_result_t duer_http_init_socket_ops(duer_http_client_t *p_client,
+        duer_http_socket_ops_t *p_ops,
+        void *socket_args);
+
+/**
+ * DESC:
+ * register data output handler callback to handle data block
+ *
+ * @param[in] p_client: pointer of the http client
+ * @param[in] data_hdlr_cb: data output handler callback to be registered
+ * @param[in] p_usr_ctx: user context for data output handler
+ *
+ * @RETURN    none
+ */
+void duer_http_reg_data_hdlr(duer_http_client_t *p_client,
+                             duer_http_data_handler data_hdlr_cb,
+                             void *p_usr_ctx);
+
+/**
+ *
+ * DESC:
+ * register callback to get the url which is used by http to get data
+ *
+ * PARAM:
+ * @param[in] p_client: pointer point to the duer_http_client_t
+ * @param[in] cb:       the callback to be registered
+ *
+ * @RETURN    none
+ */
+void duer_http_reg_url_get_cb(duer_http_client_t *p_client, duer_http_get_url_cb_t cb);
+
+/**
+ * DESC:
+ * Set custom headers for request.
+ * Pass NULL, 0 to turn off custom headers.
+ *
+ * PARAM:
+ * @param[in] p_client: pointer point to the duer_http_client_t
+ * @param[in] headers: an array (size multiple of two) key-value pairs,
+ *                     must remain valid during the whole HTTP session
+ *                     const char * hdrs[] =
+ *                     {
+ *                         "Connection", "keep-alive",
+ *                         "Accept", "text/html",
+ *                         "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)",
+ *                         "Accept-Encoding", "gzip,deflate,sdch",
+ *                         "Accept-Language", "en-US,en;q=0.8",
+ *                     };
+ *
+ *                     duer_http_set_cust_headers(hdrs, 5);
+ * @param[in] pairs:   number of key-value pairs
+ *
+ * @RETURN    none
+ */
+void duer_http_set_cust_headers(duer_http_client_t *p_client, const char **headers, size_t pairs);
+
+/**
+ * DESC:
+ * get data by http client
+ *
+ * PARAM:
+ * @param[in] p_client:          pointer point to the duer_http_client_t
+ * @param[in] url:               url resource
+ * @param[in] pos:               the position to receive the http data,
+ *                               sometimes user only want to get part of the data
+ * @param[in] connect_keep_time: how long time the connection should be kept after download finish
+ *
+ * RETURN     DUER_HTTP_OK if success, other duer_http_result_t type code if failed
+ */
+duer_http_result_t duer_http_get(duer_http_client_t *p_client,
+                                 const char *url,
+                                 const size_t pos,
+                                 const int connect_keep_time);
+
+/**
+ * DESC:
+ * get http response code
+ *
+ * PARAM:
+ * @param[in] p_client: pointer point to the duer_http_client_t
+ *
+ * @RETURN    none
+ */
+int duer_http_get_rsp_code(duer_http_client_t *p_client);
+
+/**
+ * DESC:
+ * register callback to check stop flag
+ *
+ * @param[in] p_client:   pointer of the http client
+ * @param[in] chk_stp_cb: to notify httpclient to stop
+ *
+ * @RETURN    none
+ */
+void duer_http_reg_stop_notify_cb(duer_http_client_t *p_client,
+                                  duer_http_stop_notify_cb_t chk_stp_cb);
+
+/**
+ * DESC:
+ * get http download progress
+ *
+ * PARAM:
+ * @param[in]  p_client:   pointer point to the duer_http_client_t
+ * @param[out] total_size: pointer point to a varirable to receive the total size to be download,
+ *                         0 will be returned if it's chunk transfer
+ * @param[out] recv_size:  pointer point to a varirable to receive the data size have been download
+ *
+ * RETURN      none
+ */
+void duer_http_get_download_progress(duer_http_client_t *p_client,
+                                     size_t *total_size,
+                                     size_t *recv_size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_HTTP_CLIENT_H

+ 33 - 0
include/libduer-device/include/lightduer_http_client_ops.h

@@ -0,0 +1,33 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_http_client_ops.h
+ * Author: Zhong Shuai(zhongshuai@baidu.com)
+ * Desc: HTTP Client OPS Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_CLIENT_OPS_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_CLIENT_OPS_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include "lightduer_http_client.h"
+
+extern duer_http_client_t *duer_create_http_client(void);
+#ifdef __cplusplus
+}
+#endif
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_CLIENT_OPS_H

+ 45 - 0
include/libduer-device/include/lightduer_http_dns_client_ops.h

@@ -0,0 +1,45 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_http_client_ops.h
+ * Auth: CHEN YUN (chenyun08@baidu.com)
+ * Desc: HTTP DNS Client Socket Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_DNS_CLIENT_OPS_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_DNS_CLIENT_OPS_H
+#include "lightduer_http_client.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+typedef enum
+{
+    HTTP_DNS_QUERY_UNKNOWN = -1,
+    HTTP_DNS_QUERY_START,
+    HTTP_DNS_QUERYING,
+    HTTP_DNS_QUERY_SUCC,
+    HTTP_DNS_QUERY_FAIL,
+}duer_http_dns_query_state_t;
+
+void duer_http_dns_task_init();
+void duer_http_dns_task_destroy();
+char *duer_http_dns_query(char *host, int timeout);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_HTTP_DNS_CLIENT_OPS_H

+ 93 - 0
include/libduer-device/include/lightduer_interactive_class.h

@@ -0,0 +1,93 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_interactive_class.h
+ * Auth: Chen Xihao (chenxihao@baidu.com)
+ * Desc: Interactive class APIs.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_INTERACTIVE_CLASS_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_INTERACTIVE_CLASS_H
+
+#include <stdbool.h>
+#include "baidu_json.h"
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct duer_interactive_class_handler_s {
+    duer_status_t (*handle_control_light)(bool on);
+    duer_status_t (*handle_notice)(baidu_json *payload);
+} duer_interactive_class_handler_t;
+
+/**
+ * DESC:
+ * Init interactive class.
+ *
+ * PARAM: none
+ *
+ * @RETURN: none.
+ */
+void duer_interactive_class_init(void);
+
+/**
+ * DESC:
+ * Open interactive class.
+ *
+ * PARAM: none
+ *
+ * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+duer_status_t duer_interactive_class_open(void);
+
+/**
+ * DESC:
+ * Close interactive class.
+ *
+ * PARAM: none
+ *
+ * @RETURN: success return DUER_OK, failed return DUER_ERR_FAILED.
+ */
+duer_status_t duer_interactive_class_close(void);
+
+/**
+ * DESC:
+ * Whether interactive class is living.
+ *
+ * PARAM: none
+ *
+ * @RETURN: living return true, not return false.
+ */
+bool duer_interactive_class_is_living(void);
+
+/**
+ * DESC:
+ * Set the handler to handle callbacks, such as control light, handle notice
+ *
+ * PARAM: handler, the point of handler defined by user
+ *
+ * @RETURN: none.
+ */
+void duer_interactive_class_set_handler(duer_interactive_class_handler_t *handler);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_INTERACTIVE_CLASS_H */
+

+ 64 - 0
include/libduer-device/include/lightduer_key.h

@@ -0,0 +1,64 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_key.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: Key Event Handle Interface
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_KEY_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_KEY_H
+
+enum KeyType {
+    START_KEY    = 1,
+    STOP_KEY     = 2,
+    END_KEY      = 3,
+    NEXT_KEY     = 4,
+    PREVIOUS_KEY = 5,
+    REPEAT_KEY   = 6,
+};
+
+/*
+ * Init LightDuer key handler framework
+ *
+ * @param void: None
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  DUER_ERR_FAILED.
+ */
+extern int duer_init_key_handler(void);
+
+/*
+ * Report key event to event framework
+ * Call this API in IRQ context as the last func
+ *
+ * @param void: enum KeyType key
+ *               START_KEY
+ *               STOP_KEY
+ *               END_KEY
+ *               NEXT_KEY
+ *               PREVIOUS_KEY
+ *               REPEAT_KEY
+ *
+ *               int value
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  DUER_ERR_FAILED.
+ *
+ */
+extern int duer_report_key_event(enum KeyType key, int value);
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_KEY_H

+ 43 - 0
include/libduer-device/include/lightduer_lib.h

@@ -0,0 +1,43 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The internal common header.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_INTERNAL_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_INTERNAL_H
+
+#include <string.h>
+
+#define DUER_MEMCPY(...)         memcpy(__VA_ARGS__)
+#define DUER_MEMCMP(...)         memcmp(__VA_ARGS__)
+#define DUER_MEMSET(...)         memset(__VA_ARGS__)
+#define DUER_MEMMOVE(...)        memmove(__VA_ARGS__)
+#define DUER_STRLEN(...)         strlen(__VA_ARGS__)
+#define DUER_STRCMP(...)         strcmp(__VA_ARGS__)
+#define DUER_STRNCPY(...)        strncpy(__VA_ARGS__)
+#define DUER_STRNCMP(...)        strncmp(__VA_ARGS__)
+#define DUER_SNPRINTF(...)       snprintf(__VA_ARGS__)
+#define DUER_STRNCASECMP(...)    strncasecmp(__VA_ARGS__)
+#define DUER_SSCANF(...)         sscanf(__VA_ARGS__)
+#define DUER_STRSTR(...)         strstr(__VA_ARGS__)
+#define DUER_STRCHR(...)         strchr(__VA_ARGS__)
+#define DUER_STRNCAT(...)        strncat(__VA_ARGS__)
+
+// Suppress Compiler warning Function&Variable declared never referenced
+#define ALLOW_UNUSED_LOCAL(VAR_FUNC) (void)(VAR_FUNC)
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_INTERNAL_H

+ 83 - 0
include/libduer-device/include/lightduer_log.h

@@ -0,0 +1,83 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: duer_log.h
+ * Auth: Su Hao(suhao@baidu.com)
+ * Desc: Print log.
+ */
+
+#ifndef LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_LOG_H
+#define LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_LOG_H
+
+#include "lightduer_debug.h"
+
+#define DUER_STRING_OUTPUT(_x)  (((_x) == NULL) ? ("NULL") : (_x))
+
+#ifndef DUER_DEBUG_LEVEL
+#define DUER_DEBUG_LEVEL 3
+#endif
+
+#ifdef DUER_DEBUG_LEVEL
+#define DUER_DEBUG_EXT(_l, _f, _li, ...) duer_debug(_l, _f, _li, __VA_ARGS__)
+#else
+#define DUER_DEBUG_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 5)
+#define DUER_LOGV_EXT(_f, _l, ...)   DUER_DEBUG_EXT(5, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGV_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 4)
+#define DUER_LOGD_EXT(_f, _l, ...)   DUER_DEBUG_EXT(4, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGD_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 3)
+#define DUER_LOGI_EXT(_f, _l, ...)   DUER_DEBUG_EXT(3, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGI_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 2)
+#define DUER_LOGW_EXT(_f, _l, ...)   DUER_DEBUG_EXT(2, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGW_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 1)
+#define DUER_LOGE_EXT(_f, _l, ...)   DUER_DEBUG_EXT(1, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGE_EXT(...)
+#endif
+
+#if defined(DUER_DEBUG_LEVEL) && (DUER_DEBUG_LEVEL >= 0)
+#define DUER_LOGWTF_EXT(_f, _l, ...)   DUER_DEBUG_EXT(0, _f, _l, __VA_ARGS__)
+#else
+#define DUER_LOGWTF_EXT(...)
+#endif
+
+#define DUER_DEBUG(_l, ...)      DUER_DEBUG_EXT(_l, __FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGV(...)           DUER_LOGV_EXT(__FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGD(...)           DUER_LOGD_EXT(__FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGI(...)           DUER_LOGI_EXT(__FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGW(...)           DUER_LOGW_EXT(__FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGE(...)           DUER_LOGE_EXT(__FILE__, __LINE__, __VA_ARGS__)
+#define DUER_LOGWTF(...)         DUER_LOGWTF_EXT(__FILE__, __LINE__, __VA_ARGS__)
+
+#endif/*LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_LOG_H*/

+ 253 - 0
include/libduer-device/include/lightduer_memory.h

@@ -0,0 +1,253 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The APIs for memory management.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MEMORY_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MEMORY_H
+
+#include "lightduer_types.h"
+#include "lightduer_lib.h"
+
+#ifdef DUER_HEAP_MONITOR
+#ifndef DUER_MEMORY_USAGE
+#define DUER_MEMORY_USAGE // DUER_HEAP_MONITOR rely on this
+#endif
+#ifndef DUER_HM_MODULE_NAME
+#error "DUER_HM_MODULE_NAME should be defined if the heapmonitor function needed"
+// #else
+// #define STR_HELPER(x) #x
+// #define STR(x) STR_HELPER(x)
+// #error #DUER_HM_MODULE_NAME
+// #warning "error:"##DUER_HM_MODULE_NAME
+#endif
+
+#undef HM_KEYWORD
+#define HM_KEYWORD(symbol) symbol,
+
+typedef enum _duer_memory_hm_module_e {
+    DUER_MEMORY_HM_MIN = 0,
+#include "lightduer_memory_hm_keywords.h"
+    DUER_MEMORY_HM_MAX,
+} duer_memory_hm_module_e;
+
+#endif // DUER_HEAP_MONITOR
+
+#ifdef DUER_MEMORY_USAGE
+#ifndef DUER_HEAP_MONITOR
+DUER_INT void duer_memdbg_usage();
+#define DUER_MEMDBG_USAGE(...)       duer_memdbg_usage()
+#else
+#ifdef DUER_HEAP_MONITOR_DEBUG
+DUER_INT void duer_memdbg_usage();
+#define DUER_MEMDBG_USAGE(...)       duer_memdbg_usage()
+#else // DUER_HEAP_MONITOR_DEBUG
+#define DUER_MEMDBG_USAGE(...)
+#endif // DUER_HEAP_MONITOR_DEBUG
+#endif
+#else
+#define DUER_MEMDBG_USAGE(...)
+#endif
+
+#if defined(DUER_HEAP_MONITOR)
+#define DUER_MALLOC(_s)          duer_malloc_hm(_s, DUER_HM_MODULE_NAME)
+#define DUER_CALLOC(_s, _n)      duer_calloc_hm((_s) * (_n), DUER_HM_MODULE_NAME)
+#define DUER_REALLOC(_p, _s)     duer_realloc_hm(_p, _s, DUER_HM_MODULE_NAME)
+#define DUER_FREE(_p)            duer_free_hm(_p)
+#elif DUER_MEMORY_DEBUG
+#define DUER_MALLOC(_s)          duer_malloc_ext(_s, __FILE__, __LINE__)
+#define DUER_CALLOC(_s, _n)      duer_calloc_ext((_s) * (_n), __FILE__, __LINE__)
+#define DUER_REALLOC(_p, _s)     duer_realloc_ext(_p, _s, __FILE__, __LINE__)
+#define DUER_FREE(_p)            duer_free_ext(_p, __FILE__, __LINE__)
+#else/*DUER_MEMORY_DEBUG*/
+#define DUER_MALLOC(_s)          duer_malloc(_s)
+#define DUER_CALLOC(_s, _n)      duer_calloc((_s) * (_n))
+#define DUER_REALLOC(_p, _s)     duer_realloc(_p, _s)
+#define DUER_FREE(_p)            duer_free(_p)
+#endif/*DUER_HEAP_MONITOR*/
+
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Adapt the memory management functions.
+ */
+typedef void* (*duer_malloc_f)(duer_context context, duer_size_t size);
+typedef void* (*duer_realloc_f)(duer_context context, void*, duer_size_t size);
+typedef void (*duer_free_f)(duer_context context, void* p);
+
+/*
+ * Set the memory management functions.
+ *
+ * @Param ctx, duer_context, in, the memory context for user
+ * @Param f_malloc, duer_malloc_f, the memory alloc function for user
+ * @Param f_realloc, duer_realloc_f, the memory realloc function for user,
+ * @Param f_free, duer_free_f, the memory free function for user
+ *
+ * If f_malloc or f_free set to NULL, we will use default libc instead all of it;
+ * If only f_realloc set to NULL, we will realized it by f_malloc and f_free.
+ */
+DUER_EXT void baidu_ca_memory_init(duer_context context,
+                                  duer_malloc_f f_malloc,
+                                  duer_realloc_f f_realloc,
+                                  duer_free_f f_free);
+
+/*
+ * uninit the memory operations in baidu_ca_memory_init.
+ */
+DUER_EXT void baidu_ca_memory_uninit(void);
+
+/*
+ * malloc function for bjson
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_malloc_bjson(duer_size_t size);
+
+/*
+ * free function bjson
+ *
+ * @Param ptr, void *, the alloced memory
+ */
+DUER_INT void duer_free_bjson(void* ptr);
+
+#if defined(DUER_HEAP_MONITOR)
+
+DUER_INT void baidu_ca_memory_hm_init(void);
+
+DUER_INT void baidu_ca_memory_hm_destroy(void);
+
+/*
+ * malloc function for heap monitor
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param module, duer_memory_hm_module_e, the tag for where the memroy used
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_malloc_hm(duer_size_t size, duer_memory_hm_module_e module);
+
+/*
+ * cmalloc function zero-initialize the memory before return
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param module, duer_memory_hm_module_e, the tag for where the memroy used
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_calloc_hm(duer_size_t size, duer_memory_hm_module_e module);
+
+/*
+ * realloc function
+ *
+ * @Param ptr, void *, the old alloced memory
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param module, duer_memory_hm_module_e, the tag for where the memroy used
+ * @Return the new alloced memory pointer
+ */
+DUER_INT void* duer_realloc_hm(void* ptr, duer_size_t size, duer_memory_hm_module_e module);
+
+/*
+ * free function
+ *
+ * @Param ptr, void *, the alloced memory
+ */
+DUER_INT void duer_free_hm(void* ptr);
+
+#else
+/*
+ * malloc function
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param file, const char *, the file name when alloc the memory
+ * @Param line, duer_u32_t, the line number of the file when alloc the memory
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_malloc_ext(duer_size_t size, const char* file, duer_u32_t line);
+
+/*
+ * malloc function, zero-initialize before return
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param file, const char *, the file name when alloc the memory
+ * @Param line, duer_u32_t, the line number of the file when alloc the memory
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_calloc_ext(duer_size_t size, const char* file, duer_u32_t line);
+
+/*
+ * realloc function
+ *
+ * @Param ptr, void *, the old alloced memory
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Param file, const char *, the file name when alloc the memory
+ * @Param line, duer_u32_t, the line number of the file when alloc the memory
+ * @Return the new alloced memory pointer
+ */
+DUER_INT void* duer_realloc_ext(void* ptr, duer_size_t size, const char* file,
+                              duer_u32_t line);
+
+/*
+ * free function
+ *
+ * @Param ptr, void *, the alloced memory
+ * @Param file, const char *, the file name when alloc the memory
+ * @Param line, duer_u32_t, the line number of the file when alloc the memory
+ */
+DUER_INT void duer_free_ext(void* ptr, const char* file, duer_u32_t line);
+
+/*
+ * malloc function
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_malloc(duer_size_t size);
+
+/*
+ * cmalloc function zero-initialize the memory before return
+ *
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Return the alloced memory pointer
+ */
+DUER_INT void* duer_calloc(duer_size_t size);
+
+/*
+ * realloc function
+ *
+ * @Param ptr, void *, the old alloced memory
+ * @Param size, duer_size_t, the expected size of the memory
+ * @Return the new alloced memory pointer
+ */
+DUER_INT void* duer_realloc(void* ptr, duer_size_t size);
+
+/*
+ * free function
+ *
+ * @Param ptr, void *, the alloced memory
+ */
+DUER_INT void duer_free(void* ptr);
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MEMORY_H

+ 56 - 0
include/libduer-device/include/lightduer_memory_hm_keywords.h

@@ -0,0 +1,56 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: zhangleliang(zhangleliang@baidu.com)
+//
+// Description: definition about the heap monitor module
+#ifdef DUER_HEAP_MONITOR
+#ifdef HM_KEYWORD
+    HM_KEYWORD(DUER_HM_DUER_DEVICE)
+    HM_KEYWORD(DUER_HM_PLATFORM)
+    HM_KEYWORD(DUER_HM_PORT_LPT230)
+    HM_KEYWORD(DUER_HM_PORT_LPB100)
+    HM_KEYWORD(DUER_HM_PORT_LINUX)
+    HM_KEYWORD(DUER_HM_PORT_FREERTOS)
+    HM_KEYWORD(DUER_HM_FLASH_STRINGS)
+    HM_KEYWORD(DUER_HM_CONNAGENT)
+    HM_KEYWORD(DUER_HM_DCS)
+    HM_KEYWORD(DUER_HM_DEVICE_STATUS)
+    HM_KEYWORD(DUER_HM_DEVICE_INFO)
+    HM_KEYWORD(DUER_HM_HTTP)
+    HM_KEYWORD(DUER_HM_NTP)
+    HM_KEYWORD(DUER_HM_BIND_DEVICE)
+    HM_KEYWORD(DUER_HM_VOICE_ENGINE)
+    HM_KEYWORD(DUER_HM_COAP)
+    HM_KEYWORD(DUER_HM_SYSTEM_INFO)
+    HM_KEYWORD(DUER_HM_INTERACTIVE_CLASS)
+    HM_KEYWORD(DUER_HM_OTA)
+    HM_KEYWORD(DUER_HM_DCS3_LINUX_DEMO)
+    HM_KEYWORD(DUER_HM_TRACKER)
+    HM_KEYWORD(DUER_HM_LINUX_DEMO)
+    HM_KEYWORD(DUER_HM_SPEEX_ENCODER)
+    HM_KEYWORD(DUER_HM_FRAMEWORK)
+    HM_KEYWORD(DUER_HM_MBEDTLS)
+    HM_KEYWORD(DUER_HM_NSDL)
+    HM_KEYWORD(DUER_HM_CJSON)
+    HM_KEYWORD(DUER_HM_DEVICE_VAD)
+    HM_KEYWORD(DUER_HM_SPEEX)
+    HM_KEYWORD(DUER_HM_ZLIBLITE)
+    HM_KEYWORD(DUER_HM_MBED)
+#else
+#error "define HM_KEYWORD is needed if use heap monitor function"
+#endif
+#endif
+

+ 85 - 0
include/libduer-device/include/lightduer_mutex.h

@@ -0,0 +1,85 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Wrapper for mutex
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MUTEX_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MUTEX_H
+
+#include "lightduer_types.h"
+//#include "lightduer_ca.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void* duer_mutex_t;
+
+/*
+ * Create mutex context
+ *
+ * @Return duer_mutex_t, the created mutex context
+ */
+DUER_INT duer_mutex_t duer_mutex_create(void);
+
+/*
+ * Mutex lock
+ *
+ * @Return duer_mutex_t, the mutex context
+ */
+DUER_INT duer_status_t duer_mutex_lock(duer_mutex_t mutex);
+
+/*
+ * Mutex unlock
+ *
+ * @Return duer_mutex_t, the mutex context
+ */
+DUER_INT duer_status_t duer_mutex_unlock(duer_mutex_t mutex);
+
+/*
+ * Destroy the mutex context
+ *
+ * @Return duer_mutex_t, the mutex context
+ */
+DUER_INT duer_status_t duer_mutex_destroy(duer_mutex_t mutex);
+
+/*
+ * The mutex callbacks
+ */
+typedef duer_mutex_t (*duer_mutex_create_f)();
+typedef duer_status_t (*duer_mutex_lock_f)(duer_mutex_t mtx);
+typedef duer_status_t (*duer_mutex_unlock_f)(duer_mutex_t mtx);
+typedef duer_status_t (*duer_mutex_destroy_f)(duer_mutex_t mtx);
+
+/*
+ * Initial the mutex callbacks for Baidu CA
+ *
+ * @Param f_create, in, the function create mutex context
+ * @Param f_lock, in, the function mutex lock
+ * @Param f_unlock, in, the function mutex unlock
+ * @Param f_destroy, in, the function destroy mutex context
+ */
+DUER_EXT void baidu_ca_mutex_init(duer_mutex_create_f f_create,
+                                 duer_mutex_lock_f f_lock,
+                                 duer_mutex_unlock_f f_unlock,
+                                 duer_mutex_destroy_f f_destroy);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_MUTEX_H

+ 69 - 0
include/libduer-device/include/lightduer_net_ntp.h

@@ -0,0 +1,69 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Chang Li (changli@baidu.com)
+//
+// Description: NTP client API decleration.
+
+#include <stdint.h>
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_IOT_NTP_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_IOT_NTP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct _DuerTime {
+    uint32_t sec;
+    uint32_t usec;
+} DuerTime;
+
+#define NTP_PKG_LEN 48
+#define NTP_MAC_LEN 20  // contains 4 Bytes:Key ID and 16 Bytes:Message Authentication Code
+
+#define MD5_KEYID_LEN  4
+#define MD5_DIGEST_LEN 16
+#define MD5_KEY_LEN    16
+
+typedef struct _md5_key {
+    uint32_t key_id;
+    char key_str[MD5_KEY_LEN];
+} Md5Key;
+
+/**
+ * @brief get NTP time
+ * 
+ * @param host: NTP server name. If host is NULL, use "s2c.time.edu.cn" by
+ * default.
+ *
+ * @param timeout: timeout to acquire the NTP time, unit is micro seconds. If
+ * it's 0, use 2000 ms by default. 
+ *
+ * @param result_time: the acquired NTP time.
+ *
+ * @param key: Using keyed MD5 for Authentication for some specific NTP servers.
+ * If unnecessary, specify it NULL.
+ *
+ * @return 0 is okay, netgative value is failed. 
+ */
+int duer_ntp_client(char* host, int timeout, DuerTime* result_time, Md5Key* key);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_IOT_NTP_H
+

+ 97 - 0
include/libduer-device/include/lightduer_net_trans_aes_cbc_encrypted.h

@@ -0,0 +1,97 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: LeLiang Zhang (zhangleliang@baidu.com)
+//
+// Description: Define the aes-cbc encrypted networks I/O.
+//              more info: http://agroup.baidu.com/duersh/md/article/475516
+
+#ifndef BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H
+#define BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H
+
+#include "lightduer_net_transport.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Connect to the host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_connect(duer_trans_ptr trans,
+                                                    const duer_addr_t* addr);
+
+/*
+ * Send data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, in, the data will be sent
+ * @Param size, in, the data size
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_send(duer_trans_ptr trans,
+                                                 const void* data,
+                                                 duer_size_t size,
+                                                 const duer_addr_t* addr);
+
+/*
+ * Set the timeout for receiving data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param timeout, in, the recv data timeout interval
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_set_read_timeout(duer_trans_ptr trans,
+                                                             duer_u32_t timeout);
+
+/*
+ * Receive data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, out, the data will be read
+ * @Param size, in, the data size
+ * @Param addr, out, the target address infomations
+ * @Return duer_status_t, the operation result
+ *  Note, the return size maybe different with the @size, because the BDCAEC used
+ */
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_recv(duer_trans_ptr trans,
+                                                 void* data,
+                                                 duer_size_t size,
+                                                 duer_addr_t* addr);
+
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_recv_timeout(duer_trans_ptr trans,
+                                                 void* data,
+                                                 duer_size_t size,
+                                                 duer_u32_t timeout,
+                                                 duer_addr_t* addr);
+
+/*
+ * Disconnect from the remote host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_aes_cbc_encrypted_close(duer_trans_ptr trans);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_LIGHTDUER_NET_TRANS_AES_CBC_ENCRYPTED_H

+ 95 - 0
include/libduer-device/include/lightduer_net_trans_encrypted.h

@@ -0,0 +1,95 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Define the encrypted networks I/O.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H
+
+#include "lightduer_net_transport.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Connect to the host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_encrypted_connect(duer_trans_ptr trans,
+                                                    const duer_addr_t* addr);
+
+/*
+ * Send data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, in, the data will be sent
+ * @Param size, in, the data size
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_encrypted_send(duer_trans_ptr trans,
+                                                 const void* data,
+                                                 duer_size_t size,
+                                                 const duer_addr_t* addr);
+
+/*
+ * Set the timeout for receiving data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param timeout, in, the recv data timeout interval
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_encrypted_set_read_timeout(duer_trans_ptr trans,
+                                                             duer_u32_t timeout);
+
+/*
+ * Receive data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, out, the data will be read
+ * @Param size, in, the data size
+ * @Param addr, out, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_encrypted_recv(duer_trans_ptr trans,
+                                                 void* data,
+                                                 duer_size_t size,
+                                                 duer_addr_t* addr);
+
+
+DUER_INT duer_status_t duer_trans_encrypted_recv_timeout(duer_trans_ptr trans,
+                                                 void* data,
+                                                 duer_size_t size,
+                                                 duer_u32_t timeout,
+                                                 duer_addr_t* addr);
+/*
+ * Disconnect from the remote host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_encrypted_close(duer_trans_ptr trans);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_ENCRYPTED_H

+ 188 - 0
include/libduer-device/include/lightduer_net_transport.h

@@ -0,0 +1,188 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The wrapper for user's transport.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H
+
+#include "lightduer_network_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define DUER_READ_FOREVER        ((duer_u32_t)-1)
+
+typedef void*          duer_socket_t;
+
+/*
+ * The handler operate transfer.
+ */
+typedef void* duer_trans_handler;
+
+#define DUER_SIZEOF_TCPHEADER    8        // more info _baidu_ca_coap_tcp_header_s lightduer_coap.c
+#ifdef NET_TRANS_ENCRYPTED_BY_AES_CBC
+#define DUER_MIN_BODY_SIZE       (16 + 1) // see http://agroup.baidu.com/duersh/md/article/475516
+#else
+#define DUER_MIN_BODY_SIZE       (0)
+#endif
+#define DUER_MAX_BODY_SIZE       2048     // body longest value
+
+typedef struct _duer_trans_s {
+    duer_socket_t       ctx;
+    duer_context        secure;
+    duer_transevt_func  transevt_callback;
+    void*               cert;
+    duer_size_t         cert_len;
+    duer_u32_t          read_timeout;
+    duer_addr_t         addr;
+    const void          *key_info;
+    duer_bool           is_encrypted;
+#ifdef NET_TRANS_ENCRYPTED_BY_AES_CBC
+    unsigned char       received_header[DUER_SIZEOF_TCPHEADER]; // header info
+    duer_size_t         received_header_bytes; // received header bytes[0-DUER_SIZEOF_TCPHEADER]
+    unsigned char       received_body[DUER_MAX_BODY_SIZE]; // body [0-DUER_MAX_BODY_SIZE]
+    duer_size_t         received_body_bytes; // received body bytes
+#endif
+} duer_trans_t, *duer_trans_ptr;
+
+/*
+ * The network transfer data callbacks.
+ * See the detail in @{link baidu_ca_transport_init}
+ */
+typedef duer_socket_t (*duer_soc_create_f)(duer_transevt_func func);
+typedef duer_status_t (*duer_soc_connect_f)(duer_socket_t sock,
+                                            const duer_addr_t* addr);
+typedef duer_status_t (*duer_soc_send_f)(duer_socket_t sock,
+                                         const void* data,
+                                         duer_size_t size,
+                                         const duer_addr_t* addr);
+typedef duer_status_t (*duer_soc_recv_f)(duer_socket_t sock,
+                                         void* data,
+                                         duer_size_t size,
+                                         duer_addr_t* addr);
+typedef duer_status_t (*duer_soc_recv_timeout_f)(duer_socket_t sock,
+                                                 void* data,
+                                                 duer_size_t size,
+                                                 duer_u32_t timeout,
+                                                 duer_addr_t* addr);
+typedef duer_status_t (*duer_soc_close_f)(duer_socket_t sock);
+typedef duer_status_t (*duer_soc_destroy_f)(duer_socket_t sock);
+
+/*
+ * Set network input/ouput function
+ *
+ * @Param f_create, in, the function create socket context
+ * @Param f_conn, in, the function connect socket to host
+ * @Param f_send, in, the function send message
+ * @Param f_recv, in, the function receive message
+ * @Param f_recv_timeout, in, the function receive message with timeout
+ * @Param f_close, in, the function close the socket
+ * @Param f_destroy, in, the function destroy the socket context
+ */
+DUER_EXT void baidu_ca_transport_init(duer_soc_create_f f_create,
+                                      duer_soc_connect_f f_conn,
+                                      duer_soc_send_f f_send,
+                                      duer_soc_recv_f f_recv,
+                                      duer_soc_recv_timeout_f f_recv_timeout,
+                                      duer_soc_close_f f_close,
+                                      duer_soc_destroy_f f_destroy);
+
+/*
+ * Create the socket context.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_create(duer_trans_ptr trans);
+
+/*
+ * Connect to the host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_connect(duer_trans_ptr trans,
+                                                  const duer_addr_t* addr);
+
+/*
+ * Send data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, in, the data will be sent
+ * @Param size, in, the data size
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_send(duer_trans_ptr trans,
+                                               const void* data,
+                                               duer_size_t size,
+                                               const duer_addr_t* addr);
+
+/*
+ * Receive data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, out, the data will be read
+ * @Param size, in, the data size
+ * @Param addr, out, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_recv(duer_trans_ptr trans,
+                                               void* data,
+                                               duer_size_t size,
+                                               duer_addr_t* addr);
+
+/*
+ * Receive data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, out, the data will be read
+ * @Param size, in, the data size
+ * @Param timeout, in, the receive timeout
+ * @Param addr, out, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_recv_timeout(duer_trans_ptr trans,
+                                                       void* data,
+                                                       duer_size_t size,
+                                                       duer_u32_t timeout,
+                                                       duer_addr_t* addr);
+
+/*
+ * Disconnect from the remote host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_close(duer_trans_ptr trans);
+
+/*
+ * Destroy the socket context.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_wrapper_destroy(duer_trans_ptr trans);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANS_WRAPPER_H

+ 130 - 0
include/libduer-device/include/lightduer_net_transport_wrapper.h

@@ -0,0 +1,130 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Define the adapter for networks.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H
+
+#include "lightduer_net_transport.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Acquire the handler.
+ *
+ * @Param func, in, the callback when transport event notify.
+ * @Param key_info, something used in down layer but generated on up layer
+ * @Return duer_trans_handler, the transport context
+ */
+DUER_INT duer_trans_handler duer_trans_acquire(duer_transevt_func func, const void *key_info);
+
+/*
+ * Set the public key or certificate.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, in, the content of the public key or certificate
+ * @Param size, in, the size of the data
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_set_pk(duer_trans_handler hdlr,
+                                         const void* data,
+                                         duer_size_t size);
+
+/*
+ * clear the public key or certificate, release the buffer
+ *
+ * @Param hdlr, in, the context for the transport
+ */
+DUER_INT duer_status_t duer_trans_unset_pk(duer_trans_handler hdlr);
+
+/*
+ * Set the timeout for receiving data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param timeout, in, the recv data timeout interval
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_set_read_timeout(duer_trans_handler hdlr,
+                                                   duer_u32_t timeout);
+
+/*
+ * Connect to the host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_connect(duer_trans_handler hdlr,
+                                          const duer_addr_t* addr);
+
+/*
+ * Send data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, in, the data will be sent
+ * @Param size, in, the data size
+ * @Param addr, in, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_send(duer_trans_handler hdlr,
+                                       const void* data,
+                                       duer_size_t size,
+                                       const duer_addr_t* addr);
+
+/*
+ * Receive data.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Param data, out, the data will be read
+ * @Param size, in, the data size
+ * @Param addr, out, the target address infomations
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_recv(duer_trans_handler hdlr,
+                                       void* data,
+                                       duer_size_t size,
+                                       duer_addr_t* addr);
+
+
+DUER_INT duer_status_t duer_trans_recv_timeout(duer_trans_handler hdlr,
+                                       void* data,
+                                       duer_size_t size,
+                                       duer_addr_t* addr);
+/*
+ * Disconnect from the remote host.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_close(duer_trans_handler hdlr);
+
+/*
+ * Release the handler.
+ *
+ * @Param hdlr, in, the context for the transport
+ * @Return duer_status_t, the operation result
+ */
+DUER_INT duer_status_t duer_trans_release(duer_trans_handler hdlr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TRANSPORT_H

+ 68 - 0
include/libduer-device/include/lightduer_net_util.h

@@ -0,0 +1,68 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: baidu_ca_util_network.h
+ * Auth: Su Hao(suhao@baidu.com)
+ * Date: 2016.10.20
+ * Desc: Network util tools.
+ */
+
+#ifndef __IOT_BAIDU_CA_SOURCE_BAIDU_CA_UTIL_NETWORK_H____
+#define __IOT_BAIDU_CA_SOURCE_BAIDU_CA_UTIL_NETWORK_H____
+
+#include "lightduer_types.h"
+
+/*
+ * Determine whether the system byte order is little endian
+ *
+ * @Return duer_ut_t, if big endian return 0, else return 1.
+ */
+DUER_INT duer_u8_t duer_is_little_endian(void);
+
+/*
+ * Convert the byte order from host to network for 16bit value
+ *
+ * @Param value, in, the host value
+ * @Param duer_u16_t, the network result
+ */
+DUER_INT duer_u16_t duer_htons(duer_u16_t value);
+
+/*
+ * Convert the byte order from host to network for 32bit value
+ *
+ * @Param value, in, the host value
+ * @Param duer_u32_t, the network result
+ */
+DUER_INT duer_u32_t duer_htonl(duer_u32_t value);
+
+/*
+ * Convert the byte order from network to host for 16bit value
+ *
+ * @Param value, in, the network value
+ * @Param duer_u16_t, the host result
+ */
+DUER_INT duer_u16_t duer_ntohs(duer_u16_t value);
+
+/*
+ * Convert the byte order from network to host for 32bit value
+ *
+ * @Param value, in, the network value
+ * @Param duer_u32_t, the host result
+ */
+DUER_INT duer_u32_t duer_ntohl(duer_u32_t value);
+
+#endif/*__IOT_BAIDU_CA_SOURCE_BAIDU_CA_UTIL_NETWORK_H____*/
+

+ 57 - 0
include/libduer-device/include/lightduer_network_defs.h

@@ -0,0 +1,57 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Network common definitions
+
+#ifndef LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H
+#define LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H
+
+#include "lightduer_types.h"
+
+/*
+ * protocol type
+ */
+enum _duer_transport_protocol_enum {
+    DUER_PROTO_TCP,
+    DUER_PROTO_UDP
+};
+
+/*
+ * Transport Event
+ */
+typedef enum _duer_transport_event_enum {
+    DUER_TEVT_CONNECT_RDY,
+    DUER_TEVT_SEND_RDY,
+    DUER_TEVT_RECV_RDY,
+    DUER_TEVT_CLOSE_RDY,
+    DUER_TEVT_SEND_TIMEOUT,
+    DUER_TEVT_NONE = -1
+} duer_transevt_e;
+
+/*
+ * The network address structure
+ */
+typedef struct _duer_address_s {
+    duer_u8_t    type;       // the socket type
+    duer_u16_t   port;       // the host port
+    void*       host;       // the host address
+    duer_size_t  host_size;  // the host address length
+} duer_addr_t;
+
+typedef void (*duer_transevt_func)(duer_transevt_e event);
+
+#endif // LIBDUER_DEVICE_FRAMEWORK_INCLUDE_LIGHTDUER_NETWORK_DEFS_H

+ 72 - 0
include/libduer-device/include/lightduer_nsdl_adapter.h

@@ -0,0 +1,72 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: Adapter between nsdl and Baidu CA CoAP.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H
+
+#include "ns_types.h"
+#include "sn_nsdl.h"
+#include "sn_coap_header.h"
+#include "lightduer_lib.h"
+#include "lightduer_types.h"
+#include "lightduer_coap.h"
+
+#ifndef MBED_CLIENT_C_VERSION
+#define MBED_CLIENT_C_VERSION (30001) // 3.0.1
+#endif
+
+/*
+ * Set the nsdl address from ca
+ *
+ * @Param target, in, the target will be evaluated
+ * @Param source, the CA address
+ */
+DUER_INT void duer_nsdl_address_set(sn_nsdl_addr_s* target,
+                                  const duer_addr_t* source);
+
+/*
+ * Set the nsdl CoAP header from ca
+ *
+ * @Param target, in, the target will be evaluated
+ * @Param opt, in, the CoAP options
+ * @Param source, the CA message header
+ */
+DUER_INT void duer_nsdl_header_set(sn_coap_hdr_s* target,
+                                 sn_coap_options_list_s* opt,
+                                 const duer_msg_t* source);
+
+/*
+ * Set the CoAP address from nsdl
+ *
+ * @Param target, in, the target will be evaluated
+ * @Param source, the nsdl address
+ */
+DUER_INT void duer_coap_address_set(duer_addr_t* target,
+                                  const sn_nsdl_addr_s* source);
+
+/*
+ * Set the CoAP header from nsdl
+ *
+ * @Param target, in, the target will be evaluated
+ * @Param source, the nsdl message header
+ */
+DUER_INT void duer_coap_header_set(duer_msg_t* target,
+                                 const sn_coap_hdr_s* source);
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_NSDL_ADAPTER_H

+ 207 - 0
include/libduer-device/include/lightduer_ota_decompression.h

@@ -0,0 +1,207 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_decompression.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA decompression abstraction layer header file
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H
+
+#include <stdint.h>
+#include "lightduer_mutex.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define NAME_LENGTH (20)
+
+typedef enum _duer_ota_decompression_type_s {
+    ZLIB = 0,
+    DECOMPRESSION_MAX = 1,
+} duer_ota_decompress_type_t;
+
+typedef int (*DECOMPRESS_DATA_HANDLER_FUN)(void *custom_data, char const *buf, size_t len);
+
+typedef struct _duer_ota_unpacker_decom_s {
+    char name[NAME_LENGTH + 1];
+    duer_mutex_t lock;
+    void *pdata;
+    char const *err_msg;
+    int (*init)(struct _duer_ota_unpacker_decom_s *decompression, void **pdata);
+    int (*unzip)(
+            struct _duer_ota_unpacker_decom_s *decompression,
+            void *pdata,
+            char const *compressed_data,
+            size_t data_size,
+            void *custom_data,
+            DECOMPRESS_DATA_HANDLER_FUN data_handler);
+    int (*recovery)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata);
+    int (*sync)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata);
+    int (*unint)(struct _duer_ota_unpacker_decom_s *decompression, void *pdata);
+} duer_ota_decompression_t;
+
+/*
+ * Initialize decompression module
+ *
+ * @param void:
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_init_decompression(void);
+
+/*
+ * Create a decompression object
+ *
+ * @param name: the name of decompression
+ *
+ * @return: Success: duer_ota_decompression_t *
+ *           Failed: NULL
+ */
+extern duer_ota_decompression_t *duer_ota_unpacker_create_decompression(char const *name);
+
+/*
+ * Get decompression object
+ *
+ * @param name: the name of decompression
+ *
+ * @return: Success: duer_ota_decompression_t *
+ *           Failed: NULL
+ */
+extern duer_ota_decompression_t *duer_ota_unpacker_get_decompression(
+        duer_ota_decompress_type_t type);
+
+/*
+ * Destroy decompression object
+ *
+ * @param void:
+ *
+ * @return: Success: duer_ota_decompression_t *
+ *           Failed: NULL
+ */
+extern int duer_ota_unpacker_destroy_decompression(duer_ota_decompression_t *decompression);
+
+/*
+ * Get decompression name
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: char const *
+ *           Failed: NULL
+ */
+extern const char *duer_ota_unpacker_get_decompression_name(
+        duer_ota_decompression_t const *decompression);
+
+/*
+ * Register a decompression library
+ *
+ * @param decompression: decompression object
+ *        type:          decompression library type
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_register_decompression(
+        duer_ota_decompression_t *decompression,
+        duer_ota_decompress_type_t type);
+
+/*
+ * Unregister a decompression library
+ *
+ * @param type: decompression library type
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_unregister_decompression(duer_ota_decompress_type_t type);
+
+/*
+ * Initialize decompression
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_init_decompression(duer_ota_decompression_t *decompression);
+
+/*
+ * Unzip data
+ *
+ * @param decompression: decompression object
+ *      compressed_data: compressed data
+ * compressed_data_size: size of corpressed data
+ *             unpacker: Unpacker object
+ *         data_handler: A function which receive the decompressed data
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_unzip(
+        duer_ota_decompression_t *decompression,
+        char const *compressed_data,
+        size_t compressed_data_size,
+        void *custom_data,
+        DECOMPRESS_DATA_HANDLER_FUN data_handler);
+
+/*
+ * Uninitialize decompression
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_uninit_decompression(duer_ota_decompression_t *decompression);
+
+/*
+ * Try to repair data
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_recovery_decompression(duer_ota_decompression_t *decompression);
+
+/*
+ * Synchronous data
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_unpacker_sync_decompression(duer_ota_decompression_t *decompression);
+
+/*
+ * Get decompression error message
+ *
+ * @param decompression: decompression object
+ *
+ * @return: Success: error message
+ *           Failed: NULL
+ */
+extern char const *duer_ota_decompression_get_err_msg(duer_ota_decompression_t const *decompression);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DECOMPRESSION_H

+ 229 - 0
include/libduer-device/include/lightduer_ota_downloader.h

@@ -0,0 +1,229 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_ota_downloader.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Downloader Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H
+
+#include "lightduer_mutex.h"
+#include "lightduer_http_client.h"
+
+#ifndef URL_LEN
+#define URL_LEN 301
+#endif
+
+typedef enum _duer_downloader_Protocol {
+    HTTP = 0,
+    COAP = 1,
+    LOCAL = 2,
+    MAX_PROTOCOL_COUNT = 3,
+} duer_downloader_protocol;
+
+typedef struct _duer_ota_downloader_s {
+    duer_downloader_protocol dp;
+    void *private_data;              // Set the private data you want to pass
+    duer_mutex_t lock;
+    char url[URL_LEN + 1];
+    struct _duer_ota_downloader_ops_s *ops;
+} duer_ota_downloader_t ;
+
+typedef int (*data_handler)(
+        void* private_data,
+        const char *buf,
+        size_t len);
+
+/*
+ * We need to consider unblock & timeout situation (TBD)
+ * connect_server callback function requires you that
+ * connect to server and receive the data immediately
+ * I think the abstract is not very good. Refactor (TBD)
+ */
+typedef struct _duer_ota_downloader_ops_s {
+    int (*init)(duer_ota_downloader_t *downloader);
+    int (*register_data_notify)(duer_ota_downloader_t *downloader, data_handler handler, void *private_data);
+    int (*connect_server)(duer_ota_downloader_t *downloader, const char *url);
+    int (*disconnect_server)(duer_ota_downloader_t *downloader);
+    int (*destroy)(duer_ota_downloader_t *downloader);
+} duer_ota_downloader_ops_t;
+
+/*
+ * Initialise OTA Downloader Module
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_init_ota_downloader(void);
+
+/*
+ * Uninitialise OTA Downloader Module
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_uninit_ota_downloader(void);
+/*
+ * Create OTA Downloader
+ *
+ * @param void:
+ *
+ * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t *
+ *                                  Failed:  NULL
+ */
+extern duer_ota_downloader_t *duer_ota_downloader_create_downloader(void);
+
+/*
+ * Initialise OTA Downloader
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_init_downloader(duer_ota_downloader_t *downloader);
+
+/*
+ * Register a OTA Downloader to OTA Downloader module
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @param dp: Download protocol which the downloader support
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_register_downloader(
+        duer_ota_downloader_t *downloader,
+        duer_downloader_protocol dp);
+
+/*
+ * Unregister a OTA Downloader from OTA Downloader module
+ *
+ * @param dp: Download protocol
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_unregister_downloader(duer_downloader_protocol dp);
+
+/*
+ * Get a OTA Downloader from OTA Downloader module
+ *
+ * @param dp: Download protocol which you want
+ *
+ * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t *
+ *                                  Failed:  NULL
+ */
+extern duer_ota_downloader_t *duer_ota_downloader_get_downloader(duer_downloader_protocol dp);
+
+/*
+ * Destroy a OTA Downloader, free the memory and unregister it from OTA Downloader module
+ *
+ * @param downloader: OTA downloader object
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_destroy_downloader(duer_ota_downloader_t *downloader);
+
+/*
+ * Register the downloader operations to a Downloader object
+ * If you want to support a new downloader protocol, you need to implement duer_ota_downloader_ops
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @param downloader_ops:  OTA Downloader operations
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_register_downloader_ops(
+        duer_ota_downloader_t *downloader,
+        duer_ota_downloader_ops_t *downloader_ops);
+
+/*
+ * Connect to server to get the data
+ * Note: Once you call it, the OTADownloader will download the data from server and pass it to you
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @param url: URL
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_connect_server(duer_ota_downloader_t *downloader, const char *url);
+
+/*
+ * Disconnect server
+ * Close the socket
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_disconnect_server(duer_ota_downloader_t *downloader);
+
+/*
+ * Register a callback functions "hancler" to receive the data which get from URL
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @param handler: Callback function to receive the data which get form URL
+ *
+ * @param private: param which you want to pass to handler
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_register_data_notify(
+        duer_ota_downloader_t *downloader,
+        data_handler handler,
+        void *private_data);
+
+/*
+ * Set the private data you want pass to the OTA downloader OPS
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @param private_data: data which you want to pass
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_downloader_set_private_data(
+        duer_ota_downloader_t *downloader,
+        void *private_data);
+
+/*
+ * Get the private data
+ *
+ * @param downloader: OTA Downloader object
+ *
+ * @return void *: Success: Other
+ *                 Failed:  NULL
+ */
+extern void *duer_ota_downloader_get_private_data(duer_ota_downloader_t *downloader);
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H

+ 37 - 0
include/libduer-device/include/lightduer_ota_http_downloader.h

@@ -0,0 +1,37 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_http_downloader.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA HTTP Downloader Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H
+
+#include "lightduer_ota_downloader.h"
+
+/*
+ * Initialise HTTP Downloader
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_init_http_downloader(void);
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H

+ 288 - 0
include/libduer-device/include/lightduer_ota_installer.h

@@ -0,0 +1,288 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_installer.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA installer Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H
+
+#include "lightduer_mutex.h"
+#include "lightduer_ota_package_info.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define INSTALLER_NAME_LEN 20
+
+typedef struct _duer_ota_installer_s {
+    // The name of OTA installer
+    char name[INSTALLER_NAME_LEN + 1];
+    // The customer data
+    void *custom_data;
+    // Lock the installer
+    duer_mutex_t lock;
+    // Last Error message, NULL if no error
+    char const *err_msg;
+
+    /*
+     * You can get module information
+     */
+	int (*get_module_info)(
+            struct _duer_ota_installer_s *installer,
+            void **custom_data,
+            duer_ota_module_info_t const *module_info);
+
+    /*
+     * Now we notify you, we are ready to start OTA
+     * You can do someting before receive data
+     * Such as open file, Initialise Flash, Initialise DMA, check chip ...
+     */
+    int (*notify_ota_begin)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * You can free the resource which you use in notify_ota_begin
+     * Set the boot partition ...
+     */
+    int (*notify_ota_end)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * You can get the file which you upload to Duer Cloud
+     */
+	int (*get_module_data)(
+            struct _duer_ota_installer_s *installer,
+            void *custom_data,
+            unsigned int offset,
+            unsigned char const *data,
+            size_t size);
+
+    /*
+     * You can verify the data you received
+     */
+	int (*verify_module_data)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * You can do someting before update the firmware
+     * Such as Erase Flash ...
+     */
+	int (*update_image_begin)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * Update the new firmware
+     */
+	int (*update_image)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * You can check the new firmware, and select the boot partition
+     */
+	int (*verify_image)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    /*
+     * Cancel the OTA update, free the resources and revert
+     */
+	int (*cancel_ota_update)(struct _duer_ota_installer_s *installer, void *custom_data);
+
+    struct _duer_ota_installer_s *next;
+} duer_ota_installer_t;
+
+/*
+ * Initialise OTA installer module
+ *
+ * @param void
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_init_installer(void);
+
+/*
+ * Get the customr data
+ *
+ * @param installer: OTA installer
+ *
+ * @return void *: Success: void *
+ *                  Failed: NULL
+ */
+extern void *duer_ota_installer_get_custom_data(duer_ota_installer_t const *installer);
+
+/*
+ * Create a OTA installer
+ *
+ * @param name: The name of installer
+ *
+ * @return int: Success: duer_ota_installer_t *
+ *              Failed:  NULL
+ */
+extern duer_ota_installer_t *duer_ota_installer_create_installer(char const *name);
+
+/*
+ * Destory a OTA installer
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_destroy_installer(duer_ota_installer_t *installer);
+
+/*
+ * Register a OTA installer
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_register_installer(duer_ota_installer_t *installer);
+
+/*
+ * Unregister a OTA installer
+ *
+ * @param name : The name of the OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_unregister_installer(char const *name);
+
+/*
+ * Get the OTA installer by name
+ *
+ * @param name : The name of the OTA installer
+ *
+ * @return int: Success: duer_ota_installer_t *
+ *              Failed:  NULL
+ */
+extern duer_ota_installer_t *duer_ota_installer_get_installer(char const *name);
+
+/*
+ * Notify OTA installer data begin
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_notify_ota_begin(duer_ota_installer_t *installer);
+
+/*
+ * Notify OTA installer the module information
+ *
+ * @param installer: OTA installer
+ * @param module_info: module information
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_send_module_info(
+        duer_ota_installer_t *installer,
+        duer_ota_module_info_t const *module_info);
+
+/*
+ * Send OTA installer the module data
+ *
+ * @param installer: OTA installer
+ * @param offset:    The offset of the image
+ * @param data:      OTA image data
+ * @param size       The size of OTA image data
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_send_module_data(
+        duer_ota_installer_t *installer,
+        unsigned int offset,
+        unsigned char const *data,
+        size_t size);
+
+/*
+ * Verify the module data
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_verify_module_data(duer_ota_installer_t *installer);
+
+/*
+ * Notify OTA installer bigin to update OTA image
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_update_image_begin(duer_ota_installer_t *installer);
+
+/*
+ * Notify OTA installer to update OTA image
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_update_image(duer_ota_installer_t *installer);
+
+/*
+ * Notify OTA installer to verify OTA image
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_verify_image(duer_ota_installer_t *installer);
+
+/*
+ * Notify OTA installer to end the OTA update
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_notify_ota_end(duer_ota_installer_t *installer);
+
+/*
+ * Cancel the OTA update
+ *
+ * @param installer: OTA installer
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_installer_cancel_ota_update(duer_ota_installer_t *installer);
+
+/*
+ * Get error message
+ *
+ * @param installer: OTA installer
+ *
+ * @return : Success: error message
+ *            Failed: NULL
+ */
+extern const char *duer_ota_installer_get_err_msg(duer_ota_installer_t const *installer);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_INSTALLER_H

+ 37 - 0
include/libduer-device/include/lightduer_ota_local_downloader.h

@@ -0,0 +1,37 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_local_downloader.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA local Downloader Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_LOCAL_DOWNLOADER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_LOCAL_DOWNLOADER_H
+
+#include "lightduer_ota_downloader.h"
+
+/*
+ * Initialise local Downloader
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_init_local_downloader(void);
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_HTTP_DOWNLOADER_H

+ 153 - 0
include/libduer-device/include/lightduer_ota_notifier.h

@@ -0,0 +1,153 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_notifier.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Notifier Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H
+
+#include "lightduer_dev_info.h"
+#include "lightduer_ota_updater.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define NAME_LEN   24
+#define BATCH_LEN  4
+
+#define OTA_PROTOCOL_VERSION "0.9"
+
+typedef enum _duer_ota_state {
+    OTA_IDLE = 0,
+    OTA_DOWNLOADING,
+    OTA_DOWNLOADED,
+    OTA_INSTALLING,
+    OTA_INSTALLED
+} duer_ota_state;
+
+typedef enum _duer_ota_event {
+    OTA_EVENT_BEGIN                       = 0,
+    OTA_EVENT_CONNECT_FAIL                = 1,
+    OTA_EVENT_CONNECTION_LOST             = 2,
+    OTA_EVENT_TIMEOUT                     = 3,
+    OTA_EVENT_DOWNLOADING                 = 4,
+    OTA_EVENT_DOWNLOAD_COMPLETE           = 5,
+    OTA_EVENT_DOWNLOAD_FAIL               = 6,
+    OTA_EVENT_INSTALLING                  = 7,
+    OTA_EVENT_IMAGE_INVALID               = 8,
+    OTA_EVENT_WRITE_ERROR                 = 9,
+    OTA_EVENT_INSTALLED                   = 10,
+    OTA_EVENT_DELETED                     = 11,
+    OTA_EVENT_REJECT                      = 12,
+    OTA_EVENT_UNPACK_FAIL                 = 30,
+    OTA_EVENT_CHECKSUM_FAIL               = 31,
+    OTA_EVENT_ERASE_FLASH_FAIL            = 32,
+    OTA_EVENT_BOOTLOADER_WRITE_ERROR      = 33,
+    OTA_EVENT_BOOTLOADER_CHECKSUM_FAIL    = 34,
+    OTA_EVENT_BOOTLOADER_IMAGE_INVALID    = 35,
+    OTA_EVENT_BOOTLOADER_VERSION_INVALID  = 36,
+    OTA_EVENT_BOOTLOADER_UNPACK_FAIL      = 37,
+    OTA_EVENT_BOOTLOADER_ERASE_FLASH_FAIL = 38,
+    OTA_EVENT_MAX,
+} duer_ota_event;
+
+typedef struct _duer_os_info_s {
+    char os_name[NAME_LEN + 1];
+    char developer[NAME_LEN + 1];
+    char os_version[FIRMWARE_VERSION_LEN + 1];
+    char staged_version[NAME_LEN + 1];
+} duer_os_info_t;
+
+typedef struct _duer_package_info_s {
+    char product[NAME_LEN + 1];
+    char batch[BATCH_LEN + 1];
+    duer_os_info_t os_info;
+} duer_package_info_t;
+
+typedef struct _duer_package_info_ops_s {
+    int (*get_package_info)(duer_package_info_t *info);
+} duer_package_info_ops_t;
+
+/*
+ * Notify the package information to the Duer Cloud
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_notify_package_info(void);
+
+/*
+ * Get the package information that about the current system
+ *
+ * @param info: Pointer to Package information
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_get_package_info(duer_package_info_t *info);
+
+/*
+ * Register get package information OPS
+ *
+ * @param ops: duer_package_info_ops_t * which you need to implement
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_register_package_info_ops(duer_package_info_ops_t const *ops);
+
+/*
+ * Notifier the OTA state to Duer Cloud
+ *
+ * @param updater: OTA updater object
+ *
+ * @param state: OTA state
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_notifier_state(duer_ota_updater_t const *updater, duer_ota_state state);
+
+/*
+ * Notifier OTA event to Duer Cloud
+ *
+ * @param updater: OTA updater object
+ *
+ * @param event: OTA event
+ *
+ * @param progress: It can pass information about installing progress information
+ *                  or downloading progress information
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_notifier_event(
+        duer_ota_updater_t const *updater,
+        duer_ota_event event,
+        double progress,
+        char const *desc);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_NOTIFIER_H

+ 110 - 0
include/libduer-device/include/lightduer_ota_package_info.h

@@ -0,0 +1,110 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_package_info.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Package Information Head File
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H
+
+#define KEY_LEN	                     (128)
+#define PACKAGE_NAME_LENGTH          (15)
+#define META_VERSION_LENGTH          (3)
+#define PACKAGE_VERSION_LENGTH       (15)
+#define INSTALL_PATH_LENGTH          (255)
+#define MODULE_NAME_LENGTH           (15)
+#define MODULE_VERSION_LENGTH        (15)
+#define MODULE_SIGNATURE_LENGTH      (33)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ota_package_type_s {
+    PACKAGE_TYPE_OS,                     // Operation system
+    PACKAGE_TYPE_APP,                    // Application
+    PACKAGE_TYPE_FIRMWARE,               // Chip firmware
+    PACKAGE_TYPE_MIXED,                  // MIXED
+} duer_ota_package_type_t;
+
+typedef enum _duer_ota_module_type_s {
+    MODULE_TYPE_BIN,
+    MODULE_TYPE_IMG,
+    MODULE_TYPE_JSON,
+    MODULE_TYPE_SO,
+} duer_ota_module_type_t;
+
+// package header
+typedef struct _duer_ota_package_header_s {
+   // 'mbed' verify tag
+   uint8_t tag[4];
+   // package header size
+   uint32_t package_header_size;
+   // package signature size: 1024 bits
+   uint32_t package_signature_size;
+   // package signature: include meta.json and all modules
+   unsigned char package_signature[KEY_LEN];
+   // meta.json signature size: 1024 bits
+   uint32_t meta_signature_size;
+   // meta.json signature
+   unsigned char meta_signature[KEY_LEN];
+   // meta.json size: used for decompress meta.json from package body
+   uint32_t meta_size;
+   // size of meta.json + all modules (before compress)
+   uint32_t ori_package_size;
+} duer_ota_package_header_t;
+
+typedef struct _duer_ota_package_basic_info_s {
+    uint8_t package_name[PACKAGE_NAME_LENGTH + 1];
+    uint8_t meta_version[META_VERSION_LENGTH + 1];
+    duer_ota_package_type_t package_type;
+    // Enforce the update
+    bool update;
+} duer_ota_package_basic_info_t;
+
+typedef struct _duer_ota_module_info_s {
+    uint8_t module_name[MODULE_NAME_LENGTH + 1];
+    uint32_t module_size;
+    duer_ota_module_type_t module_type;
+    // Enforce the update
+    bool update;
+    uint8_t module_signature[MODULE_SIGNATURE_LENGTH + 1];
+    uint8_t module_version[MODULE_VERSION_LENGTH + 1];
+    uint8_t module_support_hardware_version[MODULE_VERSION_LENGTH + 1];
+    uint8_t module_install_path[INSTALL_PATH_LENGTH + 1];
+} duer_ota_module_info_t;
+
+typedef struct _duer_ota_package_install_info_s {
+    uint8_t package_install_path[INSTALL_PATH_LENGTH + 1];
+    uint8_t module_count;
+    duer_ota_module_info_t *module_list;
+} duer_ota_package_install_info_t;
+
+typedef struct _duer_ota_update_info_s {
+    uint8_t package_version[PACKAGE_VERSION_LENGTH + 1];
+} duer_ota_update_info_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_PACKAGE_INFO_H

+ 180 - 0
include/libduer-device/include/lightduer_ota_unpacker.h

@@ -0,0 +1,180 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_unpacker.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA unpacker Head File
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include "lightduer_ota_verifier.h"
+#include "lightduer_ota_installer.h"
+#include "lightduer_ota_package_info.h"
+#include "lightduer_ota_decompression.h"
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ota_unpack_mode_s {
+    SAVE_PACKAGE_HEADER     = 1,
+    PARSE_PACKAGE_HEADER    = 2,
+    SAVE_META_DATA          = 3,
+    UNZIP_DATA              = 4,
+    UNZIP_DATA_DONE         = 5,
+    PARSE_META_DATA         = 6,
+    GET_OTA_INSTALLER       = 7,
+    UNZIP_MODLE_INFO        = 8,
+    UNZIP_MODLE_DATA        = 9,
+    NOTIFY_OTA_BEGIN        = 10,
+    NOTIFY_OTA_END          = 11,
+    DISTRIBUTE_MODULE_INFO  = 12,
+    DISTRIBUTE_MODULE_DATA  = 13,
+    VERIFY_MODULE_DATA      = 14,
+    PUBLIC_KEY_VERIFICATION = 15,
+    UPDATE_IMAGE_BEGIN      = 16,
+    UPDATE_IMAGE            = 17,
+    VERIFY_IMAGE            = 18,
+    CANCEL_OTA_UPDATE       = 19,
+    UNPACKER_MODE_MAX       = 21,
+} duer_ota_unpacker_mode_t;
+
+typedef struct _duer_ota_unpack_state_s {
+    duer_ota_unpacker_mode_t mode;
+    size_t received_data_size;
+    size_t processed_data_size;
+    size_t decompress_data_size;
+    size_t used_decompress_data_offset;
+    size_t used_data_offset;
+    size_t meta_data_offset;
+    size_t module_data_offset;
+    size_t package_header_data_offset;
+} duer_ota_unpack_state_t;
+
+typedef struct _duer_ota_unpacker_s {
+    // Lock the unpacker
+    duer_mutex_t lock;
+    duer_ota_verifier_t *verifier;
+    duer_ota_unpack_state_t state;
+    duer_ota_package_header_t package_header;
+    duer_ota_package_basic_info_t basic_info;
+    duer_ota_package_install_info_t install_info;
+    void *meta_data;
+    duer_ota_decompression_t *decompression;
+    duer_ota_installer_t *installer;
+    char const *err_msg;
+} duer_ota_unpacker_t;
+
+/*
+ * Create a OTA unpacker
+ *
+ * @param void:
+ *
+ * @return: Success: duer_ota_unpacker_t *
+ *           Failed: NULL
+ */
+extern duer_ota_unpacker_t *duer_ota_unpacker_create_unpacker(void);
+
+/*
+ * Destroy a OTA unpacker
+ *
+ * @param unpacker: OTA unpacker
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_unpacker_destroy_unpacker(duer_ota_unpacker_t *unpacker);
+
+/*
+ * Get the basic information of the package
+ *
+ * @param unpacker: OTA unpacker
+ *
+ * @return : Success:duer_ota_package_basic_info_t *
+ *           Failed: NULL
+ */
+extern duer_ota_package_basic_info_t const *duer_ota_unpacker_get_package_basic_info(
+        duer_ota_unpacker_t const *unpacker);
+
+/*
+ * Get the header information of the package
+ *
+ * @param unpacker: OTA unpacker
+ *
+ * @return : Success: duer_ota_package_header_t *
+ *           Failed: NULL
+ */
+extern duer_ota_package_header_t const *duer_ota_unpacker_get_package_header_info(
+        duer_ota_unpacker_t const *unpacker);
+
+/*
+ * Unpacke a OTA upadate package
+ *
+ * @param unpacker: OTA unpacker
+ *            data: data of the package
+ *            size: data size
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_unpacker_unpack_package(
+        duer_ota_unpacker_t *unpacker,
+        uint8_t const *data,
+        size_t size);
+
+/*
+ * Set the unpacker mode
+ *
+ * @param unpacker: OTA unpacker
+ *            mode: Unpacker mode
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_unpacker_set_unpacker_mode(
+        duer_ota_unpacker_t *unpacker,
+        duer_ota_unpacker_mode_t mode);
+
+/*
+ * Get the unpacker mode
+ *
+ * @param unpacker: OTA unpacker
+ *
+ * @return mode: Unpacker mode
+ */
+extern duer_ota_unpacker_mode_t duer_ota_unpacker_get_unpacker_mode(
+        duer_ota_unpacker_t const *unpacker);
+
+/*
+ * Get the unpacker error message
+ *
+ * @param unpacker: OTA unpacker
+ *
+ * @return char const *: Success: error message
+ *                       Failed:  NULL
+ */
+extern char const *duer_ota_unpacker_get_err_msg(duer_ota_unpacker_t const *unpacker);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UNPACKER_H

+ 159 - 0
include/libduer-device/include/lightduer_ota_updater.h

@@ -0,0 +1,159 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_updater.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Updater Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H
+
+#include "lightduer_ota_unpacker.h"
+#include "lightduer_ota_downloader.h"
+#include "mbedtls/md5.h"
+
+#define TRANSACTION_LEN   (65)
+#define VERSION_LEN       (16)
+#define SIGNATURE_LEN     (129)
+#define MD5_LEN           (16)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_ota_switch {
+    ENABLE_OTA  =  1,
+    DISABLE_OTA = -1,
+} duer_ota_switch;
+
+typedef enum _duer_ota_reboot {
+    ENABLE_REBOOT  =  1,
+    DISABLE_REBOOT = -1,
+} duer_ota_reboot;
+
+typedef struct _duer_ota_update_command_s {
+    char transaction[TRANSACTION_LEN + 1];
+    char version[VERSION_LEN + 1];
+    char old_version[VERSION_LEN + 1];
+    char url[URL_LEN + 1];
+    char signature[SIGNATURE_LEN + 1];
+    unsigned int size;
+} duer_ota_update_command_t;
+
+typedef struct _duer_ota_updater_s {
+    int id;
+    duer_ota_unpacker_t *unpacker;
+    duer_ota_downloader_t *downloader;
+    duer_ota_update_command_t *update_cmd;
+    int received_data_size;
+    int progress;
+    mbedtls_md5_context md5_ctx;
+} duer_ota_updater_t;
+
+typedef struct _duer_ota_init_ops_s {
+/*
+ * Call duer_ota_installer_register_installer() to register the
+ * OTA installer
+ */
+    int (*register_installer)(void);
+/*
+ * Unregister OTA installer
+ */
+    int (*unregister_installer)(void);
+/*
+ * Implement reboot system function
+ */
+    int (*reboot)(void *arg);
+} duer_ota_init_ops_t;
+
+typedef void (*OTA_Updater)(int arg, void *update_cmd);
+
+/*
+ * Call it to initialize the OTA module
+ *
+ * @param ops: You need to implement the structure OTAInitOps
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_init_ota(duer_ota_init_ops_t const *ops);
+
+/*
+ * User Call it to enable/disable OTA
+ *
+ * @param ops: You need to implement the structure OTAInitOps
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_set_switch(duer_ota_switch flag);
+
+/*
+ * Get the OTA status
+ *
+ * @param void:
+ *
+ * @return int: Enable   1
+ *              Disable -1
+ */
+extern int duer_ota_get_switch(void);
+
+/*
+ * Set the OTA reboot status
+ *
+ * @param void:
+ *
+ * @return int: Enable   1
+ *              Disable -1
+ */
+extern int duer_ota_set_reboot(duer_ota_reboot reboot);
+
+/*
+ * get the OTA reboot status
+ *
+ * @param void:
+ *
+ * @return int: Enable   1
+ *              Disable -1
+ */
+extern int duer_ota_get_reboot(void);
+
+/*
+ * get the OTA update command
+ *
+ * @param void:
+ *
+ * @return Success: duer_ota_update_command_t const *
+ *         Failed:  NULL
+ */
+extern duer_ota_update_command_t const *duer_ota_get_update_cmd(void);
+
+/*
+ * Create a OTA updater to update the firmware
+ *
+ * @param update_cmd: Update command
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+extern int duer_ota_update_firmware(duer_ota_update_command_t const *update_cmd);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_UPDATER_H

+ 86 - 0
include/libduer-device/include/lightduer_ota_verifier.h

@@ -0,0 +1,86 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_verification.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Verification Head File
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H
+
+#include "mbedtls/md5.h"
+#include "mbedtls/rsa.h"
+#include "mbedtls/sha1.h"
+#include "lightduer_ota_package_info.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct _duer_ota_verifier_s {
+    mbedtls_rsa_context rsa;
+	mbedtls_sha1_context sha1;
+} duer_ota_verifier_t;
+
+/*
+ * Create a OTA verifier
+ *
+ * @param void:
+ *
+ * @return: Success: duer_ota_verifier_t *
+ *           Failed: NULL
+ */
+extern duer_ota_verifier_t *duer_ota_verification_create_verifier(void);
+
+/*
+ * Destroy a OTA verifier
+ *
+ * @param verifier: duer_ota_verifier_t *
+ *
+ * @return void:
+ */
+extern void duer_ota_verification_destroy_verifier(duer_ota_verifier_t *verifier);
+
+/*
+ * Verify data
+ *
+ * @param verifier: duer_ota_verifier_t *
+ *        data: uint8_t *
+ *        size: size_t
+ * @return void:
+ */
+extern void duer_ota_verification_update(
+        duer_ota_verifier_t *verifier,
+        uint8_t const *data,
+        size_t size);
+
+/*
+ * Verify the OTA package
+ *
+ * @param verify: duer_ota_verifier_t *
+ *        header: Heare information
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_verification_verify(duer_ota_verifier_t *verifier, duer_ota_package_header_t const *header);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_VERIFICATION_H

+ 43 - 0
include/libduer-device/include/lightduer_ota_zlib.h

@@ -0,0 +1,43 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/*
+ * File: lightduer_ota_zlib.h
+ * Auth: Zhong Shuai (zhongshuai@baidu.com)
+ * Desc: OTA Zlib header file
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Initialize Zliblite
+ *
+ * @param void:
+ *
+ * @return: Success: DUER_OK
+ *           Failed: Other
+ */
+extern int duer_ota_init_zlib(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_ZLIB_H

+ 63 - 0
include/libduer-device/include/lightduer_queue_cache.h

@@ -0,0 +1,63 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_queue_cache.h
+ * Auth: Su Hao(suhao@baidu.com)
+ * Desc: Provide the Cache util in the Queue.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_QUEUE_CACHE_H
+#define BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_QUEUE_CACHE_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void *duer_qcache_handler;
+
+typedef void *duer_qcache_iterator;
+
+typedef void (duer_qcache_visit_func)(void *);
+
+duer_qcache_handler duer_qcache_create(void);
+
+int duer_qcache_push(duer_qcache_handler cache, void *data);
+
+int duer_qcache_insert(duer_qcache_handler cache, duer_qcache_iterator it, void *data);
+
+void *duer_qcache_top(duer_qcache_handler cache);
+
+void *duer_qcache_pop(duer_qcache_handler cache);
+
+duer_qcache_iterator duer_qcache_head(duer_qcache_handler cache);
+
+duer_qcache_iterator duer_qcache_next(duer_qcache_iterator it);
+
+void *duer_qcache_data(duer_qcache_iterator it);
+
+size_t duer_qcache_length(duer_qcache_handler cache);
+
+void duer_qcache_destroy_traverse(duer_qcache_handler cache, duer_qcache_visit_func visit);
+
+void duer_qcache_destroy(duer_qcache_handler cache);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_COMMON_LIGHTDUER_QUEUE_CACHE_H*/

+ 54 - 0
include/libduer-device/include/lightduer_random.h

@@ -0,0 +1,54 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The random APIs.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Obtain the random number
+ *
+ * @Return duer_s32_t, the random number.
+ */
+DUER_INT duer_s32_t duer_random(void);
+
+/*
+ * The random callbacks
+ * Return the random number
+ *        0, means random got fail
+ */
+typedef duer_s32_t (*duer_random_f)();
+
+/*
+ * Initial the random callback for Baidu CA
+ *
+ * @Param f_random, in, the function obtain the random
+ */
+DUER_EXT void duer_random_init(duer_random_f f_random);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_RANDOM_H

+ 37 - 0
include/libduer-device/include/lightduer_random_impl.h

@@ -0,0 +1,37 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_random_impl.h
+ * Auth: Zhang Leliang(zhangleliang@baidu.com)
+ * Desc: Provide the random APIs.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H
+#define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+duer_s32_t duer_random_impl(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_RANDOM_IMPL_H

+ 30 - 0
include/libduer-device/include/lightduer_session.h

@@ -0,0 +1,30 @@
+// Copyright (2017) Baidu Inc. All rights reserveed.
+//
+// File: lightduer_session.h
+// Auth: Su Hao (suhao@baidu.com)
+// Desc: Manage the talk session.
+
+#ifndef BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H
+#define BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "lightduer_types.h"
+
+extern void duer_session_initialize(void);
+
+extern duer_u32_t duer_session_generate(void);
+
+extern duer_bool duer_session_consume(duer_u32_t id);
+
+extern duer_bool duer_session_is_matched(duer_u32_t id);
+
+extern void duer_session_finalize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_MODULES_VOICE_ENGINE_LIGHTDUER_SESSION_H*/

+ 52 - 0
include/libduer-device/include/lightduer_sleep.h

@@ -0,0 +1,52 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Chang Li(changli@baidu.com)
+//
+// Description: The sleep APIs.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * sleep function
+ *
+ * @param ms, the sleep in ms.
+ */
+DUER_INT void duer_sleep(duer_u32_t ms);
+
+/*
+ * The sleep callbacks
+ */
+typedef void (*duer_sleep_f_t)(duer_u32_t ms);
+
+/*
+ * Initial the sleep callbacks for Baidu CA
+ *
+ * @Param _f_sleep, in, sleep function implementation from different platform
+ */
+DUER_EXT void baidu_ca_sleep_init(duer_sleep_f_t _f_sleep);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_SLEEP_H

+ 54 - 0
include/libduer-device/include/lightduer_snprintf.h

@@ -0,0 +1,54 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_snprintf.h
+ * Auth: Leliang Zhang(zhangleliang@baidu.com)
+ * Desc: Provide snprintf/vsnprintf implementation.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_SNPRINTF_H
+#define BAIDU_DUER_LIGHTDUER_SNPRINTF_H
+
+#include <stdarg.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+  *@param s: the buffer to store the formatted output string
+  *@param n: the buffer @s size
+  *@param format: the output format
+  *@param ...: the input param for the formatting
+  *@return the length of the formatted string
+  */
+int lightduer_snprintf(char *s, size_t n, const char *format, ...);
+
+/**
+  *@param s: the buffer to store the formatted output string
+  *@param n: the buffer @s size
+  *@param format: the output format
+  *@param ap: the input param for the formatting
+  *@return the length of the formatted string
+  */
+int lightduer_vsnprintf(char *s, size_t n, const char *format, va_list ap);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_SNPRINTF_H

+ 45 - 0
include/libduer-device/include/lightduer_speex.h

@@ -0,0 +1,45 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_speex.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer Speex encoder.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void *duer_speex_handler;
+
+typedef void (*duer_encoded_func)(const void *, size_t);
+
+duer_speex_handler duer_speex_create(int rate, int quality);
+
+void duer_speex_encode(duer_speex_handler handler, const void *data, size_t size, duer_encoded_func func);
+
+void duer_speex_destroy(duer_speex_handler handler);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_SPEEX_H*/

+ 30 - 0
include/libduer-device/include/lightduer_statistics.h

@@ -0,0 +1,30 @@
+/* Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * File: duerapp_system_info.h
+ * Auth: Zhong Shuai(zhongshuai@baidu.com)
+ * Desc: Provide the information about the system
+ */
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H
+#define BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Initialise System Info Module
+ *
+ * @param void:
+ *
+ * @return int: Success: DUER_OK
+ *              Failed:  Other
+ */
+
+int duer_statistics_initialize(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIBDUER_DEVICE_PLATFORM_INCLUDE_LIGHTDUER_STATISTICS_H

+ 76 - 0
include/libduer-device/include/lightduer_store_voice.h

@@ -0,0 +1,76 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_store_voice.h
+ * Auth: Zhang Leliang (zhangleliang@baidu.com)
+ * Desc: the voice/speex store interface,
+ *       different platform with different implementations,
+         make sure its implementation before use
+ */
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Initialise store
+ *
+ * @param name_id: use this to identify the differernt voices
+ *
+ * @return int: Success: 0
+ *              Failed:  Other
+ */
+int duer_store_voice_start(int name_id);
+
+/*
+ * store the voice data, e.g. PCM data
+ *
+ * @param data: voice data
+ * @param size: data size
+ *
+ * @return int: Success: 0
+ *              Failed:  Other
+ */
+int duer_store_voice_write(const void *data, uint32_t size);
+
+/*
+ * store the speex data
+ *
+ * @param data: speex data
+ * @param size: data size
+ *
+ * @return int: Success: 0
+ *              Failed:  Other
+ */
+int duer_store_speex_write(const void *data, uint32_t size);
+
+/*
+ * finilize store
+ *
+ * @return int: Success: 0
+ *              Failed:  Other
+ */
+int duer_store_voice_end(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_STORE_VOICE_H*/

+ 196 - 0
include/libduer-device/include/lightduer_system_info.h

@@ -0,0 +1,196 @@
+/*
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ * Author: Zhong Shuai (zhongshuai@baidu.com)
+ *
+ * Desc: Provide System Information
+ */
+
+#ifndef BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H
+#define BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define DUER_OS_VERSION "release-v3.1.0"
+
+#define OS_VERSION_LEN          20
+#define SW_VERSION_LEN          20
+#define FIRMWARE_VERSION_LEN    15
+#define BRAND_LEN               20
+#define EQUIPMENT_TYPE_LEN      20
+#define HARDWARE_VERSION_LEN    20
+#define DEVELOPER_NAME_LEN      20
+
+#define TASK_NAME_LEN           30
+
+#define PARTITION_NAME_LEN      10
+#define MOUNT_INFO_LEN          20
+#define UNILIZATION_LEN         5
+
+#define NETWORK_CARD_NAME_LEN   10
+#define HW_ADDRESS_LEN          17
+#define SSID_LEN                20
+#define BSSID_LEN               17
+#define IPV4_ADDRESS_LEN        32
+#define BCAST_LEN               32
+#define MASK_LEN                32
+#define IPV6_ADDRESS_LEN        128
+#define LINK_ENACP_LEN          10
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct _system_static_info_s {
+    char os_version[OS_VERSION_LEN + 1];
+    char sw_version[SW_VERSION_LEN + 1];
+    char brand[BRAND_LEN + 1];
+    char equipment_type[EQUIPMENT_TYPE_LEN + 1];
+    char hardware_version[HARDWARE_VERSION_LEN + 1];
+    uint32_t ram_size;                                 // Unit (KB)
+    uint32_t rom_size;                                 // Unit (KB)
+} duer_system_static_info_t;
+
+typedef struct _task_info_s {
+    char task_name[TASK_NAME_LEN];
+    int8_t cpu_occupancy_rate;              // If it is negative, it will not reported
+    uint32_t stack_size;
+    uint32_t stack_free_size;
+    int32_t stack_max_used_size;            // If it is negative, it will not reported
+    struct _task_info_s *next;
+} duer_task_info_t;
+
+typedef struct _system_dynamic_info_s {
+    uint64_t system_up_time;                // Unit (Heartbeat of the system)
+    uint32_t running_task;
+    uint32_t total_task;
+    duer_task_info_t *task_info;
+    char *system_load;                      // If it is NULL, it will not reported
+    size_t system_load_len;
+    char *system_crash_info;                // If it is NULL, it will not reported
+    size_t system_crash_info_len;
+} duer_system_dynamic_info_t;
+
+typedef enum _memory_type {
+    RAM   = 1,
+    PSRAM = 2,
+} duer_memory_type_t;
+
+typedef struct _memory_info {
+    duer_memory_type_t memory_type;
+    uint64_t total_memory_size;                        // Unit (KB)
+    uint64_t available_memory_size;                    // Unit (KB)
+    int64_t shared_memory_size;
+    int64_t buffer_memory_size;
+    int64_t swap_size;
+    int64_t free_swap_size;
+    uint64_t peak;                                     // Within 5 minutes Unit (KB)
+    uint64_t trough;                                   // Within 5 minutes Unit (KB)
+    uint64_t average;                                  // Within 5 minutes Unit (KB)
+} duer_memory_info_t;
+
+typedef struct _disk_info_t {
+    char partition_name[PARTITION_NAME_LEN + 1];
+    char mount_info[MOUNT_INFO_LEN + 1];
+    uint8_t unilization;                               // percentage (50%)
+    uint64_t total_size;                               // Unit (KB)
+    uint64_t used_size;                                // Unit (KB)
+    uint64_t available_size;                           // Unit (KB)
+    struct _disk_info_t *next;
+} duer_disk_info_t;
+
+typedef struct _wireless_info_s {
+    char ssid[SSID_LEN + 1];
+    char bssid[BSSID_LEN + 1];
+
+    int8_t  link;                                       // General quality of the reception
+    int32_t level;                                     // Signal strength at the receiver
+    int32_t noise;                                     // Silence level (no packet) at the receiver
+} duer_wireless_info;
+
+typedef enum {
+    ETHERNET_TYPE = 1,
+    WIFI_TYPE,
+} duer_network_type;
+
+// Only retport one network info which the Duer OS used
+typedef struct _network_info_s {
+    duer_network_type network_type;
+    char hw_address[HW_ADDRESS_LEN + 1];
+    char ipv4_address[IPV4_ADDRESS_LEN + 1];             // If it is NULL, it will not reported
+    char ipv6_address[IPV6_ADDRESS_LEN + 1];             // If it is NULL, it will not reported
+    duer_wireless_info wireless;
+    uint32_t mtu;
+    uint64_t transmitted_packets;
+    uint64_t received_packets;
+    uint64_t forwarded_packets;
+    uint64_t dropped_packets;
+    uint64_t checksum_error;
+    uint64_t invalid_length_error;
+    uint64_t routing_error;
+    uint64_t protocol_error;
+    uint64_t tx_average_speed;                            // Within 5 minutes Unit (bytes/s)
+    uint64_t rx_average_speed;                            // Within 5 minutes Unit (bytes/s)
+} duer_network_info_t;
+
+typedef struct _system_info_ops_t {
+    int (*init)(void);
+    int (*get_system_static_info)(duer_system_static_info_t *info);
+    int (*get_system_dynamic_info)(duer_system_dynamic_info_t *info);
+    int (*free_system_dynamic_info)(duer_system_dynamic_info_t *info);
+    int (*get_memory_info)(duer_memory_info_t *mem_info);
+    int (*get_disk_info)(duer_disk_info_t **disk_info);
+    int (*free_disk_info)(duer_disk_info_t *disk_info);
+    int (*get_network_info)(duer_network_info_t *network_info);
+    int (*free_network_info)(duer_network_info_t *network_info);
+} duer_system_info_ops_t;
+
+extern int duer_init_system_info(void);
+
+extern int duer_system_info_register_system_info_ops(duer_system_info_ops_t *info_ops);
+
+extern int duer_system_info_get_static_system_info(duer_system_static_info_t *info);
+
+extern int duer_system_info_get_dynamic_system_info(duer_system_dynamic_info_t *info);
+
+extern int duer_system_info_free_dynamic_system_info(duer_system_dynamic_info_t *info);
+
+extern int duer_system_info_get_mem_info(duer_memory_info_t *info);
+
+extern int duer_system_info_get_disk_info(duer_disk_info_t **info);
+
+extern int duer_system_info_free_disk_info(duer_disk_info_t *info);
+
+extern int duer_system_info_get_network_info(duer_network_info_t *info);
+
+extern int duer_system_info_free_network_info(duer_network_info_t *info);
+
+extern void duer_system_info_show_system_static_info(duer_system_static_info_t const *info);
+
+extern void duer_system_info_show_system_dynamic_info(duer_system_dynamic_info_t const *info);
+
+extern void duer_system_info_show_memory_info(duer_memory_info_t const *memory_info);
+
+extern void duer_system_info_show_disk_info(duer_disk_info_t const *disk_info);
+
+extern void duer_system_info_show_networkcard_info(duer_network_info_t const *network_info);
+
+extern int duer_uninit_system_info(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIBDUER_DEVICE_LIGHTDUER_SYSTEM_INFO_H

+ 52 - 0
include/libduer-device/include/lightduer_thread.h

@@ -0,0 +1,52 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_thread.h
+ * Auth: Zhang Leliang(zhangleliang@baidu.com)
+ * Desc: Provide the thread APIs.
+ */
+
+#ifndef LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H
+#define LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * get the thread id
+ */
+duer_u32_t duer_get_thread_id(void);
+
+/*
+ * The thread callbacks
+ */
+typedef duer_u32_t (*duer_get_thread_id_f_t)();
+
+/*
+ * Initial the thread callbacks for lightduer
+ *
+ * @Param _f_get_thread_id, in, get threadid  implementation from different platform
+ */
+DUER_EXT void duer_thread_init(duer_get_thread_id_f_t _f_get_thread_id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBDUER_DEVICE_FRAMEWORK_CORE_LIGHTDUER_THREAD_H

+ 37 - 0
include/libduer-device/include/lightduer_thread_impl.h

@@ -0,0 +1,37 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_thread_impl.h
+ * Auth: Zhang Leliang(zhangleliang@baidu.com)
+ * Desc: Provide the thread APIs.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H
+#define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+duer_u32_t duer_get_thread_id_impl();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_THREAD_IMPL_H

+ 53 - 0
include/libduer-device/include/lightduer_timers.h

@@ -0,0 +1,53 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_timers.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Provide the timer APIs.
+ */
+
+#ifndef BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H
+#define BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum _duer_timer_type_enum {
+    DUER_TIMER_ONCE,
+    DUER_TIMER_PERIODIC,
+    DUER_TIMER_TOTAL
+};
+
+typedef void *duer_timer_handler;
+
+typedef void (*duer_timer_callback)(void *);
+
+duer_timer_handler duer_timer_acquire(duer_timer_callback callback, void *param, int type);
+
+int duer_timer_start(duer_timer_handler handle, size_t delay);
+
+int duer_timer_stop(duer_timer_handler handle);
+
+void duer_timer_release(duer_timer_handler handle);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_PORT_INCLUDE_LIGHTDUER_TIMERS_H*/

+ 54 - 0
include/libduer-device/include/lightduer_timestamp.h

@@ -0,0 +1,54 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The timestamp APIs.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H
+
+#include "lightduer_types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Obtain the system timestamp by milliseconds
+ *
+ * @Return duer_u32_t, the system timestamp by milliseconds
+ */
+
+DUER_INT duer_u32_t duer_timestamp(void);
+
+/*
+ * The timestamp callbacks
+ * Return the timestamp by milliseconds
+ */
+typedef duer_u32_t (*duer_timestamp_f)();
+
+/*
+ * Initial the timestamp callbacks for Baidu CA
+ *
+ * @Param f_timestamp, in, the function obtain the timestamp
+ */
+DUER_EXT void baidu_ca_timestamp_init(duer_timestamp_f f_timestamp);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_SOURCE_BAIDU_CA_TIMESTAMP_H

+ 101 - 0
include/libduer-device/include/lightduer_types.h

@@ -0,0 +1,101 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+// Author: Su Hao (suhao@baidu.com)
+//
+// Description: The common type definitions.
+
+#ifndef BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H
+#define BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// Declaration for local varaible & function.
+#define DUER_LOC         static
+// Definition for local varaible & function.
+#define DUER_LOC_IMPL    static
+
+// Declaration for internal varaible & function.
+#define DUER_INT         extern
+// Definition for internal varaible & function.
+#define DUER_INT_IMPL
+
+// Declaration for external varaible & function.
+#define DUER_EXT         extern
+// Definition for external varaible & function.
+#define DUER_EXT_IMPL
+
+/*
+ * The ca handler
+ */
+typedef void*          duer_handler;
+
+typedef void*          duer_context;
+typedef int            duer_status_t;
+#ifdef __GNUC__
+typedef size_t   duer_size_t;
+#else
+typedef unsigned int   duer_size_t;
+#endif
+typedef unsigned int   duer_u32_t;
+typedef unsigned short duer_u16_t;
+typedef unsigned char  duer_u8_t;
+typedef signed int     duer_s32_t;
+typedef char           duer_bool;
+
+enum _baidu_ca_bool_e {
+    DUER_FALSE,
+    DUER_TRUE
+};
+
+/*
+ * The error codes.
+ */
+typedef enum _duer_errcode_enum {
+    DUER_OK,
+    DUER_ERR_FAILED                  = -0x0001,
+    DUER_ERR_CONNECT_TIMEOUT         = -0x0002,
+    DUER_CANCEL                      = -0x0003,
+    DUER_INF_TRANS_IP_BY_HTTP_DNS    = -0x0004, // IP got through http dns
+
+    /* Generic Errors */
+    DUER_ERR_INVALID_PARAMETER       = -0x0010,
+    DUER_ERR_MEMORY_OVERLOW          = -0x0011,
+    DUER_ERR_PROFILE_INVALID         = -0x0012,
+    DUER_ERR_HAS_STARTED             = -0x0013,
+    DUER_ERR_CA_NOT_CONNECTED        = -0x0014,
+    DUER_ERR_HAS_STOPPED             = -0x0015,
+
+    /* Network Errors */
+    DUER_ERR_TRANS_INTERNAL_ERROR    = -0x0030,
+    DUER_ERR_TRANS_WOULD_BLOCK       = -0x0031,
+    DUER_ERR_TRANS_TIMEOUT           = -0x0032, // send timeout
+    DUER_ERR_REG_FAIL                = -0x0033,
+    DUER_ERR_TRANS_DNS_FAIL          = -0x0034,
+
+    DUER_ERR_MBEDTLS_NET_MAX         = -0x0040,
+    // -0x0040 - -0x0060 reserved for mbed net error code , see mbedtls/net.h for more info
+    DUER_ERR_MBEDTLS_NET_MIN         = -0x0060,
+    DUER_ERR_MBEDTLS_SSL_MAX         = -0x6000,
+    // -0x6000 - -0x8000 reserved for ssl error code , see ssl.h for more info
+    DUER_ERR_MBEDTLS_SSL_MIN         = -0x8000,
+
+    DUER_ERR_REPORT_FAILED           = -0x070000,
+    DUER_ERR_REPORT_FAILED_BEGIN     = DUER_ERR_REPORT_FAILED + 0x10000
+} duer_errcode_t;
+
+#endif // BAIDU_IOT_TINYDU_IOT_OS_SRC_IOT_BAIDU_CA_INCLUDE_BAIDU_CA_TYPES_H

+ 78 - 0
include/libduer-device/include/lightduer_voice.h

@@ -0,0 +1,78 @@
+/**
+ * Copyright (2017) Baidu Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+/**
+ * File: lightduer_voice.h
+ * Auth: Su Hao (suhao@baidu.com)
+ * Desc: Light duer voice APIS.
+ */
+
+#include "baidu_json.h"
+
+#ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H
+#define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum _duer_voice_mode_enum {
+    DUER_VOICE_MODE_DEFAULT,
+    DUER_VOICE_MODE_CHINESE_TO_ENGLISH,
+    DUER_VOICE_MODE_ENGLISH_TO_CHINESE,
+    DUER_VOICE_MODE_WCHAT,
+    DUER_VOICE_MODE_C2E_BOT,
+    DUER_VOICE_MODE_INTERACTIVE_CLASS,
+}
+duer_voice_mode;
+
+typedef void (*duer_voice_delay_func)(duer_u32_t);
+typedef const char* (*duer_get_dialog_id_func)(void);
+
+void duer_voice_set_delay_threshold(duer_u32_t delay, duer_voice_delay_func);
+
+void duer_voice_set_mode(duer_voice_mode mode);
+
+duer_voice_mode duer_voice_get_mode(void);
+
+int duer_voice_start(int samplerate);
+
+int duer_voice_send(const void *data, size_t size);
+
+int duer_voice_cache(duer_bool cached);
+
+int duer_voice_stop(void);
+
+int duer_voice_terminate(void);
+
+void duer_add_translate_flag(baidu_json *data);
+
+void duer_reg_dialog_id_cb(duer_get_dialog_id_func cb);
+
+/*
+ * topic_id + 1
+ *
+ * * Return: Other topic_id value
+ *           0   Failed
+ */
+size_t duer_increase_topic_id(void);
+
+int duer_voice_set_speex_quality(int quality);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif/*BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_VOICE_H*/

+ 51 - 0
include/snowboy/include/snowboy-detect-c-wrapper.h

@@ -0,0 +1,51 @@
+// snowboy-detect-c-wrapper.h
+
+// Copyright 2017  KITT.AI (author: Guoguo Chen)
+
+#ifndef SNOWBOY_DETECT_C_WRAPPER_H_
+#define SNOWBOY_DETECT_C_WRAPPER_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+  typedef struct SnowboyDetect SnowboyDetect;
+
+  SnowboyDetect* SnowboyDetectConstructor(const char* const resource_filename,
+                                          const char* const model_str);
+
+  bool SnowboyDetectReset(SnowboyDetect* detector);
+
+  int SnowboyDetectRunDetection(SnowboyDetect* detector,
+                                const int16_t* const data,
+                                const int array_length, bool is_end);
+
+  void SnowboyDetectSetSensitivity(SnowboyDetect* detector,
+                                   const char* const sensitivity_str);
+
+  void SnowboyDetectSetAudioGain(SnowboyDetect* detector,
+                                 const float audio_gain);
+
+  void SnowboyDetectUpdateModel(SnowboyDetect* detector);
+
+  void SnowboyDetectApplyFrontend(SnowboyDetect* detector,
+                                  const bool apply_frontend);
+
+  int SnowboyDetectNumHotwords(SnowboyDetect* detector);
+
+  int SnowboyDetectSampleRate(SnowboyDetect* detector);
+
+  int SnowboyDetectNumChannels(SnowboyDetect* detector);
+
+  int SnowboyDetectBitsPerSample(SnowboyDetect* detector);
+
+  void SnowboyDetectDestructor(SnowboyDetect* detector);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // SNOWBOY_DETECT_C_WRAPPER_H_

+ 226 - 0
include/snowboy/include/snowboy-detect.h

@@ -0,0 +1,226 @@
+// include/snowboy-detect.h
+
+// Copyright 2016  KITT.AI (author: Guoguo Chen)
+
+#ifndef SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_
+#define SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_
+
+#include <memory>
+#include <string>
+
+namespace snowboy {
+
+// Forward declaration.
+struct WaveHeader;
+class PipelineDetect;
+class PipelineVad;
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// SnowboyDetect class interface.
+//
+////////////////////////////////////////////////////////////////////////////////
+class SnowboyDetect {
+ public:
+  // Constructor that takes a resource file, and a list of hotword models which
+  // are separated by comma. In the case that more than one hotword exist in the
+  // provided models, RunDetection() will return the index of the hotword, if
+  // the corresponding hotword is triggered.
+  //
+  // CAVEAT: a personal model only contain one hotword, but an universal model
+  //         may contain multiple hotwords. It is your responsibility to figure
+  //         out the index of the hotword. For example, if your model string is
+  //         "foo.pmdl,bar.umdl", where foo.pmdl contains hotword x, bar.umdl
+  //         has two hotwords y and z, the indices of different hotwords are as
+  //         follows:
+  //         x 1
+  //         y 2
+  //         z 3
+  //
+  // @param [in]  resource_filename   Filename of resource file.
+  // @param [in]  model_str           A string of multiple hotword models,
+  //                                  separated by comma.
+  SnowboyDetect(const std::string& resource_filename,
+                const std::string& model_str);
+
+  // Resets the detection. This class handles voice activity detection (VAD)
+  // internally. But if you have an external VAD, you should call Reset()
+  // whenever you see segment end from your VAD.
+  bool Reset();
+
+  // Runs hotword detection. Supported audio format is WAVE (with linear PCM,
+  // 8-bits unsigned integer, 16-bits signed integer or 32-bits signed integer).
+  // See SampleRate(), NumChannels() and BitsPerSample() for the required
+  // sampling rate, number of channels and bits per sample values. You are
+  // supposed to provide a small chunk of data (e.g., 0.1 second) each time you
+  // call RunDetection(). Larger chunk usually leads to longer delay, but less
+  // CPU usage.
+  //
+  // Definition of return values:
+  // -2: Silence.
+  // -1: Error.
+  //  0: No event.
+  //  1: Hotword 1 triggered.
+  //  2: Hotword 2 triggered.
+  //  ...
+  //
+  // @param [in]  data               Small chunk of data to be detected. See
+  //                                 above for the supported data format.
+  // @param [in]  is_end             Set it to true if it is the end of a
+  //                                 utterance or file.
+  int RunDetection(const std::string& data, bool is_end = false);
+
+  // Various versions of RunDetection() that take different format of audio. If
+  // NumChannels() > 1, e.g., NumChannels() == 2, then the array is as follows:
+  //
+  //   d1c1, d1c2, d2c1, d2c2, d3c1, d3c2, ..., dNc1, dNc2
+  //
+  // where d1c1 means data point 1 of channel 1.
+  //
+  // @param [in]  data               Small chunk of data to be detected. See
+  //                                 above for the supported data format.
+  // @param [in]  array_length       Length of the data array.
+  // @param [in]  is_end             Set it to true if it is the end of a
+  //                                 utterance or file.
+  int RunDetection(const float* const data,
+                   const int array_length, bool is_end = false);
+  int RunDetection(const int16_t* const data,
+                   const int array_length, bool is_end = false);
+  int RunDetection(const int32_t* const data,
+                   const int array_length, bool is_end = false);
+
+  // Sets the sensitivity string for the loaded hotwords. A <sensitivity_str> is
+  // a list of floating numbers between 0 and 1, and separated by comma. For
+  // example, if there are 3 loaded hotwords, your string should looks something
+  // like this:
+  //   0.4,0.5,0.8
+  // Make sure you properly align the sensitivity value to the corresponding
+  // hotword.
+  void SetSensitivity(const std::string& sensitivity_str);
+
+  // Similar to the sensitivity setting above. When set higher than the above
+  // sensitivity, the algorithm automatically chooses between the normal
+  // sensitivity set above and the higher sensitivity set here, to maximize the
+  // performance. By default, it is not set, which means the algorithm will
+  // stick with the sensitivity set above.
+  void SetHighSensitivity(const std::string& high_sensitivity_str);
+
+  // Returns the sensitivity string for the current hotwords.
+  std::string GetSensitivity() const;
+
+  // Applied a fixed gain to the input audio. In case you have a very weak
+  // microphone, you can use this function to boost input audio level.
+  void SetAudioGain(const float audio_gain);
+
+  // Writes the models to the model filenames specified in <model_str> in the
+  // constructor. This overwrites the original model with the latest parameter
+  // setting. You are supposed to call this function if you have updated the
+  // hotword sensitivities through SetSensitivity(), and you would like to store
+  // those values in the model as the default value.
+  void UpdateModel() const;
+
+  // Returns the number of the loaded hotwords. This helps you to figure the
+  // index of the hotwords.
+  int NumHotwords() const;
+
+  // If <apply_frontend> is true, then apply frontend audio processing;
+  // otherwise turns the audio processing off. Frontend audio processing
+  // includes algorithms such as automatic gain control (AGC), noise suppression
+  // (NS) and so on. Generally adding frontend audio processing helps the
+  // performance, but if the model is not trained with frontend audio
+  // processing, it may decrease the performance. The general rule of thumb is:
+  //   1. For personal models, set it to false.
+  //   2. For universal models, follow the instruction of each published model
+  void ApplyFrontend(const bool apply_frontend);
+
+  // Returns the required sampling rate, number of channels and bits per sample
+  // values for the audio data. You should use this information to set up your
+  // audio capturing interface.
+  int SampleRate() const;
+  int NumChannels() const;
+  int BitsPerSample() const;
+
+  ~SnowboyDetect();
+
+ private:
+  std::unique_ptr<WaveHeader> wave_header_;
+  std::unique_ptr<PipelineDetect> detect_pipeline_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// SnowboyVad class interface.
+//
+////////////////////////////////////////////////////////////////////////////////
+class SnowboyVad {
+ public:
+  // Constructor that takes a resource file. It shares the same resource file
+  // with SnowboyDetect.
+  SnowboyVad(const std::string& resource_filename);
+
+  // Resets the VAD.
+  bool Reset();
+
+  // Runs the VAD algorithm. Supported audio format is WAVE (with linear PCM,
+  // 8-bits unsigned integer, 16-bits signed integer or 32-bits signed integer).
+  // See SampleRate(), NumChannels() and BitsPerSample() for the required
+  // sampling rate, number of channels and bits per sample values. You are
+  // supposed to provide a small chunk of data (e.g., 0.1 second) each time you
+  // call RunDetection(). Larger chunk usually leads to longer delay, but less
+  // CPU usage.
+  //
+  // Definition of return values:
+  // -2: Silence.
+  // -1: Error.
+  //  0: Non-silence.
+  //
+  // @param [in]  data               Small chunk of data to be detected. See
+  //                                 above for the supported data format.
+  // @param [in]  is_end             Set it to true if it is the end of a
+  //                                 utterance or file.
+  int RunVad(const std::string& data, bool is_end = false);
+
+  // Various versions of RunVad() that take different format of audio. If
+  // NumChannels() > 1, e.g., NumChannels() == 2, then the array is as follows:
+  //
+  //   d1c1, d1c2, d2c1, d2c2, d3c1, d3c2, ..., dNc1, dNc2
+  //
+  // where d1c1 means data point 1 of channel 1.
+  //
+  // @param [in]  data               Small chunk of data to be detected. See
+  //                                 above for the supported data format.
+  // @param [in]  array_length       Length of the data array.
+  // @param [in]  is_end             Set it to true if it is the end of a
+  //                                 utterance or file.
+  int RunVad(const float* const data,
+             const int array_length, bool is_end = false);
+  int RunVad(const int16_t* const data,
+             const int array_length, bool is_end = false);
+  int RunVad(const int32_t* const data,
+             const int array_length, bool is_end = false);
+
+  // Applied a fixed gain to the input audio. In case you have a very weak
+  // microphone, you can use this function to boost input audio level.
+  void SetAudioGain(const float audio_gain);
+
+  // If <apply_frontend> is true, then apply frontend audio processing;
+  // otherwise turns the audio processing off.
+  void ApplyFrontend(const bool apply_frontend);
+
+  // Returns the required sampling rate, number of channels and bits per sample
+  // values for the audio data. You should use this information to set up your
+  // audio capturing interface.
+  int SampleRate() const;
+  int NumChannels() const;
+  int BitsPerSample() const;
+
+  ~SnowboyVad();
+
+ private:
+  std::unique_ptr<WaveHeader> wave_header_;
+  std::unique_ptr<PipelineVad> vad_pipeline_;
+};
+
+}  // namespace snowboy
+
+#endif  // SNOWBOY_INCLUDE_SNOWBOY_DETECT_H_

+ 41 - 0
led_test.py

@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+# encoding: utf-8
+
+import RPi.GPIO
+import time
+
+RPi.GPIO.setmode(RPi.GPIO.BCM)
+RPi.GPIO.setup(12, RPi.GPIO.OUT)
+
+# 创建一个 PWM 实例,需要两个参数,第一个是GPIO端口号,这里我们用14号
+# 第二个是频率(Hz),频率越高LED看上去越不会闪烁,相应对CPU要求就越高,设置合适的值就可以
+pwm = RPi.GPIO.PWM(12, 80)
+
+# 启用 PWM,参数是占空比,范围:0.0 <= 占空比 >= 100.0
+pwm.start(0)
+
+try:
+	while True:
+		# 电流从小到大,LED由暗到亮
+		for i in xrange(0, 101, 1):
+			# 更改占空比,
+			pwm.ChangeDutyCycle(i)
+			time.sleep(.02)
+			
+		# 再让电流从大到小,LED由亮变暗
+		for i in xrange(100, -1, -1):
+			pwm.ChangeDutyCycle(i)
+			time.sleep(.02)
+
+# 最后一段是一个小技巧。这个程序如果不强制停止会不停地执行下去。
+# 而Ctrl+C强制终端程序的话,GPIO口又没有机会清理。
+# 加上一个try except 可以捕捉到Ctrl+C强制中断的动作,
+# 试图强制中断时,程序不会马上停止而是会先跳到这里来做一些你想做完的事情,比如清理GPIO口。
+except KeyboardInterrupt:
+	pass
+
+# 停用 PWM
+pwm.stop()
+
+# 清理GPIO口
+RPi.GPIO.cleanup()

BIN
libs/libduer-device.a


BIN
libs/libsnowboy-detect-c.so


+ 1 - 0
profile

@@ -0,0 +1 @@
+{"configures":"{}","bindToken":"deb1241bb7675d1967add507222e20ea","coapPort":443,"token":"pjBZmtZ3QLUUjvyDbNn3b9gpYnTTJWmL","serverAddr":"device.iot.baidu.com","lwm2mPort":443,"uuid":"0d820000000039","rsaCaCrt":"-----BEGIN CERTIFICATE-----\nMIIDUDCCAjgCCQCmVPUErMYmCjANBgkqhkiG9w0BAQUFADBqMQswCQYDVQQGEwJD\nTjETMBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UECgwFYmFpZHUxGDAWBgNVBAMM\nDyouaW90LmJhaWR1LmNvbTEcMBoGCSqGSIb3DQEJARYNaW90QGJhaWR1LmNvbTAe\nFw0xNjAzMTEwMzMwNDlaFw0yNjAzMDkwMzMwNDlaMGoxCzAJBgNVBAYTAkNOMRMw\nEQYDVQQIDApTb21lLVN0YXRlMQ4wDAYDVQQKDAViYWlkdTEYMBYGA1UEAwwPKi5p\nb3QuYmFpZHUuY29tMRwwGgYJKoZIhvcNAQkBFg1pb3RAYmFpZHUuY29tMIIBIjAN\nBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtbhIeiN7pznzuMwsLKQj2xB02+51\nOvCJ5d116ZFLjecp9qtllqOfN7bm+AJa5N2aAHJtsetcTHMitY4dtGmOpw4dlGqx\nluoz50kWJWQjVR+z6DLPnGE4uELOS8vbKHUoYPPQTT80eNVnl9S9h/l7DcjEAJYC\nIYJbf6+K9x+Ti9VRChvWcvgZQHMRym9j1g/7CKGMCIwkC+6ihkGD/XG40r7KRCyH\nbD53KnBjBO9FH4IL3rGlZWKWzMw3zC6RTS2ekfEsgAtYDvROKd4rNs+uDU9xaBLO\ndXTl5uxgudH2VnVzWtj09OUbBtXcQFD2IhmOl20BrckYul+HEIMR0oDibwIDAQAB\nMA0GCSqGSIb3DQEBBQUAA4IBAQCzTTH91jNh/uYBEFekSVNg1h1kPSujlwEDDf/W\npjqPJPqrZvW0w0cmYsYibNDy985JB87MJMfJVESG/v0Y/YbvcnRoi5gAenWXQNL4\nh2hf08A5wEQfLO/EaD1GTH3OIierKYZ6GItGrz4uFKHV5fTMiflABCdu37ALGjrA\nrIjwjxQG6WwLr9468hkKrWNG3dMBHKvmqO8x42sZOFRJMkqBbKzaBd1uW4xY5XwM\nS1QX56tVrgO0A3S+4dEg5uiLVN4YVP/Vqh4SMtYkL7ZZiZAxD9GtNnhRyFsWlC2r\nOVSdXs1ttZxEaEBGUl7tgsBte556BIvufZX+BXGyycVJdBu3\n-----END CERTIFICATE-----\n","macId":"","version":4424}

BIN
resources/16.mp3


BIN
resources/60.mp3


BIN
resources/61.mp3


BIN
resources/62.mp3


BIN
resources/common.res


BIN
resources/left.mp3


Some files were not shown because too many files changed in this diff