添加 指令 scan lamp pwr brightness etc. master
authorsunchunyang <sunchunyang@kaihong.com>
Sun, 8 Oct 2023 08:53:54 +0000 (16:53 +0800)
committersunchunyang <sunchunyang@kaihong.com>
Sun, 8 Oct 2023 08:53:54 +0000 (16:53 +0800)
14 files changed:
gs200_test [deleted file]
obj/encryption.o [deleted file]
obj/gs200_msg_codec.o [deleted file]
obj/main.o [deleted file]
obj/utils.o [deleted file]
obj/utils_shell_cmd.o [deleted file]
obj/utils_socket_udp.o [deleted file]
obj/utils_uart.o [deleted file]
src/gs200_msg.h
src/gs200_msg_codec.c
src/main.c
src/utils.c
src/utils.h
src/utils_shell_cmd.c

diff --git a/gs200_test b/gs200_test
deleted file mode 100755 (executable)
index 585fe46..0000000
Binary files a/gs200_test and /dev/null differ
diff --git a/obj/encryption.o b/obj/encryption.o
deleted file mode 100644 (file)
index ddb06c1..0000000
Binary files a/obj/encryption.o and /dev/null differ
diff --git a/obj/gs200_msg_codec.o b/obj/gs200_msg_codec.o
deleted file mode 100644 (file)
index 1627d69..0000000
Binary files a/obj/gs200_msg_codec.o and /dev/null differ
diff --git a/obj/main.o b/obj/main.o
deleted file mode 100644 (file)
index bba63e3..0000000
Binary files a/obj/main.o and /dev/null differ
diff --git a/obj/utils.o b/obj/utils.o
deleted file mode 100644 (file)
index ca1b411..0000000
Binary files a/obj/utils.o and /dev/null differ
diff --git a/obj/utils_shell_cmd.o b/obj/utils_shell_cmd.o
deleted file mode 100644 (file)
index 2a477b3..0000000
Binary files a/obj/utils_shell_cmd.o and /dev/null differ
diff --git a/obj/utils_socket_udp.o b/obj/utils_socket_udp.o
deleted file mode 100644 (file)
index 508aee5..0000000
Binary files a/obj/utils_socket_udp.o and /dev/null differ
diff --git a/obj/utils_uart.o b/obj/utils_uart.o
deleted file mode 100644 (file)
index f988d04..0000000
Binary files a/obj/utils_uart.o and /dev/null differ
index 4ec687a..b3c7cc1 100644 (file)
@@ -60,6 +60,14 @@ enum{
     DEV_TYPE_ENVIRONMENT_SENSOR=0x0F,// 0f是环境传感器
 } device_type_enum;
 
