gwenhywfar  5.11.2beta
msg_ipc.c
Go to the documentation of this file.
1 /****************************************************************************
2  * This file is part of the project Gwenhywfar.
3  * Gwenhywfar (c) by 2023 Martin Preuss, all rights reserved.
4  *
5  * The license for this file can be found in the file COPYING which you
6  * should have received along with this file.
7  ****************************************************************************/
8 
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12 
13 /*#define DISABLE_DEBUGLOG*/
14 
15 
16 #include "msgio/msg_p.h"
17 #include "msgio/msg_ipc.h"
18 
19 
20 
21 
22 GWEN_MSG *GWEN_IpcMsg_new(uint8_t protoId, uint8_t protoVer, uint16_t code, uint32_t payloadLen, const uint8_t *payload)
23 {
24  GWEN_MSG *msg;
25  uint32_t len;
26 
27  len=GWEN_MSGIPC_OFFS_PAYLOAD+payloadLen;
28  msg=GWEN_Msg_new(len);
29  if (msg==NULL)
30  return NULL;
31 
32  msg->maxSize=len;
33 
34  msg->buffer[GWEN_MSGIPC_OFFS_SIZE+0]=len & 0xff;
35  msg->buffer[GWEN_MSGIPC_OFFS_SIZE+1]=(len>>8) & 0xff;
36  msg->buffer[GWEN_MSGIPC_OFFS_SIZE+2]=(len>>16) & 0xff;
37  msg->buffer[GWEN_MSGIPC_OFFS_SIZE+3]=(len>>24) & 0xff;
38 
39  msg->buffer[GWEN_MSGIPC_OFFS_PROTOID]=protoId;
40  msg->buffer[GWEN_MSGIPC_OFFS_PROTOVER]=protoVer;
41 
42  msg->buffer[GWEN_MSGIPC_OFFS_CODE+0]=code & 0xff;
43  msg->buffer[GWEN_MSGIPC_OFFS_CODE+1]=(code>>8) & 0xff;
44 
45  if (payloadLen && payload)
46  memmove(msg->buffer+GWEN_MSGIPC_OFFS_PAYLOAD, payload, payloadLen);
47  msg->bytesInBuffer=len;
48  return msg;
49 }
50 
51 
52 
54 {
55  if (msg && msg->bytesInBuffer>4) {
56  uint32_t len;
57 
58  len=msg->buffer[GWEN_MSGIPC_OFFS_SIZE]+
59  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+1])<<8)+
60  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+2])<<16)+
61  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+2])<<24);
62  if (len>msg->maxSize)
63  return -1;
64  else if (msg->bytesInBuffer>=len)
65  return 1;
66  }
67  return 0;
68 }
69 
70 
71 
72 uint32_t GWEN_IpcMsg_GetMsgSize(const GWEN_MSG *msg)
73 {
74  if (msg && msg->bytesInBuffer>4) {
75  return
76  (msg->buffer[GWEN_MSGIPC_OFFS_SIZE])+
77  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+1])<<8)+
78  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+2])<<16)+
79  ((msg->buffer[GWEN_MSGIPC_OFFS_SIZE+2])<<24);
80  }
81  return 0;
82 }
83 
84 
85 
86 uint8_t GWEN_IpcMsg_GetProtoId(const GWEN_MSG *msg)
87 {
88  if (msg && msg->bytesInBuffer>=GWEN_MSGIPC_OFFS_PAYLOAD)
89  return msg->buffer[GWEN_MSGIPC_OFFS_PROTOID];
90  return 0;
91 }
92 
93 
94 
96 {
97  if (msg && msg->bytesInBuffer>=GWEN_MSGIPC_OFFS_PAYLOAD)
98  return msg->buffer[GWEN_MSGIPC_OFFS_PROTOVER];
99  return 0;
100 }
101 
102 
103 
104 uint16_t GWEN_IpcMsg_GetCode(const GWEN_MSG *msg)
105 {
106  if (msg && msg->bytesInBuffer>=GWEN_MSGIPC_OFFS_PAYLOAD)
107  return msg->buffer[GWEN_MSGIPC_OFFS_CODE]+(msg->buffer[GWEN_MSGIPC_OFFS_CODE+1]<<8);
108  return 0;
109 }
110 
111 
112 
113 
114 
#define GWEN_MSGIPC_OFFS_PROTOID
Definition: msg_ipc.h:18
#define GWEN_MSGIPC_OFFS_PROTOVER
Definition: msg_ipc.h:19
uint16_t GWEN_IpcMsg_GetCode(const GWEN_MSG *msg)
Definition: msg_ipc.c:104
#define NULL
Definition: binreloc.c:300
int GWEN_IpcMsg_IsMsgComplete(const GWEN_MSG *msg)
Definition: msg_ipc.c:53
#define GWEN_MSGIPC_OFFS_CODE
Definition: msg_ipc.h:20
GWEN_MSG * GWEN_Msg_new(uint32_t bufferSize)
Definition: msg.c:33
uint8_t GWEN_IpcMsg_GetProtoVersion(const GWEN_MSG *msg)
Definition: msg_ipc.c:95
struct GWEN_MSG GWEN_MSG
Definition: msg.h:25
#define GWEN_MSGIPC_OFFS_PAYLOAD
Definition: msg_ipc.h:21
#define GWEN_MSGIPC_OFFS_SIZE
Definition: msg_ipc.h:17
GWEN_MSG * GWEN_IpcMsg_new(uint8_t protoId, uint8_t protoVer, uint16_t code, uint32_t payloadLen, const uint8_t *payload)
Definition: msg_ipc.c:22
uint8_t GWEN_IpcMsg_GetProtoId(const GWEN_MSG *msg)
Definition: msg_ipc.c:86
uint32_t GWEN_IpcMsg_GetMsgSize(const GWEN_MSG *msg)
Definition: msg_ipc.c:72