ZK-Teco Get SMS from device in JAVA

49 views Asked by At

I am encountering issues while trying to fetch SMS information from a ZK-Teco device using Java code. Although I can manually convert the response byte code to a string and display the content correctly, the code implementation is not functioning as expected. I have shared the relevant code and repository link on my GitHub. The specific problem lies in how the code processes the response from the device, and I am seeking guidance on how to troubleshoot and resolve these issues.(content work well)

Additional info(protocol): https://github.com/adrobinoga/zk-protocol/ My repo : https://github.com/virtualheart/ZKteck-biomatric-lib

response byte:208 7 170 57 137 177 3 0 253 1 0 0 0 0 0 160 174 251 45 65 106 84 109 97 97 100 106 106 97 109 51 54 49 54 59 59 46 58 59 33 63 87 106 109 103 116 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
content: AjTmaadjjam3616;;.:;!?Wjmgt
id : 1
Start date : 02-01-24
Start time : 12:56 AM
Expried Time(m) : 0

short meassage data structure
typedef struct _SMS_{
 U8 Tag; // category, public short message, non- public short message .。
 U16 ID; // data content marked 0 to express the record already invalid
 U16 ValidMinutes; // effective minute , 0 = permanent 。
 U16 Reserved;
 U32 StartTime; // Start time
 U8 Content[MAX_SMS_CONTENT_SIZE+1]; // short message content ,MAX_SMS_CONTENT_SIZE=60
}TSms, *PSms;
public SmsInfo getYourSmsList(int smsUId) throws IOException, ParseException {
            int[] smsIdArray = new int[]{smsUId & 0xFF, (smsUId >> 8) & 0xFF};

            int[] toSend = ZKCommand.getPacket(CommandCode.CMD_SMS_RRQ, sessionId, replyNo, smsIdArray);
            byte[] buf = new byte[toSend.length];
            int index = 0;

            for (int byteToSend : toSend) {
                buf[index++] = (byte) (byteToSend & 0xFF);
            }

            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
            socket.send(packet);
            replyNo++;

            int[] response = readResponse();
            CommandReplyCode replyCode = CommandReplyCode.decode(response[0] + (response[1] * 0x100));
            int replyId = response[6] + (response[7] * 0x100);

            int[] payloads = new int[response.length - 8];
            System.arraycopy(response, 8, payloads, 0, payloads.length);
            System.out.println(response.length);
            if (replyCode == CommandReplyCode.CMD_ACK_OK) {
                int tag = response[8] & 0xFF;
                int id = Short.reverseBytes((short) response[9]) & 0xFFFF;
                int validMinutes = Short.reverseBytes((short) response[10]) & 0xFFFF;
                int reserved = Short.reverseBytes((short) response[11]) & 0xFFFF;
                        
                long startTime = Integer.reverseBytes(response[12]) & 0xFFFFFFFFL;

                int contentOffset = 19;  
                byte[] contentBytes = new byte[321];
                for (int i = 0; i < 321; i++) {
                    contentBytes[i] = (byte) (response[i + contentOffset] & 0xFF);
                }

                String content = new String(contentBytes, StandardCharsets.UTF_8).trim();
                SecurityUtils.printUnsignedBytes(contentBytes);
                return new SmsInfo(tag, id, validMinutes, reserved, startTime, content);
           }
           return null;            
        }

However, there seems to be an issue with the code itself as it's not functioning as expected. I expected the code to successfully fetch and process the SMS data from the device. I have attached the relevant code and provided a reference to my GitHub repository for further inspection. Any guidance or assistance in identifying and resolving the issue would be highly appreciated. Thank you.

0

There are 0 answers