+enum{
+    ADDRESS_TYPE_INVALID=0x0000, //无效(未分配地址)
+    ADDRESS_TYPE_SINGLEADDR_MASK_0=0x0000,//单播地址 0xxx xxxx xxxx xxxx  ADDRESS_TYPE_SINGLEADDR_MASK & 0xC000 == 0x0000 || 0x4000
+    ADDRESS_TYPE_SINGLEADDR_MASK_4=0x4000,//单播地址 0xxx xxxx xxxx xxxx  ADDRESS_TYPE_SINGLEADDR_MASK & 0xC000 == 0x0000 || 0x4000
+    ADDRESS_TYPE_VIRTUALADDR_MASK=0x8000,//虚拟地址 10xx xxxx xxxx xxxx ADDRESS_TYPE_VIRTUALADDR_MASK & 0xC000 == 0x8000
+    ADDRESS_TYPE_GROUPADDR_MASK=0xC000,//群组地址 11xx xxxx xxxx xxxx  ADDRESS_TYPE_GROUPADDR_MASK & 0xC000 == 0xC000
+    ADDRESS_TYPE_BROADCAST_ADDR=0xFFFF,//广播地址    
+} address_mask_enum;
 
 
 /*********
@@ -75,6 +83,7 @@ enum {
     OPC_QUIT_SCAN_SET=0x05,// 无 无 退出扫描设备
     OPC_BEACON=0x0A,//无 1Byte 设备类型 ○1 +5Byte 软件版本号 +3Byte 支持硬件配置+ 1Byte 支持该配置的硬件版本 + 版本标签 ○2 Beacon 帧
     OPC_HEART_BEAT_STS=0x0B,// 无 1Byte 设备类型 + 16Byte 用户自定义 数据心跳包
+    OPC_HUMAN_PRESENCE_STS=0x0C,// 人体感应Sensor,人体存在消息
     OPC_GROUPING_SET=0x10,// 无 6Byte,组地址+网络ID+网络密码遥控器专用指令,复制组地址、网络 ID、网络密码
     OPC_ADD_NBR_SET=0x11,// 无 2Byte,邻组地址 遥控器专用指令,添加邻组地址
     OPC_DEL_NBR_SET=0x12,// 无 2Byte,邻组地址 遥控器专用指令,删去邻组地址
@@ -204,6 +213,7 @@ enum {
     OPC_REPORT_SETTING_STS=0x5D,                                    
 } opcode_enum; 
 
+
 #pragma pack(1)
 typedef struct
 {
@@ -240,6 +250,47 @@ typedef struct
   uint8_t seconds;//Beacon 帧发 送周期,单位:秒  扫描设备设置,设备收到后持续发送 BEACON 帧
 }BeaconBroadcastCycle_t;
 
+
+
+#pragma pack(1)
+typedef struct 
+{
+    uint8_t device_type;// 6Byte,1Byte 设备类型
+    version_t version;//+5Byte 软件版本
+}opc_dev_info_sts_t;
+
+
+#pragma pack(1)
+typedef struct 
+{
+    uint8_t device_type;// 1Byte 设备类型 +
+    uint32_t power_on_duration;//12Byte 使用信息 ( 4Byte 通电时长
+    uint32_t sensor_on_duration;//+4Byte 传感器感应时长
+    uint32_t power_on_weighted_duration;//+4Byte 通电加权时长)
+}opc_use_info_sts_t;
+
+#pragma pack(1)
+typedef struct 
+{
+    uint8_t dpid; //0x12
+    union 
+    {
+         uint8_t lamp_mode;// 1Byte,控制模式。0x00:传感器自动控制,0x01:常灭,0x02:休眠,0x03:常亮
+         uint8_t lamp_brightness;// 1Byte,亮灯状态亮度,0-255
+    }lamp_sts;   
+}opc_lamp_sts_t;
+
+#pragma pack(1)
+typedef struct 
+{//OPC_HEART_BEAT_SETTING_STS
+    uint8_t dpid; //0x02
+    union 
+    {
+        uint8_t heartbeat_cycle;//1Byte, 心跳消息周 期, Bit7-Bit1:值,Bit0:单位,1 为分钟,0 为秒
+        uint8_t heartbeat_switch;//1Byte, 心跳消息开关
+    }heartbeat_set;    
+}opc_heartbeat_sts_t;
+
 #pragma pack(1)
 typedef struct 
 {
@@ -276,10 +327,13 @@ typedef struct
     uint16_t network_id;
     uint8_t opcode; //填写数据为:opcode_enum
     union {
-        
         BeaconBroadcastCycle_t beaconbroadcastcycle;
         BeaconData_t   beacondata;
         heartbeat_t    heartbeat;
+        opc_dev_info_sts_t dev_info_sts;
+        opc_use_info_sts_t use_info_sts;
+        opc_lamp_sts_t lamp_sts;
+        opc_heartbeat_sts_t heartbeat_sts;
         uint8_t params[20];//操作指令参数最大值
     } frame_data;
 } nwk_frame_t;
@@ -308,7 +362,6 @@ typedef struct
     uint8_t snr;
     uint8_t uuid[6];
     uint16_t src_addr;
-    uint16_t target_addr;
     uint16_t network_id;
     uint16_t password;
     uint16_t group_id;
@@ -320,6 +373,7 @@ typedef struct
     uint8_t hw_version;
     uint8_t cycle_seconds;//Beacon 帧发 送周期,单位:秒  扫描设备设置,设备收到后持续发送 BEACON 帧
     uint64_t utc_time_stamp;
+    uint16_t parent_serial_port; //接收到设备消息的dongle 所属的串口,(主要解决 网关 可能连接一usb拖多dongle的 设备管理问题)
 }gs200_device_item_t;
 
 
@@ -364,7 +418,7 @@ int encode_msg(gs200_msg_t* gs200_msg_in,gs200_msg_encrypted_t *gs200_msg_encryp
 
 int gen_gs200_msg_stop_scan(uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],gs200_msg_encrypted_t *gs200_msg_encrypted_out);
 
-int gen_gs200_msg_start_scan(uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],uint8_t beacon_send_cycle_in_seconds,gs200_msg_encrypted_t *gs200_msg_encrypted_out);
+int gen_gs200_msg_start_scan(uint8_t beacon_send_cycle_in_seconds,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],gs200_msg_encrypted_t *gs200_msg_encrypted_out);
 
 
 int gen_gs200_msg_get_dongle_version(gs200_msg_encrypted_t *gs200_msg_encrypted_out);
@@ -373,4 +427,12 @@ int gen_gs200_msg_get_dongle_uuid(gs200_msg_encrypted_t *gs200_msg_encrypted_out
 
 int gen_gs200_msg_get_dongle_networksetting(gs200_msg_encrypted_t *gs200_msg_encrypted_out);
 
+
+
+int gen_gs200_msg_lamp_power(uint8_t poweron,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out);
+int gen_gs200_msg_lamp_brightness(uint8_t brightness,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out);
+int gen_gs200_msg_heartbeat_cycle(uint8_t cycle,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out);
+
+
+
 #endif // __gs200_MSG_H__
index 1af0142..ba6c966 100644 (file)
@@ -176,11 +176,16 @@ int encode_msg(gs200_msg_t* gs200_msg_in,gs200_msg_encrypted_t *gs200_msg_encryp
     uint16_t crc = getModbusCRC16(((char*)gs200_msg_encrypted_out)+2,gs200_msg_encrypted_out->size+1);
     uint16_t *pcrc = ((char*)gs200_msg_encrypted_out) +gs200_msg_encrypted_out->size+2+1;
     *pcrc = crc;
+    /**
+     *   head   size    encrypted_data  modbus_crc16
+     *     2B   1B      0~36B            2B
+    */
+    // print_hex_arr((char*)gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
     return gs200_msg_encrypted_out->size + 2 + 2 + 1;
 }
 /*********************指令生成函数********************************/
 
