mg app
authorkyz <kongyuzhong@gmail.com>
Sat, 6 May 2023 02:38:20 +0000 (10:38 +0800)
committerkyz <kongyuzhong@gmail.com>
Tue, 9 May 2023 15:52:32 +0000 (23:52 +0800)
17 files changed:
.gitignore [new file with mode: 0644]
.gitmodules [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0644]
app/CMakeLists.txt [new file with mode: 0644]
app/inc/cQueue.h [new file with mode: 0644]
app/inc/dp.h [new file with mode: 0644]
app/inc/tuya_cacert.h [new file with mode: 0644]
app/src/cQueue.c [new file with mode: 0644]
app/src/dp.c [new file with mode: 0644]
app/src/main.c [new file with mode: 0644]
button/CMakeLists.txt [new file with mode: 0644]
button/inc/button.h [new file with mode: 0644]
button/src/button.c [new file with mode: 0644]
led/CMakeLists.txt [new file with mode: 0644]
led/inc/led.h [new file with mode: 0644]
led/src/led.c [new file with mode: 0644]
tuya-iot-core-sdk [new submodule]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..b4aec0b
--- /dev/null
@@ -0,0 +1,6 @@
+build/
+*.o
+~*
+tags
+cscope.*
+
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..e889e3e
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "tuya-iot-core-sdk"]
+       path = tuya-iot-core-sdk
+       url = https://github.com/tuya/tuya-iot-core-sdk.git
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b97a105
--- /dev/null
@@ -0,0 +1,5 @@
+# Add the posix targets
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/app )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/button )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/led )
+add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tuya-iot-core-sdk )
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d9c3569
--- /dev/null
@@ -0,0 +1,38 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_app)
+
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${PROJECT_SOURCE_DIR}/../tuya-iot-core-sdk/include/
+       ${PROJECT_SOURCE_DIR}/../tuya/iot-core-sdk/interface/
+       ${PROJECT_SOURCE_DIR}/../tuya/iot-core-sdk/middleware/
+       ${PROJECT_SOURCE_DIR}/../tuya/iot-core-sdk/utils/
+       ${PROJECT_SOURCE_DIR}/../button/inc/
+       ${PROJECT_SOURCE_DIR}/../led/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+link_directories(
+       ${CMAKE_SYSROOT}/app/lib
+       ${PROJECT_SOURCE_DIR}/../tuya-iot-core-sdk
+       ${PROJECT_SOURCE_DIR}/. )
+
+aux_source_directory(./src/ SRCS)
+
+add_executable(${PROJECT_NAME} ${SRCS})
+target_link_libraries(
+       ${PROJECT_NAME}
+       PUBLIC
+        link_core
+        utils_modules
+        middleware_implementation
+        platform_port
+       mg_button
+       mg_led
+       pthread )
+
+install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app )
+
diff --git a/app/inc/cQueue.h b/app/inc/cQueue.h
new file mode 100644 (file)
index 0000000..413c296
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef cQUEUE_H
+#define cQUEUE_H
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#define cQueue_API_DECL extern
+
+/*------------------------------------------------------------------------------------------------*/
+typedef struct cQueue{          /* Queue control block  */    
+    void            **QMemory;  /* pointer to message queue storage area                          */                                   
+    void            **QStart;   /* pointer to start of queue data                                 */
+    void            **QEnd;     /* pointer to end   of queue data                                 */
+    void            **QIn;      /* pointer to where next message will be inserted in the queue    */
+    void            **QOut;     /* pointer to where next message will be extracted from the queue */
+    int             QSize;      /* size of queue -- max number of entries                         */
+    int             QEntries;   /* current number of entries in the queue                         */
+} cQueue_t;
+
+#define ERR_Q_NONE      0
+#define ERR_Q_FULL      1
+#define ERR_Q_EMPTY     2
+
+cQueue_API_DECL cQueue_t *cQcreate(int size);
+cQueue_API_DECL void cQDelete(cQueue_t *pq);
+cQueue_API_DECL void cQFlush(cQueue_t *pq);
+cQueue_API_DECL void *cQRcv(cQueue_t *pq, char *perr);
+cQueue_API_DECL int cQPost(cQueue_t *pq, void *pmsg);
+cQueue_API_DECL int cQPostFront(cQueue_t *pq, void *pmsg);
+
+/*--------------------------------------------------------*/
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/app/inc/dp.h b/app/inc/dp.h
new file mode 100644 (file)
index 0000000..f5e1b10
--- /dev/null
@@ -0,0 +1,98 @@
+#ifndef __MG_DP_H__
+#define __MG_DP_H__
+
+typedef enum {
+       DP_BTN_1 = 100,
+       DP_BTN_2 = 101,
+       DP_BTN_3 = 102,
+       DP_BTN_4 = 103,
+       DP_BTN_5 = 104,
+       DP_BTN_6 = 105,
+       DP_BTN_7 = 106,
+       DP_BTN_8 = 107,
+       DP_BTN_MAX,
+}dp_button_code_t;
+
+typedef enum{
+       DP_SINGLE_CLICK   = 0,
+       DP_DOUBLE_CLICK   = 1,
+       DP_LONG_PRESSE    = 2,
+       DP_LONG_PRESSE_UP = 3,
+       DP_BTN_EV_MAX,
+}dp_button_event_t;
+
+#if XXX
+
+typedef struct{
+       uint32_t id;
+       char name[32];
+       uint32_t value_type; //0:int, 1:string
+       uint32_t value_int;
+       char* value_str;
+       uint32_t ver;
+}point_t;
+
+typedef struct{
+       //101   按键1 事件  button_1
+       //102   按键2 事件  button_2
+       //103   按键3 事件  button_3
+       //104   按键4 事件  button_4
+       //105   按键5 事件  button_5
+       //106   按键6 事件  button_6
+       //107   按键7 事件  button_7
+       //108   按键8 事件  button_8
+       point_t button[8];
+
+       //109   按键背光最大亮度 属性 key_led_max     数值型(Value)    可下发可上报(rw)      数值范围: 0-100, 间距: 1, 倍数: 0, 单位: %    默认100%      
+       point_t key_led_max;
+       //110   氛围灯最大亮度 属性    atmos_led_max   数值型(Value)    可下发可上报(rw)      数值范围: 0-100, 间距: 1, 倍数: 0, 单位: %    默认100%
+       point_t atmos_led_max;
+       //111   手势开关状态 属性       recognition_running     布尔型(Bool)     只上报(ro)
+       point_t recognition_running;
+       //112   触发手势 事件     gesture_event   -       -       -
+       point_t gesture_event;
+       //113   手势识别补光 属性       recognition_mode        枚举型(Enum)     可下发可上报(rw)      枚举值: light_mode, dark_mode        环境光亮模式, 环境光暗模式  编辑 删除
+       point_t recognition_mode;
+       //114   手势识别灵敏度 属性    recognition_level       数值型(Value)    可下发可上报(rw)      数值范围: 1-10, 间距: 1, 倍数: 0, 单位:       默认6, 1档最不灵敏, 10档最灵敏       
+       point_t recognition_level;
+       //115   手势识别关闭延时 属性 recognition_duration    数值型(Value)    可下发可上报(rw)      数值范围: 5-1000, 间距: 1, 倍数: 0, 单位: 秒 默认300, 5分钟      
+       point_t recognition_duration;
+       //116   隐私模式 属性     privacy_mode    布尔型(Bool)     只上报(ro)       -       打开时关闭摄像头, 关闭手势识别功能, 但按键依然有效       
+       point_t privacy_mode;
+       //117   485指令 动作        485_cmd -       -       -               
+       point_t _485_cmd;
+       //120   active 属性   active  布尔型(Bool)     只上报(ro)       -               
+       point_t active;
+       //121   key 属性      key     字符型(String)   可下发可上报(rw)      -               
+       point_t key;
+       //151   氛围灯打开时间 属性    led_atmos_on_duration   数值型(Value)    可下发可上报(rw)      数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒   默认5秒      
+       point_t led_atmos_on_duration;
+       //152   人感检测距离 属性       mg_tof_threshold        数值型(Value)    可下发可上报(rw)      数值范围: 10-150, 间距: 1, 倍数: 0, 单位: 厘米      默认150       
+       point_t mg_tof_threshold;
+       //153   人感检测周期 属性       mg_tof_interval 数值型(Value)    可下发可上报(rw)      数值范围: 10-20, 间距: 1, 倍数: 0, 单位: 秒  默认10秒     
+       point_t mg_tof_interval;
+       //154   关闭联排功能 属性       mg_disalbe_row  布尔型(Bool)     可下发可上报(rw)      -               
+       point_t mg_disalbe_row;
+       //155   按键禁使能 属性  mg_key_enable   透传型(Raw)      可下发可上报(rw)      -       默认FF, 一个字节, bit0 是第一个按键, bit7是第八个按键 
+       point_t mg_key_enable;
+       //156   背光模式 属性     bl_mode 枚举型(Enum)     可下发可上报(rw)      枚举值: auto, manual 默认"自动",  自动模式: 氛围灯亮, 背光灭, 有人后反转.   手动模式: 氛围灯可调, 背光亮度可调, 两者无关联   
+       point_t bl_mode;
+       //157   人感状态 属性     mg_tof_status   布尔型(Bool)     可下发可上报(rw)      -               
+       point_t mg_tof_status;
+       //158   人感状态同步 属性       mg_tof_sync     布尔型(Bool)     只下发(wr)       -               
+       point_t mg_tof_sync;
+       //159   氛围灯关闭时间 属性    led_atmos_off_duration  数值型(Value)    可下发可上报(rw)      数值范围: 1-20, 间距: 1, 倍数: 0, 单位: 秒   默认5秒
+       point_t led_atmos_off_duration;
+} mg_dp_t;
+
+extern mg_dp_t g_mg_dp_remote;
+extern mg_dp_t g_mg_dp_local;
+
+g_mg_dp_local = {
+       .key_led_max=  { 109, "key_led_max",    },
+       .atoms_led_max={ 110, "atmos_led_max",  },
+};
+
+#endif
+
+#endif //__MG_DP_H__
diff --git a/app/inc/tuya_cacert.h b/app/inc/tuya_cacert.h
new file mode 100644 (file)
index 0000000..a62a98d
--- /dev/null
@@ -0,0 +1,25 @@
+\r
+const char tuya_cacert_pem[] = {\\r
+"-----BEGIN CERTIFICATE-----\n"\\r
+"MIIDxTCCAq2gAwIBAgIBADANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMCVVMx\n"\\r
+"EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxGjAYBgNVBAoT\n"\\r
+"EUdvRGFkZHkuY29tLCBJbmMuMTEwLwYDVQQDEyhHbyBEYWRkeSBSb290IENlcnRp\n"\\r
+"ZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTA5MDkwMTAwMDAwMFoXDTM3MTIzMTIz\n"\\r
+"NTk1OVowgYMxCzAJBgNVBAYTAlVTMRAwDgYDVQQIEwdBcml6b25hMRMwEQYDVQQH\n"\\r
+"EwpTY290dHNkYWxlMRowGAYDVQQKExFHb0RhZGR5LmNvbSwgSW5jLjExMC8GA1UE\n"\\r
+"AxMoR28gRGFkZHkgUm9vdCBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkgLSBHMjCCASIw\n"\\r
+"DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL9xYgjx+lk09xvJGKP3gElY6SKD\n"\\r
+"E6bFIEMBO4Tx5oVJnyfq9oQbTqC023CYxzIBsQU+B07u9PpPL1kwIuerGVZr4oAH\n"\\r
+"/PMWdYA5UXvl+TW2dE6pjYIT5LY/qQOD+qK+ihVqf94Lw7YZFAXK6sOoBJQ7Rnwy\n"\\r
+"DfMAZiLIjWltNowRGLfTshxgtDj6AozO091GB94KPutdfMh8+7ArU6SSYmlRJQVh\n"\\r
+"GkSBjCypQ5Yj36w6gZoOKcUcqeldHraenjAKOc7xiID7S13MMuyFYkMlNAJWJwGR\n"\\r
+"tDtwKj9useiciAF9n9T521NtYJ2/LOdYq7hfRvzOxBsDPAnrSTFcaUaz4EcCAwEA\n"\\r
+"AaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwHQYDVR0OBBYE\n"\\r
+"FDqahQcQZyi27/a9BUFuIMGU2g/eMA0GCSqGSIb3DQEBCwUAA4IBAQCZ21151fmX\n"\\r
+"WWcDYfF+OwYxdS2hII5PZYe096acvNjpL9DbWu7PdIxztDhC2gV7+AJ1uP2lsdeu\n"\\r
+"9tfeE8tTEH6KRtGX+rcuKxGrkLAngPnon1rpN5+r5N9ss4UXnT3ZJE95kTXWXwTr\n"\\r
+"gIOrmgIttRD02JDHBHNA7XIloKmf7J6raBKZV8aPEjoJpL1E/QYVN8Gb5DKj7Tjo\n"\\r
+"2GTzLH4U/ALqn83/B2gX2yKQOC16jdFU8WnjXzPKej17CuPKf1855eJ1usV2GDPO\n"\\r
+"LPAvTK33sefOT6jEm0pUBsV/fdUID+Ic/n4XuKxe9tQWskMJDE32p2u0mYRlynqI\n"\\r
+"4uJEvlz36hz1\n"\\r
+"-----END CERTIFICATE-----\n"};\r
diff --git a/app/src/cQueue.c b/app/src/cQueue.c
new file mode 100644 (file)
index 0000000..53131bf
--- /dev/null
@@ -0,0 +1,143 @@
+#include <stdlib.h>
+#include <ctype.h>
+#include "cQueue.h"
+
+
+/*
+ * Description  :   create and init the queue.
+ *
+ * @para[in]    :   start   is a pointer to the base address of the message queue 
+ *                          storage area,the stroage area MUST be declare as an array
+ *                          of pointers to 'void' as follows
+ * 
+ *                          void *messageStroage[size]
+ *                            
+ * @para[in]    :   size    is the number of elements in the storage area
+ * 
+ * @return      :   the pointer to the new queue
+ */
+cQueue_t *cQcreate(int size)
+{
+    cQueue_t *pq = NULL;
+    pq = (cQueue_t *)malloc(sizeof(cQueue_t));
+    if(pq != NULL){             /*      Initialize the queue    */
+        pq->QMemory = (void **)malloc(size*sizeof(void *));
+        pq->QStart      = pq->QMemory;
+        pq->QEnd        = &pq->QMemory[size];
+        pq->QIn         = pq->QMemory;
+        pq->QOut        = pq->QMemory;
+        pq->QSize       = size;
+        pq->QEntries    = 0;
+    }
+    return pq;
+}
+
+/*
+ * Description  :   delete the queue, and free the stroage area
+ *
+ * @para[in]    :   pq      is the pointer to the queue
+ *
+ * @return      :   NULL
+ */
+void cQDelete(cQueue_t *pq)
+{
+       free(pq->QMemory);
+    free(pq);
+}
+
+/*
+ * Description  :   flush the queue, the queue is like the new create state
+ *
+ * @para[in]    :   pq      is the pointer to the queue
+ *
+ * @return      :   NULL
+ */
+void cQFlush(cQueue_t *pq)
+{
+    pq->QIn = pq->QStart;
+    pq->QOut = pq->QStart;
+    pq->QEntries = 0;
+}
+
+
+/*
+ * Description  :   receive msg from queue, if queue is empty,return NULL
+ *
+ * @para[in]    :   pq      is the pointer to the queue
+ *
+ * @para[out]   :   perr    is the pointer to error return
+ * 
+ * @return      : the pointer to the receive msg.
+ */
+void *cQRcv(cQueue_t *pq, char *perr)
+{
+    void *pmsg = NULL;
+    
+    if(pq->QEntries > 0){                   /* See if any messages in the queue                   */
+       pmsg = *pq->QOut++;                  /* Yes, extract oldest message from the queue         */
+        pq->QEntries--;                     /* Update the number of entries in the queue          */
+        if(pq->QOut == pq->QEnd){           /* Wrap OUT pointer if we are at the end of the queue */
+            pq->QOut = pq->QStart;
+        }    
+        *perr = ERR_Q_NONE;
+    }else{
+        *perr = ERR_Q_EMPTY;                /* Queue is empty                                     */
+    }
+
+    return (pmsg);
+}
+
+/*
+ * Description  :   send a message to the queue
+ *
+ * @para[in]    :   pq      is the pointer to the queue
+ *
+ * @para[in]    :   pmsg    is the pointer to new message to send
+ * 
+ * @return      :   ERR_Q_NONE      The call was successful and the message was sent
+ *                  ERR_Q_FULL      If the queue cannot accept any more messages because it is full.
+ */
+int cQPost(cQueue_t *pq, void *pmsg)
+{
+    if(pq->QEntries >= pq->QSize){             /* Make sure queue is not full                   */
+        return ERR_Q_FULL;
+    }
+       
+    *pq->QIn++ = pmsg;
+    
+       pq->QEntries++;
+ 
+    if(pq->QIn == pq->QEnd){
+        pq->QIn = pq->QStart;
+    }
+   
+    return ERR_Q_NONE;
+}
+
+/*
+ * Description  :   send a message to the queue,the message is posted at the FRONT instead of
+ *                  the end of the queue. Using cQPostFront() alllows you to send 'priority'
+ *                  message.
+ *
+ * @para[in]    :   pq      is the pointer to the queue
+ *
+ * @para[in]    :   pmsg    is the pointer to new message to send
+ * 
+ * @return      :   ERR_Q_NONE      The call was successful and the message was sent
+ *                  ERR_Q_FULL      If the queue cannot accept any more messages because it is full.
+ */
+int cQPostFront(cQueue_t *pq, void *pmsg)
+{      
+    if(pq->QEntries >= pq->QSize){      /* Make sure queue is not full                   */
+        return ERR_Q_FULL;
+    }
+
+    if(pq->QOut == pq->QStart){         /* Wrap OUT ptr if we are at the 1st queue entry */
+        pq->QOut = pq->QEnd;
+    }
+
+    pq->QOut--;                         
+    *pq->QOut = pmsg;                   /* Insert message into queue                     */
+    pq->QEntries++;                     /* Update the nbr of entries in the queue        */
+    return ERR_Q_NONE;
+}
diff --git a/app/src/dp.c b/app/src/dp.c
new file mode 100644 (file)
index 0000000..d82c9a6
--- /dev/null
@@ -0,0 +1,228 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "cJSON.h"
+#include "tuya_cacert.h"
+#include "tuya_log.h"
+#include "tuya_error_code.h"
+#include "system_interface.h"
+#include "mqtt_client_interface.h"
+#include "tuyalink_core.h"
+
+#include "cQueue.h" 
+#include "dp.h" 
+
+#if 0
+const char productId[] = "gmabzdoevsvl****";
+const char deviceId[] = "6cc87b39279b6fb754****";
+const char deviceSecret[] = "ffad8eb31ae8****";
+#else
+//ProductID:nyitvn2sp9sccl40
+//DeviceID:2667ab8ddf0e29a4aevack
+//DeviceSecret:dTCB1BfAfAsBSZ1p
+
+//const char productId[] = "nyitvn2sp9sccl40";
+//const char deviceId[] = "2667ab8ddf0e29a4aevack";
+//const char deviceSecret[] = "dTCB1BfAfAsBSZ1p";
+
+//ProductID:3xeyrwyvbuyajz4a
+//DeviceID:26dfc49b45e25a93dcwgzg
+//DeviceSecret:IAS0hHa3mmgrKn9L
+
+const char productId[] = "3xeyrwyvbuyajz4a";
+const char deviceId[] = "26dfc49b45e25a93dcwgzg";
+const char deviceSecret[] = "IAS0hHa3mmgrKn9L";
+
+#endif
+
+tuya_mqtt_context_t client_instance;
+
+void on_connected(tuya_mqtt_context_t* context, void* user_data)
+{
+    TY_LOGI("on connected");
+
+    /* data model test code */
+    tuyalink_thing_data_model_get(context, NULL);
+    tuyalink_thing_desired_get(context, NULL, "[\"power\"]");
+    tuyalink_thing_property_report(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
+    tuyalink_thing_property_report_with_ack(context, NULL, "{\"power\":{\"value\":1234,\"time\":1631708204231}}");
+    tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"boom\",\"eventTime\":1626197189630,\"outputParams\":{\"param1\":100}}");
+    tuyalink_thing_batch_report(context, "{\"msgId\":\"45lkj3551234001\",\"time\":1626197189638,\"sys\":{\"ack\":1},\"data\":{\"properties\":{\"power\":{\"value\":11,\"time\":1626197189638}},\"events\":{\"boom\":{\"outputParams\":{\"param1\":\"10\"},\"eventTime\":1626197189001}}}}");
+
+
+    tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":88}}");
+    tuyalink_thing_property_report(context, NULL, "{\"atmos_led_max\":{\"value\":73}}");
+}
+
+void on_disconnect(tuya_mqtt_context_t* context, void* user_data)
+{
+    TY_LOGI("on disconnect");
+}
+
+void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_message_t* msg)
+{
+    TY_LOGI("on message id:%s, type:%d, code:%d", msg->msgid, msg->type, msg->code);
+    switch (msg->type) {
+        case THING_TYPE_MODEL_RSP:
+            TY_LOGI("Model data:%s", msg->data_string);
+            break;
+
+        case THING_TYPE_PROPERTY_SET:
+            TY_LOGI("property set:%s", msg->data_string);
+           tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":35}}");
+           //tuyalink_thing_event_trigger(context, NULL,   "{\"eventCode\":\"button_1\", \"outputParams\": {\
+                        \"button_1\":\"long_press\", \"btn_100\":\"click\" } }" );
+           tuyalink_thing_event_trigger(context, NULL,   "{\"eventCode\":\"button_3\", \"outputParams\": {\
+                        \"button_3\":\"single_click\" } }" );
+            break;
+
+        case THING_TYPE_PROPERTY_REPORT_RSP:
+            TY_LOGI("THING_TYPE_PROPERTY_REPORT_RSP" );
+            break;
+
+        default:
+            break;
+    }
+    printf("\r\n");
+}
+
+int dp_demo( void )
+{
+    int ret = OPRT_OK;
+
+    tuya_mqtt_context_t* client = &client_instance;
+
+    ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) {
+        .host = "m1.tuyacn.com",
+        .port = 8883,
+        .cacert = tuya_cacert_pem,
+        .cacert_len = sizeof(tuya_cacert_pem),
+        .device_id = deviceId,
+        .device_secret = deviceSecret,
+        .keepalive = 100,
+        .timeout_ms = 2000/20,
+        .on_connected = on_connected,
+        .on_disconnect = on_disconnect,
+        .on_messages = on_messages
+    });
+    assert(ret == OPRT_OK);
+
+    ret = tuya_mqtt_connect(client);
+    assert(ret == OPRT_OK);
+
+    for (;;) {
+        /* Loop to receive packets, and handles client keepalive */
+        tuya_mqtt_loop(client);
+    }
+
+    return ret;
+}
+
+cQueue_t *g_dp_event_q;
+
+typedef struct{
+       uint32_t value;
+} dp_event_t;
+
+
+char * button_code_name[]={
+       "button_1",
+       "button_2",
+       "button_3",
+       "button_4",
+       "button_5",
+       "button_6",
+       "button_7",
+       "button_8",
+};
+
+char * button_event_name[]={
+       "single_click",
+       "double_click",
+       "long_press",
+       "long_press_up"
+};
+
+//code : 1 -- 8  ->button_1 .. button8
+//event: 1 -- single_click  2--double_click  3--long_press 4--long_press_up
+#define btn_event_fmt "{\"eventCode\":\"%s\",\"outputParams\":{\"%s\":\"%s\" } }" 
+int32_t dp_button_event_push( dp_button_code_t btn_code, dp_button_event_t btn_event  )
+{
+       char msg_buf[100];
+       sprintf( msg_buf, btn_event_fmt, button_code_name[ btn_code ],  button_code_name[ btn_code ], button_event_name[btn_event] ); 
+
+       void *msg_str = strdup( msg_buf );
+       cQPost( g_dp_event_q, ( void *)msg_str );
+       //send wake event
+       return 0;
+}
+
+char* dp_event_pop( void )
+{
+       char *msg;
+       char err;
+
+       msg  = cQRcv( g_dp_event_q, &err );
+
+       return msg;
+}
+
+void* tuya_task( void * unused )
+{
+       char *msg;
+       int ret = OPRT_OK;
+       g_dp_event_q = cQcreate( 100 ); 
+
+       tuya_mqtt_context_t* client = &client_instance;
+
+       ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) {
+                       .host = "m1.tuyacn.com",
+                       .port = 8883,
+                       .cacert = tuya_cacert_pem,
+                       .cacert_len = sizeof(tuya_cacert_pem),
+                       .device_id = deviceId,
+                       .device_secret = deviceSecret,
+                       .keepalive = 100,
+                       .timeout_ms = 2000/10,
+                       .on_connected = on_connected,
+                       .on_disconnect = on_disconnect,
+                       .on_messages = on_messages
+                       });
+
+       assert(ret == OPRT_OK);
+
+       ret = tuya_mqtt_connect(client);
+       assert(ret == OPRT_OK);
+
+       for (;;) {
+               msg = dp_event_pop();
+               if( msg ){
+
+                       tuyalink_thing_event_trigger(client, NULL, msg );               
+                       free( msg );
+               }
+               /* Loop to receive packets, and handles client keepalive */
+               tuya_mqtt_loop(client);
+       }
+
+       return NULL;
+}
+
+static pthread_t tuya_thread;
+static pthread_attr_t  attr;
+
+void tuya_start( void )
+{
+       if( pthread_create( &tuya_thread, NULL, tuya_task, NULL ) != 0 ){
+               printf( "tuya task create fail!\n" );
+               goto error;
+       }
+
+       return;
+
+error:
+       return;
+
+}
diff --git a/app/src/main.c b/app/src/main.c
new file mode 100644 (file)
index 0000000..d98816c
--- /dev/null
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <unistd.h>
+
+#include "button.h"
+#include "led.h"
+#include "dp.h"
+
+extern int dp_demo( void );
+
+
+extern int32_t dp_button_event_push( button_code_t btn_code, button_event_t btn_event  );
+
+void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value )
+{
+       uint32_t dp_btn_code = 0;
+       switch( btn_code ){
+               case BUTTON_1:
+                       dp_btn_code = DP_BTN_1;
+                       break;
+               case BUTTON_2:
+                       dp_btn_code = DP_BTN_2;
+                       break;
+               case BUTTON_3:
+                       dp_btn_code = DP_BTN_3;
+                       break;
+               case BUTTON_4:
+                       dp_btn_code = DP_BTN_4;
+                       break;
+               case BUTTON_5:
+                       dp_btn_code = DP_BTN_5;
+                       break;
+               case BUTTON_6:
+                       dp_btn_code = DP_BTN_6;
+                       break;
+               case BUTTON_7:
+                       dp_btn_code = DP_BTN_7;
+                       break;
+               case BUTTON_8:
+                       dp_btn_code = DP_BTN_8;
+                       break;
+               case BUTTON_7|BUTTON_8:
+                       break;
+       }
+       if( btn_event == EVENT_SINGLE_CLICK ){
+               printf( "key KEY_%d Released!\n", dp_btn_code-DP_BTN_1+1 );
+               dp_button_event_push( dp_btn_code, DP_SINGLE_CLICK  );
+               //TODO: add led ctrl
+       }
+       return;
+}
+
+void tuya_start( void );
+
+int main( void )
+{
+       mg_button_t *button;
+
+       printf( "\n-----------------------------------------\n");
+       button = mg_button_open("/dev/input/by-path/platform-gpio_keys-event" );
+       if( button ){
+               printf( "register cb, button:%p, cb:%p\n", button,  button_single_clicked_cb );
+               button_single_clicked_register( button, button_single_clicked_cb );
+       }
+       printf( "\n-----------------------------------------\n");
+       led_open();
+       printf( "\n-----------------------------------------\n");
+       printf( "\ntuya sdk start...\n" );
+       tuya_start();
+       printf( "\n-----------------------------------------\n");
+       while(1){
+               sleep(1000);
+       }
+       return 0;
+}
+
diff --git a/button/CMakeLists.txt b/button/CMakeLists.txt
new file mode 100644 (file)
index 0000000..935139a
--- /dev/null
@@ -0,0 +1,18 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_button)
+
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+aux_source_directory(./src/ SRCS)
+
+add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+#add_library( ${PROJECT_NAME}  ${SRCS} )
+#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
diff --git a/button/inc/button.h b/button/inc/button.h
new file mode 100644 (file)
index 0000000..29e801d
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef __BUTTON__H__
+#define __BUTTON__H__
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <linux/input.h>
+#include <pthread.h>
+
+
+typedef enum{
+       BUTTON_1 = (1<<0),
+       BUTTON_2 = (1<<1),
+       BUTTON_3 = (1<<2),
+       BUTTON_4 = (1<<3),
+       BUTTON_5 = (1<<4),
+       BUTTON_6 = (1<<5),
+       BUTTON_7 = (1<<6),
+       BUTTON_8 = (1<<7),
+       BUTTON_ALL = 0xff,
+}button_code_t;
+
+typedef enum{
+       EVENT_SINGLE_CLICK  = (1<<0),
+       EVENT_DOUBLE_CLICK  = (1<<1),
+       EVENT_LONG_PRESS    = (1<<2),
+       EVENT_LONG_PRESS_UP = (1<<3),
+       EVENT_COMB_PRESS    = (1<<4),
+}button_event_t;
+
+struct mg_button;
+typedef struct mg_button mg_button_t;
+
+typedef void ( * event_cb_t )( button_code_t code, button_event_t evt, void * value );
+
+mg_button_t * mg_button_open( char * button_device_path );
+void mg_button_close( mg_button_t * btn_ctrl );
+int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, event_cb_t cb );
+int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, event_cb_t cb );
+
+//--------------------------------------------
+//for test 
+void button_single_clicked_register( mg_button_t *btn, event_cb_t cb );
+//--------------------------------------------
+
+#endif //__BUTTON__H__
+
diff --git a/button/src/button.c b/button/src/button.c
new file mode 100644 (file)
index 0000000..9a82a4b
--- /dev/null
@@ -0,0 +1,185 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+//#include <linux/input.h>
+//#include <pthread.h>
+#include "button.h"
+
+
+struct mg_button{
+       char dev_path[100];
+       int32_t fd;
+       //todo
+       int32_t lock;
+       pthread_t btn_thread;
+};
+
+mg_button_t *g_ptr_button;
+
+mg_button_t * mg_button_open( char * button_device_path );
+void mg_button_close( mg_button_t * btn_ctrl );
+
+static event_cb_t g_test_cb = NULL;
+
+volatile int32_t stop = 0;
+
+static char *  button_device_def ="/dev/input/event2";
+
+
+void button_single_clicked_register( mg_button_t *btn, event_cb_t cb );
+static void* button_task( void* param );
+
+uint32_t button_code( uint32_t key_code )
+{
+       if( key_code == KEY_1 )      return BUTTON_1;
+       else if( key_code == KEY_2 ) return BUTTON_2;
+       else if( key_code == KEY_3 ) return BUTTON_3;
+       else if( key_code == KEY_4 ) return BUTTON_4;
+       else if( key_code == KEY_5 ) return BUTTON_5;
+       else if( key_code == KEY_6 ) return BUTTON_6;
+       else if( key_code == KEY_7 ) return BUTTON_7;
+       else if( key_code == KEY_8 ) return BUTTON_8;
+       return 0;
+}
+
+void* button_task( void* param )
+{
+       mg_button_t *p_button;
+       struct input_event ev[64];
+       int i, rd;
+       fd_set rdfs;
+       int32_t fd;
+
+       p_button = ( mg_button_t*)param;
+       if( !p_button ){
+               goto error;
+       }
+       fd = p_button->fd;
+
+       FD_ZERO(&rdfs);
+       FD_SET(fd, &rdfs);
+
+       while (!stop) {
+               select(fd + 1, &rdfs, NULL, NULL, NULL);
+               if (stop)
+                       break;
+               rd = read(fd, ev, sizeof(ev));
+
+               if (rd < (int) sizeof(struct input_event)) {
+                       printf("expected %d bytes, got %d\n", (int) sizeof(struct input_event), rd);
+                       perror("\nevtest: error reading");
+                       return NULL;
+               }
+
+               for (i = 0; i < rd / sizeof(struct input_event); i++) {
+                       unsigned int type, code;
+
+                       type = ev[i].type;
+                       code = ev[i].code;
+                       
+                       //printf("++++Event: time %ld.%06ld, type:%d", ev[i].input_event_sec, ev[i].input_event_usec, type );
+                       printf("++++Event: ype:%d", type );
+                       
+                       if( type==EV_KEY ){
+                                       if(g_test_cb){
+                                               if( ev[i].value==0){
+                                                       g_test_cb( button_code(code), EVENT_SINGLE_CLICK, 0 );
+                                               }else if( ev[i].value==1){
+                                                       //g_test_cb( button_code(code), EVENT_SINGLE_CLICK, 0 );
+                                               }else if( ev[i].value==2){
+                                                       //ignore key repeate event
+                                               }
+                                       }
+
+                               printf( "EV_KEY :  KEY_%d, value:%d\n", code - KEY_1 +1, ev[i].value );
+                               continue;
+                       }
+               }
+       }
+
+       ioctl(fd, EVIOCGRAB, (void*)0);
+error:
+       return NULL;
+}
+
+
+
+mg_button_t * mg_button_open( char * button_device_path )
+{
+       int32_t fd;
+       pthread_t btn_thread;
+
+       mg_button_t *button = g_ptr_button;     
+
+       if( button ){
+               goto error;
+       }
+
+       button = ( mg_button_t *)malloc( sizeof( mg_button_t ) );
+       if( !button ){
+               goto error;
+       }
+
+       if( !button_device_path)
+               button_device_path = button_device_def;
+
+       strcpy( button->dev_path, button_device_path );
+
+       if ((fd = open(button->dev_path, O_RDONLY)) < 0) {
+               perror("button open");
+               fprintf(stderr, "open dev[%s] fail!\n", button->dev_path);
+               goto error;
+       }
+       
+       if( pthread_create( &btn_thread, NULL, button_task, (void *)button ) != 0 ){
+               perror("create button task!");
+               goto error;
+       }
+
+       button->fd = fd;
+       button->btn_thread = btn_thread;
+       g_ptr_button = button;
+
+       return button;
+error:
+       return NULL;
+}
+
+void mg_button_close( mg_button_t * btn_ctrl )
+{
+       if( btn_ctrl ){
+               if( g_ptr_button == btn_ctrl ){
+                       g_ptr_button = NULL;
+               }else{
+                       printf ( "err, button:%p, g_button:%p!\n", btn_ctrl, g_ptr_button );
+               }
+               free( btn_ctrl );
+       }
+       printf ( "tw8013 button close!\n" );
+}
+
+int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, event_cb_t cb )
+{
+       //TODO:增加事件处理函数管理
+       return 0;
+}
+int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, event_cb_t cb )
+{
+       //TODO:增加事件处理函数管理
+       return 0;
+}
+
+//临时测试用,单击按钮放开时调用
+void button_single_clicked_register( mg_button_t *btn, event_cb_t cb )
+{
+       g_test_cb = cb;
+       printf( "button regist cb: %p\n", g_test_cb );
+}
+
diff --git a/led/CMakeLists.txt b/led/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d193ce5
--- /dev/null
@@ -0,0 +1,18 @@
+cmake_minimum_required(VERSION 3.8)
+
+project(mg_led)
+
+include_directories(
+       ${PROJECT_SOURCE_DIR}/./
+       ${PROJECT_SOURCE_DIR}/src/
+       ${PROJECT_SOURCE_DIR}/inc/
+       ${CMAKE_SYSROOT}/usr/include/
+       )
+
+aux_source_directory(./src/ SRCS)
+
+add_library( ${PROJECT_NAME} STATIC  ${SRCS} )
+#add_library( ${PROJECT_NAME}  ${SRCS} )
+#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
+#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
diff --git a/led/inc/led.h b/led/inc/led.h
new file mode 100644 (file)
index 0000000..2aaabdd
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef __LED__H__
+#define __LED__H__
+
+
+int32_t led_open( void );
+int32_t led_close( uint32_t led );
+int32_t led_set( uint32_t led, uint32_t mode );
+
+#endif //__LED__H__
diff --git a/led/src/led.c b/led/src/led.c
new file mode 100644 (file)
index 0000000..a101d39
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "led.h"
+
+int32_t led_open( void )
+{
+       printf ( "tw8013 led open!\n" );
+       return 0;
+       //todo: start kbd task;
+}
+
+int32_t led_close( uint32_t kbd )
+{
+       printf ( "tw8013 led close!\n" );
+       return 0;
+}
+
+int32_t led_set( uint32_t led, uint32_t mode )
+{
+       printf ( "tw8013 led set!\n" );
+       return 0;
+}
diff --git a/tuya-iot-core-sdk b/tuya-iot-core-sdk
new file mode 160000 (submodule)
index 0000000..0208bad
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 0208badefb2003d30e70b5b875395e3187c850d9