1 /* 2 * Copyright (c) 2019-2021 Rockchip Eletronics Co., Ltd. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ___MESSAGE_PARSER_H__ 18 #define ___MESSAGE_PARSER_H__ 19 20 #include <stdlib.h> 21 #include <string.h> 22 23 #include <condition_variable> 24 #include <memory> 25 #include <mutex> 26 #include <thread> 27 #include <vector> 28 #include <atomic> 29 30 #define RKAIQ_SOCKET_DATA_EXTRA_SIZE 28 31 #define RKAIQ_SOCKET_DATA_OFFSET 24 32 #define RKAIQ_SOCKET_OLD_HEADER_LEN 2 33 #define RKAIQ_SOCKET_DATA_HEADER_LEN 4 34 #define RKAIQ_RAW_STREAM_MAX_SIZE (1024 * 512) 35 36 static const uint8_t RKAIQ_SOCKET_OLD_HEADER[2] = {'R', 'K'}; 37 static const uint8_t RKAIQ_SOCKET_DATA_HEADER[4] = {'R', 0xAA, 0xFF, 'K'}; 38 39 typedef enum __aiq_ipc_cmd_id { 40 AIQ_IPC_CMD_UNKNOWN = -1, 41 AIQ_IPC_CMD_WRITE = 0, 42 AIQ_IPC_CMD_READ = 1, 43 } aiq_ipc_cmd_id; 44 45 typedef struct __RkAiqSocketPacket_s { 46 unsigned char magic[2] = {'R', 'K'}; 47 unsigned char packetSize[4]; 48 int commandID; 49 int commandResult; 50 unsigned int dataSize; 51 char *data; 52 unsigned int dataHash; 53 } __attribute__((packed)) RkAiqSocketPacket; 54 55 typedef struct __RkAiqSocketPacket { 56 uint8_t magic[4] = {'R', 0xAA, 0xFF, 'K'}; 57 int32_t cmd_id; 58 int32_t cmd_ret; 59 uint32_t sequence; 60 uint32_t payload_size; 61 uint32_t packet_size; 62 uint8_t *data; 63 uint32_t data_hash; // different offset in data stream 64 } __attribute__((packed)) RkAiqSocketPacket_t; 65 66 typedef enum __MessageType { 67 RKAIQ_MESSAGE_UNKNOWN = -1, 68 RKAIQ_MESSAGE_OLD, 69 RKAIQ_MESSAGE_NEW, 70 } MessageType; 71 72 typedef int (*MessageCallBack)(void *, void *, MessageType type); 73 74 namespace RkMSG { 75 76 class MessageParser { 77 public: 78 explicit MessageParser(void *ptr); 79 ~MessageParser(); 80 81 public: 82 int pushRawData(const uint8_t *data, size_t size); 83 size_t remainData(); 84 int reset(); 85 int start(); 86 int stop(); setMsgCallBack(MessageCallBack cbf)87 int setMsgCallBack(MessageCallBack cbf) { 88 mCallBackFunc = cbf; 89 return 0; 90 }; 91 92 static int freePacket(void *packet, MessageType type); 93 static unsigned int MurMurHash(const void *key, int len); 94 95 private: 96 void *pri; 97 std::vector<uint8_t> raw_stream; 98 std::mutex proc_mutex; 99 std::condition_variable proc_cond; 100 std::shared_ptr<std::thread> proc_thread; 101 std::atomic<bool> is_running; 102 MessageCallBack mCallBackFunc; 103 104 int notify_wakeup(); 105 void process(); 106 107 uint8_t *bit_stream_find(uint8_t *data, int size, const uint8_t *dst, 108 int len); 109 110 void *clonePacket(void *from, MessageType type); 111 RkAiqSocketPacket_t *findValidSection(uint8_t *buffer, int len, 112 size_t *start_of, size_t *end_of); 113 RkAiqSocketPacket *findValidSection2(uint8_t *buffer, int len, 114 size_t *start_of, size_t *end_of); 115 }; 116 117 } // namespace RkMSG 118 119 #endif /*___MESSAGE_PARSER_H__*/ 120