-int gen_gs200_msg_start_scan(uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],uint8_t beacon_send_cycle_in_seconds,gs200_msg_encrypted_t *gs200_msg_encrypted_out){
+int gen_gs200_msg_start_scan(uint8_t beacon_send_cycle_in_seconds,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],gs200_msg_encrypted_t *gs200_msg_encrypted_out){
     gs200_msg_t gs200_msg_in = {0};
     gs200_msg_in.cmd_type = UART_CMD_CODE_SEND;
     gs200_msg_in.msg.nwk_frame.opcode = OPC_SCAN_DEV_SET;
@@ -191,9 +196,15 @@ int gen_gs200_msg_start_scan(uint16_t area_id,uint16_t src_addr, uint16_t target
     memcpy(gs200_msg_in.msg.nwk_frame.uuid ,uuid,sizeof(uuid));
     gs200_msg_in.msg.nwk_frame.frame_data.beaconbroadcastcycle.seconds = beacon_send_cycle_in_seconds; //1秒发送一次beacon数据
     gs200_msg_in.size = (13+2);//13B msg.len + 1B size+ 1B beaconbroadcastcycle
-
+    /**
+     * 信号强度 设备识别号 设备地址 发布地址 网络 ID 操作指令
+        1B      6B          2B      2B      2B      2B
+    */
     dump_msg(&gs200_msg_in);
-    return encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    int ret = encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
 }
 
 int gen_gs200_msg_stop_scan(uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6],gs200_msg_encrypted_t *gs200_msg_encrypted_out){
@@ -208,7 +219,10 @@ int gen_gs200_msg_stop_scan(uint16_t area_id,uint16_t src_addr, uint16_t target_
     gs200_msg_in.size = (13+1); //13B msg.len+ 1B size
     
     dump_msg(&gs200_msg_in);
-    return encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
 }
 
 
@@ -216,19 +230,104 @@ int gen_gs200_msg_get_dongle_version(gs200_msg_encrypted_t *gs200_msg_encrypted_
     gs200_msg_t gs200_msg_in = {0};
     gs200_msg_in.cmd_type = UART_CMD_CODE_GET_VER;
     gs200_msg_in.size = 0; 
-    return encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
 }
 
 int gen_gs200_msg_get_dongle_uuid(gs200_msg_encrypted_t *gs200_msg_encrypted_out){
     gs200_msg_t gs200_msg_in = {0};
     gs200_msg_in.cmd_type = UART_CMD_CODE_GET_UID;
     gs200_msg_in.size = 0; 
-    return encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
 }
 
 int gen_gs200_msg_get_dongle_networksetting(gs200_msg_encrypted_t *gs200_msg_encrypted_out){
     gs200_msg_t gs200_msg_in = {0};
     gs200_msg_in.cmd_type = UART_CMD_CODE_GET_NWK;
     gs200_msg_in.size = 0; 
-    return encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
+}
+
+int gen_gs200_msg_lamp_power(uint8_t poweron,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out){
+     gs200_msg_t gs200_msg_in = {0};
+    gs200_msg_in.cmd_type = UART_CMD_CODE_SEND;
+    gs200_msg_in.msg.nwk_frame.opcode = OPC_LAMP_SETTING_SET;
+    gs200_msg_in.msg.nwk_frame.snr = 0;
+    gs200_msg_in.msg.nwk_frame.network_id = area_id;
+    gs200_msg_in.msg.nwk_frame.src_addr = src_addr;
+    gs200_msg_in.msg.nwk_frame.target_addr = target_addr;
+    memcpy(gs200_msg_in.msg.nwk_frame.uuid ,uuid,sizeof(uuid));
+
+    gs200_msg_in.msg.nwk_frame.frame_data.lamp_sts.dpid = 0x12;
+
+    if(poweron){
+        gs200_msg_in.msg.nwk_frame.frame_data.lamp_sts.lamp_sts.lamp_mode = 0x01;
+    }else{
+        gs200_msg_in.msg.nwk_frame.frame_data.lamp_sts.lamp_sts.lamp_mode = 0x11;
+    }
+    gs200_msg_in.size = (13+1+2); //13B msg.len+ 1B size + 1B dpid + 1B lamp_mode
+    
+    dump_msg(&gs200_msg_in);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
+}
+
+int gen_gs200_msg_lamp_brightness(uint8_t brightness,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out){
+     gs200_msg_t gs200_msg_in = {0};
+    gs200_msg_in.cmd_type = UART_CMD_CODE_SEND;
+    gs200_msg_in.msg.nwk_frame.opcode = OPC_QUIT_SCAN_SET;
+    gs200_msg_in.msg.nwk_frame.snr = 0;
+    gs200_msg_in.msg.nwk_frame.network_id = area_id;
+    gs200_msg_in.msg.nwk_frame.src_addr = src_addr;
+    gs200_msg_in.msg.nwk_frame.target_addr = target_addr;
+    memcpy(gs200_msg_in.msg.nwk_frame.uuid ,uuid,sizeof(uuid));
+ 
+    gs200_msg_in.msg.nwk_frame.frame_data.lamp_sts.dpid = 0x13;
+
+    gs200_msg_in.msg.nwk_frame.frame_data.lamp_sts.lamp_sts.lamp_brightness = brightness;
+    
+    gs200_msg_in.size = (13+1+2); //13B msg.len+ 1B size + 1B dpid + 1B lamp_brightness
+    
+    dump_msg(&gs200_msg_in);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
 }
+
+
+int gen_gs200_msg_heartbeat_cycle(uint8_t cycle,uint16_t area_id,uint16_t src_addr, uint16_t target_addr, uint8_t uuid[6], gs200_msg_encrypted_t *gs200_msg_encrypted_out){
+    
+    
+    // GS200_LOGD("gen_gs200_msg_heartbeat_cycle %d \r\n",cycle);
+    gs200_msg_t gs200_msg_in = {0};
+    gs200_msg_in.cmd_type = UART_CMD_CODE_SEND;
+    gs200_msg_in.msg.nwk_frame.opcode = OPC_HEART_BEAT_SETTING_SET;
+    gs200_msg_in.msg.nwk_frame.snr = 0;
+    gs200_msg_in.msg.nwk_frame.network_id = area_id;
+    gs200_msg_in.msg.nwk_frame.src_addr = src_addr;
+    gs200_msg_in.msg.nwk_frame.target_addr = target_addr;
+    memcpy(gs200_msg_in.msg.nwk_frame.uuid ,uuid,sizeof(uuid));
+ 
+    gs200_msg_in.msg.nwk_frame.frame_data.heartbeat_sts.dpid = 0x02;
+    
+    gs200_msg_in.msg.nwk_frame.frame_data.heartbeat_sts.heartbeat_set.heartbeat_cycle = (cycle<<1);
+    
+    gs200_msg_in.size = (13+1+2); //13B msg.len+ 1B size + 1B dpid + 1B heartbeat_cycle
+    
+    dump_msg(&gs200_msg_in);
+    int ret =  encode_msg(&gs200_msg_in,gs200_msg_encrypted_out);
+    print_hex_arr(&gs200_msg_in,gs200_msg_encrypted_out->size);
+    print_hex_arr(gs200_msg_encrypted_out,gs200_msg_encrypted_out->size+5);
+    return ret;
+}
\ No newline at end of file
index 48e9645..e44a874 100644 (file)
@@ -72,15 +72,18 @@ int get_device_index(uint8_t  UUID[6]){
        int found_uuid_index=-1;
        for (int i =0 ; i < MAX_DEVICE_LIST_COUNT; i++)
        {
-               if(first_empty_uuid_index!=-1 && devicelist[i].src_addr == 0)
+
+               if(first_empty_uuid_index == -1 && devicelist[i].src_addr == 0)
                {
                        first_empty_uuid_index = i;
+                       
                }
                if(mstrncmp((char*)UUID,(char*)devicelist[i].uuid,6)==0 ){
                        found_uuid_index = i;
                        break;
                }
        }
+       // GS200_LOGD("first_empty_uuid_index %d .found_uuid_index %d\r\n",first_empty_uuid_index,found_uuid_index);    
        if(found_uuid_index == -1 ){
                return first_empty_uuid_index; // 如果没找到对应的设备,返回第一个空设备赋值,如果已经满了,返回的就是-1
        }               
@@ -88,14 +91,19 @@ int get_device_index(uint8_t  UUID[6]){
                return found_uuid_index;//返回找到的对应设备
        }
 }
-gs200_device_item_t* get_device_id(uint8_t  UUID[6]){
-       int index=get_device_index(UUID);
-       if(index > 0 && index < MAX_DEVICE_LIST_COUNT){
+gs200_device_item_t* get_device_by_index(uint8_t index){
+       if(index >= 0 && index < MAX_DEVICE_LIST_COUNT){
                return &devicelist[index];
        }else{
                return NULL;
        }
 }
+gs200_device_item_t* get_device_by_uuid(uint8_t  UUID[6]){
+       int index=get_device_index(UUID);
+       return get_device_by_index(index);
+}
+
+
 
 void print_device_type(int devicetype){
        switch(devicetype)
@@ -188,27 +196,34 @@ void dump_device(gs200_device_item_t*  gs200_dev){
                                                                                                gs200_dev->uuid[1],gs200_dev->uuid[2],gs200_dev->uuid[3],gs200_dev->uuid[4],
                                                                                                gs200_dev->uuid[5]);
        GS200_LOGD("src_addr %04X \r\n",gs200_dev->src_addr);
-       GS200_LOGD("target_addr %04X \r\n",gs200_dev->target_addr);
        GS200_LOGD("network_id %04X \r\n",gs200_dev->network_id);
        GS200_LOGD("group_id %04X \r\n",gs200_dev->group_id);
        GS200_LOGD("password %04X \r\n",gs200_dev->password);
        GS200_LOGD("software version %d.%d.%d \r\n",gs200_dev->version.MAJOR_VER,gs200_dev->version.MINOR_VER,gs200_dev->version.REVISION);
     GS200_LOGD("cycle_seconds %d \r\n",gs200_dev->cycle_seconds);
        GS200_LOGD("timestamp %ld \r\n",gs200_dev->utc_time_stamp);
+       GS200_LOGD("parent_serial_port %d \r\n",gs200_dev->parent_serial_port);
        GS200_LOGD("=======================================================\r\n");
 }
 
-void dump_all_device()
+void dump_all_device_info()
 {
        for (int i = 0; i < MAX_DEVICE_LIST_COUNT ;i++)
-       {
+       {       
                if (devicelist[i].src_addr > 0)
                {
+                       GS200_LOGD("====device index %d============================\r\n",i);
                        dump_device(&devicelist[i]);
                }
        }
 }
 
+void dump_dongle_info()
+{
+       gs200_device_item_t *item_dongle=get_current_dongle_device();
+       dump_device(item_dongle);
+}
+
 /**
  * 处理接收到的加密消息
 */
@@ -219,21 +234,20 @@ static void handle_gs200_msg(gs200_msg_encrypted_t * msg)
        decode_gs200_msg(msg,&gs200_msg);
        print_hex_arr((uint8_t*)&gs200_msg,gs200_msg.size+2);
        print_cmd_type(gs200_msg.cmd_type);
-
+       //dump_msg(&gs200_msg);
        switch(gs200_msg.cmd_type)
        {
-               case UART_CMD_CODE_RECV_REP:
                case UART_CMD_CODE_RECV:
-                       GS200_LOGD("%s %d\r\n",__func__,__LINE__);
+                       GS200_LOGD("%s %d opcode 0x%02X\r\n",__func__,__LINE__,gs200_msg.msg.nwk_frame.opcode);
                        if(OPC_BEACON == gs200_msg.msg.nwk_frame.opcode )
                        {
-                               gs200_device_item_t* item=get_device_id(gs200_msg.msg.nwk_frame.uuid);
+                               gs200_device_item_t* item=get_device_by_uuid(gs200_msg.msg.nwk_frame.uuid);
+                               gs200_device_item_t *item_dongle=get_current_dongle_device();
                                if(item != NULL)
                                {
                                        item->snr = gs200_msg.msg.nwk_frame.snr;
                                        memcpy(item->uuid,gs200_msg.msg.nwk_frame.uuid,6);
                                        item->src_addr = gs200_msg.msg.nwk_frame.src_addr;
-                                       item->target_addr = gs200_msg.msg.nwk_frame.target_addr;
                                        item->network_id = gs200_msg.msg.nwk_frame.network_id;
                                        item->device_type = gs200_msg.msg.nwk_frame.frame_data.beacondata.device_type;
                                        memcpy(&item->version,&gs200_msg.msg.nwk_frame.frame_data.beacondata.version,sizeof(version_t));
@@ -241,44 +255,103 @@ static void handle_gs200_msg(gs200_msg_encrypted_t * msg)
                                        item->reomte_config = gs200_msg.msg.nwk_frame.frame_data.beacondata.hw_config.reomte_config;
                                        item->sensor_config = gs200_msg.msg.nwk_frame.frame_data.beacondata.hw_config.sensor_config;
                                        item->hw_version = gs200_msg.msg.nwk_frame.frame_data.beacondata.hw_version;
-                                       item->utc_time_stamp =  get_timestamp();
+                                       item->utc_time_stamp =  get_timestamp();//更新消息接收时间,可以直接用来判断设备是否在线
+                                       item->parent_serial_port = item_dongle->parent_serial_port; //当前设备只有一个串口,因此采用默认值,多个串口时,要进行区分。
+                                       print_device_type(item->device_type);
                                }
-                               print_device_type(item->device_type);
 
                        }else if (OPC_HEART_BEAT_STS == gs200_msg.msg.nwk_frame.opcode)
                        {
-                               gs200_device_item_t* item=get_device_id(gs200_msg.msg.nwk_frame.uuid);
+                               gs200_device_item_t* item=get_device_by_uuid(gs200_msg.msg.nwk_frame.uuid);
                                if(item != NULL)
                                {
                                        item->device_type = gs200_msg.msg.nwk_frame.frame_data.heartbeat.device_type;
                                        item->utc_time_stamp =  get_timestamp();
+                                       print_device_type(item->device_type);
+                               }
+                               
+                       }else if (OPC_HEART_BEAT_SETTING_STS == gs200_msg.msg.nwk_frame.opcode)
+                       {
+                               gs200_device_item_t* item=get_device_by_uuid(gs200_msg.msg.nwk_frame.uuid);
+                               if(item != NULL)
+                               {
+                                       switch(gs200_msg.msg.nwk_frame.frame_data.heartbeat_sts.dpid)
+                                       {
+                                               case 0x01:
+                                                       
+                                                       GS200_LOGD("heartbeat_switch %d\r\n",gs200_msg.msg.nwk_frame.frame_data.heartbeat_sts.heartbeat_set.heartbeat_switch);
+                                                       break;
+                                               case 0x02:
+                                                       {
+                                                               int cycle = (gs200_msg.msg.nwk_frame.frame_data.heartbeat_sts.heartbeat_set.heartbeat_cycle >>1);
+                                                               int unit = (gs200_msg.msg.nwk_frame.frame_data.heartbeat_sts.heartbeat_set.heartbeat_cycle & 0x01);
+                                                               if( unit == 0 )
+                                                                       item->cycle_seconds = cycle ;
+                                                               else 
+                                                                       item->cycle_seconds = cycle * 60;
+
+                                                               GS200_LOGD("heartbeat_cycle %d , unit %d , 0x%02X\r\n",cycle,unit,gs200_msg.msg.nwk_frame.frame_data.heartbeat_sts.heartbeat_set.heartbeat_cycle);
+                                                       }
+                                                       break;
+                                       }
+                                       
+                                       item->utc_time_stamp =  get_timestamp();
+                                       print_device_type(item->device_type);
+                               }
+                               
+                       }else if (OPC_LAMP_SETTING_STS == gs200_msg.msg.nwk_frame.opcode)
+                       {
+                               gs200_device_item_t* item=get_device_by_uuid(gs200_msg.msg.nwk_frame.uuid);
+                               if(item != NULL)
+                               {
+                                       switch(gs200_msg.msg.nwk_frame.frame_data.lamp_sts.dpid)
+                                       {
+                                               case 0x12: //模式设置 0x00:传感器自动控 制,0x01:常灭,0x10:休眠,0x11:常亮
+                                                       
+                                                       GS200_LOGD("lamp switch 0x%02X \r\n",gs200_msg.msg.nwk_frame.frame_data.lamp_sts.lamp_sts.lamp_mode);
+                                                       break;
+                                               case 0x13: //1Byte,亮灯状态亮度,0-255
+                                                       
+                                                       GS200_LOGD("lamp brightness %d \r\n",gs200_msg.msg.nwk_frame.frame_data.lamp_sts.lamp_sts.lamp_brightness);
+                                                       break;
+                                       }
+                                       
+                                       item->utc_time_stamp =  get_timestamp();
+                                       print_device_type(item->device_type);
+                               }
+                               
+                       }else if (OPC_HUMAN_PRESENCE_STS == gs200_msg.msg.nwk_frame.opcode)
+                       {
+                               gs200_device_item_t* item=get_device_by_uuid(gs200_msg.msg.nwk_frame.uuid);
+                               if(item != NULL)
+                               {
+                                       GS200_LOGD("OPC_HUMAN_PRESENCE_STS addr 0x%02X ",item->src_addr);
+                                       item->utc_time_stamp =  get_timestamp();
+                                       print_device_type(item->device_type);
                                }
-                               print_device_type(item->device_type);
                        }
                
                        break;          
                case UART_CMD_CODE_GET_VER_REP:
-               case UART_CMD_CODE_GET_VER:
                        {
                                gs200_device_item_t *item=get_current_dongle_device();
                                memcpy(&item->version,&gs200_msg.msg.version,sizeof(version_t));
-                               dump_msg(&gs200_msg);
-                               dump_device(item);
+                               //dump_msg(&gs200_msg);
+                               //dump_device(item);
                        }
                        
                        break;
 
                case UART_CMD_CODE_GET_UID_REP:
-               case UART_CMD_CODE_GET_UID:
                        {
                                gs200_device_item_t *item=get_current_dongle_device();
                                memcpy(&item->uuid,&gs200_msg.msg.UUID,6);
-                               dump_msg(&gs200_msg);
-                               dump_device(item);
+                               //dump_msg(&gs200_msg);
+                               //dump_device(item);
                        }
 
                        break;
-               case UART_CMD_CODE_GET_NWK:
+               
                case UART_CMD_CODE_GET_NWK_REP:
                        {
                                gs200_device_item_t *item=get_current_dongle_device();
@@ -286,8 +359,8 @@ static void handle_gs200_msg(gs200_msg_encrypted_t * msg)
                                item->password=gs200_msg.msg.network_setting.PASSWORD;
                                item->src_addr=gs200_msg.msg.network_setting.UNICAST_ADDR;
                                item->group_id=gs200_msg.msg.network_setting.GROUP_ADDR;
-                               dump_msg(&gs200_msg);
-                               dump_device(item);
+                               //dump_msg(&gs200_msg);
+                               //dump_device(item);
                        }
 
                        break;
index 2e62018..b990583 100644 (file)
@@ -24,10 +24,11 @@ void print_hex_arr(uint8_t *buf, int len)
 {
     if (len <= 0)
         return;
-    // GS200_LOGD("0x");
+    GS200_LOGD("%d --- ",get_timestamp());
     for (int i = 0; i < len; i++)
     {
-        GS200_LOGD("0x%02X,", buf[i]);
+        //GS200_LOGD("0x%02X,", buf[i]);
+        GS200_LOGD("%02X ", buf[i]);
     }
     GS200_LOGD("\r\n");
 }
index 0b1a31a..1a14431 100644 (file)
@@ -27,7 +27,8 @@ int g_server_main(uint8_t *path);
 int g_send_msg(gs200_msg_encrypted_t *gs200_msg_encrypted_out);
 int g_set_udp_mode(uint8_t enUdp);
 gs200_device_item_t* get_current_dongle_device();
-
+gs200_device_item_t* get_device_by_index(uint8_t index);
+gs200_device_item_t* get_device_by_uuid(uint8_t  UUID[6]);
 uint64_t  get_timestamp();
 
 #endif //__UTILS_H__
\ No newline at end of file
index dcaf118..bab6d4f 100644 (file)
@@ -27,12 +27,13 @@ typedef enum{
     AT_CMD_LIST_DEVICE,     /* 打印已知列表 */
     AT_CMD_GET_DONGLE_INFO, /* 获取当前dongle详细信息 */
     AT_CMD_SET_HEARTBEAT_CYCLE, /* 设置连接间隔 */
-    
+    AT_CMD_LAMP_PWRON, /* 设置灯开  */
+    AT_CMD_LAMP_PWROFF, /* 设置灯关  */
+    AT_CMD_LAMP_BRIGHTNESS, /* 设置灯亮度  */
     AT_END
 }AT_Cmd;
 
 
-
 /* 指令执行函数 */
 unsigned char at_cmd_test(unsigned char *p, unsigned char len){
     
@@ -48,15 +49,17 @@ unsigned char at_cmd_scan(unsigned char *p, unsigned char len){
         GS200_LOGD("OK\r\n");
         gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
         gs200_device_item_t* item=get_current_dongle_device();
-        gen_gs200_msg_start_scan(item->network_id,item->src_addr,0xFFFF,item->uuid,1,&gs200_msg_encrypted_out);
+        uint8_t uuid[6]={0};
+        gen_gs200_msg_start_scan(2,item->network_id,item->src_addr,ADDRESS_TYPE_BROADCAST_ADDR,uuid,&gs200_msg_encrypted_out);
         g_send_msg(&gs200_msg_encrypted_out);
+        
 
     }else {
         
         GS200_LOGD("OK\r\n");
         gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
         gs200_device_item_t* item=get_current_dongle_device();
-        gen_gs200_msg_stop_scan(item->network_id,item->src_addr,0xFFFF,item->uuid,&gs200_msg_encrypted_out);
+        gen_gs200_msg_stop_scan(item->network_id,item->src_addr,ADDRESS_TYPE_BROADCAST_ADDR,item->uuid,&gs200_msg_encrypted_out);
         g_send_msg(&gs200_msg_encrypted_out);
     }
     return 0;
@@ -66,13 +69,13 @@ unsigned char at_cmd_getdevinfo(unsigned char *p, unsigned char len){
     
     
     GS200_LOGD("OK\r\n");
-      
+    dump_dongle_info();
     return 0;
 }
 unsigned char at_cmd_listdevices(unsigned char *p, unsigned char len){
     
     GS200_LOGD("OK\r\n");
-    dump_all_device();
+    dump_all_device_info();
     return 0;
 }
 
@@ -95,10 +98,68 @@ unsigned char at_cmd_get_dongleinfo(unsigned char *p, unsigned char len){
 
 unsigned char at_cmd_set_heartcycle(unsigned char *p, unsigned char len){
     
+    
+    gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
+    gs200_device_item_t* item=get_current_dongle_device();
+    int16_t cycle = -1;
+    int16_t index = -1;
+    sscanf(p,"%d,%d",&index,&cycle);
+    gs200_device_item_t* item_target=get_device_by_index(index);
+    if(item_target != NULL && cycle > -1){
+        // GS200_LOGD("== index %d cycle %d\r\n",index,cycle);
+        gen_gs200_msg_heartbeat_cycle(cycle,item_target->network_id,item->src_addr,item_target->src_addr,item->uuid,&gs200_msg_encrypted_out);
+        g_send_msg(&gs200_msg_encrypted_out);
+    }
+
+    return 0;
+}
+unsigned char at_cmd_set_lamp_poweron(unsigned char *p, unsigned char len){
+    
+    GS200_LOGD("OK\r\n");
+    gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
+    gs200_device_item_t* item=get_current_dongle_device();
+
+    int16_t index = -1;
+    sscanf(p,"%d",&index);
+    gs200_device_item_t* item_target=get_device_by_index(index);
+    if(item_target != NULL){
+        gen_gs200_msg_lamp_power(1,item_target->network_id,item->src_addr,item_target->src_addr,item->uuid,&gs200_msg_encrypted_out);
+        g_send_msg(&gs200_msg_encrypted_out);
+    }
+    return 0;
+}
+
+unsigned char at_cmd_set_lamp_poweroff(unsigned char *p, unsigned char len){
+    
     GS200_LOGD("OK\r\n");
+    gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
+    gs200_device_item_t* item=get_current_dongle_device();
+
+    int16_t index = -1;
+    sscanf(p,"%d",&index);
+    gs200_device_item_t* item_target=get_device_by_index(index);
+    if(item_target != NULL){
+        gen_gs200_msg_lamp_power(0,item_target->network_id,item->src_addr,item_target->src_addr,item->uuid,&gs200_msg_encrypted_out);
+        g_send_msg(&gs200_msg_encrypted_out);
+    }
     return 0;
 }
+unsigned char at_cmd_set_lamp_brightness(unsigned char *p, unsigned char len){
+    
+    GS200_LOGD("OK\r\n");
+    gs200_msg_encrypted_t gs200_msg_encrypted_out={0};
+    gs200_device_item_t* item=get_current_dongle_device();
 
+    int16_t brightness = -1;
+    int16_t index = -1;
+    sscanf(p,"%d,%d",&index,&brightness);
+    gs200_device_item_t* item_target=get_device_by_index(index);
+    if(item_target != NULL && brightness > -1){
+        gen_gs200_msg_lamp_brightness(brightness,item_target->network_id,item->src_addr,item_target->src_addr,item->uuid,&gs200_msg_encrypted_out);
+        g_send_msg(&gs200_msg_encrypted_out);
+    }
+    return 0;
+}
 typedef unsigned char (*pFunc)(unsigned char *ptr, unsigned char len);
 
 typedef struct {
@@ -113,10 +174,14 @@ const AT_cmd_func at_cmd_func[] = {
     {AT_CMD_TEST,         "AT",                at_cmd_test,"AT"},
     {AT_CMD_HELP,         "AT+HELP",                at_cmd_help,"AT+HELP"},
     {AT_CMD_SCAN,         "AT+SCAN=",          at_cmd_scan,"AT+SCAN=1 start scan , AT+SCAN=0 stop scan"},
-    {AT_CMD_GET_DEVINFO,         "AT+GET_DEVINFO=",           at_cmd_getdevinfo,"AT+GET_DEVICEINFO=12348 [uint16_t device_addr]"},
+    // {AT_CMD_GET_DEVINFO,         "AT+GET_DEVINFO=",           at_cmd_getdevinfo,"AT+GET_DEVICEINFO=12348 [uint16_t device_addr]"},
+    {AT_CMD_GET_DEVINFO,         "AT+DUMP_DONGLEINFO",           at_cmd_getdevinfo,"AT+DUMP_DONGLEINFO"},
     {AT_CMD_LIST_DEVICE,     "AT+LIST_DEVICES",            at_cmd_listdevices,"AT+LIST_DEVICES"},
     {AT_CMD_GET_DONGLE_INFO, "AT+GET_DONGLEINFO",  at_cmd_get_dongleinfo,"AT+GET_DONGLEINFO"},
-    {AT_CMD_SET_HEARTBEAT_CYCLE, "AT+SETHEARTBEATCYCLE=",  at_cmd_set_heartcycle,"AT+SETHEARTBEATCYCLE=60 , 60seconds send one heartbeat message"},
+    {AT_CMD_SET_HEARTBEAT_CYCLE, "AT+SETHEARTBEATCYCLE=",  at_cmd_set_heartcycle,"AT+SETHEARTBEATCYCLE=0,60 , device index in LIST_DEVICES and 60seconds send one heartbeat message"},
+    {AT_CMD_LAMP_PWRON, "AT+LAMP_POWERON=",  at_cmd_set_lamp_poweron,"AT+LAMP_POWERON=0 , device index in LIST_DEVICES "},
+    {AT_CMD_LAMP_PWROFF, "AT+LAMP_POWEROFF=",  at_cmd_set_lamp_poweroff,"AT+LAMP_POWEROFF=0 , device index in LIST_DEVICES "},
+    {AT_CMD_LAMP_BRIGHTNESS, "AT+LAMP_BRIGHTNESS=",  at_cmd_set_lamp_brightness,"AT+LAMP_BRIGHTNESS=0,255 , device index in LIST_DEVICES and 0~255 brightness"},
     {AT_END,              NULL,                NULL}
 };
 
@@ -129,6 +194,7 @@ unsigned char at_cmd_help(unsigned char *p, unsigned char len){
     GS200_LOGD("OK\r\n");
     return 0;
 }
+
 /* 查找指令表中对应的指令 */
 unsigned char AT_cmd_search(unsigned char *p, unsigned char len){